blob: c8a6162b7f26c601da2deebacadf933cd2a66767 [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 Salyzynd45d36e2015-02-12 15:14:26 -080016#include <endian.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080017#include <errno.h>
18#include <fcntl.h>
Yabin Cui4a6e5a32015-01-26 19:48:54 -080019#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080020#include <pthread.h>
21#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080022#include <stdarg.h>
Mark Salyzynd45d36e2015-02-12 15:14:26 -080023#include <stdatomic.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
Nick Kralevicha1703222013-03-15 09:45:12 -070027#include <sys/stat.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080028#include <sys/types.h>
29#if (FAKE_LOG_DEVICE == 0)
30#include <sys/socket.h>
31#include <sys/un.h>
32#endif
33#include <time.h>
34#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035
Dan Albertc68fedc2014-08-18 17:29:34 -070036#ifdef __BIONIC__
37#include <android/set_abort_message.h>
38#endif
39
Colin Cross9227bd32013-07-23 16:59:20 -070040#include <log/logd.h>
Mark Salyzyn154f4602014-02-20 14:59:07 -080041#include <log/logger.h>
42#include <log/log_read.h>
43#include <private/android_filesystem_config.h>
Mark Salyzyn7a809402015-01-16 13:38:07 -080044#include <private/android_logger.h>
Mark Salyzyne9c41962014-01-02 13:52:29 -080045
Mark Salyzyncf4aa032013-11-22 07:54:30 -080046#define LOG_BUF_SIZE 1024
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
48#if FAKE_LOG_DEVICE
Mark Salyzyn154f4602014-02-20 14:59:07 -080049/* This will be defined when building for the host. */
Kristian Monsenb5a98902014-01-28 11:26:56 -080050#include "fake_log_device.h"
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080051#endif
52
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080053static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr);
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -080054static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init;
Yabin Cui4a6e5a32015-01-26 19:48:54 -080055#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
57#endif
58
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 Salyzyn154f4602014-02-20 14:59:07 -080063#if FAKE_LOG_DEVICE
Mark Salyzyn99f47a92014-04-07 14:58:08 -070064static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 };
Mark Salyzyna04464a2014-04-30 08:50:53 -070065#else
66static int logd_fd = -1;
Mark Salyzynd91ab582014-12-15 10:52:12 -080067static int pstore_fd = -1;
Mark Salyzyn154f4602014-02-20 14:59:07 -080068#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069
70/*
71 * This is used by the C++ code to decide if it should write logs through
Mark Salyzyn154f4602014-02-20 14:59:07 -080072 * the C code. Basically, if /dev/socket/logd is available, we're running in
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080073 * the simulator rather than a desktop tool and want to use the device.
74 */
75static enum {
Chris Pearson19299902010-06-02 16:25:35 -070076 kLogUninitialized, kLogNotAvailable, kLogAvailable
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080077} g_log_status = kLogUninitialized;
Mark Salyzyn53016d82015-02-04 12:28:47 -080078
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080079int __android_log_dev_available(void)
80{
81 if (g_log_status == kLogUninitialized) {
Mark Salyzyn154f4602014-02-20 14:59:07 -080082 if (access("/dev/socket/logdw", W_OK) == 0)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080083 g_log_status = kLogAvailable;
84 else
85 g_log_status = kLogNotAvailable;
86 }
87
88 return (g_log_status == kLogAvailable);
89}
90
Mark Salyzyna04464a2014-04-30 08:50:53 -070091#if !FAKE_LOG_DEVICE
Mark Salyzyn8245af12014-03-25 13:45:33 -070092/* give up, resources too limited */
Mark Salyzyna04464a2014-04-30 08:50:53 -070093static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused,
94 size_t nr __unused)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080095{
96 return -1;
97}
Mark Salyzyna04464a2014-04-30 08:50:53 -070098#endif
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080099
Mark Salyzyn8245af12014-03-25 13:45:33 -0700100/* log_init_lock assumed */
101static int __write_to_log_initialize()
102{
103 int i, ret = 0;
104
105#if FAKE_LOG_DEVICE
106 for (i = 0; i < LOG_ID_MAX; i++) {
107 char buf[sizeof("/dev/log_system")];
108 snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i));
109 log_fds[i] = fakeLogOpen(buf, O_WRONLY);
110 }
111#else
112 if (logd_fd >= 0) {
113 i = logd_fd;
114 logd_fd = -1;
115 close(i);
116 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800117 if (pstore_fd >= 0) {
118 i = pstore_fd;
119 pstore_fd = -1;
120 close(i);
121 }
122 pstore_fd = open("/dev/pmsg0", O_WRONLY);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700123
Nick Kralevich118d1b32014-07-02 22:30:39 -0700124 i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700125 if (i < 0) {
126 ret = -errno;
127 write_to_log = __write_to_log_null;
128 } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) {
129 ret = -errno;
130 close(i);
131 i = -1;
132 write_to_log = __write_to_log_null;
133 } else {
134 struct sockaddr_un un;
135 memset(&un, 0, sizeof(struct sockaddr_un));
136 un.sun_family = AF_UNIX;
137 strcpy(un.sun_path, "/dev/socket/logdw");
138
139 if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) {
140 ret = -errno;
141 close(i);
142 i = -1;
143 }
144 }
145 logd_fd = i;
146#endif
147
148 return ret;
149}
150
Mark Salyzyn53016d82015-02-04 12:28:47 -0800151static 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 -0800152{
153 ssize_t ret;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700154#if FAKE_LOG_DEVICE
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800155 int log_fd;
156
157 if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) {
158 log_fd = log_fds[(int)log_id];
159 } else {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700160 return -EBADF;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 do {
Mark Salyzyn154f4602014-02-20 14:59:07 -0800163 ret = fakeLogWritev(log_fd, vec, nr);
Mark Salyzyn8245af12014-03-25 13:45:33 -0700164 if (ret < 0) {
165 ret = -errno;
166 }
167 } while (ret == -EINTR);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800168#else
Mark Salyzynd91ab582014-12-15 10:52:12 -0800169 static const unsigned header_length = 2;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700170 struct iovec newVec[nr + header_length];
Mark Salyzynd91ab582014-12-15 10:52:12 -0800171 android_log_header_t header;
172 android_pmsg_log_header_t pmsg_header;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700173 struct timespec ts;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700174 size_t i, payload_size;
Mark Salyzyn076ba812014-05-29 17:53:41 -0700175 static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800176 static pid_t last_pid = (pid_t) -1;
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800177 static atomic_int_fast32_t dropped;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700178
Mark Salyzyn076ba812014-05-29 17:53:41 -0700179 if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */
180 last_uid = getuid();
181 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800182 if (last_pid == (pid_t) -1) {
183 last_pid = getpid();
Mark Salyzyn154f4602014-02-20 14:59:07 -0800184 }
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700185 /*
186 * struct {
Mark Salyzyn53016d82015-02-04 12:28:47 -0800187 * // what we provide to pstore
Mark Salyzynd91ab582014-12-15 10:52:12 -0800188 * android_pmsg_log_header_t pmsg_header;
189 * // what we provide to socket
Mark Salyzyn7a809402015-01-16 13:38:07 -0800190 * android_log_header_t header;
Mark Salyzynb992d0d2014-03-20 16:09:38 -0700191 * // caller provides
192 * union {
193 * struct {
194 * char prio;
195 * char payload[];
196 * } string;
197 * struct {
198 * uint32_t tag
199 * char payload[];
200 * } binary;
201 * };
202 * };
203 */
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700204
Mark Salyzyn076ba812014-05-29 17:53:41 -0700205 clock_gettime(CLOCK_REALTIME, &ts);
Mark Salyzyn076ba812014-05-29 17:53:41 -0700206
Mark Salyzynd91ab582014-12-15 10:52:12 -0800207 pmsg_header.magic = LOGGER_MAGIC;
208 pmsg_header.len = sizeof(pmsg_header) + sizeof(header);
209 pmsg_header.uid = last_uid;
210 pmsg_header.pid = last_pid;
211
Mark Salyzyn7a809402015-01-16 13:38:07 -0800212 header.tid = gettid();
213 header.realtime.tv_sec = ts.tv_sec;
214 header.realtime.tv_nsec = ts.tv_nsec;
Mark Salyzyn154f4602014-02-20 14:59:07 -0800215
Mark Salyzynd91ab582014-12-15 10:52:12 -0800216 newVec[0].iov_base = (unsigned char *) &pmsg_header;
217 newVec[0].iov_len = sizeof(pmsg_header);
218 newVec[1].iov_base = (unsigned char *) &header;
219 newVec[1].iov_len = sizeof(header);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800220
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800221 if (logd_fd > 0) {
222 int32_t snapshot = atomic_exchange_explicit(&dropped, 0, memory_order_relaxed);
223 if (snapshot) {
224 android_log_event_int_t buffer;
225
226 header.id = LOG_ID_EVENTS;
227 buffer.header.tag = htole32(LIBLOG_LOG_TAG);
228 buffer.payload.type = EVENT_TYPE_INT;
229 buffer.payload.data = htole32(snapshot);
230
231 newVec[2].iov_base = &buffer;
232 newVec[2].iov_len = sizeof(buffer);
233
234 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, 2));
235 if (ret != (ssize_t)(sizeof(header) + sizeof(buffer))) {
236 atomic_fetch_add_explicit(&dropped, snapshot, memory_order_relaxed);
237 }
238 }
239 }
240
241 header.id = log_id;
242
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700243 for (payload_size = 0, i = header_length; i < nr + header_length; i++) {
244 newVec[i].iov_base = vec[i - header_length].iov_base;
245 payload_size += newVec[i].iov_len = vec[i - header_length].iov_len;
246
247 if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) {
248 newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD;
249 if (newVec[i].iov_len) {
250 ++i;
251 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800252 payload_size = LOGGER_ENTRY_MAX_PAYLOAD;
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700253 break;
254 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800255 }
Mark Salyzynd91ab582014-12-15 10:52:12 -0800256 pmsg_header.len += payload_size;
257
258 if (pstore_fd >= 0) {
259 TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i));
260 }
261
262 if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */
263 /*
264 * ignore log messages we send to ourself (logd).
265 * Such log messages are often generated by libraries we depend on
266 * which use standard Android logging.
267 */
268 return 0;
269 }
270
271 if (logd_fd < 0) {
272 return -EBADF;
273 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800274
Mark Salyzyn8245af12014-03-25 13:45:33 -0700275 /*
276 * The write below could be lost, but will never block.
277 *
Mark Salyzynd91ab582014-12-15 10:52:12 -0800278 * To logd, we drop the pmsg_header
279 *
Mark Salyzyn8245af12014-03-25 13:45:33 -0700280 * ENOTCONN occurs if logd dies.
281 * EAGAIN occurs if logd is overloaded.
282 */
Mark Salyzynd91ab582014-12-15 10:52:12 -0800283 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700284 if (ret < 0) {
285 ret = -errno;
286 if (ret == -ENOTCONN) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800287#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700288 pthread_mutex_lock(&log_init_lock);
289#endif
290 ret = __write_to_log_initialize();
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800291#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700292 pthread_mutex_unlock(&log_init_lock);
293#endif
294
295 if (ret < 0) {
296 return ret;
297 }
298
Mark Salyzynd91ab582014-12-15 10:52:12 -0800299 ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1));
Mark Salyzyn8245af12014-03-25 13:45:33 -0700300 if (ret < 0) {
301 ret = -errno;
302 }
303 }
304 }
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700305
Mark Salyzyn7a809402015-01-16 13:38:07 -0800306 if (ret > (ssize_t)sizeof(header)) {
307 ret -= sizeof(header);
Mark Salyzynd45d36e2015-02-12 15:14:26 -0800308 } else if (ret == -EAGAIN) {
309 atomic_fetch_add_explicit(&dropped, 1, memory_order_relaxed);
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700310 }
Mark Salyzyn154f4602014-02-20 14:59:07 -0800311#endif
Mark Salyzyn8444eb82014-04-24 09:43:23 -0700312
313 return ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800314}
315
Mark Salyzyn154f4602014-02-20 14:59:07 -0800316#if FAKE_LOG_DEVICE
317static const char *LOG_NAME[LOG_ID_MAX] = {
318 [LOG_ID_MAIN] = "main",
319 [LOG_ID_RADIO] = "radio",
320 [LOG_ID_EVENTS] = "events",
Mark Salyzyn99f47a92014-04-07 14:58:08 -0700321 [LOG_ID_SYSTEM] = "system",
322 [LOG_ID_CRASH] = "crash"
Mark Salyzyn154f4602014-02-20 14:59:07 -0800323};
324
Adam Lesinskia1ac84c2014-10-20 12:31:30 -0700325const char *android_log_id_to_name(log_id_t log_id)
Mark Salyzyn154f4602014-02-20 14:59:07 -0800326{
327 if (log_id >= LOG_ID_MAX) {
328 log_id = LOG_ID_MAIN;
329 }
330 return LOG_NAME[log_id];
331}
332#endif
333
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800334static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr)
335{
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800336#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800337 pthread_mutex_lock(&log_init_lock);
338#endif
339
340 if (write_to_log == __write_to_log_init) {
Mark Salyzyn8245af12014-03-25 13:45:33 -0700341 int ret;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800342
Mark Salyzyn8245af12014-03-25 13:45:33 -0700343 ret = __write_to_log_initialize();
344 if (ret < 0) {
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800345#if !defined(_WIN32)
Mark Salyzyn8245af12014-03-25 13:45:33 -0700346 pthread_mutex_unlock(&log_init_lock);
Mark Salyzyn154f4602014-02-20 14:59:07 -0800347#endif
Mark Salyzyn8245af12014-03-25 13:45:33 -0700348 return ret;
349 }
350
Mark Salyzyn53016d82015-02-04 12:28:47 -0800351 write_to_log = __write_to_log_daemon;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800352 }
353
Yabin Cui4a6e5a32015-01-26 19:48:54 -0800354#if !defined(_WIN32)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 pthread_mutex_unlock(&log_init_lock);
356#endif
357
358 return write_to_log(log_id, vec, nr);
359}
360
361int __android_log_write(int prio, const char *tag, const char *msg)
362{
363 struct iovec vec[3];
364 log_id_t log_id = LOG_ID_MAIN;
Wink Saville3761e962012-11-28 12:20:19 -0800365 char tmp_tag[32];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800366
367 if (!tag)
368 tag = "";
369
370 /* XXX: This needs to go! */
371 if (!strcmp(tag, "HTC_RIL") ||
John Michelaued7ccae2009-08-19 10:01:55 -0500372 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700373 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800374 !strcmp(tag, "AT") ||
375 !strcmp(tag, "GSM") ||
Wink Saville89efdc92009-04-02 11:00:57 -0700376 !strcmp(tag, "STK") ||
377 !strcmp(tag, "CDMA") ||
378 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800379 !strcmp(tag, "SMS")) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 log_id = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700381 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800382 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
383 tag = tmp_tag;
384 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800385
Elliott Hughes26864bf2014-05-06 20:40:15 -0700386#if __BIONIC__
387 if (prio == ANDROID_LOG_FATAL) {
Dan Albertc68fedc2014-08-18 17:29:34 -0700388 android_set_abort_message(msg);
Elliott Hughes26864bf2014-05-06 20:40:15 -0700389 }
390#endif
391
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800392 vec[0].iov_base = (unsigned char *) &prio;
393 vec[0].iov_len = 1;
394 vec[1].iov_base = (void *) tag;
395 vec[1].iov_len = strlen(tag) + 1;
396 vec[2].iov_base = (void *) msg;
397 vec[2].iov_len = strlen(msg) + 1;
398
399 return write_to_log(log_id, vec, 3);
400}
401
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800402int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg)
403{
404 struct iovec vec[3];
Wink Saville3761e962012-11-28 12:20:19 -0800405 char tmp_tag[32];
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800406
407 if (!tag)
408 tag = "";
409
410 /* XXX: This needs to go! */
Wink Saville3761e962012-11-28 12:20:19 -0800411 if ((bufID != LOG_ID_RADIO) &&
412 (!strcmp(tag, "HTC_RIL") ||
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800413 !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */
Jeff Sharkey84dcf092012-08-13 11:27:30 -0700414 !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800415 !strcmp(tag, "AT") ||
416 !strcmp(tag, "GSM") ||
417 !strcmp(tag, "STK") ||
418 !strcmp(tag, "CDMA") ||
419 !strcmp(tag, "PHONE") ||
Wink Saville3761e962012-11-28 12:20:19 -0800420 !strcmp(tag, "SMS"))) {
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800421 bufID = LOG_ID_RADIO;
Mark Salyzyn8245af12014-03-25 13:45:33 -0700422 /* Inform third party apps/ril/radio.. to use Rlog or RLOG */
Wink Saville3761e962012-11-28 12:20:19 -0800423 snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag);
424 tag = tmp_tag;
425 }
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800426
427 vec[0].iov_base = (unsigned char *) &prio;
428 vec[0].iov_len = 1;
429 vec[1].iov_base = (void *) tag;
430 vec[1].iov_len = strlen(tag) + 1;
431 vec[2].iov_base = (void *) msg;
432 vec[2].iov_len = strlen(msg) + 1;
433
434 return write_to_log(bufID, vec, 3);
435}
436
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800437int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap)
438{
Chris Pearson19299902010-06-02 16:25:35 -0700439 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800440
441 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
442
443 return __android_log_write(prio, tag, buf);
444}
445
446int __android_log_print(int prio, const char *tag, const char *fmt, ...)
447{
448 va_list ap;
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800449 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800450
451 va_start(ap, fmt);
452 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
453 va_end(ap);
454
455 return __android_log_write(prio, tag, buf);
456}
457
Joe Onoratoe2bf2ea2010-03-01 09:11:54 -0800458int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...)
459{
460 va_list ap;
461 char buf[LOG_BUF_SIZE];
462
463 va_start(ap, fmt);
464 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
465 va_end(ap);
466
467 return __android_log_buf_write(bufID, prio, tag, buf);
468}
469
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800470void __android_log_assert(const char *cond, const char *tag,
Mark Salyzyncf4aa032013-11-22 07:54:30 -0800471 const char *fmt, ...)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800472{
Chris Pearson19299902010-06-02 16:25:35 -0700473 char buf[LOG_BUF_SIZE];
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800474
Chris Pearson19299902010-06-02 16:25:35 -0700475 if (fmt) {
476 va_list ap;
477 va_start(ap, fmt);
478 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
479 va_end(ap);
480 } else {
481 /* Msg not provided, log condition. N.B. Do not use cond directly as
482 * format string as it could contain spurious '%' syntax (e.g.
483 * "%d" in "blocks%devs == 0").
484 */
485 if (cond)
486 snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond);
487 else
488 strcpy(buf, "Unspecified assertion failed");
489 }
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800490
491 __android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 __builtin_trap(); /* trap so we have a chance to debug the situation */
Elliott Hughesda6b2e22014-04-23 14:57:32 -0700493 /* NOTREACHED */
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800494}
495
496int __android_log_bwrite(int32_t tag, const void *payload, size_t len)
497{
498 struct iovec vec[2];
499
500 vec[0].iov_base = &tag;
501 vec[0].iov_len = sizeof(tag);
502 vec[1].iov_base = (void*)payload;
503 vec[1].iov_len = len;
504
505 return write_to_log(LOG_ID_EVENTS, vec, 2);
506}
507
508/*
509 * Like __android_log_bwrite, but takes the type as well. Doesn't work
510 * for the general case where we're generating lists of stuff, but very
511 * handy if we just want to dump an integer into the log.
512 */
513int __android_log_btwrite(int32_t tag, char type, const void *payload,
Mark Salyzyn154f4602014-02-20 14:59:07 -0800514 size_t len)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800515{
516 struct iovec vec[3];
517
518 vec[0].iov_base = &tag;
519 vec[0].iov_len = sizeof(tag);
520 vec[1].iov_base = &type;
521 vec[1].iov_len = sizeof(type);
522 vec[2].iov_base = (void*)payload;
523 vec[2].iov_len = len;
524
525 return write_to_log(LOG_ID_EVENTS, vec, 3);
526}
Nick Kralevich2a4d05a2014-07-01 10:57:16 -0700527
528/*
529 * Like __android_log_bwrite, but used for writing strings to the
530 * event log.
531 */
532int __android_log_bswrite(int32_t tag, const char *payload)
533{
534 struct iovec vec[4];
535 char type = EVENT_TYPE_STRING;
536 uint32_t len = strlen(payload);
537
538 vec[0].iov_base = &tag;
539 vec[0].iov_len = sizeof(tag);
540 vec[1].iov_base = &type;
541 vec[1].iov_len = sizeof(type);
542 vec[2].iov_base = &len;
543 vec[2].iov_len = sizeof(len);
544 vec[3].iov_base = (void*)payload;
545 vec[3].iov_len = len;
546
547 return write_to_log(LOG_ID_EVENTS, vec, 4);
548}