blob: 48531d5714771143523d42dc292bff75b702fa93 [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);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149
150 return ret;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800151#else
Mark Salyzyn154f4602014-02-20 14:59:07 -0800152 if (getuid() == AID_LOGD) {
153 /*
154 * ignore log messages we send to ourself.
155 * Such log messages are often generated by libraries we depend on
156 * which use standard Android logging.
157 */
158 return 0;
159 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700160
161 if (logd_fd < 0) {
162 return -EBADF;
163 }
164
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700165 /*
166 * struct {
167 * // what we provide
168 * typeof_log_id_t log_id;
169 * u16 tid;
170 * log_time realtime;
171 * // caller provides
172 * union {
173 * struct {
174 * char prio;
175 * char payload[];
176 * } string;
177 * struct {
178 * uint32_t tag
179 * char payload[];
180 * } binary;
181 * };
182 * };
183 */
184 static const unsigned header_length = 3;
185 struct iovec newVec[nr + header_length];
Mark Salyzyn154f4602014-02-20 14:59:07 -0800186 typeof_log_id_t log_id_buf = log_id;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700187 uint16_t tid = gettid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800188
189 newVec[0].iov_base = (unsigned char *) &log_id_buf;
190 newVec[0].iov_len = sizeof_log_id_t;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700191 newVec[1].iov_base = (unsigned char *) &tid;
192 newVec[1].iov_len = sizeof(tid);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800193
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800194 struct timespec ts;
195 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800196 log_time realtime_ts;
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800197 realtime_ts.tv_sec = ts.tv_sec;
198 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800199
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700200 newVec[2].iov_base = (unsigned char *) &realtime_ts;
201 newVec[2].iov_len = sizeof(log_time);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800202
203 size_t i;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700204 for (i = header_length; i < nr + header_length; i++) {
205 newVec[i].iov_base = vec[i-header_length].iov_base;
206 newVec[i].iov_len = vec[i-header_length].iov_len;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800207 }
208
Mark Salyzyn8245af12014-03-25 13:45:33 -0700209 /*
210 * The write below could be lost, but will never block.
211 *
212 * ENOTCONN occurs if logd dies.
213 * EAGAIN occurs if logd is overloaded.
214 */
215 ret = writev(logd_fd, newVec, nr + header_length);
216 if (ret < 0) {
217 ret = -errno;
218 if (ret == -ENOTCONN) {
219#ifdef HAVE_PTHREADS
220 pthread_mutex_lock(&log_init_lock);
221#endif
222 ret = __write_to_log_initialize();
223#ifdef HAVE_PTHREADS
224 pthread_mutex_unlock(&log_init_lock);
225#endif
226
227 if (ret < 0) {
228 return ret;
229 }
230
231 ret = writev(logd_fd, newVec, nr + header_length);
232 if (ret < 0) {
233 ret = -errno;
234 }
235 }
236 }
237 return ret;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800238#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800239}
240
Mark Salyzyn154f4602014-02-20 14:59:07 -0800241#if FAKE_LOG_DEVICE
242static const char *LOG_NAME[LOG_ID_MAX] = {
243 [LOG_ID_MAIN] = "main",
244 [LOG_ID_RADIO] = "radio",
245 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700246 [LOG_ID_SYSTEM] = "system",
247 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800248};
249
250const WEAK char *android_log_id_to_name(log_id_t log_id)
251{
252 if (log_id >= LOG_ID_MAX) {
253 log_id = LOG_ID_MAIN;
254 }
255 return LOG_NAME[log_id];
256}
257#endif
258
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800259static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
260{
261#ifdef HAVE_PTHREADS
262 pthread_mutex_lock(&log_init_lock);
263#endif
264
265 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700266 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800267
Mark Salyzyn8245af12014-03-25 13:45:33 -0700268 ret = __write_to_log_initialize();
269 if (ret < 0) {
270#ifdef HAVE_PTHREADS
271 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800272#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700273 return ret;
274 }
275
276 write_to_log = __write_to_log_kernel;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 }
278
279#ifdef HAVE_PTHREADS
280 pthread_mutex_unlock(&log_init_lock);
281#endif
282
283 return write_to_log(log_id, vec, nr);
284}
285
286int __android_log_write(int prio, const char *tag, const char *msg)
287{
288 struct iovec vec[3];
289 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800290 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291
292 if (!tag)
293 tag = "";
294
295 /* XXX: This needs to go! */
296 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500297 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700298 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800299 !strcmp(tag, "AT") ||
300 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700301 !strcmp(tag, "STK") ||
302 !strcmp(tag, "CDMA") ||
303 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800304 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700306 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800307 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
308 tag = tmp_tag;
309 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310
311 vec[0].iov_base = (unsigned char *) &prio;
312 vec[0].iov_len = 1;
313 vec[1].iov_base = (void *) tag;
314 vec[1].iov_len = strlen(tag) + 1;
315 vec[2].iov_base = (void *) msg;
316 vec[2].iov_len = strlen(msg) + 1;
317
318 return write_to_log(log_id, vec, 3);
319}
320
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800321int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
322{
323 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800324 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800325
326 if (!tag)
327 tag = "";
328
329 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800330 if ((bufID != LOG_ID_RADIO) &&
331 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800332 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700333 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800334 !strcmp(tag, "AT") ||
335 !strcmp(tag, "GSM") ||
336 !strcmp(tag, "STK") ||
337 !strcmp(tag, "CDMA") ||
338 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800339 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800340 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700341 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800342 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
343 tag = tmp_tag;
344 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800345
346 vec[0].iov_base = (unsigned char *) &prio;
347 vec[0].iov_len = 1;
348 vec[1].iov_base = (void *) tag;
349 vec[1].iov_len = strlen(tag) + 1;
350 vec[2].iov_base = (void *) msg;
351 vec[2].iov_len = strlen(msg) + 1;
352
353 return write_to_log(bufID, vec, 3);
354}
355
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800356int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
357{
Chris Pearson19299902010-06-02 16:25:35 -0700358 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800359
360 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
361
362 return __android_log_write(prio, tag, buf);
363}
364
365int __android_log_print(int prio, const char *tag, const char *fmt, ...)
366{
367 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800368 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369
370 va_start(ap, fmt);
371 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
372 va_end(ap);
373
374 return __android_log_write(prio, tag, buf);
375}
376
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800377int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
378{
379 va_list ap;
380 char buf[LOG_BUF_SIZE];
381
382 va_start(ap, fmt);
383 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
384 va_end(ap);
385
386 return __android_log_buf_write(bufID, prio, tag, buf);
387}
388
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800390 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800391{
Chris Pearson19299902010-06-02 16:25:35 -0700392 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800393
Chris Pearson19299902010-06-02 16:25:35 -0700394 if (fmt) {
395 va_list ap;
396 va_start(ap, fmt);
397 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
398 va_end(ap);
399 } else {
400 /* Msg not provided, log condition. N.B. Do not use cond directly as
401 * format string as it could contain spurious '%' syntax (e.g.
402 * "%d" in "blocks%devs == 0").
403 */
404 if (cond)
405 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
406 else
407 strcpy(buf, "Unspecified assertion failed");
408 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800409
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700410#if __BIONIC__
411 // Ensure debuggerd gets to see what went wrong by keeping the C library in the loop.
412 extern __noreturn void __android_fatal(const char* tag, const char* format, ...) __printflike(2, 3);
413 __android_fatal(tag ? tag : "", "%s", buf);
414#else
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800416 __builtin_trap(); /* trap so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700417#endif
418 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800419}
420
421int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
422{
423 struct iovec vec[2];
424
425 vec[0].iov_base = &tag;
426 vec[0].iov_len = sizeof(tag);
427 vec[1].iov_base = (void*)payload;
428 vec[1].iov_len = len;
429
430 return write_to_log(LOG_ID_EVENTS, vec, 2);
431}
432
433/*
434 * Like __android_log_bwrite, but takes the type as well. Doesn't work
435 * for the general case where we're generating lists of stuff, but very
436 * handy if we just want to dump an integer into the log.
437 */
438int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800439 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440{
441 struct iovec vec[3];
442
443 vec[0].iov_base = &tag;
444 vec[0].iov_len = sizeof(tag);
445 vec[1].iov_base = &type;
446 vec[1].iov_len = sizeof(type);
447 vec[2].iov_base = (void*)payload;
448 vec[2].iov_len = len;
449
450 return write_to_log(LOG_ID_EVENTS, vec, 3);
451}