attach
Attach to a running Go process by specifying its process ID (PID) for debugging and analysis using the Delve debugger interface.
Instructions
Attach to a running process
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pid | Yes | Process ID to attach to |
Implementation Reference
- src/handlers/debug.ts:26-39 (handler)The main handler function for the 'attach' tool. Validates the PID argument, starts a new debug session using startDebugSession with type 'attach', and returns a success message with the session ID.case "attach": { const pid = Number(args?.pid); if (!pid) { throw new Error("Process ID is required"); } const session = await startDebugSession("attach", pid.toString()); return { content: [{ type: "text", text: `Attached to process ${pid} with session ${session.id}` }] }; }
- src/session.ts:35-64 (helper)Core helper function that implements the attachment by spawning the Delve debugger ('dlv') process with 'attach' mode, PID as target, assigns a random ID, finds available port, and manages the session.export async function startDebugSession(type: string, target: string, args: string[] = []): Promise<DebugSession> { const port = await getAvailablePort(); const id = Math.random().toString(36).substring(7); const dlvArgs = [ type, "--headless", `--listen=:${port}`, "--accept-multiclient", "--api-version=2", target, ...args ]; const process = spawn("dlv", dlvArgs, { stdio: ["pipe", "pipe", "pipe"] }); const session: DebugSession = { id, type, target, process, port, breakpoints: new Map() }; sessions.set(id, session); return session; }
- src/server.ts:406-407 (registration)Routes calls to the 'attach' tool (and other debug tools) to the handleDebugCommands function in the CallToolRequestHandler.if (["debug", "attach", "exec", "test", "core", "dap", "replay", "trace"].includes(name)) { return handleDebugCommands(name, args);
- src/server.ts:85-98 (schema)Registers the 'attach' tool in ListTools response, providing name, description, and input schema requiring a numeric 'pid'.{ name: "attach", description: "Attach to a running process", inputSchema: { type: "object", properties: { pid: { type: "number", description: "Process ID to attach to" } }, required: ["pid"] } },