| 1 |
/* sysprof-tracefd-consumer.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/gstdio.h> |
| 24 |
|
| 25 |
#include "sysprof-instrument-private.h" |
| 26 |
#include "sysprof-recording-private.h" |
| 27 |
#include "sysprof-tracefd-consumer.h" |
| 28 |
|
| 29 |
struct _SysprofTracefdConsumer |
| 30 |
{ |
| 31 |
SysprofInstrument parent_instance; |
| 32 |
int trace_fd; |
| 33 |
}; |
| 34 |
|
| 35 |
struct _SysprofTracefdConsumerClass |
| 36 |
{ |
| 37 |
SysprofInstrumentClass parent_class; |
| 38 |
}; |
| 39 |
|
| 40 |
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SysprofCaptureReader, sysprof_capture_reader_unref) |
| 41 |
|
| 42 |
G_DEFINE_FINAL_TYPE (SysprofTracefdConsumer, sysprof_tracefd_consumer, SYSPROF_TYPE_INSTRUMENT) |
| 43 |
|
| 44 |
static DexFuture * |
| 45 |
sysprof_tracefd_consumer_augment (SysprofInstrument *instrument, |
| 46 |
SysprofRecording *recording) |
| 47 |
{ |
| 48 |
SysprofTracefdConsumer *self = (SysprofTracefdConsumer *)instrument; |
| 49 |
g_autoptr(SysprofCaptureReader) reader = NULL; |
| 50 |
SysprofCaptureWriter *writer; |
| 51 |
|
| 52 |
g_assert (SYSPROF_IS_TRACEFD_CONSUMER (self)); |
| 53 |
g_assert (SYSPROF_IS_RECORDING (recording)); |
| 54 |
|
| 55 |
if (self->trace_fd == -1) |
| 56 |
return dex_future_new_for_boolean (TRUE); |
| 57 |
|
| 58 |
writer = _sysprof_recording_writer (recording); |
| 59 |
|
| 60 |
lseek (self->trace_fd, 0, SEEK_SET); |
| 61 |
|
| 62 |
if ((reader = sysprof_capture_reader_new_from_fd (g_steal_fd (&self->trace_fd)))) |
| 63 |
sysprof_capture_writer_cat (writer, reader); |
| 64 |
|
| 65 |
return dex_future_new_for_boolean (TRUE); |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
sysprof_tracefd_consumer_finalize (GObject *object) |
| 70 |
{ |
| 71 |
SysprofTracefdConsumer *self = (SysprofTracefdConsumer *)object; |
| 72 |
|
| 73 |
g_clear_fd (&self->trace_fd, NULL); |
| 74 |
|
| 75 |
G_OBJECT_CLASS (sysprof_tracefd_consumer_parent_class)->finalize (object); |
| 76 |
} |
| 77 |
|
| 78 |
static void |
| 79 |
sysprof_tracefd_consumer_class_init (SysprofTracefdConsumerClass *klass) |
| 80 |
{ |
| 81 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 82 |
SysprofInstrumentClass *instrument_class = SYSPROF_INSTRUMENT_CLASS (klass); |
| 83 |
|
| 84 |
object_class->finalize = sysprof_tracefd_consumer_finalize; |
| 85 |
|
| 86 |
instrument_class->augment = sysprof_tracefd_consumer_augment; |
| 87 |
} |
| 88 |
|
| 89 |
static void |
| 90 |
sysprof_tracefd_consumer_init (SysprofTracefdConsumer *self) |
| 91 |
{ |
| 92 |
self->trace_fd = -1; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* sysprof_tracefd_consumer_new: |
| 97 |
* @trace_fd: a file-descriptor to read from |
| 98 |
* |
| 99 |
* This function will take ownership of @trace_fd, If you need to keep a |
| 100 |
* copy of your FD then it is suggested you use `dup()` to copy the file |
| 101 |
* descriptor. |
| 102 |
* |
| 103 |
* Returns: (transfer full): a #SysprofInstrument |
| 104 |
*/ |
| 105 |
SysprofInstrument * |
| 106 |
sysprof_tracefd_consumer_new (int trace_fd) |
| 107 |
{ |
| 108 |
SysprofTracefdConsumer *self; |
| 109 |
|
| 110 |
g_return_val_if_fail (trace_fd >= -1, NULL); |
| 111 |
|
| 112 |
self = g_object_new (SYSPROF_TYPE_TRACEFD_CONSUMER, NULL); |
| 113 |
self->trace_fd = trace_fd; |
| 114 |
|
| 115 |
return SYSPROF_INSTRUMENT (self); |
| 116 |
} |