bugreport介绍
android bugreport是一个用于记录和收集 android设备上系统信息、日志和调试信息的工具。
系统发生某些问题时,可以通过bugreport把系统当前时刻点(运行bugrepot的时刻)的系统相关的状态和信息都抓到一个zip中。
通过bugreport可以帮忙开发人员分析和解决问题。
bugreport其实就是一系列的信息的汇总,包括日志、内存状态、进程信息、崩溃信息、服务状态等等。用一个大而近乎全的现场,来帮忙更好的分析问题。
并非所有问题,都需要用bugreport抓取一份大而全的现场。可以根据经验考虑选用bugreport或者其他工具。
bugreport收集的信息一般包括:
- 设备软硬件信息
- 系统日志
- 系统运行状态,如cpu、内存、网络状态等。
- 程序崩溃信息,如anr、墓碑等。
bugreport使用方式
adb方式
adb bugreport
console方式
bugreportz -p
执行成功后,会在/data/user_de/0/com.android.shell/files/bugreports/下成一个生成一个 bugreport-*.zip的文件。
bugrepot成果物的命名方式
文件命名形式为:
bugreport-[device_name]-[build_id]-[localtime].zip
device_name:属性ro.product.name,默认为unkonw_device
build_id:属性ro.build.id的值,默认为unkown_build
localtime: 抓取bugreport时的本地时间
例如:
bugreport-arm-123.123-2024-02-28-19-18-14.zip
device_name:arm
build_id:123.123
localtime:2024-02-28-19-18-14
bugreport的实现
adb bugreport会调用adbd,让adbd执行bugreportz -p的shell命令。bugreportz 调用dumpstate -s,该命令会生成一个*.zip的bugreport文件。
生成后,bugreportz会将生成的通知通过stdout_fileno,告知adb。adb收到这个通知后,将对应的文件pull到host上。
adb bugreport到adbd执行bugrepotz -p
adb bugreport执行(host端),解析参数"bugreport"。
// packages/modules/adb/client/main.cpp int main(int argc, char* argv[], char* envp[]) { __adb_argv = const_cast<const char**>(argv); __adb_envp = const_cast<const char**>(envp); adb_trace_init(argv); return adb_commandline(argc - 1, const_cast<const char**>(argv + 1)); } // packages/modules/adb/client/commandline.cpp int adb_commandline(int argc, const char** argv) { // 省略 /* adb_connect() commands */ if (!strcmp(argv[0], "devices")) { // 省略 } else if (!strcmp(argv[0], "bugreport")) { bugreport bugreport; return bugreport.doit(argc, argv); } else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) { // 省略 }
bugreport类(adb模块)doit函数向adbd发送“bugreportz -v"和”bugreportz -p“命令。执行bugreportz -v,获取bugreportz的版本,一方面是执行版本差异的流程,另一方面也是作为test,测试bugreportz是否可以执行。
int bugreport::doit(int argc, const char** argv) { if (argc > 2) error_exit("usage: adb bugreport [[path] | [--stream]]"); // gets bugreportz version. std::string bugz_stdout, bugz_stderr; defaultstandardstreamscallback version_callback(&bugz_stdout, &bugz_stderr); int status = sendshellcommand("bugreportz -v", false, &version_callback); std::string bugz_version = android::base::trim(bugz_stderr); std::string bugz_output = android::base::trim(bugz_stdout); int bugz_ver_major = 0, bugz_ver_minor = 0; if (status != 0 || bugz_version.empty()) { d("'bugreportz' -v results: status=%d, stdout='%s', stderr='%s'", status, bugz_output.c_str(), bugz_version.c_str()); if (argc == 1) { // device does not support bugreportz: if called as 'adb bugreport', just falls out to // the flat-file version. fprintf(stderr, "failed to get bugreportz version, which is only available on devices " "running android 7.0 or later.\ntrying a plain-text bug report instead.\n"); return sendshellcommand("bugreport", false); } // but if user explicitly asked for a zipped bug report, fails instead (otherwise calling // 'bugreport' would generate a lot of output the user might not be prepared to handle). fprintf(stderr, "failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n" "if the device does not run android 7.0 or above, try this instead:\n" "\tadb bugreport > bugreport.txt\n", bugz_output.c_str(), status); return status != 0 ? status : -1; } std::sscanf(bugz_version.c_str(), "%d.%d", &bugz_ver_major, &bugz_ver_minor); std::string dest_file, dest_dir; if (argc == 1) { // no args - use current directory if (!getcwd(&dest_dir)) { perror("adb: getcwd failed"); return 1; } } else if (!strcmp(argv[1], "--stream")) { if (bugz_ver_major == 1 && bugz_ver_minor < 2) { fprintf(stderr, "failed to stream bugreport: bugreportz does not support stream.\n"); } else { return sendshellcommand("bugreportz -s", false); } } else { // check whether argument is a directory or file if (directory_exists(argv[1])) { dest_dir = argv[1]; } else { dest_file = argv[1]; } } if (dest_file.empty()) { // uses a default value until device provides the proper name dest_file = "bugreport.zip"; } else { if (!android::base::endswithignorecase(dest_file, ".zip")) { dest_file += ".zip"; } } bool show_progress = true; std::string bugz_command = "bugreportz -p"; if (bugz_version == "1.0") { // 1.0 does not support progress notifications, so print a disclaimer // message instead. fprintf(stderr, "bugreport is in progress and it could take minutes to complete.\n" "please be patient and do not cancel or disconnect your device " "until it completes.\n"); show_progress = false; bugz_command = "bugreportz"; } bugreportstandardstreamscallback bugz_callback(dest_dir, dest_file, show_progress, this); return sendshellcommand(bugz_command, false, &bugz_callback); }
adbd执行bugrepotz -p
此部分省略。就是adbd执行下面两个命令:shell bugreportz -v 和 bugreportz -p
不是主要关注点,想了了解的自行阅读源码即可。
bugreportz -p执行并调用dumpstate -s
通过bugreportz -p,收集系统当前时刻点的各种信息(信息参考上面的内容)。bugreportz -v比较简单,仅为输出一下bugreportz的版本。
// frameworks/native/cmds/bugreportz/main.cpp static constexpr char version[] = "1.2"; static void show_usage() { fprintf(stderr, "usage: bugreportz [-hpsv]\n" " -h: to display this help message\n" " -p: display progress\n" " -s: stream content to standard output\n" " -v: to display the version\n" " or no arguments to generate a zipped bugreport\n"); } static void show_version() { fprintf(stderr, "%s\n", version); } int main(int argc, char* argv[]) { bool show_progress = false; bool stream_data = false; if (argc > 1) { /* parse arguments */ int c; while ((c = getopt(argc, argv, "hpsv")) != -1) { switch (c) { case 'h': show_usage(); return exit_success; case 'p': show_progress = true; break; case 's': stream_data = true; break; case 'v': show_version(); return exit_success; default: show_usage(); return exit_failure; } } } // we don't support any non-option arguments. if (optind != argc) { show_usage(); return exit_failure; } // todo: code below was copy-and-pasted from bugreport.cpp (except by the // timeout value); // should be reused instead. // start the dumpstatez service. if (stream_data) { property_set("ctl.start", "dumpstate"); } else { // 调用dumpstatez property_set("ctl.start", "dumpstatez"); } // socket will not be available until service starts. int s = -1; for (int i = 0; i < 20; i++) { // 接连dumpstatez的socket(接收状态信息) s = socket_local_client("dumpstate", android_socket_namespace_reserved, sock_stream); if (s >= 0) break; // try again in 1 second. sleep(1); } if (s == -1) { printf("fail:failed to connect to dumpstatez service: %s\n", strerror(errno)); return exit_failure; } // set a timeout so that if nothing is read in 10 minutes, we'll stop // reading and quit. no timeout in dumpstate is longer than 60 seconds, // so this gives lots of leeway in case of unforeseen time outs. struct timeval tv; tv.tv_sec = 10 * 60; tv.tv_usec = 0; if (setsockopt(s, sol_socket, so_rcvtimeo, &tv, sizeof(tv)) == -1) { fprintf(stderr, "warning: cannot set socket timeout, bugreportz might hang indefinitely: %s\n", strerror(errno)); } int ret; if (stream_data) { ret = bugreportz_stream(s); } else { // 走这里,show_progress为true ret = bugreportz(s, show_progress); } if (close(s) == -1) { fprintf(stderr, "warning: error closing socket: %s\n", strerror(errno)); ret = exit_failure; } return ret; }
bugreportz函数中,接收dumpstatez(通过socket)返回的状态信息,并将其写到标准输出中。adb会通过标准输出,了解到命令执行的状态。为啥dumpstatez不将状态信息直接写到标准输出中?因为dumpstatez将标准输出重定向到文件了。
static constexpr char begin_prefix[] = "begin:"; static constexpr char progress_prefix[] = "progress:"; static void write_line(const std::string& line, bool show_progress) { if (line.empty()) return; // when not invoked with the -p option, it must skip begin and progress lines otherwise it// will break adb (which is expecting either ok or fail).if (!show_progress && (android::base::startswith(line, progress_prefix) || android::base::startswith(line, begin_prefix))) return; android::base::writestringtofd(line, stdout_fileno); } int bugreportz(int s, bool show_progress) { std::string line; while (1) { char buffer[65536]; ssize_t bytes_read = temp_failure_retry(read(s, buffer, sizeof(buffer))); if (bytes_read == 0) { break; } else if (bytes_read == -1) { // eagain really means time out, so change the errno.if (errno == eagain) { errno = etimedout; } printf("fail:bugreport read terminated abnormally (%s)\n", strerror(errno)); return exit_failure; } // writes line by line.for (int i = 0; i < bytes_read; i++) { char c = buffer[i]; line.append(1, c); if (c == '\n') { write_line(line, show_progress); line.clear(); } } } // process final line, in case it didn't finish with newlinewrite_line(line, show_progress); return exit_success; }
上面代码中通过 “ctl.start”, "dumpstatez"执行了dumpstatez。查看dumpstatez对应的rc文件。其对应/system/bin/dumpstate -s(注意为大写s)
service dumpstatez /system/bin/dumpstate -s
socket dumpstate stream 0660 shell log
class main
disabled
oneshot
dumpstate -s生成bugreport对应的zip文件
dumpstate -s命令执行
// frameworks/native/cmds/dumpstate/main.cpp int main(int argc, char* argv[]) { if (shouldstartserviceandwait(argc, argv)) { int ret; if ((ret = android::os::dumpstateservice::start()) != android::ok) { myloge("unable to start 'dumpstate' service: %d", ret); exit(1); } mylogi("'dumpstate' service started and will wait for a call to startbugreport()"); // waits forever for an incoming connection. // todo(b/111441001): should this time out? android::ipcthreadstate::self()->jointhreadpool(); return 0; } else { return run_main(argc, argv); } } // frameworks/native/cmds/dumpstate/dumpstate.cpp /* main entry point for dumpstate binary. */ int run_main(int argc, char* argv[]) { dumpstate::runstatus status = ds.parsecommandlineandrun(argc, argv); switch (status) { case dumpstate::runstatus::ok: exit(0); case dumpstate::runstatus::help: showusage(); exit(0); case dumpstate::runstatus::invalid_input: fprintf(stderr, "invalid combination of args\n"); showusage(); exit(1); case dumpstate::runstatus::error: fallthrough_intended; case dumpstate::runstatus::user_consent_denied: fallthrough_intended; case dumpstate::runstatus::user_consent_timed_out: exit(2); } } dumpstate::runstatus dumpstate::parsecommandlineandrun(int argc, char* argv[]) { std::unique_ptr<dumpstate::dumpoptions> options = std::make_unique<dumpstate::dumpoptions>(); dumpstate::runstatus status = options->initialize(argc, argv); if (status == dumpstate::runstatus::ok) { setoptions(std::move(options)); // when directly running dumpstate binary, the output is not expected to be written // to any external file descriptor. assert(options_->bugreport_fd.get() == -1); // calling_uid and calling_package are for user consent to share the bugreport with // an app; they are irrelevant here because bugreport is triggered via command line. // update last id before calling run(). initialize(); status = run(-1 /* calling_uid */, "" /* calling_package */); } return status; }
创建dumpstate::dumpoptions对象,调用initialize函数,解析输入参数“-s”。s(大写)会将参数的progress_updates_to_socket设置为ture,这个flag标志着dumpstate将状态告知给调用者(通过socket)
void dumpstate::dumpoptions::initialize(bugreportmode bugreport_mode, const android::base::unique_fd& bugreport_fd_in, const android::base::unique_fd& screenshot_fd_in, bool is_screenshot_requested) { // duplicate the fds because the passed in fds don't outlive the binder transaction. bugreport_fd.reset(dup(bugreport_fd_in.get())); screenshot_fd.reset(dup(screenshot_fd_in.get())); setoptionsfrommode(bugreport_mode, this, is_screenshot_requested); } dumpstate::runstatus dumpstate::dumpoptions::initialize(int argc, char* argv[]) { runstatus status = runstatus::ok; int c; while ((c = getopt(argc, argv, "dho:svqzplpbrsv:w")) != -1) { switch (c) { // clang-format off case 'o': out_dir = optarg; break; case 's': stream_to_socket = true; break; case 's': progress_updates_to_socket = true; break; case 'v': show_header_only = true; break; case 'q': do_vibrate = false; break; case 'p': do_screenshot = true; break; case 'p': do_progress_updates = true; break; case 'r': is_remote_mode = true; break; case 'l': limited_only = true; break; case 'v': case 'd': case 'z': // compatibility no-op break; case 'w': // this was already processed break; case 'h': status = runstatus::help; break; default: fprintf(stderr, "invalid option: %c\n", c); status = runstatus::invalid_input; break; // clang-format on } } for (int i = 0; i < argc; i++) { args += argv[i]; if (i < argc - 1) { args += " "; } } // reset next index used by getopt so this can be called multiple times, for eg, in tests. optind = 1; return status; }
然后调用dumpstate::initialize 和dumpstate::run,开始收集bugreport的内容。
void dumpstate::initialize() { /* gets the sequential id */ uint32_t last_id = android::base::getintproperty(property_last_id, 0); id_ = ++last_id; android::base::setproperty(property_last_id, std::to_string(last_id)); } dumpstate::runstatus dumpstate::run(int32_t calling_uid, const std::string& calling_package) { dumpstate::runstatus status = runinternal(calling_uid, calling_package); if (listener_ != nullptr) { switch (status) { case dumpstate::runstatus::ok: listener_->onfinished(); break; case dumpstate::runstatus::help: break; case dumpstate::runstatus::invalid_input: listener_->onerror(idumpstatelistener::bugreport_error_invalid_input); break; case dumpstate::runstatus::error: listener_->onerror(idumpstatelistener::bugreport_error_runtime_error); break; case dumpstate::runstatus::user_consent_denied: listener_->onerror(idumpstatelistener::bugreport_error_user_denied_consent); break; case dumpstate::runstatus::user_consent_timed_out: listener_->onerror(idumpstatelistener::bugreport_error_user_consent_timed_out); break; } } return status; }
dumpstate::run函数中调用runinternal实现bugreport的收集。该函数内容比较多,只关注三个主要流程:主要文件bugreport-*.txt的生成,log、anr等文件copy到zip文件中、zip文件的生成。
dumpstate::runstatus dumpstate::runinternal(int32_t calling_uid, const std::string& calling_package) { durationreporter duration_reporter("run internal", /* logcat_only = */true); logdumpoptions(*options_); if (!options_->validateoptions()) { myloge("invalid options specified\n"); return runstatus::invalid_input; } /* set as high priority, and protect from oom killer */ setpriority(prio_process, 0, -20); file* oom_adj = fopen("/proc/self/oom_score_adj", "we"); if (oom_adj) { fputs("-1000", oom_adj); fclose(oom_adj); } else { /* fallback to kernels <= 2.6.35 */ oom_adj = fopen("/proc/self/oom_adj", "we"); if (oom_adj) { fputs("-17", oom_adj); fclose(oom_adj); } } mylogi("dumpstate info: id=%d, args='%s', bugreport_mode= %s bugreport format version: %s\n", id_, options_->args.c_str(), options_->bugreport_mode.c_str(), version_.c_str()); // if we are going to use a socket, do it as early as possible // to avoid timeouts from bugreport. if (options_->stream_to_socket || options_->progress_updates_to_socket) { mylogd("opening control socket\n"); control_socket_fd_ = open_socket_fn_("dumpstate"); if (control_socket_fd_ == -1) { return error; } if (options_->progress_updates_to_socket) { options_->do_progress_updates = 1; } } // 准备文件 if (!preparetowritetofile()) { return error; } // 将标准输出,重定向到临时文件bugreport-*.tmp文件中 // 通过bugreport-*.tmp文件,产生最终的bugreport-*.zip // redirect stdout to tmp_path_. this is the main bugreport entry and will be // moved into zip file later, if zipping. temp_failure_retry(dup_stdout_fd = dup(fileno(stdout))); // todo: why not write to a file instead of stdout to overcome this problem? /* todo: rather than generating a text file now and zipping it later, it would be more efficient to redirect stdout to the zip entry directly, but the libziparchive doesn't support that option yet. */ if (!redirect_to_file(stdout, const_cast<char*>(tmp_path_.c_str()))) { return error; } if (chown(tmp_path_.c_str(), aid_shell, aid_shell)) { myloge("unable to change ownership of temporary bugreport file %s: %s\n", tmp_path_.c_str(), strerror(errno)); } // 输出头部分信息(就是bugreport-*.txt最开头的一些版本信息) // note: there should be no stdout output until now, otherwise it would break the header. // in particular, durationreport objects should be created passing 'title, null', so their // duration is logged into mylog instead. printheader(); bool is_dumpstate_restricted = options_->telephony_only || options_->wifi_only || options_->limited_only; if (!is_dumpstate_restricted) { // dump系统关键服务的状态 // ------ dumpsys critical (/system/bin/dumpsys) ------的信息 // invoke critical dumpsys first to preserve system state, before doing anything else. rundumpsyscritical(); } if (options_->telephony_only) { dumpstatetelephonyonly(calling_package); } else if (options_->wifi_only) { dumpstatewifionly(); } else if (options_->limited_only) { dumpstatelimitedonly(); } else { // dump state for the default case. this also drops root. // dump额外信息 runstatus s = dumpstatedefaultaftercritical(); if (s != runstatus::ok) { if (s == runstatus::user_consent_denied) { handleuserconsentdenied(); } return s; } } // 解除重定向 /* close output if needed */ temp_failure_retry(dup2(dup_stdout_fd, fileno(stdout))); // 完成zip的打包,删除临时文件 // zip the (now complete) .tmp file within the internal directory. finalizefile(); // 省略 }
preparetowritetofile函数中,确定bugreport信息写入的文件名。文件名命名方式为bugreport-[device_name]-[build_id]-[localtime]。文件信息会输出到log中。
dumpstate:
bugreport dir: [/data/user_de/0/com.android.shell/files/bugreports]
base name: [*] suffix: [2024-04-22-19-18-14]
log path: [/data/user_de/0/com.android.shell/files/bugreports/bugreport-*-2024-04-22-19-18-14-dumpstate_log-10419.txt]
temporary path: [/data/user_de/0/com.android.shell/files/bugreports/bugreport-*-2024-04-22-19-18-14-.tmp]
screenshot path: []
/* * prepares state like filename, screenshot path, etc in dumpstate. also initializes zipwriter * and adds the version file. return false if zip_file could not be open to write. */ static bool preparetowritetofile() { mayberesolvesymlink(&ds.bugreport_internal_dir_); std::string build_id = android::base::getproperty("ro.build.id", "unknown_build"); std::string device_name = android::base::getproperty("ro.product.name", "unknown_device"); ds.base_name_ = stringprintf("bugreport-%s-%s", device_name.c_str(), build_id.c_str()); char date[80]; strftime(date, sizeof(date), "%y-%m-%d-%h-%m-%s", localtime(&ds.now_)); ds.name_ = date; if (ds.options_->telephony_only) { ds.base_name_ += "-telephony"; } else if (ds.options_->wifi_only) { ds.base_name_ += "-wifi"; } if (ds.options_->do_screenshot) { ds.screenshot_path_ = ds.getpath(ds.calledbyapi() ? "-png.tmp" : ".png"); } ds.tmp_path_ = ds.getpath(".tmp"); ds.log_path_ = ds.getpath("-dumpstate_log-" + std::to_string(ds.pid_) + ".txt"); std::string destination = ds.calledbyapi() ? stringprintf("[fd:%d]", ds.options_->bugreport_fd.get()) : ds.bugreport_internal_dir_.c_str(); mylogd( "bugreport dir: [%s] " "base name: [%s] " "suffix: [%s] " "log path: [%s] " "temporary path: [%s] " "screenshot path: [%s]\n", destination.c_str(), ds.base_name_.c_str(), ds.name_.c_str(), ds.log_path_.c_str(), ds.tmp_path_.c_str(), ds.screenshot_path_.c_str()); ds.path_ = ds.getpath(ds.calledbyapi() ? "-zip.tmp" : ".zip"); mylogd("creating initial .zip file (%s)\n", ds.path_.c_str()); create_parent_dirs(ds.path_.c_str()); ds.zip_file.reset(fopen(ds.path_.c_str(), "wb")); if (ds.zip_file == nullptr) { myloge("fopen(%s, 'wb'): %s\n", ds.path_.c_str(), strerror(errno)); return false; } ds.zip_writer_.reset(new zipwriter(ds.zip_file.get())); ds.addtextzipentry("version.txt", ds.version_); return true; }
在printheader函数中,将bugreport-.txt中的头部信息输入到标准输出中,而标准输出已经重定向到了bugreport-.tmp文件中。
void dumpstate::printheader() const { std::string build, fingerprint, radio, bootloader, network; char date[80]; build = android::base::getproperty("ro.build.display.id", "(unknown)"); fingerprint = android::base::getproperty("ro.build.fingerprint", "(unknown)"); radio = android::base::getproperty("gsm.version.baseband", "(unknown)"); bootloader = android::base::getproperty("ro.bootloader", "(unknown)"); network = android::base::getproperty("gsm.operator.alpha", "(unknown)"); strftime(date, sizeof(date), "%y-%m-%d %h:%m:%s", localtime(&now_)); printf("========================================================\n"); printf("== dumpstate: %s\n", date); printf("========================================================\n"); printf("\n"); printf("build: %s\n", build.c_str()); // note: fingerprint entry format is important for other tools. printf("build fingerprint: '%s'\n", fingerprint.c_str()); printf("bootloader: %s\n", bootloader.c_str()); printf("radio: %s\n", radio.c_str()); printf("network: %s\n", network.c_str()); int64_t module_metadata_version = android::os::getmodulemetadataversion(); if (module_metadata_version != 0) { printf("module metadata version: %" prid64 "\n", module_metadata_version); } printf("sdk extension versions [r=%s s=%s]\n", android::base::getproperty("build.version.extensions.r", "-").c_str(), android::base::getproperty("build.version.extensions.s", "-").c_str()); printf("kernel: "); dumpfiletofd(stdout_fileno, "", "/proc/version"); printf("command line: %s\n", strtok(cmdline_buf, "\n")); printf("uptime: "); runcommandtofd(stdout_fileno, "", {"uptime", "-p"}, commandoptions::withtimeout(1).always().build()); printf("bugreport format version: %s\n", version_.c_str()); printf("dumpstate info: id=%d pid=%d dry_run=%d parallel_run=%d args=%s bugreport_mode=%s\n", id_, pid_, propertieshelper::isdryrun(), propertieshelper::isparallelrun(), options_->args.c_str(), options_->bugreport_mode.c_str()); printf("\n"); }
然后在rundumpsyscritical函数中,通过servicemanager获取当前系统的service,并调用service的dump,将service的dump信息输出到bugreport-*.tmp文件中。另外,会通过proto的形式再输出一份service的dump信息。
// frameworks/native/cmds/dumpstate/dumpstate.cpp static void rundumpsystext(const std::string& title, int priority, std::chrono::milliseconds timeout, std::chrono::milliseconds service_timeout) { durationreporter duration_reporter(title); dprintf(stdout_fileno, "------ %s (/system/bin/dumpsys) ------\n", title.c_str()); fsync(stdout_fileno); rundumpsystextbypriority(title, priority, timeout, service_timeout); } static void rundumpsystext(const std::string& title, int priority, std::chrono::milliseconds timeout, std::chrono::milliseconds service_timeout) { durationreporter duration_reporter(title); dprintf(stdout_fileno, "------ %s (/system/bin/dumpsys) ------\n", title.c_str()); fsync(stdout_fileno); rundumpsystextbypriority(title, priority, timeout, service_timeout); } static dumpstate::runstatus rundumpsystextbypriority(const std::string& title, int priority, std::chrono::milliseconds timeout, std::chrono::milliseconds service_timeout) { auto start = std::chrono::steady_clock::now(); sp<android::iservicemanager> sm = defaultservicemanager(); dumpsys dumpsys(sm.get()); vector<string16> args; dumpsys::setserviceargs(args, /* asproto = */ false, priority); vector<string16> services = dumpsys.listservices(priority, /* supports_proto = */ false); for (const string16& service : services) { return_if_user_denied_consent(); std::string path(title); path.append(" - ").append(string8(service).c_str()); size_t bytes_written = 0; // 在dumpthread中,调用service的dump status_t status = dumpsys.startdumpthread(dumpsys::type::dump, service, args); if (status == ok) { dumpsys.writedumpheader(stdout_fileno, service, priority); std::chrono::duration<double> elapsed_seconds; if (priority == iservicemanager::dump_flag_priority_high && service == string16("meminfo")) { // use a longer timeout for meminfo, since 30s is not always enough. status = dumpsys.writedump(stdout_fileno, service, 60s, /* as_proto = */ false, elapsed_seconds, bytes_written); } else { status = dumpsys.writedump(stdout_fileno, service, service_timeout, /* as_proto = */ false, elapsed_seconds, bytes_written); } dumpsys.writedumpfooter(stdout_fileno, service, elapsed_seconds); bool dump_complete = (status == ok); dumpsys.stopdumpthread(dump_complete); } auto elapsed_duration = std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - start); if (elapsed_duration > timeout) { myloge("*** command '%s' timed out after %llums\n", title.c_str(), elapsed_duration.count()); break; } } return dumpstate::runstatus::ok; }
在dumpstatedefaultaftercritical函数中,dump额外信息,以及将log、anr等等文件拷贝到zip文件中。
// frameworks/native/cmds/dumpstate/dumpstate.cpp dumpstate::runstatus dumpstate::dumpstatedefaultaftercritical() { // capture first logcat early on; useful to take a snapshot before dumpstate logs take over the // buffer. dologcat(); // capture timestamp after first logcat to use in next logcat time_t logcat_ts = time(nullptr); /* collect stack traces from dalvik and native processes (needs root) */ if (dump_pool_) { return_if_user_denied_consent(); // one thread is enough since we only need to enqueue dumptraces here. dump_pool_->start(/* thread_counts = */1); // dumptraces takes long time, post it to the another thread in the // pool, if pool is available dump_pool_->enqueuetask(dump_traces_task, &dumpstate::dumptraces, &ds, &dump_traces_path); } else { run_slow_function_with_consent_check_and_log(dump_traces_task, ds.dumptraces, &dump_traces_path); } /* run some operations that require root. */ ds.tombstone_data_ = getdumpfds(tombstone_dir, tombstone_file_prefix, !ds.iszipping()); ds.anr_data_ = getdumpfds(anr_dir, anr_file_prefix, !ds.iszipping()); ds.adddir(recovery_dir, true); ds.adddir(recovery_data_dir, true); ds.adddir(update_engine_log_dir, true); ds.adddir(logpersist_data_dir, false); if (!propertieshelper::isuserbuild()) { ds.adddir(profile_data_dir_cur, true); ds.adddir(profile_data_dir_ref, true); ds.addzipentry(zip_root_dir + package_dex_use_list, package_dex_use_list); } ds.adddir(prereboot_data_dir, false); add_mountinfo(); dumpiptablesasroot(); dumpdynamicpartitioninfo(); ds.adddir(ota_metadata_dir, true); // capture any ipsec policies in play. no keys are exposed here. runcommand("ip xfrm policy", {"ip", "xfrm", "policy"}, commandoptions::withtimeout(10).build()); // dump ipsec stats. no keys are exposed here. dumpfile("xfrm stats", xfrm_stat_proc_file); // run ss as root so we can see socket marks. runcommand("detailed socket state", {"ss", "-eionptu"}, commandoptions::withtimeout(10).build()); // run iotop as root to show top 100 io threads runcommand("iotop", {"iotop", "-n", "1", "-m", "100"}); // gather shared memory buffer info if the product implements it runcommand("dmabuf dump", {"dmabuf_dump"}); runcommand("dmabuf per-buffer/per-exporter/per-device stats", {"dmabuf_dump", "-b"}); dumpfile("psi cpu", "/proc/pressure/cpu"); dumpfile("psi memory", "/proc/pressure/memory"); dumpfile("psi io", "/proc/pressure/io"); if (dump_pool_) { return_if_user_denied_consent(); dump_pool_->waitfortask(dump_traces_task); // current running thread in the pool is the root user also. shutdown // the pool and restart later to ensure all threads in the pool could // drop the root user. dump_pool_->shutdown(); } if (!droprootuser()) { return dumpstate::runstatus::error; } return_if_user_denied_consent(); dumpstate::runstatus status = dumpstate(); // capture logcat since the last time we did it. dosystemlogcat(logcat_ts); return status; }
最后在finalizefile函数中,将临时文件bugreport-.tmp,copy到zip中,并命名为bugreport-.zip。然后删除临时文件,完成zip文件的落盘。
/* * finalizes writing to the file by zipping the tmp file to the final location, * printing zipped file status, etc. */static void finalizefile() { bool do_text_file = !ds.finishzipfile(); if (do_text_file) { myloge("failed to finish zip file; sending text bugreport instead\n"); } std::string final_path = ds.path_; if (ds.options_->outputtocustomfile()) { final_path = ds.getpath(ds.options_->out_dir, ".zip"); android::os::copyfiletofile(ds.path_, final_path); } if (ds.options_->stream_to_socket) { android::os::copyfiletofd(ds.path_, ds.control_socket_fd_); } else if (ds.options_->progress_updates_to_socket) { if (do_text_file) { dprintf(ds.control_socket_fd_, "fail:could not create zip file, check %s ""for more details\n", ds.log_path_.c_str()); } else { dprintf(ds.control_socket_fd_, "ok:%s\n", final_path.c_str()); } } }
adb将bugrepo-*.zip pull到本地
当bugreport文件收集好后,会触发adb将相关文件pull到本地(host),路径为执行adb命令的路径。
// packages/modules/adb/client/bugreport.cpp // custom callback used to handle the output of zipped bugreports. class bugreportstandardstreamscallback : public standardstreamscallbackinterface { public: bugreportstandardstreamscallback(const std::string& dest_dir, const std::string& dest_file, bool show_progress, bugreport* br) : br_(br), src_file_(), dest_dir_(dest_dir), dest_file_(dest_file), line_message_(), invalid_lines_(), show_progress_(show_progress), status_(0), line_(), last_progress_percentage_(0) { setlinemessage("generating"); } int done(int unused_) { // pull the generated bug report. if (status_ == 0) { // 将bugreport-*.zip文件 pull到本地(host) status_ = br_->dosyncpull(srcs, destination.c_str(), false, line_message_.c_str()) ? 0 : 1; if (status_ == 0) { printf("bug report copied to %s\n", destination.c_str()); } else { fprintf(stderr, "bug report finished but could not be copied to '%s'.\n" "try to run 'adb pull %s <directory>'\n" "to copy it to a directory that can be written.\n", destination.c_str(), src_file_.c_str()); } } return status_; }
以上就是android bugreport实现原理深入分析的详细内容,更多关于android bugreport的资料请关注代码网其它相关文章!
发表评论