Debian Codegraph @

src/libsysprof/sysprof-document-process.c

raw
1 /* sysprof-document-process.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 <glib/gi18n.h>
24
25 #include "sysprof-document-frame-private.h"
26 #include "sysprof-document-process-private.h"
27 #include "sysprof-mount.h"
28 #include "sysprof-symbol-private.h"
29 #include "sysprof-thread-info.h"
30
31 struct _SysprofDocumentProcess
32 {
33 SysprofDocumentFrame parent_instance;
34 SysprofProcessInfo *process_info;
35 };
36
37 struct _SysprofDocumentProcessClass
38 {
39 SysprofDocumentFrameClass parent_class;
40 };
41
42 enum {
43 PROP_0,
44 PROP_COMMAND_LINE,
45 PROP_DURATION,
46 PROP_MEMORY_MAPS,
47 PROP_MOUNTS,
48 PROP_EXIT_TIME,
49 PROP_THREADS,
50 PROP_TITLE,
51 N_PROPS
52 };
53
54 G_DEFINE_FINAL_TYPE (SysprofDocumentProcess, sysprof_document_process, SYSPROF_TYPE_DOCUMENT_FRAME)
55
56 static GParamSpec *properties [N_PROPS];
57
58 static void
59 sysprof_document_process_finalize (GObject *object)
60 {
61 SysprofDocumentProcess *self = (SysprofDocumentProcess *)object;
62
63 g_clear_pointer (&self->process_info, sysprof_process_info_unref);
64
65 G_OBJECT_CLASS (sysprof_document_process_parent_class)->finalize (object);
66 }
67
68 static void
69 sysprof_document_process_get_property (GObject *object,
70 guint prop_id,
71 GValue *value,
72 GParamSpec *pspec)
73 {
74 SysprofDocumentProcess *self = SYSPROF_DOCUMENT_PROCESS (object);
75
76 switch (prop_id)
77 {
78 case PROP_COMMAND_LINE:
79 g_value_set_string (value, sysprof_document_process_get_command_line (self));
80 break;
81
82 case PROP_DURATION:
83 g_value_set_int64 (value, sysprof_document_process_get_duration (self));
84 break;
85
86 case PROP_EXIT_TIME:
87 g_value_set_int64 (value, sysprof_document_process_get_exit_time (self));
88 break;
89
90 case PROP_MEMORY_MAPS:
91 g_value_take_object (value, sysprof_document_process_list_memory_maps (self));
92 break;
93
94 case PROP_MOUNTS:
95 g_value_take_object (value, sysprof_document_process_list_mounts (self));
96 break;
97
98 case PROP_THREADS:
99 g_value_take_object (value, sysprof_document_process_list_threads (self));
100 break;
101
102 case PROP_TITLE:
103 g_value_take_string (value, sysprof_document_process_dup_title (self));
104 break;
105
106 default:
107 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
108 }
109 }
110
111 static void
112 sysprof_document_process_class_init (SysprofDocumentProcessClass *klass)
113 {
114 GObjectClass *object_class = G_OBJECT_CLASS (klass);
115
116 object_class->finalize = sysprof_document_process_finalize;
117 object_class->get_property = sysprof_document_process_get_property;
118
119 properties [PROP_COMMAND_LINE] =
120 g_param_spec_string ("command-line", NULL, NULL,
121 NULL,
122 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
123
124 properties [PROP_DURATION] =
125 g_param_spec_int64 ("duration", NULL, NULL,
126 G_MININT64, G_MAXINT64, 0,
127 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
128
129 properties [PROP_EXIT_TIME] =
130 g_param_spec_int64 ("exit-time", NULL, NULL,
131 G_MININT64, G_MAXINT64, 0,
132 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
133
134 properties [PROP_MEMORY_MAPS] =
135 g_param_spec_object ("memory-maps", NULL, NULL,
136 G_TYPE_LIST_MODEL,
137 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
138
139 properties [PROP_MOUNTS] =
140 g_param_spec_object ("mounts", NULL, NULL,
141 G_TYPE_LIST_MODEL,
142 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
143
144 properties [PROP_THREADS] =
145 g_param_spec_object ("threads", NULL, NULL,
146 G_TYPE_LIST_MODEL,
147 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
148
149 properties [PROP_TITLE] =
150 g_param_spec_string ("title", NULL, NULL,
151 NULL,
152 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
153
154 g_object_class_install_properties (object_class, N_PROPS, properties);
155 }
156
157 static void
158 sysprof_document_process_init (SysprofDocumentProcess *self)
159 {
160 }
161
162 const char *
163 _sysprof_document_process_get_comm (SysprofDocumentProcess *self)
164 {
165 const SysprofCaptureProcess *proc;
166
167 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), 0);
168
169 proc = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureProcess);
170
171 return SYSPROF_DOCUMENT_FRAME_CSTRING (self, proc->cmdline);
172 }
173
174 const char *
175 sysprof_document_process_get_command_line (SysprofDocumentProcess *self)
176 {
177 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), 0);
178
179 /* Prefer ProcessInfo symbol name which gets updated from
180 * subsequent comm[] updates for process records.
181 */
182 if (self->process_info != NULL &&
183 self->process_info->symbol != NULL)
184 return self->process_info->symbol->name;
185
186 return _sysprof_document_process_get_comm (self);
187 }
188
189 /**
190 * sysprof_document_process_list_memory_maps:
191 * @self: a #SysprofDocumentProcess
192 *
193 * Lists the #SysprofDocumentMmap that are associated with the process.
194 *
195 * Returns: (transfer full): a #GListModel of #SysprofDocumentMmap
196 */
197 GListModel *
198 sysprof_document_process_list_memory_maps (SysprofDocumentProcess *self)
199 {
200 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), NULL);
201
202 if (self->process_info == NULL)
203 return G_LIST_MODEL (g_list_store_new (SYSPROF_TYPE_DOCUMENT_MMAP));
204
205 return g_object_ref (G_LIST_MODEL (self->process_info->address_layout));
206 }
207
208 /**
209 * sysprof_document_process_list_mounts:
210 * @self: a #SysprofDocumentProcess
211 *
212 * Lists the #SysprofMount that are associated with the process.
213 *
214 * Returns: (transfer full): a #GListModel of #SysprofMount
215 */
216 GListModel *
217 sysprof_document_process_list_mounts (SysprofDocumentProcess *self)
218 {
219 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), NULL);
220
221 if (self->process_info == NULL)
222 return G_LIST_MODEL (g_list_store_new (SYSPROF_TYPE_MOUNT));
223
224 return g_object_ref (G_LIST_MODEL (self->process_info->mount_namespace));
225 }
226
227 SysprofProcessInfo *
228 _sysprof_document_process_get_info (SysprofDocumentProcess *self)
229 {
230 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), NULL);
231
232 return self->process_info;
233 }
234
235 void
236 _sysprof_document_process_set_info (SysprofDocumentProcess *self,
237 SysprofProcessInfo *process_info)
238 {
239 g_return_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self));
240 g_return_if_fail (process_info != NULL);
241 g_return_if_fail (self->process_info == NULL);
242
243 self->process_info = sysprof_process_info_ref (process_info);
244 }
245
246 gint64
247 sysprof_document_process_get_exit_time (SysprofDocumentProcess *self)
248 {
249 gint64 exit_time = 0;
250 gint64 t;
251
252 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), 0);
253
254 if (self->process_info != NULL)
255 exit_time = self->process_info->exit_time;
256
257 t = sysprof_document_frame_get_time (SYSPROF_DOCUMENT_FRAME (self));
258
259 return MAX (t, exit_time);
260 }
261
262 gint64
263 sysprof_document_process_get_duration (SysprofDocumentProcess *self)
264 {
265 gint64 t;
266
267 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), 0);
268
269 t = sysprof_document_frame_get_time (SYSPROF_DOCUMENT_FRAME (self));
270
271 return sysprof_document_process_get_exit_time (self) - t;
272 }
273
274 /**
275 * sysprof_document_process_dup_title:
276 * @self: a #SysprofDocumentProcess
277 *
278 * Gets a suitable title for the process.
279 *
280 * Returns: (transfer full): a string containing a process title
281 */
282 char *
283 sysprof_document_process_dup_title (SysprofDocumentProcess *self)
284 {
285 const char *command_line;
286 int pid;
287
288 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), NULL);
289
290 pid = sysprof_document_frame_get_pid (SYSPROF_DOCUMENT_FRAME (self));
291
292 if ((command_line = sysprof_document_process_get_command_line (self)))
293 return g_strdup_printf (_("%s [Process %d]"), command_line, pid);
294
295 return g_strdup_printf (_("Process %d"), pid);
296 }
297
298 /**
299 * sysprof_document_process_list_threads:
300 * @self: a #SysprofDocumentProcess
301 *
302 * Gets the list of threads for the process.
303 *
304 * Returns: (transfer full): a #GListModel of #SysprofThreadInfo.
305 */
306 GListModel *
307 sysprof_document_process_list_threads (SysprofDocumentProcess *self)
308 {
309 GListStore *store;
310
311 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_PROCESS (self), NULL);
312
313 store = g_list_store_new (SYSPROF_TYPE_THREAD_INFO);
314
315 if (self->process_info != NULL)
316 {
317 g_autoptr(GPtrArray) threads = g_ptr_array_new_with_free_func (g_object_unref);
318 EggBitsetIter iter;
319 guint i;
320
321 if (egg_bitset_iter_init_first (&iter, self->process_info->thread_ids, &i))
322 {
323 do
324 {
325 g_ptr_array_add (threads,
326 g_object_new (SYSPROF_TYPE_THREAD_INFO,
327 "process", self,
328 "thread-id", i,
329 NULL));
330 }
331 while (egg_bitset_iter_next (&iter, &i));
332 }
333
334 if (threads->len > 0)
335 g_list_store_splice (store, 0, 0, threads->pdata, threads->len);
336 }
337
338 return G_LIST_MODEL (store);
339 }