From patchwork Thu Jan 15 18:33:16 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kratochvil X-Patchwork-Id: 4709 Received: (qmail 10235 invoked by alias); 15 Jan 2015 18:33:29 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 10207 invoked by uid 89); 15 Jan 2015 18:33:26 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.9 required=5.0 tests=AWL, BAYES_00, SPF_HELO_PASS, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Thu, 15 Jan 2015 18:33:22 +0000 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id t0FIXKlL020347 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Thu, 15 Jan 2015 13:33:20 -0500 Received: from host2.jankratochvil.net (ovpn-116-51.ams2.redhat.com [10.36.116.51]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t0FIXGSZ024433 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO) for ; Thu, 15 Jan 2015 13:33:19 -0500 Date: Thu, 15 Jan 2015 19:33:16 +0100 From: Jan Kratochvil To: gdb-patches@sourceware.org Subject: [patch] Sort threads for thread apply all (bt) Message-ID: <20150115183316.GA16405@host2.jankratochvil.net> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes Hi, downstream Fedora request: Please make it easier to find the backtrace of the crashing thread https://bugzilla.redhat.com/show_bug.cgi?id=1024504 Currently after loading a core file GDB prints: Core was generated by `./threadcrash1'. Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000000000040062f in start (arg=0x0) at threadcrash1.c:8 8 *(volatile int *)0=0; (gdb) _ there is nowhere seen which of the threads had crashed. In reality GDB always numbers that thread as #1 and it is the current thread that time. But after dumping all the info into a file for later analysis it is no longer obvious. 'thread apply all bt' even puts the thread #1 to the _end_ of the output!!! Should GDB always print after loading a core file what "thread" command would print? [Current thread is 1 (Thread 0x7fcbe28fe700 (LWP 15453))] Or should the "thread" output be printed only in non-interactive mode? Or something different? Or is the current behavior OK as is and the tools calling GDB in batch mode should indicate the thread #1 on their own? ------------------------------------------------------------------------------ I find maybe as good enough and with no risk of UI change flamewar to just sort the threads by their number. Currently they are printed as they happen in the internal GDB list which has no advantage. Printing thread #1 as the first one with assumed 'thread apply all bt' (after the core file is loaded) should make the complaint resolved I guess. No regressions on {x86_64,x86_64-m32,i686}-fedora22pre-linux-gnu. Jan diff of crashing different thread in 2-threaded program: ------------------------------------------------------------------------------ -Reading symbols from /home/jkratoch/t/threadcrash1...done. -[New LWP 15453] -[New LWP 15446] +Reading symbols from /home/jkratoch/t/threadcrash2...done. +[New LWP 29285] +[New LWP 29288] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib64/libthread_db.so.1". -Core was generated by `./threadcrash1'. +Core was generated by `./threadcrash2'. Program terminated with signal SIGSEGV, Segmentation fault. -#0 0x000000000040062f in start (arg=0x0) at threadcrash1.c:8 -8 *(volatile int *)0=0; +#0 0x0000000000400684 in main () at threadcrash2.c:19 +19 *(volatile int *)0=0; (gdb) thread apply all bt -Thread 2 (Thread 0x7fcbe2900700 (LWP 15446)): +Thread 2 (Thread 0x7feb9c14c700 (LWP 29288)): Python Exception No module named gdb.frames: #0 0x0000003424ec491d in nanosleep () at ../sysdeps/unix/syscall-template.S:81 #1 0x0000003424ec47b4 in __sleep (seconds=0) at ../sysdeps/unix/sysv/linux/sleep.c:138 -#2 0x000000000040068a in main () at threadcrash1.c:19 +#2 0x000000000040062a in start (arg=0x0) at threadcrash2.c:7 +#3 0x000000342560752a in start_thread (arg=0x7feb9c14c700) at pthread_create.c:310 +#4 0x0000003424f0079d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109 -Thread 1 (Thread 0x7fcbe28fe700 (LWP 15453)): +Thread 1 (Thread 0x7feb9c14e700 (LWP 29285)): Python Exception No module named gdb.frames: -#0 0x000000000040062f in start (arg=0x0) at threadcrash1.c:8 -#1 0x000000342560752a in start_thread (arg=0x7fcbe28fe700) at pthread_create.c:310 -#2 0x0000003424f0079d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109 +#0 0x0000000000400684 in main () at threadcrash2.c:19 (gdb) thread -[Current thread is 1 (Thread 0x7fcbe28fe700 (LWP 15453))] +[Current thread is 1 (Thread 0x7feb9c14e700 (LWP 29285))] (gdb) q gdb/ChangeLog 2015-01-15 Jan Kratochvil * thread.c (tp_array_compar): New function. (thread_apply_all_command): Sort tp_array using it. #include #include #include static void *start (void *arg) { sleep (1); *(volatile int *)0=0; return arg; } int main (void) { pthread_t thread1; int i; i = pthread_create (&thread1, NULL, start, NULL); assert (i == 0); sleep (100); i = pthread_join (thread1, NULL); assert (i == 0); return 0; } #include #include #include static void *start (void *arg) { sleep (100); return arg; } int main (void) { pthread_t thread1; int i; i = pthread_create (&thread1, NULL, start, NULL); assert (i == 0); sleep (1); *(volatile int *)0=0; i = pthread_join (thread1, NULL); assert (i == 0); return 0; } diff --git a/gdb/thread.c b/gdb/thread.c index ed20fbe..cdab5d8 100644 --- a/gdb/thread.c +++ b/gdb/thread.c @@ -1382,6 +1382,17 @@ make_cleanup_restore_current_thread (void) restore_current_thread_cleanup_dtor); } +/* Sort an array for struct thread_info pointers by their ascending NUM. */ + +static int +tp_array_compar (const void *ap_voidp, const void *bp_voidp) +{ + const struct thread_info *const *ap = ap_voidp; + const struct thread_info *const *bp = bp_voidp; + + return ((*ap)->num > (*bp)->num) - ((*ap)->num < (*bp)->num); +} + /* Apply a GDB command to a list of threads. List syntax is a whitespace seperated list of numbers, or ranges, or the keyword `all'. Ranges consist of two numbers seperated by a hyphen. Examples: @@ -1431,6 +1442,8 @@ thread_apply_all_command (char *cmd, int from_tty) i++; } + qsort (tp_array, i, sizeof (*tp_array), tp_array_compar); + make_cleanup (set_thread_refcount, &ta_cleanup); for (k = 0; k != i; k++)