xref: /xnu-8796.121.2/tools/lldbmacros/ulock.py (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributionsfrom __future__ import absolute_import, print_function
2*c54f35caSApple OSS Distributions
3*c54f35caSApple OSS Distributionsfrom builtins import range
4*c54f35caSApple OSS Distributions
5*c54f35caSApple OSS Distributionsfrom xnu import *
6*c54f35caSApple OSS Distributionsfrom scheduler import GetRecentTimestamp
7*c54f35caSApple OSS Distributionsimport xnudefines
8*c54f35caSApple OSS Distributions
9*c54f35caSApple OSS Distributionsulock_types = {
10*c54f35caSApple OSS Distributions    1: "COMPARE_AND_WAIT",
11*c54f35caSApple OSS Distributions    2: "UNFAIR_LOCK",
12*c54f35caSApple OSS Distributions    3: "UNFAIR_LOCK64_SHARED",
13*c54f35caSApple OSS Distributions    4: "COMPARE_AND_WAIT64",
14*c54f35caSApple OSS Distributions    5: "COMPARE_AND_WAIT64_SHARED"
15*c54f35caSApple OSS Distributions}
16*c54f35caSApple OSS Distributions
17*c54f35caSApple OSS Distributions@header("{:<20s} {:<20s} {:<20s} {:<10s} {:<20s} {:<20s} {:<20s}".format(
18*c54f35caSApple OSS Distributions    'ull_t', 'kind', 'addr/obj', 'pid/offs', 'owner', 'turnstile', 'waiters'))
19*c54f35caSApple OSS Distributionsdef GetUlockSummary(ull):
20*c54f35caSApple OSS Distributions    code = int(ull.ull_opcode)
21*c54f35caSApple OSS Distributions    if code in ulock_types:
22*c54f35caSApple OSS Distributions        ull_type = ulock_types[code]
23*c54f35caSApple OSS Distributions    else:
24*c54f35caSApple OSS Distributions        ull_type = "{:#x}".format(code)
25*c54f35caSApple OSS Distributions
26*c54f35caSApple OSS Distributions    s = "{ull: <#20x} {ull_type: <20s}".format(ull=ull, ull_type=ull_type)
27*c54f35caSApple OSS Distributions    ulk=ull.ull_key
28*c54f35caSApple OSS Distributions    if int(ulk.ulk_key_type) == 1:
29*c54f35caSApple OSS Distributions        s += " {ulk.ulk_addr: <#20x} {ulk.ulk_pid: <10d}".format(ulk=ulk)
30*c54f35caSApple OSS Distributions    elif int(ulk.ulk_key_type) == 2:
31*c54f35caSApple OSS Distributions        s += " {ulk.ulk_object: <#20x} {ulk.ulk_offset: <10d}".format(ulk=ulk)
32*c54f35caSApple OSS Distributions    else:
33*c54f35caSApple OSS Distributions        s += " {:<20s} {:<10s}".format("", "")
34*c54f35caSApple OSS Distributions
35*c54f35caSApple OSS Distributions    return s + " {ull.ull_owner: <#20x} {ull.ull_turnstile: <#20x} {ull.ull_nwaiters: >7d}".format(ull=ull)
36*c54f35caSApple OSS Distributions
37*c54f35caSApple OSS Distributions@lldb_command('showallulocks', fancy=True)
38*c54f35caSApple OSS Distributionsdef ShowAllUlocks(cmd_args=None, cmd_options={}, O=None):
39*c54f35caSApple OSS Distributions    """ Display a summary of all the ulocks in the system
40*c54f35caSApple OSS Distributions
41*c54f35caSApple OSS Distributions        usage: showallulocks
42*c54f35caSApple OSS Distributions    """
43*c54f35caSApple OSS Distributions
44*c54f35caSApple OSS Distributions    with O.table(GetUlockSummary.header):
45*c54f35caSApple OSS Distributions        count = kern.globals.ull_hash_buckets;
46*c54f35caSApple OSS Distributions        buckets = kern.globals.ull_bucket
47*c54f35caSApple OSS Distributions        for i in range(0, count):
48*c54f35caSApple OSS Distributions            for ull in IterateLinkageChain(addressof(buckets[i].ulb_head), 'ull_t *', 'ull_hash_link'):
49*c54f35caSApple OSS Distributions                print(GetUlockSummary(ull))
50