machdebug

package
v0.6.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 14 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package machdebug attaches to a Mach task and controls it: reading and writing its memory and registers, setting breakpoints and watchpoints, and restoring everything it changed on detach.

It is a debugger, not a bypass. It knows nothing about what it is attached to — no code-signing, no entitlements, no policy, no knowledge of any particular target binary. Consumers own their own preconditions.

Attaching to an ordinary process requires only that the target be signed with com.apple.security.get-task-allow, the standard debugger path: no root and no SIP change. Attaching to a platform binary additionally requires root and "csrutil enable --without debug"; that requirement belongs to the consumer, which must check for it at startup.

Suspension

A Process does not suspend the target on Attach. Threads stop only when they hit a breakpoint or watchpoint. Process.Wait returns an Event for one such stop, and the thread that reported it stays suspended until that Event is released — by the next call to Wait, or by Process.Detach. Other threads keep running throughout. A caller that holds an Event is holding exactly one thread, and holding it forever wedges only that thread.

A dead debugger wedges the target

A stopped thread is blocked in the kernel waiting for a reply on our exception port. If this process exits without detaching, every thread stopped at a breakpoint stays stopped, and every breakpoint still written into the target's text stops the next thread to reach it — forever. Always defer Process.Detach; Attach installs a SIGINT and SIGTERM handler that detaches for you, but nothing can save a target from a SIGKILL.

arm64e

Targets may be arm64e. Register writes go through a read-modify-write of the whole thread state so the pointer-authentication bits in Flags survive; see Thread.SetRegs. Setting PC to an address that did not come out of the same [Regs] is not supported — that needs the signed-pointer convention, and getting it subtly wrong faults the target on resume.

Scope

arm64 only. Rosetta and x86_64 targets are out of scope, as are symbolication beyond what debug/macho gives for free, DWARF, and any notion of what the target process is.

Index

Constants

This section is empty.

Variables

View Source
var ErrDetached = errors.New("machdebug: process detached")

ErrDetached is reported by every operation on a detached Process.

Functions

This section is empty.

Types

type Breakpoint

type Breakpoint struct {
	// Addr is the address the breakpoint was armed at.
	Addr uint64
	// contains filtered or unexported fields
}

Breakpoint is an armed breakpoint. Clear disarms it.

func (*Breakpoint) Clear

func (b *Breakpoint) Clear() error

Clear disarms the breakpoint, restoring the original instruction. It is safe to call on a one-shot that has already fired.

type Event

type Event struct {
	Thread Thread
	Addr   uint64
	Kind   EventKind
}

Event is one stop. The reporting thread stays suspended until the Event is released by the next call to Process.Wait or by Process.Detach.

type EventKind

type EventKind int

EventKind says why a thread stopped.

const (
	// EventBreakpoint is a breakpoint hit. Addr is the breakpoint address.
	EventBreakpoint EventKind = 1 + iota
	// EventWatchpoint is a watchpoint hit. Addr is the watched address.
	EventWatchpoint
	// EventStep is a single-step completion, reported only to callers that
	// asked for one with [Thread.Step].
	EventStep
)

Event kinds.

func (EventKind) String

func (k EventKind) String() string

String names the kind.

type Process

type Process struct {
	// contains filtered or unexported fields
}

Process is a task under our control. The zero Process is not usable; get one from Attach.

func Attach

func Attach(pid int) (*Process, error)

Attach takes control of pid: acquires its task port, saves its current exception ports, and installs our own for EXC_BREAKPOINT.

Every symbol this package needs is resolved before the target is touched. A debugger that loads a library while its target is stopped can deadlock against that target.

func (*Process) Detach

func (p *Process) Detach() error

Detach restores every patched instruction, clears every debug register, restores the exception ports saved at attach, resumes any thread still held by an unreleased Event, and releases the task port.

It is safe to call more than once, and it must succeed even after a panic: a process that exits with a breakpoint still written and no handler on the port leaves the target hung at that instruction forever.

func (*Process) Pid

func (p *Process) Pid() int

Pid reports the process the Process is attached to.

func (*Process) ReadAt

func (p *Process) ReadAt(b []byte, off int64) (int, error)

ReadAt reads from the target's address space at off, implementing io.ReaderAt. It is what lets debug/macho, encoding/binary and io.SectionReader work against a live process with no new API.

A short read reports io.EOF, the io.ReaderAt contract for an unreadable tail; the common cause is an unmapped page, not an end of file.

func (*Process) SetBreakpoint

func (p *Process) SetBreakpoint(addr uint64) (*Breakpoint, error)

SetBreakpoint arms a breakpoint at addr, saving the instruction it replaces.

A breakpoint is persistent: it fires every time the address is executed, and the resume path single-steps past the restored instruction before re-arming. Use Process.SetOneShot for a breakpoint that should fire once.

func (*Process) SetOneShot

func (p *Process) SetOneShot(addr uint64) (*Breakpoint, error)

SetOneShot arms a breakpoint at addr that disarms itself when it fires, restoring the original instruction without re-arming it. It needs no single-step cycle, which makes it both simpler and cheaper than a persistent breakpoint.

This is the primitive a temporary return-site breakpoint wants: arm it on the LR read at function entry, take the one stop, and it is gone.

func (*Process) SetWatchpoint

func (p *Process) SetWatchpoint(addr uint64, size int, w Watch) (*Watchpoint, error)

SetWatchpoint traps accesses of kind w to the size bytes at addr, using the hardware debug registers. size must be a power of two no larger than 8, and addr must be size-aligned. The number of watchpoints is a small hardware limit; SetWatchpoint reports an error when they are exhausted.

func (*Process) Threads

func (p *Process) Threads() ([]Thread, error)

Threads returns the target's current threads.

The port rights behind the returned threads are owned by the Process and released by Process.Detach.

func (*Process) Wait

func (p *Process) Wait() (*Event, error)

Wait returns the next stop, releasing the thread held by the previous Event. It blocks until a thread stops or the process detaches.

func (*Process) WaitTimeout

func (p *Process) WaitTimeout(d time.Duration) (*Event, error)

WaitTimeout is Process.Wait with a deadline: it reports os.ErrDeadlineExceeded if no thread stops within d. A zero d blocks.

func (*Process) WriteAt

func (p *Process) WriteAt(b []byte, off int64) (int, error)

WriteAt writes to the target's address space at off, implementing io.WriterAt. Writes to read-only pages, including shared-cache text, go through copy-on-write so the change is private to the target task.

type Thread

type Thread struct {
	// contains filtered or unexported fields
}

Thread is one thread of the target task.

func (Thread) Port

func (t Thread) Port() uint32

Port reports the thread's Mach port name in this task's namespace.

func (Thread) Regs

func (t Thread) Regs() (*Regs, error)

Regs reads the thread's full register state.

func (Thread) SetRegs

func (t Thread) SetRegs(r *Regs) error

SetRegs writes the thread's register state.

Always read with Thread.Regs, mutate the fields you mean, and write the same value back. A synthesized Regs loses the pointer-authentication state in Flags and faults the thread on resume.

Redirecting PC is not supported: on arm64e a PC that did not come out of the target's own state needs the signed-pointer convention, and SetRegs reports an error rather than getting that subtly wrong. Writing back the PC that Regs returned is fine.

func (Thread) Step

func (t Thread) Step() error

Step single-steps the thread one instruction. The completion arrives from Process.Wait as an EventStep.

The thread must be stopped — that is, the caller must be holding the Event that reported it. The step begins when the Event is released.

type Watch

type Watch int

Watch says which accesses a watchpoint traps.

const (
	WatchRead Watch = 1 + iota
	WatchWrite
	WatchReadWrite
)

Watch kinds.

type Watchpoint

type Watchpoint struct {
	// Addr and Size describe the watched region.
	Addr uint64
	Size int
	// contains filtered or unexported fields
}

Watchpoint is an armed watchpoint. Clear disarms it.

func (*Watchpoint) Clear

func (w *Watchpoint) Clear() error

Clear disarms the watchpoint and releases its debug register.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL