Debian Codegraph @

src/libsysprof/sysprof-document-sample.c

raw
1 /* sysprof-document-sample.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-document-frame-private.h"
26
27 #include "sysprof-document-sample.h"
28 #include "sysprof-document-traceable.h"
29
30 struct _SysprofDocumentSample
31 {
32 SysprofDocumentFrame parent_instance;
33 };
34
35 struct _SysprofDocumentSampleClass
36 {
37 SysprofDocumentFrameClass parent_class;
38 };
39
40 enum {
41 PROP_0,
42 PROP_STACK_DEPTH,
43 PROP_THREAD_ID,
44 N_PROPS
45 };
46
47 static guint
48 sysprof_document_sample_get_stack_depth (SysprofDocumentTraceable *traceable)
49 {
50 const SysprofCaptureSample *sample = SYSPROF_DOCUMENT_FRAME_GET (traceable, SysprofCaptureSample);
51
52 return SYSPROF_DOCUMENT_FRAME_UINT16 (traceable, sample->n_addrs);
53 }
54
55 static guint64
56 sysprof_document_sample_get_stack_address (SysprofDocumentTraceable *traceable,
57 guint position)
58 {
59 const SysprofCaptureSample *sample = SYSPROF_DOCUMENT_FRAME_GET (traceable, SysprofCaptureSample);
60
61 return SYSPROF_DOCUMENT_FRAME_UINT16 (traceable, sample->addrs[position]);
62 }
63
64 static guint
65 sysprof_document_sample_get_stack_addresses (SysprofDocumentTraceable *traceable,
66 guint64 *addresses,
67 guint n_addresses)
68 {
69 const SysprofCaptureSample *sample = SYSPROF_DOCUMENT_FRAME_GET (traceable, SysprofCaptureSample);
70 guint depth = MIN (n_addresses, SYSPROF_DOCUMENT_FRAME_UINT16 (traceable, sample->n_addrs));
71
72 for (guint i = 0; i < depth; i++)
73 addresses[i] = SYSPROF_DOCUMENT_FRAME_UINT64 (traceable, sample->addrs[i]);
74
75 return depth;
76 }
77
78 static int
79 sysprof_document_sample_get_thread_id (SysprofDocumentTraceable *traceable)
80 {
81 SysprofDocumentSample *self = (SysprofDocumentSample *)traceable;
82 const SysprofCaptureSample *sample;
83
84 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_SAMPLE (self), -1);
85
86 sample = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureSample);
87
88 return SYSPROF_DOCUMENT_FRAME_INT32 (self, sample->tid);
89 }
90
91 static void
92 traceable_iface_init (SysprofDocumentTraceableInterface *iface)
93 {
94 iface->get_stack_depth = sysprof_document_sample_get_stack_depth;
95 iface->get_stack_address = sysprof_document_sample_get_stack_address;
96 iface->get_stack_addresses = sysprof_document_sample_get_stack_addresses;
97 iface->get_thread_id = sysprof_document_sample_get_thread_id;
98 }
99
100 G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentSample, sysprof_document_sample, SYSPROF_TYPE_DOCUMENT_FRAME,
101 G_IMPLEMENT_INTERFACE (SYSPROF_TYPE_DOCUMENT_TRACEABLE, traceable_iface_init))
102
103 static GParamSpec *properties [N_PROPS];
104
105 static void
106 sysprof_document_sample_get_property (GObject *object,
107 guint prop_id,
108 GValue *value,
109 GParamSpec *pspec)
110 {
111 SysprofDocumentSample *self = SYSPROF_DOCUMENT_SAMPLE (object);
112
113 switch (prop_id)
114 {
115 case PROP_STACK_DEPTH:
116 g_value_set_uint (value, sysprof_document_traceable_get_stack_depth (SYSPROF_DOCUMENT_TRACEABLE (self)));
117 break;
118
119 case PROP_THREAD_ID:
120 g_value_set_int (value, sysprof_document_sample_get_thread_id (SYSPROF_DOCUMENT_TRACEABLE (self)));
121 break;
122
123 default:
124 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
125 }
126 }
127
128 static void
129 sysprof_document_sample_class_init (SysprofDocumentSampleClass *klass)
130 {
131 GObjectClass *object_class = G_OBJECT_CLASS (klass);
132 SysprofDocumentFrameClass *document_frame_class = SYSPROF_DOCUMENT_FRAME_CLASS (klass);
133
134 object_class->get_property = sysprof_document_sample_get_property;
135
136 document_frame_class->type_name = N_("Sample");
137
138 /**
139 * SysprofDocumentSample:thread-id:
140 *
141 * The thread-id where the sample occurred.
142 *
143 * On Linux, this is generally set to the value of gettid().
144 *
145 * Since: 45
146 */
147 properties [PROP_THREAD_ID] =
148 g_param_spec_int ("thread-id", NULL, NULL,
149 -1, G_MAXINT32, 0,
150 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
151
152 /**
153 * SysprofDocumentSample:stack-depth:
154 *
155 * The depth of the stack trace.
156 *
157 * Since: 45
158 */
159 properties [PROP_STACK_DEPTH] =
160 g_param_spec_uint ("stack-depth", NULL, NULL,
161 0, G_MAXUINT32, 0,
162 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
163
164 g_object_class_install_properties (object_class, N_PROPS, properties);
165 }
166
167 static void
168 sysprof_document_sample_init (SysprofDocumentSample *self)
169 {
170 }