Debian Codegraph @

src/libsysprof/sysprof-document-jitmap.c

raw
1 /* sysprof-document-jitmap.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-document-frame-private.h"
24 #include "sysprof-document-jitmap.h"
25
26 typedef struct _Jitmap
27 {
28 SysprofAddress address;
29 const char *name;
30 } Jitmap;
31
32 struct _SysprofDocumentJitmap
33 {
34 SysprofDocumentFrame parent_instance;
35 GArray *jitmaps;
36 guint initialized : 1;
37 };
38
39 struct _SysprofDocumentJitmapClass
40 {
41 SysprofDocumentFrameClass parent_class;
42 };
43
44 enum {
45 PROP_0,
46 PROP_SIZE,
47 N_PROPS
48 };
49
50 G_DEFINE_FINAL_TYPE (SysprofDocumentJitmap, sysprof_document_jitmap, SYSPROF_TYPE_DOCUMENT_FRAME)
51
52 static GParamSpec *properties [N_PROPS];
53
54 static void
55 sysprof_document_jitmap_get_property (GObject *object,
56 guint prop_id,
57 GValue *value,
58 GParamSpec *pspec)
59 {
60 SysprofDocumentJitmap *self = SYSPROF_DOCUMENT_JITMAP (object);
61
62 switch (prop_id)
63 {
64 case PROP_SIZE:
65 g_value_set_uint (value, sysprof_document_jitmap_get_size (self));
66 break;
67
68 default:
69 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
70 }
71 }
72
73 static void
74 sysprof_document_jitmap_class_init (SysprofDocumentJitmapClass *klass)
75 {
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
77
78 object_class->get_property = sysprof_document_jitmap_get_property;
79
80 properties [PROP_SIZE] =
81 g_param_spec_string ("size", NULL, NULL,
82 NULL,
83 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
84
85 g_object_class_install_properties (object_class, N_PROPS, properties);
86 }
87
88 static void
89 sysprof_document_jitmap_init (SysprofDocumentJitmap *self)
90 {
91 self->jitmaps = g_array_new (FALSE, FALSE, sizeof (Jitmap));
92 }
93
94 guint
95 sysprof_document_jitmap_get_size (SysprofDocumentJitmap *self)
96 {
97 const SysprofCaptureJitmap *jitmap;
98
99 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_JITMAP (self), 0);
100
101 jitmap = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureJitmap);
102
103 return SYSPROF_DOCUMENT_FRAME_UINT32 (self, jitmap->n_jitmaps);
104 }
105
106 /**
107 * sysprof_document_jitmap_get_mapping:
108 * @self: a #SysprofDocumentJitmap
109 * @position: the index of the mapping
110 * @address: (out): a location for the address
111 *
112 * Gets the @position mapping and returns it as @address and the return value
113 * of this function.
114 *
115 * Returns: (nullable): the name of the symbol, or %NULL if @position is
116 * out of bounds.
117 */
118 const char *
119 sysprof_document_jitmap_get_mapping (SysprofDocumentJitmap *self,
120 guint nth,
121 SysprofAddress *address)
122 {
123 const SysprofCaptureJitmap *frame;
124 const Jitmap *j;
125
126 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_JITMAP (self), NULL);
127 g_return_val_if_fail (address != NULL, NULL);
128
129 if G_UNLIKELY (!self->initialized)
130 {
131 const guint8 *pos;
132 const guint8 *endptr;
133
134 self->initialized = TRUE;
135
136 frame = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureJitmap);
137 endptr = SYSPROF_DOCUMENT_FRAME_ENDPTR (self);
138 pos = frame->data;
139
140 while (pos < endptr)
141 {
142 SysprofAddress addr;
143 Jitmap map;
144
145 if (pos + sizeof addr >= endptr)
146 break;
147
148 memcpy (&addr, pos, sizeof addr);
149 pos += sizeof addr;
150
151 map.address = SYSPROF_DOCUMENT_FRAME_UINT64 (self, addr);
152
153 if (!(map.name = SYSPROF_DOCUMENT_FRAME_CSTRING (self, (const char *)pos)))
154 break;
155
156 pos += strlen (map.name) + 1;
157
158 g_array_append_val (self->jitmaps, map);
159 }
160 }
161
162 if (nth >= self->jitmaps->len)
163 return NULL;
164
165 j = &g_array_index (self->jitmaps, Jitmap, nth);
166 *address = j->address;
167 return j->name;
168 }
169