error handling techniques?
Richard Harter
rh at smds.UUCP
Tue Nov 13 17:29:17 AEST 1990
In article <4246 at goanna.cs.rmit.oz.au>, ok at goanna.cs.rmit.oz.au (Richard A. O'Keefe) writes:
> In article <234 at smds.UUCP>, rh at smds.UUCP (Richard Harter) writes:
> > The code maintains a history of the last 128 function calls in a circular
> > buffer; this information is dumped in the error report.
> I wouldn't mind doing something like this, but how do you do it?
> Have you a set of macros to ease the job, or what?
> Is _every_ function call included, or only selected ones?
> I tend to write recursive code, the trouble with that is that
> when things go wrong all the calls in the buffer tend to be to
> the same function (or a small set of mutually recursive functions),
> have you found a good way around that?
Actually the implementation I use is brutally simple. I set up a global
array of char pointers and a global integer used as an index into the
array, e.g.
char *TR[128];
int TI;
An initialization routine zeroes out the array and sets TI=0; The standard
include file contains the macro
#define trace(foo) TR[TI--]=foo;if (TI<0) TI =127;TR[TI]=0
<Note: in this implementation TI is the next location to be filled>
The first statement in each routine (of interest) invokes trace with the
name of the routine, e.g.
trace("somefunc");
It's not terribly sophisticated, but it's very useful. As you note the
buffer can fill up with a few names if you are doing a lot of recursion
(or are calling a routine within a loop). A simple technique for dealing
with this (which I've never bothered to implement) is to add a count array
and increment the count if the pointers are equal. Something like the
following should work:
#define trace(foo) \
if (TR[TI] != foo) {\
TI--;\
if (TI<0) TI=127;\
TR[TI] = foo;\
TRCNT[TI]=1;\
}\
else TRCNT[TI]++
<Note 1: Meaning shift, TI is the index of the slot most recently filled.>
<Note 2: I haven't checked the above code.>
The error dump routine has the requisite code for going through the strings,
getting their lengths, and printing out the array in nice neat columns starting
at the right place.
--
Richard Harter, Software Maintenance and Development Systems, Inc.
Net address: jjmhome!smds!rh Phone: 508-369-7398
US Mail: SMDS Inc., PO Box 555, Concord MA 01742
This sentence no verb. This sentence short. This signature done.
More information about the Comp.lang.c
mailing list