xref: /llvm-project/lldb/examples/summaries/sp_cp.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1"""
2Summary and synthetic providers for LLDB-specific shared pointers
3
4Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5See https://llvm.org/LICENSE.txt for license information.
6SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7"""
8
9
10class SharedPtr_SyntheticChildrenProvider:
11    def __init__(self, valobj, dict):
12        self.valobj = valobj
13        self.update()
14
15    def update(self):
16        pass
17
18    def num_children(self):
19        return 1
20
21    def get_child_index(self, name):
22        if name == "ptr":
23            return 0
24        if name == "count":
25            return 1
26        return None
27
28    def get_child_at_index(self, index):
29        if index == 0:
30            return self.valobj.GetChildMemberWithName("_M_ptr")
31        if index == 1:
32            return (
33                self.valobj.GetChildMemberWithName("_M_refcount")
34                .GetChildMemberWithName("_M_pi")
35                .GetChildMemberWithName("_M_use_count")
36            )
37        return None
38
39
40def SharedPtr_SummaryProvider(valobj, dict):
41    return "use = " + str(valobj.GetChildMemberWithName("count").GetValueAsUnsigned())
42
43
44class ValueObjectSP_SyntheticChildrenProvider:
45    def __init__(self, valobj, dict):
46        self.valobj = valobj
47        self.update()
48
49    def update(self):
50        pass
51
52    def num_children(self):
53        return 1
54
55    def get_child_index(self, name):
56        if name == "ptr":
57            return 0
58        if name == "count":
59            return 1
60        return None
61
62    def get_child_at_index(self, index):
63        if index == 0:
64            return self.valobj.GetChildMemberWithName("ptr_")
65        if index == 1:
66            return self.valobj.GetChildMemberWithName("cntrl_").GetChildMemberWithName(
67                "shared_owners_"
68            )
69        return None
70
71
72def ValueObjectSP_SummaryProvider(valobj, dict):
73    return "use = " + str(
74        1 + valobj.GetChildMemberWithName("count").GetValueAsUnsigned()
75    )
76
77
78def __lldb_init_module(debugger, dict):
79    debugger.HandleCommand(
80        'type summary add -x ".*ValueObjectSP" --expand -F sp_cp.ValueObjectSP_SummaryProvider'
81    )
82    debugger.HandleCommand(
83        'type synthetic add -x ".*ValueObjectSP" -l sp_cp.ValueObjectSP_SyntheticChildrenProvider'
84    )
85    debugger.HandleCommand(
86        'type summary add -x ".*SP" --expand -F sp_cp.SharedPtr_SummaryProvider'
87    )
88    debugger.HandleCommand(
89        'type synthetic add -x ".*SP" -l sp_cp.SharedPtr_SyntheticChildrenProvider'
90    )
91