| 1 |
/* sysprof-document-allocation.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-allocation.h" |
| 28 |
#include "sysprof-document-traceable.h" |
| 29 |
|
| 30 |
struct _SysprofDocumentAllocation |
| 31 |
{ |
| 32 |
SysprofDocumentFrame parent_instance; |
| 33 |
}; |
| 34 |
|
| 35 |
struct _SysprofDocumentAllocationClass |
| 36 |
{ |
| 37 |
SysprofDocumentFrameClass parent_class; |
| 38 |
}; |
| 39 |
|
| 40 |
enum { |
| 41 |
PROP_0, |
| 42 |
PROP_ADDRESS, |
| 43 |
PROP_IS_FREE, |
| 44 |
PROP_SIZE, |
| 45 |
PROP_STACK_DEPTH, |
| 46 |
PROP_THREAD_ID, |
| 47 |
N_PROPS |
| 48 |
}; |
| 49 |
|
| 50 |
static guint |
| 51 |
sysprof_document_allocation_get_stack_depth (SysprofDocumentTraceable *traceable) |
| 52 |
{ |
| 53 |
const SysprofCaptureAllocation *allocation = SYSPROF_DOCUMENT_FRAME_GET (traceable, SysprofCaptureAllocation); |
| 54 |
|
| 55 |
return SYSPROF_DOCUMENT_FRAME_UINT16 (traceable, allocation->n_addrs); |
| 56 |
} |
| 57 |
|
| 58 |
static guint64 |
| 59 |
sysprof_document_allocation_get_stack_address (SysprofDocumentTraceable *traceable, |
| 60 |
guint position) |
| 61 |
{ |
| 62 |
const SysprofCaptureAllocation *allocation = SYSPROF_DOCUMENT_FRAME_GET (traceable, SysprofCaptureAllocation); |
| 63 |
|
| 64 |
return SYSPROF_DOCUMENT_FRAME_UINT16 (traceable, allocation->addrs[position]); |
| 65 |
} |
| 66 |
|
| 67 |
static guint |
| 68 |
sysprof_document_allocation_get_stack_addresses (SysprofDocumentTraceable *traceable, |
| 69 |
guint64 *addresses, |
| 70 |
guint n_addresses) |
| 71 |
{ |
| 72 |
const SysprofCaptureAllocation *allocation = SYSPROF_DOCUMENT_FRAME_GET (traceable, SysprofCaptureAllocation); |
| 73 |
guint depth = MIN (n_addresses, SYSPROF_DOCUMENT_FRAME_UINT16 (traceable, allocation->n_addrs)); |
| 74 |
|
| 75 |
for (guint i = 0; i < depth; i++) |
| 76 |
addresses[i] = SYSPROF_DOCUMENT_FRAME_UINT64 (traceable, allocation->addrs[i]); |
| 77 |
|
| 78 |
return depth; |
| 79 |
} |
| 80 |
|
| 81 |
static int |
| 82 |
sysprof_document_allocation_get_thread_id (SysprofDocumentTraceable *traceable) |
| 83 |
{ |
| 84 |
SysprofDocumentAllocation *self = (SysprofDocumentAllocation *)traceable; |
| 85 |
const SysprofCaptureAllocation *allocation; |
| 86 |
|
| 87 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_ALLOCATION (self), 0); |
| 88 |
|
| 89 |
allocation = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureAllocation); |
| 90 |
|
| 91 |
return SYSPROF_DOCUMENT_FRAME_INT32 (self, allocation->tid); |
| 92 |
} |
| 93 |
|
| 94 |
static void |
| 95 |
traceable_iface_init (SysprofDocumentTraceableInterface *iface) |
| 96 |
{ |
| 97 |
iface->get_stack_depth = sysprof_document_allocation_get_stack_depth; |
| 98 |
iface->get_stack_address = sysprof_document_allocation_get_stack_address; |
| 99 |
iface->get_stack_addresses = sysprof_document_allocation_get_stack_addresses; |
| 100 |
iface->get_thread_id = sysprof_document_allocation_get_thread_id; |
| 101 |
} |
| 102 |
|
| 103 |
G_DEFINE_FINAL_TYPE_WITH_CODE (SysprofDocumentAllocation, sysprof_document_allocation, SYSPROF_TYPE_DOCUMENT_FRAME, |
| 104 |
G_IMPLEMENT_INTERFACE (SYSPROF_TYPE_DOCUMENT_TRACEABLE, traceable_iface_init)) |
| 105 |
|
| 106 |
static GParamSpec *properties [N_PROPS]; |
| 107 |
|
| 108 |
static void |
| 109 |
sysprof_document_allocation_get_property (GObject *object, |
| 110 |
guint prop_id, |
| 111 |
GValue *value, |
| 112 |
GParamSpec *pspec) |
| 113 |
{ |
| 114 |
SysprofDocumentAllocation *self = SYSPROF_DOCUMENT_ALLOCATION (object); |
| 115 |
|
| 116 |
switch (prop_id) |
| 117 |
{ |
| 118 |
case PROP_ADDRESS: |
| 119 |
g_value_set_uint64 (value, sysprof_document_allocation_get_address (self)); |
| 120 |
break; |
| 121 |
|
| 122 |
case PROP_IS_FREE: |
| 123 |
g_value_set_boolean (value, sysprof_document_allocation_is_free (self)); |
| 124 |
break; |
| 125 |
|
| 126 |
case PROP_SIZE: |
| 127 |
g_value_set_int64 (value, sysprof_document_allocation_get_size (self)); |
| 128 |
break; |
| 129 |
|
| 130 |
case PROP_STACK_DEPTH: |
| 131 |
g_value_set_uint (value, sysprof_document_traceable_get_stack_depth (SYSPROF_DOCUMENT_TRACEABLE (self))); |
| 132 |
break; |
| 133 |
|
| 134 |
case PROP_THREAD_ID: |
| 135 |
g_value_set_int (value, sysprof_document_traceable_get_thread_id (SYSPROF_DOCUMENT_TRACEABLE (self))); |
| 136 |
break; |
| 137 |
|
| 138 |
default: |
| 139 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
static void |
| 144 |
sysprof_document_allocation_class_init (SysprofDocumentAllocationClass *klass) |
| 145 |
{ |
| 146 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 147 |
SysprofDocumentFrameClass *document_frame_class = SYSPROF_DOCUMENT_FRAME_CLASS (klass); |
| 148 |
|
| 149 |
object_class->get_property = sysprof_document_allocation_get_property; |
| 150 |
|
| 151 |
document_frame_class->type_name = N_("Allocation"); |
| 152 |
|
| 153 |
/** |
| 154 |
* SysprofDocumentAllocation:thread-id: |
| 155 |
* |
| 156 |
* The thread-id where the stack was traced. |
| 157 |
* |
| 158 |
* On Linux, this is generally set to the value of `gettid()`. |
| 159 |
* |
| 160 |
* Since: 45 |
| 161 |
*/ |
| 162 |
properties [PROP_THREAD_ID] = |
| 163 |
g_param_spec_int ("thread-id", NULL, NULL, |
| 164 |
-1, G_MAXINT32, 0, |
| 165 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 166 |
|
| 167 |
/** |
| 168 |
* SysprofDocumentAllocation:address: |
| 169 |
* |
| 170 |
* The address that was allocated or freed. |
| 171 |
* |
| 172 |
* Since: 45 |
| 173 |
*/ |
| 174 |
properties [PROP_ADDRESS] = |
| 175 |
g_param_spec_uint64 ("address", NULL, NULL, |
| 176 |
0, G_MAXUINT64, 0, |
| 177 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 178 |
|
| 179 |
/** |
| 180 |
* SysprofDocumentAllocation:size: |
| 181 |
* |
| 182 |
* The size of the memory that was allocated or freed. |
| 183 |
* |
| 184 |
* Since: 45 |
| 185 |
*/ |
| 186 |
properties [PROP_SIZE] = |
| 187 |
g_param_spec_int64 ("size", NULL, NULL, |
| 188 |
G_MININT64, G_MAXINT64, 0, |
| 189 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 190 |
|
| 191 |
/** |
| 192 |
* SysprofDocumentAllocation:is-free: |
| 193 |
* |
| 194 |
* If this allocation record is a call to release |
| 195 |
* #SysprofDocumentAllocation:address. |
| 196 |
* |
| 197 |
* Since: 45 |
| 198 |
*/ |
| 199 |
properties [PROP_IS_FREE] = |
| 200 |
g_param_spec_boolean ("is-free", NULL, NULL, |
| 201 |
FALSE, |
| 202 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 203 |
|
| 204 |
/** |
| 205 |
* SysprofDocumentAllocation:stack-depth: |
| 206 |
* |
| 207 |
* The depth of the stack trace. |
| 208 |
* |
| 209 |
* Since: 45 |
| 210 |
*/ |
| 211 |
properties [PROP_STACK_DEPTH] = |
| 212 |
g_param_spec_uint ("stack-depth", NULL, NULL, |
| 213 |
0, G_MAXUINT16, 0, |
| 214 |
(G_PARAM_READABLE | G_PARAM_STATIC_STRINGS)); |
| 215 |
|
| 216 |
g_object_class_install_properties (object_class, N_PROPS, properties); |
| 217 |
} |
| 218 |
|
| 219 |
static void |
| 220 |
sysprof_document_allocation_init (SysprofDocumentAllocation *self) |
| 221 |
{ |
| 222 |
} |
| 223 |
|
| 224 |
guint64 |
| 225 |
sysprof_document_allocation_get_address (SysprofDocumentAllocation *self) |
| 226 |
{ |
| 227 |
const SysprofCaptureAllocation *allocation; |
| 228 |
|
| 229 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_ALLOCATION (self), 0); |
| 230 |
|
| 231 |
allocation = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureAllocation); |
| 232 |
|
| 233 |
return SYSPROF_DOCUMENT_FRAME_UINT64 (self, allocation->alloc_addr); |
| 234 |
} |
| 235 |
|
| 236 |
gint64 |
| 237 |
sysprof_document_allocation_get_size (SysprofDocumentAllocation *self) |
| 238 |
{ |
| 239 |
const SysprofCaptureAllocation *allocation; |
| 240 |
|
| 241 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_ALLOCATION (self), 0); |
| 242 |
|
| 243 |
allocation = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureAllocation); |
| 244 |
|
| 245 |
return SYSPROF_DOCUMENT_FRAME_INT64 (self, allocation->alloc_size); |
| 246 |
} |
| 247 |
|
| 248 |
gboolean |
| 249 |
sysprof_document_allocation_is_free (SysprofDocumentAllocation *self) |
| 250 |
{ |
| 251 |
const SysprofCaptureAllocation *allocation; |
| 252 |
|
| 253 |
g_return_val_if_fail (SYSPROF_IS_DOCUMENT_ALLOCATION (self), 0); |
| 254 |
|
| 255 |
allocation = SYSPROF_DOCUMENT_FRAME_GET (self, SysprofCaptureAllocation); |
| 256 |
|
| 257 |
return allocation->alloc_size == 0; |
| 258 |
} |