The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | e9c4196 | 2014-01-02 13:52:29 -0800 | [diff] [blame] | 2 | * Copyright (C) 2007-2014 The Android Open Source Project |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | */ |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 18 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 19 | #include <pthread.h> |
| 20 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 21 | #include <stdarg.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
Nick Kralevich | a170322 | 2013-03-15 09:45:12 -0700 | [diff] [blame] | 25 | #include <sys/stat.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 26 | #include <sys/types.h> |
| 27 | #if (FAKE_LOG_DEVICE == 0) |
| 28 | #include <sys/socket.h> |
| 29 | #include <sys/un.h> |
| 30 | #endif |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 33 | |
Dan Albert | c68fedc | 2014-08-18 17:29:34 -0700 | [diff] [blame] | 34 | #ifdef __BIONIC__ |
| 35 | #include <android/set_abort_message.h> |
| 36 | #endif |
| 37 | |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 38 | #include <log/logd.h> |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 39 | #include <log/logger.h> |
| 40 | #include <log/log_read.h> |
| 41 | #include <private/android_filesystem_config.h> |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 42 | #include <private/android_logger.h> |
Mark Salyzyn | e9c4196 | 2014-01-02 13:52:29 -0800 | [diff] [blame] | 43 | |
Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 44 | #define LOG_BUF_SIZE 1024 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 45 | |
| 46 | #if FAKE_LOG_DEVICE |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 47 | /* This will be defined when building for the host. */ |
Kristian Monsen | b5a9890 | 2014-01-28 11:26:56 -0800 | [diff] [blame] | 48 | #include "fake_log_device.h" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 49 | #endif |
| 50 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 51 | static int __write_to_log_init(log_id_t, struct iovec *vec, size_t nr); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 52 | 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] | 53 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | static pthread_mutex_t log_init_lock = PTHREAD_MUTEX_INITIALIZER; |
| 55 | #endif |
| 56 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 57 | #ifndef __unused |
| 58 | #define __unused __attribute__((__unused__)) |
| 59 | #endif |
Kristian Monsen | b5a9890 | 2014-01-28 11:26:56 -0800 | [diff] [blame] | 60 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 61 | #if FAKE_LOG_DEVICE |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 62 | static int log_fds[(int)LOG_ID_MAX] = { -1, -1, -1, -1, -1 }; |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 63 | #else |
| 64 | static int logd_fd = -1; |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 65 | static int pstore_fd = -1; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 66 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * This is used by the C++ code to decide if it should write logs through |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 70 | * the C code. Basically, if /dev/socket/logd is available, we're running in |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 71 | * the simulator rather than a desktop tool and want to use the device. |
| 72 | */ |
| 73 | static enum { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 74 | kLogUninitialized, kLogNotAvailable, kLogAvailable |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | } g_log_status = kLogUninitialized; |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 76 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | int __android_log_dev_available(void) |
| 78 | { |
| 79 | if (g_log_status == kLogUninitialized) { |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 80 | if (access("/dev/socket/logdw", W_OK) == 0) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 81 | g_log_status = kLogAvailable; |
| 82 | else |
| 83 | g_log_status = kLogNotAvailable; |
| 84 | } |
| 85 | |
| 86 | return (g_log_status == kLogAvailable); |
| 87 | } |
| 88 | |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 89 | #if !FAKE_LOG_DEVICE |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 90 | /* give up, resources too limited */ |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 91 | static int __write_to_log_null(log_id_t log_fd __unused, struct iovec *vec __unused, |
| 92 | size_t nr __unused) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 93 | { |
| 94 | return -1; |
| 95 | } |
Mark Salyzyn | a04464a | 2014-04-30 08:50:53 -0700 | [diff] [blame] | 96 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 97 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 98 | /* log_init_lock assumed */ |
| 99 | static int __write_to_log_initialize() |
| 100 | { |
| 101 | int i, ret = 0; |
| 102 | |
| 103 | #if FAKE_LOG_DEVICE |
| 104 | for (i = 0; i < LOG_ID_MAX; i++) { |
| 105 | char buf[sizeof("/dev/log_system")]; |
| 106 | snprintf(buf, sizeof(buf), "/dev/log_%s", android_log_id_to_name(i)); |
| 107 | log_fds[i] = fakeLogOpen(buf, O_WRONLY); |
| 108 | } |
| 109 | #else |
| 110 | if (logd_fd >= 0) { |
| 111 | i = logd_fd; |
| 112 | logd_fd = -1; |
| 113 | close(i); |
| 114 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 115 | if (pstore_fd >= 0) { |
| 116 | i = pstore_fd; |
| 117 | pstore_fd = -1; |
| 118 | close(i); |
| 119 | } |
| 120 | pstore_fd = open("/dev/pmsg0", O_WRONLY); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 121 | |
Nick Kralevich | 118d1b3 | 2014-07-02 22:30:39 -0700 | [diff] [blame] | 122 | i = socket(PF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 123 | if (i < 0) { |
| 124 | ret = -errno; |
| 125 | write_to_log = __write_to_log_null; |
| 126 | } else if (fcntl(i, F_SETFL, O_NONBLOCK) < 0) { |
| 127 | ret = -errno; |
| 128 | close(i); |
| 129 | i = -1; |
| 130 | write_to_log = __write_to_log_null; |
| 131 | } else { |
| 132 | struct sockaddr_un un; |
| 133 | memset(&un, 0, sizeof(struct sockaddr_un)); |
| 134 | un.sun_family = AF_UNIX; |
| 135 | strcpy(un.sun_path, "/dev/socket/logdw"); |
| 136 | |
| 137 | if (connect(i, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) < 0) { |
| 138 | ret = -errno; |
| 139 | close(i); |
| 140 | i = -1; |
| 141 | } |
| 142 | } |
| 143 | logd_fd = i; |
| 144 | #endif |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 149 | static int __write_to_log_daemon(log_id_t log_id, struct iovec *vec, size_t nr) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | { |
| 151 | ssize_t ret; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 152 | #if FAKE_LOG_DEVICE |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 153 | int log_fd; |
| 154 | |
| 155 | if (/*(int)log_id >= 0 &&*/ (int)log_id < (int)LOG_ID_MAX) { |
| 156 | log_fd = log_fds[(int)log_id]; |
| 157 | } else { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 158 | return -EBADF; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 159 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 160 | do { |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 161 | ret = fakeLogWritev(log_fd, vec, nr); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 162 | if (ret < 0) { |
| 163 | ret = -errno; |
| 164 | } |
| 165 | } while (ret == -EINTR); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 166 | #else |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 167 | static const unsigned header_length = 2; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 168 | struct iovec newVec[nr + header_length]; |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 169 | android_log_header_t header; |
| 170 | android_pmsg_log_header_t pmsg_header; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 171 | struct timespec ts; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 172 | size_t i, payload_size; |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 173 | static uid_t last_uid = AID_ROOT; /* logd *always* starts up as AID_ROOT */ |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 174 | static pid_t last_pid = (pid_t) -1; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 175 | |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 176 | if (last_uid == AID_ROOT) { /* have we called to get the UID yet? */ |
| 177 | last_uid = getuid(); |
| 178 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 179 | if (last_pid == (pid_t) -1) { |
| 180 | last_pid = getpid(); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 181 | } |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 182 | /* |
| 183 | * struct { |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 184 | * // what we provide to pstore |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 185 | * android_pmsg_log_header_t pmsg_header; |
| 186 | * // what we provide to socket |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 187 | * android_log_header_t header; |
Mark Salyzyn | b992d0d | 2014-03-20 16:09:38 -0700 | [diff] [blame] | 188 | * // caller provides |
| 189 | * union { |
| 190 | * struct { |
| 191 | * char prio; |
| 192 | * char payload[]; |
| 193 | * } string; |
| 194 | * struct { |
| 195 | * uint32_t tag |
| 196 | * char payload[]; |
| 197 | * } binary; |
| 198 | * }; |
| 199 | * }; |
| 200 | */ |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 201 | |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 202 | clock_gettime(CLOCK_REALTIME, &ts); |
Mark Salyzyn | 076ba81 | 2014-05-29 17:53:41 -0700 | [diff] [blame] | 203 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 204 | pmsg_header.magic = LOGGER_MAGIC; |
| 205 | pmsg_header.len = sizeof(pmsg_header) + sizeof(header); |
| 206 | pmsg_header.uid = last_uid; |
| 207 | pmsg_header.pid = last_pid; |
| 208 | |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 209 | header.id = log_id; |
| 210 | header.tid = gettid(); |
| 211 | header.realtime.tv_sec = ts.tv_sec; |
| 212 | header.realtime.tv_nsec = ts.tv_nsec; |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 213 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 214 | newVec[0].iov_base = (unsigned char *) &pmsg_header; |
| 215 | newVec[0].iov_len = sizeof(pmsg_header); |
| 216 | newVec[1].iov_base = (unsigned char *) &header; |
| 217 | newVec[1].iov_len = sizeof(header); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 218 | |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 219 | for (payload_size = 0, i = header_length; i < nr + header_length; i++) { |
| 220 | newVec[i].iov_base = vec[i - header_length].iov_base; |
| 221 | payload_size += newVec[i].iov_len = vec[i - header_length].iov_len; |
| 222 | |
| 223 | if (payload_size > LOGGER_ENTRY_MAX_PAYLOAD) { |
| 224 | newVec[i].iov_len -= payload_size - LOGGER_ENTRY_MAX_PAYLOAD; |
| 225 | if (newVec[i].iov_len) { |
| 226 | ++i; |
| 227 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 228 | payload_size = LOGGER_ENTRY_MAX_PAYLOAD; |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 229 | break; |
| 230 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 231 | } |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 232 | pmsg_header.len += payload_size; |
| 233 | |
| 234 | if (pstore_fd >= 0) { |
| 235 | TEMP_FAILURE_RETRY(writev(pstore_fd, newVec, i)); |
| 236 | } |
| 237 | |
| 238 | if (last_uid == AID_LOGD) { /* logd, after initialization and priv drop */ |
| 239 | /* |
| 240 | * ignore log messages we send to ourself (logd). |
| 241 | * Such log messages are often generated by libraries we depend on |
| 242 | * which use standard Android logging. |
| 243 | */ |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | if (logd_fd < 0) { |
| 248 | return -EBADF; |
| 249 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 250 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 251 | /* |
| 252 | * The write below could be lost, but will never block. |
| 253 | * |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 254 | * To logd, we drop the pmsg_header |
| 255 | * |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 256 | * ENOTCONN occurs if logd dies. |
| 257 | * EAGAIN occurs if logd is overloaded. |
| 258 | */ |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 259 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 260 | if (ret < 0) { |
| 261 | ret = -errno; |
| 262 | if (ret == -ENOTCONN) { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 263 | #if !defined(_WIN32) |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 264 | pthread_mutex_lock(&log_init_lock); |
| 265 | #endif |
| 266 | ret = __write_to_log_initialize(); |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 267 | #if !defined(_WIN32) |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 268 | pthread_mutex_unlock(&log_init_lock); |
| 269 | #endif |
| 270 | |
| 271 | if (ret < 0) { |
| 272 | return ret; |
| 273 | } |
| 274 | |
Mark Salyzyn | d91ab58 | 2014-12-15 10:52:12 -0800 | [diff] [blame] | 275 | ret = TEMP_FAILURE_RETRY(writev(logd_fd, newVec + 1, i - 1)); |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 276 | if (ret < 0) { |
| 277 | ret = -errno; |
| 278 | } |
| 279 | } |
| 280 | } |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 281 | |
Mark Salyzyn | 7a80940 | 2015-01-16 13:38:07 -0800 | [diff] [blame] | 282 | if (ret > (ssize_t)sizeof(header)) { |
| 283 | ret -= sizeof(header); |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 284 | } |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 285 | #endif |
Mark Salyzyn | 8444eb8 | 2014-04-24 09:43:23 -0700 | [diff] [blame] | 286 | |
| 287 | return ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 288 | } |
| 289 | |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 290 | #if FAKE_LOG_DEVICE |
| 291 | static const char *LOG_NAME[LOG_ID_MAX] = { |
| 292 | [LOG_ID_MAIN] = "main", |
| 293 | [LOG_ID_RADIO] = "radio", |
| 294 | [LOG_ID_EVENTS] = "events", |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 295 | [LOG_ID_SYSTEM] = "system", |
| 296 | [LOG_ID_CRASH] = "crash" |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 297 | }; |
| 298 | |
Adam Lesinski | a1ac84c | 2014-10-20 12:31:30 -0700 | [diff] [blame] | 299 | const char *android_log_id_to_name(log_id_t log_id) |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 300 | { |
| 301 | if (log_id >= LOG_ID_MAX) { |
| 302 | log_id = LOG_ID_MAIN; |
| 303 | } |
| 304 | return LOG_NAME[log_id]; |
| 305 | } |
| 306 | #endif |
| 307 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 308 | static int __write_to_log_init(log_id_t log_id, struct iovec *vec, size_t nr) |
| 309 | { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 310 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 311 | pthread_mutex_lock(&log_init_lock); |
| 312 | #endif |
| 313 | |
| 314 | if (write_to_log == __write_to_log_init) { |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 315 | int ret; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 316 | |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 317 | ret = __write_to_log_initialize(); |
| 318 | if (ret < 0) { |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 319 | #if !defined(_WIN32) |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 320 | pthread_mutex_unlock(&log_init_lock); |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 321 | #endif |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 322 | return ret; |
| 323 | } |
| 324 | |
Mark Salyzyn | 53016d8 | 2015-02-04 12:28:47 -0800 | [diff] [blame] | 325 | write_to_log = __write_to_log_daemon; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Yabin Cui | 4a6e5a3 | 2015-01-26 19:48:54 -0800 | [diff] [blame] | 328 | #if !defined(_WIN32) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 329 | pthread_mutex_unlock(&log_init_lock); |
| 330 | #endif |
| 331 | |
| 332 | return write_to_log(log_id, vec, nr); |
| 333 | } |
| 334 | |
| 335 | int __android_log_write(int prio, const char *tag, const char *msg) |
| 336 | { |
| 337 | struct iovec vec[3]; |
| 338 | log_id_t log_id = LOG_ID_MAIN; |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 339 | char tmp_tag[32]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | |
| 341 | if (!tag) |
| 342 | tag = ""; |
| 343 | |
| 344 | /* XXX: This needs to go! */ |
| 345 | if (!strcmp(tag, "HTC_RIL") || |
John Michelau | ed7ccae | 2009-08-19 10:01:55 -0500 | [diff] [blame] | 346 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
Jeff Sharkey | 84dcf09 | 2012-08-13 11:27:30 -0700 | [diff] [blame] | 347 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 348 | !strcmp(tag, "AT") || |
| 349 | !strcmp(tag, "GSM") || |
Wink Saville | 89efdc9 | 2009-04-02 11:00:57 -0700 | [diff] [blame] | 350 | !strcmp(tag, "STK") || |
| 351 | !strcmp(tag, "CDMA") || |
| 352 | !strcmp(tag, "PHONE") || |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 353 | !strcmp(tag, "SMS")) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 354 | log_id = LOG_ID_RADIO; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 355 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 356 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 357 | tag = tmp_tag; |
| 358 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 359 | |
Elliott Hughes | 26864bf | 2014-05-06 20:40:15 -0700 | [diff] [blame] | 360 | #if __BIONIC__ |
| 361 | if (prio == ANDROID_LOG_FATAL) { |
Dan Albert | c68fedc | 2014-08-18 17:29:34 -0700 | [diff] [blame] | 362 | android_set_abort_message(msg); |
Elliott Hughes | 26864bf | 2014-05-06 20:40:15 -0700 | [diff] [blame] | 363 | } |
| 364 | #endif |
| 365 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 366 | vec[0].iov_base = (unsigned char *) &prio; |
| 367 | vec[0].iov_len = 1; |
| 368 | vec[1].iov_base = (void *) tag; |
| 369 | vec[1].iov_len = strlen(tag) + 1; |
| 370 | vec[2].iov_base = (void *) msg; |
| 371 | vec[2].iov_len = strlen(msg) + 1; |
| 372 | |
| 373 | return write_to_log(log_id, vec, 3); |
| 374 | } |
| 375 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 376 | int __android_log_buf_write(int bufID, int prio, const char *tag, const char *msg) |
| 377 | { |
| 378 | struct iovec vec[3]; |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 379 | char tmp_tag[32]; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 380 | |
| 381 | if (!tag) |
| 382 | tag = ""; |
| 383 | |
| 384 | /* XXX: This needs to go! */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 385 | if ((bufID != LOG_ID_RADIO) && |
| 386 | (!strcmp(tag, "HTC_RIL") || |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 387 | !strncmp(tag, "RIL", 3) || /* Any log tag with "RIL" as the prefix */ |
Jeff Sharkey | 84dcf09 | 2012-08-13 11:27:30 -0700 | [diff] [blame] | 388 | !strncmp(tag, "IMS", 3) || /* Any log tag with "IMS" as the prefix */ |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 389 | !strcmp(tag, "AT") || |
| 390 | !strcmp(tag, "GSM") || |
| 391 | !strcmp(tag, "STK") || |
| 392 | !strcmp(tag, "CDMA") || |
| 393 | !strcmp(tag, "PHONE") || |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 394 | !strcmp(tag, "SMS"))) { |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 395 | bufID = LOG_ID_RADIO; |
Mark Salyzyn | 8245af1 | 2014-03-25 13:45:33 -0700 | [diff] [blame] | 396 | /* Inform third party apps/ril/radio.. to use Rlog or RLOG */ |
Wink Saville | 3761e96 | 2012-11-28 12:20:19 -0800 | [diff] [blame] | 397 | snprintf(tmp_tag, sizeof(tmp_tag), "use-Rlog/RLOG-%s", tag); |
| 398 | tag = tmp_tag; |
| 399 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 400 | |
| 401 | vec[0].iov_base = (unsigned char *) &prio; |
| 402 | vec[0].iov_len = 1; |
| 403 | vec[1].iov_base = (void *) tag; |
| 404 | vec[1].iov_len = strlen(tag) + 1; |
| 405 | vec[2].iov_base = (void *) msg; |
| 406 | vec[2].iov_len = strlen(msg) + 1; |
| 407 | |
| 408 | return write_to_log(bufID, vec, 3); |
| 409 | } |
| 410 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 411 | int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap) |
| 412 | { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 413 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 414 | |
| 415 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 416 | |
| 417 | return __android_log_write(prio, tag, buf); |
| 418 | } |
| 419 | |
| 420 | int __android_log_print(int prio, const char *tag, const char *fmt, ...) |
| 421 | { |
| 422 | va_list ap; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 423 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | |
| 425 | va_start(ap, fmt); |
| 426 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 427 | va_end(ap); |
| 428 | |
| 429 | return __android_log_write(prio, tag, buf); |
| 430 | } |
| 431 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 432 | int __android_log_buf_print(int bufID, int prio, const char *tag, const char *fmt, ...) |
| 433 | { |
| 434 | va_list ap; |
| 435 | char buf[LOG_BUF_SIZE]; |
| 436 | |
| 437 | va_start(ap, fmt); |
| 438 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 439 | va_end(ap); |
| 440 | |
| 441 | return __android_log_buf_write(bufID, prio, tag, buf); |
| 442 | } |
| 443 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 444 | void __android_log_assert(const char *cond, const char *tag, |
Mark Salyzyn | cf4aa03 | 2013-11-22 07:54:30 -0800 | [diff] [blame] | 445 | const char *fmt, ...) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 446 | { |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 447 | char buf[LOG_BUF_SIZE]; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 448 | |
Chris Pearson | 1929990 | 2010-06-02 16:25:35 -0700 | [diff] [blame] | 449 | if (fmt) { |
| 450 | va_list ap; |
| 451 | va_start(ap, fmt); |
| 452 | vsnprintf(buf, LOG_BUF_SIZE, fmt, ap); |
| 453 | va_end(ap); |
| 454 | } else { |
| 455 | /* Msg not provided, log condition. N.B. Do not use cond directly as |
| 456 | * format string as it could contain spurious '%' syntax (e.g. |
| 457 | * "%d" in "blocks%devs == 0"). |
| 458 | */ |
| 459 | if (cond) |
| 460 | snprintf(buf, LOG_BUF_SIZE, "Assertion failed: %s", cond); |
| 461 | else |
| 462 | strcpy(buf, "Unspecified assertion failed"); |
| 463 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 464 | |
| 465 | __android_log_write(ANDROID_LOG_FATAL, tag, buf); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 466 | __builtin_trap(); /* trap so we have a chance to debug the situation */ |
Elliott Hughes | da6b2e2 | 2014-04-23 14:57:32 -0700 | [diff] [blame] | 467 | /* NOTREACHED */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | int __android_log_bwrite(int32_t tag, const void *payload, size_t len) |
| 471 | { |
| 472 | struct iovec vec[2]; |
| 473 | |
| 474 | vec[0].iov_base = &tag; |
| 475 | vec[0].iov_len = sizeof(tag); |
| 476 | vec[1].iov_base = (void*)payload; |
| 477 | vec[1].iov_len = len; |
| 478 | |
| 479 | return write_to_log(LOG_ID_EVENTS, vec, 2); |
| 480 | } |
| 481 | |
| 482 | /* |
| 483 | * Like __android_log_bwrite, but takes the type as well. Doesn't work |
| 484 | * for the general case where we're generating lists of stuff, but very |
| 485 | * handy if we just want to dump an integer into the log. |
| 486 | */ |
| 487 | int __android_log_btwrite(int32_t tag, char type, const void *payload, |
Mark Salyzyn | 154f460 | 2014-02-20 14:59:07 -0800 | [diff] [blame] | 488 | size_t len) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 489 | { |
| 490 | struct iovec vec[3]; |
| 491 | |
| 492 | vec[0].iov_base = &tag; |
| 493 | vec[0].iov_len = sizeof(tag); |
| 494 | vec[1].iov_base = &type; |
| 495 | vec[1].iov_len = sizeof(type); |
| 496 | vec[2].iov_base = (void*)payload; |
| 497 | vec[2].iov_len = len; |
| 498 | |
| 499 | return write_to_log(LOG_ID_EVENTS, vec, 3); |
| 500 | } |
Nick Kralevich | 2a4d05a | 2014-07-01 10:57:16 -0700 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * Like __android_log_bwrite, but used for writing strings to the |
| 504 | * event log. |
| 505 | */ |
| 506 | int __android_log_bswrite(int32_t tag, const char *payload) |
| 507 | { |
| 508 | struct iovec vec[4]; |
| 509 | char type = EVENT_TYPE_STRING; |
| 510 | uint32_t len = strlen(payload); |
| 511 | |
| 512 | vec[0].iov_base = &tag; |
| 513 | vec[0].iov_len = sizeof(tag); |
| 514 | vec[1].iov_base = &type; |
| 515 | vec[1].iov_len = sizeof(type); |
| 516 | vec[2].iov_base = &len; |
| 517 | vec[2].iov_len = sizeof(len); |
| 518 | vec[3].iov_base = (void*)payload; |
| 519 | vec[3].iov_len = len; |
| 520 | |
| 521 | return write_to_log(LOG_ID_EVENTS, vec, 4); |
| 522 | } |