[PATCH] Compute the function length instead of hard coding it

Pedro Alves palves@redhat.com
Thu Apr 24 14:04:00 GMT 2014


(Is there really no way to query GDB for the function's size
directly?  Seems like something that "info symbol main" should
be spit out.)

On 04/24/2014 01:43 PM, Yao Qi wrote:

> +set main_length ""
> +set test "disassemble main"
> +gdb_test_multiple $test $test {
> +    -re ".*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
> +	set main_length $expect_out(1,string)
> +	pass $test
> +    }
> +    -re ".*$gdb_prompt $" {
> +	fail $test
> +	# Bail out here, because we can't do the following tests if
> +	# $main_length is unknown.
> +	return -1
> +    }

Instead of this -re, best do

if { $main_length == "" } {
  return -1
}

after gdb_test_multiple, as $main_length is unknown on e.g., internal
error or timeout too.

> +}
> +# Calculate the size of the last instruction.  Single instruction
> +# shouldn't be longer than 10 bytes.

x86 has instructions longer than that, though it's not likely
we'll see them as last instruction.  You want to disassemble two
instructions.  So instead of having to hard code some number and
having to explain it in a comment, how about just asking GDB what
we want, with x/i ?  That is:

(gdb) x /2i main+72
   0x45d854 <main+72>:  retq
   0x45d855:    nopl   (%rax)

> +
> +set test "disassemble main+$main_length,+10"
> +gdb_test_multiple $test $test {
> +    -re ".*($hex) <main\\+$main_length>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
> +	set start $expect_out(1,string)
> +	set end $expect_out(2,string)
> +
> +	set main_length [expr $main_length + $end - $start]
> +	pass $test
> +    }
> +    -re ".*$gdb_prompt $" {
> +	fail $test
> +	# Bail out here, because we can't do the following tests if
> +	# $main_length is unknown.
> +	return -1

Same comment here as above.

-- 
Pedro Alves



More information about the Gdb-patches mailing list