Listing 2. Python Code for GDB Mutex Debugging from collections import defaultdict # A dictionary of mutex:owner mutexOwners = {} # A dictionary of blocking mutex:(threads..) threadBlockers = defaultdict(list) # Print the threads print "Process threads : " gdb.execute("info threads") print "Analysing mutexes..." # Step through processes running under gdb for process in gdb.inferiors(): # Step through each thread in the process for thread in process.threads(): # Examine the thread -- is it blocking on a mutex? thread.switch() frame = gdb.selected_frame() if frame.name() == "__lll_lock_wait": # a0 is the first argument passed to the function a0 = gdb.parse_and_eval("$a0") mutex = int(a0) # Make a note of which thread blocks on which mutex threadBlockers[mutex].append(thread) # Make a note of which thread owns this mutex if not mutex in mutexOwners: s = gdb.execute("x/4d $a0", to_string=True).split() s.reverse() mutexOwners[mutex] = int(s[1]) # Print the results of the analysis for mutex in mutexOwners: print " Mutex 0x%x :" % mutex print " -> held by thread : %d" % mutexOwners[mutex] s = ["%d" % t.ptid[2] for t in threadBlockers[mutex]] print " -> blocks threads : %s" % ' '.join(s)