blob: 11c0e68b609cb777b807ce3380fa6e9f3192e04d [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28#include <time.h>
29#include <stdio.h>
30#include <pthread.h>
31#include <unistd.h>
32#include <sys/types.h>
33#include <sys/uio.h>
34#include <arpa/inet.h>
35#include <errno.h>
36#include <string.h>
37#include <stdlib.h>
38#include <stdarg.h>
39#include <fcntl.h>
40
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#include "logd.h"
42
David 'Digit' Turner6c8a2f22010-06-10 23:34:24 -070043/* should match system/core/include/cutils/logger.h */
44#define LOGGER_LOG_MAIN "log/main"
45#define LOGGER_LOG_RADIO "log/radio"
46#define LOGGER_LOG_EVENTS "log/events"
47#define LOGGER_LOG_SYSTEM "log/system"
48
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080049#include <pthread.h>
50
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -070051/* IMPORTANT IMPORTANT IMPORTANT: TECHNICAL NOTE
52 *
53 * Some of the functions below can be called when our malloc() implementation
54 * has detected that the heap is corrupted, or even from a signal handler.
55 *
56 * These functions should *not* use a function that allocates heap memory
57 * or is not signal-safe. Using direct system calls is acceptable, and we
58 * also assume that pthread_mutex_lock/unlock can be used too.
59 */
60
David 'Digit' Turner6c8a2f22010-06-10 23:34:24 -070061#define LOG_BUF_SIZE 1024
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062
63typedef enum {
Alexey Tarasovcc05d122008-12-13 08:24:44 +100064 LOG_ID_NONE = 0,
65 LOG_ID_MAIN,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066 LOG_ID_RADIO,
Geremy Condra8b11c4c2012-06-07 17:45:06 -070067 LOG_ID_EVENTS,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068 LOG_ID_MAX
69} log_id_t;
70
Alexey Tarasovcc05d122008-12-13 08:24:44 +100071/* logger handles writing to object, pointed by log channel id */
72typedef int (*logger_function_t)(log_id_t log_id, struct iovec *vec);
73
74typedef struct {
75 logger_function_t logger;
76 int fd;
77 const char *path;
78} log_channel_t;
79
80static int __write_to_log_init(log_id_t log_id, struct iovec *vec);
81static int __write_to_log_null(log_id_t log_id, struct iovec *vec);
82
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER;
84
Andy McFadden5cdb2b72009-11-30 17:09:45 -080085static log_channel_t log_channels[LOG_ID_MAX] = {
Alexey Tarasovcc05d122008-12-13 08:24:44 +100086 { __write_to_log_null, -1, NULL },
87 { __write_to_log_init, -1, "/dev/"LOGGER_LOG_MAIN },
Geremy Condra8b11c4c2012-06-07 17:45:06 -070088 { __write_to_log_init, -1, "/dev/"LOGGER_LOG_RADIO },
89 { __write_to_log_init, -1, "/dev/"LOGGER_LOG_EVENTS }
Alexey Tarasovcc05d122008-12-13 08:24:44 +100090};
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -070092/* Important: see technical note at start of source file */
Alexey Tarasovcc05d122008-12-13 08:24:44 +100093static int __write_to_log_null(log_id_t log_id, struct iovec *vec)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094{
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -070095 /*
Alexey Tarasovcc05d122008-12-13 08:24:44 +100096 * ALTERED behaviour from previous version
97 * always returns successful result
98 */
99 int i = 0;
100 size_t res = 0;
101
102 for ( ; i < 3; ++i) {
103 res += vec[i].iov_len;
104 }
105
106 return (int)res;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800107}
108
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000109/*
110 * it's supposed, that log_id contains valid id always.
111 * this check must be performed in higher level functions
112 */
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700113/* Important: see technical note at start of source file */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec)
115{
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700116 return TEMP_FAILURE_RETRY( writev(log_channels[log_id].fd, vec, 3) );
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117}
118
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700119/* Important: see technical note at start of source file */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120static int __write_to_log_init(log_id_t log_id, struct iovec *vec)
121{
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000122 if ((LOG_ID_NONE < log_id) && (log_id < LOG_ID_MAX)) {
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700123 int fd;
124
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000125 pthread_mutex_lock(&log_init_lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700127 fd = TEMP_FAILURE_RETRY(open(log_channels[log_id].path, O_WRONLY));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000129 log_channels[log_id].logger =
130 (fd < 0) ? __write_to_log_null : __write_to_log_kernel;
Andy McFadden5cdb2b72009-11-30 17:09:45 -0800131 log_channels[log_id].fd = fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800132
Alexey Tarasovc22da7e2009-12-03 11:26:22 +1000133 log_channels[log_id].fd = fd;
134
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000135 pthread_mutex_unlock(&log_init_lock);
136
137 return log_channels[log_id].logger(log_id, vec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138 }
139
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000140 /* log_id is invalid */
141 return -1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142}
143
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700144/* Important: see technical note at start of source file */
145__LIBC_HIDDEN__
146int __libc_android_log_write(int prio, const char *tag, const char *msg)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147{
148 struct iovec vec[3];
149 log_id_t log_id = LOG_ID_MAIN;
150
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000151 if (tag == NULL)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152 tag = "";
153
154 if (!strcmp(tag, "HTC_RIL"))
155 log_id = LOG_ID_RADIO;
156
157 vec[0].iov_base = (unsigned char *) &prio;
158 vec[0].iov_len = 1;
159 vec[1].iov_base = (void *) tag;
160 vec[1].iov_len = strlen(tag) + 1;
161 vec[2].iov_base = (void *) msg;
162 vec[2].iov_len = strlen(msg) + 1;
163
Alexey Tarasovcc05d122008-12-13 08:24:44 +1000164 return log_channels[log_id].logger(log_id, vec);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165}
166
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700167/* The functions below are not designed to be called from a heap panic
168 * function or from a signal handler. As such, they are free to use complex
169 * C library functions like vsnprintf()
170 */
171__LIBC_HIDDEN__
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200172int __libc_android_log_vprint(int prio, const char *tag, const char *fmt,
173 va_list ap)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800174{
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200175 char buf[LOG_BUF_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
177 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
178
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700179 return __libc_android_log_write(prio, tag, buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180}
181
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700182__LIBC_HIDDEN__
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183int __libc_android_log_print(int prio, const char *tag, const char *fmt, ...)
184{
185 va_list ap;
David 'Digit' Turnerc4eee372009-07-08 14:22:41 +0200186 char buf[LOG_BUF_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
188 va_start(ap, fmt);
189 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
190 va_end(ap);
191
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700192 return __libc_android_log_write(prio, tag, buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193}
194
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700195__LIBC_HIDDEN__
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196int __libc_android_log_assert(const char *cond, const char *tag,
197 const char *fmt, ...)
198{
199 va_list ap;
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700200 char buf[LOG_BUF_SIZE];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
202 va_start(ap, fmt);
203 vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
204 va_end(ap);
205
Jean-Baptiste Queruc9937682011-07-06 12:58:56 -0700206 __libc_android_log_write(ANDROID_LOG_FATAL, tag, buf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207
208 exit(1);
209
210 return -1;
211}
Geremy Condra8b11c4c2012-06-07 17:45:06 -0700212
213/*
214 * Event logging.
215 */
216
217// must be kept in sync with frameworks/base/core/java/android/util/EventLog.java
218typedef enum {
219 EVENT_TYPE_INT = 0,
220 EVENT_TYPE_LONG = 1,
221 EVENT_TYPE_STRING = 2,
222 EVENT_TYPE_LIST = 3,
223} AndroidEventLogType;
224
225static int __libc_android_log_btwrite(int32_t tag, char type, const void *payload, size_t len)
226{
227 struct iovec vec[3];
228
229 vec[0].iov_base = &tag;
230 vec[0].iov_len = sizeof(tag);
231 vec[1].iov_base = &type;
232 vec[1].iov_len = sizeof(type);
233 vec[2].iov_base = (void*)payload;
234 vec[2].iov_len = len;
235
236 return log_channels[LOG_ID_EVENTS].logger(LOG_ID_EVENTS, vec);
237}
238
239__LIBC_HIDDEN__
240void __libc_android_log_event_int(int32_t tag, int value)
241{
242 __libc_android_log_btwrite(tag, EVENT_TYPE_INT, &value, sizeof(value));
243}
244
245__LIBC_HIDDEN__
246void __libc_android_log_event_uid(int32_t tag)
247{
248 __libc_android_log_event_int(tag, getuid());
249}
Nick Kralevich326ea542012-12-04 13:55:19 -0800250
251__LIBC_HIDDEN__
252void __fortify_chk_fail(const char *msg, uint32_t tag) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800253 __libc_format_log(ANDROID_LOG_FATAL, "libc", "FORTIFY_SOURCE: %s. Calling abort().\n", msg);
Nick Kralevich326ea542012-12-04 13:55:19 -0800254 if (tag != 0) {
255 __libc_android_log_event_uid(tag);
256 }
257 abort();
258}