Add pretty printers for the NPTL lock types

Message ID 1431716828-12854-1-git-send-email-martin.galvan@tallertechnologies.com
State Superseded
Headers

Commit Message

Martin Galvan May 15, 2015, 7:07 p.m. UTC
  This patch adds the pretty-printers for the following NPTL types:

- pthread_mutex_t
- pthread_mutexattr_t
- pthread_cond_t
- pthread_condattr_t
- pthread_rwlock_t
- pthread_rwlockattr_t

To load the pretty-printers into your gdb session, do:

source printers.py

You can check which printers are registered and enabled by issuing the
'info pretty-printer' gdb command. Printers should trigger automatically when
trying to print a variable of one of the types mentioned above.

I spent a lot of time trying to change the install target inside the
NPTL Makefile so that the printers would be automatically installed to
$datadir/gdb/auto-load, and properly renamed to libpthread(version)-gdb.py.
Since I didn't succeed you'll have to install them manually; it would be great
if someone who's more familiar with the build system could add it to the Makefile.

These printers are architecture-independent. However, most of the C macros
defined in nptl/pthread.h and nptl/pthreadP.h had to be replicated in the Python
code as global variables as there was no other way to check which flags were
set.

The printers were tested on both the gdb CLI and Eclipse CDT.

I have a company-wide copyright assignment for glibc. I don't have write access,
though, so if anyone could commit this for me it would be great.

ChangeLog:
2015-05-15  Martin Galvan  <martin.galvan@tallertechnologies.com>
	* nptl/printers.py: New file.
---
 nptl/printers.py | 744 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 744 insertions(+)
 create mode 100644 nptl/printers.py
  

Comments

Joseph Myers May 15, 2015, 8:42 p.m. UTC | #1
On Fri, 15 May 2015, Martin Galvan wrote:

> These printers are architecture-independent. However, most of the C macros
> defined in nptl/pthread.h and nptl/pthreadP.h had to be replicated in the Python
> code as global variables as there was no other way to check which flags were
> set.

I think we must eliminate this duplication before this patch can go in.  
That is, set up some mechanism for the values to be extracted at build / 
install time.  In addition, whenever this depends on some aspect of NPTL 
internals, there need to be comments on the relevant internals (e.g. 
structure field definitions) explaining how this code depends on them and 
so needs updating for any change.  Similarly, if this code is meant to 
handle all values in an enumeration / all flags from some set of flags, 
there needs to be a comment on that enumeration / set of flags drawing 
attention to the need to update this code when new values are added.

See the existing gen-as-const support for how compile-time values can get 
extracted for use in other contexts (there, values such as structure 
offsets get extracted for #defines for use in .S files, but much the same 
approach could extract header constants for use in Python code - though 
watch out for any signedness issues).  It's also possible to test-compile 
a sequence of programs to determine the value and type of an integer 
constant expression using the compiler.

(I have not otherwise reviewed this patch.)
  
Joseph Myers May 15, 2015, 8:54 p.m. UTC | #2
On Fri, 15 May 2015, Martin Galvan wrote:

> +# Value of __mutex for shared condvars.
> +PTHREAD_COND_SHARED = ~ctypes.c_long(0).value
> +
> +# Value of __total_seq for destroyed condvars.
> +PTHREAD_COND_DESTROYED = ctypes.c_ulonglong(-1).value

Another issue: ctypes relates, I presume, to the types used by the 
compiler used to compile Python.  But what you want here is the types used 
in glibc by the program being debugged, which may be different, both 
because of cross-debugging, and because a native 64-bit GDB may debug both 
32-bit and 64-bit inferiors.

That is, you can't compute these using ctypes; they have to be computed 
using the types for the current inferior.  I don't know enough about the 
GDB Python interfaces to know whether what you have elsewhere in this 
patch

> +PTHREAD_MUTEX_INCONSISTENT = gdb.parse_and_eval('~0u >> 1')

is safe (whether this Python code will be loaded separately for 32-bit and 
64-bit inferiors and that evaluation, taking place when this module is 
loaded, will use the correct type sizes for the inferior in question).  
If it is safe, I'd expect it to be possible to use the same approach for 
PTHREAD_COND_*.  (Of course, if you're relying on a value like this that 
isn't a constant defined in and extracted from a header, the relevant 
glibc code needs a comment to point out this dependency.  Or, better, 
define the value as a macro in an internal header, use that macro where 
glibc depends on the value, and extract from the internal header for 
Python use.)
  
Martin Galvan May 15, 2015, 10:46 p.m. UTC | #3
Hi Joseph! Thanks a lot for the feedback.

On Fri, May 15, 2015 at 5:42 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> I think we must eliminate this duplication before this patch can go in.
> That is, set up some mechanism for the values to be extracted at build /
> install time.  In addition, whenever this depends on some aspect of NPTL
> internals, there need to be comments on the relevant internals (e.g.
> structure field definitions) explaining how this code depends on them and
> so needs updating for any change.  Similarly, if this code is meant to
> handle all values in an enumeration / all flags from some set of flags,
> there needs to be a comment on that enumeration / set of flags drawing
> attention to the need to update this code when new values are added.

Wouldn't commenting on the headers suffice? Those values are scattered
all over the place. I *could* try to build an additional Python script
to extract them from the headers somehow, but it's probably gonna take
a lot of time.

> Another issue: ctypes relates, I presume, to the types used by the
> compiler used to compile Python.  But what you want here is the types used
> in glibc by the program being debugged, which may be different, both
> because of cross-debugging, and because a native 64-bit GDB may debug both
> 32-bit and 64-bit inferiors.
>
> That is, you can't compute these using ctypes; they have to be computed
> using the types for the current inferior.  I don't know enough about the
> GDB Python interfaces to know whether what you have elsewhere in this
> patch

Understood.

>> +PTHREAD_MUTEX_INCONSISTENT = gdb.parse_and_eval('~0u >> 1')
>
> is safe (whether this Python code will be loaded separately for 32-bit and
> 64-bit inferiors and that evaluation, taking place when this module is
> loaded, will use the correct type sizes for the inferior in question).

It should be.

> If it is safe, I'd expect it to be possible to use the same approach for
> PTHREAD_COND_*.

Probably. I'll give it a try and send v2.

> (Of course, if you're relying on a value like this that
> isn't a constant defined in and extracted from a header, the relevant
> glibc code needs a comment to point out this dependency.  Or, better,
> define the value as a macro in an internal header, use that macro where
> glibc depends on the value, and extract from the internal header for
> Python use.)

These values are hardcoded in many places. While I agree that it would
be good to define them as macros, I think improving the glibc coding
style is beyond the scope of this particular contribution :)

Again, thanks a lot for the feedback. I'll wait until someone else
takes a look at the patch and suggests more changes/improvements, then
I'll send v2.
  
Martin Galvan May 15, 2015, 11:02 p.m. UTC | #4
On Fri, May 15, 2015 at 7:46 PM, Martin Galvan
<martin.galvan@tallertechnologies.com> wrote:
> Hi Joseph! Thanks a lot for the feedback.
> On Fri, May 15, 2015 at 5:42 PM, Joseph Myers <joseph@codesourcery.com> wrote:
>> I think we must eliminate this duplication before this patch can go in.
>> That is, set up some mechanism for the values to be extracted at build /
>> install time.  In addition, whenever this depends on some aspect of NPTL
>> internals, there need to be comments on the relevant internals (e.g.
>> structure field definitions) explaining how this code depends on them and
>> so needs updating for any change.  Similarly, if this code is meant to
>> handle all values in an enumeration / all flags from some set of flags,
>> there needs to be a comment on that enumeration / set of flags drawing
>> attention to the need to update this code when new values are added.
>
> Wouldn't commenting on the headers suffice? Those values are scattered
> all over the place. I *could* try to build an additional Python script
> to extract them from the headers somehow, but it's probably gonna take
> a lot of time.

Actually, disregard that. I took a look at how gen-as-const seems to
work, and noticed each directory is used on has a .sym file with all
the macros and such neatly set for awk to do its thing. Silly me, I
thought you were asking me to extract them from the .h files
themselves.

What I'm gonna do is, import a macros.py file in printers.py.
macros.py will in turn be generated from a .sym file where I'll place
all the macros/enums I need. Then I'll add some comments pointing out
that any updates to the headers should be reflected on the .sym file.

The only hassle here will be integrating this to the NPTL Makefile,
though, but I'll see what I can do.
  
Martin Galvan May 15, 2015, 11:51 p.m. UTC | #5
On Fri, May 15, 2015 at 8:02 PM, Martin Galvan
<martin.galvan@tallertechnologies.com> wrote:
> Actually, disregard that. I took a look at how gen-as-const seems to
> work, and noticed each directory is used on has a .sym file with all
> the macros and such neatly set for awk to do its thing. Silly me, I
> thought you were asking me to extract them from the .h files
> themselves.
>
> What I'm gonna do is, import a macros.py file in printers.py.
> macros.py will in turn be generated from a .sym file where I'll place
> all the macros/enums I need. Then I'll add some comments pointing out
> that any updates to the headers should be reflected on the .sym file.

Come to think about it, though, I don't see what would be the real
advantage of doing this over simply updating printers.py. In both
cases the maintainer would have to update both the header and a
separate file that'll be used by the printers. How exactly does
gen-as-const work?
  
Joseph Myers May 18, 2015, 4:35 p.m. UTC | #6
On Fri, 15 May 2015, Martin Galvan wrote:

> On Fri, May 15, 2015 at 8:02 PM, Martin Galvan
> <martin.galvan@tallertechnologies.com> wrote:
> > Actually, disregard that. I took a look at how gen-as-const seems to
> > work, and noticed each directory is used on has a .sym file with all
> > the macros and such neatly set for awk to do its thing. Silly me, I
> > thought you were asking me to extract them from the .h files
> > themselves.
> >
> > What I'm gonna do is, import a macros.py file in printers.py.
> > macros.py will in turn be generated from a .sym file where I'll place
> > all the macros/enums I need. Then I'll add some comments pointing out
> > that any updates to the headers should be reflected on the .sym file.
> 
> Come to think about it, though, I don't see what would be the real
> advantage of doing this over simply updating printers.py. In both

It avoids having two copies of the values you need to make sure stay in 
sync.

> cases the maintainer would have to update both the header and a
> separate file that'll be used by the printers. How exactly does
> gen-as-const work?

It generates a C source file that uses the C constant as an asm operand, 
in an asm containing special text that can then be matched in the compiler 
output to extract the value of a constant.
  
Martin Galvan May 19, 2015, 1:01 p.m. UTC | #7
On Mon, May 18, 2015 at 1:35 PM, Joseph Myers <joseph@codesourcery.com> wrote:
> On Fri, 15 May 2015, Martin Galvan wrote:
>
>> On Fri, May 15, 2015 at 8:02 PM, Martin Galvan
>> <martin.galvan@tallertechnologies.com> wrote:
>> > Actually, disregard that. I took a look at how gen-as-const seems to
>> > work, and noticed each directory is used on has a .sym file with all
>> > the macros and such neatly set for awk to do its thing. Silly me, I
>> > thought you were asking me to extract them from the .h files
>> > themselves.
>> >
>> > What I'm gonna do is, import a macros.py file in printers.py.
>> > macros.py will in turn be generated from a .sym file where I'll place
>> > all the macros/enums I need. Then I'll add some comments pointing out
>> > that any updates to the headers should be reflected on the .sym file.
>>
>> Come to think about it, though, I don't see what would be the real
>> advantage of doing this over simply updating printers.py. In both
>
> It avoids having two copies of the values you need to make sure stay in
> sync.

I see. I guess I was wrong on my initial assumption.

>> cases the maintainer would have to update both the header and a
>> separate file that'll be used by the printers. How exactly does
>> gen-as-const work?
>
> It generates a C source file that uses the C constant as an asm operand,
> in an asm containing special text that can then be matched in the compiler
> output to extract the value of a constant.

So if I understood correctly, what you're asking me to do is to take
the macros out of the header files, place them in a .sym file and have
a script generate both a .h and a .py with those values?

I guess it would be relatively simple if only plain #defines were
involved. Here we have #defines, enums (some of whose values are
bitwise combination of other enum constants) and hardcoded values all
over the place. It sounds to me like I'd have to change quite a few
files from glibc, which I think is beyond the scope of this
contribution.

Wouldn't be enough to comment on the headers saying any updates should
be reflected in printers.py?
  
Joseph Myers May 19, 2015, 1:29 p.m. UTC | #8
On Tue, 19 May 2015, Martin Galvan wrote:

> > It generates a C source file that uses the C constant as an asm operand,
> > in an asm containing special text that can then be matched in the compiler
> > output to extract the value of a constant.
> 
> So if I understood correctly, what you're asking me to do is to take
> the macros out of the header files, place them in a .sym file and have
> a script generate both a .h and a .py with those values?

No.  The numerical values stay in the .h files.  The .sym file contains 
only the names, and the gen-as-const machinery uses the compiler to 
extract the corresponding values.  The only header changes needed are to 
comments, to say that if the *set of values in the enum (etc.)* changes 
then the pretty printers should be updated.
  

Patch

diff --git a/nptl/printers.py b/nptl/printers.py
new file mode 100644
index 0000000..fdab6e8
--- /dev/null
+++ b/nptl/printers.py
@@ -0,0 +1,744 @@ 
+# Copyright (C) 2015 Free Software Foundation, Inc.
+# This file is part of the GNU C Library.
+# Contributed by Martin Galvan <martin.galvan@tallertechnologies.com>
+#
+# The GNU C Library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# The GNU C Library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with the GNU C Library; if not, see
+# <http://www.gnu.org/licenses/>.
+
+"""Pretty printers for the NTPL lock types.
+
+This file contains the gdb pretty printers for the following types:
+
+    * pthread_mutex_t
+    * pthread_mutexattr_t
+    * pthread_cond_t
+    * pthread_condattr_t
+    * pthread_rwlock_t
+    * pthread_rwlockattr_t
+
+You can check which printers are registered and enabled by issuing the
+'info pretty-printer' gdb command. Printers should trigger automatically when
+trying to print a variable of one of the types mentioned above.
+"""
+
+import sys
+import re
+import ctypes
+import gdb
+
+class _IteratorP3(object):
+    """A simple Iterator class."""
+
+    def __init__(self, values):
+        self.values = values
+        self.length = len(self.values)
+        self.count = 0
+
+    def __iter__(self):
+        return self
+
+    def __next__(self):
+        count = self.count
+        self.count += 1
+
+        if count == self.length:
+            raise StopIteration
+        else:
+            return self.values[count]
+
+class _IteratorP2(_IteratorP3):
+    """Hack for Python 2 compatibilty."""
+
+    def next(self):
+        self.__next__()
+
+# Hack for Python 2 compatibilty
+if sys.version_info[0] > 2:
+    Iterator = _IteratorP3
+else:
+    Iterator = _IteratorP2
+
+################################################################################
+
+# These variables correspond to the macros defined in nptl/pthread.h and
+# nptl/pthreadP.h. Unfortunately, as those values are hardcoded in the glibc
+# code, updating the macros will require updating these variables as well.
+
+# Mutex types
+PTHREAD_MUTEX_KIND_MASK = 0x3
+PTHREAD_MUTEX_NORMAL = 0
+PTHREAD_MUTEX_RECURSIVE = 1
+PTHREAD_MUTEX_ERRORCHECK = 2
+PTHREAD_MUTEX_ADAPTIVE_NP = 3
+
+# Mutex status
+PTHREAD_MUTEX_DESTROYED = -1
+PTHREAD_MUTEX_UNLOCKED = 0
+PTHREAD_MUTEX_LOCKED_NO_WAITERS = 1
+
+# INT_MAX for 2's complement CPUs
+PTHREAD_MUTEX_INCONSISTENT = gdb.parse_and_eval('~0u >> 1')
+PTHREAD_MUTEX_NOTRECOVERABLE = PTHREAD_MUTEX_INCONSISTENT - 1
+
+# For robust mutexes
+FUTEX_OWNER_DIED = 0x40000000
+
+# For robust and PI mutexes
+FUTEX_WAITERS = 0x80000000
+FUTEX_TID_MASK = 0x3FFFFFFF
+
+# Mutex attributes
+PTHREAD_MUTEX_ROBUST_NORMAL_NP = 0x10
+PTHREAD_MUTEX_PRIO_INHERIT_NP = 0x20
+PTHREAD_MUTEX_PRIO_PROTECT_NP = 0x40
+PTHREAD_MUTEX_PSHARED_BIT = 0x80
+PTHREAD_MUTEX_PRIO_CEILING_SHIFT = 19
+PTHREAD_MUTEX_PRIO_CEILING_MASK = 0x7FF80000
+
+mutexTypesDict = {
+    PTHREAD_MUTEX_NORMAL: ('Type', 'Normal'),
+    PTHREAD_MUTEX_RECURSIVE: ('Type', 'Recursive'),
+    PTHREAD_MUTEX_ERRORCHECK: ('Type', 'Error check'),
+    PTHREAD_MUTEX_ADAPTIVE_NP: ('Type', 'Adaptive')
+}
+
+class MutexPrinter(object):
+    """Pretty printer for pthread_mutex_t."""
+
+    def __init__(self, mutex):
+        """Initialize the printer's internal data structures.
+
+        Args:
+            mutex: A gdb.value representing a pthread_mutex_t.
+        """
+
+        data = mutex['__data']
+        self.lock = data['__lock']
+        self.count = data['__count']
+        self.owner = data['__owner']
+        self.kind = data['__kind']
+        self.values = []
+        self.readValues()
+
+    def to_string(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_mutex_t.
+        """
+
+        return 'pthread_mutex_t'
+
+    def children(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_mutex_t.
+        """
+
+        return Iterator(self.values)
+
+    def readValues(self):
+        """Read the mutex's info and store it in self.values.
+
+        The data contained in self.values will be returned by the Iterator
+        created in self.children.
+        """
+
+        self.readType()
+        self.readStatus()
+        self.readAttributes()
+        self.readMiscInfo()
+
+    def readType(self):
+        """Read the mutex's type."""
+
+        mutexType = self.kind & PTHREAD_MUTEX_KIND_MASK
+
+        # mutexType must be casted to int because it's a gdb.Value
+        self.values.append(mutexTypesDict[int(mutexType)])
+
+    def readStatus(self):
+        """Read the mutex's status.
+
+        For architectures which support lock elision, this method reads
+        whether the mutex is actually locked (i.e. it may show it as unlocked
+        even after calling pthread_mutex_lock).
+        """
+
+        if self.kind == PTHREAD_MUTEX_DESTROYED:
+            self.values.append(('Status', 'Destroyed'))
+        elif self.kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP:
+            self.readStatusRobust()
+        else:
+            self.readStatusNonRobust()
+
+    def readStatusRobust(self):
+        """Read the status of a robust mutex.
+
+        In glibc robust mutexes are implemented in a very different way than
+        non-robust ones. This method reads their locking status,
+        whether it may have waiters, their registered owner (if any),
+        whether the owner is alive or not, and the status of the state
+        they're protecting.
+        """
+
+        if self.lock == PTHREAD_MUTEX_UNLOCKED:
+            self.values.append(('Status', 'Unlocked'))
+        else:
+            if self.lock & FUTEX_WAITERS:
+                self.values.append(('Status', 'Locked, possibly with waiters'))
+            else:
+                self.values.append(('Status', 'Locked, no waiters'))
+
+            if self.lock & FUTEX_OWNER_DIED:
+                self.values.append(('Owner ID', '%d (dead)' % self.owner))
+            else:
+                self.values.append(('Owner ID', self.lock & FUTEX_TID_MASK))
+
+        if self.owner == PTHREAD_MUTEX_INCONSISTENT:
+            self.values.append(('State protected by this mutex',
+                                'Inconsistent'))
+        elif self.owner == PTHREAD_MUTEX_NOTRECOVERABLE:
+            self.values.append(('State protected by this mutex',
+                                'Not recoverable'))
+
+    def readStatusNonRobust(self):
+        """Read the status of a non-robust mutex.
+
+        Read info on whether the mutex is locked, if it may have waiters
+        and its owner (if any).
+        """
+
+        if self.lock == PTHREAD_MUTEX_UNLOCKED:
+            self.values.append(('Status', 'Unlocked'))
+        else:
+            if self.kind & PTHREAD_MUTEX_PRIO_INHERIT_NP:
+                waiters = self.lock & FUTEX_WAITERS
+                owner = self.lock & FUTEX_TID_MASK
+            else:
+                # Mutex protocol is PP or none
+                waiters = (self.lock != PTHREAD_MUTEX_LOCKED_NO_WAITERS)
+                owner = self.owner
+
+            if waiters:
+                self.values.append(('Status', 'Locked, possibly with waiters'))
+            else:
+                self.values.append(('Status', 'Locked, no waiters'))
+
+            self.values.append(('Owner ID', owner))
+
+    def readAttributes(self):
+        """Read the mutex's attributes."""
+
+        if self.kind != PTHREAD_MUTEX_DESTROYED:
+            if self.kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP:
+                self.values.append(('Robust', 'Yes'))
+            else:
+                self.values.append(('Robust', 'No'))
+
+            # In glibc, robust mutexes always have their pshared flag set to
+            # 'shared' regardless of what the pshared flag of their
+            # mutexattr was. Therefore a robust mutex will act as shared even if
+            # it was initialized with a 'private' mutexattr.
+            if self.kind & PTHREAD_MUTEX_PSHARED_BIT:
+                self.values.append(('Shared', 'Yes'))
+            else:
+                self.values.append(('Shared', 'No'))
+
+            if self.kind & PTHREAD_MUTEX_PRIO_INHERIT_NP:
+                self.values.append(('Protocol', 'Priority inherit'))
+            elif self.kind & PTHREAD_MUTEX_PRIO_PROTECT_NP:
+                prioCeiling = ((self.lock & PTHREAD_MUTEX_PRIO_CEILING_MASK) >>
+                               PTHREAD_MUTEX_PRIO_CEILING_SHIFT)
+
+                self.values.append(('Protocol', 'Priority protect'))
+                self.values.append(('Priority ceiling', prioCeiling))
+            else:
+                # PTHREAD_PRIO_NONE
+                self.values.append(('Protocol', 'None'))
+
+    def readMiscInfo(self):
+        """Read miscellaneous info on the mutex.
+
+        For now this reads the number of times a row a recursive mutex
+        was locked by the same thread.
+        """
+
+        mutexType = self.kind & PTHREAD_MUTEX_KIND_MASK
+
+        if mutexType == PTHREAD_MUTEX_RECURSIVE and self.count > 1:
+            self.values.append(('Times locked recursively', self.count))
+
+################################################################################
+
+# Mutex attribute flags
+PTHREAD_MUTEXATTR_PROTOCOL_SHIFT = 28
+PTHREAD_MUTEXATTR_PROTOCOL_MASK = 0x30000000
+PTHREAD_MUTEXATTR_PRIO_CEILING_MASK = 0x00FFF000
+PTHREAD_MUTEXATTR_FLAG_ROBUST = 0x40000000
+PTHREAD_MUTEXATTR_FLAG_PSHARED = 0x80000000
+
+PTHREAD_MUTEXATTR_FLAG_BITS = (PTHREAD_MUTEXATTR_FLAG_ROBUST |
+                               PTHREAD_MUTEXATTR_FLAG_PSHARED |
+                               PTHREAD_MUTEXATTR_PROTOCOL_MASK |
+                               PTHREAD_MUTEXATTR_PRIO_CEILING_MASK)
+
+PTHREAD_MUTEX_NO_ELISION_NP = 0x200
+
+# Priority protocols
+PTHREAD_PRIO_NONE = 0
+PTHREAD_PRIO_INHERIT = 1
+PTHREAD_PRIO_PROTECT = 2
+
+class MutexAttributesPrinter(object):
+    """Pretty printer for pthread_mutexattr_t.
+
+    In the NPTL this is a type that's always casted to struct pthread_mutexattr,
+    which has a single 'mutexkind' field containing the actual attributes.
+    """
+
+    def __init__(self, mutexattr):
+        """Initialize the printer's internal data structures.
+
+        Args:
+            mutexattr: A gdb.value representing a pthread_mutexattr_t.
+        """
+
+        mutexattrStruct = gdb.lookup_type('struct pthread_mutexattr')
+        self.mutexattr = mutexattr.cast(mutexattrStruct)['mutexkind']
+        self.values = []
+        self.readValues()
+
+    def to_string(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_mutexattr_t.
+        """
+
+        return 'pthread_mutexattr_t'
+
+    def children(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_mutexattr_t.
+        """
+
+        return Iterator(self.values)
+
+    def readValues(self):
+        """Read the mutexattr's info and store it in self.values.
+
+        The data contained in self.values will be returned by the Iterator
+        created in self.children.
+        """
+
+        mutexattrType = (self.mutexattr &
+                         ~PTHREAD_MUTEXATTR_FLAG_BITS &
+                         ~PTHREAD_MUTEX_NO_ELISION_NP)
+
+        # mutexattrType must be casted to int because it's a gdb.Value
+        self.values.append(mutexTypesDict[int(mutexattrType)])
+
+        if self.mutexattr & PTHREAD_MUTEXATTR_FLAG_ROBUST:
+            self.values.append(('Robust', 'Yes'))
+        else:
+            self.values.append(('Robust', 'No'))
+
+        if self.mutexattr & PTHREAD_MUTEXATTR_FLAG_PSHARED:
+            self.values.append(('Shared', 'Yes'))
+        else:
+            self.values.append(('Shared', 'No'))
+
+        protocol = ((self.mutexattr & PTHREAD_MUTEXATTR_PROTOCOL_MASK) >>
+                    PTHREAD_MUTEXATTR_PROTOCOL_SHIFT)
+
+        if protocol == PTHREAD_PRIO_NONE:
+            self.values.append(('Protocol', 'None'))
+        elif protocol == PTHREAD_PRIO_INHERIT:
+            self.values.append(('Protocol', 'Priority inherit'))
+        elif protocol == PTHREAD_PRIO_PROTECT:
+            self.values.append(('Protocol', 'Priority protect'))
+
+################################################################################
+
+# Value of __mutex for shared condvars.
+PTHREAD_COND_SHARED = ~ctypes.c_long(0).value
+
+# Value of __total_seq for destroyed condvars.
+PTHREAD_COND_DESTROYED = ctypes.c_ulonglong(-1).value
+
+# __nwaiters encodes the number of threads waiting on a condvar
+# and the clock ID.
+# __nwaiters >> COND_NWAITERS_SHIFT gives us the number of waiters.
+COND_NWAITERS_SHIFT = 1
+
+class ConditionVariablePrinter(object):
+    """Pretty printer for pthread_cond_t."""
+
+    def __init__(self, cond):
+        """Initialize the printer's internal data structures.
+
+        Args:
+            cond: A gdb.value representing a pthread_cond_t.
+        """
+
+        data = cond['__data']
+        self.totalSeq = data['__total_seq']
+        self.mutex = data['__mutex']
+        self.nwaiters = data['__nwaiters']
+        self.values = []
+        self.readValues()
+
+    def to_string(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_cond_t.
+        """
+
+        return 'pthread_cond_t'
+
+    def children(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_cond_t.
+        """
+
+        return Iterator(self.values)
+
+    def readValues(self):
+        """Read the condvar's info and store it in self.values.
+
+        The data contained in self.values will be returned by the Iterator
+        created in self.children.
+        """
+
+        self.readStatus()
+        self.readAttributes()
+        self.readMutexInfo()
+
+    def readStatus(self):
+        """Read the status of the condvar.
+
+        This method reads whether the condvar is destroyed and how many threads
+        are waiting for it.
+        """
+
+        if self.totalSeq == PTHREAD_COND_DESTROYED:
+            self.values.append(('Status', 'Destroyed'))
+
+        self.values.append(('Threads waiting for this condvar',
+                           self.nwaiters >> COND_NWAITERS_SHIFT))
+
+    def readAttributes(self):
+        """Read the condvar's attributes."""
+
+        clockID = self.nwaiters & ((1 << COND_NWAITERS_SHIFT) - 1)
+        shared = (self.mutex == PTHREAD_COND_SHARED)
+
+        if shared:
+            self.values.append(('Shared', 'Yes'))
+        else:
+            self.values.append(('Shared', 'No'))
+
+        self.values.append(('Clock ID', clockID))
+
+    def readMutexInfo(self):
+        """Read the data of the mutex this condvar is bound to.
+
+        A pthread_cond_t's __data.__mutex member is a void * which
+        must be casted to pthread_mutex_t *. For shared condvars, this
+        member isn't recorded and has a value of ~0l instead.
+        """
+
+        if self.mutex and self.mutex != PTHREAD_COND_SHARED:
+            mutexType = gdb.lookup_type('pthread_mutex_t')
+            mutex = self.mutex.cast(mutexType.pointer()).dereference()
+
+            self.values.append(('Mutex', mutex))
+
+################################################################################
+
+class ConditionVariableAttributesPrinter(object):
+    """Pretty printer for pthread_condattr_t.
+
+    In the NPTL this is a type that's
+    always casted to struct pthread_condattr, which has a single 'value' field
+    containing the actual attributes.
+    """
+
+    def __init__(self, condattr):
+        """Initialize the printer's internal data structures.
+
+        Args:
+            condattr: A gdb.value representing a pthread_condattr_t.
+        """
+
+        condattrStruct = gdb.lookup_type('struct pthread_condattr')
+        self.condattr = condattr.cast(condattrStruct)['value']
+        self.values = []
+        self.readValues()
+
+    def to_string(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_condattr_t.
+        """
+
+        return 'pthread_condattr_t'
+
+    def children(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_condattr_t.
+        """
+
+        return Iterator(self.values)
+
+    def readValues(self):
+        """Read the condattr's info and store it in self.values.
+
+        The data contained in self.values will be returned by the Iterator
+        created in self.children.
+        """
+
+        clockID = self.condattr & ((1 << COND_NWAITERS_SHIFT) - 1)
+
+        if self.condattr & 1:
+            self.values.append(('Shared', 'Yes'))
+        else:
+            self.values.append(('Shared', 'No'))
+
+        self.values.append(('Clock ID', clockID))
+
+################################################################################
+
+class RWLockPrinter(object):
+    """Pretty printer for pthread_rwlock_t."""
+
+    def __init__(self, rwlock):
+        """Initialize the printer's internal data structures.
+
+        Args:
+            rwlock: A gdb.value representing a pthread_rwlock_t.
+        """
+
+        data = rwlock['__data']
+        self.readers = data['__nr_readers']
+        self.queuedReaders = data['__nr_readers_queued']
+        self.queuedWriters = data['__nr_writers_queued']
+        self.writerID = data['__writer']
+        self.shared = data['__shared']
+        self.prefersWriters = data['__flags']
+        self.values = []
+        self.readValues()
+
+    def to_string(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_rwlock_t.
+        """
+
+        return 'pthread_rwlock_t'
+
+    def children(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_rwlock_t.
+        """
+
+        return Iterator(self.values)
+
+    def readValues(self):
+        """Read the rwlock's info and store it in self.values.
+
+        The data contained in self.values will be returned by the Iterator
+        created in self.children.
+        """
+
+        self.readStatus()
+        self.readAttributes()
+
+    def readStatus(self):
+        """Read the status of the rwlock."""
+
+        if self.writerID:
+            self.values.append(('Status', 'Locked (Write)'))
+            self.values.append(('Writer ID', self.writerID))
+        elif self.readers:
+            self.values.append(('Status', 'Locked (Read)'))
+            self.values.append(('Readers', self.readers))
+        else:
+            self.values.append(('Status', 'Unlocked'))
+
+        self.values.append(('Queued readers', self.queuedReaders))
+        self.values.append(('Queued writers', self.queuedWriters))
+
+    def readAttributes(self):
+        """Read the attributes of the rwlock."""
+
+        if self.shared:
+            self.values.append(('Shared', 'Yes'))
+        else:
+            self.values.append(('Shared', 'No'))
+
+        if self.prefersWriters:
+            self.values.append(('Prefers', 'Writers'))
+        else:
+            self.values.append(('Prefers', 'Readers'))
+
+################################################################################
+
+# Rwlock attributes
+PTHREAD_RWLOCK_PREFER_READER_NP = 0
+PTHREAD_RWLOCK_PREFER_WRITER_NP = 1
+PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP = 2
+
+# 'Shared' attribute values
+PTHREAD_PROCESS_PRIVATE = 0
+PTHREAD_PROCESS_SHARED = 1
+
+class RWLockAttributesPrinter(object):
+    """Pretty printer for pthread_rwlockattr_t.
+
+    In the NPTL this is a type that's always casted to
+    struct pthread_rwlockattr, which has two fields ('lockkind' and 'pshared')
+    containing the actual attributes.
+    """
+
+    def __init__(self, rwlockattr):
+        """Initialize the printer's internal data structures.
+
+        Args:
+            rwlockattr: A gdb.value representing a pthread_rwlockattr_t.
+        """
+
+        rwlockattrStruct = gdb.lookup_type('struct pthread_rwlockattr')
+        self.rwlockattr = rwlockattr.cast(rwlockattrStruct)
+        self.values = []
+        self.readValues()
+
+    def to_string(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_rwlockattr_t."""
+
+        return 'pthread_rwlockattr_t'
+
+    def children(self):
+        """gdb API function.
+
+        This is called from gdb when we try to print a pthread_rwlockattr_t."""
+
+        return Iterator(self.values)
+
+    def readValues(self):
+        """Read the rwlockattr's info and store it in self.values.
+
+        The data contained in self.values will be returned by the Iterator
+        created in self.children.
+        """
+
+        rwlockType = self.rwlockattr['lockkind']
+        shared = self.rwlockattr['pshared']
+
+        if shared == PTHREAD_PROCESS_SHARED:
+            self.values.append(('Shared', 'Yes'))
+        else:
+            # PTHREAD_PROCESS_PRIVATE
+            self.values.append(('Shared', 'No'))
+
+        if rwlockType == PTHREAD_RWLOCK_PREFER_READER_NP:
+            self.values.append(('Prefers', 'Readers'))
+        elif (rwlockType == PTHREAD_RWLOCK_PREFER_WRITER_NP or
+              rwlockType == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP):
+            self.values.append(('Prefers', 'Writers'))
+
+################################################################################
+
+class Printer(object):
+    """Printer class which conforms to the gdb pretty printing interface."""
+
+    def __init__(self, name):
+        self.name = name
+        self.enabled = True
+        self.subprinters = []
+
+    class Subprinter(object):
+        """A regex-based printer.
+
+        Individual pretty-printers are registered as subprinters of a single
+        Printer instance.
+        """
+
+        def __init__(self, name, regex, callable):
+            """
+            Initialize a pretty-printer.
+
+            Args:
+                name: The name of the printer.
+                regex: A regular expression. When gdb tries to print a variable
+                    whose type matches the regex it'll trigger this printer.
+                callable: A function or callable object that gdb will call
+                    when trying to print some value. It should return a
+                    pretty-printer.
+            """
+            self.name = name
+            self.regex = re.compile(regex)
+            self.callable = callable
+            self.enabled = True
+
+    def addSubprinter(self, name, regex, callable):
+        """Register a regex-based subprinter."""
+
+        self.subprinters.append(self.Subprinter(name, regex, callable))
+
+    def __call__(self, value):
+        """gdb API function.
+
+        This is called when trying to print an inferior value
+        from gdb. If a registered printer's regex matches the value's type,
+        gdb will use the printer to print the value.
+        """
+
+        typeName = value.type.name
+
+        if typeName:
+            for subprinter in self.subprinters:
+                if subprinter.enabled and subprinter.regex.match(typeName):
+                    return subprinter.callable(value)
+
+        # Return None if we have no type name or if we can't find a subprinter
+        # for the given type.
+        return None
+
+################################################################################
+
+def register(objfile):
+    """Register the pretty printers within the given objfile."""
+
+    printer = Printer('Glibc pthread locks')
+
+    printer.addSubprinter('pthread_mutex_t', '^pthread_mutex_t$', MutexPrinter)
+    printer.addSubprinter('pthread_mutexattr_t', '^pthread_mutexattr_t$',
+                          MutexAttributesPrinter)
+    printer.addSubprinter('pthread_cond_t', '^pthread_cond_t$',
+                          ConditionVariablePrinter)
+    printer.addSubprinter('pthread_condattr_t', '^pthread_condattr_t$',
+                          ConditionVariableAttributesPrinter)
+    printer.addSubprinter('pthread_rwlock_t', '^pthread_rwlock_t$', RWLockPrinter)
+    printer.addSubprinter('pthread_rwlockattr_t', '^pthread_rwlockattr_t$',
+                          RWLockAttributesPrinter)
+
+    gdb.printing.register_pretty_printer(objfile, printer)
+
+register(gdb.current_objfile())