home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / d / desklib / !DeskSrc / FN / Libraries / BackTrace / c0 / backtrace next >
Encoding:
Text File  |  1996-05-10  |  1.9 KB  |  80 lines

  1. /*** backtrace.c ***/
  2. /* Generate a stack backtrace
  3.  * (c) Paul Field 1995
  4.  * Based closely on code by Tom Hughes
  5.  */
  6.  
  7. #include <assert.h>
  8. #include <stdio.h>
  9.  
  10. #include "Desk.BackTrace.h"
  11.  
  12. #include "Defs.h"
  13.  
  14.  
  15.  
  16. void    Desk_BackTrace_OutputToStdErr( void)
  17. {
  18. Desk_BackTrace_OutputToStreamWithPrefix( stderr, "");
  19. }
  20.  
  21.  
  22.  
  23.  
  24. void    Desk_BackTrace_OutputToStreamWithPrefix( FILE* stream, const char* prefix)
  25. {
  26.     _kernel_unwindblock frame;
  27.     char    *language;
  28.     
  29.     fprintf( stream, "%sStack-dump:\n", prefix);
  30.     
  31.     Desk_Backtrace_SupportCurrentFrame( &frame);
  32.     
  33.     while ( _kernel_unwind( &frame, &language) > 0)
  34.         {
  35.         
  36.         Desk_function_name_info    *Desk_name_info;
  37.         unsigned int        *Desk_save_code_pointer;
  38.         unsigned int        *Desk_frame_create_instruction;
  39.         unsigned int        *fp;
  40.         unsigned int        Desk_test_words;
  41.         
  42.         fp = (unsigned int*) (frame.fp & PCMask);
  43.         
  44.         if (fp != NULL)    {
  45.             
  46.             Desk_save_code_pointer        = (unsigned *)(*fp & PCMask);
  47.             Desk_frame_create_instruction    = Desk_save_code_pointer - SaveCodePointerOffset;
  48.             
  49.             /* Search backwards from the frame creation instruction looking for a 'name info' word */
  50.             Desk_name_info = (Desk_function_name_info *)(Desk_frame_create_instruction-1);
  51.             
  52.             for (Desk_test_words = NameInfoSearchWordLimit; Desk_name_info->Desk_ff_code != 0xff && Desk_test_words > 0; Desk_test_words--)
  53.                 {
  54.                 Desk_name_info--;
  55.                 }
  56.             
  57.             /* If we found the name info word we can print the name, otherwise the function is anonymous */
  58.             /*
  59.             if (Desk_name_info->Desk_ff_code == 0xff)
  60.             { fputs((char *)Desk_name_info - Desk_name_info->length, stderr);
  61.             fputc('\n', stderr);
  62.             }
  63.             else
  64.             { fputs("<anonymous function>\n", stderr);
  65.             }
  66.             */
  67.             fprintf( stream, "%s%s    (language is %s)\n",
  68.                 prefix,
  69.                 (Desk_name_info->Desk_ff_code == 0xff) ?
  70.                     (char *)Desk_name_info - Desk_name_info->length
  71.                     :
  72.                     "<anonymous function>",
  73.                 (language) ? language : "<unknown language>"
  74.                 );
  75.             }
  76.         }
  77.     
  78.     fprintf( stream, "%s\n", prefix);
  79.     }
  80.