设为首页收藏本站
网站公告 | 这是第一条公告
     

 找回密码
 立即注册
缓存时间13 现在时间13 缓存数据 05|快乐缺点勇气 浪漫缺点诗意\你低头不说一句\你朝着灰色走去\你住进混沌深海\你开始无望等待|词曲/编混:陈粒

05|快乐缺点勇气 浪漫缺点诗意\你低头不说一句\你朝着灰色走去\你住进混沌深海\你开始无望等待|词曲/编混:陈粒 -- 光

查看: 1035|回复: 4

Android Bugreport实现原理深入分析

[复制链接]

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
37
主题
29
精华
0
金钱
105
积分
66
注册时间
2023-10-3
最后登录
2025-5-31

发表于 2024-11-26 20:06:35 | 显示全部楼层 |阅读模式
目录


  • Bugreport介绍
  • Bugreport使用方式
  • Bugrepot成果物的命名方式
  • bugreport的实现

Bugreport介绍

Android Bugreport是一个用于记录和收集 Android设备上系统信息、日志和调试信息的工具。
系统发生某些问题时,可以通过bugreport把系统当前时刻点(运行BugRepot的时刻)的系统相关的状态和信息都抓到一个zip中。
通过bugreport可以帮忙开发人员分析和解决问题。
Bugreport其实就是一系列的信息的汇总,包括日志、内存状态、进程信息、崩溃信息、服务状态等等。用一个大而近乎全的现场,来帮忙更好的分析问题。
并非所有问题,都需要用Bugreport抓取一份大而全的现场。可以根据经验考虑选用bugreport或者其他工具。
Bugreport收集的信息一般包括:

  • 设备软硬件信息
  • 系统日志
  • 系统运行状态,如cpu、内存、网络状态等。
  • 程序崩溃信息,如anr、墓碑等。

Bugreport使用方式

adb方式
  1. adb bugreport
复制代码
console方式
  1. bugreportz -p
复制代码
执行成功后,会在/data/user_de/0/com.android.shell/files/bugreports/下成一个生成一个 bugreport-*.zip的文件。

Bugrepot成果物的命名方式

文件命名形式为:
  1. bugreport-[device_name]-[build_id]-[localtime].zip
  2. device_name:属性ro.product.name,默认为UNKONW_DEVICE
  3. build_id:属性ro.build.id的值,默认为UNKOWN_BUILD
  4. localtime: 抓取bugreport时的本地时间
复制代码
例如:
  1. bugreport-arm-123.123-2024-02-28-19-18-14.zip
  2. device_name:arm
  3. build_id:123.123
  4. 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上。
1.jpeg

adb bugreport到adbd执行bugrepotz -p
adb bugreport执行(Host端),解析参数"bugreport"。
  1. // packages/modules/adb/client/main.cpp
  2. int main(int argc, char* argv[], char* envp[]) {
  3.     __adb_argv = const_cast<const char**>(argv);
  4.     __adb_envp = const_cast<const char**>(envp);
  5.     adb_trace_init(argv);
  6.     return adb_commandline(argc - 1, const_cast<const char**>(argv + 1));
  7. }
  8. // packages/modules/adb/client/commandline.cpp
  9. int adb_commandline(int argc, const char** argv) {
  10.         // 省略
  11.     /* adb_connect() commands */
  12.     if (!strcmp(argv[0], "devices")) {
  13.                 // 省略
  14.     } else if (!strcmp(argv[0], "bugreport")) {
  15.         Bugreport bugreport;
  16.         return bugreport.DoIt(argc, argv);
  17.     } else if (!strcmp(argv[0], "forward") || !strcmp(argv[0], "reverse")) {
  18.     // 省略
  19. }
复制代码
Bugreport类(Adb模块)DoIt函数向Adbd发送“bugreportz -v"和”bugreportz -p“命令。执行bugreportz -v,获取bugreportz的版本,一方面是执行版本差异的流程,另一方面也是作为Test,测试bugreportz是否可以执行。
  1. int Bugreport::DoIt(int argc, const char** argv) {
  2.     if (argc > 2) error_exit("usage: adb bugreport [[PATH] | [--stream]]");
  3.     // Gets bugreportz version.
  4.     std::string bugz_stdout, bugz_stderr;
  5.     DefaultStandardStreamsCallback version_callback(&bugz_stdout, &bugz_stderr);
  6.     int status = SendShellCommand("bugreportz -v", false, &version_callback);
  7.     std::string bugz_version = android::base::Trim(bugz_stderr);
  8.     std::string bugz_output = android::base::Trim(bugz_stdout);
  9.     int bugz_ver_major = 0, bugz_ver_minor = 0;
  10.     if (status != 0 || bugz_version.empty()) {
  11.         D("'bugreportz' -v results: status=%d, stdout='%s', stderr='%s'", status,
  12.           bugz_output.c_str(), bugz_version.c_str());
  13.         if (argc == 1) {
  14.             // Device does not support bugreportz: if called as 'adb bugreport', just falls out to
  15.             // the flat-file version.
  16.             fprintf(stderr,
  17.                     "Failed to get bugreportz version, which is only available on devices "
  18.                     "running Android 7.0 or later.\nTrying a plain-text bug report instead.\n");
  19.             return SendShellCommand("bugreport", false);
  20.         }
  21.         // But if user explicitly asked for a zipped bug report, fails instead (otherwise calling
  22.         // 'bugreport' would generate a lot of output the user might not be prepared to handle).
  23.         fprintf(stderr,
  24.                 "Failed to get bugreportz version: 'bugreportz -v' returned '%s' (code %d).\n"
  25.                 "If the device does not run Android 7.0 or above, try this instead:\n"
  26.                 "\tadb bugreport > bugreport.txt\n",
  27.                 bugz_output.c_str(), status);
  28.         return status != 0 ? status : -1;
  29.     }
  30.     std::sscanf(bugz_version.c_str(), "%d.%d", &bugz_ver_major, &bugz_ver_minor);
  31.     std::string dest_file, dest_dir;
  32.     if (argc == 1) {
  33.         // No args - use current directory
  34.         if (!getcwd(&dest_dir)) {
  35.             perror("adb: getcwd failed");
  36.             return 1;
  37.         }
  38.     } else if (!strcmp(argv[1], "--stream")) {
  39.         if (bugz_ver_major == 1 && bugz_ver_minor < 2) {
  40.             fprintf(stderr,
  41.                     "Failed to stream bugreport: bugreportz does not support stream.\n");
  42.         } else {
  43.             return SendShellCommand("bugreportz -s", false);
  44.         }
  45.     } else {
  46.         // Check whether argument is a directory or file
  47.         if (directory_exists(argv[1])) {
  48.             dest_dir = argv[1];
  49.         } else {
  50.             dest_file = argv[1];
  51.         }
  52.     }
  53.     if (dest_file.empty()) {
  54.         // Uses a default value until device provides the proper name
  55.         dest_file = "bugreport.zip";
  56.     } else {
  57.         if (!android::base::EndsWithIgnoreCase(dest_file, ".zip")) {
  58.             dest_file += ".zip";
  59.         }
  60.     }
  61.     bool show_progress = true;
  62.     std::string bugz_command = "bugreportz -p";
  63.     if (bugz_version == "1.0") {
  64.         // 1.0 does not support progress notifications, so print a disclaimer
  65.         // message instead.
  66.         fprintf(stderr,
  67.                 "Bugreport is in progress and it could take minutes to complete.\n"
  68.                 "Please be patient and do not cancel or disconnect your device "
  69.                 "until it completes.\n");
  70.         show_progress = false;
  71.         bugz_command = "bugreportz";
  72.     }
  73.     BugreportStandardStreamsCallback bugz_callback(dest_dir, dest_file, show_progress, this);
  74.     return SendShellCommand(bugz_command, false, &bugz_callback);
  75. }
复制代码
adbd执行bugrepotz -p
此部分省略。就是adbd执行下面两个命令:shell bugreportz -v 和 bugreportz -p
不是主要关注点,想了了解的自行阅读源码即可。
bugreportz -p执行并调用dumpstate -S
通过bugreportz -p,收集系统当前时刻点的各种信息(信息参考上面的内容)。bugreportz -v比较简单,仅为输出一下bugreportz的版本。
  1. // frameworks/native/cmds/bugreportz/main.cpp
  2. static constexpr char VERSION[] = "1.2";
  3. static void show_usage() {
  4.     fprintf(stderr,
  5.             "usage: bugreportz [-hpsv]\n"
  6.             "  -h: to display this help message\n"
  7.             "  -p: display progress\n"
  8.             "  -s: stream content to standard output\n"
  9.             "  -v: to display the version\n"
  10.             "  or no arguments to generate a zipped bugreport\n");
  11. }
  12. static void show_version() {
  13.     fprintf(stderr, "%s\n", VERSION);
  14. }
  15. int main(int argc, char* argv[]) {
  16.     bool show_progress = false;
  17.     bool stream_data = false;
  18.     if (argc > 1) {
  19.         /* parse arguments */
  20.         int c;
  21.         while ((c = getopt(argc, argv, "hpsv")) != -1) {
  22.             switch (c) {
  23.                 case 'h':
  24.                     show_usage();
  25.                     return EXIT_SUCCESS;
  26.                 case 'p':
  27.                     show_progress = true;
  28.                     break;
  29.                 case 's':
  30.                     stream_data = true;
  31.                     break;
  32.                 case 'v':
  33.                     show_version();
  34.                     return EXIT_SUCCESS;
  35.                 default:
  36.                     show_usage();
  37.                     return EXIT_FAILURE;
  38.             }
  39.         }
  40.     }
  41.     // We don't support any non-option arguments.
  42.     if (optind != argc) {
  43.         show_usage();
  44.         return EXIT_FAILURE;
  45.     }
  46.     // TODO: code below was copy-and-pasted from bugreport.cpp (except by the
  47.     // timeout value);
  48.     // should be reused instead.
  49.     // Start the dumpstatez service.
  50.     if (stream_data) {
  51.         property_set("ctl.start", "dumpstate");
  52.     } else {
  53.         // 调用dumpstatez
  54.         property_set("ctl.start", "dumpstatez");
  55.     }
  56.     // Socket will not be available until service starts.
  57.     int s = -1;
  58.     for (int i = 0; i < 20; i++) {
  59.         // 接连dumpstatez的socket(接收状态信息)
  60.         s = socket_local_client("dumpstate", ANDROID_SOCKET_NAMESPACE_RESERVED, SOCK_STREAM);
  61.         if (s >= 0) break;
  62.         // Try again in 1 second.
  63.         sleep(1);
  64.     }
  65.     if (s == -1) {
  66.         printf("FAIL:Failed to connect to dumpstatez service: %s\n", strerror(errno));
  67.         return EXIT_FAILURE;
  68.     }
  69.     // Set a timeout so that if nothing is read in 10 minutes, we'll stop
  70.     // reading and quit. No timeout in dumpstate is longer than 60 seconds,
  71.     // so this gives lots of leeway in case of unforeseen time outs.
  72.     struct timeval tv;
  73.     tv.tv_sec = 10 * 60;
  74.     tv.tv_usec = 0;
  75.     if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) == -1) {
  76.         fprintf(stderr,
  77.                 "WARNING: Cannot set socket timeout, bugreportz might hang indefinitely: %s\n",
  78.                 strerror(errno));
  79.     }
  80.     int ret;
  81.     if (stream_data) {
  82.         ret = bugreportz_stream(s);
  83.     } else {
  84.         // 走这里,show_progress为True
  85.         ret = bugreportz(s, show_progress);
  86.     }
  87.     if (close(s) == -1) {
  88.         fprintf(stderr, "WARNING: error closing socket: %s\n", strerror(errno));
  89.         ret = EXIT_FAILURE;
  90.     }
  91.     return ret;
  92. }
复制代码
bugreportz函数中,接收dumpstatez(通过socket)返回的状态信息,并将其写到标准输出中。adb会通过标准输出,了解到命令执行的状态。为啥dumpstatez不将状态信息直接写到标准输出中?因为dumpstatez将标准输出重定向到文件了。
  1. static constexpr char BEGIN_PREFIX[] = "BEGIN:";
  2. static constexpr char PROGRESS_PREFIX[] = "PROGRESS:";
  3. static void write_line(const std::string& line, bool show_progress) {    if (line.empty()) return;
  4.     // 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) ||
  5.                            android::base::StartsWith(line, BEGIN_PREFIX)))
  6.         return;
  7.     android::base::WriteStringToFd(line, STDOUT_FILENO);
  8. }
  9. int bugreportz(int s, bool show_progress) {    std::string line;
  10.     while (1) {
  11.         char buffer[65536];
  12.         ssize_t bytes_read = TEMP_FAILURE_RETRY(read(s, buffer, sizeof(buffer)));
  13.         if (bytes_read == 0) {
  14.             break;
  15.         } else if (bytes_read == -1) {
  16.             // EAGAIN really means time out, so change the errno.if (errno == EAGAIN) {
  17.                 errno = ETIMEDOUT;
  18.             }
  19.             printf("FAIL:Bugreport read terminated abnormally (%s)\n", strerror(errno));
  20.             return EXIT_FAILURE;
  21.         }
  22.         // Writes line by line.for (int i = 0; i < bytes_read; i++) {
  23.             char c = buffer[i];
  24.             line.append(1, c);
  25.             if (c == '\n') {
  26.                 write_line(line, show_progress);
  27.                 line.clear();
  28.             }
  29.         }
  30.     }
  31.     // Process final line, in case it didn't finish with newlinewrite_line(line, show_progress);
  32.     return EXIT_SUCCESS;
  33. }
复制代码
上面代码中通过 “ctl.start”, "dumpstatez"执行了dumpstatez。查看dumpstatez对应的rc文件。其对应/system/bin/dumpstate -S(注意为大写S)
  1. service dumpstatez /system/bin/dumpstate -S    socket dumpstate stream 0660 shell log    class main    disabled    oneshot
复制代码
dumpstate -S生成Bugreport对应的zip文件
dumpstate -s命令执行
  1. // frameworks/native/cmds/dumpstate/main.cpp
  2. int main(int argc, char* argv[]) {
  3.     if (ShouldStartServiceAndWait(argc, argv)) {
  4.         int ret;
  5.         if ((ret = android::os::DumpstateService::Start()) != android::OK) {
  6.             MYLOGE("Unable to start 'dumpstate' service: %d", ret);
  7.             exit(1);
  8.         }
  9.         MYLOGI("'dumpstate' service started and will wait for a call to startBugreport()");
  10.         // Waits forever for an incoming connection.
  11.         // TODO(b/111441001): should this time out?
  12.         android::IPCThreadState::self()->joinThreadPool();
  13.         return 0;
  14.     } else {
  15.         return run_main(argc, argv);
  16.     }
  17. }
  18. // frameworks/native/cmds/dumpstate/dumpstate.cpp
  19. /* Main entry point for dumpstate binary. */
  20. int run_main(int argc, char* argv[]) {
  21.     Dumpstate::RunStatus status = ds.ParseCommandlineAndRun(argc, argv);
  22.     switch (status) {
  23.         case Dumpstate::RunStatus::OK:
  24.             exit(0);
  25.         case Dumpstate::RunStatus::HELP:
  26.             ShowUsage();
  27.             exit(0);
  28.         case Dumpstate::RunStatus::INVALID_INPUT:
  29.             fprintf(stderr, "Invalid combination of args\n");
  30.             ShowUsage();
  31.             exit(1);
  32.         case Dumpstate::RunStatus::ERROR:
  33.             FALLTHROUGH_INTENDED;
  34.         case Dumpstate::RunStatus::USER_CONSENT_DENIED:
  35.             FALLTHROUGH_INTENDED;
  36.         case Dumpstate::RunStatus::USER_CONSENT_TIMED_OUT:
  37.             exit(2);
  38.     }
  39. }
  40. Dumpstate::RunStatus Dumpstate::ParseCommandlineAndRun(int argc, char* argv[]) {
  41.     std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
  42.     Dumpstate::RunStatus status = options->Initialize(argc, argv);
  43.     if (status == Dumpstate::RunStatus::OK) {
  44.         SetOptions(std::move(options));
  45.         // When directly running dumpstate binary, the output is not expected to be written
  46.         // to any external file descriptor.
  47.         assert(options_->bugreport_fd.get() == -1);
  48.         // calling_uid and calling_package are for user consent to share the bugreport with
  49.         // an app; they are irrelevant here because bugreport is triggered via command line.
  50.         // Update Last ID before calling Run().
  51.         Initialize();
  52.         status = Run(-1 /* calling_uid */, "" /* calling_package */);
  53.     }
  54.     return status;
  55. }
复制代码
创建Dumpstate::DumpOptions对象,调用Initialize函数,解析输入参数“-S”。S(大写)会将参数的progress_updates_to_socket设置为ture,这个flag标志着dumpstate将状态告知给调用者(通过socket)
  1. void Dumpstate::DumpOptions::Initialize(BugreportMode bugreport_mode,
  2.                                         const android::base::unique_fd& bugreport_fd_in,
  3.                                         const android::base::unique_fd& screenshot_fd_in,
  4.                                         bool is_screenshot_requested) {
  5.     // Duplicate the fds because the passed in fds don't outlive the binder transaction.
  6.     bugreport_fd.reset(dup(bugreport_fd_in.get()));
  7.     screenshot_fd.reset(dup(screenshot_fd_in.get()));
  8.     SetOptionsFromMode(bugreport_mode, this, is_screenshot_requested);
  9. }
  10. Dumpstate::RunStatus Dumpstate::DumpOptions::Initialize(int argc, char* argv[]) {
  11.     RunStatus status = RunStatus::OK;
  12.     int c;
  13.     while ((c = getopt(argc, argv, "dho:svqzpLPBRSV:w")) != -1) {
  14.         switch (c) {
  15.             // clang-format off
  16.             case 'o': out_dir = optarg;              break;
  17.             case 's': stream_to_socket = true;       break;
  18.             case 'S': progress_updates_to_socket = true;    break;
  19.             case 'v': show_header_only = true;       break;
  20.             case 'q': do_vibrate = false;            break;
  21.             case 'p': do_screenshot = true;          break;
  22.             case 'P': do_progress_updates = true;    break;
  23.             case 'R': is_remote_mode = true;         break;
  24.             case 'L': limited_only = true;           break;
  25.             case 'V':
  26.             case 'd':
  27.             case 'z':
  28.                 // compatibility no-op
  29.                 break;
  30.             case 'w':
  31.                 // This was already processed
  32.                 break;
  33.             case 'h':
  34.                 status = RunStatus::HELP;
  35.                 break;
  36.             default:
  37.                 fprintf(stderr, "Invalid option: %c\n", c);
  38.                 status = RunStatus::INVALID_INPUT;
  39.                 break;
  40.                 // clang-format on
  41.         }
  42.     }
  43.     for (int i = 0; i < argc; i++) {
  44.         args += argv[i];
  45.         if (i < argc - 1) {
  46.             args += " ";
  47.         }
  48.     }
  49.     // Reset next index used by getopt so this can be called multiple times, for eg, in tests.
  50.     optind = 1;
  51.     return status;
  52. }
复制代码
然后调用Dumpstate::Initialize 和Dumpstate::Run,开始收集bugreport的内容。
  1. void Dumpstate::Initialize() {
  2.     /* gets the sequential id */
  3.     uint32_t last_id = android::base::GetIntProperty(PROPERTY_LAST_ID, 0);
  4.     id_ = ++last_id;
  5.     android::base::SetProperty(PROPERTY_LAST_ID, std::to_string(last_id));
  6. }
  7. Dumpstate::RunStatus Dumpstate::Run(int32_t calling_uid, const std::string& calling_package) {
  8.     Dumpstate::RunStatus status = RunInternal(calling_uid, calling_package);
  9.     if (listener_ != nullptr) {
  10.         switch (status) {
  11.             case Dumpstate::RunStatus::OK:
  12.                 listener_->onFinished();
  13.                 break;
  14.             case Dumpstate::RunStatus::HELP:
  15.                 break;
  16.             case Dumpstate::RunStatus::INVALID_INPUT:
  17.                 listener_->onError(IDumpstateListener::BUGREPORT_ERROR_INVALID_INPUT);
  18.                 break;
  19.             case Dumpstate::RunStatus::ERROR:
  20.                 listener_->onError(IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
  21.                 break;
  22.             case Dumpstate::RunStatus::USER_CONSENT_DENIED:
  23.                 listener_->onError(IDumpstateListener::BUGREPORT_ERROR_USER_DENIED_CONSENT);
  24.                 break;
  25.             case Dumpstate::RunStatus::USER_CONSENT_TIMED_OUT:
  26.                 listener_->onError(IDumpstateListener::BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT);
  27.                 break;
  28.         }
  29.     }
  30.     return status;
  31. }
复制代码
Dumpstate::Run函数中调用RunInternal实现Bugreport的收集。该函数内容比较多,只关注三个主要流程:主要文件bugreport-*.txt的生成,log、anr等文件copy到zip文件中、zip文件的生成。
  1. Dumpstate::RunStatus Dumpstate::RunInternal(int32_t calling_uid,
  2.                                             const std::string& calling_package) {
  3.     DurationReporter duration_reporter("RUN INTERNAL", /* logcat_only = */true);
  4.     LogDumpOptions(*options_);
  5.     if (!options_->ValidateOptions()) {
  6.         MYLOGE("Invalid options specified\n");
  7.         return RunStatus::INVALID_INPUT;
  8.     }
  9.     /* set as high priority, and protect from OOM killer */
  10.     setpriority(PRIO_PROCESS, 0, -20);
  11.     FILE* oom_adj = fopen("/proc/self/oom_score_adj", "we");
  12.     if (oom_adj) {
  13.         fputs("-1000", oom_adj);
  14.         fclose(oom_adj);
  15.     } else {
  16.         /* fallback to kernels <= 2.6.35 */
  17.         oom_adj = fopen("/proc/self/oom_adj", "we");
  18.         if (oom_adj) {
  19.             fputs("-17", oom_adj);
  20.             fclose(oom_adj);
  21.         }
  22.     }
  23.     MYLOGI("dumpstate info: id=%d, args='%s', bugreport_mode= %s bugreport format version: %s\n",
  24.            id_, options_->args.c_str(), options_->bugreport_mode.c_str(), version_.c_str());
  25.     // If we are going to use a socket, do it as early as possible
  26.     // to avoid timeouts from bugreport.
  27.     if (options_->stream_to_socket || options_->progress_updates_to_socket) {
  28.         MYLOGD("Opening control socket\n");
  29.         control_socket_fd_ = open_socket_fn_("dumpstate");
  30.         if (control_socket_fd_ == -1) {
  31.             return ERROR;
  32.         }
  33.         if (options_->progress_updates_to_socket) {
  34.             options_->do_progress_updates = 1;
  35.         }
  36.     }
  37.     // 准备文件
  38.     if (!PrepareToWriteToFile()) {
  39.         return ERROR;
  40.     }
  41.     // 将标准输出,重定向到临时文件Bugreport-*.tmp文件中
  42.     // 通过Bugreport-*.tmp文件,产生最终的Bugreport-*.zip
  43.     // Redirect stdout to tmp_path_. This is the main bugreport entry and will be
  44.     // moved into zip file later, if zipping.
  45.     TEMP_FAILURE_RETRY(dup_stdout_fd = dup(fileno(stdout)));
  46.     // TODO: why not write to a file instead of stdout to overcome this problem?
  47.     /* TODO: rather than generating a text file now and zipping it later,
  48.         it would be more efficient to redirect stdout to the zip entry
  49.         directly, but the libziparchive doesn't support that option yet. */
  50.     if (!redirect_to_file(stdout, const_cast<char*>(tmp_path_.c_str()))) {
  51.         return ERROR;
  52.     }
  53.     if (chown(tmp_path_.c_str(), AID_SHELL, AID_SHELL)) {
  54.         MYLOGE("Unable to change ownership of temporary bugreport file %s: %s\n",
  55.                 tmp_path_.c_str(), strerror(errno));
  56.     }
  57.     // 输出头部分信息(就是Bugreport-*.txt最开头的一些版本信息)
  58.     // NOTE: there should be no stdout output until now, otherwise it would break the header.
  59.     // In particular, DurationReport objects should be created passing 'title, NULL', so their
  60.     // duration is logged into MYLOG instead.
  61.     PrintHeader();
  62.     bool is_dumpstate_restricted = options_->telephony_only
  63.                                    || options_->wifi_only
  64.                                    || options_->limited_only;
  65.     if (!is_dumpstate_restricted) {
  66.         // Dump系统关键服务的状态
  67.         // ------ DUMPSYS CRITICAL (/system/bin/dumpsys) ------的信息
  68.         // Invoke critical dumpsys first to preserve system state, before doing anything else.
  69.         RunDumpsysCritical();
  70.     }
  71.     if (options_->telephony_only) {
  72.         DumpstateTelephonyOnly(calling_package);
  73.     } else if (options_->wifi_only) {
  74.         DumpstateWifiOnly();
  75.     } else if (options_->limited_only) {
  76.         DumpstateLimitedOnly();
  77.     } else {
  78.         // Dump state for the default case. This also drops root.
  79.         // dump额外信息
  80.         RunStatus s = DumpstateDefaultAfterCritical();
  81.         if (s != RunStatus::OK) {
  82.             if (s == RunStatus::USER_CONSENT_DENIED) {
  83.                 HandleUserConsentDenied();
  84.             }
  85.             return s;
  86.         }
  87.     }
  88.     // 解除重定向
  89.     /* close output if needed */
  90.     TEMP_FAILURE_RETRY(dup2(dup_stdout_fd, fileno(stdout)));
  91.     // 完成zip的打包,删除临时文件
  92.     // Zip the (now complete) .tmp file within the internal directory.
  93.     FinalizeFile();
  94.         // 省略
  95. }
复制代码
PrepareToWriteToFile函数中,确定bugreport信息写入的文件名。文件名命名方式为bugreport-[device_name]-[build_id]-[localtime]。文件信息会输出到Log中。
  1. 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: []
复制代码
  1. /*
  2. * Prepares state like filename, screenshot path, etc in Dumpstate. Also initializes ZipWriter
  3. * and adds the version file. Return false if zip_file could not be open to write.
  4. */
  5. static bool PrepareToWriteToFile() {
  6.     MaybeResolveSymlink(&ds.bugreport_internal_dir_);
  7.     std::string build_id = android::base::GetProperty("ro.build.id", "UNKNOWN_BUILD");
  8.     std::string device_name = android::base::GetProperty("ro.product.name", "UNKNOWN_DEVICE");
  9.     ds.base_name_ = StringPrintf("bugreport-%s-%s", device_name.c_str(), build_id.c_str());
  10.     char date[80];
  11.     strftime(date, sizeof(date), "%Y-%m-%d-%H-%M-%S", localtime(&ds.now_));
  12.     ds.name_ = date;
  13.     if (ds.options_->telephony_only) {
  14.         ds.base_name_ += "-telephony";
  15.     } else if (ds.options_->wifi_only) {
  16.         ds.base_name_ += "-wifi";
  17.     }
  18.     if (ds.options_->do_screenshot) {
  19.         ds.screenshot_path_ = ds.GetPath(ds.CalledByApi() ? "-png.tmp" : ".png");
  20.     }
  21.     ds.tmp_path_ = ds.GetPath(".tmp");
  22.     ds.log_path_ = ds.GetPath("-dumpstate_log-" + std::to_string(ds.pid_) + ".txt");
  23.     std::string destination = ds.CalledByApi()
  24.                                   ? StringPrintf("[fd:%d]", ds.options_->bugreport_fd.get())
  25.                                   : ds.bugreport_internal_dir_.c_str();
  26.     MYLOGD(
  27.         "Bugreport dir: [%s] "
  28.         "Base name: [%s] "
  29.         "Suffix: [%s] "
  30.         "Log path: [%s] "
  31.         "Temporary path: [%s] "
  32.         "Screenshot path: [%s]\n",
  33.         destination.c_str(), ds.base_name_.c_str(), ds.name_.c_str(), ds.log_path_.c_str(),
  34.         ds.tmp_path_.c_str(), ds.screenshot_path_.c_str());
  35.     ds.path_ = ds.GetPath(ds.CalledByApi() ? "-zip.tmp" : ".zip");
  36.     MYLOGD("Creating initial .zip file (%s)\n", ds.path_.c_str());
  37.     create_parent_dirs(ds.path_.c_str());
  38.     ds.zip_file.reset(fopen(ds.path_.c_str(), "wb"));
  39.     if (ds.zip_file == nullptr) {
  40.         MYLOGE("fopen(%s, 'wb'): %s\n", ds.path_.c_str(), strerror(errno));
  41.         return false;
  42.     }
  43.     ds.zip_writer_.reset(new ZipWriter(ds.zip_file.get()));
  44.     ds.AddTextZipEntry("version.txt", ds.version_);
  45.     return true;
  46. }
复制代码
在PrintHeader函数中,将bugreport-.txt中的头部信息输入到标准输出中,而标准输出已经重定向到了bugreport-.tmp文件中。
  1. void Dumpstate::PrintHeader() const {
  2.     std::string build, fingerprint, radio, bootloader, network;
  3.     char date[80];
  4.     build = android::base::GetProperty("ro.build.display.id", "(unknown)");
  5.     fingerprint = android::base::GetProperty("ro.build.fingerprint", "(unknown)");
  6.     radio = android::base::GetProperty("gsm.version.baseband", "(unknown)");
  7.     bootloader = android::base::GetProperty("ro.bootloader", "(unknown)");
  8.     network = android::base::GetProperty("gsm.operator.alpha", "(unknown)");
  9.     strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now_));
  10.     printf("========================================================\n");
  11.     printf("== dumpstate: %s\n", date);
  12.     printf("========================================================\n");
  13.     printf("\n");
  14.     printf("Build: %s\n", build.c_str());
  15.     // NOTE: fingerprint entry format is important for other tools.
  16.     printf("Build fingerprint: '%s'\n", fingerprint.c_str());
  17.     printf("Bootloader: %s\n", bootloader.c_str());
  18.     printf("Radio: %s\n", radio.c_str());
  19.     printf("Network: %s\n", network.c_str());
  20.     int64_t module_metadata_version = android::os::GetModuleMetadataVersion();
  21.     if (module_metadata_version != 0) {
  22.         printf("Module Metadata version: %" PRId64 "\n", module_metadata_version);
  23.     }
  24.     printf("SDK extension versions [r=%s s=%s]\n",
  25.            android::base::GetProperty("build.version.extensions.r", "-").c_str(),
  26.            android::base::GetProperty("build.version.extensions.s", "-").c_str());
  27.     printf("Kernel: ");
  28.     DumpFileToFd(STDOUT_FILENO, "", "/proc/version");
  29.     printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
  30.     printf("Uptime: ");
  31.     RunCommandToFd(STDOUT_FILENO, "", {"uptime", "-p"},
  32.                    CommandOptions::WithTimeout(1).Always().Build());
  33.     printf("Bugreport format version: %s\n", version_.c_str());
  34.     printf("Dumpstate info: id=%d pid=%d dry_run=%d parallel_run=%d args=%s bugreport_mode=%s\n",
  35.            id_, pid_, PropertiesHelper::IsDryRun(), PropertiesHelper::IsParallelRun(),
  36.            options_->args.c_str(), options_->bugreport_mode.c_str());
  37.     printf("\n");
  38. }
复制代码
然后在RunDumpsysCritical函数中,通过ServiceManager获取当前系统的Service,并调用Service的dump,将Service的dump信息输出到bugreport-*.tmp文件中。另外,会通过proto的形式再输出一份service的dump信息。
  1. // frameworks/native/cmds/dumpstate/dumpstate.cpp
  2. static void RunDumpsysText(const std::string& title, int priority,                           std::chrono::milliseconds timeout,
  3.                            std::chrono::milliseconds service_timeout) {
  4.     DurationReporter duration_reporter(title);
  5.     dprintf(STDOUT_FILENO, "------ %s (/system/bin/dumpsys) ------\n", title.c_str());
  6.     fsync(STDOUT_FILENO);
  7.     RunDumpsysTextByPriority(title, priority, timeout, service_timeout);
  8. }
  9. static void RunDumpsysText(const std::string& title, int priority,                           std::chrono::milliseconds timeout,
  10.                            std::chrono::milliseconds service_timeout) {
  11.     DurationReporter duration_reporter(title);
  12.     dprintf(STDOUT_FILENO, "------ %s (/system/bin/dumpsys) ------\n", title.c_str());
  13.     fsync(STDOUT_FILENO);
  14.     RunDumpsysTextByPriority(title, priority, timeout, service_timeout);
  15. }
  16. static Dumpstate::RunStatus RunDumpsysTextByPriority(const std::string& title, int priority,
  17.                                                      std::chrono::milliseconds timeout,
  18.                                                      std::chrono::milliseconds service_timeout) {
  19.     auto start = std::chrono::steady_clock::now();
  20.     sp<android::IServiceManager> sm = defaultServiceManager();
  21.     Dumpsys dumpsys(sm.get());
  22.     Vector<String16> args;
  23.     Dumpsys::setServiceArgs(args, /* asProto = */ false, priority);
  24.     Vector<String16> services = dumpsys.listServices(priority, /* supports_proto = */ false);
  25.     for (const String16& service : services) {
  26.         RETURN_IF_USER_DENIED_CONSENT();
  27.         std::string path(title);
  28.         path.append(" - ").append(String8(service).c_str());
  29.         size_t bytes_written = 0;
  30.         // 在dumpthread中,调用service的dump
  31.         status_t status = dumpsys.startDumpThread(Dumpsys::Type::DUMP, service, args);
  32.         if (status == OK) {
  33.             dumpsys.writeDumpHeader(STDOUT_FILENO, service, priority);
  34.             std::chrono::duration<double> elapsed_seconds;
  35.             if (priority == IServiceManager::DUMP_FLAG_PRIORITY_HIGH &&
  36.                 service == String16("meminfo")) {
  37.                 // Use a longer timeout for meminfo, since 30s is not always enough.
  38.                 status = dumpsys.writeDump(STDOUT_FILENO, service, 60s,
  39.                                            /* as_proto = */ false, elapsed_seconds, bytes_written);
  40.             } else {
  41.                 status = dumpsys.writeDump(STDOUT_FILENO, service, service_timeout,
  42.                                            /* as_proto = */ false, elapsed_seconds, bytes_written);
  43.             }
  44.             dumpsys.writeDumpFooter(STDOUT_FILENO, service, elapsed_seconds);
  45.             bool dump_complete = (status == OK);
  46.             dumpsys.stopDumpThread(dump_complete);
  47.         }
  48.         auto elapsed_duration = std::chrono::duration_cast<std::chrono::milliseconds>(
  49.             std::chrono::steady_clock::now() - start);
  50.         if (elapsed_duration > timeout) {
  51.             MYLOGE("*** command '%s' timed out after %llums\n", title.c_str(),
  52.                    elapsed_duration.count());
  53.             break;
  54.         }
  55.     }
  56.     return Dumpstate::RunStatus::OK;
  57. }
复制代码
在DumpstateDefaultAfterCritical函数中,dump额外信息,以及将log、anr等等文件拷贝到zip文件中。
  1. // frameworks/native/cmds/dumpstate/dumpstate.cpp
  2. Dumpstate::RunStatus Dumpstate::DumpstateDefaultAfterCritical() {
  3.     // Capture first logcat early on; useful to take a snapshot before dumpstate logs take over the
  4.     // buffer.
  5.     DoLogcat();
  6.     // Capture timestamp after first logcat to use in next logcat
  7.     time_t logcat_ts = time(nullptr);
  8.     /* collect stack traces from Dalvik and native processes (needs root) */
  9.     if (dump_pool_) {
  10.         RETURN_IF_USER_DENIED_CONSENT();
  11.         // One thread is enough since we only need to enqueue DumpTraces here.
  12.         dump_pool_->start(/* thread_counts = */1);
  13.         // DumpTraces takes long time, post it to the another thread in the
  14.         // pool, if pool is available
  15.         dump_pool_->enqueueTask(DUMP_TRACES_TASK, &Dumpstate::DumpTraces, &ds, &dump_traces_path);
  16.     } else {
  17.         RUN_SLOW_FUNCTION_WITH_CONSENT_CHECK_AND_LOG(DUMP_TRACES_TASK, ds.DumpTraces,
  18.                 &dump_traces_path);
  19.     }
  20.     /* Run some operations that require root. */
  21.     ds.tombstone_data_ = GetDumpFds(TOMBSTONE_DIR, TOMBSTONE_FILE_PREFIX, !ds.IsZipping());
  22.     ds.anr_data_ = GetDumpFds(ANR_DIR, ANR_FILE_PREFIX, !ds.IsZipping());
  23.     ds.AddDir(RECOVERY_DIR, true);
  24.     ds.AddDir(RECOVERY_DATA_DIR, true);
  25.     ds.AddDir(UPDATE_ENGINE_LOG_DIR, true);
  26.     ds.AddDir(LOGPERSIST_DATA_DIR, false);
  27.     if (!PropertiesHelper::IsUserBuild()) {
  28.         ds.AddDir(PROFILE_DATA_DIR_CUR, true);
  29.         ds.AddDir(PROFILE_DATA_DIR_REF, true);
  30.         ds.AddZipEntry(ZIP_ROOT_DIR + PACKAGE_DEX_USE_LIST, PACKAGE_DEX_USE_LIST);
  31.     }
  32.     ds.AddDir(PREREBOOT_DATA_DIR, false);
  33.     add_mountinfo();
  34.     DumpIpTablesAsRoot();
  35.     DumpDynamicPartitionInfo();
  36.     ds.AddDir(OTA_METADATA_DIR, true);
  37.     // Capture any IPSec policies in play. No keys are exposed here.
  38.     RunCommand("IP XFRM POLICY", {"ip", "xfrm", "policy"}, CommandOptions::WithTimeout(10).Build());
  39.     // Dump IPsec stats. No keys are exposed here.
  40.     DumpFile("XFRM STATS", XFRM_STAT_PROC_FILE);
  41.     // Run ss as root so we can see socket marks.
  42.     RunCommand("DETAILED SOCKET STATE", {"ss", "-eionptu"}, CommandOptions::WithTimeout(10).Build());
  43.     // Run iotop as root to show top 100 IO threads
  44.     RunCommand("IOTOP", {"iotop", "-n", "1", "-m", "100"});
  45.     // Gather shared memory buffer info if the product implements it
  46.     RunCommand("Dmabuf dump", {"dmabuf_dump"});
  47.     RunCommand("Dmabuf per-buffer/per-exporter/per-device stats", {"dmabuf_dump", "-b"});
  48.     DumpFile("PSI cpu", "/proc/pressure/cpu");
  49.     DumpFile("PSI memory", "/proc/pressure/memory");
  50.     DumpFile("PSI io", "/proc/pressure/io");
  51.     if (dump_pool_) {
  52.         RETURN_IF_USER_DENIED_CONSENT();
  53.         dump_pool_->waitForTask(DUMP_TRACES_TASK);
  54.         // Current running thread in the pool is the root user also. Shutdown
  55.         // the pool and restart later to ensure all threads in the pool could
  56.         // drop the root user.
  57.         dump_pool_->shutdown();
  58.     }
  59.     if (!DropRootUser()) {
  60.         return Dumpstate::RunStatus::ERROR;
  61.     }
  62.     RETURN_IF_USER_DENIED_CONSENT();
  63.     Dumpstate::RunStatus status = dumpstate();
  64.     // Capture logcat since the last time we did it.
  65.     DoSystemLogcat(logcat_ts);
  66.     return status;
  67. }
复制代码
最后在FinalizeFile函数中,将临时文件Bugreport-.tmp,copy到zip中,并命名为Bugreport-.zip。然后删除临时文件,完成zip文件的落盘。
  1. /*
  2. * Finalizes writing to the file by zipping the tmp file to the final location,
  3. * printing zipped file status, etc.
  4. */static void FinalizeFile() {    bool do_text_file = !ds.FinishZipFile();
  5.     if (do_text_file) {
  6.         MYLOGE("Failed to finish zip file; sending text bugreport instead\n");
  7.     }
  8.     std::string final_path = ds.path_;
  9.     if (ds.options_->OutputToCustomFile()) {
  10.         final_path = ds.GetPath(ds.options_->out_dir, ".zip");
  11.         android::os::CopyFileToFile(ds.path_, final_path);
  12.     }
  13.     if (ds.options_->stream_to_socket) {
  14.         android::os::CopyFileToFd(ds.path_, ds.control_socket_fd_);
  15.     } else if (ds.options_->progress_updates_to_socket) {
  16.         if (do_text_file) {
  17.             dprintf(ds.control_socket_fd_,
  18.                     "FAIL:could not create zip file, check %s ""for more details\n",
  19.                     ds.log_path_.c_str());
  20.         } else {
  21.             dprintf(ds.control_socket_fd_, "OK:%s\n", final_path.c_str());
  22.         }
  23.     }
  24. }
复制代码
adb将Bugrepo-*.zip pull到本地
当bugreport文件收集好后,会触发adb将相关文件pull到本地(Host),路径为执行adb命令的路径。
  1. // packages/modules/adb/client/bugreport.cpp
  2. // Custom callback used to handle the output of zipped bugreports.
  3. class BugreportStandardStreamsCallback : public StandardStreamsCallbackInterface {
  4.   public:
  5.     BugreportStandardStreamsCallback(const std::string& dest_dir, const std::string& dest_file,
  6.                                      bool show_progress, Bugreport* br)
  7.         : br_(br),
  8.           src_file_(),
  9.           dest_dir_(dest_dir),
  10.           dest_file_(dest_file),
  11.           line_message_(),
  12.           invalid_lines_(),
  13.           show_progress_(show_progress),
  14.           status_(0),
  15.           line_(),
  16.           last_progress_percentage_(0) {
  17.         SetLineMessage("generating");
  18.     }
  19.     int Done(int unused_) {
  20.         // Pull the generated bug report.
  21.         if (status_ == 0) {
  22.                         // 将Bugreport-*.zip文件 pull到本地(host)
  23.             status_ =
  24.                 br_->DoSyncPull(srcs, destination.c_str(), false, line_message_.c_str()) ? 0 : 1;
  25.             if (status_ == 0) {
  26.                 printf("Bug report copied to %s\n", destination.c_str());
  27.             } else {
  28.                 fprintf(stderr,
  29.                         "Bug report finished but could not be copied to '%s'.\n"
  30.                         "Try to run 'adb pull %s <directory>'\n"
  31.                         "to copy it to a directory that can be written.\n",
  32.                         destination.c_str(), src_file_.c_str());
  33.             }
  34.         }
  35.         return status_;
  36.     }
复制代码
以上就是Android Bugreport实现原理深入分析的详细内容,更多关于Android Bugreport的资料请关注晓枫资讯其它相关文章!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
晓枫资讯-科技资讯社区-免责声明
免责声明:以上内容为本网站转自其它媒体,相关信息仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同其观点或证实其内容的真实性。
      1、注册用户在本社区发表、转载的任何作品仅代表其个人观点,不代表本社区认同其观点。
      2、管理员及版主有权在不事先通知或不经作者准许的情况下删除其在本社区所发表的文章。
      3、本社区的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,举报反馈:点击这里给我发消息进行删除处理。
      4、本社区一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
      5、以上声明内容的最终解释权归《晓枫资讯-科技资讯社区》所有。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
14
积分
8
注册时间
2022-12-29
最后登录
2022-12-29

发表于 2024-11-28 15:24:35 | 显示全部楼层
感谢楼主分享。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
15
积分
10
注册时间
2022-12-29
最后登录
2022-12-29

发表于 2024-11-29 06:13:03 | 显示全部楼层
感谢楼主,顶。
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
19
积分
18
注册时间
2022-12-25
最后登录
2022-12-25

发表于 2025-1-2 15:00:31 | 显示全部楼层
顶顶更健康!!!
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~

  离线 

TA的专栏

等级头衔

等級:晓枫资讯-列兵

在线时间
0 小时

积分成就
威望
0
贡献
0
主题
0
精华
0
金钱
16
积分
12
注册时间
2022-12-24
最后登录
2022-12-24

发表于 2025-4-13 02:48:11 | 显示全部楼层
路过,支持一下
http://bbs.yzwlo.com 晓枫资讯--游戏IT新闻资讯~~~
严禁发布广告,淫秽、色情、赌博、暴力、凶杀、恐怖、间谍及其他违反国家法律法规的内容。!晓枫资讯-社区
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1楼
2楼
3楼
4楼
5楼

手机版|晓枫资讯--科技资讯社区 本站已运行

CopyRight © 2022-2025 晓枫资讯--科技资讯社区 ( BBS.yzwlo.com ) . All Rights Reserved .

晓枫资讯--科技资讯社区

本站内容由用户自主分享和转载自互联网,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。

如有侵权、违反国家法律政策行为,请联系我们,我们会第一时间及时清除和处理! 举报反馈邮箱:点击这里给我发消息

Powered by Discuz! X3.5

快速回复 返回顶部 返回列表