• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_PERFETTO_CMD_PERFETTO_CMD_H_
18 #define SRC_PERFETTO_CMD_PERFETTO_CMD_H_
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include <time.h>
25 
26 #include "perfetto/base/build_config.h"
27 #include "perfetto/ext/base/event_fd.h"
28 #include "perfetto/ext/base/optional.h"
29 #include "perfetto/ext/base/scoped_file.h"
30 #include "perfetto/ext/base/unix_task_runner.h"
31 #include "perfetto/ext/tracing/core/consumer.h"
32 #include "perfetto/ext/tracing/ipc/consumer_ipc_client.h"
33 #include "src/android_stats/perfetto_atoms.h"
34 #include "src/perfetto_cmd/rate_limiter.h"
35 
36 namespace perfetto {
37 
38 class PacketWriter;
39 
40 // Directory for local state and temporary files. This is automatically
41 // created by the system by setting setprop persist.traced.enable=1.
42 extern const char* kStateDir;
43 
44 class PerfettoCmd : public Consumer {
45  public:
46   int Main(int argc, char** argv);
47 
48   // perfetto::Consumer implementation.
49   void OnConnect() override;
50   void OnDisconnect() override;
51   void OnTracingDisabled(const std::string& error) override;
52   void OnTraceData(std::vector<TracePacket>, bool has_more) override;
53   void OnDetach(bool) override;
54   void OnAttach(bool, const TraceConfig&) override;
55   void OnTraceStats(bool, const TraceStats&) override;
56   void OnObservableEvents(const ObservableEvents&) override;
57 
SignalCtrlC()58   void SignalCtrlC() { ctrl_c_evt_.Notify(); }
59 
60  private:
61   bool OpenOutputFile();
62   void SetupCtrlCSignalHandler();
63   void FinalizeTraceAndExit();
64   int PrintUsage(const char* argv0);
65   void PrintServiceState(bool success, const TracingServiceState&);
66   void OnTimeout();
is_detach()67   bool is_detach() const { return !detach_key_.empty(); }
is_attach()68   bool is_attach() const { return !attach_key_.empty(); }
69 
70   // Once we call ReadBuffers we expect one or more calls to OnTraceData
71   // with the last call having |has_more| set to false. However we should
72   // gracefully handle the service failing to ever call OnTraceData or
73   // setting |has_more| incorrectly. To do this we maintain a timeout
74   // which finalizes and exits the client if we don't receive OnTraceData
75   // within OnTraceDataTimeoutMs of when we expected to.
76   void CheckTraceDataTimeout();
77 
78 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
79   static base::ScopedFile CreateUnlinkedTmpFile();
80   void SaveTraceIntoDropboxAndIncidentOrCrash();
81   void SaveOutputToIncidentTraceOrCrash();
82 #endif
83   void LogUploadEvent(PerfettoStatsdAtom atom);
84   void LogTriggerEvents(PerfettoTriggerAtom atom,
85                         const std::vector<std::string>& trigger_names);
86 
87   base::UnixTaskRunner task_runner_;
88 
89   std::unique_ptr<perfetto::TracingService::ConsumerEndpoint>
90       consumer_endpoint_;
91   std::unique_ptr<TraceConfig> trace_config_;
92 
93   std::unique_ptr<PacketWriter> packet_writer_;
94   base::ScopedFstream trace_out_stream_;
95 
96   std::string trace_out_path_;
97   base::EventFd ctrl_c_evt_;
98   bool save_to_incidentd_ = false;
99   bool statsd_logging_ = false;
100   bool update_guardrail_state_ = false;
101   uint64_t bytes_written_ = 0;
102   std::string detach_key_;
103   std::string attach_key_;
104   bool stop_trace_once_attached_ = false;
105   bool redetach_once_attached_ = false;
106   bool query_service_ = false;
107   bool query_service_output_raw_ = false;
108   bool bugreport_ = false;
109   std::string uuid_;
110 
111   // How long we expect to trace for or 0 if the trace is indefinite.
112   uint32_t expected_duration_ms_ = 0;
113   bool trace_data_timeout_armed_ = false;
114 };
115 
116 }  // namespace perfetto
117 
118 #endif  // SRC_PERFETTO_CMD_PERFETTO_CMD_H_
119