Error Checking in C

utzoo!decvax!minow utzoo!decvax!minow
Sun Oct 31 22:32:40 AEST 1982

Greg (hao!woods) asked for some form of traceback.  I implemented this
as an option to Decus C.  It should be fairly simple to add to Unix
C's as well.  If the program was compiled with profiling, the function
name is available (as an ascii string) in the memory image.  If the program
aborts, the library routine that is run by the operating system
unwinds the call frame, using magical methods to locate the string.
It then prints out something like:
	[main process subroutine function 01234 foobar error]
where "01234" is the location of a routine that wasn't compiled with
profiling.  There are a huge number of sanity checks to unwind safely.

The routine also dumps registers and a few other things.

Having the function name string in memory offers some other advantages.
For example, I wrote the "caller()" function that returns a pointer to
the name of the routine that called the function calling caller().

	extern FILE	*$$flow;

	$$flow = stderr;
	$$flow = NULL;

Setting $$flow to a non-null FILE * causes the register save routine to
print the name of the function being invoked and one or two other values.

It is quite easy to add statement (or line) number tracing to C.
The compiler must generate one of two instructions at each line or

	mov	#number, at __nbrptr		Set current line number
or	inc	@__nbrptr.

At the entry to a function, a local trace block is built (and linked
into the caller's).  __nbrptr contains the address of the current trace block.
This technique has been available on Dec's PDP-11 Fortran compilers for
many years.

Martin Minow

More information about the Comp.lang.c mailing list