6

I'm having some difficulties determining what is causing a process to exit. I have a breakpoint in some shutdown code that I'm debugging, but, after breaking in the debugger at the breakpoint and stepping once, the whole process exits immediately. Every thread reports an exit code of -1 in the output window. There are a large number of threads in the process at that time, and the code base is quite large, making searching for the culprit difficult.

I've tried installing a std::atexit function, but this doesn't get hit. I've also tried overriding SetUnhandledExceptionFilter, in case it is caused by a crash, and it also doesn't get hit. The project has exceptions disabled (#define _HAS_EXCEPTIONS=0), so I cannot call std::set_terminate or std::set_unexpected.

Is there some other way to determine what is causing the process to exit? Some option to break in the debugger when the process is about to terminate?

MuertoExcobito
  • 9,741
  • 2
  • 37
  • 78
  • I'm not very expert...Debugger step by step not help? If not help image the sequence of operation in time, use printf("1") printf ("2") in the code ecc for identify where is the code not executed – RosLuP Sep 14 '17 at 14:01
  • 1
    Exception disabled by project settings (C/C++->Code Generation) not by `#define`. So `Debug->Windows->Exception Settings` could be useful. – KonstantinL Sep 14 '17 at 14:05
  • or add a try{}catch{...} in your main and then even without debugger settings you can see if it's an uncaught exception with a break point. – UKMonkey Sep 14 '17 at 14:12
  • @UKMonkey - there is already a `__try/__finally` block inside `main`, and the `__finally` block doesn't get hit. – MuertoExcobito Sep 14 '17 at 14:24
  • "atexit" is only run on normal termination; so I'm not surprised that isn't getting called. I would look into adding some signal handlers http://en.cppreference.com/w/cpp/utility/program/signal so that you can at least track what's going on. – UKMonkey Sep 14 '17 at 14:35
  • Similar case: https://stackoverflow.com/a/28323007/17034 – Hans Passant Sep 14 '17 at 15:39
  • @HansPassant - based on that answer, my machine is about to go out the window :). Tried breakpoints on ExitProcess, TerminateProcess, NtTerminateProcess (the context specific one also didn't work for me), and _exit. The breakpoints window all says they are valid, but none get hit after stepping. – MuertoExcobito Sep 14 '17 at 16:16
  • @UKMonkey - added a signal handler for 1..NSIG, but the signal handler doesn't get hit either. – MuertoExcobito Sep 14 '17 at 16:21
  • My last guesses are that you're building something in release mode or there's some non-matching debug information... after that, I'm all out of ideas :)# – UKMonkey Sep 14 '17 at 16:55
  • Building debug, and as I said, the breakpoints say they match in the debug window, which wouldn't happen if the debug information didn't match. – MuertoExcobito Sep 14 '17 at 17:23

2 Answers2

9

Run your app with debugger and read the debug output. If the app terminates because C++ exceptions, or SEH, you’ll read it in the output window.

If you’ll see nothing interesting there, it means your app called ExitProcess/ExitThread/exit, or worse, TerminateProcess/TerminateThread/_exit.

You can put breakpoints on these. Set a breakpoint at startup, launch debugger. Ensure you have debug symbols loaded for relevant DLLs, kernel32.dll for ExitProcess and friends, some other DLL for exit, e.g. ucrtbase.dll. Press “New / Function breakpoint” in the Breakpoints window, type e.g. “ExitProcess”, press OK.

David Norman
  • 19,396
  • 12
  • 64
  • 54
Soonts
  • 20,079
  • 9
  • 57
  • 130
0

You can also try using gflags tool from Windows SDK.

If you’ll find (by reading Windows Logs > Application) the reason was self exit, you can check “Enable dump collection” in gflags, then you’ll be able to load the dump in WinDBG and get the complete call stack telling you who called what.

Unfortunately, the latest version of the tool is broken beyond repair.

But you can install older Windows SDK. You only need “Debugging tools for Windows” from there, no need to install the complete SDK.

Soonts
  • 20,079
  • 9
  • 57
  • 130