1 //===-- DWARFDebugMacro.cpp -----------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "SyntaxHighlighting.h" 11 #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" 12 #include "llvm/Support/Compiler.h" 13 #include "llvm/Support/Dwarf.h" 14 #include "llvm/Support/Format.h" 15 #include "llvm/Support/raw_ostream.h" 16 17 using namespace llvm; 18 using namespace dwarf; 19 using namespace syntax; 20 21 void DWARFDebugMacro::dump(raw_ostream &OS) const { 22 unsigned IndLevel = 0; 23 for (const Entry &E : Macros) { 24 // There should not be DW_MACINFO_end_file when IndLevel is Zero. However, 25 // this check handles the case of corrupted ".debug_macinfo" section. 26 if (IndLevel > 0) 27 IndLevel -= (E.Type == DW_MACINFO_end_file); 28 // Print indentation. 29 for (unsigned I = 0; I < IndLevel; I++) 30 OS << " "; 31 IndLevel += (E.Type == DW_MACINFO_start_file); 32 33 WithColor(OS, syntax::Macro).get() << MacinfoString(E.Type); 34 switch (E.Type) { 35 default: 36 // Got a corrupted ".debug_macinfo" section (invalid macinfo type). 37 break; 38 case DW_MACINFO_define: 39 case DW_MACINFO_undef: 40 OS << " - lineno: " << E.Line; 41 OS << " macro: " << E.MacroStr; 42 break; 43 case DW_MACINFO_start_file: 44 OS << " - lineno: " << E.Line; 45 OS << " filenum: " << E.File; 46 break; 47 case DW_MACINFO_end_file: 48 break; 49 case DW_MACINFO_vendor_ext: 50 OS << " - constant: " << E.ExtConstant; 51 OS << " string: " << E.ExtStr; 52 break; 53 } 54 OS << "\n"; 55 } 56 } 57 58 void DWARFDebugMacro::parse(DataExtractor data) { 59 uint32_t Offset = 0; 60 while (data.isValidOffset(Offset)) { 61 // A macro list entry consists of: 62 Entry E; 63 // 1. Macinfo type 64 E.Type = data.getULEB128(&Offset); 65 66 if (E.Type == 0) { 67 // Reached end of ".debug_macinfo" section. 68 return; 69 } 70 71 switch (E.Type) { 72 default: 73 // Got a corrupted ".debug_macinfo" section (invalid macinfo type). 74 // Push the corrupted entry to the list and halt parsing. 75 E.Type = DW_MACINFO_invalid; 76 Macros.push_back(E); 77 return; 78 case DW_MACINFO_define: 79 case DW_MACINFO_undef: 80 // 2. Source line 81 E.Line = data.getULEB128(&Offset); 82 // 3. Macro string 83 E.MacroStr = data.getCStr(&Offset); 84 break; 85 case DW_MACINFO_start_file: 86 // 2. Source line 87 E.Line = data.getULEB128(&Offset); 88 // 3. Source file id 89 E.File = data.getULEB128(&Offset); 90 break; 91 case DW_MACINFO_end_file: 92 break; 93 case DW_MACINFO_vendor_ext: 94 // 2. Vendor extension constant 95 E.ExtConstant = data.getULEB128(&Offset); 96 // 3. Vendor extension string 97 E.ExtStr = data.getCStr(&Offset); 98 break; 99 } 100 101 Macros.push_back(E); 102 } 103 } 104