Debian Codegraph @

src/libsysprof/sysprof-controlfd-instrument.c

raw
1 /* sysprof-controlfd-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.h>
24
25 #ifdef G_OS_UNIX
26 # include <fcntl.h>
27 # include <glib-unix.h>
28 # include <glib/gstdio.h>
29 # include <gio/gunixinputstream.h>
30 # include <gio/gunixoutputstream.h>
31 # include <gio/gunixconnection.h>
32 # include <sys/socket.h>
33 # include <sys/types.h>
34 #endif
35
36 #include "sysprof-controlfd-instrument-private.h"
37 #include "sysprof-recording-private.h"
38
39 #ifdef G_OS_UNIX
40 # include "mapped-ring-buffer.h"
41 # include "mapped-ring-buffer-source-private.h"
42 #endif
43
44 G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref)
45 G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureWriter, sysprof_capture_writer_unref)
46
47 struct _SysprofControlfdInstrument
48 {
49 SysprofInstrument parent_instance;
50
51 #ifdef G_OS_UNIX
52 GUnixConnection *connection;
53 char read_buf[10];
54 #endif
55 };
56
57 G_DEFINE_FINAL_TYPE (SysprofControlfdInstrument, sysprof_controlfd_instrument, SYSPROF_TYPE_INSTRUMENT)
58
59 #ifdef G_OS_UNIX
60 typedef struct _RingData
61 {
62 SysprofCaptureWriter *writer;
63 GArray *source_ids;
64 guint id;
65 } RingData;
66
67 static void
68 ring_data_free (gpointer data)
69 {
70 RingData *ring_data = data;
71
72 for (guint i = 0; i < ring_data->source_ids->len; i++)
73 {
74 guint *id = &g_array_index (ring_data->source_ids, guint, i);
75
76 if (*id == ring_data->id)
77 {
78 *id = 0;
79 g_array_remove_index_fast (ring_data->source_ids, i);
80 break;
81 }
82 }
83
84 ring_data->id = 0;
85
86 g_clear_pointer (&ring_data->writer, sysprof_capture_writer_unref);
87 g_array_unref (ring_data->source_ids);
88 g_free (ring_data);
89 }
90
91 static DexFuture *
92 sysprof_controlfd_instrument_prepare (SysprofInstrument *instrument,
93 SysprofRecording *recording)
94 {
95 SysprofControlfdInstrument *self = (SysprofControlfdInstrument *)instrument;
96 SysprofSpawnable *spawnable;
97 g_autofree char *child_no_str = NULL;
98 g_autoptr(GSocketConnection) stream = NULL;
99 g_autoptr(GSocket) sock = NULL;
100 int fds[2] = {-1, -1};
101 int child_no;
102
103 g_assert (SYSPROF_IS_CONTROLFD_INSTRUMENT (self));
104 g_assert (SYSPROF_IS_RECORDING (recording));
105
106 /* If the recording is not spawning a process, then there is
107 * nothing for us to do.
108 */
109 if (!(spawnable = _sysprof_recording_get_spawnable (recording)))
110 goto finish;
111
112 /* Create a socket pair to send control messages over */
113 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds) != 0)
114 return dex_future_new_reject (G_IO_ERROR,
115 G_IO_ERROR_NOT_CONNECTED,
116 "Failed to create socketpair");
117
118 /* Set FDs non-blocking so that we can use them from main
119 * context iteration without blocking.
120 */
121 g_unix_set_fd_nonblocking (fds[0], TRUE, NULL);
122 g_unix_set_fd_nonblocking (fds[1], TRUE, NULL);
123
124 /* @child_no is assigned the FD the child will receive. We can
125 * use that to set the environment variable of the control FD.
126 */
127 child_no = sysprof_spawnable_take_fd (spawnable, fds[1], -1);
128 child_no_str = g_strdup_printf ("%d", child_no);
129 sysprof_spawnable_setenv (spawnable, "SYSPROF_CONTROL_FD", child_no_str);
130
131 if (!(sock = g_socket_new_from_fd (fds[0], NULL)))
132 {
133 close (fds[0]);
134 return dex_future_new_reject (G_IO_ERROR,
135 G_IO_ERROR_NOT_CONNECTED,
136 "Failed to create socket from FD");
137 }
138
139 self->connection = G_UNIX_CONNECTION (g_socket_connection_factory_create_connection (sock));
140
141 finish:
142 return dex_future_new_for_boolean (TRUE);
143 }
144
145 typedef struct _SysprofControlfdRecording
146 {
147 GIOStream *stream;
148 SysprofRecording *recording;
149 DexFuture *cancellable;
150 GArray *source_ids;
151 } SysprofControlfdRecording;
152
153 static void
154 sysprof_controlfd_recording_free (gpointer data)
155 {
156 SysprofControlfdRecording *state = data;
157
158 dex_clear (&state->cancellable);
159 g_clear_pointer (&state->source_ids, g_array_unref);
160 g_clear_object (&state->recording);
161 g_clear_object (&state->stream);
162 g_free (state);
163 }
164
165 static bool
166 sysprof_controlfd_instrument_frame_cb (gconstpointer data,
167 gsize *length,
168 gpointer user_data)
169 {
170 const SysprofCaptureFrame *fr = data;
171 RingData *ring_data = user_data;
172
173 g_assert (ring_data != NULL);
174 g_assert (ring_data->source_ids != NULL);
175 g_assert (ring_data->writer != NULL);
176 g_assert (ring_data->id > 0);
177
178 if G_UNLIKELY (*length < sizeof *fr ||
179 *length < fr->len ||
180 fr->type >= SYSPROF_CAPTURE_FRAME_LAST)
181 return G_SOURCE_REMOVE;
182
183 _sysprof_capture_writer_add_raw (ring_data->writer, fr);
184
185 *length = fr->len;
186
187 return G_SOURCE_CONTINUE;
188 }
189
190 static DexFuture *
191 sysprof_controlfd_instrument_record_fiber (gpointer user_data)
192 {
193 SysprofControlfdRecording *state = user_data;
194 g_autoptr(SysprofCaptureWriter) temp_writer = NULL;
195 SysprofCaptureWriter *writer;
196 g_autoptr(GError) error = NULL;
197 g_autofd int mem_fd = -1;
198 GInputStream *input;
199
200 g_assert (state != NULL);
201 g_assert (SYSPROF_IS_RECORDING (state->recording));
202 g_assert (DEX_IS_CANCELLABLE (state->cancellable));
203 g_assert (state->source_ids != NULL);
204
205 input = g_io_stream_get_input_stream (G_IO_STREAM (state->stream));
206
207 if (!(mem_fd = sysprof_memfd_create ("[controlfd-memfd]")))
208 return dex_future_new_for_errno (errno);
209
210 temp_writer = sysprof_capture_writer_new_from_fd (g_steal_fd (&mem_fd), 0);
211 writer = _sysprof_recording_writer (state->recording);
212
213 for (;;)
214 {
215 g_autoptr(DexFuture) future = dex_input_stream_read_bytes (input, 10, 0);
216 g_autoptr(MappedRingBuffer) ring_buffer = NULL;
217 g_autoptr(GBytes) bytes = NULL;
218 const guint8 *data;
219 gsize len;
220
221 dex_await (dex_future_any (dex_ref (future),
222 dex_ref (state->cancellable),
223 NULL),
224 &error);
225
226 if (error != NULL)
227 goto handle_error;
228
229 if (!(bytes = dex_await_boxed (dex_ref (future), &error)))
230 goto handle_error;
231
232 data = g_bytes_get_data (bytes, &len);
233 if (len != 10 || memcmp (data, "CreatRing\0", 10) != 0)
234 break;
235
236 if ((ring_buffer = mapped_ring_buffer_new_reader (0)))
237 {
238 int fd = mapped_ring_buffer_get_fd (ring_buffer);
239 RingData *ring_data;
240
241 ring_data = g_new0 (RingData, 1);
242 ring_data->writer = sysprof_capture_writer_ref (temp_writer);
243 ring_data->source_ids = g_array_ref (state->source_ids);
244 ring_data->id = mapped_ring_buffer_create_source_full (G_PRIORITY_HIGH,
245 ring_buffer,
246 sysprof_controlfd_instrument_frame_cb,
247 ring_data,
248 (GDestroyNotify)ring_data_free);
249
250 g_array_append_val (state->source_ids, ring_data->id);
251
252 g_unix_connection_send_fd (G_UNIX_CONNECTION (state->stream), fd, NULL, NULL);
253 }
254 }
255
256 handle_error:
257 while (state->source_ids->len > 0)
258 {
259 guint id = g_array_index (state->source_ids, guint, state->source_ids->len-1);
260 state->source_ids->len--;
261 g_source_remove (id);
262 }
263
264 if (temp_writer != NULL)
265 {
266 g_autoptr(SysprofCaptureReader) reader = sysprof_capture_writer_create_reader (temp_writer);
267
268 if (reader != NULL)
269 sysprof_capture_writer_cat (writer, reader);
270 }
271
272 if (error != NULL)
273 return dex_future_new_for_error (g_steal_pointer (&error));
274
275 return dex_future_new_for_boolean (TRUE);
276 }
277
278 static void
279 _g_clear_source (gpointer data)
280 {
281 guint *id = data;
282
283 if (*id != 0)
284 g_source_remove (*id);
285 }
286
287 static DexFuture *
288 sysprof_controlfd_instrument_record (SysprofInstrument *instrument,
289 SysprofRecording *recording,
290 GCancellable *cancellable)
291 {
292 SysprofControlfdInstrument *self = (SysprofControlfdInstrument *)instrument;
293 SysprofControlfdRecording *state;
294 SysprofSpawnable *spawnable;
295
296 g_assert (SYSPROF_IS_CONTROLFD_INSTRUMENT (self));
297 g_assert (SYSPROF_IS_RECORDING (recording));
298
299 if (!(spawnable = _sysprof_recording_get_spawnable (recording)))
300 return dex_future_new_for_boolean (TRUE);
301
302 state = g_new0 (SysprofControlfdRecording, 1);
303 state->recording = g_object_ref (recording);
304 state->stream = g_object_ref (G_IO_STREAM (self->connection));
305 state->cancellable = dex_cancellable_new_from_cancellable (cancellable);
306 state->source_ids = g_array_new (FALSE, FALSE, sizeof (guint));
307 g_array_set_clear_func (state->source_ids, _g_clear_source);
308
309 return dex_scheduler_spawn (NULL, 0,
310 sysprof_controlfd_instrument_record_fiber,
311 state,
312 sysprof_controlfd_recording_free);
313 }
314
315 static void
316 sysprof_controlfd_instrument_finalize (GObject *object)
317 {
318 SysprofControlfdInstrument *self = (SysprofControlfdInstrument *)object;
319
320 g_clear_object (&self->connection);
321
322 G_OBJECT_CLASS (sysprof_controlfd_instrument_parent_class)->finalize (object);
323 }
324 #endif
325
326 static void
327 sysprof_controlfd_instrument_class_init (SysprofControlfdInstrumentClass *klass)
328 {
329 #ifdef G_OS_UNIX
330 GObjectClass *object_class = G_OBJECT_CLASS (klass);
331 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
332
333 object_class->finalize = sysprof_controlfd_instrument_finalize;
334
335 instrument_class->prepare = sysprof_controlfd_instrument_prepare;
336 instrument_class->record = sysprof_controlfd_instrument_record;
337 #endif
338 }
339
340 static void
341 sysprof_controlfd_instrument_init (SysprofControlfdInstrument *self)
342 {
343 }
344
345 SysprofInstrument *
346 _sysprof_controlfd_instrument_new (void)
347 {
348 #ifndef G_OS_UNIX
349 g_warning_once ("SysprofControlfdInstrument not supported on this platform");
350 #endif
351
352 return g_object_new (SYSPROF_TYPE_CONTROLFD_INSTRUMENT, NULL);
353 }