Just the call stack, please

delphi package - automated exception handling

Just the call stack, please

Postby pledgestar » Wed Aug 10, 2016 5:21 pm

Hi there,

I have an application with no user interface and code to catch unhandled exceptions like this:

Code: Select all
  try
    // main application logic
    ...
  except       
    on E: Exception Do
    begin
      ExceptionLog.WriteLn(Format('Exception: %s, %s', [E.ClassName, E.Message]));

      // I would like to write the call stack here like:
      // ExceptionLog.WriteLine(E.StackTrace);
      ...
    end;


I have an ExceptionLog class that the handler currently uses to write the exception's class name and text. I'd like to add a stack trace as shown in the code comment above.

I'm reading through the documentation, but I'm getting bogged down by all the IDE configuration options. It seems like overkill for my use case.

Can I avoid the IDE settings wizard and just add your unit and some code? If so, can you give an example?

Thanks,

Steve
pledgestar
 
Posts: 4
Joined: Wed Aug 10, 2016 4:57 pm

Re: Just the call stack, please

Postby madshi » Wed Aug 10, 2016 5:46 pm

Getting a callstack with function names, line numbers and unit names won't work if you just add the unit. Why? Because all that information is not included in your EXE by default. The madExcept IDE wizard extracts all that information automatically for you from either the map file or the debug information, zips it up and adds it to your EXE's resource section. This way creating nice callstacks with function names and line numbers suddenly becomes possible.

You can forget about all the settings, if you don't plan to use the majority of madExcept's features, anyway. You can even 99% disable madExcept's automatic exception hooking and all that clever stuff (although it's really nice), by just checking the option "activate madExcept" and "link in function names and line numbers", and by unchecking "link in madExcept code" and "link in madExcept settings". If you do that, no code is added to your EXE, just the function names and line numbers.

Next step, add madExcept.pas to your uses clause somewhere. Caution: If you add it to your dpr's uses clause, the madExcept IDE wizard will automatically remove it again, because you unchecked "link in madExcept code". You can avoid this problem by either adding it to some other unit's uses clause, or by adding the comment "// dontTouchUses" above the dpr's "uses" clause.

Now in order to get a callstack in that try..except block, you can use the following function, exported by madExcept.pas:

http://help.madshi.net/madExceptUnit.ht ... StackTrace

You can even call it without any parameters, and it will provide you with a nicely formatted callstack for the current exception.

Please note that all the above works very well for x86. But if your EXE is compiled as 64bit, things get slightly more complicated because in the moment when you reached the try..except block, parts of the information we need to create the callstack are already overwritten. So if you want all this to work in x64, you may have to fully enable madExcept (practically that means you have to activate "link in madExcept code" and "link in madExcept settings". Once you do that, madExcept will hook into the depths of the RTL to make everything work nicely for you. Even if you don't need all the fancy stuff, it usually shouldn't harm.
madshi
Site Admin
 
Posts: 9431
Joined: Sun Mar 21, 2004 5:25 pm

Re: Just the call stack, please

Postby pledgestar » Wed Aug 10, 2016 7:59 pm

Thanks, that makes it easier for me to wrap my head around.

So, I just need to check the "enable madExcept" box and "link in function names and line numbers" boxes, leaving everything else unchecked.

Would I have to have done any of that if I just made sure the executable had a map file?

In this case is, madexceptpatch.exe still required to be run against the executable?

Steve
pledgestar
 
Posts: 4
Joined: Wed Aug 10, 2016 4:57 pm

Re: Just the call stack, please

Postby madshi » Wed Aug 10, 2016 8:13 pm

pledgestar wrote:Would I have to have done any of that if I just made sure the executable had a map file?

Come again? That sentence doesn't make a lot of sense to me?

pledgestar wrote:In this case is, madexceptpatch.exe still required to be run against the executable?

The madExceptPatch.exe is needed after command line compiling (or MSBuild) to store the map file into the EXE file. If you compile with the IDE, all that is done automatically.

Alternatively, you can ship the map file with your EXE, then you don't need to enable madExcept at all in the IDE madExcept settings. However, personally I think having to ship a MAP file is not very nice.
madshi
Site Admin
 
Posts: 9431
Joined: Sun Mar 21, 2004 5:25 pm

Re: Just the call stack, please

Postby pledgestar » Wed Aug 10, 2016 8:56 pm

I apologize for the confusion.

Do I need to configure the madExcept settings at all in the IDE as long as I ensure that a map file gets generated for my executable. Or put another way, in my use case, what does configuring the madExcept settings in the IDE do besides setting the linker option to generate a map file?

I hope that's clearer.

Thanks,

Steve
pledgestar
 
Posts: 4
Joined: Wed Aug 10, 2016 4:57 pm

Re: Just the call stack, please

Postby madshi » Wed Aug 10, 2016 9:04 pm

Depends on whether you compile in the IDE or if you use command line compiling, and on whether you want to distribute the map file with your exe, or whether you want to have it included in the EXE's resource section instead.
madshi
Site Admin
 
Posts: 9431
Joined: Sun Mar 21, 2004 5:25 pm

Re: Just the call stack, please

Postby pledgestar » Wed Aug 10, 2016 9:30 pm

The application runs on the development machine. Sometimes it gets compiled in the IDE and sometimes from the command line.

Since I'm not shipping the application, it's fine that the map file is separate.

So... Do I need to configure the madExcept settings at all in the IDE as long as I ensure that a map file gets generated for my executable. Or put another way, in my use case, what does configuring the madExcept settings in the IDE do besides setting the linker option to generate a map file?

Thanks for your patience.

Steve
pledgestar
 
Posts: 4
Joined: Wed Aug 10, 2016 4:57 pm

Re: Just the call stack, please

Postby madshi » Wed Aug 10, 2016 10:56 pm

If you don't want/need the MAP file information to be included in your EXE file, so if you don't have any problem distributing the MAP file with your EXE file, then there's no need to activate madExcept in the IDE settings at all. At least not for x86 EXEs.
madshi
Site Admin
 
Posts: 9431
Joined: Sun Mar 21, 2004 5:25 pm


Return to madExcept

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron