blob: d3ee167bd8a4afe8491b857d7d2c1156073ee489 [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
120 log_time realtime_ts;
121 clock_gettime(CLOCK_REALTIME, &realtime_ts);
122
123 newVec[1].iov_base = (unsigned char *) &realtime_ts;
124 newVec[1].iov_len = sizeof(log_time);
125
126 size_t i;
127 for (i = 2; i < nr + 2; i++) {
128 newVec[i].iov_base = vec[i-2].iov_base;
129 newVec[i].iov_len = vec[i-2].iov_len;
130 }
131
132 /* The write below could be lost, but will never block. */
133 return writev(logd_fd, newVec, nr + 2);
134#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135}
136
Mark Salyzyn154f4602014-02-20 14:59:07 -0800137#if FAKE_LOG_DEVICE
138static const char *LOG_NAME[LOG_ID_MAX] = {
139 [LOG_ID_MAIN] = "main",
140 [LOG_ID_RADIO] = "radio",
141 [LOG_ID_EVENTS] = "events",
142 [LOG_ID_SYSTEM] = "system"
143};
144
145const WEAK char *android_log_id_to_name(log_id_t log_id)
146{
147 if (log_id >= LOG_ID_MAX) {
148 log_id = LOG_ID_MAIN;
149 }
150 return LOG_NAME[log_id];
151}
152#endif
153
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800154static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
155{
156#ifdef HAVE_PTHREADS
157 pthread_mutex_lock(&log_init_lock);
158#endif
159
160 if (write_to_log == __write_to_log_init) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 write_to_log = __write_to_log_kernel;
162
Mark Salyzyn154f4602014-02-20 14:59:07 -0800163#if FAKE_LOG_DEVICE
164 int i;
165 for (i = 0; i < LOG_ID_MAX; i++) {
166 char buf[sizeof("/dev/log_system")];
167 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
168 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
169 }
170#else
171 int sock = socket(PF_UNIX, SOCK_DGRAM, 0);
172 if (sock != -1) {
173 if (fcntl(sock, F_SETFL, O_NONBLOCK) == -1) {
174 /* NB: Loss of content */
175 close(sock);
176 sock = -1;
177 } else {
178 struct sockaddr_un un;
179 memset(&un, 0, sizeof(struct sockaddr_un));
180 un.sun_family = AF_UNIX;
181 strcpy(un.sun_path, "/dev/socket/logdw");
182
183 connect(sock, (struct sockaddr *)&un, sizeof(struct sockaddr_un));
184 }
185 } else {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800186 write_to_log = __write_to_log_null;
187 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800188 logd_fd = sock;
189#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 }
191
192#ifdef HAVE_PTHREADS
193 pthread_mutex_unlock(&log_init_lock);
194#endif
195
196 return write_to_log(log_id, vec, nr);
197}
198
199int __android_log_write(int prio, const char *tag, const char *msg)
200{
201 struct iovec vec[3];
202 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800203 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800204
205 if (!tag)
206 tag = "";
207
208 /* XXX: This needs to go! */
209 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500210 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700211 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 !strcmp(tag, "AT") ||
213 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700214 !strcmp(tag, "STK") ||
215 !strcmp(tag, "CDMA") ||
216 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800217 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 log_id = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800219 // Inform third party apps/ril/radio.. to use Rlog or RLOG
220 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
221 tag = tmp_tag;
222 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800223
224 vec[0].iov_base = (unsigned char *) &prio;
225 vec[0].iov_len = 1;
226 vec[1].iov_base = (void *) tag;
227 vec[1].iov_len = strlen(tag) + 1;
228 vec[2].iov_base = (void *) msg;
229 vec[2].iov_len = strlen(msg) + 1;
230
231 return write_to_log(log_id, vec, 3);
232}
233
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800234int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
235{
236 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800237 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800238
239 if (!tag)
240 tag = "";
241
242 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800243 if ((bufID != LOG_ID_RADIO) &&
244 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800245 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700246 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800247 !strcmp(tag, "AT") ||
248 !strcmp(tag, "GSM") ||
249 !strcmp(tag, "STK") ||
250 !strcmp(tag, "CDMA") ||
251 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800252 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800253 bufID = LOG_ID_RADIO;
Wink Saville3761e962012-11-28 12:20:19 -0800254 // Inform third party apps/ril/radio.. to use Rlog or RLOG
255 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
256 tag = tmp_tag;
257 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800258
259 vec[0].iov_base = (unsigned char *) &prio;
260 vec[0].iov_len = 1;
261 vec[1].iov_base = (void *) tag;
262 vec[1].iov_len = strlen(tag) + 1;
263 vec[2].iov_base = (void *) msg;
264 vec[2].iov_len = strlen(msg) + 1;
265
266 return write_to_log(bufID, vec, 3);
267}
268
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
270{
Chris Pearson19299902010-06-02 16:25:35 -0700271 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800272
273 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
274
275 return __android_log_write(prio, tag, buf);
276}
277
278int __android_log_print(int prio, const char *tag, const char *fmt, ...)
279{
280 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800281 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800282
283 va_start(ap, fmt);
284 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
285 va_end(ap);
286
287 return __android_log_write(prio, tag, buf);
288}
289
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800290int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
291{
292 va_list ap;
293 char buf[LOG_BUF_SIZE];
294
295 va_start(ap, fmt);
296 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
297 va_end(ap);
298
299 return __android_log_buf_write(bufID, prio, tag, buf);
300}
301
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800302void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800303 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800304{
Chris Pearson19299902010-06-02 16:25:35 -0700305 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800306
Chris Pearson19299902010-06-02 16:25:35 -0700307 if (fmt) {
308 va_list ap;
309 va_start(ap, fmt);
310 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
311 va_end(ap);
312 } else {
313 /* Msg not provided, log condition. N.B. Do not use cond directly as
314 * format string as it could contain spurious '%' syntax (e.g.
315 * "%d" in "blocks%devs == 0").
316 */
317 if (cond)
318 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
319 else
320 strcpy(buf, "Unspecified assertion failed");
321 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800322
323 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
324
325 __builtin_trap(); /* trap so we have a chance to debug the situation */
326}
327
328int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
329{
330 struct iovec vec[2];
331
332 vec[0].iov_base = &tag;
333 vec[0].iov_len = sizeof(tag);
334 vec[1].iov_base = (void*)payload;
335 vec[1].iov_len = len;
336
337 return write_to_log(LOG_ID_EVENTS, vec, 2);
338}
339
340/*
341 * Like __android_log_bwrite, but takes the type as well. Doesn't work
342 * for the general case where we're generating lists of stuff, but very
343 * handy if we just want to dump an integer into the log.
344 */
345int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800346 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347{
348 struct iovec vec[3];
349
350 vec[0].iov_base = &tag;
351 vec[0].iov_len = sizeof(tag);
352 vec[1].iov_base = &type;
353 vec[1].iov_len = sizeof(type);
354 vec[2].iov_base = (void*)payload;
355 vec[2].iov_len = len;
356
357 return write_to_log(LOG_ID_EVENTS, vec, 3);
358}