Debugging NaCl apps in Visual Studio with WinGDB using debug stub (Experimental)

Creating NaCl project in Visual Studio

WinGDB plugin overrides actions in a normal Windows project. So we need to create one first. Go to File->New->Project... menu.


Press OK button, you will see Win32 Application Wizard.


Press the Next button on the first page.


Select empty project and press finish button on the second page.

Now go to SDK folder and copy some example project to Projects\NaCl Project\NaCl Project directory. We will use hello_world further. Right click on the Source Files folder in NaCl Project in Solution Explorer view and choose Add->Existing Item... in the menu. Select all files you have just copied.


Now replace relative NaCl SDK location in make.bat to the absolute path.


Try to launch make.bat to see if the build works. If it can't compile program for win platform, you can disable it by commenting ALL_TARGETS+=win/Debug/hello_world.nmf and ALL_TARGETS+=win/Release/hello_world.nmf lines in the Makefile.
Now right click on a project, select WinGDB->Properties in the menu. Set target type to embedded Linux system.


Then set Project build command to "$(ProjectDir)make.bat" and Build working directory to $(ProjectDir). Note, that first field needs quotes but second field need not.


Press OK button and try to build project by following WinGDB->Build->Build NaCl Project in project popup menu. You should see the result in the output view. If you want to configure other build actions like clean, set action command in WinGDB properties to "$(ProjectDir)make.bat" make_target.


At last, you can tell WinGDB to intercept normal build commands. Set Intercept standard IDE commands option to Yes.


In this case you can build project by right clicking at it and choosing Build in the popup menu.

Debugging newlib application

Launching NaCl application

Enable Native Client in about:flags in chrome in order to enable NaCl on all pages. Copy httpd.py from examples directory in SDK to project directory. Replace python ../httpd.py line in Makefile with python httpd.py. Run make.bat RUN to start the HTTP server. Now go to localhost:5103 in chrome and you will see a message from your new NaCl application.

Set up chrome for debugging

In order to enable NaCl debugging, run chrome with --enable-nacl-debug and --no-sandbox command line switches. The first switch activates debug stub in all NaCl applications. Debug stub stops NaCl program at first instruction (before main), opens 4014 port and waits for gdb to connect. Since debug stub opens a TCP port, do not launch more than one NaCl application at once in this mode. Note, that this port is opened on localhost network interface and so is not accessible from outside network. If you want to debug from different computer, you will need to forward this port. This is usually done using ssh.

Every time you want to debug NaCl application, you need to run chrome first and then use Visual Studio with WinGDB to connect to debug stub.

Set up WinGDB for debugging

Go to WinGDB preferences (right click on the project, WinGDB->Properties) and set Debugger Path to C:\nacl_sdk\pepper_canary\toolchain\win_x86_newlib\bin\x86_64-nacl-gdb.exe. This debugger is 32-bit executable that is capable of debugging both 32-bit and 64-bit NaCl applications.


Now set executable path to $(ProjectDir)newlib\Debug\hello_world_x86_64.nexe and launch GDB server automatically option to No.


Set target specification to remote :4014.


Set server mode to launch executable.


Finally, press OK button.

Launch debugger from Visual Studio

Set a breakpoint in hello_world.c file. You can use Instance_DidCreate function to break application as early as possible. Now right click on the project and choose WinGDB->Start new instance in the popup menu. Debugger will connect to debug stub and NaCl application will start executing until it hits breakpoint.


Debugging glibc application

Launching NaCl and chrome is no different from newlib case. You just need to open localhost:5103/index_glibc_Debug.html in chrome.

Set up WinGDB for glibc debugging

Create gdb_init.txt file in Source Files folder. You need to add there 2 gdb commands that will be executed before connecting to the NaCl program. The first command tells gdb full path to the manifest.
nacl-manifest "c:\\Users\\username\\Documents\\Visual Studio 2010\\Projects\\NaCl Project\\NaCl Project\\glibc\\Debug\\hello_world.nmf"
The second command maps cygwin paths in the program debug information to Windows paths since NaCl compiler is cygwin-based while WinGDB is not. In case of newlib program symbols are available from the start of the program, WinGDB recognizes them and uses relative paths that doesn't need mapping.
set substitute-path /cygdrive/c/ c:/


Now open WinGDB properties (right click on the project, WinGDB->Properties). Set debugger path to C:\nacl_sdk\pepper_canary\toolchain\win_x86_newlib\bin\x86_64-nacl-gdb.exe. Note that debugger is the same for glibc and newlib, 32 and 64-bit. 


Set executable path to $(ProjectDir)glibc\Debug\lib64\runnable-ld.so and launch GDB server automatically option to No.


Set Additional debugger options to -x '$(ProjectDir)gdb_init.txt'. Unfortunately, initialization script doesn't work for nacl,
since we need to execute nacl-manifest command before gdb is connected to the target.


Set target specification to remote :4014.


Set server mode to Launch executable.


Now you can attach to NaCl glibc application in chrome by right clicking on the project and choosing WinGDB->Start new instance.


Comments