Debian Codegraph @

src/libsysprof/sysprof-profiler.c

raw
1 /* sysprof-profiler.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 "sysprof-controlfd-instrument-private.h"
24 #include "sysprof-profiler.h"
25 #include "sysprof-recording-private.h"
26
27 #ifdef __linux__
28 # include "sysprof-linux-instrument-private.h"
29 #endif
30
31 struct _SysprofProfiler
32 {
33 GObject parent_instance;
34 GPtrArray *instruments;
35 SysprofSpawnable *spawnable;
36 guint acquire_privileges : 1;
37 };
38
39 enum {
40 PROP_0,
41 PROP_ACQUIRE_PRIVILEGES,
42 PROP_SPAWNABLE,
43 N_PROPS
44 };
45
46 static GParamSpec *properties [N_PROPS];
47
48 G_DEFINE_FINAL_TYPE (SysprofProfiler, sysprof_profiler, G_TYPE_OBJECT)
49
50 static void
51 sysprof_profiler_finalize (GObject *object)
52 {
53 SysprofProfiler *self = (SysprofProfiler *)object;
54
55 g_clear_pointer (&self->instruments, g_ptr_array_unref);
56 g_clear_object (&self->spawnable);
57
58 G_OBJECT_CLASS (sysprof_profiler_parent_class)->finalize (object);
59 }
60
61 static void
62 sysprof_profiler_get_property (GObject *object,
63 guint prop_id,
64 GValue *value,
65 GParamSpec *pspec)
66 {
67 SysprofProfiler *self = SYSPROF_PROFILER (object);
68
69 switch (prop_id)
70 {
71 case PROP_ACQUIRE_PRIVILEGES:
72 g_value_set_boolean (value, sysprof_profiler_get_acquire_privileges (self));
73 break;
74
75 case PROP_SPAWNABLE:
76 g_value_set_object (value, sysprof_profiler_get_spawnable (self));
77 break;
78
79 default:
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81 }
82 }
83
84 static void
85 sysprof_profiler_set_property (GObject *object,
86 guint prop_id,
87 const GValue *value,
88 GParamSpec *pspec)
89 {
90 SysprofProfiler *self = SYSPROF_PROFILER (object);
91
92 switch (prop_id)
93 {
94 case PROP_ACQUIRE_PRIVILEGES:
95 sysprof_profiler_set_acquire_privileges (self, g_value_get_boolean (value));
96 break;
97
98 case PROP_SPAWNABLE:
99 sysprof_profiler_set_spawnable (self, g_value_get_object (value));
100 break;
101
102 default:
103 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
104 }
105 }
106
107 static void
108 sysprof_profiler_class_init (SysprofProfilerClass *klass)
109 {
110 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111
112 object_class->finalize = sysprof_profiler_finalize;
113 object_class->get_property = sysprof_profiler_get_property;
114 object_class->set_property = sysprof_profiler_set_property;
115
116 properties[PROP_ACQUIRE_PRIVILEGES] =
117 g_param_spec_boolean ("acquire-privileges", NULL, NULL,
118 TRUE,
119 (G_PARAM_READWRITE |
120 G_PARAM_EXPLICIT_NOTIFY |
121 G_PARAM_STATIC_STRINGS));
122
123 properties [PROP_SPAWNABLE] =
124 g_param_spec_object ("spawnable", NULL, NULL,
125 SYSPROF_TYPE_SPAWNABLE,
126 (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
127
128 g_object_class_install_properties (object_class, N_PROPS, properties);
129 }
130
131 static void
132 sysprof_profiler_init (SysprofProfiler *self)
133 {
134 self->acquire_privileges = TRUE;
135 self->instruments = g_ptr_array_new_with_free_func (g_object_unref);
136
137 #ifdef __linux__
138 sysprof_profiler_add_instrument (self, _sysprof_linux_instrument_new ());
139 #endif
140
141 sysprof_profiler_add_instrument (self, _sysprof_controlfd_instrument_new ());
142 }
143
144 SysprofProfiler *
145 sysprof_profiler_new (void)
146 {
147 return g_object_new (SYSPROF_TYPE_PROFILER, NULL);
148 }
149
150 /**
151 * sysprof_profiler_add_instrument:
152 * @self: a #SysprofProfiler
153 * @instrument: (transfer full): a #SysprofInstrument
154 *
155 * Adds @instrument to @profiler.
156 *
157 * When the recording session is started, @instrument will be directed to
158 * capture data into the destination capture file.
159 */
160 void
161 sysprof_profiler_add_instrument (SysprofProfiler *self,
162 SysprofInstrument *instrument)
163 {
164 g_return_if_fail (SYSPROF_IS_PROFILER (self));
165 g_return_if_fail (SYSPROF_IS_INSTRUMENT (instrument));
166
167 g_ptr_array_add (self->instruments, instrument);
168 }
169
170 void
171 sysprof_profiler_record_async (SysprofProfiler *self,
172 SysprofCaptureWriter *writer,
173 GCancellable *cancellable,
174 GAsyncReadyCallback callback,
175 gpointer user_data)
176 {
177 g_autoptr(SysprofRecording) recording = NULL;
178 g_autoptr(GTask) task = NULL;
179
180 g_return_if_fail (SYSPROF_IS_PROFILER (self));
181 g_return_if_fail (writer != NULL);
182 g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));
183
184 task = g_task_new (self, cancellable, callback, user_data);
185 g_task_set_source_tag (task, sysprof_profiler_record_async);
186
187 recording = _sysprof_recording_new (writer,
188 self->spawnable,
189 (SysprofInstrument **)self->instruments->pdata,
190 self->instruments->len,
191 self->acquire_privileges);
192
193 g_task_return_pointer (task, g_object_ref (recording), g_object_unref);
194
195 _sysprof_recording_start (recording);
196 }
197
198 /**
199 * sysprof_profiler_record_finish:
200 * @self: a #SysprofProfiler
201 * @result: a #GAsyncResult
202 * @error: a location for a #GError
203 *
204 * Completes an asynchronous request to start recording.
205 *
206 * Returns: (transfer full): an active #SysprofRecording if successful;
207 * otherwise %NULL and @error is set.
208 */
209 SysprofRecording *
210 sysprof_profiler_record_finish (SysprofProfiler *self,
211 GAsyncResult *result,
212 GError **error)
213 {
214 g_return_val_if_fail (SYSPROF_IS_PROFILER (self), NULL);
215 g_return_val_if_fail (G_IS_TASK (result), NULL);
216 g_return_val_if_fail (g_task_is_valid (result, self), NULL);
217
218 return g_task_propagate_pointer (G_TASK (result), error);
219 }
220
221 /**
222 * sysprof_profiler_get_spawnable:
223 * @self: a #SysprofProfiler
224 *
225 * Gets the #SysprofProfiler:spawnable property.
226 *
227 * Returns: (nullable) (transfer none): a #SysprofSpawnable or %NULL
228 */
229 SysprofSpawnable *
230 sysprof_profiler_get_spawnable (SysprofProfiler *self)
231 {
232 g_return_val_if_fail (SYSPROF_IS_PROFILER (self), NULL);
233
234 return self->spawnable;
235 }
236
237 void
238 sysprof_profiler_set_spawnable (SysprofProfiler *self,
239 SysprofSpawnable *spawnable)
240 {
241 g_return_if_fail (SYSPROF_IS_PROFILER (self));
242 g_return_if_fail (!spawnable || SYSPROF_IS_SPAWNABLE (spawnable));
243
244 if (g_set_object (&self->spawnable, spawnable))
245 g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_SPAWNABLE]);
246 }
247
248 gboolean
249 sysprof_profiler_get_acquire_privileges (SysprofProfiler *self)
250 {
251 g_return_val_if_fail (SYSPROF_IS_PROFILER (self), FALSE);
252
253 return self->acquire_privileges;
254 }
255
256 /**
257 * sysprof_profiler_set_acquire_privileges:
258 * @self: a [[email protected]]
259 *
260 * Sets if the the profiler should automatically acquire privileges using
261 * D-Bus, Policy-Kit, and sysprofd.
262 *
263 * Set to false if you want to profile only from what the current process
264 * can see. Generally that means your user needs perf capabilities.
265 */
266 void
267 sysprof_profiler_set_acquire_privileges (SysprofProfiler *self,
268 gboolean acquire_privileges)
269 {
270 g_return_if_fail (SYSPROF_IS_PROFILER (self));
271
272 acquire_privileges = !!acquire_privileges;
273
274 if (self->acquire_privileges != acquire_privileges)
275 {
276 self->acquire_privileges = acquire_privileges;
277 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACQUIRE_PRIVILEGES]);
278 }
279 }