blob: a8ecc8d45774e327c4f0cd430b471cc7db12d2e0 [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>
Mark Salyzyn7a2a3072015-11-06 12:26:52 -080023#include <signal.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080024#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080025#include <stdarg.h>
Mark Salyzynd45d36e2015-02-12 15:14:26 -080026#include <stdatomic.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070030#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080031#include <sys/types.h>
32#if (FAKE_LOG_DEVICE == 0)
33#include <sys/socket.h>
34#include <sys/un.h>
35#endif
36#include <time.h>
37#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080038
Dan Albertc68fedc2014-08-18 17:29:34 -070039#ifdef __BIONIC__
40#include <android/set_abort_message.h>
41#endif
42
Colin Cross9227bd32013-07-23 16:59:20 -070043#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080044#include <log/logger.h>
45#include <log/log_read.h>
46#include <private/android_filesystem_config.h>
Mark Salyzyn7a809402015-01-16 13:38:07 -080047#include <private/android_logger.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080048
Mark Salyzyncf4aa032013-11-22 07:54:30 -080049#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050
51#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080052/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080053#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054#endif
55
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080057static 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 -080058
Mark Salyzyna04464a2014-04-30 08:50:53 -070059#ifndef __unused
60#define __unused __attribute__((__unused__))
61#endif
Kristian Monsenb5a98902014-01-28 11:26:56 -080062
Mark Salyzyn7a2a3072015-11-06 12:26:52 -080063#if !defined(_WIN32)
64static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
65
66static void lock(sigset_t *sigflags)
67{
68 /*
69 * If we trigger a signal handler in the middle of locked activity and the
70 * signal handler logs a message, we could get into a deadlock state.
71 */
72 sigset_t all;
73
74 sigfillset(&all);
75 pthread_sigmask(SIG_BLOCK, &all, sigflags);
76 pthread_mutex_lock(&log_init_lock);
77}
78
79static void unlock(sigset_t *sigflags)
80{
81 pthread_mutex_unlock(&log_init_lock);
82 pthread_sigmask(SIG_UNBLOCK, sigflags, NULL);
83}
84
85#define DECLARE_SIGSET(name) sigset_t name
86
87#else /* !defined(_WIN32) */
88
89#define lock(sigflags) ((void)0)
90#define unlock(sigflags) ((void)0)
91#define DECLARE_SIGSET(name)
92
93#endif /* !defined(_WIN32) */
94
Mark Salyzyn154f4602014-02-20 14:59:07 -080095#if FAKE_LOG_DEVICE
Mark Salyzyn99f47a92014-04-07 14:58:08 -070096static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070097#else
98static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080099static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800100#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800101
102/*
103 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -0800104 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800105 * the simulator rather than a desktop tool and want to use the device.
106 */
107static enum {
Chris Pearson19299902010-06-02 16:25:35 -0700108 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -0800110
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800111int __android_log_dev_available(void)
112{
113 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800114 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 g_log_status = kLogAvailable;
116 else
117 g_log_status = kLogNotAvailable;
118 }
119
120 return (g_log_status == kLogAvailable);
121}
122
Mark Salyzyn8245af12014-03-25 13:45:33 -0700123/* log_init_lock assumed */
124static int __write_to_log_initialize()
125{
126 int i, ret = 0;
127
128#if FAKE_LOG_DEVICE
129 for (i = 0; i < LOG_ID_MAX; i++) {
130 char buf[sizeof("/dev/log_system")];
131 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
132 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
133 }
134#else
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700135 if (pstore_fd < 0) {
136 pstore_fd = TEMP_FAILURE_RETRY(open("/dev/pmsg0", O_WRONLY));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700137 }
138
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700139 if (logd_fd < 0) {
140 i = TEMP_FAILURE_RETRY(socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0));
141 if (i < 0) {
142 ret = -errno;
143 } else if (TEMP_FAILURE_RETRY(fcntl(i, F_SETFL, O_NONBLOCK)) < 0) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700144 ret = -errno;
145 close(i);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700146 } else {
147 struct sockaddr_un un;
148 memset(&un, 0, sizeof(struct sockaddr_un));
149 un.sun_family = AF_UNIX;
150 strcpy(un.sun_path, "/dev/socket/logdw");
151
152 if (TEMP_FAILURE_RETRY(connect(i, (struct sockaddr *)&un,
153 sizeof(struct sockaddr_un))) < 0) {
154 ret = -errno;
155 close(i);
156 } else {
157 logd_fd = i;
158 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700159 }
160 }
Mark Salyzyn8245af12014-03-25 13:45:33 -0700161#endif
162
163 return ret;
164}
165
Mark Salyzyn53016d82015-02-04 12:28:47 -0800166static 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 -0800167{
168 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700169#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800170 int log_fd;
171
172 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
173 log_fd = log_fds[(int)log_id];
174 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700175 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800176 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800177 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800178 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700179 if (ret < 0) {
180 ret = -errno;
181 }
182 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800183#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800184 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700185 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800186 android_log_header_t header;
187 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700188 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700189 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700190 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800191 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800192 static atomic_int_fast32_t dropped;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700193
Mark Salyzyn31f7df52015-03-13 15:57:11 -0700194 if (!nr) {
195 return -EINVAL;
196 }
197
Mark Salyzyn076ba812014-05-29 17:53:41 -0700198 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
199 last_uid = getuid();
200 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800201 if (last_pid == (pid_t) -1) {
202 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800203 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700204 /*
205 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800206 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800207 * android_pmsg_log_header_t pmsg_header;
208 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800209 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700210 * // caller provides
211 * union {
212 * struct {
213 * char prio;
214 * char payload[];
215 * } string;
216 * struct {
217 * uint32_t tag
218 * char payload[];
219 * } binary;
220 * };
221 * };
222 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700223
Mark Salyzynb6bee332015-09-08 08:56:32 -0700224 if (android_log_timestamp() == 'm') {
225 clock_gettime(CLOCK_MONOTONIC, &ts);
226 } else {
227 clock_gettime(CLOCK_REALTIME, &ts);
228 }
Mark Salyzyn076ba812014-05-29 17:53:41 -0700229
Mark Salyzynd91ab582014-12-15 10:52:12 -0800230 pmsg_header.magic = LOGGER_MAGIC;
231 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
232 pmsg_header.uid = last_uid;
233 pmsg_header.pid = last_pid;
234
Mark Salyzyn7a809402015-01-16 13:38:07 -0800235 header.tid = gettid();
236 header.realtime.tv_sec = ts.tv_sec;
237 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800238
Mark Salyzynd91ab582014-12-15 10:52:12 -0800239 newVec[0].iov_base = (unsigned char *) &pmsg_header;
240 newVec[0].iov_len = sizeof(pmsg_header);
241 newVec[1].iov_base = (unsigned char *) &header;
242 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800243
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800244 if (logd_fd > 0) {
245 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
246 if (snapshot) {
247 android_log_event_int_t buffer;
248
249 header.id = LOG_ID_EVENTS;
250 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
251 buffer.payload.type = EVENT_TYPE_INT;
252 buffer.payload.data = htole32(snapshot);
253
254 newVec[2].iov_base = &buffer;
255 newVec[2].iov_len = sizeof(buffer);
256
257 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
258 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
259 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
260 }
261 }
262 }
263
264 header.id = log_id;
265
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700266 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
267 newVec[i].iov_base = vec[i - header_length].iov_base;
268 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
269
270 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
271 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
272 if (newVec[i].iov_len) {
273 ++i;
274 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800275 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700276 break;
277 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800278 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800279 pmsg_header.len += payload_size;
280
281 if (pstore_fd >= 0) {
282 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
283 }
284
285 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
286 /*
287 * ignore log messages we send to ourself (logd).
288 * Such log messages are often generated by libraries we depend on
289 * which use standard Android logging.
290 */
291 return 0;
292 }
293
294 if (logd_fd < 0) {
295 return -EBADF;
296 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800297
Mark Salyzyn8245af12014-03-25 13:45:33 -0700298 /*
299 * The write below could be lost, but will never block.
300 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800301 * To logd, we drop the pmsg_header
302 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700303 * ENOTCONN occurs if logd dies.
304 * EAGAIN occurs if logd is overloaded.
305 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800306 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700307 if (ret < 0) {
Mark Salyzyn7a2a3072015-11-06 12:26:52 -0800308 DECLARE_SIGSET(sigflags);
309
Mark Salyzyn8245af12014-03-25 13:45:33 -0700310 ret = -errno;
311 if (ret == -ENOTCONN) {
Mark Salyzyn7a2a3072015-11-06 12:26:52 -0800312 lock(&sigflags);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700313 close(logd_fd);
314 logd_fd = -1;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700315 ret = __write_to_log_initialize();
Mark Salyzyn7a2a3072015-11-06 12:26:52 -0800316 unlock(&sigflags);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700317
318 if (ret < 0) {
319 return ret;
320 }
321
Mark Salyzynd91ab582014-12-15 10:52:12 -0800322 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700323 if (ret < 0) {
324 ret = -errno;
325 }
326 }
327 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700328
Mark Salyzyn7a809402015-01-16 13:38:07 -0800329 if (ret > (ssize_t)sizeof(header)) {
330 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800331 } else if (ret == -EAGAIN) {
332 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700333 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800334#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700335
336 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337}
338
Mark Salyzyn154f4602014-02-20 14:59:07 -0800339#if FAKE_LOG_DEVICE
340static const char *LOG_NAME[LOG_ID_MAX] = {
341 [LOG_ID_MAIN] = "main",
342 [LOG_ID_RADIO] = "radio",
343 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700344 [LOG_ID_SYSTEM] = "system",
Mark Salyzyn440e1092014-10-10 15:13:15 -0700345 [LOG_ID_CRASH] = "crash",
346 [LOG_ID_KERNEL] = "kernel",
Mark Salyzyn154f4602014-02-20 14:59:07 -0800347};
348
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700349const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800350{
351 if (log_id >= LOG_ID_MAX) {
352 log_id = LOG_ID_MAIN;
353 }
354 return LOG_NAME[log_id];
355}
356#endif
357
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
359{
Mark Salyzyn7a2a3072015-11-06 12:26:52 -0800360 DECLARE_SIGSET(sigflags);
361
362 lock(&sigflags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800363
364 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700365 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
Mark Salyzyn8245af12014-03-25 13:45:33 -0700367 ret = __write_to_log_initialize();
368 if (ret < 0) {
Mark Salyzyn7a2a3072015-11-06 12:26:52 -0800369 unlock(&sigflags);
Mark Salyzyn0d00a442015-03-13 12:15:57 -0700370#if (FAKE_LOG_DEVICE == 0)
371 if (pstore_fd >= 0) {
372 __write_to_log_daemon(log_id, vec, nr);
373 }
374#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700375 return ret;
376 }
377
Mark Salyzyn53016d82015-02-04 12:28:47 -0800378 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 }
380
Mark Salyzyn7a2a3072015-11-06 12:26:52 -0800381 unlock(&sigflags);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382
383 return write_to_log(log_id, vec, nr);
384}
385
386int __android_log_write(int prio, const char *tag, const char *msg)
387{
Dan Albertc7aadc42015-04-02 10:32:44 -0700388 return __android_log_buf_write(LOG_ID_MAIN, prio, tag, msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389}
390
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800391int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
392{
393 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800394 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800395
396 if (!tag)
397 tag = "";
398
399 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800400 if ((bufID != LOG_ID_RADIO) &&
401 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800402 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700403 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800404 !strcmp(tag, "AT") ||
405 !strcmp(tag, "GSM") ||
406 !strcmp(tag, "STK") ||
407 !strcmp(tag, "CDMA") ||
408 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800409 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800410 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700411 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800412 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
413 tag = tmp_tag;
414 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800415
Dan Albertc7aadc42015-04-02 10:32:44 -0700416#if __BIONIC__
417 if (prio == ANDROID_LOG_FATAL) {
418 android_set_abort_message(msg);
419 }
420#endif
421
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800422 vec[0].iov_base = (unsigned char *) &prio;
423 vec[0].iov_len = 1;
424 vec[1].iov_base = (void *) tag;
425 vec[1].iov_len = strlen(tag) + 1;
426 vec[2].iov_base = (void *) msg;
427 vec[2].iov_len = strlen(msg) + 1;
428
429 return write_to_log(bufID, vec, 3);
430}
431
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800432int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
433{
Chris Pearson19299902010-06-02 16:25:35 -0700434 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800435
436 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
437
438 return __android_log_write(prio, tag, buf);
439}
440
441int __android_log_print(int prio, const char *tag, const char *fmt, ...)
442{
443 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800444 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800445
446 va_start(ap, fmt);
447 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
448 va_end(ap);
449
450 return __android_log_write(prio, tag, buf);
451}
452
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800453int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
454{
455 va_list ap;
456 char buf[LOG_BUF_SIZE];
457
458 va_start(ap, fmt);
459 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
460 va_end(ap);
461
462 return __android_log_buf_write(bufID, prio, tag, buf);
463}
464
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800465void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800466 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800467{
Chris Pearson19299902010-06-02 16:25:35 -0700468 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800469
Chris Pearson19299902010-06-02 16:25:35 -0700470 if (fmt) {
471 va_list ap;
472 va_start(ap, fmt);
473 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
474 va_end(ap);
475 } else {
476 /* Msg not provided, log condition. N.B. Do not use cond directly as
477 * format string as it could contain spurious '%' syntax (e.g.
478 * "%d" in "blocks%devs == 0").
479 */
480 if (cond)
481 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
482 else
483 strcpy(buf, "Unspecified assertion failed");
484 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800485
486 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
Elliott Hughes02ff4b82015-03-07 11:21:37 -0800487 abort(); /* abort so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700488 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489}
490
491int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
492{
493 struct iovec vec[2];
494
495 vec[0].iov_base = &tag;
496 vec[0].iov_len = sizeof(tag);
497 vec[1].iov_base = (void*)payload;
498 vec[1].iov_len = len;
499
500 return write_to_log(LOG_ID_EVENTS, vec, 2);
501}
502
503/*
504 * Like __android_log_bwrite, but takes the type as well. Doesn't work
505 * for the general case where we're generating lists of stuff, but very
506 * handy if we just want to dump an integer into the log.
507 */
508int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800509 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800510{
511 struct iovec vec[3];
512
513 vec[0].iov_base = &tag;
514 vec[0].iov_len = sizeof(tag);
515 vec[1].iov_base = &type;
516 vec[1].iov_len = sizeof(type);
517 vec[2].iov_base = (void*)payload;
518 vec[2].iov_len = len;
519
520 return write_to_log(LOG_ID_EVENTS, vec, 3);
521}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700522
523/*
524 * Like __android_log_bwrite, but used for writing strings to the
525 * event log.
526 */
527int __android_log_bswrite(int32_t tag, const char *payload)
528{
529 struct iovec vec[4];
530 char type = EVENT_TYPE_STRING;
531 uint32_t len = strlen(payload);
532
533 vec[0].iov_base = &tag;
534 vec[0].iov_len = sizeof(tag);
535 vec[1].iov_base = &type;
536 vec[1].iov_len = sizeof(type);
537 vec[2].iov_base = &len;
538 vec[2].iov_len = sizeof(len);
539 vec[3].iov_base = (void*)payload;
540 vec[3].iov_len = len;
541
542 return write_to_log(LOG_ID_EVENTS, vec, 4);
543}