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
If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.
Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.
Sponsored by ActiveState
| Non-Linux FOSS: libnotify, OS X Style | Jun 18, 2013 |
| Containers—Not Virtual Machines—Are the Future Cloud | Jun 17, 2013 |
| Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer | Jun 12, 2013 |
| Weechat, Irssi's Little Brother | Jun 11, 2013 |
| One Tail Just Isn't Enough | Jun 07, 2013 |
| Introduction to MapReduce with Hadoop on Linux | Jun 05, 2013 |
- Containers—Not Virtual Machines—Are the Future Cloud
- Non-Linux FOSS: libnotify, OS X Style
- Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer
- Linux Systems Administrator
- Validate an E-Mail Address with PHP, the Right Way
- Introduction to MapReduce with Hadoop on Linux
- RSS Feeds
- Weechat, Irssi's Little Brother
- New Products
- Tech Tip: Really Simple HTTP Server with Python




2 hours 10 sec ago
2 hours 1 min ago
2 hours 46 min ago
3 hours 34 min ago
3 hours 58 min ago
5 hours 35 min ago
5 hours 37 min ago
7 hours 30 min ago
10 hours 19 min ago
15 hours 32 min ago