Home » C++ » How to make Visual Studio Pause after executing a console app in debug mode?

How to make Visual Studio Pause after executing a console app in debug mode?

Posted by: admin November 30, 2017 Leave a comment

Questions:

I have a collection of boost unit tests I want to run as a console application.

When I’m working on the project and I run the tests I would like to be able to debug the tests and I would like to have the console stay open after the tests run.

I see that if I run in release mode the console window stays up after the program exits, but in debug mode this is not the case.

I do not want to add ‘system(“pause”);’ or any other hacks like reading a character to my program. I just want to make Visual Studio pause after running the tests with debugging like it would if I were running in release mode. I would also like it if the output of tests were captured in one of Visual Studio’s output windows but that also seems to be harder than it should be.

How can I do this?

Answers:

boost test offers the following usage recommendations for visual studio that would enable you to run the unit tests automatically at the end of compilation and capture the output into the build window.

the nice side effect of this trick is it enable you to treat test failures as compilation errors. “…you could jump through these errors using usual keyboard shortcuts/mouse clicks you use for compilation error analysis…”

Questions:
Answers:

Try to run the app with the Ctrl+F5 combination.

Questions:
Answers:

http://connect.microsoft.com/VisualStudio/feedback/details/540969/missing-press-any-key-to-continue-when-lauching-with-ctrl-f5

In the older versions it would default to the console subsystem even if you selected “empty project”, but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don’t have to worry about this). Then select “project” from the menu bar drop down menus, then select “project_name properties” > “configuration properties” > “linker” > “system” and set the first property, the drop down “subsystem” property to “console (/SUBSYSTEM:CONSOLE)”. The console window should now stay open after execution as usual.

Questions:
Answers:

Set a breakpoint on the last line of code.

Questions:
Answers:

Just copied from http://social.msdn.microsoft.com/forums/en-US/Vsexpressvc/thread/1555ce45-8313-4669-a31e-b95b5d28c787/?prof=required:

The following works for me 🙂

/////////////////////////////////////////////////////////////////////////////////////
Here is another reason the console may disappear and the solution.

With the new visual studio 2010 you might see this behavior even when you use ctrl f5 aka “start without debugging”. This is most likely because you created an “empty project” instead of a “Win32 console application”. If you create the project as a “Win32 console application” you can disregard this as it does not apply.

In the older versions it would default to the console subsystem even if you selected “empty project”, but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don’t have to worry about this). Then select “project” from the menu bar drop down menus, then select “*project_name* properties” > “configuration properties” > “linker” > “system” and set the first property, the drop down “subsystem” property to “console (/SUBSYSTEM:CONSOLE)”. The console window should now stay open after execution as usual.

/////////////////////////////////////////////////////////////////////////////////////

Questions:
Answers:

If it is a Console Application, use Cntrl+F5

Questions:
Answers:

Why not use the system(“pause”) hack?

If it’s because you don’t want the program to prompt when it’s not being debugged, there’s a way around that. This works for me:

void pause ()   {
system ("pause");
}
int main (int argc, char ** argv)   {
    //  If "launched", then don't let the console close at the end until the
    //  user has seen the report.
    //  (See the MSDN ConGUI sample code)
    //
    do {
        HANDLE hConsoleOutput = ::GetStdHandle (STD_OUTPUT_HANDLE);
        if (INVALID_HANDLE_VALUE == hConsoleOutput) break;
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        if (0 == ::GetConsoleScreenBufferInfo (hConsoleOutput, &csbi))  break;
        if (0 != csbi.dwCursorPosition.X)   break;
        if (0 != csbi.dwCursorPosition.Y)   break;
        if (csbi.dwSize.X <= 0) break;
        if (csbi.dwSize.Y <= 0) break;
        atexit (pause);
        } while (0);

I just paste this code into each new console app I’m writing. If the program is being run from a command window, the cursor position won’t be <0,0> and it won’t call atexit(). If it has been launched from you debugger (any debugger) the console cursor position will be <0,0> and the atexit() call will be executed.

I got the idea from a sample program that used to be in the MSDN library, but I think it’s been deleted.

Edit addition: The Microsft VS implementation of the system() routine requires the COMSPEC environment variable to identify the command line interpreter. If this environment variable gets messed up — for example, if you’ve got a problem in the Visual Studio project’s debugging properties so that the environment variables aren’t properly passed down when the program is launched — then it will just fail silently.

Questions:
Answers:

In Boost.Test there is –auto_start_dbg parameter for breaking into debugger when test fails (on exception or on assertion failure). For some reason it doesn’t work for me.

See http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/usage-recommendations/dot-net-specific.html

For this reason i have create my custom test_observer that will break into the debugger when there is an assertion failure or an exception. This is enabled on debug builds when we are running under a debugger.

In one of the source files of my unit test exe I have added this code:

#ifdef _DEBUG
#include <boost/test/framework.hpp>
#include <boost/test/test_observer.hpp>

struct BoostUnitTestCrtBreakpointInDebug: boost::unit_test::test_observer
{
BoostUnitTestCrtBreakpointInDebug()
{
    boost::unit_test::framework::register_observer(*this);
}

virtual ~BoostUnitTestCrtBreakpointInDebug()
{
    boost::unit_test::framework::deregister_observer(*this);
}

virtual void    assertion_result( bool passed /* passed */ )
{
    if (!passed)
        BreakIfInDebugger();
}

virtual void    exception_caught( boost::execution_exception const& )
{
    BreakIfInDebugger();
}

void BreakIfInDebugger()
{
    if (IsDebuggerPresent())
    {
        /**
         * Hello, I know you are here staring at the debugger :)
         *
         * If you got here then there is exception in your unit test code.
         * Walk the call stack to find the actual cause.
         */
        _CrtDbgBreak();
    }
}
};

BOOST_GLOBAL_FIXTURE(BoostUnitTestCrtBreakpointInDebug);

#endif

Questions:
Answers:

It would actually be more effort, but you could just build in VS.Net, run it from the regular command line (cmd.exe), and then attach to the process after it starts running. This is probably not the solution you are looking for however.

Questions:
Answers:

I would use a “wait”-command in a specific time (milliseconds) of your own
choice. Application executes until the line you want to inspect, and then continues
after the time expired.

Include the <time.h> header

clock_t wait;

wait = clock();
while (clock() <= (wait + 5000));  // Wait for 5 seconds and then continue
wait=0;

Questions:
Answers:

What about just using a logging library, like log4net, and have it log to a file appender?

Questions:
Answers:

You could also setup your executable as an external tool, and mark the tool for Use output window. That way the output of the tool will be visible within Visual Studio itself, not a separate window.

Questions:
Answers:

I start the app with F11 and get a breakpoint somewhere in unit_test_main.ipp (can be assembly code). I use shift-f11 (Step out) to run the unit test and get the next assembly instruction in the CRT (normally in mainCRTStartup()). I use F9 to set a breakpoint at that instruction.

On the next invocation, I can start the app with F5 and the app will break after running the tests, therefore giving me a chance to peek at the console window

Questions:
Answers:

Or you could use boost_test “Test Log Output.”

http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/user-guide/test-output/test-log.html

Then it won’t matter whether the console window shows up at all AND your build logging can preserve the unit testing output as an artifact for examination on failed builds…

Questions:
Answers:

Adding the following line will do a simple MS-DOS pause displaying no message.

system("pause >nul | set /p \"=\"");

And there is no need to Ctrl+F5 (which will make your application run in Release Mode)

Questions:
Answers:

Do a readline at the end, (Its the “forma cochina”, like we say in Colombia, but it works)

static void Main(string[] args)
{
            .
            .
            .
            String temp = Console.ReadLine();
}