Debian Codegraph @

src/libsysprof/sysprof-leak-detector.c

raw
1 /* sysprof-leak-detector.c
2 *
3 * Copyright 2023 Christian Hergert <[email protected]>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "config.h"
22
23 #include "sysprof-document-allocation.h"
24 #include "sysprof-document-frame-private.h"
25 #include "sysprof-document-private.h"
26 #include "sysprof-leak-detector-private.h"
27
28 typedef struct _SysprofAllocNode SysprofAllocNode;
29
30 #include "tree.h"
31
32 struct _SysprofAllocNode
33 {
34 RB_ENTRY(_SysprofAllocNode) link;
35 guint64 address;
36 gint64 size;
37 guint pos;
38 };
39
40 static int
41 sysprof_alloc_node_compare (SysprofAllocNode *a,
42 SysprofAllocNode *b)
43 {
44 if (a->address < b->address)
45 return -1;
46 else if (a->address > b->address)
47 return 1;
48 else
49 return 0;
50 }
51
52 typedef struct _SysprofAllocs
53 {
54 RB_HEAD(sysprof_allocs, _SysprofAllocNode) head;
55 } SysprofAllocs;
56
57 RB_GENERATE_STATIC(sysprof_allocs, _SysprofAllocNode, link, sysprof_alloc_node_compare);
58
59 static void
60 sysprof_alloc_node_free (SysprofAllocNode *node)
61 {
62 SysprofAllocNode *right = RB_RIGHT(node, link);
63 SysprofAllocNode *left = RB_LEFT(node, link);
64
65 g_free (left);
66 g_free (right);
67 g_free (node);
68 }
69
70 EggBitset *
71 sysprof_leak_detector_detect (SysprofDocument *document,
72 EggBitset *allocations)
73 {
74 SysprofAllocs allocs;
75 g_autoptr(SysprofDocumentFrame) frame = NULL;
76 g_autoptr(EggBitset) leaks = NULL;
77 g_autoptr(GArray) frames = NULL;
78 const char *base_addr;
79 EggBitsetIter iter;
80 SysprofAllocNode find = {0};
81 SysprofAllocNode *res;
82 guint pos;
83
84 g_return_val_if_fail (SYSPROF_IS_DOCUMENT (document), NULL);
85 g_return_val_if_fail (allocations != NULL, NULL);
86
87 leaks = egg_bitset_new_empty ();
88
89 if (!egg_bitset_iter_init_first (&iter, allocations, &pos))
90 return g_steal_pointer (&leaks);
91
92 frames = _sysprof_document_get_frames (document);
93
94 /* Load the first allocation to look at. To avoid creating lots
95 * of GObjects, we will update the internal data in the alloc
96 * object to point at each subsequence record. That way it's just
97 * used as a demarshalling container.
98 */
99 frame = g_list_model_get_item (G_LIST_MODEL (document), pos);
100 frame->time_offset = 0;
101
102 g_assert (frame != NULL);
103 g_assert (SYSPROF_IS_DOCUMENT_ALLOCATION (frame));
104
105 base_addr = g_mapped_file_get_contents (frame->mapped_file);
106
107 RB_INIT (&allocs.head);
108
109 for (;;)
110 {
111 SysprofDocumentAllocation *alloc = (SysprofDocumentAllocation *)frame;
112 SysprofDocumentFramePointer *ptr = &g_array_index (frames, SysprofDocumentFramePointer, pos);
113 guint64 address = sysprof_document_allocation_get_address (alloc);
114 gint64 size = sysprof_document_allocation_get_size (alloc);
115
116 /* Generally we'd want to take into account the PID as well, but
117 * since we only support recording from LD_PRELOAD currently, there
118 * isn't much of a chance of someone muxing multiple records into
119 * a single capture. Therefore, don't waste the time on it.
120 */
121
122 if (size > 0)
123 {
124 SysprofAllocNode *node = g_new0 (SysprofAllocNode, 1);
125
126 node->pos = pos;
127 node->address = address;
128 node->size = size;
129
130 RB_INSERT (sysprof_allocs, &allocs.head, node);
131 }
132 else
133 {
134 find.address = address;
135
136 if ((res = RB_FIND (sysprof_allocs, &allocs.head, &find)))
137 RB_REMOVE (sysprof_allocs, &allocs.head, res);
138 }
139
140 if (!egg_bitset_iter_next (&iter, &pos))
141 break;
142
143 frame->frame = (SysprofCaptureFrame *)&base_addr[ptr->offset];
144 frame->frame_len = ptr->length;
145 }
146
147 RB_FOREACH (res, sysprof_allocs, &allocs.head) {
148 g_assert (res->size > 0);
149
150 egg_bitset_add (leaks, res->pos);
151 }
152
153 if (RB_ROOT (&allocs.head))
154 sysprof_alloc_node_free (RB_ROOT (&allocs.head));
155
156 return g_steal_pointer (&leaks);
157 }