Debian Codegraph @

src/libsysprof/sysprof-proxied-instrument.c

raw
1 /* sysprof-proxied-instrument.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/gstdio.h>
24
25 #include "sysprof-proxied-instrument-private.h"
26 #include "sysprof-recording-private.h"
27
28 enum {
29 PROP_0,
30 PROP_BUS_TYPE,
31 PROP_BUS_NAME,
32 PROP_OBJECT_PATH,
33 N_PROPS
34 };
35
36 G_DEFINE_TYPE (SysprofProxiedInstrument, sysprof_proxied_instrument, SYSPROF_TYPE_INSTRUMENT)
37
38 static GParamSpec *properties [N_PROPS];
39
40 typedef struct _Record
41 {
42 SysprofRecording *recording;
43 DexFuture *cancellable;
44 char *bus_name;
45 char *object_path;
46 GVariant *options;
47 GBusType bus_type;
48 guint call_stop_first : 1;
49 } Record;
50
51 static void
52 record_free (Record *record)
53 {
54 g_clear_object (&record->recording);
55 dex_clear (&record->cancellable);
56 g_clear_pointer (&record->bus_name, g_free);
57 g_clear_pointer (&record->object_path, g_free);
58 g_clear_pointer (&record->options, g_variant_unref);
59 g_free (record);
60 }
61
62 static DexFuture *
63 sysprof_proxied_instrument_record_fiber (gpointer user_data)
64 {
65 Record *record = user_data;
66 SysprofCaptureWriter *writer;
67 SysprofCaptureReader *reader;
68 g_autoptr(GDBusConnection) connection = NULL;
69 g_autoptr(GUnixFDList) fd_list = NULL;
70 g_autoptr(DexFuture) started = NULL;
71 g_autoptr(GError) error = NULL;
72 g_autofd int proxy_fd = -1;
73 int handle;
74
75 g_assert (record != NULL);
76 g_assert (record->bus_type != G_BUS_TYPE_NONE);
77 g_assert (record->bus_name != NULL);
78 g_assert (record->object_path != NULL);
79 g_assert (SYSPROF_IS_RECORDING (record->recording));
80 g_assert (DEX_IS_CANCELLABLE (record->cancellable));
81
82 /* Wait for our connection to be available */
83 if (!(connection = dex_await_object (dex_bus_get (record->bus_type), &error)))
84 return dex_future_new_for_error (g_steal_pointer (&error));
85
86 /* If we should try calling stop first to cancel any in-flight
87 * recording. We could pipeline this, but if the other side
88 * handles messages on threads, they would race.
89 */
90 if (record->call_stop_first)
91 dex_await (dex_dbus_connection_call (connection,
92 record->bus_name,
93 record->object_path,
94 "org.gnome.Sysprof3.Profiler",
95 "Stop",
96 g_variant_new ("()"),
97 G_VARIANT_TYPE ("()"),
98 G_DBUS_CALL_FLAGS_NONE,
99 -1),
100 NULL);
101
102 /* Create a new FD that the peer will be able to write to and
103 * we will concatenate into the capture after recording.
104 */
105 if (-1 == (proxy_fd = sysprof_memfd_create ("[sysprof-proxy]")))
106 return dex_future_new_for_errno (errno);
107
108 /* Create FDList to pass to the peer so they can get our FD */
109 fd_list = g_unix_fd_list_new ();
110 if (-1 == (handle = g_unix_fd_list_append (fd_list, proxy_fd, &error)))
111 return dex_future_new_for_error (g_steal_pointer (&error));
112
113 /* Call the proxy and give it our FD to start recording */
114 if (!dex_await (dex_dbus_connection_call_with_unix_fd_list (connection,
115 record->bus_name,
116 record->object_path,
117 "org.gnome.Sysprof3.Profiler",
118 "Start",
119 g_variant_new ("(@a{sv}h)",
120 record->options,
121 handle),
122 G_VARIANT_TYPE ("()"),
123 G_DBUS_CALL_FLAGS_ALLOW_INTERACTIVE_AUTHORIZATION,
124 G_MAXUINT,
125 fd_list),
126 &error))
127 {
128 g_debug ("Failed to start profiler at %s %s: %s",
129 record->bus_name,
130 record->object_path,
131 error->message);
132 return dex_future_new_for_error (g_steal_pointer (&error));
133 }
134
135 /* Await completion of recording */
136 dex_await (dex_ref (record->cancellable), NULL);
137
138 /* Call the proxy and let them know to stop */
139 dex_await (dex_dbus_connection_call (connection,
140 record->bus_name,
141 record->object_path,
142 "org.gnome.Sysprof3.Profiler",
143 "Stop",
144 g_variant_new ("()"),
145 G_VARIANT_TYPE ("()"),
146 G_DBUS_CALL_FLAGS_NONE,
147 -1),
148 &error);
149
150 if (error != NULL)
151 g_warning ("Failed to stop profiler at %s %s: %s",
152 record->bus_name,
153 record->object_path,
154 error->message);
155
156 /* Reset the file position to the start */
157 lseek (proxy_fd, 0, SEEK_SET);
158
159 /* Now cat the recording into our writer */
160 writer = _sysprof_recording_writer (record->recording);
161
162 if ((reader = sysprof_capture_reader_new_from_fd (g_steal_fd (&proxy_fd))))
163 {
164 sysprof_capture_writer_cat (writer, reader);
165 sysprof_capture_reader_unref (reader);
166 }
167
168 return dex_future_new_for_boolean (TRUE);
169 }
170
171 static GVariant *
172 create_empty_dictionary (void)
173 {
174 GVariantDict dict = G_VARIANT_DICT_INIT (NULL);
175 return g_variant_take_ref (g_variant_dict_end (&dict));
176 }
177
178 static DexFuture *
179 sysprof_proxied_instrument_record (SysprofInstrument *instrument,
180 SysprofRecording *recording,
181 GCancellable *cancellable)
182 {
183 SysprofProxiedInstrument *self = (SysprofProxiedInstrument *)instrument;
184 Record *record;
185
186 g_assert (SYSPROF_IS_PROXIED_INSTRUMENT (self));
187 g_assert (SYSPROF_IS_RECORDING (recording));
188 g_assert (G_IS_CANCELLABLE (cancellable));
189
190 record = g_new0 (Record, 1);
191 record->recording = g_object_ref (recording);
192 record->cancellable = dex_cancellable_new_from_cancellable (cancellable);
193 record->bus_name = g_strdup (self->bus_name);
194 record->object_path = g_strdup (self->object_path);
195 record->bus_type = self->bus_type;
196 record->call_stop_first = self->call_stop_first;
197
198 if (self->options != NULL)
199 record->options = g_variant_ref (self->options);
200 else
201 record->options = create_empty_dictionary ();
202
203 return dex_scheduler_spawn (NULL, 0,
204 sysprof_proxied_instrument_record_fiber,
205 record,
206 (GDestroyNotify)record_free);
207 }
208
209 static void
210 sysprof_proxied_instrument_finalize (GObject *object)
211 {
212 SysprofProxiedInstrument *self = (SysprofProxiedInstrument *)object;
213
214 g_clear_pointer (&self->bus_name, g_free);
215 g_clear_pointer (&self->object_path, g_free);
216 g_clear_pointer (&self->options, g_variant_unref);
217
218 G_OBJECT_CLASS (sysprof_proxied_instrument_parent_class)->finalize (object);
219 }
220
221 static void
222 sysprof_proxied_instrument_get_property (GObject *object,
223 guint prop_id,
224 GValue *value,
225 GParamSpec *pspec)
226 {
227 SysprofProxiedInstrument *self = SYSPROF_PROXIED_INSTRUMENT (object);
228
229 switch (prop_id)
230 {
231 case PROP_BUS_TYPE:
232 g_value_set_enum (value, self->bus_type);
233 break;
234
235 case PROP_BUS_NAME:
236 g_value_set_string (value, self->bus_name);
237 break;
238
239 case PROP_OBJECT_PATH:
240 g_value_set_string (value, self->object_path);
241 break;
242
243 default:
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 }
246 }
247
248 static void
249 sysprof_proxied_instrument_set_property (GObject *object,
250 guint prop_id,
251 const GValue *value,
252 GParamSpec *pspec)
253 {
254 SysprofProxiedInstrument *self = SYSPROF_PROXIED_INSTRUMENT (object);
255
256 switch (prop_id)
257 {
258 case PROP_BUS_TYPE:
259 self->bus_type = g_value_get_enum (value);
260 break;
261
262 case PROP_BUS_NAME:
263 self->bus_name = g_value_dup_string (value);
264 break;
265
266 case PROP_OBJECT_PATH:
267 self->object_path = g_value_dup_string (value);
268 break;
269
270 default:
271 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 }
273 }
274
275 static void
276 sysprof_proxied_instrument_class_init (SysprofProxiedInstrumentClass *klass)
277 {
278 GObjectClass *object_class = G_OBJECT_CLASS (klass);
279 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
280
281 object_class->finalize = sysprof_proxied_instrument_finalize;
282 object_class->get_property = sysprof_proxied_instrument_get_property;
283 object_class->set_property = sysprof_proxied_instrument_set_property;
284
285 instrument_class->record = sysprof_proxied_instrument_record;
286
287 properties [PROP_BUS_TYPE] =
288 g_param_spec_enum ("bus-type", NULL, NULL,
289 G_TYPE_BUS_TYPE,
290 G_BUS_TYPE_NONE,
291 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
292
293 properties [PROP_BUS_NAME] =
294 g_param_spec_string ("bus-name", NULL, NULL,
295 NULL,
296 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
297
298 properties [PROP_OBJECT_PATH] =
299 g_param_spec_string ("object-path", NULL, NULL,
300 NULL,
301 (G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
302
303 g_object_class_install_properties (object_class, N_PROPS, properties);
304 }
305
306 static void
307 sysprof_proxied_instrument_init (SysprofProxiedInstrument *self)
308 {
309 }
310
311 SysprofInstrument *
312 sysprof_proxied_instrument_new_with_options (GBusType bus_type,
313 const char *bus_name,
314 const char *object_path,
315 GVariant *options)
316 {
317 SysprofProxiedInstrument *self;
318
319 g_return_val_if_fail (bus_type == G_BUS_TYPE_SYSTEM ||
320 bus_type == G_BUS_TYPE_SESSION, NULL);
321 g_return_val_if_fail (bus_name != NULL, NULL);
322 g_return_val_if_fail (object_path != NULL, NULL);
323 g_return_val_if_fail (options == NULL || g_variant_is_of_type (options, G_VARIANT_TYPE_VARDICT), NULL);
324
325 self = g_object_new (SYSPROF_TYPE_PROXIED_INSTRUMENT,
326 "bus-type", bus_type,
327 "bus-name", bus_name,
328 "object-path", object_path,
329 NULL);
330
331 if (options != NULL)
332 self->options = g_variant_ref_sink (options);
333
334 return SYSPROF_INSTRUMENT (self);
335 }
336
337 SysprofInstrument *
338 sysprof_proxied_instrument_new (GBusType bus_type,
339 const char *bus_name,
340 const char *object_path)
341 {
342 return sysprof_proxied_instrument_new_with_options (bus_type, bus_name, object_path, NULL);
343 }