blob: f649a5ed13262ae9b6240237bfef21037f44ee4e [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
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
Felipe Lemef0292972016-11-22 13:57:05 -080017#define LOG_TAG "dumpstate"
18
19#include "dumpstate.h"
20
Colin Crossf45fa6b2012-03-26 12:38:26 -070021#include <dirent.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070022#include <fcntl.h>
Felipe Lemee184f662016-10-27 10:04:47 -070023#include <libgen.h>
Felipe Leme7447d7c2016-11-03 18:12:22 -070024#include <math.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070025#include <poll.h>
26#include <signal.h>
27#include <stdarg.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Felipe Lemecf6a8b42016-03-11 10:38:19 -080031#include <sys/capability.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070032#include <sys/inotify.h>
Felipe Lemee184f662016-10-27 10:04:47 -070033#include <sys/klog.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070034#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070035#include <sys/stat.h>
36#include <sys/time.h>
37#include <sys/wait.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070038#include <time.h>
39#include <unistd.h>
Mark Salyzyn261a7332016-05-24 12:38:40 -070040
Steven Moreland17b29e12017-03-21 18:38:05 -070041#include <set>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070042#include <string>
Felipe Leme36b3f6f2015-11-19 15:41:04 -080043#include <vector>
Colin Crossf45fa6b2012-03-26 12:38:26 -070044
Mark Salyzyn290f4b92016-05-16 08:33:59 -070045#include <android-base/file.h>
Felipe Leme96c2bbb2016-09-26 09:21:21 -070046#include <android-base/properties.h>
Felipe Leme2b9b06c2016-10-14 09:13:06 -070047#include <android-base/stringprintf.h>
Felipe Leme7447d7c2016-11-03 18:12:22 -070048#include <android-base/strings.h>
Steven Moreland17b29e12017-03-21 18:38:05 -070049#include <android/hidl/manager/1.0/IServiceManager.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070050#include <cutils/properties.h>
51#include <cutils/sockets.h>
Josh Gaod2db0242017-01-05 18:27:33 -080052#include <debuggerd/client.h>
Mark Salyzyn4eb13822017-01-12 13:57:51 -080053#include <log/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070054#include <private/android_filesystem_config.h>
55
Felipe Lemef0292972016-11-22 13:57:05 -080056#include "DumpstateInternal.h"
Jeff Brown1dc94e32014-09-11 14:15:27 -070057
Felipe Leme47e9be22016-12-21 15:37:07 -080058// TODO: remove once moved to namespace
59using android::os::dumpstate::CommandOptions;
60using android::os::dumpstate::DumpFileToFd;
61using android::os::dumpstate::PropertiesHelper;
62
Brian Carlstroma3322752017-03-19 17:48:01 -070063// Keep in sync with
64// frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
Felipe Leme61884122016-06-13 09:23:30 -070065static const int TRACE_DUMP_TIMEOUT_MS = 10000; // 10 seconds
66
Felipe Lemef0292972016-11-22 13:57:05 -080067/* Most simple commands have 10 as timeout, so 5 is a good estimate */
68static const int32_t WEIGHT_FILE = 5;
69
Felipe Lemee844a9d2016-09-21 15:01:39 -070070// TODO: temporary variables and functions used during C++ refactoring
71static Dumpstate& ds = Dumpstate::GetInstance();
Felipe Leme9a523ae2016-10-20 15:10:33 -070072static int RunCommand(const std::string& title, const std::vector<std::string>& full_command,
Felipe Leme678727a2016-09-21 17:22:11 -070073 const CommandOptions& options = CommandOptions::DEFAULT) {
Felipe Leme9a523ae2016-10-20 15:10:33 -070074 return ds.RunCommand(title, full_command, options);
Felipe Leme678727a2016-09-21 17:22:11 -070075}
Felipe Lemee844a9d2016-09-21 15:01:39 -070076
Jeff Brownbf7f4922012-06-07 16:40:01 -070077/* list of native processes to include in the native dumps */
Andy Hung5c04e742016-04-13 19:35:34 -070078// This matches the /proc/pid/exe link instead of /proc/pid/cmdline.
Jeff Brownbf7f4922012-06-07 16:40:01 -070079static const char* native_processes_to_dump[] = {
Andy Hung9609bbd2015-12-15 12:42:50 -080080 "/system/bin/audioserver",
Chien-Yu Chenf5248da2016-01-28 14:23:03 -080081 "/system/bin/cameraserver",
James Dong1fc4f802012-09-10 16:08:48 -070082 "/system/bin/drmserver",
Andy Hung5c04e742016-04-13 19:35:34 -070083 "/system/bin/mediadrmserver",
84 "/system/bin/mediaextractor", // media.extractor
Jeff Brownbf7f4922012-06-07 16:40:01 -070085 "/system/bin/mediaserver",
86 "/system/bin/sdcard",
87 "/system/bin/surfaceflinger",
keunyoungd907b322015-10-16 15:21:43 -070088 "/system/bin/vehicle_network_service",
Jeff Brownbf7f4922012-06-07 16:40:01 -070089 NULL,
90};
91
Steven Moreland17b29e12017-03-21 18:38:05 -070092/* list of hal interface to dump containing process during native dumps */
93static const char* hal_interfaces_to_dump[] {
94 "android.hardware.audio@2.0::IDevicesFactory",
95 "android.hardware.bluetooth@1.0::IBluetoothHci",
96 "android.hardware.camera.provider@2.4::ICameraProvider",
Chia-I Wu108c0f02017-04-20 14:31:50 -070097 "android.hardware.graphics.composer@2.1::IComposer",
Steven Moreland17b29e12017-03-21 18:38:05 -070098 "android.hardware.vr@1.0::IVr",
99 "android.hardware.media.omx@1.0::IOmx",
100 NULL,
101};
102
Felipe Leme7447d7c2016-11-03 18:12:22 -0700103// Reasonable value for max stats.
104static const int STATS_MAX_N_RUNS = 1000;
105static const long STATS_MAX_AVERAGE = 100000;
106
Felipe Lemebda15a02016-11-16 17:48:25 -0800107CommandOptions Dumpstate::DEFAULT_DUMPSYS = CommandOptions::WithTimeout(30).Build();
Felipe Leme30dbfa12016-09-02 12:43:26 -0700108
Felipe Lemef0292972016-11-22 13:57:05 -0800109Dumpstate::Dumpstate(const std::string& version)
110 : pid_(getpid()), version_(version), now_(time(nullptr)) {
Felipe Lemee844a9d2016-09-21 15:01:39 -0700111}
112
113Dumpstate& Dumpstate::GetInstance() {
Felipe Lemef0292972016-11-22 13:57:05 -0800114 static Dumpstate singleton_(android::base::GetProperty("dumpstate.version", VERSION_CURRENT));
Felipe Leme9a523ae2016-10-20 15:10:33 -0700115 return singleton_;
Felipe Lemee844a9d2016-09-21 15:01:39 -0700116}
117
Felipe Leme46b85da2016-11-21 17:40:45 -0800118DurationReporter::DurationReporter(const std::string& title, bool log_only)
119 : title_(title), log_only_(log_only) {
Felipe Leme678727a2016-09-21 17:22:11 -0700120 if (!title_.empty()) {
Felipe Lemef0292972016-11-22 13:57:05 -0800121 started_ = Nanotime();
Felipe Leme78f2c862015-12-21 09:55:22 -0800122 }
123}
124
125DurationReporter::~DurationReporter() {
Felipe Leme678727a2016-09-21 17:22:11 -0700126 if (!title_.empty()) {
Felipe Lemef0292972016-11-22 13:57:05 -0800127 uint64_t elapsed = Nanotime() - started_;
Felipe Leme46b85da2016-11-21 17:40:45 -0800128 if (log_only_) {
Felipe Leme678727a2016-09-21 17:22:11 -0700129 MYLOGD("Duration of '%s': %.3fs\n", title_.c_str(), (float)elapsed / NANOS_PER_SEC);
Felipe Leme46b85da2016-11-21 17:40:45 -0800130 } else {
131 // Use "Yoda grammar" to make it easier to grep|sort sections.
Felipe Lemed8b94e52016-12-08 10:21:44 -0800132 printf("------ %.3fs was the duration of '%s' ------\n", (float)elapsed / NANOS_PER_SEC,
133 title_.c_str());
Felipe Leme608385d2016-02-01 10:35:38 -0800134 }
Felipe Leme78f2c862015-12-21 09:55:22 -0800135 }
136}
137
Felipe Leme7447d7c2016-11-03 18:12:22 -0700138const int32_t Progress::kDefaultMax = 5000;
139
140Progress::Progress(const std::string& path) : Progress(Progress::kDefaultMax, 1.1, path) {
141}
142
143Progress::Progress(int32_t initial_max, int32_t progress, float growth_factor)
144 : Progress(initial_max, growth_factor, "") {
145 progress_ = progress;
146}
147
148Progress::Progress(int32_t initial_max, float growth_factor, const std::string& path)
149 : initial_max_(initial_max),
150 progress_(0),
151 max_(initial_max),
152 growth_factor_(growth_factor),
153 n_runs_(0),
154 average_max_(0),
155 path_(path) {
156 if (!path_.empty()) {
157 Load();
158 }
159}
160
161void Progress::Load() {
162 MYLOGD("Loading stats from %s\n", path_.c_str());
163 std::string content;
164 if (!android::base::ReadFileToString(path_, &content)) {
165 MYLOGI("Could not read stats from %s; using max of %d\n", path_.c_str(), max_);
166 return;
167 }
168 if (content.empty()) {
169 MYLOGE("No stats (empty file) on %s; using max of %d\n", path_.c_str(), max_);
170 return;
171 }
172 std::vector<std::string> lines = android::base::Split(content, "\n");
173
174 if (lines.size() < 1) {
175 MYLOGE("Invalid stats on file %s: not enough lines (%d). Using max of %d\n", path_.c_str(),
176 (int)lines.size(), max_);
177 return;
178 }
179 char* ptr;
180 n_runs_ = strtol(lines[0].c_str(), &ptr, 10);
181 average_max_ = strtol(ptr, nullptr, 10);
182 if (n_runs_ <= 0 || average_max_ <= 0 || n_runs_ > STATS_MAX_N_RUNS ||
183 average_max_ > STATS_MAX_AVERAGE) {
184 MYLOGE("Invalid stats line on file %s: %s\n", path_.c_str(), lines[0].c_str());
185 initial_max_ = Progress::kDefaultMax;
186 } else {
187 initial_max_ = average_max_;
188 }
189 max_ = initial_max_;
190
191 MYLOGI("Average max progress: %d in %d runs; estimated max: %d\n", average_max_, n_runs_, max_);
192}
193
194void Progress::Save() {
195 int32_t total = n_runs_ * average_max_ + progress_;
196 int32_t runs = n_runs_ + 1;
197 int32_t average = floor(((float)total) / runs);
198 MYLOGI("Saving stats (total=%d, runs=%d, average=%d) on %s\n", total, runs, average,
199 path_.c_str());
200 if (path_.empty()) {
201 return;
202 }
203
204 std::string content = android::base::StringPrintf("%d %d\n", runs, average);
205 if (!android::base::WriteStringToFile(content, path_)) {
206 MYLOGE("Could not save stats on %s\n", path_.c_str());
207 }
208}
209
210int32_t Progress::Get() const {
211 return progress_;
212}
213
214bool Progress::Inc(int32_t delta) {
215 bool changed = false;
216 if (delta >= 0) {
217 progress_ += delta;
218 if (progress_ > max_) {
219 int32_t old_max = max_;
220 max_ = floor((float)progress_ * growth_factor_);
221 MYLOGD("Adjusting max progress from %d to %d\n", old_max, max_);
222 changed = true;
223 }
224 }
225 return changed;
226}
227
228int32_t Progress::GetMax() const {
229 return max_;
230}
231
232int32_t Progress::GetInitialMax() const {
233 return initial_max_;
234}
235
236void Progress::Dump(int fd, const std::string& prefix) const {
237 const char* pr = prefix.c_str();
238 dprintf(fd, "%sprogress: %d\n", pr, progress_);
239 dprintf(fd, "%smax: %d\n", pr, max_);
240 dprintf(fd, "%sinitial_max: %d\n", pr, initial_max_);
241 dprintf(fd, "%sgrowth_factor: %0.2f\n", pr, growth_factor_);
242 dprintf(fd, "%spath: %s\n", pr, path_.c_str());
243 dprintf(fd, "%sn_runs: %d\n", pr, n_runs_);
244 dprintf(fd, "%saverage_max: %d\n", pr, average_max_);
245}
246
Felipe Leme75876a22016-10-27 16:31:27 -0700247bool Dumpstate::IsZipping() const {
248 return zip_writer_ != nullptr;
249}
250
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700251std::string Dumpstate::GetPath(const std::string& suffix) const {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700252 return android::base::StringPrintf("%s/%s-%s%s", bugreport_dir_.c_str(), base_name_.c_str(),
Felipe Leme2b9b06c2016-10-14 09:13:06 -0700253 name_.c_str(), suffix.c_str());
Felipe Lemebbaf3c12016-10-11 14:32:25 -0700254}
255
Felipe Leme7447d7c2016-11-03 18:12:22 -0700256void Dumpstate::SetProgress(std::unique_ptr<Progress> progress) {
257 progress_ = std::move(progress);
258}
259
John Spurlock5ecd4be2014-01-29 14:14:40 -0500260void for_each_userid(void (*func)(int), const char *header) {
Felipe Leme8f00ed02016-12-07 17:42:44 -0800261 std::string title = header == nullptr ? "for_each_userid" : android::base::StringPrintf(
262 "for_each_userid(%s)", header);
263 DurationReporter duration_reporter(title);
Felipe Lemef0292972016-11-22 13:57:05 -0800264 if (PropertiesHelper::IsDryRun()) return;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700265
John Spurlock5ecd4be2014-01-29 14:14:40 -0500266 DIR *d;
267 struct dirent *de;
268
Felipe Lemed8b94e52016-12-08 10:21:44 -0800269 if (header) printf("\n------ %s ------\n", header);
John Spurlock5ecd4be2014-01-29 14:14:40 -0500270 func(0);
271
272 if (!(d = opendir("/data/system/users"))) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800273 printf("Failed to open /data/system/users (%s)\n", strerror(errno));
John Spurlock5ecd4be2014-01-29 14:14:40 -0500274 return;
275 }
276
277 while ((de = readdir(d))) {
278 int userid;
279 if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
280 continue;
281 }
282 func(userid);
283 }
284
285 closedir(d);
286}
287
Colin Cross0c22e8b2012-11-02 15:46:56 -0700288static void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700289 DIR *d;
290 struct dirent *de;
291
292 if (!(d = opendir("/proc"))) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800293 printf("Failed to open /proc (%s)\n", strerror(errno));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700294 return;
295 }
296
Felipe Lemed8b94e52016-12-08 10:21:44 -0800297 if (header) printf("\n------ %s ------\n", header);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700298 while ((de = readdir(d))) {
299 int pid;
300 int fd;
301 char cmdpath[255];
302 char cmdline[255];
303
304 if (!(pid = atoi(de->d_name))) {
305 continue;
306 }
307
Colin Crossf45fa6b2012-03-26 12:38:26 -0700308 memset(cmdline, 0, sizeof(cmdline));
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800309
310 snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid);
311 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
312 TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700313 close(fd);
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800314 if (cmdline[0]) {
315 helper(pid, cmdline, arg);
316 continue;
317 }
318 }
319
320 // if no cmdline, a kernel thread has comm
321 snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid);
322 if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
323 TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4));
324 close(fd);
325 if (cmdline[1]) {
326 cmdline[0] = '[';
327 size_t len = strcspn(cmdline, "\f\b\r\n");
328 cmdline[len] = ']';
329 cmdline[len+1] = '\0';
330 }
331 }
332 if (!cmdline[0]) {
333 strcpy(cmdline, "N/A");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700334 }
Colin Cross0c22e8b2012-11-02 15:46:56 -0700335 helper(pid, cmdline, arg);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700336 }
337
338 closedir(d);
339}
340
Colin Cross0c22e8b2012-11-02 15:46:56 -0700341static void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
Felipe Leme8620bb42015-11-10 11:04:45 -0800342 for_each_pid_func *func = (for_each_pid_func*) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700343 func(pid, cmdline);
344}
345
346void for_each_pid(for_each_pid_func func, const char *header) {
Felipe Leme8f00ed02016-12-07 17:42:44 -0800347 std::string title = header == nullptr ? "for_each_pid"
348 : android::base::StringPrintf("for_each_pid(%s)", header);
349 DurationReporter duration_reporter(title);
Felipe Lemef0292972016-11-22 13:57:05 -0800350 if (PropertiesHelper::IsDryRun()) return;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700351
Felipe Leme515eb0d2015-12-14 15:09:56 -0800352 __for_each_pid(for_each_pid_helper, header, (void *) func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700353}
354
355static void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
356 DIR *d;
357 struct dirent *de;
358 char taskpath[255];
Felipe Leme8620bb42015-11-10 11:04:45 -0800359 for_each_tid_func *func = (for_each_tid_func *) arg;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700360
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700361 snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700362
363 if (!(d = opendir(taskpath))) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800364 printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
Colin Cross0c22e8b2012-11-02 15:46:56 -0700365 return;
366 }
367
368 func(pid, pid, cmdline);
369
370 while ((de = readdir(d))) {
371 int tid;
372 int fd;
373 char commpath[255];
374 char comm[255];
375
376 if (!(tid = atoi(de->d_name))) {
377 continue;
378 }
379
380 if (tid == pid)
381 continue;
382
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700383 snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid);
Colin Cross1493a392012-11-07 11:25:31 -0800384 memset(comm, 0, sizeof(comm));
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700385 if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
Colin Cross0c22e8b2012-11-02 15:46:56 -0700386 strcpy(comm, "N/A");
387 } else {
388 char *c;
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800389 TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2));
Colin Cross0c22e8b2012-11-02 15:46:56 -0700390 close(fd);
391
392 c = strrchr(comm, '\n');
393 if (c) {
394 *c = '\0';
395 }
396 }
397 func(pid, tid, comm);
398 }
399
400 closedir(d);
401}
402
403void for_each_tid(for_each_tid_func func, const char *header) {
Felipe Leme8f00ed02016-12-07 17:42:44 -0800404 std::string title = header == nullptr ? "for_each_tid"
405 : android::base::StringPrintf("for_each_tid(%s)", header);
406 DurationReporter duration_reporter(title);
Felipe Lemed8b94e52016-12-08 10:21:44 -0800407
Felipe Lemef0292972016-11-22 13:57:05 -0800408 if (PropertiesHelper::IsDryRun()) return;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700409
Felipe Leme8620bb42015-11-10 11:04:45 -0800410 __for_each_pid(for_each_tid_helper, header, (void *) func);
Colin Cross0c22e8b2012-11-02 15:46:56 -0700411}
412
413void show_wchan(int pid, int tid, const char *name) {
Felipe Lemef0292972016-11-22 13:57:05 -0800414 if (PropertiesHelper::IsDryRun()) return;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700415
Colin Crossf45fa6b2012-03-26 12:38:26 -0700416 char path[255];
417 char buffer[255];
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800418 int fd, ret, save_errno;
Colin Cross0c22e8b2012-11-02 15:46:56 -0700419 char name_buffer[255];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700420
421 memset(buffer, 0, sizeof(buffer));
422
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700423 snprintf(path, sizeof(path), "/proc/%d/wchan", tid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700424 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800425 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700426 return;
427 }
428
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800429 ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
430 save_errno = errno;
431 close(fd);
432
433 if (ret < 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800434 printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800435 return;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700436 }
437
Colin Cross0c22e8b2012-11-02 15:46:56 -0700438 snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
439 pid == tid ? 0 : 3, "", name);
440
Felipe Lemed8b94e52016-12-08 10:21:44 -0800441 printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700442
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800443 return;
444}
445
446// print time in centiseconds
447static void snprcent(char *buffer, size_t len, size_t spc,
448 unsigned long long time) {
449 static long hz; // cache discovered hz
450
451 if (hz <= 0) {
452 hz = sysconf(_SC_CLK_TCK);
453 if (hz <= 0) {
454 hz = 1000;
455 }
456 }
457
458 // convert to centiseconds
459 time = (time * 100 + (hz / 2)) / hz;
460
461 char str[16];
462
463 snprintf(str, sizeof(str), " %llu.%02u",
464 time / 100, (unsigned)(time % 100));
465 size_t offset = strlen(buffer);
466 snprintf(buffer + offset, (len > offset) ? len - offset : 0,
467 "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
468}
469
470// print permille as a percent
471static void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) {
472 char str[16];
473
474 snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10);
475 size_t offset = strlen(buffer);
476 snprintf(buffer + offset, (len > offset) ? len - offset : 0,
477 "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
478}
479
480void show_showtime(int pid, const char *name) {
Felipe Lemef0292972016-11-22 13:57:05 -0800481 if (PropertiesHelper::IsDryRun()) return;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700482
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800483 char path[255];
484 char buffer[1023];
485 int fd, ret, save_errno;
486
487 memset(buffer, 0, sizeof(buffer));
488
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700489 snprintf(path, sizeof(path), "/proc/%d/stat", pid);
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800490 if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800491 printf("Failed to open '%s' (%s)\n", path, strerror(errno));
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800492 return;
493 }
494
495 ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
496 save_errno = errno;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700497 close(fd);
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800498
499 if (ret < 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800500 printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800501 return;
502 }
503
504 // field 14 is utime
505 // field 15 is stime
506 // field 42 is iotime
507 unsigned long long utime = 0, stime = 0, iotime = 0;
508 if (sscanf(buffer,
Mark Salyzyn791ddd32016-02-10 07:41:12 -0800509 "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d "
510 "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d "
511 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
512 "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ",
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800513 &utime, &stime, &iotime) != 3) {
514 return;
515 }
516
517 unsigned long long total = utime + stime;
518 if (!total) {
519 return;
520 }
521
522 unsigned permille = (iotime * 1000 + (total / 2)) / total;
523 if (permille > 1000) {
524 permille = 1000;
525 }
526
527 // try to beautify and stabilize columns at <80 characters
528 snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name);
529 if ((name[0] != '[') || utime) {
530 snprcent(buffer, sizeof(buffer), 57, utime);
531 }
532 snprcent(buffer, sizeof(buffer), 65, stime);
533 if ((name[0] != '[') || iotime) {
534 snprcent(buffer, sizeof(buffer), 73, iotime);
535 }
536 if (iotime) {
537 snprdec(buffer, sizeof(buffer), 79, permille);
538 }
Felipe Lemed8b94e52016-12-08 10:21:44 -0800539 puts(buffer); // adds a trailing newline
Mark Salyzyn0751efa2016-02-05 15:33:17 -0800540
Colin Crossf45fa6b2012-03-26 12:38:26 -0700541 return;
542}
543
544void do_dmesg() {
Felipe Leme78f2c862015-12-21 09:55:22 -0800545 const char *title = "KERNEL LOG (dmesg)";
546 DurationReporter duration_reporter(title);
Felipe Lemed8b94e52016-12-08 10:21:44 -0800547 printf("------ %s ------\n", title);
Felipe Leme78f2c862015-12-21 09:55:22 -0800548
Felipe Lemef0292972016-11-22 13:57:05 -0800549 if (PropertiesHelper::IsDryRun()) return;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700550
Elliott Hughes5f87b312012-09-17 11:43:40 -0700551 /* Get size of kernel buffer */
552 int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700553 if (size <= 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800554 printf("Unexpected klogctl return value: %d\n\n", size);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700555 return;
556 }
557 char *buf = (char *) malloc(size + 1);
558 if (buf == NULL) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800559 printf("memory allocation failed\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700560 return;
561 }
562 int retval = klogctl(KLOG_READ_ALL, buf, size);
563 if (retval < 0) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800564 printf("klogctl failure\n\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700565 free(buf);
566 return;
567 }
568 buf[retval] = '\0';
Felipe Lemed8b94e52016-12-08 10:21:44 -0800569 printf("%s\n\n", buf);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700570 free(buf);
571 return;
572}
573
574void do_showmap(int pid, const char *name) {
575 char title[255];
576 char arg[255];
577
Nick Kralevichf0922cc2016-05-14 16:47:44 -0700578 snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name);
579 snprintf(arg, sizeof(arg), "%d", pid);
Felipe Lemef0292972016-11-22 13:57:05 -0800580 RunCommand(title, {"showmap", "-q", arg}, CommandOptions::AS_ROOT);
Felipe Lemebda15a02016-11-16 17:48:25 -0800581}
582
Felipe Leme678727a2016-09-21 17:22:11 -0700583int Dumpstate::DumpFile(const std::string& title, const std::string& path) {
Felipe Leme9a523ae2016-10-20 15:10:33 -0700584 DurationReporter duration_reporter(title);
Felipe Leme46b85da2016-11-21 17:40:45 -0800585
Felipe Lemef0292972016-11-22 13:57:05 -0800586 int status = DumpFileToFd(STDOUT_FILENO, title, path);
Felipe Leme46b85da2016-11-21 17:40:45 -0800587
Felipe Lemef0292972016-11-22 13:57:05 -0800588 UpdateProgress(WEIGHT_FILE);
Felipe Leme46b85da2016-11-21 17:40:45 -0800589
Felipe Leme46b85da2016-11-21 17:40:45 -0800590 return status;
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800591}
592
Felipe Leme71a74ac2016-03-17 15:43:25 -0700593int read_file_as_long(const char *path, long int *output) {
594 int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
595 if (fd < 0) {
596 int err = errno;
597 MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err));
598 return -1;
599 }
600 char buffer[50];
601 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
602 if (bytes_read == -1) {
603 MYLOGE("Error reading file %s: %s\n", path, strerror(errno));
604 return -2;
605 }
606 if (bytes_read == 0) {
607 MYLOGE("File %s is empty\n", path);
608 return -3;
609 }
610 *output = atoi(buffer);
611 return 0;
612}
613
Mark Salyzyn326842f2015-04-30 09:49:41 -0700614/* calls skip to gate calling dump_from_fd recursively
615 * in the specified directory. dump_from_fd defaults to
616 * dump_file_from_fd above when set to NULL. skip defaults
617 * to false when set to NULL. dump_from_fd will always be
618 * called with title NULL.
619 */
Felipe Leme678727a2016-09-21 17:22:11 -0700620int dump_files(const std::string& title, const char* dir, bool (*skip)(const char* path),
621 int (*dump_from_fd)(const char* title, const char* path, int fd)) {
Felipe Leme78f2c862015-12-21 09:55:22 -0800622 DurationReporter duration_reporter(title);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700623 DIR *dirp;
624 struct dirent *d;
625 char *newpath = NULL;
Felipe Leme8620bb42015-11-10 11:04:45 -0800626 const char *slash = "/";
Mark Salyzyn326842f2015-04-30 09:49:41 -0700627 int fd, retval = 0;
628
Felipe Leme678727a2016-09-21 17:22:11 -0700629 if (!title.empty()) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800630 printf("------ %s (%s) ------\n", title.c_str(), dir);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700631 }
Felipe Lemef0292972016-11-22 13:57:05 -0800632 if (PropertiesHelper::IsDryRun()) return 0;
Mark Salyzyn326842f2015-04-30 09:49:41 -0700633
634 if (dir[strlen(dir) - 1] == '/') {
635 ++slash;
636 }
637 dirp = opendir(dir);
638 if (dirp == NULL) {
639 retval = -errno;
Felipe Leme107a05f2016-03-08 15:11:15 -0800640 MYLOGE("%s: %s\n", dir, strerror(errno));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700641 return retval;
642 }
643
644 if (!dump_from_fd) {
645 dump_from_fd = dump_file_from_fd;
646 }
647 for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
648 if ((d->d_name[0] == '.')
649 && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
650 || (d->d_name[1] == '\0'))) {
651 continue;
652 }
653 asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
654 (d->d_type == DT_DIR) ? "/" : "");
655 if (!newpath) {
656 retval = -errno;
657 continue;
658 }
659 if (skip && (*skip)(newpath)) {
660 continue;
661 }
662 if (d->d_type == DT_DIR) {
Felipe Leme678727a2016-09-21 17:22:11 -0700663 int ret = dump_files("", newpath, skip, dump_from_fd);
Mark Salyzyn326842f2015-04-30 09:49:41 -0700664 if (ret < 0) {
665 retval = ret;
666 }
667 continue;
668 }
669 fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
670 if (fd < 0) {
671 retval = fd;
Felipe Lemed8b94e52016-12-08 10:21:44 -0800672 printf("*** %s: %s\n", newpath, strerror(errno));
Mark Salyzyn326842f2015-04-30 09:49:41 -0700673 continue;
674 }
675 (*dump_from_fd)(NULL, newpath, fd);
676 }
677 closedir(dirp);
Felipe Leme678727a2016-09-21 17:22:11 -0700678 if (!title.empty()) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800679 printf("\n");
Mark Salyzyn326842f2015-04-30 09:49:41 -0700680 }
681 return retval;
682}
683
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800684/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
685 * it's possible to avoid issues where opening the file itself can get
686 * stuck.
687 */
688int dump_file_from_fd(const char *title, const char *path, int fd) {
Felipe Lemef0292972016-11-22 13:57:05 -0800689 if (PropertiesHelper::IsDryRun()) return 0;
Felipe Lemed402e7d2016-08-03 09:22:27 -0700690
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800691 int flags = fcntl(fd, F_GETFL);
692 if (flags == -1) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800693 printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800694 close(fd);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800695 return -1;
696 } else if (!(flags & O_NONBLOCK)) {
Felipe Lemed8b94e52016-12-08 10:21:44 -0800697 printf("*** %s: fd must have O_NONBLOCK set.\n", path);
Christopher Ferrised24d2a2015-11-12 14:01:56 -0800698 close(fd);
Christopher Ferris54bcc5f2015-02-10 12:15:01 -0800699 return -1;
700 }
Felipe Lemef0292972016-11-22 13:57:05 -0800701 return DumpFileFromFdToFd(title, path, fd, STDOUT_FILENO, PropertiesHelper::IsDryRun());
Colin Crossf45fa6b2012-03-26 12:38:26 -0700702}
703
Felipe Leme46b85da2016-11-21 17:40:45 -0800704int Dumpstate::RunCommand(const std::string& title, const std::vector<std::string>& full_command,
705 const CommandOptions& options) {
706 DurationReporter duration_reporter(title);
707
Felipe Lemef0292972016-11-22 13:57:05 -0800708 int status = RunCommandToFd(STDOUT_FILENO, title, full_command, options);
Felipe Leme46b85da2016-11-21 17:40:45 -0800709
Felipe Lemef0292972016-11-22 13:57:05 -0800710 /* TODO: for now we're simplifying the progress calculation by using the
711 * timeout as the weight. It's a good approximation for most cases, except when calling dumpsys,
712 * where its weight should be much higher proportionally to its timeout.
713 * Ideally, it should use a options.EstimatedDuration() instead...*/
714 UpdateProgress(options.Timeout());
Felipe Leme46b85da2016-11-21 17:40:45 -0800715
Felipe Leme46b85da2016-11-21 17:40:45 -0800716 return status;
717}
718
Felipe Leme9a523ae2016-10-20 15:10:33 -0700719void Dumpstate::RunDumpsys(const std::string& title, const std::vector<std::string>& dumpsys_args,
Felipe Leme678727a2016-09-21 17:22:11 -0700720 const CommandOptions& options, long dumpsysTimeout) {
Felipe Leme5bcce572016-09-27 09:21:08 -0700721 long timeout = dumpsysTimeout > 0 ? dumpsysTimeout : options.Timeout();
722 std::vector<std::string> dumpsys = {"/system/bin/dumpsys", "-t", std::to_string(timeout)};
Felipe Leme9a523ae2016-10-20 15:10:33 -0700723 dumpsys.insert(dumpsys.end(), dumpsys_args.begin(), dumpsys_args.end());
Felipe Leme678727a2016-09-21 17:22:11 -0700724 RunCommand(title, dumpsys, options);
Felipe Leme30dbfa12016-09-02 12:43:26 -0700725}
726
Felipe Leme2628e9e2016-04-12 16:36:51 -0700727int open_socket(const char *service) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700728 int s = android_get_control_socket(service);
729 if (s < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800730 MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700731 exit(1);
732 }
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700733 fcntl(s, F_SETFD, FD_CLOEXEC);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700734 if (listen(s, 4) < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800735 MYLOGE("listen(control socket): %s\n", strerror(errno));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700736 exit(1);
737 }
738
739 struct sockaddr addr;
740 socklen_t alen = sizeof(addr);
741 int fd = accept(s, &addr, &alen);
742 if (fd < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800743 MYLOGE("accept(control socket): %s\n", strerror(errno));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700744 exit(1);
745 }
746
Felipe Leme2628e9e2016-04-12 16:36:51 -0700747 return fd;
748}
749
750/* redirect output to a service control socket */
751void redirect_to_socket(FILE *redirect, const char *service) {
752 int fd = open_socket(service);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700753 fflush(redirect);
754 dup2(fd, fileno(redirect));
755 close(fd);
756}
757
Felipe Leme2628e9e2016-04-12 16:36:51 -0700758// TODO: should call is_valid_output_file and/or be merged into it.
Felipe Leme111b9d02016-02-03 09:28:24 -0800759void create_parent_dirs(const char *path) {
Srinath Sridharanfdf52d32016-02-01 15:50:22 -0800760 char *chp = const_cast<char *> (path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700761
762 /* skip initial slash */
763 if (chp[0] == '/')
764 chp++;
765
766 /* create leading directories, if necessary */
Felipe Leme111b9d02016-02-03 09:28:24 -0800767 struct stat dir_stat;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700768 while (chp && chp[0]) {
769 chp = strchr(chp, '/');
770 if (chp) {
771 *chp = 0;
Felipe Leme111b9d02016-02-03 09:28:24 -0800772 if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800773 MYLOGI("Creating directory %s\n", path);
Felipe Leme111b9d02016-02-03 09:28:24 -0800774 if (mkdir(path, 0770)) { /* drwxrwx--- */
Felipe Lemecbce55d2016-02-08 09:53:18 -0800775 MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno));
Felipe Leme111b9d02016-02-03 09:28:24 -0800776 } else if (chown(path, AID_SHELL, AID_SHELL)) {
Felipe Lemecbce55d2016-02-08 09:53:18 -0800777 MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno));
Felipe Leme111b9d02016-02-03 09:28:24 -0800778 }
779 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700780 *chp++ = '/';
781 }
782 }
Felipe Leme111b9d02016-02-03 09:28:24 -0800783}
784
Felipe Leme0f3fb202016-06-10 17:10:53 -0700785void _redirect_to_file(FILE *redirect, char *path, int truncate_flag) {
Felipe Leme111b9d02016-02-03 09:28:24 -0800786 create_parent_dirs(path);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700787
Felipe Leme0f3fb202016-06-10 17:10:53 -0700788 int fd = TEMP_FAILURE_RETRY(open(path,
789 O_WRONLY | O_CREAT | truncate_flag | O_CLOEXEC | O_NOFOLLOW,
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800790 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700791 if (fd < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800792 MYLOGE("%s: %s\n", path, strerror(errno));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700793 exit(1);
794 }
795
Christopher Ferrisff4a4dc2015-02-09 16:24:47 -0800796 TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
Colin Crossf45fa6b2012-03-26 12:38:26 -0700797 close(fd);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700798}
799
Felipe Leme0f3fb202016-06-10 17:10:53 -0700800void redirect_to_file(FILE *redirect, char *path) {
801 _redirect_to_file(redirect, path, O_TRUNC);
802}
803
804void redirect_to_existing_file(FILE *redirect, char *path) {
805 _redirect_to_file(redirect, path, O_APPEND);
806}
807
Steven Moreland17b29e12017-03-21 18:38:05 -0700808static bool should_dump_hal_interface(const char* interface) {
809 for (const char** i = hal_interfaces_to_dump; *i; i++) {
810 if (!strcmp(*i, interface)) {
811 return true;
812 }
813 }
814 return false;
815}
816
Jeff Brownbf7f4922012-06-07 16:40:01 -0700817static bool should_dump_native_traces(const char* path) {
818 for (const char** p = native_processes_to_dump; *p; p++) {
819 if (!strcmp(*p, path)) {
820 return true;
821 }
822 }
823 return false;
824}
825
Steven Moreland17b29e12017-03-21 18:38:05 -0700826std::set<int> get_interesting_hal_pids() {
827 using android::hidl::manager::V1_0::IServiceManager;
828 using android::sp;
829 using android::hardware::Return;
830
831 sp<IServiceManager> manager = IServiceManager::getService();
832 std::set<int> pids;
833
834 Return<void> ret = manager->debugDump([&](auto& hals) {
835 for (const auto &info : hals) {
836 if (info.pid == static_cast<int>(IServiceManager::PidConstant::NO_PID)) {
837 continue;
838 }
839
Scott Randolph5a0c4852017-04-03 14:05:49 -0700840 if (!should_dump_hal_interface(info.interfaceName.c_str())) {
Steven Moreland17b29e12017-03-21 18:38:05 -0700841 continue;
842 }
843
844 pids.insert(info.pid);
845 }
846 });
847
848 if (!ret.isOk()) {
849 MYLOGE("Could not get list of HAL PIDs: %s\n", ret.description().c_str());
850 }
851
852 return pids; // whether it was okay or not
853}
854
Jeff Brownbf7f4922012-06-07 16:40:01 -0700855/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
856const char *dump_traces() {
Felipe Lemee184f662016-10-27 10:04:47 -0700857 DurationReporter duration_reporter("DUMP TRACES");
Felipe Lemed402e7d2016-08-03 09:22:27 -0700858
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700859 const char* result = nullptr;
Jeff Brownbf7f4922012-06-07 16:40:01 -0700860
Felipe Lemee184f662016-10-27 10:04:47 -0700861 std::string traces_path = android::base::GetProperty("dalvik.vm.stack-trace-file", "");
862 if (traces_path.empty()) return nullptr;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700863
864 /* move the old traces.txt (if any) out of the way temporarily */
Felipe Lemee184f662016-10-27 10:04:47 -0700865 std::string anrtraces_path = traces_path + ".anr";
866 if (rename(traces_path.c_str(), anrtraces_path.c_str()) && errno != ENOENT) {
867 MYLOGE("rename(%s, %s): %s\n", traces_path.c_str(), anrtraces_path.c_str(), strerror(errno));
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700868 return nullptr; // Can't rename old traces.txt -- no permission? -- leave it alone instead
Colin Crossf45fa6b2012-03-26 12:38:26 -0700869 }
870
Colin Crossf45fa6b2012-03-26 12:38:26 -0700871 /* create a new, empty traces.txt file to receive stack dumps */
Josh Gao989b8122017-01-13 22:09:40 -0800872 int fd = TEMP_FAILURE_RETRY(
Josh Gaoe75d9632017-01-18 16:14:03 -0800873 open(traces_path.c_str(), O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
Josh Gao989b8122017-01-13 22:09:40 -0800874 0666)); /* -rw-rw-rw- */
Colin Crossf45fa6b2012-03-26 12:38:26 -0700875 if (fd < 0) {
Felipe Lemee184f662016-10-27 10:04:47 -0700876 MYLOGE("%s: %s\n", traces_path.c_str(), strerror(errno));
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700877 return nullptr;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700878 }
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700879 int chmod_ret = fchmod(fd, 0666);
880 if (chmod_ret < 0) {
Felipe Lemee184f662016-10-27 10:04:47 -0700881 MYLOGE("fchmod on %s failed: %s\n", traces_path.c_str(), strerror(errno));
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700882 close(fd);
Felipe Leme96c2bbb2016-09-26 09:21:21 -0700883 return nullptr;
Nick Kralevichc7f1fe22012-04-06 09:31:28 -0700884 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700885
Felipe Leme8620bb42015-11-10 11:04:45 -0800886 /* Variables below must be initialized before 'goto' statements */
887 int dalvik_found = 0;
888 int ifd, wfd = -1;
Steven Moreland17b29e12017-03-21 18:38:05 -0700889 std::set<int> hal_pids = get_interesting_hal_pids();
Felipe Leme8620bb42015-11-10 11:04:45 -0800890
Colin Crossf45fa6b2012-03-26 12:38:26 -0700891 /* walk /proc and kill -QUIT all Dalvik processes */
892 DIR *proc = opendir("/proc");
893 if (proc == NULL) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800894 MYLOGE("/proc: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700895 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700896 }
897
898 /* use inotify to find when processes are done dumping */
Felipe Leme8620bb42015-11-10 11:04:45 -0800899 ifd = inotify_init();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700900 if (ifd < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800901 MYLOGE("inotify_init: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700902 goto error_close_fd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700903 }
904
Felipe Lemee184f662016-10-27 10:04:47 -0700905 wfd = inotify_add_watch(ifd, traces_path.c_str(), IN_CLOSE_WRITE);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700906 if (wfd < 0) {
Felipe Lemee184f662016-10-27 10:04:47 -0700907 MYLOGE("inotify_add_watch(%s): %s\n", traces_path.c_str(), strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700908 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700909 }
910
911 struct dirent *d;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700912 while ((d = readdir(proc))) {
913 int pid = atoi(d->d_name);
914 if (pid <= 0) continue;
915
Jeff Brownbf7f4922012-06-07 16:40:01 -0700916 char path[PATH_MAX];
917 char data[PATH_MAX];
Colin Crossf45fa6b2012-03-26 12:38:26 -0700918 snprintf(path, sizeof(path), "/proc/%d/exe", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700919 ssize_t len = readlink(path, data, sizeof(data) - 1);
920 if (len <= 0) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700921 continue;
922 }
Jeff Brownbf7f4922012-06-07 16:40:01 -0700923 data[len] = '\0';
Colin Crossf45fa6b2012-03-26 12:38:26 -0700924
Colin Cross0d6180f2014-07-16 19:00:46 -0700925 if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700926 /* skip zygote -- it won't dump its stack anyway */
927 snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -0700928 int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700929 len = read(cfd, data, sizeof(data) - 1);
930 close(cfd);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700931 if (len <= 0) {
932 continue;
933 }
934 data[len] = '\0';
Colin Cross0d6180f2014-07-16 19:00:46 -0700935 if (!strncmp(data, "zygote", strlen("zygote"))) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700936 continue;
937 }
938
939 ++dalvik_found;
Felipe Lemef0292972016-11-22 13:57:05 -0800940 uint64_t start = Nanotime();
Jeff Brownbf7f4922012-06-07 16:40:01 -0700941 if (kill(pid, SIGQUIT)) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800942 MYLOGE("kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700943 continue;
944 }
945
946 /* wait for the writable-close notification from inotify */
947 struct pollfd pfd = { ifd, POLLIN, 0 };
Felipe Leme61884122016-06-13 09:23:30 -0700948 int ret = poll(&pfd, 1, TRACE_DUMP_TIMEOUT_MS);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700949 if (ret < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800950 MYLOGE("poll: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700951 } else if (ret == 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800952 MYLOGE("warning: timed out dumping pid %d\n", pid);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700953 } else {
954 struct inotify_event ie;
955 read(ifd, &ie, sizeof(ie));
956 }
Jeff Brown1dc94e32014-09-11 14:15:27 -0700957
958 if (lseek(fd, 0, SEEK_END) < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800959 MYLOGE("lseek: %s\n", strerror(errno));
Jeff Brown1dc94e32014-09-11 14:15:27 -0700960 } else {
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700961 dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n", pid,
Felipe Lemef0292972016-11-22 13:57:05 -0800962 (float)(Nanotime() - start) / NANOS_PER_SEC);
Jeff Brown1dc94e32014-09-11 14:15:27 -0700963 }
Steven Moreland17b29e12017-03-21 18:38:05 -0700964 } else if (should_dump_native_traces(data) ||
965 hal_pids.find(pid) != hal_pids.end()) {
Jeff Brownbf7f4922012-06-07 16:40:01 -0700966 /* dump native process if appropriate */
967 if (lseek(fd, 0, SEEK_END) < 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800968 MYLOGE("lseek: %s\n", strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700969 } else {
Christopher Ferris31ef8552015-01-14 13:23:30 -0800970 static uint16_t timeout_failures = 0;
Felipe Lemef0292972016-11-22 13:57:05 -0800971 uint64_t start = Nanotime();
Christopher Ferris31ef8552015-01-14 13:23:30 -0800972
973 /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
974 if (timeout_failures == 3) {
975 dprintf(fd, "too many stack dump failures, skipping...\n");
976 } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
977 dprintf(fd, "dumping failed, likely due to a timeout\n");
978 timeout_failures++;
979 } else {
980 timeout_failures = 0;
981 }
Felipe Lemeb0f669d2016-09-26 18:26:11 -0700982 dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n", pid,
Felipe Lemef0292972016-11-22 13:57:05 -0800983 (float)(Nanotime() - start) / NANOS_PER_SEC);
Jeff Brownbf7f4922012-06-07 16:40:01 -0700984 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700985 }
986 }
987
Colin Crossf45fa6b2012-03-26 12:38:26 -0700988 if (dalvik_found == 0) {
Felipe Leme107a05f2016-03-08 15:11:15 -0800989 MYLOGE("Warning: no Dalvik processes found to dump stacks\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700990 }
991
Felipe Lemee184f662016-10-27 10:04:47 -0700992 static std::string dumptraces_path = android::base::StringPrintf(
993 "%s/bugreport-%s", dirname(traces_path.c_str()), basename(traces_path.c_str()));
994 if (rename(traces_path.c_str(), dumptraces_path.c_str())) {
995 MYLOGE("rename(%s, %s): %s\n", traces_path.c_str(), dumptraces_path.c_str(),
996 strerror(errno));
Jeff Brownbf7f4922012-06-07 16:40:01 -0700997 goto error_close_ifd;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700998 }
Felipe Lemee184f662016-10-27 10:04:47 -0700999 result = dumptraces_path.c_str();
Colin Crossf45fa6b2012-03-26 12:38:26 -07001000
1001 /* replace the saved [ANR] traces.txt file */
Felipe Lemee184f662016-10-27 10:04:47 -07001002 rename(anrtraces_path.c_str(), traces_path.c_str());
Jeff Brownbf7f4922012-06-07 16:40:01 -07001003
1004error_close_ifd:
1005 close(ifd);
1006error_close_fd:
1007 close(fd);
1008 return result;
Colin Crossf45fa6b2012-03-26 12:38:26 -07001009}
1010
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -07001011void dump_route_tables() {
Felipe Leme78f2c862015-12-21 09:55:22 -08001012 DurationReporter duration_reporter("DUMP ROUTE TABLES");
Felipe Lemef0292972016-11-22 13:57:05 -08001013 if (PropertiesHelper::IsDryRun()) return;
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -07001014 const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
Felipe Lemec7fe8fe2016-09-21 18:13:20 -07001015 ds.DumpFile("RT_TABLES", RT_TABLES_PATH);
Nick Kralevichcd67e9f2015-03-19 11:30:59 -07001016 FILE* fp = fopen(RT_TABLES_PATH, "re");
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -07001017 if (!fp) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001018 printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -07001019 return;
1020 }
1021 char table[16];
1022 // Each line has an integer (the table number), a space, and a string (the table name). We only
1023 // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
1024 // Add a fixed max limit so this doesn't go awry.
1025 for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
Felipe Lemeb0f669d2016-09-26 18:26:11 -07001026 RunCommand("ROUTE TABLE IPv4", {"ip", "-4", "route", "show", "table", table});
1027 RunCommand("ROUTE TABLE IPv6", {"ip", "-6", "route", "show", "table", table});
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -07001028 }
1029 fclose(fp);
1030}
Felipe Leme71bbfc52015-11-23 14:14:51 -08001031
Felipe Leme71bbfc52015-11-23 14:14:51 -08001032// TODO: make this function thread safe if sections are generated in parallel.
Felipe Leme7447d7c2016-11-03 18:12:22 -07001033void Dumpstate::UpdateProgress(int32_t delta) {
1034 if (progress_ == nullptr) {
1035 MYLOGE("UpdateProgress: progress_ not set\n");
1036 return;
1037 }
Felipe Leme71bbfc52015-11-23 14:14:51 -08001038
Felipe Leme7447d7c2016-11-03 18:12:22 -07001039 // Always update progess so stats can be tuned...
1040 bool max_changed = progress_->Inc(delta);
1041
1042 // ...but only notifiy listeners when necessary.
1043 if (!update_progress_) return;
Felipe Leme71bbfc52015-11-23 14:14:51 -08001044
Felipe Leme009ecbb2016-11-07 10:18:44 -08001045 int progress = progress_->Get();
1046 int max = progress_->GetMax();
Felipe Lemead5f6c42015-11-30 14:26:46 -08001047
1048 // adjusts max on the fly
Felipe Leme009ecbb2016-11-07 10:18:44 -08001049 if (max_changed && listener_ != nullptr) {
1050 listener_->onMaxProgressUpdated(max);
Felipe Lemead5f6c42015-11-30 14:26:46 -08001051 }
1052
Felipe Leme009ecbb2016-11-07 10:18:44 -08001053 int32_t last_update_delta = progress - last_updated_progress_;
1054 if (last_updated_progress_ > 0 && last_update_delta < update_progress_threshold_) {
1055 return;
1056 }
1057 last_updated_progress_ = progress;
Felipe Leme7447d7c2016-11-03 18:12:22 -07001058
Felipe Leme9a523ae2016-10-20 15:10:33 -07001059 if (control_socket_fd_ >= 0) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001060 dprintf(control_socket_fd_, "PROGRESS:%d/%d\n", progress, max);
Felipe Leme9a523ae2016-10-20 15:10:33 -07001061 fsync(control_socket_fd_);
Felipe Leme02b7e002016-07-22 12:03:20 -07001062 }
1063
Felipe Leme75876a22016-10-27 16:31:27 -07001064 if (listener_ != nullptr) {
Felipe Leme7447d7c2016-11-03 18:12:22 -07001065 if (progress % 100 == 0) {
Felipe Leme75876a22016-10-27 16:31:27 -07001066 // We don't want to spam logcat, so only log multiples of 100.
Felipe Leme7447d7c2016-11-03 18:12:22 -07001067 MYLOGD("Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress, max);
Felipe Leme75876a22016-10-27 16:31:27 -07001068 } else {
1069 // stderr is ignored on normal invocations, but useful when calling
1070 // /system/bin/dumpstate directly for debuggging.
Felipe Leme7447d7c2016-11-03 18:12:22 -07001071 fprintf(stderr, "Setting progress (%s): %d/%d\n", listener_name_.c_str(), progress, max);
Felipe Leme75876a22016-10-27 16:31:27 -07001072 }
Felipe Leme7447d7c2016-11-03 18:12:22 -07001073 listener_->onProgressUpdated(progress);
Felipe Leme71bbfc52015-11-23 14:14:51 -08001074 }
1075}
Felipe Lemee338bf62015-12-07 14:03:50 -08001076
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001077void Dumpstate::TakeScreenshot(const std::string& path) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001078 const std::string& real_path = path.empty() ? screenshot_path_ : path;
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001079 int status =
Felipe Leme9a523ae2016-10-20 15:10:33 -07001080 RunCommand("", {"/system/bin/screencap", "-p", real_path},
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001081 CommandOptions::WithTimeout(10).Always().DropRoot().RedirectStderr().Build());
1082 if (status == 0) {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001083 MYLOGD("Screenshot saved on %s\n", real_path.c_str());
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001084 } else {
Felipe Leme9a523ae2016-10-20 15:10:33 -07001085 MYLOGE("Failed to take screenshot on %s\n", real_path.c_str());
Felipe Lemebbaf3c12016-10-11 14:32:25 -07001086 }
Felipe Lemee338bf62015-12-07 14:03:50 -08001087}
Mark Salyzynf55d4022015-12-11 07:32:31 -08001088
Felipe Leme0c80cf02016-01-05 13:25:34 -08001089bool is_dir(const char* pathname) {
1090 struct stat info;
1091 if (stat(pathname, &info) == -1) {
1092 return false;
1093 }
1094 return S_ISDIR(info.st_mode);
1095}
1096
1097time_t get_mtime(int fd, time_t default_mtime) {
1098 struct stat info;
1099 if (fstat(fd, &info) == -1) {
1100 return default_mtime;
1101 }
1102 return info.st_mtime;
1103}
1104
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001105void dump_emmc_ecsd(const char *ext_csd_path) {
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001106 // List of interesting offsets
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001107 struct hex {
1108 char str[2];
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001109 };
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001110 static const size_t EXT_CSD_REV = 192 * sizeof(hex);
1111 static const size_t EXT_PRE_EOL_INFO = 267 * sizeof(hex);
1112 static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_A = 268 * sizeof(hex);
1113 static const size_t EXT_DEVICE_LIFE_TIME_EST_TYP_B = 269 * sizeof(hex);
1114
1115 std::string buffer;
1116 if (!android::base::ReadFileToString(ext_csd_path, &buffer)) {
1117 return;
1118 }
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001119
Felipe Lemed8b94e52016-12-08 10:21:44 -08001120 printf("------ %s Extended CSD ------\n", ext_csd_path);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001121
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001122 if (buffer.length() < (EXT_CSD_REV + sizeof(hex))) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001123 printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001124 return;
1125 }
1126
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001127 int ext_csd_rev = 0;
1128 std::string sub = buffer.substr(EXT_CSD_REV, sizeof(hex));
1129 if (sscanf(sub.c_str(), "%2x", &ext_csd_rev) != 1) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001130 printf("*** %s: EXT_CSD_REV parse error \"%s\"\n\n", ext_csd_path, sub.c_str());
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001131 return;
1132 }
1133
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001134 static const char *ver_str[] = {
1135 "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0"
1136 };
Felipe Lemed8b94e52016-12-08 10:21:44 -08001137 printf("rev 1.%d (MMC %s)\n", ext_csd_rev,
1138 (ext_csd_rev < (int)(sizeof(ver_str) / sizeof(ver_str[0]))) ? ver_str[ext_csd_rev]
1139 : "Unknown");
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001140 if (ext_csd_rev < 7) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001141 printf("\n");
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001142 return;
1143 }
1144
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001145 if (buffer.length() < (EXT_PRE_EOL_INFO + sizeof(hex))) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001146 printf("*** %s: truncated content %zu\n\n", ext_csd_path, buffer.length());
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001147 return;
1148 }
1149
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001150 int ext_pre_eol_info = 0;
1151 sub = buffer.substr(EXT_PRE_EOL_INFO, sizeof(hex));
1152 if (sscanf(sub.c_str(), "%2x", &ext_pre_eol_info) != 1) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001153 printf("*** %s: PRE_EOL_INFO parse error \"%s\"\n\n", ext_csd_path, sub.c_str());
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001154 return;
1155 }
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001156
1157 static const char *eol_str[] = {
1158 "Undefined",
1159 "Normal",
1160 "Warning (consumed 80% of reserve)",
1161 "Urgent (consumed 90% of reserve)"
1162 };
Felipe Lemed8b94e52016-12-08 10:21:44 -08001163 printf(
1164 "PRE_EOL_INFO %d (MMC %s)\n", ext_pre_eol_info,
Felipe Leme8f00ed02016-12-07 17:42:44 -08001165 eol_str[(ext_pre_eol_info < (int)(sizeof(eol_str) / sizeof(eol_str[0]))) ? ext_pre_eol_info
1166 : 0]);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001167
1168 for (size_t lifetime = EXT_DEVICE_LIFE_TIME_EST_TYP_A;
1169 lifetime <= EXT_DEVICE_LIFE_TIME_EST_TYP_B;
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001170 lifetime += sizeof(hex)) {
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001171 int ext_device_life_time_est;
1172 static const char *est_str[] = {
1173 "Undefined",
1174 "0-10% of device lifetime used",
1175 "10-20% of device lifetime used",
1176 "20-30% of device lifetime used",
1177 "30-40% of device lifetime used",
1178 "40-50% of device lifetime used",
1179 "50-60% of device lifetime used",
1180 "60-70% of device lifetime used",
1181 "70-80% of device lifetime used",
1182 "80-90% of device lifetime used",
1183 "90-100% of device lifetime used",
1184 "Exceeded the maximum estimated device lifetime",
1185 };
1186
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001187 if (buffer.length() < (lifetime + sizeof(hex))) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001188 printf("*** %s: truncated content %zu\n", ext_csd_path, buffer.length());
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001189 break;
1190 }
1191
1192 ext_device_life_time_est = 0;
Mark Salyzyn290f4b92016-05-16 08:33:59 -07001193 sub = buffer.substr(lifetime, sizeof(hex));
1194 if (sscanf(sub.c_str(), "%2x", &ext_device_life_time_est) != 1) {
Felipe Lemed8b94e52016-12-08 10:21:44 -08001195 printf("*** %s: DEVICE_LIFE_TIME_EST_TYP_%c parse error \"%s\"\n", ext_csd_path,
1196 (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A',
1197 sub.c_str());
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001198 continue;
1199 }
Felipe Lemed8b94e52016-12-08 10:21:44 -08001200 printf("DEVICE_LIFE_TIME_EST_TYP_%c %d (MMC %s)\n",
1201 (unsigned)((lifetime - EXT_DEVICE_LIFE_TIME_EST_TYP_A) / sizeof(hex)) + 'A',
1202 ext_device_life_time_est,
1203 est_str[(ext_device_life_time_est < (int)(sizeof(est_str) / sizeof(est_str[0])))
1204 ? ext_device_life_time_est
1205 : 0]);
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001206 }
1207
Felipe Lemed8b94e52016-12-08 10:21:44 -08001208 printf("\n");
Mark Salyzyn8c8130e2015-12-09 11:21:28 -08001209}