|
| 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 encqoded 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 | +| `1000xxxx` | `rep` | Repeat the previous frame | |
| 58 | +| `1xxxxxxx` | reserved | Reserved for future expansion | |
| 59 | + |
| 60 | +If the bit labelled `a` is set, it means that the address computation |
| 61 | +is absolute rather than being relative to the previously computed |
| 62 | +address. |
| 63 | + |
| 64 | +### `end`/`trunc` |
| 65 | + |
| 66 | +#### Encoding |
| 67 | + |
| 68 | +~~~ |
| 69 | + 7 6 5 4 3 2 1 0 |
| 70 | + ┌───────────────────────────┬───┐ |
| 71 | + │ 0 0 0 0 0 0 0 │ t │ end (or trunc if t is 1) |
| 72 | + └───────────────────────────┴───┘ |
| 73 | +~~~ |
| 74 | + |
| 75 | +#### Meaning |
| 76 | + |
| 77 | +Marks the end of the backtrace data. If `t` is set, it indicates that |
| 78 | +the backtrace was truncated at this point (for instance because we hit |
| 79 | +a frame limit while capturing). |
| 80 | + |
| 81 | +It is not strictly necessary to use the `end` instruction if the |
| 82 | +CBF data is of a known length. |
| 83 | + |
| 84 | +### `pc`, `ra`, `async` |
| 85 | + |
| 86 | +#### Encoding |
| 87 | + |
| 88 | +~~~ |
| 89 | + 7 6 5 4 3 2 1 0 |
| 90 | + ┌────────────────┬───┬──────────┐ |
| 91 | + │ 0 0 0 1 │ a │ count │ pc |
| 92 | + └────────────────┴───┴──────────┘ |
| 93 | + ┌────────────────┬───┬──────────┐ |
| 94 | + │ 0 0 1 0 │ a │ count │ ra |
| 95 | + └────────────────┴───┴──────────┘ |
| 96 | + ┌────────────────┬───┬──────────┐ |
| 97 | + │ 0 0 1 1 │ a │ count │ async |
| 98 | + └────────────────┴───┴──────────┘ |
| 99 | +~~~ |
| 100 | + |
| 101 | +#### Meaning |
| 102 | + |
| 103 | +Each of these instructions represents a frame on the stack. For `pc` |
| 104 | +frames, the computed address is an actual program counter (aka |
| 105 | +instruction pointer) value. `ra` instructions instead represent a |
| 106 | +_return address_, the difference being that the program has not yet |
| 107 | +executed that instruction. `async` instructions point at the entry |
| 108 | +point of an async resume function, and are used when walking stacks on |
| 109 | +systems that support `async`/`await` primitives that are implemented |
| 110 | +by function splitting (typically an `async` instruction will point at |
| 111 | +the start of a function containing the code immediately following an |
| 112 | +`await`). |
| 113 | + |
| 114 | +The next `count + 1` bytes following the instruction are an address |
| 115 | +value. If `a` is set, the computed address is equal to the address |
| 116 | +value. If `a` is not set, the computed address is equal to the |
| 117 | +preceding computed address *plus* the address value. |
| 118 | + |
| 119 | +Address values are sign-extended to the machine word width before |
| 120 | +processing. Thus a single address byte with value `0xff` on a 32-bit |
| 121 | +backtrace represents the address value `0xffffffff`. |
| 122 | + |
| 123 | +### `omit` |
| 124 | + |
| 125 | +#### Encoding |
| 126 | + |
| 127 | +~~~ |
| 128 | + 7 6 5 4 3 2 1 0 |
| 129 | + ┌───────┬───┬───────────────────┐ |
| 130 | + │ 0 1 │ x │ count │ omit |
| 131 | + └───────┴───┴───────────────────┘ |
| 132 | +~~~ |
| 133 | + |
| 134 | +#### Meaning |
| 135 | + |
| 136 | +Indicates that a number of frames were skipped when capturing the |
| 137 | +backtrace. This is used to allow a backtrace to include both the top |
| 138 | +and bottom of the stack, without carrying every intervening frame, and |
| 139 | +is useful to prevent the data from exploding where recursion has taken |
| 140 | +place. |
| 141 | + |
| 142 | +If `x` is `1`, the instruction is followed by `count + 1` bytes (up to the |
| 143 | +machine word length) that are zero-extended to machine word length and |
| 144 | +that represent a count of the number of frames that were omitted. |
| 145 | + |
| 146 | +If `x` is `0`, `count + 1` is the number of frames that were omitted. |
| 147 | + |
| 148 | +### `rep` |
| 149 | + |
| 150 | +#### Encoding |
| 151 | + |
| 152 | +~~~ |
| 153 | + 7 6 5 4 3 2 1 0 |
| 154 | + ┌────────────────┬───┬──────────┐ |
| 155 | + │ 1 0 0 0 │ x │ count │ repeat |
| 156 | + └────────────────┴───┴──────────┘ |
| 157 | +~~~ |
| 158 | + |
| 159 | +#### Meaning |
| 160 | + |
| 161 | +Repeat the previous frame. |
| 162 | + |
| 163 | +If `x` is `1`, the instruction is followed by `count + 1` bytes that are zero |
| 164 | +extended to machine word length and that represent a count of the number of |
| 165 | +times to repeat the preceding frame. |
| 166 | + |
| 167 | +If `x` is `0`, the previous frame should be repeated `count + 1` times. |
0 commit comments