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/show_section_sizes.s =================================================================== --- /dev/null +++ llvm/test/tools/llvm-dwarfdump/X86/show_section_sizes.s @@ -0,0 +1,268 @@ +# RUN: llvm-mc -triple x86_64-pc-linux %s -filetype=obj | llvm-dwarfdump --show-section-sizes - | FileCheck %s + +# CHECK: .debug_abbrev 120 +# CHECK: .debug_info 132 +# CHECK: .debug_line 76 +# CHECK: .debug_loc 89 +# CHECK: .debug_str 45 +# CHECK: all debug sections: 462 +# CHECK: TOTAL FILE SIZE: 1376 + + .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,112 @@ +//===-- 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 "llvm/DebugInfo/DIContext.h" +#include "llvm/DebugInfo/DWARF/DWARFContext.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/WithColor.h" + +#include + +#define DEBUG_TYPE "dwarfdump" +using namespace llvm; +using namespace object; + +using SectionSizeMap = std::map; + +/// Holds cumulative section sizes for an object file. +struct SectionSizes { + /// Total number of bytes of all sections. + uint64_t TotalObjectSize = 0; + /// Total number of bytes of all debug sections. + uint64_t TotalDebugSectionsSize = 0; + /// Map the .debug section and total number of bytes of the debug section. + SectionSizeMap DebugSectionSizes; +}; + +static void prettyPrintTheSecsSizes(const ObjectFile &Obj, + const SectionSizes &Sizes, + raw_ostream &OS) { + OS << "----------------------------------------------------" + << "\n"; + OS << " SECTION \t\t\t SIZE\n"; + OS << "------------ \t\t -----------------------\n"; + + for (const auto &DebugSec : Sizes.DebugSectionSizes) { + OS << " " << DebugSec.first << " \t\t " << DebugSec.second << " (" + << format("%0.2f", DebugSec.second / (double)Sizes.TotalObjectSize * 100) + << "%)\n"; + } + OS << "\n"; + OS << " all debug sections: \t " << Sizes.TotalDebugSectionsSize << " (" + << format("%0.2f", Sizes.TotalDebugSectionsSize / + (double)Sizes.TotalObjectSize * 100) + << "%)\n"; + OS << "----------------------------------------------------" + << "\n"; + OS << " TOTAL FILE SIZE: " << Sizes.TotalObjectSize << "\n"; + OS << "----------------------------------------------------" + << "\n"; +} + +static bool isDebugSection(StringRef SectionName) { + return SectionName.startswith(".debug") || + SectionName.startswith(".zdebug") || SectionName == ".gdb_index"; +} + +static bool calculateSizes(const ObjectFile &Obj, SectionSizes &Sizes) { + for (const SectionRef &Section : Obj.sections()) { + StringRef SectionName; + if (Expected NameOrErr = Section.getName()) { + SectionName = *NameOrErr; + } else { + WithColor::error() << errorToErrorCode(NameOrErr.takeError()).message() + << "\n"; + return false; + } + + LLVM_DEBUG(dbgs() << SectionName.str() << ": " << Section.getSize()); + + // Include sizes of all sections. + Sizes.TotalObjectSize += Section.getSize(); + + if (!isDebugSection(SectionName)) + continue; + + Sizes.TotalDebugSectionsSize += Section.getSize(); + Sizes.DebugSectionSizes[SectionName] += 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; }