1 # Pretty printers for the NPTL lock types.
3 # Copyright (C) 2016 Free Software Foundation, Inc.
4 # This file is part of the GNU C Library.
6 # The GNU C Library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # The GNU C Library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with the GNU C Library; if not, see
18 # <http://www.gnu.org/licenses/>.
20 """This file contains the gdb pretty printers for the following types:
27 * pthread_rwlockattr_t
29 You can check which printers are registered and enabled by issuing the
30 'info pretty-printer' gdb command. Printers should trigger automatically when
31 trying to print a variable of one of the types mentioned above.
34 from __future__
import print_function
38 from nptl_lock_constants
import *
41 PTHREAD_MUTEX_NORMAL
: ('Type', 'Normal'),
42 PTHREAD_MUTEX_RECURSIVE
: ('Type', 'Recursive'),
43 PTHREAD_MUTEX_ERRORCHECK
: ('Type', 'Error check'),
44 PTHREAD_MUTEX_ADAPTIVE_NP
: ('Type', 'Adaptive')
47 class MutexPrinter(object):
48 """Pretty printer for pthread_mutex_t."""
50 def __init__(self
, mutex
):
51 """Initialize the printer's internal data structures.
54 mutex: A gdb.value representing a pthread_mutex_t.
57 data
= mutex
['__data']
58 self
.lock
= data
['__lock']
59 self
.count
= data
['__count']
60 self
.owner
= data
['__owner']
61 self
.kind
= data
['__kind']
68 This is called from gdb when we try to print a pthread_mutex_t.
71 return 'pthread_mutex_t'
76 This is called from gdb when we try to print a pthread_mutex_t.
81 def read_values(self
):
82 """Read the mutex's info and store it in self.values.
84 The data contained in self.values will be returned by the Iterator
85 created in self.children.
90 self
.read_attributes()
94 """Read the mutex's type."""
96 mutex_type
= self
.kind
& PTHREAD_MUTEX_KIND_MASK
98 # mutex_type must be casted to int because it's a gdb.Value
99 self
.values
.append(MUTEX_TYPES
[int(mutex_type
)])
101 def read_status(self
):
102 """Read the mutex's status.
104 For architectures which support lock elision, this method reads
105 whether the mutex appears as locked in memory (i.e. it may show it as
106 unlocked even after calling pthread_mutex_lock).
109 if self
.kind
== PTHREAD_MUTEX_DESTROYED
:
110 self
.values
.append(('Status', 'Destroyed'))
111 elif self
.kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
112 self
.read_status_robust()
114 self
.read_status_no_robust()
116 def read_status_robust(self
):
117 """Read the status of a robust mutex.
119 In glibc robust mutexes are implemented in a very different way than
120 non-robust ones. This method reads their locking status,
121 whether it may have waiters, their registered owner (if any),
122 whether the owner is alive or not, and the status of the state
126 if self
.lock
== PTHREAD_MUTEX_UNLOCKED
:
127 self
.values
.append(('Status', 'Unlocked'))
129 if self
.lock
& FUTEX_WAITERS
:
130 self
.values
.append(('Status', 'Locked, possibly with waiters'))
132 self
.values
.append(('Status',
133 'Locked, possibly with no waiters'))
135 if self
.lock
& FUTEX_OWNER_DIED
:
136 self
.values
.append(('Owner ID', '%d (dead)' % self
.owner
))
138 self
.values
.append(('Owner ID', self
.lock
& FUTEX_TID_MASK
))
140 if self
.owner
== PTHREAD_MUTEX_INCONSISTENT
:
141 self
.values
.append(('State protected by this mutex',
143 elif self
.owner
== PTHREAD_MUTEX_NOTRECOVERABLE
:
144 self
.values
.append(('State protected by this mutex',
147 def read_status_no_robust(self
):
148 """Read the status of a non-robust mutex.
150 Read info on whether the mutex is locked, if it may have waiters
151 and its owner (if any).
154 lock_value
= self
.lock
156 if self
.kind
& PTHREAD_MUTEX_PRIO_PROTECT_NP
:
157 lock_value
&= ~
(PTHREAD_MUTEX_PRIO_CEILING_MASK
)
159 if lock_value
== PTHREAD_MUTEX_UNLOCKED
:
160 self
.values
.append(('Status', 'Unlocked'))
162 if self
.kind
& PTHREAD_MUTEX_PRIO_INHERIT_NP
:
163 waiters
= self
.lock
& FUTEX_WAITERS
164 owner
= self
.lock
& FUTEX_TID_MASK
166 # Mutex protocol is PP or none
167 waiters
= (self
.lock
!= PTHREAD_MUTEX_LOCKED_NO_WAITERS
)
171 self
.values
.append(('Status', 'Locked, possibly with waiters'))
173 self
.values
.append(('Status',
174 'Locked, possibly with no waiters'))
176 self
.values
.append(('Owner ID', owner
))
178 def read_attributes(self
):
179 """Read the mutex's attributes."""
181 if self
.kind
!= PTHREAD_MUTEX_DESTROYED
:
182 if self
.kind
& PTHREAD_MUTEX_ROBUST_NORMAL_NP
:
183 self
.values
.append(('Robust', 'Yes'))
185 self
.values
.append(('Robust', 'No'))
187 # In glibc, robust mutexes always have their pshared flag set to
188 # 'shared' regardless of what the pshared flag of their
189 # mutexattr was. Therefore a robust mutex will act as shared
190 # even if it was initialized with a 'private' mutexattr.
191 if self
.kind
& PTHREAD_MUTEX_PSHARED_BIT
:
192 self
.values
.append(('Shared', 'Yes'))
194 self
.values
.append(('Shared', 'No'))
196 if self
.kind
& PTHREAD_MUTEX_PRIO_INHERIT_NP
:
197 self
.values
.append(('Protocol', 'Priority inherit'))
198 elif self
.kind
& PTHREAD_MUTEX_PRIO_PROTECT_NP
:
199 prio_ceiling
= ((self
.lock
& PTHREAD_MUTEX_PRIO_CEILING_MASK
)
200 >> PTHREAD_MUTEX_PRIO_CEILING_SHIFT
)
202 self
.values
.append(('Protocol', 'Priority protect'))
203 self
.values
.append(('Priority ceiling', prio_ceiling
))
206 self
.values
.append(('Protocol', 'None'))
208 def read_misc_info(self
):
209 """Read miscellaneous info on the mutex.
211 For now this reads the number of times a recursive mutex was locked
215 mutex_type
= self
.kind
& PTHREAD_MUTEX_KIND_MASK
217 if mutex_type
== PTHREAD_MUTEX_RECURSIVE
and self
.count
> 1:
218 self
.values
.append(('Times locked recursively', self
.count
))
220 class MutexAttributesPrinter(object):
221 """Pretty printer for pthread_mutexattr_t.
223 In the NPTL this is a type that's always casted to struct pthread_mutexattr
224 which has a single 'mutexkind' field containing the actual attributes.
227 def __init__(self
, mutexattr
):
228 """Initialize the printer's internal data structures.
231 mutexattr: A gdb.value representing a pthread_mutexattr_t.
237 mutexattr_struct
= gdb
.lookup_type('struct pthread_mutexattr')
238 self
.mutexattr
= mutexattr
.cast(mutexattr_struct
)['mutexkind']
241 # libpthread doesn't have debug symbols, thus we can't find the
242 # real struct type. Just print the union members.
243 self
.values
.append(('__size', mutexattr
['__size']))
244 self
.values
.append(('__align', mutexattr
['__align']))
249 This is called from gdb when we try to print a pthread_mutexattr_t.
252 return 'pthread_mutexattr_t'
257 This is called from gdb when we try to print a pthread_mutexattr_t.
262 def read_values(self
):
263 """Read the mutexattr's info and store it in self.values.
265 The data contained in self.values will be returned by the Iterator
266 created in self.children.
269 mutexattr_type
= (self
.mutexattr
270 & ~PTHREAD_MUTEXATTR_FLAG_BITS
271 & ~PTHREAD_MUTEX_NO_ELISION_NP
)
273 # mutexattr_type must be casted to int because it's a gdb.Value
274 self
.values
.append(MUTEX_TYPES
[int(mutexattr_type
)])
276 if self
.mutexattr
& PTHREAD_MUTEXATTR_FLAG_ROBUST
:
277 self
.values
.append(('Robust', 'Yes'))
279 self
.values
.append(('Robust', 'No'))
281 if self
.mutexattr
& PTHREAD_MUTEXATTR_FLAG_PSHARED
:
282 self
.values
.append(('Shared', 'Yes'))
284 self
.values
.append(('Shared', 'No'))
286 protocol
= ((self
.mutexattr
& PTHREAD_MUTEXATTR_PROTOCOL_MASK
) >>
287 PTHREAD_MUTEXATTR_PROTOCOL_SHIFT
)
289 if protocol
== PTHREAD_PRIO_NONE
:
290 self
.values
.append(('Protocol', 'None'))
291 elif protocol
== PTHREAD_PRIO_INHERIT
:
292 self
.values
.append(('Protocol', 'Priority inherit'))
293 elif protocol
== PTHREAD_PRIO_PROTECT
:
294 self
.values
.append(('Protocol', 'Priority protect'))
297 CLOCK_REALTIME
: 'CLOCK_REALTIME',
298 CLOCK_MONOTONIC
: 'CLOCK_MONOTONIC',
299 CLOCK_PROCESS_CPUTIME_ID
: 'CLOCK_PROCESS_CPUTIME_ID',
300 CLOCK_THREAD_CPUTIME_ID
: 'CLOCK_THREAD_CPUTIME_ID',
301 CLOCK_MONOTONIC_RAW
: 'CLOCK_MONOTONIC_RAW',
302 CLOCK_REALTIME_COARSE
: 'CLOCK_REALTIME_COARSE',
303 CLOCK_MONOTONIC_COARSE
: 'CLOCK_MONOTONIC_COARSE'
306 class ConditionVariablePrinter(object):
307 """Pretty printer for pthread_cond_t."""
309 def __init__(self
, cond
):
310 """Initialize the printer's internal data structures.
313 cond: A gdb.value representing a pthread_cond_t.
316 # Since PTHREAD_COND_SHARED is an integer, we need to cast it to void *
317 # to be able to compare it to the condvar's __data.__mutex member.
319 # While it looks like self.shared_value should be a class variable,
320 # that would result in it having an incorrect size if we're loading
321 # these printers through .gdbinit for a 64-bit objfile in AMD64.
322 # This is because gdb initially assumes the pointer size to be 4 bytes,
323 # and only sets it to 8 after loading the 64-bit objfiles. Since
324 # .gdbinit runs before any objfiles are loaded, this would effectively
325 # make self.shared_value have a size of 4, thus breaking later
326 # comparisons with pointers whose types are looked up at runtime.
327 void_ptr_type
= gdb
.lookup_type('void').pointer()
328 self
.shared_value
= gdb
.Value(PTHREAD_COND_SHARED
).cast(void_ptr_type
)
330 data
= cond
['__data']
331 self
.total_seq
= data
['__total_seq']
332 self
.mutex
= data
['__mutex']
333 self
.nwaiters
= data
['__nwaiters']
341 This is called from gdb when we try to print a pthread_cond_t.
344 return 'pthread_cond_t'
349 This is called from gdb when we try to print a pthread_cond_t.
354 def read_values(self
):
355 """Read the condvar's info and store it in self.values.
357 The data contained in self.values will be returned by the Iterator
358 created in self.children.
362 self
.read_attributes()
363 self
.read_mutex_info()
365 def read_status(self
):
366 """Read the status of the condvar.
368 This method reads whether the condvar is destroyed and how many threads
372 if self
.total_seq
== PTHREAD_COND_DESTROYED
:
373 self
.values
.append(('Status', 'Destroyed'))
375 self
.values
.append(('Threads waiting for this condvar',
376 self
.nwaiters
>> COND_NWAITERS_SHIFT
))
378 def read_attributes(self
):
379 """Read the condvar's attributes."""
381 clock_id
= self
.nwaiters
& ((1 << COND_NWAITERS_SHIFT
) - 1)
383 # clock_id must be casted to int because it's a gdb.Value
384 self
.values
.append(('Clock ID', CLOCK_IDS
[int(clock_id
)]))
386 shared
= (self
.mutex
== self
.shared_value
)
389 self
.values
.append(('Shared', 'Yes'))
391 self
.values
.append(('Shared', 'No'))
393 def read_mutex_info(self
):
394 """Read the data of the mutex this condvar is bound to.
396 A pthread_cond_t's __data.__mutex member is a void * which
397 must be casted to pthread_mutex_t *. For shared condvars, this
398 member isn't recorded and has a special value instead.
401 if self
.mutex
and self
.mutex
!= self
.shared_value
:
402 mutex_type
= gdb
.lookup_type('pthread_mutex_t')
403 mutex
= self
.mutex
.cast(mutex_type
.pointer()).dereference()
405 self
.values
.append(('Mutex', mutex
))
407 class ConditionVariableAttributesPrinter(object):
408 """Pretty printer for pthread_condattr_t.
410 In the NPTL this is a type that's always casted to struct pthread_condattr,
411 which has a single 'value' field containing the actual attributes.
414 def __init__(self
, condattr
):
415 """Initialize the printer's internal data structures.
418 condattr: A gdb.value representing a pthread_condattr_t.
424 condattr_struct
= gdb
.lookup_type('struct pthread_condattr')
425 self
.condattr
= condattr
.cast(condattr_struct
)['value']
428 # libpthread doesn't have debug symbols, thus we can't find the
429 # real struct type. Just print the union members.
430 self
.values
.append(('__size', condattr
['__size']))
431 self
.values
.append(('__align', condattr
['__align']))
436 This is called from gdb when we try to print a pthread_condattr_t.
439 return 'pthread_condattr_t'
444 This is called from gdb when we try to print a pthread_condattr_t.
449 def read_values(self
):
450 """Read the condattr's info and store it in self.values.
452 The data contained in self.values will be returned by the Iterator
453 created in self.children.
456 clock_id
= self
.condattr
& ((1 << COND_NWAITERS_SHIFT
) - 1)
458 # clock_id must be casted to int because it's a gdb.Value
459 self
.values
.append(('Clock ID', CLOCK_IDS
[int(clock_id
)]))
461 if self
.condattr
& 1:
462 self
.values
.append(('Shared', 'Yes'))
464 self
.values
.append(('Shared', 'No'))
466 class RWLockPrinter(object):
467 """Pretty printer for pthread_rwlock_t."""
469 def __init__(self
, rwlock
):
470 """Initialize the printer's internal data structures.
473 rwlock: A gdb.value representing a pthread_rwlock_t.
476 data
= rwlock
['__data']
477 self
.readers
= data
['__nr_readers']
478 self
.queued_readers
= data
['__nr_readers_queued']
479 self
.queued_writers
= data
['__nr_writers_queued']
480 self
.writer_id
= data
['__writer']
481 self
.shared
= data
['__shared']
482 self
.prefers_writers
= data
['__flags']
489 This is called from gdb when we try to print a pthread_rwlock_t.
492 return 'pthread_rwlock_t'
497 This is called from gdb when we try to print a pthread_rwlock_t.
502 def read_values(self
):
503 """Read the rwlock's info and store it in self.values.
505 The data contained in self.values will be returned by the Iterator
506 created in self.children.
510 self
.read_attributes()
512 def read_status(self
):
513 """Read the status of the rwlock."""
515 # Right now pthread_rwlock_destroy doesn't do anything, so there's no
516 # way to check if an rwlock is destroyed.
519 self
.values
.append(('Status', 'Locked (Write)'))
520 self
.values
.append(('Writer ID', self
.writer_id
))
522 self
.values
.append(('Status', 'Locked (Read)'))
523 self
.values
.append(('Readers', self
.readers
))
525 self
.values
.append(('Status', 'Unlocked'))
527 self
.values
.append(('Queued readers', self
.queued_readers
))
528 self
.values
.append(('Queued writers', self
.queued_writers
))
530 def read_attributes(self
):
531 """Read the attributes of the rwlock."""
534 self
.values
.append(('Shared', 'Yes'))
536 self
.values
.append(('Shared', 'No'))
538 if self
.prefers_writers
:
539 self
.values
.append(('Prefers', 'Writers'))
541 self
.values
.append(('Prefers', 'Readers'))
543 class RWLockAttributesPrinter(object):
544 """Pretty printer for pthread_rwlockattr_t.
546 In the NPTL this is a type that's always casted to
547 struct pthread_rwlockattr, which has two fields ('lockkind' and 'pshared')
548 containing the actual attributes.
551 def __init__(self
, rwlockattr
):
552 """Initialize the printer's internal data structures.
555 rwlockattr: A gdb.value representing a pthread_rwlockattr_t.
561 rwlockattr_struct
= gdb
.lookup_type('struct pthread_rwlockattr')
562 self
.rwlockattr
= rwlockattr
.cast(rwlockattr_struct
)
565 # libpthread doesn't have debug symbols, thus we can't find the
566 # real struct type. Just print the union members.
567 self
.values
.append(('__size', rwlockattr
['__size']))
568 self
.values
.append(('__align', rwlockattr
['__align']))
573 This is called from gdb when we try to print a pthread_rwlockattr_t.
576 return 'pthread_rwlockattr_t'
581 This is called from gdb when we try to print a pthread_rwlockattr_t.
586 def read_values(self
):
587 """Read the rwlockattr's info and store it in self.values.
589 The data contained in self.values will be returned by the Iterator
590 created in self.children.
593 rwlock_type
= self
.rwlockattr
['lockkind']
594 shared
= self
.rwlockattr
['pshared']
596 if shared
== PTHREAD_PROCESS_SHARED
:
597 self
.values
.append(('Shared', 'Yes'))
599 # PTHREAD_PROCESS_PRIVATE
600 self
.values
.append(('Shared', 'No'))
602 if (rwlock_type
== PTHREAD_RWLOCK_PREFER_READER_NP
or
603 rwlock_type
== PTHREAD_RWLOCK_PREFER_WRITER_NP
):
604 # This is a known bug. Using PTHREAD_RWLOCK_PREFER_WRITER_NP will
605 # still make the rwlock prefer readers.
606 self
.values
.append(('Prefers', 'Readers'))
607 elif rwlock_type
== PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP
:
608 self
.values
.append(('Prefers', 'Writers'))
610 def register(objfile
):
611 """Register the pretty printers within the given objfile."""
613 printer
= gdb
.printing
.RegexpCollectionPrettyPrinter('glibc-pthread-locks')
615 printer
.add_printer('pthread_mutex_t', r
'^pthread_mutex_t$',
617 printer
.add_printer('pthread_mutexattr_t', r
'^pthread_mutexattr_t$',
618 MutexAttributesPrinter
)
619 printer
.add_printer('pthread_cond_t', r
'^pthread_cond_t$',
620 ConditionVariablePrinter
)
621 printer
.add_printer('pthread_condattr_t', r
'^pthread_condattr_t$',
622 ConditionVariableAttributesPrinter
)
623 printer
.add_printer('pthread_rwlock_t', r
'^pthread_rwlock_t$',
625 printer
.add_printer('pthread_rwlockattr_t', r
'^pthread_rwlockattr_t$',
626 RWLockAttributesPrinter
)
631 gdb
.printing
.register_pretty_printer(objfile
, printer
)
633 register(gdb
.current_objfile())