Debian Codegraph @

src/libsysprof-capture/sysprof-capture-types.h

raw
1 /* sysprof-capture-types.h
2 *
3 * Copyright 2016-2019 Christian Hergert <[email protected]>
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * Subject to the terms and conditions of this license, each copyright holder
17 * and contributor hereby grants to those receiving rights under this license
18 * a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
19 * irrevocable (except for failure to satisfy the conditions of this license)
20 * patent license to make, have made, use, offer to sell, sell, import, and
21 * otherwise transfer this software, where such license applies only to those
22 * patent claims, already acquired or hereafter acquired, licensable by such
23 * copyright holder or contributor that are necessarily infringed by:
24 *
25 * (a) their Contribution(s) (the licensed copyrights of copyright holders
26 * and non-copyrightable additions of contributors, in source or binary
27 * form) alone; or
28 *
29 * (b) combination of their Contribution(s) with the work of authorship to
30 * which such Contribution(s) was added by such copyright holder or
31 * contributor, if, at the time the Contribution is added, such addition
32 * causes such combination to be necessarily infringed. The patent license
33 * shall not apply to any other combinations which include the
34 * Contribution.
35 *
36 * Except as expressly stated above, no rights or licenses from any copyright
37 * holder or contributor is granted under this license, whether expressly, by
38 * implication, estoppel or otherwise.
39 *
40 * DISCLAIMER
41 *
42 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
43 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
44 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
46 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
47 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
48 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
49 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
50 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
51 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
52 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53 *
54 * SPDX-License-Identifier: BSD-2-Clause-Patent
55 */
56
57 #pragma once
58
59 #include <assert.h>
60 #ifdef __APPLE__
61 # include <machine/endian.h>
62 #else
63 # include <endian.h>
64 #endif
65 #include <inttypes.h>
66 #include <stddef.h>
67 #include <stdint.h>
68
69 #include "sysprof-clock.h"
70 #include "sysprof-macros.h"
71
72 SYSPROF_BEGIN_DECLS
73
74 #define SYSPROF_CAPTURE_MAGIC (htole32(0xFDCA975E))
75 #define SYSPROF_CAPTURE_ALIGN (sizeof(SysprofCaptureAddress))
76
77 #if defined(_MSC_VER)
78 # define SYSPROF_ALIGNED_BEGIN(_N) __declspec(align (_N))
79 # define SYSPROF_ALIGNED_END(_N)
80 #else
81 # define SYSPROF_ALIGNED_BEGIN(_N)
82 # define SYSPROF_ALIGNED_END(_N) __attribute__((aligned ((_N))))
83 #endif
84
85 #define SYSPROF_CAPTURE_ADDRESS_FORMAT "0x%016" PRIx64
86
87 #ifndef __GI_SCANNER__
88 SYSPROF_STATIC_ASSERT (sizeof (void *) == sizeof (uintptr_t), "UINTPTR_MAX can’t be used to determine sizeof(void*) at compile time");
89 #endif
90
91 #if UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu
92 # define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE000000000000000)
93 #elif UINTPTR_MAX == 0xFFFFFFFF
94 # define SYSPROF_CAPTURE_JITMAP_MARK SYSPROF_UINT64_CONSTANT(0xE0000000)
95 #else
96 #error Unknown UINTPTR_MAX
97 #endif
98
99 #define SYSPROF_CAPTURE_CURRENT_TIME (sysprof_clock_get_current_time())
100 #define SYSPROF_CAPTURE_COUNTER_INT64 0
101 #define SYSPROF_CAPTURE_COUNTER_DOUBLE 1
102
103 typedef struct _SysprofCaptureReader SysprofCaptureReader;
104 typedef struct _SysprofCaptureWriter SysprofCaptureWriter;
105 typedef struct _SysprofCaptureCursor SysprofCaptureCursor;
106 typedef struct _SysprofCaptureCondition SysprofCaptureCondition;
107
108 typedef uint64_t SysprofCaptureAddress;
109
110 typedef struct
111 {
112 /*
113 * The number of frames indexed by SysprofCaptureFrameType
114 */
115 size_t frame_count[16];
116
117 /*
118 * Padding for future expansion.
119 */
120 size_t padding[48];
121 } SysprofCaptureStat;
122
123 typedef union
124 {
125 int64_t v64;
126 double vdbl;
127 } SysprofCaptureCounterValue;
128
129 typedef enum
130 {
131 SYSPROF_CAPTURE_FRAME_TIMESTAMP = 1,
132 SYSPROF_CAPTURE_FRAME_SAMPLE = 2,
133 SYSPROF_CAPTURE_FRAME_MAP = 3,
134 SYSPROF_CAPTURE_FRAME_PROCESS = 4,
135 SYSPROF_CAPTURE_FRAME_FORK = 5,
136 SYSPROF_CAPTURE_FRAME_EXIT = 6,
137 SYSPROF_CAPTURE_FRAME_JITMAP = 7,
138 SYSPROF_CAPTURE_FRAME_CTRDEF = 8,
139 SYSPROF_CAPTURE_FRAME_CTRSET = 9,
140 SYSPROF_CAPTURE_FRAME_MARK = 10,
141 SYSPROF_CAPTURE_FRAME_METADATA = 11,
142 SYSPROF_CAPTURE_FRAME_LOG = 12,
143 SYSPROF_CAPTURE_FRAME_FILE_CHUNK = 13,
144 SYSPROF_CAPTURE_FRAME_ALLOCATION = 14,
145 SYSPROF_CAPTURE_FRAME_OVERLAY = 15,
146 SYSPROF_CAPTURE_FRAME_TRACE = 16,
147 SYSPROF_CAPTURE_FRAME_DBUS_MESSAGE = 17,
148 } SysprofCaptureFrameType;
149
150 /* Not part of ABI */
151 #define SYSPROF_CAPTURE_FRAME_LAST 18
152
153 SYSPROF_ALIGNED_BEGIN(1)
154 typedef struct
155 {
156 uint32_t magic;
157 uint32_t version : 8;
158 uint32_t little_endian : 1;
159 uint32_t padding : 23;
160 char capture_time[64];
161 int64_t time;
162 int64_t end_time;
163 char suffix[168];
164 } SysprofCaptureFileHeader
165 SYSPROF_ALIGNED_END(1);
166
167 SYSPROF_ALIGNED_BEGIN(1)
168 typedef struct
169 {
170 uint16_t len;
171 int16_t cpu;
172 int32_t pid;
173 int64_t time;
174 uint32_t type : 8;
175 uint32_t padding1 : 24;
176 uint32_t padding2;
177 uint8_t data[0];
178 } SysprofCaptureFrame
179 SYSPROF_ALIGNED_END(1);
180
181 SYSPROF_ALIGNED_BEGIN(1)
182 typedef struct
183 {
184 SysprofCaptureFrame frame;
185 uint32_t layer : 8;
186 uint32_t padding : 24;
187 uint32_t src_len : 16;
188 uint32_t dst_len : 16;
189 char data[0];
190 } SysprofCaptureOverlay
191 SYSPROF_ALIGNED_END(1);
192
193 SYSPROF_ALIGNED_BEGIN(1)
194 typedef struct
195 {
196 SysprofCaptureFrame frame;
197 uint64_t start;
198 uint64_t end;
199 uint64_t offset;
200 uint64_t inode;
201 char filename[0];
202 } SysprofCaptureMap
203 SYSPROF_ALIGNED_END(1);
204
205 SYSPROF_ALIGNED_BEGIN(1)
206 typedef struct
207 {
208 SysprofCaptureFrame frame;
209 uint32_t n_jitmaps;
210 uint8_t data[0];
211 } SysprofCaptureJitmap
212 SYSPROF_ALIGNED_END(1);
213
214 SYSPROF_ALIGNED_BEGIN(1)
215 typedef struct
216 {
217 SysprofCaptureFrame frame;
218 char cmdline[0];
219 } SysprofCaptureProcess
220 SYSPROF_ALIGNED_END(1);
221
222 SYSPROF_ALIGNED_BEGIN(1)
223 typedef struct
224 {
225 SysprofCaptureFrame frame;
226 uint32_t n_addrs : 16;
227 uint32_t padding1 : 16;
228 int32_t tid;
229 SysprofCaptureAddress addrs[0];
230 } SysprofCaptureSample
231 SYSPROF_ALIGNED_END(1);
232
233 SYSPROF_ALIGNED_BEGIN(1)
234 typedef struct
235 {
236 SysprofCaptureFrame frame;
237 uint32_t n_addrs : 16;
238 uint32_t entering : 1;
239 uint32_t padding1 : 15;
240 int32_t tid;
241 SysprofCaptureAddress addrs[0];
242 } SysprofCaptureTrace
243 SYSPROF_ALIGNED_END(1);
244
245 SYSPROF_ALIGNED_BEGIN(1)
246 typedef struct
247 {
248 SysprofCaptureFrame frame;
249 int32_t child_pid;
250 } SysprofCaptureFork
251 SYSPROF_ALIGNED_END(1);
252
253 SYSPROF_ALIGNED_BEGIN(1)
254 typedef struct
255 {
256 SysprofCaptureFrame frame;
257 } SysprofCaptureExit
258 SYSPROF_ALIGNED_END(1);
259
260 SYSPROF_ALIGNED_BEGIN(1)
261 typedef struct
262 {
263 SysprofCaptureFrame frame;
264 } SysprofCaptureTimestamp
265 SYSPROF_ALIGNED_END(1);
266
267 SYSPROF_ALIGNED_BEGIN(1)
268 typedef struct
269 {
270 char category[32];
271 char name[32];
272 char description[52];
273 uint32_t id : 24;
274 uint32_t type : 8;
275 SysprofCaptureCounterValue value;
276 } SysprofCaptureCounter
277 SYSPROF_ALIGNED_END(1);
278
279 SYSPROF_ALIGNED_BEGIN(1)
280 typedef struct
281 {
282 SysprofCaptureFrame frame;
283 uint32_t n_counters : 16;
284 uint32_t padding1 : 16;
285 uint32_t padding2;
286 SysprofCaptureCounter counters[0];
287 } SysprofCaptureCounterDefine
288 SYSPROF_ALIGNED_END(1);
289
290 SYSPROF_ALIGNED_BEGIN(1)
291 typedef struct
292 {
293 /*
294 * 96 bytes might seem a bit odd, but the counter frame header is 32
295 * bytes. So this makes a nice 2-cacheline aligned size which is
296 * useful when the number of counters is rather small.
297 */
298 uint32_t ids[8];
299 SysprofCaptureCounterValue values[8];
300 } SysprofCaptureCounterValues
301 SYSPROF_ALIGNED_END(1);
302
303 SYSPROF_ALIGNED_BEGIN(1)
304 typedef struct
305 {
306 SysprofCaptureFrame frame;
307 uint32_t n_values : 16;
308 uint32_t padding1 : 16;
309 uint32_t padding2;
310 SysprofCaptureCounterValues values[0];
311 } SysprofCaptureCounterSet
312 SYSPROF_ALIGNED_END(1);
313
314 SYSPROF_ALIGNED_BEGIN(1)
315 typedef struct
316 {
317 SysprofCaptureFrame frame;
318 int64_t duration;
319 char group[24];
320 char name[40];
321 char message[0];
322 } SysprofCaptureMark
323 SYSPROF_ALIGNED_END(1);
324
325 SYSPROF_ALIGNED_BEGIN(1)
326 typedef struct
327 {
328 SysprofCaptureFrame frame;
329 char id[40];
330 char metadata[0];
331 } SysprofCaptureMetadata
332 SYSPROF_ALIGNED_END(1);
333
334 SYSPROF_ALIGNED_BEGIN(1)
335 typedef struct
336 {
337 SysprofCaptureFrame frame;
338 uint32_t severity : 16;
339 uint32_t padding1 : 16;
340 uint32_t padding2 : 32;
341 char domain[32];
342 char message[0];
343 } SysprofCaptureLog
344 SYSPROF_ALIGNED_END(1);
345
346 SYSPROF_ALIGNED_BEGIN(1)
347 typedef struct
348 {
349 SysprofCaptureFrame frame;
350 uint32_t is_last : 1;
351 uint32_t padding1 : 15;
352 uint32_t len : 16;
353 char path[256];
354 uint8_t data[0];
355 } SysprofCaptureFileChunk
356 SYSPROF_ALIGNED_END(1);
357
358 SYSPROF_ALIGNED_BEGIN(1)
359 typedef struct
360 {
361 SysprofCaptureFrame frame;
362 SysprofCaptureAddress alloc_addr;
363 int64_t alloc_size;
364 int32_t tid;
365 uint32_t n_addrs : 16;
366 uint32_t padding1 : 16;
367 SysprofCaptureAddress addrs[0];
368 } SysprofCaptureAllocation
369 SYSPROF_ALIGNED_END(1);
370
371 SYSPROF_ALIGNED_BEGIN(1)
372 typedef struct
373 {
374 SysprofCaptureFrame frame;
375 uint16_t bus_type : 2;
376 uint16_t flags : 14;
377 uint16_t message_len;
378 uint8_t message[0];
379 } SysprofCaptureDBusMessage
380 SYSPROF_ALIGNED_END(1);
381
382 #define SYSPROF_CAPTURE_DBUS_TYPE_SYSTEM 0
383 #define SYSPROF_CAPTURE_DBUS_TYPE_SESSION 1
384 #define SYSPROF_CAPTURE_DBUS_TYPE_A11Y 2
385 #define SYSPROF_CAPTURE_DBUS_TYPE_OTHER 3
386
387 #define SYSPROF_CAPTURE_DBUS_FLAGS_MESSAGE_TOO_LARGE (1<<0)
388
389 #ifndef __GI_SCANNER__
390 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureFileHeader) == 256, "SysprofCaptureFileHeader changed size");
391 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureFrame) == 24, "SysprofCaptureFrame changed size");
392 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureMap) == 56, "SysprofCaptureMap changed size");
393 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureJitmap) == 28, "SysprofCaptureJitmap changed size");
394 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureProcess) == 24, "SysprofCaptureProcess changed size");
395 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureSample) == 32, "SysprofCaptureSample changed size");
396 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureTrace) == 32, "SysprofCaptureTrace changed size");
397 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureFork) == 28, "SysprofCaptureFork changed size");
398 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureExit) == 24, "SysprofCaptureExit changed size");
399 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureTimestamp) == 24, "SysprofCaptureTimestamp changed size");
400 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureCounter) == 128, "SysprofCaptureCounter changed size");
401 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureCounterValues) == 96, "SysprofCaptureCounterValues changed size");
402 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureCounterDefine) == 32, "SysprofCaptureCounterDefine changed size");
403 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureCounterSet) == 32, "SysprofCaptureCounterSet changed size");
404 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureMark) == 96, "SysprofCaptureMark changed size");
405 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureMetadata) == 64, "SysprofCaptureMetadata changed size");
406 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureLog) == 64, "SysprofCaptureLog changed size");
407 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureFileChunk) == 284, "SysprofCaptureFileChunk changed size");
408 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureAllocation) == 48, "SysprofCaptureAllocation changed size");
409 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureOverlay) == 32, "SysprofCaptureOverlay changed size");
410 SYSPROF_STATIC_ASSERT (sizeof (SysprofCaptureDBusMessage) == 28, "SysprofCaptureDBusMessage changed size");
411
412 SYSPROF_STATIC_ASSERT ((offsetof (SysprofCaptureAllocation, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureAllocation.addrs is not aligned");
413 SYSPROF_STATIC_ASSERT ((offsetof (SysprofCaptureSample, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureSample.addrs is not aligned");
414 SYSPROF_STATIC_ASSERT ((offsetof (SysprofCaptureTrace, addrs) % SYSPROF_CAPTURE_ALIGN) == 0, "SysprofCaptureTrace.addrs is not aligned");
415
416 static inline int
417 sysprof_capture_address_compare (SysprofCaptureAddress a,
418 SysprofCaptureAddress b)
419 {
420 if (a < b)
421 return -1;
422 if (a > b)
423 return 1;
424 else
425 return 0;
426 }
427 #endif
428
429 /**
430 * SysprofBacktraceFunc:
431 * @addrs: (inout) (array length=n_addrs): an array to place addresses
432 * into the capture frame
433 * @n_addrs: the length of @addrs
434 * @user_data: (scope call): closure data for the callback
435 *
436 * Returns: the number of addresses filled in @addrs
437 */
438 typedef int (*SysprofBacktraceFunc) (SysprofCaptureAddress *addrs,
439 unsigned int n_addrs,
440 void *user_data);
441
442 SYSPROF_END_DECLS