libsysprof-capture: add frame type for tracing

This is like sample but has an "enter/exit" flag with it. This can be
useful when you want to provide tracing instead of sampling. We use a
different frame type so that we can denote that this isn't traditional
sampling, and the flag can be used to find the next exit for the current
enter for calculating durations.

The entire stack trace is provided to make things easier on tools
which may want to deal with indirect functions that were not instrumented
but can be unwound. That may allow for tooling to give the user some
insight that it's not *just* this function entering, but some functions
before it were entered too.

This also adds a SysprofTracer instrument which will preload a
libsysprof-tracer-6.so into the process providing the
__cyg_profile_func_enter() and __cyg_profile_func_leave() hooks.
This commit is contained in:
Christian Hergert
2023-06-13 12:11:55 -07:00
parent 81eafb9232
commit 3a94170b0a
17 changed files with 369 additions and 9 deletions

View File

@ -13,6 +13,7 @@ libsysprof_profile_public_sources = [
'sysprof-recording.c',
'sysprof-sampler.c',
'sysprof-spawnable.c',
'sysprof-tracer.c',
]
libsysprof_profile_private_sources = [
@ -41,6 +42,7 @@ libsysprof_profile_public_headers = [
'sysprof-recording.h',
'sysprof-sampler.h',
'sysprof-spawnable.h',
'sysprof-tracer.h',
]
if host_machine.system() == 'linux'

View File

@ -39,6 +39,7 @@ G_BEGIN_DECLS
# include "sysprof-recording.h"
# include "sysprof-sampler.h"
# include "sysprof-spawnable.h"
# include "sysprof-tracer.h"
#undef SYSPROF_PROFILE_INSIDE
G_END_DECLS

View File

@ -0,0 +1,88 @@
/* sysprof-tracer.c
*
* Copyright 2023 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
*/
#include "config.h"
#include <glib/gi18n.h>
#include "sysprof-instrument-private.h"
#include "sysprof-recording-private.h"
#include "sysprof-tracer.h"
/**
* SysprofTracer:
*
* The `SysprofTracer` instrument provides integration with GCC's
* `-finstrument-functions`. It uses an `LD_PRELOAD` on Linux to record
* stacktraces on `__cyg_profile_func_enter` and `__cyg_profile_func_exit`.
*
* On macOS, `profile_func_enter` and `profile_func_exit` are used.
*/
struct _SysprofTracer
{
SysprofInstrument parent_instance;
};
struct _SysprofTracerClass
{
SysprofInstrumentClass parent_class;
};
G_DEFINE_FINAL_TYPE (SysprofTracer, sysprof_tracer, SYSPROF_TYPE_INSTRUMENT)
static DexFuture *
sysprof_tracer_prepare (SysprofInstrument *instrument,
SysprofRecording *recording)
{
SysprofSpawnable *spawnable;
g_assert (SYSPROF_IS_INSTRUMENT (instrument));
g_assert (SYSPROF_IS_RECORDING (recording));
if ((spawnable = _sysprof_recording_get_spawnable (recording)))
sysprof_spawnable_add_ld_preload (spawnable,
PACKAGE_LIBDIR"/libsysprof-tracer-" API_VERSION_S ".so");
else
_sysprof_recording_diagnostic (recording,
_("Tracer"),
_("Tracing requires spawning a program compiled with -finstrument-functions. Tracing will not be available."));
return dex_future_new_for_boolean (TRUE);
}
static void
sysprof_tracer_class_init (SysprofTracerClass *klass)
{
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
instrument_class->prepare = sysprof_tracer_prepare;
}
static void
sysprof_tracer_init (SysprofTracer *self)
{
}
SysprofInstrument *
sysprof_tracer_new (void)
{
return g_object_new (SYSPROF_TYPE_TRACER, NULL);
}

View File

@ -0,0 +1,42 @@
/* sysprof-tracer.h
*
* Copyright 2023 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
*/
#pragma once
#include "sysprof-instrument.h"
G_BEGIN_DECLS
#define SYSPROF_TYPE_TRACER (sysprof_tracer_get_type())
#define SYSPROF_IS_TRACER(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, SYSPROF_TYPE_TRACER)
#define SYSPROF_TRACER(obj) G_TYPE_CHECK_INSTANCE_CAST(obj, SYSPROF_TYPE_TRACER, SysprofTracer)
#define SYSPROF_TRACER_CLASS(klass) G_TYPE_CHECK_CLASS_CAST(klass, SYSPROF_TYPE_TRACER, SysprofTracerClass)
typedef struct _SysprofTracer SysprofTracer;
typedef struct _SysprofTracerClass SysprofTracerClass;
SYSPROF_AVAILABLE_IN_ALL
GType sysprof_tracer_get_type (void) G_GNUC_CONST;
SYSPROF_AVAILABLE_IN_ALL
SysprofInstrument *sysprof_tracer_new (void);
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofTracer, g_object_unref)
G_END_DECLS

View File

@ -29,9 +29,11 @@ static GMainLoop *main_loop;
static char *capture_file;
static SysprofRecording *active_recording;
static gboolean memprof;
static gboolean tracer;
static const GOptionEntry entries[] = {
{ "capture", 'c', 0, G_OPTION_ARG_FILENAME, &capture_file, "The file to capture into", "CAPTURE" },
{ "memprof", 'm', 0, G_OPTION_ARG_NONE, &memprof, "Do memory allocation tracking on subprocess" },
{ "tracer", 't', 0, G_OPTION_ARG_NONE, &tracer, "Enable tracing with __cyg_profile_enter" },
{ 0 }
};
@ -172,11 +174,15 @@ main (int argc,
sysprof_profiler_add_instrument (profiler, sysprof_energy_usage_new ());
sysprof_profiler_add_instrument (profiler, sysprof_memory_usage_new ());
sysprof_profiler_add_instrument (profiler, sysprof_network_usage_new ());
sysprof_profiler_add_instrument (profiler, sysprof_sampler_new ());
if (memprof)
sysprof_profiler_add_instrument (profiler, sysprof_malloc_tracing_new ());
if (tracer)
sysprof_profiler_add_instrument (profiler, sysprof_tracer_new ());
else
sysprof_profiler_add_instrument (profiler, sysprof_sampler_new ());
for (int i = 1; i < argc; i++)
{
if (strcmp (argv[i], "--") == 0 && i+1 < argc)