xref: /xnu-11417.121.6/tools/lldbmacros/counter.py (revision a1e26a70f38d1d7daa7b49b258e2f8538ad81650)
1*a1e26a70SApple OSS Distributionsfrom memory import IterateZPerCPU
2*a1e26a70SApple OSS Distributionsfrom xnu import (
3*a1e26a70SApple OSS Distributions    LazyTarget, value, ArgumentError,
4*a1e26a70SApple OSS Distributions    lldb_command, lldb_type_summary, header
5*a1e26a70SApple OSS Distributions)
6*a1e26a70SApple OSS Distributions
7*a1e26a70SApple OSS Distributions
8*a1e26a70SApple OSS Distributions@lldb_type_summary(['scalable_counter_t'])
9*a1e26a70SApple OSS Distributions@header("Counter Value\n-------------")
10*a1e26a70SApple OSS Distributionsdef GetSimpleCounter(counter):
11*a1e26a70SApple OSS Distributions    """ Prints out the value of a percpu counter
12*a1e26a70SApple OSS Distributions        params: counter: value - value object representing counter
13*a1e26a70SApple OSS Distributions        returns: str - THe value of the counter as a string.
14*a1e26a70SApple OSS Distributions    """
15*a1e26a70SApple OSS Distributions    val = 0
16*a1e26a70SApple OSS Distributions    for v in IterateZPerCPU(counter):
17*a1e26a70SApple OSS Distributions        val += v
18*a1e26a70SApple OSS Distributions    return str(val)
19*a1e26a70SApple OSS Distributions
20*a1e26a70SApple OSS Distributions@lldb_command('showcounter')
21*a1e26a70SApple OSS Distributionsdef ShowSimpleCounter(cmd_args=None):
22*a1e26a70SApple OSS Distributions    """ Show the value of a percpu counter.
23*a1e26a70SApple OSS Distributions        Usage: showcounter <address of counter>
24*a1e26a70SApple OSS Distributions    """
25*a1e26a70SApple OSS Distributions    if cmd_args is None or len(cmd_args) == 0:
26*a1e26a70SApple OSS Distributions        raise ArgumentError("Please specify the address of the "
27*a1e26a70SApple OSS Distributions                            "counter you want to read.")
28*a1e26a70SApple OSS Distributions
29*a1e26a70SApple OSS Distributions    val = LazyTarget.GetTarget().chkCreateValueFromExpression(
30*a1e26a70SApple OSS Distributions        'value', f"(scalable_counter_t){cmd_args[0]}")
31*a1e26a70SApple OSS Distributions    print(GetSimpleCounter(value(val)))
32