blob: c3efc33f7070e85e351325a087d2901abf320d79 [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 }
114 struct iovec newVec[nr + 2];
115 typeof_log_id_t log_id_buf = log_id;
116
117 newVec[0].iov_base = (unsigned char *) &log_id_buf;
118 newVec[0].iov_len = sizeof_log_id_t;
119
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800120 struct timespec ts;
121 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800122 log_time realtime_ts;
Mark Salyzyn7e2f83c2014-03-05 07:41:49 -0800123 realtime_ts.tv_sec = ts.tv_sec;
124 realtime_ts.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800125
126 newVec[1].iov_base = (unsigned char *) &realtime_ts;
127 newVec[1].iov_len = sizeof(log_time);
128
129 size_t i;
130 for (i = 2; i < nr + 2; i++) {
131 newVec[i].iov_base = vec[i-2].iov_base;
132 newVec[i].iov_len = vec[i-2].iov_len;
133 }
134
135 /* The write below could be lost, but will never block. */
136 return writev(logd_fd, newVec, nr + 2);
137#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138}
139
Mark Salyzyn154f4602014-02-20 14:59:07 -0800140#if FAKE_LOG_DEVICE
141static const char *LOG_NAME[LOG_ID_MAX] = {
142 [LOG_ID_MAIN] = "main",
143 [LOG_ID_RADIO] = "radio",
144 [LOG_ID_EVENTS] = "events",
145 [LOG_ID_SYSTEM] = "system"
146};
147
148const WEAK char *android_log_id_to_name(log_id_t log_id)
149{
150 if (log_id >= LOG_ID_MAX) {
151 log_id = LOG_ID_MAIN;
152 }
153 return LOG_NAME[log_id];
154}
155#endif
156
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800157static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
158{
159#ifdef HAVE_PTHREADS
160 pthread_mutex_lock(&log_init_lock);
161#endif
162
163 if (write_to_log == __write_to_log_init) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164 write_to_log = __write_to_log_kernel;
165
Mark Salyzyn154f4602014-02-20 14:59:07 -0800166#if FAKE_LOG_DEVICE
167 int i;
168 for (i = 0; i < LOG_ID_MAX; i++) {
169 char buf[sizeof("/dev/log_system")];
170 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
171 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
172 }
173#else
174 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
175 if (sock != -1) {
176 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
177 /* NB: Loss of content */
178 close(sock);
179 sock = -1;
180 } else {
181 struct sockaddr_un un;
182 memset(&un, 0, sizeof(struct sockaddr_un));
183 un.sun_family = AF_UNIX;
184 strcpy(un.sun_path, "/dev/socket/logdw");
185
186 connect(sock, (struct sockaddr *)&un, sizeof(struct sockaddr_un));
187 }
188 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800189 write_to_log = __write_to_log_null;
190 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800191 logd_fd = sock;
192#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800193 }
194
195#ifdef HAVE_PTHREADS
196 pthread_mutex_unlock(&log_init_lock);
197#endif
198
199 return write_to_log(log_id, vec, nr);
200}
201
202int __android_log_write(int prio, const char *tag, const char *msg)
203{
204 struct iovec vec[3];
205 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800206 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800207
208 if (!tag)
209 tag = "";
210
211 /* XXX: This needs to go! */
212 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500213 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700214 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 !strcmp(tag, "AT") ||
216 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700217 !strcmp(tag, "STK") ||
218 !strcmp(tag, "CDMA") ||
219 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800220 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800221 log_id = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800222 // Inform third party apps/ril/radio.. to use Rlog or RLOG
223 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
224 tag = tmp_tag;
225 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800226
227 vec[0].iov_base = (unsigned char *) &prio;
228 vec[0].iov_len = 1;
229 vec[1].iov_base = (void *) tag;
230 vec[1].iov_len = strlen(tag) + 1;
231 vec[2].iov_base = (void *) msg;
232 vec[2].iov_len = strlen(msg) + 1;
233
234 return write_to_log(log_id, vec, 3);
235}
236
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800237int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
238{
239 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800240 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800241
242 if (!tag)
243 tag = "";
244
245 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800246 if ((bufID != LOG_ID_RADIO) &&
247 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800248 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700249 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800250 !strcmp(tag, "AT") ||
251 !strcmp(tag, "GSM") ||
252 !strcmp(tag, "STK") ||
253 !strcmp(tag, "CDMA") ||
254 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800255 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800256 bufID = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800257 // Inform third party apps/ril/radio.. to use Rlog or RLOG
258 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
259 tag = tmp_tag;
260 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800261
262 vec[0].iov_base = (unsigned char *) &prio;
263 vec[0].iov_len = 1;
264 vec[1].iov_base = (void *) tag;
265 vec[1].iov_len = strlen(tag) + 1;
266 vec[2].iov_base = (void *) msg;
267 vec[2].iov_len = strlen(msg) + 1;
268
269 return write_to_log(bufID, vec, 3);
270}
271
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
273{
Chris Pearson19299902010-06-02 16:25:35 -0700274 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800275
276 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
277
278 return __android_log_write(prio, tag, buf);
279}
280
281int __android_log_print(int prio, const char *tag, const char *fmt, ...)
282{
283 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800284 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800285
286 va_start(ap, fmt);
287 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
288 va_end(ap);
289
290 return __android_log_write(prio, tag, buf);
291}
292
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800293int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
294{
295 va_list ap;
296 char buf[LOG_BUF_SIZE];
297
298 va_start(ap, fmt);
299 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
300 va_end(ap);
301
302 return __android_log_buf_write(bufID, prio, tag, buf);
303}
304
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800305void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800306 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800307{
Chris Pearson19299902010-06-02 16:25:35 -0700308 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800309
Chris Pearson19299902010-06-02 16:25:35 -0700310 if (fmt) {
311 va_list ap;
312 va_start(ap, fmt);
313 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
314 va_end(ap);
315 } else {
316 /* Msg not provided, log condition. N.B. Do not use cond directly as
317 * format string as it could contain spurious '%' syntax (e.g.
318 * "%d" in "blocks%devs == 0").
319 */
320 if (cond)
321 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
322 else
323 strcpy(buf, "Unspecified assertion failed");
324 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800325
326 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
327
328 __builtin_trap(); /* trap so we have a chance to debug the situation */
329}
330
331int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
332{
333 struct iovec vec[2];
334
335 vec[0].iov_base = &tag;
336 vec[0].iov_len = sizeof(tag);
337 vec[1].iov_base = (void*)payload;
338 vec[1].iov_len = len;
339
340 return write_to_log(LOG_ID_EVENTS, vec, 2);
341}
342
343/*
344 * Like __android_log_bwrite, but takes the type as well. Doesn't work
345 * for the general case where we're generating lists of stuff, but very
346 * handy if we just want to dump an integer into the log.
347 */
348int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800349 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350{
351 struct iovec vec[3];
352
353 vec[0].iov_base = &tag;
354 vec[0].iov_len = sizeof(tag);
355 vec[1].iov_base = &type;
356 vec[1].iov_len = sizeof(type);
357 vec[2].iov_base = (void*)payload;
358 vec[2].iov_len = len;
359
360 return write_to_log(LOG_ID_EVENTS, vec, 3);
361}