mirror of
https://github.com/varun-r-mallya/sysprof.git
synced 2025-12-31 20:36:25 +00:00
sysprofd: add support for unwinding without frame pointers
This provides a new sysprof-live-unwinder subprocess that runs as root to allow accessing all processes on the system via /proc/$pid/. It is spawned by sysprofd with various perf event FDs and a FD to write captures to. Ideally the capture_fd is something that will naturally error if the client application crashes (such as a socketpair() having the peer close). This is not enforced but encouraged. Additionally, an event_fd is used to allow the client application to signal the live-unwinder to exit. Unwinding is performed by looking at the modules loaded into the target pid and using libdwfl to access DWARF/CFI/etc state machinery. Stack data does not touch the disk as it exists in a mmap buffer from perf and is then translated into a callchain and sent to the Sysprof client. Unwinding occurs as normal post-mortem though is improved through the use of debuginfod to locate the appropriate symbols.
This commit is contained in:
247
src/sysprofd/ipc-unwinder-impl.c
Normal file
247
src/sysprofd/ipc-unwinder-impl.c
Normal file
@ -0,0 +1,247 @@
|
||||
/*
|
||||
* ipc-unwinder-impl.c
|
||||
*
|
||||
* Copyright 2024 Christian Hergert <chergert@redhat.com>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*/
|
||||
|
||||
#define G_LOG_DOMAIN "ipc-unwinder-impl"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#include <signal.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/socket.h>
|
||||
|
||||
#include <glib/gstdio.h>
|
||||
|
||||
#include <polkit/polkit.h>
|
||||
|
||||
#include "ipc-unwinder-impl.h"
|
||||
|
||||
struct _IpcUnwinderImpl
|
||||
{
|
||||
IpcUnwinderSkeleton parent_instance;
|
||||
};
|
||||
|
||||
static void
|
||||
child_setup (gpointer data)
|
||||
{
|
||||
prctl (PR_SET_PDEATHSIG, SIGKILL);
|
||||
}
|
||||
|
||||
static gboolean
|
||||
ipc_unwinder_impl_handle_unwind (IpcUnwinder *unwinder,
|
||||
GDBusMethodInvocation *invocation,
|
||||
GUnixFDList *fd_list,
|
||||
guint stack_size,
|
||||
GVariant *arg_perf_fds,
|
||||
GVariant *arg_event_fd)
|
||||
{
|
||||
g_autoptr(GSubprocessLauncher) launcher = NULL;
|
||||
g_autoptr(GSubprocess) subprocess = NULL;
|
||||
g_autoptr(GUnixFDList) out_fd_list = NULL;
|
||||
g_autoptr(GPtrArray) argv = NULL;
|
||||
g_autoptr(GError) error = NULL;
|
||||
g_autofd int our_fd = -1;
|
||||
g_autofd int their_fd = -1;
|
||||
g_autofd int event_fd = -1;
|
||||
GVariantIter iter;
|
||||
int capture_fd_handle;
|
||||
int pair[2];
|
||||
int next_target_fd = 3;
|
||||
int perf_fd_handle;
|
||||
int cpu;
|
||||
|
||||
g_assert (IPC_IS_UNWINDER_IMPL (unwinder));
|
||||
g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
|
||||
g_assert (!fd_list || G_IS_UNIX_FD_LIST (fd_list));
|
||||
|
||||
if (stack_size == 0 || stack_size % sysconf (_SC_PAGESIZE) != 0)
|
||||
{
|
||||
g_dbus_method_invocation_return_error_literal (g_steal_pointer (&invocation),
|
||||
G_DBUS_ERROR,
|
||||
G_DBUS_ERROR_INVALID_ARGS,
|
||||
"Stack size must be a multiple of the page size");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if (fd_list == NULL)
|
||||
{
|
||||
g_dbus_method_invocation_return_error_literal (g_steal_pointer (&invocation),
|
||||
G_DBUS_ERROR,
|
||||
G_DBUS_ERROR_FILE_NOT_FOUND,
|
||||
"Missing perf FDs");
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
launcher = g_subprocess_launcher_new (0);
|
||||
argv = g_ptr_array_new_with_free_func (g_free);
|
||||
|
||||
g_ptr_array_add (argv, g_strdup (PACKAGE_LIBEXECDIR "/sysprof-live-unwinder"));
|
||||
g_ptr_array_add (argv, g_strdup_printf ("--stack-size=%u", stack_size));
|
||||
|
||||
if (-1 == (event_fd = g_unix_fd_list_get (fd_list, g_variant_get_handle (arg_event_fd), &error)))
|
||||
{
|
||||
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
g_ptr_array_add (argv, g_strdup_printf ("--event-fd=%u", next_target_fd));
|
||||
g_subprocess_launcher_take_fd (launcher, g_steal_fd (&event_fd), next_target_fd++);
|
||||
|
||||
g_variant_iter_init (&iter, arg_perf_fds);
|
||||
|
||||
while (g_variant_iter_loop (&iter, "(hi)", &perf_fd_handle, &cpu))
|
||||
{
|
||||
g_autofd int perf_fd = g_unix_fd_list_get (fd_list, perf_fd_handle, &error);
|
||||
|
||||
if (perf_fd < 0)
|
||||
{
|
||||
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
g_ptr_array_add (argv, g_strdup_printf ("--perf-fd=%d:%d", next_target_fd, cpu));
|
||||
g_subprocess_launcher_take_fd (launcher,
|
||||
g_steal_fd (&perf_fd),
|
||||
next_target_fd++);
|
||||
}
|
||||
|
||||
g_subprocess_launcher_set_child_setup (launcher, child_setup, NULL, NULL);
|
||||
|
||||
if (socketpair (AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, pair) < 0)
|
||||
{
|
||||
int errsv = errno;
|
||||
g_dbus_method_invocation_return_error_literal (g_steal_pointer (&invocation),
|
||||
G_IO_ERROR,
|
||||
g_io_error_from_errno (errsv),
|
||||
g_strerror (errsv));
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
our_fd = g_steal_fd (&pair[0]);
|
||||
their_fd = g_steal_fd (&pair[1]);
|
||||
|
||||
out_fd_list = g_unix_fd_list_new ();
|
||||
capture_fd_handle = g_unix_fd_list_append (out_fd_list, their_fd, &error);
|
||||
|
||||
if (capture_fd_handle < 0)
|
||||
{
|
||||
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
g_ptr_array_add (argv, g_strdup_printf ("--capture-fd=%d", next_target_fd));
|
||||
g_subprocess_launcher_take_fd (launcher, g_steal_fd (&our_fd), next_target_fd++);
|
||||
|
||||
g_ptr_array_add (argv, NULL);
|
||||
|
||||
if (!(subprocess = g_subprocess_launcher_spawnv (launcher, (const char * const *)argv->pdata, &error)))
|
||||
{
|
||||
g_dbus_method_invocation_return_gerror (g_steal_pointer (&invocation), error);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
ipc_unwinder_complete_unwind (unwinder,
|
||||
g_steal_pointer (&invocation),
|
||||
out_fd_list,
|
||||
g_variant_new_handle (capture_fd_handle));
|
||||
|
||||
g_subprocess_wait_async (subprocess, NULL, NULL, NULL);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static void
|
||||
unwinder_iface_init (IpcUnwinderIface *iface)
|
||||
{
|
||||
iface->handle_unwind = ipc_unwinder_impl_handle_unwind;
|
||||
}
|
||||
|
||||
G_DEFINE_FINAL_TYPE_WITH_CODE (IpcUnwinderImpl, ipc_unwinder_impl, IPC_TYPE_UNWINDER_SKELETON,
|
||||
G_IMPLEMENT_INTERFACE (IPC_TYPE_UNWINDER, unwinder_iface_init))
|
||||
|
||||
static gboolean
|
||||
ipc_unwinder_impl_g_authorize_method (GDBusInterfaceSkeleton *skeleton,
|
||||
GDBusMethodInvocation *invocation)
|
||||
{
|
||||
PolkitAuthorizationResult *res = NULL;
|
||||
PolkitAuthority *authority = NULL;
|
||||
PolkitSubject *subject = NULL;
|
||||
const gchar *peer_name;
|
||||
gboolean ret = TRUE;
|
||||
|
||||
g_assert (IPC_IS_UNWINDER_IMPL (skeleton));
|
||||
g_assert (G_IS_DBUS_METHOD_INVOCATION (invocation));
|
||||
|
||||
peer_name = g_dbus_method_invocation_get_sender (invocation);
|
||||
|
||||
if (!(authority = polkit_authority_get_sync (NULL, NULL)) ||
|
||||
!(subject = polkit_system_bus_name_new (peer_name)) ||
|
||||
!(res = polkit_authority_check_authorization_sync (authority,
|
||||
POLKIT_SUBJECT (subject),
|
||||
"org.gnome.sysprof3.profile",
|
||||
NULL,
|
||||
POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
|
||||
NULL,
|
||||
NULL)) ||
|
||||
!polkit_authorization_result_get_is_authorized (res))
|
||||
{
|
||||
g_dbus_method_invocation_return_error (g_steal_pointer (&invocation),
|
||||
G_DBUS_ERROR,
|
||||
G_DBUS_ERROR_ACCESS_DENIED,
|
||||
"Not authorized to make request");
|
||||
ret = FALSE;
|
||||
}
|
||||
|
||||
g_clear_object (&authority);
|
||||
g_clear_object (&subject);
|
||||
g_clear_object (&res);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void
|
||||
ipc_unwinder_impl_finalize (GObject *object)
|
||||
{
|
||||
G_OBJECT_CLASS (ipc_unwinder_impl_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static void
|
||||
ipc_unwinder_impl_class_init (IpcUnwinderImplClass *klass)
|
||||
{
|
||||
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
||||
GDBusInterfaceSkeletonClass *skeleton_class = G_DBUS_INTERFACE_SKELETON_CLASS (klass);
|
||||
|
||||
object_class->finalize = ipc_unwinder_impl_finalize;
|
||||
|
||||
skeleton_class->g_authorize_method = ipc_unwinder_impl_g_authorize_method;
|
||||
}
|
||||
|
||||
static void
|
||||
ipc_unwinder_impl_init (IpcUnwinderImpl *self)
|
||||
{
|
||||
}
|
||||
|
||||
IpcUnwinder *
|
||||
ipc_unwinder_impl_new (void)
|
||||
{
|
||||
return g_object_new (IPC_TYPE_UNWINDER_IMPL, NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user