[comp.lang.c] How to Trap Runtime System Error under MSC?

MHARRIS@G.BBN.COM (01/14/88)

Can anyone tell me how to trap Microsoft C "serious runtime errors", e.g.,
"Error 2000 Stack Overflow", before they print to the terminal screen?
I want to deal with them myself in the code that evoked the offending
.EXE file (with system()).  Thanks.

-- Michael Harris     MHarris@G.BBN.COM     (617) 873-3794

Alan_T._Cote.OsbuSouth@Xerox.COM (01/15/88)

Michael Harris asks,

>Can anyone tell me how to trap Microsoft C "serious runtime errors", e.g.,
>"Error 2000 Stack Overflow", before they print to the terminal screen?

The answer to this is somewhat complex.

Microsoft C uses a subroutine called "__chkstk" to allocate space for
temporaries on the stack.  This routine also checks to determine whether the SP
register would be decremented below its lowest safe value, and prints the cited
error message if it is.  Thus, there is no direct way to "trap" this in the
parent program.

There is, however, light at the end of this tunnel.  You could rewrite __chkstk
(use MASM, of course) to provide the kind of communication between the child and
the parent that you want, and rebuild your child program(s) using the __chkstk
object module to override inclusion of the Microsoft version of the routine.
Don't forget that the code in the parent routine must initialize the
miscellaneous registers and flags that MSC expects in its runtime environment,
if it's going to use any C code.  Don't, under any circumstances, enter the
parent program's trap routine directly in C.

I grant that this solution requires a rather clear understanding of the nature
of __chkstk and the MSC runtime environment, but that's life.

	- Al

farren@gethen.UUCP (Michael J. Farren) (01/16/88)

In article <11263@brl-adm.ARPA> MHARRIS@G.BBN.COM writes:
>Can anyone tell me how to trap Microsoft C "serious runtime errors", e.g.,
>"Error 2000 Stack Overflow", before they print to the terminal screen?
>I want to deal with them myself in the code that evoked the offending
>.EXE file (with system()).  Thanks.

Without hacking the source code to the runtime package, you can't, for
the most part.  The 'Stack Overflow' messages are a special case, as
they are produced by a routine called, I believe, _chkstk, and you 
could easily replace this routine with one of your own.  The thing
to keep in mind, though, is that the error detection embedded in the
runtime package is designed to allow the code to gracefully recover
from disastrous errors, under which there would be no guarantee that
YOUR code would execute properly, if at all.  Stack checking, for
example, is invoked on entry to a function, and simply determines if
there is enough room on the stack for allocation of all the auto
variables.  While you could write a replacement routine that 'grew'
the stack, you would have to ensure that that routine did not use
the existing stack, or you would be setting yourself up for a royal
fall.  Similar constraints apply for other runtime errors.

-- 
Michael J. Farren             | "INVESTIGATE your point of view, don't just 
{ucbvax, uunet, hoptoad}!     | dogmatize it!  Reflect on it and re-evaluate
        unisoft!gethen!farren | it.  You may want to change your mind someday."
gethen!farren@lll-winken.llnl.gov ----- Tom Reingold, from alt.flame 

cramer@optilink.UUCP (Clayton Cramer) (01/20/88)

> Michael Harris asks,
> 
> >Can anyone tell me how to trap Microsoft C "serious runtime errors", e.g.,
> >"Error 2000 Stack Overflow", before they print to the terminal screen?
> 
> The answer to this is somewhat complex.
> 
> Microsoft C uses a subroutine called "__chkstk" to allocate space for
> temporaries on the stack.  This routine also checks to determine whether the SP
> register would be decremented below its lowest safe value, and prints the cited
> error message if it is.  Thus, there is no direct way to "trap" this in the
> parent program.
> 
> There is, however, light at the end of this tunnel.  You could rewrite __chkstk
> (use MASM, of course) to provide the kind of communication between the child and
> the parent that you want, and rebuild your child program(s) using the __chkstk
> object module to override inclusion of the Microsoft version of the routine.
> 
> 	- Al

You might find out if the $150 Microsoft charges for runtime library
routine sources includes __chkstk.  If not, take a look at the disassembled
code from CodeView -- it's tiny.

Clayton E. Cramer

gary@apex.UUCP (Gary Wisniewski) (01/28/88)

In article <1854@optilink.UUCP> cramer@optilink.UUCP (Clayton Cramer) writes:
>> Michael Harris asks,
>> 
>> >Can anyone tell me how to trap Microsoft C "serious runtime errors", e.g.,
>> >"Error 2000 Stack Overflow", before they print to the terminal screen?
>> 
>
>You might find out if the $150 Microsoft charges for runtime library
>routine sources includes __chkstk.  If not, take a look at the disassembled
>code from CodeView -- it's tiny.
>
>Clayton E. Cramer

The MSC Startup Code diskette contains source to both the __chkstk routine
(in the chkstk.asm module) and to the error message generator.  You can
modify these and rebuild the startup code (CRT0.OBJ et.al.).  We rewrote
it all to take advantage of EMS, etc.

BY THE WAY!  The MSC runtime source code DOES not include floating point
and compiler "glue" modules like $i8_foo_bar you may have seen in the
maps or while debugging ... just the documented stuff is included ...
we were depressed when ours arrived.

-- 
Gary J. Wisniewski				Apex Software Corporation
{allegra,bellcore,cadre}!pitt!darth!apex!gary	Phone: (412) 681-4343