|
| 1 | +Compact Backtrace Format |
| 2 | +======================== |
| 3 | + |
| 4 | +We would like to be able to efficiently store and access backtraces, |
| 5 | +but we also wish to minimise the memory used to store them. Since |
| 6 | +backtraces typically contain a good deal of redundancy, it should be |
| 7 | +possible to compress the data. |
| 8 | + |
| 9 | +Compact Backtrace Format (CBF) is a binary format for holding a |
| 10 | +backtrace; this specification addresses only the storage of the actual |
| 11 | +stack backtrace, and it does not consider storage of ancillary data |
| 12 | +(register contents, image lists and so on). Those will be dealt with |
| 13 | +separately elsewhere. |
| 14 | + |
| 15 | +## General Format |
| 16 | + |
| 17 | +Compact Backtrace Format data is byte aligned and starts with an |
| 18 | +information byte: |
| 19 | + |
| 20 | +~~~ |
| 21 | + 7 6 5 4 3 2 1 0 |
| 22 | + ┌───────────────────────┬───────┐ |
| 23 | + │ version │ size │ |
| 24 | + └───────────────────────┴───────┘ |
| 25 | +~~~ |
| 26 | + |
| 27 | +The `version` field identifies the version of CBF that is in use; this |
| 28 | +document describes version `0`. The `size` field is encoded as |
| 29 | +follows: |
| 30 | + |
| 31 | +| `size` | Machine word size | |
| 32 | +| :----: | :---------------- | |
| 33 | +| 00 | 16-bit | |
| 34 | +| 01 | 32-bit | |
| 35 | +| 10 | 64-bit | |
| 36 | +| 11 | Reserved | |
| 37 | + |
| 38 | +This is followed by a series of instructions that tell the reader how |
| 39 | +to decode subsequent data. |
| 40 | + |
| 41 | +The first instruction that computes an address _must_ specify an |
| 42 | +absolute address (the `a` bit must be set). |
| 43 | + |
| 44 | +## Instructions |
| 45 | + |
| 46 | +The following instructions are currently defined |
| 47 | + |
| 48 | +| `opcode` | Mnemonic | Meaning | |
| 49 | +| :--------: | :------- | :---------------------------------------- | |
| 50 | +| `00000000` | `end` | Marks the end of the backtrace | |
| 51 | +| `00000001` | `trunc` | As above, but the backtrace was truncated | |
| 52 | +| `0000xxxx` | reserved | Reserved for future expansion | |
| 53 | +| `0001axxx` | `pc` | A program counter value follows | |
| 54 | +| `0010axxx` | `ra` | A return address value follows | |
| 55 | +| `0011axxx` | `async` | An async resume point follows | |
| 56 | +| `01xxxxxx` | `omit` | Indicates frames have been omitted | |
| 57 | +| `1xxxxxxx` | reserved | Reserved for future expansion | |
| 58 | + |
| 59 | +### `end`/`trunc` |
| 60 | + |
| 61 | +#### Encoding |
| 62 | + |
| 63 | +~~~ |
| 64 | + 7 6 5 4 3 2 1 0 |
| 65 | + ┌───────────────────────────┬───┐ |
| 66 | + │ 0 0 0 0 0 0 0 │ t │ end (or trunc if t is 1) |
| 67 | + └───────────────────────────┴───┘ |
| 68 | +~~~ |
| 69 | + |
| 70 | +#### Meaning |
| 71 | + |
| 72 | +Marks the end of the backtrace data. If `t` is set, it indicates that |
| 73 | +the backtrace was truncated at this point (for instance because we hit |
| 74 | +a frame limit while capturing). |
| 75 | + |
| 76 | +It is not strictly necessary to use the `end` instruction if the |
| 77 | +CBF data is of a known length. |
| 78 | + |
| 79 | +### `pc`, `ra`, `async` |
| 80 | + |
| 81 | +#### Encoding |
| 82 | + |
| 83 | +~~~ |
| 84 | + 7 6 5 4 3 2 1 0 |
| 85 | + ┌────────────────┬───┬──────────┐ |
| 86 | + │ 0 0 0 1 │ a │ count │ pc |
| 87 | + └────────────────┴───┴──────────┘ |
| 88 | + ┌────────────────┬───┬──────────┐ |
| 89 | + │ 0 0 1 0 │ a │ count │ ra |
| 90 | + └────────────────┴───┴──────────┘ |
| 91 | + ┌────────────────┬───┬──────────┐ |
| 92 | + │ 0 0 1 1 │ a │ count │ async |
| 93 | + └────────────────┴───┴──────────┘ |
| 94 | +~~~ |
| 95 | + |
| 96 | +#### Meaning |
| 97 | + |
| 98 | +Each of these instructions represents a frame on the stack. For `pc` |
| 99 | +frames, the computed address is an actual program counter (aka |
| 100 | +instruction pointer) value. `ra` instructions instead represent a |
| 101 | +_return address_, the difference being that the program has not yet |
| 102 | +executed that instruction. `async` instructions point at the entry |
| 103 | +point of an async resume function, and are used when walking stacks on |
| 104 | +systems that support `async`/`await` primitives that are implemented |
| 105 | +by function splitting (typically an `async` instruction will point at |
| 106 | +the start of a function containing the code immediately following an |
| 107 | +`await`). |
| 108 | + |
| 109 | +The next `count + 1` bytes following the instruction are an address |
| 110 | +value. If `a` is set, the computed address is equal to the address |
| 111 | +value. If `a` is not set, the computed address is equal to the |
| 112 | +preceding computed address *plus* the address value. |
| 113 | + |
| 114 | +Address values are sign-extended to the machine word width before |
| 115 | +processing. Thus a single address byte with value `0xff` on a 32-bit |
| 116 | +backtrace represents the address value `0xffffffff`. |
| 117 | + |
| 118 | +### `omit` |
| 119 | + |
| 120 | +#### Encoding |
| 121 | + |
| 122 | +~~~ |
| 123 | + 7 6 5 4 3 2 1 0 |
| 124 | + ┌───────┬───┬───────────────────┐ |
| 125 | + │ 0 1 │ x │ count │ omit |
| 126 | + └───────┴───┴───────────────────┘ |
| 127 | +~~~ |
| 128 | + |
| 129 | +#### Meaning |
| 130 | + |
| 131 | +Indicates that a number of frames were skipped when capturing the |
| 132 | +backtrace. This is used to allow a backtrace to include both the top |
| 133 | +and bottom of the stack, without carrying every intervening frame, and |
| 134 | +is useful to prevent the data from exploding where recursion has taken |
| 135 | +place. |
| 136 | + |
| 137 | +If `x` is `1`, the instruction is followed by `count + 1` bytes (up to the |
| 138 | +machine word length) that are zero-extended to machine word length and |
| 139 | +that represent a count of the number of frames that were omitted. |
| 140 | + |
| 141 | +If `x` is `0`, `count + 1` is the number of frames that were omitted. |
0 commit comments