From 078d7dadde442e430434c230c07f86aa101c7115 Mon Sep 17 00:00:00 2001 From: HukunaMatata Date: Wed, 11 Mar 2020 20:28:50 +0800 Subject: [PATCH] fix hang in stop_all_threads --- ...hang-in-stop_all_threads-after-killi.patch | 135 ++++++++++++++++++ gdb.spec | 6 +- 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 gdb-threads-Fix-hang-in-stop_all_threads-after-killi.patch diff --git a/gdb-threads-Fix-hang-in-stop_all_threads-after-killi.patch b/gdb-threads-Fix-hang-in-stop_all_threads-after-killi.patch new file mode 100644 index 0000000..55a2987 --- /dev/null +++ b/gdb-threads-Fix-hang-in-stop_all_threads-after-killi.patch @@ -0,0 +1,135 @@ + inferior + +Consider a two-threaded testcase a.out, sleeping in both its threads: +... +$ gdb -ex r --args a.out +Reading symbols from a.out... +Starting program: /data/gdb_versions/devel/a.out +[Thread debugging using libthread_db enabled] +Using host libthread_db library "/lib64/libthread_db.so.1". +[New Thread 0x7ffff77fe700 (LWP 31268)] +... + +Typing ^C causes stop_all_threads to be executed, and if an external SIGKILL +(such as caused by killall -9 a.out) arrives at the start of stop_all_threads, +gdb hangs in stop_all_threads after giving this warning: +... +warning: unable to open /proc file '/proc/24938/status' +... + +Using "set debug infrun 1" we can see in more detail where we hang: +... +infrun: stop_all_threads +infrun: stop_all_threads, pass=0, iterations=0 +infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing +infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, need stop +infrun: target_wait (-1.0.0, status) = +infrun: 10264.10268.0 [Thread 0x7ffff77fe700 (LWP 10268)], +infrun: status->kind = signalled, signal = GDB_SIGNAL_KILL +infrun: stop_all_threads status->kind = signalled, signal = GDB_SIGNAL_KILL \ + Thread 0x7ffff77fe700 (LWP 10268) +infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing +infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping +warning: unable to open /proc file '/proc/10264/status' +infrun: target_wait (-1.0.0, status) = +infrun: -1.0.0 [process -1], +infrun: status->kind = no-resumed +infrun: infrun_async(0) +infrun: stop_all_threads status->kind = no-resumed process -1 +infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing +infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping +infrun: stop_all_threads status->kind = no-resumed process -1 +infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing +infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping +infrun: stop_all_threads status->kind = no-resumed process -1 +infrun: Thread 0x7ffff7fa6740 (LWP 10264) not executing +infrun: Thread 0x7ffff77fe700 (LWP 10268) executing, already stopping + +...... + +So, we're hanging in the 'while (1)' loop in stop_all_threads as follows: +- thread t is tested, and both t->executing and t->stop_requested are found + to be 1 (noted with 'executing, already stopping') +- consequently need_wait is set 1 +- consequently wait_one is executed +- wait_one returns a TARGET_WAITKIND_NO_RESUMED event, which is handled by + continuing at the start of the loop + +The loop actually starts with update_thread_list (), but that doesn't seem +to change the state of the threads. + +Fix the hang by: +- detecting the first sign of trouble: the TARGET_WAITKIND_SIGNALLED event + with signal GDB_SIGNAL_KILL, +- making that event pending again, +- making sure the corresponding thread will not set need_wait again + (by setting t->executing == 0) +- making sure that the corresponding thread keeps t->resumed == 1 in the + the all_non_exited_threads loop + +This results in the ^C being handled without showing the user that the +test-case was killed: +... +^C +Thread 1 received signal SIGINT, Interrupt. +0x00007ffff78c50f0 in nanosleep () from /lib64/libc.so.6 +(gdb) +... + +But a subsequent continue does show that: +... +(gdb) c +Continuing. + +Program terminated with signal SIGKILL, Killed. +The program no longer exists. +(gdb) +.... +link: https://gnutoolchain-gerrit.osci.io/r/c/binutils-gdb/+/759 + +Signed-off-by: Tom de Vries +--- + gdb/infrun.c | 20 ++++++++++++++++++-- + 1 file changed, 18 insertions(+), 2 deletions(-) + +diff --git a/gdb/infrun.c b/gdb/infrun.c +index ecd1346..3bd32c2 100644 +--- a/gdb/infrun.c ++++ b/gdb/infrun.c +@@ -4354,7 +4354,12 @@ stop_all_threads (void) + + /* The thread may be not executing, but still be + resumed with a pending status to process. */ +- t->resumed = 0; ++ if (t->suspend.waitstatus.kind == TARGET_WAITKIND_SIGNALLED ++ && t->suspend.waitstatus.value.sig == GDB_SIGNAL_KILL ++ && t->suspend.waitstatus_pending_p) ++ ; ++ else ++ t->resumed = 0; + } + } + +@@ -4369,7 +4374,18 @@ stop_all_threads (void) + + event_ptid = wait_one (&ws); + +- if (ws.kind == TARGET_WAITKIND_NO_RESUMED) ++ if (ws.kind == TARGET_WAITKIND_SIGNALLED ++ && ws.value.sig == GDB_SIGNAL_KILL) ++ { ++ thread_info *t = find_thread_ptid (event_ptid); ++ if (t != NULL) ++ { ++ save_waitstatus (t, &ws); ++ t->resumed = 1; ++ t->executing = 0; ++ } ++ } ++ else if (ws.kind == TARGET_WAITKIND_NO_RESUMED) + { + /* All resumed threads exited. */ + } +-- +2.19.1 + diff --git a/gdb.spec b/gdb.spec index 0c7c19e..5d4242a 100644 --- a/gdb.spec +++ b/gdb.spec @@ -1,6 +1,6 @@ Name: gdb Version: 8.3.1 -Release: 10 +Release: 11 License: GPLv3+ and GPLv3+ with exceptions and GPLv2+ and GPLv2+ with exceptions and GPL+ and LGPLv2+ and LGPLv3+ and BSD and Public Domain and GFDL Source: ftp://sourceware.org/pub/gdb/releases/gdb-%{version}.tar.xz @@ -169,6 +169,7 @@ Patch121: gdb-rhbz1553086-binutils-warning-loadable-section-outside-elf.patch # Patch from upstream Patch6000: gdb-detect-invalid-length-field-in-debug-frame-FDE-header.patch +Patch6001: gdb-threads-Fix-hang-in-stop_all_threads-after-killi.patch # Upstream patch end BuildRequires: rpm-libs @@ -418,6 +419,9 @@ rm -f $RPM_BUILD_ROOT%{_datadir}/gdb/python/gdb/command/backtrace.py %{_infodir}/gdb.info* %changelog +* Wed Mar 11 2020 yuxiangyang - 8.3.1-11 +- backport upstream patch to fix hang in stop_all_stop + * Mon Feb 3 2020 yuxiangyang - 8.3.1-10 - fix CVE-2017-9778 -- Gitee