This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFC v2] Pretty printers for NPTL lock types


Changes from v1:
  * Rewrote the printers so that to_string returns a string instead of
doing the actual printing.

Follow-up from https://sourceware.org/ml/libc-alpha/2015-03/msg00472.html .

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

I've tested the printers on both the gdb CLI and Eclipse CDT. The
Eclipse version works fine when using the gdb console, but hovering
over the variables' names won't show the pretty printers' output as it
should (perhaps this is an Eclipse problem?).

The testing was made on a few simple examples, some of which deadlock
on purpose. I'm not sure of how could I offer more detail on my
testing; do I have to provide unit tests at this stage? If that's the
case I could write a few Python scripts which could drive gdb over the
examples and try using something like PExpect to check the output of
the pretty printers.

-- 

MartÃn GalvÃn

Software Engineer

Taller Technologies Argentina

San Lorenzo 47, 3rd Floor, Office 5

CÃrdoba, Argentina

Phone: 54 351 4217888 / +54 351 4218211
import re
import ctypes
import gdb

# 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
PTHREAD_MUTEX_INCONSISTENT = gdb.parse_and_eval("~0u >> 1")  # INT_MAX for 2's complement CPUs
PTHREAD_MUTEX_NOTRECOVERABLE = PTHREAD_MUTEX_INCONSISTENT - 1
FUTEX_OWNER_DIED = 0x40000000  # For robust mutexes
FUTEX_WAITERS = 0x80000000  # For robust and PI mutexes
FUTEX_TID_MASK = 0x3FFFFFFF  # For robust and PI mutexes

# 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

class MutexPrinter(object):
    """Pretty printer for pthread_mutex_t."""

    def __init__(self, mutex):
        data = mutex['__data']
        self.lock = data['__lock']
        self.count = data['__count']
        self.owner = data['__owner']
        self.kind = data['__kind']
        self.output = ""

    def to_string(self):
        """gdb API function. This is called from gdb when we try to print
        a mutex."""

        self.output += "pthread_mutex_t:\n"

        self.printType()
        self.printStatus()
        self.printAttributes()
        self.printMiscInfo()

        return self.output

    def printType(self):
        """Print the mutex's type."""

        mutexType = self.kind & PTHREAD_MUTEX_KIND_MASK

        if mutexType == PTHREAD_MUTEX_NORMAL:
            self.output += "* Type: Normal\n"
        elif mutexType == PTHREAD_MUTEX_RECURSIVE:
            self.output += "* Type: Recursive\n"
        elif mutexType == PTHREAD_MUTEX_ERRORCHECK:
            self.output += "* Type: Error check\n"
        elif mutexType == PTHREAD_MUTEX_ADAPTIVE_NP:
            self.output += "* Type: Adaptive\n"

    def printStatus(self):
        """Print the mutex's status. For architectures which support lock elision,
        this method prints whether the mutex is actually locked (i.e. it may show it
        as unlocked after calling pthread_mutex_lock)."""

        if self.kind == PTHREAD_MUTEX_DESTROYED:
            self.output += "* Status: Destroyed\n"
        elif self.kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP:
            self.printStatusRobust()
        else:
            self.printStatusNonRobust()

    def printStatusRobust(self):
        """In glibc robust mutexes are implemented in a very different way than
        non-robust ones. This method prints their locking status, as well as
        their registered owner (whether it's alive or not) and the status of
        the state they're protecting."""

        if self.lock == PTHREAD_MUTEX_UNLOCKED:
            self.output += "* Status: Unlocked\n"
        else:  # Mutex is locked
            if self.lock & FUTEX_WAITERS:
                self.output += "* Status: Locked, possibly with waiters\n"
            else:
                self.output += "* Status: Locked, no waiters\n"

            if self.lock & FUTEX_OWNER_DIED:
                self.output += "* Owner ID: %d (dead)\n" % self.owner
            else:
                self.output += "* Owner ID: %d\n" % (self.lock & FUTEX_TID_MASK)

        if self.owner == PTHREAD_MUTEX_INCONSISTENT:
            self.output += "* The state protected by this mutex is inconsistent\n"
        elif self.owner == PTHREAD_MUTEX_NOTRECOVERABLE:
            self.output += "* The state protected by this mutex is not recoverable\n"

    def printStatusNonRobust(self):
        """Print whether the mutex is locked, if it may have waiters and its
        owner (if any)."""

        if self.lock == PTHREAD_MUTEX_UNLOCKED:
            self.output += "* Status: Unlocked\n"
        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.output += "* Status: Locked, possibly with waiters\n"
            else:
                self.output += "* Status: Locked, no waiters\n"

            self.output += "* Owner ID: %d\n" % owner

    def printAttributes(self):
        """Print the mutex's attributes."""

        if self.kind != PTHREAD_MUTEX_DESTROYED:
            self.output += "* Attributes:\n"

            if self.kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP:
                self.output += " - Robust\n"
            else:
                self.output += " - Non-robust\n"

            # 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.output += " - Shared\n"
            else:
                self.output += " - Private\n"

            if self.kind & PTHREAD_MUTEX_PRIO_INHERIT_NP:
                self.output += " - Protocol: Priority inherit\n"
            elif self.kind & PTHREAD_MUTEX_PRIO_PROTECT_NP:
                priorityCeiling = ((self.lock & PTHREAD_MUTEX_PRIO_CEILING_MASK) >>
                                   PTHREAD_MUTEX_PRIO_CEILING_SHIFT)

                self.output += " - Protocol: Priority protect\n"
                self.output += " - Priority ceiling: %d\n" % priorityCeiling
            else:  # PTHREAD_PRIO_NONE
                self.output += " - Protocol: None\n"

    def printMiscInfo(self):
        """Print miscellaneous info on the mutex, such as the number of times in 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.output += ("* This recursive mutex has been locked %d times in a row"
                            " by the same thread\n" % 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):
        mutexattrStruct = gdb.lookup_type('struct pthread_mutexattr')
        self.mutexattr = mutexattr.cast(mutexattrStruct)['mutexkind']
        self.output = ""

    def to_string(self):
        """gdb API function. This is called from gdb when we try to print
        a mutexattr."""

        self.output += "pthread_mutexattr_t:\n"

        mutexattrType = (self.mutexattr &
                         ~PTHREAD_MUTEXATTR_FLAG_BITS &
                         ~PTHREAD_MUTEX_NO_ELISION_NP)

        if mutexattrType == PTHREAD_MUTEX_NORMAL:
            self.output += "* Type: Normal\n"
        elif mutexattrType == PTHREAD_MUTEX_RECURSIVE:
            self.output += "* Type: Recursive\n"
        elif mutexattrType == PTHREAD_MUTEX_ERRORCHECK:
            self.output += "* Type: Error check\n"
        elif mutexattrType == PTHREAD_MUTEX_ADAPTIVE_NP:
            self.output += "* Type: Adaptive\n"

        self.output += "* Attributes:\n"

        if self.mutexattr & PTHREAD_MUTEXATTR_FLAG_ROBUST:
            self.output += " - Robust\n"
        else:
            self.output += " - Non-robust\n"

        if self.mutexattr & PTHREAD_MUTEXATTR_FLAG_PSHARED:
            self.output += " - Shared\n"
        else:
            self.output += " - Private\n"

        protocol = ((self.mutexattr & PTHREAD_MUTEXATTR_PROTOCOL_MASK) >>
                    PTHREAD_MUTEXATTR_PROTOCOL_SHIFT)

        if protocol == PTHREAD_PRIO_NONE:
            self.output += " - Protocol: None\n"
        elif protocol == PTHREAD_PRIO_INHERIT:
            self.output += " - Protocol: Priority inherit\n"
        elif protocol == PTHREAD_PRIO_PROTECT:
            self.output += " - Protocol: Priority protect\n"

        return self.output

################################################################################

# 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):
        data = cond['__data']
        self.totalSeq = data['__total_seq']
        self.mutex = data['__mutex']
        self.nwaiters = data['__nwaiters']
        self.output = ""

    def to_string(self):
        """gdb API function. This is called from gdb when we try to print
        a condvar."""

        self.output += "pthread_cond_t:\n"

        self.printStatus()
        self.printAttributes()
        self.printMutexInfo()

        return self.output

    def printStatus(self):
        """Print whether the condvar is destroyed, and how many threads are
        waiting for it."""

        if self.totalSeq == PTHREAD_COND_DESTROYED:
            self.output += "* This condvar is destroyed\n"

        self.output += ("* There are %d threads waiting for this condvar\n" %
                        (self.nwaiters >> COND_NWAITERS_SHIFT))

    def printAttributes(self):
        """Print the condvar's attributes."""

        clockID = self.nwaiters & ((1 << COND_NWAITERS_SHIFT) - 1)
        shared = (self.mutex == PTHREAD_COND_SHARED)

        self.output += "* Attributes:\n"

        if shared:
            self.output += " - Shared\n"
        else:
            self.output += " - Private\n"

        self.output += " - Clock ID: %d\n" % clockID

    def printMutexInfo(self):
        """Print info on 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:
            mutex = self.mutex.cast(gdb.lookup_type('pthread_mutex_t').pointer()).dereference()

            self.output += "This condition variable is bound to the following mutex:\n"

            self.output += MutexPrinter(mutex).to_string()

################################################################################

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):
        condattrStruct = gdb.lookup_type('struct pthread_condattr')
        self.condattr = condattr.cast(condattrStruct)['value']
        self.output = ""

    def to_string(self):
        """gdb API function. This is called from gdb when we try to print
        a condattr."""

        self.output += "pthread_condattr_t:\n"

        clockID = self.condattr & ((1 << COND_NWAITERS_SHIFT) - 1)

        self.output += "* Attributes:\n"

        if self.condattr & 1:
            self.output += " - Shared\n"
        else:
            self.output += " - Private\n"

        self.output += " - Clock ID: %d\n" % clockID

        return self.output

################################################################################

class RWLockPrinter(object):
    def __init__(self, rwlock):
        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.output = ""

    def to_string(self):
        """gdb API function. This is called from gdb when we try to print
        a rwlock."""

        self.printStatus()
        self.printAttributes()

        return self.output

    def printStatus(self):
        """Print the status of the rwlock."""

        self.output += "pthread_rwlock_t:\n"

        if self.writerID:
            self.output += "* Status: Locked (Write)\n"
            self.output += "* Writer ID: %d\n" % self.writerID
        elif self.readers:
            self.output += "* Status: Locked (Read)\n"
            self.output += "* Readers: %d\n" % self.readers
        else:
            self.output += "* Status: Unlocked\n"

        self.output += "* Queued readers: %d\n" % self.queuedReaders
        self.output += "* Queued writers: %d\n" % self.queuedWriters

    def printAttributes(self):
        """Print the attributes of the rwlock."""

        self.output += "* Attributes:\n"

        if self.shared:
            self.output += " - Shared\n"
        else:
            self.output += " - Private\n"

        if self.prefersWriters:
            self.output += " - Prefers writers\n"
        else:
            self.output += " - Prefers readers\n"

################################################################################

# 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):
        rwlockattrStruct = gdb.lookup_type('struct pthread_rwlockattr')
        self.rwlockattr = rwlockattr.cast(rwlockattrStruct)
        self.output = ""

    def to_string(self):
        """gdb API function. This is called from gdb when we try to print
        a rwlockattr."""

        self.output += "pthread_rwlockattr_t:\n"

        rwlockType = self.rwlockattr['lockkind']
        shared = self.rwlockattr['pshared']

        self.output += "* Attributes:\n"

        if shared == PTHREAD_PROCESS_SHARED:
            self.output += " - Shared\n"
        else:  # PTHREAD_PROCESS_PRIVATE
            self.output += " - Private\n"

        if rwlockType == PTHREAD_RWLOCK_PREFER_READER_NP:
            self.output += " - Prefers readers\n"
        elif (rwlockType == PTHREAD_RWLOCK_PREFER_WRITER_NP or
              rwlockType == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP):
            self.output += " - Prefers writers\n"

        return self.output

################################################################################

class Printer(object):
    """Basic 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):
            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

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(gdb.current_objfile(), printer)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]