blob: bd36a65c624a22208dce42e959934ae368523377 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
Mark Salyzyne9c41962014-01-02 13:52:29 -08002 * Copyright (C) 2007-2014 The Android Open Source Project
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Mark Salyzyn154f4602014-02-20 14:59:07 -080016#include <errno.h>
17#include <fcntl.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080018#ifdef HAVE_PTHREADS
19#include <pthread.h>
20#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080021#include <stdarg.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080022#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070025#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080026#include <sys/types.h>
27#if (FAKE_LOG_DEVICE == 0)
28#include <sys/socket.h>
29#include <sys/un.h>
30#endif
31#include <time.h>
32#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033
Colin Cross9227bd32013-07-23 16:59:20 -070034#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080035#include <log/logger.h>
36#include <log/log_read.h>
37#include <private/android_filesystem_config.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080038
Mark Salyzyncf4aa032013-11-22 07:54:30 -080039#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
41#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080042/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080043#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080044#endif
45
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080046static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080047static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048#ifdef HAVE_PTHREADS
49static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
50#endif
51
Kristian Monsenb5a98902014-01-28 11:26:56 -080052#define UNUSED __attribute__((__unused__))
53
Mark Salyzyn154f4602014-02-20 14:59:07 -080054static int logd_fd = -1;
55#if FAKE_LOG_DEVICE
56#define WEAK __attribute__((weak))
Mark Salyzyn99f47a92014-04-07 14:58:08 -070057static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyn154f4602014-02-20 14:59:07 -080058#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080059
60/*
61 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080062 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080063 * the simulator rather than a desktop tool and want to use the device.
64 */
65static enum {
Chris Pearson19299902010-06-02 16:25:35 -070066 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080067} g_log_status = kLogUninitialized;
68int __android_log_dev_available(void)
69{
70 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -080071 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080072 g_log_status = kLogAvailable;
73 else
74 g_log_status = kLogNotAvailable;
75 }
76
77 return (g_log_status == kLogAvailable);
78}
79
Mark Salyzyn8245af12014-03-25 13:45:33 -070080/* give up, resources too limited */
Kristian Monsen7ddca5a2014-01-28 13:18:24 -080081static int __write_to_log_null(log_id_t log_fd UNUSED, struct iovec *vec UNUSED,
Mark Salyzyn154f4602014-02-20 14:59:07 -080082 size_t nr UNUSED)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083{
84 return -1;
85}
86
Mark Salyzyn8245af12014-03-25 13:45:33 -070087/* log_init_lock assumed */
88static int __write_to_log_initialize()
89{
90 int i, ret = 0;
91
92#if FAKE_LOG_DEVICE
93 for (i = 0; i < LOG_ID_MAX; i++) {
94 char buf[sizeof("/dev/log_system")];
95 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
96 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
97 }
98#else
99 if (logd_fd >= 0) {
100 i = logd_fd;
101 logd_fd = -1;
102 close(i);
103 }
104
105 i = socket(PF_UNIX, SOCK_DGRAM, 0);
106 if (i < 0) {
107 ret = -errno;
108 write_to_log = __write_to_log_null;
109 } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) {
110 ret = -errno;
111 close(i);
112 i = -1;
113 write_to_log = __write_to_log_null;
114 } else {
115 struct sockaddr_un un;
116 memset(&un, 0, sizeof(struct sockaddr_un));
117 un.sun_family = AF_UNIX;
118 strcpy(un.sun_path, "/dev/socket/logdw");
119
120 if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) {
121 ret = -errno;
122 close(i);
123 i = -1;
124 }
125 }
126 logd_fd = i;
127#endif
128
129 return ret;
130}
131
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
133{
134 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700135#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 int log_fd;
137
138 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
139 log_fd = log_fds[(int)log_id];
140 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700141 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800143 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800144 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700145 if (ret < 0) {
146 ret = -errno;
147 }
148 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800149#else
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700150 static const unsigned header_length = 3;
151 struct iovec newVec[nr + header_length];
152 typeof_log_id_t log_id_buf;
153 uint16_t tid;
154 struct timespec ts;
155 log_time realtime_ts;
156 size_t i, payload_size;
157
Mark Salyzyn154f4602014-02-20 14:59:07 -0800158 if (getuid() == AID_LOGD) {
159 /*
160 * ignore log messages we send to ourself.
161 * Such log messages are often generated by libraries we depend on
162 * which use standard Android logging.
163 */
164 return 0;
165 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700166
167 if (logd_fd < 0) {
168 return -EBADF;
169 }
170
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700171 /*
172 * struct {
173 * // what we provide
174 * typeof_log_id_t log_id;
175 * u16 tid;
176 * log_time realtime;
177 * // caller provides
178 * union {
179 * struct {
180 * char prio;
181 * char payload[];
182 * } string;
183 * struct {
184 * uint32_t tag
185 * char payload[];
186 * } binary;
187 * };
188 * };
189 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700190
191 log_id_buf = log_id;
192 tid = gettid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800193
194 newVec[0].iov_base = (unsigned char *) &log_id_buf;
195 newVec[0].iov_len = sizeof_log_id_t;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700196 newVec[1].iov_base = (unsigned char *) &tid;
197 newVec[1].iov_len = sizeof(tid);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800198
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800199 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800200 realtime_ts.tv_sec = ts.tv_sec;
201 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800202
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700203 newVec[2].iov_base = (unsigned char *) &realtime_ts;
204 newVec[2].iov_len = sizeof(log_time);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800205
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700206 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
207 newVec[i].iov_base = vec[i - header_length].iov_base;
208 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
209
210 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
211 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
212 if (newVec[i].iov_len) {
213 ++i;
214 }
215 break;
216 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800217 }
218
Mark Salyzyn8245af12014-03-25 13:45:33 -0700219 /*
220 * The write below could be lost, but will never block.
221 *
222 * ENOTCONN occurs if logd dies.
223 * EAGAIN occurs if logd is overloaded.
224 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700225 ret = writev(logd_fd, newVec, i);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700226 if (ret < 0) {
227 ret = -errno;
228 if (ret == -ENOTCONN) {
229#ifdef HAVE_PTHREADS
230 pthread_mutex_lock(&log_init_lock);
231#endif
232 ret = __write_to_log_initialize();
233#ifdef HAVE_PTHREADS
234 pthread_mutex_unlock(&log_init_lock);
235#endif
236
237 if (ret < 0) {
238 return ret;
239 }
240
241 ret = writev(logd_fd, newVec, nr + header_length);
242 if (ret < 0) {
243 ret = -errno;
244 }
245 }
246 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700247
248 if (ret > (ssize_t)(sizeof_log_id_t + sizeof(tid) + sizeof(log_time))) {
249 ret -= sizeof_log_id_t + sizeof(tid) + sizeof(log_time);
250 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800251#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700252
253 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254}
255
Mark Salyzyn154f4602014-02-20 14:59:07 -0800256#if FAKE_LOG_DEVICE
257static const char *LOG_NAME[LOG_ID_MAX] = {
258 [LOG_ID_MAIN] = "main",
259 [LOG_ID_RADIO] = "radio",
260 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700261 [LOG_ID_SYSTEM] = "system",
262 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800263};
264
265const WEAK char *android_log_id_to_name(log_id_t log_id)
266{
267 if (log_id >= LOG_ID_MAX) {
268 log_id = LOG_ID_MAIN;
269 }
270 return LOG_NAME[log_id];
271}
272#endif
273
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
275{
276#ifdef HAVE_PTHREADS
277 pthread_mutex_lock(&log_init_lock);
278#endif
279
280 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700281 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282
Mark Salyzyn8245af12014-03-25 13:45:33 -0700283 ret = __write_to_log_initialize();
284 if (ret < 0) {
285#ifdef HAVE_PTHREADS
286 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800287#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700288 return ret;
289 }
290
291 write_to_log = __write_to_log_kernel;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800292 }
293
294#ifdef HAVE_PTHREADS
295 pthread_mutex_unlock(&log_init_lock);
296#endif
297
298 return write_to_log(log_id, vec, nr);
299}
300
301int __android_log_write(int prio, const char *tag, const char *msg)
302{
303 struct iovec vec[3];
304 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800305 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306
307 if (!tag)
308 tag = "";
309
310 /* XXX: This needs to go! */
311 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500312 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700313 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314 !strcmp(tag, "AT") ||
315 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700316 !strcmp(tag, "STK") ||
317 !strcmp(tag, "CDMA") ||
318 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800319 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800320 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700321 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800322 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
323 tag = tmp_tag;
324 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325
326 vec[0].iov_base = (unsigned char *) &prio;
327 vec[0].iov_len = 1;
328 vec[1].iov_base = (void *) tag;
329 vec[1].iov_len = strlen(tag) + 1;
330 vec[2].iov_base = (void *) msg;
331 vec[2].iov_len = strlen(msg) + 1;
332
333 return write_to_log(log_id, vec, 3);
334}
335
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800336int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
337{
338 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800339 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800340
341 if (!tag)
342 tag = "";
343
344 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800345 if ((bufID != LOG_ID_RADIO) &&
346 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800347 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700348 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800349 !strcmp(tag, "AT") ||
350 !strcmp(tag, "GSM") ||
351 !strcmp(tag, "STK") ||
352 !strcmp(tag, "CDMA") ||
353 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800354 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800355 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700356 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800357 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
358 tag = tmp_tag;
359 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800360
361 vec[0].iov_base = (unsigned char *) &prio;
362 vec[0].iov_len = 1;
363 vec[1].iov_base = (void *) tag;
364 vec[1].iov_len = strlen(tag) + 1;
365 vec[2].iov_base = (void *) msg;
366 vec[2].iov_len = strlen(msg) + 1;
367
368 return write_to_log(bufID, vec, 3);
369}
370
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
372{
Chris Pearson19299902010-06-02 16:25:35 -0700373 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374
375 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
376
377 return __android_log_write(prio, tag, buf);
378}
379
380int __android_log_print(int prio, const char *tag, const char *fmt, ...)
381{
382 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800383 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384
385 va_start(ap, fmt);
386 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
387 va_end(ap);
388
389 return __android_log_write(prio, tag, buf);
390}
391
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800392int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
393{
394 va_list ap;
395 char buf[LOG_BUF_SIZE];
396
397 va_start(ap, fmt);
398 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
399 va_end(ap);
400
401 return __android_log_buf_write(bufID, prio, tag, buf);
402}
403
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800404void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800405 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800406{
Chris Pearson19299902010-06-02 16:25:35 -0700407 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
Chris Pearson19299902010-06-02 16:25:35 -0700409 if (fmt) {
410 va_list ap;
411 va_start(ap, fmt);
412 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
413 va_end(ap);
414 } else {
415 /* Msg not provided, log condition. N.B. Do not use cond directly as
416 * format string as it could contain spurious '%' syntax (e.g.
417 * "%d" in "blocks%devs == 0").
418 */
419 if (cond)
420 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
421 else
422 strcpy(buf, "Unspecified assertion failed");
423 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800424
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700425#if __BIONIC__
426 // Ensure debuggerd gets to see what went wrong by keeping the C library in the loop.
427 extern __noreturn void __android_fatal(const char* tag, const char* format, ...) __printflike(2, 3);
428 __android_fatal(tag ? tag : "", "%s", buf);
429#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800430 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800431 __builtin_trap(); /* trap so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700432#endif
433 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800434}
435
436int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
437{
438 struct iovec vec[2];
439
440 vec[0].iov_base = &tag;
441 vec[0].iov_len = sizeof(tag);
442 vec[1].iov_base = (void*)payload;
443 vec[1].iov_len = len;
444
445 return write_to_log(LOG_ID_EVENTS, vec, 2);
446}
447
448/*
449 * Like __android_log_bwrite, but takes the type as well. Doesn't work
450 * for the general case where we're generating lists of stuff, but very
451 * handy if we just want to dump an integer into the log.
452 */
453int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800454 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800455{
456 struct iovec vec[3];
457
458 vec[0].iov_base = &tag;
459 vec[0].iov_len = sizeof(tag);
460 vec[1].iov_base = &type;
461 vec[1].iov_len = sizeof(type);
462 vec[2].iov_base = (void*)payload;
463 vec[2].iov_len = len;
464
465 return write_to_log(LOG_ID_EVENTS, vec, 3);
466}