Debian Codegraph @

src/libsysprof/sysprof-dbus-monitor.c

raw
1 /* sysprof-dbus-monitor.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-instrument-private.h"
26 #include "sysprof-dbus-monitor.h"
27 #include "sysprof-recording-private.h"
28
29 #define MAX_EMBEDDABLE_MESSAGE_SIZE ((1<<16)-sizeof(SysprofCaptureDBusMessage)-16)
30
31 struct _SysprofDBusMonitor
32 {
33 SysprofInstrument parent_instance;
34 GBusType bus_type;
35 char *bus_address;
36 };
37
38 struct _SysprofDBusMonitorClass
39 {
40 SysprofInstrumentClass parent_instance;
41 };
42
43 enum {
44 PROP_0,
45 PROP_BUS_ADDRESS,
46 PROP_BUS_TYPE,
47 N_PROPS
48 };
49
50 G_DEFINE_FINAL_TYPE (SysprofDBusMonitor, sysprof_dbus_monitor, SYSPROF_TYPE_INSTRUMENT)
51
52 static GParamSpec *properties [N_PROPS];
53
54 static char **
55 sysprof_dbus_monitor_list_required_policy (SysprofInstrument *instrument)
56 {
57 /* TODO: If we add a new policy for monitoring the system bus, then we can
58 * make sysprofd open the monitor and feed that data to the instrument
59 * for recording. That would allow for monitoring --system without
60 * having to be in fallback observation mode.
61 *
62 * "org.gnome.sysprof3.profile" is probably enough though.
63 */
64 return NULL;
65 }
66
67 typedef struct _Record
68 {
69 SysprofRecording *recording;
70 DexFuture *cancellable;
71 char *bus_address;
72 GBusType bus_type;
73 } Record;
74
75 static void
76 record_free (gpointer data)
77 {
78 Record *record = data;
79
80 g_clear_object (&record->recording);
81 dex_clear (&record->cancellable);
82 g_free (record);
83 }
84
85 static DexFuture *
86 sysprof_dbus_monitor_record_fiber (gpointer user_data)
87 {
88 Record *record = user_data;
89 g_autofree guint8 *read_buffer = NULL;
90 g_autoptr(GSubprocessLauncher) launcher = NULL;
91 g_autoptr(GSubprocess) subprocess = NULL;
92 g_autoptr(GPtrArray) argv = NULL;
93 g_autoptr(GError) error = NULL;
94 SysprofCaptureWriter *writer;
95 GInputStream *stdout_stream;
96
97 g_assert (record != NULL);
98 g_assert (SYSPROF_IS_RECORDING (record->recording));
99 g_assert (DEX_IS_FUTURE (record->cancellable));
100
101 writer = _sysprof_recording_writer (record->recording);
102 read_buffer = g_malloc (MAX_EMBEDDABLE_MESSAGE_SIZE);
103 argv = g_ptr_array_new_null_terminated (0, NULL, TRUE);
104
105 g_ptr_array_add (argv, (char *)"dbus-monitor");
106 g_ptr_array_add (argv, (char *)"--profile");
107 g_ptr_array_add (argv, (char *)"--binary");
108
109 if (record->bus_address != NULL)
110 {
111 g_ptr_array_add (argv, (char *)"--address");
112 g_ptr_array_add (argv, (char *)record->bus_address);
113 }
114 else if (record->bus_type == G_BUS_TYPE_SESSION)
115 {
116 g_ptr_array_add (argv, (char *)"--session");
117 }
118 else if (record->bus_type == G_BUS_TYPE_SYSTEM)
119 {
120 g_ptr_array_add (argv, (char *)"--system");
121 }
122 else
123 {
124 _sysprof_recording_diagnostic (record->recording,
125 "D-Bus Monitor",
126 "Misconfigured D-Bus monitor, will not record");
127 return dex_future_new_for_boolean (TRUE);
128 }
129
130 launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE|G_SUBPROCESS_FLAGS_STDERR_SILENCE);
131 subprocess = g_subprocess_launcher_spawnv (launcher, (const char * const *)argv->pdata, &error);
132
133 if (subprocess == NULL)
134 return dex_future_new_for_error (g_steal_pointer (&error));
135
136 stdout_stream = g_subprocess_get_stdout_pipe (subprocess);
137
138 for (;;)
139 {
140 g_autoptr(DexFuture) header_future = NULL;
141 guint8 header[16];
142 gssize n_needed;
143
144 /* Read enough bytes to decode message length */
145 header_future = dex_input_stream_read (stdout_stream,
146 header,
147 sizeof header,
148 G_PRIORITY_DEFAULT);
149
150 /* Wait until we have incoming data or the recording is cancelled */
151 if (!dex_await (dex_future_first (dex_ref (record->cancellable),
152 dex_ref (header_future),
153 NULL), &error))
154 break;
155
156 /* Stop if we reached EOF or errored on reading message header */
157 if (dex_await_int64 (dex_ref (header_future), &error) != sizeof header)
158 break;
159
160 /* If we got a decoding error, just bail */
161 if ((n_needed = g_dbus_message_bytes_needed (header, sizeof header, NULL)) < 0)
162 break;
163
164 g_assert (n_needed >= 16);
165
166 /* If this is a message that will be too large to store, then note that
167 * and skip the appropriate number of bytes. Our largest frame size is
168 * 64k - sizeof(SysprofCaptureFrameDBus).
169 */
170 if (n_needed > MAX_EMBEDDABLE_MESSAGE_SIZE)
171 {
172 sysprof_capture_writer_add_dbus_message (writer,
173 SYSPROF_CAPTURE_CURRENT_TIME,
174 -1,
175 -1,
176 record->bus_type,
177 SYSPROF_CAPTURE_DBUS_FLAGS_MESSAGE_TOO_LARGE,
178 NULL, 0);
179
180 /* Skip the remaining bytes for the message or bail on cancellation */
181 if (!dex_await (dex_future_first (dex_ref (record->cancellable),
182 dex_input_stream_skip (stdout_stream,
183 n_needed - sizeof header,
184 G_PRIORITY_DEFAULT),
185 NULL), &error))
186 break;
187 }
188 else
189 {
190 g_autoptr(DexFuture) body_future = NULL;
191
192 /* Read the rest of the incoming message */
193 memcpy (read_buffer, header, sizeof header);
194 body_future = dex_input_stream_read (stdout_stream,
195 read_buffer + sizeof header,
196 n_needed - sizeof header,
197 G_PRIORITY_DEFAULT);
198
199 /* Wait for body or cancellation of recording */
200 if (!dex_await (dex_future_first (dex_ref (record->cancellable),
201 dex_ref (body_future),
202 NULL), &error))
203 break;
204
205 /* If we didn't read what we expected to, just bail */
206 if (dex_await_int64 (dex_ref (body_future), &error) != n_needed - sizeof header)
207 break;
208
209 sysprof_capture_writer_add_dbus_message (writer,
210 SYSPROF_CAPTURE_CURRENT_TIME,
211 -1,
212 -1,
213 record->bus_type,
214 0,
215 read_buffer, n_needed);
216 }
217 }
218
219 g_subprocess_force_exit (subprocess);
220
221 return dex_future_new_for_boolean (TRUE);
222 }
223
224 static DexFuture *
225 sysprof_dbus_monitor_record (SysprofInstrument *instrument,
226 SysprofRecording *recording,
227 GCancellable *cancellable)
228 {
229 SysprofDBusMonitor *self = (SysprofDBusMonitor *)instrument;
230 Record *record;
231
232 g_assert (SYSPROF_IS_DBUS_MONITOR (self));
233 g_assert (SYSPROF_IS_RECORDING (recording));
234 g_assert (G_IS_CANCELLABLE (cancellable));
235
236 record = g_new0 (Record, 1);
237 record->recording = g_object_ref (recording);
238 record->cancellable = dex_cancellable_new_from_cancellable (cancellable);
239 record->bus_type = self->bus_type;
240 record->bus_address = g_strdup (self->bus_address);
241
242 return dex_scheduler_spawn (NULL, 0,
243 sysprof_dbus_monitor_record_fiber,
244 record,
245 record_free);
246 }
247
248 static void
249 sysprof_dbus_monitor_finalize (GObject *object)
250 {
251 SysprofDBusMonitor *self = (SysprofDBusMonitor *)object;
252
253 g_clear_pointer (&self->bus_address, g_free);
254
255 G_OBJECT_CLASS (sysprof_dbus_monitor_parent_class)->finalize (object);
256 }
257
258 static void
259 sysprof_dbus_monitor_get_property (GObject *object,
260 guint prop_id,
261 GValue *value,
262 GParamSpec *pspec)
263 {
264 SysprofDBusMonitor *self = SYSPROF_DBUS_MONITOR (object);
265
266 switch (prop_id)
267 {
268 case PROP_BUS_ADDRESS:
269 g_value_set_string (value, sysprof_dbus_monitor_get_bus_address (self));
270 break;
271
272 case PROP_BUS_TYPE:
273 g_value_set_enum (value, sysprof_dbus_monitor_get_bus_type (self));
274 break;
275
276 default:
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
278 }
279 }
280
281 static void
282 sysprof_dbus_monitor_set_property (GObject *object,
283 guint prop_id,
284 const GValue *value,
285 GParamSpec *pspec)
286 {
287 SysprofDBusMonitor *self = SYSPROF_DBUS_MONITOR (object);
288
289 switch (prop_id)
290 {
291 case PROP_BUS_ADDRESS:
292 self->bus_address = g_value_dup_string (value);
293 break;
294
295 case PROP_BUS_TYPE:
296 self->bus_type = g_value_get_enum (value);
297 break;
298
299 default:
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301 }
302 }
303
304 static void
305 sysprof_dbus_monitor_class_init (SysprofDBusMonitorClass *klass)
306 {
307 GObjectClass *object_class = G_OBJECT_CLASS (klass);
308 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
309
310 object_class->finalize = sysprof_dbus_monitor_finalize;
311 object_class->get_property = sysprof_dbus_monitor_get_property;
312 object_class->set_property = sysprof_dbus_monitor_set_property;
313
314 instrument_class->list_required_policy = sysprof_dbus_monitor_list_required_policy;
315 instrument_class->record = sysprof_dbus_monitor_record;
316
317 properties[PROP_BUS_ADDRESS] =
318 g_param_spec_string ("bus-address", NULL, NULL,
319 NULL,
320 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
321
322 properties[PROP_BUS_TYPE] =
323 g_param_spec_enum ("bus-type", NULL, NULL,
324 G_TYPE_BUS_TYPE,
325 G_BUS_TYPE_SESSION,
326 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
327
328 g_object_class_install_properties (object_class, N_PROPS, properties);
329 }
330
331 static void
332 sysprof_dbus_monitor_init (SysprofDBusMonitor *self)
333 {
334 }
335
336 SysprofInstrument *
337 sysprof_dbus_monitor_new (GBusType bus_type)
338 {
339 g_return_val_if_fail (bus_type == G_BUS_TYPE_SESSION || bus_type == G_BUS_TYPE_SYSTEM,
340 NULL);
341
342 return g_object_new (SYSPROF_TYPE_DBUS_MONITOR,
343 "bus-type", bus_type,
344 NULL);
345 }
346
347 SysprofInstrument *
348 sysprof_dbus_monitor_new_for_bus_address (const char *bus_address)
349 {
350 g_return_val_if_fail (bus_address != NULL, NULL);
351
352 return g_object_new (SYSPROF_TYPE_DBUS_MONITOR,
353 "bus-address", bus_address,
354 NULL);
355 }
356
357 GBusType
358 sysprof_dbus_monitor_get_bus_type (SysprofDBusMonitor *self)
359 {
360 g_return_val_if_fail (SYSPROF_IS_DBUS_MONITOR (self), 0);
361
362 return self->bus_type;
363 }
364
365 const char *
366 sysprof_dbus_monitor_get_bus_address (SysprofDBusMonitor *self)
367 {
368 g_return_val_if_fail (SYSPROF_IS_DBUS_MONITOR (self), NULL);
369
370 return self->bus_address;
371 }