xref: /aosp_15_r20/external/pytorch/torch/csrc/profiler/unwind/unwind_fb.cpp (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #if defined(__linux__) && (defined(__x86_64__) || defined(__aarch64__)) && \
2     defined(__has_include) &&                                              \
3     __has_include("ext/stdio_filebuf.h") && defined(FBCODE_CAFFE2)
4 #include <c10/util/flat_hash_map.h>
5 #include <llvm/DebugInfo/Symbolize/Symbolize.h>
6 #include <torch/csrc/profiler/unwind/unwind.h>
7 
8 namespace torch::unwind {
9 
symbolize(const std::vector<void * > & frames,Mode mode)10 std::vector<Frame> symbolize(const std::vector<void*>& frames, Mode mode) {
11   static std::mutex symbolize_mutex;
12   static llvm::symbolize::LLVMSymbolizer symbolizer;
13   static ska::flat_hash_map<void*, Frame> frame_map_;
14 
15   std::lock_guard<std::mutex> guard(symbolize_mutex);
16   std::vector<Frame> results;
17   results.reserve(frames.size());
18   for (auto addr : frames) {
19     if (!frame_map_.count(addr)) {
20       auto frame = Frame{"??", "<unwind unsupported>", 0};
21       auto maybe_library = libraryFor(addr);
22       if (maybe_library) {
23         auto libaddress = maybe_library->second - 1;
24         auto r = symbolizer.symbolizeCode(
25             maybe_library->first,
26             {libaddress, llvm::object::SectionedAddress::UndefSection});
27         if (r) {
28           frame.filename = r->FileName;
29           frame.funcname = r->FunctionName;
30           frame.lineno = r->Line;
31         }
32       }
33       frame_map_[addr] = std::move(frame);
34     }
35     results.emplace_back(frame_map_[addr]);
36   }
37   return results;
38 }
39 
40 } // namespace torch::unwind
41 
42 #endif
43