| 1 |
/* sysprof-cli.c |
| 2 |
* |
| 3 |
* Copyright 2016-2019 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 <fcntl.h> |
| 24 |
#include <locale.h> |
| 25 |
#include <signal.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <sys/types.h> |
| 28 |
|
| 29 |
#include <glib-unix.h> |
| 30 |
#include <glib/gi18n.h> |
| 31 |
#include <glib/gstdio.h> |
| 32 |
|
| 33 |
#include <libdex.h> |
| 34 |
#include <sysprof.h> |
| 35 |
|
| 36 |
#if HAVE_POLKIT_AGENT |
| 37 |
# define POLKIT_AGENT_I_KNOW_API_IS_SUBJECT_TO_CHANGE |
| 38 |
# include <polkit/polkit.h> |
| 39 |
# include <polkitagent/polkitagent.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
#if HAVE_LIBSYSTEMD |
| 43 |
# include <systemd/sd-daemon.h> |
| 44 |
#endif |
| 45 |
|
| 46 |
#include "sysprof-capture-util-private.h" |
| 47 |
|
| 48 |
#define DEFAULT_BUFFER_SIZE (1024L * 1024L * 8L /* 8mb */) |
| 49 |
|
| 50 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref) |
| 51 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureWriter, sysprof_capture_writer_unref) |
| 52 |
|
| 53 |
static gboolean no_decode; |
| 54 |
static gboolean disable_debuginfod; |
| 55 |
static GMainLoop *main_loop; |
| 56 |
static SysprofRecording *active_recording; |
| 57 |
|
| 58 |
static void |
| 59 |
diagnostics_items_changed_cb (GListModel *model, |
| 60 |
guint position, |
| 61 |
guint removed, |
| 62 |
guint added, |
| 63 |
gpointer user_data) |
| 64 |
{ |
| 65 |
g_assert (G_IS_LIST_MODEL (model)); |
| 66 |
g_assert (user_data == NULL); |
| 67 |
|
| 68 |
for (guint i = 0; i < added; i++) |
| 69 |
{ |
| 70 |
g_autoptr(SysprofDiagnostic) diagnostic = g_list_model_get_item (model, position+i); |
| 71 |
|
| 72 |
g_printerr ("%s: %s\n", |
| 73 |
sysprof_diagnostic_get_domain (diagnostic), |
| 74 |
sysprof_diagnostic_get_message (diagnostic)); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static gboolean |
| 79 |
sigint_handler (gpointer user_data) |
| 80 |
{ |
| 81 |
static int count; |
| 82 |
|
| 83 |
if (count >= 2) |
| 84 |
{ |
| 85 |
g_main_loop_quit (main_loop); |
| 86 |
return G_SOURCE_REMOVE; |
| 87 |
} |
| 88 |
|
| 89 |
g_printerr ("\n"); |
| 90 |
|
| 91 |
if (count == 0) |
| 92 |
{ |
| 93 |
g_printerr ("%s\n", "Stopping profiler. Press twice more ^C to force exit."); |
| 94 |
if (!no_decode) |
| 95 |
g_printerr ("Decoding symbols and appending to capture.\n"); |
| 96 |
sysprof_recording_stop_async (active_recording, NULL, NULL, NULL); |
| 97 |
} |
| 98 |
|
| 99 |
count++; |
| 100 |
|
| 101 |
return G_SOURCE_CONTINUE; |
| 102 |
} |
| 103 |
|
| 104 |
static int |
| 105 |
merge_files (int argc, |
| 106 |
char **argv, |
| 107 |
GOptionContext *context) |
| 108 |
{ |
| 109 |
g_autoptr(SysprofCaptureWriter) writer = NULL; |
| 110 |
g_autofree char *contents = NULL; |
| 111 |
g_autofree char *tmpname = NULL; |
| 112 |
gsize len; |
| 113 |
int fd; |
| 114 |
|
| 115 |
if (argc < 3) |
| 116 |
{ |
| 117 |
g_autofree char *help = NULL; |
| 118 |
|
| 119 |
help = g_option_context_get_help (context, FALSE, NULL); |
| 120 |
g_printerr (_("--merge requires at least 2 filename arguments")); |
| 121 |
g_printerr ("\n\n%s\n", help); |
| 122 |
|
| 123 |
return EXIT_FAILURE; |
| 124 |
} |
| 125 |
|
| 126 |
if (isatty (STDOUT_FILENO)) |
| 127 |
{ |
| 128 |
g_printerr ("stdout is a TTY, refusing to write binary data to stdout.\n"); |
| 129 |
return EXIT_FAILURE; |
| 130 |
} |
| 131 |
|
| 132 |
for (guint i = 1; i < argc; i++) |
| 133 |
{ |
| 134 |
if (!g_file_test (argv[i], G_FILE_TEST_IS_REGULAR)) |
| 135 |
{ |
| 136 |
g_printerr ("\"%s\" is not a regular file.\n", argv[i]); |
| 137 |
return EXIT_FAILURE; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (-1 == (fd = g_file_open_tmp (".sysprof-cat-XXXXXX", &tmpname, NULL))) |
| 142 |
{ |
| 143 |
g_printerr ("Failed to create memfd for capture file.\n"); |
| 144 |
return EXIT_FAILURE; |
| 145 |
} |
| 146 |
|
| 147 |
writer = sysprof_capture_writer_new_from_fd (fd, 0); |
| 148 |
|
| 149 |
for (guint i = 1; i < argc; i++) |
| 150 |
{ |
| 151 |
g_autoptr(SysprofCaptureReader) reader = NULL; |
| 152 |
|
| 153 |
if (!(reader = sysprof_capture_reader_new (argv[i]))) |
| 154 |
{ |
| 155 |
int errsv = errno; |
| 156 |
g_printerr ("Failed to create reader for \"%s\": %s\n", |
| 157 |
argv[i], g_strerror (errsv)); |
| 158 |
return EXIT_FAILURE; |
| 159 |
} |
| 160 |
|
| 161 |
if (!sysprof_capture_writer_cat (writer, reader)) |
| 162 |
{ |
| 163 |
int errsv = errno; |
| 164 |
g_printerr ("Failed to join \"%s\": %s\n", |
| 165 |
argv[i], g_strerror (errsv)); |
| 166 |
return EXIT_FAILURE; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
if (g_file_get_contents (tmpname, &contents, &len, NULL)) |
| 171 |
{ |
| 172 |
const char *buf = contents; |
| 173 |
gsize to_write = len; |
| 174 |
|
| 175 |
while (to_write > 0) |
| 176 |
{ |
| 177 |
gssize n_written; |
| 178 |
|
| 179 |
n_written = _sysprof_write (STDOUT_FILENO, buf, to_write); |
| 180 |
|
| 181 |
if (n_written < 0) |
| 182 |
{ |
| 183 |
g_printerr ("%s\n", g_strerror (errno)); |
| 184 |
g_unlink (tmpname); |
| 185 |
return EXIT_FAILURE; |
| 186 |
} |
| 187 |
|
| 188 |
buf += n_written; |
| 189 |
to_write -= n_written; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
g_unlink (tmpname); |
| 194 |
|
| 195 |
return EXIT_SUCCESS; |
| 196 |
} |
| 197 |
|
| 198 |
static void |
| 199 |
sysprof_cli_wait_cb (GObject *object, |
| 200 |
GAsyncResult *result, |
| 201 |
gpointer user_data) |
| 202 |
{ |
| 203 |
SysprofRecording *recording = (SysprofRecording *)object; |
| 204 |
g_autoptr(GError) error = NULL; |
| 205 |
|
| 206 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 207 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 208 |
g_assert (user_data == NULL); |
| 209 |
|
| 210 |
if (!sysprof_recording_wait_finish (recording, result, &error)) |
| 211 |
{ |
| 212 |
if (error->domain != G_SPAWN_EXIT_ERROR) |
| 213 |
g_printerr ("Failed to complete recording: %s\n", |
| 214 |
error->message); |
| 215 |
} |
| 216 |
|
| 217 |
g_main_loop_quit (main_loop); |
| 218 |
} |
| 219 |
|
| 220 |
static void |
| 221 |
sysprof_cli_record_cb (GObject *object, |
| 222 |
GAsyncResult *result, |
| 223 |
gpointer user_data) |
| 224 |
{ |
| 225 |
SysprofProfiler *profiler = (SysprofProfiler *)object; |
| 226 |
g_autoptr(SysprofRecording) recording = NULL; |
| 227 |
g_autoptr(GListModel) diagnostics = NULL; |
| 228 |
g_autoptr(GError) error = NULL; |
| 229 |
|
| 230 |
g_assert (SYSPROF_IS_PROFILER (profiler)); |
| 231 |
g_assert (G_IS_ASYNC_RESULT (result)); |
| 232 |
g_assert (user_data == NULL); |
| 233 |
|
| 234 |
if (!(recording = sysprof_profiler_record_finish (profiler, result, &error))) |
| 235 |
g_error ("Failed to start profiling session: %s", error->message); |
| 236 |
|
| 237 |
diagnostics = sysprof_recording_list_diagnostics (recording); |
| 238 |
g_signal_connect (diagnostics, |
| 239 |
"items-changed", |
| 240 |
G_CALLBACK (diagnostics_items_changed_cb), |
| 241 |
NULL); |
| 242 |
diagnostics_items_changed_cb (diagnostics, |
| 243 |
0, |
| 244 |
0, |
| 245 |
g_list_model_get_n_items (diagnostics), |
| 246 |
NULL); |
| 247 |
|
| 248 |
sysprof_recording_wait_async (recording, |
| 249 |
NULL, |
| 250 |
sysprof_cli_wait_cb, |
| 251 |
NULL); |
| 252 |
|
| 253 |
g_set_object (&active_recording, recording); |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
add_trace_fd (SysprofProfiler *profiler, |
| 258 |
SysprofSpawnable *spawnable, |
| 259 |
const char *name) |
| 260 |
{ |
| 261 |
int trace_fd = -1; |
| 262 |
|
| 263 |
g_assert (SYSPROF_IS_PROFILER (profiler)); |
| 264 |
g_assert (!spawnable || SYSPROF_IS_SPAWNABLE (spawnable)); |
| 265 |
|
| 266 |
if (spawnable == NULL) |
| 267 |
return; |
| 268 |
|
| 269 |
trace_fd = sysprof_spawnable_add_trace_fd (spawnable, name); |
| 270 |
sysprof_profiler_add_instrument (profiler, sysprof_tracefd_consumer_new (g_steal_fd (&trace_fd))); |
| 271 |
} |
| 272 |
|
| 273 |
int |
| 274 |
main (int argc, |
| 275 |
char *argv[]) |
| 276 |
{ |
| 277 |
#if HAVE_POLKIT_AGENT |
| 278 |
PolkitAgentListener *polkit = NULL; |
| 279 |
PolkitSubject *subject = NULL; |
| 280 |
#endif |
| 281 |
g_autoptr(SysprofCaptureWriter) writer = NULL; |
| 282 |
g_autoptr(SysprofProfiler) profiler = NULL; |
| 283 |
g_autofree char *power_profile = NULL; |
| 284 |
g_auto(GStrv) child_argv = NULL; |
| 285 |
g_auto(GStrv) envs = NULL; |
| 286 |
g_auto(GStrv) monitor_bus = NULL; |
| 287 |
GMainContext *main_context; |
| 288 |
GOptionContext *context; |
| 289 |
const char *filename = "capture.syscap"; |
| 290 |
GError *error = NULL; |
| 291 |
char *command = NULL; |
| 292 |
gboolean gjs = FALSE; |
| 293 |
gboolean gtk = FALSE; |
| 294 |
gboolean no_battery = FALSE; |
| 295 |
gboolean no_cpu = FALSE; |
| 296 |
gboolean no_disk = FALSE; |
| 297 |
gboolean no_memory = FALSE; |
| 298 |
gboolean no_network = FALSE; |
| 299 |
gboolean no_perf = FALSE; |
| 300 |
gboolean no_throttle = FALSE; |
| 301 |
gboolean version = FALSE; |
| 302 |
gboolean force = FALSE; |
| 303 |
gboolean use_trace_fd = FALSE; |
| 304 |
gboolean gnome_shell = FALSE; |
| 305 |
gboolean rapl = FALSE; |
| 306 |
gboolean memprof = FALSE; |
| 307 |
gboolean merge = FALSE; |
| 308 |
gboolean speedtrack = FALSE; |
| 309 |
gboolean scheduler_details = FALSE; |
| 310 |
gboolean system_bus = FALSE; |
| 311 |
gboolean session_bus = FALSE; |
| 312 |
gboolean no_sysprofd = FALSE; |
| 313 |
int stack_size = 0; |
| 314 |
int pid = -1; |
| 315 |
int fd; |
| 316 |
int flags; |
| 317 |
int n_buffer_pages = (DEFAULT_BUFFER_SIZE / sysprof_getpagesize ()); |
| 318 |
GOptionEntry entries[] = { |
| 319 |
{ "no-throttle", 0, 0, G_OPTION_ARG_NONE, &no_throttle, N_("Disable CPU throttling while profiling [Deprecated for --power-profile]") }, |
| 320 |
{ "pid", 'p', 0, G_OPTION_ARG_INT, &pid, N_("Make sysprof specific to a task [Deprecated]"), N_("PID") }, |
| 321 |
{ "command", 'c', 0, G_OPTION_ARG_STRING, &command, N_("Run a command and profile the process"), N_("COMMAND") }, |
| 322 |
{ "env", 'e', 0, G_OPTION_ARG_STRING_ARRAY, &envs, N_("Set environment variable for spawned process. Can be used multiple times."), N_("VAR=VALUE") }, |
| 323 |
{ "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Force overwrite the capture file") }, |
| 324 |
{ "no-battery", 0, 0, G_OPTION_ARG_NONE, &no_battery, N_("Disable recording of battery statistics") }, |
| 325 |
{ "no-cpu", 0, 0, G_OPTION_ARG_NONE, &no_cpu, N_("Disable recording of CPU statistics") }, |
| 326 |
{ "no-disk", 0, 0, G_OPTION_ARG_NONE, &no_disk, N_("Disable recording of Disk statistics") }, |
| 327 |
{ "no-perf", 0, 0, G_OPTION_ARG_NONE, &no_perf, N_("Do not record stacktraces using Linux perf") }, |
| 328 |
{ "no-decode", 0, 0, G_OPTION_ARG_NONE, &no_decode, N_("Do not append symbol name information from local machine") }, |
| 329 |
{ "no-memory", 0, 0, G_OPTION_ARG_NONE, &no_memory, N_("Disable recording of memory statistics") }, |
| 330 |
{ "no-network", 0, 0, G_OPTION_ARG_NONE, &no_network, N_("Disable recording of network statistics") }, |
| 331 |
{ "use-trace-fd", 0, 0, G_OPTION_ARG_NONE, &use_trace_fd, N_("Set SYSPROF_TRACE_FD environment for subprocess") }, |
| 332 |
{ "scheduler", 0, 0, G_OPTION_ARG_NONE, &scheduler_details, N_("Track when processes are scheduled") }, |
| 333 |
{ "session-bus", 0, 0, G_OPTION_ARG_NONE, &session_bus, N_("Profile the D-Bus session bus") }, |
| 334 |
{ "system-bus", 0, 0, G_OPTION_ARG_NONE, &system_bus, N_("Profile the D-Bus system bus") }, |
| 335 |
{ "gjs", 0, 0, G_OPTION_ARG_NONE, &gjs, N_("Set GJS_TRACE_FD environment to trace GJS processes") }, |
| 336 |
{ "gtk", 0, 0, G_OPTION_ARG_NONE, >k, N_("Set GTK_TRACE_FD environment to trace a GTK application") }, |
| 337 |
{ "rapl", 0, 0, G_OPTION_ARG_NONE, &rapl, N_("Include RAPL energy statistics") }, |
| 338 |
{ "memprof", 0, 0, G_OPTION_ARG_NONE, &memprof, N_("Profile memory allocations and frees") }, |
| 339 |
{ "gnome-shell", 0, 0, G_OPTION_ARG_NONE, &gnome_shell, N_("Connect to org.gnome.Shell for profiler statistics") }, |
| 340 |
{ "speedtrack", 0, 0, G_OPTION_ARG_NONE, &speedtrack, N_("Track performance of the applications main loop") }, |
| 341 |
{ "power-profile", 0, 0, G_OPTION_ARG_STRING, &power_profile, "Use POWER_PROFILE for duration of recording", "power-saver|balanced|performance" }, |
| 342 |
{ "merge", 0, 0, G_OPTION_ARG_NONE, &merge, N_("Merge all provided *.syscap files and write to stdout") }, |
| 343 |
{ "version", 0, 0, G_OPTION_ARG_NONE, &version, N_("Print the sysprof-cli version and exit") }, |
| 344 |
{ "buffer-size", 0, 0, G_OPTION_ARG_INT, &n_buffer_pages, N_("The size of the buffer in pages (1 = 1 page)") }, |
| 345 |
{ "monitor-bus", 0, 0, G_OPTION_ARG_STRING_ARRAY, &monitor_bus, N_("Additional D-Bus address to monitor") }, |
| 346 |
{ "stack-size", 0, 0, G_OPTION_ARG_INT, &stack_size, N_("Stack size to copy for unwinding in user-space") }, |
| 347 |
{ "no-debuginfod", 0, 0, G_OPTION_ARG_NONE, &disable_debuginfod, N_("Do not use debuginfod to resolve symbols") }, |
| 348 |
{ "no-sysprofd", 0, 0, G_OPTION_ARG_NONE, &no_sysprofd, N_("Do not use Sysprofd to acquire privileges") }, |
| 349 |
{ NULL } |
| 350 |
}; |
| 351 |
|
| 352 |
sysprof_clock_init (); |
| 353 |
dex_init (); |
| 354 |
|
| 355 |
g_unix_signal_add (SIGINT, sigint_handler, main_loop); |
| 356 |
g_unix_signal_add (SIGTERM, sigint_handler, main_loop); |
| 357 |
|
| 358 |
/* Set up gettext translations */ |
| 359 |
setlocale (LC_ALL, ""); |
| 360 |
bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
| 361 |
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
| 362 |
textdomain (GETTEXT_PACKAGE); |
| 363 |
|
| 364 |
/* Before we start processing argv, look for "--" and take everything after |
| 365 |
* it as arguments as a "-c" replacement. |
| 366 |
*/ |
| 367 |
for (guint i = 1; i < argc; i++) |
| 368 |
{ |
| 369 |
if (g_str_equal (argv[i], "--")) |
| 370 |
{ |
| 371 |
GPtrArray *ar = g_ptr_array_new (); |
| 372 |
for (guint j = i + 1; j < argc; j++) |
| 373 |
g_ptr_array_add (ar, g_strdup (argv[j])); |
| 374 |
g_ptr_array_add (ar, NULL); |
| 375 |
child_argv = (char **)g_ptr_array_free (ar, FALSE); |
| 376 |
/* Skip everything from -- beyond */ |
| 377 |
argc = i; |
| 378 |
break; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
context = g_option_context_new (_("[CAPTURE_FILE] [-- COMMAND ARGS] — Sysprof")); |
| 383 |
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE); |
| 384 |
|
| 385 |
g_option_context_set_summary (context, N_("\n\ |
| 386 |
Examples:\n\ |
| 387 |
\n\ |
| 388 |
# Record gtk4-widget-factory using trace-fd to get application provided\n\ |
| 389 |
# data as well as GTK and GNOME Shell data providers\n\ |
| 390 |
sysprof-cli --gtk --gnome-shell --use-trace-fd -- gtk4-widget-factory\n\ |
| 391 |
\n\ |
| 392 |
# Merge multiple syscap files into one\n\ |
| 393 |
sysprof-cli --merge a.syscap b.syscap > c.syscap\n\ |
| 394 |
\n\ |
| 395 |
# Unwind by capturing stack/register contents instead of frame-pointers\n\ |
| 396 |
# where the stack-size is a multiple of page-size\n\ |
| 397 |
sysprof-cli --stack-size=8192\n\ |
| 398 |
")); |
| 399 |
|
| 400 |
if (!g_option_context_parse (context, &argc, &argv, &error)) |
| 401 |
{ |
| 402 |
g_printerr ("%s\n", error->message); |
| 403 |
return EXIT_FAILURE; |
| 404 |
} |
| 405 |
|
| 406 |
if (version) |
| 407 |
{ |
| 408 |
g_print ("Sysprof "PACKAGE_VERSION"\n"); |
| 409 |
return EXIT_SUCCESS; |
| 410 |
} |
| 411 |
|
| 412 |
if (pid != -1) |
| 413 |
g_printerr ("--pid is no longer supported and will be ignored\n"); |
| 414 |
|
| 415 |
/* If merge is set, we aren't recording, but instead merging a bunch of |
| 416 |
* files together into a single syscap. |
| 417 |
*/ |
| 418 |
if (merge) |
| 419 |
return merge_files (argc, argv, context); |
| 420 |
|
| 421 |
if (argc > 2) |
| 422 |
{ |
| 423 |
int i; |
| 424 |
|
| 425 |
g_printerr (_("Too many arguments were passed to sysprof-cli:")); |
| 426 |
|
| 427 |
for (i = 2; i < argc; i++) |
| 428 |
g_printerr (" %s", argv [i]); |
| 429 |
g_printerr ("\n"); |
| 430 |
|
| 431 |
return EXIT_FAILURE; |
| 432 |
} |
| 433 |
|
| 434 |
main_loop = g_main_loop_new (NULL, FALSE); |
| 435 |
|
| 436 |
#if HAVE_POLKIT_AGENT |
| 437 |
/* Start polkit agent so that we can elevate privileges from a TTY */ |
| 438 |
if (g_getenv ("DESKTOP_SESSION") == NULL && |
| 439 |
(subject = polkit_unix_process_new_for_owner (getpid (), 0, -1))) |
| 440 |
{ |
| 441 |
g_autoptr(GError) pkerror = NULL; |
| 442 |
|
| 443 |
polkit = polkit_agent_text_listener_new (NULL, NULL); |
| 444 |
polkit_agent_listener_register (polkit, |
| 445 |
POLKIT_AGENT_REGISTER_FLAGS_NONE, |
| 446 |
subject, |
| 447 |
NULL, |
| 448 |
NULL, |
| 449 |
&pkerror); |
| 450 |
|
| 451 |
if (pkerror != NULL) |
| 452 |
{ |
| 453 |
g_dbus_error_strip_remote_error (pkerror); |
| 454 |
g_printerr ("Failed to register polkit agent: %s\n", |
| 455 |
pkerror->message); |
| 456 |
} |
| 457 |
} |
| 458 |
#endif |
| 459 |
|
| 460 |
/* Warn about access if we're in a container */ |
| 461 |
if (g_file_test ("/.flatpak-info", G_FILE_TEST_EXISTS)) |
| 462 |
{ |
| 463 |
if (!g_file_test ("/var/run/host/usr", G_FILE_TEST_EXISTS)) |
| 464 |
g_printerr ("Warning: Flatpak detected but cannot access host, set --filesystem=host\n"); |
| 465 |
} |
| 466 |
|
| 467 |
profiler = sysprof_profiler_new (); |
| 468 |
sysprof_profiler_set_acquire_privileges (profiler, !no_sysprofd); |
| 469 |
|
| 470 |
if (argc == 2) |
| 471 |
filename = argv[1]; |
| 472 |
|
| 473 |
flags = O_CREAT | O_RDWR | O_CLOEXEC; |
| 474 |
if (!force) |
| 475 |
{ |
| 476 |
/* if we don't force overwrite we want to ensure the file doesn't exist |
| 477 |
* and never overwrite it. O_EXCL will prevent opening in that case */ |
| 478 |
flags |= O_EXCL; |
| 479 |
if (g_file_test (filename, G_FILE_TEST_EXISTS)) |
| 480 |
{ |
| 481 |
/* Translators: %s is a file name. */ |
| 482 |
g_printerr (_("%s exists. Use --force to overwrite\n"), filename); |
| 483 |
return EXIT_FAILURE; |
| 484 |
} |
| 485 |
} |
| 486 |
if (-1 == (fd = g_open (filename, flags, 0640))) |
| 487 |
{ |
| 488 |
g_printerr ("Failed to open %s\n", filename); |
| 489 |
return EXIT_FAILURE; |
| 490 |
} |
| 491 |
|
| 492 |
writer = sysprof_capture_writer_new_from_fd (fd, n_buffer_pages * sysprof_getpagesize ()); |
| 493 |
|
| 494 |
if (command != NULL || child_argv != NULL) |
| 495 |
{ |
| 496 |
g_autoptr(SysprofSpawnable) spawnable = sysprof_spawnable_new (); |
| 497 |
g_auto(GStrv) current_env = g_get_environ (); |
| 498 |
int child_argc; |
| 499 |
|
| 500 |
if (child_argv != NULL) |
| 501 |
{ |
| 502 |
child_argc = g_strv_length (child_argv); |
| 503 |
} |
| 504 |
else if (command && !g_shell_parse_argv (command, &child_argc, &child_argv, &error)) |
| 505 |
{ |
| 506 |
g_printerr ("Invalid command: %s\n", error->message); |
| 507 |
return EXIT_FAILURE; |
| 508 |
} |
| 509 |
|
| 510 |
sysprof_spawnable_set_cwd (spawnable, g_get_current_dir ()); |
| 511 |
sysprof_spawnable_append_args (spawnable, (const char * const *)child_argv); |
| 512 |
sysprof_spawnable_set_environ (spawnable, (const char * const *)current_env); |
| 513 |
|
| 514 |
if (envs != NULL) |
| 515 |
{ |
| 516 |
for (guint i = 0; envs[i]; i++) |
| 517 |
{ |
| 518 |
g_autofree char *key = NULL; |
| 519 |
const char *eq; |
| 520 |
|
| 521 |
if (!(eq = strchr (envs[i], '='))) |
| 522 |
{ |
| 523 |
sysprof_spawnable_setenv (spawnable, envs[i], ""); |
| 524 |
continue; |
| 525 |
} |
| 526 |
|
| 527 |
key = g_strndup (envs[i], eq - envs[i]); |
| 528 |
sysprof_spawnable_setenv (spawnable, key, eq+1); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
sysprof_profiler_set_spawnable (profiler, spawnable); |
| 533 |
|
| 534 |
if (gjs) |
| 535 |
{ |
| 536 |
sysprof_spawnable_setenv (spawnable, "GJS_ENABLE_PROFILER", "1"); |
| 537 |
add_trace_fd (profiler, spawnable, "GJS_TRACE_FD"); |
| 538 |
} |
| 539 |
|
| 540 |
if (use_trace_fd) |
| 541 |
add_trace_fd (profiler, spawnable, NULL); |
| 542 |
|
| 543 |
if (memprof) |
| 544 |
sysprof_spawnable_add_ld_preload (spawnable, PACKAGE_LIBDIR"/libsysprof-memory-"API_VERSION_S".so"); |
| 545 |
|
| 546 |
if (speedtrack) |
| 547 |
sysprof_spawnable_add_ld_preload (spawnable, PACKAGE_LIBDIR"/libsysprof-speedtrack-"API_VERSION_S".so"); |
| 548 |
} |
| 549 |
|
| 550 |
sysprof_profiler_add_instrument (profiler, sysprof_system_logs_new ()); |
| 551 |
|
| 552 |
if (!no_perf) |
| 553 |
{ |
| 554 |
if (stack_size == 0) |
| 555 |
sysprof_profiler_add_instrument (profiler, sysprof_sampler_new ()); |
| 556 |
else |
| 557 |
sysprof_profiler_add_instrument (profiler, sysprof_user_sampler_new (stack_size)); |
| 558 |
} |
| 559 |
|
| 560 |
if (!no_disk) |
| 561 |
sysprof_profiler_add_instrument (profiler, sysprof_disk_usage_new ()); |
| 562 |
|
| 563 |
if (!no_decode) |
| 564 |
{ |
| 565 |
SysprofInstrument *bundle = sysprof_symbols_bundle_new (); |
| 566 |
sysprof_symbols_bundle_set_enable_debuginfod (SYSPROF_SYMBOLS_BUNDLE (bundle), !disable_debuginfod); |
| 567 |
sysprof_profiler_add_instrument (profiler, g_steal_pointer (&bundle)); |
| 568 |
} |
| 569 |
|
| 570 |
if (!no_cpu) |
| 571 |
sysprof_profiler_add_instrument (profiler, sysprof_cpu_usage_new ()); |
| 572 |
|
| 573 |
if (!no_memory) |
| 574 |
sysprof_profiler_add_instrument (profiler, sysprof_memory_usage_new ()); |
| 575 |
|
| 576 |
if (!no_battery) |
| 577 |
sysprof_profiler_add_instrument (profiler, sysprof_battery_charge_new ()); |
| 578 |
|
| 579 |
if (rapl) |
| 580 |
sysprof_profiler_add_instrument (profiler, sysprof_energy_usage_new ()); |
| 581 |
|
| 582 |
if (!no_network) |
| 583 |
sysprof_profiler_add_instrument (profiler, sysprof_network_usage_new ()); |
| 584 |
|
| 585 |
if (power_profile) |
| 586 |
sysprof_profiler_add_instrument (profiler, sysprof_power_profile_new (power_profile)); |
| 587 |
else if (no_throttle) |
| 588 |
sysprof_profiler_add_instrument (profiler, sysprof_power_profile_new ("performance")); |
| 589 |
|
| 590 |
if (gnome_shell) |
| 591 |
sysprof_profiler_add_instrument (profiler, |
| 592 |
sysprof_proxied_instrument_new (G_BUS_TYPE_SESSION, |
| 593 |
"org.gnome.Shell", |
| 594 |
"/org/gnome/Sysprof3/Profiler")); |
| 595 |
|
| 596 |
if (session_bus) |
| 597 |
sysprof_profiler_add_instrument (profiler, sysprof_dbus_monitor_new (G_BUS_TYPE_SESSION)); |
| 598 |
|
| 599 |
if (system_bus) |
| 600 |
sysprof_profiler_add_instrument (profiler, sysprof_dbus_monitor_new (G_BUS_TYPE_SYSTEM)); |
| 601 |
|
| 602 |
if (scheduler_details) |
| 603 |
sysprof_profiler_add_instrument (profiler, sysprof_scheduler_details_new ()); |
| 604 |
|
| 605 |
if (monitor_bus != NULL) |
| 606 |
{ |
| 607 |
for (guint i = 0; monitor_bus[i]; i++) |
| 608 |
sysprof_profiler_add_instrument (profiler, |
| 609 |
sysprof_dbus_monitor_new_for_bus_address (monitor_bus[i])); |
| 610 |
} |
| 611 |
|
| 612 |
sysprof_profiler_add_instrument (profiler, |
| 613 |
g_object_new (SYSPROF_TYPE_SUBPROCESS_OUTPUT, |
| 614 |
"stdout-path", "eglinfo", |
| 615 |
"command-argv", (const char * const[]) {"eglinfo", NULL}, |
| 616 |
NULL)); |
| 617 |
sysprof_profiler_add_instrument (profiler, |
| 618 |
g_object_new (SYSPROF_TYPE_SUBPROCESS_OUTPUT, |
| 619 |
"stdout-path", "glxinfo", |
| 620 |
"command-argv", (const char * const[]) {"glxinfo", NULL}, |
| 621 |
NULL)); |
| 622 |
|
| 623 |
sysprof_profiler_record_async (profiler, |
| 624 |
writer, |
| 625 |
NULL, |
| 626 |
sysprof_cli_record_cb, |
| 627 |
NULL); |
| 628 |
|
| 629 |
g_unix_signal_add (SIGINT, sigint_handler, main_loop); |
| 630 |
g_unix_signal_add (SIGTERM, sigint_handler, main_loop); |
| 631 |
|
| 632 |
g_printerr ("Recording, press ^C to exit\n"); |
| 633 |
|
| 634 |
#if HAVE_LIBSYSTEMD |
| 635 |
if (command == NULL && child_argv == NULL) |
| 636 |
sd_notify (TRUE, "READY=1"); |
| 637 |
#endif |
| 638 |
|
| 639 |
g_main_loop_run (main_loop); |
| 640 |
|
| 641 |
sysprof_capture_writer_flush (writer); |
| 642 |
|
| 643 |
main_context = g_main_loop_get_context (main_loop); |
| 644 |
while (g_main_context_pending (main_context)) |
| 645 |
g_main_context_iteration (main_context, FALSE); |
| 646 |
|
| 647 |
sysprof_capture_writer_flush (writer); |
| 648 |
|
| 649 |
return EXIT_SUCCESS; |
| 650 |
} |