Home | History | Annotate | Line # | Download | only in llvm-pdbutil
      1 //===- FormatUtil.cpp ----------------------------------------- *- C++ --*-===//
      2 //
      3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
      4 // See https://llvm.org/LICENSE.txt for license information.
      5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
      6 //
      7 //===----------------------------------------------------------------------===//
      8 
      9 #include "FormatUtil.h"
     10 #include "llvm/ADT/STLExtras.h"
     11 #include "llvm/ADT/StringExtras.h"
     12 #include "llvm/BinaryFormat/COFF.h"
     13 #include "llvm/DebugInfo/CodeView/CodeView.h"
     14 #include "llvm/Support/FormatAdapters.h"
     15 #include "llvm/Support/FormatVariadic.h"
     16 
     17 using namespace llvm;
     18 using namespace llvm::codeview;
     19 using namespace llvm::pdb;
     20 
     21 std::string llvm::pdb::truncateStringBack(StringRef S, uint32_t MaxLen) {
     22   if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3)
     23     return std::string(S);
     24 
     25   assert(MaxLen >= 3);
     26   uint32_t FinalLen = std::min<size_t>(S.size(), MaxLen - 3);
     27   S = S.take_front(FinalLen);
     28   return std::string(S) + std::string("...");
     29 }
     30 
     31 std::string llvm::pdb::truncateStringMiddle(StringRef S, uint32_t MaxLen) {
     32   if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3)
     33     return std::string(S);
     34 
     35   assert(MaxLen >= 3);
     36   uint32_t FinalLen = std::min<size_t>(S.size(), MaxLen - 3);
     37   StringRef Front = S.take_front(FinalLen / 2);
     38   StringRef Back = S.take_back(Front.size());
     39   return std::string(Front) + std::string("...") + std::string(Back);
     40 }
     41 
     42 std::string llvm::pdb::truncateStringFront(StringRef S, uint32_t MaxLen) {
     43   if (MaxLen == 0 || S.size() <= MaxLen || S.size() <= 3)
     44     return std::string(S);
     45 
     46   assert(MaxLen >= 3);
     47   S = S.take_back(MaxLen - 3);
     48   return std::string("...") + std::string(S);
     49 }
     50 
     51 std::string llvm::pdb::truncateQuotedNameFront(StringRef Label, StringRef Name,
     52                                                uint32_t MaxLen) {
     53   uint32_t RequiredExtraChars = Label.size() + 1 + 2;
     54   if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen)
     55     return formatv("{0} \"{1}\"", Label, Name).str();
     56 
     57   assert(MaxLen >= RequiredExtraChars);
     58   std::string TN = truncateStringFront(Name, MaxLen - RequiredExtraChars);
     59   return formatv("{0} \"{1}\"", Label, TN).str();
     60 }
     61 
     62 std::string llvm::pdb::truncateQuotedNameBack(StringRef Label, StringRef Name,
     63                                               uint32_t MaxLen) {
     64   uint32_t RequiredExtraChars = Label.size() + 1 + 2;
     65   if (MaxLen == 0 || RequiredExtraChars + Name.size() <= MaxLen)
     66     return formatv("{0} \"{1}\"", Label, Name).str();
     67 
     68   assert(MaxLen >= RequiredExtraChars);
     69   std::string TN = truncateStringBack(Name, MaxLen - RequiredExtraChars);
     70   return formatv("{0} \"{1}\"", Label, TN).str();
     71 }
     72 
     73 std::string llvm::pdb::typesetItemList(ArrayRef<std::string> Opts,
     74                                        uint32_t IndentLevel, uint32_t GroupSize,
     75                                        StringRef Sep) {
     76   std::string Result;
     77   while (!Opts.empty()) {
     78     ArrayRef<std::string> ThisGroup;
     79     ThisGroup = Opts.take_front(GroupSize);
     80     Opts = Opts.drop_front(ThisGroup.size());
     81     Result += join(ThisGroup, Sep);
     82     if (!Opts.empty()) {
     83       Result += Sep;
     84       Result += "\n";
     85       Result += std::string(formatv("{0}", fmt_repeat(' ', IndentLevel)));
     86     }
     87   }
     88   return Result;
     89 }
     90 
     91 std::string llvm::pdb::typesetStringList(uint32_t IndentLevel,
     92                                          ArrayRef<StringRef> Strings) {
     93   std::string Result = "[";
     94   for (const auto &S : Strings) {
     95     Result += std::string(formatv("\n{0}{1}", fmt_repeat(' ', IndentLevel), S));
     96   }
     97   Result += "]";
     98   return Result;
     99 }
    100 
    101 std::string llvm::pdb::formatChunkKind(DebugSubsectionKind Kind,
    102                                        bool Friendly) {
    103   if (Friendly) {
    104     switch (Kind) {
    105       RETURN_CASE(DebugSubsectionKind, None, "none");
    106       RETURN_CASE(DebugSubsectionKind, Symbols, "symbols");
    107       RETURN_CASE(DebugSubsectionKind, Lines, "lines");
    108       RETURN_CASE(DebugSubsectionKind, StringTable, "strings");
    109       RETURN_CASE(DebugSubsectionKind, FileChecksums, "checksums");
    110       RETURN_CASE(DebugSubsectionKind, FrameData, "frames");
    111       RETURN_CASE(DebugSubsectionKind, InlineeLines, "inlinee lines");
    112       RETURN_CASE(DebugSubsectionKind, CrossScopeImports, "xmi");
    113       RETURN_CASE(DebugSubsectionKind, CrossScopeExports, "xme");
    114       RETURN_CASE(DebugSubsectionKind, ILLines, "il lines");
    115       RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap, "func md token map");
    116       RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap, "type md token map");
    117       RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
    118                   "merged assembly input");
    119       RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA, "coff symbol rva");
    120     }
    121   } else {
    122     switch (Kind) {
    123       RETURN_CASE(DebugSubsectionKind, None, "none");
    124       RETURN_CASE(DebugSubsectionKind, Symbols, "DEBUG_S_SYMBOLS");
    125       RETURN_CASE(DebugSubsectionKind, Lines, "DEBUG_S_LINES");
    126       RETURN_CASE(DebugSubsectionKind, StringTable, "DEBUG_S_STRINGTABLE");
    127       RETURN_CASE(DebugSubsectionKind, FileChecksums, "DEBUG_S_FILECHKSMS");
    128       RETURN_CASE(DebugSubsectionKind, FrameData, "DEBUG_S_FRAMEDATA");
    129       RETURN_CASE(DebugSubsectionKind, InlineeLines, "DEBUG_S_INLINEELINES");
    130       RETURN_CASE(DebugSubsectionKind, CrossScopeImports,
    131                   "DEBUG_S_CROSSSCOPEIMPORTS");
    132       RETURN_CASE(DebugSubsectionKind, CrossScopeExports,
    133                   "DEBUG_S_CROSSSCOPEEXPORTS");
    134       RETURN_CASE(DebugSubsectionKind, ILLines, "DEBUG_S_IL_LINES");
    135       RETURN_CASE(DebugSubsectionKind, FuncMDTokenMap,
    136                   "DEBUG_S_FUNC_MDTOKEN_MAP");
    137       RETURN_CASE(DebugSubsectionKind, TypeMDTokenMap,
    138                   "DEBUG_S_TYPE_MDTOKEN_MAP");
    139       RETURN_CASE(DebugSubsectionKind, MergedAssemblyInput,
    140                   "DEBUG_S_MERGED_ASSEMBLYINPUT");
    141       RETURN_CASE(DebugSubsectionKind, CoffSymbolRVA,
    142                   "DEBUG_S_COFF_SYMBOL_RVA");
    143     }
    144   }
    145   return formatUnknownEnum(Kind);
    146 }
    147 
    148 std::string llvm::pdb::formatSymbolKind(SymbolKind K) {
    149   switch (uint32_t(K)) {
    150 #define SYMBOL_RECORD(EnumName, value, name)                                   \
    151   case EnumName:                                                               \
    152     return #EnumName;
    153 #define CV_SYMBOL(EnumName, value) SYMBOL_RECORD(EnumName, value, EnumName)
    154 #include "llvm/DebugInfo/CodeView/CodeViewSymbols.def"
    155   }
    156   return formatUnknownEnum(K);
    157 }
    158 
    159 std::string llvm::pdb::formatTypeLeafKind(TypeLeafKind K) {
    160   switch (K) {
    161 #define TYPE_RECORD(EnumName, value, name)                                     \
    162   case EnumName:                                                               \
    163     return #EnumName;
    164 #include "llvm/DebugInfo/CodeView/CodeViewTypes.def"
    165   default:
    166     return formatv("UNKNOWN RECORD ({0:X})",
    167                    static_cast<std::underlying_type_t<TypeLeafKind>>(K))
    168         .str();
    169   }
    170 }
    171 
    172 std::string llvm::pdb::formatSegmentOffset(uint16_t Segment, uint32_t Offset) {
    173   return std::string(formatv("{0:4}:{1:4}", Segment, Offset));
    174 }
    175 
    176 #define PUSH_CHARACTERISTIC_FLAG(Enum, TheOpt, Value, Style, Descriptive)      \
    177   PUSH_FLAG(Enum, TheOpt, Value,                                               \
    178             ((Style == CharacteristicStyle::HeaderDefinition) ? #TheOpt        \
    179                                                               : Descriptive))
    180 
    181 #define PUSH_MASKED_CHARACTERISTIC_FLAG(Enum, Mask, TheOpt, Value, Style,      \
    182                                         Descriptive)                           \
    183   PUSH_MASKED_FLAG(Enum, Mask, TheOpt, Value,                                  \
    184                    ((Style == CharacteristicStyle::HeaderDefinition)           \
    185                         ? #TheOpt                                              \
    186                         : Descriptive))
    187 
    188 std::string llvm::pdb::formatSectionCharacteristics(uint32_t IndentLevel,
    189                                                     uint32_t C,
    190                                                     uint32_t FlagsPerLine,
    191                                                     StringRef Separator,
    192                                                     CharacteristicStyle Style) {
    193   using SC = COFF::SectionCharacteristics;
    194   std::vector<std::string> Opts;
    195   if (C == COFF::SC_Invalid)
    196     return "invalid";
    197   if (C == 0)
    198     return "none";
    199   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NOLOAD, C, Style, "noload");
    200   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_TYPE_NO_PAD, C, Style, "no padding");
    201   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_CODE, C, Style, "code");
    202   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_INITIALIZED_DATA, C, Style,
    203                            "initialized data");
    204   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_CNT_UNINITIALIZED_DATA, C, Style,
    205                            "uninitialized data");
    206   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_OTHER, C, Style, "other");
    207   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_INFO, C, Style, "info");
    208   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_REMOVE, C, Style, "remove");
    209   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_COMDAT, C, Style, "comdat");
    210   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_GPREL, C, Style, "gp rel");
    211   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PURGEABLE, C, Style, "purgeable");
    212   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_16BIT, C, Style, "16-bit");
    213   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_LOCKED, C, Style, "locked");
    214   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_PRELOAD, C, Style, "preload");
    215   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1BYTES, C,
    216                                   Style, "1 byte align");
    217   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2BYTES, C,
    218                                   Style, "2 byte align");
    219   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4BYTES, C,
    220                                   Style, "4 byte align");
    221   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8BYTES, C,
    222                                   Style, "8 byte align");
    223   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_16BYTES, C,
    224                                   Style, "16 byte align");
    225   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_32BYTES, C,
    226                                   Style, "32 byte align");
    227   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_64BYTES, C,
    228                                   Style, "64 byte align");
    229   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_128BYTES, C,
    230                                   Style, "128 byte align");
    231   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_256BYTES, C,
    232                                   Style, "256 byte align");
    233   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_512BYTES, C,
    234                                   Style, "512 byte align");
    235   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_1024BYTES, C,
    236                                   Style, "1024 byte align");
    237   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_2048BYTES, C,
    238                                   Style, "2048 byte align");
    239   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_4096BYTES, C,
    240                                   Style, "4096 byte align");
    241   PUSH_MASKED_CHARACTERISTIC_FLAG(SC, 0xF00000, IMAGE_SCN_ALIGN_8192BYTES, C,
    242                                   Style, "8192 byte align");
    243   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_LNK_NRELOC_OVFL, C, Style,
    244                            "noreloc overflow");
    245   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_DISCARDABLE, C, Style,
    246                            "discardable");
    247   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_CACHED, C, Style,
    248                            "not cached");
    249   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_NOT_PAGED, C, Style, "not paged");
    250   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_SHARED, C, Style, "shared");
    251   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_EXECUTE, C, Style,
    252                            "execute permissions");
    253   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_READ, C, Style,
    254                            "read permissions");
    255   PUSH_CHARACTERISTIC_FLAG(SC, IMAGE_SCN_MEM_WRITE, C, Style,
    256                            "write permissions");
    257   return typesetItemList(Opts, IndentLevel, FlagsPerLine, Separator);
    258 }
    259