home *** CD-ROM | disk | FTP | other *** search
-
- ontrap() & trapcode() for the SAS C(tm) 5.10a developement language.
- Written by John V. Pope, Turlock CA.
- Plink John*Pope (Sec 11, The Developers' Den)
- CIS 71566,537
-
- The supplied c.o module and related functions improves the multitasking
- friendliness of any program linked with the modified c.o.
-
- The changes simply add an EXCEPTION handler to the programs you link
- with the supplied c.o. The ontrap handler puts up a system requester on
- the workbench screen (similar to break) to inform the user that the
- program hit a TRAP, then exits without a GURU.
-
- That's right, NO GURU!!!
-
- In addition, a simple call to the ontrap function passing a pointer
- to a replacement trap handler can cause a custom trap handler to be
- invoked when the program causes an exception. The custom trap handler
- can close files and explain to the user what happened. Then free all
- allocated memory, close windows, ANYTHING you want done before the code
- is unloaded by AmigaDOS.
-
- The default trap handler cxtrap and code added to c.o and _main
- is about 450 bytes. A very small price to pay for code that won't
- cause a GURU for a simple bug in a program.
-
- You might be asking, "If it's so simple, why isn't an EXECPTION
- HANDLER built into AmigaDOS?". One IS!! It's the one that put's up
- a system requester saying "Software Failure!!!, System task held".
- That's how our favorite computer handles stray EXCEPTIONS.
-
- The technical reason my code works is this, I didn't really have
- to re-invent the wheel. Just add the spokes. The Amiga OS is very
- complex. Exec has the job of signaling task's (or process's) of sevaral
- things. Messages, software traps and EXCEPTIONS to name a few.
- When a task (or process) is added to the system by AmigaDOS, the task
- structure is initialized with some default information. One field,
- tc_TrapCode, is initialize with an address. The address of the code
- that puts "Software Failure ..." on the workbench screen. That is the
- address that is invoked in the event of an exception. All I had to do
- was give it another location to report to and have that code handle an
- EXCEPTION interrupt. No sweat! (I can say that now after lots of GURU's).
-
- One important bit of information to keep in mind. The trap handler
- should NOT call exit(), _exit() or Exit(). The function MUST return.
- I am actually calling the traphandler function set up by the user as
- a subroutine to the c.o routines.
-
- I am calling the user defined trap function from a point in c.o
- PRIOR to the onexit trap. BEWARE!! If you have set an onexit trap to
- free memeory, it will NOT be neccesary to free the memory from inside
- the user defined trap function. You'll know this happened if you get
- a FreeTwice error from exec.
-
- As much as I would like for this enhancement to be a fix all for all
- of the GURU problems in the Amiga, it's not. Some ROM functions call the
- GURU (Alert) directly. This comepletly bypasses the Exec exception
- handler, thus my trap handler.
-
- One should also know that GOMF appears to set up an exception handler
- in the hardware vector thus bypassing the Exec exception handler. All
- my good intentions and all your cleanup code will be completly ignored!
-
- Happy coding!!!
-
- John V. Pope
-
- ---------------------------------------------------------------------------
- NAME
- ontrap() Plant EXECPTION handler.
-
- SYNOPSIS
- ontrap(function);
-
- int (*function)(); Pointer to the function to be called.
-
- function The address of the user supplied trap handler.
-
- Function can be 0, this will serve to use the c.o routines to call
- the cxtrap requester. Thus removing any previously set functions. The
- program will terminate with the return of 1000+exception value.
-
- The external variable _TRAPCODE contains the trap code that is placed
- on the supervisor stack by the MC680x0 exception interupt routine in c.o.
-
- The external variable _ONTRAP recieves the function address. If this
- variable is zero, no custom or default trap handler is called. The
- program just terminates quietly with 1000+exception in register D0.
-
- RETURNS
- NONE.
-
- NOTES
- The c.o module can be used with or without the ontrap functions.
-
- ---------------------------------------------------------------------------
- NAME
- trapcode() Extract the MC68000 exception code.
-
- SYNOPSIS
- code = trapcode(void);
-
- int code;
-
- This function supplies a very simple method of extracting the
- exception value that the exception handler code in c.o recieves.
-
- ---------------------------------------------------------------------------
-
- MC68000 EXCEPTION CODE SUMMARY
-
- 2 Bus error
- 3 Address error (odd address)
- 4 Illegal instruction
- 5 Zero divide
- 6 CHK instruction (check register bounds)
- 7 TRAPV instruction (arithmetic overflow trap)
- 8 Priviledge violation
- 9 Trace interrupt
- 10 Line 1010 emulator ($Axxx instructions)
- 11 Line 1111 emulator ($Fxxx instructions)
- 37-47 Trap instructions
-
- ---------------------------------------------------------------------------
-
- EXAMPLES NOTE: These are just examples. Don't try to compile them.
-
- /* EXAMPLE. Using ontrap and trapcode */
- /* To avoid alot of declaration hassles, put this function ABOVE main() */
- int
- traphandler()
- {
- extern char *_ProgramName;
-
- printf("%s hit trap code %d\n",_ProgramName,trapcode());
- clean_up_allocated_memory();
- return(0);
- }
-
- void
- main()
- {
-
- ontrap(traphandler);
- ProneToGuru();
- clean_up_allocated_memory();
- }
-
-
- OR you can just do this ...
-
- /* EXAMPLE. Using cxtrap requester only */
- main()
- {
- ontrap(0); /* just put up a requester and exit, don't bother with cleanup */
-
- ProneToGuru();
- clean_up_allocated_memory();
- }
-
- This is yet another alternative ...
-
- /* EXAMPLE. Not using trapcode */
- int
- traphandler()
- {
- extern char *_ProgramName;
- extern long _TRAPCODE; /* exception code from the supervisor stack */
-
- printf("%s hit trap code %ld\n",_ProgramName,_TRAPCODE);
- clean_up_allocated_memory();
- return(0);
- }
-
- void
- main()
- {
-
- ontrap(traphandler);
- ProneToGuru();
- clean_up_allocated_memory();
- }
-
- Then again, this will also work ...
-
- NOTE: Only the new c.o file is required for this one to work.
-
- /* EXAMPLE. Not using main, ontrap, or trapcode */
- int
- traphandler()
- {
- extern char *_ProgramName;
- extern long _TRAPCODE;
-
- printf("%s hit trap code %ld\n",_ProgramName,_TRAPCODE);
- clean_up_allocated_memory();
- return(0);
- }
-
- extern int (*_ONTRAP)();
-
- _main()
- {
- _ONTRAP = traphandler;
-
- ProneToGuru();
- clean_up_allocated_memory();
- }
-
-
- ********************************************************************
- ********************************************************************
-
- Highlights of changes to ontrap for Lattice C 4.01
-
- ********************************************************************
- ********************************************************************
-
- 1. You don't need trap.lib or trapnb.lib as in 4.0. The oml program now
- handles indexed libraries, the ontrap function, cxtrap and the modified
- umain.o can actually be placed in lattice libraries.
-
- 2. The c.o routines handle 16 or 32 bit int modes a bit better.
-
- 3. cxtrap (the default trap handler) is available in both 16 and 32 bit
- linkable versions.
-
- 4. A non-GURU exit is ALWAYS taken by the exception handler. You may
- lose some memory though.
-
- 5 1000 is added to the value placed on the supervisor stack by the
- MC68000 exception handler. This values is returned to AmigaDOS by c.o
- in register D0 upon exiting.
-
- 6. The trapcode() funcion is supplied for no nonsense exception code access.
-
- 7. The user supplied trap handling routine is no longer passed the exception
- code. I did this because the 16 bit mode of the compiler pulls only
- the upper half of the long word exception value from the stack.
- Instead, the exception value is available in the external longword
- _TRAPCODE or the trapcode function. See the examples above.
-
-
-
-
- ********************************************************************
- ********************************************************************
-
- Highlights of changes to ontrap for Lattice C 5.04
-
- ********************************************************************
- ********************************************************************
-
- 1. Simple update to correctly install in the 5.04 environment.
- No functional changes result.
- 2. As a result of 1 above, cres.o is now installed.
-
-
-
-
- ********************************************************************
- ********************************************************************
-
- Highlights of changes to ontrap for Lattice C 5.05
-
- ********************************************************************
- ********************************************************************
-
- 1. Simple update to correctly install in the 5.05 environment.
- No functional changes result.
-
-
-
-
- ********************************************************************
- ********************************************************************
-
- Highlights of changes to ontrap for SAS C 5.10a
-
- ********************************************************************
- ********************************************************************
-
- 1. Simple update to correctly install in the 5.10a environment.
- No functional changes result.
-