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