Index: llvm/docs/CommandGuide/llvm-dwarfdump.rst =================================================================== --- llvm/docs/CommandGuide/llvm-dwarfdump.rst +++ llvm/docs/CommandGuide/llvm-dwarfdump.rst @@ -105,6 +105,10 @@ When displaying debug info entries, only show children to a maximum depth of . +.. option:: --show-section-sizes + + Show the sizes of all debug sections. + .. option:: --statistics Collect debug info quality metrics and print the results Index: llvm/test/tools/llvm-dwarfdump/X86/macho_archive_section_sizes.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-dwarfdump/X86/macho_archive_section_sizes.test @@ -0,0 +1,47 @@ +RUN: llvm-dwarfdump --show-section-sizes %S/../../dsymutil/Inputs/libfat-test.a \ +RUN: | FileCheck %s --match-full-lines --strict-whitespace + + CHECK:file: {{.*}}libfat-test.a(x86_64)(fat-test.o) +CHECK-NEXT:---------------------------------------------------- +CHECK-NEXT:SECTION SIZE (b) +CHECK-NEXT:-------------- -------- +CHECK-NEXT:__debug_info 60 (3.83%) +CHECK-NEXT:__debug_loc 0 (0.00%) +CHECK-NEXT:__debug_ranges 0 (0.00%) +CHECK-NEXT:__debug_abbrev 44 (2.81%) +CHECK-NEXT:__debug_line 43 (2.74%) +CHECK-NEXT:__debug_str 69 (4.40%) +CHECK-EMPTY: +CHECK-NEXT: Total Size: 216 (13.78%) +CHECK-NEXT: Total File Size: 1568 +CHECK-NEXT:---------------------------------------------------- + + CHECK:file: {{.*}}libfat-test.a(i386)(fat-test.o) +CHECK-NEXT:---------------------------------------------------- +CHECK-NEXT:SECTION SIZE (b) +CHECK-NEXT:-------------- -------- +CHECK-NEXT:__debug_info 56 (4.00%) +CHECK-NEXT:__debug_loc 0 (0.00%) +CHECK-NEXT:__debug_ranges 0 (0.00%) +CHECK-NEXT:__debug_abbrev 44 (3.14%) +CHECK-NEXT:__debug_line 43 (3.07%) +CHECK-NEXT:__debug_str 67 (4.79%) +CHECK-EMPTY: +CHECK-NEXT: Total Size: 210 (15.00%) +CHECK-NEXT: Total File Size: 1400 +CHECK-NEXT:---------------------------------------------------- + + CHECK:file: {{.*}}libfat-test.a(x86_64h)(fat-test.o) +CHECK-NEXT:---------------------------------------------------- +CHECK-NEXT:SECTION SIZE (b) +CHECK-NEXT:-------------- -------- +CHECK-NEXT:__debug_info 60 (3.83%) +CHECK-NEXT:__debug_loc 0 (0.00%) +CHECK-NEXT:__debug_ranges 0 (0.00%) +CHECK-NEXT:__debug_abbrev 44 (2.81%) +CHECK-NEXT:__debug_line 43 (2.74%) +CHECK-NEXT:__debug_str 70 (4.46%) +CHECK-EMPTY: +CHECK-NEXT: Total Size: 217 (13.84%) +CHECK-NEXT: Total File Size: 1568 +CHECK-NEXT:---------------------------------------------------- Index: llvm/test/tools/llvm-dwarfdump/X86/section_sizes.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-dwarfdump/X86/section_sizes.test @@ -0,0 +1,48 @@ +## Check how llvm-dwarfdump calculates section sizes with --show-section-sizes. + +# RUN: yaml2obj --docnum=1 %s -o %t +# RUN: llvm-dwarfdump --show-section-sizes %t | FileCheck %s --match-full-lines --strict-whitespace + +# CHECK:file: {{.*}} +# CHECK-NEXT:---------------------------------------------------- +# CHECK-NEXT:SECTION SIZE (b) +# CHECK-NEXT:--------------- -------- +# CHECK-NEXT:.debug_info 17 (1.74%) +# CHECK-NEXT:.debug_loc 1 (0.10%) +# CHECK-NEXT:.debug_type 26 (2.66%) +# CHECK-NEXT:.debug_foo 100 (10.25%) +# CHECK-NEXT:.debug_info.dwo 9 (0.92%) +# CHECK-NEXT:.debug_line 19 (1.95%) +# CHECK-EMPTY: +# CHECK-NEXT: Total Size: 172 (17.62%) +# CHECK-NEXT: Total File Size: 976 +# CHECK-NEXT:---------------------------------------------------- + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .debug_info + Type: SHT_PROGBITS + Size: 17 + - Name: .debug_line + Type: SHT_PROGBITS + Size: 19 + - Name: .debug_loc + Type: SHT_PROGBITS + Size: 1 + - Name: .debug_type + Type: SHT_PROGBITS + Size: 13 + - Name: .debug_type [1] + Type: SHT_PROGBITS + Size: 13 + - Name: .debug_foo + Type: SHT_PROGBITS + Size: 100 + - Name: .debug_info.dwo + Type: SHT_PROGBITS + Size: 9 Index: llvm/test/tools/llvm-dwarfdump/X86/section_sizes_no_debug_sections.test =================================================================== --- /dev/null +++ llvm/test/tools/llvm-dwarfdump/X86/section_sizes_no_debug_sections.test @@ -0,0 +1,25 @@ +# RUN: yaml2obj --docnum=1 %s -o %t +# RUN: llvm-dwarfdump --show-section-sizes %t | FileCheck %s --match-full-lines --strict-whitespace + +# CHECK:file: {{.*}} +# CHECK-NEXT:---------------------------------------------------- +# CHECK-NEXT:SECTION SIZE (b) +# CHECK-NEXT:------- -------- +# CHECK-EMPTY: +# CHECK-NEXT: Total Size: 0 (0.00%) +# CHECK-NEXT: Total File Size: 456 +# CHECK-NEXT:---------------------------------------------------- + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .foo + Type: SHT_PROGBITS + Size: 17 + - Name: .bar + Type: SHT_PROGBITS + Size: 19 Index: llvm/test/tools/llvm-dwarfdump/X86/statistics.ll =================================================================== --- llvm/test/tools/llvm-dwarfdump/X86/statistics.ll +++ llvm/test/tools/llvm-dwarfdump/X86/statistics.ll @@ -48,7 +48,11 @@ ; CHECK: "scope bytes covered": ; CHECK: "total function size":[[FUNCSIZE:[0-9]+]] ; CHECK: "total inlined function size":[[INLINESIZE:[0-9]+]] - +; CHECK: "size of __debug_info":380 +; CHECK: "size of __debug_loc":35 +; CHECK: "size of __debug_abbrev":303 +; CHECK: "size of __debug_line":117 +; CHECK: "size of __debug_str":204 ; ModuleID = '/tmp/quality.cpp' source_filename = "/tmp/quality.cpp" Index: llvm/test/tools/llvm-dwarfdump/coff-x86_64.yaml =================================================================== --- llvm/test/tools/llvm-dwarfdump/coff-x86_64.yaml +++ llvm/test/tools/llvm-dwarfdump/coff-x86_64.yaml @@ -1,9 +1,26 @@ # RUN: yaml2obj %s | llvm-dwarfdump - | FileCheck %s +# RUN: yaml2obj %s | llvm-dwarfdump - --show-section-sizes \ +# RUN: | FileCheck %s --check-prefix=SECTION-SIZES --match-full-lines --strict-whitespace + # CHECK: DW_TAG_compile_unit # CHECK-NEXT: DW_AT_producer ("producer string") # CHECK-NEXT: DW_AT_language (DW_LANG_C99) # CHECK-NEXT: DW_AT_name ("test.c") # CHECK-NEXT: DW_AT_comp_dir ("/path/to/src") + +# SECTION-SIZES:---------------------------------------------------- +# SECTION-SIZES-NEXT:file: {{.*}} +# SECTION-SIZES-NEXT:---------------------------------------------------- +# SECTION-SIZES-NEXT:SECTION SIZE (b) +# SECTION-SIZES-NEXT:------------- -------- +# SECTION-SIZES-NEXT:.debug_info 27 (3.70%) +# SECTION-SIZES-NEXT:.debug_abbrev 14 (1.92%) +# SECTION-SIZES-NEXT:.debug_str 36 (4.94%) +# SECTION-SIZES-EMPTY: +# SECTION-SIZES-NEXT: Total Size: 77 (10.56%) +# SECTION-SIZES-NEXT: Total File Size: 729 +# SECTION-SIZES-NEXT:---------------------------------------------------- + --- !COFF header: Machine: IMAGE_FILE_MACHINE_AMD64 Index: llvm/tools/llvm-dwarfdump/CMakeLists.txt =================================================================== --- llvm/tools/llvm-dwarfdump/CMakeLists.txt +++ llvm/tools/llvm-dwarfdump/CMakeLists.txt @@ -8,6 +8,7 @@ ) add_llvm_tool(llvm-dwarfdump + SectionSizes.cpp Statistics.cpp llvm-dwarfdump.cpp ) Index: llvm/tools/llvm-dwarfdump/SectionSizes.h =================================================================== --- /dev/null +++ llvm/tools/llvm-dwarfdump/SectionSizes.h @@ -0,0 +1,38 @@ +//===- SectionSizes.h - Debug section sizes ---------------------*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===-----------------------------------------------------------------------===/ + +#ifndef LLVM_TOOLS_SECTION_SIZES_H +#define LLVM_TOOLS_SECTION_SIZES_H + +#include "llvm/DebugInfo/DIContext.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/WithColor.h" + +namespace llvm { + +using SectionSizeMap = StringMap; + +/// Holds cumulative section sizes for an object file. +struct SectionSizes { + /// Map of .debug section names and their sizes across all such-named + /// sections. + SectionSizeMap DebugSectionSizes; + /// Total number of bytes of all sections. + uint64_t TotalObjectSize = 0; + /// Total number of bytes of all debug sections. + uint64_t TotalDebugSectionsSize = 0; +}; + +/// Calculate the section sizes. +void calculateSectionSizes(const object::ObjectFile &Obj, SectionSizes &Sizes, + const Twine &Filename); + +} // end namespace llvm + +#endif // LLVM_TOOLS_SECTION_SIZES_H Index: llvm/tools/llvm-dwarfdump/SectionSizes.cpp =================================================================== --- /dev/null +++ llvm/tools/llvm-dwarfdump/SectionSizes.cpp @@ -0,0 +1,124 @@ +//===-- SectionSizes.cpp - Debug section sizes ----------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "SectionSizes.h" + +#define DEBUG_TYPE "dwarfdump" + +using namespace llvm; +using namespace object; + +static size_t getNameColumnWidth(const SectionSizes &Sizes, + const StringRef SectionNameTitle) { + // The minimum column width should be the size of "SECTION". + size_t Width = SectionNameTitle.size(); + for (const auto &DebugSec : Sizes.DebugSectionSizes) { + StringRef SectionName = DebugSec.getKey(); + Width = std::max(Width, SectionName.size()); + } + return Width; +} + +static size_t getSizeColumnWidth(const SectionSizes &Sizes, + const StringRef SectionSizeTitle) { + // The minimum column width should be the size of "SIZE (b)". + size_t Width = SectionSizeTitle.size(); + for (const auto &DebugSec : Sizes.DebugSectionSizes) { + size_t NumWidth = std::to_string(DebugSec.getValue()).size(); + Width = std::max(Width, NumWidth); + } + return Width; +} + +static void prettyPrintSectionSizes(const ObjectFile &Obj, + const SectionSizes &Sizes, + raw_ostream &OS) { + const StringRef SectionNameTitle = "SECTION"; + const StringRef SectionSizeTitle = "SIZE (b)"; + + size_t NameColWidth = getNameColumnWidth(Sizes, SectionNameTitle); + size_t SizeColWidth = getSizeColumnWidth(Sizes, SectionSizeTitle); + + OS << "----------------------------------------------------" << '\n'; + OS << SectionNameTitle; + size_t SectionNameTitleWidth = SectionNameTitle.size(); + for (unsigned i = 0; i < (NameColWidth - SectionNameTitleWidth) + 2; i++) + OS << " "; + OS << SectionSizeTitle << '\n'; + for (unsigned i = 0; i < NameColWidth; i++) + OS << "-"; + OS << " "; + + for (unsigned i = 0; i < SizeColWidth; i++) + OS << "-"; + OS << '\n'; + + for (const auto &DebugSec : Sizes.DebugSectionSizes) { + OS << left_justify(DebugSec.getKey(), NameColWidth) << " "; + + auto NumBytes = std::to_string(DebugSec.getValue()); + OS << right_justify(NumBytes, SizeColWidth) << " (" + << format("%0.2f", DebugSec.getValue() / + static_cast(Sizes.TotalObjectSize) * 100) + << "%)\n"; + } + + OS << '\n'; + OS << " Total Size: " << Sizes.TotalDebugSectionsSize << " (" + << format("%0.2f", Sizes.TotalDebugSectionsSize / + static_cast(Sizes.TotalObjectSize) * 100) + << "%)\n"; + OS << " Total File Size: " << Sizes.TotalObjectSize << '\n'; + OS << "----------------------------------------------------" << '\n'; +} + +namespace llvm { + +void calculateSectionSizes(const ObjectFile &Obj, SectionSizes &Sizes, + const Twine &Filename) { + // Get total size. + Sizes.TotalObjectSize = Obj.getData().size(); + + for (const SectionRef &Section : Obj.sections()) { + StringRef SectionName; + if (Expected NameOrErr = Section.getName()) + SectionName = *NameOrErr; + else + WithColor::defaultWarningHandler( + createFileError(Filename, NameOrErr.takeError())); + + LLVM_DEBUG(dbgs() << SectionName.str() << ": " << Section.getSize() + << '\n'); + + if (!Section.isDebugSection(SectionName)) + continue; + + Sizes.TotalDebugSectionsSize += Section.getSize(); + Sizes.DebugSectionSizes[SectionName] += Section.getSize(); + } +} + +} // end namespace llvm + +bool collectObjectSectionSizes(ObjectFile &Obj, DWARFContext & /*DICtx*/, + const Twine &Filename, raw_ostream &OS) { + SectionSizes Sizes; + + // Get the section sizes. + calculateSectionSizes(Obj, Sizes, Filename); + + OS << "----------------------------------------------------\n"; + OS << "file: " << Filename.str() << '\n'; + + prettyPrintSectionSizes(Obj, Sizes, OS); + + // TODO: If the input file is an archive, print the cumulative summary of all + // files from the archive. + + return true; +} Index: llvm/tools/llvm-dwarfdump/Statistics.cpp =================================================================== --- llvm/tools/llvm-dwarfdump/Statistics.cpp +++ llvm/tools/llvm-dwarfdump/Statistics.cpp @@ -15,6 +15,8 @@ #include "llvm/Object/ObjectFile.h" #include "llvm/Support/JSON.h" +#include "SectionSizes.h" + #define DEBUG_TYPE "dwarfdump" using namespace llvm; using namespace object; @@ -468,6 +470,7 @@ OS << ",\"" << Key << "\":" << Value; LLVM_DEBUG(llvm::dbgs() << Key << ": " << Value << '\n'); } + static void printLocationStats(raw_ostream &OS, const char *Key, std::vector &LocationStats) { @@ -491,6 +494,12 @@ LLVM_DEBUG(llvm::dbgs() << Key << " with 100% of its scope covered: " << LocationStats[NumOfCoverageCategories - 1]); } + +static void printSectionSizes(raw_ostream &OS, const SectionSizes &Sizes) { + for (const auto &DebugSec : Sizes.DebugSectionSizes) + OS << ",\"size of " << DebugSec.getKey() << "\":" << DebugSec.getValue(); +} + /// \} /// Collect debug info quality metrics for an entire DIContext. @@ -512,6 +521,10 @@ collectStatsRecursive(CUDie, "/", "g", 0, 0, Statistics, GlobalStats, LocStats); + /// Collect the sizes of debug sections. + SectionSizes Sizes; + calculateSectionSizes(Obj, Sizes, Filename); + /// The version number should be increased every time the algorithm is changed /// (including bug fixes). New metrics may be added without increasing the /// version. @@ -602,6 +615,7 @@ printDatum(OS, "vars with binary location", VarWithLoc); printDatum(OS, "total variables procesed by location statistics", LocStats.NumVarParam); + printSectionSizes(OS, Sizes); printLocationStats(OS, "variables", LocStats.VarParamLocStats); printLocationStats(OS, "variables (excluding the debug entry values)", LocStats.VarParamNonEntryValLocStats); Index: llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp =================================================================== --- llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp +++ llvm/tools/llvm-dwarfdump/llvm-dwarfdump.cpp @@ -208,6 +208,10 @@ Statistics("statistics", cl::desc("Emit JSON-formatted debug info quality metrics."), cat(DwarfDumpCategory)); +static cl::opt + ShowSectionSizes("show-section-sizes", + cl::desc("Show the sizes of all debug sections."), + cat(DwarfDumpCategory)); static opt Verify("verify", desc("Verify the DWARF debug info."), cat(DwarfDumpCategory)); static opt Quiet("quiet", desc("Use with -verify to not emit to STDOUT."), @@ -413,6 +417,9 @@ bool collectStatsForObjectFile(ObjectFile &Obj, DWARFContext &DICtx, const Twine &Filename, raw_ostream &OS); +bool collectObjectSectionSizes(ObjectFile &Obj, DWARFContext & /*DICtx*/, + const Twine &Filename, raw_ostream &OS); + static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, const Twine &Filename, raw_ostream &OS) { logAllUnhandledErrors(DICtx.loadRegisterInfo(Obj), errs(), @@ -635,12 +642,16 @@ return handleFile(Object, verifyObjectFile, OutputFile.os()); })) return 1; - } else if (Statistics) + } else if (Statistics) { for (auto Object : Objects) handleFile(Object, collectStatsForObjectFile, OutputFile.os()); - else + } else if (ShowSectionSizes) { + for (auto Object : Objects) + handleFile(Object, collectObjectSectionSizes, OutputFile.os()); + } else { for (auto Object : Objects) handleFile(Object, dumpObjectFile, OutputFile.os()); + } return EXIT_SUCCESS; }