service: Scrape SMBs on flush, disable and producer disconnect

Scrape chunks from the SMBs that have been started but haven't yet been
committed by the producers (i.e. they are either completed or currently
being written to). We do this in three locations:
- When flushing, e.g. to handle unresponsive producers or producers
  that are unable to flush their active chunks.
- When a producer disconnects, e.g. to handle crashed producers.
- After the session was disabled, e.g. to gather data from producers
  that didn't stop their data sources in time.

Bug: 73828976
Bug: 117310221
Change-Id: I4ed5394b6c47ea35a4999e4ff95b469337a5d273
diff --git a/src/tracing/core/tracing_service_impl.h b/src/tracing/core/tracing_service_impl.h
index 1cc07db..ae4fe30 100644
--- a/src/tracing/core/tracing_service_impl.h
+++ b/src/tracing/core/tracing_service_impl.h
@@ -227,6 +227,10 @@
       Consumer*,
       uid_t) override;
 
+  void SetSMBScrapingEnabled(bool enabled) override {
+    smb_scraping_enabled_ = enabled;
+  }
+
   // Exposed mainly for testing.
   size_t num_producers() const { return producers_.size(); }
   ProducerEndpointImpl* GetProducer(ProducerID) const;
@@ -373,6 +377,11 @@
   void OnDisableTracingTimeout(TracingSessionID);
   void DisableTracingNotifyConsumerAndFlushFile(TracingSession*);
   void PeriodicFlushTask(TracingSessionID, bool post_next_only);
+  void CompleteFlush(TracingSessionID tsid,
+                     ConsumerEndpoint::FlushCallback callback,
+                     bool success);
+  void ScrapeSharedMemoryBuffers(TracingSession* tracing_session,
+                                 ProducerEndpointImpl* producer);
   TraceBuffer* GetBufferByID(BufferID);
 
   base::TaskRunner* const task_runner_;
@@ -393,6 +402,7 @@
   std::map<TracingSessionID, TracingSession> tracing_sessions_;
   std::map<BufferID, std::unique_ptr<TraceBuffer>> buffers_;
 
+  bool smb_scraping_enabled_ = false;
   bool lockdown_mode_ = false;
   uint32_t min_write_period_ms_ = 100;  // Overridable for testing.