1 /*
2 * Copyright (C) 2024 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 "src/traceconv/trace_to_firefox.h"
18
19 #include <ios>
20 #include <istream>
21 #include <memory>
22 #include <string>
23
24 #include "perfetto/base/logging.h"
25 #include "perfetto/trace_processor/basic_types.h"
26 #include "perfetto/trace_processor/trace_processor.h"
27 #include "src/traceconv/utils.h"
28
29 namespace perfetto {
30 namespace trace_to_text {
31 namespace {
32
ExportFirefoxProfile(trace_processor::TraceProcessor & tp,std::ostream * output)33 void ExportFirefoxProfile(trace_processor::TraceProcessor& tp,
34 std::ostream* output) {
35 auto it = tp.ExecuteQuery(R"(
36 INCLUDE PERFETTO MODULE export.to_firefox_profile;
37 SELECT CAST(export_to_firefox_profile() AS BLOB);
38 )");
39 PERFETTO_CHECK(it.Next());
40
41 it.Get(0).AsBytes();
42 output->write(reinterpret_cast<const char*>(it.Get(0).AsBytes()),
43 static_cast<std::streamsize>(it.Get(0).bytes_count));
44
45 PERFETTO_CHECK(!it.Next());
46 PERFETTO_CHECK(it.Status().ok());
47 }
48
LoadTrace(std::istream * input)49 std::unique_ptr<trace_processor::TraceProcessor> LoadTrace(
50 std::istream* input) {
51 trace_processor::Config config;
52 std::unique_ptr<trace_processor::TraceProcessor> tp =
53 trace_processor::TraceProcessor::CreateInstance(config);
54
55 if (!ReadTraceUnfinalized(tp.get(), input)) {
56 return nullptr;
57 }
58 if (auto status = tp->NotifyEndOfFile(); !status.ok()) {
59 return nullptr;
60 }
61 return tp;
62 }
63
64 } // namespace
65
TraceToFirefoxProfile(std::istream * input,std::ostream * output)66 bool TraceToFirefoxProfile(std::istream* input, std::ostream* output) {
67 auto tp = LoadTrace(input);
68 if (!tp) {
69 return false;
70 }
71 ExportFirefoxProfile(*tp, output);
72 return true;
73 }
74
75 } // namespace trace_to_text
76 } // namespace perfetto
77