Debian Codegraph @

src/libsysprof/sysprof-symbol.c

raw
1 /*
2 * sysprof-symbol.c
3 *
4 * Copyright 2023 Christian Hergert <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GPL-3.0-or-later
20 */
21
22 #include "config.h"
23
24 #include "sysprof-symbol-private.h"
25
26 G_DEFINE_FINAL_TYPE (SysprofSymbol, sysprof_symbol, G_TYPE_OBJECT)
27
28 enum {
29 PROP_0,
30 PROP_BINARY_NICK,
31 PROP_BINARY_PATH,
32 PROP_KIND,
33 PROP_NAME,
34 PROP_TOOLTIP_TEXT,
35 N_PROPS
36 };
37
38 static GParamSpec *properties [N_PROPS];
39
40 static void
41 sysprof_symbol_finalize (GObject *object)
42 {
43 SysprofSymbol *self = (SysprofSymbol *)object;
44
45 g_clear_pointer (&self->name, g_ref_string_release);
46 g_clear_pointer (&self->binary_path, g_ref_string_release);
47 g_clear_pointer (&self->binary_nick, g_ref_string_release);
48
49 G_OBJECT_CLASS (sysprof_symbol_parent_class)->finalize (object);
50 }
51
52 static void
53 sysprof_symbol_get_property (GObject *object,
54 guint prop_id,
55 GValue *value,
56 GParamSpec *pspec)
57 {
58 SysprofSymbol *self = SYSPROF_SYMBOL (object);
59
60 switch (prop_id)
61 {
62 case PROP_NAME:
63 g_value_set_string (value, sysprof_symbol_get_name (self));
64 break;
65
66 case PROP_BINARY_NICK:
67 g_value_set_string (value, sysprof_symbol_get_binary_nick (self));
68 break;
69
70 case PROP_BINARY_PATH:
71 g_value_set_string (value, sysprof_symbol_get_binary_path (self));
72 break;
73
74 case PROP_KIND:
75 g_value_set_enum (value, sysprof_symbol_get_kind (self));
76 break;
77
78 case PROP_TOOLTIP_TEXT:
79 g_value_take_string (value, sysprof_symbol_dup_tooltip_text (self));
80 break;
81
82 default:
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
84 }
85 }
86
87 static void
88 sysprof_symbol_class_init (SysprofSymbolClass *klass)
89 {
90 GObjectClass *object_class = G_OBJECT_CLASS (klass);
91
92 object_class->finalize = sysprof_symbol_finalize;
93 object_class->get_property = sysprof_symbol_get_property;
94
95 properties [PROP_NAME] =
96 g_param_spec_string ("name", NULL, NULL,
97 NULL,
98 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
99
100 properties [PROP_BINARY_NICK] =
101 g_param_spec_string ("binary-nick", NULL, NULL,
102 NULL,
103 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
104
105 properties [PROP_BINARY_PATH] =
106 g_param_spec_string ("binary-path", NULL, NULL,
107 NULL,
108 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
109
110 properties [PROP_TOOLTIP_TEXT] =
111 g_param_spec_string ("tooltip-text", NULL, NULL,
112 NULL,
113 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
114
115 properties [PROP_KIND] =
116 g_param_spec_enum ("kind", NULL, NULL,
117 SYSPROF_TYPE_SYMBOL_KIND,
118 SYSPROF_SYMBOL_KIND_USER,
119 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
120
121 g_object_class_install_properties (object_class, N_PROPS, properties);
122 }
123
124 static void
125 sysprof_symbol_init (SysprofSymbol *self)
126 {
127 self->kind = SYSPROF_SYMBOL_KIND_USER;
128 }
129
130 const char *
131 sysprof_symbol_get_name (SysprofSymbol *self)
132 {
133 g_return_val_if_fail (SYSPROF_IS_SYMBOL (self), NULL);
134
135 return self->name;
136 }
137
138 const char *
139 sysprof_symbol_get_binary_nick (SysprofSymbol *self)
140 {
141 g_return_val_if_fail (SYSPROF_IS_SYMBOL (self), NULL);
142
143 return self->binary_nick;
144 }
145
146 const char *
147 sysprof_symbol_get_binary_path (SysprofSymbol *self)
148 {
149 g_return_val_if_fail (SYSPROF_IS_SYMBOL (self), NULL);
150
151 return self->binary_path;
152 }
153
154 static gboolean
155 sniff_maybe_kernel_process (const char *str)
156 {
157 if (g_str_has_prefix (str, "kworker/") ||
158 g_str_equal (str, "rcu_preempt") ||
159 g_str_has_prefix (str, "migration/") ||
160 g_str_has_prefix (str, "dmcrypt_write/") ||
161 g_str_has_prefix (str, "hwrng") ||
162 g_str_has_prefix (str, "irq/") ||
163 g_str_has_prefix (str, "ksoftirqd/"))
164 return TRUE;
165
166 return FALSE;
167 }
168
169 SysprofSymbol *
170 _sysprof_symbol_new (GRefString *name,
171 GRefString *binary_path,
172 GRefString *binary_nick,
173 SysprofAddress begin_address,
174 SysprofAddress end_address,
175 SysprofSymbolKind kind)
176 {
177 SysprofSymbol *self;
178
179 if (binary_nick != NULL && binary_nick[0] == 0)
180 binary_nick = NULL;
181
182 if (binary_path != NULL && binary_path[0] == 0)
183 binary_path = NULL;
184
185 self = g_object_new (SYSPROF_TYPE_SYMBOL, NULL);
186 self->name = name;
187 self->binary_path = binary_path;
188 self->binary_nick = binary_nick;
189 self->begin_address = begin_address;
190 self->end_address = end_address;
191 self->simple_hash = g_str_hash (name);
192 self->kind = kind;
193
194 if (self->kind == SYSPROF_SYMBOL_KIND_PROCESS)
195 self->is_kernel_process = sniff_maybe_kernel_process (name);
196
197 if (binary_nick != NULL)
198 self->simple_hash ^= g_str_hash (binary_nick);
199
200 self->hash = self->simple_hash;
201
202 /* If we got a path for the symbol, add that to the hash so that we
203 * can be sure that we're working with a symbol in the same file when
204 * there are collisions.
205 *
206 * That way, we have a chance of joining symbols from different runtimes
207 * and/or containers, but only if they are reasonably the same ABI.
208 */
209 if (binary_path != NULL)
210 {
211 const char *base = strrchr (binary_path, '/');
212
213 if (base != NULL)
214 self->hash ^= g_str_hash (base);
215 }
216
217 return self;
218 }
219
220 gboolean
221 sysprof_symbol_equal (const SysprofSymbol *a,
222 const SysprofSymbol *b)
223 {
224 return _sysprof_symbol_equal (a, b);
225 }
226
227 guint
228 sysprof_symbol_hash (const SysprofSymbol *self)
229 {
230 return self->hash;
231 }
232
233 SysprofSymbolKind
234 sysprof_symbol_get_kind (SysprofSymbol *self)
235 {
236 return self ? self->kind : 0;
237 }
238
239 /**
240 * sysprof_symbol_dup_tooltip_text:
241 * @self: a #SysprofSymbol
242 *
243 * Returns: (transfer full): the tooltip text
244 */
245 char *
246 sysprof_symbol_dup_tooltip_text (SysprofSymbol *self)
247 {
248 GString *str;
249
250 g_return_val_if_fail (SYSPROF_IS_SYMBOL (self), NULL);
251
252 str = g_string_new (self->name);
253
254 if (!g_str_has_prefix (str->str, "In File") && self->binary_path)
255 g_string_append_printf (str, " [%s+0x%"G_GINT64_MODIFIER"x]", self->binary_path, self->begin_address);
256
257 return g_string_free (str, FALSE);
258 }
259
260 G_DEFINE_ENUM_TYPE (SysprofSymbolKind, sysprof_symbol_kind,
261 G_DEFINE_ENUM_VALUE (SYSPROF_SYMBOL_KIND_ROOT, "root"),
262 G_DEFINE_ENUM_VALUE (SYSPROF_SYMBOL_KIND_PROCESS, "process"),
263 G_DEFINE_ENUM_VALUE (SYSPROF_SYMBOL_KIND_CONTEXT_SWITCH, "context-switch"),
264 G_DEFINE_ENUM_VALUE (SYSPROF_SYMBOL_KIND_USER, "user"),
265 G_DEFINE_ENUM_VALUE (SYSPROF_SYMBOL_KIND_KERNEL, "kernel"),
266 G_DEFINE_ENUM_VALUE (SYSPROF_SYMBOL_KIND_UNWINDABLE, "unwindable"))