Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <dirent.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <limits.h> |
| 21 | #include <poll.h> |
| 22 | #include <signal.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 26 | #include <string> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 27 | #include <string.h> |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 28 | #include <sys/capability.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 29 | #include <sys/inotify.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <sys/wait.h> |
| 33 | #include <sys/klog.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 36 | #include <vector> |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 37 | #include <sys/prctl.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 38 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 39 | #define LOG_TAG "dumpstate" |
Mark Salyzyn | 261a733 | 2016-05-24 12:38:40 -0700 | [diff] [blame] | 40 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 41 | #include <android-base/file.h> |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 43 | #include <cutils/debugger.h> |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 44 | #include <cutils/log.h> |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 45 | #include <cutils/properties.h> |
| 46 | #include <cutils/sockets.h> |
| 47 | #include <private/android_filesystem_config.h> |
| 48 | |
Robert Craig | 9579837 | 2013-04-04 06:33:10 -0400 | [diff] [blame] | 49 | #include <selinux/android.h> |
| 50 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 51 | #include "dumpstate.h" |
| 52 | |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 53 | static const int64_t NANOS_PER_SEC = 1000000000; |
| 54 | |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 55 | static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds |
| 56 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 57 | /* list of native processes to include in the native dumps */ |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 58 | // This matches the /proc/pid/exe link instead of /proc/pid/cmdline. |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 59 | static const char* native_processes_to_dump[] = { |
Andy Hung | 9609bbd | 2015-12-15 12:42:50 -0800 | [diff] [blame] | 60 | "/system/bin/audioserver", |
Chien-Yu Chen | f5248da | 2016-01-28 14:23:03 -0800 | [diff] [blame] | 61 | "/system/bin/cameraserver", |
James Dong | 1fc4f80 | 2012-09-10 16:08:48 -0700 | [diff] [blame] | 62 | "/system/bin/drmserver", |
Andy Hung | 5c04e74 | 2016-04-13 19:35:34 -0700 | [diff] [blame] | 63 | "/system/bin/mediacodec", // media.codec |
| 64 | "/system/bin/mediadrmserver", |
| 65 | "/system/bin/mediaextractor", // media.extractor |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 66 | "/system/bin/mediaserver", |
| 67 | "/system/bin/sdcard", |
| 68 | "/system/bin/surfaceflinger", |
keunyoung | d907b32 | 2015-10-16 15:21:43 -0700 | [diff] [blame] | 69 | "/system/bin/vehicle_network_service", |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 70 | NULL, |
| 71 | }; |
| 72 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 73 | CommandOptions CommandOptions::DEFAULT = CommandOptions::WithTimeout(10).Build(); |
| 74 | CommandOptions CommandOptions::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build(); |
| 75 | CommandOptions CommandOptions::AS_ROOT_5 = CommandOptions::WithTimeout(5).AsRoot().Build(); |
| 76 | CommandOptions CommandOptions::AS_ROOT_10 = CommandOptions::WithTimeout(10).AsRoot().Build(); |
| 77 | CommandOptions CommandOptions::AS_ROOT_20 = CommandOptions::WithTimeout(20).AsRoot().Build(); |
| 78 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 79 | CommandOptions::CommandOptionsBuilder::CommandOptionsBuilder(long timeout) : values_(timeout) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Always() { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 83 | values_.always_ = true; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 84 | return *this; |
| 85 | } |
| 86 | |
| 87 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::AsRoot() { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 88 | values_.rootMode_ = SU_ROOT; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::DropRoot() { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 93 | values_.rootMode_ = DROP_ROOT; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 94 | return *this; |
| 95 | } |
| 96 | |
| 97 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::RedirectStderr() { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 98 | values_.stdoutMode_ = REDIRECT_TO_STDERR; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 99 | return *this; |
| 100 | } |
| 101 | |
| 102 | CommandOptions::CommandOptionsBuilder& CommandOptions::CommandOptionsBuilder::Log( |
| 103 | const std::string& message) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 104 | values_.loggingMessage_ = message; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | CommandOptions CommandOptions::CommandOptionsBuilder::Build() { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 109 | return CommandOptions(values_); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | CommandOptions::CommandOptionsValues::CommandOptionsValues(long timeout) |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 113 | : timeout_(timeout), |
| 114 | always_(false), |
| 115 | rootMode_(DONT_DROP_ROOT), |
| 116 | stdoutMode_(NORMAL_STDOUT), |
| 117 | loggingMessage_("") { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 120 | CommandOptions::CommandOptions(const CommandOptionsValues& values) : values_(values) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | long CommandOptions::Timeout() const { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 124 | return values_.timeout_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | bool CommandOptions::Always() const { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 128 | return values_.always_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | RootMode CommandOptions::RootMode() const { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 132 | return values_.rootMode_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | StdoutMode CommandOptions::StdoutMode() const { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 136 | return values_.stdoutMode_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | std::string CommandOptions::LoggingMessage() const { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 140 | return values_.loggingMessage_; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | CommandOptions::CommandOptionsBuilder CommandOptions::WithTimeout(long timeout) { |
| 144 | return CommandOptions::CommandOptionsBuilder(timeout); |
| 145 | } |
| 146 | |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 147 | DurationReporter::DurationReporter(const char *title) : DurationReporter(title, stdout) {} |
| 148 | |
| 149 | DurationReporter::DurationReporter(const char *title, FILE *out) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 150 | title_ = title; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 151 | if (title != nullptr) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 152 | started_ = DurationReporter::Nanotime(); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 153 | } |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 154 | out_ = out; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | DurationReporter::~DurationReporter() { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 158 | if (title_ != nullptr) { |
| 159 | uint64_t elapsed = DurationReporter::Nanotime() - started_; |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 160 | // Use "Yoda grammar" to make it easier to grep|sort sections. |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 161 | if (out_ != nullptr) { |
| 162 | fprintf(out_, "------ %.3fs was the duration of '%s' ------\n", |
| 163 | (float)elapsed / NANOS_PER_SEC, title_); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 164 | } else { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 165 | MYLOGD("Duration of '%s': %.3fs\n", title_, (float)elapsed / NANOS_PER_SEC); |
Felipe Leme | 608385d | 2016-02-01 10:35:38 -0800 | [diff] [blame] | 166 | } |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 170 | uint64_t DurationReporter::DurationReporter::Nanotime() { |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 171 | struct timespec ts; |
| 172 | clock_gettime(CLOCK_MONOTONIC, &ts); |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 173 | return (uint64_t) ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 174 | } |
| 175 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 176 | void for_each_userid(void (*func)(int), const char *header) { |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 177 | if (is_dry_run()) return; |
| 178 | |
John Spurlock | 5ecd4be | 2014-01-29 14:14:40 -0500 | [diff] [blame] | 179 | DIR *d; |
| 180 | struct dirent *de; |
| 181 | |
| 182 | if (header) printf("\n------ %s ------\n", header); |
| 183 | func(0); |
| 184 | |
| 185 | if (!(d = opendir("/data/system/users"))) { |
| 186 | printf("Failed to open /data/system/users (%s)\n", strerror(errno)); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | while ((de = readdir(d))) { |
| 191 | int userid; |
| 192 | if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) { |
| 193 | continue; |
| 194 | } |
| 195 | func(userid); |
| 196 | } |
| 197 | |
| 198 | closedir(d); |
| 199 | } |
| 200 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 201 | static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 202 | DIR *d; |
| 203 | struct dirent *de; |
| 204 | |
| 205 | if (!(d = opendir("/proc"))) { |
| 206 | printf("Failed to open /proc (%s)\n", strerror(errno)); |
| 207 | return; |
| 208 | } |
| 209 | |
Felipe Leme | 635ca31 | 2016-01-05 14:23:02 -0800 | [diff] [blame] | 210 | if (header) printf("\n------ %s ------\n", header); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 211 | while ((de = readdir(d))) { |
| 212 | int pid; |
| 213 | int fd; |
| 214 | char cmdpath[255]; |
| 215 | char cmdline[255]; |
| 216 | |
| 217 | if (!(pid = atoi(de->d_name))) { |
| 218 | continue; |
| 219 | } |
| 220 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 221 | memset(cmdline, 0, sizeof(cmdline)); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 222 | |
| 223 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid); |
| 224 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 225 | TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 226 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 227 | if (cmdline[0]) { |
| 228 | helper(pid, cmdline, arg); |
| 229 | continue; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // if no cmdline, a kernel thread has comm |
| 234 | snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid); |
| 235 | if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) { |
| 236 | TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4)); |
| 237 | close(fd); |
| 238 | if (cmdline[1]) { |
| 239 | cmdline[0] = '['; |
| 240 | size_t len = strcspn(cmdline, "\f\b\r\n"); |
| 241 | cmdline[len] = ']'; |
| 242 | cmdline[len+1] = '\0'; |
| 243 | } |
| 244 | } |
| 245 | if (!cmdline[0]) { |
| 246 | strcpy(cmdline, "N/A"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 247 | } |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 248 | helper(pid, cmdline, arg); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | closedir(d); |
| 252 | } |
| 253 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 254 | static void for_each_pid_helper(int pid, const char *cmdline, void *arg) { |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 255 | for_each_pid_func *func = (for_each_pid_func*) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 256 | func(pid, cmdline); |
| 257 | } |
| 258 | |
| 259 | void for_each_pid(for_each_pid_func func, const char *header) { |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 260 | if (is_dry_run()) return; |
| 261 | |
Felipe Leme | 515eb0d | 2015-12-14 15:09:56 -0800 | [diff] [blame] | 262 | __for_each_pid(for_each_pid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void for_each_tid_helper(int pid, const char *cmdline, void *arg) { |
| 266 | DIR *d; |
| 267 | struct dirent *de; |
| 268 | char taskpath[255]; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 269 | for_each_tid_func *func = (for_each_tid_func *) arg; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 270 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 271 | snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 272 | |
| 273 | if (!(d = opendir(taskpath))) { |
| 274 | printf("Failed to open %s (%s)\n", taskpath, strerror(errno)); |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | func(pid, pid, cmdline); |
| 279 | |
| 280 | while ((de = readdir(d))) { |
| 281 | int tid; |
| 282 | int fd; |
| 283 | char commpath[255]; |
| 284 | char comm[255]; |
| 285 | |
| 286 | if (!(tid = atoi(de->d_name))) { |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (tid == pid) |
| 291 | continue; |
| 292 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 293 | snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid); |
Colin Cross | 1493a39 | 2012-11-07 11:25:31 -0800 | [diff] [blame] | 294 | memset(comm, 0, sizeof(comm)); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 295 | if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 296 | strcpy(comm, "N/A"); |
| 297 | } else { |
| 298 | char *c; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 299 | TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2)); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 300 | close(fd); |
| 301 | |
| 302 | c = strrchr(comm, '\n'); |
| 303 | if (c) { |
| 304 | *c = '\0'; |
| 305 | } |
| 306 | } |
| 307 | func(pid, tid, comm); |
| 308 | } |
| 309 | |
| 310 | closedir(d); |
| 311 | } |
| 312 | |
| 313 | void for_each_tid(for_each_tid_func func, const char *header) { |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 314 | if (is_dry_run()) return; |
| 315 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 316 | __for_each_pid(for_each_tid_helper, header, (void *) func); |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void show_wchan(int pid, int tid, const char *name) { |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 320 | if (is_dry_run()) return; |
| 321 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 322 | char path[255]; |
| 323 | char buffer[255]; |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 324 | int fd, ret, save_errno; |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 325 | char name_buffer[255]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 326 | |
| 327 | memset(buffer, 0, sizeof(buffer)); |
| 328 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 329 | snprintf(path, sizeof(path), "/proc/%d/wchan", tid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 330 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 331 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 332 | return; |
| 333 | } |
| 334 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 335 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 336 | save_errno = errno; |
| 337 | close(fd); |
| 338 | |
| 339 | if (ret < 0) { |
| 340 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 341 | return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Colin Cross | 0c22e8b | 2012-11-02 15:46:56 -0700 | [diff] [blame] | 344 | snprintf(name_buffer, sizeof(name_buffer), "%*s%s", |
| 345 | pid == tid ? 0 : 3, "", name); |
| 346 | |
| 347 | printf("%-7d %-32s %s\n", tid, name_buffer, buffer); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 348 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 349 | return; |
| 350 | } |
| 351 | |
| 352 | // print time in centiseconds |
| 353 | static void snprcent(char *buffer, size_t len, size_t spc, |
| 354 | unsigned long long time) { |
| 355 | static long hz; // cache discovered hz |
| 356 | |
| 357 | if (hz <= 0) { |
| 358 | hz = sysconf(_SC_CLK_TCK); |
| 359 | if (hz <= 0) { |
| 360 | hz = 1000; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | // convert to centiseconds |
| 365 | time = (time * 100 + (hz / 2)) / hz; |
| 366 | |
| 367 | char str[16]; |
| 368 | |
| 369 | snprintf(str, sizeof(str), " %llu.%02u", |
| 370 | time / 100, (unsigned)(time % 100)); |
| 371 | size_t offset = strlen(buffer); |
| 372 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 373 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 374 | } |
| 375 | |
| 376 | // print permille as a percent |
| 377 | static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) { |
| 378 | char str[16]; |
| 379 | |
| 380 | snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10); |
| 381 | size_t offset = strlen(buffer); |
| 382 | snprintf(buffer + offset, (len > offset) ? len - offset : 0, |
| 383 | "%*s", (spc > offset) ? (int)(spc - offset) : 0, str); |
| 384 | } |
| 385 | |
| 386 | void show_showtime(int pid, const char *name) { |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 387 | if (is_dry_run()) return; |
| 388 | |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 389 | char path[255]; |
| 390 | char buffer[1023]; |
| 391 | int fd, ret, save_errno; |
| 392 | |
| 393 | memset(buffer, 0, sizeof(buffer)); |
| 394 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 395 | snprintf(path, sizeof(path), "/proc/%d/stat", pid); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 396 | if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) { |
| 397 | printf("Failed to open '%s' (%s)\n", path, strerror(errno)); |
| 398 | return; |
| 399 | } |
| 400 | |
| 401 | ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 402 | save_errno = errno; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 403 | close(fd); |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 404 | |
| 405 | if (ret < 0) { |
| 406 | printf("Failed to read '%s' (%s)\n", path, strerror(save_errno)); |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | // field 14 is utime |
| 411 | // field 15 is stime |
| 412 | // field 42 is iotime |
| 413 | unsigned long long utime = 0, stime = 0, iotime = 0; |
| 414 | if (sscanf(buffer, |
Mark Salyzyn | 791ddd3 | 2016-02-10 07:41:12 -0800 | [diff] [blame] | 415 | "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d " |
| 416 | "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d " |
| 417 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d " |
| 418 | "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ", |
Mark Salyzyn | 0751efa | 2016-02-05 15:33:17 -0800 | [diff] [blame] | 419 | &utime, &stime, &iotime) != 3) { |
| 420 | return; |
| 421 | } |
| 422 | |
| 423 | unsigned long long total = utime + stime; |
| 424 | if (!total) { |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | unsigned permille = (iotime * 1000 + (total / 2)) / total; |
| 429 | if (permille > 1000) { |
| 430 | permille = 1000; |
| 431 | } |
| 432 | |
| 433 | // try to beautify and stabilize columns at <80 characters |
| 434 | snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name); |
| 435 | if ((name[0] != '[') || utime) { |
| 436 | snprcent(buffer, sizeof(buffer), 57, utime); |
| 437 | } |
| 438 | snprcent(buffer, sizeof(buffer), 65, stime); |
| 439 | if ((name[0] != '[') || iotime) { |
| 440 | snprcent(buffer, sizeof(buffer), 73, iotime); |
| 441 | } |
| 442 | if (iotime) { |
| 443 | snprdec(buffer, sizeof(buffer), 79, permille); |
| 444 | } |
| 445 | puts(buffer); // adds a trailing newline |
| 446 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 447 | return; |
| 448 | } |
| 449 | |
| 450 | void do_dmesg() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 451 | const char *title = "KERNEL LOG (dmesg)"; |
| 452 | DurationReporter duration_reporter(title); |
| 453 | printf("------ %s ------\n", title); |
| 454 | |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 455 | if (is_dry_run()) return; |
| 456 | |
Elliott Hughes | 5f87b31 | 2012-09-17 11:43:40 -0700 | [diff] [blame] | 457 | /* Get size of kernel buffer */ |
| 458 | int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 459 | if (size <= 0) { |
| 460 | printf("Unexpected klogctl return value: %d\n\n", size); |
| 461 | return; |
| 462 | } |
| 463 | char *buf = (char *) malloc(size + 1); |
| 464 | if (buf == NULL) { |
| 465 | printf("memory allocation failed\n\n"); |
| 466 | return; |
| 467 | } |
| 468 | int retval = klogctl(KLOG_READ_ALL, buf, size); |
| 469 | if (retval < 0) { |
| 470 | printf("klogctl failure\n\n"); |
| 471 | free(buf); |
| 472 | return; |
| 473 | } |
| 474 | buf[retval] = '\0'; |
| 475 | printf("%s\n\n", buf); |
| 476 | free(buf); |
| 477 | return; |
| 478 | } |
| 479 | |
| 480 | void do_showmap(int pid, const char *name) { |
| 481 | char title[255]; |
| 482 | char arg[255]; |
| 483 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 484 | snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name); |
| 485 | snprintf(arg, sizeof(arg), "%d", pid); |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 486 | RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT_10); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 489 | static int _dump_file_from_fd(const char *title, const char *path, int fd) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 490 | if (title) { |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 491 | printf("------ %s (%s", title, path); |
| 492 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 493 | struct stat st; |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 494 | // Only show the modification time of non-device files. |
| 495 | size_t path_len = strlen(path); |
| 496 | if ((path_len < 6 || memcmp(path, "/proc/", 6)) && |
| 497 | (path_len < 5 || memcmp(path, "/sys/", 5)) && |
| 498 | (path_len < 3 || memcmp(path, "/d/", 3)) && |
| 499 | !fstat(fd, &st)) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 500 | char stamp[80]; |
| 501 | time_t mtime = st.st_mtime; |
| 502 | strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime)); |
| 503 | printf(": %s", stamp); |
| 504 | } |
| 505 | printf(") ------\n"); |
| 506 | } |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 507 | if (is_dry_run()) { |
| 508 | update_progress(WEIGHT_FILE); |
| 509 | close(fd); |
| 510 | return 0; |
| 511 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 512 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 513 | bool newline = false; |
| 514 | fd_set read_set; |
| 515 | struct timeval tm; |
| 516 | while (1) { |
| 517 | FD_ZERO(&read_set); |
| 518 | FD_SET(fd, &read_set); |
| 519 | /* Timeout if no data is read for 30 seconds. */ |
| 520 | tm.tv_sec = 30; |
| 521 | tm.tv_usec = 0; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 522 | uint64_t elapsed = DurationReporter::Nanotime(); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 523 | int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm)); |
| 524 | if (ret == -1) { |
| 525 | printf("*** %s: select failed: %s\n", path, strerror(errno)); |
| 526 | newline = true; |
| 527 | break; |
| 528 | } else if (ret == 0) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 529 | elapsed = DurationReporter::Nanotime() - elapsed; |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 530 | printf("*** %s: Timed out after %.3fs\n", path, |
| 531 | (float) elapsed / NANOS_PER_SEC); |
| 532 | newline = true; |
| 533 | break; |
| 534 | } else { |
| 535 | char buffer[65536]; |
| 536 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 537 | if (bytes_read > 0) { |
| 538 | fwrite(buffer, bytes_read, 1, stdout); |
| 539 | newline = (buffer[bytes_read-1] == '\n'); |
| 540 | } else { |
| 541 | if (bytes_read == -1) { |
| 542 | printf("*** %s: Failed to read from fd: %s", path, strerror(errno)); |
| 543 | newline = true; |
| 544 | } |
| 545 | break; |
| 546 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 547 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 548 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 549 | update_progress(WEIGHT_FILE); |
Elliott Hughes | 997abb6 | 2015-05-15 17:05:40 -0700 | [diff] [blame] | 550 | close(fd); |
Christopher Ferris | 7dc7f32 | 2014-07-22 16:08:19 -0700 | [diff] [blame] | 551 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 552 | if (!newline) printf("\n"); |
| 553 | if (title) printf("\n"); |
| 554 | return 0; |
| 555 | } |
| 556 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 557 | int dump_file(const char *title, const char *path) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 558 | return DumpFile(title, path); |
Felipe Leme | 0bcc7ca | 2016-09-13 16:45:56 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 561 | int DumpFile(const char* title, const std::string& path) { |
Felipe Leme | 0bcc7ca | 2016-09-13 16:45:56 -0700 | [diff] [blame] | 562 | DurationReporter durationReporter(title); |
| 563 | int fd = TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 564 | if (fd < 0) { |
| 565 | int err = errno; |
Felipe Leme | 0bcc7ca | 2016-09-13 16:45:56 -0700 | [diff] [blame] | 566 | printf("*** %s: %s\n", path.c_str(), strerror(err)); |
| 567 | if (title != nullptr) printf("\n"); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 568 | return -1; |
| 569 | } |
Felipe Leme | 0bcc7ca | 2016-09-13 16:45:56 -0700 | [diff] [blame] | 570 | return _dump_file_from_fd(title, path.c_str(), fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Felipe Leme | 71a74ac | 2016-03-17 15:43:25 -0700 | [diff] [blame] | 573 | int read_file_as_long(const char *path, long int *output) { |
| 574 | int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 575 | if (fd < 0) { |
| 576 | int err = errno; |
| 577 | MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err)); |
| 578 | return -1; |
| 579 | } |
| 580 | char buffer[50]; |
| 581 | ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer))); |
| 582 | if (bytes_read == -1) { |
| 583 | MYLOGE("Error reading file %s: %s\n", path, strerror(errno)); |
| 584 | return -2; |
| 585 | } |
| 586 | if (bytes_read == 0) { |
| 587 | MYLOGE("File %s is empty\n", path); |
| 588 | return -3; |
| 589 | } |
| 590 | *output = atoi(buffer); |
| 591 | return 0; |
| 592 | } |
| 593 | |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 594 | /* calls skip to gate calling dump_from_fd recursively |
| 595 | * in the specified directory. dump_from_fd defaults to |
| 596 | * dump_file_from_fd above when set to NULL. skip defaults |
| 597 | * to false when set to NULL. dump_from_fd will always be |
| 598 | * called with title NULL. |
| 599 | */ |
| 600 | int dump_files(const char *title, const char *dir, |
| 601 | bool (*skip)(const char *path), |
| 602 | int (*dump_from_fd)(const char *title, const char *path, int fd)) { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 603 | DurationReporter duration_reporter(title); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 604 | DIR *dirp; |
| 605 | struct dirent *d; |
| 606 | char *newpath = NULL; |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 607 | const char *slash = "/"; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 608 | int fd, retval = 0; |
| 609 | |
| 610 | if (title) { |
| 611 | printf("------ %s (%s) ------\n", title, dir); |
| 612 | } |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 613 | if (is_dry_run()) return 0; |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 614 | |
| 615 | if (dir[strlen(dir) - 1] == '/') { |
| 616 | ++slash; |
| 617 | } |
| 618 | dirp = opendir(dir); |
| 619 | if (dirp == NULL) { |
| 620 | retval = -errno; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 621 | MYLOGE("%s: %s\n", dir, strerror(errno)); |
Mark Salyzyn | 326842f | 2015-04-30 09:49:41 -0700 | [diff] [blame] | 622 | return retval; |
| 623 | } |
| 624 | |
| 625 | if (!dump_from_fd) { |
| 626 | dump_from_fd = dump_file_from_fd; |
| 627 | } |
| 628 | for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) { |
| 629 | if ((d->d_name[0] == '.') |
| 630 | && (((d->d_name[1] == '.') && (d->d_name[2] == '\0')) |
| 631 | || (d->d_name[1] == '\0'))) { |
| 632 | continue; |
| 633 | } |
| 634 | asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name, |
| 635 | (d->d_type == DT_DIR) ? "/" : ""); |
| 636 | if (!newpath) { |
| 637 | retval = -errno; |
| 638 | continue; |
| 639 | } |
| 640 | if (skip && (*skip)(newpath)) { |
| 641 | continue; |
| 642 | } |
| 643 | if (d->d_type == DT_DIR) { |
| 644 | int ret = dump_files(NULL, newpath, skip, dump_from_fd); |
| 645 | if (ret < 0) { |
| 646 | retval = ret; |
| 647 | } |
| 648 | continue; |
| 649 | } |
| 650 | fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC)); |
| 651 | if (fd < 0) { |
| 652 | retval = fd; |
| 653 | printf("*** %s: %s\n", newpath, strerror(errno)); |
| 654 | continue; |
| 655 | } |
| 656 | (*dump_from_fd)(NULL, newpath, fd); |
| 657 | } |
| 658 | closedir(dirp); |
| 659 | if (title) { |
| 660 | printf("\n"); |
| 661 | } |
| 662 | return retval; |
| 663 | } |
| 664 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 665 | /* fd must have been opened with the flag O_NONBLOCK. With this flag set, |
| 666 | * it's possible to avoid issues where opening the file itself can get |
| 667 | * stuck. |
| 668 | */ |
| 669 | int dump_file_from_fd(const char *title, const char *path, int fd) { |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 670 | if (is_dry_run()) return 0; |
| 671 | |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 672 | int flags = fcntl(fd, F_GETFL); |
| 673 | if (flags == -1) { |
| 674 | printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno)); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 675 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 676 | return -1; |
| 677 | } else if (!(flags & O_NONBLOCK)) { |
| 678 | printf("*** %s: fd must have O_NONBLOCK set.\n", path); |
Christopher Ferris | ed24d2a | 2015-11-12 14:01:56 -0800 | [diff] [blame] | 679 | close(fd); |
Christopher Ferris | 54bcc5f | 2015-02-10 12:15:01 -0800 | [diff] [blame] | 680 | return -1; |
| 681 | } |
| 682 | return _dump_file_from_fd(title, path, fd); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 683 | } |
| 684 | |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 685 | bool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) { |
| 686 | sigset_t child_mask, old_mask; |
| 687 | sigemptyset(&child_mask); |
| 688 | sigaddset(&child_mask, SIGCHLD); |
| 689 | |
| 690 | if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) { |
| 691 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 692 | return false; |
| 693 | } |
| 694 | |
| 695 | struct timespec ts; |
| 696 | ts.tv_sec = timeout_seconds; |
| 697 | ts.tv_nsec = 0; |
| 698 | int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts)); |
| 699 | int saved_errno = errno; |
| 700 | // Set the signals back the way they were. |
| 701 | if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) { |
| 702 | printf("*** sigprocmask failed: %s\n", strerror(errno)); |
| 703 | if (ret == 0) { |
| 704 | return false; |
| 705 | } |
| 706 | } |
| 707 | if (ret == -1) { |
| 708 | errno = saved_errno; |
| 709 | if (errno == EAGAIN) { |
| 710 | errno = ETIMEDOUT; |
| 711 | } else { |
| 712 | printf("*** sigtimedwait failed: %s\n", strerror(errno)); |
| 713 | } |
| 714 | return false; |
| 715 | } |
| 716 | |
| 717 | pid_t child_pid = waitpid(pid, status, WNOHANG); |
| 718 | if (child_pid != pid) { |
| 719 | if (child_pid != -1) { |
| 720 | printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid); |
| 721 | } else { |
| 722 | printf("*** waitpid failed: %s\n", strerror(errno)); |
| 723 | } |
| 724 | return false; |
| 725 | } |
| 726 | return true; |
| 727 | } |
| 728 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 729 | int run_command(const char* title, int timeout_seconds, const char* command, ...) { |
| 730 | std::vector<std::string> fullCommand = {command}; |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 731 | size_t arg; |
| 732 | va_list ap; |
| 733 | va_start(ap, command); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 734 | for (arg = 0; arg < MAX_ARGS_ARRAY_SIZE; ++arg) { |
| 735 | const char* ptr = va_arg(ap, const char*); |
| 736 | if (ptr == nullptr) { |
Felipe Leme | a34efb7 | 2016-03-11 09:33:32 -0800 | [diff] [blame] | 737 | break; |
| 738 | } |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 739 | fullCommand.push_back(ptr); |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 740 | } |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 741 | va_end(ap); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 742 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 743 | return RunCommand(title, fullCommand, CommandOptions::WithTimeout(timeout_seconds).Build()); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 746 | int RunCommand(const char* title, const std::vector<std::string>& fullCommand, |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 747 | const CommandOptions& options) { |
| 748 | if (fullCommand.empty()) { |
| 749 | MYLOGE("No arguments on command '%s'\n", title); |
| 750 | return -1; |
| 751 | } |
| 752 | DurationReporter durationReporter(title); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 753 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 754 | int size = fullCommand.size() + 1; // null terminated |
| 755 | if (options.RootMode() == SU_ROOT) { |
| 756 | size += 2; // "su" "root" |
| 757 | } |
| 758 | |
| 759 | const char* args[size]; |
| 760 | |
Felipe Leme | c5d6cfc | 2016-09-26 15:57:04 -0700 | [diff] [blame] | 761 | if (title) { |
| 762 | printf("------ %s (", title); |
| 763 | } |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 764 | |
| 765 | std::string commandString; |
| 766 | int i = 0; |
| 767 | if (options.RootMode() == SU_ROOT) { |
| 768 | args[0] = SU_PATH; |
| 769 | commandString += SU_PATH; |
| 770 | args[1] = "root"; |
| 771 | commandString += " root "; |
| 772 | } |
| 773 | for (auto arg = fullCommand.begin(); arg < fullCommand.end(); arg++) { |
| 774 | args[i++] = arg->c_str(); |
| 775 | commandString += arg->c_str(); |
| 776 | if (arg != fullCommand.end() - 1) { |
| 777 | commandString += " "; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 778 | } |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 779 | } |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 780 | args[i] = nullptr; |
| 781 | const char* path = args[0]; |
| 782 | const char* command = commandString.c_str(); |
Felipe Leme | c5d6cfc | 2016-09-26 15:57:04 -0700 | [diff] [blame] | 783 | |
| 784 | if (title) { |
| 785 | printf("%s) ------\n", command); |
| 786 | } |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 787 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 788 | fflush(stdout); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 789 | |
| 790 | const std::string& loggingMessage = options.LoggingMessage(); |
| 791 | if (!loggingMessage.empty()) { |
| 792 | MYLOGI(loggingMessage.c_str(), commandString.c_str()); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 793 | } |
| 794 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 795 | if (is_dry_run() && !options.Always()) { |
| 796 | update_progress(options.Timeout()); |
| 797 | return 0; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 798 | } |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 799 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 800 | bool silent = (options.StdoutMode() == REDIRECT_TO_STDERR); |
Felipe Leme | ea160d1 | 2016-03-24 11:29:44 -0700 | [diff] [blame] | 801 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 802 | /* TODO: for now we're simplifying the progress calculation by using the |
| 803 | * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys, |
| 804 | * where its weight should be much higher proportionally to its timeout. |
| 805 | * Ideally, it should use a options.EstimatedDuration() instead...*/ |
| 806 | int weight = options.Timeout(); |
Felipe Leme | 93d705b | 2015-11-10 20:10:25 -0800 | [diff] [blame] | 807 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 808 | uint64_t start = DurationReporter::Nanotime(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 809 | pid_t pid = fork(); |
| 810 | |
| 811 | /* handle error case */ |
| 812 | if (pid < 0) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 813 | if (!silent) printf("*** fork: %s\n", strerror(errno)); |
| 814 | MYLOGE("*** fork: %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 815 | return pid; |
| 816 | } |
| 817 | |
| 818 | /* handle child case */ |
| 819 | if (pid == 0) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 820 | if (options.RootMode() == DROP_ROOT && !drop_root_user()) { |
| 821 | if (!silent) |
| 822 | printf("*** failed to drop root before running %s: %s\n", command, strerror(errno)); |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 823 | MYLOGE("*** could not drop root before running %s: %s\n", command, strerror(errno)); |
Felipe Leme | 73f731c | 2016-03-23 16:47:00 -0700 | [diff] [blame] | 824 | return -1; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 825 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 826 | |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 827 | if (silent) { |
| 828 | // Redirect stderr to stdout |
| 829 | dup2(STDERR_FILENO, STDOUT_FILENO); |
| 830 | } |
| 831 | |
John Michelau | e7b6cf1 | 2013-03-07 15:35:35 -0600 | [diff] [blame] | 832 | /* make sure the child dies when dumpstate dies */ |
| 833 | prctl(PR_SET_PDEATHSIG, SIGKILL); |
| 834 | |
Andres Morales | 2e671bb | 2014-08-21 12:38:22 -0700 | [diff] [blame] | 835 | /* just ignore SIGPIPE, will go down with parent's */ |
| 836 | struct sigaction sigact; |
| 837 | memset(&sigact, 0, sizeof(sigact)); |
| 838 | sigact.sa_handler = SIG_IGN; |
| 839 | sigaction(SIGPIPE, &sigact, NULL); |
| 840 | |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 841 | execvp(path, (char**)args); |
| 842 | // execvp's result will be handled after waitpid_with_timeout() below, but |
| 843 | // if it failed, it's safer to exit dumpstate. |
| 844 | MYLOGD("execvp on command '%s' failed (error: %s)\n", command, strerror(errno)); |
Felipe Leme | ec72578 | 2016-03-23 11:47:00 -0700 | [diff] [blame] | 845 | fflush(stdout); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 846 | // Must call _exit (instead of exit), otherwise it will corrupt the zip |
| 847 | // file. |
Felipe Leme | baa85bd | 2016-03-29 13:29:11 -0700 | [diff] [blame] | 848 | _exit(EXIT_FAILURE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /* handle parent case */ |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 852 | int status; |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 853 | bool ret = waitpid_with_timeout(pid, options.Timeout(), &status); |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 854 | uint64_t elapsed = DurationReporter::Nanotime() - start; |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 855 | if (!ret) { |
| 856 | if (errno == ETIMEDOUT) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 857 | if (!silent) |
| 858 | printf("*** command '%s' timed out after %.3fs (killing pid %d)\n", command, |
| 859 | (float)elapsed / NANOS_PER_SEC, pid); |
| 860 | MYLOGE("command '%s' timed out after %.3fs (killing pid %d)\n", command, |
| 861 | (float)elapsed / NANOS_PER_SEC, pid); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 862 | } else { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 863 | if (!silent) |
| 864 | printf("*** command '%s': Error after %.4fs (killing pid %d)\n", command, |
| 865 | (float)elapsed / NANOS_PER_SEC, pid); |
| 866 | MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", command, |
| 867 | (float)elapsed / NANOS_PER_SEC, pid); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 868 | } |
| 869 | kill(pid, SIGTERM); |
| 870 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
| 871 | kill(pid, SIGKILL); |
| 872 | if (!waitpid_with_timeout(pid, 5, NULL)) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 873 | if (!silent) |
| 874 | printf("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, |
| 875 | pid); |
Felipe Leme | 14e034a | 2016-03-30 18:51:03 -0700 | [diff] [blame] | 876 | MYLOGE("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 877 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 878 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 879 | return -1; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 880 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 881 | |
| 882 | if (WIFSIGNALED(status)) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 883 | if (!silent) printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
| 884 | MYLOGE("*** %s: Killed by signal %d\n", command, WTERMSIG(status)); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 885 | } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) { |
Felipe Leme | 29c3971 | 2016-04-01 10:02:00 -0700 | [diff] [blame] | 886 | if (!silent) printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
| 887 | MYLOGE("*** %s: Exit code %d\n", command, WEXITSTATUS(status)); |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 888 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 889 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 890 | if (weight > 0) { |
| 891 | update_progress(weight); |
| 892 | } |
Christopher Ferris | 1a9a338 | 2015-01-30 11:00:52 -0800 | [diff] [blame] | 893 | return status; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 896 | void RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsysArgs, |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 897 | const CommandOptions& options) { |
| 898 | std::vector<std::string> dumpsys = {"/system/bin/dumpsys", "-t", |
| 899 | std::to_string(options.Timeout())}; |
| 900 | dumpsys.insert(dumpsys.end(), dumpsysArgs.begin(), dumpsysArgs.end()); |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 901 | RunCommand(title.c_str(), dumpsys, options); |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 904 | bool drop_root_user() { |
| 905 | if (getgid() == AID_SHELL && getuid() == AID_SHELL) { |
| 906 | MYLOGD("drop_root_user(): already running as Shell"); |
| 907 | return true; |
| 908 | } |
| 909 | /* ensure we will keep capabilities when we drop root */ |
| 910 | if (prctl(PR_SET_KEEPCAPS, 1) < 0) { |
| 911 | MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno)); |
| 912 | return false; |
| 913 | } |
| 914 | |
| 915 | gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, |
Ajay Panicker | d886ec4 | 2016-09-14 12:26:46 -0700 | [diff] [blame] | 916 | AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC, AID_WAKELOCK, |
| 917 | AID_BLUETOOTH }; |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 918 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { |
| 919 | MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); |
| 920 | return false; |
| 921 | } |
| 922 | if (setgid(AID_SHELL) != 0) { |
| 923 | MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno)); |
| 924 | return false; |
| 925 | } |
| 926 | if (setuid(AID_SHELL) != 0) { |
| 927 | MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno)); |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | struct __user_cap_header_struct capheader; |
| 932 | struct __user_cap_data_struct capdata[2]; |
| 933 | memset(&capheader, 0, sizeof(capheader)); |
| 934 | memset(&capdata, 0, sizeof(capdata)); |
| 935 | capheader.version = _LINUX_CAPABILITY_VERSION_3; |
| 936 | capheader.pid = 0; |
| 937 | |
Wei Liu | f87959e | 2016-08-26 14:51:42 -0700 | [diff] [blame] | 938 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = |
| 939 | (CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_BLOCK_SUSPEND)); |
| 940 | capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = |
| 941 | (CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_BLOCK_SUSPEND)); |
Felipe Leme | cf6a8b4 | 2016-03-11 10:38:19 -0800 | [diff] [blame] | 942 | capdata[0].inheritable = 0; |
| 943 | capdata[1].inheritable = 0; |
| 944 | |
| 945 | if (capset(&capheader, &capdata[0]) < 0) { |
| 946 | MYLOGE("capset failed: %s\n", strerror(errno)); |
| 947 | return false; |
| 948 | } |
| 949 | |
| 950 | return true; |
| 951 | } |
| 952 | |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 953 | void send_broadcast(const std::string& action, const std::vector<std::string>& args) { |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 954 | std::vector<std::string> am = {"/system/bin/am", "broadcast", "--user", "0", "-a", action}; |
| 955 | |
| 956 | am.insert(am.end(), args.begin(), args.end()); |
| 957 | |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 958 | RunCommand(nullptr, am, CommandOptions::WithTimeout(20) |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 959 | .Log("Sending broadcast: '%s'\n") |
| 960 | .Always() |
| 961 | .DropRoot() |
| 962 | .RedirectStderr() |
| 963 | .Build()); |
Felipe Leme | 36b3f6f | 2015-11-19 15:41:04 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 966 | size_t num_props = 0; |
| 967 | static char* props[2000]; |
| 968 | |
| 969 | static void print_prop(const char *key, const char *name, void *user) { |
| 970 | (void) user; |
| 971 | if (num_props < sizeof(props) / sizeof(props[0])) { |
| 972 | char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10]; |
| 973 | snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name); |
| 974 | props[num_props++] = strdup(buf); |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | static int compare_prop(const void *a, const void *b) { |
| 979 | return strcmp(*(char * const *) a, *(char * const *) b); |
| 980 | } |
| 981 | |
| 982 | /* prints all the system properties */ |
| 983 | void print_properties() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 984 | const char* title = "SYSTEM PROPERTIES"; |
| 985 | DurationReporter duration_reporter(title); |
| 986 | printf("------ %s ------\n", title); |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 987 | if (is_dry_run()) return; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 988 | size_t i; |
| 989 | num_props = 0; |
| 990 | property_list(print_prop, NULL); |
| 991 | qsort(&props, num_props, sizeof(props[0]), compare_prop); |
| 992 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 993 | for (i = 0; i < num_props; ++i) { |
| 994 | fputs(props[i], stdout); |
| 995 | free(props[i]); |
| 996 | } |
| 997 | printf("\n"); |
| 998 | } |
| 999 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 1000 | int open_socket(const char *service) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1001 | int s = android_get_control_socket(service); |
| 1002 | if (s < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1003 | MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1004 | exit(1); |
| 1005 | } |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1006 | fcntl(s, F_SETFD, FD_CLOEXEC); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1007 | if (listen(s, 4) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1008 | MYLOGE("listen(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1009 | exit(1); |
| 1010 | } |
| 1011 | |
| 1012 | struct sockaddr addr; |
| 1013 | socklen_t alen = sizeof(addr); |
| 1014 | int fd = accept(s, &addr, &alen); |
| 1015 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1016 | MYLOGE("accept(control socket): %s\n", strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1017 | exit(1); |
| 1018 | } |
| 1019 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 1020 | return fd; |
| 1021 | } |
| 1022 | |
| 1023 | /* redirect output to a service control socket */ |
| 1024 | void redirect_to_socket(FILE *redirect, const char *service) { |
| 1025 | int fd = open_socket(service); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1026 | fflush(redirect); |
| 1027 | dup2(fd, fileno(redirect)); |
| 1028 | close(fd); |
| 1029 | } |
| 1030 | |
Felipe Leme | 2628e9e | 2016-04-12 16:36:51 -0700 | [diff] [blame] | 1031 | // TODO: should call is_valid_output_file and/or be merged into it. |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1032 | void create_parent_dirs(const char *path) { |
Srinath Sridharan | fdf52d3 | 2016-02-01 15:50:22 -0800 | [diff] [blame] | 1033 | char *chp = const_cast<char *> (path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1034 | |
| 1035 | /* skip initial slash */ |
| 1036 | if (chp[0] == '/') |
| 1037 | chp++; |
| 1038 | |
| 1039 | /* create leading directories, if necessary */ |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1040 | struct stat dir_stat; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1041 | while (chp && chp[0]) { |
| 1042 | chp = strchr(chp, '/'); |
| 1043 | if (chp) { |
| 1044 | *chp = 0; |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1045 | if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1046 | MYLOGI("Creating directory %s\n", path); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1047 | if (mkdir(path, 0770)) { /* drwxrwx--- */ |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1048 | MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1049 | } else if (chown(path, AID_SHELL, AID_SHELL)) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1050 | MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno)); |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1051 | } |
| 1052 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1053 | *chp++ = '/'; |
| 1054 | } |
| 1055 | } |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 1058 | void _redirect_to_file(FILE *redirect, char *path, int truncate_flag) { |
Felipe Leme | 111b9d0 | 2016-02-03 09:28:24 -0800 | [diff] [blame] | 1059 | create_parent_dirs(path); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1060 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 1061 | int fd = TEMP_FAILURE_RETRY(open(path, |
| 1062 | O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW, |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 1063 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1064 | if (fd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1065 | MYLOGE("%s: %s\n", path, strerror(errno)); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1066 | exit(1); |
| 1067 | } |
| 1068 | |
Christopher Ferris | ff4a4dc | 2015-02-09 16:24:47 -0800 | [diff] [blame] | 1069 | TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect))); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1070 | close(fd); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1071 | } |
| 1072 | |
Felipe Leme | 0f3fb20 | 2016-06-10 17:10:53 -0700 | [diff] [blame] | 1073 | void redirect_to_file(FILE *redirect, char *path) { |
| 1074 | _redirect_to_file(redirect, path, O_TRUNC); |
| 1075 | } |
| 1076 | |
| 1077 | void redirect_to_existing_file(FILE *redirect, char *path) { |
| 1078 | _redirect_to_file(redirect, path, O_APPEND); |
| 1079 | } |
| 1080 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1081 | static bool should_dump_native_traces(const char* path) { |
| 1082 | for (const char** p = native_processes_to_dump; *p; p++) { |
| 1083 | if (!strcmp(*p, path)) { |
| 1084 | return true; |
| 1085 | } |
| 1086 | } |
| 1087 | return false; |
| 1088 | } |
| 1089 | |
| 1090 | /* dump Dalvik and native stack traces, return the trace file location (NULL if none) */ |
| 1091 | const char *dump_traces() { |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1092 | DurationReporter duration_reporter("DUMP TRACES", nullptr); |
| 1093 | if (is_dry_run()) return nullptr; |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 1094 | |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1095 | const char* result = nullptr; |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1096 | |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1097 | std::string tracesPath = android::base::GetProperty("dalvik.vm.stack-trace-file", ""); |
| 1098 | if (tracesPath.empty()) return nullptr; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1099 | |
| 1100 | /* move the old traces.txt (if any) out of the way temporarily */ |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1101 | std::string anrTracesPath = tracesPath + ".anr"; |
| 1102 | if (rename(tracesPath.c_str(), anrTracesPath.c_str()) && errno != ENOENT) { |
| 1103 | MYLOGE("rename(%s, %s): %s\n", tracesPath.c_str(), anrTracesPath.c_str(), strerror(errno)); |
| 1104 | return nullptr; // Can't rename old traces.txt -- no permission? -- leave it alone instead |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1107 | /* create a new, empty traces.txt file to receive stack dumps */ |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1108 | int fd = TEMP_FAILURE_RETRY(open(tracesPath.c_str(), |
| 1109 | O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC, |
| 1110 | 0666)); /* -rw-rw-rw- */ |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1111 | if (fd < 0) { |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1112 | MYLOGE("%s: %s\n", tracesPath.c_str(), strerror(errno)); |
| 1113 | return nullptr; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1114 | } |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1115 | int chmod_ret = fchmod(fd, 0666); |
| 1116 | if (chmod_ret < 0) { |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1117 | MYLOGE("fchmod on %s failed: %s\n", tracesPath.c_str(), strerror(errno)); |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1118 | close(fd); |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1119 | return nullptr; |
Nick Kralevich | c7f1fe2 | 2012-04-06 09:31:28 -0700 | [diff] [blame] | 1120 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1121 | |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1122 | /* Variables below must be initialized before 'goto' statements */ |
| 1123 | int dalvik_found = 0; |
| 1124 | int ifd, wfd = -1; |
| 1125 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1126 | /* walk /proc and kill -QUIT all Dalvik processes */ |
| 1127 | DIR *proc = opendir("/proc"); |
| 1128 | if (proc == NULL) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1129 | MYLOGE("/proc: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1130 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | /* use inotify to find when processes are done dumping */ |
Felipe Leme | 8620bb4 | 2015-11-10 11:04:45 -0800 | [diff] [blame] | 1134 | ifd = inotify_init(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1135 | if (ifd < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1136 | MYLOGE("inotify_init: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1137 | goto error_close_fd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1140 | wfd = inotify_add_watch(ifd, tracesPath.c_str(), IN_CLOSE_WRITE); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1141 | if (wfd < 0) { |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1142 | MYLOGE("inotify_add_watch(%s): %s\n", tracesPath.c_str(), strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1143 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | struct dirent *d; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1147 | while ((d = readdir(proc))) { |
| 1148 | int pid = atoi(d->d_name); |
| 1149 | if (pid <= 0) continue; |
| 1150 | |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1151 | char path[PATH_MAX]; |
| 1152 | char data[PATH_MAX]; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1153 | snprintf(path, sizeof(path), "/proc/%d/exe", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1154 | ssize_t len = readlink(path, data, sizeof(data) - 1); |
| 1155 | if (len <= 0) { |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1156 | continue; |
| 1157 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1158 | data[len] = '\0'; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1159 | |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1160 | if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1161 | /* skip zygote -- it won't dump its stack anyway */ |
| 1162 | snprintf(path, sizeof(path), "/proc/%d/cmdline", pid); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1163 | int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1164 | len = read(cfd, data, sizeof(data) - 1); |
| 1165 | close(cfd); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1166 | if (len <= 0) { |
| 1167 | continue; |
| 1168 | } |
| 1169 | data[len] = '\0'; |
Colin Cross | 0d6180f | 2014-07-16 19:00:46 -0700 | [diff] [blame] | 1170 | if (!strncmp(data, "zygote", strlen("zygote"))) { |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1171 | continue; |
| 1172 | } |
| 1173 | |
| 1174 | ++dalvik_found; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 1175 | uint64_t start = DurationReporter::Nanotime(); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1176 | if (kill(pid, SIGQUIT)) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1177 | MYLOGE("kill(%d, SIGQUIT): %s\n", pid, strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1178 | continue; |
| 1179 | } |
| 1180 | |
| 1181 | /* wait for the writable-close notification from inotify */ |
| 1182 | struct pollfd pfd = { ifd, POLLIN, 0 }; |
Felipe Leme | 6188412 | 2016-06-13 09:23:30 -0700 | [diff] [blame] | 1183 | int ret = poll(&pfd, 1, TRACE_DUMP_TIMEOUT_MS); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1184 | if (ret < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1185 | MYLOGE("poll: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1186 | } else if (ret == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1187 | MYLOGE("warning: timed out dumping pid %d\n", pid); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1188 | } else { |
| 1189 | struct inotify_event ie; |
| 1190 | read(ifd, &ie, sizeof(ie)); |
| 1191 | } |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1192 | |
| 1193 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1194 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1195 | } else { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 1196 | dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", pid, |
| 1197 | (float)(DurationReporter::Nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | 1dc94e3 | 2014-09-11 14:15:27 -0700 | [diff] [blame] | 1198 | } |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1199 | } else if (should_dump_native_traces(data)) { |
| 1200 | /* dump native process if appropriate */ |
| 1201 | if (lseek(fd, 0, SEEK_END) < 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1202 | MYLOGE("lseek: %s\n", strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1203 | } else { |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1204 | static uint16_t timeout_failures = 0; |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 1205 | uint64_t start = DurationReporter::Nanotime(); |
Christopher Ferris | 31ef855 | 2015-01-14 13:23:30 -0800 | [diff] [blame] | 1206 | |
| 1207 | /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */ |
| 1208 | if (timeout_failures == 3) { |
| 1209 | dprintf(fd, "too many stack dump failures, skipping...\n"); |
| 1210 | } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) { |
| 1211 | dprintf(fd, "dumping failed, likely due to a timeout\n"); |
| 1212 | timeout_failures++; |
| 1213 | } else { |
| 1214 | timeout_failures = 0; |
| 1215 | } |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 1216 | dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", pid, |
| 1217 | (float)(DurationReporter::Nanotime() - start) / NANOS_PER_SEC); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1218 | } |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1219 | } |
| 1220 | } |
| 1221 | |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1222 | if (dalvik_found == 0) { |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1223 | MYLOGE("Warning: no Dalvik processes found to dump stacks\n"); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1226 | static std::string dumpTracesPath = tracesPath + ".bugreport"; |
| 1227 | if (rename(tracesPath.c_str(), dumpTracesPath.c_str())) { |
| 1228 | MYLOGE("rename(%s, %s): %s\n", tracesPath.c_str(), dumpTracesPath.c_str(), strerror(errno)); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1229 | goto error_close_ifd; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1230 | } |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1231 | result = dumpTracesPath.c_str(); |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1232 | |
| 1233 | /* replace the saved [ANR] traces.txt file */ |
Felipe Leme | 96c2bbb | 2016-09-26 09:21:21 -0700 | [diff] [blame] | 1234 | rename(anrTracesPath.c_str(), tracesPath.c_str()); |
Jeff Brown | bf7f492 | 2012-06-07 16:40:01 -0700 | [diff] [blame] | 1235 | |
| 1236 | error_close_ifd: |
| 1237 | close(ifd); |
| 1238 | error_close_fd: |
| 1239 | close(fd); |
| 1240 | return result; |
Colin Cross | f45fa6b | 2012-03-26 12:38:26 -0700 | [diff] [blame] | 1241 | } |
| 1242 | |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1243 | void dump_route_tables() { |
Felipe Leme | 78f2c86 | 2015-12-21 09:55:22 -0800 | [diff] [blame] | 1244 | DurationReporter duration_reporter("DUMP ROUTE TABLES"); |
Felipe Leme | d402e7d | 2016-08-03 09:22:27 -0700 | [diff] [blame] | 1245 | if (is_dry_run()) return; |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1246 | const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables"; |
| 1247 | dump_file("RT_TABLES", RT_TABLES_PATH); |
Nick Kralevich | cd67e9f | 2015-03-19 11:30:59 -0700 | [diff] [blame] | 1248 | FILE* fp = fopen(RT_TABLES_PATH, "re"); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1249 | if (!fp) { |
| 1250 | printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno)); |
| 1251 | return; |
| 1252 | } |
| 1253 | char table[16]; |
| 1254 | // Each line has an integer (the table number), a space, and a string (the table name). We only |
| 1255 | // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name. |
| 1256 | // Add a fixed max limit so this doesn't go awry. |
| 1257 | for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 1258 | RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table}); |
| 1259 | RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table}); |
Sreeram Ramachandran | 2b3bba3 | 2014-07-08 15:40:55 -0700 | [diff] [blame] | 1260 | } |
| 1261 | fclose(fp); |
| 1262 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1263 | |
| 1264 | /* overall progress */ |
| 1265 | int progress = 0; |
Felipe Leme | 08b5578 | 2015-12-01 08:40:52 -0800 | [diff] [blame] | 1266 | int do_update_progress = 0; // Set by dumpstate.cpp |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1267 | int weight_total = WEIGHT_TOTAL; |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1268 | |
| 1269 | // TODO: make this function thread safe if sections are generated in parallel. |
| 1270 | void update_progress(int delta) { |
| 1271 | if (!do_update_progress) return; |
| 1272 | |
| 1273 | progress += delta; |
| 1274 | |
| 1275 | char key[PROPERTY_KEY_MAX]; |
| 1276 | char value[PROPERTY_VALUE_MAX]; |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1277 | |
| 1278 | // adjusts max on the fly |
| 1279 | if (progress > weight_total) { |
| 1280 | int new_total = weight_total * 1.2; |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1281 | MYLOGD("Adjusting total weight from %d to %d\n", weight_total, new_total); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1282 | weight_total = new_total; |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 1283 | snprintf(key, sizeof(key), "dumpstate.%d.max", getpid()); |
| 1284 | snprintf(value, sizeof(value), "%d", weight_total); |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1285 | int status = property_set(key, value); |
| 1286 | if (status) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1287 | MYLOGE("Could not update max weight by setting system property %s to %s: %d\n", |
Felipe Leme | ad5f6c4 | 2015-11-30 14:26:46 -0800 | [diff] [blame] | 1288 | key, value, status); |
| 1289 | } |
| 1290 | } |
| 1291 | |
Nick Kralevich | f0922cc | 2016-05-14 16:47:44 -0700 | [diff] [blame] | 1292 | snprintf(key, sizeof(key), "dumpstate.%d.progress", getpid()); |
| 1293 | snprintf(value, sizeof(value), "%d", progress); |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1294 | |
Felipe Leme | 107a05f | 2016-03-08 15:11:15 -0800 | [diff] [blame] | 1295 | if (progress % 100 == 0) { |
| 1296 | // We don't want to spam logcat, so only log multiples of 100. |
| 1297 | MYLOGD("Setting progress (%s): %s/%d\n", key, value, weight_total); |
| 1298 | } else { |
| 1299 | // stderr is ignored on normal invocations, but useful when calling /system/bin/dumpstate |
| 1300 | // directly for debuggging. |
| 1301 | fprintf(stderr, "Setting progress (%s): %s/%d\n", key, value, weight_total); |
| 1302 | } |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1303 | |
Felipe Leme | 02b7e00 | 2016-07-22 12:03:20 -0700 | [diff] [blame] | 1304 | if (control_socket_fd >= 0) { |
| 1305 | dprintf(control_socket_fd, "PROGRESS:%d/%d\n", progress, weight_total); |
| 1306 | fsync(control_socket_fd); |
| 1307 | } |
| 1308 | |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1309 | int status = property_set(key, value); |
| 1310 | if (status) { |
Felipe Leme | cbce55d | 2016-02-08 09:53:18 -0800 | [diff] [blame] | 1311 | MYLOGE("Could not update progress by setting system property %s to %s: %d\n", |
Felipe Leme | 71bbfc5 | 2015-11-23 14:14:51 -0800 | [diff] [blame] | 1312 | key, value, status); |
| 1313 | } |
| 1314 | } |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1315 | |
Felipe Leme | 3634a1e | 2015-12-09 10:11:47 -0800 | [diff] [blame] | 1316 | void take_screenshot(const std::string& path) { |
Felipe Leme | b0f669d | 2016-09-26 18:26:11 -0700 | [diff] [blame^] | 1317 | RunCommand(nullptr, {"/system/bin/screencap", "-p", path}, |
Felipe Leme | 30dbfa1 | 2016-09-02 12:43:26 -0700 | [diff] [blame] | 1318 | CommandOptions::WithTimeout(10).Always().RedirectStderr().Build()); |
Felipe Leme | e338bf6 | 2015-12-07 14:03:50 -0800 | [diff] [blame] | 1319 | } |
Mark Salyzyn | f55d402 | 2015-12-11 07:32:31 -0800 | [diff] [blame] | 1320 | |
Felipe Leme | 0c80cf0 | 2016-01-05 13:25:34 -0800 | [diff] [blame] | 1321 | void vibrate(FILE* vibrator, int ms) { |
| 1322 | fprintf(vibrator, "%d\n", ms); |
| 1323 | fflush(vibrator); |
| 1324 | } |
| 1325 | |
| 1326 | bool is_dir(const char* pathname) { |
| 1327 | struct stat info; |
| 1328 | if (stat(pathname, &info) == -1) { |
| 1329 | return false; |
| 1330 | } |
| 1331 | return S_ISDIR(info.st_mode); |
| 1332 | } |
| 1333 | |
| 1334 | time_t get_mtime(int fd, time_t default_mtime) { |
| 1335 | struct stat info; |
| 1336 | if (fstat(fd, &info) == -1) { |
| 1337 | return default_mtime; |
| 1338 | } |
| 1339 | return info.st_mtime; |
| 1340 | } |
| 1341 | |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1342 | void dump_emmc_ecsd(const char *ext_csd_path) { |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1343 | // List of interesting offsets |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1344 | struct hex { |
| 1345 | char str[2]; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1346 | }; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1347 | static const size_t EXT_CSD_REV = 192 * sizeof(hex); |
| 1348 | static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex); |
| 1349 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex); |
| 1350 | static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex); |
| 1351 | |
| 1352 | std::string buffer; |
| 1353 | if (!android::base::ReadFileToString(ext_csd_path, &buffer)) { |
| 1354 | return; |
| 1355 | } |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1356 | |
| 1357 | printf("------ %s Extended CSD ------\n", ext_csd_path); |
| 1358 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1359 | if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) { |
| 1360 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1361 | return; |
| 1362 | } |
| 1363 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1364 | int ext_csd_rev = 0; |
| 1365 | std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex)); |
| 1366 | if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) { |
| 1367 | printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", |
| 1368 | ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1369 | return; |
| 1370 | } |
| 1371 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1372 | static const char *ver_str[] = { |
| 1373 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0" |
| 1374 | }; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1375 | printf("rev 1.%d (MMC %s)\n", |
| 1376 | ext_csd_rev, |
| 1377 | (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? |
| 1378 | ver_str[ext_csd_rev] : |
| 1379 | "Unknown"); |
| 1380 | if (ext_csd_rev < 7) { |
| 1381 | printf("\n"); |
| 1382 | return; |
| 1383 | } |
| 1384 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1385 | if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) { |
| 1386 | printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1387 | return; |
| 1388 | } |
| 1389 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1390 | int ext_pre_eol_info = 0; |
| 1391 | sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex)); |
| 1392 | if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) { |
| 1393 | printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", |
| 1394 | ext_csd_path, sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1395 | return; |
| 1396 | } |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1397 | |
| 1398 | static const char *eol_str[] = { |
| 1399 | "Undefined", |
| 1400 | "Normal", |
| 1401 | "Warning (consumed 80% of reserve)", |
| 1402 | "Urgent (consumed 90% of reserve)" |
| 1403 | }; |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1404 | printf("PRE_EOL_INFO %d (MMC %s)\n", |
| 1405 | ext_pre_eol_info, |
| 1406 | eol_str[(ext_pre_eol_info < (int) |
| 1407 | (sizeof(eol_str) / sizeof(eol_str[0]))) ? |
| 1408 | ext_pre_eol_info : 0]); |
| 1409 | |
| 1410 | for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A; |
| 1411 | lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1412 | lifetime += sizeof(hex)) { |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1413 | int ext_device_life_time_est; |
| 1414 | static const char *est_str[] = { |
| 1415 | "Undefined", |
| 1416 | "0-10% of device lifetime used", |
| 1417 | "10-20% of device lifetime used", |
| 1418 | "20-30% of device lifetime used", |
| 1419 | "30-40% of device lifetime used", |
| 1420 | "40-50% of device lifetime used", |
| 1421 | "50-60% of device lifetime used", |
| 1422 | "60-70% of device lifetime used", |
| 1423 | "70-80% of device lifetime used", |
| 1424 | "80-90% of device lifetime used", |
| 1425 | "90-100% of device lifetime used", |
| 1426 | "Exceeded the maximum estimated device lifetime", |
| 1427 | }; |
| 1428 | |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1429 | if (buffer.length() < (lifetime + sizeof(hex))) { |
| 1430 | printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1431 | break; |
| 1432 | } |
| 1433 | |
| 1434 | ext_device_life_time_est = 0; |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1435 | sub = buffer.substr(lifetime, sizeof(hex)); |
| 1436 | if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) { |
| 1437 | printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1438 | ext_csd_path, |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1439 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / |
| 1440 | sizeof(hex)) + 'A', |
| 1441 | sub.c_str()); |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1442 | continue; |
| 1443 | } |
| 1444 | printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n", |
Mark Salyzyn | 290f4b9 | 2016-05-16 08:33:59 -0700 | [diff] [blame] | 1445 | (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / |
| 1446 | sizeof(hex)) + 'A', |
Mark Salyzyn | 8c8130e | 2015-12-09 11:21:28 -0800 | [diff] [blame] | 1447 | ext_device_life_time_est, |
| 1448 | est_str[(ext_device_life_time_est < (int) |
| 1449 | (sizeof(est_str) / sizeof(est_str[0]))) ? |
| 1450 | ext_device_life_time_est : 0]); |
| 1451 | } |
| 1452 | |
| 1453 | printf("\n"); |
| 1454 | } |