---
name: "backtrace_pack"
section: "9"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["macos"]
url: "https://new.carta.tech/backtrace_pack"
---

# backtrace\_pack(9)

convert a backtrace to and from compact formats

## NAME

**backtrace\_pack**, **backtrace\_unpack** – convert a backtrace to and from compact formats

## SYNOPSIS

**#include \<kern/backtrace.h\>**

*size\_t*\

**backtrace\_pack**(*backtrace\_pack\_t packing*, *uint8\_t \*dst*, *size\_t dst\_size*, *uintptr\_t \*src*, *unsigned int src\_len*);

*unsigned* *int*\

**backtrace\_unpack**(*backtrace\_pack\_t packing*, *uintptr\_t \*dst*, *unsigned int dst\_len*, *uint8\_t \*src*, *size\_t src\_size*);

## DESCRIPTION

The **backtrace\_pack** and **backtrace\_unpack** functions convert to and from a compact representation of a backtrace.

## RETURN VALUES

The **backtrace\_pack**() function returns the number of bytes written to the destination buffer. The **backtrace\_unpack**() function returns the number of addresses written to the destination buffer.

## FORMATS

- **BTP\_NONE**

  Just copy the addresses unpacked to the destination buffer.

- **BTP\_KERN\_OFFSET\_32**

  Subtract the kernel base address from each address so they fit in 4 bytes.

## EXAMPLE

```
uintptr_t bt[8] = { 0 };
enum backtrace_info bti = BTI_NONE;
unsigned int len = backtrace(bt, sizeof(bt) / sizeof(bt[0]), NULL, &bti);
uint8_t bt_packed[1024] = { 0 };
size_t packed_size = backtrace_pack(BTP_KERN_OFFSET_32, bt_packed,
		sizeof(bt_packed), bt, len);
uintptr_t bt_unpacked[8] = { 0 };
unsigned int unpacked_len = backtrace_unpack(BTP_KERN_OFFSET_32, bt_unpacked,
		sizeof(bt_unpacked) / sizeof(bt_unpacked[0]), bt_packed, packed_size);
assert(len == unpacked_len);
for (unsigned int i = 0; i < len; i++) {
	assert(bt[i] == unpacked_bt[i]);
}
```

## SEE ALSO

[backtrace(9)](/backtrace/9.md)
