tools: add --env=VAR=VALUE command line option

This commit is contained in:
Christian Hergert
2020-02-21 12:40:48 -08:00
parent cd2c2c954a
commit 98f6dc6642

View File

@ -184,6 +184,7 @@ main (gint argc,
gchar *argv[])
{
g_auto(GStrv) child_argv = NULL;
g_auto(GStrv) envs = NULL;
PolkitAgentListener *polkit = NULL;
PolkitSubject *subject = NULL;
SysprofCaptureWriter *writer;
@ -215,6 +216,7 @@ main (gint argc,
GOptionEntry entries[] = {
{ "pid", 'p', 0, G_OPTION_ARG_INT, &pid, N_("Make sysprof specific to a task"), N_("PID") },
{ "command", 'c', 0, G_OPTION_ARG_STRING, &command, N_("Run a command and profile the process"), N_("COMMAND") },
{ "env", 'e', 0, G_OPTION_ARG_STRING_ARRAY, &envs, N_("Set environment variable for spawned process. Can be used multiple times."), N_("VAR=VALUE") },
{ "force", 'f', 0, G_OPTION_ARG_NONE, &force, N_("Force overwrite the capture file") },
{ "no-battery", 0, 0, G_OPTION_ARG_NONE, &no_battery, N_("Disable recording of battery statistics") },
{ "no-cpu", 0, 0, G_OPTION_ARG_NONE, &no_cpu, N_("Disable recording of CPU statistics") },
@ -384,6 +386,24 @@ Examples:\n\
cwd = g_get_current_dir ();
if (envs != NULL)
{
for (guint e = 0; envs[e]; e++)
{
const gchar *eq = strchr (envs[e], '=');
if (eq == NULL)
{
env = g_environ_setenv (env, envs[e], "", TRUE);
}
else
{
g_autofree gchar *key = g_strndup (envs[e], eq - envs[e]);
env = g_environ_setenv (env, key, eq+1, TRUE);
}
}
}
sysprof_profiler_set_spawn (profiler, TRUE);
sysprof_profiler_set_spawn_cwd (profiler, cwd);
sysprof_profiler_set_spawn_argv (profiler, (const gchar * const *)child_argv);