Software Side Roads

Scenic Detours Along the Way to Release


Debugging C++ CGI Applications Under Windows

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

About Me

The focus of my work is writing software tools in support of engineering and financial analysis plus taking care of the source and results data for quality and documentation purposes. Most of my professional time has been spent in the engineering, particularly naval architecture, and finance sectors. Many side tasks which aren’t strictly central to my deliverables crop up and must be dealt with. These posts are notes on the little side tours and cul-de-sacs along the way.

Newsletter

Discover more from Software Side Roads

Subscribe now to keep reading and get access to the full archive.

Continue reading