Debugging Embedded Linux Platforms with GDB and Python
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)
The deadlock now becomes very clear. Thread 740 is waiting for a mutex currently owned by thread 768, and thread 768 in turn is waiting for the mutex that thread 740 currently owns. Neither thread can run until the mutex owned by the other becomes available. Returning to the GDB prompt, we can generate backtraces for both threads to gain more insight:
(gdb) t 30 [Switching to thread 30 (Thread 740)] #0 0x2aac1068 in __lll_lock_wait () (gdb) bt #0 0x2aac1068 in __lll_lock_wait () #1 0x2aaba568 in pthread_mutex_lock () #2 0x00400970 in good_printer (data=0x0) at hello_world.c:45 #3 0x2aab7f9c in start_thread () #4 0x2aac2200 in __thread_start () Backtrace stopped: frame did not save the PC (gdb) t 2 [Switching to thread 2 (Thread 768)] #0 0x2aac1068 in __lll_lock_wait () (gdb) bt #0 0x2aac1068 in __lll_lock_wait () #1 0x2aaba568 in pthread_mutex_lock () #2 0x00400a04 in bad_printer (data=0x0) at hello_world.c:60 #3 0x2aab7f9c in start_thread () #4 0x2aac2200 in __thread_start () Backtrace stopped: frame did not save the PC (gdb)
As the backtraces show, the two threads have followed two different code paths to end up in the deadlock situation. Reviewing the code for hello_world in light of this information should allow us to find the bug: bad_printer() is taking the print and statistics locks in the wrong order.
Adding a Python API to GDB provides another capable weapon in the Linux debugging arsenal. For embedded systems, where other debugging tools may not be so widely available, a powerful programmatic interface to GDB can make the difference between hours of painstaking debugging and minutes of enjoyable scripting.
Astute readers will have noted that the bug we have discovered in this article is not the only bug in hello_world.c. The task of finding and fixing the remaining bugs is left as an exercise for readers to tackle with their new-found GDB Python knowledge. Have fun!
MIPS Registers
The MIPS architecture has 32 general-purpose integer registers. Of these, the hardware architecture specifies that registers 0 and 31 are used for the value zero and the function return address, respectively. The usage of the rest of the registers is entirely defined by the software toolchain.
By convention, however, the use of the general-purpose MIPS registers is quite firmly set to allow software interoperability. For example, registers 4 to 7 are used to pass the first four non-floating-point arguments to functions and are given the names a0 to a3.
Resources
GDB/Python Wiki: sourceware.org/gdb/wiki/PythonGdb
Tom Tromey's Excellent Blog Posts about GDB and Python: tromey.com/blog/?cat=17
OpenWrt's GDB Cross-Compilation Makefile: https://dev.openwrt.org/browser/trunk/toolchain/gdb/Makefile
A How-To for GDB/gdbserver Usage: www.linux.com/archive/feature/121735
uClibc Project: uclibc.org
Linux Futex Information: kernel.org/doc/man-pages/online/pages/man2/futex.2.html
Tom Parkin (tom.parkin@gmail.com) has been working with Linux and embedded systems for ten years and is still finding new things to get excited about. When not in front of a computer, he enjoys 10k runs and Real Ale, although not in combination.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Using Salt Stack and Vagrant for Drupal Development
- Validate an E-Mail Address with PHP, the Right Way
- Build a Skype Server for Your Home Phone System
- Tech Tip: Really Simple HTTP Server with Python
- Why Python?
- A Topic for Discussion - Open Source Feature-Richness?




2 hours 19 min ago
6 hours 6 min ago
6 hours 14 min ago
8 hours 29 min ago
10 hours 59 min ago
21 hours 2 min ago
1 day 1 hour ago
1 day 5 hours ago
1 day 5 hours ago
1 day 8 hours ago