Add bugreport pre-dump functionality

Allow apps to trigger the dump of certain critical data, e.g. data stored in short
ring buffers that might get lost by the time a bugreport is requested.

Then, a bugreport request can specify whether the pre-dumped data should be used.

Fixes: 205138504
Test: atest com.android.os.bugreports.tests.BugreportManagerTest
Ignore-AOSP-First: depends on changes (surfaceflinger) that cannot go into AOSP
Change-Id: I976f2ed3189e83f5bd71dc81e20306527c411d10
diff --git a/cmds/dumpstate/DumpstateService.cpp b/cmds/dumpstate/DumpstateService.cpp
index e42ee05..42e9e0f 100644
--- a/cmds/dumpstate/DumpstateService.cpp
+++ b/cmds/dumpstate/DumpstateService.cpp
@@ -51,7 +51,7 @@
 
 // Creates a bugreport and exits, thus preserving the oneshot nature of the service.
 // Note: takes ownership of data.
-[[noreturn]] static void* dumpstate_thread_main(void* data) {
+[[noreturn]] static void* dumpstate_thread_bugreport(void* data) {
     std::unique_ptr<DumpstateInfo> ds_info(static_cast<DumpstateInfo*>(data));
     ds_info->ds->Run(ds_info->calling_uid, ds_info->calling_package);
     MYLOGD("Finished taking a bugreport. Exiting.\n");
@@ -84,11 +84,28 @@
     return android::OK;
 }
 
+binder::Status DumpstateService::preDumpUiData(const std::string&) {
+    std::lock_guard<std::mutex> lock(lock_);
+    MYLOGI("preDumpUiData()");
+
+    if (ds_ != nullptr) {
+        MYLOGE("Error! DumpstateService is currently already being used. Returning.");
+        return exception(binder::Status::EX_SERVICE_SPECIFIC,
+                         "DumpstateService is already being used");
+    }
+
+    ds_ = &(Dumpstate::GetInstance());
+    ds_->PreDumpUiData();
+
+    return binder::Status::ok();
+}
+
 binder::Status DumpstateService::startBugreport(int32_t calling_uid,
                                                 const std::string& calling_package,
                                                 android::base::unique_fd bugreport_fd,
                                                 android::base::unique_fd screenshot_fd,
                                                 int bugreport_mode,
+                                                int bugreport_flags,
                                                 const sp<IDumpstateListener>& listener,
                                                 bool is_screenshot_requested) {
     MYLOGI("startBugreport() with mode: %d\n", bugreport_mode);
@@ -96,12 +113,12 @@
     // Ensure there is only one bugreport in progress at a time.
     std::lock_guard<std::mutex> lock(lock_);
     if (ds_ != nullptr) {
-        MYLOGE("Error! There is already a bugreport in progress. Returning.");
+        MYLOGE("Error! DumpstateService is currently already being used. Returning.");
         if (listener != nullptr) {
             listener->onError(IDumpstateListener::BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS);
         }
         return exception(binder::Status::EX_SERVICE_SPECIFIC,
-                         "There is already a bugreport in progress");
+                         "DumpstateService is already being used");
     }
 
     // From here on, all conditions that indicate we are done with this incoming request should
@@ -123,8 +140,8 @@
     }
 
     std::unique_ptr<Dumpstate::DumpOptions> options = std::make_unique<Dumpstate::DumpOptions>();
-    options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_fd,
-                        screenshot_fd, is_screenshot_requested);
+    options->Initialize(static_cast<Dumpstate::BugreportMode>(bugreport_mode), bugreport_flags,
+                        bugreport_fd, screenshot_fd, is_screenshot_requested);
 
     if (bugreport_fd.get() == -1 || (options->do_screenshot && screenshot_fd.get() == -1)) {
         MYLOGE("Invalid filedescriptor");
@@ -148,10 +165,9 @@
     pthread_t thread;
     // Initialize dumpstate
     ds_->Initialize();
-    status_t err = pthread_create(&thread, nullptr, dumpstate_thread_main, ds_info);
+    status_t err = pthread_create(&thread, nullptr, dumpstate_thread_bugreport, ds_info);
     if (err != 0) {
         delete ds_info;
-        ds_info = nullptr;
         MYLOGE("Could not create a thread");
         signalErrorAndExit(listener, IDumpstateListener::BUGREPORT_ERROR_RUNTIME_ERROR);
     }