Index: test/lit.cfg =================================================================== --- test/lit.cfg +++ test/lit.cfg @@ -194,6 +194,7 @@ config.substitutions.append( ('%shlibext', config.llvm_shlib_ext) ) config.substitutions.append( ('%exeext', config.llvm_exe_ext) ) config.substitutions.append( ('%python', config.python_executable) ) +config.substitutions.append( ('%host_cc', config.host_cc) ) # OCaml substitutions. # Support tests for both native and bytecode builds. @@ -276,6 +277,7 @@ r"\bllvm-split\b", r"\bllvm-tblgen\b", r"\bllvm-c-test\b", + NOJUNK + r"\bllvm-symbolizer\b", NOJUNK + r"\bopt\b", r"\bFileCheck\b", r"\bobj2yaml\b", Index: test/tools/llvm-symbolizer/print_context.c =================================================================== --- /dev/null +++ test/tools/llvm-symbolizer/print_context.c @@ -0,0 +1,22 @@ +// REQUIRES: x86_64-linux +// RUN: %host_cc -O0 -g %s -o %t 2>&1 +// RUN: %t 2>&1 | llvm-symbolizer -print-source-context-lines=5 -obj=%t | FileCheck %s --check-prefix=CHECK + +#include + +int inc(int a) { + return a+1; +} + +int main () { + printf ("%p\n", inc); + return 0; +} + +// CHECK: inc +// CHECK: print_context.c:7 +// CHECK: 5 : #include +// CHECK: 6 : +// CHECK: 7 >: int inc +// CHECK: 8 : return +// CHECK: 9 : } Index: tools/llvm-symbolizer/llvm-symbolizer.cpp =================================================================== --- tools/llvm-symbolizer/llvm-symbolizer.cpp +++ tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -82,6 +82,10 @@ ClPrettyPrint("pretty-print", cl::init(false), cl::desc("Make the output more human friendly")); +static cl::opt ClPrintSourceContextLines( + "print-source-context-lines", cl::init(0), + cl::desc("Print N number of source file context")); + static bool error(std::error_code ec) { if (!ec) return false; @@ -136,6 +140,38 @@ return !StringRef(pos, offset_length).getAsInteger(0, ModuleOffset); } +static void printContextIfNeeded(std::string FileName, uint32_t Line) { + if (ClPrintSourceContextLines <= 0) + return; + + ErrorOr> BufOrErr = + MemoryBuffer::getFile(FileName); + if (!BufOrErr) + return; + + std::unique_ptr Buf = std::move(BufOrErr.get()); + StringRef FileContent = Buf->getBuffer(); + + size_t FirstLine = std::max(1u, Line - ClPrintSourceContextLines / 2); + size_t LastLine = FirstLine + ClPrintSourceContextLines; + size_t MaxLineNumberWidth = std::ceil(std::log10(LastLine)); + + size_t LineStart = 0; + for (uint32_t I = 0; LineStart != StringRef::npos && I <= LastLine; ++I) { + size_t LineEnd = FileContent.find_first_of("\n", LineStart); + if (I + 1 >= FirstLine && I < LastLine) { + outs() << format_decimal(I + 1, MaxLineNumberWidth); + if (I + 1 == Line) { + outs() << " >: "; + } else { + outs() << " : "; + } + outs() << FileContent.substr(LineStart, LineEnd - LineStart) << "\n"; + } + LineStart = FileContent.find_first_not_of("\r", LineEnd + 1); + } +} + int main(int argc, char **argv) { // Print stack trace if we signal out. sys::PrintStackTraceOnErrorSignal(); @@ -176,11 +212,19 @@ Printer << (error(ResOrErr.getError()) ? DIGlobal() : ResOrErr.get()); } else if (ClPrintInlining) { auto ResOrErr = Symbolizer.symbolizeInlinedCode(ModuleName, ModuleOffset); - Printer << (error(ResOrErr.getError()) ? DIInliningInfo() - : ResOrErr.get()); + auto DebugInfo = + error(ResOrErr.getError()) ? DIInliningInfo() : ResOrErr.get(); + Printer << DebugInfo; + for (uint32_t I = 0; I < DebugInfo.getNumberOfFrames(); ++I) { + printContextIfNeeded(DebugInfo.getFrame(I).FileName, + DebugInfo.getFrame(I).Line); + } } else { auto ResOrErr = Symbolizer.symbolizeCode(ModuleName, ModuleOffset); - Printer << (error(ResOrErr.getError()) ? DILineInfo() : ResOrErr.get()); + auto DebugInfo = + (error(ResOrErr.getError()) ? DILineInfo() : ResOrErr.get()); + Printer << DebugInfo; + printContextIfNeeded(DebugInfo.FileName, DebugInfo.Line); } outs() << "\n"; outs().flush();