| 1 |
/* sysprof-symbols-bundle.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 <errno.h> |
| 24 |
|
| 25 |
#include <glib/gi18n.h> |
| 26 |
#include <glib/gstdio.h> |
| 27 |
|
| 28 |
#include <sysprof.h> |
| 29 |
|
| 30 |
#include "sysprof-document-loader-private.h" |
| 31 |
#include "sysprof-document-private.h" |
| 32 |
|
| 33 |
#include "sysprof-debuginfod-symbolizer.h" |
| 34 |
#include "sysprof-instrument-private.h" |
| 35 |
#include "sysprof-recording-private.h" |
| 36 |
#include "sysprof-symbols-bundle.h" |
| 37 |
|
| 38 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref) |
| 39 |
|
| 40 |
struct _SysprofSymbolsBundle |
| 41 |
{ |
| 42 |
SysprofInstrument parent_instance; |
| 43 |
guint enable_debuginfod : 1; |
| 44 |
}; |
| 45 |
|
| 46 |
struct _SysprofSymbolsBundleClass |
| 47 |
{ |
| 48 |
SysprofInstrumentClass parent_class; |
| 49 |
}; |
| 50 |
|
| 51 |
G_DEFINE_FINAL_TYPE (SysprofSymbolsBundle, sysprof_symbols_bundle, SYSPROF_TYPE_INSTRUMENT) |
| 52 |
|
| 53 |
enum { |
| 54 |
PROP_0, |
| 55 |
PROP_ENABLE_DEBUGINFOD, |
| 56 |
N_PROPS |
| 57 |
}; |
| 58 |
|
| 59 |
static GParamSpec *properties[N_PROPS]; |
| 60 |
|
| 61 |
typedef struct _Augment |
| 62 |
{ |
| 63 |
SysprofRecording *recording; |
| 64 |
guint enable_debuginfod : 1; |
| 65 |
} Augment; |
| 66 |
|
| 67 |
static void |
| 68 |
augment_free (Augment *augment) |
| 69 |
{ |
| 70 |
g_clear_object (&augment->recording); |
| 71 |
g_free (augment); |
| 72 |
} |
| 73 |
|
| 74 |
static DexFuture * |
| 75 |
sysprof_symbols_bundle_augment_fiber (gpointer user_data) |
| 76 |
{ |
| 77 |
Augment *augment = user_data; |
| 78 |
SysprofRecording *recording = augment->recording; |
| 79 |
g_autoptr(SysprofDocumentLoader) loader = NULL; |
| 80 |
g_autoptr(SysprofMultiSymbolizer) multi = NULL; |
| 81 |
g_autoptr(SysprofDocument) document = NULL; |
| 82 |
g_autoptr(GBytes) bytes = NULL; |
| 83 |
g_autoptr(GError) error = NULL; |
| 84 |
g_autofd int fd = -1; |
| 85 |
|
| 86 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 87 |
|
| 88 |
if (-1 == (fd = sysprof_recording_dup_fd (recording))) |
| 89 |
return dex_future_new_for_errno (errno); |
| 90 |
g_assert (fd > -1); |
| 91 |
|
| 92 |
if (!(loader = sysprof_document_loader_new_for_fd (fd, &error))) |
| 93 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 94 |
g_assert (SYSPROF_IS_DOCUMENT_LOADER (loader)); |
| 95 |
|
| 96 |
multi = sysprof_multi_symbolizer_new (); |
| 97 |
sysprof_multi_symbolizer_take (multi, sysprof_elf_symbolizer_new ()); |
| 98 |
|
| 99 |
if (augment->enable_debuginfod) |
| 100 |
{ |
| 101 |
g_autoptr(SysprofSymbolizer) debuginfod = NULL; |
| 102 |
g_autoptr(GError) debuginfod_error = NULL; |
| 103 |
|
| 104 |
if (!(debuginfod = sysprof_debuginfod_symbolizer_new (&debuginfod_error))) |
| 105 |
g_warning ("Failed to create debuginfod symbolizer: %s", debuginfod_error->message); |
| 106 |
else |
| 107 |
sysprof_multi_symbolizer_take (multi, g_steal_pointer (&debuginfod)); |
| 108 |
} |
| 109 |
|
| 110 |
sysprof_document_loader_set_symbolizer (loader, SYSPROF_SYMBOLIZER (multi)); |
| 111 |
|
| 112 |
if (!(document = dex_await_object (_sysprof_document_loader_load (loader), &error))) |
| 113 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 114 |
g_assert (SYSPROF_IS_DOCUMENT (document)); |
| 115 |
|
| 116 |
if (!(bytes = dex_await_boxed (_sysprof_document_serialize_symbols (document), &error))) |
| 117 |
return dex_future_new_for_error (g_steal_pointer (&error)); |
| 118 |
|
| 119 |
_sysprof_recording_add_file_data (recording, |
| 120 |
"__symbols__", |
| 121 |
g_bytes_get_data (bytes, NULL), |
| 122 |
g_bytes_get_size (bytes), |
| 123 |
TRUE); |
| 124 |
|
| 125 |
return dex_future_new_for_boolean (TRUE); |
| 126 |
} |
| 127 |
|
| 128 |
static DexFuture * |
| 129 |
sysprof_symbols_bundle_augment (SysprofInstrument *instrument, |
| 130 |
SysprofRecording *recording) |
| 131 |
{ |
| 132 |
SysprofSymbolsBundle *self = (SysprofSymbolsBundle *)instrument; |
| 133 |
Augment *state; |
| 134 |
|
| 135 |
g_assert (SYSPROF_IS_SYMBOLS_BUNDLE (self)); |
| 136 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 137 |
|
| 138 |
state = g_new0 (Augment, 1); |
| 139 |
state->recording = g_object_ref (recording); |
| 140 |
state->enable_debuginfod = self->enable_debuginfod; |
| 141 |
|
| 142 |
return dex_scheduler_spawn (NULL, 0, |
| 143 |
sysprof_symbols_bundle_augment_fiber, |
| 144 |
state, |
| 145 |
(GDestroyNotify) augment_free); |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
sysprof_symbols_bundle_get_property (GObject *object, |
| 150 |
guint prop_id, |
| 151 |
GValue *value, |
| 152 |
GParamSpec *pspec) |
| 153 |
{ |
| 154 |
SysprofSymbolsBundle *self = SYSPROF_SYMBOLS_BUNDLE (object); |
| 155 |
|
| 156 |
switch (prop_id) |
| 157 |
{ |
| 158 |
case PROP_ENABLE_DEBUGINFOD: |
| 159 |
g_value_set_boolean (value, sysprof_symbols_bundle_get_enable_debuginfod (self)); |
| 160 |
break; |
| 161 |
|
| 162 |
default: |
| 163 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
static void |
| 168 |
sysprof_symbols_bundle_set_property (GObject *object, |
| 169 |
guint prop_id, |
| 170 |
const GValue *value, |
| 171 |
GParamSpec *pspec) |
| 172 |
{ |
| 173 |
SysprofSymbolsBundle *self = SYSPROF_SYMBOLS_BUNDLE (object); |
| 174 |
|
| 175 |
switch (prop_id) |
| 176 |
{ |
| 177 |
case PROP_ENABLE_DEBUGINFOD: |
| 178 |
sysprof_symbols_bundle_set_enable_debuginfod (self, g_value_get_boolean (value)); |
| 179 |
break; |
| 180 |
|
| 181 |
default: |
| 182 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
sysprof_symbols_bundle_class_init (SysprofSymbolsBundleClass *klass) |
| 188 |
{ |
| 189 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 190 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 191 |
|
| 192 |
object_class->get_property = sysprof_symbols_bundle_get_property; |
| 193 |
object_class->set_property = sysprof_symbols_bundle_set_property; |
| 194 |
|
| 195 |
instrument_class->augment = sysprof_symbols_bundle_augment; |
| 196 |
|
| 197 |
properties[PROP_ENABLE_DEBUGINFOD] = |
| 198 |
g_param_spec_boolean ("enable-debuginfod", NULL, NULL, |
| 199 |
FALSE, |
| 200 |
(G_PARAM_READWRITE | |
| 201 |
G_PARAM_EXPLICIT_NOTIFY | |
| 202 |
G_PARAM_STATIC_STRINGS)); |
| 203 |
|
| 204 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
static void |
| 209 |
sysprof_symbols_bundle_init (SysprofSymbolsBundle *self) |
| 210 |
{ |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* sysprof_symbols_bundle_new: |
| 215 |
* |
| 216 |
* Creates a new instrment that will decode symbols at the end of a recording |
| 217 |
* and attach them to the recording. |
| 218 |
* |
| 219 |
* Returns: (transfer full): a #SysprofSymbolsBundle |
| 220 |
*/ |
| 221 |
SysprofInstrument * |
| 222 |
sysprof_symbols_bundle_new (void) |
| 223 |
{ |
| 224 |
return g_object_new (SYSPROF_TYPE_SYMBOLS_BUNDLE, NULL); |
| 225 |
} |
| 226 |
|
| 227 |
gboolean |
| 228 |
sysprof_symbols_bundle_get_enable_debuginfod (SysprofSymbolsBundle *self) |
| 229 |
{ |
| 230 |
g_return_val_if_fail (SYSPROF_IS_SYMBOLS_BUNDLE (self), FALSE); |
| 231 |
|
| 232 |
return self->enable_debuginfod; |
| 233 |
} |
| 234 |
|
| 235 |
void |
| 236 |
sysprof_symbols_bundle_set_enable_debuginfod (SysprofSymbolsBundle *self, |
| 237 |
gboolean enable_debuginfod) |
| 238 |
{ |
| 239 |
g_return_if_fail (SYSPROF_IS_SYMBOLS_BUNDLE (self)); |
| 240 |
|
| 241 |
enable_debuginfod = !!enable_debuginfod; |
| 242 |
|
| 243 |
if (enable_debuginfod != self->enable_debuginfod) |
| 244 |
{ |
| 245 |
self->enable_debuginfod = enable_debuginfod; |
| 246 |
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ENABLE_DEBUGINFOD]); |
| 247 |
} |
| 248 |
} |