blob: 9c73dade2558f653946e8a0f7fdb29be112947ce [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))
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080057static int log_fds[(int)LOG_ID_MAX] = { -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",
246 [LOG_ID_SYSTEM] = "system"
247};
248
249const WEAK char *android_log_id_to_name(log_id_t log_id)
250{
251 if (log_id >= LOG_ID_MAX) {
252 log_id = LOG_ID_MAIN;
253 }
254 return LOG_NAME[log_id];
255}
256#endif
257
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800258static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
259{
260#ifdef HAVE_PTHREADS
261 pthread_mutex_lock(&log_init_lock);
262#endif
263
264 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700265 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266
Mark Salyzyn8245af12014-03-25 13:45:33 -0700267 ret = __write_to_log_initialize();
268 if (ret < 0) {
269#ifdef HAVE_PTHREADS
270 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800271#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700272 return ret;
273 }
274
275 write_to_log = __write_to_log_kernel;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800276 }
277
278#ifdef HAVE_PTHREADS
279 pthread_mutex_unlock(&log_init_lock);
280#endif
281
282 return write_to_log(log_id, vec, nr);
283}
284
285int __android_log_write(int prio, const char *tag, const char *msg)
286{
287 struct iovec vec[3];
288 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800289 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290
291 if (!tag)
292 tag = "";
293
294 /* XXX: This needs to go! */
295 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500296 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700297 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298 !strcmp(tag, "AT") ||
299 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700300 !strcmp(tag, "STK") ||
301 !strcmp(tag, "CDMA") ||
302 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800303 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700305 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800306 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
307 tag = tmp_tag;
308 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309
310 vec[0].iov_base = (unsigned char *) &prio;
311 vec[0].iov_len = 1;
312 vec[1].iov_base = (void *) tag;
313 vec[1].iov_len = strlen(tag) + 1;
314 vec[2].iov_base = (void *) msg;
315 vec[2].iov_len = strlen(msg) + 1;
316
317 return write_to_log(log_id, vec, 3);
318}
319
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800320int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
321{
322 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800323 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800324
325 if (!tag)
326 tag = "";
327
328 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800329 if ((bufID != LOG_ID_RADIO) &&
330 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800331 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700332 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800333 !strcmp(tag, "AT") ||
334 !strcmp(tag, "GSM") ||
335 !strcmp(tag, "STK") ||
336 !strcmp(tag, "CDMA") ||
337 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800338 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800339 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700340 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800341 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
342 tag = tmp_tag;
343 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800344
345 vec[0].iov_base = (unsigned char *) &prio;
346 vec[0].iov_len = 1;
347 vec[1].iov_base = (void *) tag;
348 vec[1].iov_len = strlen(tag) + 1;
349 vec[2].iov_base = (void *) msg;
350 vec[2].iov_len = strlen(msg) + 1;
351
352 return write_to_log(bufID, vec, 3);
353}
354
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
356{
Chris Pearson19299902010-06-02 16:25:35 -0700357 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358
359 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
360
361 return __android_log_write(prio, tag, buf);
362}
363
364int __android_log_print(int prio, const char *tag, const char *fmt, ...)
365{
366 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800367 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368
369 va_start(ap, fmt);
370 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
371 va_end(ap);
372
373 return __android_log_write(prio, tag, buf);
374}
375
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800376int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
377{
378 va_list ap;
379 char buf[LOG_BUF_SIZE];
380
381 va_start(ap, fmt);
382 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
383 va_end(ap);
384
385 return __android_log_buf_write(bufID, prio, tag, buf);
386}
387
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800389 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800390{
Chris Pearson19299902010-06-02 16:25:35 -0700391 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392
Chris Pearson19299902010-06-02 16:25:35 -0700393 if (fmt) {
394 va_list ap;
395 va_start(ap, fmt);
396 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
397 va_end(ap);
398 } else {
399 /* Msg not provided, log condition. N.B. Do not use cond directly as
400 * format string as it could contain spurious '%' syntax (e.g.
401 * "%d" in "blocks%devs == 0").
402 */
403 if (cond)
404 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
405 else
406 strcpy(buf, "Unspecified assertion failed");
407 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
409 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
410
411 __builtin_trap(); /* trap so we have a chance to debug the situation */
412}
413
414int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
415{
416 struct iovec vec[2];
417
418 vec[0].iov_base = &tag;
419 vec[0].iov_len = sizeof(tag);
420 vec[1].iov_base = (void*)payload;
421 vec[1].iov_len = len;
422
423 return write_to_log(LOG_ID_EVENTS, vec, 2);
424}
425
426/*
427 * Like __android_log_bwrite, but takes the type as well. Doesn't work
428 * for the general case where we're generating lists of stuff, but very
429 * handy if we just want to dump an integer into the log.
430 */
431int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800432 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800433{
434 struct iovec vec[3];
435
436 vec[0].iov_base = &tag;
437 vec[0].iov_len = sizeof(tag);
438 vec[1].iov_base = &type;
439 vec[1].iov_len = sizeof(type);
440 vec[2].iov_base = (void*)payload;
441 vec[2].iov_len = len;
442
443 return write_to_log(LOG_ID_EVENTS, vec, 3);
444}