Files
binutils-gdb/gdb/testsuite/gdb.base/info-program.exp
Pedro Alves 6bf09ec03d Improve "info program"
With gdb.base/catch-follow-exec.exp, we currently see:

~~~~~~~~~~~~~~~
 (gdb)
 continue
 Continuing.
 process 693251 is executing new program: /usr/bin/ls
 [New inferior 2]
 [New process 693251]
 [Switching to process 693251]

 Thread 2.1 "ls" hit Catchpoint 2 (exec'd /usr/bin/ls), 0x00007ffff7fd0100 in _start () from /lib64/ld-linux-x86-64.so.2
 (gdb)
 info prog
 No selected thread.
~~~~~~~~~~~~~~~

Note the "No selected thread" output.  That is totally bogus, because
there _is_ a selected thread.  What GDB really means, is that it can't
find the thread that had the latest (user-visible) stop.  And that
happens because "info program" gets that info from
get_last_target_status, and the last target status has been cleared.

However, GDB also checks if there is a selected thread, here:

  if (ptid == null_ptid || ptid == minus_one_ptid)
    error (_("No selected thread."));

.. the null_ptid part.  That is also bogus, because what matters is
the thread that last reported a stop, not the current thread:

 - in all-stop mode, "info program" displays info about the last stop.
   That may have happened on a thread different from the selected
   thread.

 - in non-stop mode, because all threads are controlled individually,
   "info program" shows info about the last stop of the selected
   thread.

The current code already behaves this way, though in a poor way.  This
patch reimplements it, such that the all-stop version now finds the
thread that last reported an event via the 'previous_thread' strong
reference.  Being a strong reference means that if that thread has
exited since the event was reported, 'previous_thread' will still
point to it, so we can say that the thread exited meanwhile.

The patch also extends "info program" output a little, to let the user
know which thread we are printing info for.  For example, for the
gdb.base/catch-follow-exec.exp case we shown above, we now get:

 (gdb) info prog
 Last stopped for thread 2.1 (process 710867).
	 Using the running image of child process 710867.
 Program stopped at 0x7ffff7fd0100.
 It stopped at breakpoint 2.
 Type "info stack" or "info registers" for more information.
 (gdb)

while in non-stop mode, we get:

 (gdb) info prog
 Selected thread 2.1 (process 710867).
	 Using the running image of child process 710867.
 Program stopped at 0x7ffff7fd0100.
 It stopped at breakpoint 2.
 Type "info stack" or "info registers" for more information.
 (gdb)

In both cases, the first line of output is new.

The existing code considered these running/exited cases as an error,
but I think that that's incorrect, since this is IMO just plain
execution info as well.  So the patch makes those cases regular
prints, not errors.

If the thread is running, we get, in non-stop mode:

 (gdb) info prog
 Selected thread 2.1 (process 710867).
 Selected thread is running.

... and in all-stop:

 (gdb) info prog
 Last stopped for thread 2.1 (process 710867).
 Thread is now running.

If the thread has exited, we get, in non-stop mode:

 (gdb) info prog
 Selected thread 2.1 (process 710867).
 Selected thread has exited.

... and in all-stop:

 (gdb) info prog
 Last stopped for thread 2.1 (process 710867).
 Thread has since exited.

The gdb.base/info-program.exp testcase was much extended to test
all-stop/non-stop and single-threaded/multi-threaded.

Change-Id: I51d9d445f772d872af3eead3449ad4aa445781b1
2023-02-27 19:12:28 +00:00

137 lines
4.1 KiB
Plaintext

# Copyright 1997-2023 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/>.
# Test "info program".
#
# We build both single-threaded and multi-threaded programs so that if
# the target doesn't support multi-threading, we still exercise the
# command.
#
# With the multi-threaded program, we test that in all-stop mode, GDB
# prints information about the last thread that stopped, not the
# current thread. In non-stop mode, the command always prints info
# about the selected thread, so we test that.
standard_testfile
# Run the test with the given parameters:
#
# - THREADS: threads flavor, either single-threaded or
# multi-threaded.
# - NON-STOP: "set non-stop" value, "on" or "off".
proc do_test { threads non-stop } {
save_vars { ::GDBFLAGS } {
append ::GDBFLAGS " -ex \"set non-stop ${non-stop}\""
clean_restart $::binfile-$threads
}
gdb_test "info program" \
"The program being debugged is not being run." \
"info program before run"
if { ![runto done] } {
return -1
}
if {${non-stop} == "on"} {
set thread_line "Selected"
} else {
set thread_line "Last stopped for"
}
gdb_test "info program" \
[multi_line \
"$thread_line thread 1 (\[^\r\n\]+)\\." \
".*" \
"Program stopped at $::hex\." \
"It stopped at breakpoint $::decimal\." \
"Type \"info stack\" or \"info registers\" for more information\."] \
"info program after run to main"
# We don't really care where this step lands, so long as GDB reports
# that the inferior stopped due to a step in the subsequent test.
gdb_test "next" ".*" "step before info program"
gdb_test "info program" \
[multi_line \
"$thread_line thread 1 (\[^\r\n\]+)\\." \
".*" \
"Program stopped at $::hex\." \
"It stopped after being stepped\." \
"Type \"info stack\" or \"info registers\" for more information\."] \
"info program after next"
if {$threads == "mt"} {
gdb_test "thread 2" "\\\[Switching to thread 2 .*"
if {${non-stop} == "on"} {
gdb_test "info program" \
[multi_line \
"$thread_line thread 2 (\[^\r\n\]+)\\." \
"Selected thread is running\\."] \
"info program after next, other thread"
} else {
gdb_test "info program" \
[multi_line \
"$thread_line thread 1 (\[^\r\n\]+)\\." \
".*" \
"Program stopped at $::hex\." \
"It stopped after being stepped\." \
"Type \"info stack\" or \"info registers\" for more information\."] \
"info program after next, other thread"
}
}
gdb_test "kill" "" "kill program" \
"Kill the program being debugged.*y or n. $" "y"
gdb_test "info program" "The program being debugged is not being run." \
"info program, after kill"
if { ![runto done] } {
return -1
}
delete_breakpoints
gdb_test "info program" \
[multi_line \
"$thread_line thread 1 (\[^\r\n\]+)\\." \
".*" \
"Program stopped at $::hex\." \
"It stopped at a breakpoint that has since been deleted\." \
"Type \"info stack\" or \"info registers\" for more information\."] \
"info program after deleting all breakpoints"
}
# Build executables and test them, one for each
# single-thread/multi-thread flavor.
foreach_with_prefix threads {st mt} {
set opts {debug}
if {$threads == "mt"} {
lappend opts pthreads "additional_flags=-DUSE_THREADS"
}
if { [build_executable "failed to prepare $threads" \
${testfile}-${threads} ${srcfile} $opts] } {
continue
}
foreach_with_prefix non-stop {on off} {
do_test ${threads} ${non-stop}
}
}