Debugging in C++

From CryWiki

Jump to: navigation, search

Introduction

The Visual Studio debugger is a wonderful thing. It grants the programmer the power to stop time and take a peek at the state of various objects at any point in code.

Unfortunately, Crysis itself isn't debuggable in the ordinary way, because pirates and cheaters abound in such a fashion that Crytek licensed Sony's SecuROM to attempt to protect their game. Sony's SecuROM is a heavy handed rootkit that doesn't even do its job properly, but I digress.

Suffice it to say that our benign debugger is detected as a hack, and Crysis quits with a mysterious error message. Attempting to patch out the SecuROM protection is illegal and of dubious value. After all, we'd like to test against the platform that our mods will be running on. What's a mod developer to do?

Well, there are two ways we can debug. If you don't need to test Multiplayer code or main menus, you can debug your mod in the editor. If you need to test in either of those categories, you'll have to attach the debugger to a running Crysis instance.

For the purposes of this tutorial, we will assume that we are working with the included CrysisMod, and that it has already compiled using the Debug profile. Other tutorials will explain how to get this far.

Debugging in the editor

Fortunately, the editor is not protected by SecuROM. So, we can tell the editor to load our mod and debug in that.

Open up Visual Studio, then open up the properties of the CrysisMod project. Under "Configuration Properties" click "Debugging". For "Command", enter the full path to your Crysis.exe. On my system, that's "C:\Program Files\Electronic Arts\Crytek\Crysis\Bin32\Editor.exe". For "Command Arguments" enter "-MOD CrysisMod". Click OK.

Congratulations, you're debugging Crysis! To verify, switch to the "Output" tab in the lower right hand corner of Visual Studio. You should find a line similar to this one:

'Editor.exe': Loaded 'C:\Program Files\Electronic Arts\Crytek\Crysis\Mods\CrysisMod\bin32\CrysisMod.dll', Symbols loaded.

Make sure that it says "Symbols loaded" for the CrysisMod dll. If it doesn't, something went wrong with your build and your breakpoints will not be hit. Double check that you're compiling using the Debug profile (in the drop box right next to the play button). It's OK if other dlls don't have the "Symbols loaded" annotation.

Attaching to a running Crysis instance

Luckily for us, the SecuROM protection only checks once at the beginning for hacks. After that, it seems that it leaves Crysis to go about its business. Why is this lucky, you ask? Because the Visual Studio debugger has an "Attach" option. Basically, it can go ahead and work its magic on already existing processes. Meaning, we can debug almost everything our dll is doing without having to worry about SecuROM halting the game.

So, if Visual Studio isn't going to launch Crysis for us, we need something that will. For simplicity's sake, we'll write a batch file for the purpose in this tutorial. You could also create a shortcut for the purpose.

So, crack open your Crytek\Crysis\Bin32 folder, and create a new text file. In it, paste:

crysis.exe -DEVMODE -MOD CrysisMod +r_Fullscreen 0

Save it, and rename it to LaunchCryMod.bat. Optionally, send it to the desktop for ease of launch.

Note
"-DEVMODE" is optional. "+r_Fullscreen 0", while technically optional, will make it much easier to launch and use the debugger.

When you save the file, make sure that the icon changes from a spiral-bound notepad to a window with a big gear in the middle. If it doesn't, you've saved it wrong and it still has a hidden extension of .txt. To change the extension, you can either show file extensions, or open it up in Notepad and Save As... In the "Save as type" drop down box, choose "All Files", and save it as "LaunchCryMod.bat" again.

OK, so now we have a way to launch Crysis without it exploding in our faces, and it will load our mod in DevMode. Next, we'll set up Visual Studio to attach itself to Crysis and debug our mod.

Open up Visual Studio, then open up the properties of the CrysisMod project. Under "Configuration Properties" click "Debugging". For "Command", enter the full path to your Crysis.exe. On my system, that's "C:\Program Files\Electronic Arts\Crytek\Crysis\Bin32\Crysis.exe". Change "Attach" to "Yes". Click OK.

Now, we're prepared to debug our mod. Make sure the mod is built by pressing Ctrl+Shift+B. Check to make sure that 0 failed. Now, launch Crysis using the batch file we prepared. You will see a small black window with text pop up. Be patient. When you see the actual Crysis window pop up, with Nomad in the upper left hand corner, go ahead and click on the green play button in Visual Studio.

Note
If you can't see your mouse, don't fret. Press Alt+Tab repeatedly until the Visual Studio infinity logo is selected.

Congratulations, you're debugging Crysis! To verify, switch to the "Output" tab in the lower right hand corner of Visual Studio. You should find a line similar to this one:

'Crysis.exe': Loaded 'C:\Program Files\Electronic Arts\Crytek\Crysis\Mods\CrysisMod\bin32\CrysisMod.dll', Symbols loaded.

Make sure that it says "Symbols loaded" for the CrysisMod dll. If it doesn't, something went wrong with your build and your breakpoints will not be hit. Double check that you're compiling using the Debug profile (in the drop box right next to the play button). It's OK if other dlls don't have the "Symbols loaded" annotation.

Personal tools