| 1 |
/* sysprof-subprocess-output.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 "sysprof-enums.h" |
| 24 |
#include "sysprof-instrument-private.h" |
| 25 |
#include "sysprof-recording-private.h" |
| 26 |
#include "sysprof-subprocess-output.h" |
| 27 |
|
| 28 |
struct _SysprofSubprocessOutput |
| 29 |
{ |
| 30 |
SysprofInstrument parent_instance; |
| 31 |
|
| 32 |
char *stdout_path; |
| 33 |
char *command_cwd; |
| 34 |
char **command_argv; |
| 35 |
char **command_environ; |
| 36 |
|
| 37 |
SysprofRecording *recording; |
| 38 |
GCancellable *cancellable; |
| 39 |
|
| 40 |
SysprofRecordingPhase phase; |
| 41 |
}; |
| 42 |
|
| 43 |
struct _SysprofSubprocessOutputClass |
| 44 |
{ |
| 45 |
SysprofInstrumentClass parent_class; |
| 46 |
}; |
| 47 |
|
| 48 |
enum { |
| 49 |
PROP_0, |
| 50 |
PROP_COMMAND_ARGV, |
| 51 |
PROP_COMMAND_ENVIRON, |
| 52 |
PROP_COMMAND_CWD, |
| 53 |
PROP_PHASE, |
| 54 |
PROP_STDOUT_PATH, |
| 55 |
N_PROPS |
| 56 |
}; |
| 57 |
|
| 58 |
G_DEFINE_FINAL_TYPE (SysprofSubprocessOutput, sysprof_subprocess_output, SYSPROF_TYPE_INSTRUMENT) |
| 59 |
|
| 60 |
static GParamSpec *properties [N_PROPS]; |
| 61 |
|
| 62 |
static void |
| 63 |
add_process_output_as_file_cb (GObject *object, |
| 64 |
GAsyncResult *result, |
| 65 |
gpointer user_data) |
| 66 |
{ |
| 67 |
g_autoptr(DexPromise) promise = user_data; |
| 68 |
g_autoptr(GError) error = NULL; |
| 69 |
g_autofree char *stdout_buf = NULL; |
| 70 |
|
| 71 |
g_assert (G_IS_SUBPROCESS (object)); |
| 72 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 73 |
g_assert (DEX_IS_PROMISE (promise)); |
| 74 |
|
| 75 |
if (g_subprocess_communicate_utf8_finish (G_SUBPROCESS (object), result, &stdout_buf, NULL, &error)) |
| 76 |
dex_promise_resolve_string (promise, g_steal_pointer (&stdout_buf)); |
| 77 |
else |
| 78 |
dex_promise_reject (promise, g_steal_pointer (&error)); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
add_process_output_as_file (SysprofRecording *recording, |
| 83 |
const char * const *argv, |
| 84 |
const char * const *env, |
| 85 |
const char *filename, |
| 86 |
gboolean compress, |
| 87 |
GCancellable *cancellable) |
| 88 |
{ |
| 89 |
g_autoptr(GSubprocessLauncher) launcher = NULL; |
| 90 |
g_autoptr(GSubprocess) subprocess = NULL; |
| 91 |
g_autoptr(DexPromise) promise = NULL; |
| 92 |
g_autoptr(GError) error = NULL; |
| 93 |
g_autofree char *string = NULL; |
| 94 |
|
| 95 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 96 |
|
| 97 |
launcher = g_subprocess_launcher_new (G_SUBPROCESS_FLAGS_STDOUT_PIPE | G_SUBPROCESS_FLAGS_STDERR_SILENCE); |
| 98 |
if (env != NULL) |
| 99 |
g_subprocess_launcher_set_environ (launcher, (char **)env); |
| 100 |
if (!(subprocess = g_subprocess_launcher_spawnv (launcher, argv, &error))) |
| 101 |
goto error; |
| 102 |
|
| 103 |
promise = dex_promise_new (); |
| 104 |
g_subprocess_communicate_utf8_async (subprocess, |
| 105 |
NULL, |
| 106 |
cancellable, |
| 107 |
add_process_output_as_file_cb, |
| 108 |
dex_ref (promise)); |
| 109 |
|
| 110 |
if (!(string = dex_await_string (dex_ref (promise), &error))) |
| 111 |
goto error; |
| 112 |
|
| 113 |
_sysprof_recording_add_file_data (recording, filename, string, -1, compress); |
| 114 |
|
| 115 |
return; |
| 116 |
|
| 117 |
error: |
| 118 |
_sysprof_recording_diagnostic (recording, |
| 119 |
"Subprocess", |
| 120 |
"Failed to get command output: %s", |
| 121 |
error->message); |
| 122 |
} |
| 123 |
|
| 124 |
static DexFuture * |
| 125 |
sysprof_subprocess_output_record_fiber (gpointer user_data) |
| 126 |
{ |
| 127 |
SysprofSubprocessOutput *self = user_data; |
| 128 |
|
| 129 |
g_assert (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 130 |
|
| 131 |
if (self->command_argv && self->stdout_path) |
| 132 |
add_process_output_as_file (self->recording, |
| 133 |
(const char * const *)self->command_argv, |
| 134 |
(const char * const *)self->command_environ, |
| 135 |
self->stdout_path, |
| 136 |
TRUE, |
| 137 |
self->cancellable); |
| 138 |
|
| 139 |
return dex_future_new_for_boolean (TRUE); |
| 140 |
} |
| 141 |
|
| 142 |
static DexFuture * |
| 143 |
sysprof_subprocess_output_record (SysprofInstrument *instrument, |
| 144 |
SysprofRecording *recording, |
| 145 |
GCancellable *cancellable) |
| 146 |
{ |
| 147 |
SysprofSubprocessOutput *self = (SysprofSubprocessOutput *)instrument; |
| 148 |
|
| 149 |
g_assert (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 150 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 151 |
g_assert (G_IS_CANCELLABLE (cancellable)); |
| 152 |
|
| 153 |
if (self->phase != SYSPROF_RECORDING_PHASE_RECORD) |
| 154 |
return dex_future_new_for_boolean (TRUE); |
| 155 |
|
| 156 |
g_set_object (&self->recording, recording); |
| 157 |
g_set_object (&self->cancellable, cancellable); |
| 158 |
|
| 159 |
return dex_scheduler_spawn (NULL, 0, |
| 160 |
sysprof_subprocess_output_record_fiber, |
| 161 |
g_object_ref (self), |
| 162 |
g_object_unref); |
| 163 |
} |
| 164 |
|
| 165 |
static DexFuture * |
| 166 |
sysprof_subprocess_output_prepare (SysprofInstrument *instrument, |
| 167 |
SysprofRecording *recording) |
| 168 |
{ |
| 169 |
SysprofSubprocessOutput *self = (SysprofSubprocessOutput *)instrument; |
| 170 |
|
| 171 |
g_assert (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 172 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 173 |
|
| 174 |
if (self->phase != SYSPROF_RECORDING_PHASE_PREPARE) |
| 175 |
return dex_future_new_for_boolean (TRUE); |
| 176 |
|
| 177 |
g_set_object (&self->recording, recording); |
| 178 |
|
| 179 |
return dex_scheduler_spawn (NULL, 0, |
| 180 |
sysprof_subprocess_output_record_fiber, |
| 181 |
g_object_ref (self), |
| 182 |
g_object_unref); |
| 183 |
} |
| 184 |
|
| 185 |
static DexFuture * |
| 186 |
sysprof_subprocess_output_augment (SysprofInstrument *instrument, |
| 187 |
SysprofRecording *recording) |
| 188 |
{ |
| 189 |
SysprofSubprocessOutput *self = (SysprofSubprocessOutput *)instrument; |
| 190 |
|
| 191 |
g_assert (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 192 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 193 |
|
| 194 |
if (self->phase != SYSPROF_RECORDING_PHASE_AUGMENT) |
| 195 |
return dex_future_new_for_boolean (TRUE); |
| 196 |
|
| 197 |
g_set_object (&self->recording, recording); |
| 198 |
|
| 199 |
return dex_scheduler_spawn (NULL, 0, |
| 200 |
sysprof_subprocess_output_record_fiber, |
| 201 |
g_object_ref (self), |
| 202 |
g_object_unref); |
| 203 |
} |
| 204 |
|
| 205 |
static void |
| 206 |
sysprof_subprocess_output_dispose (GObject *object) |
| 207 |
{ |
| 208 |
SysprofSubprocessOutput *self = (SysprofSubprocessOutput *)object; |
| 209 |
|
| 210 |
g_clear_pointer (&self->command_argv, g_strfreev); |
| 211 |
g_clear_pointer (&self->command_environ, g_strfreev); |
| 212 |
g_clear_pointer (&self->command_cwd, g_free); |
| 213 |
g_clear_pointer (&self->stdout_path, g_free); |
| 214 |
g_clear_object (&self->recording); |
| 215 |
g_clear_object (&self->cancellable); |
| 216 |
|
| 217 |
G_OBJECT_CLASS (sysprof_subprocess_output_parent_class)->dispose (object); |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
sysprof_subprocess_output_get_property (GObject *object, |
| 222 |
guint prop_id, |
| 223 |
GValue *value, |
| 224 |
GParamSpec *pspec) |
| 225 |
{ |
| 226 |
SysprofSubprocessOutput *self = SYSPROF_SUBPROCESS_OUTPUT (object); |
| 227 |
|
| 228 |
switch (prop_id) |
| 229 |
{ |
| 230 |
case PROP_COMMAND_CWD: |
| 231 |
g_value_set_string (value, sysprof_subprocess_output_get_command_cwd (self)); |
| 232 |
break; |
| 233 |
|
| 234 |
case PROP_COMMAND_ARGV: |
| 235 |
g_value_set_boxed (value, sysprof_subprocess_output_get_command_argv (self)); |
| 236 |
break; |
| 237 |
|
| 238 |
case PROP_COMMAND_ENVIRON: |
| 239 |
g_value_set_boxed (value, sysprof_subprocess_output_get_command_environ (self)); |
| 240 |
break; |
| 241 |
|
| 242 |
case PROP_PHASE: |
| 243 |
g_value_set_enum (value, sysprof_subprocess_output_get_phase (self)); |
| 244 |
break; |
| 245 |
|
| 246 |
case PROP_STDOUT_PATH: |
| 247 |
g_value_set_string (value, sysprof_subprocess_output_get_stdout_path (self)); |
| 248 |
break; |
| 249 |
|
| 250 |
default: |
| 251 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
static void |
| 256 |
sysprof_subprocess_output_set_property (GObject *object, |
| 257 |
guint prop_id, |
| 258 |
const GValue *value, |
| 259 |
GParamSpec *pspec) |
| 260 |
{ |
| 261 |
SysprofSubprocessOutput *self = SYSPROF_SUBPROCESS_OUTPUT (object); |
| 262 |
|
| 263 |
switch (prop_id) |
| 264 |
{ |
| 265 |
case PROP_COMMAND_CWD: |
| 266 |
sysprof_subprocess_output_set_command_cwd (self, g_value_get_string (value)); |
| 267 |
break; |
| 268 |
|
| 269 |
case PROP_COMMAND_ARGV: |
| 270 |
sysprof_subprocess_output_set_command_argv (self, g_value_get_boxed (value)); |
| 271 |
break; |
| 272 |
|
| 273 |
case PROP_COMMAND_ENVIRON: |
| 274 |
sysprof_subprocess_output_set_command_environ (self, g_value_get_boxed (value)); |
| 275 |
break; |
| 276 |
|
| 277 |
case PROP_PHASE: |
| 278 |
sysprof_subprocess_output_set_phase (self, g_value_get_enum (value)); |
| 279 |
break; |
| 280 |
|
| 281 |
case PROP_STDOUT_PATH: |
| 282 |
sysprof_subprocess_output_set_stdout_path (self, g_value_get_string (value)); |
| 283 |
break; |
| 284 |
|
| 285 |
default: |
| 286 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
static void |
| 291 |
sysprof_subprocess_output_class_init (SysprofSubprocessOutputClass *klass) |
| 292 |
{ |
| 293 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 294 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 295 |
|
| 296 |
object_class->dispose = sysprof_subprocess_output_dispose; |
| 297 |
object_class->get_property = sysprof_subprocess_output_get_property; |
| 298 |
object_class->set_property = sysprof_subprocess_output_set_property; |
| 299 |
|
| 300 |
instrument_class->prepare = sysprof_subprocess_output_prepare; |
| 301 |
instrument_class->record = sysprof_subprocess_output_record; |
| 302 |
instrument_class->augment = sysprof_subprocess_output_augment; |
| 303 |
|
| 304 |
properties[PROP_COMMAND_CWD] = |
| 305 |
g_param_spec_string ("command-cwd", NULL, NULL, |
| 306 |
NULL, |
| 307 |
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); |
| 308 |
|
| 309 |
properties[PROP_COMMAND_ARGV] = |
| 310 |
g_param_spec_boxed ("command-argv", NULL, NULL, |
| 311 |
G_TYPE_STRV, |
| 312 |
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); |
| 313 |
|
| 314 |
properties[PROP_COMMAND_ENVIRON] = |
| 315 |
g_param_spec_boxed ("command-environ", NULL, NULL, |
| 316 |
G_TYPE_STRV, |
| 317 |
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); |
| 318 |
|
| 319 |
properties[PROP_PHASE] = |
| 320 |
g_param_spec_enum ("phase", NULL, NULL, |
| 321 |
SYSPROF_TYPE_RECORDING_PHASE, |
| 322 |
SYSPROF_RECORDING_PHASE_PREPARE, |
| 323 |
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); |
| 324 |
|
| 325 |
properties[PROP_STDOUT_PATH] = |
| 326 |
g_param_spec_string ("stdout-path", NULL, NULL, |
| 327 |
NULL, |
| 328 |
(G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS)); |
| 329 |
|
| 330 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 331 |
} |
| 332 |
|
| 333 |
static void |
| 334 |
sysprof_subprocess_output_init (SysprofSubprocessOutput *self) |
| 335 |
{ |
| 336 |
self->phase = SYSPROF_RECORDING_PHASE_PREPARE; |
| 337 |
} |
| 338 |
|
| 339 |
const char * |
| 340 |
sysprof_subprocess_output_get_command_cwd (SysprofSubprocessOutput *self) |
| 341 |
{ |
| 342 |
g_return_val_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self), NULL); |
| 343 |
|
| 344 |
return self->command_cwd; |
| 345 |
} |
| 346 |
|
| 347 |
void |
| 348 |
sysprof_subprocess_output_set_command_cwd (SysprofSubprocessOutput *self, |
| 349 |
const char *command_cwd) |
| 350 |
{ |
| 351 |
g_return_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 352 |
|
| 353 |
if (g_set_str (&self->command_cwd, command_cwd)) |
| 354 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COMMAND_CWD]); |
| 355 |
} |
| 356 |
|
| 357 |
const char * const * |
| 358 |
sysprof_subprocess_output_get_command_argv (SysprofSubprocessOutput *self) |
| 359 |
{ |
| 360 |
g_return_val_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self), NULL); |
| 361 |
|
| 362 |
return (const char * const *)self->command_argv; |
| 363 |
} |
| 364 |
|
| 365 |
void |
| 366 |
sysprof_subprocess_output_set_command_argv (SysprofSubprocessOutput *self, |
| 367 |
const char * const *command_argv) |
| 368 |
{ |
| 369 |
char **copy; |
| 370 |
|
| 371 |
g_return_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 372 |
|
| 373 |
if (command_argv == (gpointer)self->command_argv || |
| 374 |
(command_argv && self->command_argv && |
| 375 |
g_strv_equal (command_argv, (const char * const *)self->command_argv))) |
| 376 |
return; |
| 377 |
|
| 378 |
copy = g_strdupv ((char **)command_argv); |
| 379 |
|
| 380 |
g_strfreev (self->command_argv); |
| 381 |
self->command_argv = copy; |
| 382 |
|
| 383 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COMMAND_ARGV]); |
| 384 |
} |
| 385 |
|
| 386 |
const char * const * |
| 387 |
sysprof_subprocess_output_get_command_environ (SysprofSubprocessOutput *self) |
| 388 |
{ |
| 389 |
g_return_val_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self), NULL); |
| 390 |
|
| 391 |
return (const char * const *)self->command_environ; |
| 392 |
} |
| 393 |
|
| 394 |
void |
| 395 |
sysprof_subprocess_output_set_command_environ (SysprofSubprocessOutput *self, |
| 396 |
const char * const *command_environ) |
| 397 |
{ |
| 398 |
char **copy; |
| 399 |
|
| 400 |
g_return_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 401 |
|
| 402 |
if (command_environ == (gpointer)self->command_environ || |
| 403 |
(command_environ && self->command_environ && |
| 404 |
g_strv_equal (command_environ, (const char * const *)self->command_environ))) |
| 405 |
return; |
| 406 |
|
| 407 |
copy = g_strdupv ((char **)command_environ); |
| 408 |
|
| 409 |
g_strfreev (self->command_environ); |
| 410 |
self->command_environ = copy; |
| 411 |
|
| 412 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_COMMAND_ENVIRON]); |
| 413 |
} |
| 414 |
|
| 415 |
const char * |
| 416 |
sysprof_subprocess_output_get_stdout_path (SysprofSubprocessOutput *self) |
| 417 |
{ |
| 418 |
g_return_val_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self), NULL); |
| 419 |
|
| 420 |
return self->stdout_path; |
| 421 |
} |
| 422 |
|
| 423 |
void |
| 424 |
sysprof_subprocess_output_set_stdout_path (SysprofSubprocessOutput *self, |
| 425 |
const char *stdout_path) |
| 426 |
{ |
| 427 |
g_return_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self)); |
| 428 |
|
| 429 |
if (g_set_str (&self->stdout_path, stdout_path)) |
| 430 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_STDOUT_PATH]); |
| 431 |
} |
| 432 |
|
| 433 |
SysprofRecordingPhase |
| 434 |
sysprof_subprocess_output_get_phase (SysprofSubprocessOutput *self) |
| 435 |
{ |
| 436 |
g_return_val_if_fail (SYSPROF_IS_SUBPROCESS_OUTPUT (self), 0); |
| 437 |
|
| 438 |
return self->phase; |
| 439 |
} |
| 440 |
|
| 441 |
void |
| 442 |
sysprof_subprocess_output_set_phase (SysprofSubprocessOutput *self, |
| 443 |
SysprofRecordingPhase phase) |
| 444 |
{ |
| 445 |
g_return_if_fail (phase > 0); |
| 446 |
g_return_if_fail (phase <= SYSPROF_RECORDING_PHASE_AUGMENT); |
| 447 |
|
| 448 |
if (phase == self->phase) |
| 449 |
return; |
| 450 |
|
| 451 |
self->phase = phase; |
| 452 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PHASE]); |
| 453 |
} |