From 90dbfce36bd096692264bbfc7d1651ced8f140b3 Mon Sep 17 00:00:00 2001 From: Philippe Waroquiers Date: Tue, 8 Aug 2017 21:15:45 +0000 Subject: [PATCH] Ensure host stack trace has better chance to work when valgrind is exiting When investigating bug 383275, the host stacktrace was containing only one IP. This is because the tid corresponding to the lwpid is dead, and so no valid thread state was returned. This then gave a rubbish stacktop of 0, which means unwinding stops at first frame. So, try harder to find a valid thread state when reporting the host stacktrace. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@16470 --- coregrind/m_libcassert.c | 2 +- coregrind/m_threadstate.c | 14 ++++++++++++++ coregrind/pub_core_threadstate.h | 7 +++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/coregrind/m_libcassert.c b/coregrind/m_libcassert.c index a6f3069681..63fb64b240 100644 --- a/coregrind/m_libcassert.c +++ b/coregrind/m_libcassert.c @@ -343,7 +343,7 @@ static void show_sched_status_wrk ( Bool host_stacktrace, Addr ips[BACKTRACE_DEPTH]; Int n_ips; ThreadState *tst - = VG_(get_ThreadState)( VG_(lwpid_to_vgtid)( VG_(gettid)() ) ); + = VG_(get_ThreadState)( VG_(lwpid_to_vgtid_dead_ok)(VG_(gettid)())); // If necessary, fake up an ExeContext which is of our actual real CPU // state. Could cause problems if we got the panic/exception within the diff --git a/coregrind/m_threadstate.c b/coregrind/m_threadstate.c index 6edf2260fc..cba4ef8695 100644 --- a/coregrind/m_threadstate.c +++ b/coregrind/m_threadstate.c @@ -177,6 +177,20 @@ ThreadId VG_(lwpid_to_vgtid)(Int lwp) return VG_INVALID_THREADID; } +ThreadId VG_(lwpid_to_vgtid_dead_ok)(Int lwp) +{ + ThreadId tid = VG_(lwpid_to_vgtid)(lwp); + + if (tid != VG_INVALID_THREADID) + return tid; + + for(tid = 1; tid < VG_N_THREADS; tid++) + if (VG_(threads)[tid].os_state.lwpid == lwp) + return tid; + + return VG_INVALID_THREADID; +} + /*--------------------------------------------------------------------*/ /*--- end ---*/ /*--------------------------------------------------------------------*/ diff --git a/coregrind/pub_core_threadstate.h b/coregrind/pub_core_threadstate.h index 19a998246c..c62429e36a 100644 --- a/coregrind/pub_core_threadstate.h +++ b/coregrind/pub_core_threadstate.h @@ -466,6 +466,13 @@ extern Int VG_(count_runnable_threads)(void); ThreadId */ extern ThreadId VG_(lwpid_to_vgtid)(Int lwpid); + +/* Same as VG_(lwpid_to_vgtid), but if no corresponding living thread is found, + searches also in dead threads. + This can be used when the tid is exiting, but the corresponding + lwpid is still running. */ +extern ThreadId VG_(lwpid_to_vgtid_dead_ok)(Int lwpid); + #endif // __PUB_CORE_THREADSTATE_H /*--------------------------------------------------------------------*/ -- 2.47.2