perfetto_cmd: do not start incident reports for empty traces

Backport of aosp/1095032. Fixes the problem where when using "tracing
into an incident", empty traces would unintentionally cause an incident
to be created (either empty, or containing the last saved trace of this
kind).

This is preventing the use of incidentd->BetterBug upload path for
perfetto traces from droidfooders (as there are tracing configurations
which intentionally end up with empty traces, which should be discarded
without side-effects).

See b/138780197 for a longer explanation.

Test: manual on crosshatch-userdebug
Bug: 138649485
Merged-In: Ibed6b03f708a994d2c72788b5c251ded152679bc
Change-Id: Id75c451a34f0e04f2a7febf8bd162a89fc8699ce
diff --git a/src/perfetto_cmd/perfetto_cmd.cc b/src/perfetto_cmd/perfetto_cmd.cc
index e1f71dd..51d9dba 100644
--- a/src/perfetto_cmd/perfetto_cmd.cc
+++ b/src/perfetto_cmd/perfetto_cmd.cc
@@ -683,17 +683,26 @@
   // Otherwise, write to Dropbox unless there's a special override in the
   // incident report config.
   if (!trace_config_->incident_report_config().skip_dropbox()) {
-    SaveOutputToDropboxOrCrash();
+    if (bytes_written_ == 0) {
+      PERFETTO_LOG("Skipping write to dropbox. Empty trace.");
+    } else {
+      SaveOutputToDropboxOrCrash();
+    }
   }
 
   // Optionally save the trace as an incident. This is either in addition to, or
   // instead of, the Dropbox write.
   if (!trace_config_->incident_report_config().destination_package().empty()) {
-    SaveOutputToIncidentTraceOrCrash();
+    if (bytes_written_ == 0) {
+      PERFETTO_LOG("Skipping incident report. Empty trace.");
+    } else {
+      SaveOutputToIncidentTraceOrCrash();
 
-    // Ask incidentd to create a report, which will read the file we just wrote.
-    PERFETTO_CHECK(
-        StartIncidentReport(trace_config_->incident_report_config()));
+      // Ask incidentd to create a report, which will read the file we just
+      // wrote.
+      PERFETTO_CHECK(
+          StartIncidentReport(trace_config_->incident_report_config()));
+    }
   }
 
   did_process_full_trace_ = true;
@@ -702,10 +711,6 @@
 
 void PerfettoCmd::SaveOutputToDropboxOrCrash() {
 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
-  if (bytes_written_ == 0) {
-    PERFETTO_LOG("Skipping write to dropbox. Empty trace.");
-    return;
-  }
   android::sp<android::os::DropBoxManager> dropbox =
       new android::os::DropBoxManager();
   PERFETTO_CHECK(fseek(*trace_out_stream_, 0, SEEK_SET) == 0);
@@ -730,16 +735,12 @@
 }
 
 // Open a staging file (unlinking the previous instance), copy the trace
-// contents over, then rename to a final hardcoded path. Such tracing sessions
-// should not normally overlap. We do not use unique unique filenames to avoid
-// creating an unbounded amount of files in case of errors.
+// contents over, then rename to a final hardcoded path (known to incidentd).
+// Such tracing sessions should not normally overlap. We do not use unique
+// unique filenames to avoid creating an unbounded amount of files in case of
+// errors.
 void PerfettoCmd::SaveOutputToIncidentTraceOrCrash() {
 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
-  if (bytes_written_ == 0) {
-    PERFETTO_LOG("Skipping incident report. Empty trace.");
-    return;
-  }
-
   PERFETTO_CHECK(unlink(kTempIncidentTraceLocation) == 0 || errno == ENOENT);
 
   // SELinux constrains the set of readers.