xref: /aosp_15_r20/external/perfetto/src/traceconv/symbolize_profile.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2019 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/symbolize_profile.h"
18 
19 #include <vector>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/trace_processor/trace_processor.h"
23 
24 #include "src/profiling/symbolizer/breakpad_symbolizer.h"
25 #include "src/profiling/symbolizer/local_symbolizer.h"
26 #include "src/profiling/symbolizer/symbolize_database.h"
27 #include "src/profiling/symbolizer/symbolizer.h"
28 
29 #include "protos/perfetto/trace/trace.pbzero.h"
30 #include "protos/perfetto/trace/trace_packet.pbzero.h"
31 #include "src/traceconv/utils.h"
32 
33 namespace perfetto {
34 namespace trace_to_text {
35 
36 // Ingest profile, and emit a symbolization table for each sequence. This can
37 // be prepended to the profile to attach the symbol information.
SymbolizeProfile(std::istream * input,std::ostream * output)38 int SymbolizeProfile(std::istream* input, std::ostream* output) {
39   std::unique_ptr<profiling::Symbolizer> symbolizer;
40   const char* breakpad_dir = getenv("BREAKPAD_SYMBOL_DIR");
41   if (breakpad_dir == nullptr) {
42     symbolizer = profiling::LocalSymbolizerOrDie(
43         profiling::GetPerfettoBinaryPath(), getenv("PERFETTO_SYMBOLIZER_MODE"));
44   } else {
45     symbolizer.reset(new profiling::BreakpadSymbolizer(breakpad_dir));
46   }
47 
48   if (!symbolizer)
49     PERFETTO_FATAL("No symbolizer selected");
50   trace_processor::Config config;
51   std::unique_ptr<trace_processor::TraceProcessor> tp =
52       trace_processor::TraceProcessor::CreateInstance(config);
53 
54   if (!ReadTraceUnfinalized(tp.get(), input))
55     PERFETTO_FATAL("Failed to read trace.");
56 
57   tp->Flush();
58   if (auto status = tp->NotifyEndOfFile(); !status.ok()) {
59     PERFETTO_FATAL("%s", status.c_message());
60   }
61 
62   SymbolizeDatabase(
63       tp.get(), symbolizer.get(),
64       [output](const std::string& trace_proto) { *output << trace_proto; });
65 
66   return 0;
67 }
68 
69 }  // namespace trace_to_text
70 }  // namespace perfetto
71