Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007-2014 The Android Open Source Project |
| 3 | * |
| 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 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #ifdef HAVE_PTHREADS |
| 20 | #include <pthread.h> |
| 21 | #endif |
| 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <time.h> |
| 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <log/log.h> |
| 32 | #include <log/logd.h> |
| 33 | #include <log/logger.h> |
| 34 | |
| 35 | #define LOGGER_LOG_MAIN "log/main" |
| 36 | #define LOGGER_LOG_RADIO "log/radio" |
| 37 | #define LOGGER_LOG_EVENTS "log/events" |
| 38 | #define LOGGER_LOG_SYSTEM "log/system" |
| 39 | |
| 40 | #define LOG_BUF_SIZE 1024 |
| 41 | |
| 42 | #if FAKE_LOG_DEVICE |
| 43 | /* This will be defined when building for the host. */ |
| 44 | #include "fake_log_device.h" |
| 45 | #define log_open(pathname, flags) fakeLogOpen(pathname, flags) |
| 46 | #define log_writev(filedes, vector, count) fakeLogWritev(filedes, vector, count) |
| 47 | #define log_close(filedes) fakeLogClose(filedes) |
| 48 | #else |
| 49 | #define log_open(pathname, flags) open(pathname, (flags) | O_CLOEXEC) |
| 50 | #define log_writev(filedes, vector, count) writev(filedes, vector, count) |
| 51 | #define log_close(filedes) close(filedes) |
| 52 | #endif |
| 53 | |
| 54 | static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr); |
| 55 | static int (*write_to_log)(log_id_t, struct iovec *vec, size_t nr) = __write_to_log_init; |
| 56 | #ifdef HAVE_PTHREADS |
| 57 | static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER; |
| 58 | #endif |
| 59 | |
| 60 | #define UNUSED __attribute__((__unused__)) |
| 61 | |
| 62 | static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1 }; |
| 63 | |
| 64 | /* |
| 65 | * This is used by the C++ code to decide if it should write logs through |
| 66 | * the C code. Basically, if /dev/log/... is available, we're running in |
| 67 | * the simulator rather than a desktop tool and want to use the device. |
| 68 | */ |
| 69 | static enum { |
| 70 | kLogUninitialized, kLogNotAvailable, kLogAvailable |
| 71 | } g_log_status = kLogUninitialized; |
| 72 | int __android_log_dev_available(void) |
| 73 | { |
| 74 | if (g_log_status == kLogUninitialized) { |
| 75 | if (access("/dev/"LOGGER_LOG_MAIN, W_OK) == 0) |
| 76 | g_log_status = kLogAvailable; |
| 77 | else |
| 78 | g_log_status = kLogNotAvailable; |
| 79 | } |
| 80 | |
| 81 | return (g_log_status == kLogAvailable); |
| 82 | } |
| 83 | |
| 84 | static int __write_to_log_null(log_id_t log_fd UNUSED, struct iovec *vec UNUSED, |
| 85 | size_t nr UNUSED) |
| 86 | { |
| 87 | return -1; |
| 88 | } |
| 89 | |
| 90 | static int __write_to_log_kernel(log_id_t log_id, struct iovec *vec, size_t nr) |
| 91 | { |
| 92 | ssize_t ret; |
| 93 | int log_fd; |
| 94 | |
| 95 | if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) { |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 96 | if (log_id == LOG_ID_CRASH) { |
| 97 | log_id = LOG_ID_MAIN; |
| 98 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 99 | log_fd = log_fds[(int)log_id]; |
| 100 | } else { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 101 | return -EBADF; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | do { |
| 105 | ret = log_writev(log_fd, vec, nr); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 106 | if (ret < 0) { |
| 107 | ret = -errno; |
| 108 | } |
| 109 | } while (ret == -EINTR); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
| 115 | { |
| 116 | #ifdef HAVE_PTHREADS |
| 117 | pthread_mutex_lock(&log_init_lock); |
| 118 | #endif |
| 119 | |
| 120 | if (write_to_log == __write_to_log_init) { |
| 121 | log_fds[LOG_ID_MAIN] = log_open("/dev/"LOGGER_LOG_MAIN, O_WRONLY); |
| 122 | log_fds[LOG_ID_RADIO] = log_open("/dev/"LOGGER_LOG_RADIO, O_WRONLY); |
| 123 | log_fds[LOG_ID_EVENTS] = log_open("/dev/"LOGGER_LOG_EVENTS, O_WRONLY); |
| 124 | log_fds[LOG_ID_SYSTEM] = log_open("/dev/"LOGGER_LOG_SYSTEM, O_WRONLY); |
| 125 | |
| 126 | write_to_log = __write_to_log_kernel; |
| 127 | |
| 128 | if (log_fds[LOG_ID_MAIN] < 0 || log_fds[LOG_ID_RADIO] < 0 || |
| 129 | log_fds[LOG_ID_EVENTS] < 0) { |
| 130 | log_close(log_fds[LOG_ID_MAIN]); |
| 131 | log_close(log_fds[LOG_ID_RADIO]); |
| 132 | log_close(log_fds[LOG_ID_EVENTS]); |
| 133 | log_fds[LOG_ID_MAIN] = -1; |
| 134 | log_fds[LOG_ID_RADIO] = -1; |
| 135 | log_fds[LOG_ID_EVENTS] = -1; |
| 136 | write_to_log = __write_to_log_null; |
| 137 | } |
| 138 | |
| 139 | if (log_fds[LOG_ID_SYSTEM] < 0) { |
| 140 | log_fds[LOG_ID_SYSTEM] = log_fds[LOG_ID_MAIN]; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | #ifdef HAVE_PTHREADS |
| 145 | pthread_mutex_unlock(&log_init_lock); |
| 146 | #endif |
| 147 | |
| 148 | return write_to_log(log_id, vec, nr); |
| 149 | } |
| 150 | |
| 151 | int __android_log_write(int prio, const char *tag, const char *msg) |
| 152 | { |
| 153 | struct iovec vec[3]; |
| 154 | log_id_t log_id = LOG_ID_MAIN; |
| 155 | char tmp_tag[32]; |
| 156 | |
| 157 | if (!tag) |
| 158 | tag = ""; |
| 159 | |
| 160 | /* XXX: This needs to go! */ |
| 161 | if (!strcmp(tag, "HTC_RIL") || |
| 162 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
| 163 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
| 164 | !strcmp(tag, "AT") || |
| 165 | !strcmp(tag, "GSM") || |
| 166 | !strcmp(tag, "STK") || |
| 167 | !strcmp(tag, "CDMA") || |
| 168 | !strcmp(tag, "PHONE") || |
| 169 | !strcmp(tag, "SMS")) { |
| 170 | log_id = LOG_ID_RADIO; |
| 171 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
| 172 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 173 | tag = tmp_tag; |
| 174 | } |
| 175 | |
Elliott Hughes | 26864bf | 2014-05-06 20:40:15 -0700 | [diff] [blame^] | 176 | #if __BIONIC__ |
| 177 | if (prio == ANDROID_LOG_FATAL) { |
| 178 | extern void __android_set_abort_message(const char*); |
| 179 | __android_set_abort_message(msg); |
| 180 | } |
| 181 | #endif |
| 182 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 183 | vec[0].iov_base = (unsigned char *) &prio; |
| 184 | vec[0].iov_len = 1; |
| 185 | vec[1].iov_base = (void *) tag; |
| 186 | vec[1].iov_len = strlen(tag) + 1; |
| 187 | vec[2].iov_base = (void *) msg; |
| 188 | vec[2].iov_len = strlen(msg) + 1; |
| 189 | |
| 190 | return write_to_log(log_id, vec, 3); |
| 191 | } |
| 192 | |
| 193 | int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg) |
| 194 | { |
| 195 | struct iovec vec[3]; |
| 196 | char tmp_tag[32]; |
| 197 | |
| 198 | if (!tag) |
| 199 | tag = ""; |
| 200 | |
| 201 | /* XXX: This needs to go! */ |
| 202 | if ((bufID != LOG_ID_RADIO) && |
| 203 | (!strcmp(tag, "HTC_RIL") || |
| 204 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
| 205 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
| 206 | !strcmp(tag, "AT") || |
| 207 | !strcmp(tag, "GSM") || |
| 208 | !strcmp(tag, "STK") || |
| 209 | !strcmp(tag, "CDMA") || |
| 210 | !strcmp(tag, "PHONE") || |
| 211 | !strcmp(tag, "SMS"))) { |
| 212 | bufID = LOG_ID_RADIO; |
| 213 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
| 214 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 215 | tag = tmp_tag; |
| 216 | } |
| 217 | |
| 218 | vec[0].iov_base = (unsigned char *) &prio; |
| 219 | vec[0].iov_len = 1; |
| 220 | vec[1].iov_base = (void *) tag; |
| 221 | vec[1].iov_len = strlen(tag) + 1; |
| 222 | vec[2].iov_base = (void *) msg; |
| 223 | vec[2].iov_len = strlen(msg) + 1; |
| 224 | |
| 225 | return write_to_log(bufID, vec, 3); |
| 226 | } |
| 227 | |
| 228 | int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap) |
| 229 | { |
| 230 | char buf[LOG_BUF_SIZE]; |
| 231 | |
| 232 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 233 | |
| 234 | return __android_log_write(prio, tag, buf); |
| 235 | } |
| 236 | |
| 237 | int __android_log_print(int prio, const char *tag, const char *fmt, ...) |
| 238 | { |
| 239 | va_list ap; |
| 240 | char buf[LOG_BUF_SIZE]; |
| 241 | |
| 242 | va_start(ap, fmt); |
| 243 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 244 | va_end(ap); |
| 245 | |
| 246 | return __android_log_write(prio, tag, buf); |
| 247 | } |
| 248 | |
| 249 | int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...) |
| 250 | { |
| 251 | va_list ap; |
| 252 | char buf[LOG_BUF_SIZE]; |
| 253 | |
| 254 | va_start(ap, fmt); |
| 255 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 256 | va_end(ap); |
| 257 | |
| 258 | return __android_log_buf_write(bufID, prio, tag, buf); |
| 259 | } |
| 260 | |
| 261 | void __android_log_assert(const char *cond, const char *tag, |
| 262 | const char *fmt, ...) |
| 263 | { |
| 264 | char buf[LOG_BUF_SIZE]; |
| 265 | |
| 266 | if (fmt) { |
| 267 | va_list ap; |
| 268 | va_start(ap, fmt); |
| 269 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 270 | va_end(ap); |
| 271 | } else { |
| 272 | /* Msg not provided, log condition. N.B. Do not use cond directly as |
| 273 | * format string as it could contain spurious '%' syntax (e.g. |
| 274 | * "%d" in "blocks%devs == 0"). |
| 275 | */ |
| 276 | if (cond) |
| 277 | snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond); |
| 278 | else |
| 279 | strcpy(buf, "Unspecified assertion failed"); |
| 280 | } |
| 281 | |
| 282 | __android_log_write(ANDROID_LOG_FATAL, tag, buf); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 283 | __builtin_trap(); /* trap so we have a chance to debug the situation */ |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 284 | /* NOTREACHED */ |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int __android_log_bwrite(int32_t tag, const void *payload, size_t len) |
| 288 | { |
| 289 | struct iovec vec[2]; |
| 290 | |
| 291 | vec[0].iov_base = &tag; |
| 292 | vec[0].iov_len = sizeof(tag); |
| 293 | vec[1].iov_base = (void*)payload; |
| 294 | vec[1].iov_len = len; |
| 295 | |
| 296 | return write_to_log(LOG_ID_EVENTS, vec, 2); |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Like __android_log_bwrite, but takes the type as well. Doesn't work |
| 301 | * for the general case where we're generating lists of stuff, but very |
| 302 | * handy if we just want to dump an integer into the log. |
| 303 | */ |
| 304 | int __android_log_btwrite(int32_t tag, char type, const void *payload, |
| 305 | size_t len) |
| 306 | { |
| 307 | struct iovec vec[3]; |
| 308 | |
| 309 | vec[0].iov_base = &tag; |
| 310 | vec[0].iov_len = sizeof(tag); |
| 311 | vec[1].iov_base = &type; |
| 312 | vec[1].iov_len = sizeof(type); |
| 313 | vec[2].iov_base = (void*)payload; |
| 314 | vec[2].iov_len = len; |
| 315 | |
| 316 | return write_to_log(LOG_ID_EVENTS, vec, 3); |
| 317 | } |