There is a C library onexit() handler, and you could add a call there.
Is this app MFC or raw Win32? If MFC, there is a virtual method of the
CWinApp-derived class where you could set a breakpoint,
CYourApp::ExitInstance, and if it doesn’t exist, set a breakpoint at
CWinApp::ExitInstance.
Look also for calls to PostQuitMessage. This is often used to shut down
secondary threads that have a message pump, and if accidentally executed
in the main thread, will cause the app to shut down.
If non-MFC, find the message pump, and set a breakpoint after it; this
will tell you if you have exited the main thread. Look for where that
WFMO call is being done, and see what it is waiting on; this can tell you
a lot.
The problem with projects like this, that “just growed”, is that someone
decides to cause a thread exit when function X is called, thinking it is
never called from the main thread. Perhaps at the time that decision was
made, it was true. Then a later programmer spots this function, says
“Hey, this does everything I need!” and calls it from the main thread, not
noticing it also shuts down its calling thread. Oopsie. I’ve seen one
case where a WFMO was waiting on the calling thread’s handle, so it
couldn’t proceed to exit until it already exited.
A breakpoint on the C library onexit() call will reveal function addresses
that are to be called when the app exits. Putting breakpoints on one of
these may allow you to trace back to who might be the guilty party.
If it is MFC, then the secondary threads will also have ExitInstance
methods. Otherwise, you need to find the secondary thread message pump(s)
and breakpoint those after the GetMessage loop.
How are the threads being created? If the CreateThread API is used
dihrectly, all bets are off; the world is screwed up from the start. If
it is pure Win32, it should be calling the C library _beginthreadex call,
otherwise te C library is not properly initialized for the thread. Also,
if CreateThread is called directly it is possible to link without the C
multithreaded library, and you are definitely hosed. If it is MFC, you
/must/ call AfxBeginThread to create threads; otherwise, the per-thread
state for te MFC library is hosed. _beginthread can be dangerous because
it automatically closes the thread handle on thread termination, which
makes WFMO on thread handles impossible.
Which reminds me: that hung call on WFMO–where was it called in the
source? Knowing the path that gets you to the caller’s routine can tell
you a lot.
I spent most of my time in user space, and for 15 years was a C++/MFC MVP,
so I actually know many of these tricks for debugging. My apologies to
the rest of the readers for this lengthy OT answer. We can take this
offline here; email me at xxxxx@flounder.com.
joe
Apologies for a user-mode question; I seem to be spending far too much of
my
time debugging UM apps these days.
I’ve got a very large program on my hands that has been (and still is)
developed by many people over the years, some of whom are dead and many
retired. So I can’t “ask the guy that wrote it” – he is dead, retired, or
both. The program uses lots of MS process and threading interfaces, C++
libraries and stuff, and raw calls to C library functions. And a bunch of
DLLs, some of which are 3rd party.
The program runs along just fine until memory gets tight. Then (with the
debugger attached) instead of having 50+ threads doing different things, I
suddenly have one completely arbitrary thread hanging in
WaitForMultipleObjects or the like. Obviously someone called ExitProcess
or
some similar function on some thread (not this one). Somewhere. There is
no
debug output that could be helpful.
I’ve done the usual text searches for exit-anything and put breakpoints or
debug stuff or disabled them, and I’m still not catching who is exiting. I
suspect someone in a DLL or maybe even in ntdll, but I can’t prove that,
yet.
Anyone know how I can catch who the bad guy is that is blowing out the
program? Unfortunately I CANNOT use kernel debugging on this test system,
so
it will have to be some UM trick to catch it.
Thanks, and apologies for talking about UM stuff. You guys know more about
UM than most of the UM experts floating around.
Loren
NTDEV is sponsored by OSR
OSR is HIRING!! See http://www.osr.com/careers
For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars
To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer