Most cgi/fastcgi frameworks allow logging and replay of web service requests. That’s the ideal way to set up test cases and run regression tests.
During development, it’s sometimes handy to hop straight from the web browser to the Visual Studio IDE to trace or debug code. In that case, the following simple approach is a fast and simple method which doesn’t interfere with general code flow.
Unlike regular exe’s which can be directly run from the IDE in debug mode, and unlike DLL’s which can be attached to midflight when the application is at a convenient startup or break point, the time for a cgi executable to be running once invoked by the web server is usually very short.
Two quick code additions and a UI step to start debugging will get it done. While the approach is generally applicable, I’ll note that I’m currently doing it with MS Visual Studio 2022 and Apache 2.4 (not necessarily the deployment server but easy to run locally in Windows).
A code structure like this allows fast switching between normal runs and debugging:
#include <windows.h>
#include <debugapi.h>
...
//inside MAIN
goto normal;
while (1) {//spin until we attach to the process
if (IsDebuggerPresent()) {//MS visual studio
goto normal;
}
}
normal:
Commenting out the “goto normal” line and compiling (with post-build automatic copy to the web server’s cgi directory) sets up for a debug run. Place breakpoints as desired.
- Make sure you’re compiling+copying a debug build, not a release build.
- Load the web page- the load will not complete because the C++ loop is running.
- Set breakpoint(s) as desired
- Use Debug|Attach to Process and select the cgi executable. After the first time, Reattach will be available.
Execution will switch over to the debug environment.
This allows tracing. While webserver timeouts may be raised, it will probably still be typical for the page to be served too slow in debugging to avoid a timeout: I use this approach mainly for exploring internal state and much less for checking whether the output and formatting is perfect, since that can be checked more easily in other ways such as outputting to static html or using the browser’s developer tools.
Leave a Reply