This patch aims to turn 'set print inferior-events' always on, and do
some cleanup on the messages printed by GDB when various inferior
events happen (attach, detach, fork, kill, exit).
To make sure that the patch is correct, I've tested it with a handful
of combinations of 'set follow-fork-mode', 'set detach-on-fork' and
'set print inferior-events'. In the end, I decided to make my
hand-made test into an official testcase. More on that below.
Using the following program as an example:
#include <unistd.h>
int main ()
{
fork ();
return 0;
}
We see the following outputs from the patched GDB:
- With 'set print inferior-events on':
(gdb) r
Starting program: a.out
[Detaching after fork from child process 27749]
[Inferior 1 (process 27745) exited normally]
(gdb)
- With 'set print inferior-events off':
(gdb) r
Starting program: a.out
[Inferior 1 (process 27823) exited normally]
(gdb)
Comparing this against an unpatched GDB:
- With 'set print inferior-events off' and 'set follow-fork-mode
child':
(gdb) r
Starting program: a.out
[Inferior 2 (process 5993) exited normally]
(gdb)
Compare this against an unpatched GDB:
(unpatched-gdb) r
Starting program: a.out
[New process 5702]
[Inferior 2 (process 5702) exited normally]
(unpatched-gdb)
It is possible to notice that, in this scenario, the patched GDB
will lose the '[New process %d]' message.
- With 'set print inferior-events on', 'set follow-fork-mode child'
and 'set detach-on-fork on':
(gdb) r
Starting program: a.out
[Attaching after process 27905 fork to child process 27909]
[New inferior 2 (process 27909)]
[Detaching after fork from parent process 27905]
[Inferior 1 (process 27905) detached]
[Inferior 2 (process 27909) exited normally]
(gdb)
Compare this output with an unpatched GDB, using the same settings:
(unpatched-gdb) r
Starting program: a.out
[New inferior 28033]
[Inferior 28029 detached]
[New process 28033]
[Inferior 2 (process 28033) exited normally]
[Inferior 28033 exited]
(unpatched-gdb)
As can be seen above, I've also made a few modifications to messages
that are printed when 'set print inferior-events' is on. For example,
a few of the messages did not contain the '[' and ']' as
prefix/suffix, which led to a few inconsistencies like:
Attaching after process 22995 fork to child process 22999.
[New inferior 22999]
Detaching after fork from child process 22999.
[Inferior 22995 detached]
[Inferior 2 (process 22999) exited normally]
So I took the opportunity and included the square brackets where
applicable. I have also made the existing messages more uniform, by
always printing "Inferior %d (process %d)..." where applicable. This
makes it easier to identify the inferior number and the PID number
from the messages.
As suggested by Pedro, the "[Inferior %d exited]" message from
'exit_inferior' has been removed, because it got duplicated when
'inferior-events' is on. I'm also using the
'add_{thread,inferior}_silent' versions (instead of their verbose
counterparts) on some locations, also to avoid duplicated messages.
For example, a patched GDB with 'set print inferior-events on', 'set
detach-on-fork on' and 'set follow-fork-mode child', but using
'add_thread', would print:
(gdb) run
Starting program: a.out
[Attaching after process 25088 fork to child process 25092.]
[New inferior 25092] <--- duplicated
[Detaching after fork from child process 25092.]
[Inferior 25088 detached]
[New process 25092] <--- duplicated
[Inferior 2 (process 25092) exited normally]
But if we use 'add_thread_silent' (with the same configuration as
before):
(gdb) run
Starting program: a.out
[Attaching after process 31606 fork to child process 31610]
[New inferior 2 (process 31610)]
[Detaching after fork from parent process 31606]
[Inferior 1 (process 31606) detached]
[Inferior 2 (process 31610) exited normally]
As for the tests, the configuration options being exercised are:
- follow-fork-mode: child/parent
- detach-on-fork: on/off
- print inferior-events: on/off
It was also necessary to perform adjustments on several testcases,
because the expected messages changed considerably.
Built and regtested on BuildBot, without regressions.
gdb/ChangeLog:
2018-04-24 Jan Kratochvil <jan.kratochvil@redhat.com>
Sergio Durigan Junior <sergiodj@redhat.com>
Pedro Alves <palves@redhat.com>
* infcmd.c (kill_command): Print message when inferior has
been killed.
* inferior.c (print_inferior_events): Remove 'static'. Set as
'1'.
(add_inferior): Improve message printed when
'print_inferior_events' is on.
(exit_inferior): Remove message printed when
'print_inferior_events' is on.
(detach_inferior): Improve message printed when
'print_inferior_events' is on.
(initialize_inferiors): Use 'add_inferior_silent' to set
'current_inferior_'.
* inferior.h (print_inferior_events): Declare here as
'extern'.
* infrun.c (follow_fork_inferior): Print '[Attaching...]' or
'[Detaching...]' messages when 'print_inferior_events' is on.
Use 'add_thread_silent' instead of 'add_thread'. Add '[' and ']'
as prefix/suffix for messages. Remove periods. Fix erroneous
'Detaching after fork from child...', replace it by '... from
parent...'.
(handle_vfork_child_exec_or_exit): Add '[' and ']' as
prefix/suffix when printing 'Detaching...' messages. Print
them when 'print_inferior_events' is on.
* remote.c (remote_detach_1): Print message when detaching
from inferior and '!is_fork_parent'.
gdb/testsuite/ChangeLog:
2018-04-24 Jan Kratochvil <jan.kratochvil@redhat.com>
Sergio Durigan Junior <sergiodj@redhat.com>
Pedro Alves <palves@redhat.com>
* gdb.base/attach-non-pgrp-leader.exp: Adjust 'Detaching...'
regexps to expect for '[Inferior ... detached]' as well.
* gdb.base/attach.exp: Likewise.
* gdb.base/catch-syscall.exp (check_for_program_end): Adjust
"gdb_continue_to_end".
(test_catch_syscall_with_wrong_args): Likewise.
* gdb.base/foll-fork.exp: Adjust regexps to match '[' and
']'. Don't set 'verbose' on.
* gdb.base/foll-vfork.exp: Likewise.
* gdb.base/fork-print-inferior-events.c: New file.
* gdb.base/fork-print-inferior-events.exp: New file.
* gdb.base/hook-stop.exp: Adjust regexps to expect for new
'[Inferior ... has been killed]' message.
* gdb.base/kill-after-signal.exp: Likewise.
* gdb.base/solib-overlap.exp: Adjust regexps to expect for new
detach message.
* gdb.threads/kill.exp: Adjust regexps to expect for new kill
message.
* gdb.threads/clone-attach-detach.exp: Adjust 'Detaching...'
regexps to expect for '[Inferior ... detached]' as well.
* gdb.threads/process-dies-while-detaching.exp: Likewise.
463 lines
13 KiB
Plaintext
463 lines
13 KiB
Plaintext
# Copyright 1997-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/>. */
|
|
|
|
if {![can_spawn_for_attach]} {
|
|
return 0
|
|
}
|
|
|
|
standard_testfile attach.c attach2.c
|
|
set binfile2 ${binfile}2
|
|
set escapedbinfile [string_to_regexp $binfile]
|
|
|
|
#execute_anywhere "rm -f ${binfile} ${binfile2}"
|
|
remote_exec build "rm -f ${binfile} ${binfile2}"
|
|
# For debugging this test
|
|
#
|
|
#log_user 1
|
|
|
|
# build the first test case
|
|
#
|
|
if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
|
|
untested "failed to compile"
|
|
return -1
|
|
}
|
|
|
|
# Build the in-system-call test
|
|
|
|
if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
|
|
untested "failed to compile in-system-call test"
|
|
return -1
|
|
}
|
|
|
|
if [get_compiler_info] {
|
|
return -1
|
|
}
|
|
|
|
proc do_attach_tests {} {
|
|
global gdb_prompt
|
|
global binfile
|
|
global escapedbinfile
|
|
global srcfile
|
|
global testfile
|
|
global subdir
|
|
global timeout
|
|
global decimal
|
|
|
|
# Figure out a regular expression that will match the sysroot,
|
|
# noting that the default sysroot is "target:", and also noting
|
|
# that GDB will strip "target:" from the start of filenames when
|
|
# operating on the local filesystem. However the default sysroot
|
|
# can be set via configure option --with-sysroot, which can be "/".
|
|
# If $binfile is a absolute path, so pattern
|
|
# "$sysroot$escapedbinfile" below is wrong. Use [^\r\n]* to make
|
|
# $sysroot simple.
|
|
set sysroot "\[^\r\n\]*"
|
|
|
|
# Start the program running and then wait for a bit, to be sure
|
|
# that it can be attached to.
|
|
|
|
set test_spawn_id [spawn_wait_for_attach $binfile]
|
|
set testpid [spawn_id_get_pid $test_spawn_id]
|
|
|
|
# Verify that we cannot attach to nonsense.
|
|
|
|
set test "attach to nonsense is prohibited"
|
|
gdb_test_multiple "attach abc" "$test" {
|
|
-re "Illegal process-id: abc\\.\r\n$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
|
|
# Response expected from /proc-based systems.
|
|
pass "$test"
|
|
}
|
|
-re "Can't attach to process..*$gdb_prompt $" {
|
|
# Response expected on Cygwin
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*$gdb_prompt $" {
|
|
fail "$test (bogus pid allowed)"
|
|
}
|
|
}
|
|
|
|
# Verify that we cannot attach to nonsense even if its initial part is
|
|
# a valid PID.
|
|
|
|
set test "attach to digits-starting nonsense is prohibited"
|
|
gdb_test_multiple "attach ${testpid}x" "$test" {
|
|
-re "Illegal process-id: ${testpid}x\\.\r\n$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
|
|
# Response expected from /proc-based systems.
|
|
pass "$test"
|
|
}
|
|
-re "Can't attach to process..*$gdb_prompt $" {
|
|
# Response expected on Cygwin
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*$gdb_prompt $" {
|
|
fail "$test (bogus pid allowed)"
|
|
}
|
|
}
|
|
|
|
# Verify that we cannot attach to what appears to be a valid
|
|
# process ID, but is a process that doesn't exist. Traditionally,
|
|
# most systems didn't have a process with ID 0, so we take that as
|
|
# the default. However, there are a few exceptions.
|
|
|
|
set boguspid 0
|
|
if { [istarget "*-*-*bsd*"] } {
|
|
# In FreeBSD 5.0, PID 0 is used for "swapper". Use -1 instead
|
|
# (which should have the desired effect on any version of
|
|
# FreeBSD, and probably other *BSD's too).
|
|
set boguspid -1
|
|
}
|
|
set test "attach to nonexistent process is prohibited"
|
|
gdb_test_multiple "attach $boguspid" "$test" {
|
|
-re "Attaching to.*, process $boguspid.*No such process.*$gdb_prompt $" {
|
|
# Response expected on ptrace-based systems (i.e. HP-UX 10.20).
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process $boguspid failed.*Hint.*$gdb_prompt $" {
|
|
# Response expected on ttrace-based systems (i.e. HP-UX 11.0).
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process $boguspid.*denied.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process $boguspid.*not permitted.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process .*couldn't open /proc file.*$gdb_prompt $" {
|
|
# Response expected from /proc-based systems.
|
|
pass "$test"
|
|
}
|
|
-re "Can't attach to process..*$gdb_prompt $" {
|
|
# Response expected on Cygwin
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*, process $boguspid.*failed.*$gdb_prompt $" {
|
|
# Response expected on the extended-remote target.
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
# Verify that we can attach to the process by first giving its
|
|
# executable name via the file command, and using attach with the
|
|
# process ID.
|
|
|
|
# (Actually, the test system appears to do this automatically for
|
|
# us. So, we must also be prepared to be asked if we want to
|
|
# discard an existing set of symbols.)
|
|
|
|
set test "set file, before attach1"
|
|
gdb_test_multiple "file $binfile" "$test" {
|
|
-re "Load new symbol table from.*y or n. $" {
|
|
gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
|
|
"$test (re-read)"
|
|
}
|
|
-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
set test "attach1, after setting file"
|
|
gdb_test_multiple "attach $testpid" "$test" {
|
|
-re "Attaching to program.*`?$escapedbinfile'?, process $testpid.*main.*at .*$srcfile:.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to program.*`?$escapedbinfile\.exe'?, process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
|
|
# Response expected on Cygwin
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
# Verify that we can "see" the variable "should_exit" in the
|
|
# program, and that it is zero.
|
|
|
|
gdb_test "print should_exit" " = 0" "after attach1, print should_exit"
|
|
|
|
# Detach the process.
|
|
|
|
gdb_test "detach" \
|
|
"Detaching from program: .*$escapedbinfile, process $testpid\r\n\\\[Inferior $decimal \\(.*\\) detached\\\]" \
|
|
"attach1 detach"
|
|
|
|
# Wait a bit for gdb to finish detaching
|
|
|
|
exec sleep 5
|
|
|
|
# Purge the symbols from gdb's brain. (We want to be certain the
|
|
# next attach, which won't be preceded by a "file" command, is
|
|
# really getting the executable file without our help.)
|
|
|
|
set old_timeout $timeout
|
|
set timeout 15
|
|
set test "attach1, purging symbols after detach"
|
|
gdb_test_multiple "file" "$test" {
|
|
-re "No executable file now.*Discard symbol table.*y or n. $" {
|
|
gdb_test "y" "No symbol file now." "$test"
|
|
}
|
|
}
|
|
set timeout $old_timeout
|
|
|
|
# Verify that we can attach to the process just by giving the
|
|
# process ID.
|
|
|
|
set test "attach2, with no file"
|
|
set found_exec_file 0
|
|
gdb_test_multiple "attach $testpid" "$test" {
|
|
-re "Attaching to process $testpid.*Load new symbol table from \"$sysroot$escapedbinfile\.exe\".*y or n. $" {
|
|
# On Cygwin, the DLL's symbol tables are loaded prior to the
|
|
# executable's symbol table. This in turn always results in
|
|
# asking the user for actually loading the symbol table of the
|
|
# executable.
|
|
gdb_test "y" "Reading symbols from $sysroot$escapedbinfile\.\.\.*done." \
|
|
"$test (reset file)"
|
|
|
|
set found_exec_file 1
|
|
}
|
|
-re "Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*$gdb_prompt $" {
|
|
pass "$test"
|
|
set found_exec_file 1
|
|
}
|
|
}
|
|
|
|
if {$found_exec_file == 0} {
|
|
set test "load file manually, after attach2"
|
|
gdb_test_multiple "file $binfile" "$test" {
|
|
-re "A program is being debugged already..*Are you sure you want to change the file.*y or n. $" {
|
|
gdb_test "y" "Reading symbols from $escapedbinfile\.\.\.*done." \
|
|
"$test (re-read)"
|
|
}
|
|
-re "Reading symbols from $escapedbinfile\.\.\.*done.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
}
|
|
}
|
|
|
|
# Verify that we can modify the variable "should_exit" in the
|
|
# program.
|
|
|
|
gdb_test_no_output "set should_exit=1" "after attach2, set should_exit"
|
|
|
|
# Verify that the modification really happened.
|
|
|
|
gdb_breakpoint [gdb_get_line_number "postloop"] temporary
|
|
gdb_continue_to_breakpoint "postloop" ".* postloop .*"
|
|
|
|
# Allow the test process to exit, to cleanup after ourselves.
|
|
|
|
gdb_continue_to_end "after attach2, exit"
|
|
|
|
# Make sure we don't leave a process around to confuse
|
|
# the next test run (and prevent the compile by keeping
|
|
# the text file busy), in case the "set should_exit" didn't
|
|
# work.
|
|
|
|
kill_wait_spawned_process $test_spawn_id
|
|
|
|
set test_spawn_id [spawn_wait_for_attach $binfile]
|
|
set testpid [spawn_id_get_pid $test_spawn_id]
|
|
|
|
# Verify that we can attach to the process, and find its a.out
|
|
# when we're cd'd to some directory that doesn't contain the
|
|
# a.out. (We use the source path set by the "dir" command.)
|
|
|
|
gdb_test "dir [standard_output_file {}]" "Source directories searched: .*" \
|
|
"set source path"
|
|
|
|
gdb_test "cd /tmp" "Working directory /tmp." \
|
|
"cd away from process working directory"
|
|
|
|
# Explicitly flush out any knowledge of the previous attachment.
|
|
|
|
set test "before attach3, flush symbols"
|
|
gdb_test_multiple "symbol-file" "$test" {
|
|
-re "Discard symbol table from.*y or n. $" {
|
|
gdb_test "y" "No symbol file now." \
|
|
"$test"
|
|
}
|
|
-re "No symbol file now.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
gdb_test "exec" "No executable file now." \
|
|
"before attach3, flush exec"
|
|
|
|
gdb_test "attach $testpid" \
|
|
"Attaching to process $testpid.*Reading symbols from $sysroot$escapedbinfile.*main.*at .*" \
|
|
"attach when process' a.out not in cwd"
|
|
|
|
set test "after attach3, exit"
|
|
gdb_test "kill" \
|
|
"" \
|
|
"$test" \
|
|
"Kill the program being debugged.*y or n. $" \
|
|
"y"
|
|
|
|
# Another "don't leave a process around"
|
|
kill_wait_spawned_process $test_spawn_id
|
|
}
|
|
|
|
proc do_call_attach_tests {} {
|
|
global gdb_prompt
|
|
global binfile2
|
|
|
|
set test_spawn_id [spawn_wait_for_attach $binfile2]
|
|
set testpid [spawn_id_get_pid $test_spawn_id]
|
|
|
|
# Attach
|
|
|
|
gdb_test "file $binfile2" ".*" "load file"
|
|
set test "attach call"
|
|
gdb_test_multiple "attach $testpid" "$test" {
|
|
-re "warning: reading register.*I.*O error.*$gdb_prompt $" {
|
|
fail "$test (read register error)"
|
|
}
|
|
-re "Attaching to.*process $testpid.*libc.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
-re "Attaching to.*process $testpid.*\[Switching to thread $testpid\..*\].*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
# See if other registers are problems
|
|
|
|
set test "info other register"
|
|
gdb_test_multiple "i r r3" "$test" {
|
|
-re "warning: reading register.*$gdb_prompt $" {
|
|
fail "$test"
|
|
}
|
|
-re "r3.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
# Get rid of the process
|
|
|
|
gdb_test "p should_exit = 1"
|
|
gdb_continue_to_end
|
|
|
|
# Be paranoid
|
|
|
|
kill_wait_spawned_process $test_spawn_id
|
|
}
|
|
|
|
proc do_command_attach_tests {} {
|
|
global gdb_prompt
|
|
global binfile
|
|
global verbose
|
|
global GDB
|
|
global INTERNAL_GDBFLAGS
|
|
global GDBFLAGS
|
|
|
|
if ![isnative] then {
|
|
unsupported "command attach test"
|
|
return 0
|
|
}
|
|
|
|
set test_spawn_id [spawn_wait_for_attach $binfile]
|
|
set testpid [spawn_id_get_pid $test_spawn_id]
|
|
|
|
gdb_exit
|
|
|
|
set res [gdb_spawn_with_cmdline_opts "--pid=$testpid"]
|
|
set test "starting with --pid"
|
|
gdb_test_multiple "" $test {
|
|
-re "Reading symbols from.*$gdb_prompt $" {
|
|
pass "$test"
|
|
}
|
|
}
|
|
|
|
# Get rid of the process
|
|
kill_wait_spawned_process $test_spawn_id
|
|
}
|
|
|
|
# Test ' gdb --pid PID -ex "run" '. GDB used to have a bug where
|
|
# "run" would run before the attach finished - PR17347.
|
|
|
|
proc test_command_line_attach_run {} {
|
|
global gdb_prompt
|
|
global binfile
|
|
|
|
if ![isnative] then {
|
|
unsupported "commandline attach run test"
|
|
return 0
|
|
}
|
|
|
|
with_test_prefix "cmdline attach run" {
|
|
set test_spawn_id [spawn_wait_for_attach $binfile]
|
|
set testpid [spawn_id_get_pid $test_spawn_id]
|
|
|
|
set test "run to prompt"
|
|
gdb_exit
|
|
|
|
set res [gdb_spawn_with_cmdline_opts \
|
|
"-iex \"set height 0\" -iex \"set width 0\" --pid=$testpid -ex \"start\""]
|
|
if { $res != 0} {
|
|
fail $test
|
|
kill_wait_spawned_process $test_spawn_id
|
|
return $res
|
|
}
|
|
gdb_test_multiple "" $test {
|
|
-re {Attaching to.*Start it from the beginning\? \(y or n\) } {
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
send_gdb "y\n"
|
|
|
|
set test "run to main"
|
|
gdb_test_multiple "" $test {
|
|
-re "Temporary breakpoint .* main .*$gdb_prompt $" {
|
|
pass $test
|
|
}
|
|
}
|
|
|
|
# Get rid of the process
|
|
kill_wait_spawned_process $test_spawn_id
|
|
}
|
|
}
|
|
|
|
# Start with a fresh gdb
|
|
|
|
gdb_exit
|
|
gdb_start
|
|
gdb_reinitialize_dir $srcdir/$subdir
|
|
gdb_load ${binfile}
|
|
|
|
# This is a test of gdb's ability to attach to a running process.
|
|
|
|
do_attach_tests
|
|
|
|
# Test attaching when the target is inside a system call
|
|
|
|
gdb_exit
|
|
gdb_start
|
|
|
|
gdb_reinitialize_dir $srcdir/$subdir
|
|
do_call_attach_tests
|
|
|
|
# Test "gdb --pid"
|
|
|
|
do_command_attach_tests
|
|
|
|
test_command_line_attach_run
|
|
|
|
return 0
|