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.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
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
| 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 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- RSS Feeds
- New Products
- Using Salt Stack and Vagrant for Drupal Development
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Validate an E-Mail Address with PHP, the Right Way
- New Products
- Tech Tip: Really Simple HTTP Server with Python




3 hours 21 min ago
9 hours 20 min ago
9 hours 43 min ago
9 hours 53 min ago
9 hours 57 min ago
10 hours 27 min ago
13 hours 19 min ago
13 hours 54 min ago
13 hours 55 min ago
13 hours 56 min ago