mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-27 04:52:05 +08:00
7022349d5c
The fact that GDB defaults to assuming that functions return int, when it has no debug info for the function has been a recurring source of user confusion. Recently this came up on the errno pretty printer discussions. Shortly after, it came up again on IRC, with someone wondering why does getenv() in GDB return a negative int: (gdb) p getenv("PATH") $1 = -6185 This question (with s/getenv/random-other-C-runtime-function) is a FAQ on IRC. The reason for the above is: (gdb) p getenv $2 = {<text variable, no debug info>} 0x7ffff7751d80 <getenv> (gdb) ptype getenv type = int () ... which means that GDB truncated the 64-bit pointer that is actually returned from getent to 32-bit, and then sign-extended it: (gdb) p /x -6185 $6 = 0xffffe7d7 The workaround is to cast the function to the right type, like: (gdb) p ((char *(*) (const char *)) getenv) ("PATH") $3 = 0x7fffffffe7d7 "/usr/local/bin:/"... IMO, we should do better than this. I see the "assume-int" issue the same way I see printing bogus values for optimized-out variables instead of "<optimized out>" -- I'd much rather that the debugger tells me "I don't know" and tells me how to fix it than showing me bogus misleading results, making me go around tilting at windmills. If GDB prints a signed integer when you're expecting a pointer or aggregate, you at least have some sense that something is off, but consider the case of the function actually returning a 64-bit integer. For example, compile this without debug info: unsigned long long function () { return 0x7fffffffffffffff; } Currently, with pristine GDB, you get: (gdb) p function () $1 = -1 # incorrect (gdb) p /x function () $2 = 0xffffffff # incorrect maybe after spending a few hours debugging you suspect something is wrong with that -1, and do: (gdb) ptype function type = int () and maybe, just maybe, you realize that the function actually returns unsigned long long. And you try to fix it with: (gdb) p /x (unsigned long long) function () $3 = 0xffffffffffffffff # incorrect ... which still produces the wrong result, because GDB simply applied int to unsigned long long conversion. Meaning, it sign-extended the integer that it extracted from the return of the function, to 64-bits. and then maybe, after asking around on IRC, you realize you have to cast the function to a pointer of the right type, and call that. It won't be easy, but after a few missteps, you'll get to it: ..... (gdb) p /x ((unsigned long long(*) ()) function) () $666 = 0x7fffffffffffffff # finally! :-) So to improve on the user experience, this patch does the following (interrelated) things: - makes no-debug-info functions no longer default to "int" as return type. Instead, they're left with NULL/"<unknown return type>" return type. (gdb) ptype getenv type = <unknown return type> () - makes calling a function with unknown return type an error. (gdb) p getenv ("PATH") 'getenv' has unknown return type; cast the call to its declared return type - and then to make it easier to call the function, makes it possible to _only_ cast the return of the function to the right type, instead of having to cast the function to a function pointer: (gdb) p (char *) getenv ("PATH") # now Just Works $3 = 0x7fffffffe7d7 "/usr/local/bin:/"... (gdb) p ((char *(*) (const char *)) getenv) ("PATH") # continues working $4 = 0x7fffffffe7d7 "/usr/local/bin:/"... I.e., it makes GDB default the function's return type to the type of the cast, and the function's parameters to the type of the arguments passed down. After this patch, here's what you'll get for the "unsigned long long" example above: (gdb) p function () 'function' has unknown return type; cast the call to its declared return type (gdb) p /x (unsigned long long) function () $4 = 0x7fffffffffffffff # correct! Note that while with "print" GDB shows the name of the function that has the problem: (gdb) p getenv ("PATH") 'getenv' has unknown return type; cast the call to its declared return type which can by handy in more complicated expressions, "ptype" does not: (gdb) ptype getenv ("PATH") function has unknown return type; cast the call to its declared return type This will be fixed in the next patch. gdb/ChangeLog: 2017-09-04 Pedro Alves <palves@redhat.com> * ada-lang.c (ada_evaluate_subexp) <TYPE_CODE_FUNC>: Don't handle TYPE_GNU_IFUNC specially here. Throw error if return type is unknown. * ada-typeprint.c (print_func_type): Handle functions with unknown return type. * c-typeprint.c (c_type_print_base): Handle functions and methods with unknown return type. * compile/compile-c-symbols.c (convert_symbol_bmsym) <mst_text_gnu_ifunc>: Use nodebug_text_gnu_ifunc_symbol. * compile/compile-c-types.c: Include "objfiles.h". (convert_func): For functions with unknown return type, warn and default to int. * compile/compile-object-run.c (compile_object_run): Adjust call to call_function_by_hand_dummy. * elfread.c (elf_gnu_ifunc_resolve_addr): Adjust call to call_function_by_hand. * eval.c (evaluate_subexp_standard): Adjust calls to call_function_by_hand. Handle functions and methods with unknown return type. Pass expect_type to call_function_by_hand. * f-typeprint.c (f_type_print_base): Handle functions with unknown return type. * gcore.c (call_target_sbrk): Adjust call to call_function_by_hand. * gdbtypes.c (objfile_type): Leave nodebug text symbol with NULL return type instead of int. Make nodebug_text_gnu_ifunc_symbol be an integer address type instead of nodebug. * guile/scm-value.c (gdbscm_value_call): Adjust call to call_function_by_hand. * infcall.c (error_call_unknown_return_type): New function. (call_function_by_hand): New "default_return_type" parameter. Pass it down. (call_function_by_hand_dummy): New "default_return_type" parameter. Use it instead of defaulting to int. If there's no default and the return type is unknown, throw an error. If there's a default return type, and the called function has no debug info, then assume the function is prototyped. * infcall.h (call_function_by_hand, call_function_by_hand_dummy): New "default_return_type" parameter. (error_call_unknown_return_type): New declaration. * linux-fork.c (call_lseek): Cast return type of lseek. (inferior_call_waitpid, checkpoint_command): Adjust calls to call_function_by_hand. * linux-tdep.c (linux_infcall_mmap, linux_infcall_munmap): Adjust calls to call_function_by_hand. * m2-typeprint.c (m2_procedure): Handle functions with unknown return type. * objc-lang.c (lookup_objc_class, lookup_child_selector) (value_nsstring, print_object_command): Adjust calls to call_function_by_hand. * p-typeprint.c (pascal_type_print_varspec_prefix): Handle functions with unknown return type. (pascal_type_print_func_varspec_suffix): New function. (pascal_type_print_varspec_suffix) <TYPE_CODE_FUNC, TYPE_CODE_METHOD>: Use it. * python/py-value.c (valpy_call): Adjust call to call_function_by_hand. * rust-lang.c (rust_evaluate_funcall): Adjust call to call_function_by_hand. * valarith.c (value_x_binop, value_x_unop): Adjust calls to call_function_by_hand. * valops.c (value_allocate_space_in_inferior): Adjust call to call_function_by_hand. * typeprint.c (type_print_unknown_return_type): New function. * typeprint.h (type_print_unknown_return_type): New declaration. gdb/testsuite/ChangeLog: 2017-09-04 Pedro Alves <palves@redhat.com> * gdb.base/break-main-file-remove-fail.exp (test_remove_bp): Cast return type of munmap in infcall. * gdb.base/break-probes.exp: Cast return type of foo in infcall. * gdb.base/checkpoint.exp: Simplify using for loop. Cast return type of ftell in infcall. * gdb.base/dprintf-detach.exp (dprintf_detach_test): Cast return type of getpid in infcall. * gdb.base/infcall-exec.exp: Cast return type of execlp in infcall. * gdb.base/info-os.exp: Cast return type of getpid in infcall. Bail on failure to extract the pid. * gdb.base/nodebug.c: #include <stdint.h>. (multf, multf_noproto, mult, mult_noproto, add8, add8_noproto): New functions. * gdb.base/nodebug.exp (test_call_promotion): New procedure. Change expected output of print/whatis/ptype with functions with no debug info. Test all supported languages. Call test_call_promotion. * gdb.compile/compile.exp: Adjust expected output to expect warning. * gdb.threads/siginfo-threads.exp: Likewise.
183 lines
5.5 KiB
Plaintext
183 lines
5.5 KiB
Plaintext
# Copyright 2011-2017 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/>.
|
|
|
|
standard_testfile .c
|
|
|
|
# This test is Linux-only.
|
|
if ![istarget *-*-linux*] then {
|
|
unsupported "info-os.exp"
|
|
return -1
|
|
}
|
|
|
|
# Support for XML-output is needed to run this test.
|
|
if [gdb_skip_xml_test] then {
|
|
unsupported "info-os.exp"
|
|
return -1
|
|
}
|
|
|
|
# Compile test program.
|
|
if { [prepare_for_testing "failed to prepare" $testfile $srcfile {debug additional_flags=-lpthread}] } {
|
|
fail "cannot compile test program"
|
|
return -1
|
|
}
|
|
|
|
if ![runto_main] then {
|
|
fail "cannot run to main"
|
|
return -1
|
|
}
|
|
|
|
# Get PID of test program.
|
|
set inferior_pid ""
|
|
set test "get inferior process ID"
|
|
gdb_test_multiple "call (int) getpid()" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set inferior_pid $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
if {$inferior_pid == ""} {
|
|
untested "failed to get pid"
|
|
return
|
|
}
|
|
|
|
gdb_breakpoint ${srcfile}:[gdb_get_line_number "Set breakpoint here"]
|
|
gdb_continue_to_breakpoint "Set breakpoint here"
|
|
|
|
# Get keys and IDs of the IPC object instances.
|
|
set shmkey -1
|
|
set test "get shared memory key"
|
|
gdb_test_multiple "print shmkey" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set shmkey $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
set shmid -1
|
|
set test "get shared memory ID"
|
|
gdb_test_multiple "print shmid" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set shmid $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
set semkey -1
|
|
set test "get semaphore key"
|
|
gdb_test_multiple "print semkey" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set semkey $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
set semid -1
|
|
set test "get semaphore ID"
|
|
gdb_test_multiple "print semid" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set semid $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
set msgkey -1
|
|
set test "get message queue key"
|
|
gdb_test_multiple "print msgkey" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set msgkey $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
set msqid -1
|
|
set test "get message queue ID"
|
|
gdb_test_multiple "print msqid" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set msqid $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
# Get port number of test socket.
|
|
set port -1
|
|
set test "get socket port number"
|
|
gdb_test_multiple "print port" $test {
|
|
-re ".* = ($decimal).*$gdb_prompt $" {
|
|
set port $expect_out(1,string)
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
# Act like gdb_test but prevent: +ERROR: internal buffer is full.
|
|
|
|
proc expect_multiline { command expect test } {
|
|
global gdb_prompt
|
|
|
|
# Do not duplicate FAILs from gdb_test_multiple.
|
|
set pass 0
|
|
set fail 0
|
|
gdb_test_multiple $command $test {
|
|
-re "^$expect *\r\n" {
|
|
pass $test
|
|
set pass 1
|
|
exp_continue
|
|
}
|
|
-re "^$gdb_prompt $" {
|
|
if !$pass {
|
|
set fail 1
|
|
}
|
|
# Exit the loop.
|
|
}
|
|
-re "\r\n" {
|
|
# Drop the buffer.
|
|
exp_continue
|
|
}
|
|
}
|
|
if $fail {
|
|
fail $test
|
|
}
|
|
}
|
|
|
|
# Test output of the 'info os' commands against the expected results.
|
|
|
|
# pid user command cores
|
|
expect_multiline "info os processes" "$inferior_pid +\\S+ +\\S*info-os +\[0-9,\]+" "get process list"
|
|
|
|
# pgid leader pid command line
|
|
expect_multiline "info os procgroups" "$inferior_pid +info-os +$inferior_pid +\\S*info-os" "get process groups"
|
|
|
|
# pid command tid core
|
|
expect_multiline "info os threads" "$inferior_pid +info-os +\\d+ +\\d+" "get threads"
|
|
|
|
# pid command fd name
|
|
expect_multiline "info os files" "$inferior_pid +info-os +\\d+ +/dev/null" "get file descriptors"
|
|
|
|
# local address l-port remote addr r-port state user family protocol
|
|
expect_multiline "info os sockets" "0\\.0\\.0\\.0 +$port +0\\.0\\.0\\.0 +0 +LISTEN +\\S+ +INET +STREAM" "get internet-domain sockets"
|
|
|
|
# key shmid perm size creator command last op command num attached user group creator user creator group last shmat() time last shmdt() time last shmctl() time
|
|
expect_multiline "info os shm" "$shmkey +$shmid +666 +4096 +info-os .*" "get shared-memory regions"
|
|
|
|
# key semid perm num semaphores user group creator user creator group last semop() time last semctl() time
|
|
expect_multiline "info os semaphores" "$semkey +$semid +666 +1 .*" "get semaphores"
|
|
|
|
# key msqid perm num used bytes num messages last msgsnd() command last msgrcv() command user group creator user creator group last msgsnd() time last msgrcv() time last msgctl() time
|
|
expect_multiline "info os msg" "$msgkey +$msqid +666 .*" "get message queues"
|
|
|
|
|
|
# The SysV IPC primitives linger on after the creating process is killed
|
|
# unless they are destroyed explicitly, so allow the test program to tidy
|
|
# up after itself.
|
|
gdb_test "continue" "exited.*"
|