binutils-gdb/gdb/testsuite/gdb.dwarf2/dw2-inline-header-3.exp
Andrew Burgess 1313c56ef9 gdb: Preserve is-stmt lines when switch between files
After the is-stmt support commit:

  commit 8c95582da8
  Date:   Mon Dec 30 21:04:51 2019 +0000

      gdb: Add support for tracking the DWARF line table is-stmt field

A regression was observed where a breakpoint could no longer be placed
in some cases.

Consider a line table like this:

  File 1: test.c
  File 2: test.h

  | Addr | File | Line | Stmt |
  |------|------|------|------|
  | 1    | 1    | 16   | Y    |
  | 2    | 1    | 17   | Y    |
  | 3    | 2    | 21   | Y    |
  | 4    | 2    | 22   | Y    |
  | 4    | 1    | 18   | N    |
  | 5    | 2    | 23   | N    |
  | 6    | 1    | 24   | Y    |
  | 7    | 1    | END  | Y    |
  |------|------|------|------|

Before the is-stmt patch GDB would ignore any non-stmt lines, so GDB
built two line table structures:

  File 1                 File 2
  ------                 ------

  | Addr | Line |        | Addr | Line |
  |------|------|        |------|------|
  | 1    | 16   |        | 3    | 21   |
  | 2    | 17   |        | 4    | 22   |
  | 3    | END  |        | 6    | END  |
  | 6    | 24   |        |------|------|
  | 7    | END  |
  |------|------|

After the is-stmt patch GDB now records non-stmt lines, so the
generated line table structures look like this:

  File 1                   File 2
  ------                   ------

  | Addr | Line | Stmt |  | Addr | Line | Stmt |
  |------|------|------|  |------|------|------|
  | 1    | 16   | Y    |  | 3    | 21   | Y    |
  | 2    | 17   | Y    |  | 4    | 22   | Y    |
  | 3    | END  | Y    |  | 4    | END  | Y    |
  | 4    | 18   | N    |  | 5    | 23   | N    |
  | 5    | END  | Y    |  | 6    | END  | Y    |
  | 6    | 24   | Y    |  |------|------|------|
  | 7    | END  | Y    |
  |------|------|------|

The problem is that in 'File 2', end END marker at address 4 causes
the previous line table entry to be discarded, so we actually end up
with this:

  File 2
  ------

  | Addr | Line | Stmt |
  |------|------|------|
  | 3    | 21   | Y    |
  | 4    | END  | Y    |
  | 5    | 23   | N    |
  | 6    | END  | Y    |
  |------|------|------|

When a user tries to place a breakpoint in file 2 at line 22, this is
no longer possible.

The solution I propose here is that we ignore line table entries that
would trigger a change of file if:

  1. The new line being added is at the same address as the previous
  line, and

  2. We have previously seen an is-stmt line at the current address.

The result of this is that GDB switches file, and knows that some line
entry (or entries) are going to be discarded, prefer to keep is-stmt
lines and discard non-stmt lines.

After this commit the lines tables are now:

  File 1                   File 2
  ------                   ------

  | Addr | Line | Stmt |  | Addr | Line | Stmt |
  |------|------|------|  |------|------|------|
  | 1    | 16   | Y    |  | 3    | 21   | Y    |
  | 2    | 17   | Y    |  | 4    | 22   | Y    |
  | 3    | END  | Y    |  | 5    | 23   | N    |
  | 5    | END  | Y    |  | 6    | END  | Y    |
  | 6    | 24   | Y    |  |------|------|------|
  | 7    | END  | Y    |
  |------|------|------|

We've lost the non-stmt entry for file 1, line 18, but retained the
is-stmt entry for file 2, line 22.  The user can now place a
breakpoint at that location.

One problem that came from this commit was the test
gdb.cp/step-and-next-inline.exp, which broke in several places.  After
looking at this test again I think that in some cases this test was
only ever passing by pure luck.  The debug GCC is producing for this
test is pretty broken.  I raised this GCC bug:

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94474

for this and disabled one entire half of the test.  There are still
some cases in here that do pass, and if/when GCC is fixed it would be
great to enable this test again.

gdb/ChangeLog:

	* dwarf2/read.c (class lnp_state_machine) <m_last_address>: New
	member variable.
	<m_stmt_at_address>: New member variable.
	(lnp_state_machine::record_line): Don't record some lines, update
	tracking of is_stmt at the same address.
	(lnp_state_machine::lnp_state_machine): Initialise new member
	variables.

gdb/testsuite/ChangeLog:

	* gdb.cp/step-and-next-inline.exp (do_test): Skip all tests in the
	use_header case.
	* gdb.dwarf2/dw2-inline-header-1.exp: New file.
	* gdb.dwarf2/dw2-inline-header-2.exp: New file.
	* gdb.dwarf2/dw2-inline-header-3.exp: New file.
	* gdb.dwarf2/dw2-inline-header-lbls.c: New file.
	* gdb.dwarf2/dw2-inline-header.c: New file.
	* gdb.dwarf2/dw2-inline-header.h: New file.
2020-06-01 10:02:44 +01:00

194 lines
5.1 KiB
Plaintext

# Copyright 2020 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/>.
# Setup a line table where:
#
# | Addr | File | Line | Stmt | Inline |
# |------|------|------|------|--------|
# | 1 | 1 | 16 | Y | |
# | 2 | 1 | 17 | Y | |
# | 3 | 2 | 21 | Y | X |
# | 4 | 2 | 22 | Y | X |
# | 4 | 1 | 18 | N | |
# | 5 | 1 | 19 | N | |
# | 6 | 1 | 20 | Y | |
# | 7 | 1 | END | Y | |
# |------|------|------|------|--------|
#
# Break at file 2, line 22, then single instruction step forward. We
# should pass through line 19 and then encounter line 20.
#
# Currently we don't expect GDB to see file 1, line 18, as this is a
# non-stmt line in a different file at the same address as the
# previous is-stmt line.
load_lib dwarf.exp
# This test can only be run on targets which support DWARF-2 and use gas.
if {![dwarf2_support]} {
return 0
}
# The .c files use __attribute__.
if [get_compiler_info] {
return -1
}
if !$gcc_compiled {
return 0
}
standard_testfile dw2-inline-header-lbls.c dw2-inline-header.S \
dw2-inline-header.c dw2-inline-header.h
set build_options {nodebug optimize=-O1}
set asm_file [standard_output_file $srcfile2]
Dwarf::assemble $asm_file {
global srcdir subdir srcfile srcfile3 srcfile4
global build_options
declare_labels lines_label callee_subprog_label
get_func_info main $build_options
cu {} {
compile_unit {
{producer "gcc" }
{language @DW_LANG_C}
{name ${srcfile3}}
{low_pc 0 addr}
{stmt_list ${lines_label} DW_FORM_sec_offset}
} {
callee_subprog_label: subprogram {
{external 1 flag}
{name callee}
{inline 3 data1}
}
subprogram {
{external 1 flag}
{name main}
{low_pc $main_start addr}
{high_pc "$main_start + $main_len" addr}
} {
inlined_subroutine {
{abstract_origin %$callee_subprog_label}
{low_pc line_label_3 addr}
{high_pc line_label_5 addr}
{call_file 1 data1}
{call_line 18 data1}
}
}
}
}
lines {version 2 default_is_stmt 1} lines_label {
include_dir "${srcdir}/${subdir}"
file_name "$srcfile3" 1
file_name "$srcfile4" 1
program {
{DW_LNE_set_address line_label_1}
{DW_LNS_advance_line 15}
{DW_LNS_copy}
{DW_LNE_set_address line_label_2}
{DW_LNS_advance_line 1}
{DW_LNS_copy}
{DW_LNS_set_file 2}
{DW_LNE_set_address line_label_3}
{DW_LNS_advance_line 4}
{DW_LNS_copy}
{DW_LNE_set_address line_label_4}
{DW_LNS_advance_line 1}
{DW_LNS_copy}
{DW_LNS_advance_line -4}
{DW_LNS_set_file 1}
{DW_LNS_negate_stmt}
{DW_LNS_copy}
{DW_LNE_set_address line_label_5}
{DW_LNS_advance_line 1}
{DW_LNS_copy}
{DW_LNE_set_address line_label_6}
{DW_LNS_advance_line 1}
{DW_LNS_negate_stmt}
{DW_LNS_copy}
{DW_LNE_set_address line_label_7}
{DW_LNE_end_sequence}
}
}
}
if { [prepare_for_testing "failed to prepare" ${testfile} \
[list $srcfile $asm_file] $build_options] } {
return -1
}
if ![runto_main] {
return -1
}
# Delete all breakpoints so that the output of "info breakpoints"
# below will only contain a single breakpoint.
delete_breakpoints
# Place a breakpoint within the function in the header file.
gdb_breakpoint "${srcfile4}:22"
# Check that the breakpoint was placed where we expected. It should
# appear at the requested line. When the bug in GDB was present the
# breakpoint would be placed on one of the following lines instead.
gdb_test "info breakpoints" \
".* in callee at \[^\r\n\]+${srcfile4}:22\\y.*"
gdb_continue_to_breakpoint "${srcfile4}:22" \
".* ${srcfile4} : 22 .*"
# Now single instruction step forward. Eventually we should hit
# ${srcfile3}:20, but before we do we should hit the non-statement
# line ${srcfile3}:19.
#
# We don't know how many instructions we'll need to step, but 100
# should be enough for everyone (surely), and this stops us looping
# forever if something goes wrong.
set found_line_19 0
set found_line_20 0
set keep_going 1
for { set i 0 } { $i < 100 && $keep_going } { incr i } {
set keep_going 0
gdb_test_multiple "stepi" "stepi ${i}" {
-re "${srcfile3} : 19 .*${gdb_prompt} " {
set found_line_19 1
set keep_going 1
}
-re "${srcfile3} : 20 .*${gdb_prompt} " {
set found_line_20 1
}
-re "${srcfile4} : 22 .*${gdb_prompt} " {
# Not left line 22 yet.
set keep_going 1
}
}
}
gdb_assert { $found_line_19 && $found_line_20 } \
"found line 19 and 20"