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 #include <stdio.h>
18
19 #include <fstream>
20 #include <iostream>
21 #include <limits>
22 #include <vector>
23
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/string_utils.h"
26 #include "perfetto/ext/base/version.h"
27 #include "tools/trace_to_text/deobfuscate_profile.h"
28 #include "tools/trace_to_text/symbolize_profile.h"
29 #include "tools/trace_to_text/trace_to_hprof.h"
30 #include "tools/trace_to_text/trace_to_json.h"
31 #include "tools/trace_to_text/trace_to_profile.h"
32 #include "tools/trace_to_text/trace_to_systrace.h"
33 #include "tools/trace_to_text/trace_to_text.h"
34
35 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
36 #include <unistd.h>
37 #endif
38
39 namespace perfetto {
40 namespace trace_to_text {
41 namespace {
42
Usage(const char * argv0)43 int Usage(const char* argv0) {
44 fprintf(stderr,
45 "Usage: %s MODE [OPTIONS] [input file] [output file]\n"
46 "modes:\n"
47 " systrace|json|ctrace|text|profile|hprof|symbolize|deobfuscate\n"
48 "options:\n"
49 " [--truncate start|end]\n"
50 " [--full-sort]\n"
51 "\"profile\" mode options:\n"
52 " [--perf] generate a perf profile instead of a heap profile\n"
53 " [--no-annotations] do not suffix frame names with derived "
54 "annotations\n"
55 " [--timestamps TIMESTAMP1,TIMESTAMP2,...] generate profiles "
56 "only for these *specific* timestamps\n"
57 " [--pid PID] generate profiles only for this process id\n",
58 argv0);
59 return 1;
60 }
61
StringToUint64OrDie(const char * str)62 uint64_t StringToUint64OrDie(const char* str) {
63 char* end;
64 uint64_t number = static_cast<uint64_t>(strtoll(str, &end, 10));
65 if (*end != '\0') {
66 PERFETTO_ELOG("Invalid %s. Expected decimal integer.", str);
67 exit(1);
68 }
69 return number;
70 }
71
Main(int argc,char ** argv)72 int Main(int argc, char** argv) {
73 std::vector<const char*> positional_args;
74 Keep truncate_keep = Keep::kAll;
75 uint64_t pid = 0;
76 std::vector<uint64_t> timestamps;
77 bool full_sort = false;
78 bool perf_profile = false;
79 bool profile_no_annotations = false;
80 for (int i = 1; i < argc; i++) {
81 if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
82 printf("%s\n", base::GetVersionString());
83 return 0;
84 } else if (strcmp(argv[i], "-t") == 0 ||
85 strcmp(argv[i], "--truncate") == 0) {
86 i++;
87 if (i <= argc && strcmp(argv[i], "start") == 0) {
88 truncate_keep = Keep::kStart;
89 } else if (i <= argc && strcmp(argv[i], "end") == 0) {
90 truncate_keep = Keep::kEnd;
91 } else {
92 PERFETTO_ELOG(
93 "--truncate must specify whether to keep the end or the "
94 "start of the trace.");
95 return Usage(argv[0]);
96 }
97 } else if (i <= argc && strcmp(argv[i], "--pid") == 0) {
98 i++;
99 pid = StringToUint64OrDie(argv[i]);
100 } else if (i <= argc && strcmp(argv[i], "--timestamps") == 0) {
101 i++;
102 std::vector<std::string> ts_strings = base::SplitString(argv[i], ",");
103 for (const std::string& ts : ts_strings) {
104 timestamps.emplace_back(StringToUint64OrDie(ts.c_str()));
105 }
106 } else if (strcmp(argv[i], "--perf") == 0) {
107 perf_profile = true;
108 } else if (strcmp(argv[i], "--no-annotations") == 0) {
109 profile_no_annotations = true;
110 } else if (strcmp(argv[i], "--full-sort") == 0) {
111 full_sort = true;
112 } else {
113 positional_args.push_back(argv[i]);
114 }
115 }
116
117 if (positional_args.empty())
118 return Usage(argv[0]);
119
120 std::istream* input_stream;
121 std::ifstream file_istream;
122 if (positional_args.size() > 1) {
123 const char* file_path = positional_args[1];
124 file_istream.open(file_path, std::ios_base::in | std::ios_base::binary);
125 if (!file_istream.is_open())
126 PERFETTO_FATAL("Could not open %s", file_path);
127 input_stream = &file_istream;
128 } else {
129 #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
130 if (isatty(STDIN_FILENO)) {
131 PERFETTO_ELOG("Reading from stdin but it's connected to a TTY");
132 PERFETTO_LOG("It is unlikely that you want to type in some binary.");
133 PERFETTO_LOG("Either pass a file path to the cmdline or pipe stdin");
134 return Usage(argv[0]);
135 }
136 #endif
137 input_stream = &std::cin;
138 }
139
140 std::ostream* output_stream;
141 std::ofstream file_ostream;
142 if (positional_args.size() > 2) {
143 const char* file_path = positional_args[2];
144 file_ostream.open(file_path, std::ios_base::out | std::ios_base::trunc);
145 if (!file_ostream.is_open())
146 PERFETTO_FATAL("Could not open %s", file_path);
147 output_stream = &file_ostream;
148 } else {
149 output_stream = &std::cout;
150 }
151
152 std::string format(positional_args[0]);
153
154 if ((format != "profile" && format != "hprof") &&
155 (pid != 0 || !timestamps.empty())) {
156 PERFETTO_ELOG(
157 "--pid and --timestamps are supported only for profile "
158 "formats.");
159 return 1;
160 }
161 if (perf_profile && format != "profile") {
162 PERFETTO_ELOG("--perf requires profile format.");
163 return 1;
164 }
165
166 if (format == "json")
167 return TraceToJson(input_stream, output_stream, /*compress=*/false,
168 truncate_keep, full_sort);
169
170 if (format == "systrace")
171 return TraceToSystrace(input_stream, output_stream, /*ctrace=*/false,
172 truncate_keep, full_sort);
173
174 if (format == "ctrace")
175 return TraceToSystrace(input_stream, output_stream, /*ctrace=*/true,
176 truncate_keep, full_sort);
177
178 if (truncate_keep != Keep::kAll) {
179 PERFETTO_ELOG(
180 "--truncate is unsupported for text|profile|symbolize format.");
181 return 1;
182 }
183
184 if (full_sort) {
185 PERFETTO_ELOG(
186 "--full-sort is unsupported for text|profile|symbolize format.");
187 return 1;
188 }
189
190 if (format == "text")
191 return TraceToText(input_stream, output_stream);
192
193 if (format == "profile") {
194 return perf_profile
195 ? TraceToPerfProfile(input_stream, output_stream, pid,
196 timestamps, !profile_no_annotations)
197 : TraceToHeapProfile(input_stream, output_stream, pid,
198 timestamps, !profile_no_annotations);
199 }
200
201 if (format == "hprof")
202 return TraceToHprof(input_stream, output_stream, pid, timestamps);
203
204 if (format == "symbolize")
205 return SymbolizeProfile(input_stream, output_stream);
206
207 if (format == "deobfuscate")
208 return DeobfuscateProfile(input_stream, output_stream);
209 return Usage(argv[0]);
210 }
211
212 } // namespace
213 } // namespace trace_to_text
214 } // namespace perfetto
215
main(int argc,char ** argv)216 int main(int argc, char** argv) {
217 return perfetto::trace_to_text::Main(argc, argv);
218 }
219