Index: llvm/docs/CommandGuide/llvm-dwarfdump.rst =================================================================== --- llvm/docs/CommandGuide/llvm-dwarfdump.rst +++ llvm/docs/CommandGuide/llvm-dwarfdump.rst @@ -110,6 +110,10 @@ Collect debug info quality metrics and print the results as machine-readable single-line JSON output. +.. option:: --show-section-sizes + + Show the sizes of all .debug_* sections. + .. option:: --summarize-types Abbreviate the description of type unit entries. Index: llvm/test/tools/llvm-dwarfdump/X86/show_section_sizes.s =================================================================== --- /dev/null +++ llvm/test/tools/llvm-dwarfdump/X86/show_section_sizes.s @@ -0,0 +1,274 @@ +# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj | llvm-dwarfdump --show-section-sizes - | FileCheck %s +# +# CHECK: .debug_info 132 +# CHECK: .debug_str 45 +# CHECK: .debug_abbrev 120 +# CHECK: .debug_line 76 +# CHECK: .debug_ranges 0 +# CHECK: .debug_loc 89 +# CHECK: .debug_frame 0 +# CHECK: .debug_macinfo 0 +# CHECK: .debug_pubnames 0 +# CHECK: .debug_pubtypes 0 +# CHECK: .debug_types 0 +# CHECK: .debug_*: 462 +# CHECK: TOTAL FILE SIZE: 540 +# + .text + .file "test.c" + .globl fn1 # -- Begin function fn1 + .p2align 4, 0x90 + .type fn1,@function +fn1: # @fn1 +.Lfn1$local: +.Lfunc_begin0: + .file 1 "/" "test.c" + .loc 1 3 0 # test.c:3:0 + .cfi_startproc +# %bb.0: # %entry + #DEBUG_VALUE: fn1:x <- $edi + pushq %rbx + .cfi_def_cfa_offset 16 + .cfi_offset %rbx, -16 + movl %edi, %ebx +.Ltmp0: + #DEBUG_VALUE: fn1:x <- $ebx + .loc 1 4 11 prologue_end # test.c:4:11 + xorl %eax, %eax + callq fn +.Ltmp1: + #DEBUG_VALUE: fn1:y <- $eax + .loc 1 5 9 # test.c:5:9 + cmpl %ebx, %eax + movl $-1, %ecx +.Ltmp2: + .loc 1 0 0 is_stmt 0 # test.c:0:0 + cmovll %ecx, %eax +.Ltmp3: + .loc 1 10 1 is_stmt 1 # test.c:10:1 + popq %rbx +.Ltmp4: + .cfi_def_cfa_offset 8 + retq +.Ltmp5: +.Lfunc_end0: + .size fn1, .Lfunc_end0-fn1 + .cfi_endproc + # -- End function + .section .debug_str,"MS",@progbits,1 +.Linfo_string0: + .asciz "clang version 11.0.0" # string offset=0 +.Linfo_string1: + .asciz "test.c" # string offset=105 +.Linfo_string2: + .asciz "/" # string offset=112 +.Linfo_string3: + .asciz "fn" # string offset=163 +.Linfo_string4: + .asciz "int" # string offset=166 +.Linfo_string5: + .asciz "fn1" # string offset=170 +.Linfo_string6: + .asciz "x" # string offset=174 +.Linfo_string7: + .asciz "y" # string offset=176 + .section .debug_loc,"",@progbits +.Ldebug_loc0: + .quad .Lfunc_begin0-.Lfunc_begin0 + .quad .Ltmp0-.Lfunc_begin0 + .short 1 # Loc expr size + .byte 85 # super-register DW_OP_reg5 + .quad .Ltmp0-.Lfunc_begin0 + .quad .Ltmp4-.Lfunc_begin0 + .short 1 # Loc expr size + .byte 83 # super-register DW_OP_reg3 + .quad 0 + .quad 0 +.Ldebug_loc1: + .quad .Ltmp1-.Lfunc_begin0 + .quad .Ltmp3-.Lfunc_begin0 + .short 1 # Loc expr size + .byte 80 # super-register DW_OP_reg0 + .quad 0 + .quad 0 + .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 + .ascii "\227B" # DW_AT_GNU_all_call_sites + .byte 25 # DW_FORM_flag_present + .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 39 # DW_AT_prototyped + .byte 25 # DW_FORM_flag_present + .byte 73 # DW_AT_type + .byte 19 # DW_FORM_ref4 + .byte 63 # DW_AT_external + .byte 25 # DW_FORM_flag_present + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 3 # Abbreviation Code + .byte 5 # DW_TAG_formal_parameter + .byte 0 # DW_CHILDREN_no + .byte 2 # DW_AT_location + .byte 23 # DW_FORM_sec_offset + .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 52 # DW_TAG_variable + .byte 0 # DW_CHILDREN_no + .byte 2 # DW_AT_location + .byte 23 # DW_FORM_sec_offset + .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 5 # Abbreviation Code + .ascii "\211\202\001" # DW_TAG_GNU_call_site + .byte 0 # DW_CHILDREN_no + .byte 49 # DW_AT_abstract_origin + .byte 19 # DW_FORM_ref4 + .byte 17 # DW_AT_low_pc + .byte 1 # DW_FORM_addr + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 6 # Abbreviation Code + .byte 46 # DW_TAG_subprogram + .byte 1 # DW_CHILDREN_yes + .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 60 # DW_AT_declaration + .byte 25 # DW_FORM_flag_present + .byte 63 # DW_AT_external + .byte 25 # DW_FORM_flag_present + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 7 # Abbreviation Code + .byte 24 # DW_TAG_unspecified_parameters + .byte 0 # DW_CHILDREN_no + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 8 # 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 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 8 # Address Size (in bytes) + .byte 1 # Abbrev [1] 0xb:0x79 DW_TAG_compile_unit + .long .Linfo_string0 # DW_AT_producer + .short 12 # DW_AT_language + .long .Linfo_string1 # DW_AT_name + .long .Lline_table_start0 # DW_AT_stmt_list + .long .Linfo_string2 # DW_AT_comp_dir + .quad .Lfunc_begin0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc + .byte 2 # Abbrev [2] 0x2a:0x45 DW_TAG_subprogram + .quad .Lfunc_begin0 # DW_AT_low_pc + .long .Lfunc_end0-.Lfunc_begin0 # DW_AT_high_pc + .byte 1 # DW_AT_frame_base + .byte 87 + # DW_AT_GNU_all_call_sites + .long .Linfo_string5 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 3 # DW_AT_decl_line + # DW_AT_prototyped + .long 124 # DW_AT_type + # DW_AT_external + .byte 3 # Abbrev [3] 0x43:0xf DW_TAG_formal_parameter + .long .Ldebug_loc0 # DW_AT_location + .long .Linfo_string6 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 3 # DW_AT_decl_line + .long 124 # DW_AT_type + .byte 4 # Abbrev [4] 0x52:0xf DW_TAG_variable + .long .Ldebug_loc1 # DW_AT_location + .long .Linfo_string7 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 4 # DW_AT_decl_line + .long 124 # DW_AT_type + .byte 5 # Abbrev [5] 0x61:0xd DW_TAG_GNU_call_site + .long 111 # DW_AT_abstract_origin + .quad .Ltmp1 # DW_AT_low_pc + .byte 0 # End Of Children Mark + .byte 6 # Abbrev [6] 0x6f:0xd DW_TAG_subprogram + .long .Linfo_string3 # DW_AT_name + .byte 1 # DW_AT_decl_file + .byte 1 # DW_AT_decl_line + .long 124 # DW_AT_type + # DW_AT_declaration + # DW_AT_external + .byte 7 # Abbrev [7] 0x7a:0x1 DW_TAG_unspecified_parameters + .byte 0 # End Of Children Mark + .byte 8 # Abbrev [8] 0x7c:0x7 DW_TAG_base_type + .long .Linfo_string4 # DW_AT_name + .byte 5 # DW_AT_encoding + .byte 4 # DW_AT_byte_size + .byte 0 # End Of Children Mark +.Ldebug_info_end0: + .ident "clang version 11.0.0" + .section ".note.GNU-stack","",@progbits + .addrsig + .section .debug_line,"",@progbits +.Lline_table_start0: Index: llvm/tools/llvm-dwarfdump/CMakeLists.txt =================================================================== --- llvm/tools/llvm-dwarfdump/CMakeLists.txt +++ llvm/tools/llvm-dwarfdump/CMakeLists.txt @@ -9,6 +9,7 @@ add_llvm_tool(llvm-dwarfdump Statistics.cpp + SectionSizes.cpp llvm-dwarfdump.cpp ) Index: llvm/tools/llvm-dwarfdump/SectionSizes.cpp =================================================================== --- /dev/null +++ llvm/tools/llvm-dwarfdump/SectionSizes.cpp @@ -0,0 +1,226 @@ +//===- SectionSizes.cpp ----------------------------------------- *- 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 +// +//===----------------------------------------------------------------------===// + +#include "llvm/DebugInfo/DIContext.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/WithColor.h" + +#define DEBUG_TYPE "dwarfdump" +using namespace llvm; +using namespace object; + +/// Holds cumulative section sizes for an object file. +struct SectionSizes { + /// Total number of bytes of the object file. + /// This includes .text, .data, .bss and all .debug_* sections. + uint64_t TotalObjectSize = 0; + /// Total number of bytes of all .debug_* sections. + uint64_t TotalDebugSectionsSize = 0; + /// Total number of bytes of the .debug_info section. + uint64_t TotalDebugInfo = 0; + /// Total number of bytes of the .debug_str section. + uint64_t TotalDebugStr = 0; + /// Total number of bytes of the .debug_abbrev section. + uint64_t TotalDebugAbbrev = 0; + /// Total number of bytes of the .debug_line section. + uint64_t TotalDebugLine = 0; + /// Total number of bytes of the .debug_ranges section. + uint64_t TotalDebugRanges = 0; + /// Total number of bytes of the .debug_loc section. + uint64_t TotalDebugLoc = 0; + /// Total number of bytes of the .debug_frame section. + uint64_t TotalDebugFrame = 0; + /// Total number of bytes of the .debug_macinfo section. + uint64_t TotalDebugMacInfo = 0; + /// Total number of bytes of the .debug_pubnames section. + uint64_t TotalDebugPubnames = 0; + /// Total number of bytes of the .debug_pubtypes section. + uint64_t TotalDebugPubtypes = 0; + /// Total number of bytes of the .debug_types section. + uint64_t TotalDebugTypes = 0; +}; + +static void prettyPrintTheSecsSizes(const ObjectFile &Obj, + const SectionSizes &Sizes, + raw_ostream &OS) { + OS << "----------------------------------------------------" + << "\n"; + OS << " SECTION \t\t" + << " SIZE" + << "\n"; + OS << "------------ \t\t ------------\n"; + + OS << " .debug_info \t\t " << Sizes.TotalDebugInfo << "\n"; + OS << " .debug_str \t\t " << Sizes.TotalDebugStr << "\n"; + OS << " .debug_abbrev \t\t " << Sizes.TotalDebugAbbrev << "\n"; + OS << " .debug_line \t\t " << Sizes.TotalDebugLine << "\n"; + OS << " .debug_ranges \t\t " << Sizes.TotalDebugRanges << "\n"; + OS << " .debug_loc \t\t " << Sizes.TotalDebugLoc << "\n"; + OS << " .debug_frame \t\t " << Sizes.TotalDebugFrame << "\n"; + OS << " .debug_macinfo \t " << Sizes.TotalDebugMacInfo << "\n"; + OS << " .debug_pubnames \t " << Sizes.TotalDebugPubnames << "\n"; + OS << " .debug_pubtypes \t " << Sizes.TotalDebugPubtypes << "\n"; + OS << " .debug_types \t\t " << Sizes.TotalDebugTypes << "\n\n"; + OS << " .debug_*: \t\t " << Sizes.TotalDebugSectionsSize << "\n"; + OS << "----------------------------------------------------" + << "\n"; + OS << " TOTAL FILE SIZE: " << Sizes.TotalObjectSize << "\n"; + OS << "----------------------------------------------------" + << "\n"; +} + +static bool isDebugInfo(StringRef SectionName) { + if (SectionName == ".debug_info") + return true; + return false; +} + +static bool isDebugStr(StringRef SectionName) { + if (SectionName == ".debug_str") + return true; + return false; +} + +static bool isDebugAbbrev(StringRef SectionName) { + if (SectionName == ".debug_abbrev") + return true; + return false; +} + +static bool isDebugLine(StringRef SectionName) { + if (SectionName == ".debug_line") + return true; + return false; +} + +static bool isDebugRanges(StringRef SectionName) { + if (SectionName == ".debug_ranges") + return true; + return false; +} + +static bool isDebugLoc(StringRef SectionName) { + if (SectionName == ".debug_loc") + return true; + return false; +} + +static bool isDebugFrame(StringRef SectionName) { + if (SectionName == ".debug_frame") + return true; + return false; +} + +static bool isDebugMacInfo(StringRef SectionName) { + if (SectionName == ".debug_macinfo") + return true; + return false; +} + +static bool isDebugPubnames(StringRef SectionName) { + if (SectionName == ".debug_pubnames") + return true; + return false; +} + +static bool isDebugPubtypes(StringRef SectionName) { + if (SectionName == ".debug_pubtypes") + return true; + return false; +} + +static bool isDebugTypes(StringRef SectionName) { + if (SectionName == ".debug_types") + return true; + return false; +} + +static bool isDebugSection(StringRef SectionName) { + if (isDebugInfo(SectionName) || isDebugStr(SectionName) || + isDebugAbbrev(SectionName) || isDebugLine(SectionName) || + isDebugRanges(SectionName) || isDebugLoc(SectionName) || + isDebugFrame(SectionName) || isDebugMacInfo(SectionName) || + isDebugPubnames(SectionName) || isDebugPubtypes(SectionName) || + isDebugTypes(SectionName)) + return true; + return false; +} + +static bool calculateSizes(const ObjectFile &Obj, SectionSizes &Sizes) { + for (const SectionRef &Section : Obj.sections()) { + StringRef SectionName; + if (Expected NameOrErr = Section.getName()) { + SectionName = *NameOrErr; + } else { + consumeError(NameOrErr.takeError()); + return false; + } + + LLVM_DEBUG(dbgs() << SectionName.str() << ": " << Section.getSize()); + + if (Section.isBerkeleyText() || Section.isBerkeleyData() || + Section.isBSS() || isDebugSection(SectionName)) + Sizes.TotalObjectSize += Section.getSize(); + + if (isDebugSection(SectionName)) + Sizes.TotalDebugSectionsSize += Section.getSize(); + + if (isDebugInfo(SectionName)) { + Sizes.TotalDebugInfo += Section.getSize(); + } else if (isDebugStr(SectionName)) { + Sizes.TotalDebugStr += Section.getSize(); + } else if (isDebugAbbrev(SectionName)) { + Sizes.TotalDebugAbbrev += Section.getSize(); + } else if (isDebugLine(SectionName)) { + Sizes.TotalDebugLine += Section.getSize(); + } else if (isDebugRanges(SectionName)) { + Sizes.TotalDebugRanges += Section.getSize(); + } else if (isDebugLoc(SectionName)) { + Sizes.TotalDebugLoc += Section.getSize(); + } else if (isDebugFrame(SectionName)) { + Sizes.TotalDebugFrame += Section.getSize(); + } else if (isDebugMacInfo(SectionName)) { + Sizes.TotalDebugMacInfo += Section.getSize(); + } else if (isDebugPubnames(SectionName)) { + Sizes.TotalDebugPubnames += Section.getSize(); + } else if (isDebugPubtypes(SectionName)) { + Sizes.TotalDebugPubtypes += Section.getSize(); + } else if (isDebugTypes(SectionName)) { + Sizes.TotalDebugTypes += Section.getSize(); + } + } + + return true; +} + +bool collectSecsSizesForObjectFile(ObjectFile &Obj, DWARFContext &DICtx, + Twine Filename, raw_ostream &OS) { + SectionSizes Sizes; + if (!calculateSizes(Obj, Sizes)) { + WithColor::error() << "Unable to find sections within " << Filename.str() + << "\n"; + exit(1); + } + + OS << "----------------------------------------------------" + << "\n"; + OS << Filename.str() << ": file format " << Obj.getFileFormatName() << "\n"; + + uint64_t TotalCUs = 0; + for (const auto &CU : static_cast(&DICtx)->compile_units()) { + (void)CU; + ++TotalCUs; + } + + LLVM_DEBUG(dbgs() << "Total num of compile units: " << TotalCUs << "\n"); + + prettyPrintTheSecsSizes(Obj, Sizes, OS); + + return true; +} 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 + ShowSecSizes("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, Twine Filename, raw_ostream &OS); +bool collectSecsSizesForObjectFile(ObjectFile &Obj, DWARFContext &DICtx, + Twine Filename, raw_ostream &OS); + static bool dumpObjectFile(ObjectFile &Obj, DWARFContext &DICtx, 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 (ShowSecSizes) { + for (auto Object : Objects) + handleFile(Object, collectSecsSizesForObjectFile, OutputFile.os()); + } else { for (auto Object : Objects) handleFile(Object, dumpObjectFile, OutputFile.os()); + } return EXIT_SUCCESS; }