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 ¶
- Variables
- type Breakpoint
- type Event
- type EventKind
- type Process
- func (p *Process) Detach() error
- func (p *Process) Pid() int
- func (p *Process) ReadAt(b []byte, off int64) (int, error)
- func (p *Process) SetBreakpoint(addr uint64) (*Breakpoint, error)
- func (p *Process) SetOneShot(addr uint64) (*Breakpoint, error)
- func (p *Process) SetWatchpoint(addr uint64, size int, w Watch) (*Watchpoint, error)
- func (p *Process) Threads() ([]Thread, error)
- func (p *Process) Wait() (*Event, error)
- func (p *Process) WaitTimeout(d time.Duration) (*Event, error)
- func (p *Process) WriteAt(b []byte, off int64) (int, error)
- type Thread
- type Watch
- type Watchpoint
Constants ¶
This section is empty.
Variables ¶
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 ¶
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.
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 ¶
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 ¶
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) ReadAt ¶
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 ¶
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 ¶
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 ¶
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 ¶
WaitTimeout is Process.Wait with a deadline: it reports os.ErrDeadlineExceeded if no thread stops within d. A zero d blocks.
type Thread ¶
type Thread struct {
// contains filtered or unexported fields
}
Thread is one thread of the target task.
func (Thread) SetRegs ¶
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.
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.