Debian Codegraph @

src/libsysprof/sysprof-callgraph-symbol.c

raw
1 /* sysprof-callgraph-symbol.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 <gio/gio.h>
24
25 #include "sysprof-callgraph-private.h"
26 #include "sysprof-callgraph-symbol-private.h"
27
28 struct _SysprofCallgraphSymbol
29 {
30 GObject parent_instance;
31 SysprofCallgraph *callgraph;
32 SysprofSymbol *symbol;
33 };
34
35 enum {
36 PROP_0,
37 PROP_CALLGRAPH,
38 PROP_SYMBOL,
39 N_PROPS
40 };
41
42 G_DEFINE_FINAL_TYPE (SysprofCallgraphSymbol, sysprof_callgraph_symbol, G_TYPE_OBJECT)
43
44 static GParamSpec *properties [N_PROPS];
45
46 static void
47 sysprof_callgraph_symbol_finalize (GObject *object)
48 {
49 SysprofCallgraphSymbol *self = (SysprofCallgraphSymbol *)object;
50
51 g_clear_object (&self->callgraph);
52 g_clear_object (&self->symbol);
53
54 G_OBJECT_CLASS (sysprof_callgraph_symbol_parent_class)->finalize (object);
55 }
56
57 static void
58 sysprof_callgraph_symbol_get_property (GObject *object,
59 guint prop_id,
60 GValue *value,
61 GParamSpec *pspec)
62 {
63 SysprofCallgraphSymbol *self = SYSPROF_CALLGRAPH_SYMBOL (object);
64
65 switch (prop_id)
66 {
67 case PROP_CALLGRAPH:
68 g_value_set_object (value, self->callgraph);
69 break;
70
71 case PROP_SYMBOL:
72 g_value_set_object (value, sysprof_callgraph_symbol_get_symbol (self));
73 break;
74
75 default:
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77 }
78 }
79
80 static void
81 sysprof_callgraph_symbol_class_init (SysprofCallgraphSymbolClass *klass)
82 {
83 GObjectClass *object_class = G_OBJECT_CLASS (klass);
84
85 object_class->finalize = sysprof_callgraph_symbol_finalize;
86 object_class->get_property = sysprof_callgraph_symbol_get_property;
87
88 properties [PROP_CALLGRAPH] =
89 g_param_spec_object ("callgraph", NULL, NULL,
90 SYSPROF_TYPE_CALLGRAPH,
91 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
92
93 properties [PROP_SYMBOL] =
94 g_param_spec_object ("symbol", NULL, NULL,
95 SYSPROF_TYPE_SYMBOL,
96 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
97
98 g_object_class_install_properties (object_class, N_PROPS, properties);
99 }
100
101 static void
102 sysprof_callgraph_symbol_init (SysprofCallgraphSymbol *self)
103 {
104 }
105
106 SysprofCallgraphSymbol *
107 _sysprof_callgraph_symbol_new (SysprofCallgraph *callgraph,
108 SysprofSymbol *symbol)
109 {
110 SysprofCallgraphSymbol *self;
111
112 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (callgraph), NULL);
113 g_return_val_if_fail (SYSPROF_IS_SYMBOL (symbol), NULL);
114
115 self = g_object_new (SYSPROF_TYPE_CALLGRAPH_SYMBOL, NULL);
116 g_set_object (&self->callgraph, callgraph);
117 g_set_object (&self->symbol, symbol);
118
119 return self;
120 }
121
122 /**
123 * sysprof_callgraph_symbol_get_symbol:
124 * @self: a #SysprofCallgraphSymbol
125 *
126 * Gets the symbol for the symbol.
127 *
128 * Returns: (nullable) (transfer none): a #SysprofSymbol
129 */
130 SysprofSymbol *
131 sysprof_callgraph_symbol_get_symbol (SysprofCallgraphSymbol *self)
132 {
133 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_SYMBOL (self), NULL);
134
135 return self->symbol;
136 }
137
138 /**
139 * sysprof_callgraph_symbol_get_summary_augment: (skip)
140 * @self: a #SysprofCallgraphSymbol
141 *
142 * Gets the augmentation that was attached to the summary for
143 * the callgraph node's symbol.
144 *
145 * Returns: (nullable) (transfer none): the augmentation data
146 */
147 gpointer
148 sysprof_callgraph_symbol_get_summary_augment (SysprofCallgraphSymbol *self)
149 {
150 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_SYMBOL (self), NULL);
151
152 if (self->callgraph == NULL)
153 return NULL;
154
155 return _sysprof_callgraph_get_symbol_augment (self->callgraph, self->symbol);
156 }
157
158 /**
159 * sysprof_callgraph_symbol_get_callgraph:
160 * @self: a #SysprofCallgraphSymbol
161 *
162 * Gets the callgraph the symbol belongs to.
163 *
164 * Returns: (transfer none) (nullable): a #SysprofCallgraph, or %NULL
165 */
166 SysprofCallgraph *
167 sysprof_callgraph_symbol_get_callgraph (SysprofCallgraphSymbol *self)
168 {
169 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH_SYMBOL (self), NULL);
170
171 return self->callgraph;
172 }
173
174 typedef struct _SysprofCallgraphSymbolListModel
175 {
176 GObject parent_instance;
177 SysprofCallgraph *callgraph;
178 GPtrArray *symbols;
179 } SysprofCallgraphSymbolListModel;
180
181 static guint
182 sysprof_callgraph_symbol_list_model_get_n_items (GListModel *model)
183 {
184 SysprofCallgraphSymbolListModel *self = (SysprofCallgraphSymbolListModel *)model;
185
186 if (self->symbols != NULL)
187 return self->symbols->len;
188
189 return 0;
190 }
191
192 static GType
193 sysprof_callgraph_symbol_list_model_get_item_type (GListModel *model)
194 {
195 return SYSPROF_TYPE_CALLGRAPH_SYMBOL;
196 }
197
198 static gpointer
199 sysprof_callgraph_symbol_list_model_get_item (GListModel *model,
200 guint position)
201 {
202 SysprofCallgraphSymbolListModel *self = (SysprofCallgraphSymbolListModel *)model;
203
204 if (self->symbols == NULL || position >= self->symbols->len || self->callgraph == NULL)
205 return NULL;
206
207 return _sysprof_callgraph_symbol_new (self->callgraph,
208 g_ptr_array_index (self->symbols, position));
209 }
210
211 static void
212 list_model_iface_init (GListModelInterface *iface)
213 {
214 iface->get_n_items = sysprof_callgraph_symbol_list_model_get_n_items;
215 iface->get_item_type = sysprof_callgraph_symbol_list_model_get_item_type;
216 iface->get_item = sysprof_callgraph_symbol_list_model_get_item;
217 }
218
219 #define SYSPROF_TYPE_CALLGRAPH_SYMBOL_LIST_MODEL (sysprof_callgraph_symbol_list_model_get_type())
220 G_DECLARE_FINAL_TYPE (SysprofCallgraphSymbolListModel, sysprof_callgraph_symbol_list_model, SYSPROF, CALLGRAPH_SYMBOL_LIST_MODEL, GObject)
221 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofCallgraphSymbolListModel, sysprof_callgraph_symbol_list_model, G_TYPE_OBJECT,
222 G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
223
224 static void
225 sysprof_callgraph_symbol_list_model_dispose (GObject *object)
226 {
227 SysprofCallgraphSymbolListModel *self = (SysprofCallgraphSymbolListModel *)object;
228
229 g_clear_pointer (&self->symbols, g_ptr_array_unref);
230 g_clear_object (&self->callgraph);
231
232 G_OBJECT_CLASS (sysprof_callgraph_symbol_parent_class)->dispose (object);
233 }
234
235 static void
236 sysprof_callgraph_symbol_list_model_class_init (SysprofCallgraphSymbolListModelClass *klass)
237 {
238 GObjectClass *object_class = G_OBJECT_CLASS (klass);
239
240 object_class->dispose = sysprof_callgraph_symbol_list_model_dispose;
241 }
242
243 static void
244 sysprof_callgraph_symbol_list_model_init (SysprofCallgraphSymbolListModel *self)
245 {
246 }
247
248 GListModel *
249 _sysprof_callgraph_symbol_list_model_new (SysprofCallgraph *callgraph,
250 GPtrArray *symbols)
251 {
252 SysprofCallgraphSymbolListModel *self;
253
254 g_return_val_if_fail (SYSPROF_IS_CALLGRAPH (callgraph), NULL);
255
256 self = g_object_new (SYSPROF_TYPE_CALLGRAPH_SYMBOL_LIST_MODEL, NULL);
257 g_set_object (&self->callgraph, callgraph);
258
259 if (symbols != NULL)
260 self->symbols = g_ptr_array_ref (symbols);
261
262 return G_LIST_MODEL (self);
263 }
264
265 G_END_DECLS