blob: aa0ad39c265e2c40f630556a40c283e5042cfb0a [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
Kristian Monsen7ddca5a2014-01-28 13:18:24 -080080static int __write_to_log_null(log_id_t log_fd UNUSED, struct iovec *vec UNUSED,
Mark Salyzyn154f4602014-02-20 14:59:07 -080081 size_t nr UNUSED)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080082{
83 return -1;
84}
85
86static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr)
87{
Mark Salyzyn154f4602014-02-20 14:59:07 -080088#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080089 ssize_t ret;
90 int log_fd;
91
92 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
93 log_fd = log_fds[(int)log_id];
94 } else {
95 return EBADF;
96 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080097 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -080098 ret = fakeLogWritev(log_fd, vec, nr);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099 } while (ret < 0 && errno == EINTR);
100
101 return ret;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800102#else
103 if (logd_fd == -1) {
104 return -1;
105 }
106 if (getuid() == AID_LOGD) {
107 /*
108 * ignore log messages we send to ourself.
109 * Such log messages are often generated by libraries we depend on
110 * which use standard Android logging.
111 */
112 return 0;
113 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700114 /*
115 * struct {
116 * // what we provide
117 * typeof_log_id_t log_id;
118 * u16 tid;
119 * log_time realtime;
120 * // caller provides
121 * union {
122 * struct {
123 * char prio;
124 * char payload[];
125 * } string;
126 * struct {
127 * uint32_t tag
128 * char payload[];
129 * } binary;
130 * };
131 * };
132 */
133 static const unsigned header_length = 3;
134 struct iovec newVec[nr + header_length];
Mark Salyzyn154f4602014-02-20 14:59:07 -0800135 typeof_log_id_t log_id_buf = log_id;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700136 uint16_t tid = gettid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800137
138 newVec[0].iov_base = (unsigned char *) &log_id_buf;
139 newVec[0].iov_len = sizeof_log_id_t;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700140 newVec[1].iov_base = (unsigned char *) &tid;
141 newVec[1].iov_len = sizeof(tid);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800142
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800143 struct timespec ts;
144 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800145 log_time realtime_ts;
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800146 realtime_ts.tv_sec = ts.tv_sec;
147 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800148
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700149 newVec[2].iov_base = (unsigned char *) &realtime_ts;
150 newVec[2].iov_len = sizeof(log_time);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800151
152 size_t i;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700153 for (i = header_length; i < nr + header_length; i++) {
154 newVec[i].iov_base = vec[i-header_length].iov_base;
155 newVec[i].iov_len = vec[i-header_length].iov_len;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800156 }
157
158 /* The write below could be lost, but will never block. */
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700159 return writev(logd_fd, newVec, nr + header_length);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800160#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161}
162
Mark Salyzyn154f4602014-02-20 14:59:07 -0800163#if FAKE_LOG_DEVICE
164static const char *LOG_NAME[LOG_ID_MAX] = {
165 [LOG_ID_MAIN] = "main",
166 [LOG_ID_RADIO] = "radio",
167 [LOG_ID_EVENTS] = "events",
168 [LOG_ID_SYSTEM] = "system"
169};
170
171const WEAK char *android_log_id_to_name(log_id_t log_id)
172{
173 if (log_id >= LOG_ID_MAX) {
174 log_id = LOG_ID_MAIN;
175 }
176 return LOG_NAME[log_id];
177}
178#endif
179
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800180static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
181{
182#ifdef HAVE_PTHREADS
183 pthread_mutex_lock(&log_init_lock);
184#endif
185
186 if (write_to_log == __write_to_log_init) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 write_to_log = __write_to_log_kernel;
188
Mark Salyzyn154f4602014-02-20 14:59:07 -0800189#if FAKE_LOG_DEVICE
190 int i;
191 for (i = 0; i < LOG_ID_MAX; i++) {
192 char buf[sizeof("/dev/log_system")];
193 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
194 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
195 }
196#else
197 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
198 if (sock != -1) {
199 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
200 /* NB: Loss of content */
201 close(sock);
202 sock = -1;
203 } else {
204 struct sockaddr_un un;
205 memset(&un, 0, sizeof(struct sockaddr_un));
206 un.sun_family = AF_UNIX;
207 strcpy(un.sun_path, "/dev/socket/logdw");
208
209 connect(sock, (struct sockaddr *)&un, sizeof(struct sockaddr_un));
210 }
211 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 write_to_log = __write_to_log_null;
213 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800214 logd_fd = sock;
215#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800216 }
217
218#ifdef HAVE_PTHREADS
219 pthread_mutex_unlock(&log_init_lock);
220#endif
221
222 return write_to_log(log_id, vec, nr);
223}
224
225int __android_log_write(int prio, const char *tag, const char *msg)
226{
227 struct iovec vec[3];
228 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800229 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800230
231 if (!tag)
232 tag = "";
233
234 /* XXX: This needs to go! */
235 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500236 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700237 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 !strcmp(tag, "AT") ||
239 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700240 !strcmp(tag, "STK") ||
241 !strcmp(tag, "CDMA") ||
242 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800243 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 log_id = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800245 // Inform third party apps/ril/radio.. to use Rlog or RLOG
246 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
247 tag = tmp_tag;
248 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800249
250 vec[0].iov_base = (unsigned char *) &prio;
251 vec[0].iov_len = 1;
252 vec[1].iov_base = (void *) tag;
253 vec[1].iov_len = strlen(tag) + 1;
254 vec[2].iov_base = (void *) msg;
255 vec[2].iov_len = strlen(msg) + 1;
256
257 return write_to_log(log_id, vec, 3);
258}
259
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800260int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
261{
262 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800263 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800264
265 if (!tag)
266 tag = "";
267
268 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800269 if ((bufID != LOG_ID_RADIO) &&
270 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800271 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700272 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800273 !strcmp(tag, "AT") ||
274 !strcmp(tag, "GSM") ||
275 !strcmp(tag, "STK") ||
276 !strcmp(tag, "CDMA") ||
277 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800278 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800279 bufID = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800280 // Inform third party apps/ril/radio.. to use Rlog or RLOG
281 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
282 tag = tmp_tag;
283 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800284
285 vec[0].iov_base = (unsigned char *) &prio;
286 vec[0].iov_len = 1;
287 vec[1].iov_base = (void *) tag;
288 vec[1].iov_len = strlen(tag) + 1;
289 vec[2].iov_base = (void *) msg;
290 vec[2].iov_len = strlen(msg) + 1;
291
292 return write_to_log(bufID, vec, 3);
293}
294
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800295int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
296{
Chris Pearson19299902010-06-02 16:25:35 -0700297 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800298
299 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
300
301 return __android_log_write(prio, tag, buf);
302}
303
304int __android_log_print(int prio, const char *tag, const char *fmt, ...)
305{
306 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800307 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800308
309 va_start(ap, fmt);
310 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
311 va_end(ap);
312
313 return __android_log_write(prio, tag, buf);
314}
315
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800316int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
317{
318 va_list ap;
319 char buf[LOG_BUF_SIZE];
320
321 va_start(ap, fmt);
322 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
323 va_end(ap);
324
325 return __android_log_buf_write(bufID, prio, tag, buf);
326}
327
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800329 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800330{
Chris Pearson19299902010-06-02 16:25:35 -0700331 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800332
Chris Pearson19299902010-06-02 16:25:35 -0700333 if (fmt) {
334 va_list ap;
335 va_start(ap, fmt);
336 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
337 va_end(ap);
338 } else {
339 /* Msg not provided, log condition. N.B. Do not use cond directly as
340 * format string as it could contain spurious '%' syntax (e.g.
341 * "%d" in "blocks%devs == 0").
342 */
343 if (cond)
344 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
345 else
346 strcpy(buf, "Unspecified assertion failed");
347 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800348
349 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
350
351 __builtin_trap(); /* trap so we have a chance to debug the situation */
352}
353
354int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
355{
356 struct iovec vec[2];
357
358 vec[0].iov_base = &tag;
359 vec[0].iov_len = sizeof(tag);
360 vec[1].iov_base = (void*)payload;
361 vec[1].iov_len = len;
362
363 return write_to_log(LOG_ID_EVENTS, vec, 2);
364}
365
366/*
367 * Like __android_log_bwrite, but takes the type as well. Doesn't work
368 * for the general case where we're generating lists of stuff, but very
369 * handy if we just want to dump an integer into the log.
370 */
371int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800372 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373{
374 struct iovec vec[3];
375
376 vec[0].iov_base = &tag;
377 vec[0].iov_len = sizeof(tag);
378 vec[1].iov_base = &type;
379 vec[1].iov_len = sizeof(type);
380 vec[2].iov_base = (void*)payload;
381 vec[2].iov_len = len;
382
383 return write_to_log(LOG_ID_EVENTS, vec, 3);
384}