Debian Codegraph @

src/libsysprof/sysprof-document-mmap.c

raw
1 /*
2 * sysprof-document-mmap.c
3 *
4 * Copyright 2023 Christian Hergert <[email protected]>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GPL-3.0-or-later
20 */
21
22 #include "config.h"
23
24 #include "sysprof-document-frame-private.h"
25 #include "sysprof-document-mmap.h"
26
27 struct _SysprofDocumentMmap
28 {
29 SysprofDocumentFrame parent_instance;
30 };
31
32 struct _SysprofDocumentMmapClass
33 {
34 SysprofDocumentFrameClass parent_class;
35 };
36
37 enum {
38 PROP_0,
39 PROP_BUILD_ID,
40 PROP_END_ADDRESS,
41 PROP_FILE,
42 PROP_FILE_INODE,
43 PROP_FILE_OFFSET,
44 PROP_START_ADDRESS,
45 N_PROPS
46 };
47
48 G_DEFINE_FINAL_TYPE (SysprofDocumentMmap, sysprof_document_mmap, SYSPROF_TYPE_DOCUMENT_FRAME)
49
50 static GParamSpec *properties [N_PROPS];
51
52 static void
53 sysprof_document_mmap_get_property (GObject *object,
54 guint prop_id,
55 GValue *value,
56 GParamSpec *pspec)
57 {
58 SysprofDocumentMmap *self = SYSPROF_DOCUMENT_MMAP (object);
59
60 switch (prop_id)
61 {
62 case PROP_START_ADDRESS:
63 g_value_set_uint64 (value, sysprof_document_mmap_get_start_address (self));
64 break;
65
66 case PROP_END_ADDRESS:
67 g_value_set_uint64 (value, sysprof_document_mmap_get_end_address (self));
68 break;
69
70 case PROP_FILE:
71 g_value_set_string (value, sysprof_document_mmap_get_file (self));
72 break;
73
74 case PROP_FILE_OFFSET:
75 g_value_set_uint64 (value, sysprof_document_mmap_get_file_offset (self));
76 break;
77
78 case PROP_FILE_INODE:
79 g_value_set_uint64 (value, sysprof_document_mmap_get_file_inode (self));
80 break;
81
82 case PROP_BUILD_ID:
83 g_value_set_string (value, sysprof_document_mmap_get_build_id (self));
84 break;
85
86 default:
87 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
88 }
89 }
90
91 static void
92 sysprof_document_mmap_class_init (SysprofDocumentMmapClass *klass)
93 {
94 GObjectClass *object_class = G_OBJECT_CLASS (klass);
95
96 object_class->get_property = sysprof_document_mmap_get_property;
97
98 properties [PROP_START_ADDRESS] =
99 g_param_spec_uint64 ("start-address", NULL, NULL,
100 0, G_MAXUINT64, 0,
101 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
102
103 properties [PROP_END_ADDRESS] =
104 g_param_spec_uint64 ("end-address", NULL, NULL,
105 0, G_MAXUINT64, 0,
106 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
107
108 properties [PROP_FILE] =
109 g_param_spec_string ("file", NULL, NULL,
110 NULL,
111 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
112
113 properties [PROP_FILE_INODE] =
114 g_param_spec_uint64 ("file-inode", NULL, NULL,
115 0, G_MAXUINT64, 0,
116 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
117
118 properties [PROP_FILE_OFFSET] =
119 g_param_spec_uint64 ("file-offset", NULL, NULL,
120 0, G_MAXUINT64, 0,
121 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
122
123 properties [PROP_BUILD_ID] =
124 g_param_spec_string ("build-id", NULL, NULL,
125 NULL,
126 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
127
128 g_object_class_install_properties (object_class, N_PROPS, properties);
129 }
130
131 static void
132 sysprof_document_mmap_init (SysprofDocumentMmap *self)
133 {
134 }
135
136 guint64
137 sysprof_document_mmap_get_start_address (SysprofDocumentMmap *self)
138 {
139 const SysprofCaptureMap *mmap;
140
141 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_MMAP (self), 0);
142
143 mmap = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureMap);
144
145 return SYSPROF_DOCUMENT_FRAME_UINT64 (self, mmap->start);
146 }
147
148 guint64
149 sysprof_document_mmap_get_end_address (SysprofDocumentMmap *self)
150 {
151 const SysprofCaptureMap *mmap;
152
153 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_MMAP (self), 0);
154
155 mmap = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureMap);
156
157 return SYSPROF_DOCUMENT_FRAME_UINT64 (self, mmap->end);
158 }
159
160 guint64
161 sysprof_document_mmap_get_file_inode (SysprofDocumentMmap *self)
162 {
163 const SysprofCaptureMap *mmap;
164
165 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_MMAP (self), 0);
166
167 mmap = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureMap);
168
169 return SYSPROF_DOCUMENT_FRAME_UINT64 (self, mmap->inode);
170 }
171
172 guint64
173 sysprof_document_mmap_get_file_offset (SysprofDocumentMmap *self)
174 {
175 const SysprofCaptureMap *mmap;
176
177 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_MMAP (self), 0);
178
179 mmap = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureMap);
180
181 return SYSPROF_DOCUMENT_FRAME_UINT64 (self, mmap->offset);
182 }
183
184 const char *
185 sysprof_document_mmap_get_file (SysprofDocumentMmap *self)
186 {
187 const SysprofCaptureMap *mmap;
188
189 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_MMAP (self), NULL);
190
191 mmap = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureMap);
192
193 return SYSPROF_DOCUMENT_FRAME_CSTRING (self, mmap->filename);
194 }
195
196 const char *
197 sysprof_document_mmap_get_build_id (SysprofDocumentMmap *self)
198 {
199 const char *file;
200 const char *build_id;
201 gsize len;
202
203 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_MMAP (self), NULL);
204
205 if (!(file = sysprof_document_mmap_get_file (self)))
206 return NULL;
207
208 /* The build-id may be tacked on after the filename if after the
209 * Nil byte we get '@'. SYSPROF_DOCUMENT_FRAME_CSTRING() will check
210 * for bounds so we can feed it a position we don't know is part
211 * of our frame or not. We expect "FILE\0@BUILD_ID_IN_HEX\0".
212 */
213
214 len = strlen (file);
215
216 if (!(build_id = SYSPROF_DOCUMENT_FRAME_CSTRING (self, &file[len+1])))
217 return NULL;
218
219 if (build_id[0] != '@')
220 return NULL;
221
222 return &build_id[1];
223 }