buzzchurch.com Forum Index The Buzz Congregation
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Exceptions question

 
Post new topic   Reply to topic    buzzchurch.com Forum Index -> The Monastery
View previous topic :: View next topic  
Author Message
IX
Cardinal


Joined: 29 May 2005
Posts: 906
Location: mcr:uk

PostPosted: Tuesday July 20th, 2010 14:17    Post subject: Exceptions question Reply with quote

My knowledge and understanding of c++ exceptions is very poor so please forgive me if this is a stupid question. It probably is.

I want to try to add some kind of simple crash dump to a machine so that when it inevitably goes down in flames, I'll be able to see what led up to the crash. So how can I catch any unhandled exception in the machine dll so I can execute some code when something goes wrong? I can't wrap my machine in a try/catch (or can I?) so is it possible to have a catch-all exception handler or do I have to put try/catch statements absolutely everywhere?
_________________
Toshiba Satellite Pro L300-28W. Intel Core 2 Duo T5870 2GHz, 3GB ram, Windows 7 Home Premium 32 bit.
Back to top
View user's profile Send private message
oskari
yeah, seriously.


Joined: 08 Jun 2007
Posts: 410

PostPosted: Tuesday July 20th, 2010 14:41    Post subject: Reply with quote

You can do that but /nocr and a debugger should be easier.
Back to top
View user's profile Send private message Visit poster's website
IX
Cardinal


Joined: 29 May 2005
Posts: 906
Location: mcr:uk

PostPosted: Tuesday July 20th, 2010 15:10    Post subject: Reply with quote

oskari wrote:
You can do that...

Do you mean that it is possible to do what I want, or that I'd have to use lots of try/catch statements?

oskari wrote:
...but /nocr and a debugger should be easier.

That's what I normally do but sometimes it's hard/impossible to recreate a crash. I'm thinking that a crash dump/log file would make reproducing such elusive problems much easier.
_________________
Toshiba Satellite Pro L300-28W. Intel Core 2 Duo T5870 2GHz, 3GB ram, Windows 7 Home Premium 32 bit.
Back to top
View user's profile Send private message
btd
Cardinal


Joined: 13 Aug 2004
Posts: 779
Location: York, UK

PostPosted: Tuesday July 20th, 2010 15:52    Post subject: Reply with quote

I'm not convinced that catching exceptions will help you. Correct me if I'm wrong, but the exception generally only tells you what went wrong, not where or why. If you really must debug without a debugger, then OutputDebugString statements will probably be more helpful than try/catch blocks.

If you must wrap everything in try/catch blocks, you shouldn't need too many of them. If your machine crashes, it's probably going to do it in Tick() or Work().

OT: C++ exceptions are abused far more often than they're used properly. This makes me sad. And yes, wrapping code in a big try/catch to catch all exceptions counts as abuse.
Back to top
View user's profile Send private message Visit poster's website
oskari
yeah, seriously.


Joined: 08 Jun 2007
Posts: 410

PostPosted: Tuesday July 20th, 2010 16:31    Post subject: Reply with quote

Windows catches all exceptions your try/catch would anyway, so what's the point?
Back to top
View user's profile Send private message Visit poster's website
IX
Cardinal


Joined: 29 May 2005
Posts: 906
Location: mcr:uk

PostPosted: Tuesday July 20th, 2010 18:16    Post subject: Reply with quote

Please be tolerant, I'm only a drummer Smile

What I'm aiming to do is keep a log of the machine's state so that when one of those elusive bugs pops up and crashes the machine, I'll be able to see not only where it went wrong but more importantly what steps led to the failure. Armed with that information, I'll be able to easily recreate the problem catch it with the debugger.

So the idea is that I'll either output significant information into a file as the machine is running or store it in some kind of buffer. Then when a crash happens, I'll need to close any open file handles or dump the stored info to a file and clean up. I figured that this would probably involve catching exceptions but perhaps I'm wrong. If I was making a standalone executable I'd try putting a try/catch in main() but I don't know how that could be done for a dll, or even whether it's possible.

So in short, all I really want to do is detect when it all turns to shit and save some useful info that will help me reproduce the crash.

@btd: Is the try/catch in main() approach bad? Almost every tutorial I've seen does that.
_________________
Toshiba Satellite Pro L300-28W. Intel Core 2 Duo T5870 2GHz, 3GB ram, Windows 7 Home Premium 32 bit.
Back to top
View user's profile Send private message
oskari
yeah, seriously.


Joined: 08 Jun 2007
Posts: 410

PostPosted: Tuesday July 20th, 2010 19:09    Post subject: Reply with quote

This is what buzz does:

Code:

int ExFilter(int n_except, LPEXCEPTION_POINTERS ep)
{
        if (!g_CrashRecovery)
                return EXCEPTION_CONTINUE_SEARCH;

        memcpy(&exctx, ep->ContextRecord, sizeof(CONTEXT));
        return EXCEPTION_EXECUTE_HANDLER;
}

#define BEGIN_PLUGIN_CALL(p) __try {
#define END_PLUGIN_CALL(p) } __except(ExFilter(GetExceptionCode(), GetExceptionInformation())) { HandleException(p, GetExceptionCode()); }

BEGIN_PLUGIN_CALL(pmac)
pmac->pInterface->Work(...);
END_PLUGIN_CALL(pmac)



You could wrap Work and Tick the same way in your code.
Back to top
View user's profile Send private message Visit poster's website
btd
Cardinal


Joined: 13 Aug 2004
Posts: 779
Location: York, UK

PostPosted: Tuesday July 20th, 2010 19:43    Post subject: Reply with quote

I'm still not convinced that exception handling is the right tool for this job. If all you're worried about is having an open handle to your log file, just open and close the file each time you write a message to it (although I suspect this is a non-issue anyway, as Windows will close any open handles when buzz.exe exits). Or just use OutputDebugString and DebugView instead.

Also: many of these "hard to reproduce" bugs are caused by multithreading issues, or by writing past the end of a buffer into some other memory location. Neither of which is easy to debug, no matter how much logging you do.
Back to top
View user's profile Send private message Visit poster's website
IX
Cardinal


Joined: 29 May 2005
Posts: 906
Location: mcr:uk

PostPosted: Tuesday July 20th, 2010 20:06    Post subject: Reply with quote

oskari wrote:
This is what buzz does...

Thanks! I would never have thought of that.

btd wrote:
just open and close the file each time you write a message to it (although I suspect this is a non-issue anyway, as Windows will close any open handles when buzz.exe exits). Or just use OutputDebugString and DebugView instead.

Yeah, you're probably right. Thanks for the links. DebugView looks useful.

btd wrote:
Also: many of these "hard to reproduce" bugs are caused by multithreading issues, or by writing past the end of a buffer into some other memory location. Neither of which is easy to debug, no matter how much logging you do.

Stop trying to scare me! LOL
_________________
Toshiba Satellite Pro L300-28W. Intel Core 2 Duo T5870 2GHz, 3GB ram, Windows 7 Home Premium 32 bit.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    buzzchurch.com Forum Index -> The Monastery All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2002 phpBB Group