Skip to content

Commit 75f1f15

Browse files
committed
[symbolizer] Change error message if module not found
If llvm-symbolize did not find module, the error looked like: LLVMSymbolizer: error reading file: No such file or directory This message does not follow common practice: LLVMSymbolizer is not an utility name. Also the message did not not contain the name of missed file. With this change the error message looks differently: llvm-symbolizer: error: 'abc': No such file or directory This format is closer to messages produced by other utilities and allow proper coloring. Differential Revision: https://reviews.llvm.org/D148032
1 parent a39b807 commit 75f1f15

File tree

9 files changed

+42
-35
lines changed

9 files changed

+42
-35
lines changed

llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ class DIPrinter {
5151
StringRef Command) = 0;
5252

5353
virtual bool printError(const Request &Request,
54-
const ErrorInfoBase &ErrorInfo,
55-
StringRef ErrorBanner) = 0;
54+
const ErrorInfoBase &ErrorInfo) = 0;
5655

5756
virtual void listBegin() = 0;
5857
virtual void listEnd() = 0;
@@ -66,10 +65,12 @@ struct PrinterConfig {
6665
int SourceContextLines;
6766
};
6867

68+
using ErrorHandler = function_ref<void(const ErrorInfoBase &, StringRef)>;
69+
6970
class PlainPrinterBase : public DIPrinter {
7071
protected:
7172
raw_ostream &OS;
72-
raw_ostream &ES;
73+
ErrorHandler ErrHandler;
7374
PrinterConfig Config;
7475

7576
void print(const DILineInfo &Info, bool Inlined);
@@ -85,8 +86,8 @@ class PlainPrinterBase : public DIPrinter {
8586
void printHeader(uint64_t Address);
8687

8788
public:
88-
PlainPrinterBase(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
89-
: OS(OS), ES(ES), Config(Config) {}
89+
PlainPrinterBase(raw_ostream &OS, ErrorHandler EH, PrinterConfig &Config)
90+
: OS(OS), ErrHandler(EH), Config(Config) {}
9091

9192
void print(const Request &Request, const DILineInfo &Info) override;
9293
void print(const Request &Request, const DIInliningInfo &Info) override;
@@ -96,8 +97,8 @@ class PlainPrinterBase : public DIPrinter {
9697

9798
void printInvalidCommand(const Request &Request, StringRef Command) override;
9899

99-
bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
100-
StringRef ErrorBanner) override;
100+
bool printError(const Request &Request,
101+
const ErrorInfoBase &ErrorInfo) override;
101102

102103
void listBegin() override {}
103104
void listEnd() override {}
@@ -110,17 +111,18 @@ class LLVMPrinter : public PlainPrinterBase {
110111
void printFooter() override;
111112

112113
public:
113-
LLVMPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
114-
: PlainPrinterBase(OS, ES, Config) {}
114+
LLVMPrinter(raw_ostream &OS, ErrorHandler EH, PrinterConfig &Config)
115+
: PlainPrinterBase(OS, EH, Config) {}
115116
};
116117

117118
class GNUPrinter : public PlainPrinterBase {
118119
private:
119120
void printSimpleLocation(StringRef Filename, const DILineInfo &Info) override;
120121

121122
public:
122-
GNUPrinter(raw_ostream &OS, raw_ostream &ES, PrinterConfig &Config)
123-
: PlainPrinterBase(OS, ES, Config) {}
123+
GNUPrinter(raw_ostream &OS, ErrorHandler EH, PrinterConfig &Config)
124+
: PlainPrinterBase(OS, EH, Config) {}
125+
124126
};
125127

126128
class JSONPrinter : public DIPrinter {
@@ -147,8 +149,8 @@ class JSONPrinter : public DIPrinter {
147149

148150
void printInvalidCommand(const Request &Request, StringRef Command) override;
149151

150-
bool printError(const Request &Request, const ErrorInfoBase &ErrorInfo,
151-
StringRef ErrorBanner) override;
152+
bool printError(const Request &Request,
153+
const ErrorInfoBase &ErrorInfo) override;
152154

153155
void listBegin() override;
154156
void listEnd() override;

llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,8 @@ void PlainPrinterBase::printInvalidCommand(const Request &Request,
266266
}
267267

268268
bool PlainPrinterBase::printError(const Request &Request,
269-
const ErrorInfoBase &ErrorInfo,
270-
StringRef ErrorBanner) {
271-
ES << ErrorBanner;
272-
ErrorInfo.log(ES);
273-
ES << '\n';
269+
const ErrorInfoBase &ErrorInfo) {
270+
ErrHandler(ErrorInfo, Request.ModuleName);
274271
// Print an empty struct too.
275272
return true;
276273
}
@@ -374,13 +371,11 @@ void JSONPrinter::printInvalidCommand(const Request &Request,
374371
StringRef Command) {
375372
printError(Request,
376373
StringError("unable to parse arguments: " + Command,
377-
std::make_error_code(std::errc::invalid_argument)),
378-
"");
374+
std::make_error_code(std::errc::invalid_argument)));
379375
}
380376

381377
bool JSONPrinter::printError(const Request &Request,
382-
const ErrorInfoBase &ErrorInfo,
383-
StringRef ErrorBanner) {
378+
const ErrorInfoBase &ErrorInfo) {
384379
json::Object Json = toJSON(Request, ErrorInfo.message());
385380
if (ObjectList)
386381
ObjectList->push_back(std::move(Json));

llvm/lib/DebugInfo/Symbolize/Symbolize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -632,8 +632,7 @@ LLVMSymbolizer::getOrCreateModuleInfo(ArrayRef<uint8_t> BuildID) {
632632
std::string Path;
633633
if (!getOrFindDebugBinary(BuildID, Path)) {
634634
return createStringError(errc::no_such_file_or_directory,
635-
Twine("could not find build ID '") +
636-
toHex(BuildID) + "'");
635+
"could not find build ID");
637636
}
638637
return getOrCreateModuleInfo(Path);
639638
}

llvm/test/DebugInfo/Symbolize/ELF/symtab-file2.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Symbols:
4949
# RUN: yaml2obj --docnum=2 %s -o %t2
5050
# RUN: llvm-symbolizer --obj=%t2 0 0 2>&1 | FileCheck %s --check-prefix=CHECK2
5151

52-
# CHECK2: error reading file: st_name (0xffff) is past the end of the string table of size
52+
# CHECK2: llvm-symbolizer{{.*}}: error: '{{.*}}symtab-file2.yaml.tmp2': st_name (0xffff) is past the end of the string table of size
5353
# CHECK2-NEXT: ??
5454
# CHECK2-NEXT: ??:0:0
5555
# CHECK2-EMPTY:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUN: llvm-symbolizer --obj=unexisting-file 0x1234 2>&1 | FileCheck -DMSG=%errc_ENOENT %s
22

3-
CHECK: LLVMSymbolizer: error reading file: [[MSG]]
3+
CHECK: llvm-symbolizer{{.*}}: error: 'unexisting-file': [[MSG]]

llvm/test/tools/llvm-symbolizer/debuginfod-missing-build-id.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ STDOUT: ??:0:0
77
STDOUT: ??
88
STDOUT: ??:0:0
99

10-
STDERR-COUNT-2: LLVMSymbolizer: error reading file: could not find build ID 'ABAD'
10+
STDERR-COUNT-2: llvm-symbolizer{{.*}}: error: 'ABAD': could not find build ID

llvm/test/tools/llvm-symbolizer/output-style-inlined.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ RUN: | FileCheck %s --check-prefix=NOT-EXIST-GNU -DMSG=%errc_ENOENT
3737
RUN: llvm-symbolizer --output-style=LLVM --obj=%p/Inputs/not.exist 0x1 0x2 --no-inlines 2>&1 \
3838
RUN: | FileCheck %s --check-prefix=NOT-EXIST-LLVM -DMSG=%errc_ENOENT
3939

40-
# NOT-EXIST-GNU: LLVMSymbolizer: error reading file: [[MSG]]
40+
# NOT-EXIST-GNU: llvm-symbolizer{{.*}}: error: '{{.*}}Inputs/not.exist': [[MSG]]
4141
# NOT-EXIST-GNU-NEXT: ??
4242
# NOT-EXIST-GNU-NEXT: ??:0
4343
# NOT-EXIST-GNU-NEXT: ??
4444
# NOT-EXIST-GNU-NEXT: ??:0
4545

46-
# NOT-EXIST-LLVM: LLVMSymbolizer: error reading file: [[MSG]]
46+
# NOT-EXIST-LLVM: llvm-symbolizer{{.*}}: error: '{{.*}}Inputs/not.exist': [[MSG]]
4747
# NOT-EXIST-LLVM-NEXT: ??
4848
# NOT-EXIST-LLVM-NEXT: ??:0:0
4949
# NOT-EXIST-LLVM-EMPTY:

llvm/test/tools/llvm-symbolizer/pdb/missing_pdb.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN: FileCheck -DMSG=%errc_ENOENT --check-prefix=ERROR %s < %t.err
44

55
llvm-symbolizer should print one error and two unknown line info records.
66

7-
ERROR: LLVMSymbolizer: error reading file: {{.*}}: [[MSG]]
7+
ERROR: llvm-symbolizer{{.*}}: error: '{{.*}}missing_pdb.pdb': [[MSG]]
88
ERROR-NOT: error reading file
99

1010
CHECK: ??

llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "llvm/Support/InitLLVM.h"
3737
#include "llvm/Support/Path.h"
3838
#include "llvm/Support/StringSaver.h"
39+
#include "llvm/Support/WithColor.h"
3940
#include "llvm/Support/raw_ostream.h"
4041
#include <algorithm>
4142
#include <cstdio>
@@ -83,6 +84,16 @@ class SymbolizerOptTable : public opt::GenericOptTable {
8384
};
8485
} // namespace
8586

87+
static std::string ToolName;
88+
89+
static void printError(const ErrorInfoBase &EI, StringRef Path) {
90+
WithColor::error(errs(), ToolName);
91+
if (!EI.isA<FileError>())
92+
errs() << "'" << Path << "': ";
93+
EI.log(errs());
94+
errs() << '\n';
95+
}
96+
8697
template <typename T>
8798
static void print(const Request &Request, Expected<T> &ResOrErr,
8899
DIPrinter &Printer) {
@@ -96,8 +107,7 @@ static void print(const Request &Request, Expected<T> &ResOrErr,
96107
bool PrintEmpty = true;
97108
handleAllErrors(std::move(ResOrErr.takeError()),
98109
[&](const ErrorInfoBase &EI) {
99-
PrintEmpty = Printer.printError(
100-
Request, EI, "LLVMSymbolizer: error reading file: ");
110+
PrintEmpty = Printer.printError(Request, EI);
101111
});
102112

103113
if (PrintEmpty)
@@ -378,7 +388,8 @@ int main(int argc, char **argv) {
378388
InitLLVM X(argc, argv);
379389
sys::InitializeCOMRAII COM(sys::COMThreadingMode::MultiThreaded);
380390

381-
bool IsAddr2Line = sys::path::stem(argv[0]).contains("addr2line");
391+
ToolName = argv[0];
392+
bool IsAddr2Line = sys::path::stem(ToolName).contains("addr2line");
382393
BumpPtrAllocator A;
383394
StringSaver Saver(A);
384395
SymbolizerOptTable Tbl;
@@ -461,11 +472,11 @@ int main(int argc, char **argv) {
461472

462473
std::unique_ptr<DIPrinter> Printer;
463474
if (Style == OutputStyle::GNU)
464-
Printer = std::make_unique<GNUPrinter>(outs(), errs(), Config);
475+
Printer = std::make_unique<GNUPrinter>(outs(), printError, Config);
465476
else if (Style == OutputStyle::JSON)
466477
Printer = std::make_unique<JSONPrinter>(outs(), Config);
467478
else
468-
Printer = std::make_unique<LLVMPrinter>(outs(), errs(), Config);
479+
Printer = std::make_unique<LLVMPrinter>(outs(), printError, Config);
469480

470481
std::vector<std::string> InputAddresses = Args.getAllArgValues(OPT_INPUT);
471482
if (InputAddresses.empty()) {

0 commit comments

Comments
 (0)