home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / d / desklib / !DeskSrc / FN / Libraries / BackTrace / c / GetFns < prev    next >
Encoding:
Text File  |  1996-10-31  |  2.9 KB  |  117 lines

  1. #include "Desk.Debug.h"
  2. #include "Desk.Error.h"
  3.  
  4. #include "Desk.BackTrace.h"
  5.  
  6. #include "Defs.h"
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. typedef struct    {
  14.     char*        fnname;
  15.     unsigned int*    Desk_save_instruction;
  16.     unsigned int*    Desk_frame_create_instruction;
  17.     unsigned int*    fp;
  18.     }
  19.     Desk_BackTrace_frameblock;
  20.  
  21.  
  22.  
  23.  
  24. static void    Desk_BackTrace_GetFrameInfo( Desk_BackTrace_frameblock* frameblock, const _kernel_unwindblock* frame)
  25.     {
  26.     Desk_function_name_info    *Desk_name_info;
  27.     unsigned int        Desk_test_words;
  28.     
  29.     frameblock->fnname            = NULL;
  30.     frameblock->Desk_save_instruction    = NULL;
  31.     frameblock->Desk_frame_create_instruction    = NULL;
  32.     
  33.     frameblock->fp = (unsigned int*) (frame->fp & PCMask);
  34.     
  35.     if ( frameblock->fp)    {
  36.         
  37.         frameblock->Desk_save_instruction        = (unsigned int*) (*(frameblock->fp) & PCMask);
  38.         
  39.         if ( frameblock->Desk_save_instruction)    {
  40.             frameblock->Desk_frame_create_instruction    = frameblock->Desk_save_instruction - SaveCodePointerOffset;
  41.             
  42.             /* Search backwards from the frame creation instruction looking for a 'name info' word */
  43.             Desk_name_info = (Desk_function_name_info *)(frameblock->Desk_frame_create_instruction-1);
  44.             
  45.             for ( Desk_test_words = NameInfoSearchWordLimit; Desk_name_info->Desk_ff_code != 0xff && Desk_test_words > 0; Desk_test_words--)
  46.                 {
  47.                 Desk_name_info--;
  48.                 }
  49.             
  50.             if ( Desk_name_info->Desk_ff_code == 0xff)
  51.                 frameblock->fnname = (char*) Desk_name_info - Desk_name_info->length;
  52.             else    frameblock->fnname = NULL;
  53.             }
  54.         
  55.         
  56.         }
  57.     }
  58.  
  59.  
  60. const char*    Desk_BackTrace_GetFunctionName( const void* ptr)
  61. {
  62. Desk_function_name_info*    fn = (Desk_function_name_info*) ptr;
  63. int            i;
  64.  
  65. Desk_Debug2_Printf( "Desk_BackTrace_GetFunctionName( 0x%p)\n", ptr);
  66.  
  67. for ( i=0; i<5; i++, fn--)    {
  68.     
  69.     Desk_Debug2_Printf( "fn->Desk_ff_code = %i, fn->length = %i\n",
  70.         fn->Desk_ff_code, fn->length
  71.         );
  72.         
  73.     if ( fn->Desk_ff_code == 0xff && fn->length<4096)    {
  74.         const char*    c = (const char*) fn - fn->length;
  75.         Desk_Debug2_Printf( "Desk_BackTrace_GetFunctionName returning 0x%p '%s'\n",
  76.             c, c
  77.             );
  78.         return c;
  79.         }
  80.     }
  81.  
  82. Desk_Debug2_Printf( "Desk_BackTrace_GetFunctionName returning NULL\n");
  83. return NULL;
  84. }
  85.  
  86.  
  87.  
  88. const Desk_BackTrace_functionlist*    Desk_BackTrace_GetCurrentFunctions( void)
  89.     {
  90.     static Desk_BackTrace_functionlist    functionlist;
  91.     static unsigned int*            functions[ Desk_BackTrace_MAXFNS];
  92.     
  93.     _kernel_unwindblock        frame;
  94.     Desk_BackTrace_frameblock    frameblock;
  95.     char                *language;
  96.     /*int            n;*/
  97.     
  98.     Desk_BackTrace_SupportCurrentFrame( &frame);
  99.     
  100.     functionlist.n = 0;
  101.     functionlist.functions    = functions;
  102.     
  103.     while ( _kernel_unwind( &frame, &language) > 0)    {
  104.         if ( frame.fp)    {
  105.             Desk_BackTrace_GetFrameInfo( &frameblock, &frame);
  106.             if ( !frameblock.Desk_save_instruction)    break;
  107.             
  108.             functions[ functionlist.n] = frameblock.Desk_frame_create_instruction;
  109.             Desk_Debug2_Printf( Desk_error_PLACE "0x%p\n", functions[ functionlist.n]);
  110.             functionlist.n++ ;
  111.             if ( functionlist.n >= Desk_BackTrace_MAXFNS)    break;
  112.             }
  113.         }
  114.     
  115.     return &functionlist;
  116.     }
  117.