Skip to content

Commit 4f38c95

Browse files
committed
traceconv: implement function to go from text -> binary
Implement it in a very trivial way but better than not having it at all
1 parent 89ae182 commit 4f38c95

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/traceconv/BUILD.gn

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,16 @@ source_set("lib") {
8383
"../../include/perfetto/ext/traced:sys_stats_counters",
8484
"../../include/perfetto/protozero",
8585
"../../protos/perfetto/trace:zero",
86-
"../../src/profiling:deobfuscator",
87-
"../../src/profiling/symbolizer",
88-
"../../src/profiling/symbolizer:symbolize_database",
89-
"../../src/protozero:proto_ring_buffer",
90-
"../../src/trace_processor:lib",
91-
"../../src/trace_processor:storage_minimal",
92-
"../../src/trace_processor/util:descriptors",
93-
"../../src/trace_processor/util:gzip",
94-
"../../src/trace_processor/util:protozero_to_text",
95-
"../../src/trace_processor/util:trace_type",
86+
"../profiling:deobfuscator",
87+
"../profiling/symbolizer",
88+
"../profiling/symbolizer:symbolize_database",
89+
"../protozero:proto_ring_buffer",
90+
"../trace_processor:lib",
91+
"../trace_processor:storage_minimal",
92+
"../trace_processor/util:descriptors",
93+
"../trace_processor/util:gzip",
94+
"../trace_processor/util:protozero_to_text",
95+
"../trace_processor/util:trace_type",
9696
]
9797
sources = [
9898
"deobfuscate_profile.cc",
@@ -119,10 +119,12 @@ source_set("lib") {
119119

120120
source_set("main") {
121121
deps = [
122+
":gen_cc_trace_descriptor",
122123
":lib",
123124
"../../gn:default_deps",
124125
"../../include/perfetto/ext/base:base",
125126
"../base:version",
127+
"../protozero/text_to_proto",
126128
]
127129
sources = [ "main.cc" ]
128130
}

src/traceconv/main.cc

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <stdio.h>
18-
17+
#include <cstdint>
18+
#include <cstdio>
19+
#include <cstdlib>
20+
#include <cstring>
1921
#include <fstream>
2022
#include <iostream>
21-
#include <limits>
23+
#include <iterator>
24+
#include <string>
2225
#include <vector>
2326

27+
#include "perfetto/base/build_config.h"
2428
#include "perfetto/base/logging.h"
2529
#include "perfetto/ext/base/string_utils.h"
2630
#include "perfetto/ext/base/version.h"
31+
#include "src/protozero/text_to_proto/text_to_proto.h"
2732
#include "src/traceconv/deobfuscate_profile.h"
2833
#include "src/traceconv/symbolize_profile.h"
34+
#include "src/traceconv/trace.descriptor.h"
2935
#include "src/traceconv/trace_to_firefox.h"
3036
#include "src/traceconv/trace_to_hprof.h"
3137
#include "src/traceconv/trace_to_json.h"
@@ -41,8 +47,7 @@
4147
#include <unistd.h>
4248
#endif
4349

44-
namespace perfetto {
45-
namespace trace_to_text {
50+
namespace perfetto::trace_to_text {
4651
namespace {
4752

4853
int Usage(const char* argv0) {
@@ -51,7 +56,7 @@ int Usage(const char* argv0) {
5156
"Usage: %s MODE [OPTIONS] [input file] [output file]\n"
5257
"modes:\n"
5358
" systrace|json|ctrace|text|profile|hprof|symbolize|deobfuscate|firefox"
54-
"|java_heap_profile|decompress_packets\n"
59+
"|java_heap_profile|decompress_packets|binary\n"
5560
"options:\n"
5661
" [--truncate start|end]\n"
5762
" [--full-sort]\n"
@@ -76,6 +81,23 @@ uint64_t StringToUint64OrDie(const char* str) {
7681
return number;
7782
}
7883

84+
int TextToTrace(std::istream* input, std::ostream* output) {
85+
std::string trace_text((std::istreambuf_iterator<char>(*input)),
86+
(std::istreambuf_iterator<char>()));
87+
auto proto_status =
88+
protozero::TextToProto(kTraceDescriptor.data(), kTraceDescriptor.size(),
89+
".perfetto.protos.Trace", "trace", trace_text);
90+
if (!proto_status.ok()) {
91+
PERFETTO_ELOG("Failed to parse trace: %s",
92+
proto_status.status().c_message());
93+
return 1;
94+
}
95+
const std::vector<uint8_t>& trace_proto = proto_status.value();
96+
output->write(reinterpret_cast<const char*>(trace_proto.data()),
97+
static_cast<int64_t>(trace_proto.size()));
98+
return 0;
99+
}
100+
79101
int Main(int argc, char** argv) {
80102
std::vector<const char*> positional_args;
81103
Keep truncate_keep = Keep::kAll;
@@ -88,8 +110,8 @@ int Main(int argc, char** argv) {
88110
if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--version") == 0) {
89111
printf("%s\n", base::GetVersionString());
90112
return 0;
91-
} else if (strcmp(argv[i], "-t") == 0 ||
92-
strcmp(argv[i], "--truncate") == 0) {
113+
}
114+
if (strcmp(argv[i], "-t") == 0 || strcmp(argv[i], "--truncate") == 0) {
93115
i++;
94116
if (i <= argc && strcmp(argv[i], "start") == 0) {
95117
truncate_keep = Keep::kStart;
@@ -176,6 +198,10 @@ int Main(int argc, char** argv) {
176198
return 1;
177199
}
178200

201+
if (format == "binary") {
202+
return TextToTrace(input_stream, output_stream);
203+
}
204+
179205
if (format == "json")
180206
return TraceToJson(input_stream, output_stream, /*compress=*/false,
181207
truncate_keep, full_sort);
@@ -238,8 +264,7 @@ int Main(int argc, char** argv) {
238264
}
239265

240266
} // namespace
241-
} // namespace trace_to_text
242-
} // namespace perfetto
267+
} // namespace perfetto::trace_to_text
243268

244269
int main(int argc, char** argv) {
245270
return perfetto::trace_to_text::Main(argc, argv);

0 commit comments

Comments
 (0)