xref: /xnu-11417.101.15/doc/debugging/task_ref.md (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS DistributionsTask References
2*e3723e1fSApple OSS Distributions===============
3*e3723e1fSApple OSS Distributions
4*e3723e1fSApple OSS DistributionsFinding the source of task reference count leaks.
5*e3723e1fSApple OSS Distributions
6*e3723e1fSApple OSS DistributionsBackground
7*e3723e1fSApple OSS Distributions----------
8*e3723e1fSApple OSS Distributions
9*e3723e1fSApple OSS DistributionsTasks in XNU are reference counted. When a task is created it starts with two
10*e3723e1fSApple OSS Distributionsreferences - one for the caller and one for the task itself. Over the lifetime
11*e3723e1fSApple OSS Distributionsof the task this reference count is modified, for example when a thread is
12*e3723e1fSApple OSS Distributionscreated it increments the reference count and when it exits that count drops.
13*e3723e1fSApple OSS DistributionsWhen a reference count reaches zero, the task is freed.
14*e3723e1fSApple OSS Distributions
15*e3723e1fSApple OSS DistributionsTo grab a reference:
16*e3723e1fSApple OSS Distributions```c
17*e3723e1fSApple OSS Distributionstask_reference()
18*e3723e1fSApple OSS Distributions```
19*e3723e1fSApple OSS Distributions
20*e3723e1fSApple OSS DistributionsTo release a reference:
21*e3723e1fSApple OSS Distributions```c
22*e3723e1fSApple OSS Distributionstask_deallocate()
23*e3723e1fSApple OSS Distributions```
24*e3723e1fSApple OSS Distributions
25*e3723e1fSApple OSS DistributionsOne of the big problems seen with task references is that difficult to debug
26*e3723e1fSApple OSS Distributions_leaks_ commonly occur. This happens when a reference is taken but never
27*e3723e1fSApple OSS Distributionsreleased. The task is kept around indefinitely and eventually the system runs
28*e3723e1fSApple OSS Distributionsout of a finite resource (for example ASIDs). At this point there is very little
29*e3723e1fSApple OSS Distributionsinformation to determine what code was responsible for the leak.
30*e3723e1fSApple OSS Distributions
31*e3723e1fSApple OSS Distributions
32*e3723e1fSApple OSS DistributionsTask Reference Groups
33*e3723e1fSApple OSS Distributions--------------------
34*e3723e1fSApple OSS Distributions
35*e3723e1fSApple OSS DistributionsReference groups are a feature which keep track of statistics (and when
36*e3723e1fSApple OSS Distributionsconfigured backtrace information) for a set of references. Reference groups are
37*e3723e1fSApple OSS Distributionshierarchical. To help with debugging the following task reference group
38*e3723e1fSApple OSS Distributionshierarchy is used:
39*e3723e1fSApple OSS Distributions
40*e3723e1fSApple OSS Distributions```
41*e3723e1fSApple OSS Distributionstask
42*e3723e1fSApple OSS Distributions   -> task_internal
43*e3723e1fSApple OSS Distributions      -> task_local_internal
44*e3723e1fSApple OSS Distributions   -> task_kernel
45*e3723e1fSApple OSS Distributions      -> task_local_internal
46*e3723e1fSApple OSS Distributions   -> task_mig
47*e3723e1fSApple OSS Distributions      -> task_local_internal
48*e3723e1fSApple OSS Distributions   -> task_external
49*e3723e1fSApple OSS Distributions      -> task_local_external
50*e3723e1fSApple OSS Distributions      -> task_com.apple.security.sandbox
51*e3723e1fSApple OSS Distributions          -> task_com.apple.security.sandbox
52*e3723e1fSApple OSS Distributions      -> task_com.apple.driver.AppleHV
53*e3723e1fSApple OSS Distributions          -> task_com.apple.driver.AppleHV
54*e3723e1fSApple OSS Distributions      ...
55*e3723e1fSApple OSS Distributions```
56*e3723e1fSApple OSS Distributions
57*e3723e1fSApple OSS DistributionsThe `task` group contains a count of all task references in the system. The
58*e3723e1fSApple OSS Distributionsfirst-level groups are static and sub-divide task references based on the
59*e3723e1fSApple OSS Distributionssub-system they come from. `task_external` is used for kext references and each
60*e3723e1fSApple OSS Distributionskext will be dynamically assigned a reference group as needed (if there's
61*e3723e1fSApple OSS Distributionsone available). At the bottom level, there's a per-task (local) ref group under
62*e3723e1fSApple OSS Distributionseach global group.
63*e3723e1fSApple OSS DistributionsThe exact hierarchy of task references (specifically what per-task reference
64*e3723e1fSApple OSS Distributionsgroups are created) changes depending on the 'task_refgrp' boot arg.
65*e3723e1fSApple OSS Distributions
66*e3723e1fSApple OSS DistributionsTask reference groups can be explored in `lldb` as follows:
67*e3723e1fSApple OSS Distributions
68*e3723e1fSApple OSS Distributions```
69*e3723e1fSApple OSS Distributions(lldb) showglobaltaskrefgrps
70*e3723e1fSApple OSS Distributionsos_refgrp          name                                           count     retain    release   log
71*e3723e1fSApple OSS Distributions0xffffff801ace9250 task_kernel                                    68        367663    367595    0x0
72*e3723e1fSApple OSS Distributions0xffffff801ace9288 task_internal                                  974       4953      3979      0x0
73*e3723e1fSApple OSS Distributions0xffffff801ace92c0 task_mig                                       0         3670      3670      0x0
74*e3723e1fSApple OSS Distributions0xffffff801ace9218 task_external                                  35        108       73        0x0
75*e3723e1fSApple OSS Distributions0xffffff9369dc7b20 task_com.apple.iokit.IOAcceleratorFamily2      29        77        48        0x0
76*e3723e1fSApple OSS Distributions0xffffff936a3f0a20 task_com.apple.iokit.CoreAnalyticsFamily       1         1         0         0x0
77*e3723e1fSApple OSS Distributions0xffffff936a22cb20 task_com.apple.iokit.EndpointSecurity          0         1         1         0x0
78*e3723e1fSApple OSS Distributions0xffffff936a283f60 task_com.apple.iokit.IOSurface                 5         5         0         0x0
79*e3723e1fSApple OSS Distributions0xffffff936a3f08a0 task_com.apple.security.sandbox                0         24        24        0x0
80*e3723e1fSApple OSS Distributions
81*e3723e1fSApple OSS Distributions```
82*e3723e1fSApple OSS Distributions
83*e3723e1fSApple OSS DistributionsDisplay a task's reference groups:
84*e3723e1fSApple OSS Distributions
85*e3723e1fSApple OSS Distributions```
86*e3723e1fSApple OSS Distributions(lldb) showtaskrefgrps kernel_task
87*e3723e1fSApple OSS Distributionsos_refgrp          name                                           count     retain    release   log
88*e3723e1fSApple OSS Distributions0xffffff936a4b9200 task_local_kernel                              1         6         5         0x0
89*e3723e1fSApple OSS Distributions0xffffff936a4b9238 task_local_internal                            132       619       487       0x0
90*e3723e1fSApple OSS Distributions```
91*e3723e1fSApple OSS Distributions
92*e3723e1fSApple OSS DistributionsThe reference group hierarchy for a specific group can be displayed as follows:
93*e3723e1fSApple OSS Distributions
94*e3723e1fSApple OSS Distributions```
95*e3723e1fSApple OSS Distributions(lldb) showosrefgrphierarchy 0xffffff936a3f08a0
96*e3723e1fSApple OSS Distributions0xffffff801ace9988 all                                            1121      377740    376619    0x0
97*e3723e1fSApple OSS Distributions0xffffff801ace91e0 task                                           1077      376394    375317    0x0
98*e3723e1fSApple OSS Distributions0xffffff801ace9218 task_external                                  35        108       73        0x0
99*e3723e1fSApple OSS Distributions0xffffff936a3f08a0 task_com.apple.security.sandbox                0         24        24        0x0
100*e3723e1fSApple OSS Distributions```
101*e3723e1fSApple OSS Distributions
102*e3723e1fSApple OSS DistributionsReference groups are normally disabled, but task reference group statistics
103*e3723e1fSApple OSS Distributions*are* enabled by default (for `RELEASE` builds, reference groups are not available
104*e3723e1fSApple OSS Distributionsat all). Backtrace logging for all groups is disabled, including task reference
105*e3723e1fSApple OSS Distributionsgroups. To enable backtrace logging and reference group statistics, the `rlog`
106*e3723e1fSApple OSS Distributionsboot-arg must be used. Backtrace logging for task reference groups is only
107*e3723e1fSApple OSS Distributionsenabled when `rlog` has been set to a suitable value.
108*e3723e1fSApple OSS Distributions
109*e3723e1fSApple OSS DistributionsFor example
110*e3723e1fSApple OSS Distributions
111*e3723e1fSApple OSS DistributionsTo enable statistics for all reference groups and backtrace logging for the
112*e3723e1fSApple OSS Distributions*task_external* reference group in particular:
113*e3723e1fSApple OSS Distributions
114*e3723e1fSApple OSS Distributions```
115*e3723e1fSApple OSS Distributionsnvram boot-args="rlog=task_external ..."
116*e3723e1fSApple OSS Distributions```
117*e3723e1fSApple OSS Distributions
118*e3723e1fSApple OSS Distributions```
119*e3723e1fSApple OSS Distributions(lldb) showglobaltaskrefgrps
120*e3723e1fSApple OSS Distributionsos_refgrp          name                                           count     retain    release   log
121*e3723e1fSApple OSS Distributions0xffffff801e0e9250 task_kernel                                    1259      132739    131480    0x0
122*e3723e1fSApple OSS Distributions0xffffff801e0e9218 task_external                                  35        100       65        0xffffffa05b3fc000
123*e3723e1fSApple OSS Distributions0xffffff936d117be0 task_com.apple.iokit.IOAcceleratorFamily2      29        77        48        0x0
124*e3723e1fSApple OSS Distributions0xffffff936db9fa20 task_com.apple.iokit.CoreAnalyticsFamily       1         1         0         0x0
125*e3723e1fSApple OSS Distributions0xffffff936d9dbb20 task_com.apple.iokit.EndpointSecurity          0         1         1         0x0
126*e3723e1fSApple OSS Distributions0xffffff936da324e0 task_com.apple.iokit.IOSurface                 5         5         0         0x0
127*e3723e1fSApple OSS Distributions0xffffff936db9f8a0 task_com.apple.security.sandbox                0         16        16        0x0
128*e3723e1fSApple OSS Distributions
129*e3723e1fSApple OSS Distributions
130*e3723e1fSApple OSS Distributions(lldb) showbtlogrecords 0xffffffa05b3fc000
131*e3723e1fSApple OSS Distributions-------- OP 1 Stack Index 0 with active refs 1 of 165 --------
132*e3723e1fSApple OSS Distributions0xffffff801da7c1cb <kernel.development`ref_log_op at refcnt.c:107>
133*e3723e1fSApple OSS Distributions0xffffff801d27c35d <kernel.development`task_reference_grp at task_ref.c:274>
134*e3723e1fSApple OSS Distributions0xffffff801ecc014e <EndpointSecurity`VMMap::taskSelf()>
135*e3723e1fSApple OSS Distributions0xffffff801eccc845 <EndpointSecurity`EndpointSecurityClient::create(ScopedPointer<MachSendWrapper> const&, proc*, ScopedPointer<EndpointSecurityExternalClient> const&, es_client_config_t const&)>
136*e3723e1fSApple OSS Distributions...
137*e3723e1fSApple OSS Distributions```
138