Debian Codegraph @

src/libsysprof/sysprof-tracer.c

raw
1 /* sysprof-tracer.c
2 *
3 * Copyright 2023 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 <glib/gi18n.h>
24
25 #include "sysprof-instrument-private.h"
26 #include "sysprof-recording-private.h"
27 #include "sysprof-tracer.h"
28
29 /**
30 * SysprofTracer:
31 *
32 * The `SysprofTracer` instrument provides integration with GCC's
33 * `-finstrument-functions`. It uses an `LD_PRELOAD` on Linux to record
34 * stacktraces on `__cyg_profile_func_enter` and `__cyg_profile_func_exit`.
35 *
36 * On macOS, `profile_func_enter` and `profile_func_exit` are used.
37 */
38
39 struct _SysprofTracer
40 {
41 SysprofInstrument parent_instance;
42 };
43
44 struct _SysprofTracerClass
45 {
46 SysprofInstrumentClass parent_class;
47 };
48
49 G_DEFINE_FINAL_TYPE (SysprofTracer, sysprof_tracer, SYSPROF_TYPE_INSTRUMENT)
50
51 static DexFuture *
52 sysprof_tracer_prepare (SysprofInstrument *instrument,
53 SysprofRecording *recording)
54 {
55 SysprofSpawnable *spawnable;
56
57 g_assert (SYSPROF_IS_INSTRUMENT (instrument));
58 g_assert (SYSPROF_IS_RECORDING (recording));
59
60 if ((spawnable = _sysprof_recording_get_spawnable (recording)))
61 sysprof_spawnable_add_ld_preload (spawnable,
62 PACKAGE_LIBDIR"/libsysprof-tracer-" API_VERSION_S ".so");
63 else
64 _sysprof_recording_diagnostic (recording,
65 _("Tracer"),
66 _("Tracing requires spawning a program compiled with ‘-finstrument-functions’. Tracing will not be available."));
67
68 return dex_future_new_for_boolean (TRUE);
69 }
70
71 static void
72 sysprof_tracer_class_init (SysprofTracerClass *klass)
73 {
74 SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass);
75
76 instrument_class->prepare = sysprof_tracer_prepare;
77 }
78
79 static void
80 sysprof_tracer_init (SysprofTracer *self)
81 {
82 }
83
84 SysprofInstrument *
85 sysprof_tracer_new (void)
86 {
87 return g_object_new (SYSPROF_TYPE_TRACER, NULL);
88 }