blob: 121d84dff08fc6b6b864efc4c79b6a98162402ef [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
Mark Salyzyna04464a2014-04-30 08:50:53 -070052#ifndef __unused
53#define __unused __attribute__((__unused__))
54#endif
Kristian Monsenb5a98902014-01-28 11:26:56 -080055
Mark Salyzyn154f4602014-02-20 14:59:07 -080056#if FAKE_LOG_DEVICE
57#define WEAK __attribute__((weak))
Mark Salyzyn99f47a92014-04-07 14:58:08 -070058static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070059#else
60static int logd_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080061#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062
63/*
64 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080065 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080066 * the simulator rather than a desktop tool and want to use the device.
67 */
68static enum {
Chris Pearson19299902010-06-02 16:25:35 -070069 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070} g_log_status = kLogUninitialized;
71int __android_log_dev_available(void)
72{
73 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -080074 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075 g_log_status = kLogAvailable;
76 else
77 g_log_status = kLogNotAvailable;
78 }
79
80 return (g_log_status == kLogAvailable);
81}
82
Mark Salyzyna04464a2014-04-30 08:50:53 -070083#if !FAKE_LOG_DEVICE
Mark Salyzyn8245af12014-03-25 13:45:33 -070084/* give up, resources too limited */
Mark Salyzyna04464a2014-04-30 08:50:53 -070085static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
86 size_t nr __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087{
88 return -1;
89}
Mark Salyzyna04464a2014-04-30 08:50:53 -070090#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080091
Mark Salyzyn8245af12014-03-25 13:45:33 -070092/* log_init_lock assumed */
93static int __write_to_log_initialize()
94{
95 int i, ret = 0;
96
97#if FAKE_LOG_DEVICE
98 for (i = 0; i < LOG_ID_MAX; i++) {
99 char buf[sizeof("/dev/log_system")];
100 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
101 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
102 }
103#else
104 if (logd_fd >= 0) {
105 i = logd_fd;
106 logd_fd = -1;
107 close(i);
108 }
109
110 i = socket(PF_UNIX, SOCK_DGRAM, 0);
111 if (i < 0) {
112 ret = -errno;
113 write_to_log = __write_to_log_null;
114 } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) {
115 ret = -errno;
116 close(i);
117 i = -1;
118 write_to_log = __write_to_log_null;
119 } else {
120 struct sockaddr_un un;
121 memset(&un, 0, sizeof(struct sockaddr_un));
122 un.sun_family = AF_UNIX;
123 strcpy(un.sun_path, "/dev/socket/logdw");
124
125 if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) {
126 ret = -errno;
127 close(i);
128 i = -1;
129 }
130 }
131 logd_fd = i;
132#endif
133
134 return ret;
135}
136
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800137static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
138{
139 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700140#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141 int log_fd;
142
143 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
144 log_fd = log_fds[(int)log_id];
145 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700146 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800147 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800148 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800149 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700150 if (ret < 0) {
151 ret = -errno;
152 }
153 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800154#else
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700155 static const unsigned header_length = 3;
156 struct iovec newVec[nr + header_length];
157 typeof_log_id_t log_id_buf;
158 uint16_t tid;
159 struct timespec ts;
160 log_time realtime_ts;
161 size_t i, payload_size;
162
Mark Salyzyn154f4602014-02-20 14:59:07 -0800163 if (getuid() == AID_LOGD) {
164 /*
165 * ignore log messages we send to ourself.
166 * Such log messages are often generated by libraries we depend on
167 * which use standard Android logging.
168 */
169 return 0;
170 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700171
172 if (logd_fd < 0) {
173 return -EBADF;
174 }
175
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700176 /*
177 * struct {
178 * // what we provide
179 * typeof_log_id_t log_id;
180 * u16 tid;
181 * log_time realtime;
182 * // caller provides
183 * union {
184 * struct {
185 * char prio;
186 * char payload[];
187 * } string;
188 * struct {
189 * uint32_t tag
190 * char payload[];
191 * } binary;
192 * };
193 * };
194 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700195
196 log_id_buf = log_id;
197 tid = gettid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800198
199 newVec[0].iov_base = (unsigned char *) &log_id_buf;
200 newVec[0].iov_len = sizeof_log_id_t;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700201 newVec[1].iov_base = (unsigned char *) &tid;
202 newVec[1].iov_len = sizeof(tid);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800203
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800204 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800205 realtime_ts.tv_sec = ts.tv_sec;
206 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800207
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700208 newVec[2].iov_base = (unsigned char *) &realtime_ts;
209 newVec[2].iov_len = sizeof(log_time);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800210
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700211 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
212 newVec[i].iov_base = vec[i - header_length].iov_base;
213 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
214
215 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
216 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
217 if (newVec[i].iov_len) {
218 ++i;
219 }
220 break;
221 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800222 }
223
Mark Salyzyn8245af12014-03-25 13:45:33 -0700224 /*
225 * The write below could be lost, but will never block.
226 *
227 * ENOTCONN occurs if logd dies.
228 * EAGAIN occurs if logd is overloaded.
229 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700230 ret = writev(logd_fd, newVec, i);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700231 if (ret < 0) {
232 ret = -errno;
233 if (ret == -ENOTCONN) {
234#ifdef HAVE_PTHREADS
235 pthread_mutex_lock(&log_init_lock);
236#endif
237 ret = __write_to_log_initialize();
238#ifdef HAVE_PTHREADS
239 pthread_mutex_unlock(&log_init_lock);
240#endif
241
242 if (ret < 0) {
243 return ret;
244 }
245
246 ret = writev(logd_fd, newVec, nr + header_length);
247 if (ret < 0) {
248 ret = -errno;
249 }
250 }
251 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700252
253 if (ret > (ssize_t)(sizeof_log_id_t + sizeof(tid) + sizeof(log_time))) {
254 ret -= sizeof_log_id_t + sizeof(tid) + sizeof(log_time);
255 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800256#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700257
258 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259}
260
Mark Salyzyn154f4602014-02-20 14:59:07 -0800261#if FAKE_LOG_DEVICE
262static const char *LOG_NAME[LOG_ID_MAX] = {
263 [LOG_ID_MAIN] = "main",
264 [LOG_ID_RADIO] = "radio",
265 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700266 [LOG_ID_SYSTEM] = "system",
267 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800268};
269
270const WEAK char *android_log_id_to_name(log_id_t log_id)
271{
272 if (log_id >= LOG_ID_MAX) {
273 log_id = LOG_ID_MAIN;
274 }
275 return LOG_NAME[log_id];
276}
277#endif
278
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
280{
281#ifdef HAVE_PTHREADS
282 pthread_mutex_lock(&log_init_lock);
283#endif
284
285 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700286 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800287
Mark Salyzyn8245af12014-03-25 13:45:33 -0700288 ret = __write_to_log_initialize();
289 if (ret < 0) {
290#ifdef HAVE_PTHREADS
291 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800292#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700293 return ret;
294 }
295
296 write_to_log = __write_to_log_kernel;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800297 }
298
299#ifdef HAVE_PTHREADS
300 pthread_mutex_unlock(&log_init_lock);
301#endif
302
303 return write_to_log(log_id, vec, nr);
304}
305
306int __android_log_write(int prio, const char *tag, const char *msg)
307{
308 struct iovec vec[3];
309 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800310 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800311
312 if (!tag)
313 tag = "";
314
315 /* XXX: This needs to go! */
316 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500317 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700318 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800319 !strcmp(tag, "AT") ||
320 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700321 !strcmp(tag, "STK") ||
322 !strcmp(tag, "CDMA") ||
323 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800324 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700326 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800327 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
328 tag = tmp_tag;
329 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330
Elliott Hughes26864bf2014-05-06 20:40:15 -0700331#if __BIONIC__
332 if (prio == ANDROID_LOG_FATAL) {
333 extern void __android_set_abort_message(const char*);
334 __android_set_abort_message(msg);
335 }
336#endif
337
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 vec[0].iov_base = (unsigned char *) &prio;
339 vec[0].iov_len = 1;
340 vec[1].iov_base = (void *) tag;
341 vec[1].iov_len = strlen(tag) + 1;
342 vec[2].iov_base = (void *) msg;
343 vec[2].iov_len = strlen(msg) + 1;
344
345 return write_to_log(log_id, vec, 3);
346}
347
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800348int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
349{
350 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800351 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800352
353 if (!tag)
354 tag = "";
355
356 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800357 if ((bufID != LOG_ID_RADIO) &&
358 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800359 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700360 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800361 !strcmp(tag, "AT") ||
362 !strcmp(tag, "GSM") ||
363 !strcmp(tag, "STK") ||
364 !strcmp(tag, "CDMA") ||
365 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800366 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800367 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700368 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800369 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
370 tag = tmp_tag;
371 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800372
373 vec[0].iov_base = (unsigned char *) &prio;
374 vec[0].iov_len = 1;
375 vec[1].iov_base = (void *) tag;
376 vec[1].iov_len = strlen(tag) + 1;
377 vec[2].iov_base = (void *) msg;
378 vec[2].iov_len = strlen(msg) + 1;
379
380 return write_to_log(bufID, vec, 3);
381}
382
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800383int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
384{
Chris Pearson19299902010-06-02 16:25:35 -0700385 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386
387 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
388
389 return __android_log_write(prio, tag, buf);
390}
391
392int __android_log_print(int prio, const char *tag, const char *fmt, ...)
393{
394 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800395 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396
397 va_start(ap, fmt);
398 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
399 va_end(ap);
400
401 return __android_log_write(prio, tag, buf);
402}
403
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800404int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
405{
406 va_list ap;
407 char buf[LOG_BUF_SIZE];
408
409 va_start(ap, fmt);
410 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
411 va_end(ap);
412
413 return __android_log_buf_write(bufID, prio, tag, buf);
414}
415
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800417 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800418{
Chris Pearson19299902010-06-02 16:25:35 -0700419 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800420
Chris Pearson19299902010-06-02 16:25:35 -0700421 if (fmt) {
422 va_list ap;
423 va_start(ap, fmt);
424 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
425 va_end(ap);
426 } else {
427 /* Msg not provided, log condition. N.B. Do not use cond directly as
428 * format string as it could contain spurious '%' syntax (e.g.
429 * "%d" in "blocks%devs == 0").
430 */
431 if (cond)
432 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
433 else
434 strcpy(buf, "Unspecified assertion failed");
435 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800436
437 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800438 __builtin_trap(); /* trap so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700439 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440}
441
442int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
443{
444 struct iovec vec[2];
445
446 vec[0].iov_base = &tag;
447 vec[0].iov_len = sizeof(tag);
448 vec[1].iov_base = (void*)payload;
449 vec[1].iov_len = len;
450
451 return write_to_log(LOG_ID_EVENTS, vec, 2);
452}
453
454/*
455 * Like __android_log_bwrite, but takes the type as well. Doesn't work
456 * for the general case where we're generating lists of stuff, but very
457 * handy if we just want to dump an integer into the log.
458 */
459int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800460 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461{
462 struct iovec vec[3];
463
464 vec[0].iov_base = &tag;
465 vec[0].iov_len = sizeof(tag);
466 vec[1].iov_base = &type;
467 vec[1].iov_len = sizeof(type);
468 vec[2].iov_base = (void*)payload;
469 vec[2].iov_len = len;
470
471 return write_to_log(LOG_ID_EVENTS, vec, 3);
472}