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/base/event.h"
28 #include "perfetto/base/scoped_file.h"
29 #include "perfetto/base/unix_task_runner.h"
30 #include "perfetto/tracing/core/consumer.h"
31 #include "perfetto/tracing/ipc/consumer_ipc_client.h"
32 #include "src/perfetto_cmd/rate_limiter.h"
33 
34 #include "src/perfetto_cmd/perfetto_cmd_state.pb.h"
35 
36 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
37 #include "perfetto/base/android_task_runner.h"
38 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
39 
40 namespace perfetto {
41 
42 class PacketWriter;
43 
44 // Temporary directory for DropBox traces. Note that this is automatically
45 // created by the system by setting setprop persist.traced.enable=1.
46 extern const char* kTempDropBoxTraceDir;
47 
48 #if PERFETTO_BUILDFLAG(PERFETTO_ANDROID_BUILD)
49 using PlatformTaskRunner = base::AndroidTaskRunner;
50 #else
51 using PlatformTaskRunner = base::UnixTaskRunner;
52 #endif
53 
54 class PerfettoCmd : public Consumer {
55  public:
56   int Main(int argc, char** argv);
57 
58   // perfetto::Consumer implementation.
59   void OnConnect() override;
60   void OnDisconnect() override;
61   void OnTracingDisabled() override;
62   void OnTraceData(std::vector<TracePacket>, bool has_more) override;
63   void OnDetach(bool) override;
64   void OnAttach(bool, const TraceConfig&) override;
65   void OnTraceStats(bool, const TraceStats&) override;
66   void OnObservableEvents(const ObservableEvents&) override;
67 
SignalCtrlC()68   void SignalCtrlC() { ctrl_c_evt_.Notify(); }
69 
70  private:
71   bool OpenOutputFile();
72   void SetupCtrlCSignalHandler();
73   void FinalizeTraceAndExit();
74   int PrintUsage(const char* argv0);
75   void OnTimeout();
is_detach()76   bool is_detach() const { return !detach_key_.empty(); }
is_attach()77   bool is_attach() const { return !attach_key_.empty(); }
78   void SaveOutputToDropboxOrCrash();
79   void SaveOutputToIncidentTraceOrCrash();
80 
81   PlatformTaskRunner task_runner_;
82   std::unique_ptr<perfetto::TracingService::ConsumerEndpoint>
83       consumer_endpoint_;
84   std::unique_ptr<TraceConfig> trace_config_;
85 
86   std::unique_ptr<PacketWriter> packet_writer_;
87   base::ScopedFstream trace_out_stream_;
88 
89   std::string trace_out_path_;
90   base::Event ctrl_c_evt_;
91   std::string dropbox_tag_;
92   bool did_process_full_trace_ = false;
93   uint64_t bytes_written_ = 0;
94   std::string detach_key_;
95   std::string attach_key_;
96   bool stop_trace_once_attached_ = false;
97   bool redetach_once_attached_ = false;
98 };
99 
100 }  // namespace perfetto
101 
102 #endif  // SRC_PERFETTO_CMD_PERFETTO_CMD_H_
103