Skip to content

godbg ¤

Classes:

  • FormatOpts
  • Type
  • GoTypeKind
  • GoTypeMeta
  • BackrefType

    A temporary placeholder type used when dumping recursive types, e.g. type a []a

  • BasicType

    A primitive Go type.

  • SliceType

    A slice type in Go, notated as []inner.

  • PointerType

    A pointer type in Go, notated as *inner.

  • ArrayType

    An array type in Go, notated as [count]inner.

  • MapType

    A map type in Go, notated as map[key]val.

  • StructType

    A struct type in Go, notated as struct(SIZE){FIELDS},

  • RuntimeType

    A value of a runtime reflection type in Go, notated as runtime(SIZE)ADDRESS,

Functions:

Attributes:

line_width module-attribute ¤

line_width = add_param(
    "go-dump-line-width", 80, "the soft line width for go-dump pretty printing"
)

indent_amount module-attribute ¤

indent_amount = add_param(
    "go-dump-indent-amount", 4, "the indent amount for go-dump pretty printing"
)

debug_color module-attribute ¤

debug_color = add_color_param(
    "go-dump-debug",
    "blue",
    "color for 'go-dump' command's debug info when --debug is specified",
)

hex_digits module-attribute ¤

hex_digits = set('0123456789abcdefABCDEFxX')

FormatOpts dataclass ¤

FormatOpts(
    int_hex: bool = False,
    debug: bool = False,
    pretty: bool = False,
    float_decimals: int | None = None,
)

Methods:

Attributes:

int_hex class-attribute instance-attribute ¤

int_hex: bool = False

debug class-attribute instance-attribute ¤

debug: bool = False

pretty class-attribute instance-attribute ¤

pretty: bool = False

float_decimals class-attribute instance-attribute ¤

float_decimals: int | None = None

fmt_int ¤

fmt_int(val: int) -> str

fmt_float ¤

fmt_float(val: float) -> str

fmt_str ¤

fmt_str(val: str) -> str

fmt_bytes ¤

fmt_bytes(val: bytes) -> str

fmt_debug ¤

fmt_debug(val: str, default: str = '') -> str

fmt_elems ¤

fmt_elems(elems: Iterable[str]) -> str

fmt_ptr ¤

fmt_ptr(val: int) -> str

Type dataclass ¤

Type(meta: GoTypeMeta | None)

Bases: ABC

Methods:

  • dump

    Dump a type from memory given an address and format.

  • size

    Returns the size of a type in bytes.

  • get_typename

    Returns the typename of a type. Should be reparsable via _parse_ty.

  • is_cyclic

    Checks if a type is cyclic (contains references to itself), e.g. type a []a

  • additional_metadata

    Returns a list of lines of additional metadata to dump from the go-type command.

  • __str__

Attributes:

meta instance-attribute ¤

meta: GoTypeMeta | None

dump abstractmethod ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

Dump a type from memory given an address and format.

size abstractmethod ¤

size() -> int

Returns the size of a type in bytes.

Used for computing array and struct layouts.

get_typename abstractmethod ¤

get_typename() -> str

Returns the typename of a type. Should be reparsable via _parse_ty.

Also used to get the string representation.

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

additional_metadata ¤

additional_metadata() -> list[str]

Returns a list of lines of additional metadata to dump from the go-type command.

__str__ ¤

__str__() -> str

GoTypeKind ¤

Bases: IntEnum

Methods:

Attributes:

INVALID class-attribute instance-attribute ¤

INVALID = 0

BOOL class-attribute instance-attribute ¤

BOOL = 1

INT class-attribute instance-attribute ¤

INT = 2

INT8 class-attribute instance-attribute ¤

INT8 = 3

INT16 class-attribute instance-attribute ¤

INT16 = 4

INT32 class-attribute instance-attribute ¤

INT32 = 5

INT64 class-attribute instance-attribute ¤

INT64 = 6

UINT class-attribute instance-attribute ¤

UINT = 7

UINT8 class-attribute instance-attribute ¤

UINT8 = 8

UINT16 class-attribute instance-attribute ¤

UINT16 = 9

UINT32 class-attribute instance-attribute ¤

UINT32 = 10

UINT64 class-attribute instance-attribute ¤

UINT64 = 11

UINTPTR class-attribute instance-attribute ¤

UINTPTR = 12

FLOAT32 class-attribute instance-attribute ¤

FLOAT32 = 13

FLOAT64 class-attribute instance-attribute ¤

FLOAT64 = 14

COMPLEX64 class-attribute instance-attribute ¤

COMPLEX64 = 15

COMPLEX128 class-attribute instance-attribute ¤

COMPLEX128 = 16

ARRAY class-attribute instance-attribute ¤

ARRAY = 17

CHAN class-attribute instance-attribute ¤

CHAN = 18

FUNC class-attribute instance-attribute ¤

FUNC = 19

INTERFACE class-attribute instance-attribute ¤

INTERFACE = 20

MAP class-attribute instance-attribute ¤

MAP = 21

POINTER class-attribute instance-attribute ¤

POINTER = 22

SLICE class-attribute instance-attribute ¤

SLICE = 23

STRING class-attribute instance-attribute ¤

STRING = 24

STRUCT class-attribute instance-attribute ¤

STRUCT = 25

UNSAFEPOINTER class-attribute instance-attribute ¤

UNSAFEPOINTER = 26

get_simple_name ¤

get_simple_name() -> str | None

GoTypeMeta dataclass ¤

GoTypeMeta(
    name: str,
    kind: GoTypeKind,
    addr: int,
    size: int = 0,
    align: int = 1,
    direct_iface: bool = False,
)

Attributes:

name instance-attribute ¤

name: str

kind instance-attribute ¤

kind: GoTypeKind

addr instance-attribute ¤

addr: int

size class-attribute instance-attribute ¤

size: int = 0

align class-attribute instance-attribute ¤

align: int = 1

direct_iface class-attribute instance-attribute ¤

direct_iface: bool = False

BackrefType dataclass ¤

BackrefType(meta: GoTypeMeta | None, key: int)

Bases: Type

A temporary placeholder type used when dumping recursive types, e.g. type a []a

Methods:

Attributes:

key instance-attribute ¤

key: int

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts())

size ¤

size() -> int

get_typename ¤

get_typename() -> str

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

additional_metadata ¤

additional_metadata() -> list[str]

Returns a list of lines of additional metadata to dump from the go-type command.

__str__ ¤

__str__() -> str

BasicType dataclass ¤

BasicType(meta: GoTypeMeta | None, name: str, extra_meta: list[str] = list())

Bases: Type

A primitive Go type.

Complex numbers are laid out as a real and imaginary part (both floats). Strings are laid out as a pointer and a length.

Methodless interfaces (the interface{} type) are denoted as any, and interfaces with methods are denoted as interface.

Function pointers are denoted as funcptr.

Methods:

Attributes:

name instance-attribute ¤

name: str

sz class-attribute instance-attribute ¤

sz: int = field(init=False)

extra_meta class-attribute instance-attribute ¤

extra_meta: list[str] = field(default_factory=list)

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

additional_metadata ¤

additional_metadata() -> list[str]

__post_init__ ¤

__post_init__() -> None

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

__str__ ¤

__str__() -> str

SliceType dataclass ¤

SliceType(meta: GoTypeMeta | None, inner: Type)

Bases: Type

A slice type in Go, notated as []inner.

Slices are laid out as a pointer, length, and capacity.

Methods:

Attributes:

inner instance-attribute ¤

inner: Type

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

additional_metadata ¤

additional_metadata() -> list[str]

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

__str__ ¤

__str__() -> str

PointerType dataclass ¤

PointerType(meta: GoTypeMeta | None, inner: Type)

Bases: Type

A pointer type in Go, notated as *inner.

Methods:

Attributes:

inner instance-attribute ¤

inner: Type

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

additional_metadata ¤

additional_metadata() -> list[str]

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

__str__ ¤

__str__() -> str

ArrayType dataclass ¤

ArrayType(meta: GoTypeMeta | None, inner: Type, count: int)

Bases: Type

An array type in Go, notated as [count]inner.

Arrays are laid out as contiguous data.

Methods:

Attributes:

inner instance-attribute ¤

inner: Type

count instance-attribute ¤

count: int

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

additional_metadata ¤

additional_metadata() -> list[str]

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

__str__ ¤

__str__() -> str

MapType dataclass ¤

MapType(meta: GoTypeMeta | None, key: Type, val: Type)

Bases: Type

A map type in Go, notated as map[key]val.

Note that maps in Go are actually pointers to the inner map, but the map type printer here directly prints the inner map.

Maps don't have a simple layout, and may reasonably change, but the last change was in 2017, so it probably won't.

The layout assumed is as follows (taken from src/runtime/map.go commit 1b4f1dc):

type hmap struct { count int flags uint8 B uint8 noverflow uint16 hash0 uint32 buckets unsafe.Pointer oldbuckets unsafe.Pointer nevacuate uintptr extra *mapextra }

Methods:

Attributes:

key instance-attribute ¤

key: Type

val instance-attribute ¤

val: Type

meta instance-attribute ¤

meta: GoTypeMeta | None

field_offsets staticmethod ¤

field_offsets() -> dict[str, int]

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

additional_metadata ¤

additional_metadata() -> list[str]

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

__str__ ¤

__str__() -> str

StructType dataclass ¤

StructType(
    meta: GoTypeMeta | None,
    fields: list[tuple[str, str | Type, int]],
    sz: int,
    name: str | None = None,
)

Bases: Type

A struct type in Go, notated as struct(SIZE){FIELDS}, where SIZE is the size of the struct in bytes, and FIELDS is a semicolon-separated list of OFFSET:NAME:TYPE fields.

Methods:

Attributes:

fields instance-attribute ¤

fields: list[tuple[str, str | Type, int]]

sz instance-attribute ¤

sz: int

name class-attribute instance-attribute ¤

name: str | None = None

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

additional_metadata ¤

additional_metadata() -> list[str]

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

__str__ ¤

__str__() -> str

RuntimeType dataclass ¤

RuntimeType(meta: GoTypeMeta | None, sz: int, addr: int)

Bases: Type

A value of a runtime reflection type in Go, notated as runtime(SIZE)ADDRESS, where SIZE is the size of the type's value in bytes, and ADDRESS is the address of the type.

This type is useful for serializing cyclic types.

Methods:

Attributes:

sz instance-attribute ¤

sz: int

addr instance-attribute ¤

addr: int

meta instance-attribute ¤

meta: GoTypeMeta | None

dump ¤

dump(addr: int, fmt: FormatOpts = FormatOpts()) -> str

size ¤

size() -> int

get_typename ¤

get_typename() -> str

is_cyclic ¤

is_cyclic() -> bool

Checks if a type is cyclic (contains references to itself), e.g. type a []a

additional_metadata ¤

additional_metadata() -> list[str]

Returns a list of lines of additional metadata to dump from the go-type command.

__str__ ¤

__str__() -> str

word_size ¤

word_size() -> int

Gets the Go word size for the current architecture.

Values taken from https://github.com/golang/go/blob/20b79fd5775c39061d949569743912ad5e58b0e7/src/go/types/sizes.go#L233-L252

compute_offsets ¤

compute_offsets(fields: Iterable[tuple[int, int]]) -> list[int]

Given a list of (size, alignment) for struct field types, returns a list of field offsets for the struct. The last element will be the offset of the struct's end (the struct size).

Layout computation taken from src/go/types/sizes.go commit 1b4f1dc

compute_named_offsets ¤

compute_named_offsets(fields: Iterable[tuple[str, int, int]]) -> dict[str, int]

Like compute_offsets, but takes in field names and returns a dictionary mapping field name to offset instead.

Also maps in a special $size field with the size of the struct.

load_uint ¤

load_uint(data: bytes, endian: Literal['little', 'big'] | None = None) -> int

load_int ¤

load_int(data: bytes) -> int

load_float ¤

load_float(data: bytes) -> float

emit_warning ¤

emit_warning(msg: str)

get_elf ¤

get_elf() -> ELFInfo | None

read_buildversion ¤

read_buildversion(addr: int) -> str

Reads a Go runtime.buildVersion string to extract the version.

get_go_version ¤

get_go_version() -> tuple[int, ...] | None

Try to determine the Go version used to compile the binary.

None can be returned if the version couldn't be inferred, at which point it's probably best to assume latest version.

get_type_start ¤

get_type_start(addr: int | None = None) -> int | None

Given the address to a type, try to find the moduledata types section containing it.

Necessary to determine the base address that the type name is offset by.

read_varint_str ¤

read_varint_str(addr: int) -> bytes

Read a length-prefix string encoded with Go's variable length encoding.

Implementation taken from https://github.com/golang/go/blob/9d33956503c0d96c0c5666d374173f7ac9756d98/src/internal/abi/type.go#L640-L649

read_type_name ¤

read_type_name(addr: int) -> bytes

Reads a Go type name given the address to the name.

Go type names are stored as a 1 byte bitfield followed by a varint length prefixed string after 1.17.

Prior to 1.17, they were stored as a 1 byte bitfield followed by a 2 byte length prefixed string.

decode_runtime_type ¤

decode_runtime_type(
    addr: int, keep_backrefs: bool = False
) -> tuple[GoTypeMeta, Type | None]

Decodes a runtime reflection type from memory, returning a (meta, type) tuplee.

The layout assumed is as follows (taken from src/internal/abi/type.go commit 1b4f1dc):

type Type struct { Size_ uintptr PtrBytes uintptr Hash uint32 TFlag TFlag Align_ uint8 FieldAlign_ uint8 Kind_ Kind Equal func(unsafe.Pointer, unsafe.Pointer) bool GCData *byte Str NameOff PtrToThis TypeOff }

parse_type ¤

parse_type(ty: str) -> Type