Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 0eeb3d4

Browse files
committed
Search for llvm-symbolizer binary in the same directory as argv[0], before
looking for it along $PATH. This allows installs of LLVM tools outside of $PATH to find the symbolizer and produce pretty backtraces if they crash. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@272232 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 14f9bce commit 0eeb3d4

File tree

41 files changed

+87
-73
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+87
-73
lines changed

include/llvm/Support/Signals.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ namespace sys {
3838

3939
/// When an error signal (such as SIBABRT or SIGSEGV) is delivered to the
4040
/// process, print a stack trace and then exit.
41-
/// @brief Print a stack trace if a fatal signal occurs.
42-
void PrintStackTraceOnErrorSignal(bool DisableCrashReporting = false);
41+
/// \brief Print a stack trace if a fatal signal occurs.
42+
/// \param Argv0 the current binary name, used to find the symbolizer
43+
/// relative to the current binary before searching $PATH; can be
44+
/// StringRef(), in which case we will only search $PATH.
45+
/// \param DisableCrashReporting if \c true, disable the normal crash
46+
/// reporting mechanisms on the underlying operating system.
47+
void PrintStackTraceOnErrorSignal(StringRef Argv0,
48+
bool DisableCrashReporting = false);
4349

4450
/// Disable all system dialog boxes that appear when the process crashes.
4551
void DisableSystemDialogsOnCrash();

lib/Support/Signals.cpp

+23-11
Original file line numberDiff line numberDiff line change
@@ -62,28 +62,40 @@ static FormattedNumber format_ptr(void *PC) {
6262
return format_hex((uint64_t)PC, PtrWidth);
6363
}
6464

65-
static bool printSymbolizedStackTrace(void **StackTrace, int Depth,
65+
static bool printSymbolizedStackTrace(StringRef Argv0,
66+
void **StackTrace, int Depth,
6667
llvm::raw_ostream &OS)
6768
LLVM_ATTRIBUTE_USED;
6869

6970
/// Helper that launches llvm-symbolizer and symbolizes a backtrace.
70-
static bool printSymbolizedStackTrace(void **StackTrace, int Depth,
71+
static bool printSymbolizedStackTrace(StringRef Argv0,
72+
void **StackTrace, int Depth,
7173
llvm::raw_ostream &OS) {
74+
// Don't recursively invoke the llvm-symbolizer binary.
75+
if (Argv0.find("llvm-symbolizer") != std::string::npos)
76+
return false;
77+
7278
// FIXME: Subtract necessary number from StackTrace entries to turn return addresses
7379
// into actual instruction addresses.
74-
// Use llvm-symbolizer tool to symbolize the stack traces.
75-
ErrorOr<std::string> LLVMSymbolizerPathOrErr =
76-
sys::findProgramByName("llvm-symbolizer");
80+
// Use llvm-symbolizer tool to symbolize the stack traces. First look for it
81+
// alongside our binary, then in $PATH.
82+
ErrorOr<std::string> LLVMSymbolizerPathOrErr = std::error_code();
83+
if (!Argv0.empty()) {
84+
StringRef Parent = llvm::sys::path::parent_path(Argv0);
85+
if (!Parent.empty())
86+
LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer", Parent);
87+
}
88+
if (!LLVMSymbolizerPathOrErr)
89+
LLVMSymbolizerPathOrErr = sys::findProgramByName("llvm-symbolizer");
7790
if (!LLVMSymbolizerPathOrErr)
7891
return false;
7992
const std::string &LLVMSymbolizerPath = *LLVMSymbolizerPathOrErr;
80-
// We don't know argv0 or the address of main() at this point, but try
81-
// to guess it anyway (it's possible on some platforms).
82-
std::string MainExecutableName = sys::fs::getMainExecutable(nullptr, nullptr);
83-
if (MainExecutableName.empty() ||
84-
MainExecutableName.find("llvm-symbolizer") != std::string::npos)
85-
return false;
8693

94+
// If we don't know argv0 or the address of main() at this point, try
95+
// to guess it anyway (it's possible on some platforms).
96+
std::string MainExecutableName =
97+
Argv0.empty() ? sys::fs::getMainExecutable(nullptr, nullptr)
98+
: (std::string)Argv0;
8799
BumpPtrAllocator Allocator;
88100
StringSaver StrPool(Allocator);
89101
std::vector<const char *> Modules(Depth, nullptr);

lib/Support/Unix/Signals.inc

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ static void (*InterruptFunction)() = nullptr;
6868

6969
static ManagedStatic<std::vector<std::string>> FilesToRemove;
7070

71+
static StringRef Argv0;
72+
7173
// IntSigs - Signals that represent requested termination. There's no bug
7274
// or failure, or if there is, it's not our direct responsibility. For whatever
7375
// reason, our continued execution is no longer desirable.
@@ -408,7 +410,7 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS) {
408410
if (!depth)
409411
return;
410412

411-
if (printSymbolizedStackTrace(StackTrace, depth, OS))
413+
if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS))
412414
return;
413415
#if HAVE_DLFCN_H && __GNUG__
414416
int width = 0;
@@ -471,7 +473,10 @@ void llvm::sys::DisableSystemDialogsOnCrash() {}
471473

472474
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIGABRT or
473475
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
474-
void llvm::sys::PrintStackTraceOnErrorSignal(bool DisableCrashReporting) {
476+
void llvm::sys::PrintStackTraceOnErrorSignal(StringRef Argv0,
477+
bool DisableCrashReporting) {
478+
::Argv0 = Argv0;
479+
475480
AddSignalHandler(PrintStackTraceSignalHandler, nullptr);
476481

477482
#if defined(__APPLE__) && defined(ENABLE_CRASH_OVERRIDES)

lib/Support/Windows/Signals.inc

+7-2
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ static PTOP_LEVEL_EXCEPTION_FILTER OldFilter = NULL;
206206
static CRITICAL_SECTION CriticalSection;
207207
static bool CriticalSectionInitialized = false;
208208

209+
static StringRef Argv0;
210+
209211
enum {
210212
#if defined(_M_X64)
211213
NativeMachineType = IMAGE_FILE_MACHINE_AMD64
@@ -240,7 +242,7 @@ static bool printStackTraceWithLLVMSymbolizer(llvm::raw_ostream &OS,
240242
break;
241243
}
242244

243-
return printSymbolizedStackTrace(&StackTrace[0], Depth, OS);
245+
return printSymbolizedStackTrace(Argv0, &StackTrace[0], Depth, OS);
244246
}
245247

246248
namespace {
@@ -496,7 +498,10 @@ void sys::DisableSystemDialogsOnCrash() {
496498

497499
/// PrintStackTraceOnErrorSignal - When an error signal (such as SIBABRT or
498500
/// SIGSEGV) is delivered to the process, print a stack trace and then exit.
499-
void sys::PrintStackTraceOnErrorSignal(bool DisableCrashReporting) {
501+
void sys::PrintStackTraceOnErrorSignal(StringRef Argv0,
502+
bool DisableCrashReporting) {
503+
::Argv0 = Argv0;
504+
500505
if (DisableCrashReporting || getenv("LLVM_DISABLE_CRASH_REPORT"))
501506
Process::PreventCoreFiles();
502507

tools/bugpoint/bugpoint.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void initializePollyPasses(llvm::PassRegistry &Registry);
113113

114114
int main(int argc, char **argv) {
115115
#ifndef DEBUG_BUGPOINT
116-
llvm::sys::PrintStackTraceOnErrorSignal();
116+
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
117117
llvm::PrettyStackTraceProgram X(argc, argv);
118118
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
119119
#endif

tools/dsymutil/dsymutil.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ void llvm::dsymutil::exitDsymutil(int ExitStatus) {
237237
}
238238

239239
int main(int argc, char **argv) {
240-
llvm::sys::PrintStackTraceOnErrorSignal();
240+
llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
241241
llvm::PrettyStackTraceProgram StackPrinter(argc, argv);
242242
llvm::llvm_shutdown_obj Shutdown;
243243
LinkOptions Options;

tools/llc/llc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context) {
202202
// main - Entry point for the llc compiler.
203203
//
204204
int main(int argc, char **argv) {
205-
sys::PrintStackTraceOnErrorSignal();
205+
sys::PrintStackTraceOnErrorSignal(argv[0]);
206206
PrettyStackTraceProgram X(argc, argv);
207207

208208
// Enable debug stream buffering.

tools/lli/lli.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ CodeGenOpt::Level getOptLevel() {
365365
// main Driver function
366366
//
367367
int main(int argc, char **argv, char * const *envp) {
368-
sys::PrintStackTraceOnErrorSignal();
368+
sys::PrintStackTraceOnErrorSignal(argv[0]);
369369
PrettyStackTraceProgram X(argc, argv);
370370

371371
atexit(llvm_shutdown); // Call llvm_shutdown() on exit.

tools/llvm-ar/llvm-ar.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ static int ranlib_main() {
773773
int main(int argc, char **argv) {
774774
ToolName = argv[0];
775775
// Print a stack trace if we signal out.
776-
sys::PrintStackTraceOnErrorSignal();
776+
sys::PrintStackTraceOnErrorSignal(argv[0]);
777777
PrettyStackTraceProgram X(argc, argv);
778778
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
779779

tools/llvm-as/llvm-as.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static void WriteOutputFile(const Module *M) {
8989

9090
int main(int argc, char **argv) {
9191
// Print a stack trace if we signal out.
92-
sys::PrintStackTraceOnErrorSignal();
92+
sys::PrintStackTraceOnErrorSignal(argv[0]);
9393
PrettyStackTraceProgram X(argc, argv);
9494
LLVMContext Context;
9595
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.

tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ static int AnalyzeBitcode() {
915915

916916
int main(int argc, char **argv) {
917917
// Print a stack trace if we signal out.
918-
sys::PrintStackTraceOnErrorSignal();
918+
sys::PrintStackTraceOnErrorSignal(argv[0]);
919919
PrettyStackTraceProgram X(argc, argv);
920920
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
921921
cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");

tools/llvm-cov/CodeCoverage.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@
2626
#include "llvm/Support/CommandLine.h"
2727
#include "llvm/Support/FileSystem.h"
2828
#include "llvm/Support/Format.h"
29-
#include "llvm/Support/ManagedStatic.h"
3029
#include "llvm/Support/Path.h"
31-
#include "llvm/Support/PrettyStackTrace.h"
3230
#include "llvm/Support/Process.h"
33-
#include "llvm/Support/Signals.h"
3431
#include <functional>
3532
#include <system_error>
3633

@@ -240,11 +237,6 @@ std::unique_ptr<CoverageMapping> CodeCoverageTool::load() {
240237
}
241238

242239
int CodeCoverageTool::run(Command Cmd, int argc, const char **argv) {
243-
// Print a stack trace if we signal out.
244-
sys::PrintStackTraceOnErrorSignal();
245-
PrettyStackTraceProgram X(argc, argv);
246-
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
247-
248240
cl::opt<std::string, true> ObjectFilename(
249241
cl::Positional, cl::Required, cl::location(this->ObjectFilename),
250242
cl::desc("Covered executable or object file."));

tools/llvm-cov/TestingSupport.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
#include "llvm/ProfileData/InstrProf.h"
1212
#include "llvm/Support/CommandLine.h"
1313
#include "llvm/Support/LEB128.h"
14-
#include "llvm/Support/ManagedStatic.h"
15-
#include "llvm/Support/PrettyStackTrace.h"
16-
#include "llvm/Support/Signals.h"
1714
#include "llvm/Support/raw_ostream.h"
1815
#include <functional>
1916
#include <system_error>
@@ -22,10 +19,6 @@ using namespace llvm;
2219
using namespace object;
2320

2421
int convertForTestingMain(int argc, const char *argv[]) {
25-
sys::PrintStackTraceOnErrorSignal();
26-
PrettyStackTraceProgram X(argc, argv);
27-
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
28-
2922
cl::opt<std::string> InputSourceFile(cl::Positional, cl::Required,
3023
cl::desc("<Source file>"));
3124

tools/llvm-cov/gcov.cpp

-8
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
#include "llvm/Support/Errc.h"
1717
#include "llvm/Support/FileSystem.h"
1818
#include "llvm/Support/GCOV.h"
19-
#include "llvm/Support/ManagedStatic.h"
2019
#include "llvm/Support/Path.h"
21-
#include "llvm/Support/PrettyStackTrace.h"
22-
#include "llvm/Support/Signals.h"
2320
#include <system_error>
2421
using namespace llvm;
2522

@@ -85,11 +82,6 @@ static void reportCoverage(StringRef SourceFile, StringRef ObjectDir,
8582
}
8683

8784
int gcovMain(int argc, const char *argv[]) {
88-
// Print a stack trace if we signal out.
89-
sys::PrintStackTraceOnErrorSignal();
90-
PrettyStackTraceProgram X(argc, argv);
91-
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
92-
9385
cl::list<std::string> SourceFiles(cl::Positional, cl::OneOrMore,
9486
cl::desc("SOURCEFILE"));
9587

tools/llvm-cov/llvm-cov.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
1414
#include "llvm/ADT/StringRef.h"
1515
#include "llvm/ADT/StringSwitch.h"
1616
#include "llvm/Support/CommandLine.h"
17+
#include "llvm/Support/ManagedStatic.h"
1718
#include "llvm/Support/Path.h"
19+
#include "llvm/Support/PrettyStackTrace.h"
1820
#include "llvm/Support/Process.h"
21+
#include "llvm/Support/Signals.h"
1922
#include "llvm/Support/raw_ostream.h"
2023
#include <string>
2124

@@ -51,6 +54,11 @@ static int versionMain(int argc, const char *argv[]) {
5154
}
5255

5356
int main(int argc, const char **argv) {
57+
// Print a stack trace if we signal out.
58+
sys::PrintStackTraceOnErrorSignal(argv[0]);
59+
PrettyStackTraceProgram X(argc, argv);
60+
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
61+
5462
// If argv[0] is or ends with 'gcov', always be gcov compatible
5563
if (sys::path::stem(argv[0]).endswith_lower("gcov"))
5664
return gcovMain(argc, argv);

tools/llvm-cxxdump/llvm-cxxdump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ static void dumpInput(StringRef File) {
525525
}
526526

527527
int main(int argc, const char *argv[]) {
528-
sys::PrintStackTraceOnErrorSignal();
528+
sys::PrintStackTraceOnErrorSignal(argv[0]);
529529
PrettyStackTraceProgram X(argc, argv);
530530
llvm_shutdown_obj Y;
531531

tools/llvm-dis/llvm-dis.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
134134

135135
int main(int argc, char **argv) {
136136
// Print a stack trace if we signal out.
137-
sys::PrintStackTraceOnErrorSignal();
137+
sys::PrintStackTraceOnErrorSignal(argv[0]);
138138
PrettyStackTraceProgram X(argc, argv);
139139

140140
LLVMContext Context;

tools/llvm-dwarfdump/llvm-dwarfdump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static std::vector<std::string> expandBundle(const std::string &InputPath) {
146146

147147
int main(int argc, char **argv) {
148148
// Print a stack trace if we signal out.
149-
sys::PrintStackTraceOnErrorSignal();
149+
sys::PrintStackTraceOnErrorSignal(argv[0]);
150150
PrettyStackTraceProgram X(argc, argv);
151151
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
152152

tools/llvm-extract/llvm-extract.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static cl::opt<bool> PreserveAssemblyUseListOrder(
102102

103103
int main(int argc, char **argv) {
104104
// Print a stack trace if we signal out.
105-
sys::PrintStackTraceOnErrorSignal();
105+
sys::PrintStackTraceOnErrorSignal(argv[0]);
106106
PrettyStackTraceProgram X(argc, argv);
107107

108108
LLVMContext Context;

tools/llvm-jitlistener/llvm-jitlistener.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ InputFilename(cl::Positional, cl::desc("<input IR file>"),
179179

180180
int main(int argc, char **argv) {
181181
// Print a stack trace if we signal out.
182-
sys::PrintStackTraceOnErrorSignal();
182+
sys::PrintStackTraceOnErrorSignal(argv[0]);
183183
PrettyStackTraceProgram X(argc, argv);
184184
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
185185

tools/llvm-link/llvm-link.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
335335

336336
int main(int argc, char **argv) {
337337
// Print a stack trace if we signal out.
338-
sys::PrintStackTraceOnErrorSignal();
338+
sys::PrintStackTraceOnErrorSignal(argv[0]);
339339
PrettyStackTraceProgram X(argc, argv);
340340

341341
LLVMContext Context;

tools/llvm-lto/llvm-lto.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ class ThinLTOProcessing {
691691

692692
int main(int argc, char **argv) {
693693
// Print a stack trace if we signal out.
694-
sys::PrintStackTraceOnErrorSignal();
694+
sys::PrintStackTraceOnErrorSignal(argv[0]);
695695
PrettyStackTraceProgram X(argc, argv);
696696

697697
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.

tools/llvm-mc/llvm-mc.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ static int AssembleInput(const char *ProgName, const Target *TheTarget,
369369

370370
int main(int argc, char **argv) {
371371
// Print a stack trace if we signal out.
372-
sys::PrintStackTraceOnErrorSignal();
372+
sys::PrintStackTraceOnErrorSignal(argv[0]);
373373
PrettyStackTraceProgram X(argc, argv);
374374
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
375375

tools/llvm-mcmarkup/llvm-mcmarkup.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static void parseMCMarkup(StringRef Filename) {
208208

209209
int main(int argc, char **argv) {
210210
// Print a stack trace if we signal out.
211-
sys::PrintStackTraceOnErrorSignal();
211+
sys::PrintStackTraceOnErrorSignal(argv[0]);
212212
PrettyStackTraceProgram X(argc, argv);
213213

214214
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.

tools/llvm-nm/llvm-nm.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ static void dumpSymbolNamesFromFile(std::string &Filename) {
13471347

13481348
int main(int argc, char **argv) {
13491349
// Print a stack trace if we signal out.
1350-
sys::PrintStackTraceOnErrorSignal();
1350+
sys::PrintStackTraceOnErrorSignal(argv[0]);
13511351
PrettyStackTraceProgram X(argc, argv);
13521352

13531353
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.

tools/llvm-objdump/llvm-objdump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ static void DumpInput(StringRef file) {
17361736

17371737
int main(int argc, char **argv) {
17381738
// Print a stack trace if we signal out.
1739-
sys::PrintStackTraceOnErrorSignal();
1739+
sys::PrintStackTraceOnErrorSignal(argv[0]);
17401740
PrettyStackTraceProgram X(argc, argv);
17411741
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
17421742

tools/llvm-pdbdump/llvm-pdbdump.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ static void dumpInput(StringRef Path) {
433433

434434
int main(int argc_, const char *argv_[]) {
435435
// Print a stack trace if we signal out.
436-
sys::PrintStackTraceOnErrorSignal();
436+
sys::PrintStackTraceOnErrorSignal(argv_[0]);
437437
PrettyStackTraceProgram X(argc_, argv_);
438438

439439
ExitOnErr.setBanner("llvm-pdbdump: ");

0 commit comments

Comments
 (0)