xref: /aosp_15_r20/external/llvm/lib/Support/Signals.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- Signals.cpp - Signal Handling support --------------------*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines some helpful functions for dealing with the possibility of
11*9880d681SAndroid Build Coastguard Worker // Unix signals occurring while your program is running.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/STLExtras.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringRef.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Config/config.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorOr.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileSystem.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileUtilities.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Format.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ManagedStatic.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MemoryBuffer.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Mutex.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Program.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Signals.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/StringSaver.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
29*9880d681SAndroid Build Coastguard Worker #include <vector>
30*9880d681SAndroid Build Coastguard Worker 
31*9880d681SAndroid Build Coastguard Worker namespace llvm {
32*9880d681SAndroid Build Coastguard Worker using namespace sys;
33*9880d681SAndroid Build Coastguard Worker 
34*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
35*9880d681SAndroid Build Coastguard Worker //=== WARNING: Implementation here must contain only TRULY operating system
36*9880d681SAndroid Build Coastguard Worker //===          independent code.
37*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
38*9880d681SAndroid Build Coastguard Worker 
39*9880d681SAndroid Build Coastguard Worker static ManagedStatic<std::vector<std::pair<void (*)(void *), void *>>>
40*9880d681SAndroid Build Coastguard Worker     CallBacksToRun;
RunSignalHandlers()41*9880d681SAndroid Build Coastguard Worker void sys::RunSignalHandlers() {
42*9880d681SAndroid Build Coastguard Worker   if (!CallBacksToRun.isConstructed())
43*9880d681SAndroid Build Coastguard Worker     return;
44*9880d681SAndroid Build Coastguard Worker   for (auto &I : *CallBacksToRun)
45*9880d681SAndroid Build Coastguard Worker     I.first(I.second);
46*9880d681SAndroid Build Coastguard Worker   CallBacksToRun->clear();
47*9880d681SAndroid Build Coastguard Worker }
48*9880d681SAndroid Build Coastguard Worker }
49*9880d681SAndroid Build Coastguard Worker 
50*9880d681SAndroid Build Coastguard Worker using namespace llvm;
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker static bool findModulesAndOffsets(void **StackTrace, int Depth,
53*9880d681SAndroid Build Coastguard Worker                                   const char **Modules, intptr_t *Offsets,
54*9880d681SAndroid Build Coastguard Worker                                   const char *MainExecutableName,
55*9880d681SAndroid Build Coastguard Worker                                   StringSaver &StrPool);
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker /// Format a pointer value as hexadecimal. Zero pad it out so its always the
58*9880d681SAndroid Build Coastguard Worker /// same width.
format_ptr(void * PC)59*9880d681SAndroid Build Coastguard Worker static FormattedNumber format_ptr(void *PC) {
60*9880d681SAndroid Build Coastguard Worker   // Each byte is two hex digits plus 2 for the 0x prefix.
61*9880d681SAndroid Build Coastguard Worker   unsigned PtrWidth = 2 + 2 * sizeof(void *);
62*9880d681SAndroid Build Coastguard Worker   return format_hex((uint64_t)PC, PtrWidth);
63*9880d681SAndroid Build Coastguard Worker }
64*9880d681SAndroid Build Coastguard Worker 
65*9880d681SAndroid Build Coastguard Worker static bool printSymbolizedStackTrace(StringRef Argv0,
66*9880d681SAndroid Build Coastguard Worker                                       void **StackTrace, int Depth,
67*9880d681SAndroid Build Coastguard Worker                                       llvm::raw_ostream &OS)
68*9880d681SAndroid Build Coastguard Worker   LLVM_ATTRIBUTE_USED;
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker /// Helper that launches llvm-symbolizer and symbolizes a backtrace.
printSymbolizedStackTrace(StringRef Argv0,void ** StackTrace,int Depth,llvm::raw_ostream & OS)71*9880d681SAndroid Build Coastguard Worker static bool printSymbolizedStackTrace(StringRef Argv0,
72*9880d681SAndroid Build Coastguard Worker                                       void **StackTrace, int Depth,
73*9880d681SAndroid Build Coastguard Worker                                       llvm::raw_ostream &OS) {
74*9880d681SAndroid Build Coastguard Worker   // Don't recursively invoke the llvm-symbolizer binary.
75*9880d681SAndroid Build Coastguard Worker   if (Argv0.find("llvm-symbolizer") != std::string::npos)
76*9880d681SAndroid Build Coastguard Worker     return false;
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   // FIXME: Subtract necessary number from StackTrace entries to turn return addresses
79*9880d681SAndroid Build Coastguard Worker   // into actual instruction addresses.
80*9880d681SAndroid Build Coastguard Worker   // Use llvm-symbolizer tool to symbolize the stack traces. First look for it
81*9880d681SAndroid Build Coastguard Worker   // alongside our binary, then in $PATH.
82*9880d681SAndroid Build Coastguard Worker   ErrorOr<std::string> LLVMSymbolizerPathOrErr = std::error_code();
83*9880d681SAndroid Build Coastguard Worker   if (!Argv0.empty()) {
84*9880d681SAndroid Build Coastguard Worker     StringRef Parent = llvm::sys::path::parent_path(Argv0);
85*9880d681SAndroid Build Coastguard Worker     if (!Parent.empty())
86*9880d681SAndroid Build Coastguard Worker       LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer", Parent);
87*9880d681SAndroid Build Coastguard Worker   }
88*9880d681SAndroid Build Coastguard Worker   if (!LLVMSymbolizerPathOrErr)
89*9880d681SAndroid Build Coastguard Worker     LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer");
90*9880d681SAndroid Build Coastguard Worker   if (!LLVMSymbolizerPathOrErr)
91*9880d681SAndroid Build Coastguard Worker     return false;
92*9880d681SAndroid Build Coastguard Worker   const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr;
93*9880d681SAndroid Build Coastguard Worker 
94*9880d681SAndroid Build Coastguard Worker   // If we don't know argv0 or the address of main() at this point, try
95*9880d681SAndroid Build Coastguard Worker   // to guess it anyway (it's possible on some platforms).
96*9880d681SAndroid Build Coastguard Worker   std::string MainExecutableName =
97*9880d681SAndroid Build Coastguard Worker       Argv0.empty() ? sys::fs::getMainExecutable(nullptr, nullptr)
98*9880d681SAndroid Build Coastguard Worker                     : (std::string)Argv0;
99*9880d681SAndroid Build Coastguard Worker   BumpPtrAllocator Allocator;
100*9880d681SAndroid Build Coastguard Worker   StringSaver StrPool(Allocator);
101*9880d681SAndroid Build Coastguard Worker   std::vector<const char *> Modules(Depth, nullptr);
102*9880d681SAndroid Build Coastguard Worker   std::vector<intptr_t> Offsets(Depth, 0);
103*9880d681SAndroid Build Coastguard Worker   if (!findModulesAndOffsets(StackTrace, Depth, Modules.data(), Offsets.data(),
104*9880d681SAndroid Build Coastguard Worker                              MainExecutableName.c_str(), StrPool))
105*9880d681SAndroid Build Coastguard Worker     return false;
106*9880d681SAndroid Build Coastguard Worker   int InputFD;
107*9880d681SAndroid Build Coastguard Worker   SmallString<32> InputFile, OutputFile;
108*9880d681SAndroid Build Coastguard Worker   sys::fs::createTemporaryFile("symbolizer-input", "", InputFD, InputFile);
109*9880d681SAndroid Build Coastguard Worker   sys::fs::createTemporaryFile("symbolizer-output", "", OutputFile);
110*9880d681SAndroid Build Coastguard Worker   FileRemover InputRemover(InputFile.c_str());
111*9880d681SAndroid Build Coastguard Worker   FileRemover OutputRemover(OutputFile.c_str());
112*9880d681SAndroid Build Coastguard Worker 
113*9880d681SAndroid Build Coastguard Worker   {
114*9880d681SAndroid Build Coastguard Worker     raw_fd_ostream Input(InputFD, true);
115*9880d681SAndroid Build Coastguard Worker     for (int i = 0; i < Depth; i++) {
116*9880d681SAndroid Build Coastguard Worker       if (Modules[i])
117*9880d681SAndroid Build Coastguard Worker         Input << Modules[i] << " " << (void*)Offsets[i] << "\n";
118*9880d681SAndroid Build Coastguard Worker     }
119*9880d681SAndroid Build Coastguard Worker   }
120*9880d681SAndroid Build Coastguard Worker 
121*9880d681SAndroid Build Coastguard Worker   StringRef InputFileStr(InputFile);
122*9880d681SAndroid Build Coastguard Worker   StringRef OutputFileStr(OutputFile);
123*9880d681SAndroid Build Coastguard Worker   StringRef StderrFileStr;
124*9880d681SAndroid Build Coastguard Worker   const StringRef *Redirects[] = {&InputFileStr, &OutputFileStr,
125*9880d681SAndroid Build Coastguard Worker                                   &StderrFileStr};
126*9880d681SAndroid Build Coastguard Worker   const char *Args[] = {"llvm-symbolizer", "--functions=linkage", "--inlining",
127*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_ON_WIN32
128*9880d681SAndroid Build Coastguard Worker                         // Pass --relative-address on Windows so that we don't
129*9880d681SAndroid Build Coastguard Worker                         // have to add ImageBase from PE file.
130*9880d681SAndroid Build Coastguard Worker                         // FIXME: Make this the default for llvm-symbolizer.
131*9880d681SAndroid Build Coastguard Worker                         "--relative-address",
132*9880d681SAndroid Build Coastguard Worker #endif
133*9880d681SAndroid Build Coastguard Worker                         "--demangle", nullptr};
134*9880d681SAndroid Build Coastguard Worker   int RunResult =
135*9880d681SAndroid Build Coastguard Worker       sys::ExecuteAndWait(LLVMSymbolizerPath, Args, nullptr, Redirects);
136*9880d681SAndroid Build Coastguard Worker   if (RunResult != 0)
137*9880d681SAndroid Build Coastguard Worker     return false;
138*9880d681SAndroid Build Coastguard Worker 
139*9880d681SAndroid Build Coastguard Worker   // This report format is based on the sanitizer stack trace printer.  See
140*9880d681SAndroid Build Coastguard Worker   // sanitizer_stacktrace_printer.cc in compiler-rt.
141*9880d681SAndroid Build Coastguard Worker   auto OutputBuf = MemoryBuffer::getFile(OutputFile.c_str());
142*9880d681SAndroid Build Coastguard Worker   if (!OutputBuf)
143*9880d681SAndroid Build Coastguard Worker     return false;
144*9880d681SAndroid Build Coastguard Worker   StringRef Output = OutputBuf.get()->getBuffer();
145*9880d681SAndroid Build Coastguard Worker   SmallVector<StringRef, 32> Lines;
146*9880d681SAndroid Build Coastguard Worker   Output.split(Lines, "\n");
147*9880d681SAndroid Build Coastguard Worker   auto CurLine = Lines.begin();
148*9880d681SAndroid Build Coastguard Worker   int frame_no = 0;
149*9880d681SAndroid Build Coastguard Worker   for (int i = 0; i < Depth; i++) {
150*9880d681SAndroid Build Coastguard Worker     if (!Modules[i]) {
151*9880d681SAndroid Build Coastguard Worker       OS << '#' << frame_no++ << ' ' << format_ptr(StackTrace[i]) << '\n';
152*9880d681SAndroid Build Coastguard Worker       continue;
153*9880d681SAndroid Build Coastguard Worker     }
154*9880d681SAndroid Build Coastguard Worker     // Read pairs of lines (function name and file/line info) until we
155*9880d681SAndroid Build Coastguard Worker     // encounter empty line.
156*9880d681SAndroid Build Coastguard Worker     for (;;) {
157*9880d681SAndroid Build Coastguard Worker       if (CurLine == Lines.end())
158*9880d681SAndroid Build Coastguard Worker         return false;
159*9880d681SAndroid Build Coastguard Worker       StringRef FunctionName = *CurLine++;
160*9880d681SAndroid Build Coastguard Worker       if (FunctionName.empty())
161*9880d681SAndroid Build Coastguard Worker         break;
162*9880d681SAndroid Build Coastguard Worker       OS << '#' << frame_no++ << ' ' << format_ptr(StackTrace[i]) << ' ';
163*9880d681SAndroid Build Coastguard Worker       if (!FunctionName.startswith("??"))
164*9880d681SAndroid Build Coastguard Worker         OS << FunctionName << ' ';
165*9880d681SAndroid Build Coastguard Worker       if (CurLine == Lines.end())
166*9880d681SAndroid Build Coastguard Worker         return false;
167*9880d681SAndroid Build Coastguard Worker       StringRef FileLineInfo = *CurLine++;
168*9880d681SAndroid Build Coastguard Worker       if (!FileLineInfo.startswith("??"))
169*9880d681SAndroid Build Coastguard Worker         OS << FileLineInfo;
170*9880d681SAndroid Build Coastguard Worker       else
171*9880d681SAndroid Build Coastguard Worker         OS << "(" << Modules[i] << '+' << format_hex(Offsets[i], 0) << ")";
172*9880d681SAndroid Build Coastguard Worker       OS << "\n";
173*9880d681SAndroid Build Coastguard Worker     }
174*9880d681SAndroid Build Coastguard Worker   }
175*9880d681SAndroid Build Coastguard Worker   return true;
176*9880d681SAndroid Build Coastguard Worker }
177*9880d681SAndroid Build Coastguard Worker 
178*9880d681SAndroid Build Coastguard Worker // Include the platform-specific parts of this class.
179*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_ON_UNIX
180*9880d681SAndroid Build Coastguard Worker #include "Unix/Signals.inc"
181*9880d681SAndroid Build Coastguard Worker #endif
182*9880d681SAndroid Build Coastguard Worker #ifdef LLVM_ON_WIN32
183*9880d681SAndroid Build Coastguard Worker #include "Windows/Signals.inc"
184*9880d681SAndroid Build Coastguard Worker #endif
185