diff --git a/llvm/docs/CommandGuide/llvm-symbolizer.rst b/llvm/docs/CommandGuide/llvm-symbolizer.rst --- a/llvm/docs/CommandGuide/llvm-symbolizer.rst +++ b/llvm/docs/CommandGuide/llvm-symbolizer.rst @@ -236,7 +236,7 @@ .. _llvm-symbolizer-opt-output-style: -.. option:: --output-style +.. option:: --output-style Specify the preferred output style. Defaults to ``LLVM``. When the output style is set to ``GNU``, the tool follows the style of GNU's **addr2line**. @@ -253,6 +253,8 @@ * Prints an address's debug-data discriminator when it is non-zero. One way to produce discriminators is to compile with clang's -fdebug-info-for-profiling. + ``JSON`` style provides a machine readable output in JSON. + .. code-block:: console $ llvm-symbolizer --obj=inlined.elf 0x4004be 0x400486 -p @@ -270,6 +272,13 @@ baz() at /tmp/test.cpp:11 foo() at /tmp/test.cpp:6 + $ llvm-symbolizer --output-style=JSON --obj=inlined.elf 0x4004be 0x400486 + {"Frames":[ + {"FunctionName":"baz()","StartFileName":"/tmp/test.cpp","StartLine":9,"FileName":"/tmp/test.cpp","Line":11,"Column":18}, + {"FunctionName":"main","StartFileName":"/tmp/test.cpp","StartLine":14,"FileName":"/tmp/test.cpp","Line":15}]} + {"Frames":[ + {"FunctionName":"foo()","StartFileName":"/tmp/test.cpp","StartLine":5,"FileName":"/tmp/test.cpp","Line": 6,"Column": 3}]} + $ clang -g -fdebug-info-for-profiling test.cpp -o profiling.elf $ llvm-symbolizer --output-style=GNU --obj=profiling.elf 0x401167 -p --no-inlines main at /tmp/test.cpp:15 (discriminator 2) diff --git a/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h b/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h --- a/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h +++ b/llvm/include/llvm/DebugInfo/Symbolize/DIPrinter.h @@ -15,19 +15,21 @@ #define LLVM_DEBUGINFO_SYMBOLIZE_DIPRINTER_H #include +#include namespace llvm { struct DILineInfo; class DIInliningInfo; struct DIGlobal; struct DILocal; +class ErrorInfoBase; class raw_ostream; namespace symbolize { class DIPrinter { public: - enum class OutputStyle { LLVM, GNU }; + enum class OutputStyle { LLVM, GNU, JSON }; private: raw_ostream &OS; @@ -51,7 +53,8 @@ DIPrinter &operator<<(const DILineInfo &Info); DIPrinter &operator<<(const DIInliningInfo &Info); DIPrinter &operator<<(const DIGlobal &Global); - DIPrinter &operator<<(const DILocal &Local); + DIPrinter &operator<<(const std::vector &Locals); + DIPrinter &operator<<(const ErrorInfoBase &EI); }; } } diff --git a/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp b/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp --- a/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp +++ b/llvm/lib/DebugInfo/Symbolize/DIPrinter.cpp @@ -16,6 +16,7 @@ #include "llvm/DebugInfo/DIContext.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/Format.h" +#include "llvm/Support/JSON.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" @@ -30,6 +31,27 @@ namespace llvm { namespace symbolize { +void toJSON(json::OStream &J, const DILineInfo &Info) { + J.objectBegin(); + if (Info.Source) + J.attribute("Source", *Info.Source); + if (Info.FunctionName != DILineInfo::BadString) + J.attribute("FunctionName", Info.FunctionName); + if (Info.StartFileName != DILineInfo::BadString) + J.attribute("StartFileName", Info.StartFileName); + if (Info.StartLine) + J.attribute("StartLine", Info.StartLine); + if (Info.FileName != DILineInfo::BadString) + J.attribute("FileName", Info.FileName); + J.attribute("Line", Info.Line); + // Print only if column is valid to reduce a noise in the output + if (Info.Column) + J.attribute("Column", Info.Column); + if (Info.Discriminator) + J.attribute("Discriminator", Info.Discriminator); + J.objectEnd(); +} + // Prints source code around in the FileName the Line. void DIPrinter::printContext(const std::string &FileName, int64_t Line) { if (PrintSourceContext <= 0) @@ -95,12 +117,36 @@ } DIPrinter &DIPrinter::operator<<(const DILineInfo &Info) { + if (Style == OutputStyle::JSON) { + json::OStream J(OS); + toJSON(J, Info); + OS << '\n'; + return *this; + } + print(Info, false); return *this; } DIPrinter &DIPrinter::operator<<(const DIInliningInfo &Info) { uint32_t FramesNum = Info.getNumberOfFrames(); + + if (Style == OutputStyle::JSON) { + json::OStream J(OS); + J.objectBegin(); + J.attributeBegin("Frames"); + J.arrayBegin(); + for (uint32_t I = 0; I < FramesNum; ++I) { + OS << '\n'; + toJSON(J, Info.getFrame(I)); + } + J.arrayEnd(); + J.attributeEnd(); + J.objectEnd(); + OS << '\n'; + return *this; + } + if (FramesNum == 0) { print(DILineInfo(), false); return *this; @@ -111,6 +157,18 @@ } DIPrinter &DIPrinter::operator<<(const DIGlobal &Global) { + if (Style == OutputStyle::JSON) { + json::OStream J(OS); + J.objectBegin(); + if (Global.Name != DILineInfo::BadString) + J.attribute("Name", Global.Name); + J.attribute("Start", int64_t(Global.Start)); + J.attribute("Size", int64_t(Global.Size)); + J.objectEnd(); + OS << '\n'; + return *this; + } + std::string Name = Global.Name; if (Name == DILineInfo::BadString) Name = DILineInfo::Addr2LineBadString; @@ -119,39 +177,87 @@ return *this; } -DIPrinter &DIPrinter::operator<<(const DILocal &Local) { - if (Local.FunctionName.empty()) - OS << "??\n"; - else - OS << Local.FunctionName << '\n'; - - if (Local.Name.empty()) - OS << "??\n"; - else - OS << Local.Name << '\n'; - - if (Local.DeclFile.empty()) - OS << "??"; - else - OS << Local.DeclFile; - OS << ':' << Local.DeclLine << '\n'; - - if (Local.FrameOffset) - OS << *Local.FrameOffset << ' '; - else - OS << "?? "; - - if (Local.Size) - OS << *Local.Size << ' '; - else - OS << "?? "; - - if (Local.TagOffset) - OS << *Local.TagOffset << '\n'; - else - OS << "??\n"; +DIPrinter &DIPrinter::operator<<(const std::vector &Locals) { + if (Style == OutputStyle::JSON) { + json::OStream J(OS); + J.arrayBegin(); + for (const DILocal &L : Locals) { + OS << '\n'; + J.objectBegin(); + if (!L.FunctionName.empty()) + J.attribute("FunctionName", L.FunctionName); + if (!L.Name.empty()) + J.attribute("Name", L.Name); + if (!L.DeclFile.empty()) + J.attribute("DeclFile", L.DeclFile); + J.attribute("DeclLine", int64_t(L.DeclLine)); + if (L.FrameOffset) + J.attribute("FrameOffset", *L.FrameOffset); + if (L.Size) + J.attribute("Size", int64_t(*L.Size)); + if (L.TagOffset) + J.attribute("TagOffset", int64_t(*L.TagOffset)); + J.objectEnd(); + } + J.arrayEnd(); + OS << '\n'; + return *this; + } + + if (Locals.empty()) { + outs() << "??\n"; + return *this; + } + + for (const DILocal &Local : Locals) { + if (Local.FunctionName.empty()) + OS << "??\n"; + else + OS << Local.FunctionName << '\n'; + + if (Local.Name.empty()) + OS << "??\n"; + else + OS << Local.Name << '\n'; + + if (Local.DeclFile.empty()) + OS << "??"; + else + OS << Local.DeclFile; + OS << ':' << Local.DeclLine << '\n'; + + if (Local.FrameOffset) + OS << *Local.FrameOffset << ' '; + else + OS << "?? "; + + if (Local.Size) + OS << *Local.Size << ' '; + else + OS << "?? "; + + if (Local.TagOffset) + OS << *Local.TagOffset << '\n'; + else + OS << "??\n"; + } return *this; } +DIPrinter &DIPrinter::operator<<(const ErrorInfoBase &EI) { + if (Style == OutputStyle::JSON) { + json::OStream J(OS); + J.objectBegin(); + J.attributeObject("Error", [&] { + J.attribute("Code", int64_t(EI.convertToErrorCode().value())); + J.attribute("Message", EI.message()); + }); + J.objectEnd(); + OS << '\n'; + return *this; + } + llvm_unreachable("Method not implemented"); +} + } // end namespace symbolize } // end namespace llvm diff --git a/llvm/test/tools/llvm-symbolizer/output-style-json-data.test b/llvm/test/tools/llvm-symbolizer/output-style-json-data.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/output-style-json-data.test @@ -0,0 +1,36 @@ +## This test checks JSON output for DATA. + +# REQUIRES: x86-registered-target + +# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o + +## Test JSON output style of DIGlobal in case of error. +# RUN: llvm-symbolizer "DATA %t.o Z" --output-style=JSON \ +# RUN: | FileCheck %s --check-prefix=INVARG --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# INVARG:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} + +## Test JSON output style of DIGlobal in case of error. +# RUN: llvm-symbolizer "DATA %t-no-file.o 0" --output-style=JSON \ +# RUN: | FileCheck %s --check-prefix=NO-FILE --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NO-FILE:{"Error":{"Code":2,"Message":"{{N|n}}o such file or directory"}} + +## Test JSON output style of empty DIGlobal. +# RUN: llvm-symbolizer "DATA %t.o 1000000" --output-style=JSON \ +# RUN: | FileCheck %s --check-prefix=NOT-FOUND --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NOT-FOUND:{"Start":0,"Size":0} + +## Test JSON output style of DIGlobal. +# RUN: llvm-symbolizer "DATA %t.o 0" --output-style=JSON \ +# RUN: | FileCheck %s --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# CHECK:{"Name":"foo","Start":0,"Size":4} + +.data +.globl foo +.type foo, @object +.size foo, 4 +foo = . + 0x1100000000000000 +.4byte 1 diff --git a/llvm/test/tools/llvm-symbolizer/output-style-json-frame.test b/llvm/test/tools/llvm-symbolizer/output-style-json-frame.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/output-style-json-frame.test @@ -0,0 +1,520 @@ +## This test checks JSON output for FRAME. + +# REQUIRES: x86-registered-target + +# RUN: llvm-mc -filetype=obj -triple=i386-linux-gnu -o %t.o %s + +## Test JSON output style of DILocal in case of error. +# RUN: llvm-symbolizer "FRAME %t.o Z" --output-style=JSON \ +# RUN: | FileCheck %s --check-prefix=INVARG --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# INVARG:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} + +## Test JSON output style of DILocal in case of error. +# RUN: llvm-symbolizer "FRAME %t-no-file.o 0" --output-style=JSON \ +# RUN: | FileCheck %s --check-prefix=NO-FILE --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NO-FILE:{"Error":{"Code":2,"Message":"{{N|n}}o such file or directory"}} + +## Test JSON output style of empty DILocal. +# RUN: llvm-symbolizer "FRAME %t.o 1000000" --output-style=JSON \ +# RUN: | FileCheck %s --check-prefix=NOT-FOUND --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NOT-FOUND:[] + +## Test JSON output style of DILocal. +# RUN: llvm-symbolizer "FRAME %t.o 0" --output-style=JSON \ +# RUN: | FileCheck %s --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# CHECK:[ +# CHECK-NEXT:{"FunctionName":"f","Name":"a","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":4,"FrameOffset":-1,"Size":1} +# CHECK-NEXT:,{"FunctionName":"f","Name":"b","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":5,"FrameOffset":-8,"Size":4} +# CHECK-NEXT:,{"FunctionName":"f","Name":"c","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":6,"FrameOffset":-12,"Size":4} +# CHECK-NEXT:,{"FunctionName":"f","Name":"d","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":7,"FrameOffset":-16,"Size":4} +# CHECK-NEXT:,{"FunctionName":"f","Name":"e","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":8,"FrameOffset":-32,"Size":8} +# CHECK-NEXT:,{"FunctionName":"f","Name":"f","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":9,"FrameOffset":-36,"Size":4} +# CHECK-NEXT:,{"FunctionName":"f","Name":"g","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":10,"FrameOffset":-37,"Size":1} +# CHECK-NEXT:,{"FunctionName":"f","Name":"h","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":11,"FrameOffset":-38,"Size":1} +# CHECK-NEXT:,{"FunctionName":"f","Name":"i","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":12,"FrameOffset":-44,"Size":4} +# CHECK-NEXT:,{"FunctionName":"f","Name":"j","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":14,"FrameOffset":-45,"Size":1} +# CHECK-NEXT:,{"FunctionName":"f","Name":"k","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":15,"FrameOffset":-57,"Size":12} +# CHECK-NEXT:,{"FunctionName":"f","Name":"l","DeclFile":"/tmp{{/|\\\\}}frame-types.cpp","DeclLine":16,"FrameOffset":-345,"Size":288}] + +## Generated from: +## +## struct S; +## +## void f() { +## char a; +## char *b; +## char &c = a; +## char &&d = 1; +## char (S::*e)(); +## char S::*f; +## const char g = 2; +## volatile char h; +## char *__restrict i; +## typedef char char_typedef; +## char_typedef j; +## char k[12]; +## char l[12][24]; +## } +## +## clang++ --target=i386-linux-gnu frame-types.cpp -g -std=c++11 -S -o frame-types.s + + .text + .file "frame-types.cpp" + .globl _Z1fv # -- Begin function _Z1fv + .p2align 4, 0x90 + .type _Z1fv,@function +_Z1fv: # @_Z1fv +.Lfunc_begin0: + .file 1 "/tmp" "frame-types.cpp" + .loc 1 3 0 # frame-types.cpp:3:0 + .cfi_sections .debug_frame + .cfi_startproc +# %bb.0: # %entry + pushl %ebp + .cfi_def_cfa_offset 8 + .cfi_offset %ebp, -8 + movl %esp, %ebp + .cfi_def_cfa_register %ebp + subl $352, %esp # imm = 0x160 +.Ltmp0: + .loc 1 6 9 prologue_end # frame-types.cpp:6:9 + leal -1(%ebp), %eax +.Ltmp1: + #DEBUG_VALUE: f:a <- [$eax+0] + movl %eax, -12(%ebp) + .loc 1 7 14 # frame-types.cpp:7:14 + movb $1, -17(%ebp) + .loc 1 7 10 is_stmt 0 # frame-types.cpp:7:10 + leal -17(%ebp), %eax +.Ltmp2: + movl %eax, -16(%ebp) + .loc 1 10 14 is_stmt 1 # frame-types.cpp:10:14 + movb $2, -37(%ebp) + .loc 1 17 1 # frame-types.cpp:17:1 + addl $352, %esp # imm = 0x160 + popl %ebp + .cfi_def_cfa %esp, 4 + retl +.Ltmp3: +.Lfunc_end0: + .size _Z1fv, .Lfunc_end0-_Z1fv + .cfi_endproc + # -- End function + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 9.0.0 " # string offset=0 +.Linfo_string1: + .asciz "frame-types.cpp" # string offset=21 +.Linfo_string2: + .asciz "/tmp" # string offset=37 +.Linfo_string3: + .asciz "_Z1fv" # string offset=42 +.Linfo_string4: + .asciz "f" # string offset=48 +.Linfo_string5: + .asciz "a" # string offset=50 +.Linfo_string6: + .asciz "char" # string offset=52 +.Linfo_string7: + .asciz "b" # string offset=57 +.Linfo_string8: + .asciz "c" # string offset=59 +.Linfo_string9: + .asciz "d" # string offset=61 +.Linfo_string10: + .asciz "e" # string offset=63 +.Linfo_string11: + .asciz "S" # string offset=65 +.Linfo_string12: + .asciz "g" # string offset=67 +.Linfo_string13: + .asciz "h" # string offset=69 +.Linfo_string14: + .asciz "i" # string offset=71 +.Linfo_string15: + .asciz "j" # string offset=73 +.Linfo_string16: + .asciz "char_typedef" # string offset=75 +.Linfo_string17: + .asciz "k" # string offset=88 +.Linfo_string18: + .asciz "__ARRAY_SIZE_TYPE__" # string offset=90 +.Linfo_string19: + .asciz "l" # string offset=110 + .section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 1 # DW_CHILDREN_yes + .byte 37 # DW_AT_producer + .byte 14 # DW_FORM_strp + .byte 19 # DW_AT_language + .byte 5 # DW_FORM_data2 + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 27 # DW_AT_comp_dir + .byte 14 # DW_FORM_strp + .byte 17 # DW_AT_low_pc + .byte 1 # DW_FORM_addr + .byte 18 # DW_AT_high_pc + .byte 6 # DW_FORM_data4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 2 # Abbreviation Code + .byte 46 # DW_TAG_subprogram + .byte 1 # DW_CHILDREN_yes + .byte 17 # DW_AT_low_pc + .byte 1 # DW_FORM_addr + .byte 18 # DW_AT_high_pc + .byte 6 # DW_FORM_data4 + .byte 64 # DW_AT_frame_base + .byte 24 # DW_FORM_exprloc + .byte 110 # DW_AT_linkage_name + .byte 14 # DW_FORM_strp + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 58 # DW_AT_decl_file + .byte 11 # DW_FORM_data1 + .byte 59 # DW_AT_decl_line + .byte 11 # DW_FORM_data1 + .byte 63 # DW_AT_external + .byte 25 # DW_FORM_flag_present + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 3 # Abbreviation Code + .byte 52 # DW_TAG_variable + .byte 0 # DW_CHILDREN_no + .byte 2 # DW_AT_location + .byte 24 # DW_FORM_exprloc + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 58 # DW_AT_decl_file + .byte 11 # DW_FORM_data1 + .byte 59 # DW_AT_decl_line + .byte 11 # DW_FORM_data1 + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 4 # Abbreviation Code + .byte 22 # DW_TAG_typedef + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 58 # DW_AT_decl_file + .byte 11 # DW_FORM_data1 + .byte 59 # DW_AT_decl_line + .byte 11 # DW_FORM_data1 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 5 # Abbreviation Code + .byte 36 # DW_TAG_base_type + .byte 0 # DW_CHILDREN_no + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 62 # DW_AT_encoding + .byte 11 # DW_FORM_data1 + .byte 11 # DW_AT_byte_size + .byte 11 # DW_FORM_data1 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 6 # Abbreviation Code + .byte 15 # DW_TAG_pointer_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 7 # Abbreviation Code + .byte 16 # DW_TAG_reference_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 8 # Abbreviation Code + .byte 66 # DW_TAG_rvalue_reference_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 9 # Abbreviation Code + .byte 31 # DW_TAG_ptr_to_member_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 29 # DW_AT_containing_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 10 # Abbreviation Code + .byte 21 # DW_TAG_subroutine_type + .byte 1 # DW_CHILDREN_yes + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 11 # Abbreviation Code + .byte 5 # DW_TAG_formal_parameter + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 52 # DW_AT_artificial + .byte 25 # DW_FORM_flag_present + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 12 # Abbreviation Code + .byte 19 # DW_TAG_structure_type + .byte 0 # DW_CHILDREN_no + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 60 # DW_AT_declaration + .byte 25 # DW_FORM_flag_present + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 13 # Abbreviation Code + .byte 38 # DW_TAG_const_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 14 # Abbreviation Code + .byte 53 # DW_TAG_volatile_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 15 # Abbreviation Code + .byte 55 # DW_TAG_restrict_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 16 # Abbreviation Code + .byte 1 # DW_TAG_array_type + .byte 1 # DW_CHILDREN_yes + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 17 # Abbreviation Code + .byte 33 # DW_TAG_subrange_type + .byte 0 # DW_CHILDREN_no + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 55 # DW_AT_count + .byte 11 # DW_FORM_data1 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 18 # Abbreviation Code + .byte 36 # DW_TAG_base_type + .byte 0 # DW_CHILDREN_no + .byte 3 # DW_AT_name + .byte 14 # DW_FORM_strp + .byte 11 # DW_AT_byte_size + .byte 11 # DW_FORM_data1 + .byte 62 # DW_AT_encoding + .byte 11 # DW_FORM_data1 + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + .section .debug_info,"",@progbits +.Lcu_begin0: + .long .Ldebug_info_end0-.Ldebug_info_start0 # Length of Unit +.Ldebug_info_start0: + .short 4 # DWARF version number + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 4 # Address Size (in bytes) + .byte 1 # Abbrev [1] 0xb:0x157 DW_TAG_compile_unit + .long .Linfo_string0 # DW_AT_producer + .short 4 # DW_AT_language + .long .Linfo_string1 # DW_AT_name + .long .Lline_table_start0 # DW_AT_stmt_list + .long .Linfo_string2 # DW_AT_comp_dir + .long .Lfunc_begin0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc + .byte 2 # Abbrev [2] 0x26:0xca DW_TAG_subprogram + .long .Lfunc_begin0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc + .byte 1 # DW_AT_frame_base + .byte 85 + .long .Linfo_string3 # DW_AT_linkage_name + .long .Linfo_string4 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 3 # DW_AT_decl_line + # DW_AT_external + .byte 3 # Abbrev [3] 0x3b:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 127 + .long .Linfo_string5 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 4 # DW_AT_decl_line + .long 240 # DW_AT_type + .byte 3 # Abbrev [3] 0x49:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 120 + .long .Linfo_string7 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 5 # DW_AT_decl_line + .long 247 # DW_AT_type + .byte 3 # Abbrev [3] 0x57:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 116 + .long .Linfo_string8 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 6 # DW_AT_decl_line + .long 252 # DW_AT_type + .byte 3 # Abbrev [3] 0x65:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 112 + .long .Linfo_string9 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 7 # DW_AT_decl_line + .long 257 # DW_AT_type + .byte 3 # Abbrev [3] 0x73:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 96 + .long .Linfo_string10 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 8 # DW_AT_decl_line + .long 262 # DW_AT_type + .byte 3 # Abbrev [3] 0x81:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 92 + .long .Linfo_string4 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 9 # DW_AT_decl_line + .long 292 # DW_AT_type + .byte 3 # Abbrev [3] 0x8f:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 91 + .long .Linfo_string12 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 10 # DW_AT_decl_line + .long 301 # DW_AT_type + .byte 3 # Abbrev [3] 0x9d:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 90 + .long .Linfo_string13 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 11 # DW_AT_decl_line + .long 306 # DW_AT_type + .byte 3 # Abbrev [3] 0xab:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 84 + .long .Linfo_string14 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 12 # DW_AT_decl_line + .long 311 # DW_AT_type + .byte 3 # Abbrev [3] 0xb9:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 83 + .long .Linfo_string15 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 14 # DW_AT_decl_line + .long 228 # DW_AT_type + .byte 3 # Abbrev [3] 0xc7:0xe DW_TAG_variable + .byte 2 # DW_AT_location + .byte 145 + .byte 71 + .long .Linfo_string17 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 15 # DW_AT_decl_line + .long 316 # DW_AT_type + .byte 3 # Abbrev [3] 0xd5:0xf DW_TAG_variable + .byte 3 # DW_AT_location + .byte 145 + .ascii "\247}" + .long .Linfo_string19 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 16 # DW_AT_decl_line + .long 335 # DW_AT_type + .byte 4 # Abbrev [4] 0xe4:0xb DW_TAG_typedef + .long 240 # DW_AT_type + .long .Linfo_string16 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 13 # DW_AT_decl_line + .byte 0 # End Of Children Mark + .byte 5 # Abbrev [5] 0xf0:0x7 DW_TAG_base_type + .long .Linfo_string6 # DW_AT_name + .byte 6 # DW_AT_encoding + .byte 1 # DW_AT_byte_size + .byte 6 # Abbrev [6] 0xf7:0x5 DW_TAG_pointer_type + .long 240 # DW_AT_type + .byte 7 # Abbrev [7] 0xfc:0x5 DW_TAG_reference_type + .long 240 # DW_AT_type + .byte 8 # Abbrev [8] 0x101:0x5 DW_TAG_rvalue_reference_type + .long 240 # DW_AT_type + .byte 9 # Abbrev [9] 0x106:0x9 DW_TAG_ptr_to_member_type + .long 271 # DW_AT_type + .long 287 # DW_AT_containing_type + .byte 10 # Abbrev [10] 0x10f:0xb DW_TAG_subroutine_type + .long 240 # DW_AT_type + .byte 11 # Abbrev [11] 0x114:0x5 DW_TAG_formal_parameter + .long 282 # DW_AT_type + # DW_AT_artificial + .byte 0 # End Of Children Mark + .byte 6 # Abbrev [6] 0x11a:0x5 DW_TAG_pointer_type + .long 287 # DW_AT_type + .byte 12 # Abbrev [12] 0x11f:0x5 DW_TAG_structure_type + .long .Linfo_string11 # DW_AT_name + # DW_AT_declaration + .byte 9 # Abbrev [9] 0x124:0x9 DW_TAG_ptr_to_member_type + .long 240 # DW_AT_type + .long 287 # DW_AT_containing_type + .byte 13 # Abbrev [13] 0x12d:0x5 DW_TAG_const_type + .long 240 # DW_AT_type + .byte 14 # Abbrev [14] 0x132:0x5 DW_TAG_volatile_type + .long 240 # DW_AT_type + .byte 15 # Abbrev [15] 0x137:0x5 DW_TAG_restrict_type + .long 247 # DW_AT_type + .byte 16 # Abbrev [16] 0x13c:0xc DW_TAG_array_type + .long 240 # DW_AT_type + .byte 17 # Abbrev [17] 0x141:0x6 DW_TAG_subrange_type + .long 328 # DW_AT_type + .byte 12 # DW_AT_count + .byte 0 # End Of Children Mark + .byte 18 # Abbrev [18] 0x148:0x7 DW_TAG_base_type + .long .Linfo_string18 # DW_AT_name + .byte 8 # DW_AT_byte_size + .byte 7 # DW_AT_encoding + .byte 16 # Abbrev [16] 0x14f:0x12 DW_TAG_array_type + .long 240 # DW_AT_type + .byte 17 # Abbrev [17] 0x154:0x6 DW_TAG_subrange_type + .long 328 # DW_AT_type + .byte 12 # DW_AT_count + .byte 17 # Abbrev [17] 0x15a:0x6 DW_TAG_subrange_type + .long 328 # DW_AT_type + .byte 24 # DW_AT_count + .byte 0 # End Of Children Mark + .byte 0 # End Of Children Mark +.Ldebug_info_end0: + .section .debug_macinfo,"",@progbits + .byte 0 # End Of Macro List Mark + + .ident "clang version 9.0.0 " + .section ".note.GNU-stack","",@progbits + .addrsig + .section .debug_line,"",@progbits +.Lline_table_start0: diff --git a/llvm/test/tools/llvm-symbolizer/output-style-json.test b/llvm/test/tools/llvm-symbolizer/output-style-json.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-symbolizer/output-style-json.test @@ -0,0 +1,56 @@ +## This test checks JSON output for CODE. + +## Test JSON output style of DILineInfo in case of error. +# RUN: llvm-symbolizer --output-style=JSON --no-inlines -e %p/no-file.exe 0 \ +# RUN: | FileCheck %s --check-prefix=NO-FILE-1 --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NO-FILE-1:{"Error":{"Code":2,"Message":"{{N|n}}o such file or directory"}} + +## Test JSON output style of empty DILineInfo. +# RUN: llvm-symbolizer --output-style=JSON --no-inlines -e %p/Inputs/addr.exe 100000000000 \ +# RUN: | FileCheck %s --check-prefix=NOT-FOUND-1 --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NOT-FOUND-1:{"Line":0} + +## Test JSON output style of DILineInfo. +# RUN: llvm-symbolizer --output-style=JSON --no-inlines -e %p/Inputs/addr.exe < %p/Inputs/addr.inp \ +# RUN: | FileCheck %s --check-prefix=NO-INLINES --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NO-INLINES:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} +# NO-INLINES-NEXT:{"FunctionName":"main","StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":2,"FileName":"/tmp{{/|\\\\}}x.c","Line":3,"Column":3} +# NO-INLINES-NEXT:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} + +## Test JSON output style of DIInliningInfo in case of error. +# RUN: llvm-symbolizer --output-style=JSON -e %p/no-file.exe 0 \ +# RUN: | FileCheck %s --check-prefix=NO-FILE-2 --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NO-FILE-2:{"Error":{"Code":2,"Message":"{{N|n}}o such file or directory"}} + +## Test JSON output style of empty DIInliningInfo. +# RUN: llvm-symbolizer --output-style=JSON -e %p/Inputs/addr.exe 100000000000 \ +# RUN: | FileCheck %s --check-prefix=NOT-FOUND-2 --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# NOT-FOUND-2:{"Frames":[ +# NOT-FOUND-2-NEXT:{"Line":0}]} + +## Test JSON output style of DIInliningInfo. +# RUN: llvm-symbolizer --output-style=JSON -e %p/Inputs/addr.exe < %p/Inputs/addr.inp \ +# RUN: | FileCheck %s --check-prefix=INLINE --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# INLINE:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} +# INLINE-NEXT:{"Frames":[ +# INLINE-NEXT:{"FunctionName":"inctwo","StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":2,"FileName":"/tmp{{/|\\\\}}x.c","Line":3,"Column":3} +# INLINE-NEXT:,{"FunctionName":"inc","StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":6,"FileName":"/tmp{{/|\\\\}}x.c","Line":7} +# INLINE-NEXT:,{"FunctionName":"main","StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":12,"FileName":"/tmp{{/|\\\\}}x.c","Line":14}]} +# INLINE-NEXT:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} + +## Test JSON output style of DIInliningInfo from llvm-addr2line without function names. +# RUN: llvm-addr2line --output-style=JSON -i -e %p/Inputs/addr.exe < %p/Inputs/addr.inp \ +# RUN: | FileCheck %s --check-prefix=INLINE-A2L --strict-whitespace --match-full-lines --implicit-check-not={{.}} + +# INLINE-A2L:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} +# INLINE-A2L-NEXT:{"Frames":[ +# INLINE-A2L-NEXT:{"StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":2,"FileName":"/tmp{{/|\\\\}}x.c","Line":3,"Column":3} +# INLINE-A2L-NEXT:,{"StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":6,"FileName":"/tmp{{/|\\\\}}x.c","Line":7} +# INLINE-A2L-NEXT:,{"StartFileName":"/tmp{{/|\\\\}}x.c","StartLine":12,"FileName":"/tmp{{/|\\\\}}x.c","Line":14}]} +# INLINE-A2L-NEXT:{"Error":{"Code":22,"Message":"{{I|i}}nvalid argument (address)"}} diff --git a/llvm/tools/llvm-symbolizer/Opts.td b/llvm/tools/llvm-symbolizer/Opts.td --- a/llvm/tools/llvm-symbolizer/Opts.td +++ b/llvm/tools/llvm-symbolizer/Opts.td @@ -33,9 +33,9 @@ : Eq<"obj", "Path to object file to be symbolized (if not provided, " "object file should be specified for each input line)">, MetaVarName<"">; defm output_style - : Eq<"output-style", "Specify print style. Supported styles: LLVM, GNU">, + : Eq<"output-style", "Specify print style. Supported styles: LLVM, GNU, JSON">, MetaVarName<"style">, - Values<"LLVM,GNU">; + Values<"LLVM,GNU,JSON">; def pretty_print : F<"pretty-print", "Make the output more human friendly">; defm print_source_context_lines : Eq<"print-source-context-lines", "Print N lines of source file context">; def relative_address : F<"relative-address", "Interpret addresses as addresses relative to the image base">; diff --git a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp --- a/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp +++ b/llvm/tools/llvm-symbolizer/llvm-symbolizer.cpp @@ -83,6 +83,22 @@ return true; } +template +static void +printResOrErr(Expected &ResOrErr, DIPrinter::OutputStyle OutputStyle, + DIPrinter &Printer, bool BackwardCompatibleErr = true) { + if (ResOrErr) { + Printer << ResOrErr.get(); + } else if (OutputStyle == DIPrinter::OutputStyle::JSON) { + handleAllErrors(std::move(ResOrErr.takeError()), + [&](const ErrorInfoBase &EI) { Printer << EI; }); + } else { + error(ResOrErr); + if (BackwardCompatibleErr) + Printer << T(); + } +} + enum class Command { Code, Data, @@ -144,7 +160,11 @@ uint64_t Offset = 0; if (!parseCommand(Args.getLastArgValue(OPT_obj_EQ), IsAddr2Line, StringRef(InputString), Cmd, ModuleName, Offset)) { - outs() << InputString << "\n"; + if (OutputStyle == DIPrinter::OutputStyle::JSON) + Printer << StringError(std::make_error_code(std::errc::invalid_argument), + "(address)"); + else + outs() << InputString << "\n"; return; } @@ -158,20 +178,15 @@ if (Cmd == Command::Data) { auto ResOrErr = Symbolizer.symbolizeData( ModuleName, {Offset, object::SectionedAddress::UndefSection}); - Printer << (error(ResOrErr) ? DIGlobal() : ResOrErr.get()); + printResOrErr(ResOrErr, OutputStyle, Printer); } else if (Cmd == Command::Frame) { auto ResOrErr = Symbolizer.symbolizeFrame( ModuleName, {Offset, object::SectionedAddress::UndefSection}); - if (!error(ResOrErr)) { - for (DILocal Local : *ResOrErr) - Printer << Local; - if (ResOrErr->empty()) - outs() << "??\n"; - } + printResOrErr(ResOrErr, OutputStyle, Printer, false); } else if (Args.hasFlag(OPT_inlines, OPT_no_inlines, !IsAddr2Line)) { auto ResOrErr = Symbolizer.symbolizeInlinedCode( ModuleName, {Offset, object::SectionedAddress::UndefSection}); - Printer << (error(ResOrErr) ? DIInliningInfo() : ResOrErr.get()); + printResOrErr(ResOrErr, OutputStyle, Printer); } else if (OutputStyle == DIPrinter::OutputStyle::GNU) { // With PrintFunctions == FunctionNameKind::LinkageName (default) // and UseSymbolTable == true (also default), Symbolizer.symbolizeCode() @@ -190,7 +205,7 @@ } else { auto ResOrErr = Symbolizer.symbolizeCode( ModuleName, {Offset, object::SectionedAddress::UndefSection}); - Printer << (error(ResOrErr) ? DILineInfo() : ResOrErr.get()); + printResOrErr(ResOrErr, OutputStyle, Printer); } if (OutputStyle == DIPrinter::OutputStyle::LLVM) outs() << "\n"; @@ -316,9 +331,12 @@ auto OutputStyle = IsAddr2Line ? DIPrinter::OutputStyle::GNU : DIPrinter::OutputStyle::LLVM; if (const opt::Arg *A = Args.getLastArg(OPT_output_style_EQ)) { - OutputStyle = strcmp(A->getValue(), "GNU") == 0 - ? DIPrinter::OutputStyle::GNU - : DIPrinter::OutputStyle::LLVM; + if (strcmp(A->getValue(), "GNU") == 0) + OutputStyle = DIPrinter::OutputStyle::GNU; + else if (strcmp(A->getValue(), "JSON") == 0) + OutputStyle = DIPrinter::OutputStyle::JSON; + else + OutputStyle = DIPrinter::OutputStyle::LLVM; } LLVMSymbolizer Symbolizer(Opts);