ida_add_pseudocode_comment
Add comments to specific addresses in decompiled pseudocode to enhance understanding and documentation of analyzed functions.
Instructions
Add a comment to a specific address in the function's decompiled pseudocode
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| address | Yes | ||
| comment | Yes | ||
| function_name | Yes | ||
| is_repeatable | No |
Implementation Reference
- Core handler that adds pseudocode comment using IDA Hex-Rays API: decompiles function, validates address in function, creates treeloc_t, sets user comment via cfunc.set_user_cmt and saves.def add_pseudocode_comment(self, function_name: str, address: str, comment: str, is_repeatable: bool) -> Dict[str, Any]: """Add a comment to a specific address in the function's decompiled pseudocode""" return self._add_pseudocode_comment_internal(function_name, address, comment, is_repeatable) def _add_pseudocode_comment_internal(self, function_name: str, address: str, comment: str, is_repeatable: bool) -> Dict[str, Any]: """Internal implementation for add_pseudocode_comment without sync wrapper""" try: # Parameter validation if not function_name: return {"success": False, "message": "Function name cannot be empty"} if not address: return {"success": False, "message": "Address cannot be empty"} if not comment: # Allow empty comment to clear the comment comment = "" # Get function address func_addr: int = ida_name.get_name_ea(0, function_name) if func_addr == idaapi.BADADDR: return {"success": False, "message": f"Function '{function_name}' not found"} # Check if it's a function func: Optional[ida_funcs.func_t] = ida_funcs.get_func(func_addr) if not func: return {"success": False, "message": f"'{function_name}' is not a function"} # Check if decompiler is available if not ida_hexrays.init_hexrays_plugin(): return {"success": False, "message": "Hex-Rays decompiler is not available"} # Get decompilation result cfunc: Optional[ida_hexrays.cfunc_t] = ida_hexrays.decompile(func_addr) if not cfunc: return {"success": False, "message": f"Failed to decompile function '{function_name}'"} # Open pseudocode view ida_hexrays.open_pseudocode(func_addr, 0) # Convert address string to integer addr: int if isinstance(address, str): if address.startswith("0x"): addr = int(address, 16) else: try: addr = int(address, 16) # Try parsing as hex except ValueError: try: addr = int(address) # Try parsing as decimal except ValueError: return {"success": False, "message": f"Invalid address format: {address}"} else: addr = address # Check if address is valid if addr == idaapi.BADADDR or not ida_bytes.is_loaded(addr): return {"success": False, "message": f"Invalid or unloaded address: {hex(addr)}"} # Check if address is within function if not (func.start_ea <= addr < func.end_ea): return {"success": False, "message": f"Address {hex(addr)} is not within function '{function_name}'"} # Create treeloc_t object for comment location loc = ida_hexrays.treeloc_t() loc.ea = addr loc.itp = ida_hexrays.ITP_BLOCK1 # Comment location # Set comment cfunc.set_user_cmt(loc, comment) cfunc.save_user_cmts() # Refresh view self._refresh_view_internal() comment_type: str = "repeatable" if is_repeatable else "regular" return { "success": True, "message": f"Added {comment_type} comment at address {hex(addr)} in function '{function_name}'" } except Exception as e: print(f"Error adding pseudocode comment: {str(e)}") traceback.print_exc() return {"success": False, "message": str(e)}
- src/mcp_server_ida/server.py:75-80 (schema)Pydantic schema defining input parameters for the tool: function_name, address, comment, is_repeatable.class AddPseudocodeComment(BaseModel): function_name: str address: str # Address in the pseudocode comment: str is_repeatable: bool = False # Whether comment should be repeated at all occurrences
- src/mcp_server_ida/server.py:998-1002 (registration)MCP tool registration in list_tools() with name, description, and input schema.Tool( name=IDATools.ADD_PSEUDOCODE_COMMENT, description="Add a comment to a specific address in the function's decompiled pseudocode", inputSchema=AddPseudocodeComment.schema(), ),
- src/mcp_server_ida/server.py:704-730 (handler)MCP server-side handler in IDAProFunctions that sends 'add_pseudocode_comment' request to IDA plugin.def add_pseudocode_comment(self, function_name: str, address: str, comment: str, is_repeatable: bool = False) -> str: """Add a comment to a specific address in the function's decompiled pseudocode""" try: response: Dict[str, Any] = self.communicator.send_request( "add_pseudocode_comment", { "function_name": function_name, "address": address, "comment": comment, "is_repeatable": is_repeatable } ) if "error" in response: return f"Error adding comment at address {address} in function '{function_name}': {response['error']}" success: bool = response.get("success", False) message: str = response.get("message", "") if success: comment_type: str = "repeatable" if is_repeatable else "regular" return f"Successfully added {comment_type} comment at address {address} in function '{function_name}': {message}" else: return f"Failed to add comment at address {address} in function '{function_name}': {message}" except Exception as e: self.logger.error(f"Error adding pseudocode comment: {str(e)}", exc_info=True) return f"Error adding comment at address {address} in function '{function_name}': {str(e)}"
- src/mcp_server_ida/server.py:104-104 (registration)Tool name constant definition in IDATools enum.ADD_PSEUDOCODE_COMMENT = "ida_add_pseudocode_comment"