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] Pretty printers for NPTL lock types


Hello everyone! Following what was discussed in
https://sourceware.org/ml/libc-alpha/2015-02/msg00194.html I wrote 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

These have been tested on x86, but they should work on most (if not
all) supported architectures since they don't touch arch-specific
fields (such as those related to lock elision). The script implements
a simple Printer class which conforms to the gdb interface, and
individual printers are registered as subprinters of a single Printer
instance.

As you can see, I used several global variables to represent the
macros used in the NPTL code (e.g. those defined in pthreadP.h). I'm
aware of how ugly this is, as they'll have to be maintained together
with the NPTL values, but the alternative would be having users
re-compile their glibc with debugging symbols (even then I'm not sure
if it would work). Any suggestions on how to improve this will be
appreciated.

Right now to load the script you have to run gdb and then type:

source printers.py

Ideally, the commited version will have a different name and will be
installed in a way such that gdb will auto-load it. As this is an RFC
I'm attaching the script as a .py file (as opposed to a proper patch).
Suggestions of where to place it in the source tree are welcome.

As always, any feedback will be appreciated. Thanks a lot!

-- 

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']


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

        print("pthread_mutex_t:")

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

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

        mutexType = self.kind & PTHREAD_MUTEX_KIND_MASK

        if mutexType == PTHREAD_MUTEX_NORMAL:
            print("* Type: Normal")
        elif mutexType == PTHREAD_MUTEX_RECURSIVE:
            print("* Type: Recursive")
        elif mutexType == PTHREAD_MUTEX_ERRORCHECK:
            print("* Type: Error check")
        elif mutexType == PTHREAD_MUTEX_ADAPTIVE_NP:
            print("* Type: Adaptive")

    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:
            print("* Status: Destroyed")
        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:
            print("* Status: Unlocked")
        else:  # Mutex is locked
            if self.lock & FUTEX_WAITERS:
                print("* Status: Locked, possibly with waiters")
            else:
                print("* Status: Locked, no waiters")

            if self.lock & FUTEX_OWNER_DIED:
                print("* Owner ID: %d (dead)" % self.owner)
            else:
                print("* Owner ID: %d" % (self.lock & FUTEX_TID_MASK))

        if self.owner == PTHREAD_MUTEX_INCONSISTENT:
            print("* The state protected by this mutex is inconsistent")
        elif self.owner == PTHREAD_MUTEX_NOTRECOVERABLE:
            print("* The state protected by this mutex is not recoverable")

    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:
            print("* 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:
                print("* Status: Locked, possibly with waiters")
            else:
                print("* Status: Locked, no waiters")

            print("* Owner ID: %d" % owner)

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

        if self.kind != PTHREAD_MUTEX_DESTROYED:
            print("* Attributes:")

            if self.kind & PTHREAD_MUTEX_ROBUST_NORMAL_NP:
                print(" - Robust")
            else:
                print(" - Non-robust")

            # 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:
                print(" - Shared")
            else:
                print(" - Private")

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

                print(" - Protocol: Priority protect")
                print(" - Priority ceiling: %d" % priorityCeiling)
            else:  # PTHREAD_PRIO_NONE
                print(" - Protocol: None")

    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:
            print("* This recursive mutex has been locked %d times in a row"
                  " by the same thread" % 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']

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

        print("pthread_mutexattr_t:")

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

        if mutexattrType == PTHREAD_MUTEX_NORMAL:
            print("* Type: Normal")
        elif mutexattrType == PTHREAD_MUTEX_RECURSIVE:
            print("* Type: Recursive")
        elif mutexattrType == PTHREAD_MUTEX_ERRORCHECK:
            print("* Type: Error check")
        elif mutexattrType == PTHREAD_MUTEX_ADAPTIVE_NP:
            print("* Type: Adaptive")

        print("* Attributes:")

        if self.mutexattr & PTHREAD_MUTEXATTR_FLAG_ROBUST:
            print(" - Robust")
        else:
            print(" - Non-robust")

        if self.mutexattr & PTHREAD_MUTEXATTR_FLAG_PSHARED:
            print(" - Shared")
        else:
            print(" - Private")

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

        if protocol == PTHREAD_PRIO_NONE:
            print(" - Protocol: None")
        elif protocol == PTHREAD_PRIO_INHERIT:
            print(" - Protocol: Priority inherit")
        elif protocol == PTHREAD_PRIO_PROTECT:
            print(" - 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):
        data = cond['__data']
        self.totalSeq = data['__total_seq']
        self.mutex = data['__mutex']
        self.nwaiters = data['__nwaiters']

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

        print("pthread_cond_t:")

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

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

        if self.totalSeq == PTHREAD_COND_DESTROYED:
            print("* This condvar is destroyed")

        print("* There are %d threads waiting for this condvar" % (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)

        print("* Attributes:")

        if shared:
            print(" - Shared")
        else:
            print(" - Private")

        print(" - Clock ID: %d" % 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()

            print("This condition variable is bound to the following mutex:")

            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']

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

        print("pthread_condattr_t:")

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

        print("* Attributes:")

        if self.condattr & 1:
            print(" - Shared")
        else:
            print(" - Private")

        print(" - Clock ID: %d" % clockID)

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

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 = shared = data['__shared']
        self.prefersWriters = data['__flags']

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

        self.printStatus()
        self.printAttributes()

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

        print("pthread_rwlock_t:")

        if self.writerID:
            print("* Status: Locked (Write)")
            print("* Writer ID: %d" % self.writerID)
        elif self.readers:
            print("* Status: Locked (Read)")
            print("* Readers: %d" % self.readers)
        else:
            print("* Status: Unlocked")

        print("* Queued readers: %d" % self.queuedReaders)
        print("* Queued writers: %d" % self.queuedWriters)

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

        print("* Attributes:")

        if self.shared:
            print(" - Shared")
        else:
            print(" - Private")

        if self.prefersWriters:
            print(" - Prefers writers")
        else:
            print(" - 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):
        rwlockattrStruct = gdb.lookup_type("struct pthread_rwlockattr")
        self.rwlockattr = rwlockattr.cast(rwlockattrStruct)

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

        print("pthread_rwlockattr_t:")

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

        print("* Attributes:")

        if shared == PTHREAD_PROCESS_SHARED:
            print(" - Shared")
        else:  # PTHREAD_PROCESS_PRIVATE
            print(" - Private")

        if rwlockType == PTHREAD_RWLOCK_PREFER_READER_NP:
            print(" - Prefers readers")
        elif (rwlockType == PTHREAD_RWLOCK_PREFER_WRITER_NP or
              rwlockType == PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP):
            print(" - Prefers writers")

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

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)

print("Loaded glibc pthread locks pretty-printers")

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