Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 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 | |
Primiano Tucci | c64b05f | 2019-12-05 10:30:52 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 19 | #include <fstream> |
| 20 | #include <iostream> |
Lalit Maganti | 84d6adf | 2019-07-19 19:59:05 +0530 | [diff] [blame] | 21 | #include <limits> |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 22 | #include <vector> |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 23 | |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 24 | #include "perfetto/base/logging.h" |
Neda Topoljanac | c597f53 | 2019-10-09 16:39:59 +0100 | [diff] [blame] | 25 | #include "perfetto/ext/base/string_utils.h" |
Florian Mayer | 3be55fb | 2019-11-22 13:08:47 +0000 | [diff] [blame] | 26 | #include "tools/trace_to_text/deobfuscate_profile.h" |
Neda Topoljanac | 3928801 | 2019-09-25 15:25:01 +0100 | [diff] [blame] | 27 | #include "tools/trace_to_text/symbolize_profile.h" |
Eric Seckler | a60a6b4 | 2019-10-07 10:35:48 +0100 | [diff] [blame] | 28 | #include "tools/trace_to_text/trace_to_json.h" |
Florian Mayer | 0ca7fb4 | 2018-11-15 21:13:42 +0000 | [diff] [blame] | 29 | #include "tools/trace_to_text/trace_to_profile.h" |
Primiano Tucci | 20dc8f7 | 2018-10-23 12:28:29 +0100 | [diff] [blame] | 30 | #include "tools/trace_to_text/trace_to_systrace.h" |
| 31 | #include "tools/trace_to_text/trace_to_text.h" |
Lalit Maganti | 9b78d5d | 2019-07-19 09:02:33 +0100 | [diff] [blame] | 32 | |
Primiano Tucci | 02c1176 | 2019-08-30 00:57:59 +0200 | [diff] [blame] | 33 | #if PERFETTO_BUILDFLAG(PERFETTO_VERSION_GEN) |
Hector Dearman | dc1fb89 | 2019-03-05 13:20:09 +0000 | [diff] [blame] | 34 | #include "perfetto_version.gen.h" |
| 35 | #else |
| 36 | #define PERFETTO_GET_GIT_REVISION() "unknown" |
| 37 | #endif |
| 38 | |
Primiano Tucci | c64b05f | 2019-12-05 10:30:52 +0000 | [diff] [blame] | 39 | #if !PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) |
| 40 | #include <unistd.h> |
| 41 | #endif |
| 42 | |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 43 | namespace perfetto { |
| 44 | namespace trace_to_text { |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 45 | namespace { |
| 46 | |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 47 | int Usage(const char* argv0) { |
Florian Mayer | 1d00084 | 2019-10-24 14:52:57 +0100 | [diff] [blame] | 48 | fprintf(stderr, |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 49 | "Usage: %s systrace|json|ctrace|text|profile [--pid PID] " |
| 50 | "[--timestamps TIMESTAMP1,TIMESTAMP2,...] " |
| 51 | "[--truncate start|end] " |
| 52 | "[--full-sort] " |
| 53 | "[trace.pb] " |
| 54 | "[trace.txt]\n" |
| 55 | "\nProfile mode only:\n" |
| 56 | "\t--timestamps TIMESTAMP1,TIMESTAMP2,... generate profiles " |
| 57 | "only for these timestamps\n" |
| 58 | "\t--pid PID generate profiles only for this process id\n", |
| 59 | argv0); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 60 | return 1; |
| 61 | } |
| 62 | |
Neda Topoljanac | c597f53 | 2019-10-09 16:39:59 +0100 | [diff] [blame] | 63 | uint64_t StringToUint64OrDie(const char* str) { |
| 64 | char* end; |
| 65 | uint64_t number = static_cast<uint64_t>(strtoll(str, &end, 10)); |
| 66 | if (*end != '\0') { |
| 67 | PERFETTO_ELOG("Invalid %s. Expected decimal integer.", str); |
| 68 | exit(1); |
| 69 | } |
| 70 | return number; |
| 71 | } |
| 72 | |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 73 | int Main(int argc, char** argv) { |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 74 | std::vector<const char*> positional_args; |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 75 | Keep truncate_keep = Keep::kAll; |
Neda Topoljanac | c597f53 | 2019-10-09 16:39:59 +0100 | [diff] [blame] | 76 | uint64_t pid = 0; |
| 77 | std::vector<uint64_t> timestamps; |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 78 | bool full_sort = false; |
Hector Dearman | dc1fb89 | 2019-03-05 13:20:09 +0000 | [diff] [blame] | 79 | for (int i = 1; i < argc; i++) { |
| 80 | if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) { |
| 81 | printf("%s\n", PERFETTO_GET_GIT_REVISION()); |
| 82 | return 0; |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 83 | } else if (strcmp(argv[i], "-t") == 0 || |
| 84 | strcmp(argv[i], "--truncate") == 0) { |
Isabelle Taylor | 53a982a | 2019-08-08 14:26:38 +0100 | [diff] [blame] | 85 | i++; |
| 86 | if (i <= argc && strcmp(argv[i], "start") == 0) { |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 87 | truncate_keep = Keep::kStart; |
Isabelle Taylor | 53a982a | 2019-08-08 14:26:38 +0100 | [diff] [blame] | 88 | } else if (i <= argc && strcmp(argv[i], "end") == 0) { |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 89 | truncate_keep = Keep::kEnd; |
Isabelle Taylor | 53a982a | 2019-08-08 14:26:38 +0100 | [diff] [blame] | 90 | } else { |
| 91 | PERFETTO_ELOG( |
| 92 | "--truncate must specify whether to keep the end or the " |
| 93 | "start of the trace."); |
| 94 | return Usage(argv[0]); |
| 95 | } |
Neda Topoljanac | c597f53 | 2019-10-09 16:39:59 +0100 | [diff] [blame] | 96 | } else if (i <= argc && strcmp(argv[i], "--pid") == 0) { |
| 97 | i++; |
| 98 | pid = StringToUint64OrDie(argv[i]); |
| 99 | } else if (i <= argc && strcmp(argv[i], "--timestamps") == 0) { |
| 100 | i++; |
| 101 | std::vector<std::string> ts_strings = base::SplitString(argv[i], ","); |
| 102 | for (const std::string& ts : ts_strings) { |
| 103 | timestamps.emplace_back(StringToUint64OrDie(ts.c_str())); |
| 104 | } |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 105 | } else if (strcmp(argv[i], "--full-sort") == 0) { |
| 106 | full_sort = true; |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 107 | } else { |
| 108 | positional_args.push_back(argv[i]); |
Hector Dearman | dc1fb89 | 2019-03-05 13:20:09 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 112 | if (positional_args.size() < 1) |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 113 | return Usage(argv[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 114 | |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 115 | std::istream* input_stream; |
| 116 | std::ifstream file_istream; |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 117 | if (positional_args.size() > 1) { |
| 118 | const char* file_path = positional_args[1]; |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 119 | file_istream.open(file_path, std::ios_base::in | std::ios_base::binary); |
| 120 | if (!file_istream.is_open()) |
| 121 | PERFETTO_FATAL("Could not open %s", file_path); |
| 122 | input_stream = &file_istream; |
| 123 | } else { |
| 124 | if (isatty(STDIN_FILENO)) { |
| 125 | PERFETTO_ELOG("Reading from stdin but it's connected to a TTY"); |
| 126 | PERFETTO_LOG("It is unlikely that you want to type in some binary."); |
| 127 | PERFETTO_LOG("Either pass a file path to the cmdline or pipe stdin"); |
| 128 | return Usage(argv[0]); |
| 129 | } |
| 130 | input_stream = &std::cin; |
| 131 | } |
| 132 | |
| 133 | std::ostream* output_stream; |
| 134 | std::ofstream file_ostream; |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 135 | if (positional_args.size() > 2) { |
| 136 | const char* file_path = positional_args[2]; |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 137 | file_ostream.open(file_path, std::ios_base::out | std::ios_base::trunc); |
| 138 | if (!file_ostream.is_open()) |
| 139 | PERFETTO_FATAL("Could not open %s", file_path); |
| 140 | output_stream = &file_ostream; |
| 141 | } else { |
| 142 | output_stream = &std::cout; |
Primiano Tucci | 1c752c1 | 2018-10-23 09:27:19 +0100 | [diff] [blame] | 143 | } |
| 144 | |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 145 | std::string format(positional_args[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 146 | |
Neda Topoljanac | c597f53 | 2019-10-09 16:39:59 +0100 | [diff] [blame] | 147 | if (format != "profile" && (pid != 0 || !timestamps.empty())) { |
| 148 | PERFETTO_ELOG( |
| 149 | "--pid and --timestamps are supported only for profile format."); |
| 150 | return 1; |
| 151 | } |
| 152 | |
Hector Dearman | 1b8b342 | 2019-03-26 16:54:08 +0000 | [diff] [blame] | 153 | if (format == "json") |
Eric Seckler | a60a6b4 | 2019-10-07 10:35:48 +0100 | [diff] [blame] | 154 | return TraceToJson(input_stream, output_stream, /*compress=*/false, |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 155 | truncate_keep, full_sort); |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 156 | |
Hector Dearman | 1b8b342 | 2019-03-26 16:54:08 +0000 | [diff] [blame] | 157 | if (format == "systrace") |
Eric Seckler | a60a6b4 | 2019-10-07 10:35:48 +0100 | [diff] [blame] | 158 | return TraceToSystrace(input_stream, output_stream, /*compress=*/false, |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 159 | truncate_keep, full_sort); |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 160 | |
| 161 | if (format == "ctrace") |
Eric Seckler | a60a6b4 | 2019-10-07 10:35:48 +0100 | [diff] [blame] | 162 | return TraceToSystrace(input_stream, output_stream, /*compress=*/true, |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 163 | truncate_keep, full_sort); |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 164 | |
| 165 | if (truncate_keep != Keep::kAll) { |
Isabelle Taylor | 453dbd5 | 2019-07-19 11:47:41 +0100 | [diff] [blame] | 166 | PERFETTO_ELOG( |
| 167 | "--truncate is unsupported for text|profile|symbolize format."); |
| 168 | return 1; |
| 169 | } |
| 170 | |
Lalit Maganti | 9b3a6c7 | 2019-10-25 10:33:08 +0100 | [diff] [blame] | 171 | if (full_sort) { |
| 172 | PERFETTO_ELOG( |
| 173 | "--full-sort is unsupported for text|profile|symbolize format."); |
| 174 | return 1; |
| 175 | } |
| 176 | |
Primiano Tucci | 45c9b18 | 2018-03-29 14:10:51 +0100 | [diff] [blame] | 177 | if (format == "text") |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 178 | return TraceToText(input_stream, output_stream); |
Primiano Tucci | f026990 | 2018-04-13 11:13:19 +0100 | [diff] [blame] | 179 | |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 180 | if (format == "profile") |
Neda Topoljanac | c597f53 | 2019-10-09 16:39:59 +0100 | [diff] [blame] | 181 | return TraceToProfile(input_stream, output_stream, pid, timestamps); |
Florian Mayer | 0ca7fb4 | 2018-11-15 21:13:42 +0000 | [diff] [blame] | 182 | |
Florian Mayer | 0b06375 | 2019-07-08 19:51:31 +0100 | [diff] [blame] | 183 | if (format == "symbolize") |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 184 | return SymbolizeProfile(input_stream, output_stream); |
Florian Mayer | 0b06375 | 2019-07-08 19:51:31 +0100 | [diff] [blame] | 185 | |
Florian Mayer | 3be55fb | 2019-11-22 13:08:47 +0000 | [diff] [blame] | 186 | if (format == "deobfuscate") |
| 187 | return DeobfuscateProfile(input_stream, output_stream); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 188 | return Usage(argv[0]); |
Hector Dearman | 20b3c1c | 2018-01-15 15:34:03 +0000 | [diff] [blame] | 189 | } |
Hector Dearman | 566badf | 2019-09-26 17:15:43 +0100 | [diff] [blame] | 190 | |
| 191 | } // namespace |
| 192 | } // namespace trace_to_text |
| 193 | } // namespace perfetto |
| 194 | |
| 195 | int main(int argc, char** argv) { |
| 196 | return perfetto::trace_to_text::Main(argc, argv); |
| 197 | } |