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