libsysprof: use helpers to get proc info from host

This commit is contained in:
Christian Hergert
2019-05-10 21:04:27 -07:00
parent 36ecfd3dd1
commit df1217bbb8

View File

@ -134,22 +134,17 @@ sysprof_proc_source_get_command_line (GPid pid,
static void static void
sysprof_proc_source_populate_process (SysprofProcSource *self, sysprof_proc_source_populate_process (SysprofProcSource *self,
GPid pid) GPid pid,
const gchar *cmdline)
{ {
gchar *cmdline;
g_assert (SYSPROF_IS_PROC_SOURCE (self)); g_assert (SYSPROF_IS_PROC_SOURCE (self));
g_assert (pid > 0); g_assert (pid > 0);
if (NULL != (cmdline = sysprof_proc_source_get_command_line (pid, NULL))) sysprof_capture_writer_add_process (self->writer,
{ SYSPROF_CAPTURE_CURRENT_TIME,
sysprof_capture_writer_add_process (self->writer, -1,
SYSPROF_CAPTURE_CURRENT_TIME, pid,
-1, cmdline);
pid,
cmdline);
g_free (cmdline);
}
} }
static gboolean static gboolean
@ -208,10 +203,10 @@ find_mount (GStrv mounts,
*/ */
static void static void
sysprof_proc_source_translate_path (const gchar *file, sysprof_proc_source_translate_path (const gchar *file,
GStrv mountinfo, GStrv mountinfo,
GStrv mounts, GStrv mounts,
gchar *out_file, gchar *out_file,
gsize out_file_len) gsize out_file_len)
{ {
g_autofree gchar *closest_host = NULL; g_autofree gchar *closest_host = NULL;
g_autofree gchar *closest_guest = NULL; g_autofree gchar *closest_guest = NULL;
@ -295,23 +290,24 @@ failure:
static void static void
sysprof_proc_source_populate_maps (SysprofProcSource *self, sysprof_proc_source_populate_maps (SysprofProcSource *self,
GPid pid, GPid pid,
GStrv mounts) GStrv mounts,
const gchar *mapsstr,
const gchar *mountinfostr)
{ {
g_auto(GStrv) lines = NULL; g_auto(GStrv) maps = NULL;
g_auto(GStrv) mountinfo = NULL; g_auto(GStrv) mountinfo = NULL;
guint i; guint i;
g_assert (SYSPROF_IS_PROC_SOURCE (self)); g_assert (SYSPROF_IS_PROC_SOURCE (self));
g_assert (mapsstr != NULL);
g_assert (mountinfostr != NULL);
g_assert (pid > 0); g_assert (pid > 0);
if (NULL == (lines = proc_readlines ("/proc/%d/maps", pid))) maps = g_strsplit (mapsstr, "\n", 0);
return; mountinfo = g_strsplit (mountinfostr, "\n", 0);
if (NULL == (mountinfo = proc_readlines ("/proc/%d/mountinfo", pid))) for (i = 0; maps [i] != NULL; i++)
return;
for (i = 0; lines [i] != NULL; i++)
{ {
gchar file[256]; gchar file[256];
gchar translated[256]; gchar translated[256];
@ -322,7 +318,7 @@ sysprof_proc_source_populate_maps (SysprofProcSource *self,
gulong inode; gulong inode;
gint r; gint r;
r = sscanf (lines [i], r = sscanf (maps [i],
"%lx-%lx %*15s %lx %*x:%*x %lu %255s", "%lx-%lx %*15s %lx %*x:%*x %lu %255s",
&start, &end, &offset, &inode, file); &start, &end, &offset, &inode, file);
@ -357,58 +353,114 @@ sysprof_proc_source_populate_maps (SysprofProcSource *self,
* for the path. * for the path.
*/ */
sysprof_proc_source_translate_path (file, sysprof_proc_source_translate_path (file,
mountinfo, mountinfo,
mounts, mounts,
translated, translated,
sizeof translated); sizeof translated);
fileptr = translated; fileptr = translated;
} }
sysprof_capture_writer_add_map (self->writer, sysprof_capture_writer_add_map (self->writer,
SYSPROF_CAPTURE_CURRENT_TIME, SYSPROF_CAPTURE_CURRENT_TIME,
-1, -1,
pid, pid,
start, start,
end, end,
offset, offset,
inode, inode,
fileptr); fileptr);
} }
} }
static void static gboolean
sysprof_proc_source_populate (SysprofProcSource *self) pid_is_interesting (SysprofProcSource *self,
GPid pid)
{
if (self->pids == NULL || self->pids->len == 0)
return TRUE;
for (guint i = 0; i < self->pids->len; i++)
{
if (g_array_index (self->pids, GPid, i) == pid)
return TRUE;
}
return FALSE;
}
static void
sysprof_proc_source_populate (SysprofProcSource *self,
GVariant *info)
{ {
SysprofHelpers *helpers = sysprof_helpers_get_default ();
g_auto(GStrv) mounts = NULL; g_auto(GStrv) mounts = NULL;
g_autofree gint32 *pids = NULL;
gsize n_pids = 0; gsize n_pids = 0;
g_assert (SYSPROF_IS_PROC_SOURCE (self)); g_assert (SYSPROF_IS_PROC_SOURCE (self));
g_assert (info != NULL);
g_assert (g_variant_is_of_type (info, G_VARIANT_TYPE ("aa{sv}")));
if (!(mounts = proc_readlines ("/proc/mounts"))) if (!(mounts = proc_readlines ("/proc/mounts")))
return; return;
if (self->pids->len > 0) n_pids = g_variant_n_children (info);
for (gsize i = 0; i < n_pids; i++)
{ {
for (guint i = 0; i < self->pids->len; i++) g_autoptr(GVariant) pidinfo = g_variant_get_child_value (info, i);
{ GVariantDict dict;
GPid pid = g_array_index (self->pids, GPid, i); const gchar *cmdline;
const gchar *comm;
const gchar *mountinfo;
const gchar *maps;
gint32 pid;
sysprof_proc_source_populate_process (self, pid); g_variant_dict_init (&dict, pidinfo);
sysprof_proc_source_populate_maps (self, pid, mounts);
}
return; if (!g_variant_dict_lookup (&dict, "pid", "i", &pid) ||
!pid_is_interesting (self, pid))
goto skip;
if (!g_variant_dict_lookup (&dict, "cmdline", "&s", &cmdline))
cmdline = "";
if (!g_variant_dict_lookup (&dict, "comm", "&s", &comm))
comm = "";
if (!g_variant_dict_lookup (&dict, "mountinfo", "&s", &mountinfo))
mountinfo = "";
if (!g_variant_dict_lookup (&dict, "maps", "&s", &maps))
maps = "";
sysprof_proc_source_populate_process (self, pid, *cmdline ? cmdline : comm);
sysprof_proc_source_populate_maps (self, pid, mounts, maps, mountinfo);
skip:
g_variant_dict_clear (&dict);
} }
}
if (sysprof_helpers_list_processes (helpers, NULL, &pids, &n_pids, NULL)) static void
sysprof_proc_source_get_process_info_cb (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
SysprofHelpers *helpers = (SysprofHelpers *)object;
g_autoptr(SysprofProcSource) self = user_data;
g_autoptr(GVariant) info = NULL;
g_autoptr(GError) error = NULL;
g_assert (SYSPROF_IS_HELPERS (helpers));
g_assert (G_IS_ASYNC_RESULT (result));
g_assert (SYSPROF_IS_PROC_SOURCE (self));
if (!sysprof_helpers_get_process_info_finish (helpers, result, &info, &error))
{ {
for (guint i = 0; i < n_pids; i++) sysprof_source_emit_failed (SYSPROF_SOURCE (self), error);
{ }
sysprof_proc_source_populate_process (self, pids[i]); else
sysprof_proc_source_populate_maps (self, pids[i], mounts); {
} sysprof_proc_source_populate (self, info);
sysprof_source_emit_finished (SYSPROF_SOURCE (self));
} }
} }
@ -416,12 +468,16 @@ static void
sysprof_proc_source_start (SysprofSource *source) sysprof_proc_source_start (SysprofSource *source)
{ {
SysprofProcSource *self = (SysprofProcSource *)source; SysprofProcSource *self = (SysprofProcSource *)source;
SysprofHelpers *helpers = sysprof_helpers_get_default ();
g_assert (SYSPROF_IS_PROC_SOURCE (self)); g_assert (SYSPROF_IS_PROC_SOURCE (self));
g_assert (self->writer != NULL); g_assert (self->writer != NULL);
sysprof_proc_source_populate (self); sysprof_helpers_get_process_info_async (helpers,
sysprof_source_emit_finished (source); "pid,maps,mountinfo,cmdline,comm",
NULL,
sysprof_proc_source_get_process_info_cb,
g_object_ref (self));
} }
static void static void