aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads
diff options
context:
space:
mode:
authorPedro Alves <palves@redhat.com>2017-04-19 13:12:23 +0100
committerPedro Alves <palves@redhat.com>2017-04-19 13:12:23 +0100
commit3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1 (patch)
treec76d8c677a430fc2511e380fa895258e78811882 /gdb/testsuite/gdb.threads
parent9bcb1f1630b05594fa86bfd017639cfcc966b11c (diff)
downloadgdb-3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1.zip
gdb-3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1.tar.gz
gdb-3a3fd0fd2c4c87fdd588c51d879961a49e38f0c1.tar.bz2
Fix removing inferiors from within "thread apply" commands
This patch fixes an internal error exposed by a test that does something like: define kill-and-remove kill inferiors 2 remove-inferiors 2 end # Start one inferior. start # Start another inferior. add-inferior 2 inferior 2 start # Kill and remove inferior 1 while inferior 2 is selected. thread apply 1.1 kill-and-remove The internal error looks like this: Thread 1.1 (Thread 0x7ffff7fc2700 (LWP 20677)): [Switching to inferior 1 [process 20677] (gdb/testsuite/outputs/gdb.threads/threadapply/threadapply)] [Switching to thread 1.1 (Thread 0x7ffff7fc2700 (LWP 20677))] #0 main () at src/gdb/testsuite/gdb.threads/threadapply.c:38 38 for (i = 0; i < NUM; i++) src/gdb/inferior.c:66: internal-error: void set_current_inferior(inferior*): Assertion `inf != NULL' failed. A problem internal to GDB has been detected, further debugging may prove unreliable. Quit this debugging session? (y or n) FAIL: gdb.threads/threadapply.exp: kill_and_remove_inferior: try kill-and-remove: thread apply 1.1 kill-and-remove (GDB internal error) There are several problems around this area of the code. One is that in do_restore_current_thread_cleanup, we do a look up of inferior by ptid, which can find the wrong inferior if the previously selected inferior exited and some other inferior was started with a reused pid (rare, but still...). The other problem is that the "remove-inferiors" command rejects attempts to remove the current inferior, but when we get to "remove-inferiors" in a "thread apply THR remove-inferiors 2" command, the current inferior is the inferior of thread THR, not the previously selected inferior, so if the previously selected inferior was inferior 2, that command still manages to wipe it, and then gdb restores the old selected inferior, which is now a dangling pointer... So the fix here is: - Make make_cleanup_restore_current_thread store a pointer to the previously selected inferior directly, and use it directly instead of doing ptid look ups. - Add a refcount to inferiors, very similar to thread_info's refcount, that is incremented/decremented by make_cleanup_restore_current_thread, and checked before deleting an inferior. To avoid duplication, a new refcounted_object type is added, that both thread_info and inferior inherit from. gdb/ChangeLog: 2017-04-19 Pedro Alves <palves@redhat.com> * common/refcounted-object.h: New file. * gdbthread.h: Include "common/refcounted-object.h". (thread_info): Inherit from refcounted_object and add comments. (thread_info::incref, thread_info::decref) (thread_info::m_refcount): Delete. (thread_info::deletable): Use the refcounted_object::refcount() method. * inferior.c (current_inferior_): Add comment. (set_current_inferior): Increment/decrement refcounts. (prune_inferiors, remove_inferior_command): Skip inferiors marked not-deletable instead of comparing with the current inferior. (initialize_inferiors): Increment the initial inferior's refcount. * inferior.h (struct inferior): Forward declare. Include "common/refcounted-object.h". (current_inferior, set_current_inferior): Move declaration to before struct inferior's definition, and fix comment. (inferior): Inherit from refcounted_object. Add comments. * thread.c (switch_to_thread_no_regs): Reference the thread's inferior pointer directly instead of doing a ptid lookup. (switch_to_no_thread): New function. (switch_to_thread(thread_info *)): New function, factored out from ... (switch_to_thread(ptid_t)): ... this. (restore_current_thread): Delete. (current_thread_cleanup): Remove 'inf_id' and 'was_removable' fields, and add 'inf' field. (do_restore_current_thread_cleanup): Check whether old->inf is alive instead of looking up an inferior by ptid. Use switch_to_thread and switch_to_no_thread. (restore_current_thread_cleanup_dtor): Use old->inf directly instead of lookup up an inferior by id. Decref the inferior. Don't restore 'removable'. (make_cleanup_restore_current_thread): Same the inferior pointer in old, instead of the inferior number. Incref the inferior. Don't save/clear 'removable'. gdb/testsuite/ChangeLog: 2017-04-19 Pedro Alves <palves@redhat.com> * gdb.threads/threadapply.exp (kill_and_remove_inferior): New procedure. (top level): Call it. * lib/gdb.exp (gdb_define_cmd): New procedure.
Diffstat (limited to 'gdb/testsuite/gdb.threads')
-rw-r--r--gdb/testsuite/gdb.threads/threadapply.exp135
1 files changed, 135 insertions, 0 deletions
diff --git a/gdb/testsuite/gdb.threads/threadapply.exp b/gdb/testsuite/gdb.threads/threadapply.exp
index 959e8b9..39687da 100644
--- a/gdb/testsuite/gdb.threads/threadapply.exp
+++ b/gdb/testsuite/gdb.threads/threadapply.exp
@@ -93,3 +93,138 @@ proc thr_apply_detach {thread_set} {
foreach thread_set {"all" "1.1 1.2 1.3"} {
thr_apply_detach $thread_set
}
+
+# Test killing and removing inferiors from a command run via "thread
+# apply THREAD_SET". THREAD_SET can be either "1.1", or "all". GDB
+# used to mistakenly allow deleting the previously-selected inferior,
+# in some cases, leading to crashes.
+
+proc kill_and_remove_inferior {thread_set} {
+ global binfile
+ global gdb_prompt
+
+ # The test starts multiple inferiors, therefore non-extended
+ # remote is not supported.
+ if [target_info exists use_gdb_stub] {
+ unsupported "using gdb stub"
+ return
+ }
+
+ set any "\[^\r\n\]*"
+ set ws "\[ \t\]\+"
+
+ clean_restart ${binfile}
+
+ with_test_prefix "start inferior 1" {
+ runto_main
+ }
+
+ # Add and start inferior number NUM.
+ proc add_and_start_inferior {num} {
+ global binfile
+
+ # Start another inferior.
+ gdb_test "add-inferior" "Added inferior $num.*" \
+ "add empty inferior $num"
+ gdb_test "inferior $num" "Switching to inferior $num.*" \
+ "switch to inferior $num"
+ gdb_test "file ${binfile}" ".*" "load file in inferior $num"
+
+ with_test_prefix "start inferior $num" {
+ runto_main
+ }
+ }
+
+ # Start another inferior.
+ add_and_start_inferior 2
+
+ # And yet another.
+ add_and_start_inferior 3
+
+ gdb_test "thread 2.1" "Switching to thread 2.1 .*"
+
+ # Try removing an active inferior via a "thread apply" command.
+ # Should fail/warn.
+ with_test_prefix "try remove" {
+
+ gdb_define_cmd "remove" {
+ "remove-inferiors 3"
+ }
+
+ # Inferior 3 is still alive, so can't remove it.
+ gdb_test "thread apply $thread_set remove" \
+ "warning: Can not remove active inferior 3.*"
+ # Check that GDB restored the selected thread.
+ gdb_test "thread" "Current thread is 2.1 .*"
+
+ gdb_test "info inferiors" \
+ [multi_line \
+ "${ws}1${ws}process ${any}" \
+ "\\\* 2${ws}process ${any}" \
+ "${ws}3${ws}process ${any}" \
+ ]
+ }
+
+ # Kill and try to remove inferior 2 while inferior 2 is selected.
+ # Removing the inferior should fail/warn.
+ with_test_prefix "try kill-and-remove" {
+
+ # The "inferior 1" command works around PR gdb/19318 ("kill
+ # inferior N" shouldn't switch to inferior N).
+ gdb_define_cmd "kill-and-remove" {
+ "kill inferiors 2"
+ "inferior 1"
+ "remove-inferiors 2"
+ }
+
+ # Note that when threads=1.1, this makes sure we're really
+ # testing failure to remove the inferior the user had selected
+ # before the "thread apply" command, instead of testing
+ # refusal to remove the currently-iterated inferior.
+ gdb_test "thread apply $thread_set kill-and-remove" \
+ "warning: Can not remove current inferior 2.*"
+ gdb_test "thread" "No thread selected" \
+ "switched to no thread selected"
+
+ gdb_test "info inferiors" \
+ [multi_line \
+ "${ws}1${ws}process ${any}" \
+ "\\\* 2${ws}<null>${any}" \
+ "${ws}3${ws}process ${any}" \
+ ]
+ }
+
+ # Try removing (the now dead) inferior 2 while inferior 1 is
+ # selected. This should succeed.
+ with_test_prefix "try remove 2" {
+
+ gdb_test "thread 1.1" "Switching to thread 1.1 .*"
+
+ gdb_define_cmd "remove-again" {
+ "remove-inferiors 2"
+ }
+
+ set test "thread apply $thread_set remove-again"
+ gdb_test_multiple $test $test {
+ -re "warning: Can not remove.*$gdb_prompt $" {
+ fail $test
+ }
+ -re "$gdb_prompt $" {
+ pass $test
+ }
+ }
+ gdb_test "thread" "Current thread is 1.1 .*"
+ # Check that only inferiors 1 and 3 are around.
+ gdb_test "info inferiors" \
+ [multi_line \
+ "\\\* 1${ws}process ${any}" \
+ "${ws}3${ws}process ${any}" \
+ ]
+ }
+}
+
+# Test both "all" and a thread list, because those are implemented as
+# different commands in GDB.
+foreach_with_prefix thread_set {"all" "1.1"} {
+ kill_and_remove_inferior $thread_set
+}