Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1 | /* |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 2 | * Copyright (C) 2006-2017 The Android Open Source Project |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -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 | */ |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 16 | |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 17 | #include <arpa/inet.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | #include <ctype.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 20 | #include <dirent.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 23 | #include <math.h> |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 24 | #include <pthread.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 25 | #include <sched.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 26 | #include <stdarg.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 29 | #include <string.h> |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 30 | #include <sys/cdefs.h> |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 31 | #include <sys/resource.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 32 | #include <sys/socket.h> |
| 33 | #include <sys/stat.h> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 34 | #include <sys/types.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 35 | #include <time.h> |
| 36 | #include <unistd.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 37 | |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 38 | #include <atomic> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 39 | #include <memory> |
| 40 | #include <string> |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 41 | #include <vector> |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 42 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 43 | #include <android-base/file.h> |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 44 | #include <android-base/properties.h> |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 45 | #include <android-base/stringprintf.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 46 | #include <android-base/strings.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 47 | #include <cutils/sched_policy.h> |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 48 | #include <cutils/sockets.h> |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 49 | #include <log/event_tag_map.h> |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 50 | #include <log/getopt.h> |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 51 | #include <log/logcat.h> |
Colin Cross | 9227bd3 | 2013-07-23 16:59:20 -0700 | [diff] [blame] | 52 | #include <log/logprint.h> |
Mark Salyzyn | aeaaf81 | 2016-09-30 13:30:33 -0700 | [diff] [blame] | 53 | #include <private/android_logger.h> |
Elliott Hughes | b9e53b4 | 2016-02-17 11:58:01 -0800 | [diff] [blame] | 54 | #include <system/thread_defs.h> |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 55 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 56 | #include <pcrecpp.h> |
| 57 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 58 | #define DEFAULT_MAX_ROTATED_LOGS 4 |
| 59 | |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 60 | struct log_device_t { |
| 61 | const char* device; |
| 62 | bool binary; |
| 63 | struct logger* logger; |
| 64 | struct logger_list* logger_list; |
| 65 | bool printed; |
| 66 | |
| 67 | log_device_t* next; |
| 68 | |
| 69 | log_device_t(const char* d, bool b) { |
| 70 | device = d; |
| 71 | binary = b; |
| 72 | next = nullptr; |
| 73 | printed = false; |
| 74 | logger = nullptr; |
| 75 | logger_list = nullptr; |
| 76 | } |
| 77 | }; |
| 78 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 79 | struct android_logcat_context_internal { |
| 80 | // status |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 81 | volatile std::atomic_int retval; // valid if thread_stopped set |
| 82 | // Arguments passed in, or copies and storage thereof if a thread. |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 83 | int argc; |
| 84 | char* const* argv; |
| 85 | char* const* envp; |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 86 | std::vector<std::string> args; |
| 87 | std::vector<const char*> argv_hold; |
| 88 | std::vector<std::string> envs; |
| 89 | std::vector<const char*> envp_hold; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 90 | int output_fd; // duplication of fileno(output) (below) |
| 91 | int error_fd; // duplication of fileno(error) (below) |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 92 | |
| 93 | // library |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 94 | int fds[2]; // From popen call |
| 95 | FILE* output; // everything writes to fileno(output), buffer unused |
| 96 | FILE* error; // unless error == output. |
| 97 | pthread_t thr; |
| 98 | volatile std::atomic_bool stop; // quick exit flag |
| 99 | volatile std::atomic_bool thread_stopped; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 100 | bool stderr_null; // shell "2>/dev/null" |
| 101 | bool stderr_stdout; // shell "2>&1" |
| 102 | |
| 103 | // global variables |
| 104 | AndroidLogFormat* logformat; |
| 105 | const char* outputFileName; |
| 106 | // 0 means "no log rotation" |
| 107 | size_t logRotateSizeKBytes; |
| 108 | // 0 means "unbounded" |
| 109 | size_t maxRotatedLogs; |
| 110 | size_t outByteCount; |
| 111 | int printBinary; |
| 112 | int devCount; // >1 means multiple |
| 113 | pcrecpp::RE* regex; |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 114 | log_device_t* devices; |
| 115 | EventTagMap* eventTagMap; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 116 | // 0 means "infinite" |
| 117 | size_t maxCount; |
| 118 | size_t printCount; |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 119 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 120 | bool printItAnyways; |
| 121 | bool debug; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 122 | bool hasOpenedEventTagMap; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | // Creates a context associated with this logcat instance |
| 126 | android_logcat_context create_android_logcat() { |
| 127 | android_logcat_context_internal* context; |
| 128 | |
| 129 | context = (android_logcat_context_internal*)calloc( |
| 130 | 1, sizeof(android_logcat_context_internal)); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 131 | if (!context) return nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 132 | |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 133 | context->fds[0] = -1; |
| 134 | context->fds[1] = -1; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 135 | context->output_fd = -1; |
| 136 | context->error_fd = -1; |
| 137 | context->maxRotatedLogs = DEFAULT_MAX_ROTATED_LOGS; |
| 138 | |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 139 | context->argv_hold.clear(); |
| 140 | context->args.clear(); |
| 141 | context->envp_hold.clear(); |
| 142 | context->envs.clear(); |
| 143 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 144 | return (android_logcat_context)context; |
| 145 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 146 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 147 | // logd prefixes records with a length field |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 148 | #define RECORD_LENGTH_FIELD_SIZE_BYTES sizeof(uint32_t) |
| 149 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 150 | namespace android { |
| 151 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 152 | enum helpType { HELP_FALSE, HELP_TRUE, HELP_FORMAT }; |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 153 | |
Mark Salyzyn | aa730c1 | 2016-03-30 12:38:29 -0700 | [diff] [blame] | 154 | // if showHelp is set, newline required in fmt statement to transition to usage |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 155 | static void logcat_panic(android_logcat_context_internal* context, |
| 156 | enum helpType showHelp, const char* fmt, ...) |
| 157 | __printflike(3, 4); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 158 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 159 | static int openLogFile(const char* pathname) { |
Edwin Vane | 80b221c | 2012-08-13 12:55:07 -0400 | [diff] [blame] | 160 | 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] | 161 | } |
| 162 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 163 | static void close_output(android_logcat_context_internal* context) { |
| 164 | // split output_from_error |
| 165 | if (context->error == context->output) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 166 | context->output = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 167 | context->output_fd = -1; |
| 168 | } |
| 169 | if (context->error && (context->output_fd == fileno(context->error))) { |
| 170 | context->output_fd = -1; |
| 171 | } |
| 172 | if (context->output_fd == context->error_fd) { |
| 173 | context->output_fd = -1; |
| 174 | } |
| 175 | // close output channel |
| 176 | if (context->output) { |
| 177 | if (context->output != stdout) { |
| 178 | if (context->output_fd == fileno(context->output)) { |
| 179 | context->output_fd = -1; |
| 180 | } |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 181 | if (context->fds[1] == fileno(context->output)) { |
| 182 | context->fds[1] = -1; |
| 183 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 184 | fclose(context->output); |
| 185 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 186 | context->output = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 187 | } |
| 188 | if (context->output_fd >= 0) { |
| 189 | if (context->output_fd != fileno(stdout)) { |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 190 | if (context->fds[1] == context->output_fd) { |
| 191 | context->fds[1] = -1; |
| 192 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 193 | close(context->output_fd); |
| 194 | } |
| 195 | context->output_fd = -1; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | static void close_error(android_logcat_context_internal* context) { |
| 200 | // split error_from_output |
| 201 | if (context->output == context->error) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 202 | context->error = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 203 | context->error_fd = -1; |
| 204 | } |
| 205 | if (context->output && (context->error_fd == fileno(context->output))) { |
| 206 | context->error_fd = -1; |
| 207 | } |
| 208 | if (context->error_fd == context->output_fd) { |
| 209 | context->error_fd = -1; |
| 210 | } |
| 211 | // close error channel |
| 212 | if (context->error) { |
| 213 | if ((context->error != stderr) && (context->error != stdout)) { |
| 214 | if (context->error_fd == fileno(context->error)) { |
| 215 | context->error_fd = -1; |
| 216 | } |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 217 | if (context->fds[1] == fileno(context->error)) { |
| 218 | context->fds[1] = -1; |
| 219 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 220 | fclose(context->error); |
| 221 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 222 | context->error = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 223 | } |
| 224 | if (context->error_fd >= 0) { |
| 225 | if ((context->error_fd != fileno(stdout)) && |
| 226 | (context->error_fd != fileno(stderr))) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 227 | if (context->fds[1] == context->error_fd) context->fds[1] = -1; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 228 | close(context->error_fd); |
| 229 | } |
| 230 | context->error_fd = -1; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static void rotateLogs(android_logcat_context_internal* context) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 235 | int err; |
| 236 | |
| 237 | // Can't rotate logs if we're not outputting to a file |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 238 | if (!context->outputFileName) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 239 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 240 | close_output(context); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 241 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 242 | // Compute the maximum number of digits needed to count up to |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 243 | // maxRotatedLogs in decimal. eg: |
| 244 | // maxRotatedLogs == 30 |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 245 | // -> log10(30) == 1.477 |
| 246 | // -> maxRotationCountDigits == 2 |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 247 | int maxRotationCountDigits = |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 248 | (context->maxRotatedLogs > 0) |
| 249 | ? (int)(floor(log10(context->maxRotatedLogs) + 1)) |
| 250 | : 0; |
Aristidis Papaioannou | eba7344 | 2014-10-16 22:19:55 -0700 | [diff] [blame] | 251 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 252 | for (int i = context->maxRotatedLogs; i > 0; i--) { |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 253 | std::string file1 = android::base::StringPrintf( |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 254 | "%s.%.*d", context->outputFileName, maxRotationCountDigits, i); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 255 | |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 256 | std::string file0; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 257 | if (!(i - 1)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 258 | file0 = android::base::StringPrintf("%s", context->outputFileName); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 259 | } else { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 260 | file0 = |
| 261 | android::base::StringPrintf("%s.%.*d", context->outputFileName, |
| 262 | maxRotationCountDigits, i - 1); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 265 | if (!file0.length() || !file1.length()) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 266 | perror("while rotating log files"); |
| 267 | break; |
| 268 | } |
| 269 | |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 270 | err = rename(file0.c_str(), file1.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 271 | |
| 272 | if (err < 0 && errno != ENOENT) { |
| 273 | perror("while rotating log files"); |
| 274 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 275 | } |
| 276 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 277 | context->output_fd = openLogFile(context->outputFileName); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 278 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 279 | if (context->output_fd < 0) { |
| 280 | logcat_panic(context, HELP_FALSE, "couldn't open output file"); |
| 281 | return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 282 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 283 | context->output = fdopen(context->output_fd, "web"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 284 | if (!context->output) { |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 285 | logcat_panic(context, HELP_FALSE, "couldn't fdopen output file"); |
| 286 | return; |
| 287 | } |
| 288 | if (context->stderr_stdout) { |
| 289 | close_error(context); |
| 290 | context->error = context->output; |
| 291 | context->error_fd = context->output_fd; |
| 292 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 293 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 294 | context->outByteCount = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 297 | void printBinary(android_logcat_context_internal* context, struct log_msg* buf) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 298 | size_t size = buf->len(); |
| 299 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 300 | TEMP_FAILURE_RETRY(write(context->output_fd, buf, size)); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 301 | } |
| 302 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 303 | static bool regexOk(android_logcat_context_internal* context, |
| 304 | const AndroidLogEntry& entry) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 305 | if (!context->regex) return true; |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 306 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 307 | std::string messageString(entry.message, entry.messageLen); |
| 308 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 309 | return context->regex->PartialMatch(messageString); |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 312 | static void processBuffer(android_logcat_context_internal* context, |
| 313 | log_device_t* dev, struct log_msg* buf) { |
Mathias Agopian | 5084452 | 2010-03-17 16:10:26 -0700 | [diff] [blame] | 314 | int bytesWritten = 0; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 315 | int err; |
| 316 | AndroidLogEntry entry; |
| 317 | char binaryMsgBuf[1024]; |
| 318 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 319 | if (dev->binary) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 320 | if (!context->eventTagMap && !context->hasOpenedEventTagMap) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 321 | context->eventTagMap = android_openEventTagMap(nullptr); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 322 | context->hasOpenedEventTagMap = true; |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 323 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 324 | err = android_log_processBinaryLogBuffer( |
| 325 | &buf->entry_v1, &entry, context->eventTagMap, binaryMsgBuf, |
| 326 | sizeof(binaryMsgBuf)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 327 | // printf(">>> pri=%d len=%d msg='%s'\n", |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 328 | // entry.priority, entry.messageLen, entry.message); |
| 329 | } else { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 330 | err = android_log_processLogBuffer(&buf->entry_v1, &entry); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 331 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 332 | if ((err < 0) && !context->debug) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 333 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 334 | if (android_log_shouldPrintLine( |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 335 | context->logformat, std::string(entry.tag, entry.tagLen).c_str(), |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 336 | entry.priority)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 337 | bool match = regexOk(context, entry); |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 338 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 339 | context->printCount += match; |
| 340 | if (match || context->printItAnyways) { |
| 341 | bytesWritten = android_log_printLogLine(context->logformat, |
| 342 | context->output_fd, &entry); |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 343 | |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 344 | if (bytesWritten < 0) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 345 | logcat_panic(context, HELP_FALSE, "output error"); |
| 346 | return; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 347 | } |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 348 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 349 | } |
| 350 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 351 | context->outByteCount += bytesWritten; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 352 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 353 | if (context->logRotateSizeKBytes > 0 && |
| 354 | (context->outByteCount / 1024) >= context->logRotateSizeKBytes) { |
| 355 | rotateLogs(context); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 356 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 359 | static void maybePrintStart(android_logcat_context_internal* context, |
| 360 | log_device_t* dev, bool printDividers) { |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 361 | if (!dev->printed || printDividers) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 362 | if (context->devCount > 1 && !context->printBinary) { |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 363 | char buf[1024]; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 364 | snprintf(buf, sizeof(buf), "--------- %s %s\n", |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 365 | dev->printed ? "switch to" : "beginning of", dev->device); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 366 | if (write(context->output_fd, buf, strlen(buf)) < 0) { |
| 367 | logcat_panic(context, HELP_FALSE, "output error"); |
| 368 | return; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 371 | dev->printed = true; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 375 | static void setupOutputAndSchedulingPolicy( |
| 376 | android_logcat_context_internal* context, bool blocking) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 377 | if (!context->outputFileName) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 378 | |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 379 | if (blocking) { |
| 380 | // Lower priority and set to batch scheduling if we are saving |
| 381 | // the logs into files and taking continuous content. |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 382 | if ((set_sched_policy(0, SP_BACKGROUND) < 0) && context->error) { |
| 383 | fprintf(context->error, |
| 384 | "failed to set background scheduling policy\n"); |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | struct sched_param param; |
| 388 | memset(¶m, 0, sizeof(param)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 389 | if (sched_setscheduler((pid_t)0, SCHED_BATCH, ¶m) < 0) { |
Mark Salyzyn | 3ef730c | 2015-06-02 07:57:16 -0700 | [diff] [blame] | 390 | fprintf(stderr, "failed to set to batch scheduler\n"); |
| 391 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 392 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 393 | if ((setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_BACKGROUND) < 0) && |
| 394 | context->error) { |
| 395 | fprintf(context->error, "failed set to priority\n"); |
Riley Andrews | aede989 | 2015-06-08 23:36:34 -0700 | [diff] [blame] | 396 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 397 | } |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 398 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 399 | close_output(context); |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 400 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 401 | context->output_fd = openLogFile(context->outputFileName); |
| 402 | |
| 403 | if (context->output_fd < 0) { |
| 404 | logcat_panic(context, HELP_FALSE, "couldn't open output file"); |
| 405 | return; |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | struct stat statbuf; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 409 | if (fstat(context->output_fd, &statbuf) == -1) { |
| 410 | close_output(context); |
| 411 | logcat_panic(context, HELP_FALSE, "couldn't get output file stat\n"); |
| 412 | return; |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 415 | if ((size_t)statbuf.st_size > SIZE_MAX || statbuf.st_size < 0) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 416 | close_output(context); |
| 417 | logcat_panic(context, HELP_FALSE, "invalid output file stat\n"); |
| 418 | return; |
Mark Salyzyn | ad5e411 | 2016-08-04 07:53:52 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 421 | context->output = fdopen(context->output_fd, "web"); |
| 422 | |
| 423 | context->outByteCount = statbuf.st_size; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 424 | } |
| 425 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 426 | // clang-format off |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 427 | static void show_help(android_logcat_context_internal* context) { |
| 428 | if (!context->error) return; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 429 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 430 | const char* cmd = strrchr(context->argv[0], '/'); |
| 431 | cmd = cmd ? cmd + 1 : context->argv[0]; |
| 432 | |
| 433 | fprintf(context->error, "Usage: %s [options] [filterspecs]\n", cmd); |
| 434 | |
| 435 | fprintf(context->error, "options include:\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 436 | " -s Set default filter to silent. Equivalent to filterspec '*:S'\n" |
| 437 | " -f <file>, --file=<file> Log to file. Default is stdout\n" |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 438 | " -r <kbytes>, --rotate-kbytes=<kbytes>\n" |
| 439 | " Rotate log every kbytes. Requires -f option\n" |
| 440 | " -n <count>, --rotate-count=<count>\n" |
| 441 | " Sets max number of rotated logs to <count>, default 4\n" |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 442 | " --id=<id> If the signature id for logging to file changes, then clear\n" |
| 443 | " the fileset and continue\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 444 | " -v <format>, --format=<format>\n" |
Mark Salyzyn | 9cfd1c6 | 2016-07-06 11:12:14 -0700 | [diff] [blame] | 445 | " Sets log print format verb and adverbs, where <format> is:\n" |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 446 | " brief help long process raw tag thread threadtime time\n" |
Mark Salyzyn | e735a73 | 2016-07-06 13:30:40 -0700 | [diff] [blame] | 447 | " and individually flagged modifying adverbs can be added:\n" |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 448 | " color descriptive epoch monotonic printable uid\n" |
| 449 | " usec UTC year zone\n" |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 450 | " Multiple -v parameters or comma separated list of format and\n" |
| 451 | " format modifiers are allowed.\n" |
Mark Salyzyn | 9b4d7e1 | 2017-01-30 09:16:09 -0800 | [diff] [blame] | 452 | // private and undocumented nsec, no signal, too much noise |
| 453 | // useful for -T or -t <timestamp> accurate testing though. |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 454 | " -D, --dividers Print dividers between each log buffer\n" |
| 455 | " -c, --clear Clear (flush) the entire log and exit\n" |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 456 | " if Log to File specified, clear fileset instead\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 457 | " -d Dump the log and then exit (don't block)\n" |
| 458 | " -e <expr>, --regex=<expr>\n" |
| 459 | " Only print lines where the log message matches <expr>\n" |
Brendan Jackman | a514113 | 2017-10-31 12:44:54 +0000 | [diff] [blame] | 460 | " where <expr> is a Perl-compatible regular expression\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 461 | // Leave --head undocumented as alias for -m |
| 462 | " -m <count>, --max-count=<count>\n" |
| 463 | " Quit after printing <count> lines. This is meant to be\n" |
| 464 | " paired with --regex, but will work on its own.\n" |
| 465 | " --print Paired with --regex and --max-count to let content bypass\n" |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 466 | " regex filter but still stop at number of matches.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 467 | // Leave --tail undocumented as alias for -t |
| 468 | " -t <count> Print only the most recent <count> lines (implies -d)\n" |
| 469 | " -t '<time>' Print most recent lines since specified time (implies -d)\n" |
| 470 | " -T <count> Print only the most recent <count> lines (does not imply -d)\n" |
| 471 | " -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] | 472 | " 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] | 473 | " 'YYYY-MM-DD hh:mm:ss.mmm...' or 'sssss.mmm...' format\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 474 | " -g, --buffer-size Get the size of the ring buffer.\n" |
| 475 | " -G <size>, --buffer-size=<size>\n" |
| 476 | " Set size of log ring buffer, may suffix with K or M.\n" |
Oleksiy Avramchenko | 39e2d22 | 2016-11-29 12:48:11 +0100 | [diff] [blame] | 477 | " -L, --last Dump logs from prior to last reboot\n" |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 478 | // Leave security (Device Owner only installations) and |
| 479 | // kernel (userdebug and eng) buffers undocumented. |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 480 | " -b <buffer>, --buffer=<buffer> Request alternate ring buffer, 'main',\n" |
| 481 | " 'system', 'radio', 'events', 'crash', 'default' or 'all'.\n" |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 482 | " Multiple -b parameters or comma separated list of buffers are\n" |
| 483 | " allowed. Buffers interleaved. Default -b main,system,crash.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 484 | " -B, --binary Output the log in binary.\n" |
| 485 | " -S, --statistics Output statistics.\n" |
| 486 | " -p, --prune Print prune white and ~black list. Service is specified as\n" |
| 487 | " UID, UID/PID or /PID. Weighed for quicker pruning if prefix\n" |
Mark Salyzyn | bbbe14f | 2014-04-11 13:49:43 -0700 | [diff] [blame] | 488 | " with ~, otherwise weighed for longevity if unadorned. All\n" |
| 489 | " other pruning activity is oldest first. Special case ~!\n" |
| 490 | " represents an automatic quicker pruning for the noisiest\n" |
| 491 | " UID as determined by the current statistics.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 492 | " -P '<list> ...', --prune='<list> ...'\n" |
| 493 | " Set prune white and ~black list, using same format as\n" |
| 494 | " listed above. Must be quoted.\n" |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 495 | " --pid=<pid> Only prints logs from the given pid.\n" |
Mark Salyzyn | 378f474 | 2016-04-12 09:11:46 -0700 | [diff] [blame] | 496 | // Check ANDROID_LOG_WRAP_DEFAULT_TIMEOUT value for match to 2 hours |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 497 | " --wrap Sleep for 2 hours or when buffer about to wrap whichever\n" |
| 498 | " comes first. Improves efficiency of polling by providing\n" |
| 499 | " an about-to-wrap wakeup.\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 500 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 501 | fprintf(context->error, "\nfilterspecs are a series of \n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 502 | " <tag>[:priority]\n\n" |
| 503 | "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] | 504 | " V Verbose (default for <tag>)\n" |
| 505 | " D Debug (default for '*')\n" |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 506 | " I Info\n" |
| 507 | " W Warn\n" |
| 508 | " E Error\n" |
| 509 | " F Fatal\n" |
Mark Salyzyn | bba894a | 2015-03-09 09:32:56 -0700 | [diff] [blame] | 510 | " S Silent (suppress all output)\n" |
| 511 | "\n'*' by itself means '*:D' and <tag> by itself means <tag>:V.\n" |
| 512 | "If no '*' filterspec or -s on command line, all filter defaults to '*:V'.\n" |
| 513 | "eg: '*:S <tag>' prints only <tag>, '<tag>:S' suppresses all <tag> log messages.\n" |
| 514 | "\nIf not specified on the command line, filterspec is set from ANDROID_LOG_TAGS.\n" |
| 515 | "\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] | 516 | "or defaults to \"threadtime\"\n\n"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 517 | } |
| 518 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 519 | static void show_format_help(android_logcat_context_internal* context) { |
| 520 | if (!context->error) return; |
| 521 | fprintf(context->error, |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 522 | "-v <format>, --format=<format> options:\n" |
| 523 | " Sets log print format verb and adverbs, where <format> is:\n" |
| 524 | " brief long process raw tag thread threadtime time\n" |
| 525 | " and individually flagged modifying adverbs can be added:\n" |
| 526 | " color descriptive epoch monotonic printable uid usec UTC year zone\n" |
| 527 | "\nSingle format verbs:\n" |
| 528 | " brief — Display priority/tag and PID of the process issuing the message.\n" |
| 529 | " long — Display all metadata fields, separate messages with blank lines.\n" |
| 530 | " process — Display PID only.\n" |
| 531 | " raw — Display the raw log message, with no other metadata fields.\n" |
| 532 | " tag — Display the priority/tag only.\n" |
Mark Salyzyn | c74f8d9 | 2017-05-15 13:40:33 -0700 | [diff] [blame] | 533 | " thread — Display priority, PID and TID of process issuing the message.\n" |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 534 | " threadtime — Display the date, invocation time, priority, tag, and the PID\n" |
| 535 | " and TID of the thread issuing the message. (the default format).\n" |
| 536 | " time — Display the date, invocation time, priority/tag, and PID of the\n" |
| 537 | " process issuing the message.\n" |
| 538 | "\nAdverb modifiers can be used in combination:\n" |
| 539 | " color — Display in highlighted color to match priority. i.e. \x1B[38;5;231mVERBOSE\n" |
| 540 | " \x1B[38;5;75mDEBUG \x1B[38;5;40mINFO \x1B[38;5;166mWARNING \x1B[38;5;196mERROR FATAL\x1B[0m\n" |
| 541 | " descriptive — events logs only, descriptions from event-log-tags database.\n" |
| 542 | " epoch — Display time as seconds since Jan 1 1970.\n" |
| 543 | " monotonic — Display time as cpu seconds since last boot.\n" |
| 544 | " printable — Ensure that any binary logging content is escaped.\n" |
| 545 | " uid — If permitted, display the UID or Android ID of logged process.\n" |
| 546 | " usec — Display time down the microsecond precision.\n" |
| 547 | " UTC — Display time as UTC.\n" |
| 548 | " year — Add the year to the displayed time.\n" |
| 549 | " zone — Add the local timezone to the displayed time.\n" |
| 550 | " \"<zone>\" — Print using this public named timezone (experimental).\n\n" |
| 551 | ); |
| 552 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 553 | // clang-format on |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 554 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 555 | static int setLogFormat(android_logcat_context_internal* context, |
| 556 | const char* formatString) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 557 | AndroidLogPrintFormat format; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 558 | |
| 559 | format = android_log_formatFromString(formatString); |
| 560 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 561 | // invalid string? |
| 562 | if (format == FORMAT_OFF) return -1; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 563 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 564 | return android_log_setPrintFormat(context->logformat, format); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 565 | } |
| 566 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 567 | static const char multipliers[][2] = { { "" }, { "K" }, { "M" }, { "G" } }; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 568 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 569 | static unsigned long value_of_size(unsigned long value) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 570 | for (unsigned i = 0; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 571 | (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024); |
| 572 | value /= 1024, ++i) |
| 573 | ; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 574 | return value; |
| 575 | } |
| 576 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 577 | static const char* multiplier_of_size(unsigned long value) { |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 578 | unsigned i; |
| 579 | for (i = 0; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 580 | (i < sizeof(multipliers) / sizeof(multipliers[0])) && (value >= 1024); |
| 581 | value /= 1024, ++i) |
| 582 | ; |
Mark Salyzyn | 671e343 | 2014-05-06 07:34:59 -0700 | [diff] [blame] | 583 | return multipliers[i]; |
| 584 | } |
| 585 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 586 | // String to unsigned int, returns -1 if it fails |
| 587 | static bool getSizeTArg(const char* ptr, size_t* val, size_t min = 0, |
| 588 | size_t max = SIZE_MAX) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 589 | if (!ptr) return false; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 590 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 591 | char* endp; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 592 | errno = 0; |
| 593 | size_t ret = (size_t)strtoll(ptr, &endp, 0); |
| 594 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 595 | if (endp[0] || errno) return false; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 596 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 597 | if ((ret > max) || (ret < min)) return false; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 598 | |
| 599 | *val = ret; |
| 600 | return true; |
| 601 | } |
| 602 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 603 | static void logcat_panic(android_logcat_context_internal* context, |
| 604 | enum helpType showHelp, const char* fmt, ...) { |
| 605 | context->retval = EXIT_FAILURE; |
| 606 | if (!context->error) { |
| 607 | context->stop = true; |
| 608 | return; |
| 609 | } |
| 610 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 611 | va_list args; |
Mark Salyzyn | 77d7e81 | 2015-04-13 09:27:57 -0700 | [diff] [blame] | 612 | va_start(args, fmt); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 613 | vfprintf(context->error, fmt, args); |
Mark Salyzyn | 77d7e81 | 2015-04-13 09:27:57 -0700 | [diff] [blame] | 614 | va_end(args); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 615 | |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 616 | switch (showHelp) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 617 | case HELP_TRUE: |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 618 | show_help(context); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 619 | break; |
| 620 | case HELP_FORMAT: |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 621 | show_format_help(context); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 622 | break; |
| 623 | case HELP_FALSE: |
| 624 | default: |
| 625 | break; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 626 | } |
| 627 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 628 | context->stop = true; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 629 | } |
| 630 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 631 | static char* parseTime(log_time& t, const char* cp) { |
| 632 | char* ep = t.strptime(cp, "%m-%d %H:%M:%S.%q"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 633 | if (ep) return ep; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 634 | ep = t.strptime(cp, "%Y-%m-%d %H:%M:%S.%q"); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 635 | if (ep) return ep; |
Mark Salyzyn | 4cbed02 | 2015-08-31 15:53:41 -0700 | [diff] [blame] | 636 | return t.strptime(cp, "%s.%q"); |
Mark Salyzyn | f28f6a9 | 2015-08-31 08:01:33 -0700 | [diff] [blame] | 637 | } |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 638 | |
Mark Salyzyn | 3196106 | 2016-08-04 07:43:46 -0700 | [diff] [blame] | 639 | // Find last logged line in <outputFileName>, or <outputFileName>.1 |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 640 | static log_time lastLogTime(const char* outputFileName) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 641 | log_time retval(log_time::EPOCH); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 642 | if (!outputFileName) return retval; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 643 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 644 | std::string directory; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 645 | const char* file = strrchr(outputFileName, '/'); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 646 | if (!file) { |
| 647 | directory = "."; |
| 648 | file = outputFileName; |
| 649 | } else { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 650 | directory = std::string(outputFileName, file - outputFileName); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 651 | ++file; |
| 652 | } |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 653 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 654 | std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir(directory.c_str()), |
| 655 | closedir); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 656 | if (!dir.get()) return retval; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 657 | |
Mark Salyzyn | 3196106 | 2016-08-04 07:43:46 -0700 | [diff] [blame] | 658 | log_time now(android_log_clockid()); |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 659 | |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 660 | size_t len = strlen(file); |
| 661 | log_time modulo(0, NS_PER_SEC); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 662 | struct dirent* dp; |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 663 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 664 | while (!!(dp = readdir(dir.get()))) { |
| 665 | if ((dp->d_type != DT_REG) || !!strncmp(dp->d_name, file, len) || |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 666 | (dp->d_name[len] && ((dp->d_name[len] != '.') || |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 667 | (strtoll(dp->d_name + 1, nullptr, 10) != 1)))) { |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 668 | continue; |
| 669 | } |
| 670 | |
| 671 | std::string file_name = directory; |
| 672 | file_name += "/"; |
| 673 | file_name += dp->d_name; |
| 674 | std::string file; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 675 | if (!android::base::ReadFileToString(file_name, &file)) continue; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 676 | |
| 677 | bool found = false; |
| 678 | for (const auto& line : android::base::Split(file, "\n")) { |
| 679 | log_time t(log_time::EPOCH); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 680 | char* ep = parseTime(t, line.c_str()); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 681 | if (!ep || (*ep != ' ')) continue; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 682 | // determine the time precision of the logs (eg: msec or usec) |
| 683 | for (unsigned long mod = 1UL; mod < modulo.tv_nsec; mod *= 10) { |
| 684 | if (t.tv_nsec % (mod * 10)) { |
| 685 | modulo.tv_nsec = mod; |
| 686 | break; |
| 687 | } |
| 688 | } |
| 689 | // We filter any times later than current as we may not have the |
| 690 | // year stored with each log entry. Also, since it is possible for |
| 691 | // entries to be recorded out of order (very rare) we select the |
| 692 | // maximum we find just in case. |
| 693 | if ((t < now) && (t > retval)) { |
| 694 | retval = t; |
| 695 | found = true; |
| 696 | } |
| 697 | } |
| 698 | // We count on the basename file to be the definitive end, so stop here. |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 699 | if (!dp->d_name[len] && found) break; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 700 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 701 | if (retval == log_time::EPOCH) return retval; |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 702 | // tail_time prints matching or higher, round up by the modulo to prevent |
| 703 | // a replay of the last entry we have just checked. |
| 704 | retval += modulo; |
| 705 | return retval; |
| 706 | } |
| 707 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 708 | const char* getenv(android_logcat_context_internal* context, const char* name) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 709 | if (!context->envp || !name || !*name) return nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 710 | |
| 711 | for (size_t len = strlen(name), i = 0; context->envp[i]; ++i) { |
| 712 | if (strncmp(context->envp[i], name, len)) continue; |
| 713 | if (context->envp[i][len] == '=') return &context->envp[i][len + 1]; |
| 714 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 715 | return nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 718 | } // namespace android |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 719 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 720 | void reportErrorName(const char** current, const char* name, |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 721 | bool blockSecurity) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 722 | if (*current) return; |
| 723 | if (!blockSecurity || (android_name_to_log_id(name) != LOG_ID_SECURITY)) { |
| 724 | *current = name; |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 725 | } |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 726 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 727 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 728 | static int __logcat(android_logcat_context_internal* context) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 729 | using namespace android; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 730 | int err; |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 731 | bool hasSetLogFormat = false; |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 732 | bool clearLog = false; |
| 733 | bool allSelected = false; |
| 734 | bool getLogSize = false; |
| 735 | bool getPruneList = false; |
| 736 | bool printStatistics = false; |
| 737 | bool printDividers = false; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 738 | unsigned long setLogSize = 0; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 739 | const char* setPruneList = nullptr; |
| 740 | const char* setId = nullptr; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 741 | int mode = ANDROID_LOG_RDONLY; |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 742 | std::string forceFilters; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 743 | log_device_t* dev; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 744 | struct logger_list* logger_list; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 745 | size_t tail_lines = 0; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 746 | log_time tail_time(log_time::EPOCH); |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 747 | size_t pid = 0; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 748 | bool got_t = false; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 749 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 750 | // object instantiations before goto's can happen |
| 751 | log_device_t unexpected("unexpected", false); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 752 | const char* openDeviceFail = nullptr; |
| 753 | const char* clearFail = nullptr; |
| 754 | const char* setSizeFail = nullptr; |
| 755 | const char* getSizeFail = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 756 | int argc = context->argc; |
| 757 | char* const* argv = context->argv; |
Mark Salyzyn | 65772ca | 2013-12-13 11:10:11 -0800 | [diff] [blame] | 758 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 759 | context->output = stdout; |
| 760 | context->error = stderr; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 761 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 762 | for (int i = 0; i < argc; ++i) { |
| 763 | // Simulate shell stderr redirect parsing |
| 764 | if ((argv[i][0] != '2') || (argv[i][1] != '>')) continue; |
| 765 | |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 766 | // Append to file not implemented, just open file |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 767 | size_t skip = (argv[i][2] == '>') + 2; |
| 768 | if (!strcmp(&argv[i][skip], "/dev/null")) { |
| 769 | context->stderr_null = true; |
| 770 | } else if (!strcmp(&argv[i][skip], "&1")) { |
| 771 | context->stderr_stdout = true; |
| 772 | } else { |
| 773 | // stderr file redirections are not supported |
| 774 | fprintf(context->stderr_stdout ? stdout : stderr, |
| 775 | "stderr redirection to file %s unsupported, skipping\n", |
| 776 | &argv[i][skip]); |
| 777 | } |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 778 | // Only the first one |
| 779 | break; |
| 780 | } |
| 781 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 782 | const char* filename = nullptr; |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 783 | for (int i = 0; i < argc; ++i) { |
| 784 | // Simulate shell stdout redirect parsing |
| 785 | if (argv[i][0] != '>') continue; |
| 786 | |
| 787 | // Append to file not implemented, just open file |
| 788 | filename = &argv[i][(argv[i][1] == '>') + 1]; |
| 789 | // Only the first one |
| 790 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 791 | } |
| 792 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 793 | // Deal with setting up file descriptors and FILE pointers |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 794 | if (context->error_fd >= 0) { // Is an error file descriptor supplied? |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 795 | if (context->error_fd == context->output_fd) { |
| 796 | context->stderr_stdout = true; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 797 | } else if (context->stderr_null) { // redirection told us to close it |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 798 | close(context->error_fd); |
| 799 | context->error_fd = -1; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 800 | } else { // All Ok, convert error to a FILE pointer |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 801 | context->error = fdopen(context->error_fd, "web"); |
| 802 | if (!context->error) { |
| 803 | context->retval = -errno; |
| 804 | fprintf(context->stderr_stdout ? stdout : stderr, |
| 805 | "Failed to fdopen(error_fd=%d) %s\n", context->error_fd, |
| 806 | strerror(errno)); |
| 807 | goto exit; |
| 808 | } |
| 809 | } |
| 810 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 811 | if (context->output_fd >= 0) { // Is an output file descriptor supplied? |
| 812 | if (filename) { // redirect to file, close supplied file descriptor. |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 813 | close(context->output_fd); |
| 814 | context->output_fd = -1; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 815 | } else { // All Ok, convert output to a FILE pointer |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 816 | context->output = fdopen(context->output_fd, "web"); |
| 817 | if (!context->output) { |
| 818 | context->retval = -errno; |
| 819 | fprintf(context->stderr_stdout ? stdout : context->error, |
| 820 | "Failed to fdopen(output_fd=%d) %s\n", |
| 821 | context->output_fd, strerror(errno)); |
| 822 | goto exit; |
| 823 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 824 | } |
| 825 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 826 | if (filename) { // We supplied an output file redirected in command line |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 827 | context->output = fopen(filename, "web"); |
| 828 | } |
| 829 | // Deal with 2>&1 |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 830 | if (context->stderr_stdout) context->error = context->output; |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 831 | // Deal with 2>/dev/null |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 832 | if (context->stderr_null) { |
| 833 | context->error_fd = -1; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 834 | context->error = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 835 | } |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 836 | // Only happens if output=stdout or output=filename |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 837 | if ((context->output_fd < 0) && context->output) { |
| 838 | context->output_fd = fileno(context->output); |
| 839 | } |
| 840 | // Only happens if error=stdout || error=stderr |
| 841 | if ((context->error_fd < 0) && context->error) { |
| 842 | context->error_fd = fileno(context->error); |
| 843 | } |
| 844 | |
| 845 | context->logformat = android_log_format_new(); |
| 846 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 847 | if (argc == 2 && !strcmp(argv[1], "--help")) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 848 | show_help(context); |
| 849 | context->retval = EXIT_SUCCESS; |
| 850 | goto exit; |
| 851 | } |
| 852 | |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 853 | // meant to catch comma-delimited values, but cast a wider |
| 854 | // net for stability dealing with possible mistaken inputs. |
| 855 | static const char delimiters[] = ",:; \t\n\r\f"; |
| 856 | |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 857 | struct getopt_context optctx; |
| 858 | INIT_GETOPT_CONTEXT(optctx); |
| 859 | optctx.opterr = !!context->error; |
| 860 | optctx.optstderr = context->error; |
| 861 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 862 | for (;;) { |
| 863 | int ret; |
| 864 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 865 | int option_index = 0; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 866 | // list of long-argument only strings for later comparison |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 867 | static const char pid_str[] = "pid"; |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 868 | static const char debug_str[] = "debug"; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 869 | static const char id_str[] = "id"; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 870 | static const char wrap_str[] = "wrap"; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 871 | static const char print_str[] = "print"; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 872 | // clang-format off |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 873 | static const struct option long_options[] = { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 874 | { "binary", no_argument, nullptr, 'B' }, |
| 875 | { "buffer", required_argument, nullptr, 'b' }, |
| 876 | { "buffer-size", optional_argument, nullptr, 'g' }, |
| 877 | { "clear", no_argument, nullptr, 'c' }, |
| 878 | { debug_str, no_argument, nullptr, 0 }, |
| 879 | { "dividers", no_argument, nullptr, 'D' }, |
| 880 | { "file", required_argument, nullptr, 'f' }, |
| 881 | { "format", required_argument, nullptr, 'v' }, |
Mark Salyzyn | c18c213 | 2016-04-01 07:52:20 -0700 | [diff] [blame] | 882 | // hidden and undocumented reserved alias for --regex |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 883 | { "grep", required_argument, nullptr, 'e' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 884 | // hidden and undocumented reserved alias for --max-count |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 885 | { "head", required_argument, nullptr, 'm' }, |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 886 | { "help", no_argument, nullptr, 'h' }, |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 887 | { id_str, required_argument, nullptr, 0 }, |
| 888 | { "last", no_argument, nullptr, 'L' }, |
| 889 | { "max-count", required_argument, nullptr, 'm' }, |
| 890 | { pid_str, required_argument, nullptr, 0 }, |
| 891 | { print_str, no_argument, nullptr, 0 }, |
| 892 | { "prune", optional_argument, nullptr, 'p' }, |
| 893 | { "regex", required_argument, nullptr, 'e' }, |
| 894 | { "rotate-count", required_argument, nullptr, 'n' }, |
| 895 | { "rotate-kbytes", required_argument, nullptr, 'r' }, |
| 896 | { "statistics", no_argument, nullptr, 'S' }, |
Mark Salyzyn | d85f646 | 2016-03-30 09:15:09 -0700 | [diff] [blame] | 897 | // hidden and undocumented reserved alias for -t |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 898 | { "tail", required_argument, nullptr, 't' }, |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 899 | // support, but ignore and do not document, the optional argument |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 900 | { wrap_str, optional_argument, nullptr, 0 }, |
| 901 | { nullptr, 0, nullptr, 0 } |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 902 | }; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 903 | // clang-format on |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 904 | |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 905 | ret = getopt_long_r(argc, argv, ":cdDhLt:T:gG:sQf:r:n:v:b:BSpP:m:e:", |
| 906 | long_options, &option_index, &optctx); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 907 | if (ret < 0) break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 908 | |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 909 | switch (ret) { |
| 910 | case 0: |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 911 | // only long options |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 912 | if (long_options[option_index].name == pid_str) { |
| 913 | // ToDo: determine runtime PID_MAX? |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 914 | if (!getSizeTArg(optctx.optarg, &pid, 1)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 915 | logcat_panic(context, HELP_TRUE, "%s %s out of range\n", |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 916 | long_options[option_index].name, |
| 917 | optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 918 | goto exit; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 919 | } |
| 920 | break; |
| 921 | } |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 922 | if (long_options[option_index].name == wrap_str) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 923 | mode |= ANDROID_LOG_WRAP | ANDROID_LOG_RDONLY | |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 924 | ANDROID_LOG_NONBLOCK; |
| 925 | // ToDo: implement API that supports setting a wrap timeout |
| 926 | size_t dummy = ANDROID_LOG_WRAP_DEFAULT_TIMEOUT; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 927 | if (optctx.optarg && |
| 928 | !getSizeTArg(optctx.optarg, &dummy, 1)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 929 | logcat_panic(context, HELP_TRUE, "%s %s out of range\n", |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 930 | long_options[option_index].name, |
| 931 | optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 932 | goto exit; |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 933 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 934 | if ((dummy != ANDROID_LOG_WRAP_DEFAULT_TIMEOUT) && |
| 935 | context->error) { |
| 936 | fprintf(context->error, |
Mark Salyzyn | 41ba25f | 2015-11-30 13:48:56 -0800 | [diff] [blame] | 937 | "WARNING: %s %u seconds, ignoring %zu\n", |
| 938 | long_options[option_index].name, |
| 939 | ANDROID_LOG_WRAP_DEFAULT_TIMEOUT, dummy); |
| 940 | } |
| 941 | break; |
| 942 | } |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 943 | if (long_options[option_index].name == print_str) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 944 | context->printItAnyways = true; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 945 | break; |
| 946 | } |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 947 | if (long_options[option_index].name == debug_str) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 948 | context->debug = true; |
Mark Salyzyn | 538bc12 | 2016-11-16 15:28:31 -0800 | [diff] [blame] | 949 | break; |
| 950 | } |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 951 | if (long_options[option_index].name == id_str) { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 952 | setId = (optctx.optarg && optctx.optarg[0]) ? optctx.optarg |
| 953 | : nullptr; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 954 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 955 | break; |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 956 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 957 | case 's': |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 958 | // default to all silent |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 959 | android_log_addFilterRule(context->logformat, "*:s"); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 960 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 961 | |
| 962 | case 'c': |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 963 | clearLog = true; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 964 | mode |= ANDROID_LOG_WRONLY; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 965 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 966 | |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 967 | case 'L': |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 968 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_PSTORE | |
| 969 | ANDROID_LOG_NONBLOCK; |
| 970 | break; |
Mark Salyzyn | 7c975ac | 2014-12-15 10:01:31 -0800 | [diff] [blame] | 971 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 972 | case 'd': |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 973 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 974 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 975 | |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 976 | case 't': |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 977 | got_t = true; |
Mark Salyzyn | 2d3f38a | 2015-01-26 10:46:44 -0800 | [diff] [blame] | 978 | mode |= ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 979 | // FALLTHRU |
Mark Salyzyn | 5d3d1f1 | 2013-12-09 13:47:00 -0800 | [diff] [blame] | 980 | case 'T': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 981 | if (strspn(optctx.optarg, "0123456789") != |
| 982 | strlen(optctx.optarg)) { |
| 983 | char* cp = parseTime(tail_time, optctx.optarg); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 984 | if (!cp) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 985 | logcat_panic(context, HELP_FALSE, |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 986 | "-%c \"%s\" not in time format\n", ret, |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 987 | optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 988 | goto exit; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 989 | } |
| 990 | if (*cp) { |
| 991 | char c = *cp; |
| 992 | *cp = '\0'; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 993 | if (context->error) { |
| 994 | fprintf( |
| 995 | context->error, |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 996 | "WARNING: -%c \"%s\"\"%c%s\" time truncated\n", |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 997 | ret, optctx.optarg, c, cp + 1); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 998 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 999 | *cp = c; |
| 1000 | } |
| 1001 | } else { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1002 | if (!getSizeTArg(optctx.optarg, &tail_lines, 1)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1003 | if (context->error) { |
| 1004 | fprintf(context->error, |
| 1005 | "WARNING: -%c %s invalid, setting to 1\n", |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1006 | ret, optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1007 | } |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1008 | tail_lines = 1; |
| 1009 | } |
| 1010 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1011 | break; |
Dan Egnor | d1d3b6d | 2010-03-11 20:32:17 -0800 | [diff] [blame] | 1012 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1013 | case 'D': |
| 1014 | printDividers = true; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1015 | break; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1016 | |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 1017 | case 'e': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1018 | context->regex = new pcrecpp::RE(optctx.optarg); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1019 | break; |
Casey Dahlin | dc42a87 | 2016-03-17 16:18:55 -0700 | [diff] [blame] | 1020 | |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1021 | case 'm': { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1022 | if (!getSizeTArg(optctx.optarg, &context->maxCount)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1023 | logcat_panic(context, HELP_FALSE, |
| 1024 | "-%c \"%s\" isn't an " |
| 1025 | "integer greater than zero\n", |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1026 | ret, optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1027 | goto exit; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1028 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1029 | } break; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1030 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1031 | case 'g': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1032 | if (!optctx.optarg) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1033 | getLogSize = true; |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 1034 | break; |
| 1035 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1036 | // FALLTHRU |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1037 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1038 | case 'G': { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1039 | char* cp; |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1040 | if (strtoll(optctx.optarg, &cp, 0) > 0) { |
| 1041 | setLogSize = strtoll(optctx.optarg, &cp, 0); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1042 | } else { |
| 1043 | setLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1044 | } |
| 1045 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1046 | switch (*cp) { |
| 1047 | case 'g': |
| 1048 | case 'G': |
| 1049 | setLogSize *= 1024; |
| 1050 | // FALLTHRU |
| 1051 | case 'm': |
| 1052 | case 'M': |
| 1053 | setLogSize *= 1024; |
| 1054 | // FALLTHRU |
| 1055 | case 'k': |
| 1056 | case 'K': |
| 1057 | setLogSize *= 1024; |
| 1058 | // FALLTHRU |
| 1059 | case '\0': |
| 1060 | break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1061 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1062 | default: |
| 1063 | setLogSize = 0; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | if (!setLogSize) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1067 | logcat_panic(context, HELP_FALSE, |
| 1068 | "ERROR: -G <num><multiplier>\n"); |
| 1069 | goto exit; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1070 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1071 | } break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1072 | |
| 1073 | case 'p': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1074 | if (!optctx.optarg) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1075 | getPruneList = true; |
Mark Salyzyn | f8bff87 | 2015-11-30 12:57:56 -0800 | [diff] [blame] | 1076 | break; |
| 1077 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1078 | // FALLTHRU |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1079 | |
| 1080 | case 'P': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1081 | setPruneList = optctx.optarg; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1082 | break; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1083 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1084 | case 'b': { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1085 | std::unique_ptr<char, void (*)(void*)> buffers( |
| 1086 | strdup(optctx.optarg), free); |
| 1087 | char* arg = buffers.get(); |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1088 | unsigned idMask = 0; |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1089 | char* sv = nullptr; // protect against -ENOMEM above |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1090 | while (!!(arg = strtok_r(arg, delimiters, &sv))) { |
| 1091 | if (!strcmp(arg, "default")) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1092 | idMask |= (1 << LOG_ID_MAIN) | (1 << LOG_ID_SYSTEM) | |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1093 | (1 << LOG_ID_CRASH); |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1094 | } else if (!strcmp(arg, "all")) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1095 | allSelected = true; |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1096 | idMask = (unsigned)-1; |
| 1097 | } else { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1098 | log_id_t log_id = android_name_to_log_id(arg); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1099 | const char* name = android_log_id_to_name(log_id); |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1100 | |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1101 | if (!!strcmp(name, arg)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1102 | logcat_panic(context, HELP_TRUE, |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1103 | "unknown buffer %s\n", arg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1104 | goto exit; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1105 | } |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1106 | if (log_id == LOG_ID_SECURITY) allSelected = false; |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1107 | idMask |= (1 << log_id); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1108 | } |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1109 | arg = nullptr; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1112 | for (int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1113 | const char* name = android_log_id_to_name((log_id_t)i); |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1114 | log_id_t log_id = android_name_to_log_id(name); |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1115 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1116 | if (log_id != (log_id_t)i) continue; |
| 1117 | if (!(idMask & (1 << i))) continue; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1118 | |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1119 | bool found = false; |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1120 | for (dev = context->devices; dev; dev = dev->next) { |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1121 | if (!strcmp(name, dev->device)) { |
| 1122 | found = true; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1123 | break; |
| 1124 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1125 | if (!dev->next) break; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1126 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1127 | if (found) continue; |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1128 | |
Stefan Lafon | 701a065 | 2017-08-24 20:14:06 -0700 | [diff] [blame] | 1129 | bool binary = !strcmp(name, "events") || |
| 1130 | !strcmp(name, "security") || |
| 1131 | !strcmp(name, "stats"); |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1132 | log_device_t* d = new log_device_t(name, binary); |
| 1133 | |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1134 | if (dev) { |
Mark Salyzyn | 4517773 | 2016-04-11 14:03:48 -0700 | [diff] [blame] | 1135 | dev->next = d; |
| 1136 | dev = d; |
| 1137 | } else { |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1138 | context->devices = dev = d; |
Mark Salyzyn | 083b037 | 2015-12-04 10:59:45 -0800 | [diff] [blame] | 1139 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1140 | context->devCount++; |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1141 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1142 | } break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1143 | |
| 1144 | case 'B': |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1145 | context->printBinary = 1; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1146 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1147 | |
| 1148 | case 'f': |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1149 | if ((tail_time == log_time::EPOCH) && !tail_lines) { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1150 | tail_time = lastLogTime(optctx.optarg); |
Mark Salyzyn | f3555d9 | 2015-05-27 07:39:56 -0700 | [diff] [blame] | 1151 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1152 | // redirect output to a file |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1153 | context->outputFileName = optctx.optarg; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1154 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1155 | |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 1156 | case 'r': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1157 | if (!getSizeTArg(optctx.optarg, &context->logRotateSizeKBytes, |
| 1158 | 1)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1159 | logcat_panic(context, HELP_TRUE, |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1160 | "Invalid parameter \"%s\" to -r\n", |
| 1161 | optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1162 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1163 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1164 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1165 | |
Mark Salyzyn | 1325ebf | 2016-06-07 13:03:10 -0700 | [diff] [blame] | 1166 | case 'n': |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1167 | if (!getSizeTArg(optctx.optarg, &context->maxRotatedLogs, 1)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1168 | logcat_panic(context, HELP_TRUE, |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1169 | "Invalid parameter \"%s\" to -n\n", |
| 1170 | optctx.optarg); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1171 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1172 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1173 | break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1174 | |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1175 | case 'v': { |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1176 | if (!strcmp(optctx.optarg, "help") || |
| 1177 | !strcmp(optctx.optarg, "--help")) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1178 | show_format_help(context); |
| 1179 | context->retval = EXIT_SUCCESS; |
| 1180 | goto exit; |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1181 | } |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1182 | std::unique_ptr<char, void (*)(void*)> formats( |
| 1183 | strdup(optctx.optarg), free); |
| 1184 | char* arg = formats.get(); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1185 | char* sv = nullptr; // protect against -ENOMEM above |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1186 | while (!!(arg = strtok_r(arg, delimiters, &sv))) { |
| 1187 | err = setLogFormat(context, arg); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1188 | if (err < 0) { |
| 1189 | logcat_panic(context, HELP_FORMAT, |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1190 | "Invalid parameter \"%s\" to -v\n", arg); |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1191 | goto exit; |
| 1192 | } |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1193 | arg = nullptr; |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1194 | if (err) hasSetLogFormat = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1195 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1196 | } break; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1197 | |
| 1198 | case 'Q': |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1199 | #define LOGCAT_FILTER "androidboot.logcat=" |
| 1200 | #define CONSOLE_PIPE_OPTION "androidboot.consolepipe=" |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1201 | #define CONSOLE_OPTION "androidboot.console=" |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1202 | #define QEMU_PROPERTY "ro.kernel.qemu" |
| 1203 | #define QEMU_CMDLINE "qemu.cmdline" |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1204 | // This is a *hidden* option used to start a version of logcat |
| 1205 | // in an emulated device only. It basically looks for |
| 1206 | // androidboot.logcat= on the kernel command line. If |
| 1207 | // something is found, it extracts a log filter and uses it to |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1208 | // run the program. The logcat output will go to consolepipe if |
| 1209 | // androiboot.consolepipe (e.g. qemu_pipe) is given, otherwise, |
| 1210 | // it goes to androidboot.console (e.g. tty) |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1211 | { |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1212 | // if not in emulator, exit quietly |
| 1213 | if (false == android::base::GetBoolProperty(QEMU_PROPERTY, false)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1214 | context->retval = EXIT_SUCCESS; |
| 1215 | goto exit; |
| 1216 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1217 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1218 | std::string cmdline = android::base::GetProperty(QEMU_CMDLINE, ""); |
| 1219 | if (cmdline.empty()) { |
| 1220 | android::base::ReadFileToString("/proc/cmdline", &cmdline); |
| 1221 | } |
| 1222 | |
| 1223 | const char* logcatFilter = strstr(cmdline.c_str(), LOGCAT_FILTER); |
| 1224 | // if nothing found or invalid filters, exit quietly |
| 1225 | if (!logcatFilter) { |
| 1226 | context->retval = EXIT_SUCCESS; |
| 1227 | goto exit; |
| 1228 | } |
| 1229 | |
| 1230 | const char* p = logcatFilter + strlen(LOGCAT_FILTER); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1231 | const char* q = strpbrk(p, " \t\n\r"); |
| 1232 | if (!q) q = p + strlen(p); |
| 1233 | forceFilters = std::string(p, q); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1234 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1235 | // redirect our output to the emulator console pipe or console |
| 1236 | const char* consolePipe = |
| 1237 | strstr(cmdline.c_str(), CONSOLE_PIPE_OPTION); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1238 | const char* console = |
| 1239 | strstr(cmdline.c_str(), CONSOLE_OPTION); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1240 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1241 | if (consolePipe) { |
| 1242 | p = consolePipe + strlen(CONSOLE_PIPE_OPTION); |
| 1243 | } else if (console) { |
| 1244 | p = console + strlen(CONSOLE_OPTION); |
| 1245 | } else { |
| 1246 | context->retval = EXIT_FAILURE; |
| 1247 | goto exit; |
| 1248 | } |
| 1249 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1250 | q = strpbrk(p, " \t\n\r"); |
| 1251 | int len = q ? q - p : strlen(p); |
| 1252 | std::string devname = "/dev/" + std::string(p, len); |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1253 | std::string pipePurpose("pipe:logcat"); |
| 1254 | if (consolePipe) { |
| 1255 | // example: "qemu_pipe,pipe:logcat" |
| 1256 | // upon opening of /dev/qemu_pipe, the "pipe:logcat" |
| 1257 | // string with trailing '\0' should be written to the fd |
Chih-Hung Hsieh | e5d975c | 2017-08-03 13:56:49 -0700 | [diff] [blame] | 1258 | size_t pos = devname.find(','); |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1259 | if (pos != std::string::npos) { |
| 1260 | pipePurpose = devname.substr(pos + 1); |
| 1261 | devname = devname.substr(0, pos); |
| 1262 | } |
| 1263 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1264 | cmdline.erase(); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1265 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1266 | if (context->error) { |
| 1267 | fprintf(context->error, "logcat using %s\n", |
| 1268 | devname.c_str()); |
| 1269 | } |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1270 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1271 | FILE* fp = fopen(devname.c_str(), "web"); |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1272 | devname.erase(); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1273 | if (!fp) break; |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1274 | |
bohu | 94aab86 | 2017-02-21 14:31:19 -0800 | [diff] [blame] | 1275 | if (consolePipe) { |
| 1276 | // need the trailing '\0' |
| 1277 | if(!android::base::WriteFully(fileno(fp), pipePurpose.c_str(), |
| 1278 | pipePurpose.size() + 1)) { |
| 1279 | fclose(fp); |
| 1280 | context->retval = EXIT_FAILURE; |
| 1281 | goto exit; |
| 1282 | } |
| 1283 | } |
| 1284 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1285 | // close output and error channels, replace with console |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1286 | android::close_output(context); |
| 1287 | android::close_error(context); |
| 1288 | context->stderr_stdout = true; |
| 1289 | context->output = fp; |
Mark Salyzyn | f9dbdbc | 2017-02-21 14:45:58 -0800 | [diff] [blame] | 1290 | context->output_fd = fileno(fp); |
| 1291 | if (context->stderr_null) break; |
| 1292 | context->stderr_stdout = true; |
| 1293 | context->error = fp; |
| 1294 | context->error_fd = fileno(fp); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1295 | } |
| 1296 | break; |
| 1297 | |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1298 | case 'S': |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1299 | printStatistics = true; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1300 | break; |
| 1301 | |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1302 | case ':': |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1303 | logcat_panic(context, HELP_TRUE, |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1304 | "Option -%c needs an argument\n", optctx.optopt); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1305 | goto exit; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1306 | |
Mark Salyzyn | e74e51d | 2017-04-03 09:30:20 -0700 | [diff] [blame] | 1307 | case 'h': |
| 1308 | show_help(context); |
| 1309 | show_format_help(context); |
| 1310 | goto exit; |
| 1311 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1312 | default: |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1313 | logcat_panic(context, HELP_TRUE, "Unrecognized Option %c\n", |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1314 | optctx.optopt); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1315 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1316 | } |
| 1317 | } |
| 1318 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1319 | if (context->maxCount && got_t) { |
| 1320 | logcat_panic(context, HELP_TRUE, |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1321 | "Cannot use -m (--max-count) and -t together\n"); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1322 | goto exit; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1323 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1324 | if (context->printItAnyways && (!context->regex || !context->maxCount)) { |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 1325 | // One day it would be nice if --print -v color and --regex <expr> |
| 1326 | // could play with each other and show regex highlighted content. |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1327 | // clang-format off |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1328 | if (context->error) { |
| 1329 | fprintf(context->error, "WARNING: " |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 1330 | "--print ignored, to be used in combination with\n" |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1331 | " " |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 1332 | "--regex <expr> and --max-count <N>\n"); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1333 | } |
| 1334 | context->printItAnyways = false; |
Mark Salyzyn | c920277 | 2016-03-30 09:38:31 -0700 | [diff] [blame] | 1335 | } |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1336 | |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1337 | if (!context->devices) { |
| 1338 | dev = context->devices = new log_device_t("main", false); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1339 | context->devCount = 1; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1340 | if (android_name_to_log_id("system") == LOG_ID_SYSTEM) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1341 | dev = dev->next = new log_device_t("system", false); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1342 | context->devCount++; |
Joe Onorato | e2bf2ea | 2010-03-01 09:11:54 -0800 | [diff] [blame] | 1343 | } |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 1344 | if (android_name_to_log_id("crash") == LOG_ID_CRASH) { |
Mark Salyzyn | 5f6738a | 2015-02-27 13:41:34 -0800 | [diff] [blame] | 1345 | dev = dev->next = new log_device_t("crash", false); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1346 | context->devCount++; |
Mark Salyzyn | 99f47a9 | 2014-04-07 14:58:08 -0700 | [diff] [blame] | 1347 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1350 | if (!!context->logRotateSizeKBytes && !context->outputFileName) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1351 | logcat_panic(context, HELP_TRUE, "-r requires -f as well\n"); |
| 1352 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1353 | } |
| 1354 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1355 | if (!!setId) { |
| 1356 | if (!context->outputFileName) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1357 | logcat_panic(context, HELP_TRUE, |
| 1358 | "--id='%s' requires -f as well\n", setId); |
| 1359 | goto exit; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1362 | std::string file_name = android::base::StringPrintf( |
| 1363 | "%s.id", context->outputFileName); |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1364 | std::string file; |
| 1365 | bool file_ok = android::base::ReadFileToString(file_name, &file); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1366 | android::base::WriteStringToFile(setId, file_name, S_IRUSR | S_IWUSR, |
| 1367 | getuid(), getgid()); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1368 | if (!file_ok || !file.compare(setId)) setId = nullptr; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1369 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1370 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1371 | if (!hasSetLogFormat) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1372 | const char* logFormat = android::getenv(context, "ANDROID_PRINTF_LOG"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1373 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1374 | if (!!logFormat) { |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1375 | std::unique_ptr<char, void (*)(void*)> formats(strdup(logFormat), |
| 1376 | free); |
| 1377 | char* sv = nullptr; // protect against -ENOMEM above |
| 1378 | char* arg = formats.get(); |
| 1379 | while (!!(arg = strtok_r(arg, delimiters, &sv))) { |
| 1380 | err = setLogFormat(context, arg); |
| 1381 | // environment should not cause crash of logcat |
| 1382 | if ((err < 0) && context->error) { |
| 1383 | fprintf(context->error, |
| 1384 | "invalid format in ANDROID_PRINTF_LOG '%s'\n", arg); |
| 1385 | } |
| 1386 | arg = nullptr; |
| 1387 | if (err > 0) hasSetLogFormat = true; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1388 | } |
Mark Salyzyn | b45a175 | 2017-02-28 09:20:31 -0800 | [diff] [blame] | 1389 | } |
| 1390 | if (!hasSetLogFormat) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1391 | setLogFormat(context, "threadtime"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
Mark Salyzyn | f329029 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1395 | if (forceFilters.size()) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1396 | err = android_log_addFilterString(context->logformat, |
| 1397 | forceFilters.c_str()); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1398 | if (err < 0) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1399 | logcat_panic(context, HELP_FALSE, |
Mark Salyzyn | 4fd0507 | 2016-10-18 11:30:11 -0700 | [diff] [blame] | 1400 | "Invalid filter expression in logcat args\n"); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1401 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1402 | } |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1403 | } else if (argc == optctx.optind) { |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1404 | // Add from environment variable |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1405 | const char* env_tags_orig = android::getenv(context, "ANDROID_LOG_TAGS"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1406 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1407 | if (!!env_tags_orig) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1408 | err = android_log_addFilterString(context->logformat, |
| 1409 | env_tags_orig); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1410 | |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1411 | if (err < 0) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1412 | logcat_panic(context, HELP_TRUE, |
| 1413 | "Invalid filter expression in ANDROID_LOG_TAGS\n"); |
| 1414 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1415 | } |
| 1416 | } |
| 1417 | } else { |
| 1418 | // Add from commandline |
Mark Salyzyn | e9ade17 | 2017-03-02 15:09:41 -0800 | [diff] [blame] | 1419 | for (int i = optctx.optind ; i < argc ; i++) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1420 | // skip stderr redirections of _all_ kinds |
| 1421 | if ((argv[i][0] == '2') && (argv[i][1] == '>')) continue; |
Mark Salyzyn | e3d0c96 | 2017-02-17 13:15:51 -0800 | [diff] [blame] | 1422 | // skip stdout redirections of _all_ kinds |
| 1423 | if (argv[i][0] == '>') continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1424 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1425 | err = android_log_addFilterString(context->logformat, argv[i]); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1426 | if (err < 0) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1427 | logcat_panic(context, HELP_TRUE, |
| 1428 | "Invalid filter expression '%s'\n", argv[i]); |
| 1429 | goto exit; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1434 | dev = context->devices; |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1435 | if (tail_time != log_time::EPOCH) { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 1436 | logger_list = android_logger_list_alloc_time(mode, tail_time, pid); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1437 | } else { |
Kristian Monsen | 562e513 | 2015-06-05 14:10:12 -0700 | [diff] [blame] | 1438 | logger_list = android_logger_list_alloc(mode, tail_lines, pid); |
Mark Salyzyn | fa3716b | 2014-02-14 16:05:05 -0800 | [diff] [blame] | 1439 | } |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1440 | // We have three orthogonal actions below to clear, set log size and |
| 1441 | // get log size. All sharing the same iteration loop. |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1442 | while (dev) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1443 | dev->logger_list = logger_list; |
| 1444 | dev->logger = android_logger_open(logger_list, |
| 1445 | android_name_to_log_id(dev->device)); |
| 1446 | if (!dev->logger) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1447 | reportErrorName(&openDeviceFail, dev->device, allSelected); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1448 | dev = dev->next; |
| 1449 | continue; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1450 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1451 | |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1452 | if (clearLog || setId) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1453 | if (context->outputFileName) { |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1454 | int maxRotationCountDigits = |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1455 | (context->maxRotatedLogs > 0) ? |
| 1456 | (int)(floor(log10(context->maxRotatedLogs) + 1)) : |
| 1457 | 0; |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1458 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1459 | for (int i = context->maxRotatedLogs ; i >= 0 ; --i) { |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1460 | std::string file; |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1461 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1462 | if (!i) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1463 | file = android::base::StringPrintf( |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1464 | "%s", context->outputFileName); |
| 1465 | } else { |
| 1466 | file = android::base::StringPrintf("%s.%.*d", |
| 1467 | context->outputFileName, maxRotationCountDigits, i); |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1468 | } |
| 1469 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1470 | if (!file.length()) { |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1471 | perror("while clearing log files"); |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1472 | reportErrorName(&clearFail, dev->device, allSelected); |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | |
Mark Salyzyn | 5b1a538 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1476 | err = unlink(file.c_str()); |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1477 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1478 | if (err < 0 && errno != ENOENT && !clearFail) { |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1479 | perror("while clearing log files"); |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1480 | reportErrorName(&clearFail, dev->device, allSelected); |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1481 | } |
Mark Salyzyn | b7d059b | 2016-06-06 14:56:00 -0700 | [diff] [blame] | 1482 | } |
| 1483 | } else if (android_logger_clear(dev->logger)) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1484 | reportErrorName(&clearFail, dev->device, allSelected); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1485 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1486 | } |
| 1487 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1488 | if (setLogSize) { |
| 1489 | if (android_logger_set_log_size(dev->logger, setLogSize)) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1490 | reportErrorName(&setSizeFail, dev->device, allSelected); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1491 | } |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1494 | if (getLogSize) { |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1495 | long size = android_logger_get_log_size(dev->logger); |
| 1496 | long readable = android_logger_get_log_readable_size(dev->logger); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1497 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1498 | if ((size < 0) || (readable < 0)) { |
Mark Salyzyn | 26a1fac | 2017-01-20 14:07:29 -0800 | [diff] [blame] | 1499 | reportErrorName(&getSizeFail, dev->device, allSelected); |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1500 | } else { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1501 | std::string str = android::base::StringPrintf( |
| 1502 | "%s: ring buffer is %ld%sb (%ld%sb consumed)," |
| 1503 | " max entry is %db, max payload is %db\n", |
| 1504 | dev->device, |
| 1505 | value_of_size(size), multiplier_of_size(size), |
| 1506 | value_of_size(readable), multiplier_of_size(readable), |
| 1507 | (int)LOGGER_ENTRY_MAX_LEN, |
| 1508 | (int)LOGGER_ENTRY_MAX_PAYLOAD); |
| 1509 | TEMP_FAILURE_RETRY(write(context->output_fd, |
| 1510 | str.data(), str.length())); |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1511 | } |
Joe Onorato | 6fa09a0 | 2010-02-26 10:04:23 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | dev = dev->next; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1515 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1516 | |
| 1517 | context->retval = EXIT_SUCCESS; |
| 1518 | |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1519 | // report any errors in the above loop and exit |
| 1520 | if (openDeviceFail) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1521 | logcat_panic(context, HELP_FALSE, |
| 1522 | "Unable to open log device '%s'\n", openDeviceFail); |
| 1523 | goto close; |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1524 | } |
| 1525 | if (clearFail) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1526 | logcat_panic(context, HELP_FALSE, |
| 1527 | "failed to clear the '%s' log\n", clearFail); |
| 1528 | goto close; |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1529 | } |
| 1530 | if (setSizeFail) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1531 | logcat_panic(context, HELP_FALSE, |
| 1532 | "failed to set the '%s' log size\n", setSizeFail); |
| 1533 | goto close; |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1534 | } |
| 1535 | if (getSizeFail) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1536 | logcat_panic(context, HELP_FALSE, |
| 1537 | "failed to get the readable '%s' log size", getSizeFail); |
| 1538 | goto close; |
Mark Salyzyn | 603b8e5 | 2015-09-16 15:34:00 -0700 | [diff] [blame] | 1539 | } |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1540 | |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1541 | if (setPruneList) { |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1542 | size_t len = strlen(setPruneList); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1543 | // extra 32 bytes are needed by android_logger_set_prune_list |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1544 | size_t bLen = len + 32; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1545 | char* buf = nullptr; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1546 | if (asprintf(&buf, "%-*s", (int)(bLen - 1), setPruneList) > 0) { |
| 1547 | buf[len] = '\0'; |
| 1548 | if (android_logger_set_prune_list(logger_list, buf, bLen)) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1549 | logcat_panic(context, HELP_FALSE, |
| 1550 | "failed to set the prune list"); |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1551 | } |
| 1552 | free(buf); |
| 1553 | } else { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1554 | logcat_panic(context, HELP_FALSE, |
| 1555 | "failed to set the prune list (alloc)"); |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1556 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1557 | goto close; |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1558 | } |
| 1559 | |
Mark Salyzyn | 1c95047 | 2014-04-01 17:19:47 -0700 | [diff] [blame] | 1560 | if (printStatistics || getPruneList) { |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1561 | size_t len = 8192; |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1562 | char* buf; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1563 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1564 | for (int retry = 32; (retry >= 0) && ((buf = new char[len])); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1565 | delete[] buf, buf = nullptr, --retry) { |
Mark Salyzyn | dfa7a07 | 2014-02-11 12:29:31 -0800 | [diff] [blame] | 1566 | if (getPruneList) { |
| 1567 | android_logger_get_prune_list(logger_list, buf, len); |
| 1568 | } else { |
| 1569 | android_logger_get_statistics(logger_list, buf, len); |
| 1570 | } |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1571 | buf[len - 1] = '\0'; |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1572 | if (atol(buf) < 3) { |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1573 | delete[] buf; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1574 | buf = nullptr; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1575 | break; |
| 1576 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1577 | size_t ret = atol(buf) + 1; |
| 1578 | if (ret <= len) { |
| 1579 | len = ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1580 | break; |
| 1581 | } |
Traian Schiau | 5976303 | 2015-04-10 15:51:39 +0300 | [diff] [blame] | 1582 | len = ret; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1583 | } |
| 1584 | |
| 1585 | if (!buf) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1586 | logcat_panic(context, HELP_FALSE, "failed to read data"); |
| 1587 | goto close; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | // remove trailing FF |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1591 | char* cp = buf + len - 1; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1592 | *cp = '\0'; |
| 1593 | bool truncated = *--cp != '\f'; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1594 | if (!truncated) *cp = '\0'; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1595 | |
| 1596 | // squash out the byte count |
| 1597 | cp = buf; |
| 1598 | if (!truncated) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1599 | while (isdigit(*cp)) ++cp; |
| 1600 | if (*cp == '\n') ++cp; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1603 | len = strlen(cp); |
| 1604 | TEMP_FAILURE_RETRY(write(context->output_fd, cp, len)); |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1605 | delete[] buf; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1606 | goto close; |
Mark Salyzyn | 34facab | 2014-02-06 14:48:50 -0800 | [diff] [blame] | 1607 | } |
| 1608 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1609 | if (getLogSize || setLogSize || clearLog) goto close; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1610 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1611 | setupOutputAndSchedulingPolicy(context, !(mode & ANDROID_LOG_NONBLOCK)); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1612 | if (context->stop) goto close; |
Mark Salyzyn | 02687e7 | 2016-08-03 14:20:41 -0700 | [diff] [blame] | 1613 | |
Mark Salyzyn | 5f60660 | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1614 | // LOG_EVENT_INT(10, 12345); |
| 1615 | // LOG_EVENT_LONG(11, 0x1122334455667788LL); |
| 1616 | // LOG_EVENT_STRING(0, "whassup, doc?"); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1617 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1618 | dev = nullptr; |
Casey Dahlin | 6ac498d | 2016-03-17 14:04:52 -0700 | [diff] [blame] | 1619 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1620 | while (!context->stop && |
| 1621 | (!context->maxCount || (context->printCount < context->maxCount))) { |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1622 | struct log_msg log_msg; |
| 1623 | int ret = android_logger_list_read(logger_list, &log_msg); |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1624 | if (!ret) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1625 | logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n"); |
| 1626 | break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1627 | } |
| 1628 | |
| 1629 | if (ret < 0) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1630 | if (ret == -EAGAIN) break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1631 | |
| 1632 | if (ret == -EIO) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1633 | logcat_panic(context, HELP_FALSE, "read: unexpected EOF!\n"); |
| 1634 | break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1635 | } |
| 1636 | if (ret == -EINVAL) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1637 | logcat_panic(context, HELP_FALSE, "read: unexpected length.\n"); |
| 1638 | break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1639 | } |
Luca Stefani | 0870514 | 2017-07-08 17:48:00 +0200 | [diff] [blame] | 1640 | logcat_panic(context, HELP_FALSE, "logcat read failure\n"); |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1641 | break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1642 | } |
| 1643 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1644 | log_device_t* d; |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1645 | for (d = context->devices; d; d = d->next) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1646 | if (android_name_to_log_id(d->device) == log_msg.id()) break; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1647 | } |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1648 | if (!d) { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1649 | context->devCount = 2; // set to Multiple |
Mark Salyzyn | 9421b0c | 2015-02-26 14:33:35 -0800 | [diff] [blame] | 1650 | d = &unexpected; |
| 1651 | d->binary = log_msg.id() == LOG_ID_EVENTS; |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1652 | } |
| 1653 | |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1654 | if (dev != d) { |
| 1655 | dev = d; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1656 | maybePrintStart(context, dev, printDividers); |
| 1657 | if (context->stop) break; |
Mark Salyzyn | 7b30ff8 | 2015-01-26 13:41:33 -0800 | [diff] [blame] | 1658 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1659 | if (context->printBinary) { |
| 1660 | printBinary(context, &log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1661 | } else { |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1662 | processBuffer(context, dev, &log_msg); |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1666 | close: |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1667 | // Short and sweet. Implemented generic version in android_logcat_destroy. |
| 1668 | while (!!(dev = context->devices)) { |
| 1669 | context->devices = dev->next; |
| 1670 | delete dev; |
| 1671 | } |
Mark Salyzyn | 95132e9 | 2013-11-22 10:55:48 -0800 | [diff] [blame] | 1672 | android_logger_list_free(logger_list); |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1673 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1674 | exit: |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1675 | // close write end of pipe to help things along |
| 1676 | if (context->output_fd == context->fds[1]) { |
| 1677 | android::close_output(context); |
| 1678 | } |
| 1679 | if (context->error_fd == context->fds[1]) { |
| 1680 | android::close_error(context); |
| 1681 | } |
| 1682 | if (context->fds[1] >= 0) { |
| 1683 | // NB: should be closed by the above |
| 1684 | int save_errno = errno; |
| 1685 | close(context->fds[1]); |
| 1686 | errno = save_errno; |
| 1687 | context->fds[1] = -1; |
| 1688 | } |
| 1689 | context->thread_stopped = true; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1690 | return context->retval; |
| 1691 | } |
| 1692 | |
| 1693 | // Can block |
| 1694 | int android_logcat_run_command(android_logcat_context ctx, |
| 1695 | int output, int error, |
| 1696 | int argc, char* const* argv, |
| 1697 | char* const* envp) { |
| 1698 | android_logcat_context_internal* context = ctx; |
| 1699 | |
| 1700 | context->output_fd = output; |
| 1701 | context->error_fd = error; |
| 1702 | context->argc = argc; |
| 1703 | context->argv = argv; |
| 1704 | context->envp = envp; |
| 1705 | context->stop = false; |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1706 | context->thread_stopped = false; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1707 | return __logcat(context); |
| 1708 | } |
| 1709 | |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1710 | // starts a thread, opens a pipe, returns reading end. |
| 1711 | int android_logcat_run_command_thread(android_logcat_context ctx, |
| 1712 | int argc, char* const* argv, |
| 1713 | char* const* envp) { |
| 1714 | android_logcat_context_internal* context = ctx; |
| 1715 | |
| 1716 | int save_errno = EBUSY; |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1717 | if ((context->fds[0] >= 0) || (context->fds[1] >= 0)) goto exit; |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1718 | |
| 1719 | if (pipe(context->fds) < 0) { |
| 1720 | save_errno = errno; |
| 1721 | goto exit; |
| 1722 | } |
| 1723 | |
| 1724 | pthread_attr_t attr; |
| 1725 | if (pthread_attr_init(&attr)) { |
| 1726 | save_errno = errno; |
| 1727 | goto close_exit; |
| 1728 | } |
| 1729 | |
| 1730 | struct sched_param param; |
| 1731 | memset(¶m, 0, sizeof(param)); |
| 1732 | pthread_attr_setschedparam(&attr, ¶m); |
| 1733 | pthread_attr_setschedpolicy(&attr, SCHED_BATCH); |
| 1734 | if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)) { |
Mark Salyzyn | 210e43c | 2017-08-04 16:28:47 -0700 | [diff] [blame] | 1735 | save_errno = errno; |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1736 | goto pthread_attr_exit; |
| 1737 | } |
| 1738 | |
| 1739 | context->stop = false; |
| 1740 | context->thread_stopped = false; |
| 1741 | context->output_fd = context->fds[1]; |
| 1742 | // save off arguments so they remain while thread is active. |
| 1743 | for (int i = 0; i < argc; ++i) { |
| 1744 | context->args.push_back(std::string(argv[i])); |
| 1745 | } |
| 1746 | // save off environment so they remain while thread is active. |
| 1747 | if (envp) for (size_t i = 0; envp[i]; ++i) { |
| 1748 | context->envs.push_back(std::string(envp[i])); |
| 1749 | } |
| 1750 | |
| 1751 | for (auto& str : context->args) { |
| 1752 | context->argv_hold.push_back(str.c_str()); |
| 1753 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1754 | context->argv_hold.push_back(nullptr); |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1755 | for (auto& str : context->envs) { |
| 1756 | context->envp_hold.push_back(str.c_str()); |
| 1757 | } |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1758 | context->envp_hold.push_back(nullptr); |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1759 | |
| 1760 | context->argc = context->argv_hold.size() - 1; |
| 1761 | context->argv = (char* const*)&context->argv_hold[0]; |
| 1762 | context->envp = (char* const*)&context->envp_hold[0]; |
| 1763 | |
| 1764 | #ifdef DEBUG |
| 1765 | fprintf(stderr, "argv[%d] = {", context->argc); |
| 1766 | for (auto str : context->argv_hold) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1767 | fprintf(stderr, " \"%s\"", str ?: "nullptr"); |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1768 | } |
| 1769 | fprintf(stderr, " }\n"); |
| 1770 | fflush(stderr); |
| 1771 | #endif |
| 1772 | context->retval = EXIT_SUCCESS; |
| 1773 | if (pthread_create(&context->thr, &attr, |
| 1774 | (void*(*)(void*))__logcat, context)) { |
Mark Salyzyn | 210e43c | 2017-08-04 16:28:47 -0700 | [diff] [blame] | 1775 | save_errno = errno; |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1776 | goto argv_exit; |
| 1777 | } |
| 1778 | pthread_attr_destroy(&attr); |
| 1779 | |
| 1780 | return context->fds[0]; |
| 1781 | |
| 1782 | argv_exit: |
| 1783 | context->argv_hold.clear(); |
| 1784 | context->args.clear(); |
| 1785 | context->envp_hold.clear(); |
| 1786 | context->envs.clear(); |
| 1787 | pthread_attr_exit: |
| 1788 | pthread_attr_destroy(&attr); |
| 1789 | close_exit: |
| 1790 | close(context->fds[0]); |
| 1791 | context->fds[0] = -1; |
| 1792 | close(context->fds[1]); |
| 1793 | context->fds[1] = -1; |
| 1794 | exit: |
| 1795 | errno = save_errno; |
| 1796 | context->stop = true; |
| 1797 | context->thread_stopped = true; |
| 1798 | context->retval = EXIT_FAILURE; |
| 1799 | return -1; |
| 1800 | } |
| 1801 | |
| 1802 | // test if the thread is still doing 'stuff' |
| 1803 | int android_logcat_run_command_thread_running(android_logcat_context ctx) { |
| 1804 | android_logcat_context_internal* context = ctx; |
| 1805 | |
| 1806 | return context->thread_stopped == false; |
| 1807 | } |
| 1808 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1809 | // Finished with context |
| 1810 | int android_logcat_destroy(android_logcat_context* ctx) { |
| 1811 | android_logcat_context_internal* context = *ctx; |
| 1812 | |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1813 | if (!context) return -EBADF; |
| 1814 | |
| 1815 | *ctx = nullptr; |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1816 | |
| 1817 | context->stop = true; |
| 1818 | |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1819 | while (context->thread_stopped == false) { |
Mark Salyzyn | de022a8 | 2017-03-01 08:30:06 -0800 | [diff] [blame] | 1820 | // Makes me sad, replace thread_stopped with semaphore. Short lived. |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1821 | sched_yield(); |
| 1822 | } |
| 1823 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1824 | delete context->regex; |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1825 | context->argv_hold.clear(); |
| 1826 | context->args.clear(); |
| 1827 | context->envp_hold.clear(); |
| 1828 | context->envs.clear(); |
| 1829 | if (context->fds[0] >= 0) { |
| 1830 | close(context->fds[0]); |
| 1831 | context->fds[0] = -1; |
| 1832 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1833 | android::close_output(context); |
| 1834 | android::close_error(context); |
Josh Gao | 03d055d | 2017-10-18 15:42:00 -0700 | [diff] [blame] | 1835 | |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1836 | if (context->fds[1] >= 0) { |
Josh Gao | 03d055d | 2017-10-18 15:42:00 -0700 | [diff] [blame] | 1837 | // NB: this should be closed by close_output, but just in case... |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1838 | close(context->fds[1]); |
Mark Salyzyn | 1d6928b | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1839 | context->fds[1] = -1; |
| 1840 | } |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1841 | |
| 1842 | android_closeEventTagMap(context->eventTagMap); |
| 1843 | |
Mark Salyzyn | 13e4735 | 2017-03-06 14:56:04 -0800 | [diff] [blame] | 1844 | // generic cleanup of devices list to handle all possible dirty cases |
| 1845 | log_device_t* dev; |
| 1846 | while (!!(dev = context->devices)) { |
| 1847 | struct logger_list* logger_list = dev->logger_list; |
| 1848 | if (logger_list) { |
| 1849 | for (log_device_t* d = dev; d; d = d->next) { |
| 1850 | if (d->logger_list == logger_list) d->logger_list = nullptr; |
| 1851 | } |
| 1852 | android_logger_list_free(logger_list); |
| 1853 | } |
| 1854 | context->devices = dev->next; |
| 1855 | delete dev; |
| 1856 | } |
| 1857 | |
Mark Salyzyn | c0cf90d | 2017-02-10 13:09:07 -0800 | [diff] [blame] | 1858 | int retval = context->retval; |
| 1859 | |
| 1860 | free(context); |
| 1861 | |
| 1862 | return retval; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1863 | } |