* [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines @ 2024-10-16 15:47 Andrew Burgess 2024-10-17 20:03 ` Tom Tromey ` (2 more replies) 0 siblings, 3 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-16 15:47 UTC (permalink / raw) To: gdb-patches; +Cc: Andrew Burgess, Bernd Edlinger Bernd, I'd like to propose this as an alternative to your existing entry_pc patch[1]. I've included the tests from your original proposal here, and I've tested with gcc's ranging from 8.4 to 14.2. Instead of permuting the range order within the block to bring the entry pc to the front I instead store the entry pc within the block object itself. I collect the entry-pc in all cases, not just for inline subroutines, and I've added support for the constant form (introduced in DWARF-5). I've moved the test into gdb.opt as it's an optimised code test, and I've added a bunch of tests using the DWARF assembler into gdb.dwarf2/, which should cover most (all) of the supported ways in which the entry pc can be specified in GDB right now. I'm continuing to look at the other parts of your optimised code debug patch series. I'd love to hear your (or anyone else's) thoughts. Thanks, Andrew [1] https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com --- The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set to a specific value, this is the value returned. I have not removed the old code in block::entry_pc(). If the specific entry PC value was not set then we still fall back on the old approach of just returning the base address. Doing this feels more useful than just claiming there is no entry PC. This should also mean that we shouldn't see any change in behaviour for compilers that don't emit the DW_AT_entry_pc, or for debug readers (e.g. stabs) that don't set the entry PC value within a block. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> --- gdb/block.h | 36 +- gdb/dwarf2/read.c | 83 +++- gdb/objfiles.c | 1 + gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ 7 files changed, 737 insertions(+), 13 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp diff --git a/gdb/block.h b/gdb/block.h index c3babad52f3..e8f63de1492 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -179,27 +179,35 @@ struct block : public allocate_on_obstack<block> /* Return the "entry PC" of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. - - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + If the entry PC has been set to a specific value then this is + returned. Otherwise, the entry PC is the lowest (start) address for + the block when all addresses within the block are contiguous. If + non-contiguous, then use the start address for the first range in the + block. - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry PC + wherever possible, so this non-standard fallback case is only used as + a last resort. */ CORE_ADDR entry_pc () const { - if (this->is_contiguous ()) + if (m_entry_pc.has_value ()) + return *m_entry_pc; + else if (this->is_contiguous ()) return this->start (); else return this->ranges ()[0].start (); } + /* Set this block's entry PC. */ + + void set_entry_pc (CORE_ADDR addr) + { + m_entry_pc = addr; + } + /* Return the objfile of this block. */ struct objfile *objfile () const; @@ -344,6 +352,10 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* If set this is the entry PC for the block. */ + + std::optional<CORE_ADDR> m_entry_pc; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index ea31d8dd851..c534664dff7 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11300,8 +11300,87 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + block->set_entry_pc (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + { + CORE_ADDR entry_pc = per_objfile->relocate (attr->as_address ()); + block->set_entry_pc (entry_pc); + } + } + else + { + std::optional<CORE_ADDR> entry + = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + block->set_entry_pc (entry.value ()); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11356,6 +11435,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/objfiles.c b/gdb/objfiles.c index 0e076fe36be..f64d822bebf 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -626,6 +626,7 @@ objfile_relocate1 (struct objfile *objfile, { b->set_start (b->start () + delta[block_line_section]); b->set_end (b->end () + delta[block_line_section]); + b->set_entry_pc (b->entry_pc () + delta[block_line_section]); for (blockrange &r : b->ranges ()) { diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..f31e1fa5d7b --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,487 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. It will make things easier later if +# we convert this to an actual address. +set foo_start [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start\.\.$::foo_end" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $::foo_middle_addr addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::foo_start] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end $::r1_s $::r1_e + start_end $::r2_s $::r2_e + start_end $::r3_s $::r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range $::r1_s $::r1_e + range $::r2_s $::r2_e + range $::r3_s $::r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $::foo_middle_addr addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end $::r1_s $::r1_e + start_end $::r2_s $::r2_e + start_end $::r3_s $::r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range $::r1_s $::r1_e + range $::r2_s $::r2_e + range $::r3_s $::r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end $::r1_s $::r1_e + start_end $::r2_s $::r2_e + start_end $::r3_s $::r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range $::r1_s $::r1_e + range $::r2_s $::r2_e + range $::r3_s $::r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..2427ebbfd3f --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,51 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end base-commit: 2dbb779c83f4825619ef171ccaee9a3f664d5a34 -- 2.25.4 ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-16 15:47 [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines Andrew Burgess @ 2024-10-17 20:03 ` Tom Tromey 2024-10-18 10:06 ` Andrew Burgess 2024-10-18 10:26 ` Gerlicher, Klaus 2024-10-18 13:53 ` [PATCHv2] " Andrew Burgess 2024-10-18 14:24 ` [PATCH] " Bernd Edlinger 2 siblings, 2 replies; 41+ messages in thread From: Tom Tromey @ 2024-10-17 20:03 UTC (permalink / raw) To: Andrew Burgess; +Cc: gdb-patches, Bernd Edlinger >>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: Andrew> This commit extends the block class to carry the entry PC as a Andrew> separate member variable. Then the DWARF reader is extended to read Andrew> and set the entry PC for the block. Now in block::entry_pc(), if the Andrew> entry PC has been set to a specific value, this is the value returned. I wonder what the size cost is of adding a field to block -- I mean the overall cost, not just sizeof(block). But I suppose in the end it's not very important, since we normally operate on the assumption that most CUs won't be expanded. Andrew> CORE_ADDR entry_pc () const Andrew> { Andrew> - if (this->is_contiguous ()) Andrew> + if (m_entry_pc.has_value ()) Andrew> + return *m_entry_pc; Andrew> + else if (this->is_contiguous ()) Andrew> return this->start (); Andrew> else Andrew> return this->ranges ()[0].start (); Andrew> + Andrew> + /* If set this is the entry PC for the block. */ Andrew> + Andrew> + std::optional<CORE_ADDR> m_entry_pc; Andrew> }; std::optional here means carrying an extra word for just the flag bit. However I wonder if it would work to use CORE_ADDR here and use 0 as a sentinel to mean unset; or some other scheme making it so the entry PC and the 'start' are identical by default. Tom ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-17 20:03 ` Tom Tromey @ 2024-10-18 10:06 ` Andrew Burgess 2024-10-18 13:57 ` Andrew Burgess 2024-10-18 10:26 ` Gerlicher, Klaus 1 sibling, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-10-18 10:06 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches, Bernd Edlinger Tom Tromey <tom@tromey.com> writes: >>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: > > Andrew> This commit extends the block class to carry the entry PC as a > Andrew> separate member variable. Then the DWARF reader is extended to read > Andrew> and set the entry PC for the block. Now in block::entry_pc(), if the > Andrew> entry PC has been set to a specific value, this is the value returned. > > I wonder what the size cost is of adding a field to block -- I mean the > overall cost, not just sizeof(block). > > But I suppose in the end it's not very important, since we normally > operate on the assumption that most CUs won't be expanded. > > Andrew> CORE_ADDR entry_pc () const > Andrew> { > Andrew> - if (this->is_contiguous ()) > Andrew> + if (m_entry_pc.has_value ()) > Andrew> + return *m_entry_pc; > Andrew> + else if (this->is_contiguous ()) > Andrew> return this->start (); > Andrew> else > Andrew> return this->ranges ()[0].start (); > > Andrew> + > Andrew> + /* If set this is the entry PC for the block. */ > Andrew> + > Andrew> + std::optional<CORE_ADDR> m_entry_pc; > Andrew> }; > > std::optional here means carrying an extra word for just the flag bit. > > However I wonder if it would work to use CORE_ADDR here and use 0 as a > sentinel to mean unset; or some other scheme making it so the entry PC > and the 'start' are identical by default. Having worked on targets where address 0 was valid, I really don't like using "special" addresses. If all attributes of the block were set just once at creation time then we could for sure calculate the entry-pc then ... but blocks are built piece by piece ... and then updated later piece by piece when we relocate them, so it's hard to know (I think) if the entry-pc was set to a specific value, or should pick up the default value. I'll take another pass on this and see what I can come up with. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-18 10:06 ` Andrew Burgess @ 2024-10-18 13:57 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-18 13:57 UTC (permalink / raw) To: Tom Tromey; +Cc: gdb-patches, Bernd Edlinger Andrew Burgess <aburgess@redhat.com> writes: > Tom Tromey <tom@tromey.com> writes: > >>>>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: >> >> Andrew> This commit extends the block class to carry the entry PC as a >> Andrew> separate member variable. Then the DWARF reader is extended to read >> Andrew> and set the entry PC for the block. Now in block::entry_pc(), if the >> Andrew> entry PC has been set to a specific value, this is the value returned. >> >> I wonder what the size cost is of adding a field to block -- I mean the >> overall cost, not just sizeof(block). >> >> But I suppose in the end it's not very important, since we normally >> operate on the assumption that most CUs won't be expanded. >> >> Andrew> CORE_ADDR entry_pc () const >> Andrew> { >> Andrew> - if (this->is_contiguous ()) >> Andrew> + if (m_entry_pc.has_value ()) >> Andrew> + return *m_entry_pc; >> Andrew> + else if (this->is_contiguous ()) >> Andrew> return this->start (); >> Andrew> else >> Andrew> return this->ranges ()[0].start (); >> >> Andrew> + >> Andrew> + /* If set this is the entry PC for the block. */ >> Andrew> + >> Andrew> + std::optional<CORE_ADDR> m_entry_pc; >> Andrew> }; >> >> std::optional here means carrying an extra word for just the flag bit. >> >> However I wonder if it would work to use CORE_ADDR here and use 0 as a >> sentinel to mean unset; or some other scheme making it so the entry PC >> and the 'start' are identical by default. > > Having worked on targets where address 0 was valid, I really don't like > using "special" addresses. If all attributes of the block were set just > once at creation time then we could for sure calculate the entry-pc then > ... but blocks are built piece by piece ... and then updated later piece > by piece when we relocate them, so it's hard to know (I think) if the > entry-pc was set to a specific value, or should pick up the default > value. > > I'll take another pass on this and see what I can come up with. I looked at this for a bit. Other than Klaus' idea of removing m_start and m_end to claw back more space than I'm consuming I can't see anyway to keep the block size down. I posted v2 switching to use 0 as a special marker value, this means that (on a 64-bit target) the block size increases by 8-bytes rather than the 16-bytes with the original patch. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* RE: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-17 20:03 ` Tom Tromey 2024-10-18 10:06 ` Andrew Burgess @ 2024-10-18 10:26 ` Gerlicher, Klaus 2024-10-18 13:55 ` Andrew Burgess 1 sibling, 1 reply; 41+ messages in thread From: Gerlicher, Klaus @ 2024-10-18 10:26 UTC (permalink / raw) To: Tom Tromey, Andrew Burgess; +Cc: gdb-patches, Bernd Edlinger Hi Andrew, Tom, Just an observation on the concern for size of the block struct. Inline. > -----Original Message----- > From: Tom Tromey <tom@tromey.com> > Sent: Thursday, October 17, 2024 10:03 PM > To: Andrew Burgess <aburgess@redhat.com> > Cc: gdb-patches@sourceware.org; Bernd Edlinger > <bernd.edlinger@hotmail.de> > Subject: Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined > subroutines > > >>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: > > Andrew> This commit extends the block class to carry the entry PC as a > Andrew> separate member variable. Then the DWARF reader is extended to > read > Andrew> and set the entry PC for the block. Now in block::entry_pc(), if the > Andrew> entry PC has been set to a specific value, this is the value returned. > > I wonder what the size cost is of adding a field to block -- I mean the > overall cost, not just sizeof(block). > > But I suppose in the end it's not very important, since we normally > operate on the assumption that most CUs won't be expanded. > > Andrew> CORE_ADDR entry_pc () const > Andrew> { > Andrew> - if (this->is_contiguous ()) > Andrew> + if (m_entry_pc.has_value ()) > Andrew> + return *m_entry_pc; > Andrew> + else if (this->is_contiguous ()) > Andrew> return this->start (); > Andrew> else > Andrew> return this->ranges ()[0].start (); > > Andrew> + > Andrew> + /* If set this is the entry PC for the block. */ > Andrew> + > Andrew> + std::optional<CORE_ADDR> m_entry_pc; > Andrew> }; > > std::optional here means carrying an extra word for just the flag bit. > > However I wonder if it would work to use CORE_ADDR here and use 0 as a > sentinel to mean unset; or some other scheme making it so the entry PC > and the 'start' are identical by default. > If size of a block struct is a concern, couldn't we remove m_start and m_end in favour of only using the m_ranges member? This comment seems to indicate that they are kind of exclusive... /* Address ranges for blocks with non-contiguous ranges. If this is NULL, then there is only one range which is specified by startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; Thanks Klaus > Tom Intel Deutschland GmbH Registered Address: Am Campeon 10, 85579 Neubiberg, Germany Tel: +49 89 99 8853-0, www.intel.de Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau Registered Office: Munich Commercial Register: Amtsgericht Muenchen HRB 186928 ^ permalink raw reply [flat|nested] 41+ messages in thread
* RE: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-18 10:26 ` Gerlicher, Klaus @ 2024-10-18 13:55 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-18 13:55 UTC (permalink / raw) To: Gerlicher, Klaus, Tom Tromey; +Cc: gdb-patches, Bernd Edlinger "Gerlicher, Klaus" <klaus.gerlicher@intel.com> writes: > Hi Andrew, Tom, > > Just an observation on the concern for size of the block struct. Inline. > >> -----Original Message----- >> From: Tom Tromey <tom@tromey.com> >> Sent: Thursday, October 17, 2024 10:03 PM >> To: Andrew Burgess <aburgess@redhat.com> >> Cc: gdb-patches@sourceware.org; Bernd Edlinger >> <bernd.edlinger@hotmail.de> >> Subject: Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined >> subroutines >> >> >>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: >> >> Andrew> This commit extends the block class to carry the entry PC as a >> Andrew> separate member variable. Then the DWARF reader is extended to >> read >> Andrew> and set the entry PC for the block. Now in block::entry_pc(), if the >> Andrew> entry PC has been set to a specific value, this is the value returned. >> >> I wonder what the size cost is of adding a field to block -- I mean the >> overall cost, not just sizeof(block). >> >> But I suppose in the end it's not very important, since we normally >> operate on the assumption that most CUs won't be expanded. >> >> Andrew> CORE_ADDR entry_pc () const >> Andrew> { >> Andrew> - if (this->is_contiguous ()) >> Andrew> + if (m_entry_pc.has_value ()) >> Andrew> + return *m_entry_pc; >> Andrew> + else if (this->is_contiguous ()) >> Andrew> return this->start (); >> Andrew> else >> Andrew> return this->ranges ()[0].start (); >> >> Andrew> + >> Andrew> + /* If set this is the entry PC for the block. */ >> Andrew> + >> Andrew> + std::optional<CORE_ADDR> m_entry_pc; >> Andrew> }; >> >> std::optional here means carrying an extra word for just the flag bit. >> >> However I wonder if it would work to use CORE_ADDR here and use 0 as a >> sentinel to mean unset; or some other scheme making it so the entry PC >> and the 'start' are identical by default. >> > > If size of a block struct is a concern, couldn't we remove m_start and m_end in favour of only using > the m_ranges member? This comment seems to indicate that they are kind of exclusive... > > /* Address ranges for blocks with non-contiguous ranges. If this > is NULL, then there is only one range which is specified by > startaddr and endaddr above. */ > > struct blockranges *m_ranges = nullptr; I did take a brief look at this. I agree that it seems like we should be able to drop these fields, but I don't think it will be a trivial change. For now I've just switched to using 0 as a special case, hopefully that will be enough for now. Thanks, Andrew > > Thanks > Klaus > >> Tom > > Intel Deutschland GmbH > Registered Address: Am Campeon 10, 85579 Neubiberg, Germany > Tel: +49 89 99 8853-0, www.intel.de > Managing Directors: Sean Fennelly, Jeffrey Schneiderman, Tiffany Doon Silva > Chairperson of the Supervisory Board: Nicole Lau > Registered Office: Munich > Commercial Register: Amtsgericht Muenchen HRB 186928 ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCHv2] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-16 15:47 [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines Andrew Burgess 2024-10-17 20:03 ` Tom Tromey @ 2024-10-18 13:53 ` Andrew Burgess 2024-10-28 13:45 ` [PATCHv3] " Andrew Burgess 2024-10-18 14:24 ` [PATCH] " Bernd Edlinger 2 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-10-18 13:53 UTC (permalink / raw) To: gdb-patches; +Cc: Andrew Burgess, Tom Tromey, Bernd Edlinger In v2: - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead just use CORE_ADDR. This does mean that targets where 0 is a valid code address might run into problems, but those targets likely have plenty of other problems, and I'd rather land this fix than get hung up on this edge case. In the future it might be possible to remove block::m_start and block::m_end as Klaus suggested, but this doesn't look like a simple task. If we did drop those fields then it might be possible to space a word for some flag bits which would allow us to remove the 0 address special case. - Fixes to the DWARF assembler testcase to handle compiling with '-pie', the Linaro CI exposed these issues. The changes are pretty minor, mostly using label names rather than addresses when building the DWARF. --- The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set to a specific value, this is the value returned. I have not removed the old code in block::entry_pc(). If the specific entry PC value was not set then we still fall back on the old approach of just returning the base address. Doing this feels more useful than just claiming there is no entry PC. This should also mean that we shouldn't see any change in behaviour for compilers that don't emit the DW_AT_entry_pc, or for debug readers (e.g. stabs) that don't set the entry PC value within a block. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> --- gdb/block.h | 44 +- gdb/dwarf2/read.c | 83 +++- gdb/objfiles.c | 1 + gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ 7 files changed, 745 insertions(+), 13 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp diff --git a/gdb/block.h b/gdb/block.h index c3babad52f3..3bd2c515425 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -179,27 +179,35 @@ struct block : public allocate_on_obstack<block> /* Return the "entry PC" of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. - - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + If the entry PC has been set to a specific value then this is + returned. Otherwise, the entry PC is the lowest (start) address for + the block when all addresses within the block are contiguous. If + non-contiguous, then use the start address for the first range in the + block. - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry PC + wherever possible, so this non-standard fallback case is only used as + a last resort. */ CORE_ADDR entry_pc () const { - if (this->is_contiguous ()) + if (m_entry_pc != 0) + return m_entry_pc; + else if (this->is_contiguous ()) return this->start (); else return this->ranges ()[0].start (); } + /* Set this block's entry PC. */ + + void set_entry_pc (CORE_ADDR addr) + { + m_entry_pc = addr; + } + /* Return the objfile of this block. */ struct objfile *objfile () const; @@ -344,6 +352,18 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* The entry address for this block. The value 0 is special and + indicates that the entry address has not been set. + + Using 0 as a special value is not ideal, targets for which 0 is a + valid code address might run into problems if they want to use 0 as a + block's entry address, but the alternative is to carry a flag + indicating if m_entry_pc is valid or not, but that would make 'struct + block' even bigger, and we want to keep 'struct block' as small as + possible (we might have a lot of blocks). */ + + CORE_ADDR m_entry_pc = 0; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index ea31d8dd851..c534664dff7 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11300,8 +11300,87 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + block->set_entry_pc (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + { + CORE_ADDR entry_pc = per_objfile->relocate (attr->as_address ()); + block->set_entry_pc (entry_pc); + } + } + else + { + std::optional<CORE_ADDR> entry + = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + block->set_entry_pc (entry.value ()); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11356,6 +11435,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/objfiles.c b/gdb/objfiles.c index 0e076fe36be..f64d822bebf 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -626,6 +626,7 @@ objfile_relocate1 (struct objfile *objfile, { b->set_start (b->start () + delta[block_line_section]); b->set_end (b->end () + delta[block_line_section]); + b->set_entry_pc (b->entry_pc () + delta[block_line_section]); for (blockrange &r : b->ranges ()) { diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..718848d9b6d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,487 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. To check the 'maint info blocks' +# output we need these converted into actual addresses. +set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::foo_start_addr] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..2427ebbfd3f --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,51 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end base-commit: 2dbb779c83f4825619ef171ccaee9a3f664d5a34 -- 2.25.4 ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCHv3] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-18 13:53 ` [PATCHv2] " Andrew Burgess @ 2024-10-28 13:45 ` Andrew Burgess 2024-10-29 14:49 ` [PATCHv4] " Andrew Burgess 2024-10-29 15:29 ` [PATCHv3] " Sam James 0 siblings, 2 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-28 13:45 UTC (permalink / raw) To: gdb-patches; +Cc: Andrew Burgess, Bernd Edlinger In v3: - Entry PC values are now clamped to the block's start/end range. This was causing failures in the self-tests when GDB was compiled with GCC using default optimisation, as GCC emits an (invalid?) entry-pc which is outside the blocks start/end range. In v2: - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead just use CORE_ADDR. This does mean that targets where 0 is a valid code address might run into problems, but those targets likely have plenty of other problems, and I'd rather land this fix than get hung up on this edge case. In the future it might be possible to remove block::m_start and block::m_end as Klaus suggested, but this doesn't look like a simple task. If we did drop those fields then it might be possible to space a word for some flag bits which would allow us to remove the 0 address special case. - Fixes to the DWARF assembler testcase to handle compiling with '-pie', the Linaro CI exposed these issues. The changes are pretty minor, mostly using label names rather than addresses when building the DWARF. --- The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set to a specific value, this is the value returned. I have not removed the old code in block::entry_pc(). If the specific entry PC value was not set then we still fall back on the old approach of just returning the base address. Doing this feels more useful than just claiming there is no entry PC. This should also mean that we shouldn't see any change in behaviour for compilers that don't emit the DW_AT_entry_pc, or for debug readers (e.g. stabs) that don't set the entry PC value within a block. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. There was one issue that I ran into when testing this patch. GCC seems to be quite bad at tracking code ranges when the default level of optimisation is applied. As a result I was seeing failures in gdb.gdb/python-helper.exp and gdb.gdb/selftest.exp when we debug the generated GDB executable. What happens is that the DW_AT_entry_pc is given as an entry address which is outside of any of the blocks ranges. Previously we ignored the DW_AT_entry_pc and when we later ask for the entry-pc we just report the first range's start address. After this commit we track the declared entry-pc, which is then what we report. What this means is that when the user (or DejaGNU) tries to place a breakpoint on an inline function, the breakpoint is placed at the entry-pc. Then when GDB stops at this address and tries to figure out which block it's in, GDB doesn't think it's actually within the inline function that the user asked to stop in. In the test I mention about we place a breakpoint within GDB's 'captured_main' function. In the builds I was looked at, this function was inline within 'gdb_main', but the DW_AT_entry_pc for the 'captured_main' function was not within the declared ranges of 'captured_main'. As a result when the breakpoint was hit, GDB would report the inferior as within 'gdb_main' instead of 'captured_main'. For now I propose fixing this by checking the entry-pc against the blocks start address. If the entry-pc is before the start address then we move the entry-pc to be the start address. This effectively restores the pre-patch behaviour for this problem case. In the future I think we might be able to do better than this, I'd like to have GDB examine the line table and then possibly extend the block's ranges to start at the entry-pc, rather than moving the entry-pc, but this will require additional infrastructure changes in GDB. As the fix I propose here is basically "maintain the status quo" I'm hoping this will be acceptable, and a better solution can be added later. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> --- gdb/block.h | 44 +- gdb/dwarf2/read.c | 105 ++++- gdb/objfiles.c | 1 + gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ 7 files changed, 767 insertions(+), 13 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp diff --git a/gdb/block.h b/gdb/block.h index c3babad52f3..3bd2c515425 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -179,27 +179,35 @@ struct block : public allocate_on_obstack<block> /* Return the "entry PC" of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. - - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + If the entry PC has been set to a specific value then this is + returned. Otherwise, the entry PC is the lowest (start) address for + the block when all addresses within the block are contiguous. If + non-contiguous, then use the start address for the first range in the + block. - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry PC + wherever possible, so this non-standard fallback case is only used as + a last resort. */ CORE_ADDR entry_pc () const { - if (this->is_contiguous ()) + if (m_entry_pc != 0) + return m_entry_pc; + else if (this->is_contiguous ()) return this->start (); else return this->ranges ()[0].start (); } + /* Set this block's entry PC. */ + + void set_entry_pc (CORE_ADDR addr) + { + m_entry_pc = addr; + } + /* Return the objfile of this block. */ struct objfile *objfile () const; @@ -344,6 +352,18 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* The entry address for this block. The value 0 is special and + indicates that the entry address has not been set. + + Using 0 as a special value is not ideal, targets for which 0 is a + valid code address might run into problems if they want to use 0 as a + block's entry address, but the alternative is to carry a flag + indicating if m_entry_pc is valid or not, but that would make 'struct + block' even bigger, and we want to keep 'struct block' as small as + possible (we might have a lot of blocks). */ + + CORE_ADDR m_entry_pc = 0; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 6ac6f7c6a39..55d99dbbe57 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11305,8 +11305,109 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Filled with the entry-pc if we can find it. */ + std::optional<CORE_ADDR> entry; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + entry.emplace (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + entry.emplace (per_objfile->relocate (attr->as_address ())); + } + else + entry = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + { + CORE_ADDR entry_pc = entry.value (); + + /* We sometimes see GCC give an entry-pc that is just before the + start of (one of) a block's ranges. In the cases I've seen + we'd actually be better off moving the block's range start + address to include the entry-pc, but that requires some + addtional infrastructure changes, so for now we just adjust + the entry-pc to be the block's start address. + + Though this bug is known to occur with GCC, we don't include a + producer check, if this does happen with any producer then it's + going to cause problems, so lets work around it. + + Then we additionally check for ENTRY_PC being after the block's + end address. This has never been observed in the wild, but seems + like a cheap sanity check. If the entry_pc falls outside a + blocks range then this is going to cause GDB problems, so lets + avoid that. Using the block's start as the fallback in this case + just replicates GDB's default entry_pc calculation logic if the + entry-pc is not specified. */ + if (entry_pc < block->start () || entry_pc > block->end ()) + entry_pc = block->start (); + + block->set_entry_pc (entry_pc); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11361,6 +11462,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/objfiles.c b/gdb/objfiles.c index 555195dc61f..761a090b260 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -621,6 +621,7 @@ objfile_relocate1 (struct objfile *objfile, { b->set_start (b->start () + delta[block_line_section]); b->set_end (b->end () + delta[block_line_section]); + b->set_entry_pc (b->entry_pc () + delta[block_line_section]); for (blockrange &r : b->ranges ()) { diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..718848d9b6d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,487 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. To check the 'maint info blocks' +# output we need these converted into actual addresses. +set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::foo_start_addr] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..2427ebbfd3f --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,51 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end base-commit: a723c56efb07c4f8b3f6a3ed4b878a2f8f5572cc -- 2.25.4 ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-28 13:45 ` [PATCHv3] " Andrew Burgess @ 2024-10-29 14:49 ` Andrew Burgess 2024-10-29 15:28 ` Bernd Edlinger ` (2 more replies) 2024-10-29 15:29 ` [PATCHv3] " Sam James 1 sibling, 3 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-29 14:49 UTC (permalink / raw) To: gdb-patches; +Cc: Andrew Burgess, Bernd Edlinger In v4: - I still have one failed being reported from Linaro CI, gdb.stabs/gdb11479.exp. I've been unable to reproduce this failure. However, on re-reading this patch I did notice that, if the m_entry_pc field was not set then we would still end up giving it a value during relocation, this would result in m_entry_pc holding an address which had been relocated twice. Fixed this by moving the block's address relocation logic into the block class, and only relocating the m_entry_pc variable if it actually has been assigned a value. In v3: - Entry PC values are now clamped to the block's start/end range. This was causing failures in the self-tests when GDB was compiled with GCC using default optimisation, as GCC emits an (invalid?) entry-pc which is outside the blocks start/end range. In v2: - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead just use CORE_ADDR. This does mean that targets where 0 is a valid code address might run into problems, but those targets likely have plenty of other problems, and I'd rather land this fix than get hung up on this edge case. In the future it might be possible to remove block::m_start and block::m_end as Klaus suggested, but this doesn't look like a simple task. If we did drop those fields then it might be possible to space a word for some flag bits which would allow us to remove the 0 address special case. - Fixes to the DWARF assembler testcase to handle compiling with '-pie', the Linaro CI exposed these issues. The changes are pretty minor, mostly using label names rather than addresses when building the DWARF. --- The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set to a specific value, this is the value returned. I have not removed the old code in block::entry_pc(). If the specific entry PC value was not set then we still fall back on the old approach of just returning the base address. Doing this feels more useful than just claiming there is no entry PC. This should also mean that we shouldn't see any change in behaviour for compilers that don't emit the DW_AT_entry_pc, or for debug readers (e.g. stabs) that don't set the entry PC value within a block. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. There was one issue that I ran into when testing this patch. GCC seems to be quite bad at tracking code ranges when the default level of optimisation is applied. As a result I was seeing failures in gdb.gdb/python-helper.exp and gdb.gdb/selftest.exp when we debug the generated GDB executable. What happens is that the DW_AT_entry_pc is given as an entry address which is outside of any of the blocks ranges. Previously we ignored the DW_AT_entry_pc and when we later ask for the entry-pc we just report the first range's start address. After this commit we track the declared entry-pc, which is then what we report. What this means is that when the user (or DejaGNU) tries to place a breakpoint on an inline function, the breakpoint is placed at the entry-pc. Then when GDB stops at this address and tries to figure out which block it's in, GDB doesn't think it's actually within the inline function that the user asked to stop in. In the test I mention about we place a breakpoint within GDB's 'captured_main' function. In the builds I was looked at, this function was inline within 'gdb_main', but the DW_AT_entry_pc for the 'captured_main' function was not within the declared ranges of 'captured_main'. As a result when the breakpoint was hit, GDB would report the inferior as within 'gdb_main' instead of 'captured_main'. For now I propose fixing this by checking the entry-pc against the blocks start address. If the entry-pc is before the start address then we move the entry-pc to be the start address. This effectively restores the pre-patch behaviour for this problem case. In the future I think we might be able to do better than this, I'd like to have GDB examine the line table and then possibly extend the block's ranges to start at the entry-pc, rather than moving the entry-pc, but this will require additional infrastructure changes in GDB. As the fix I propose here is basically "maintain the status quo" I'm hoping this will be acceptable, and a better solution can be added later. As part of this commit I've moved the code that relocates a block's addresses from objfiles.c (function objfile_relocate1) into the block class as block::relocate_addresses(). This allows the entry-pc to be relocated correctly when this variable's value is optional. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> --- gdb/block.c | 19 + gdb/block.h | 49 ++- gdb/dwarf2/read.c | 105 ++++- gdb/objfiles.c | 9 +- gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ 8 files changed, 791 insertions(+), 21 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp diff --git a/gdb/block.c b/gdb/block.c index 6f608777854..969f8b815c8 100644 --- a/gdb/block.c +++ b/gdb/block.c @@ -118,6 +118,25 @@ block::inlined_p () const return function () != nullptr && function ()->is_inlined (); } +/* See block.h. */ + +void +block::relocate_addresses (CORE_ADDR delta) +{ + m_start += delta; + m_end += delta; + + if (m_entry_pc != 0) + m_entry_pc += delta; + + for (blockrange &r : this->ranges ()) + { + r.set_start (r.start () + delta); + r.set_end (r.end () + delta); + } +} + + /* A helper function that checks whether PC is in the blockvector BL. It returns the containing block if there is one, or else NULL. */ diff --git a/gdb/block.h b/gdb/block.h index c3babad52f3..cf953b58d7c 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -179,27 +179,35 @@ struct block : public allocate_on_obstack<block> /* Return the "entry PC" of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. - - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + If the entry PC has been set to a specific value then this is + returned. Otherwise, the entry PC is the lowest (start) address for + the block when all addresses within the block are contiguous. If + non-contiguous, then use the start address for the first range in the + block. - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry PC + wherever possible, so this non-standard fallback case is only used as + a last resort. */ CORE_ADDR entry_pc () const { - if (this->is_contiguous ()) + if (m_entry_pc != 0) + return m_entry_pc; + else if (this->is_contiguous ()) return this->start (); else return this->ranges ()[0].start (); } + /* Set this block's entry PC. */ + + void set_entry_pc (CORE_ADDR addr) + { + m_entry_pc = addr; + } + /* Return the objfile of this block. */ struct objfile *objfile () const; @@ -306,6 +314,11 @@ struct block : public allocate_on_obstack<block> bool contains (const struct block *a, bool allow_nested = false) const; + /* Adjust m_start, m_end, and m_entry_pc by DELTA. Also update the + start and end address of every range in m_ranges by DELTA. */ + + void relocate_addresses (CORE_ADDR delta); + private: /* If the namespace_info is NULL, allocate it via OBSTACK and @@ -344,6 +357,18 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* The entry address for this block. The value 0 is special and + indicates that the entry address has not been set. + + Using 0 as a special value is not ideal, targets for which 0 is a + valid code address might run into problems if they want to use 0 as a + block's entry address, but the alternative is to carry a flag + indicating if m_entry_pc is valid or not, but that would make 'struct + block' even bigger, and we want to keep 'struct block' as small as + possible (we might have a lot of blocks). */ + + CORE_ADDR m_entry_pc = 0; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 6ac6f7c6a39..55d99dbbe57 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11305,8 +11305,109 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Filled with the entry-pc if we can find it. */ + std::optional<CORE_ADDR> entry; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + entry.emplace (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + entry.emplace (per_objfile->relocate (attr->as_address ())); + } + else + entry = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + { + CORE_ADDR entry_pc = entry.value (); + + /* We sometimes see GCC give an entry-pc that is just before the + start of (one of) a block's ranges. In the cases I've seen + we'd actually be better off moving the block's range start + address to include the entry-pc, but that requires some + addtional infrastructure changes, so for now we just adjust + the entry-pc to be the block's start address. + + Though this bug is known to occur with GCC, we don't include a + producer check, if this does happen with any producer then it's + going to cause problems, so lets work around it. + + Then we additionally check for ENTRY_PC being after the block's + end address. This has never been observed in the wild, but seems + like a cheap sanity check. If the entry_pc falls outside a + blocks range then this is going to cause GDB problems, so lets + avoid that. Using the block's start as the fallback in this case + just replicates GDB's default entry_pc calculation logic if the + entry-pc is not specified. */ + if (entry_pc < block->start () || entry_pc > block->end ()) + entry_pc = block->start (); + + block->set_entry_pc (entry_pc); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11361,6 +11462,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/objfiles.c b/gdb/objfiles.c index 555195dc61f..d95505c0154 100644 --- a/gdb/objfiles.c +++ b/gdb/objfiles.c @@ -619,14 +619,7 @@ objfile_relocate1 (struct objfile *objfile, for (block *b : bv->blocks ()) { - b->set_start (b->start () + delta[block_line_section]); - b->set_end (b->end () + delta[block_line_section]); - - for (blockrange &r : b->ranges ()) - { - r.set_start (r.start () + delta[block_line_section]); - r.set_end (r.end () + delta[block_line_section]); - } + b->relocate_addresses (delta[block_line_section]); /* We only want to iterate over the local symbols, not any symbols in included symtabs. */ diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..718848d9b6d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,487 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. To check the 'maint info blocks' +# output we need these converted into actual addresses. +set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::foo_start_addr] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..2427ebbfd3f --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,51 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end base-commit: a723c56efb07c4f8b3f6a3ed4b878a2f8f5572cc -- 2.25.4 ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-29 14:49 ` [PATCHv4] " Andrew Burgess @ 2024-10-29 15:28 ` Bernd Edlinger 2024-10-31 10:57 ` Andrew Burgess 2024-10-29 16:34 ` Bernd Edlinger 2024-10-31 15:00 ` [PATCHv5] " Andrew Burgess 2 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-10-29 15:28 UTC (permalink / raw) To: Andrew Burgess, gdb-patches Hi Andrew, On 10/29/24 15:49, Andrew Burgess wrote: > In v4: > > - I still have one failed being reported from Linaro CI, > gdb.stabs/gdb11479.exp. I've been unable to reproduce this > failure. However, on re-reading this patch I did notice that, if > the m_entry_pc field was not set then we would still end up giving > it a value during relocation, this would result in m_entry_pc > holding an address which had been relocated twice. > > Fixed this by moving the block's address relocation logic into the > block class, and only relocating the m_entry_pc variable if it > actually has been assigned a value. > > In v3: > > - Entry PC values are now clamped to the block's start/end range. > This was causing failures in the self-tests when GDB was compiled > with GCC using default optimisation, as GCC emits an (invalid?) > entry-pc which is outside the blocks start/end range. > > In v2: > > - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead > just use CORE_ADDR. This does mean that targets where 0 is a > valid code address might run into problems, but those targets > likely have plenty of other problems, and I'd rather land this fix > than get hung up on this edge case. > > In the future it might be possible to remove block::m_start and > block::m_end as Klaus suggested, but this doesn't look like a > simple task. If we did drop those fields then it might be > possible to space a word for some flag bits which would allow us > to remove the 0 address special case. > > - Fixes to the DWARF assembler testcase to handle compiling with > '-pie', the Linaro CI exposed these issues. The changes are > pretty minor, mostly using label names rather than addresses when > building the DWARF. > > --- > > The entry PC for a DIE, e.g. an inline function, might not be the base > address of the DIE. Currently though, in block::entry_pc(), GDB > always returns the base address (low-pc or the first address of the > first range) as the entry PC. > > This commit extends the block class to carry the entry PC as a > separate member variable. Then the DWARF reader is extended to read > and set the entry PC for the block. Now in block::entry_pc(), if the > entry PC has been set to a specific value, this is the value returned. > > I have not removed the old code in block::entry_pc(). If the specific > entry PC value was not set then we still fall back on the old approach > of just returning the base address. Doing this feels more useful than > just claiming there is no entry PC. This should also mean that we > shouldn't see any change in behaviour for compilers that don't emit > the DW_AT_entry_pc, or for debug readers (e.g. stabs) that don't set > the entry PC value within a block. > > The DWARF-5 spec for reading the entry PC is a super-set of the spec > as found in DWARF-4. For example, if there is no DW_AT_entry_pc then > DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base > address, which is DW_AT_low_pc or the first address in the first range > specified by DW_AT_ranges if there is no DW_AT_low_pc. > > I have taken the approach of just implementing the DWARF-5 spec for > everyone. There doesn't seem to be any benefit to deliberately > ignoring a ranges based entry PC value for DWARF-4. If some naughty > compiler has emitted that, then lets use it. > > Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 > allows an address or a constant, where the constant is an offset from > the base address. I allow both approaches for all DWARF versions. > There doesn't seem to be any downsides to this approach. > > There was one issue that I ran into when testing this patch. GCC > seems to be quite bad at tracking code ranges when the default level > of optimisation is applied. As a result I was seeing failures in > gdb.gdb/python-helper.exp and gdb.gdb/selftest.exp when we debug the > generated GDB executable. > > What happens is that the DW_AT_entry_pc is given as an entry address > which is outside of any of the blocks ranges. Previously we ignored > the DW_AT_entry_pc and when we later ask for the entry-pc we just > report the first range's start address. > > After this commit we track the declared entry-pc, which is then what > we report. > > What this means is that when the user (or DejaGNU) tries to place a > breakpoint on an inline function, the breakpoint is placed at the > entry-pc. Then when GDB stops at this address and tries to figure out > which block it's in, GDB doesn't think it's actually within the inline > function that the user asked to stop in. > > In the test I mention about we place a breakpoint within GDB's > 'captured_main' function. In the builds I was looked at, this > function was inline within 'gdb_main', but the DW_AT_entry_pc for the > 'captured_main' function was not within the declared ranges of > 'captured_main'. As a result when the breakpoint was hit, GDB would > report the inferior as within 'gdb_main' instead of 'captured_main'. > I think you might have hit a case that I mentioned on part 3 of my patch: Additionally it may happen that one inline sub-range is empty or the inline is completely empty. But filtering that information away is not the right solution, since although there is no actual code from the inline, it is still possible that variables from an inline function can be inspected here. You probably had the entry_pc pointing to an empty sub-range, that is usually the lowest address, currently that is filtered away by the dwarf reader, but part 2+3 change that behavior, but since I did not try to solve the entry_pc problem completely, there was no apparent dependency between part 1 and part 2+3. But probably this is no longer the case, with your patch, since the checks are more strict, you could try to layer your entry_pc path over part 2+3. > For now I propose fixing this by checking the entry-pc against the > blocks start address. If the entry-pc is before the start address > then we move the entry-pc to be the start address. This effectively > restores the pre-patch behaviour for this problem case. > I think this plausibilty check is not strict enough, instead of just checking against the low and hig bounds, you should only accept the entry_pc value if it is inside the bounds of any of the sub-ranges, since we have no chance to get the correct behaviour otherwise, and the previous state is to prefer in that case. Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-29 15:28 ` Bernd Edlinger @ 2024-10-31 10:57 ` Andrew Burgess 2024-10-31 14:01 ` Bernd Edlinger 0 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-10-31 10:57 UTC (permalink / raw) To: Bernd Edlinger, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > Hi Andrew, > > On 10/29/24 15:49, Andrew Burgess wrote: >> In v4: >> >> - I still have one failed being reported from Linaro CI, >> gdb.stabs/gdb11479.exp. I've been unable to reproduce this >> failure. However, on re-reading this patch I did notice that, if >> the m_entry_pc field was not set then we would still end up giving >> it a value during relocation, this would result in m_entry_pc >> holding an address which had been relocated twice. >> >> Fixed this by moving the block's address relocation logic into the >> block class, and only relocating the m_entry_pc variable if it >> actually has been assigned a value. >> >> In v3: >> >> - Entry PC values are now clamped to the block's start/end range. >> This was causing failures in the self-tests when GDB was compiled >> with GCC using default optimisation, as GCC emits an (invalid?) >> entry-pc which is outside the blocks start/end range. >> >> In v2: >> >> - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead >> just use CORE_ADDR. This does mean that targets where 0 is a >> valid code address might run into problems, but those targets >> likely have plenty of other problems, and I'd rather land this fix >> than get hung up on this edge case. >> >> In the future it might be possible to remove block::m_start and >> block::m_end as Klaus suggested, but this doesn't look like a >> simple task. If we did drop those fields then it might be >> possible to space a word for some flag bits which would allow us >> to remove the 0 address special case. >> >> - Fixes to the DWARF assembler testcase to handle compiling with >> '-pie', the Linaro CI exposed these issues. The changes are >> pretty minor, mostly using label names rather than addresses when >> building the DWARF. >> >> --- >> >> The entry PC for a DIE, e.g. an inline function, might not be the base >> address of the DIE. Currently though, in block::entry_pc(), GDB >> always returns the base address (low-pc or the first address of the >> first range) as the entry PC. >> >> This commit extends the block class to carry the entry PC as a >> separate member variable. Then the DWARF reader is extended to read >> and set the entry PC for the block. Now in block::entry_pc(), if the >> entry PC has been set to a specific value, this is the value returned. >> >> I have not removed the old code in block::entry_pc(). If the specific >> entry PC value was not set then we still fall back on the old approach >> of just returning the base address. Doing this feels more useful than >> just claiming there is no entry PC. This should also mean that we >> shouldn't see any change in behaviour for compilers that don't emit >> the DW_AT_entry_pc, or for debug readers (e.g. stabs) that don't set >> the entry PC value within a block. >> >> The DWARF-5 spec for reading the entry PC is a super-set of the spec >> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >> address, which is DW_AT_low_pc or the first address in the first range >> specified by DW_AT_ranges if there is no DW_AT_low_pc. >> >> I have taken the approach of just implementing the DWARF-5 spec for >> everyone. There doesn't seem to be any benefit to deliberately >> ignoring a ranges based entry PC value for DWARF-4. If some naughty >> compiler has emitted that, then lets use it. >> >> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >> allows an address or a constant, where the constant is an offset from >> the base address. I allow both approaches for all DWARF versions. >> There doesn't seem to be any downsides to this approach. >> >> There was one issue that I ran into when testing this patch. GCC >> seems to be quite bad at tracking code ranges when the default level >> of optimisation is applied. As a result I was seeing failures in >> gdb.gdb/python-helper.exp and gdb.gdb/selftest.exp when we debug the >> generated GDB executable. >> >> What happens is that the DW_AT_entry_pc is given as an entry address >> which is outside of any of the blocks ranges. Previously we ignored >> the DW_AT_entry_pc and when we later ask for the entry-pc we just >> report the first range's start address. >> >> After this commit we track the declared entry-pc, which is then what >> we report. >> >> What this means is that when the user (or DejaGNU) tries to place a >> breakpoint on an inline function, the breakpoint is placed at the >> entry-pc. Then when GDB stops at this address and tries to figure out >> which block it's in, GDB doesn't think it's actually within the inline >> function that the user asked to stop in. >> >> In the test I mention about we place a breakpoint within GDB's >> 'captured_main' function. In the builds I was looked at, this >> function was inline within 'gdb_main', but the DW_AT_entry_pc for the >> 'captured_main' function was not within the declared ranges of >> 'captured_main'. As a result when the breakpoint was hit, GDB would >> report the inferior as within 'gdb_main' instead of 'captured_main'. >> > > I think you might have hit a case that I mentioned on part 3 of my patch: > > Additionally it may happen that one inline sub-range > is empty or the inline is completely empty. But > filtering that information away is not the right > solution, since although there is no actual code > from the inline, it is still possible that variables > from an inline function can be inspected here. > > You probably had the entry_pc pointing to an empty sub-range, > that is usually the lowest address, currently that is filtered > away by the dwarf reader, but part 2+3 change that behavior, > but since I did not try to solve the entry_pc problem completely, > there was no apparent dependency between part 1 and part 2+3. > But probably this is no longer the case, with your patch, > since the checks are more strict, you could try to layer > your entry_pc path over part 2+3. You are correct that the entry-pc is pointing to an empty sub-range. And as you say, if we later (as I think we should) merge those parts of your series that deal with empty sub-ranges, then this will resolve this problem and allow the entry-pc to point to a non-empty range. I would however, like to avoid, if at all possible, making this change depend on the later work, and if we follow up on your following suggestion, then I think the dependency is gone. > >> For now I propose fixing this by checking the entry-pc against the >> blocks start address. If the entry-pc is before the start address >> then we move the entry-pc to be the start address. This effectively >> restores the pre-patch behaviour for this problem case. >> > > I think this plausibilty check is not strict enough, instead of just > checking against the low and hig bounds, you should only accept the > entry_pc value if it is inside the bounds of any of the sub-ranges, That's a reasonable position to take, and it's trivial to change the patch to do this. It's worth pointing out that ignoring the entry-pc is (currently) the same as just using the lower bound. But I'll make this change and post a v4. > since we have no chance to get the correct behaviour otherwise, and > the previous state is to prefer in that case. I don't understand what you mean by "the previous state is to prefer in that case", so I'm not sure if I've missed something important here. Could you clarify this point please. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-31 10:57 ` Andrew Burgess @ 2024-10-31 14:01 ` Bernd Edlinger 2024-10-31 14:56 ` Andrew Burgess 0 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-10-31 14:01 UTC (permalink / raw) To: Andrew Burgess, gdb-patches On 10/31/24 11:57, Andrew Burgess wrote: > Bernd Edlinger <bernd.edlinger@hotmail.de> writes: >> since we have no chance to get the correct behaviour otherwise, and >> the previous state is to prefer in that case. > > I don't understand what you mean by "the previous state is to prefer in > that case", so I'm not sure if I've missed something important here. > Could you clarify this point please. > Okay, I wanted to say, if the entry_pc is outside of any sub-range, we should complain about it, but ignore the value, so the user should no see any difference to the debugging of that code with the gdb before this patch. Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-31 14:01 ` Bernd Edlinger @ 2024-10-31 14:56 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-31 14:56 UTC (permalink / raw) To: Bernd Edlinger, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > On 10/31/24 11:57, Andrew Burgess wrote: >> Bernd Edlinger <bernd.edlinger@hotmail.de> writes: >>> since we have no chance to get the correct behaviour otherwise, and >>> the previous state is to prefer in that case. >> >> I don't understand what you mean by "the previous state is to prefer in >> that case", so I'm not sure if I've missed something important here. >> Could you clarify this point please. >> > > Okay, I wanted to say, if the entry_pc is outside of any sub-range, we should > complain about it, but ignore the value, so the user should no see any > difference to the debugging of that code with the gdb before this patch. Great. I think we're now aligned on this then. I'm about to post v5 which does this, let me know what you think. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-29 14:49 ` [PATCHv4] " Andrew Burgess 2024-10-29 15:28 ` Bernd Edlinger @ 2024-10-29 16:34 ` Bernd Edlinger 2024-10-31 10:59 ` Andrew Burgess 2024-10-31 15:00 ` [PATCHv5] " Andrew Burgess 2 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-10-29 16:34 UTC (permalink / raw) To: Andrew Burgess, gdb-patches On 10/29/24 15:49, Andrew Burgess wrote: > @@ -344,6 +357,18 @@ struct block : public allocate_on_obstack<block> > startaddr and endaddr above. */ > > struct blockranges *m_ranges = nullptr; > + > + /* The entry address for this block. The value 0 is special and > + indicates that the entry address has not been set. > + > + Using 0 as a special value is not ideal, targets for which 0 is a > + valid code address might run into problems if they want to use 0 as a > + block's entry address, but the alternative is to carry a flag > + indicating if m_entry_pc is valid or not, but that would make 'struct > + block' even bigger, and we want to keep 'struct block' as small as > + possible (we might have a lot of blocks). */ > + > + CORE_ADDR m_entry_pc = 0; > }; > Aehm, I've just had an idea how you can get this much simpler: Instead of using an exceptional value, and fiddle with the later relocation steps, you should simply store here the offset from ranges[0].start, or block.start if no subranges, this is just a constant value, that just has to be added in block.entry_pc(), 0 means just the current behavour, and if >0 it is always a constant value, even after objfile relocation. That means there is no chance of any special value that cannot be handled here, and a lot of complications can be avoided. What do you think? Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv4] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-29 16:34 ` Bernd Edlinger @ 2024-10-31 10:59 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-31 10:59 UTC (permalink / raw) To: Bernd Edlinger, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > On 10/29/24 15:49, Andrew Burgess wrote: >> @@ -344,6 +357,18 @@ struct block : public allocate_on_obstack<block> >> startaddr and endaddr above. */ >> >> struct blockranges *m_ranges = nullptr; >> + >> + /* The entry address for this block. The value 0 is special and >> + indicates that the entry address has not been set. >> + >> + Using 0 as a special value is not ideal, targets for which 0 is a >> + valid code address might run into problems if they want to use 0 as a >> + block's entry address, but the alternative is to carry a flag >> + indicating if m_entry_pc is valid or not, but that would make 'struct >> + block' even bigger, and we want to keep 'struct block' as small as >> + possible (we might have a lot of blocks). */ >> + >> + CORE_ADDR m_entry_pc = 0; >> }; >> > > Aehm, I've just had an idea how you can get this much simpler: > > Instead of using an exceptional value, and fiddle with the later relocation steps, > you should simply store here the offset from ranges[0].start, or block.start > if no subranges, this is just a constant value, that just has to be added in > block.entry_pc(), 0 means just the current behavour, and if >0 it is always a > constant value, even after objfile relocation. That means there is no chance of any > special value that cannot be handled here, and a lot of complications can be avoided. > > What do you think? I like it. I'll give this a go and see what this looks like in v4. Thanks for the great suggestion. Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-29 14:49 ` [PATCHv4] " Andrew Burgess 2024-10-29 15:28 ` Bernd Edlinger 2024-10-29 16:34 ` Bernd Edlinger @ 2024-10-31 15:00 ` Andrew Burgess 2024-11-01 18:13 ` Tom Tromey ` (2 more replies) 2 siblings, 3 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-31 15:00 UTC (permalink / raw) To: gdb-patches; +Cc: Bernd Edlinger, Andrew Burgess In v5: - If DW_AT_entry_pc is outside of a block's start()..end() range, then the DW_AT_entry_pc is now ignored. - The entry-pc is stored as an offset from block::start() within the block class, this means we no longer need to worry about special address values, i.e. and entry-pc of zero is fine. This also means that we no longer need to relocate the entry-pc in objfile_relocate1. Credit for this idea to Bernd. In v4: - I still have one failed being reported from Linaro CI, gdb.stabs/gdb11479.exp. I've been unable to reproduce this failure. However, on re-reading this patch I did notice that, if the m_entry_pc field was not set then we would still end up giving it a value during relocation, this would result in m_entry_pc holding an address which had been relocated twice. Fixed this by moving the block's address relocation logic into the block class, and only relocating the m_entry_pc variable if it actually has been assigned a value. In v3: - Entry PC values are now clamped to the block's start/end range. This was causing failures in the self-tests when GDB was compiled with GCC using default optimisation, as GCC emits an (invalid?) entry-pc which is outside the blocks start/end range. In v2: - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead just use CORE_ADDR. This does mean that targets where 0 is a valid code address might run into problems, but those targets likely have plenty of other problems, and I'd rather land this fix than get hung up on this edge case. In the future it might be possible to remove block::m_start and block::m_end as Klaus suggested, but this doesn't look like a simple task. If we did drop those fields then it might be possible to space a word for some flag bits which would allow us to remove the 0 address special case. - Fixes to the DWARF assembler testcase to handle compiling with '-pie', the Linaro CI exposed these issues. The changes are pretty minor, mostly using label names rather than addresses when building the DWARF. --- The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set, then this is the value returned. I have not removed the old code in block::entry_pc(). Not every DIE will set the entry-pc, but GDB still needs to have an entry-pc for every block, so the existing logic supplies the entry-pc for any block where the entry-pc was not set. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. I ran into an issue when testing this patch where GCC would have the DW_AT_entry_pc point to an empty range. When GDB parses the ranges any empty ranges are ignored. As a consequence, the entry-pc appears to be outside the address range of a block. The empty range problem is certainly something that we can, and should address, but that is not the focus of this patch, so for now I'm ignoring that problem. What I have done is added a check: if the DW_AT_entry_pc is outside the range of a block then the entry-pc is ignored, GDB will then fall-back to its default algorithm for computing the entry-pc. If/when in the future we address the empty range problem, these DW_AT_entry_pc attributes will suddenly become valid and GDB will start using them. Until then, GDB continues to operate as it always had. An early version of this patch stored the entry-pc within the block like this: std::optional<CORE_ADDR> m_entry_pc; However, a concern was raised that this, on a 64-bit host, effectively increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, and 8-bytes for the std::optional's bool plus padding). If we remove the std::optional part and just use a CORE_ADDR then we need to have a "special" address to indicate if m_entry_pc is in use or not. I don't really like using special addresses; different targets can access different address ranges, even zero is a valid address on some targets. However, Bernd Edlinger suggested storing the entry-pc as an offset, and I think that will resolve my concerns. So, we store the entry-pc as an offset from the block::start() value. If this offset is zero then the entry-pc is block::start(), or was unset and we should just use the default algorithm. If the entry-pc was set, and is not equal to block::start() then the offset will be non-zero, and we can recalculate the entry-pc when needed. With this done, on a 64-bit host, block only needs to increase by 8-bytes. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> --- gdb/block.h | 47 ++- gdb/dwarf2/read.c | 102 ++++- gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ 6 files changed, 766 insertions(+), 13 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp diff --git a/gdb/block.h b/gdb/block.h index c3babad52f3..e284d67f550 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -179,27 +179,37 @@ struct block : public allocate_on_obstack<block> /* Return the "entry PC" of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. - - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + If the entry PC has been set to a specific value then this is + returned. Otherwise, the entry PC is the lowest (start) address for + the block when all addresses within the block are contiguous. If + non-contiguous, then use the start address for the first range in the + block. - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry PC + wherever possible, so this non-standard fallback case is only used as + a last resort. */ CORE_ADDR entry_pc () const { - if (this->is_contiguous ()) + if (m_entry_pc_offset != 0) + return this->start () + m_entry_pc_offset; + else if (this->is_contiguous ()) return this->start (); else return this->ranges ()[0].start (); } + /* Set this block's entry-pc to ADDR, which must lie between start() and + end(). */ + + void set_entry_pc (CORE_ADDR addr) + { + gdb_assert (addr >= this->start () && addr < this->end ()); + m_entry_pc_offset = addr - this->start (); + } + /* Return the objfile of this block. */ struct objfile *objfile () const; @@ -344,6 +354,19 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* The offset of the entry-pc for this block from the value returned by + start(). If space was no object then we'd store an actual address + along with a flag to indicate if the address has been set or not. + + We would like to avoid using 0 as a special address, as some targets + do allow for accesses to address 0. + + If the offset is 0 then entry_pc() will return a default entry-pc + based on the blocks ranges. Otherwise, if this offset is non-zero, + the entry_pc() will return 'start() + m_entry_pc_offset'. */ + + ULONGEST m_entry_pc_offset = 0; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 6ac6f7c6a39..994763f77c5 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11305,8 +11305,106 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Filled with the entry-pc if we can find it. */ + std::optional<CORE_ADDR> entry; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + entry.emplace (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + entry.emplace (per_objfile->relocate (attr->as_address ())); + } + else + entry = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + { + CORE_ADDR entry_pc = entry.value (); + + /* Some compilers (e.g. GCC) will have the DW_AT_entry_pc point at an + empty sub-range, which by a strict reading of the DWARF means that + the entry-pc is outside the blocks code range. If we continue + using this address then GDB will confuse itself, breakpoints will + be placed at the entry-pc, but once stopped there, GDB will not + recognise that it is inside this block. + + To avoid this, ignore entry-pc values that are outside the block's + range, GDB will then select a suitable default entry-pc. */ + if (entry_pc >= block->start () && entry_pc < block->end ()) + block->set_entry_pc (entry_pc); + else + complaint (_("in %s, DIE %s, DW_AT_entry_pc (%s) outside " + "block range (%s -> %s)"), + objfile_name (per_objfile->objfile), + sect_offset_str (die->sect_off), + paddress (per_objfile->objfile->arch (), entry_pc), + paddress (per_objfile->objfile->arch (), block->start ()), + paddress (per_objfile->objfile->arch (), block->end ())); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11361,6 +11459,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..718848d9b6d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,487 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. To check the 'maint info blocks' +# output we need these converted into actual addresses. +set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::foo_start_addr] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..2427ebbfd3f --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,51 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end base-commit: a723c56efb07c4f8b3f6a3ed4b878a2f8f5572cc -- 2.25.4 ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-31 15:00 ` [PATCHv5] " Andrew Burgess @ 2024-11-01 18:13 ` Tom Tromey 2024-11-01 20:27 ` Bernd Edlinger 2024-11-05 11:21 ` [PATCHv6] " Andrew Burgess 2 siblings, 0 replies; 41+ messages in thread From: Tom Tromey @ 2024-11-01 18:13 UTC (permalink / raw) To: Andrew Burgess; +Cc: gdb-patches, Bernd Edlinger >>>>> "Andrew" == Andrew Burgess <aburgess@redhat.com> writes: Andrew> In v5: Andrew> - If DW_AT_entry_pc is outside of a block's start()..end() range, Andrew> then the DW_AT_entry_pc is now ignored. Andrew> - The entry-pc is stored as an offset from block::start() within the Andrew> block class, this means we no longer need to worry about special Andrew> address values, i.e. and entry-pc of zero is fine. This also Andrew> means that we no longer need to relocate the entry-pc in Andrew> objfile_relocate1. Credit for this idea to Bernd. Thanks for doing this. FWIW this patch looks good to me, though I did not read the test case in detail. Approved-By: Tom Tromey <tom@tromey.com> Tom ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-31 15:00 ` [PATCHv5] " Andrew Burgess 2024-11-01 18:13 ` Tom Tromey @ 2024-11-01 20:27 ` Bernd Edlinger 2024-11-05 11:25 ` Andrew Burgess 2024-11-05 11:21 ` [PATCHv6] " Andrew Burgess 2 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-11-01 20:27 UTC (permalink / raw) To: Andrew Burgess, gdb-patches On 10/31/24 16:00, Andrew Burgess wrote: > In v5: > > - If DW_AT_entry_pc is outside of a block's start()..end() range, > then the DW_AT_entry_pc is now ignored. > > - The entry-pc is stored as an offset from block::start() within the > block class, this means we no longer need to worry about special > address values, i.e. and entry-pc of zero is fine. This also > means that we no longer need to relocate the entry-pc in > objfile_relocate1. Credit for this idea to Bernd. > > In v4: > > - I still have one failed being reported from Linaro CI, > gdb.stabs/gdb11479.exp. I've been unable to reproduce this > failure. However, on re-reading this patch I did notice that, if > the m_entry_pc field was not set then we would still end up giving > it a value during relocation, this would result in m_entry_pc > holding an address which had been relocated twice. > > Fixed this by moving the block's address relocation logic into the > block class, and only relocating the m_entry_pc variable if it > actually has been assigned a value. > > In v3: > > - Entry PC values are now clamped to the block's start/end range. > This was causing failures in the self-tests when GDB was compiled > with GCC using default optimisation, as GCC emits an (invalid?) > entry-pc which is outside the blocks start/end range. > > In v2: > > - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead > just use CORE_ADDR. This does mean that targets where 0 is a > valid code address might run into problems, but those targets > likely have plenty of other problems, and I'd rather land this fix > than get hung up on this edge case. > > In the future it might be possible to remove block::m_start and > block::m_end as Klaus suggested, but this doesn't look like a > simple task. If we did drop those fields then it might be > possible to space a word for some flag bits which would allow us > to remove the 0 address special case. > > - Fixes to the DWARF assembler testcase to handle compiling with > '-pie', the Linaro CI exposed these issues. The changes are > pretty minor, mostly using label names rather than addresses when > building the DWARF. > > --- > > The entry PC for a DIE, e.g. an inline function, might not be the base > address of the DIE. Currently though, in block::entry_pc(), GDB > always returns the base address (low-pc or the first address of the > first range) as the entry PC. > > This commit extends the block class to carry the entry PC as a > separate member variable. Then the DWARF reader is extended to read > and set the entry PC for the block. Now in block::entry_pc(), if the > entry PC has been set, then this is the value returned. > > I have not removed the old code in block::entry_pc(). Not every DIE > will set the entry-pc, but GDB still needs to have an entry-pc for > every block, so the existing logic supplies the entry-pc for any block > where the entry-pc was not set. > > The DWARF-5 spec for reading the entry PC is a super-set of the spec > as found in DWARF-4. For example, if there is no DW_AT_entry_pc then > DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base > address, which is DW_AT_low_pc or the first address in the first range > specified by DW_AT_ranges if there is no DW_AT_low_pc. > > I have taken the approach of just implementing the DWARF-5 spec for > everyone. There doesn't seem to be any benefit to deliberately > ignoring a ranges based entry PC value for DWARF-4. If some naughty > compiler has emitted that, then lets use it. > > Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 > allows an address or a constant, where the constant is an offset from > the base address. I allow both approaches for all DWARF versions. > There doesn't seem to be any downsides to this approach. > > I ran into an issue when testing this patch where GCC would have the > DW_AT_entry_pc point to an empty range. When GDB parses the ranges > any empty ranges are ignored. As a consequence, the entry-pc appears > to be outside the address range of a block. > > The empty range problem is certainly something that we can, and should > address, but that is not the focus of this patch, so for now I'm > ignoring that problem. What I have done is added a check: if the > DW_AT_entry_pc is outside the range of a block then the entry-pc is > ignored, GDB will then fall-back to its default algorithm for > computing the entry-pc. > > If/when in the future we address the empty range problem, these > DW_AT_entry_pc attributes will suddenly become valid and GDB will > start using them. Until then, GDB continues to operate as it always > had. > > An early version of this patch stored the entry-pc within the block > like this: > > std::optional<CORE_ADDR> m_entry_pc; > > However, a concern was raised that this, on a 64-bit host, effectively > increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, > and 8-bytes for the std::optional's bool plus padding). > > If we remove the std::optional part and just use a CORE_ADDR then we > need to have a "special" address to indicate if m_entry_pc is in use > or not. I don't really like using special addresses; different > targets can access different address ranges, even zero is a valid > address on some targets. > > However, Bernd Edlinger suggested storing the entry-pc as an offset, > and I think that will resolve my concerns. So, we store the entry-pc > as an offset from the block::start() value. If this offset is zero > then the entry-pc is block::start(), or was unset and we should just > use the default algorithm. If the entry-pc was set, and is not equal > to block::start() then the offset will be non-zero, and we can > recalculate the entry-pc when needed. > > With this done, on a 64-bit host, block only needs to increase by > 8-bytes. > > The inline-entry.exp test was originally contributed by Bernd here: > > https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com > > though I have made some edits, making more use of lib/gdb.exp > functions, making the gdb_test output patterns a little tighter, and > updating the test to run with Clang. I also moved the test to > gdb.opt/ as that seemed like a better home for it. > > Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> > --- > gdb/block.h | 47 ++- > gdb/dwarf2/read.c | 102 ++++- > gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ > gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ > gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ > gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ > 6 files changed, 766 insertions(+), 13 deletions(-) > create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c > create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp > create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c > create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp > > diff --git a/gdb/block.h b/gdb/block.h > index c3babad52f3..e284d67f550 100644 > --- a/gdb/block.h > +++ b/gdb/block.h > @@ -179,27 +179,37 @@ struct block : public allocate_on_obstack<block> > > /* Return the "entry PC" of this block. > > - The entry PC is the lowest (start) address for the block when all addresses > - within the block are contiguous. If non-contiguous, then use the start > - address for the first range in the block. > - > - At the moment, this almost matches what DWARF specifies as the entry > - pc. (The missing bit is support for DW_AT_entry_pc which should be > - preferred over range data and the low_pc.) > + If the entry PC has been set to a specific value then this is > + returned. Otherwise, the entry PC is the lowest (start) address for > + the block when all addresses within the block are contiguous. If > + non-contiguous, then use the start address for the first range in the > + block. > > - Once support for DW_AT_entry_pc is added, I expect that an entry_pc > - field will be added to one of these data structures. Once that's done, > - the entry_pc field can be set from the dwarf reader (and other readers > - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ > + This almost matches what DWARF specifies as the entry pc, except that > + the final case, using the first address of the first range, is a GDB > + extension. However, the DWARF reader sets the specific entry PC > + wherever possible, so this non-standard fallback case is only used as > + a last resort. */ > > CORE_ADDR entry_pc () const > { > - if (this->is_contiguous ()) > + if (m_entry_pc_offset != 0) > + return this->start () + m_entry_pc_offset; > + else if (this->is_contiguous ()) > return this->start (); > else > return this->ranges ()[0].start (); > } I wonder if it can happen that this->start is different than ranges[0].start, because if that is possible, then it would not work to set_entry_pc(this->start) because the offset will be 0 and the else path will pick the ranges[0],start. Actually I thought of something like this: if (this->is_contiguous ()) pc = this->start (); else pc = this->ranges ()[0].start (); pc += m_entry_pc_offset; return pc; so without any additional branches, is there a reason why that cannot work? > > + /* Set this block's entry-pc to ADDR, which must lie between start() and > + end(). */ > + > + void set_entry_pc (CORE_ADDR addr) > + { > + gdb_assert (addr >= this->start () && addr < this->end ()); note that my patch series part 3/3 has an example where the inline funcion has an empty instruction range, I wonder if this assertion will break, maybe addr <= this->end ? So, when this function is called, are the ranges already set, or will that happen later? Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-01 20:27 ` Bernd Edlinger @ 2024-11-05 11:25 ` Andrew Burgess 2024-11-05 15:26 ` Bernd Edlinger 0 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-05 11:25 UTC (permalink / raw) To: Bernd Edlinger, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > On 10/31/24 16:00, Andrew Burgess wrote: >> In v5: >> >> - If DW_AT_entry_pc is outside of a block's start()..end() range, >> then the DW_AT_entry_pc is now ignored. >> >> - The entry-pc is stored as an offset from block::start() within the >> block class, this means we no longer need to worry about special >> address values, i.e. and entry-pc of zero is fine. This also >> means that we no longer need to relocate the entry-pc in >> objfile_relocate1. Credit for this idea to Bernd. >> >> In v4: >> >> - I still have one failed being reported from Linaro CI, >> gdb.stabs/gdb11479.exp. I've been unable to reproduce this >> failure. However, on re-reading this patch I did notice that, if >> the m_entry_pc field was not set then we would still end up giving >> it a value during relocation, this would result in m_entry_pc >> holding an address which had been relocated twice. >> >> Fixed this by moving the block's address relocation logic into the >> block class, and only relocating the m_entry_pc variable if it >> actually has been assigned a value. >> >> In v3: >> >> - Entry PC values are now clamped to the block's start/end range. >> This was causing failures in the self-tests when GDB was compiled >> with GCC using default optimisation, as GCC emits an (invalid?) >> entry-pc which is outside the blocks start/end range. >> >> In v2: >> >> - Don't use std::optional<CORE_ADDR> for block::m_entry_pc, instead >> just use CORE_ADDR. This does mean that targets where 0 is a >> valid code address might run into problems, but those targets >> likely have plenty of other problems, and I'd rather land this fix >> than get hung up on this edge case. >> >> In the future it might be possible to remove block::m_start and >> block::m_end as Klaus suggested, but this doesn't look like a >> simple task. If we did drop those fields then it might be >> possible to space a word for some flag bits which would allow us >> to remove the 0 address special case. >> >> - Fixes to the DWARF assembler testcase to handle compiling with >> '-pie', the Linaro CI exposed these issues. The changes are >> pretty minor, mostly using label names rather than addresses when >> building the DWARF. >> >> --- >> >> The entry PC for a DIE, e.g. an inline function, might not be the base >> address of the DIE. Currently though, in block::entry_pc(), GDB >> always returns the base address (low-pc or the first address of the >> first range) as the entry PC. >> >> This commit extends the block class to carry the entry PC as a >> separate member variable. Then the DWARF reader is extended to read >> and set the entry PC for the block. Now in block::entry_pc(), if the >> entry PC has been set, then this is the value returned. >> >> I have not removed the old code in block::entry_pc(). Not every DIE >> will set the entry-pc, but GDB still needs to have an entry-pc for >> every block, so the existing logic supplies the entry-pc for any block >> where the entry-pc was not set. >> >> The DWARF-5 spec for reading the entry PC is a super-set of the spec >> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >> address, which is DW_AT_low_pc or the first address in the first range >> specified by DW_AT_ranges if there is no DW_AT_low_pc. >> >> I have taken the approach of just implementing the DWARF-5 spec for >> everyone. There doesn't seem to be any benefit to deliberately >> ignoring a ranges based entry PC value for DWARF-4. If some naughty >> compiler has emitted that, then lets use it. >> >> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >> allows an address or a constant, where the constant is an offset from >> the base address. I allow both approaches for all DWARF versions. >> There doesn't seem to be any downsides to this approach. >> >> I ran into an issue when testing this patch where GCC would have the >> DW_AT_entry_pc point to an empty range. When GDB parses the ranges >> any empty ranges are ignored. As a consequence, the entry-pc appears >> to be outside the address range of a block. >> >> The empty range problem is certainly something that we can, and should >> address, but that is not the focus of this patch, so for now I'm >> ignoring that problem. What I have done is added a check: if the >> DW_AT_entry_pc is outside the range of a block then the entry-pc is >> ignored, GDB will then fall-back to its default algorithm for >> computing the entry-pc. >> >> If/when in the future we address the empty range problem, these >> DW_AT_entry_pc attributes will suddenly become valid and GDB will >> start using them. Until then, GDB continues to operate as it always >> had. >> >> An early version of this patch stored the entry-pc within the block >> like this: >> >> std::optional<CORE_ADDR> m_entry_pc; >> >> However, a concern was raised that this, on a 64-bit host, effectively >> increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, >> and 8-bytes for the std::optional's bool plus padding). >> >> If we remove the std::optional part and just use a CORE_ADDR then we >> need to have a "special" address to indicate if m_entry_pc is in use >> or not. I don't really like using special addresses; different >> targets can access different address ranges, even zero is a valid >> address on some targets. >> >> However, Bernd Edlinger suggested storing the entry-pc as an offset, >> and I think that will resolve my concerns. So, we store the entry-pc >> as an offset from the block::start() value. If this offset is zero >> then the entry-pc is block::start(), or was unset and we should just >> use the default algorithm. If the entry-pc was set, and is not equal >> to block::start() then the offset will be non-zero, and we can >> recalculate the entry-pc when needed. >> >> With this done, on a 64-bit host, block only needs to increase by >> 8-bytes. >> >> The inline-entry.exp test was originally contributed by Bernd here: >> >> https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com >> >> though I have made some edits, making more use of lib/gdb.exp >> functions, making the gdb_test output patterns a little tighter, and >> updating the test to run with Clang. I also moved the test to >> gdb.opt/ as that seemed like a better home for it. >> >> Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> >> --- >> gdb/block.h | 47 ++- >> gdb/dwarf2/read.c | 102 ++++- >> gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ >> gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ >> gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ >> gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ >> 6 files changed, 766 insertions(+), 13 deletions(-) >> create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c >> create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp >> create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c >> create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp >> >> diff --git a/gdb/block.h b/gdb/block.h >> index c3babad52f3..e284d67f550 100644 >> --- a/gdb/block.h >> +++ b/gdb/block.h >> @@ -179,27 +179,37 @@ struct block : public allocate_on_obstack<block> >> >> /* Return the "entry PC" of this block. >> >> - The entry PC is the lowest (start) address for the block when all addresses >> - within the block are contiguous. If non-contiguous, then use the start >> - address for the first range in the block. >> - >> - At the moment, this almost matches what DWARF specifies as the entry >> - pc. (The missing bit is support for DW_AT_entry_pc which should be >> - preferred over range data and the low_pc.) >> + If the entry PC has been set to a specific value then this is >> + returned. Otherwise, the entry PC is the lowest (start) address for >> + the block when all addresses within the block are contiguous. If >> + non-contiguous, then use the start address for the first range in the >> + block. >> >> - Once support for DW_AT_entry_pc is added, I expect that an entry_pc >> - field will be added to one of these data structures. Once that's done, >> - the entry_pc field can be set from the dwarf reader (and other readers >> - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ >> + This almost matches what DWARF specifies as the entry pc, except that >> + the final case, using the first address of the first range, is a GDB >> + extension. However, the DWARF reader sets the specific entry PC >> + wherever possible, so this non-standard fallback case is only used as >> + a last resort. */ >> >> CORE_ADDR entry_pc () const >> { >> - if (this->is_contiguous ()) >> + if (m_entry_pc_offset != 0) >> + return this->start () + m_entry_pc_offset; >> + else if (this->is_contiguous ()) >> return this->start (); >> else >> return this->ranges ()[0].start (); >> } > > I wonder if it can happen that this->start is different > than ranges[0].start, because if that is possible, > then it would not work to set_entry_pc(this->start) > because the offset will be 0 and the else path will > pick the ranges[0],start. > > Actually I thought of something like this: > if (this->is_contiguous ()) > pc = this->start (); > else > pc = this->ranges ()[0].start (); > pc += m_entry_pc_offset; > return pc; > > so without any additional branches, > is there a reason why that cannot work? You're correct. I've adopted this approach in v6 which I just posted. It is possible, and valid, for a block's sub-ranges to not be in ascending address order. In this case the start() can be lower than the start of the first range. > >> >> + /* Set this block's entry-pc to ADDR, which must lie between start() and >> + end(). */ >> + >> + void set_entry_pc (CORE_ADDR addr) >> + { >> + gdb_assert (addr >= this->start () && addr < this->end ()); > > note that my patch series part 3/3 has an example where > the inline funcion has an empty instruction range, I wonder > if this assertion will break, maybe addr <= this->end ? I ran the tests from the full optimised debug series. Obviously there's plenty of failures at this point, but there are no assertions triggered. Using 'addr <= this->end' would, I think not be correct, as the block's end() address is not inclusive. That is, if 'addr == this->end ()' then the entry-pc would be the first address outside the block's range, which is clearly not correct. > So, when this function is called, are the ranges already set, > or will that happen later? The ranges are already set by the time a block's entry-pc is set. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-05 11:25 ` Andrew Burgess @ 2024-11-05 15:26 ` Bernd Edlinger 2024-11-05 16:52 ` Andrew Burgess 0 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-11-05 15:26 UTC (permalink / raw) To: Andrew Burgess, gdb-patches On 11/5/24 12:25, Andrew Burgess wrote: > Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > >> On 10/31/24 16:00, Andrew Burgess wrote: >>> + /* Set this block's entry-pc to ADDR, which must lie between start() and >>> + end(). */ >>> + >>> + void set_entry_pc (CORE_ADDR addr) >>> + { >>> + gdb_assert (addr >= this->start () && addr < this->end ()); >> >> note that my patch series part 3/3 has an example where >> the inline funcion has an empty instruction range, I wonder >> if this assertion will break, maybe addr <= this->end ? > > I ran the tests from the full optimised debug series. Obviously there's > plenty of failures at this point, but there are no assertions triggered. > Ah, okay, then this is probably just a complaint. > Using 'addr <= this->end' would, I think not be correct, as the block's > end() address is not inclusive. That is, if 'addr == this->end ()' then > the entry-pc would be the first address outside the block's range, which > is clearly not correct. > Yeah likely, but maybe with the exception when start==end && start==entry_pc? Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-05 15:26 ` Bernd Edlinger @ 2024-11-05 16:52 ` Andrew Burgess 2024-11-05 19:57 ` Bernd Edlinger 0 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-05 16:52 UTC (permalink / raw) To: Bernd Edlinger, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > On 11/5/24 12:25, Andrew Burgess wrote: >> Bernd Edlinger <bernd.edlinger@hotmail.de> writes: >> >>> On 10/31/24 16:00, Andrew Burgess wrote: >>>> + /* Set this block's entry-pc to ADDR, which must lie between start() and >>>> + end(). */ >>>> + >>>> + void set_entry_pc (CORE_ADDR addr) >>>> + { >>>> + gdb_assert (addr >= this->start () && addr < this->end ()); >>> >>> note that my patch series part 3/3 has an example where >>> the inline funcion has an empty instruction range, I wonder >>> if this assertion will break, maybe addr <= this->end ? >> >> I ran the tests from the full optimised debug series. Obviously there's >> plenty of failures at this point, but there are no assertions triggered. >> > > Ah, okay, then this is probably just a complaint. > >> Using 'addr <= this->end' would, I think not be correct, as the block's >> end() address is not inclusive. That is, if 'addr == this->end ()' then >> the entry-pc would be the first address outside the block's range, which >> is clearly not correct. >> > > Yeah likely, but maybe with the exception when start==end && start==entry_pc? But right now such empty ranges (when start == end) are discarded, so this would appear as an entry_pc outside of the block's range, which would mean we don't attempt to set the entry-pc. If/when we later "fix" the handling of empty ranges then we might have to do something here, but that will depend on how we choose to handle the empty range problem. I don't think we should preemptively try to adjust for that future fix at this point. But it's certainly something we can keep in mind that might need investigating later on. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv5] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-05 16:52 ` Andrew Burgess @ 2024-11-05 19:57 ` Bernd Edlinger 0 siblings, 0 replies; 41+ messages in thread From: Bernd Edlinger @ 2024-11-05 19:57 UTC (permalink / raw) To: Andrew Burgess, gdb-patches On 11/5/24 17:52, Andrew Burgess wrote: > Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > >> On 11/5/24 12:25, Andrew Burgess wrote: >>> Bernd Edlinger <bernd.edlinger@hotmail.de> writes: >>> >>>> On 10/31/24 16:00, Andrew Burgess wrote: >>>>> + /* Set this block's entry-pc to ADDR, which must lie between start() and >>>>> + end(). */ >>>>> + >>>>> + void set_entry_pc (CORE_ADDR addr) >>>>> + { >>>>> + gdb_assert (addr >= this->start () && addr < this->end ()); >>>> >>>> note that my patch series part 3/3 has an example where >>>> the inline funcion has an empty instruction range, I wonder >>>> if this assertion will break, maybe addr <= this->end ? >>> >>> I ran the tests from the full optimised debug series. Obviously there's >>> plenty of failures at this point, but there are no assertions triggered. >>> >> >> Ah, okay, then this is probably just a complaint. >> >>> Using 'addr <= this->end' would, I think not be correct, as the block's >>> end() address is not inclusive. That is, if 'addr == this->end ()' then >>> the entry-pc would be the first address outside the block's range, which >>> is clearly not correct. >>> >> >> Yeah likely, but maybe with the exception when start==end && start==entry_pc? > > But right now such empty ranges (when start == end) are discarded, so > this would appear as an entry_pc outside of the block's range, which > would mean we don't attempt to set the entry-pc. > > If/when we later "fix" the handling of empty ranges then we might have > to do something here, but that will depend on how we choose to handle > the empty range problem. I don't think we should preemptively try to > adjust for that future fix at this point. But it's certainly something > we can keep in mind that might need investigating later on. > Yeah sure, we can fix that later. Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-31 15:00 ` [PATCHv5] " Andrew Burgess 2024-11-01 18:13 ` Tom Tromey 2024-11-01 20:27 ` Bernd Edlinger @ 2024-11-05 11:21 ` Andrew Burgess 2024-11-13 13:49 ` Andrew Burgess 2 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-05 11:21 UTC (permalink / raw) To: gdb-patches; +Cc: Andrew Burgess, Bernd Edlinger The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set, this is the value returned. If the entry-pc has not been set to a specific value then the old behaviour of block::entry_pc() remains, GDB will use the block's base address. Not every DIE will set the entry-pc, but GDB still needs to have an entry-pc for every block, so the existing logic supplies the entry-pc for any block where the entry-pc was not set. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. I ran into an issue when testing this patch where GCC would have the DW_AT_entry_pc point to an empty range. When GDB parses the ranges any empty ranges are ignored. As a consequence, the entry-pc appears to be outside the address range of a block. The empty range problem is certainly something that we can, and should address, but that is not the focus of this patch, so for now I'm ignoring that problem. What I have done is added a check: if the DW_AT_entry_pc is outside the range of a block then the entry-pc is ignored, GDB will then fall-back to its default algorithm for computing the entry-pc. If/when in the future we address the empty range problem, these DW_AT_entry_pc attributes will suddenly become valid and GDB will start using them. Until then, GDB continues to operate as it always has. An early version of this patch stored the entry-pc within the block like this: std::optional<CORE_ADDR> m_entry_pc; However, a concern was raised that this, on a 64-bit host, effectively increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, and 8-bytes for the std::optional's bool plus padding). If we remove the std::optional part and just use a CORE_ADDR then we need to have a "special" address to indicate if m_entry_pc is in use or not. I don't really like using special addresses; different targets can access different address ranges, even zero is a valid address on some targets. However, Bernd Edlinger suggested storing the entry-pc as an offset, and I think that will resolve my concerns. So, we store the entry-pc as a signed offset from the block's base address (the first address of the first range, or the start() address value if there are now ranges). Remember, ranges can be out of order, in which case the first address of the first range might be greater than the entry-pc. When GDB needs to read the entry-pc we can add the offset onto the blocks base address to recalculate it. With this done, on a 64-bit host, block only needs to increase by 8-bytes. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> --- gdb/block.h | 71 +++- gdb/dwarf2/read.c | 102 ++++- gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c | 51 +++ gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp | 487 ++++++++++++++++++++++ gdb/testsuite/gdb.opt/inline-entry.c | 41 ++ gdb/testsuite/gdb.opt/inline-entry.exp | 51 +++ 6 files changed, 786 insertions(+), 17 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp create mode 100644 gdb/testsuite/gdb.opt/inline-entry.c create mode 100644 gdb/testsuite/gdb.opt/inline-entry.exp diff --git a/gdb/block.h b/gdb/block.h index c3babad52f3..235bc54af24 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -177,27 +177,31 @@ struct block : public allocate_on_obstack<block> bool is_contiguous () const { return this->ranges ().size () <= 1; } - /* Return the "entry PC" of this block. + /* Return the entry-pc of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. + If the entry PC has been set to a specific value then this is + returned. Otherwise, the default_entry_pc() address is returned. */ - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + CORE_ADDR entry_pc () const + { + return default_entry_pc () + m_entry_pc_offset; + } - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + /* Set this block's entry-pc to ADDR, which must lie between start() and + end(). The entry-pc is stored as the signed offset from the + default_entry_pc() address. - CORE_ADDR entry_pc () const + Note that block sub-ranges can be out of order, as such the offset of + the entry-pc might be negative. */ + + void set_entry_pc (CORE_ADDR addr) { - if (this->is_contiguous ()) - return this->start (); - else - return this->ranges ()[0].start (); + CORE_ADDR start = default_entry_pc (); + + gdb_assert (addr >= this->start () && addr < this->end ()); + gdb_assert (start >= this->start () && start < this->end ()); + + m_entry_pc_offset = addr - start; } /* Return the objfile of this block. */ @@ -308,6 +312,26 @@ struct block : public allocate_on_obstack<block> private: + /* Return the default entry-pc of this block. The default is the address + we use if the debug information hasn't specifically set a different + entry-pc value. This is the lowest address for the block when all + addresses within the block are contiguous. If non-contiguous, then + use the start address for the first range in the block. + + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry-pc + wherever possible, so this non-standard fallback case is only used as + a last resort. */ + + CORE_ADDR default_entry_pc () const + { + if (this->is_contiguous ()) + return this->start (); + else + return this->ranges ()[0].start (); + } + /* If the namespace_info is NULL, allocate it via OBSTACK and initialize its members to zero. */ void initialize_namespace (struct obstack *obstack); @@ -344,6 +368,21 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* The offset of the actual entry-pc value from the default entry-pc + value. If space was no object then we'd store an actual address along + with a flag to indicate if the address has been set or not. But we'd + like to keep the size of block low, so we'd like to use a single + member variable. + + We would also like to avoid using 0 as a special address; some targets + do allow for accesses to address 0. + + So instead we store the offset of the defined entry-pc from the + default entry-pc. See default_entry_pc() for the definition of the + default entry-pc. See entry_pc() for how this offset is used. */ + + LONGEST m_entry_pc_offset = 0; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 2a29b844845..8a91a3008f9 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11314,8 +11314,106 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Filled with the entry-pc if we can find it. */ + std::optional<CORE_ADDR> entry; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + entry.emplace (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + entry.emplace (per_objfile->relocate (attr->as_address ())); + } + else + entry = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + { + CORE_ADDR entry_pc = entry.value (); + + /* Some compilers (e.g. GCC) will have the DW_AT_entry_pc point at an + empty sub-range, which by a strict reading of the DWARF means that + the entry-pc is outside the blocks code range. If we continue + using this address then GDB will confuse itself, breakpoints will + be placed at the entry-pc, but once stopped there, GDB will not + recognise that it is inside this block. + + To avoid this, ignore entry-pc values that are outside the block's + range, GDB will then select a suitable default entry-pc. */ + if (entry_pc >= block->start () && entry_pc < block->end ()) + block->set_entry_pc (entry_pc); + else + complaint (_("in %s, DIE %s, DW_AT_entry_pc (%s) outside " + "block range (%s -> %s)"), + objfile_name (per_objfile->objfile), + sect_offset_str (die->sect_off), + paddress (per_objfile->objfile->arch (), entry_pc), + paddress (per_objfile->objfile->arch (), block->start ()), + paddress (per_objfile->objfile->arch (), block->end ())); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11370,6 +11468,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..718848d9b6d --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,487 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. To check the 'maint info blocks' +# output we need these converted into actual addresses. +set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::foo_start_addr] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_decl_line [gdb_get_line_number "foo decl line"] + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..2427ebbfd3f --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,51 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end base-commit: ebc73070f4b85e4adee7d2f49cb32e2bd7b16324 -- 2.25.4 ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-05 11:21 ` [PATCHv6] " Andrew Burgess @ 2024-11-13 13:49 ` Andrew Burgess 2024-11-13 16:59 ` Andrew Burgess 0 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-13 13:49 UTC (permalink / raw) To: gdb-patches; +Cc: Bernd Edlinger Andrew Burgess <aburgess@redhat.com> writes: > The entry PC for a DIE, e.g. an inline function, might not be the base > address of the DIE. Currently though, in block::entry_pc(), GDB > always returns the base address (low-pc or the first address of the > first range) as the entry PC. > > This commit extends the block class to carry the entry PC as a > separate member variable. Then the DWARF reader is extended to read > and set the entry PC for the block. Now in block::entry_pc(), if the > entry PC has been set, this is the value returned. > > If the entry-pc has not been set to a specific value then the old > behaviour of block::entry_pc() remains, GDB will use the block's base > address. Not every DIE will set the entry-pc, but GDB still needs to > have an entry-pc for every block, so the existing logic supplies the > entry-pc for any block where the entry-pc was not set. > > The DWARF-5 spec for reading the entry PC is a super-set of the spec > as found in DWARF-4. For example, if there is no DW_AT_entry_pc then > DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base > address, which is DW_AT_low_pc or the first address in the first range > specified by DW_AT_ranges if there is no DW_AT_low_pc. > > I have taken the approach of just implementing the DWARF-5 spec for > everyone. There doesn't seem to be any benefit to deliberately > ignoring a ranges based entry PC value for DWARF-4. If some naughty > compiler has emitted that, then lets use it. > > Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 > allows an address or a constant, where the constant is an offset from > the base address. I allow both approaches for all DWARF versions. > There doesn't seem to be any downsides to this approach. > > I ran into an issue when testing this patch where GCC would have the > DW_AT_entry_pc point to an empty range. When GDB parses the ranges > any empty ranges are ignored. As a consequence, the entry-pc appears > to be outside the address range of a block. > > The empty range problem is certainly something that we can, and should > address, but that is not the focus of this patch, so for now I'm > ignoring that problem. What I have done is added a check: if the > DW_AT_entry_pc is outside the range of a block then the entry-pc is > ignored, GDB will then fall-back to its default algorithm for > computing the entry-pc. > > If/when in the future we address the empty range problem, these > DW_AT_entry_pc attributes will suddenly become valid and GDB will > start using them. Until then, GDB continues to operate as it always > has. > > An early version of this patch stored the entry-pc within the block > like this: > > std::optional<CORE_ADDR> m_entry_pc; > > However, a concern was raised that this, on a 64-bit host, effectively > increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, > and 8-bytes for the std::optional's bool plus padding). > > If we remove the std::optional part and just use a CORE_ADDR then we > need to have a "special" address to indicate if m_entry_pc is in use > or not. I don't really like using special addresses; different > targets can access different address ranges, even zero is a valid > address on some targets. > > However, Bernd Edlinger suggested storing the entry-pc as an offset, > and I think that will resolve my concerns. So, we store the entry-pc > as a signed offset from the block's base address (the first address of > the first range, or the start() address value if there are now > ranges). Remember, ranges can be out of order, in which case the > first address of the first range might be greater than the entry-pc. > > When GDB needs to read the entry-pc we can add the offset onto the > blocks base address to recalculate it. > > With this done, on a 64-bit host, block only needs to increase by > 8-bytes. > > The inline-entry.exp test was originally contributed by Bernd here: > > https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com > > though I have made some edits, making more use of lib/gdb.exp > functions, making the gdb_test output patterns a little tighter, and > updating the test to run with Clang. I also moved the test to > gdb.opt/ as that seemed like a better home for it. > > Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> I've pushed the patch attached below. The only changes are some minor test tweaks in order to get more of the 'check-all-boards' passing. There are still some failures on e.g. gold and fission based boards, but I think these might be due to issues with the board file. I'll post a patch for those soon. Thanks, Andrew --- commit b9de07a5ff74663ff39bf03632d1b2ea417bf8d5 Author: Andrew Burgess <aburgess@redhat.com> Date: Thu Oct 10 11:37:34 2024 +0100 gdb: fix handling of DW_AT_entry_pc of inlined subroutines The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set, this is the value returned. If the entry-pc has not been set to a specific value then the old behaviour of block::entry_pc() remains, GDB will use the block's base address. Not every DIE will set the entry-pc, but GDB still needs to have an entry-pc for every block, so the existing logic supplies the entry-pc for any block where the entry-pc was not set. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. I ran into an issue when testing this patch where GCC would have the DW_AT_entry_pc point to an empty range. When GDB parses the ranges any empty ranges are ignored. As a consequence, the entry-pc appears to be outside the address range of a block. The empty range problem is certainly something that we can, and should address, but that is not the focus of this patch, so for now I'm ignoring that problem. What I have done is added a check: if the DW_AT_entry_pc is outside the range of a block then the entry-pc is ignored, GDB will then fall-back to its default algorithm for computing the entry-pc. If/when in the future we address the empty range problem, these DW_AT_entry_pc attributes will suddenly become valid and GDB will start using them. Until then, GDB continues to operate as it always has. An early version of this patch stored the entry-pc within the block like this: std::optional<CORE_ADDR> m_entry_pc; However, a concern was raised that this, on a 64-bit host, effectively increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, and 8-bytes for the std::optional's bool plus padding). If we remove the std::optional part and just use a CORE_ADDR then we need to have a "special" address to indicate if m_entry_pc is in use or not. I don't really like using special addresses; different targets can access different address ranges, even zero is a valid address on some targets. However, Bernd Edlinger suggested storing the entry-pc as an offset, and I think that will resolve my concerns. So, we store the entry-pc as a signed offset from the block's base address (the first address of the first range, or the start() address value if there are now ranges). Remember, ranges can be out of order, in which case the first address of the first range might be greater than the entry-pc. When GDB needs to read the entry-pc we can add the offset onto the blocks base address to recalculate it. With this done, on a 64-bit host, block only needs to increase by 8-bytes. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> diff --git a/gdb/block.h b/gdb/block.h index b70c82949d1..05daa734642 100644 --- a/gdb/block.h +++ b/gdb/block.h @@ -178,27 +178,31 @@ struct block : public allocate_on_obstack<block> bool is_contiguous () const { return this->ranges ().size () <= 1; } - /* Return the "entry PC" of this block. + /* Return the entry-pc of this block. - The entry PC is the lowest (start) address for the block when all addresses - within the block are contiguous. If non-contiguous, then use the start - address for the first range in the block. + If the entry PC has been set to a specific value then this is + returned. Otherwise, the default_entry_pc() address is returned. */ - At the moment, this almost matches what DWARF specifies as the entry - pc. (The missing bit is support for DW_AT_entry_pc which should be - preferred over range data and the low_pc.) + CORE_ADDR entry_pc () const + { + return default_entry_pc () + m_entry_pc_offset; + } - Once support for DW_AT_entry_pc is added, I expect that an entry_pc - field will be added to one of these data structures. Once that's done, - the entry_pc field can be set from the dwarf reader (and other readers - too). ENTRY_PC can then be redefined to be less DWARF-centric. */ + /* Set this block's entry-pc to ADDR, which must lie between start() and + end(). The entry-pc is stored as the signed offset from the + default_entry_pc() address. - CORE_ADDR entry_pc () const + Note that block sub-ranges can be out of order, as such the offset of + the entry-pc might be negative. */ + + void set_entry_pc (CORE_ADDR addr) { - if (this->is_contiguous ()) - return this->start (); - else - return this->ranges ()[0].start (); + CORE_ADDR start = default_entry_pc (); + + gdb_assert (addr >= this->start () && addr < this->end ()); + gdb_assert (start >= this->start () && start < this->end ()); + + m_entry_pc_offset = addr - start; } /* Return the objfile of this block. */ @@ -309,6 +313,26 @@ struct block : public allocate_on_obstack<block> private: + /* Return the default entry-pc of this block. The default is the address + we use if the debug information hasn't specifically set a different + entry-pc value. This is the lowest address for the block when all + addresses within the block are contiguous. If non-contiguous, then + use the start address for the first range in the block. + + This almost matches what DWARF specifies as the entry pc, except that + the final case, using the first address of the first range, is a GDB + extension. However, the DWARF reader sets the specific entry-pc + wherever possible, so this non-standard fallback case is only used as + a last resort. */ + + CORE_ADDR default_entry_pc () const + { + if (this->is_contiguous ()) + return this->start (); + else + return this->ranges ()[0].start (); + } + /* If the namespace_info is NULL, allocate it via OBSTACK and initialize its members to zero. */ void initialize_namespace (struct obstack *obstack); @@ -345,6 +369,21 @@ struct block : public allocate_on_obstack<block> startaddr and endaddr above. */ struct blockranges *m_ranges = nullptr; + + /* The offset of the actual entry-pc value from the default entry-pc + value. If space was no object then we'd store an actual address along + with a flag to indicate if the address has been set or not. But we'd + like to keep the size of block low, so we'd like to use a single + member variable. + + We would also like to avoid using 0 as a special address; some targets + do allow for accesses to address 0. + + So instead we store the offset of the defined entry-pc from the + default entry-pc. See default_entry_pc() for the definition of the + default entry-pc. See entry_pc() for how this offset is used. */ + + LONGEST m_entry_pc_offset = 0; }; /* The global block is singled out so that we can provide a back-link diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 665e00bb8e7..5f0b0d4e5d6 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -11313,8 +11313,106 @@ get_scope_pc_bounds (struct die_info *die, *highpc = best_high; } +/* Return the base address for DIE (which is represented by BLOCK) within + CU. The base address is the DW_AT_low_pc, or if that is not present, + the first address in the first range defined by DW_AT_ranges. + + The DWARF standard actually says that if DIE has neither DW_AT_low_pc or + DW_AT_ranges then we should search in the parent of DIE for those + properties, and so on up the hierarchy, until we find a die with one of + those attributes, and use that as the base address. We don't implement + that yet simply because we've never encountered a need for it. */ + +static std::optional<CORE_ADDR> +dwarf2_die_base_address (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + struct attribute *attr = dwarf2_attr (die, DW_AT_low_pc, cu); + if (attr != nullptr) + return per_objfile->relocate (attr->as_address ()); + else if (block->ranges ().size () > 0) + return block->ranges ()[0].start (); + + return {}; +} + +/* Set the entry PC for BLOCK which represents DIE from CU. Relies on the + range information (if present) already having been read from DIE and + stored into BLOCK. */ + +static void +dwarf2_record_block_entry_pc (struct die_info *die, struct block *block, + struct dwarf2_cu *cu) +{ + dwarf2_per_objfile *per_objfile = cu->per_objfile; + + /* Filled with the entry-pc if we can find it. */ + std::optional<CORE_ADDR> entry; + + /* Set the block's entry PC where possible. */ + struct attribute *attr = dwarf2_attr (die, DW_AT_entry_pc, cu); + if (attr != nullptr) + { + /* DWARF-5 allows for the DW_AT_entry_pc to be an unsigned constant + offset from the containing DIE's base address. We don't limit the + constant handling to DWARF-5 though. If a broken compiler emits + this for DWARF-4 then we handle it just as we would for DWARF-5. */ + if (attr->form_is_constant ()) + { + if (attr->form_is_unsigned ()) + { + CORE_ADDR offset = attr->as_unsigned (); + + std::optional<CORE_ADDR> base + = dwarf2_die_base_address (die, block, cu); + + if (base.has_value ()) + entry.emplace (base.value () + offset); + } + else + { + /* We could possibly handle signed constants, but this is out + of spec, so for now, just complain and ignore it. */ + complaint (_("Unhandled constant for DW_AT_entry_pc, value (%s)"), + plongest (attr->as_nonnegative ())); + } + } + else + entry.emplace (per_objfile->relocate (attr->as_address ())); + } + else + entry = dwarf2_die_base_address (die, block, cu); + + if (entry.has_value ()) + { + CORE_ADDR entry_pc = entry.value (); + + /* Some compilers (e.g. GCC) will have the DW_AT_entry_pc point at an + empty sub-range, which by a strict reading of the DWARF means that + the entry-pc is outside the blocks code range. If we continue + using this address then GDB will confuse itself, breakpoints will + be placed at the entry-pc, but once stopped there, GDB will not + recognise that it is inside this block. + + To avoid this, ignore entry-pc values that are outside the block's + range, GDB will then select a suitable default entry-pc. */ + if (entry_pc >= block->start () && entry_pc < block->end ()) + block->set_entry_pc (entry_pc); + else + complaint (_("in %s, DIE %s, DW_AT_entry_pc (%s) outside " + "block range (%s -> %s)"), + objfile_name (per_objfile->objfile), + sect_offset_str (die->sect_off), + paddress (per_objfile->objfile->arch (), entry_pc), + paddress (per_objfile->objfile->arch (), block->start ()), + paddress (per_objfile->objfile->arch (), block->end ())); + } +} + /* Record the address ranges for BLOCK, offset by BASEADDR, as given - in DIE. */ + in DIE. Also set the entry PC for BLOCK. */ static void dwarf2_record_block_ranges (struct die_info *die, struct block *block, @@ -11369,6 +11467,8 @@ dwarf2_record_block_ranges (struct die_info *die, struct block *block, block->set_ranges (make_blockranges (objfile, blockvec)); } + + dwarf2_record_block_entry_pc (die, block, cu); } /* Check whether the producer field indicates either of GCC < 4.6, or the diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c new file mode 100644 index 00000000000..363781d22bf --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.c @@ -0,0 +1,51 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +volatile int global_var = 0; + +void +foo (void) /* foo decl line */ +{ + /* This label is used to find the start of 'foo' when generating the + debug information. */ + asm ("foo_label: .globl foo_label"); + + /* These labels define a range within foo. */ + asm ("foo_r1_s: .globl foo_r1_s"); + ++global_var; + asm ("foo_r1_e: .globl foo_r1_e"); + + ++global_var; + + asm ("foo_r2_s: .globl foo_r2_s"); + ++global_var; + asm ("foo_middle: .globl foo_middle"); + ++global_var; + asm ("foo_r2_e: .globl foo_r2_e"); + + ++global_var; + + asm ("foo_r3_s: .globl foo_r3_s"); + ++global_var; + asm ("foo_r3_e: .globl foo_r3_e"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); +} diff --git a/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp new file mode 100644 index 00000000000..eb4d7cd8bb4 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/dw2-entry-pc.exp @@ -0,0 +1,483 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test different ways in which DW_AT_entry_pc can be expressed in the +# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test +# procs below precise details of what DW_AT_entry_pc forms are tested. + +load_lib dwarf.exp + +require dwarf2_support + +standard_testfile + +# This compiles the source file and starts and stops GDB, so run it +# before calling prepare_for_testing otherwise GDB will have exited. +get_func_info foo + +if { [prepare_for_testing "failed to prepare" ${testfile} \ + [list ${srcfile}]] } { + return -1 +} + +if ![runto_main] { + return -1 +} + +# Address for the middle of foo. This is used as our entry point when +# the entry_pc is defined as an address. +set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \ + "get address for middle of foo"] + +# The FOO_START and FOO_END we get from get_func_info is an expression +# involving symbols and offsets. To check the 'maint info blocks' +# output we need these converted into actual addresses. +set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \ + "get address for start of foo"] +set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \ + "get address for end of foo"] + +# The ranges within foo. Used when foo is defined using ranges rather +# than a low pc and high pc pair. The entry point is in the middle of +# the second range. +foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } { + set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \ + "get address for foo_$var"] +} + +# Line on which 'foo' is declared. Used in generated debug. +set foo_decl_line [gdb_get_line_number "foo decl line"] + +if [is_ilp32_target] { + set ptr_type "data4" +} else { + set ptr_type "data8" +} + +# Generate a suffix number. Called from each of the test procs below +# to acquire a unique suffix for naming asm files and executables. + +set global_test_suffix 0 +proc get_next_suffix {} { + global global_test_suffix + incr global_test_suffix + + return $global_test_suffix +} + +# Helper for the two build_and_test_* procs below. Combine ASM_FILE +# with the global SRCFILE and build an executable. Use SUFFIX to give +# the executable a unique name. + +proc build_and_runto_main { suffix asm_file } { + if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \ + [list $::srcfile $asm_file] {nodebug}]} { + return false + } + + if ![runto_main] { + return false + } + + # Ensure the CU containing 'foo' is expanded, so the blocks are + # visible. + gdb_test "info function foo" \ + "File \[^\r\n\]+/$::srcfile:\r\n$::foo_decl_line:\\s+void foo\\(\\);.*" + + return true +} + + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' with a continuous address range +# and an entry address of ENTRY_PC. + +proc build_and_test_continuous { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " is contiguous"] +} + +# Combine ASM_FILE with the global SRCFILE and build an executable, +# use SUFFIX to make the executable name unique. +# +# Then check the blocks at the symbol `foo_middle'. The inner most +# block should be a block for 'foo' which has 3 address ranges and an +# entry address of ENTRY_PC. + +proc build_and_test_ranged { suffix asm_file entry_pc } { + if { ![build_and_runto_main $suffix $asm_file] } { + return false + } + + gdb_test "maint info blocks foo_middle" \ + [multi_line \ + "\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \ + " entry pc: $entry_pc" \ + " function: foo" \ + " address ranges:" \ + " $::r1_s\.\.$::r1_e" \ + " $::r2_s\.\.$::r2_e" \ + " $::r3_s\.\.$::r3_e" ] +} + +# The function's address range is defined using low/high bounds and +# the entry_pc attribute is not given. The function's entry PC will +# default to the low address. + +proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $::foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_start_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an address), which will +# be used as the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $::foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using low/high bounds and an +# entry_pc attribute is given (which contains an offset from the base +# address), which will be used to compute the function's entry address. + +proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + global srcfile + + declare_labels lines_table + + set foo_offset [expr $::foo_middle_addr - $::foo_start_addr] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $::foo_decl_line data1} + {decl_column 1 data1} + {low_pc $::foo_start addr} + {high_pc $::foo_len $::ptr_type} + {external 1 flag} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + } + + build_and_test_continuous $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information. No +# entry_pc attribute is used. The entry PC for the function will +# default to the first address of the first range. + +proc_with_prefix use_ranges_without_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $::foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::r1_s +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an address) is used, this will be +# the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_pc { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $::foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc foo_middle addr} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# The function's address range is defined using range information and +# an entry_pc attribute (which is an offset) is used, this will be +# used to calculate the entry PC for the function. + +proc_with_prefix use_ranges_with_entry_offset { dwarf_version } { + set suffix [get_next_suffix] + + # Make some DWARF for the test. + set asm_file [standard_output_file "$::testfile-dw-$suffix.S"] + Dwarf::assemble $asm_file { + upvar dwarf_version dwarf_version + global srcfile + + declare_labels lines_table ranges_label + + set foo_offset [expr $::foo_middle_addr - $::r1_s] + + cu { version $::dwarf_version } { + compile_unit { + {producer "gcc"} + {language @DW_LANG_C} + {name ${srcfile}} + {comp_dir /tmp} + {stmt_list $lines_table DW_FORM_sec_offset} + {low_pc 0 addr} + } { + subprogram { + {name foo} + {decl_file 1 data1} + {decl_line $::foo_decl_line data1} + {decl_column 1 data1} + {external 1 flag} + {ranges ${ranges_label} DW_FORM_sec_offset} + {entry_pc $foo_offset data4} + } + } + } + + lines {version 2} lines_table { + include_dir "$::srcdir/$::subdir" + file_name "$srcfile" 1 + } + + if { $dwarf_version == 5 } { + rnglists {} { + table {} { + ranges_label: list_ { + start_end foo_r1_s foo_r1_e + start_end foo_r2_s foo_r2_e + start_end foo_r3_s foo_r3_e + } + } + } + } else { + ranges { } { + ranges_label: sequence { + range foo_r1_s foo_r1_e + range foo_r2_s foo_r2_e + range foo_r3_s foo_r3_e + } + } + } + } + + build_and_test_ranged $suffix $asm_file $::foo_middle_addr +} + +# Run the tests. +foreach_with_prefix dwarf_version { 4 5 } { + use_low_high_bounds_without_entry_pc $dwarf_version + use_low_high_bounds_with_entry_offset $dwarf_version + use_low_high_bounds_with_entry_pc $dwarf_version + use_ranges_without_entry_pc $dwarf_version + use_ranges_with_entry_pc $dwarf_version + use_ranges_with_entry_offset $dwarf_version +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.c b/gdb/testsuite/gdb.opt/inline-entry.c new file mode 100644 index 00000000000..891b22a28b1 --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.c @@ -0,0 +1,41 @@ +/* Copyright 2024 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#include "attributes.h" + +volatile int global = 0; + +__attribute__((noinline)) ATTRIBUTE_NOCLONE void +foo (int arg) +{ + global += arg; +} + +inline __attribute__((always_inline)) int +bar (int val) +{ + if (global == val) + return 1; + foo (1); + return 1; +} + +int +main (void) +{ + if ((global && bar (1)) || bar (2)) + return 0; + return 1; +} diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp new file mode 100644 index 00000000000..6f87ea3a00d --- /dev/null +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -0,0 +1,58 @@ +# Copyright 2024 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +# Test some code which relies on GDB interpreting the DW_AT_entry_pc +# correctly in order to place the breakpoints. This was tested with +# versions of GCC between 8.4 and 14.2 and in all cases the entry_pc +# was required. +# +# Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the +# Clang generated code didn't depend on the entry_pc being parsed. + +standard_testfile + +set options {debug optimize=-O2} +lappend_include_file options $srcdir/lib/attributes.h + +if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { + return +} + +if ![runto_main] { + return +} + +# This test makes use of inline functions. +get_debug_format +if { [skip_inline_frame_tests] } { + untested "skipping inline frame tests" + return +} + +gdb_breakpoint "bar" +set bp_bar_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of bar breakpoint"] + +gdb_breakpoint "foo" +set bp_foo_num [get_integer_valueof "\$bpnum" "*UNKNOWN*" \ + "get number of foo breakpoint"] + +gdb_test "continue" \ + "Breakpoint ${bp_bar_num}(?:\\.$decimal)?, bar .*" "continue to bar" + +gdb_test "continue" \ + "Breakpoint ${bp_foo_num}(?:\\.$decimal)?, foo .*" "continue to foo" + +gdb_continue_to_end ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-13 13:49 ` Andrew Burgess @ 2024-11-13 16:59 ` Andrew Burgess 2024-11-14 9:20 ` Tom de Vries 0 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-13 16:59 UTC (permalink / raw) To: gdb-patches; +Cc: Bernd Edlinger Andrew Burgess <aburgess@redhat.com> writes: > Andrew Burgess <aburgess@redhat.com> writes: > >> The entry PC for a DIE, e.g. an inline function, might not be the base >> address of the DIE. Currently though, in block::entry_pc(), GDB >> always returns the base address (low-pc or the first address of the >> first range) as the entry PC. >> >> This commit extends the block class to carry the entry PC as a >> separate member variable. Then the DWARF reader is extended to read >> and set the entry PC for the block. Now in block::entry_pc(), if the >> entry PC has been set, this is the value returned. >> >> If the entry-pc has not been set to a specific value then the old >> behaviour of block::entry_pc() remains, GDB will use the block's base >> address. Not every DIE will set the entry-pc, but GDB still needs to >> have an entry-pc for every block, so the existing logic supplies the >> entry-pc for any block where the entry-pc was not set. >> >> The DWARF-5 spec for reading the entry PC is a super-set of the spec >> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >> address, which is DW_AT_low_pc or the first address in the first range >> specified by DW_AT_ranges if there is no DW_AT_low_pc. >> >> I have taken the approach of just implementing the DWARF-5 spec for >> everyone. There doesn't seem to be any benefit to deliberately >> ignoring a ranges based entry PC value for DWARF-4. If some naughty >> compiler has emitted that, then lets use it. >> >> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >> allows an address or a constant, where the constant is an offset from >> the base address. I allow both approaches for all DWARF versions. >> There doesn't seem to be any downsides to this approach. >> >> I ran into an issue when testing this patch where GCC would have the >> DW_AT_entry_pc point to an empty range. When GDB parses the ranges >> any empty ranges are ignored. As a consequence, the entry-pc appears >> to be outside the address range of a block. >> >> The empty range problem is certainly something that we can, and should >> address, but that is not the focus of this patch, so for now I'm >> ignoring that problem. What I have done is added a check: if the >> DW_AT_entry_pc is outside the range of a block then the entry-pc is >> ignored, GDB will then fall-back to its default algorithm for >> computing the entry-pc. >> >> If/when in the future we address the empty range problem, these >> DW_AT_entry_pc attributes will suddenly become valid and GDB will >> start using them. Until then, GDB continues to operate as it always >> has. >> >> An early version of this patch stored the entry-pc within the block >> like this: >> >> std::optional<CORE_ADDR> m_entry_pc; >> >> However, a concern was raised that this, on a 64-bit host, effectively >> increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, >> and 8-bytes for the std::optional's bool plus padding). >> >> If we remove the std::optional part and just use a CORE_ADDR then we >> need to have a "special" address to indicate if m_entry_pc is in use >> or not. I don't really like using special addresses; different >> targets can access different address ranges, even zero is a valid >> address on some targets. >> >> However, Bernd Edlinger suggested storing the entry-pc as an offset, >> and I think that will resolve my concerns. So, we store the entry-pc >> as a signed offset from the block's base address (the first address of >> the first range, or the start() address value if there are now >> ranges). Remember, ranges can be out of order, in which case the >> first address of the first range might be greater than the entry-pc. >> >> When GDB needs to read the entry-pc we can add the offset onto the >> blocks base address to recalculate it. >> >> With this done, on a 64-bit host, block only needs to increase by >> 8-bytes. >> >> The inline-entry.exp test was originally contributed by Bernd here: >> >> https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com >> >> though I have made some edits, making more use of lib/gdb.exp >> functions, making the gdb_test output patterns a little tighter, and >> updating the test to run with Clang. I also moved the test to >> gdb.opt/ as that seemed like a better home for it. >> >> Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> > > I've pushed the patch attached below. The only changes are some minor > test tweaks in order to get more of the 'check-all-boards' passing. > There are still some failures on e.g. gold and fission based boards, but > I think these might be due to issues with the board file. I'll post a > patch for those soon. I'm aware that this patch has caused a regression on the buildbot[1]. I'm investigating this but it will be at least tomorrow before I have fix. Thanks, Andrew [1] https://builder.sourceware.org/buildbot/#/builders/290/builds/1903 ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-13 16:59 ` Andrew Burgess @ 2024-11-14 9:20 ` Tom de Vries 2024-11-14 19:33 ` Andrew Burgess 0 siblings, 1 reply; 41+ messages in thread From: Tom de Vries @ 2024-11-14 9:20 UTC (permalink / raw) To: Andrew Burgess, gdb-patches; +Cc: Bernd Edlinger On 11/13/24 17:59, Andrew Burgess wrote: > Andrew Burgess <aburgess@redhat.com> writes: > >> Andrew Burgess <aburgess@redhat.com> writes: >> >>> The entry PC for a DIE, e.g. an inline function, might not be the base >>> address of the DIE. Currently though, in block::entry_pc(), GDB >>> always returns the base address (low-pc or the first address of the >>> first range) as the entry PC. >>> >>> This commit extends the block class to carry the entry PC as a >>> separate member variable. Then the DWARF reader is extended to read >>> and set the entry PC for the block. Now in block::entry_pc(), if the >>> entry PC has been set, this is the value returned. >>> >>> If the entry-pc has not been set to a specific value then the old >>> behaviour of block::entry_pc() remains, GDB will use the block's base >>> address. Not every DIE will set the entry-pc, but GDB still needs to >>> have an entry-pc for every block, so the existing logic supplies the >>> entry-pc for any block where the entry-pc was not set. >>> >>> The DWARF-5 spec for reading the entry PC is a super-set of the spec >>> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >>> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >>> address, which is DW_AT_low_pc or the first address in the first range >>> specified by DW_AT_ranges if there is no DW_AT_low_pc. >>> >>> I have taken the approach of just implementing the DWARF-5 spec for >>> everyone. There doesn't seem to be any benefit to deliberately >>> ignoring a ranges based entry PC value for DWARF-4. If some naughty >>> compiler has emitted that, then lets use it. >>> >>> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >>> allows an address or a constant, where the constant is an offset from >>> the base address. I allow both approaches for all DWARF versions. >>> There doesn't seem to be any downsides to this approach. >>> >>> I ran into an issue when testing this patch where GCC would have the >>> DW_AT_entry_pc point to an empty range. When GDB parses the ranges >>> any empty ranges are ignored. As a consequence, the entry-pc appears >>> to be outside the address range of a block. >>> >>> The empty range problem is certainly something that we can, and should >>> address, but that is not the focus of this patch, so for now I'm >>> ignoring that problem. What I have done is added a check: if the >>> DW_AT_entry_pc is outside the range of a block then the entry-pc is >>> ignored, GDB will then fall-back to its default algorithm for >>> computing the entry-pc. >>> >>> If/when in the future we address the empty range problem, these >>> DW_AT_entry_pc attributes will suddenly become valid and GDB will >>> start using them. Until then, GDB continues to operate as it always >>> has. >>> >>> An early version of this patch stored the entry-pc within the block >>> like this: >>> >>> std::optional<CORE_ADDR> m_entry_pc; >>> >>> However, a concern was raised that this, on a 64-bit host, effectively >>> increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, >>> and 8-bytes for the std::optional's bool plus padding). >>> >>> If we remove the std::optional part and just use a CORE_ADDR then we >>> need to have a "special" address to indicate if m_entry_pc is in use >>> or not. I don't really like using special addresses; different >>> targets can access different address ranges, even zero is a valid >>> address on some targets. >>> >>> However, Bernd Edlinger suggested storing the entry-pc as an offset, >>> and I think that will resolve my concerns. So, we store the entry-pc >>> as a signed offset from the block's base address (the first address of >>> the first range, or the start() address value if there are now >>> ranges). Remember, ranges can be out of order, in which case the >>> first address of the first range might be greater than the entry-pc. >>> >>> When GDB needs to read the entry-pc we can add the offset onto the >>> blocks base address to recalculate it. >>> >>> With this done, on a 64-bit host, block only needs to increase by >>> 8-bytes. >>> >>> The inline-entry.exp test was originally contributed by Bernd here: >>> >>> https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com >>> >>> though I have made some edits, making more use of lib/gdb.exp >>> functions, making the gdb_test output patterns a little tighter, and >>> updating the test to run with Clang. I also moved the test to >>> gdb.opt/ as that seemed like a better home for it. >>> >>> Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> >> >> I've pushed the patch attached below. The only changes are some minor >> test tweaks in order to get more of the 'check-all-boards' passing. >> There are still some failures on e.g. gold and fission based boards, but >> I think these might be due to issues with the board file. I'll post a >> patch for those soon. > > I'm aware that this patch has caused a regression on the buildbot[1]. I'm > investigating this but it will be at least tomorrow before I have fix. > In case this helps, I see the same assert in a large amount of ada test-cases. I reproduced the first one using gdb.ada/access_to_packed_array.exp, with system gcc 7 and then with gcc 8. With gcc 9 - 14, the assert no longer reproduces. Thanks, - Tom > Thanks, > Andrew > > [1] https://builder.sourceware.org/buildbot/#/builders/290/builds/1903 > ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-14 9:20 ` Tom de Vries @ 2024-11-14 19:33 ` Andrew Burgess 2024-11-15 8:50 ` Tom de Vries 0 siblings, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-14 19:33 UTC (permalink / raw) To: Tom de Vries, gdb-patches; +Cc: Bernd Edlinger Tom de Vries <tdevries@suse.de> writes: > On 11/13/24 17:59, Andrew Burgess wrote: >> Andrew Burgess <aburgess@redhat.com> writes: >> >>> Andrew Burgess <aburgess@redhat.com> writes: >>> >>>> The entry PC for a DIE, e.g. an inline function, might not be the base >>>> address of the DIE. Currently though, in block::entry_pc(), GDB >>>> always returns the base address (low-pc or the first address of the >>>> first range) as the entry PC. >>>> >>>> This commit extends the block class to carry the entry PC as a >>>> separate member variable. Then the DWARF reader is extended to read >>>> and set the entry PC for the block. Now in block::entry_pc(), if the >>>> entry PC has been set, this is the value returned. >>>> >>>> If the entry-pc has not been set to a specific value then the old >>>> behaviour of block::entry_pc() remains, GDB will use the block's base >>>> address. Not every DIE will set the entry-pc, but GDB still needs to >>>> have an entry-pc for every block, so the existing logic supplies the >>>> entry-pc for any block where the entry-pc was not set. >>>> >>>> The DWARF-5 spec for reading the entry PC is a super-set of the spec >>>> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >>>> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >>>> address, which is DW_AT_low_pc or the first address in the first range >>>> specified by DW_AT_ranges if there is no DW_AT_low_pc. >>>> >>>> I have taken the approach of just implementing the DWARF-5 spec for >>>> everyone. There doesn't seem to be any benefit to deliberately >>>> ignoring a ranges based entry PC value for DWARF-4. If some naughty >>>> compiler has emitted that, then lets use it. >>>> >>>> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >>>> allows an address or a constant, where the constant is an offset from >>>> the base address. I allow both approaches for all DWARF versions. >>>> There doesn't seem to be any downsides to this approach. >>>> >>>> I ran into an issue when testing this patch where GCC would have the >>>> DW_AT_entry_pc point to an empty range. When GDB parses the ranges >>>> any empty ranges are ignored. As a consequence, the entry-pc appears >>>> to be outside the address range of a block. >>>> >>>> The empty range problem is certainly something that we can, and should >>>> address, but that is not the focus of this patch, so for now I'm >>>> ignoring that problem. What I have done is added a check: if the >>>> DW_AT_entry_pc is outside the range of a block then the entry-pc is >>>> ignored, GDB will then fall-back to its default algorithm for >>>> computing the entry-pc. >>>> >>>> If/when in the future we address the empty range problem, these >>>> DW_AT_entry_pc attributes will suddenly become valid and GDB will >>>> start using them. Until then, GDB continues to operate as it always >>>> has. >>>> >>>> An early version of this patch stored the entry-pc within the block >>>> like this: >>>> >>>> std::optional<CORE_ADDR> m_entry_pc; >>>> >>>> However, a concern was raised that this, on a 64-bit host, effectively >>>> increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, >>>> and 8-bytes for the std::optional's bool plus padding). >>>> >>>> If we remove the std::optional part and just use a CORE_ADDR then we >>>> need to have a "special" address to indicate if m_entry_pc is in use >>>> or not. I don't really like using special addresses; different >>>> targets can access different address ranges, even zero is a valid >>>> address on some targets. >>>> >>>> However, Bernd Edlinger suggested storing the entry-pc as an offset, >>>> and I think that will resolve my concerns. So, we store the entry-pc >>>> as a signed offset from the block's base address (the first address of >>>> the first range, or the start() address value if there are now >>>> ranges). Remember, ranges can be out of order, in which case the >>>> first address of the first range might be greater than the entry-pc. >>>> >>>> When GDB needs to read the entry-pc we can add the offset onto the >>>> blocks base address to recalculate it. >>>> >>>> With this done, on a 64-bit host, block only needs to increase by >>>> 8-bytes. >>>> >>>> The inline-entry.exp test was originally contributed by Bernd here: >>>> >>>> https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com >>>> >>>> though I have made some edits, making more use of lib/gdb.exp >>>> functions, making the gdb_test output patterns a little tighter, and >>>> updating the test to run with Clang. I also moved the test to >>>> gdb.opt/ as that seemed like a better home for it. >>>> >>>> Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> >>> >>> I've pushed the patch attached below. The only changes are some minor >>> test tweaks in order to get more of the 'check-all-boards' passing. >>> There are still some failures on e.g. gold and fission based boards, but >>> I think these might be due to issues with the board file. I'll post a >>> patch for those soon. >> >> I'm aware that this patch has caused a regression on the buildbot[1]. I'm >> investigating this but it will be at least tomorrow before I have fix. >> > > In case this helps, I see the same assert in a large amount of ada > test-cases. > > I reproduced the first one using gdb.ada/access_to_packed_array.exp, > with system gcc 7 and then with gcc 8. With gcc 9 - 14, the assert no > longer reproduces. Thanks Tom, I've posted a proposed fix here: https://inbox.sourceware.org/gdb-patches/23102df35a659d9ef4725d2d3822f08f2790e52d.1731612468.git.aburgess@redhat.com I would be really grateful if you could give this a try and let me know if this fixes all the problems you are seeing. I posted the fix as a new thread because it's not "fix a tiny bug" type thing that I'm happy to just commit without giving others a chance to fully review it. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-14 19:33 ` Andrew Burgess @ 2024-11-15 8:50 ` Tom de Vries 2024-11-15 10:53 ` Bernd Edlinger 2024-11-15 13:45 ` Andrew Burgess 0 siblings, 2 replies; 41+ messages in thread From: Tom de Vries @ 2024-11-15 8:50 UTC (permalink / raw) To: Andrew Burgess, gdb-patches; +Cc: Bernd Edlinger [-- Attachment #1: Type: text/plain, Size: 7570 bytes --] On 11/14/24 20:33, Andrew Burgess wrote: > Tom de Vries <tdevries@suse.de> writes: > >> On 11/13/24 17:59, Andrew Burgess wrote: >>> Andrew Burgess <aburgess@redhat.com> writes: >>> >>>> Andrew Burgess <aburgess@redhat.com> writes: >>>> >>>>> The entry PC for a DIE, e.g. an inline function, might not be the base >>>>> address of the DIE. Currently though, in block::entry_pc(), GDB >>>>> always returns the base address (low-pc or the first address of the >>>>> first range) as the entry PC. >>>>> >>>>> This commit extends the block class to carry the entry PC as a >>>>> separate member variable. Then the DWARF reader is extended to read >>>>> and set the entry PC for the block. Now in block::entry_pc(), if the >>>>> entry PC has been set, this is the value returned. >>>>> >>>>> If the entry-pc has not been set to a specific value then the old >>>>> behaviour of block::entry_pc() remains, GDB will use the block's base >>>>> address. Not every DIE will set the entry-pc, but GDB still needs to >>>>> have an entry-pc for every block, so the existing logic supplies the >>>>> entry-pc for any block where the entry-pc was not set. >>>>> >>>>> The DWARF-5 spec for reading the entry PC is a super-set of the spec >>>>> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >>>>> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >>>>> address, which is DW_AT_low_pc or the first address in the first range >>>>> specified by DW_AT_ranges if there is no DW_AT_low_pc. >>>>> >>>>> I have taken the approach of just implementing the DWARF-5 spec for >>>>> everyone. There doesn't seem to be any benefit to deliberately >>>>> ignoring a ranges based entry PC value for DWARF-4. If some naughty >>>>> compiler has emitted that, then lets use it. >>>>> >>>>> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >>>>> allows an address or a constant, where the constant is an offset from >>>>> the base address. I allow both approaches for all DWARF versions. >>>>> There doesn't seem to be any downsides to this approach. >>>>> >>>>> I ran into an issue when testing this patch where GCC would have the >>>>> DW_AT_entry_pc point to an empty range. When GDB parses the ranges >>>>> any empty ranges are ignored. As a consequence, the entry-pc appears >>>>> to be outside the address range of a block. >>>>> >>>>> The empty range problem is certainly something that we can, and should >>>>> address, but that is not the focus of this patch, so for now I'm >>>>> ignoring that problem. What I have done is added a check: if the >>>>> DW_AT_entry_pc is outside the range of a block then the entry-pc is >>>>> ignored, GDB will then fall-back to its default algorithm for >>>>> computing the entry-pc. >>>>> >>>>> If/when in the future we address the empty range problem, these >>>>> DW_AT_entry_pc attributes will suddenly become valid and GDB will >>>>> start using them. Until then, GDB continues to operate as it always >>>>> has. >>>>> >>>>> An early version of this patch stored the entry-pc within the block >>>>> like this: >>>>> >>>>> std::optional<CORE_ADDR> m_entry_pc; >>>>> >>>>> However, a concern was raised that this, on a 64-bit host, effectively >>>>> increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, >>>>> and 8-bytes for the std::optional's bool plus padding). >>>>> >>>>> If we remove the std::optional part and just use a CORE_ADDR then we >>>>> need to have a "special" address to indicate if m_entry_pc is in use >>>>> or not. I don't really like using special addresses; different >>>>> targets can access different address ranges, even zero is a valid >>>>> address on some targets. >>>>> >>>>> However, Bernd Edlinger suggested storing the entry-pc as an offset, >>>>> and I think that will resolve my concerns. So, we store the entry-pc >>>>> as a signed offset from the block's base address (the first address of >>>>> the first range, or the start() address value if there are now >>>>> ranges). Remember, ranges can be out of order, in which case the >>>>> first address of the first range might be greater than the entry-pc. >>>>> >>>>> When GDB needs to read the entry-pc we can add the offset onto the >>>>> blocks base address to recalculate it. >>>>> >>>>> With this done, on a 64-bit host, block only needs to increase by >>>>> 8-bytes. >>>>> >>>>> The inline-entry.exp test was originally contributed by Bernd here: >>>>> >>>>> https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com >>>>> >>>>> though I have made some edits, making more use of lib/gdb.exp >>>>> functions, making the gdb_test output patterns a little tighter, and >>>>> updating the test to run with Clang. I also moved the test to >>>>> gdb.opt/ as that seemed like a better home for it. >>>>> >>>>> Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> >>>> >>>> I've pushed the patch attached below. The only changes are some minor >>>> test tweaks in order to get more of the 'check-all-boards' passing. >>>> There are still some failures on e.g. gold and fission based boards, but >>>> I think these might be due to issues with the board file. I'll post a >>>> patch for those soon. >>> >>> I'm aware that this patch has caused a regression on the buildbot[1]. I'm >>> investigating this but it will be at least tomorrow before I have fix. >>> >> >> In case this helps, I see the same assert in a large amount of ada >> test-cases. >> >> I reproduced the first one using gdb.ada/access_to_packed_array.exp, >> with system gcc 7 and then with gcc 8. With gcc 9 - 14, the assert no >> longer reproduces. > > Thanks Tom, > > I've posted a proposed fix here: > > https://inbox.sourceware.org/gdb-patches/23102df35a659d9ef4725d2d3822f08f2790e52d.1731612468.git.aburgess@redhat.com > > I would be really grateful if you could give this a try and let me know > if this fixes all the problems you are seeing. > Hi Andrew, it does fix all the regressions in ada test-cases. There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): ... FAIL: gdb.opt/inline-entry.exp: continue to foo FAIL: gdb.opt/inline-entry.exp: continue until exit ... Gdb.log attached. The test-case sets a breakpoint at foo and bar, and expects that the program: - stops at bar, - stops at foo, and - exits. This is what I see when using -O0. What happens instead (with -O2) is that the program: - stops at bar, - stops at bar again, - stops at foo, and - exits. For context, relevant code: ... inline __attribute__((always_inline)) int bar (int val) { if (global == val) return 1; foo (1); return 1; } int main (void) { if ((global && bar (1)) || bar (2)) return 0; return 1; } ... From what I can tell, what happened in source terms is that the compiler inlined the call "bar (1)", and then moved the first instruction of bar to before the &&: In disassembly: ... 00000000004003c0 <main>: 4003c0: mov 0x1c5e(%rip),%eax # Load global to reg 4003c6: test %eax,%eax # If global is zero, set ZF 4003c8: mov 0x1c56(%rip),%eax # Local global to reg (1st of bar) 4003ce: je 4003d8 <main+0x18> # If the ZF is set, skip "bar (1)" ... At first glance, gdb is behaving ok. Thanks, - Tom > I posted the fix as a new thread because it's not "fix a tiny bug" type > thing that I'm happy to just commit without giving others a chance to > fully review it. > > Thanks, > Andrew > [-- Attachment #2: gdb.log --] [-- Type: text/x-log, Size: 6251 bytes --] Test run by vries on Fri Nov 15 09:04:46 2024 Native configuration is x86_64-pc-linux-gnu === gdb tests === Schedule of variations: unix Running target unix Using /usr/share/dejagnu/baseboards/unix.exp as board description file for target. Using /usr/share/dejagnu/config/unix.exp as generic interface file for target. Using /data/vries/gdb/src/gdb/testsuite/config/unix.exp as tool-and-target-specific interface file. Running /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.exp ... Executing on build: rm -rf /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry (timeout = 300) builtin_spawn -ignore SIGHUP rm -rf /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry gdb_do_cache: universal_compile_options_c ( ) Executing on host: gcc -fdiagnostics-color=never -c -o /data/vries/gdb/leap-15-5/build/gdb/testsuite/temp/2484/ccopts.o /data/vries/gdb/leap-15-5/build/gdb/testsuite/temp/2484/ccopts.c (timeout = 300) builtin_spawn -ignore SIGHUP gcc -fdiagnostics-color=never -c -o /data/vries/gdb/leap-15-5/build/gdb/testsuite/temp/2484/ccopts.o /data/vries/gdb/leap-15-5/build/gdb/testsuite/temp/2484/ccopts.c get_compiler_info: gcc-7-5-0 Executing on host: gcc -fno-stack-protector -fdiagnostics-color=never -I/data/vries/gdb/src/gdb/testsuite/lib -c -g -O2 -o /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry0.o /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c (timeout = 300) builtin_spawn -ignore SIGHUP gcc -fno-stack-protector -fdiagnostics-color=never -I/data/vries/gdb/src/gdb/testsuite/lib -c -g -O2 -o /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry0.o /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c gdb_do_cache: universal_compile_options_c ( ) Executing on host: gcc -fno-stack-protector /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry0.o -fdiagnostics-color=never -I/data/vries/gdb/src/gdb/testsuite/lib -g -O2 -lm -o /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry (timeout = 300) builtin_spawn -ignore SIGHUP gcc -fno-stack-protector /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry0.o -fdiagnostics-color=never -I/data/vries/gdb/src/gdb/testsuite/lib -g -O2 -lm -o /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry builtin_spawn /data/vries/gdb/leap-15-5/build/gdb/testsuite/../../gdb/gdb -nw -nx -q -iex set height 0 -iex set width 0 -data-directory /data/vries/gdb/leap-15-5/build/gdb/data-directory (gdb) set height 0 (gdb) set width 0 (gdb) dir Reinitialize source path to empty? (y or n) y Source directories searched: $cdir:$cwd (gdb) dir /data/vries/gdb/src/gdb/testsuite/gdb.opt Source directories searched: /data/vries/gdb/src/gdb/testsuite/gdb.opt:$cdir:$cwd (gdb) kill The program is not being run. (gdb) file /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry Reading symbols from /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry... (gdb) delete breakpoints (gdb) info breakpoints No breakpoints, watchpoints, tracepoints, or catchpoints. (gdb) break -qualified main Breakpoint 1 at 0x4003d0: file /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c, line 38. (gdb) run Starting program: /data/vries/gdb/leap-15-5/build/gdb/testsuite/outputs/gdb.opt/inline-entry/inline-entry Breakpoint 1, main () at /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c:38 38 if ((global && bar (1)) || bar (2)) (gdb) info source Current source file is /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c Compilation directory is /data/vries/gdb/leap-15-5/build/gdb/testsuite Located in /data/vries/gdb/binutils-gdb.git/gdb/testsuite/gdb.opt/inline-entry.c Contains 41 lines. Source language is c. Producer is GNU C11 7.5.0 -mtune=generic -march=x86-64 -g -O2 -fno-stack-protector. Compiled with DWARF 4 debugging format. Does not include preprocessor macro info. (gdb) break bar Breakpoint 2 at 0x4003d8: bar. (2 locations) (gdb) print /d $bpnum $1 = 2 (gdb) PASS: gdb.opt/inline-entry.exp: get number of bar breakpoint break foo Breakpoint 3 at 0x4004e0: file /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c, line 23. (gdb) print /d $bpnum $2 = 3 (gdb) PASS: gdb.opt/inline-entry.exp: get number of foo breakpoint continue Continuing. Breakpoint 2.1, bar (val=<optimized out>) at /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c:29 29 if (global == val) (gdb) PASS: gdb.opt/inline-entry.exp: continue to bar continue Continuing. Breakpoint 2.2, bar (val=2) at /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c:29 29 if (global == val) (gdb) FAIL: gdb.opt/inline-entry.exp: continue to foo continue Continuing. Breakpoint 3, foo (arg=arg@entry=1) at /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.c:23 23 global += arg; (gdb) FAIL: gdb.opt/inline-entry.exp: continue until exit testcase /data/vries/gdb/src/gdb/testsuite/gdb.opt/inline-entry.exp completed in 0 seconds === gdb Summary === # of expected passes 3 # of unexpected failures 2 Executing on host: /data/vries/gdb/leap-15-5/build/gdb/testsuite/../../gdb/gdb -nw -nx -q -iex "set height 0" -iex "set width 0" -data-directory /data/vries/gdb/leap-15-5/build/gdb/data-directory --version (timeout = 300) builtin_spawn -ignore SIGHUP /data/vries/gdb/leap-15-5/build/gdb/testsuite/../../gdb/gdb -nw -nx -q -iex set height 0 -iex set width 0 -data-directory /data/vries/gdb/leap-15-5/build/gdb/data-directory --version GNU gdb (GDB) 16.0.50.20241114-git Copyright (C) 2024 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. /data/vries/gdb/leap-15-5/build/gdb/gdb version 16.0.50.20241114-git -nw -nx -q -iex "set height 0" -iex "set width 0" -data-directory /data/vries/gdb/leap-15-5/build/gdb/data-directory runtest completed at Fri Nov 15 09:04:46 2024 ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 8:50 ` Tom de Vries @ 2024-11-15 10:53 ` Bernd Edlinger 2024-11-15 14:00 ` Andrew Burgess 2024-11-15 13:45 ` Andrew Burgess 1 sibling, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-11-15 10:53 UTC (permalink / raw) To: Tom de Vries, Andrew Burgess, gdb-patches On 11/15/24 09:50, Tom de Vries wrote: > Hi Andrew, > > it does fix all the regressions in ada test-cases. > > There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): > ... > FAIL: gdb.opt/inline-entry.exp: continue to foo > FAIL: gdb.opt/inline-entry.exp: continue until exit > ... > I think that is to be expected, the test case depends on the statement frontiers feature which initially introduced in gcc-8 by Alexandre Oliva. The -gstatement-frontiers enables things like non-statement line number without that it wont work. my patch had this guard to prevent such fallout: if { [test_compiler_info gcc*] && ![supports_statement_frontiers] } { untested "this test needs gcc with statement frontiers" return -1 } global srcfile testfile set options {debug nowarnings optimize=-O2} if { [supports_statement_frontiers] } { lappend options additional_flags=-gstatement-frontiers } Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 10:53 ` Bernd Edlinger @ 2024-11-15 14:00 ` Andrew Burgess 2024-11-15 14:30 ` Tom de Vries 2024-11-15 17:00 ` Bernd Edlinger 0 siblings, 2 replies; 41+ messages in thread From: Andrew Burgess @ 2024-11-15 14:00 UTC (permalink / raw) To: Bernd Edlinger, Tom de Vries, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > On 11/15/24 09:50, Tom de Vries wrote: >> Hi Andrew, >> >> it does fix all the regressions in ada test-cases. >> >> There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): >> ... >> FAIL: gdb.opt/inline-entry.exp: continue to foo >> FAIL: gdb.opt/inline-entry.exp: continue until exit >> ... >> > > I think that is to be expected, the test case depends on the statement frontiers > feature which initially introduced in gcc-8 by Alexandre Oliva. > > The -gstatement-frontiers enables things like non-statement line number without > that it wont work. > > my patch had this guard to prevent such fallout: > > if { [test_compiler_info gcc*] && ![supports_statement_frontiers] } { > untested "this test needs gcc with statement frontiers" > return -1 > } > > global srcfile testfile > > set options {debug nowarnings optimize=-O2} > if { [supports_statement_frontiers] } { > lappend options additional_flags=-gstatement-frontiers > } Sorry Bernd, I missed this reply before posting a few moments ago. Below is a patch based on your feedback above. With this in place the test is skipped for gcc 7.x (and older), but continues to run for Clang. Let me know what you think, Thanks, Andrew --- commit e79041be4a93a393dbf78e245592ed4eadfb609e Author: Andrew Burgess <aburgess@redhat.com> Date: Fri Nov 15 13:07:09 2024 +0000 gdb/testsuite: skip gdb.opt/inline-entry.exp for gcc 7 and older It was pointed out that the recently added gdb.opt/inline-entry.exp test would fail when run using gcc 7 and earlier, on an x86-64 target: https://inbox.sourceware.org/gdb-patches/9fe35ea1-d99b-444d-bd1b-e3a1f108dd77@suse.de Bernd Edlinger points out that, for GCC, the test relies on the -gstatement-frontiers work which was added in GCC 8.x: https://inbox.sourceware.org/gdb-patches/DU2PR08MB10263357597688D9D66EA745CE4242@DU2PR08MB10263.eurprd08.prod.outlook.com For GCC 7.x and older, the compiler is indeed generating incorrect DW_AT_entry_pc values. Here is the interesting source line from inline-entry.c: if ((global && bar (1)) || bar (2)) And here's some of the relevant disassembly output: Dump of assembler code for function main: 0x401020 <+0>: mov 0x3006(%rip),%eax (1) 0x401026 <+6>: test %eax,%eax (2) 0x401028 <+8>: mov 0x2ffe(%rip),%eax (3) 0x40102e <+14>: je 0x401038 <main+24> (4) 0x401030 <+16>: sub $0x1,%eax (5) 0x401033 <+19>: jne 0x40103d <main+29> (6) Lines (1), (2), and (4) represent the check of 'global'. However, line (3) is actually the first instruction for 'bar' which has been inlined. Lines (5) and (6) are also part of the first inlined 'bar' function. If the check of 'global' returns false then the first call to 'bar' should never happen, this is accomplished by the branch at (4) being taken. For gcc 8+, gcc generates a DW_AT_entry_pc with the value 0x401030, this is where GDB places a breakpoint for 'bar', and this address is after the branch at line (4), and so, if the call to 'bar' never happens, the breakpoint is never hit. For gcc 7 and older, gcc generates a DW_AT_entry_pc with the value 0x401028, which also happens to be the first address associated with the inline 'bar' function. Unfortunately, this address is also before the check of 'global' has completed, this means that GDB hits the 'bar' breakpoint before the inferior has decided if 'bar' should actually be called or not. I don't think there's really much GDB can do in this case, given the misleading debug information. I've incorporated the sanity check for -gstatement-frontiers support that Bernd suggests. Now the test will be skipped for older versions of GCC. diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp index 6f87ea3a00d..5c64d734eaf 100644 --- a/gdb/testsuite/gdb.opt/inline-entry.exp +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -21,10 +21,18 @@ # Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the # Clang generated code didn't depend on the entry_pc being parsed. +# Older versions of GCC, those prior to the -gstatement-frontiers work +# added in 8.x, would generate incorrect DW_AT_entry_pc values, +# causing the test to fail. +require {expr ![is_c_compiler_gcc] || [supports_statement_frontiers]} + standard_testfile set options {debug optimize=-O2} lappend_include_file options $srcdir/lib/attributes.h +if { [supports_statement_frontiers] } { + lappend options additional_flags=-gstatement-frontiers +} if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { return ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 14:00 ` Andrew Burgess @ 2024-11-15 14:30 ` Tom de Vries 2024-11-15 16:46 ` Andrew Burgess 2024-11-15 19:24 ` Andrew Burgess 2024-11-15 17:00 ` Bernd Edlinger 1 sibling, 2 replies; 41+ messages in thread From: Tom de Vries @ 2024-11-15 14:30 UTC (permalink / raw) To: Andrew Burgess, Bernd Edlinger, gdb-patches On 11/15/24 15:00, Andrew Burgess wrote: > Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > >> On 11/15/24 09:50, Tom de Vries wrote: >>> Hi Andrew, >>> >>> it does fix all the regressions in ada test-cases. >>> >>> There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): >>> ... >>> FAIL: gdb.opt/inline-entry.exp: continue to foo >>> FAIL: gdb.opt/inline-entry.exp: continue until exit >>> ... >>> >> >> I think that is to be expected, the test case depends on the statement frontiers >> feature which initially introduced in gcc-8 by Alexandre Oliva. >> >> The -gstatement-frontiers enables things like non-statement line number without >> that it wont work. >> >> my patch had this guard to prevent such fallout: >> >> if { [test_compiler_info gcc*] && ![supports_statement_frontiers] } { >> untested "this test needs gcc with statement frontiers" >> return -1 >> } >> >> global srcfile testfile >> >> set options {debug nowarnings optimize=-O2} >> if { [supports_statement_frontiers] } { >> lappend options additional_flags=-gstatement-frontiers >> } > > Sorry Bernd, I missed this reply before posting a few moments ago. > > Below is a patch based on your feedback above. With this in place the > test is skipped for gcc 7.x (and older), but continues to run for Clang. > > Let me know what you think, > > Thanks, > Andrew > > --- > > commit e79041be4a93a393dbf78e245592ed4eadfb609e > Author: Andrew Burgess <aburgess@redhat.com> > Date: Fri Nov 15 13:07:09 2024 +0000 > > gdb/testsuite: skip gdb.opt/inline-entry.exp for gcc 7 and older > > It was pointed out that the recently added gdb.opt/inline-entry.exp > test would fail when run using gcc 7 and earlier, on an x86-64 target: > > https://inbox.sourceware.org/gdb-patches/9fe35ea1-d99b-444d-bd1b-e3a1f108dd77@suse.de > > Bernd Edlinger points out that, for GCC, the test relies on the > -gstatement-frontiers work which was added in GCC 8.x: > > https://inbox.sourceware.org/gdb-patches/DU2PR08MB10263357597688D9D66EA745CE4242@DU2PR08MB10263.eurprd08.prod.outlook.com > > For GCC 7.x and older, the compiler is indeed generating incorrect > DW_AT_entry_pc values. > I think the "incorrect" is too strong. It might actually be argued, given the definition of DW_AT_entry_pc in the dwarf standard that gcc 7 gets it right and gcc 8 gets it wrong. But, the interpretation of gcc 8 is much better for debugging experience. So, choosing something more neutral like "different" might be a good idea. > Here is the interesting source line from inline-entry.c: > > if ((global && bar (1)) || bar (2)) > > And here's some of the relevant disassembly output: > > Dump of assembler code for function main: > 0x401020 <+0>: mov 0x3006(%rip),%eax (1) > 0x401026 <+6>: test %eax,%eax (2) > 0x401028 <+8>: mov 0x2ffe(%rip),%eax (3) > 0x40102e <+14>: je 0x401038 <main+24> (4) > 0x401030 <+16>: sub $0x1,%eax (5) > 0x401033 <+19>: jne 0x40103d <main+29> (6) > > Lines (1), (2), and (4) represent the check of 'global'. However, > line (3) is actually the first instruction for 'bar' which has been > inlined. Lines (5) and (6) are also part of the first inlined 'bar' > function. > > If the check of 'global' returns false then the first call to 'bar' > should never happen, this is accomplished by the branch at (4) being > taken. > > For gcc 8+, gcc generates a DW_AT_entry_pc with the value 0x401030, > this is where GDB places a breakpoint for 'bar', and this address is > after the branch at line (4), and so, if the call to 'bar' never > happens, the breakpoint is never hit. > > For gcc 7 and older, gcc generates a DW_AT_entry_pc with the value > 0x401028, which also happens to be the first address associated with > the inline 'bar' function. Unfortunately, this address is also before > the check of 'global' has completed, this means that GDB hits the > 'bar' breakpoint before the inferior has decided if 'bar' should > actually be called or not. > > I don't think there's really much GDB can do in this case, given the > misleading debug information. > As above, "misleading" is perhaps too strong, I'd drop it. Regardless, LGTM. Approved-By: Tom de Vries <tdevries@suse.de> Thanks, - Tom > I've incorporated the sanity check for -gstatement-frontiers support > that Bernd suggests. Now the test will be skipped for older versions > of GCC. > > diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp > index 6f87ea3a00d..5c64d734eaf 100644 > --- a/gdb/testsuite/gdb.opt/inline-entry.exp > +++ b/gdb/testsuite/gdb.opt/inline-entry.exp > @@ -21,10 +21,18 @@ > # Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the > # Clang generated code didn't depend on the entry_pc being parsed. > > +# Older versions of GCC, those prior to the -gstatement-frontiers work > +# added in 8.x, would generate incorrect DW_AT_entry_pc values, > +# causing the test to fail. > +require {expr ![is_c_compiler_gcc] || [supports_statement_frontiers]} > + > standard_testfile > > set options {debug optimize=-O2} > lappend_include_file options $srcdir/lib/attributes.h > +if { [supports_statement_frontiers] } { > + lappend options additional_flags=-gstatement-frontiers > +} > > if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { > return > ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 14:30 ` Tom de Vries @ 2024-11-15 16:46 ` Andrew Burgess 2024-11-15 19:24 ` Andrew Burgess 1 sibling, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-11-15 16:46 UTC (permalink / raw) To: Tom de Vries, Bernd Edlinger, gdb-patches Tom de Vries <tdevries@suse.de> writes: > On 11/15/24 15:00, Andrew Burgess wrote: >> Bernd Edlinger <bernd.edlinger@hotmail.de> writes: >> >>> On 11/15/24 09:50, Tom de Vries wrote: >>>> Hi Andrew, >>>> >>>> it does fix all the regressions in ada test-cases. >>>> >>>> There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): >>>> ... >>>> FAIL: gdb.opt/inline-entry.exp: continue to foo >>>> FAIL: gdb.opt/inline-entry.exp: continue until exit >>>> ... >>>> >>> >>> I think that is to be expected, the test case depends on the statement frontiers >>> feature which initially introduced in gcc-8 by Alexandre Oliva. >>> >>> The -gstatement-frontiers enables things like non-statement line number without >>> that it wont work. >>> >>> my patch had this guard to prevent such fallout: >>> >>> if { [test_compiler_info gcc*] && ![supports_statement_frontiers] } { >>> untested "this test needs gcc with statement frontiers" >>> return -1 >>> } >>> >>> global srcfile testfile >>> >>> set options {debug nowarnings optimize=-O2} >>> if { [supports_statement_frontiers] } { >>> lappend options additional_flags=-gstatement-frontiers >>> } >> >> Sorry Bernd, I missed this reply before posting a few moments ago. >> >> Below is a patch based on your feedback above. With this in place the >> test is skipped for gcc 7.x (and older), but continues to run for Clang. >> >> Let me know what you think, >> >> Thanks, >> Andrew >> >> --- >> >> commit e79041be4a93a393dbf78e245592ed4eadfb609e >> Author: Andrew Burgess <aburgess@redhat.com> >> Date: Fri Nov 15 13:07:09 2024 +0000 >> >> gdb/testsuite: skip gdb.opt/inline-entry.exp for gcc 7 and older >> >> It was pointed out that the recently added gdb.opt/inline-entry.exp >> test would fail when run using gcc 7 and earlier, on an x86-64 target: >> >> https://inbox.sourceware.org/gdb-patches/9fe35ea1-d99b-444d-bd1b-e3a1f108dd77@suse.de >> >> Bernd Edlinger points out that, for GCC, the test relies on the >> -gstatement-frontiers work which was added in GCC 8.x: >> >> https://inbox.sourceware.org/gdb-patches/DU2PR08MB10263357597688D9D66EA745CE4242@DU2PR08MB10263.eurprd08.prod.outlook.com >> >> For GCC 7.x and older, the compiler is indeed generating incorrect >> DW_AT_entry_pc values. >> > > I think the "incorrect" is too strong. > > It might actually be argued, given the definition of DW_AT_entry_pc in > the dwarf standard that gcc 7 gets it right and gcc 8 gets it wrong. > > But, the interpretation of gcc 8 is much better for debugging experience. > > So, choosing something more neutral like "different" might be a good > idea. Thanks for pointing this out. You are correct, I'll reword things throughout and then get this committed, though it might end up being tomorrow before that happens now. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 14:30 ` Tom de Vries 2024-11-15 16:46 ` Andrew Burgess @ 2024-11-15 19:24 ` Andrew Burgess 2024-11-17 23:52 ` Bernd Edlinger 1 sibling, 1 reply; 41+ messages in thread From: Andrew Burgess @ 2024-11-15 19:24 UTC (permalink / raw) To: Tom de Vries, Bernd Edlinger, gdb-patches Tom de Vries <tdevries@suse.de> writes: > On 11/15/24 15:00, Andrew Burgess wrote: >> Bernd Edlinger <bernd.edlinger@hotmail.de> writes: >> >>> On 11/15/24 09:50, Tom de Vries wrote: >>>> Hi Andrew, >>>> >>>> it does fix all the regressions in ada test-cases. >>>> >>>> There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): >>>> ... >>>> FAIL: gdb.opt/inline-entry.exp: continue to foo >>>> FAIL: gdb.opt/inline-entry.exp: continue until exit >>>> ... >>>> >>> >>> I think that is to be expected, the test case depends on the statement frontiers >>> feature which initially introduced in gcc-8 by Alexandre Oliva. >>> >>> The -gstatement-frontiers enables things like non-statement line number without >>> that it wont work. >>> >>> my patch had this guard to prevent such fallout: >>> >>> if { [test_compiler_info gcc*] && ![supports_statement_frontiers] } { >>> untested "this test needs gcc with statement frontiers" >>> return -1 >>> } >>> >>> global srcfile testfile >>> >>> set options {debug nowarnings optimize=-O2} >>> if { [supports_statement_frontiers] } { >>> lappend options additional_flags=-gstatement-frontiers >>> } >> >> Sorry Bernd, I missed this reply before posting a few moments ago. >> >> Below is a patch based on your feedback above. With this in place the >> test is skipped for gcc 7.x (and older), but continues to run for Clang. >> >> Let me know what you think, >> >> Thanks, >> Andrew >> >> --- >> >> commit e79041be4a93a393dbf78e245592ed4eadfb609e >> Author: Andrew Burgess <aburgess@redhat.com> >> Date: Fri Nov 15 13:07:09 2024 +0000 >> >> gdb/testsuite: skip gdb.opt/inline-entry.exp for gcc 7 and older >> >> It was pointed out that the recently added gdb.opt/inline-entry.exp >> test would fail when run using gcc 7 and earlier, on an x86-64 target: >> >> https://inbox.sourceware.org/gdb-patches/9fe35ea1-d99b-444d-bd1b-e3a1f108dd77@suse.de >> >> Bernd Edlinger points out that, for GCC, the test relies on the >> -gstatement-frontiers work which was added in GCC 8.x: >> >> https://inbox.sourceware.org/gdb-patches/DU2PR08MB10263357597688D9D66EA745CE4242@DU2PR08MB10263.eurprd08.prod.outlook.com >> >> For GCC 7.x and older, the compiler is indeed generating incorrect >> DW_AT_entry_pc values. >> > > I think the "incorrect" is too strong. > > It might actually be argued, given the definition of DW_AT_entry_pc in > the dwarf standard that gcc 7 gets it right and gcc 8 gets it wrong. > > But, the interpretation of gcc 8 is much better for debugging experience. > > So, choosing something more neutral like "different" might be a good idea. > >> Here is the interesting source line from inline-entry.c: >> >> if ((global && bar (1)) || bar (2)) >> >> And here's some of the relevant disassembly output: >> >> Dump of assembler code for function main: >> 0x401020 <+0>: mov 0x3006(%rip),%eax (1) >> 0x401026 <+6>: test %eax,%eax (2) >> 0x401028 <+8>: mov 0x2ffe(%rip),%eax (3) >> 0x40102e <+14>: je 0x401038 <main+24> (4) >> 0x401030 <+16>: sub $0x1,%eax (5) >> 0x401033 <+19>: jne 0x40103d <main+29> (6) >> >> Lines (1), (2), and (4) represent the check of 'global'. However, >> line (3) is actually the first instruction for 'bar' which has been >> inlined. Lines (5) and (6) are also part of the first inlined 'bar' >> function. >> >> If the check of 'global' returns false then the first call to 'bar' >> should never happen, this is accomplished by the branch at (4) being >> taken. >> >> For gcc 8+, gcc generates a DW_AT_entry_pc with the value 0x401030, >> this is where GDB places a breakpoint for 'bar', and this address is >> after the branch at line (4), and so, if the call to 'bar' never >> happens, the breakpoint is never hit. >> >> For gcc 7 and older, gcc generates a DW_AT_entry_pc with the value >> 0x401028, which also happens to be the first address associated with >> the inline 'bar' function. Unfortunately, this address is also before >> the check of 'global' has completed, this means that GDB hits the >> 'bar' breakpoint before the inferior has decided if 'bar' should >> actually be called or not. >> >> I don't think there's really much GDB can do in this case, given the >> misleading debug information. >> > > As above, "misleading" is perhaps too strong, I'd drop it. > > Regardless, LGTM. > > Approved-By: Tom de Vries <tdevries@suse.de> I pushed the version of the patch below. Thanks, Andrew --- commit 82eff6743b77908a502b4cf9030acc93caf69e74 Author: Andrew Burgess <aburgess@redhat.com> Date: Fri Nov 15 13:07:09 2024 +0000 gdb/testsuite: skip gdb.opt/inline-entry.exp for gcc 7 and older It was pointed out that the recently added gdb.opt/inline-entry.exp test would fail when run using gcc 7 and earlier, on an x86-64 target: https://inbox.sourceware.org/gdb-patches/9fe35ea1-d99b-444d-bd1b-e3a1f108dd77@suse.de Bernd Edlinger points out that, for gcc, the test relies on the -gstatement-frontiers work which was added in gcc 8.x: https://inbox.sourceware.org/gdb-patches/DU2PR08MB10263357597688D9D66EA745CE4242@DU2PR08MB10263.eurprd08.prod.outlook.com For gcc 7.x and older, without the -gstatement-frontiers work, the compiler uses DW_AT_entry_pc differently, which leads to a poorer debug experience. Here is the interesting source line from inline-entry.c: if ((global && bar (1)) || bar (2)) And here's some of the relevant disassembly output: Dump of assembler code for function main: 0x401020 <+0>: mov 0x3006(%rip),%eax (1) 0x401026 <+6>: test %eax,%eax (2) 0x401028 <+8>: mov 0x2ffe(%rip),%eax (3) 0x40102e <+14>: je 0x401038 <main+24> (4) 0x401030 <+16>: sub $0x1,%eax (5) 0x401033 <+19>: jne 0x40103d <main+29> (6) Lines (1), (2), and (4) represent the check of 'global'. However, line (3) is actually the first instruction for 'bar' which has been inlined. Lines (5) and (6) are also part of the first inlined 'bar' function. If the check of 'global' returns false then the first call to 'bar' should never happen, this is accomplished by the branch at (4) being taken. For gcc 8+, gcc generates a DW_AT_entry_pc with the value 0x401030, this is where GDB places a breakpoint for 'bar', and this address is after the branch at line (4), and so, if the call to 'bar' never happens, the breakpoint is never hit. For gcc 7 and older, gcc generates a DW_AT_entry_pc with the value 0x401028, which is the first address associated with the inline 'bar' function. Unfortunately, this address is also before the check of 'global' has completed, this means that GDB hits the 'bar' breakpoint before the inferior has decided if 'bar' should actually be called or not. I don't think there's really much GDB can do in the older gcc versions, we are placing the breakpoint at the entry point, and this is within bar. Given that this test does really depend on the newer gcc behaviour, I think the only sensible solution is to skip this test when an older version of gcc is being used. I've incorporated the check for -gstatement-frontiers support that Bernd suggested and now the test will be skipped for older versions of GCC. Approved-By: Tom de Vries <tdevries@suse.de> diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp index 6f87ea3a00d..16443c6f30b 100644 --- a/gdb/testsuite/gdb.opt/inline-entry.exp +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -21,10 +21,27 @@ # Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the # Clang generated code didn't depend on the entry_pc being parsed. +# Older versions of GCC, those prior to the -gstatement-frontiers work +# added in 8.x, would generate DW_AT_entry_pc values pointing to the +# first instruction of an inlined function. This first instruction +# could then be reordered such that the first instruction might be +# executed even when the actual call to the inline function ended up +# being skipped. GDB can then hit a breakpoint for a function that +# ends up never being called. +# +# This test is specifically testing that GDB correctly handles the +# case where DW_AT_entry_pc is not the first instruction of an inlined +# function, as can be the case in gcc 8.x with the +# -gstatement-frontiers work in place. +require {expr ![is_c_compiler_gcc] || [supports_statement_frontiers]} + standard_testfile set options {debug optimize=-O2} lappend_include_file options $srcdir/lib/attributes.h +if { [supports_statement_frontiers] } { + lappend options additional_flags=-gstatement-frontiers +} if { [prepare_for_testing "failed to prepare" $binfile $srcfile $options] } { return ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 19:24 ` Andrew Burgess @ 2024-11-17 23:52 ` Bernd Edlinger 2024-11-19 9:29 ` Andrew Burgess 0 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-11-17 23:52 UTC (permalink / raw) To: Andrew Burgess, Tom de Vries, gdb-patches On 11/15/24 20:24, Andrew Burgess wrote: > Tom de Vries <tdevries@suse.de> writes: [...] >> Regardless, LGTM. >> >> Approved-By: Tom de Vries <tdevries@suse.de> > > I pushed the version of the patch below. > > Thanks, > Andrew > Hmm Sorry Andrey, but I have noticed another difference in the patch which unfortunately weakens it a bit. I've overlooked it before, but my patch had a couple __builtin_expect here in bar and main: inline __attribute__((always_inline)) int bar (int val) { if (__builtin_expect(global == val, 0)) return 1; foo (1); return 1; } int main (void) { if ((__builtin_expect(global, 0) && bar (1)) || bar (2)) return 1; return 0; } but those are removed from your version, I have added them to make the optimizer do some wrong decisions that what I wanted it to do to get the bar function have an entry pc not at the first subrange. Without them the test case still works in x86_64 but is weaker, because the bar function has more subranges. With the original test case x86_64 has this debug info: <2><6e>: Abbrev Number: 6 (DW_TAG_inlined_subroutine) <6f> DW_AT_abstract_origin: <0xd2> <73> DW_AT_entry_pc : 0x1065 <7b> DW_AT_GNU_entry_view: 0 <7c> DW_AT_ranges : 0xc 0000000c 0000000000001048 (base address) 00000015 0000000000001048 000000000000104e 00000018 0000000000001055 000000000000105f 0000001b 0000000000001065 000000000000106c 0000001e <End of list> so three separate ranges, and the entry_pc is the third one, while the test case now has only two subranges: <2><6e>: Abbrev Number: 6 (DW_TAG_inlined_subroutine) <6f> DW_AT_abstract_origin: <0xd2> <73> DW_AT_entry_pc : 0x1050 <7b> DW_AT_GNU_entry_view: 0 <7c> DW_AT_ranges : 0xc 0000000c 0000000000001048 (base address) 00000015 0000000000001048 000000000000104e 00000018 0000000000001050 000000000000105f 0000001b <End of list> Yet the test case is still functional with x86_64. But with powerpc64-unknown-linux-gcc-9 I have seen that the the bar function does no longer have the entry_pc at the lowest address: This is the code from the current version of the test case: 0000000010000400 <.main>: 10000400: 60 00 00 00 nop 10000404: 81 22 81 68 lwz r9,-32408(r2) 10000408: 2c 09 00 00 cmpwi r9,0 1000040c: 41 82 00 38 beq 10000444 <.main+0x44> 10000410: 81 22 81 68 lwz r9,-32408(r2) 10000414: 2c 09 00 01 cmpwi r9,1 10000418: 41 82 00 3c beq 10000454 <.main+0x54> 1000041c: 7c 08 02 a6 mflr r0 10000420: 38 60 00 01 li r3,1 10000424: f8 01 00 10 std r0,16(r1) 10000428: f8 21 ff 91 stdu r1,-112(r1) 1000042c: 48 00 02 35 bl 10000660 <.foo> <a6e> DW_AT_abstract_origin: <0xad7> <a72> DW_AT_entry_pc : 0x10000410 <a7a> DW_AT_GNU_entry_view: 0 <a7b> DW_AT_ranges : 0x40 00000040 0000000010000410 000000001000041c 00000040 0000000010000420 0000000010000424 00000040 000000001000042c 0000000010000430 00000040 <End of list> so this test cannot fail even before the fix. While with the original version the code was: 0000000010000400 <.main>: 10000400: 60 00 00 00 nop 10000404: 81 22 81 68 lwz r9,-32408(r2) 10000408: 2c 09 00 00 cmpwi r9,0 1000040c: 60 00 00 00 nop 10000410: 81 22 81 68 lwz r9,-32408(r2) 10000414: 40 82 00 34 bne 10000448 <.main+0x48> 10000418: 2c 09 00 02 cmpwi r9,2 1000041c: 41 82 00 34 beq 10000450 <.main+0x50> 10000420: 7c 08 02 a6 mflr r0 10000424: 38 60 00 01 li r3,1 10000428: f8 01 00 10 std r0,16(r1) 1000042c: f8 21 ff 91 stdu r1,-112(r1) 10000430: 48 00 02 21 bl 10000650 <.foo> 10000448: 2c 09 00 01 cmpwi r9,1 1000044c: 40 82 ff d4 bne 10000420 <.main+0x20> 10000450: 38 60 00 01 li r3,1 10000454: 4e 80 00 20 blr <2><a6d>: Abbrev Number: 6 (DW_TAG_inlined_subroutine) <a6e> DW_AT_abstract_origin: <0xad7> <a72> DW_AT_entry_pc : 0x10000448 <a7a> DW_AT_GNU_entry_view: 0 <a7b> DW_AT_ranges : 0x40 00000040 000000001000040c 0000000010000414 00000040 0000000010000420 0000000010000420 (start == end) 00000040 0000000010000424 0000000010000428 00000040 0000000010000430 0000000010000434 00000040 0000000010000448 0000000010000450 00000040 <End of list> So the bar function has much more subranges and the entry_pc should make the test case actually test the interesting case. Therefore I would suggest to add the __builtin_expect again, what do you think? Thanks Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-17 23:52 ` Bernd Edlinger @ 2024-11-19 9:29 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-11-19 9:29 UTC (permalink / raw) To: Bernd Edlinger, Tom de Vries, gdb-patches Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > On 11/15/24 20:24, Andrew Burgess wrote: >> Tom de Vries <tdevries@suse.de> writes: > [...] >>> Regardless, LGTM. >>> >>> Approved-By: Tom de Vries <tdevries@suse.de> >> >> I pushed the version of the patch below. >> >> Thanks, >> Andrew >> > > Hmm Sorry Andrey, but I have noticed another difference in the patch > which unfortunately weakens it a bit. > I've overlooked it before, but my patch had a couple __builtin_expect > here in bar and main: > > inline __attribute__((always_inline)) int > bar (int val) > { > if (__builtin_expect(global == val, 0)) > return 1; > foo (1); > return 1; > } > > int > main (void) > { > if ((__builtin_expect(global, 0) && bar (1)) || bar (2)) > return 1; > return 0; > } > > but those are removed from your version, I have added them to make > the optimizer do some wrong decisions that what I wanted it to do > to get the bar function have an entry pc not at the first subrange. > Without them the test case still works in x86_64 but is weaker, > because the bar function has more subranges. > > With the original test case x86_64 has this debug info: > > <2><6e>: Abbrev Number: 6 (DW_TAG_inlined_subroutine) > <6f> DW_AT_abstract_origin: <0xd2> > <73> DW_AT_entry_pc : 0x1065 > <7b> DW_AT_GNU_entry_view: 0 > <7c> DW_AT_ranges : 0xc > > 0000000c 0000000000001048 (base address) > 00000015 0000000000001048 000000000000104e > 00000018 0000000000001055 000000000000105f > 0000001b 0000000000001065 000000000000106c > 0000001e <End of list> > > so three separate ranges, and the entry_pc is the > third one, while the test case now has only two subranges: > > <2><6e>: Abbrev Number: 6 (DW_TAG_inlined_subroutine) > <6f> DW_AT_abstract_origin: <0xd2> > <73> DW_AT_entry_pc : 0x1050 > <7b> DW_AT_GNU_entry_view: 0 > <7c> DW_AT_ranges : 0xc > > 0000000c 0000000000001048 (base address) > 00000015 0000000000001048 000000000000104e > 00000018 0000000000001050 000000000000105f > 0000001b <End of list> > > Yet the test case is still functional with x86_64. > > But with powerpc64-unknown-linux-gcc-9 I have seen that the > the bar function does no longer have the entry_pc at the lowest > address: > > This is the code from the current version of the test case: > > 0000000010000400 <.main>: > 10000400: 60 00 00 00 nop > 10000404: 81 22 81 68 lwz r9,-32408(r2) > 10000408: 2c 09 00 00 cmpwi r9,0 > 1000040c: 41 82 00 38 beq 10000444 <.main+0x44> > 10000410: 81 22 81 68 lwz r9,-32408(r2) > 10000414: 2c 09 00 01 cmpwi r9,1 > 10000418: 41 82 00 3c beq 10000454 <.main+0x54> > 1000041c: 7c 08 02 a6 mflr r0 > 10000420: 38 60 00 01 li r3,1 > 10000424: f8 01 00 10 std r0,16(r1) > 10000428: f8 21 ff 91 stdu r1,-112(r1) > 1000042c: 48 00 02 35 bl 10000660 <.foo> > > <a6e> DW_AT_abstract_origin: <0xad7> > <a72> DW_AT_entry_pc : 0x10000410 > <a7a> DW_AT_GNU_entry_view: 0 > <a7b> DW_AT_ranges : 0x40 > > 00000040 0000000010000410 000000001000041c > 00000040 0000000010000420 0000000010000424 > 00000040 000000001000042c 0000000010000430 > 00000040 <End of list> > > so this test cannot fail even before the fix. > > While with the original version the code was: > > 0000000010000400 <.main>: > 10000400: 60 00 00 00 nop > 10000404: 81 22 81 68 lwz r9,-32408(r2) > 10000408: 2c 09 00 00 cmpwi r9,0 > 1000040c: 60 00 00 00 nop > 10000410: 81 22 81 68 lwz r9,-32408(r2) > 10000414: 40 82 00 34 bne 10000448 <.main+0x48> > 10000418: 2c 09 00 02 cmpwi r9,2 > 1000041c: 41 82 00 34 beq 10000450 <.main+0x50> > 10000420: 7c 08 02 a6 mflr r0 > 10000424: 38 60 00 01 li r3,1 > 10000428: f8 01 00 10 std r0,16(r1) > 1000042c: f8 21 ff 91 stdu r1,-112(r1) > 10000430: 48 00 02 21 bl 10000650 <.foo> > > 10000448: 2c 09 00 01 cmpwi r9,1 > 1000044c: 40 82 ff d4 bne 10000420 <.main+0x20> > 10000450: 38 60 00 01 li r3,1 > 10000454: 4e 80 00 20 blr > > <2><a6d>: Abbrev Number: 6 (DW_TAG_inlined_subroutine) > <a6e> DW_AT_abstract_origin: <0xad7> > <a72> DW_AT_entry_pc : 0x10000448 > <a7a> DW_AT_GNU_entry_view: 0 > <a7b> DW_AT_ranges : 0x40 > > 00000040 000000001000040c 0000000010000414 > 00000040 0000000010000420 0000000010000420 (start == end) > 00000040 0000000010000424 0000000010000428 > 00000040 0000000010000430 0000000010000434 > 00000040 0000000010000448 0000000010000450 > 00000040 <End of list> > > So the bar function has much more subranges and the entry_pc > should make the test case actually test the interesting case. > > Therefore I would suggest to add the __builtin_expect again, > what do you think? Sounds reasonable. I'd certainly have no objections to adding these back in. I would suggest attaching a comment that explains why they are there. I've added an item to my todo list, and will get around to this soon. You're welcome to post a patch sooner if you're keen! I'm not too worried though, the compiler's optimiser could change at any time, which is why, for optimisation focused patches, I try to also include DWARF assembler tests too, so we should be covered for all targets that support gas. Also, with our maintainer hats on, we know that this feature is all common code (range parsing and block handling), so, as long as one of the big targets covers it, then regressions are likely to show up pretty quick. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 14:00 ` Andrew Burgess 2024-11-15 14:30 ` Tom de Vries @ 2024-11-15 17:00 ` Bernd Edlinger 1 sibling, 0 replies; 41+ messages in thread From: Bernd Edlinger @ 2024-11-15 17:00 UTC (permalink / raw) To: Andrew Burgess, Tom de Vries, gdb-patches On 11/15/24 15:00, Andrew Burgess wrote: > Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > >> On 11/15/24 09:50, Tom de Vries wrote: >>> Hi Andrew, >>> >>> it does fix all the regressions in ada test-cases. >>> >>> There's one problem left: a test-case introduced by the commit (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): >>> ... >>> FAIL: gdb.opt/inline-entry.exp: continue to foo >>> FAIL: gdb.opt/inline-entry.exp: continue until exit >>> ... >>> >> >> I think that is to be expected, the test case depends on the statement frontiers >> feature which initially introduced in gcc-8 by Alexandre Oliva. >> >> The -gstatement-frontiers enables things like non-statement line number without >> that it wont work. >> >> my patch had this guard to prevent such fallout: >> >> if { [test_compiler_info gcc*] && ![supports_statement_frontiers] } { >> untested "this test needs gcc with statement frontiers" >> return -1 >> } >> >> global srcfile testfile >> >> set options {debug nowarnings optimize=-O2} >> if { [supports_statement_frontiers] } { >> lappend options additional_flags=-gstatement-frontiers >> } > > Sorry Bernd, I missed this reply before posting a few moments ago. > > Below is a patch based on your feedback above. With this in place the > test is skipped for gcc 7.x (and older), but continues to run for Clang. > > Let me know what you think, > Yeah, looks good, thanks. Bernd. ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv6] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-11-15 8:50 ` Tom de Vries 2024-11-15 10:53 ` Bernd Edlinger @ 2024-11-15 13:45 ` Andrew Burgess 1 sibling, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-11-15 13:45 UTC (permalink / raw) To: Tom de Vries, gdb-patches; +Cc: Bernd Edlinger Tom de Vries <tdevries@suse.de> writes: > On 11/14/24 20:33, Andrew Burgess wrote: >> Tom de Vries <tdevries@suse.de> writes: >> >>> On 11/13/24 17:59, Andrew Burgess wrote: >>>> Andrew Burgess <aburgess@redhat.com> writes: >>>> >>>>> Andrew Burgess <aburgess@redhat.com> writes: >>>>> >>>>>> The entry PC for a DIE, e.g. an inline function, might not be the base >>>>>> address of the DIE. Currently though, in block::entry_pc(), GDB >>>>>> always returns the base address (low-pc or the first address of the >>>>>> first range) as the entry PC. >>>>>> >>>>>> This commit extends the block class to carry the entry PC as a >>>>>> separate member variable. Then the DWARF reader is extended to read >>>>>> and set the entry PC for the block. Now in block::entry_pc(), if the >>>>>> entry PC has been set, this is the value returned. >>>>>> >>>>>> If the entry-pc has not been set to a specific value then the old >>>>>> behaviour of block::entry_pc() remains, GDB will use the block's base >>>>>> address. Not every DIE will set the entry-pc, but GDB still needs to >>>>>> have an entry-pc for every block, so the existing logic supplies the >>>>>> entry-pc for any block where the entry-pc was not set. >>>>>> >>>>>> The DWARF-5 spec for reading the entry PC is a super-set of the spec >>>>>> as found in DWARF-4. For example, if there is no DW_AT_entry_pc then >>>>>> DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base >>>>>> address, which is DW_AT_low_pc or the first address in the first range >>>>>> specified by DW_AT_ranges if there is no DW_AT_low_pc. >>>>>> >>>>>> I have taken the approach of just implementing the DWARF-5 spec for >>>>>> everyone. There doesn't seem to be any benefit to deliberately >>>>>> ignoring a ranges based entry PC value for DWARF-4. If some naughty >>>>>> compiler has emitted that, then lets use it. >>>>>> >>>>>> Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 >>>>>> allows an address or a constant, where the constant is an offset from >>>>>> the base address. I allow both approaches for all DWARF versions. >>>>>> There doesn't seem to be any downsides to this approach. >>>>>> >>>>>> I ran into an issue when testing this patch where GCC would have the >>>>>> DW_AT_entry_pc point to an empty range. When GDB parses the ranges >>>>>> any empty ranges are ignored. As a consequence, the entry-pc appears >>>>>> to be outside the address range of a block. >>>>>> >>>>>> The empty range problem is certainly something that we can, and should >>>>>> address, but that is not the focus of this patch, so for now I'm >>>>>> ignoring that problem. What I have done is added a check: if the >>>>>> DW_AT_entry_pc is outside the range of a block then the entry-pc is >>>>>> ignored, GDB will then fall-back to its default algorithm for >>>>>> computing the entry-pc. >>>>>> >>>>>> If/when in the future we address the empty range problem, these >>>>>> DW_AT_entry_pc attributes will suddenly become valid and GDB will >>>>>> start using them. Until then, GDB continues to operate as it always >>>>>> has. >>>>>> >>>>>> An early version of this patch stored the entry-pc within the block >>>>>> like this: >>>>>> >>>>>> std::optional<CORE_ADDR> m_entry_pc; >>>>>> >>>>>> However, a concern was raised that this, on a 64-bit host, effectively >>>>>> increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, >>>>>> and 8-bytes for the std::optional's bool plus padding). >>>>>> >>>>>> If we remove the std::optional part and just use a CORE_ADDR then we >>>>>> need to have a "special" address to indicate if m_entry_pc is in use >>>>>> or not. I don't really like using special addresses; different >>>>>> targets can access different address ranges, even zero is a valid >>>>>> address on some targets. >>>>>> >>>>>> However, Bernd Edlinger suggested storing the entry-pc as an offset, >>>>>> and I think that will resolve my concerns. So, we store the entry-pc >>>>>> as a signed offset from the block's base address (the first address of >>>>>> the first range, or the start() address value if there are now >>>>>> ranges). Remember, ranges can be out of order, in which case the >>>>>> first address of the first range might be greater than the entry-pc. >>>>>> >>>>>> When GDB needs to read the entry-pc we can add the offset onto the >>>>>> blocks base address to recalculate it. >>>>>> >>>>>> With this done, on a 64-bit host, block only needs to increase by >>>>>> 8-bytes. >>>>>> >>>>>> The inline-entry.exp test was originally contributed by Bernd here: >>>>>> >>>>>> https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com >>>>>> >>>>>> though I have made some edits, making more use of lib/gdb.exp >>>>>> functions, making the gdb_test output patterns a little tighter, and >>>>>> updating the test to run with Clang. I also moved the test to >>>>>> gdb.opt/ as that seemed like a better home for it. >>>>>> >>>>>> Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de> >>>>> >>>>> I've pushed the patch attached below. The only changes are some minor >>>>> test tweaks in order to get more of the 'check-all-boards' passing. >>>>> There are still some failures on e.g. gold and fission based boards, but >>>>> I think these might be due to issues with the board file. I'll post a >>>>> patch for those soon. >>>> >>>> I'm aware that this patch has caused a regression on the buildbot[1]. I'm >>>> investigating this but it will be at least tomorrow before I have fix. >>>> >>> >>> In case this helps, I see the same assert in a large amount of ada >>> test-cases. >>> >>> I reproduced the first one using gdb.ada/access_to_packed_array.exp, >>> with system gcc 7 and then with gcc 8. With gcc 9 - 14, the assert no >>> longer reproduces. >> >> Thanks Tom, >> >> I've posted a proposed fix here: >> >> https://inbox.sourceware.org/gdb-patches/23102df35a659d9ef4725d2d3822f08f2790e52d.1731612468.git.aburgess@redhat.com >> >> I would be really grateful if you could give this a try and let me know >> if this fixes all the problems you are seeing. >> > > Hi Andrew, > > it does fix all the regressions in ada test-cases. > > There's one problem left: a test-case introduced by the commit > (gdb.opt/inline-entry.exp) fails with gcc 7 (not with gcc 8 and higher): > ... > FAIL: gdb.opt/inline-entry.exp: continue to foo > FAIL: gdb.opt/inline-entry.exp: continue until exit > ... > > Gdb.log attached. > > The test-case sets a breakpoint at foo and bar, and expects that the > program: > - stops at bar, > - stops at foo, and > - exits. > > This is what I see when using -O0. > > What happens instead (with -O2) is that the program: > - stops at bar, > - stops at bar again, > - stops at foo, and > - exits. > > For context, relevant code: > ... > inline __attribute__((always_inline)) int > bar (int val) > { > if (global == val) > return 1; > foo (1); > return 1; > } > > int > main (void) > { > if ((global && bar (1)) || bar (2)) > return 0; > return 1; > } > ... > > From what I can tell, what happened in source terms is that the > compiler inlined the call "bar (1)", and then moved the first > instruction of bar to before the &&: > > In disassembly: > ... > 00000000004003c0 <main>: > 4003c0: mov 0x1c5e(%rip),%eax # Load global to reg > 4003c6: test %eax,%eax # If global is zero, set ZF > 4003c8: mov 0x1c56(%rip),%eax # Local global to reg (1st of bar) > 4003ce: je 4003d8 <main+0x18> # If the ZF is set, skip "bar (1)" > ... > > At first glance, gdb is behaving ok. I agree with your diagnosis. I'm pretty sure this is a bug in older versions of gcc, the DW_AT_entry_pc (for older gcc) points to the very first instruction of bar, even though that instruction has moved before the &&. Newer (8+) versions of gcc correctly set DW_AT_entry_pc to the first instruction after the &&, in your assembler above this is the first instruction after the 'je' instruction. GDB is doing the best is can given the DWARF available. I propose that we just skip this test for older versions of gcc. There's a patch below for this. If you agree then I'll get this checked in. The other patch I'll check in once it's had time for a fuller review. Thanks, Andrew --- commit b6ad91d9baeeae0fb5275fce2de0cb917ae3ab4f Author: Andrew Burgess <aburgess@redhat.com> Date: Fri Nov 15 13:07:09 2024 +0000 gdb/testsuite: skip gdb.opt/inline-entry.exp for gcc 7 and older It was pointed out that the recently added gdb.opt/inline-entry.exp test would fail when run using gcc 7 and earlier, on an x86-64 target. It would appear that these older versions of gcc would emit an incorrect DW_AT_entry_pc value which is causing the failure. Here is the interesting source line from inline-entry.c: if ((global && bar (1)) || bar (2)) And here's some of the relevant disassembly output: Dump of assembler code for function main: 0x401020 <+0>: mov 0x3006(%rip),%eax (1) 0x401026 <+6>: test %eax,%eax (2) 0x401028 <+8>: mov 0x2ffe(%rip),%eax (3) 0x40102e <+14>: je 0x401038 <main+24> (4) 0x401030 <+16>: sub $0x1,%eax (5) 0x401033 <+19>: jne 0x40103d <main+29> (6) Lines (1), (2), and (4) represent the check of 'global'. However, line (3) is actually the first instruction for 'bar' which has been inlined. Lines (5) and (6) are also part of the first inlined 'bar' function. If the check of 'global' returns false then the first call to 'bar' should never happen, this is accomplished by the branch at (4) being taken. For gcc 8+, gcc generates a DW_AT_entry_pc with the value 0x401030, this is where GDB places a breakpoint for 'bar', and this address is after the branch at line (4), and so, if the call to 'bar' never happens, the breakpoint is never hit. For gcc 7 and older, gcc generates a DW_AT_entry_pc with the value 0x401028, which also happens to be the first address associated with the inline 'bar' function. Unfortunately, this address is also before the check of 'global' has completed, this means that GDB hits the 'bar' breakpoint before the inferior has decided if 'bar' should actually be called or not. I don't think there's really much GDB can do in this case, the debug information seems to be misleading. I propose that we skip this test if we are using gcc, and the version is 7 or older. Newer versions of gcc, or Clang, all seem to handle this test fine. diff --git a/gdb/testsuite/gdb.opt/inline-entry.exp b/gdb/testsuite/gdb.opt/inline-entry.exp index 6f87ea3a00d..4f82fa6ed1c 100644 --- a/gdb/testsuite/gdb.opt/inline-entry.exp +++ b/gdb/testsuite/gdb.opt/inline-entry.exp @@ -21,6 +21,9 @@ # Testing with Clang 9.0.1 and 15.0.2 seemed to indicate that the # Clang generated code didn't depend on the entry_pc being parsed. +# Older versions of GCC emit an incorrect DW_AT_entry_pc. +require {expr [gcc_major_version] >= 8 || ![is_c_compiler_gcc]} + standard_testfile set options {debug optimize=-O2} ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv3] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-28 13:45 ` [PATCHv3] " Andrew Burgess 2024-10-29 14:49 ` [PATCHv4] " Andrew Burgess @ 2024-10-29 15:29 ` Sam James 2024-10-31 9:48 ` Andrew Burgess 1 sibling, 1 reply; 41+ messages in thread From: Sam James @ 2024-10-29 15:29 UTC (permalink / raw) To: Andrew Burgess; +Cc: gdb-patches, Bernd Edlinger Andrew Burgess <aburgess@redhat.com> writes: > In v3: > > - Entry PC values are now clamped to the block's start/end range. > This was causing failures in the self-tests when GDB was compiled > with GCC using default optimisation, as GCC emits an (invalid?) > entry-pc which is outside the blocks start/end range. Can you file a GCC bug and link it here when you get a chance? Thanks. > [...] thanks, sam ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCHv3] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-29 15:29 ` [PATCHv3] " Sam James @ 2024-10-31 9:48 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-31 9:48 UTC (permalink / raw) To: Sam James; +Cc: gdb-patches, Bernd Edlinger Sam James <sam@gentoo.org> writes: > Andrew Burgess <aburgess@redhat.com> writes: > >> In v3: >> >> - Entry PC values are now clamped to the block's start/end range. >> This was causing failures in the self-tests when GDB was compiled >> with GCC using default optimisation, as GCC emits an (invalid?) >> entry-pc which is outside the blocks start/end range. > > Can you file a GCC bug and link it here when you get a chance? Thanks. I believe Issue #1 in this bug report is an example of an entry_pc not within the blocks range: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94474 Thanks, Andrew > >> [...] > > thanks, > sam ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-16 15:47 [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines Andrew Burgess 2024-10-17 20:03 ` Tom Tromey 2024-10-18 13:53 ` [PATCHv2] " Andrew Burgess @ 2024-10-18 14:24 ` Bernd Edlinger 2024-10-28 13:26 ` Andrew Burgess 2 siblings, 1 reply; 41+ messages in thread From: Bernd Edlinger @ 2024-10-18 14:24 UTC (permalink / raw) To: Andrew Burgess, gdb-patches; +Cc: Tom Tromey Hi Andrew, thanks for your patch, I have not yet looked into it, I just wanted to say that I don't remember having seen any entry_pc for non-inline functions, have you been able to find such functions, I am just curious because for non-inline functions I thought the break-points are just on the function name or so. Is it theoretically possible to have a conflict with the entry_pc? Just one thing about the test case, I think you should know, because at the same time I was also working on a riscv toolchain, where I was putting everything together, and tested this patch together with lots of riscv-patches as you know, and surprise, I was seeing the test failing on the riscv-simulator, and was already considering to weaken the test case in a way, to make it somehow pass on the riscv simulator, but then I realized that the entry_pc was just plain wrong, and so I decided against changing the test case, and instead fixed the gcc debug info, as it turned out to be always just bogus or even completely missing on this architecture. See: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=76c29548b3de8bd06c3eef6084c19696019a7a2f So that means, it is quite possible that a debug info can be wrong, and in that case it might be better to find one of these cases, look if the debug improves with your patch or if the current behavior to ignore it is better. When I think about this entry_pc inside an inline sub-range, it is a bit funny, as if it was inside a loop or something, and if that is the case, it could mean the break-point will be hit in each iteration, but having such a test case would be nice, because, it would make the decision much more easy, what needs to be fixed, gdb or gcc. It could as well be the gcc ;-) you know... Thanks Bernd. On 10/16/24 17:47, Andrew Burgess wrote: > Bernd, > > I'd like to propose this as an alternative to your existing entry_pc > patch[1]. I've included the tests from your original proposal here, > and I've tested with gcc's ranging from 8.4 to 14.2. > > Instead of permuting the range order within the block to bring the > entry pc to the front I instead store the entry pc within the block > object itself. > > I collect the entry-pc in all cases, not just for inline subroutines, > and I've added support for the constant form (introduced in DWARF-5). > > I've moved the test into gdb.opt as it's an optimised code test, and > I've added a bunch of tests using the DWARF assembler into > gdb.dwarf2/, which should cover most (all) of the supported ways in > which the entry pc can be specified in GDB right now. > > I'm continuing to look at the other parts of your optimised code debug > patch series. > > I'd love to hear your (or anyone else's) thoughts. > > Thanks, > Andrew > > [1] https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com > ^ permalink raw reply [flat|nested] 41+ messages in thread
* Re: [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines 2024-10-18 14:24 ` [PATCH] " Bernd Edlinger @ 2024-10-28 13:26 ` Andrew Burgess 0 siblings, 0 replies; 41+ messages in thread From: Andrew Burgess @ 2024-10-28 13:26 UTC (permalink / raw) To: Bernd Edlinger, gdb-patches; +Cc: Tom Tromey Bernd Edlinger <bernd.edlinger@hotmail.de> writes: > Hi Andrew, > > thanks for your patch, I have not yet looked into it, > I just wanted to say that I don't remember having seen > any entry_pc for non-inline functions, have you been able to > find such functions, I've not seen an DW_AT_entry_pc for anything other than an inlined subroutine. > I am just curious because for non-inline > functions I thought the break-points are just on the function name > or so. Well something like `break main` is going to find the symbol for `main` then apply gdbarch_skip_prologue to that. I don't think that the entry_pc plays any particular role. We do end up checking the entry-pc for the block of `main` though during ifunc detection. > Is it theoretically possible to have a conflict with the > entry_pc? I'm not sure what you mean by "conflict". I'm guess you maybe mean that could GDB handling the entry-pc on non-inline-subroutines cause issues that would not exist if we just ignored the entry-pc. If that is the question, then my answer is, sure, I guess. We can possibly convince ourselves that GDB is parsing and storing the attribute correctly. So the problem would be that the compilers intended meaning for entry-pc differs from GDB's interpretation of the attribute. But I'm not sure how we can predict such bad interactions. So then the question becomes, is it better to not parse the attribute for things that are not inline-subroutines? I clearly think it's better to handle it. But there's an equally good argument that we should ignore it. I think the important thing is that _either_ case might mean GDB does the wrong thing. > > Just one thing about the test case, I think you should know, because > at the same time I was also working on a riscv toolchain, where > I was putting everything together, and tested this patch together > with lots of riscv-patches as you know, and surprise, I was seeing > the test failing on the riscv-simulator, and was already considering > to weaken the test case in a way, to make it somehow pass on the > riscv simulator, but then I realized that the entry_pc was > just plain wrong, and so I decided against changing the test case, > and instead fixed the gcc debug info, as it turned out to > be always just bogus or even completely missing on this architecture. > > See: https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=76c29548b3de8bd06c3eef6084c19696019a7a2f I didn't fully follow this explanation, but it sounds like you tested this patch, found that risc-v gcc was emitting broken entry-pc, and fixed it. Which sounds great. > So that means, it is quite possible that a debug info can be > wrong, and in that case it might be better to find one of these > cases, look if the debug improves with your patch or if the current > behavior to ignore it is better. I don't think I followed what you're asking for here. It sounds like you'd like me to find an example where DW_AT_entry_pc is emitted for something that is not a DW_TAG_inlined_subroutine, and the value emitted for the attribute is clearly wrong due to a compiler bug. Then you'd like me to see if ignoring the incorrect value is better than consuming the incorrect value... ... like I said, I don't think I've understood you, because that all sounds a little contrived. In most (I'd guess) cases, GDB would be better off ignoring an incorrect attribute, whatever attribute that is. > When I think about this entry_pc inside an inline sub-range, it is > a bit funny, as if it was inside a loop or something, and if that > is the case, it could mean the break-point will be hit in each > iteration, but having such a test case would be nice, because, > it would make the decision much more easy, what needs to be > fixed, gdb or gcc. It could as well be the gcc ;-) you know... The patch as it currently stands can easily be changed to only look for DW_AT_entry_pc on specific TAG types. I haven't done that as all of GDB's blocks support block::entry_pc() and are expected to give a sane answer, so it seemed like tracking what was in the DWARF should make sense. It's then left to the DWARF producer to decide when adding the entry-pc attribute makes sense, but if we find some producers are regularly creating bogus information for some TAG types, we can easily trim these, either with an producer specific check, or with a general check, whatever seems best at the time. Since posting V2 I've already run into a problem with GCC (which seems to be pretty bad with its range tracking), such that the entry-pc would in some case be outside the block start/end range, which was causing failures. I have a work around for this problem, but it's certainly possible that additional issues might crop up later on. Thanks, Andrew ^ permalink raw reply [flat|nested] 41+ messages in thread
end of thread, other threads:[~2024-11-19 9:30 UTC | newest] Thread overview: 41+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2024-10-16 15:47 [PATCH] gdb: fix handling of DW_AT_entry_pc of inlined subroutines Andrew Burgess 2024-10-17 20:03 ` Tom Tromey 2024-10-18 10:06 ` Andrew Burgess 2024-10-18 13:57 ` Andrew Burgess 2024-10-18 10:26 ` Gerlicher, Klaus 2024-10-18 13:55 ` Andrew Burgess 2024-10-18 13:53 ` [PATCHv2] " Andrew Burgess 2024-10-28 13:45 ` [PATCHv3] " Andrew Burgess 2024-10-29 14:49 ` [PATCHv4] " Andrew Burgess 2024-10-29 15:28 ` Bernd Edlinger 2024-10-31 10:57 ` Andrew Burgess 2024-10-31 14:01 ` Bernd Edlinger 2024-10-31 14:56 ` Andrew Burgess 2024-10-29 16:34 ` Bernd Edlinger 2024-10-31 10:59 ` Andrew Burgess 2024-10-31 15:00 ` [PATCHv5] " Andrew Burgess 2024-11-01 18:13 ` Tom Tromey 2024-11-01 20:27 ` Bernd Edlinger 2024-11-05 11:25 ` Andrew Burgess 2024-11-05 15:26 ` Bernd Edlinger 2024-11-05 16:52 ` Andrew Burgess 2024-11-05 19:57 ` Bernd Edlinger 2024-11-05 11:21 ` [PATCHv6] " Andrew Burgess 2024-11-13 13:49 ` Andrew Burgess 2024-11-13 16:59 ` Andrew Burgess 2024-11-14 9:20 ` Tom de Vries 2024-11-14 19:33 ` Andrew Burgess 2024-11-15 8:50 ` Tom de Vries 2024-11-15 10:53 ` Bernd Edlinger 2024-11-15 14:00 ` Andrew Burgess 2024-11-15 14:30 ` Tom de Vries 2024-11-15 16:46 ` Andrew Burgess 2024-11-15 19:24 ` Andrew Burgess 2024-11-17 23:52 ` Bernd Edlinger 2024-11-19 9:29 ` Andrew Burgess 2024-11-15 17:00 ` Bernd Edlinger 2024-11-15 13:45 ` Andrew Burgess 2024-10-29 15:29 ` [PATCHv3] " Sam James 2024-10-31 9:48 ` Andrew Burgess 2024-10-18 14:24 ` [PATCH] " Bernd Edlinger 2024-10-28 13:26 ` Andrew Burgess
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).