gdb_call_function
Execute any accessible function in a debugged program's context to test logic, verify state, or invoke cleanup routines during a GDB session.
Instructions
Call a function in the target process. WARNING: This is a privileged operation that executes code in the debugged program. It can call any function accessible in the current context, including: - Standard library functions: printf, malloc, free, etc. - Program functions: any function defined in the program - System calls via wrappers The function executes with full privileges of the debugged process. Use with caution as it may have side effects and modify program state. Examples: 'printf("debug: x=%d\n", x)', 'my_cleanup_func()', 'strlen(str)'. Requires session_id parameter (obtained from gdb_start_session).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | Session ID from gdb_start_session | |
| function_call | Yes | Function call expression (e.g., 'printf("hello\n")' or 'my_func(arg1, arg2)') |
Implementation Reference
- src/gdb_mcp/server.py:431-446 (registration)Tool registration for gdb_call_function - defines the tool name, description, and input schema.
Tool( name="gdb_call_function", description=( "Call a function in the target process. " "WARNING: This is a privileged operation that executes code in the debugged program. " "It can call any function accessible in the current context, including: " "- Standard library functions: printf, malloc, free, etc. " "- Program functions: any function defined in the program " "- System calls via wrappers " "The function executes with full privileges of the debugged process. " "Use with caution as it may have side effects and modify program state. " "Examples: 'printf(\"debug: x=%d\\n\", x)', 'my_cleanup_func()', 'strlen(str)'. " "Requires session_id parameter (obtained from gdb_start_session)." ), inputSchema=CallFunctionArgs.model_json_schema(), ), - src/gdb_mcp/server.py:173-179 (schema)Input schema for gdb_call_function - defines the session_id and function_call parameters.
class CallFunctionArgs(BaseModel): session_id: int = Field(..., description="Session ID from gdb_start_session") function_call: str = Field( ..., description="Function call expression (e.g., 'printf(\"hello\\n\")' or 'my_func(arg1, arg2)')", ) - src/gdb_mcp/server.py:569-571 (handler)Handler dispatch for gdb_call_function - parses arguments and delegates to GDBSession.call_function().
elif name == "gdb_call_function": call_args: CallFunctionArgs = CallFunctionArgs(**arguments) result = session.call_function(function_call=call_args.function_call) - Actual implementation in GDBSession.call_function() - builds the GDB 'call' command and sends it via the MI interpreter.
def call_function( self, function_call: str, timeout_sec: int = DEFAULT_TIMEOUT_SEC ) -> dict[str, Any]: """ Call a function in the target process. This is a privileged operation that executes the GDB 'call' command, which invokes a function in the debugged program. This can execute arbitrary code in the target process and may have side effects. WARNING: Use with caution as this can modify program state. Args: function_call: Function call expression (e.g., "printf(\\"hello\\n\\")" or "my_function(arg1, arg2)") timeout_sec: Timeout for command execution Returns: Dict with the function's return value or error """ if not self.controller: return {"status": "error", "message": "No active GDB session"} if not self._is_gdb_alive(): return { "status": "error", "message": "GDB process has exited - cannot execute call", } # Build the call command command = f"call {function_call}" # Escape for MI command escaped_command = command.replace("\\", "\\\\").replace('"', '\\"') mi_command = f'-interpreter-exec console "{escaped_command}"' result = self._send_command_and_wait_for_prompt(mi_command, timeout_sec) if "error" in result: return { "status": "error", "message": result["error"], "function_call": function_call, } if result.get("timed_out"): return { "status": "error", "message": f"Timeout waiting for call to complete after {timeout_sec}s", "function_call": function_call, } parsed = self._parse_responses(result.get("command_responses", [])) console_output = "".join(parsed.get("console", [])) return { "status": "success", "function_call": function_call, "result": console_output.strip() if console_output else "(no return value)", }