blob: c2b0ec2ea3b0dfc93ad448cb5d1f4b9e5ea1f803 [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 Salyzyn31dd00f2015-03-04 17:01:30 -080016#if (FAKE_LOG_DEVICE == 0)
Mark Salyzynd45d36e2015-02-12 15:14:26 -080017#include <endian.h>
Mark Salyzyn31dd00f2015-03-04 17:01:30 -080018#endif
Mark Salyzyn154f4602014-02-20 14:59:07 -080019#include <errno.h>
20#include <fcntl.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080021#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <pthread.h>
23#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#include <stdarg.h>
Mark Salyzynd45d36e2015-02-12 15:14:26 -080025#include <stdatomic.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070029#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080030#include <sys/types.h>
31#if (FAKE_LOG_DEVICE == 0)
32#include <sys/socket.h>
33#include <sys/un.h>
34#endif
35#include <time.h>
36#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037
Dan Albertc68fedc2014-08-18 17:29:34 -070038#ifdef __BIONIC__
39#include <android/set_abort_message.h>
40#endif
41
Colin Cross9227bd32013-07-23 16:59:20 -070042#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080043#include <log/logger.h>
44#include <log/log_read.h>
45#include <private/android_filesystem_config.h>
Mark Salyzyn7a809402015-01-16 13:38:07 -080046#include <private/android_logger.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080047
Mark Salyzyncf4aa032013-11-22 07:54:30 -080048#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080051/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080052#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053#endif
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080056static 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 -080057
Mark Salyzyna04464a2014-04-30 08:50:53 -070058#ifndef __unused
59#define __unused __attribute__((__unused__))
60#endif
Kristian Monsenb5a98902014-01-28 11:26:56 -080061
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -080062#if !defined(_WIN32)
63static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
64
65static void lock()
66{
67 /*
68 * If we trigger a signal handler in the middle of locked activity and the
69 * signal handler logs a message, we could get into a deadlock state.
70 */
71 pthread_mutex_lock(&log_init_lock);
72}
73
74static void unlock()
75{
76 pthread_mutex_unlock(&log_init_lock);
77}
78
79#else /* !defined(_WIN32) */
80
81#define lock() ((void)0)
82#define unlock() ((void)0)
83
84#endif /* !defined(_WIN32) */
85
Mark Salyzyn154f4602014-02-20 14:59:07 -080086#if FAKE_LOG_DEVICE
Mark Salyzyn083b0372015-12-04 10:59:45 -080087static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070088#else
89static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080090static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080091#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092
93/*
94 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080095 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080096 * the simulator rather than a desktop tool and want to use the device.
97 */
98static enum {
Chris Pearson19299902010-06-02 16:25:35 -070099 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800100} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -0800101
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800102int __android_log_dev_available(void)
103{
104 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800105 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106 g_log_status = kLogAvailable;
107 else
108 g_log_status = kLogNotAvailable;
109 }
110
111 return (g_log_status == kLogAvailable);
112}
113
Mark Salyzyn8245af12014-03-25 13:45:33 -0700114/* log_init_lock assumed */
115static int __write_to_log_initialize()
116{
117 int i, ret = 0;
118
119#if FAKE_LOG_DEVICE
120 for (i = 0; i < LOG_ID_MAX; i++) {
121 char buf[sizeof("/dev/log_system")];
122 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
123 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
124 }
125#else
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700126 if (pstore_fd < 0) {
127 pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700128 }
129
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700130 if (logd_fd < 0) {
131 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
132 if (i < 0) {
133 ret = -errno;
134 } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700135 ret = -errno;
136 close(i);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700137 } else {
138 struct sockaddr_un un;
139 memset(&un, 0, sizeof(struct sockaddr_un));
140 un.sun_family = AF_UNIX;
141 strcpy(un.sun_path, "/dev/socket/logdw");
142
143 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
144 sizeof(struct sockaddr_un))) < 0) {
145 ret = -errno;
146 close(i);
147 } else {
148 logd_fd = i;
149 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700150 }
151 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700152#endif
153
154 return ret;
155}
156
Mark Salyzyn53016d82015-02-04 12:28:47 -0800157static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158{
159 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700160#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 int log_fd;
162
163 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
164 log_fd = log_fds[(int)log_id];
165 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700166 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800169 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700170 if (ret < 0) {
171 ret = -errno;
172 }
173 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800174#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800175 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700176 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800177 android_log_header_t header;
178 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700179 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700180 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700181 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800182 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800183 static atomic_int_fast32_t dropped;
Mark Salyzyn083b0372015-12-04 10:59:45 -0800184 static atomic_int_fast32_t dropped_security;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700185
Mark Salyzyn31f7df52015-03-13 15:57:11 -0700186 if (!nr) {
187 return -EINVAL;
188 }
189
Mark Salyzyn076ba812014-05-29 17:53:41 -0700190 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
191 last_uid = getuid();
192 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800193 if (last_pid == (pid_t) -1) {
194 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800195 }
Mark Salyzyn083b0372015-12-04 10:59:45 -0800196 if (log_id == LOG_ID_SECURITY) {
197 if ((last_uid != AID_SYSTEM) && (last_uid != AID_ROOT)) {
198 uid_t uid = geteuid();
199 if ((uid != AID_SYSTEM) && (uid != AID_ROOT)) {
200 gid_t gid = getgid();
201 if ((gid != AID_SYSTEM) && (gid != AID_ROOT)) {
202 gid = getegid();
203 if ((gid != AID_SYSTEM) && (gid != AID_ROOT)) {
204 return -EPERM;
205 }
206 }
207 }
208 }
209 if (!__android_log_security()) {
210 return -EPERM;
211 }
212 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700213 /*
214 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800215 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800216 * android_pmsg_log_header_t pmsg_header;
217 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800218 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700219 * // caller provides
220 * union {
221 * struct {
222 * char prio;
223 * char payload[];
224 * } string;
225 * struct {
226 * uint32_t tag
227 * char payload[];
228 * } binary;
229 * };
230 * };
231 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700232
Mark Salyzynba7a9a02015-12-01 15:57:25 -0800233 clock_gettime(android_log_clockid(), &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700234
Mark Salyzynd91ab582014-12-15 10:52:12 -0800235 pmsg_header.magic = LOGGER_MAGIC;
236 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
237 pmsg_header.uid = last_uid;
238 pmsg_header.pid = last_pid;
239
Mark Salyzyn7a809402015-01-16 13:38:07 -0800240 header.tid = gettid();
241 header.realtime.tv_sec = ts.tv_sec;
242 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800243
Mark Salyzynd91ab582014-12-15 10:52:12 -0800244 newVec[0].iov_base = (unsigned char *) &pmsg_header;
245 newVec[0].iov_len = sizeof(pmsg_header);
246 newVec[1].iov_base = (unsigned char *) &header;
247 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800248
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800249 if (logd_fd > 0) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800250 int32_t snapshot = atomic_exchange_explicit(&dropped_security, 0,
251 memory_order_relaxed);
252 if (snapshot) {
253 android_log_event_int_t buffer;
254
255 header.id = LOG_ID_SECURITY;
256 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
257 buffer.payload.type = EVENT_TYPE_INT;
258 buffer.payload.data = htole32(snapshot);
259
260 newVec[2].iov_base = &buffer;
261 newVec[2].iov_len = sizeof(buffer);
262
263 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
264 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
265 atomic_fetch_add_explicit(&dropped_security, snapshot,
266 memory_order_relaxed);
267 }
268 }
269 snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800270 if (snapshot) {
271 android_log_event_int_t buffer;
272
273 header.id = LOG_ID_EVENTS;
274 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
275 buffer.payload.type = EVENT_TYPE_INT;
276 buffer.payload.data = htole32(snapshot);
277
278 newVec[2].iov_base = &buffer;
279 newVec[2].iov_len = sizeof(buffer);
280
281 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
282 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
Mark Salyzyn083b0372015-12-04 10:59:45 -0800283 atomic_fetch_add_explicit(&dropped, snapshot,
284 memory_order_relaxed);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800285 }
286 }
287 }
288
289 header.id = log_id;
290
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700291 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
292 newVec[i].iov_base = vec[i - header_length].iov_base;
293 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
294
295 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
296 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
297 if (newVec[i].iov_len) {
298 ++i;
299 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800300 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700301 break;
302 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800303 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800304 pmsg_header.len += payload_size;
305
306 if (pstore_fd >= 0) {
307 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
308 }
309
310 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
311 /*
312 * ignore log messages we send to ourself (logd).
313 * Such log messages are often generated by libraries we depend on
314 * which use standard Android logging.
315 */
316 return 0;
317 }
318
319 if (logd_fd < 0) {
320 return -EBADF;
321 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800322
Mark Salyzyn8245af12014-03-25 13:45:33 -0700323 /*
324 * The write below could be lost, but will never block.
325 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800326 * To logd, we drop the pmsg_header
327 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700328 * ENOTCONN occurs if logd dies.
329 * EAGAIN occurs if logd is overloaded.
330 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800331 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700332 if (ret < 0) {
333 ret = -errno;
334 if (ret == -ENOTCONN) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800335 lock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700336 close(logd_fd);
337 logd_fd = -1;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700338 ret = __write_to_log_initialize();
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800339 unlock();
Mark Salyzyn8245af12014-03-25 13:45:33 -0700340
341 if (ret < 0) {
342 return ret;
343 }
344
Mark Salyzynd91ab582014-12-15 10:52:12 -0800345 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700346 if (ret < 0) {
347 ret = -errno;
348 }
349 }
350 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700351
Mark Salyzyn7a809402015-01-16 13:38:07 -0800352 if (ret > (ssize_t)sizeof(header)) {
353 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800354 } else if (ret == -EAGAIN) {
355 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn083b0372015-12-04 10:59:45 -0800356 if (log_id == LOG_ID_SECURITY) {
357 atomic_fetch_add_explicit(&dropped_security, 1,
358 memory_order_relaxed);
359 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700360 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800361#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700362
363 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364}
365
Mark Salyzyn154f4602014-02-20 14:59:07 -0800366#if FAKE_LOG_DEVICE
367static const char *LOG_NAME[LOG_ID_MAX] = {
368 [LOG_ID_MAIN] = "main",
369 [LOG_ID_RADIO] = "radio",
370 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700371 [LOG_ID_SYSTEM] = "system",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700372 [LOG_ID_CRASH] = "crash",
Mark Salyzyn083b0372015-12-04 10:59:45 -0800373 [LOG_ID_SECURITY] = "security",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700374 [LOG_ID_KERNEL] = "kernel",
Mark Salyzyn154f4602014-02-20 14:59:07 -0800375};
376
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700377const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800378{
379 if (log_id >= LOG_ID_MAX) {
380 log_id = LOG_ID_MAIN;
381 }
382 return LOG_NAME[log_id];
383}
384#endif
385
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800386static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
387{
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800388 lock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389
390 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700391 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392
Mark Salyzyn8245af12014-03-25 13:45:33 -0700393 ret = __write_to_log_initialize();
394 if (ret < 0) {
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800395 unlock();
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700396#if (FAKE_LOG_DEVICE == 0)
397 if (pstore_fd >= 0) {
398 __write_to_log_daemon(log_id, vec, nr);
399 }
400#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700401 return ret;
402 }
403
Mark Salyzyn53016d82015-02-04 12:28:47 -0800404 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 }
406
Mark Salyzyn2d2e0a52015-11-06 12:26:52 -0800407 unlock();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800408
409 return write_to_log(log_id, vec, nr);
410}
411
412int __android_log_write(int prio, const char *tag, const char *msg)
413{
Dan Albertc7aadc42015-04-02 10:32:44 -0700414 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800415}
416
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800417int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
418{
419 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800420 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800421
422 if (!tag)
423 tag = "";
424
425 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800426 if ((bufID != LOG_ID_RADIO) &&
427 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800428 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700429 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800430 !strcmp(tag, "AT") ||
431 !strcmp(tag, "GSM") ||
432 !strcmp(tag, "STK") ||
433 !strcmp(tag, "CDMA") ||
434 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800435 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800436 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700437 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800438 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
439 tag = tmp_tag;
440 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800441
Dan Albertc7aadc42015-04-02 10:32:44 -0700442#if __BIONIC__
443 if (prio == ANDROID_LOG_FATAL) {
444 android_set_abort_message(msg);
445 }
446#endif
447
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800448 vec[0].iov_base = (unsigned char *) &prio;
449 vec[0].iov_len = 1;
450 vec[1].iov_base = (void *) tag;
451 vec[1].iov_len = strlen(tag) + 1;
452 vec[2].iov_base = (void *) msg;
453 vec[2].iov_len = strlen(msg) + 1;
454
455 return write_to_log(bufID, vec, 3);
456}
457
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800458int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
459{
Chris Pearson19299902010-06-02 16:25:35 -0700460 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800461
462 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
463
464 return __android_log_write(prio, tag, buf);
465}
466
467int __android_log_print(int prio, const char *tag, const char *fmt, ...)
468{
469 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800470 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800471
472 va_start(ap, fmt);
473 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
474 va_end(ap);
475
476 return __android_log_write(prio, tag, buf);
477}
478
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800479int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
480{
481 va_list ap;
482 char buf[LOG_BUF_SIZE];
483
484 va_start(ap, fmt);
485 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
486 va_end(ap);
487
488 return __android_log_buf_write(bufID, prio, tag, buf);
489}
490
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800491void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800492 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800493{
Chris Pearson19299902010-06-02 16:25:35 -0700494 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800495
Chris Pearson19299902010-06-02 16:25:35 -0700496 if (fmt) {
497 va_list ap;
498 va_start(ap, fmt);
499 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
500 va_end(ap);
501 } else {
502 /* Msg not provided, log condition. N.B. Do not use cond directly as
503 * format string as it could contain spurious '%' syntax (e.g.
504 * "%d" in "blocks%devs == 0").
505 */
506 if (cond)
507 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
508 else
509 strcpy(buf, "Unspecified assertion failed");
510 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511
512 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800513 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700514 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515}
516
517int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
518{
519 struct iovec vec[2];
520
521 vec[0].iov_base = &tag;
522 vec[0].iov_len = sizeof(tag);
523 vec[1].iov_base = (void*)payload;
524 vec[1].iov_len = len;
525
526 return write_to_log(LOG_ID_EVENTS, vec, 2);
527}
528
Mark Salyzyn083b0372015-12-04 10:59:45 -0800529int __android_log_security_bwrite(int32_t tag, const void *payload, size_t len)
530{
531 struct iovec vec[2];
532
533 vec[0].iov_base = &tag;
534 vec[0].iov_len = sizeof(tag);
535 vec[1].iov_base = (void*)payload;
536 vec[1].iov_len = len;
537
538 return write_to_log(LOG_ID_SECURITY, vec, 2);
539}
540
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800541/*
542 * Like __android_log_bwrite, but takes the type as well. Doesn't work
543 * for the general case where we're generating lists of stuff, but very
544 * handy if we just want to dump an integer into the log.
545 */
546int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800547 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800548{
549 struct iovec vec[3];
550
551 vec[0].iov_base = &tag;
552 vec[0].iov_len = sizeof(tag);
553 vec[1].iov_base = &type;
554 vec[1].iov_len = sizeof(type);
555 vec[2].iov_base = (void*)payload;
556 vec[2].iov_len = len;
557
558 return write_to_log(LOG_ID_EVENTS, vec, 3);
559}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700560
561/*
562 * Like __android_log_bwrite, but used for writing strings to the
563 * event log.
564 */
565int __android_log_bswrite(int32_t tag, const char *payload)
566{
567 struct iovec vec[4];
568 char type = EVENT_TYPE_STRING;
569 uint32_t len = strlen(payload);
570
571 vec[0].iov_base = &tag;
572 vec[0].iov_len = sizeof(tag);
573 vec[1].iov_base = &type;
574 vec[1].iov_len = sizeof(type);
575 vec[2].iov_base = &len;
576 vec[2].iov_len = sizeof(len);
577 vec[3].iov_base = (void*)payload;
578 vec[3].iov_len = len;
579
580 return write_to_log(LOG_ID_EVENTS, vec, 4);
581}