Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1 | // Copyright 2006-2015 The Android Open Source Project |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 3 | #include <arpa/inet.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 4 | #include <assert.h> |
| 5 | #include <ctype.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 6 | #include <dirent.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 9 | #include <getopt.h> |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 10 | #include <math.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 11 | #include <sched.h> |
| 12 | #include <signal.h> |
| 13 | #include <stdarg.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 16 | #include <string.h> |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 17 | #include <sys/cdefs.h> |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 18 | #include <sys/resource.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 21 | #include <sys/types.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 22 | #include <time.h> |
| 23 | #include <unistd.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 24 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <string> |
| 27 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 28 | #include <android-base/file.h> |
| 29 | #include <android-base/strings.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 30 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 31 | #include <cutils/sockets.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 32 | #include <log/event_tag_map.h> |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 33 | #include <log/log.h> |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 34 | #include <log/log_read.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 35 | #include <log/logd.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 36 | #include <log/logger.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 37 | #include <log/logprint.h> |
Elliott Hughes | b9e53b4 | 2016-02-17 11:58:01 -0800 | [diff] [blame^] | 38 | #include <system/thread_defs.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 39 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 41 | |
| 42 | static AndroidLogFormat * g_logformat; |
| 43 | |
| 44 | /* logd prefixes records with a length field */ |
| 45 | #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t) |
| 46 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 47 | struct log_device_t { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 48 | const char* device; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 49 | bool binary; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 50 | struct logger *logger; |
| 51 | struct logger_list *logger_list; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 52 | bool printed; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 53 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 54 | log_device_t* next; |
| 55 | |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 56 | log_device_t(const char* d, bool b) { |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 57 | device = d; |
| 58 | binary = b; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 59 | next = NULL; |
| 60 | printed = false; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 61 | logger = NULL; |
| 62 | logger_list = NULL; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 63 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 64 | }; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 65 | |
| 66 | namespace android { |
| 67 | |
| 68 | /* Global Variables */ |
| 69 | |
| 70 | static const char * g_outputFileName = NULL; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 71 | // 0 means "no log rotation" |
| 72 | static size_t g_logRotateSizeKBytes = 0; |
| 73 | // 0 means "unbounded" |
| 74 | static size_t g_maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 75 | static int g_outFD = -1; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 76 | static size_t g_outByteCount = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 77 | static int g_printBinary = 0; |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 78 | static int g_devCount = 0; // >1 means multiple |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 79 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 80 | __noreturn static void logcat_panic(bool showHelp, const char *fmt, ...) __printflike(2,3); |
| 81 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 82 | static int openLogFile (const char *pathname) |
| 83 | { |
Edwin Vane | 80b221c | 2012-08-13 12:55:07 -0400 | [diff] [blame] | 84 | return open(pathname, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static void rotateLogs() |
| 88 | { |
| 89 | int err; |
| 90 | |
| 91 | // Can't rotate logs if we're not outputting to a file |
| 92 | if (g_outputFileName == NULL) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | close(g_outFD); |
| 97 | |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 98 | // Compute the maximum number of digits needed to count up to g_maxRotatedLogs in decimal. |
| 99 | // eg: g_maxRotatedLogs == 30 -> log10(30) == 1.477 -> maxRotationCountDigits == 2 |
| 100 | int maxRotationCountDigits = |
| 101 | (g_maxRotatedLogs > 0) ? (int) (floor(log10(g_maxRotatedLogs) + 1)) : 0; |
| 102 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 103 | for (int i = g_maxRotatedLogs ; i > 0 ; i--) { |
| 104 | char *file0, *file1; |
| 105 | |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 106 | asprintf(&file1, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 107 | |
| 108 | if (i - 1 == 0) { |
| 109 | asprintf(&file0, "%s", g_outputFileName); |
| 110 | } else { |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 111 | asprintf(&file0, "%s.%.*d", g_outputFileName, maxRotationCountDigits, i - 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 112 | } |
| 113 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 114 | if (!file0 || !file1) { |
| 115 | perror("while rotating log files"); |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | err = rename(file0, file1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 120 | |
| 121 | if (err < 0 && errno != ENOENT) { |
| 122 | perror("while rotating log files"); |
| 123 | } |
| 124 | |
| 125 | free(file1); |
| 126 | free(file0); |
| 127 | } |
| 128 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 129 | g_outFD = openLogFile(g_outputFileName); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 130 | |
| 131 | if (g_outFD < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 132 | logcat_panic(false, "couldn't open output file"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | g_outByteCount = 0; |
| 136 | |
| 137 | } |
| 138 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 139 | void printBinary(struct log_msg *buf) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 140 | { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 141 | size_t size = buf->len(); |
| 142 | |
| 143 | TEMP_FAILURE_RETRY(write(g_outFD, buf, size)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 146 | static void processBuffer(log_device_t* dev, struct log_msg *buf) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 147 | { |
Mathias Agopian | 5084452 | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 148 | int bytesWritten = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 149 | int err; |
| 150 | AndroidLogEntry entry; |
| 151 | char binaryMsgBuf[1024]; |
| 152 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 153 | if (dev->binary) { |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 154 | static bool hasOpenedEventTagMap = false; |
| 155 | static EventTagMap *eventTagMap = NULL; |
| 156 | |
| 157 | if (!eventTagMap && !hasOpenedEventTagMap) { |
| 158 | eventTagMap = android_openEventTagMap(EVENT_TAG_MAP_FILE); |
| 159 | hasOpenedEventTagMap = true; |
| 160 | } |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 161 | err = android_log_processBinaryLogBuffer(&buf->entry_v1, &entry, |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 162 | eventTagMap, |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 163 | binaryMsgBuf, |
| 164 | sizeof(binaryMsgBuf)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 165 | //printf(">>> pri=%d len=%d msg='%s'\n", |
| 166 | // entry.priority, entry.messageLen, entry.message); |
| 167 | } else { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 168 | err = android_log_processLogBuffer(&buf->entry_v1, &entry); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 169 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 170 | if (err < 0) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 171 | goto error; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 172 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 173 | |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 174 | if (android_log_shouldPrintLine(g_logformat, entry.tag, entry.priority)) { |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 175 | bytesWritten = android_log_printLogLine(g_logformat, g_outFD, &entry); |
| 176 | |
| 177 | if (bytesWritten < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 178 | logcat_panic(false, "output error"); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 179 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | g_outByteCount += bytesWritten; |
| 183 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 184 | if (g_logRotateSizeKBytes > 0 |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 185 | && (g_outByteCount / 1024) >= g_logRotateSizeKBytes |
| 186 | ) { |
| 187 | rotateLogs(); |
| 188 | } |
| 189 | |
| 190 | error: |
| 191 | //fprintf (stderr, "Error processing record\n"); |
| 192 | return; |
| 193 | } |
| 194 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 195 | static void maybePrintStart(log_device_t* dev, bool printDividers) { |
| 196 | if (!dev->printed || printDividers) { |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 197 | if (g_devCount > 1 && !g_printBinary) { |
| 198 | char buf[1024]; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 199 | snprintf(buf, sizeof(buf), "--------- %s %s\n", |
| 200 | dev->printed ? "switch to" : "beginning of", |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 201 | dev->device); |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 202 | if (write(g_outFD, buf, strlen(buf)) < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 203 | logcat_panic(false, "output error"); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 206 | dev->printed = true; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 210 | static void setupOutput() |
| 211 | { |
| 212 | |
| 213 | if (g_outputFileName == NULL) { |
| 214 | g_outFD = STDOUT_FILENO; |
| 215 | |
| 216 | } else { |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 217 | if (set_sched_policy(0, SP_BACKGROUND) < 0) { |
| 218 | fprintf(stderr, "failed to set background scheduling policy\n"); |
| 219 | } |
| 220 | |
| 221 | struct sched_param param; |
| 222 | memset(¶m, 0, sizeof(param)); |
| 223 | if (sched_setscheduler((pid_t) 0, SCHED_BATCH, ¶m) < 0) { |
| 224 | fprintf(stderr, "failed to set to batch scheduler\n"); |
| 225 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 226 | |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 227 | if (setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) { |
| 228 | fprintf(stderr, "failed set to priority\n"); |
| 229 | } |
| 230 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 231 | g_outFD = openLogFile (g_outputFileName); |
| 232 | |
| 233 | if (g_outFD < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 234 | logcat_panic(false, "couldn't open output file"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 237 | struct stat statbuf; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 238 | if (fstat(g_outFD, &statbuf) == -1) { |
| 239 | close(g_outFD); |
| 240 | logcat_panic(false, "couldn't get output file stat\n"); |
| 241 | } |
| 242 | |
| 243 | if ((size_t) statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) { |
| 244 | close(g_outFD); |
| 245 | logcat_panic(false, "invalid output file stat\n"); |
| 246 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 247 | |
| 248 | g_outByteCount = statbuf.st_size; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | static void show_help(const char *cmd) |
| 253 | { |
| 254 | fprintf(stderr,"Usage: %s [options] [filterspecs]\n", cmd); |
| 255 | |
| 256 | fprintf(stderr, "options include:\n" |
| 257 | " -s Set default filter to silent.\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 258 | " Like specifying filterspec '*:S'\n" |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 259 | " -f <filename> Log to file. Default is stdout\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 260 | " --file=<filename>\n" |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 261 | " -r <kbytes> Rotate log every kbytes. Requires -f\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 262 | " --rotate_kbytes=<kbytes>\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | " -n <count> Sets max number of rotated logs to <count>, default 4\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 264 | " --rotate_count=<count>\n" |
| 265 | " -v <format> Sets the log print format, where <format> is:\n" |
| 266 | " --format=<format>\n" |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 267 | " brief color epoch long monotonic printable process raw\n" |
Mark Salyzyn | 90e7af3 | 2015-12-07 16:52:42 -0800 | [diff] [blame] | 268 | " tag thread threadtime time uid usec UTC year zone\n\n" |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 269 | " -D print dividers between each log buffer\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 270 | " --dividers\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | " -c clear (flush) the entire log and exit\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 272 | " --clear\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 273 | " -d dump the log and then exit (don't block)\n" |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 274 | " -t <count> print only the most recent <count> lines (implies -d)\n" |
Mark Salyzyn | 190b7ac | 2014-09-01 10:41:33 -0700 | [diff] [blame] | 275 | " -t '<time>' print most recent lines since specified time (implies -d)\n" |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 276 | " -T <count> print only the most recent <count> lines (does not imply -d)\n" |
Mark Salyzyn | 190b7ac | 2014-09-01 10:41:33 -0700 | [diff] [blame] | 277 | " -T '<time>' print most recent lines since specified time (not imply -d)\n" |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 278 | " count is pure numerical, time is 'MM-DD hh:mm:ss.mmm...'\n" |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 279 | " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 280 | " -g get the size of the log's ring buffer and exit\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 281 | " --buffer_size\n" |
| 282 | " -G <size> set size of log ring buffer, may suffix with K or M.\n" |
| 283 | " --buffer_size=<size>\n" |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 284 | " -L dump logs from prior to last reboot\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 285 | " --last\n" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 286 | // Leave security (Device Owner only installations) and |
| 287 | // kernel (userdebug and eng) buffers undocumented. |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 288 | " -b <buffer> Request alternate ring buffer, 'main', 'system', 'radio',\n" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 289 | " --buffer=<buffer> 'events', 'crash', 'default' or 'all'. Multiple -b\n" |
| 290 | " parameters are allowed and results are interleaved. The\n" |
| 291 | " default is -b main -b system -b crash.\n" |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 292 | " -B output the log in binary.\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 293 | " --binary\n" |
Mark Salyzyn | bbbe14f | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 294 | " -S output statistics.\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 295 | " --statistics\n" |
Mark Salyzyn | bbbe14f | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 296 | " -p print prune white and ~black list. Service is specified as\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 297 | " --prune UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n" |
Mark Salyzyn | bbbe14f | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 298 | " with ~, otherwise weighed for longevity if unadorned. All\n" |
| 299 | " other pruning activity is oldest first. Special case ~!\n" |
| 300 | " represents an automatic quicker pruning for the noisiest\n" |
| 301 | " UID as determined by the current statistics.\n" |
| 302 | " -P '<list> ...' set prune white and ~black list, using same format as\n" |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 303 | " --prune='<list> ...' printed above. Must be quoted.\n" |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 304 | " --pid=<pid> Only prints logs from the given pid.\n" |
| 305 | // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value |
| 306 | " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n" |
| 307 | " comes first. Improves efficiency of polling by providing\n" |
| 308 | " an about-to-wrap wakeup.\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 309 | |
| 310 | fprintf(stderr,"\nfilterspecs are a series of \n" |
| 311 | " <tag>[:priority]\n\n" |
| 312 | "where <tag> is a log component tag (or * for all) and priority is:\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 313 | " V Verbose (default for <tag>)\n" |
| 314 | " D Debug (default for '*')\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | " I Info\n" |
| 316 | " W Warn\n" |
| 317 | " E Error\n" |
| 318 | " F Fatal\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 319 | " S Silent (suppress all output)\n" |
| 320 | "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n" |
| 321 | "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n" |
| 322 | "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n" |
| 323 | "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 324 | "\nIf not specified with -v on command line, format is set from ANDROID_PRINTF_LOG\n" |
Mark Salyzyn | 649fc60 | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 325 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 326 | } |
| 327 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | static int setLogFormat(const char * formatString) |
| 329 | { |
| 330 | static AndroidLogPrintFormat format; |
| 331 | |
| 332 | format = android_log_formatFromString(formatString); |
| 333 | |
| 334 | if (format == FORMAT_OFF) { |
| 335 | // FORMAT_OFF means invalid string |
| 336 | return -1; |
| 337 | } |
| 338 | |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 339 | return android_log_setPrintFormat(g_logformat, format); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 342 | static const char multipliers[][2] = { |
| 343 | { "" }, |
| 344 | { "K" }, |
| 345 | { "M" }, |
| 346 | { "G" } |
| 347 | }; |
| 348 | |
| 349 | static unsigned long value_of_size(unsigned long value) |
| 350 | { |
| 351 | for (unsigned i = 0; |
| 352 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 353 | value /= 1024, ++i) ; |
| 354 | return value; |
| 355 | } |
| 356 | |
| 357 | static const char *multiplier_of_size(unsigned long value) |
| 358 | { |
| 359 | unsigned i; |
| 360 | for (i = 0; |
| 361 | (i < sizeof(multipliers)/sizeof(multipliers[0])) && (value >= 1024); |
| 362 | value /= 1024, ++i) ; |
| 363 | return multipliers[i]; |
| 364 | } |
| 365 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 366 | /*String to unsigned int, returns -1 if it fails*/ |
| 367 | static bool getSizeTArg(char *ptr, size_t *val, size_t min = 0, |
| 368 | size_t max = SIZE_MAX) |
| 369 | { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 370 | if (!ptr) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 374 | char *endp; |
| 375 | errno = 0; |
| 376 | size_t ret = (size_t)strtoll(ptr, &endp, 0); |
| 377 | |
| 378 | if (endp[0] || errno) { |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | if ((ret > max) || (ret < min)) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 383 | return false; |
| 384 | } |
| 385 | |
| 386 | *val = ret; |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | static void logcat_panic(bool showHelp, const char *fmt, ...) |
| 391 | { |
Mark Salyzyn | 77d7e81 | 2015-04-13 09:27:57 -0700 | [diff] [blame] | 392 | va_list args; |
| 393 | va_start(args, fmt); |
| 394 | vfprintf(stderr, fmt, args); |
| 395 | va_end(args); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 396 | |
| 397 | if (showHelp) { |
| 398 | show_help(getprogname()); |
| 399 | } |
| 400 | |
| 401 | exit(EXIT_FAILURE); |
| 402 | } |
| 403 | |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 404 | static char *parseTime(log_time &t, const char *cp) { |
| 405 | |
| 406 | char *ep = t.strptime(cp, "%m-%d %H:%M:%S.%q"); |
| 407 | if (ep) { |
| 408 | return ep; |
| 409 | } |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 410 | ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q"); |
| 411 | if (ep) { |
| 412 | return ep; |
| 413 | } |
| 414 | return t.strptime(cp, "%s.%q"); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 415 | } |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 416 | |
| 417 | // Find last logged line in gestalt of all matching existing output files |
| 418 | static log_time lastLogTime(char *outputFileName) { |
| 419 | log_time retval(log_time::EPOCH); |
| 420 | if (!outputFileName) { |
| 421 | return retval; |
| 422 | } |
| 423 | |
Mark Salyzyn | ba7a9a0 | 2015-12-01 15:57:25 -0800 | [diff] [blame] | 424 | clockid_t clock_type = android_log_clockid(); |
| 425 | log_time now(clock_type); |
| 426 | bool monotonic = clock_type == CLOCK_MONOTONIC; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 427 | |
| 428 | std::string directory; |
| 429 | char *file = strrchr(outputFileName, '/'); |
| 430 | if (!file) { |
| 431 | directory = "."; |
| 432 | file = outputFileName; |
| 433 | } else { |
| 434 | *file = '\0'; |
| 435 | directory = outputFileName; |
| 436 | *file = '/'; |
| 437 | ++file; |
| 438 | } |
| 439 | size_t len = strlen(file); |
| 440 | log_time modulo(0, NS_PER_SEC); |
| 441 | std::unique_ptr<DIR, int(*)(DIR*)>dir(opendir(directory.c_str()), closedir); |
| 442 | struct dirent *dp; |
| 443 | while ((dp = readdir(dir.get())) != NULL) { |
| 444 | if ((dp->d_type != DT_REG) |
Mark Salyzyn | b6bee33 | 2015-09-08 08:56:32 -0700 | [diff] [blame] | 445 | // If we are using realtime, check all files that match the |
| 446 | // basename for latest time. If we are using monotonic time |
| 447 | // then only check the main file because time cycles on |
| 448 | // every reboot. |
| 449 | || strncmp(dp->d_name, file, len + monotonic) |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 450 | || (dp->d_name[len] |
| 451 | && ((dp->d_name[len] != '.') |
| 452 | || !isdigit(dp->d_name[len+1])))) { |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | std::string file_name = directory; |
| 457 | file_name += "/"; |
| 458 | file_name += dp->d_name; |
| 459 | std::string file; |
| 460 | if (!android::base::ReadFileToString(file_name, &file)) { |
| 461 | continue; |
| 462 | } |
| 463 | |
| 464 | bool found = false; |
| 465 | for (const auto& line : android::base::Split(file, "\n")) { |
| 466 | log_time t(log_time::EPOCH); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 467 | char *ep = parseTime(t, line.c_str()); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 468 | if (!ep || (*ep != ' ')) { |
| 469 | continue; |
| 470 | } |
| 471 | // determine the time precision of the logs (eg: msec or usec) |
| 472 | for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) { |
| 473 | if (t.tv_nsec % (mod * 10)) { |
| 474 | modulo.tv_nsec = mod; |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | // We filter any times later than current as we may not have the |
| 479 | // year stored with each log entry. Also, since it is possible for |
| 480 | // entries to be recorded out of order (very rare) we select the |
| 481 | // maximum we find just in case. |
| 482 | if ((t < now) && (t > retval)) { |
| 483 | retval = t; |
| 484 | found = true; |
| 485 | } |
| 486 | } |
| 487 | // We count on the basename file to be the definitive end, so stop here. |
| 488 | if (!dp->d_name[len] && found) { |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | if (retval == log_time::EPOCH) { |
| 493 | return retval; |
| 494 | } |
| 495 | // tail_time prints matching or higher, round up by the modulo to prevent |
| 496 | // a replay of the last entry we have just checked. |
| 497 | retval += modulo; |
| 498 | return retval; |
| 499 | } |
| 500 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 501 | } /* namespace android */ |
| 502 | |
| 503 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 504 | int main(int argc, char **argv) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 505 | { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 506 | using namespace android; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 507 | int err; |
| 508 | int hasSetLogFormat = 0; |
| 509 | int clearLog = 0; |
| 510 | int getLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 511 | unsigned long setLogSize = 0; |
| 512 | int getPruneList = 0; |
| 513 | char *setPruneList = NULL; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 514 | int printStatistics = 0; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 515 | int mode = ANDROID_LOG_RDONLY; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 516 | const char *forceFilters = NULL; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 517 | log_device_t* devices = NULL; |
| 518 | log_device_t* dev; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 519 | bool printDividers = false; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 520 | struct logger_list *logger_list; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 521 | size_t tail_lines = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 522 | log_time tail_time(log_time::EPOCH); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 523 | size_t pid = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 524 | |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 525 | signal(SIGPIPE, exit); |
| 526 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 527 | g_logformat = android_log_format_new(); |
| 528 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 529 | if (argc == 2 && 0 == strcmp(argv[1], "--help")) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 530 | show_help(argv[0]); |
| 531 | return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | for (;;) { |
| 535 | int ret; |
| 536 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 537 | int option_index = 0; |
| 538 | static const char pid_str[] = "pid"; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 539 | static const char wrap_str[] = "wrap"; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 540 | static const struct option long_options[] = { |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 541 | { "binary", no_argument, NULL, 'B' }, |
| 542 | { "buffer", required_argument, NULL, 'b' }, |
| 543 | { "buffer_size", optional_argument, NULL, 'g' }, |
| 544 | { "clear", no_argument, NULL, 'c' }, |
| 545 | { "dividers", no_argument, NULL, 'D' }, |
| 546 | { "file", required_argument, NULL, 'f' }, |
| 547 | { "format", required_argument, NULL, 'v' }, |
| 548 | { "last", no_argument, NULL, 'L' }, |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 549 | { pid_str, required_argument, NULL, 0 }, |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 550 | { "prune", optional_argument, NULL, 'p' }, |
| 551 | { "rotate_count", required_argument, NULL, 'n' }, |
| 552 | { "rotate_kbytes", required_argument, NULL, 'r' }, |
| 553 | { "statistics", no_argument, NULL, 'S' }, |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 554 | // support, but ignore and do not document, the optional argument |
| 555 | { wrap_str, optional_argument, NULL, 0 }, |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 556 | { NULL, 0, NULL, 0 } |
| 557 | }; |
| 558 | |
| 559 | ret = getopt_long(argc, argv, ":cdDLt:T:gG:sQf:r:n:v:b:BSpP:", |
| 560 | long_options, &option_index); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 561 | |
| 562 | if (ret < 0) { |
| 563 | break; |
| 564 | } |
| 565 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 566 | switch (ret) { |
| 567 | case 0: |
| 568 | // One of the long options |
| 569 | if (long_options[option_index].name == pid_str) { |
| 570 | // ToDo: determine runtime PID_MAX? |
| 571 | if (!getSizeTArg(optarg, &pid, 1)) { |
| 572 | logcat_panic(true, "%s %s out of range\n", |
| 573 | long_options[option_index].name, optarg); |
| 574 | } |
| 575 | break; |
| 576 | } |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 577 | if (long_options[option_index].name == wrap_str) { |
| 578 | mode |= ANDROID_LOG_WRAP | |
| 579 | ANDROID_LOG_RDONLY | |
| 580 | ANDROID_LOG_NONBLOCK; |
| 581 | // ToDo: implement API that supports setting a wrap timeout |
| 582 | size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT; |
| 583 | if (optarg && !getSizeTArg(optarg, &dummy, 1)) { |
| 584 | logcat_panic(true, "%s %s out of range\n", |
| 585 | long_options[option_index].name, optarg); |
| 586 | } |
| 587 | if (dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) { |
| 588 | fprintf(stderr, |
| 589 | "WARNING: %s %u seconds, ignoring %zu\n", |
| 590 | long_options[option_index].name, |
| 591 | ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy); |
| 592 | } |
| 593 | break; |
| 594 | } |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 595 | break; |
| 596 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 597 | case 's': |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 598 | // default to all silent |
| 599 | android_log_addFilterRule(g_logformat, "*:s"); |
| 600 | break; |
| 601 | |
| 602 | case 'c': |
| 603 | clearLog = 1; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 604 | mode |= ANDROID_LOG_WRONLY; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 605 | break; |
| 606 | |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 607 | case 'L': |
| 608 | mode |= ANDROID_LOG_PSTORE; |
| 609 | break; |
| 610 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 611 | case 'd': |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 612 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 613 | break; |
| 614 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 615 | case 't': |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 616 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 617 | /* FALLTHRU */ |
| 618 | case 'T': |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 619 | if (strspn(optarg, "0123456789") != strlen(optarg)) { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 620 | char *cp = parseTime(tail_time, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 621 | if (!cp) { |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 622 | logcat_panic(false, "-%c \"%s\" not in time format\n", |
| 623 | ret, optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 624 | } |
| 625 | if (*cp) { |
| 626 | char c = *cp; |
| 627 | *cp = '\0'; |
| 628 | fprintf(stderr, |
| 629 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
| 630 | ret, optarg, c, cp + 1); |
| 631 | *cp = c; |
| 632 | } |
| 633 | } else { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 634 | if (!getSizeTArg(optarg, &tail_lines, 1)) { |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 635 | fprintf(stderr, |
| 636 | "WARNING: -%c %s invalid, setting to 1\n", |
| 637 | ret, optarg); |
| 638 | tail_lines = 1; |
| 639 | } |
| 640 | } |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 641 | break; |
| 642 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 643 | case 'D': |
| 644 | printDividers = true; |
| 645 | break; |
| 646 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 647 | case 'g': |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 648 | if (!optarg) { |
| 649 | getLogSize = 1; |
| 650 | break; |
| 651 | } |
| 652 | // FALLTHRU |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 653 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 654 | case 'G': { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 655 | char *cp; |
| 656 | if (strtoll(optarg, &cp, 0) > 0) { |
| 657 | setLogSize = strtoll(optarg, &cp, 0); |
| 658 | } else { |
| 659 | setLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | switch(*cp) { |
| 663 | case 'g': |
| 664 | case 'G': |
| 665 | setLogSize *= 1024; |
| 666 | /* FALLTHRU */ |
| 667 | case 'm': |
| 668 | case 'M': |
| 669 | setLogSize *= 1024; |
| 670 | /* FALLTHRU */ |
| 671 | case 'k': |
| 672 | case 'K': |
| 673 | setLogSize *= 1024; |
| 674 | /* FALLTHRU */ |
| 675 | case '\0': |
| 676 | break; |
| 677 | |
| 678 | default: |
| 679 | setLogSize = 0; |
| 680 | } |
| 681 | |
| 682 | if (!setLogSize) { |
| 683 | fprintf(stderr, "ERROR: -G <num><multiplier>\n"); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 684 | return EXIT_FAILURE; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 685 | } |
| 686 | } |
| 687 | break; |
| 688 | |
| 689 | case 'p': |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 690 | if (!optarg) { |
| 691 | getPruneList = 1; |
| 692 | break; |
| 693 | } |
| 694 | // FALLTHRU |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 695 | |
| 696 | case 'P': |
| 697 | setPruneList = optarg; |
| 698 | break; |
| 699 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 700 | case 'b': { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 701 | if (strcmp(optarg, "default") == 0) { |
| 702 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 703 | switch (i) { |
| 704 | case LOG_ID_SECURITY: |
| 705 | case LOG_ID_EVENTS: |
| 706 | continue; |
| 707 | case LOG_ID_MAIN: |
| 708 | case LOG_ID_SYSTEM: |
| 709 | case LOG_ID_CRASH: |
| 710 | break; |
| 711 | default: |
| 712 | continue; |
| 713 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 714 | |
Mark Salyzyn | d0bd1b1 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 715 | const char *name = android_log_id_to_name((log_id_t)i); |
| 716 | log_id_t log_id = android_name_to_log_id(name); |
| 717 | |
| 718 | if (log_id != (log_id_t)i) { |
| 719 | continue; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 720 | } |
Mark Salyzyn | d0bd1b1 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 721 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 722 | bool found = false; |
| 723 | for (dev = devices; dev; dev = dev->next) { |
| 724 | if (!strcmp(optarg, dev->device)) { |
| 725 | found = true; |
| 726 | break; |
| 727 | } |
| 728 | if (!dev->next) { |
| 729 | break; |
| 730 | } |
| 731 | } |
| 732 | if (found) { |
| 733 | break; |
| 734 | } |
| 735 | |
| 736 | log_device_t* d = new log_device_t(name, false); |
| 737 | |
| 738 | if (dev) { |
| 739 | dev->next = d; |
| 740 | dev = d; |
| 741 | } else { |
| 742 | devices = dev = d; |
| 743 | } |
| 744 | g_devCount++; |
| 745 | } |
| 746 | break; |
| 747 | } |
| 748 | |
| 749 | if (strcmp(optarg, "all") == 0) { |
| 750 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 751 | const char *name = android_log_id_to_name((log_id_t)i); |
| 752 | log_id_t log_id = android_name_to_log_id(name); |
| 753 | |
| 754 | if (log_id != (log_id_t)i) { |
| 755 | continue; |
| 756 | } |
| 757 | |
| 758 | bool found = false; |
| 759 | for (dev = devices; dev; dev = dev->next) { |
| 760 | if (!strcmp(optarg, dev->device)) { |
| 761 | found = true; |
| 762 | break; |
| 763 | } |
| 764 | if (!dev->next) { |
| 765 | break; |
| 766 | } |
| 767 | } |
| 768 | if (found) { |
| 769 | break; |
| 770 | } |
| 771 | |
| 772 | bool binary = !strcmp(name, "events") || |
| 773 | !strcmp(name, "security"); |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 774 | log_device_t* d = new log_device_t(name, binary); |
Mark Salyzyn | d0bd1b1 | 2014-10-10 15:25:38 -0700 | [diff] [blame] | 775 | |
| 776 | if (dev) { |
| 777 | dev->next = d; |
| 778 | dev = d; |
| 779 | } else { |
| 780 | devices = dev = d; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 781 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 782 | g_devCount++; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 783 | } |
| 784 | break; |
| 785 | } |
| 786 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 787 | bool binary = !(strcmp(optarg, "events") && |
| 788 | strcmp(optarg, "security")); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 789 | |
| 790 | if (devices) { |
| 791 | dev = devices; |
| 792 | while (dev->next) { |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 793 | if (!strcmp(optarg, dev->device)) { |
| 794 | dev = NULL; |
| 795 | break; |
| 796 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 797 | dev = dev->next; |
| 798 | } |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 799 | if (dev) { |
| 800 | dev->next = new log_device_t(optarg, binary); |
| 801 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 802 | } else { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 803 | devices = new log_device_t(optarg, binary); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 804 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 805 | g_devCount++; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 806 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 807 | break; |
| 808 | |
| 809 | case 'B': |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 810 | g_printBinary = 1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 811 | break; |
| 812 | |
| 813 | case 'f': |
Mark Salyzyn | 9812fc4 | 2015-10-06 08:59:02 -0700 | [diff] [blame] | 814 | if ((tail_time == log_time::EPOCH) && (tail_lines == 0)) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 815 | tail_time = lastLogTime(optarg); |
| 816 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 817 | // redirect output to a file |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 818 | g_outputFileName = optarg; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 819 | break; |
| 820 | |
| 821 | case 'r': |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 822 | if (!getSizeTArg(optarg, &g_logRotateSizeKBytes, 1)) { |
| 823 | logcat_panic(true, "Invalid parameter %s to -r\n", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 824 | } |
| 825 | break; |
| 826 | |
| 827 | case 'n': |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 828 | if (!getSizeTArg(optarg, &g_maxRotatedLogs, 1)) { |
| 829 | logcat_panic(true, "Invalid parameter %s to -n\n", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 830 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 831 | break; |
| 832 | |
| 833 | case 'v': |
| 834 | err = setLogFormat (optarg); |
| 835 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 836 | logcat_panic(true, "Invalid parameter %s to -v\n", optarg); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 837 | } |
Mark Salyzyn | e1f2004 | 2015-05-06 08:40:40 -0700 | [diff] [blame] | 838 | hasSetLogFormat |= err; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 839 | break; |
| 840 | |
| 841 | case 'Q': |
| 842 | /* this is a *hidden* option used to start a version of logcat */ |
| 843 | /* in an emulated device only. it basically looks for androidboot.logcat= */ |
| 844 | /* on the kernel command line. If something is found, it extracts a log filter */ |
| 845 | /* and uses it to run the program. If nothing is found, the program should */ |
| 846 | /* quit immediately */ |
| 847 | #define KERNEL_OPTION "androidboot.logcat=" |
| 848 | #define CONSOLE_OPTION "androidboot.console=" |
| 849 | { |
| 850 | int fd; |
| 851 | char* logcat; |
| 852 | char* console; |
| 853 | int force_exit = 1; |
| 854 | static char cmdline[1024]; |
| 855 | |
| 856 | fd = open("/proc/cmdline", O_RDONLY); |
| 857 | if (fd >= 0) { |
| 858 | int n = read(fd, cmdline, sizeof(cmdline)-1 ); |
| 859 | if (n < 0) n = 0; |
| 860 | cmdline[n] = 0; |
| 861 | close(fd); |
| 862 | } else { |
| 863 | cmdline[0] = 0; |
| 864 | } |
| 865 | |
| 866 | logcat = strstr( cmdline, KERNEL_OPTION ); |
| 867 | console = strstr( cmdline, CONSOLE_OPTION ); |
| 868 | if (logcat != NULL) { |
| 869 | char* p = logcat + sizeof(KERNEL_OPTION)-1;; |
| 870 | char* q = strpbrk( p, " \t\n\r" );; |
| 871 | |
| 872 | if (q != NULL) |
| 873 | *q = 0; |
| 874 | |
| 875 | forceFilters = p; |
| 876 | force_exit = 0; |
| 877 | } |
| 878 | /* if nothing found or invalid filters, exit quietly */ |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 879 | if (force_exit) { |
| 880 | return EXIT_SUCCESS; |
| 881 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 882 | |
| 883 | /* redirect our output to the emulator console */ |
| 884 | if (console) { |
| 885 | char* p = console + sizeof(CONSOLE_OPTION)-1; |
| 886 | char* q = strpbrk( p, " \t\n\r" ); |
| 887 | char devname[64]; |
| 888 | int len; |
| 889 | |
| 890 | if (q != NULL) { |
| 891 | len = q - p; |
| 892 | } else |
| 893 | len = strlen(p); |
| 894 | |
| 895 | len = snprintf( devname, sizeof(devname), "/dev/%.*s", len, p ); |
| 896 | fprintf(stderr, "logcat using %s (%d)\n", devname, len); |
| 897 | if (len < (int)sizeof(devname)) { |
| 898 | fd = open( devname, O_WRONLY ); |
| 899 | if (fd >= 0) { |
| 900 | dup2(fd, 1); |
| 901 | dup2(fd, 2); |
| 902 | close(fd); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | break; |
| 908 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 909 | case 'S': |
| 910 | printStatistics = 1; |
| 911 | break; |
| 912 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 913 | case ':': |
| 914 | logcat_panic(true, "Option -%c needs an argument\n", optopt); |
| 915 | break; |
| 916 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 917 | default: |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 918 | logcat_panic(true, "Unrecognized Option %c\n", optopt); |
| 919 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 923 | if (!devices) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 924 | dev = devices = new log_device_t("main", false); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 925 | g_devCount = 1; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 926 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 927 | dev = dev->next = new log_device_t("system", false); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 928 | g_devCount++; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 929 | } |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 930 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 931 | dev = dev->next = new log_device_t("crash", false); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 932 | g_devCount++; |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 933 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 934 | } |
| 935 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 936 | if (g_logRotateSizeKBytes != 0 && g_outputFileName == NULL) { |
| 937 | logcat_panic(true, "-r requires -f as well\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 938 | } |
| 939 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 940 | setupOutput(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 941 | |
| 942 | if (hasSetLogFormat == 0) { |
| 943 | const char* logFormat = getenv("ANDROID_PRINTF_LOG"); |
| 944 | |
| 945 | if (logFormat != NULL) { |
| 946 | err = setLogFormat(logFormat); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 947 | if (err < 0) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 948 | fprintf(stderr, "invalid format in ANDROID_PRINTF_LOG '%s'\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 949 | logFormat); |
| 950 | } |
Mark Salyzyn | 649fc60 | 2014-09-16 09:15:15 -0700 | [diff] [blame] | 951 | } else { |
| 952 | setLogFormat("threadtime"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | if (forceFilters) { |
| 957 | err = android_log_addFilterString(g_logformat, forceFilters); |
| 958 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 959 | logcat_panic(false, "Invalid filter expression in logcat args\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 960 | } |
| 961 | } else if (argc == optind) { |
| 962 | // Add from environment variable |
| 963 | char *env_tags_orig = getenv("ANDROID_LOG_TAGS"); |
| 964 | |
| 965 | if (env_tags_orig != NULL) { |
| 966 | err = android_log_addFilterString(g_logformat, env_tags_orig); |
| 967 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 968 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 969 | logcat_panic(true, |
| 970 | "Invalid filter expression in ANDROID_LOG_TAGS\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 971 | } |
| 972 | } |
| 973 | } else { |
| 974 | // Add from commandline |
| 975 | for (int i = optind ; i < argc ; i++) { |
| 976 | err = android_log_addFilterString(g_logformat, argv[i]); |
| 977 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 978 | if (err < 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 979 | logcat_panic(true, "Invalid filter expression '%s'\n", argv[i]); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 984 | dev = devices; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 985 | if (tail_time != log_time::EPOCH) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 986 | logger_list = android_logger_list_alloc_time(mode, tail_time, pid); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 987 | } else { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 988 | logger_list = android_logger_list_alloc(mode, tail_lines, pid); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 989 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 990 | const char *openDeviceFail = NULL; |
| 991 | const char *clearFail = NULL; |
| 992 | const char *setSizeFail = NULL; |
| 993 | const char *getSizeFail = NULL; |
| 994 | // We have three orthogonal actions below to clear, set log size and |
| 995 | // get log size. All sharing the same iteration loop. |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 996 | while (dev) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 997 | dev->logger_list = logger_list; |
| 998 | dev->logger = android_logger_open(logger_list, |
| 999 | android_name_to_log_id(dev->device)); |
| 1000 | if (!dev->logger) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1001 | openDeviceFail = openDeviceFail ?: dev->device; |
| 1002 | dev = dev->next; |
| 1003 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1004 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1005 | |
| 1006 | if (clearLog) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1007 | if (android_logger_clear(dev->logger)) { |
| 1008 | clearFail = clearFail ?: dev->device; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1009 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1010 | } |
| 1011 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1012 | if (setLogSize) { |
| 1013 | if (android_logger_set_log_size(dev->logger, setLogSize)) { |
| 1014 | setSizeFail = setSizeFail ?: dev->device; |
| 1015 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1018 | if (getLogSize) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1019 | long size = android_logger_get_log_size(dev->logger); |
| 1020 | long readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1021 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1022 | if ((size < 0) || (readable < 0)) { |
| 1023 | getSizeFail = getSizeFail ?: dev->device; |
| 1024 | } else { |
| 1025 | printf("%s: ring buffer is %ld%sb (%ld%sb consumed), " |
| 1026 | "max entry is %db, max payload is %db\n", dev->device, |
| 1027 | value_of_size(size), multiplier_of_size(size), |
| 1028 | value_of_size(readable), multiplier_of_size(readable), |
| 1029 | (int) LOGGER_ENTRY_MAX_LEN, |
| 1030 | (int) LOGGER_ENTRY_MAX_PAYLOAD); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1031 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | dev = dev->next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1035 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1036 | // report any errors in the above loop and exit |
| 1037 | if (openDeviceFail) { |
| 1038 | logcat_panic(false, "Unable to open log device '%s'\n", openDeviceFail); |
| 1039 | } |
| 1040 | if (clearFail) { |
| 1041 | logcat_panic(false, "failed to clear the '%s' log\n", clearFail); |
| 1042 | } |
| 1043 | if (setSizeFail) { |
| 1044 | logcat_panic(false, "failed to set the '%s' log size\n", setSizeFail); |
| 1045 | } |
| 1046 | if (getSizeFail) { |
| 1047 | logcat_panic(false, "failed to get the readable '%s' log size", |
| 1048 | getSizeFail); |
| 1049 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1050 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1051 | if (setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1052 | size_t len = strlen(setPruneList); |
| 1053 | /*extra 32 bytes are needed by android_logger_set_prune_list */ |
| 1054 | size_t bLen = len + 32; |
| 1055 | char *buf = NULL; |
| 1056 | if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) { |
| 1057 | buf[len] = '\0'; |
| 1058 | if (android_logger_set_prune_list(logger_list, buf, bLen)) { |
| 1059 | logcat_panic(false, "failed to set the prune list"); |
| 1060 | } |
| 1061 | free(buf); |
| 1062 | } else { |
| 1063 | logcat_panic(false, "failed to set the prune list (alloc)"); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1064 | } |
| 1065 | } |
| 1066 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 1067 | if (printStatistics || getPruneList) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1068 | size_t len = 8192; |
| 1069 | char *buf; |
| 1070 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1071 | for (int retry = 32; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1072 | (retry >= 0) && ((buf = new char [len])); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1073 | delete [] buf, buf = NULL, --retry) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1074 | if (getPruneList) { |
| 1075 | android_logger_get_prune_list(logger_list, buf, len); |
| 1076 | } else { |
| 1077 | android_logger_get_statistics(logger_list, buf, len); |
| 1078 | } |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1079 | buf[len-1] = '\0'; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1080 | if (atol(buf) < 3) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1081 | delete [] buf; |
| 1082 | buf = NULL; |
| 1083 | break; |
| 1084 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1085 | size_t ret = atol(buf) + 1; |
| 1086 | if (ret <= len) { |
| 1087 | len = ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1088 | break; |
| 1089 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1090 | len = ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | if (!buf) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1094 | logcat_panic(false, "failed to read data"); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | // remove trailing FF |
| 1098 | char *cp = buf + len - 1; |
| 1099 | *cp = '\0'; |
| 1100 | bool truncated = *--cp != '\f'; |
| 1101 | if (!truncated) { |
| 1102 | *cp = '\0'; |
| 1103 | } |
| 1104 | |
| 1105 | // squash out the byte count |
| 1106 | cp = buf; |
| 1107 | if (!truncated) { |
Mark Salyzyn | 22e287d | 2014-03-21 13:12:16 -0700 | [diff] [blame] | 1108 | while (isdigit(*cp)) { |
| 1109 | ++cp; |
| 1110 | } |
| 1111 | if (*cp == '\n') { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1112 | ++cp; |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | printf("%s", cp); |
| 1117 | delete [] buf; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1118 | return EXIT_SUCCESS; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1122 | if (getLogSize) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1123 | return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1124 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1125 | if (setLogSize || setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1126 | return EXIT_SUCCESS; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1127 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1128 | if (clearLog) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1129 | return EXIT_SUCCESS; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1130 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1131 | |
| 1132 | //LOG_EVENT_INT(10, 12345); |
| 1133 | //LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 1134 | //LOG_EVENT_STRING(0, "whassup, doc?"); |
| 1135 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1136 | dev = NULL; |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1137 | log_device_t unexpected("unexpected", false); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1138 | while (1) { |
| 1139 | struct log_msg log_msg; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1140 | log_device_t* d; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1141 | int ret = android_logger_list_read(logger_list, &log_msg); |
| 1142 | |
| 1143 | if (ret == 0) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1144 | logcat_panic(false, "read: unexpected EOF!\n"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | if (ret < 0) { |
| 1148 | if (ret == -EAGAIN) { |
| 1149 | break; |
| 1150 | } |
| 1151 | |
| 1152 | if (ret == -EIO) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1153 | logcat_panic(false, "read: unexpected EOF!\n"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1154 | } |
| 1155 | if (ret == -EINVAL) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1156 | logcat_panic(false, "read: unexpected length.\n"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1157 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1158 | logcat_panic(false, "logcat read failure"); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1159 | } |
| 1160 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1161 | for (d = devices; d; d = d->next) { |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1162 | if (android_name_to_log_id(d->device) == log_msg.id()) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1163 | break; |
| 1164 | } |
| 1165 | } |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1166 | if (!d) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1167 | g_devCount = 2; // set to Multiple |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 1168 | d = &unexpected; |
| 1169 | d->binary = log_msg.id() == LOG_ID_EVENTS; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1170 | } |
| 1171 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1172 | if (dev != d) { |
| 1173 | dev = d; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1174 | maybePrintStart(dev, printDividers); |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1175 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1176 | if (g_printBinary) { |
| 1177 | printBinary(&log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1178 | } else { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1179 | processBuffer(dev, &log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1180 | } |
| 1181 | } |
| 1182 | |
| 1183 | android_logger_list_free(logger_list); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1184 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1185 | return EXIT_SUCCESS; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1186 | } |