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