home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / d / desklib / !DeskSrc / FN / Libraries / BackTrace / c0 / GetFns < prev   
Encoding:
Text File  |  1996-05-10  |  2.5 KB  |  105 lines

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