How to debug a BWAPI AIModule with Visual Studio

From StarCraft AI, the resource for custom StarCraft Brood War AIs
Jump to: navigation, search

Debugging an AI Module for BWAPI is not as easy as debugging a regular VC++ application. When you compile an AI Module a DLL file is created which BWAPI will inject into the running StarCraft process once a game is started.

For your AI Module to be debuggable you have to compile it in Debug mode. You also have to make sure that your AI Module uses the Multithreaded Debug DLL (/MDd). Notice that you will need to link all external DLL in their debug version, i.e. BWAPId.lib, BWTAd.lib, ...

Now you need to edit bwapi.ini to config your ai on debug mode:

ai_dbg = bwapi-data\AI\ExampleAIModuled.dll
; make sure pause_dbg is ON
pause_dbg = ON

Now to actually debug your DLL start ChaosLauncher with BWAPI DEBUG injector checked.

ChaosLaunhcer Debug.png

StarCraft will wait until a debugger is attached to the process. To do that, you have to use Debug -> Attach to Process from the VC++ main menu and find the already launched StarCraft.exe in the list of running processes.

Attach to process.png

Only after you successfully attached to the running StarCraft.exe the VC++ debugger will be able to halt the game at breakpoints and point you at the location where exceptions are thrown, invalid memory accesses and crashed are produced.