Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can GDB display a list of pthread mutexes held by each thread?

I have GDB attached to a deadlocked application written with pthreads. There are ~10 threads that are all blocked, and I'd like to know which locks are held by which threads. This is possible in WinDbg using SOS.dll; is this possible in GDB?

like image 737
Mark P Neyer Avatar asked Oct 21 '10 01:10

Mark P Neyer


People also ask

How do I see all threads in GDB?

Use the "info threads" command to see the IDs of currently known threads. The GDB thread debugging facility allows you to observe all threads while your program runs--but whenever GDB takes control, one thread in particular is always the focus of debugging. This thread is called the current thread.

What is Pthread_mutex?

A pthread mutex is a structure of type pthread_mutex_t that implement a behavior based on the Pthread mutexes. An MI mutex is a structure built into the machine that implement a similar sort of serialization construct. The maximum number of recursive locks by the owning thread is 32,767.

How do you use mutexes?

A mutex is initialized in the beginning of the main function. The same mutex is locked in the 'doSomeThing()' function while using the shared resource 'counter' At the end of the function 'doSomeThing()' the same mutex is unlocked. At the end of the main function when both the threads are done, the mutex is destroyed.


2 Answers

On at least one flavor of Linux, the C++11 std::mutex has a member called __owner which contains the thread id of the thread that currently has the mutex locked. Using "info threads" in gdb shows the thread numbers used by gdb along with the thread ids (see the "LWP" number), allowing you to switch to that thread ("thread N") and then examine the call stack ("backtrace").

like image 192
michael Avatar answered Sep 24 '22 17:09

michael


It's not GDB you should be asking about, but rather the specific pthread library and OS you are using.

The pthread library implements mutexes in cooperation with the kernel via some set of system calls. If its implementation of mutexes embeds something to tie the last thread holding the mutex into the mutex data structure, then you can use GDB to get at that information.

It is possible your kernel tracks that information. Under Mac OS X, for example, the collection of GDB scripts bundled with the kernel debugging kit, kgmacros, includes a command showallmtx that will do exactly what you want. The catch: to use it, you have to be debugging the machine's kernel at the time, which means you need to be doing the debugging using a different machine.

Of course, you might have a /dev/kmem device file, which would let you poke around in the kernel's memory and access the necessary data structure, provided you can locate it.

But this all really depends on your system - your pthread library and OS kernel - not on GDB.

You could also try creating a mutex of type PTHREAD_MUTEX_ERRORCHECK; this will cause pthread_mutex_lock() to return EDEADLK instead of deadlocking. You can then break when that occurs and root around in your non-deadlocked process.

like image 31
Jeremy W. Sherman Avatar answered Sep 26 '22 17:09

Jeremy W. Sherman