binutils-gdb/gdb/testsuite/gdb.threads/fork-thread-pending.exp
Pedro Alves c1a747c109 Linux: Skip thread_db thread event reporting if PTRACE_EVENT_CLONE is supported
[A test I wrote stumbled on a libthread_db issue related to thread
event breakpoints.  See glibc PR17705:
 [nptl_db: stale thread create/death events if debugger detaches]
 https://sourceware.org/bugzilla/show_bug.cgi?id=17705

This patch avoids that whole issue by making GDB stop using thread
event breakpoints in the first place, which is good for other reasons
as well, anyway.]

Before PTRACE_EVENT_CLONE (Linux 2.6), the only way to learn about new
threads in the inferior (to attach to them) or to learn about thread
exit was to coordinate with the inferior's glibc/runtime, using
libthread_db.  That works by putting a breakpoint at a magic address
which is called when a new thread is spawned, or when a thread is
about to exit.  When that breakpoint is hit, all threads are stopped,
and then GDB coordinates with libthread_db to read data structures out
of the inferior to learn about what happened.  Then the breakpoint is
single-stepped, and then all threads are re-resumed.  This isn't very
efficient (stops all threads) and is more fragile (inferior's thread
list in memory may be corrupt; libthread_db bugs, etc.) than ideal.

When the kernel supports PTRACE_EVENT_CLONE (which we already make use
of), there's really no need to use libthread_db's event reporting
mechanism to learn about new LWPs.  And if the kernel supports that,
then we learn about LWP exits through regular WIFEXITED wait statuses,
so no need for the death event breakpoint either.

GDBserver has been likewise skipping the thread_db events for a long
while:
  https://sourceware.org/ml/gdb-patches/2007-10/msg00547.html

There's one user-visible difference: we'll no longer print about
threads being created and exiting while the program is running, like:

 [Thread 0x7ffff7dbb700 (LWP 30670) exited]
 [New Thread 0x7ffff7db3700 (LWP 30671)]
 [Thread 0x7ffff7dd3700 (LWP 30667) exited]
 [New Thread 0x7ffff7dab700 (LWP 30672)]
 [Thread 0x7ffff7db3700 (LWP 30671) exited]
 [Thread 0x7ffff7dcb700 (LWP 30668) exited]

This is exactly the same behavior as when debugging against remote
targets / gdbserver.  I actually think that's a good thing (and as
such have listed this in the local/remote parity wiki page a while
ago), as the printing slows down the inferior.  It's also a
distraction to keep bothering the user about short-lived threads that
she won't be able to interact with anyway.  Instead, the user (and
frontend) will be informed about new threads that currently exist in
the program when the program next stops:

 (gdb) c
 ...
 * ctrl-c *
 [New Thread 0x7ffff7963700 (LWP 7797)]
 [New Thread 0x7ffff796b700 (LWP 7796)]

 Program received signal SIGINT, Interrupt.
 [Switching to Thread 0x7ffff796b700 (LWP 7796)]
 clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:81
 81              testq   %rax,%rax
 (gdb) info threads

A couple of tests had assumptions on GDB thread numbers that no longer
hold.

Tested on x86_64 Fedora 20.

gdb/
2014-01-09  Pedro Alves  <palves@redhat.com>

	Skip enabling event reporting if the kernel supports
	PTRACE_EVENT_CLONE.
	* linux-thread-db.c: Include "nat/linux-ptrace.h".
	(thread_db_use_events): New function.
	(try_thread_db_load_1): Check thread_db_use_events before enabling
	event reporting.
	(update_thread_state): New function.
	(attach_thread): Use it.  Check thread_db_use_events before
	enabling event reporting.
	(thread_db_detach): Check thread_db_use_events before disabling
	event reporting.
	(find_new_threads_callback): Check thread_db_use_events before
	enabling event reporting.  Update the thread's state if not using
	libthread_db events.

gdb/testsuite/
2014-01-09  Pedro Alves  <palves@redhat.com>

	* gdb.threads/fork-thread-pending.exp: Switch to the main thread
	instead of to thread 2.
	* gdb.threads/signal-command-multiple-signals-pending.c (main):
	Add barrier around each pthread_create call instead of around all
	calls.
	* gdb.threads/signal-command-multiple-signals-pending.exp (test):
	Set a break on thread_function and have the child threads hit it
	one at at a time.
2015-01-09 11:42:57 +00:00

124 lines
3.4 KiB
Plaintext

# Copyright (C) 2009-2015 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/>.
# There's no support for `set follow-fork-mode' in the remote
# protocol.
if { [is_remote target] } {
return 0
}
# Only GNU/Linux is known to support `set follow-fork-mode child'.
#
if { ! [istarget "*-*-linux*"] } {
return 0
}
standard_testfile
if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
return -1
}
clean_restart ${binfile}
if ![runto_main] then {
fail "Can't run to main"
return 0
}
gdb_test_no_output "set follow-fork-mode child" "1, set follow-fork-mode child"
gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "1, insert fork catchpoint"
gdb_breakpoint "start" "" "1, set breakpoint at start"
gdb_test "continue" "Catchpoint.*" "1, get to the fork event"
gdb_test "info threads" " Thread .* Thread .* Thread .* Thread .*" "1, multiple threads found"
gdb_test "thread 1" ".*" "1, switched away from event thread"
gdb_test "continue" "Not resuming.*" "1, refused to resume"
set test "1, followed to the child, found one thread"
gdb_test_multiple "info threads" "metest" {
-re " Thread .* Thread .*$gdb_prompt $" {
fail "$test"
}
-re " Thread .*$gdb_prompt $" {
pass "$test"
}
-re "$gdb_prompt $" {
fail "$test (unknown output)"
}
timeout {
fail "$test (timeout)"
}
}
gdb_test "continue" "Breakpoint 3, start.*" "1, get to the spawned thread in fork child"
set test "1, followed to the child, found two threads"
gdb_test_multiple "info threads" "$test" {
-re " Thread .* Thread .* Thread .*$gdb_prompt $" {
fail "$test"
}
-re " Thread .* Thread .*$gdb_prompt $" {
pass "$test"
}
-re "$gdb_prompt $" {
fail "$test (unknown output)"
}
timeout {
fail "$test (timeout)"
}
}
# Start over, but this time, don't switch away from the fork event thread.
gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}
if ![runto_main] then {
fail "Can't run to main"
return 0
}
gdb_test_no_output "set follow-fork-mode child" "2, set follow-fork-mode child"
gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "2, insert fork catchpoint"
gdb_breakpoint "start"
gdb_test "continue" "Catchpoint.*" "2, get to the fork event"
gdb_test "info threads" " Thread .* Thread .* Thread .* Thread .*" "2, multiple threads found"
gdb_test "continue" "Breakpoint 3, start.*" "2, get to the spawned thread in fork child"
set test "2, followed to the child, found two threads"
gdb_test_multiple "info threads" "$test" {
-re " Thread .* Thread .* Thread .*$gdb_prompt $" {
fail "$test"
}
-re " Thread .* Thread .*$gdb_prompt $" {
pass "$test"
}
-re "$gdb_prompt $" {
fail "$test (unknown output)"
}
timeout {
fail "$test (timeout)"
}
}