Debian Codegraph @

libsframe/doc/sframe-spec.info

raw
1 This is sframe-spec.info, produced by makeinfo version 7.1 from
2 sframe-spec.texi.
3
4 Copyright © 2021-2025 Free Software Foundation, Inc.
5
6 Permission is granted to copy, distribute and/or modify this document
7 under the terms of the GNU General Public License, Version 3 or any
8 later version published by the Free Software Foundation. A copy of the
9 license is included in the section entitled "GNU General Public
10 License".
11
12 INFO-DIR-SECTION Software development
13 START-INFO-DIR-ENTRY
14 * SFrame: (sframe-spec). The Simple Frame format.
15 END-INFO-DIR-ENTRY
16
17 
18 File: sframe-spec.info, Node: Top, Next: Introduction, Up: (dir)
19
20 The SFrame format
21 *****************
22
23 This manual describes version 2 of the SFrame file format. SFrame
24 stands for Simple Frame. The SFrame format keeps track of the minimal
25 necessary information needed for generating stack traces:
26
27 − Canonical Frame Address (CFA).
28 − Frame Pointer (FP).
29 − Return Address (RA).
30
31 The reason for existence of the SFrame format is to provide a simple,
32 fast and low-overhead mechanism to generate stack traces.
33
34 * Menu:
35
36 * Introduction::
37 * SFrame Section::
38 * ABI/arch-specific Definition::
39
40 Appendices
41 * Generating Stack Traces using SFrame::
42
43 * Index::
44
45 
46 File: sframe-spec.info, Node: Introduction, Next: SFrame Section, Prev: Top, Up: Top
47
48 1 Introduction
49 **************
50
51 * Menu:
52
53 * Overview::
54 * Changes from Version 1 to Version 2::
55
56 
57 File: sframe-spec.info, Node: Overview, Next: Changes from Version 1 to Version 2, Up: Introduction
58
59 1.1 Overview
60 ============
61
62 The SFrame stack trace information is provided in a loaded section,
63 known as the ‘.sframe’ section. When available, the ‘.sframe’ section
64 appears in a new segment of its own, PT_GNU_SFRAME.
65
66 The SFrame format is currently supported only for select ABIs,
67 namely, AMD64 and AAPCS64.
68
69 A portion of the SFrame format follows an unaligned on-disk
70 representation. Some data structures, however, (namely the SFrame
71 header and the SFrame function descriptor entry) have elements at their
72 natural boundaries. All data structures are packed, unless otherwise
73 stated.
74
75 The contents of the SFrame section are stored in the target
76 endianness, i.e., in the endianness of the system on which the section
77 is targeted to be used. An SFrame section reader may use the magic
78 number in the SFrame header to identify the endianness of the SFrame
79 section.
80
81 Addresses in this specification are expressed in bytes.
82
83 The rest of this specification describes the current version of the
84 format, ‘SFRAME_VERSION_2’, in detail. Additional sections outline the
85 major changes made to each previously published version of the SFrame
86 stack trace format.
87
88 The associated API to decode, probe and encode the SFrame section,
89 provided via ‘libsframe’, is not accompanied here at this time. This
90 will be added later.
91
92 This document is intended to be in sync with the C code in
93 ‘sframe.h’. Please report discrepancies between the two, if any.
94
95 
96 File: sframe-spec.info, Node: Changes from Version 1 to Version 2, Prev: Overview, Up: Introduction
97
98 1.2 Changes from Version 1 to Version 2
99 =======================================
100
101 The following is a list of the changes made to the SFrame stack trace
102 format since Version 1 was published.
103
104 • Add an unsigned 8-bit integral field to the SFrame function
105 descriptor entry to encode the size of the repetitive code blocks.
106 Such code blocks, e.g, pltN entries, use an SFrame function
107 descriptor entry of type SFRAME_FDE_TYPE_PCMASK.
108 • Add an unsigned 16-bit integral field to the SFrame function
109 descriptor entry to serve as padding. This helps ensure natural
110 alignment for the members of the data structure.
111 • The above two imply that each SFrame function descriptor entry has
112 a fixed size of 20 bytes instead of its size of 17 bytes in SFrame
113 format version 1.
114
115 SFrame version 1 is now obsolete and should not be used.
116
117 
118 File: sframe-spec.info, Node: SFrame Section, Next: ABI/arch-specific Definition, Prev: Introduction, Up: Top
119
120 2 SFrame Section
121 ****************
122
123 The SFrame section consists of an SFrame header, starting with a
124 preamble, and two other sub-sections, namely the SFrame function
125 descriptor entry (SFrame FDE) sub-section, and the SFrame frame row
126 entry (SFrame FRE) sub-section.
127
128 * Menu:
129
130 * SFrame Preamble::
131 * SFrame Header::
132 * SFrame Function Descriptor Entries::
133 * SFrame Frame Row Entries::
134
135 
136 File: sframe-spec.info, Node: SFrame Preamble, Next: SFrame Header, Up: SFrame Section
137
138 2.1 SFrame Preamble
139 ===================
140
141 The preamble is a 32-bit packed structure; the only part of the SFrame
142 section whose format cannot vary between versions.
143
144 typedef struct sframe_preamble
145 {
146 uint16_t sfp_magic;
147 uint8_t sfp_version;
148 uint8_t sfp_flags;
149 } ATTRIBUTE_PACKED sframe_preamble;
150
151 Every element of the SFrame preamble is naturally aligned.
152
153 All values are stored in the endianness of the target system for
154 which the SFrame section is intended. Further details:
155
156 Offset Type Name Description
157 -------------------------------------------------------------------------------------
158 0x00 ‘uint16_t’ ‘sfp_magic’ The magic number for SFrame section: 0xdee2.
159 Defined as a macro ‘SFRAME_MAGIC’.
160
161 0x02 ‘uint8_t’ ‘sfp_version’ The version number of this SFrame section.
162 *Note SFrame Version::, for the set of valid
163 values. Current version is
164 ‘SFRAME_VERSION_2’.
165
166 0x03 ‘uint8_t’ ‘sfp_flags’ Flags (section-wide) for this SFrame
167 section. *Note SFrame Flags::, for the set
168 of valid values.
169
170 * Menu:
171
172 * SFrame Magic Number and Endianness::
173 * SFrame Version::
174 * SFrame Flags::
175
176 
177 File: sframe-spec.info, Node: SFrame Magic Number and Endianness, Next: SFrame Version, Up: SFrame Preamble
178
179 2.1.1 SFrame Magic Number and Endianness
180 ----------------------------------------
181
182 SFrame sections are stored in the target endianness of the system that
183 consumes them. A consumer library reading or writing SFrame sections
184 should detect foreign-endianness by inspecting the SFrame magic number
185 in the ‘sfp_magic’ field in the SFrame header. It may then provide
186 means to endian-flip the SFrame section as necessary.
187
188 
189 File: sframe-spec.info, Node: SFrame Version, Next: SFrame Flags, Prev: SFrame Magic Number and Endianness, Up: SFrame Preamble
190
191 2.1.2 SFrame Version
192 --------------------
193
194 The version of the SFrame format can be determined by inspecting
195 ‘sfp_version’. The following versions are currently valid:
196
197 Version Name Number Description
198 ------------------------------------------------------------------
199 ‘SFRAME_VERSION_1’ 1 First version, obsolete.
200 ‘SFRAME_VERSION_2’ 2 Current version, under development.
201
202 This document describes ‘SFRAME_VERSION_2’.
203
204 
205 File: sframe-spec.info, Node: SFrame Flags, Prev: SFrame Version, Up: SFrame Preamble
206
207 2.1.3 SFrame Flags
208 ------------------
209
210 The preamble contains bitflags in its ‘sfp_flags’ field that describe
211 various section-wide properties.
212
213 The following flags are currently defined.
214
215 Flag Versions Value Meaning
216 ----------------------------------------------------------------------------
217 ‘SFRAME_F_FDE_SORTED’ All 0x1 Function Descriptor Entries
218 are sorted on PC.
219 ‘SFRAME_F_FRAME_POINTER’ All 0x2 All functions in the object
220 file preserve frame
221 pointer.
222
223 The purpose of SFRAME_F_FRAME_POINTER flag is to facilitate stack
224 tracers to reliably fallback on the frame pointer based stack tracing
225 method, if SFrame information is not present for some function in the
226 SFrame section.
227
228 Further flags may be added in future.
229
230 
231 File: sframe-spec.info, Node: SFrame Header, Next: SFrame Function Descriptor Entries, Prev: SFrame Preamble, Up: SFrame Section
232
233 2.2 SFrame Header
234 =================
235
236 The SFrame header is the first part of an SFrame section. It begins
237 with the SFrame preamble. All parts of it other than the preamble
238 (*note SFrame Preamble::) can vary between SFrame file versions. It
239 contains things that apply to the section as a whole, and offsets to the
240 various other sub-sections defined in the format. As with the rest of
241 the SFrame section, all values are stored in the endianness of the
242 target system.
243
244 The two sub-sections tile the SFrame section: each section runs from
245 the offset given until the start of the next section. An explicit
246 length is given for the last sub-section, the SFrame Frame Row Entry
247 (SFrame FRE) sub-section.
248
249 typedef struct sframe_header
250 {
251 sframe_preamble sfh_preamble;
252 uint8_t sfh_abi_arch;
253 int8_t sfh_cfa_fixed_fp_offset;
254 int8_t sfh_cfa_fixed_ra_offset;
255 uint8_t sfh_auxhdr_len;
256 uint32_t sfh_num_fdes;
257 uint32_t sfh_num_fres;
258 uint32_t sfh_fre_len;
259 uint32_t sfh_fdeoff;
260 uint32_t sfh_freoff;
261 } ATTRIBUTE_PACKED sframe_header;
262
263 Every element of the SFrame header is naturally aligned.
264
265 The sub-section offsets, namely ‘sfh_fdeoff’ and ‘sfh_freoff’, in the
266 SFrame header are relative to the _end_ of the SFrame header; they are
267 each an offset in bytes into the SFrame section where the SFrame FDE
268 sub-section and the SFrame FRE sub-section respectively start.
269
270 The SFrame section contains ‘sfh_num_fdes’ number of fixed-length
271 array elements in the SFrame FDE sub-section. Each array element is of
272 type SFrame function descriptor entry; each providing a high-level
273 function description for the purpose of stack tracing. More details in
274 a subsequent section. *Note SFrame Function Descriptor Entries::.
275
276 Next, the SFrame FRE sub-section, starting at offset ‘sfh_fre_off’,
277 describes the stack trace information for each function, using a total
278 of ‘sfh_num_fres’ number of variable-length array elements. Each array
279 element is of type SFrame frame row entry. *Note SFrame Frame Row
280 Entries::.
281
282 SFrame header allows specifying explicitly the fixed offsets from
283 CFA, if any, from which FP or RA may be recovered. For example, in
284 AMD64, the stack offset of the return address is ‘CFA - 8’. Since these
285 offsets are expected to be in close vicinity to the CFA in most ABIs,
286 ‘sfh_cfa_fixed_fp_offset’ and ‘sfh_cfa_fixed_ra_offset’ are limited to
287 signed 8-bit integers.
288
289 The SFrame format has made some provisions for supporting more
290 ABIs/architectures in the future. One of them is the concept of the
291 auxiliary SFrame header. Bytes in the auxiliary SFrame header may be
292 used to convey further ABI-specific information. The ‘sframe_header’
293 structure provides an unsigned 8-bit integral field to denote the size
294 (in bytes) of an auxiliary SFrame header. The auxiliary SFrame header
295 follows right after the ‘sframe_header’ structure. As for the
296 calculation of the sub-section offsets, namely ‘sfh_fdeoff’ and
297 ‘sfh_freoff’, the _end_ of SFrame header must be the end of the
298 auxiliary SFrame header, if the latter is present.
299
300 Putting it all together:
301
302 Offset Type Name Description
303 -------------------------------------------------------------------------------------
304 0x00 ‘sframe_ ‘sfh_preamble’ The SFrame preamble.
305 preamble’ *Note SFrame Preamble::.
306
307 0x04 ‘uint8_t’ ‘sfh_abi_arch’ The ABI/arch identifier.
308 *Note SFrame ABI/arch Identifier::.
309
310 0x05 ‘int8_t’ ‘sfh_cfa_fixed_fp_offset’ The CFA fixed FP offset, if any.
311
312 0x06 ‘int8_t’ ‘sfh_cfa_fixed_ra_offset’ The CFA fixed RA offset, if any.
313
314 0x07 ‘uint8_t’ ‘sfh_auxhdr_len’ Size in bytes of the auxiliary
315 header that follows the
316 ‘sframe_header’ structure.
317
318 0x08 ‘uint32_t’ ‘sfh_num_fdes’ The number of SFrame FDEs in the
319 section.
320
321 0x0c ‘uint32_t’ ‘sfh_num_fres’ The number of SFrame FREs in the
322 section.
323
324 0x10 ‘uint32_t’ ‘sfh_fre_len’ The length in bytes of the
325 SFrame FRE sub-section.
326
327 0x14 ‘uint32_t’ ‘sfh_fdeoff’ The offset in bytes to the
328 SFrame FDE sub-section.
329
330 0x18 ‘uint32_t’ ‘sfh_freoff’ The offset in bytes to the
331 SFrame FRE sub-section.
332
333
334 * Menu:
335
336 * SFrame ABI/arch Identifier::
337
338 
339 File: sframe-spec.info, Node: SFrame ABI/arch Identifier, Up: SFrame Header
340
341 2.2.1 SFrame ABI/arch Identifier
342 --------------------------------
343
344 SFrame header identifies the ABI/arch of the target system for which the
345 executable and hence, the stack trace information contained in the
346 SFrame section, is intended. There are currently three identifiable
347 ABI/arch values in the format.
348
349 ABI/arch Identifier Value Description
350
351 ---------------------------------------------------------------------
352 ‘SFRAME_ABI_AARCH64_ENDIAN_BIG’ 1 AARCH64 big-endian
353
354 ‘SFRAME_ABI_AARCH64_ENDIAN_LITTLE’ 2 AARCH64 little-endian
355
356 ‘SFRAME_ABI_AMD64_ENDIAN_LITTLE’ 3 AMD64 little-endian
357
358
359 The presence of an explicit identification of ABI/arch in SFrame may
360 allow stack trace generators to make certain ABI/arch-specific
361 decisions.
362
363 
364 File: sframe-spec.info, Node: SFrame Function Descriptor Entries, Next: SFrame Frame Row Entries, Prev: SFrame Header, Up: SFrame Section
365
366 2.3 SFrame FDE
367 ==============
368
369 The SFrame function descriptor entry sub-section is an array of the
370 fixed-length SFrame function descriptor entries (SFrame FDEs). Each
371 SFrame FDE is a packed structure which contains information to describe
372 a function's stack trace information at a high-level.
373
374 The array of SFrame FDEs is sorted on the ‘sfde_func_start_address’
375 if the SFrame section header flag ‘sfp_flags’ has ‘SFRAME_F_FDE_SORTED’
376 set. Typically (as is the case with GNU ld) a linked object or
377 executable will have the ‘SFRAME_F_FDE_SORTED’ set. This makes the job
378 of a stack tracer easier as it may then employ binary search schemes to
379 look for the pertinent SFrame FDE.
380
381 typedef struct sframe_func_desc_entry
382 {
383 int32_t sfde_func_start_address;
384 uint32_t sfde_func_size;
385 uint32_t sfde_func_start_fre_off;
386 uint32_t sfde_func_num_fres;
387 uint8_t sfde_func_info;
388 uint8_t sfde_func_rep_size;
389 uint16_t sfde_func_padding2;
390 } ATTRIBUTE_PACKED sframe_func_desc_entry;
391
392 Every element of the SFrame function descriptor entry is naturally
393 aligned.
394
395 ‘sfde_func_start_fre_off’ is the offset to the first SFrame FRE for
396 the function. This offset is relative to the _end of the SFrame FDE_
397 sub-section (unlike the sub-section offsets in the SFrame header, which
398 are relative to the _end_ of the SFrame header).
399
400 ‘sfde_func_info’ is the SFrame FDE "info word", containing
401 information on the FRE type and the FDE type for the function *Note The
402 SFrame FDE Info Word::.
403
404 Apart from the ‘sfde_func_padding2’, the SFrame FDE has some
405 currently unused bits in the SFrame FDE info word, *Note The SFrame FDE
406 Info Word::, that may be used for the purpose of extending the SFrame
407 file format specification for future ABIs.
408
409 Following table describes each component of the SFrame FDE structure:
410
411 Offset Type Name Description
412 ----------------------------------------------------------------------------------------------
413 0x00 ‘int32_t’ ‘sfde_func_start_address’ Signed 32-bit integral field denoting the
414 virtual memory address of the described
415 function.
416
417 0x04 ‘uint32_t’ ‘sfde_func_size’ Unsigned 32-bit integral field specifying
418 the size of the function in bytes.
419
420 0x08 ‘uint32_t’ ‘sfde_func_start_fre_off’ Unsigned 32-bit integral field specifying
421 the offset in bytes of the function's
422 first SFrame FRE in the SFrame section.
423
424 0x0c ‘uint32_t’ ‘sfde_func_num_fres’ Unsigned 32-bit integral field specifying
425 the total number of SFrame FREs used for
426 the function.
427
428 0x10 ‘uint8_t’ ‘sfde_func_info’ Unsigned 8-bit integral field specifying
429 the SFrame FDE info word.
430 *Note The SFrame FDE Info Word::.
431
432 0x11 ‘uint8_t’ ‘sfde_func_rep_size’ Unsigned 8-bit integral field specifying
433 the size of the repetitive code block for
434 which an SFrame FDE of type
435 SFRAME_FDE_TYPE_PCMASK is used. For
436 example, in AMD64, the size of a pltN
437 entry is 16 bytes.
438
439 0x12 ‘uint16_t’ ‘sfde_func_padding2’ Padding of 2 bytes. Currently unused
440 bytes.
441
442
443 * Menu:
444
445 * The SFrame FDE Info Word::
446 * The SFrame FDE Types::
447 * The SFrame FRE Types::
448
449 
450 File: sframe-spec.info, Node: The SFrame FDE Info Word, Next: The SFrame FDE Types, Up: SFrame Function Descriptor Entries
451
452 2.3.1 The SFrame FDE Info Word
453 ------------------------------
454
455 The info word is a bitfield split into three parts. From MSB to LSB:
456
457 Bit offset Name Description
458 ----------------------------------------------------------------------------------------
459 7-6 ‘unused’ Unused bits.
460
461 5 ‘pauth_key’ (For AARCH64) Specify which key is used for signing the
462 return addresses in the SFrame FDE. Two possible values:
463 SFRAME_AARCH64_PAUTH_KEY_A (0), or
464 SFRAME_AARCH64_PAUTH_KEY_B (1).
465 Ununsed in AMD64.
466
467 4 ‘fdetype’ Specify the SFrame FDE type. Two possible values:
468 SFRAME_FDE_TYPE_PCMASK (1), or
469 SFRAME_FDE_TYPE_PCINC (0).
470 *Note The SFrame FDE Types::.
471
472 0-3 ‘fretype’ Choice of three SFrame FRE types.
473 *Note The SFrame FRE Types::.
474
475 
476 File: sframe-spec.info, Node: The SFrame FDE Types, Next: The SFrame FRE Types, Prev: The SFrame FDE Info Word, Up: SFrame Function Descriptor Entries
477
478 2.3.2 The SFrame FDE Types
479 --------------------------
480
481 The SFrame format defines two types of FDE entries. The choice of which
482 SFrame FDE type to use is made based on the instruction patterns in the
483 relevant program stub.
484
485 An SFrame FDE of type ‘SFRAME_FDE_TYPE_PCINC’ is an indication that
486 the PCs in the FREs should be treated as increments in bytes. This is
487 used fo the the bulk of the executable code of a program, which contains
488 instructions with no specific pattern.
489
490 In contrast, an SFrame FDE of type ‘SFRAME_FDE_TYPE_PCMASK’ is an
491 indication that the PCs in the FREs should be treated as masks. This
492 type is useful for the cases where a small pattern of instructions in a
493 program stub is used repeatedly for a specific functionality. Typical
494 usecases are pltN entries and trampolines.
495
496 Name of SFrame FDE Value Description
497 type
498 ---------------------------------------------------------------------------
499 SFRAME_FDE_TYPE_PCINC 0 Stacktracers perform a
500 (PC >= FRE_START_ADDR) to look up a
501 matching FRE.
502
503 SFRAME_FDE_TYPE_PCMASK 1 Stacktracers perform a
504 (PC % REP_BLOCK_SIZE
505 >= FRE_START_ADDR) to look up a
506 matching FRE. REP_BLOCK_SIZE is the
507 size in bytes of the repeating block of
508 program instructions and is encoded via
509 ‘sfde_func_rep_size’ in the SFrame FDE.
510
511
512 
513 File: sframe-spec.info, Node: The SFrame FRE Types, Prev: The SFrame FDE Types, Up: SFrame Function Descriptor Entries
514
515 2.3.3 The SFrame FRE Types
516 --------------------------
517
518 A real world application can have functions of size big and small.
519 SFrame format defines three types of SFrame FRE entries to effeciently
520 encode the stack trace information for such a variety of function sizes.
521 These representations vary in the number of bits needed to encode the
522 start address offset in the SFrame FRE.
523
524 The following constants are defined and used to identify the SFrame
525 FRE types:
526
527 Name Value Description
528
529 -------------------------------------------------------------------------------
530 ‘SFRAME_FRE_TYPE_ADDR1’ 0 The start address offset (in bytes) of the
531 SFrame FRE is an unsigned 8-bit value.
532
533 ‘SFRAME_FRE_TYPE_ADDR2’ 1 The start address offset (in bytes) of the
534 SFrame FRE is an unsigned 16-bit value.
535
536 ‘SFRAME_FRE_TYPE_ADDR4’ 2 The start address offset (in bytes) of the
537 SFrame FRE is an unsigned 32-bit value.
538
539 A single function must use the same type of SFrame FRE throughout.
540 The identifier to reflect the chosen SFrame FRE type is stored in the
541 ‘fretype’ bits in the SFrame FDE info word, *Note The SFrame FDE Info
542 Word::.
543
544 
545 File: sframe-spec.info, Node: SFrame Frame Row Entries, Prev: SFrame Function Descriptor Entries, Up: SFrame Section
546
547 2.4 SFrame FRE
548 ==============
549
550 The SFrame frame row entry sub-section contains the core of the stack
551 trace information. An SFrame frame row entry (FRE) is a self-sufficient
552 record containing SFrame stack trace information for a range of
553 contiguous (instruction) addresses, starting at the specified offset
554 from the start of the function.
555
556 Each SFrame FRE encodes the stack offsets to recover the CFA, FP and
557 RA (where applicable) for the respective instruction addresses. To
558 encode this information, each SFrame FRE is followed by S*N bytes,
559 where:
560
561 − ‘S’ is the size of a stack offset for the FRE, and
562 − ‘N’ is the number of stack offsets in the FRE
563
564 The entities ‘S’, ‘N’ are encoded in the SFrame FRE info word, via
565 the ‘fre_offset_size’ and the ‘fre_offset_count’ respectively. More
566 information about the precise encoding and range of values for ‘S’ and
567 ‘N’ is provided later in the *Note The SFrame FRE Info Word::.
568
569 It is important to underline here that although the canonical
570 interpretation of these bytes is as stack offsets (to recover CFA, FP
571 and RA), these bytes _may_ be used by future ABIs/architectures to
572 convey other information on a per SFrame FRE basis.
573
574 In summary, SFrame file format, by design, supports a variable number
575 of stack offsets at the tail end of each SFrame FRE. To keep the SFrame
576 file format specification flexible yet extensible, the interpretation of
577 the stack offsets is ABI/arch-specific. The precise interpretation of
578 the FRE stack offsets in the currently supported ABIs/architectures is
579 covered in the ABI/arch-specific definition of the SFrame file format,
580 *Note ABI/arch-specific Definition::.
581
582 Next, the definitions of the three SFrame FRE types are as follows:
583
584 typedef struct sframe_frame_row_entry_addr1
585 {
586 uint8_t sfre_start_address;
587 sframe_fre_info sfre_info;
588 } ATTRIBUTE_PACKED sframe_frame_row_entry_addr1;
589
590 typedef struct sframe_frame_row_entry_addr2
591 {
592 uint16_t sfre_start_address;
593 sframe_fre_info sfre_info;
594 } ATTRIBUTE_PACKED sframe_frame_row_entry_addr2;
595
596 typedef struct sframe_frame_row_entry_addr4
597 {
598 uint32_t sfre_start_address;
599 sframe_fre_info sfre_info;
600 } ATTRIBUTE_PACKED sframe_frame_row_entry_addr4;
601
602 For ensuring compactness, SFrame frame row entries are stored
603 unaligned on disk. Appropriate mechanisms need to be employed, as
604 necessary, by the serializing and deserializing entities, if unaligned
605 accesses need to be avoided.
606
607 ‘sfre_start_address’ is an unsigned 8-bit/16-bit/32-bit integral
608 field identifies the start address of the range of program counters, for
609 which the SFrame FRE applies. The value encoded in the
610 ‘sfre_start_address’ field is the offset in bytes of the start address
611 of the SFrame FRE, from the start address of the function.
612
613 Further SFrame FRE types may be added in future.
614
615 * Menu:
616
617 * The SFrame FRE Info Word::
618
619 
620 File: sframe-spec.info, Node: The SFrame FRE Info Word, Up: SFrame Frame Row Entries
621
622 2.4.1 The SFrame FRE Info Word
623 ------------------------------
624
625 The SFrame FRE info word is a bitfield split into four parts. From MSB
626 to LSB:
627
628 Bit offset Name Description
629 -------------------------------------------------------------------------------------
630 7 ‘fre_mangled_ra_p’ Indicate whether the return address is
631 mangled with any authorization bits (signed
632 RA).
633
634 5-6 ‘fre_offset_size’ Size of stack offsets in bytes. Valid values
635 are:
636 SFRAME_FRE_OFFSET_1B,
637 SFRAME_FRE_OFFSET_2B, and
638 SFRAME_FRE_OFFSET_4B.
639
640 1-4 ‘fre_offset_count’ A max value of 15 is allowed. Typically, a
641 value of upto 3 is sufficient for most ABIs
642 to track all three of CFA, FP and RA.
643
644 0 ‘fre_cfa_base_reg_id’ Distinguish between SP or FP based CFA
645 recovery.
646
647
648 Name Value Description
649
650 --------------------------------------------------------------------------------
651 ‘SFRAME_FRE_OFFSET_1B’ 0 All stack offsets following the fixed-length
652 FRE structure are 1 byte long.
653
654 ‘SFRAME_FRE_OFFSET_2B’ 1 All stack offsets following the fixed-length
655 FRE structure are 2 bytes long.
656
657 ‘SFRAME_FRE_OFFSET_4B’ 2 All stack offsets following the fixed-length
658 FRE structure are 4 bytes long.
659
660
661 
662 File: sframe-spec.info, Node: ABI/arch-specific Definition, Next: Generating Stack Traces using SFrame, Prev: SFrame Section, Up: Top
663
664 3 ABI/arch-specific Definition
665 ******************************
666
667 This section covers the ABI/arch-specific definition of the SFrame file
668 format.
669
670 Currently, the only part of the SFrame file format definition that is
671 ABI/arch-specific is the interpretation of the variable number of bytes
672 at the tail end of each SFrame FRE. Currently, these bytes are only used
673 for representing stack offsets (for all the currently supported ABIs).
674 It is recommended to peruse this section along with *Note SFrame Frame
675 Row Entries:: for clarity of context.
676
677 Future ABIs must specify the algorithm for identifying the
678 appropriate SFrame FRE stack offsets in this chapter. This should
679 inevitably include the blueprint for interpreting the variable number of
680 bytes at the tail end of the SFrame FRE for the specific ABI/arch. Any
681 further provisions, e.g., using the auxiliary SFrame header, etc., if
682 used, must also be outlined here.
683
684 * Menu:
685
686 * AMD64::
687 * AArch64::
688
689 
690 File: sframe-spec.info, Node: AMD64, Next: AArch64, Up: ABI/arch-specific Definition
691
692 3.1 AMD64
693 =========
694
695 Irrespective of the ABI, the first stack offset is always used to locate
696 the CFA, by interpreting it as: CFA = ‘BASE_REG’ + offset1. The
697 identification of the ‘BASE_REG’ is done by using the
698 ‘fre_cfa_base_reg_id’ field in the SFrame FRE info word.
699
700 In AMD64, the return address (RA) is always saved on stack when a
701 function call is executed. Further, AMD64 ABI mandates that the RA be
702 saved at a ‘fixed offset’ from the CFA when entering a new function.
703 This means that the RA does not need to be tracked per SFrame FRE. The
704 fixed offset is encoded in the SFrame file format in the field
705 ‘sfh_cfa_fixed_ra_offset’ in the SFrame header. *Note SFrame Header::.
706
707 Hence, the second stack offset (in the SFrame FRE), when present,
708 will be used to locate the FP, by interpreting it as: FP = CFA +
709 offset2.
710
711 Hence, in summary:
712
713 Offset ID Interpretation in AMD64
714 -----------------------------------------------
715 1 CFA = ‘BASE_REG’ + offset1
716 2 FP = CFA + offset2
717
718 
719 File: sframe-spec.info, Node: AArch64, Prev: AMD64, Up: ABI/arch-specific Definition
720
721 3.2 AArch64
722 ===========
723
724 Irrespective of the ABI, the first stack offset is always used to locate
725 the CFA, by interpreting it as: CFA = ‘BASE_REG’ + offset1. The
726 identification of the ‘BASE_REG’ is done by using the
727 ‘fre_cfa_base_reg_id’ field in the SFrame FRE info word.
728
729 In AARCH64, the AAPCS64 standard specifies that the Frame Record
730 saves both FP and LR (a.k.a the RA). However, the standard does not
731 mandate the precise location in the function where the frame record is
732 created, if at all. Hence the need to track RA in the SFrame stack
733 trace format. As RA is being tracked in this ABI, the second stack
734 offset is always used to locate the RA, by interpreting it as: RA = CFA
735 + offset2. The third stack offset will be used to locate the FP, by
736 interpreting it as: FP = CFA + offset3.
737
738 Given the nature of things, the number of stack offsets seen on
739 AARCH64 per SFrame FRE is either 1 or 3.
740
741 Hence, in summary:
742
743 Offset ID Interpretation in AArch64
744 ---------------------------------------------
745 1 CFA = ‘BASE_REG’ + offset1
746 2 RA = CFA + offset2
747 3 FP = CFA + offset3
748
749 
750 File: sframe-spec.info, Node: Generating Stack Traces using SFrame, Next: Index, Prev: ABI/arch-specific Definition, Up: Top
751
752 Appendix A Generating Stack Traces using SFrame
753 ***********************************************
754
755 Using some C-like pseudocode, this section highlights how SFrame
756 provides a simple, fast and low-overhead mechanism to generate stack
757 traces. Needless to say that for generating accurate and useful stack
758 traces, several other aspects will need attention: finding and decoding
759 bits of SFrame section(s) in the program binary, symbolization of
760 addresses, to name a few.
761
762 In the current context, a ‘frame’ is the abstract construct that
763 encapsulates the following information:
764 − program counter (PC),
765 − stack pointer (SP), and
766 − frame pointer (FP)
767
768 With that said, establishing the first ‘frame’ should be trivial:
769
770 // frame 0
771 frame->pc = current_IP;
772 frame->sp = get_reg_value (REG_SP);
773 frame->fp = get_reg_value (REG_FP);
774
775 where ‘REG_SP’ and ‘REG_FP’ are are ABI-designated stack pointer and
776 frame pointer registers respectively.
777
778 Next, given frame N, generating stack trace needs us to get frame
779 N+1. This can be done as follows:
780
781 // Get the PC, SP, and FP for frame N.
782 pc = frame->pc;
783 sp = frame->sp;
784 fp = frame->fp;
785 // Populate frame N+1.
786 int err = get_next_frame (&next_frame, pc, sp, fp);
787
788 where given the values of the program counter, stack pointer and
789 frame pointer from frame N, ‘get_next_frame’ populates the provided
790 ‘next_frame’ object and returns the error code, if any. In the
791 following pseudocode for ‘get_next_frame’, the ‘sframe_*’ functions
792 fetch information from the SFrame section.
793
794 fre = sframe_find_fre (pc);
795 if (fre)
796 // Whether the base register for CFA tracking is REG_FP.
797 base_reg_val = sframe_fre_base_reg_fp_p (fre) ? fp : sp;
798 // Get the CFA stack offset from the FRE.
799 cfa_offset = sframe_fre_get_cfa_offset (fre);
800 // Get the fixed RA offset or FRE stack offset as applicable.
801 ra_offset = sframe_fre_get_ra_offset (fre);
802 // Get the fixed FP offset or FRE stack offset as applicable.
803 fp_offset = sframe_fre_get_fp_offset (fre);
804
805 cfa = base_reg_val + cfa_offset;
806 next_frame->sp = cfa;
807
808 ra_stack_loc = cfa + ra_offset;
809 // Get the address stored in the stack location.
810 next_frame->pc = read_value (ra_stack_loc);
811
812 if (fp_offset is VALID)
813 fp_stack_loc = cfa + fp_offset;
814 // Get the value stored in the stack location.
815 next_frame->fp = read_value (fp_stack_loc);
816 else
817 // Continue to use the value of fp as it has not
818 // been clobbered by the current frame yet.
819 next_frame->fp = fp;
820 else
821 ret = ERR_NO_SFRAME_FRE;
822
823 
824 File: sframe-spec.info, Node: Index, Prev: Generating Stack Traces using SFrame, Up: Top
825
826 Index
827 *****
828
829 [index]
830 * Menu:
831
832 * ABI/arch-specific Definition: ABI/arch-specific Definition.
833 (line 6)
834 * Changes from Version 1 to Version 2: Changes from Version 1 to Version 2.
835 (line 6)
836 * endianness: SFrame Magic Number and Endianness.
837 (line 6)
838 * Introduction: Introduction. (line 6)
839 * Overview: Overview. (line 6)
840 * Provisions for future ABIs: SFrame Header. (line 59)
841 * Provisions for future ABIs <1>: SFrame Function Descriptor Entries.
842 (line 41)
843 * Provisions for future ABIs <2>: SFrame Frame Row Entries.
844 (line 25)
845 * SFrame ABI/arch Identifier: SFrame ABI/arch Identifier.
846 (line 6)
847 * SFrame FDE: SFrame Function Descriptor Entries.
848 (line 6)
849 * SFrame Flags: SFrame Flags. (line 6)
850 * SFrame FRE: SFrame Frame Row Entries.
851 (line 6)
852 * SFrame header: SFrame Header. (line 6)
853 * SFrame magic number: SFrame Magic Number and Endianness.
854 (line 6)
855 * SFrame preamble: SFrame Preamble. (line 6)
856 * SFrame Section: SFrame Section. (line 6)
857 * SFrame versions: SFrame Version. (line 9)
858 * SFRAME_ABI_AARCH64_ENDIAN_BIG: SFrame ABI/arch Identifier.
859 (line 13)
860 * SFRAME_ABI_AARCH64_ENDIAN_LITTLE: SFrame ABI/arch Identifier.
861 (line 16)
862 * SFRAME_ABI_AMD64_ENDIAN_LITTLE: SFrame ABI/arch Identifier.
863 (line 18)
864 * SFRAME_F_FDE_SORTED: SFrame Flags. (line 11)
865 * SFRAME_F_FRAME_POINTER: SFrame Flags. (line 14)
866 * SFRAME_FDE_TYPE_PCINC: The SFrame FDE Types. (line 6)
867 * SFRAME_FDE_TYPE_PCMASK: The SFrame FDE Types. (line 6)
868 * SFRAME_FRE_OFFSET_1B: The SFrame FRE Info Word.
869 (line 31)
870 * SFRAME_FRE_OFFSET_2B: The SFrame FRE Info Word.
871 (line 35)
872 * SFRAME_FRE_OFFSET_4B: The SFrame FRE Info Word.
873 (line 38)
874 * SFRAME_FRE_TYPE_ADDR1: The SFrame FRE Types. (line 17)
875 * SFRAME_FRE_TYPE_ADDR2: The SFrame FRE Types. (line 21)
876 * SFRAME_FRE_TYPE_ADDR4: The SFrame FRE Types. (line 24)
877 * SFRAME_MAGIC: SFrame Preamble. (line 24)
878 * SFRAME_VERSION_1: SFrame Version. (line 9)
879 * The SFrame FDE Info Word: SFrame Function Descriptor Entries.
880 (line 85)
881 * The SFrame FRE Info Word: SFrame Frame Row Entries.
882 (line 74)
883
884
885 
886 Tag Table:
887 Node: Top563
888 Node: Introduction1237
889 Node: Overview1423
890 Node: Changes from Version 1 to Version 23009
891 Node: SFrame Section3986
892 Node: SFrame Preamble4485
893 Node: SFrame Magic Number and Endianness6084
894 Node: SFrame Version6620
895 Node: SFrame Flags7213
896 Node: SFrame Header8240
897 Node: SFrame ABI/arch Identifier13798
898 Node: SFrame Function Descriptor Entries14838
899 Node: The SFrame FDE Info Word19366
900 Node: The SFrame FDE Types20596
901 Node: The SFrame FRE Types22430
902 Node: SFrame Frame Row Entries23934
903 Node: The SFrame FRE Info Word27030
904 Node: ABI/arch-specific Definition29136
905 Node: AMD6430231
906 Node: AArch6431355
907 Node: Generating Stack Traces using SFrame32580
908 Node: Index35639
909 
910 End Tag Table
911
912 
913 Local Variables:
914 coding: utf-8
915 End: