【Android12】Bugreport实现原理

Bugreport实现原理

Bugreport

Bugreport介绍

Android Bugreport是一个用于记录和收集 Android设备上系统信息、日志和调试信息的工具。
系统发生某些问题时,可以通过bugreport把系统当前时刻点(运行BugRepot的时刻)的系统相关的状态和信息都抓到一个zip中。
通过bugreport可以帮忙开发人员分析和解决问题。

Bugreport其实就是一系列的信息的汇总,包括日志、内存状态、进程信息、崩溃信息、服务状态等等。用一个大而近乎全的现场,来帮忙更好的分析问题。
并非所有问题,都需要用Bugreport抓取一份大而全的现场。可以根据经验考虑选用bugreport或者其他工具。
Bugreport收集的信息一般包括:

  1. 设备软硬件信息
  2. 系统日志
  3. 系统运行状态,如cpu、内存、网络状态等。
  4. 程序崩溃信息,如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.cppint 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.cppint 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 directoryif (!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 fileif (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 namedest_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 {// 调用dumpstatezproperty_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为Trueret = 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 -Ssocket dumpstate stream 0660 shell logclass maindisabledoneshot
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 offcase '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-opbreak;case 'w':// This was already processedbreak;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 entrydirectly, 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的dumpstatus_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 logcattime_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 availabledump_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 threadsRunCommand("IOTOP", {"iotop", "-n", "1", "-m", "100"});// Gather shared memory buffer info if the product implements itRunCommand("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_;}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/828959.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

HarmonyOS 实战开发-MindSpore Lite引擎进行模型推理

场景介绍 MindSpore Lite 是一款 AI 引擎&#xff0c;它提供了面向不同硬件设备 AI 模型推理的功能&#xff0c;目前已经在图像分类、目标识别、人脸识别、文字识别等应用中广泛使用。 本文介绍使用 MindSpore Lite 推理引擎进行模型推理的通用开发流程。 基本概念 在进行开…

YOLOv5利用Labelimg标注自己数据集

目录 一、Labelimg介绍二、Labelimg下载三、Labelimg安装四、Labelimg使用1、准备2、开始标注 一、Labelimg介绍 LabelImg是一款开源的图片标注工具&#xff0c;使用Python编写&#xff0c;基于PyQt5框架。它提供了一个直观的图形用户界面&#xff0c;方便用户对图片进行标注&…

axios——503响应超时重复多次请求——技能提升

今天在写后台管理系统时&#xff0c;遇到一个问题&#xff0c;就是每天早上一启动项目&#xff0c;接口会提示503超时&#xff0c;因此项目运行必须重新刷新请求成功后才可以正常使用。 后端同事说请求超时了&#xff0c;需要前端处理一下&#xff0c;如果是503的状态码&#…

Golang特殊init函数

介绍 init()函数是一个特殊的函数&#xff0c;存在一下特性 不能被其它函数调用&#xff0c;而是子main()函数之前自动调用不能作为参数传入不能有传入参数和返回值 作用&#xff1a; 对变量进行初始化检查/修复程序状态注册运行一次计算 以下是<<the way to go>>…

Web APIs 学习归纳1---Web API概述简单的元素获取

JS基础中我们学习了JS的基本语句&#xff0c;这些是后续学习中的基础。 有了上述JS的基础以后&#xff0c;我们就可以开始学习交互效果的实现方法。这里很多时候直接调用JS封装好的API即可。 一、Web APIs 和 JS 基础关联性 这里有一张图很好的解释了这个问题&#xff1a; 我们…

python高校贫困学生资助奖学金管理系统vue+django

我们根据项目文档&#xff0c;包括规格说明、文档及在设计过程中形成的测试大纲、测试内容及测试的通过准则、再次全面熟悉系统&#xff0c;做好测试准备。为了保证测试的质量&#xff0c;我们将测试过程分为几个阶段。第一阶段&#xff0c;在单元测试阶段我们对每个子模块进行…

可视化+多人协同技术原理和案例分享

前言 hi&#xff0c;大家好&#xff0c;我是徐小夕&#xff0c;之前和大家分享了很多可视化低代码的技术实践&#xff0c;最近也做了一款非常有意思的文档搭建引擎——Nocode/Doc&#xff1a; 也做了一些分享&#xff1a; Nocode/Doc&#xff0c;可视化 零代码打造下一代文件编…

SpringBoot+vue开发记录(二)

说明&#xff1a;本篇文章的主要内容为SpringBoot开发中后端的创建 项目创建: 1. 新建项目&#xff1a; 如下&#xff0c;这样简单创建就行了&#xff0c;JDK什么的就先17&#xff0c;当然1.8也是可以的&#xff0c;后面可以改。 这样就创建好了&#xff1a; 2. pom.xml…

ChatGPT全方位指导:学术论文写作从零开始,轻松搞定高质量论文!

目录 文末福利 一、论文选题的深度探讨 二、撰写摘要的艺术 三、关键词的精选 四、引言的构建 五、正文的结构设计 六、撰写结论的策略 七、致谢的编写 八、附录的有效利用 九、参考文献的整理 文末有福利哦 撰写一篇高质量的学术论文是一项既复杂又耗时的任务。这个…

MongoDB数据恢复—拷贝MongoDB数据库文件后无法启动服务的数据恢复案例

服务器数据恢复环境&#xff1a; 一台Windows Server操作系统服务器&#xff0c;服务器上部署MongoDB数据库。 MongoDB数据库故障&检测&#xff1a; 工作人员在未关闭MongoDB数据库服务的情况下&#xff0c;将数据库文件拷贝到其他分区。拷贝完成后将原MongoDB数据库所在分…

8个拿来即用的Python自动化脚本!

每天你都可能会执行许多重复的任务&#xff0c;例如阅读新闻、发邮件、查看天气、清理文件夹等等&#xff0c;使用自动化脚本&#xff0c;就无需手动一次又一次地完成这些任务&#xff0c;非常方便。而在某种程度上&#xff0c;Python 就是自动化的代名词。 今天分享 8 个非常…

医学影像增强:空间域方法与频域方法等

医学影像图像增强是一项关键技术,旨在改善图像质量,以便更好地进行疾病诊断和评估。增强方法通常分为两大类:空间域方法和频域方法。 一、 空间域方法 空间域方法涉及直接对医学影像的像素值进行操作,以提高图像的视觉质量。以下是一些常用的空间域方法: 对比度调整:通过…

《逍遥游·六十八拐》

五月阳光映大观&#xff0c;艳丽队服身上穿。海埂西门集合后&#xff0c;蓝光城外相谈欢。 松茂水库映蓝天&#xff0c;阳宗镇上舞蹁跹。 六十八拐道崎岖&#xff0c;一鼓作气意志坚。 宜良宿&#xff0c;夜幕深&#xff0c;梦中山水情相牵。待破晓&#xff0c;新日升&#xf…

mongodb 分片集群认证

增加认证 副本间认证外部使用认证 如果是开启状态,先关闭路由,再关闭配置服务,最后关闭分片数据复本集中的每个mongod&#xff0c;从次节点开始。直到副本集的所 有成员都离线&#xff0c;包括任何仲裁者。主节点必须是最后一个成员关闭以避免潜在的回滚.最好通过 db.shutdow…

janus模块介绍-SIP Gateway

模块启动 默认的SIP GateWay也是https协议&#xff0c;端口为8088或者8089 如果需要在自己搭建的测试服务上测试SIP GateWay模块&#xff0c;则也需要修改为wss 具体改动如下: 找到/opt/janus/share/janus/demos/siptest.js var server "wss://" window.location…

比较好的平民衣服品牌有哪些?平价质量好短袖品牌推荐

随着气候变暖&#xff0c;夏天的持续时间似乎越来越长&#xff0c;短袖作为夏季的必备服装&#xff0c;受到了广大男士的青睐。然而&#xff0c;面对市场上众多的短袖品牌和不同的质量&#xff0c;大家都觉得选短袖的时候实在难以找到质量好且合适自己的。 选择合适的短袖确实…

第59篇:创建Nios II工程之控制LED<一>

Q&#xff1a;还记得第1篇吗&#xff1f;设计简单的逻辑电路&#xff0c;控制DE2-115开发板上LED的亮与熄灭&#xff0c;一行Verilog HDL的assign赋值语句即可实现。本期开始创建Nios II工程&#xff0c;用C语言代码控制DE2-115开发板上的LED实现流水灯效果。 A&#xff1a;在…

VPP 中注册的node是如何被调用起来的

当我们在VPP/plugins目录下注册了自己的node后&#xff0c; 肯定有一个node.func(), 那这个函数是如何执行到的呢&#xff1a; 1. 首先我们要看一下这个插件注册的时候做了什么&#xff0c; 假设node 如下&#xff1a; 编译成功后&#xff0c; 我们可以从函数vlib_plugin_earl…

回归与聚类——K-Means(六)

什么是无监督学习 一家广告平台需要根据相似的人口学特征和购买习惯将美国人口分成不同的小 组&#xff0c;以便广告客户可以通过有关联的广告接触到他们的目标客户。Airbnb 需要将自己的房屋清单分组成不同的社区&#xff0c;以便用户能更轻松地查阅这些清单。一个数据科学团队…

工作记录:vue-grid-layout 修改 margin 导致 item 高度剧烈变化

问题 用 vue-gird-layout 时发现&#xff0c;当改变 margin 值时&#xff0c;item 的尺寸也会跟着变化。 如下图&#xff1a;row height 和每个 item 的 h 都保持不变。修改 margin-y&#xff0c;item 的实际高度也跟着变了&#xff1a; 原因 研究了一番&#xff0c;发现原…