This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [PATCH v2 8/8] Test case for functions with non-contiguous ranges


On 08/14/2018 01:14 AM, Kevin Buettner wrote:
> See comments in the new files for what this is about - I tried to
> explain it all there.
> 
> gdb/testsuite/ChangeLog:
>     
>     	* gdb.dwarf2/dw2-ranges-func.c: New file.
>     	* gdb.dwarf2/dw2-ranges-func.exp: New file.
> ---
>  gdb/testsuite/gdb.dwarf2/dw2-ranges-func.c   |  78 ++++++
>  gdb/testsuite/gdb.dwarf2/dw2-ranges-func.exp | 400 +++++++++++++++++++++++++++
>  2 files changed, 478 insertions(+)
> 
> diff --git a/gdb/testsuite/gdb.dwarf2/dw2-ranges-func.c b/gdb/testsuite/gdb.dwarf2/dw2-ranges-func.c
> new file mode 100644
> index 0000000..864803c
> --- /dev/null
> +++ b/gdb/testsuite/gdb.dwarf2/dw2-ranges-func.c
> @@ -0,0 +1,78 @@
> +/* Copyright 2018 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/>.  */
> +
> +/* The idea here is to, via use of the dwarf assembler, create a function
> +   which occupies two non-contiguous address ranges.
> +
> +   foo_low and foo will be combined into a single function foo with a
> +   function bar in between these two ranges.
> +
> +   This test case was motivated by a bug in which a function which
> +   occupied two non-contiguous address ranges was calling another
> +   function which resides in between these ranges.  So we end up with
> +   a situation in which the low/start address of our constructed foo
> +   (in this case) will be less than any of the addresses in bar, but
> +   the high/end address of foo will be greater than any of bar's
> +   addresses.
> +
> +   This situation was causing a problem in the caching code of
> +   find_pc_partial_function:  When the low and high addresses of foo
> +   are placed in the cache, the simple check that was used to see if
> +   the cache was applicable would (incorrectly) succeed when presented
> +   with an address in bar.  I.e. an address in bar presented as an
> +   input to find_pc_partial_function could produce the answer "this
> +   address belongs to foo".  */
> +
> +volatile int e = 0;
> +
> +void
> +baz (void)
> +{
> +  asm ("baz_label: .globl baz_label");
> +}						/* baz end */
> +
> +void
> +foo_low (void)
> +{						/* foo_low prologue */
> +  asm ("foo_low_label: .globl foo_low_label");
> +  baz ();					/* foo_low baz call */
> +  asm ("foo_low_label2: .globl foo_low_label2");
> +}						/* foo_low end */
> +
> +void
> +bar (void)
> +{
> +  asm ("bar_label: .globl bar_label");
> +}						/* bar end */
> +
> +void
> +foo (void)
> +{						/* foo prologue */
> +  asm ("foo_label: .globl foo_label");
> +  bar ();					/* foo bar call */
> +  asm ("foo_label2: .globl foo_label2");
> +  if (e) foo_low ();				/* foo foo_low call */
> +  asm ("foo_label3: .globl foo_label3");
> +}						/* foo end */
> +
> +int
> +main (void)
> +{						/* main prologue */
> +  asm ("main_label: .globl main_label");
> +  foo ();					/* main foo call */
> +  asm ("main_label2: .globl main_label2");
> +  return 0;					/* main return */
> +}						/* main end */
> +
> diff --git a/gdb/testsuite/gdb.dwarf2/dw2-ranges-func.exp b/gdb/testsuite/gdb.dwarf2/dw2-ranges-func.exp
> new file mode 100644
> index 0000000..227c753
> --- /dev/null
> +++ b/gdb/testsuite/gdb.dwarf2/dw2-ranges-func.exp
> @@ -0,0 +1,400 @@
> +# Copyright 2018 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/>.
> +load_lib dwarf.exp
> +
> +# Test DW_AT_ranges in the context of a subprogram scope.
> +
> +# This test can only be run on targets which support DWARF-2 and use gas.
> +if {![dwarf2_support]} {
> +    unsupported "dwarf2 support required for this test"
> +    return 0
> +}
> +
> +if [get_compiler_info] {
> +    return -1
> +}
> +if !$gcc_compiled {
> +    unsupported "gcc required for this test"
> +    return 0
> +}
> +
> +standard_testfile dw2-ranges-func.c dw2-ranges-func-dw.S
> +
> +# We need to know the size of integer and address types in order to
> +# write some of the debugging info we'd like to generate.
> +#
> +# For that, we ask GDB by debugging our test program.  Any program
> +# would do, but since we already have it specifically for this
> +# testcase, might as well use that.
> +
> +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
> +    return -1
> +}
> +
> +set asm_file [standard_output_file $srcfile2]
> +Dwarf::assemble $asm_file {
> +    global srcdir subdir srcfile srcfile2
> +    declare_labels integer_label volatile_label func_ranges_label cu_ranges_label L
> +    set int_size [get_sizeof "int" 4]
> +
> +    # Find start address and length for our functions.
> +    lassign [function_range main [list ${srcdir}/${subdir}/$srcfile]] \
> +	main_start main_len
> +    lassign [function_range foo [list ${srcdir}/${subdir}/$srcfile]] \
> +	foo_start foo_len
> +    set foo_end {$foo_start + $foo_len}
> +    lassign [function_range foo_low [list ${srcdir}/${subdir}/$srcfile]] \
> +	foo_low_start foo_low_len
> +    set foo_low_end {$foo_low_start + $foo_low_len}
> +    lassign [function_range bar [list ${srcdir}/${subdir}/$srcfile]] \
> +	bar_start bar_len
> +    lassign [function_range baz [list ${srcdir}/${subdir}/$srcfile]] \
> +	baz_start baz_len
> +
> +    set e_var [gdb_target_symbol e]
> +
> +    cu {} {
> +	compile_unit {
> +	    {language @DW_LANG_C}
> +	    {name dw-ranges-func.c}
> +	    {stmt_list $L DW_FORM_sec_offset}
> +	    {low_pc 0 addr}
> +	    {ranges ${cu_ranges_label} DW_FORM_sec_offset}
> +	} {
> +            integer_label: DW_TAG_base_type {
> +                {DW_AT_byte_size $int_size DW_FORM_sdata}
> +                {DW_AT_encoding  @DW_ATE_signed}
> +                {DW_AT_name      integer}
> +            }

Tab vs spaces mixup here.

> +	    volatile_label: DW_TAG_volatile_type {
> +		{type :$integer_label}
> +	    }
> +	    DW_TAG_variable {
> +		{name e}
> +		{external 1 flag}
> +		{type :$volatile_label}
> +		{location {addr $e_var} SPECIAL_expr}
> +	    }
> +	    subprogram {
> +		{external 1 flag}
> +		{name main}
> +		{DW_AT_type :$integer_label}
> +		{low_pc $main_start addr}
> +		{high_pc $main_len DW_FORM_data4}
> +	    }
> +	    subprogram {
> +		{external 1 flag}
> +		{name foo}
> +		{ranges ${func_ranges_label} DW_FORM_sec_offset}
> +	    }
> +	    subprogram {
> +		{external 1 flag}
> +		{name bar}
> +		{low_pc $bar_start addr}
> +		{high_pc $bar_len DW_FORM_data4}
> +	    }
> +	    subprogram {
> +		{external 1 flag}
> +		{name baz}
> +		{low_pc $baz_start addr}
> +		{high_pc $baz_len DW_FORM_data4}
> +	    }
> +	}
> +    }
> +
> +    lines {version 2} L {
> +	include_dir "${srcdir}/${subdir}"
> +	file_name "$srcfile" 1
> +
> +	# Generate a line table program. I attempted to make it reasonably
> +	# accurate as it made debugging the test case easier.

Double space after ".".  

Nit: I'm not a big fan of using "I", "me", etc. in comments, it reads
a bit awkwardly personal to me.  If several people edit these
comments later on, who becomes the "I" then?

> +clean_restart ${testfile}
> +if ![runto_main] {
> +    return -1
> +}

Should move these "clean_restart / runto_main" calls into the
following with_test_prefix, in case the runto_main issues
an internal FAIL.

> +
> +with_test_prefix "step-test-2" {
> +    # Note that the RE used for the following test will fail when the
> +    # breakpoint has been set on multiple locations. E.g. "(2 locations)". 
> +    # This is intentional since that behavior is one of the bugs that
> +    # this test case tests for.
> +    gdb_test "break foo" \
> +	"Breakpoint.*at.* file .*$srcfile, line \\d+\\." \
> +	"break foo"
> +
> +    # Continue to foo.  Allow execution to stop either on the prologue
> +    # or on the call to bar since either behavior is acceptable though
> +    # the latter is preferred.
> +    set test "continue to foo"
> +    gdb_test_multiple "continue" $test {
> +	-re "Breakpoint \\d+, foo \\(\\).*foo prologue.*${gdb_prompt}" {
> +	    pass $test
> +	    gdb_test "step" \
> +		     "foo bar call .*" \
> +		     "step to call of bar after landing on prologue"
> +	}
> +	-re "Breakpoint \\d+, foo \\(\\).*foo bar call.*${gdb_prompt}" {
> +	    pass $test
> +	}
> +    }
> +
> +    gdb_test "step" \
> +	"bar \\(\\).*bar end.*" \
> +	"step into bar"
> +
> +    gdb_test "step" \
> +	"foo \\(\\).*foo foo_low call.*" \
> +	"step out of bar, back into foo"
> +}
> +
> +clean_restart ${testfile}
> +if ![runto_main] {
> +    return -1
> +}
> +
> +# Disassembly of foo should have multiple address ranges.
> +# Note: I couldn't get $hex to work in the gdb_test_sequence regex.

Did you try to use [list .....] instead of {.....} ?

> +gdb_test_sequence "disassemble foo" "" {
> +    "Dump of assembler code for function foo:"
> +    "Address range 0x[\\dabcdefABCDEF]+ to 0x[\\dabcdefABCDEF]+:"
> +    "   0x[\\dabcdefABCDEF]+ <\\+0>:"
> +    "Address range 0x[\\dabcdefABCDEF]+ to 0x[\\dabcdefABCDEF]+:"
> +    "   0x[\\dabcdefABCDEF]+ <(.+?)>:"
> +    "End of assembler dump\\." 
> +}
> +
> +set test "x/i foo_low"
> +gdb_test_multiple $test $test {
> +    -re "   ($hex) <foo.*?>.*${gdb_prompt}" {
> +        set foo_low_addr $expect_out(1,string)
> +	pass $test

Spaces vs tabs mixup, seemingly.

Also, this leaves "foo_low_addr" unset if the gdb_test_multiple
ever fails, leading to a TCL error when next foo_low_addr is
referenced.

The usual pattern around this is to default the variable,
something like:

set foo_low_addr -1
gdb_test_multiple $test $test {
    -re "   ($hex) <foo.*?>.*${gdb_prompt}" {
        set foo_low_addr $expect_out(1,string)
	pass $test
    }

> +    }
> +}
> +
> +set test "x/i foo"
> +gdb_test_multiple $test $test {
> +    -re "   ($hex) <foo.*?>.*${gdb_prompt}" {
> +        set foo_addr $expect_out(1,string)
> +	pass $test

Ditto, indentation and default.

> +    }
> +}
> +
> +gdb_assert {$foo_low_addr != $foo_addr} "foo and foo_low are at different addresses"
> +
> +# This more permissive RE for "break foo" will allow a breakpoint on
> +# multiple locations to PASS.  */
> +gdb_test "break foo" \
> +    "Breakpoint.*at.*" \
> +    "break foo (2nd time)"

Don't use tail parens for "(2nd time)":

  https://sourceware.org/gdb/wiki/GDBTestcaseCookbook#Do_not_use_.22tail_parentheses.22_on_test_messages

Use e.g., "break foo, 2nd time", or create a with_test_prefix
block, if it makes sense.

> +
> +gdb_test "break baz" \
> +    "Breakpoint.*at.* file .*$srcfile, line \\d+\\."
> +
> +gdb_test "continue" \
> +    "Breakpoint \\d+, foo \\(\\).*" \
> +    "Continue to foo"

Lowercase "continue".

> +
> +gdb_test_no_output "set variable e=1"
> +
> +# If GDB incorrectly places the foo breakpoint on multiple locations,
> +# then GDB will (incorrectly) stop in foo_low instead of in baz.
> +gdb_test "continue" \
> +    "Breakpoint \\d+, (?:$hex in )?baz \\(\\).*" \
> +    "Continue to baz"

Ditto.

> +
> +clean_restart ${testfile}
> +if ![runto_main] {
> +    return -1
> +}
> +
Thanks,
Pedro Alves


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]