scripts/gdb: add $lx_clk_core_lookup function
[sfrench/cifs-2.6.git] / scripts / gdb / linux / clk.py
1 # SPDX-License-Identifier: GPL-2.0
2 #
3 # Copyright (c) NXP 2019
4
5 import gdb
6 import sys
7
8 from linux import utils, lists
9
10 clk_core_type = utils.CachedType("struct clk_core")
11
12
13 def clk_core_for_each_child(hlist_head):
14     return lists.hlist_for_each_entry(hlist_head,
15             clk_core_type.get_type().pointer(), "child_node")
16
17
18 class LxClkSummary(gdb.Command):
19     """Print Linux kernel log buffer."""
20
21     def __init__(self):
22         super(LxClkSummary, self).__init__("lx-clk-summary", gdb.COMMAND_DATA)
23
24     def show_subtree(self, clk, level):
25         gdb.write("%*s%-*s %7d %8d %8d\n" % (
26                 level * 3 + 1, "",
27                 30 - level * 3,
28                 clk['name'].string(),
29                 clk['enable_count'],
30                 clk['prepare_count'],
31                 clk['protect_count']))
32
33         for child in clk_core_for_each_child(clk['children']):
34             self.show_subtree(child, level + 1)
35
36     def invoke(self, arg, from_tty):
37         gdb.write("                                 enable  prepare  protect\n")
38         gdb.write("   clock                          count    count    count\n")
39         gdb.write("---------------------------------------------------------\n")
40         for clk in clk_core_for_each_child(gdb.parse_and_eval("clk_root_list")):
41             self.show_subtree(clk, 0)
42         for clk in clk_core_for_each_child(gdb.parse_and_eval("clk_orphan_list")):
43             self.show_subtree(clk, 0)
44
45
46 LxClkSummary()
47
48
49 class LxClkCoreLookup(gdb.Function):
50     """Find struct clk_core by name"""
51
52     def __init__(self):
53         super(LxClkCoreLookup, self).__init__("lx_clk_core_lookup")
54
55     def lookup_hlist(self, hlist_head, name):
56         for child in clk_core_for_each_child(hlist_head):
57             if child['name'].string() == name:
58                 return child
59             result = self.lookup_hlist(child['children'], name)
60             if result:
61                 return result
62
63     def invoke(self, name):
64         name = name.string()
65         return (self.lookup_hlist(gdb.parse_and_eval("clk_root_list"), name) or
66                 self.lookup_hlist(gdb.parse_and_eval("clk_orphan_list"), name))
67
68
69 LxClkCoreLookup()