home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / sd386v50.zip / sd386src.zip / FINDFP.C < prev    next >
Text File  |  1996-03-14  |  19KB  |  386 lines

  1. /*****************************************************************************/
  2. /* File:                                                                     */
  3. /*                                                                           */
  4. /*   findfp.c                                                                */
  5. /*                                                                           */
  6. /* Description:                                                              */
  7. /*                                                                           */
  8. /*   Find/build a view.                                                      */
  9. /*                                                                           */
  10. /* History:                                                                  */
  11. /*                                                                           */
  12. /*****************************************************************************/
  13.  
  14. #include "all.h"
  15.  
  16. /**Externs********************************************************************/
  17.  
  18. extern uint        LinesPer;
  19. extern uint        ExprAddr;            /* Set by ParseExpr               101*/
  20. extern uint        ProcessID;
  21. extern CmdParms    cmd;                 /* pointer to CmdParms structure  701*/
  22. extern PROCESS_NODE *pnode;                                             /*827*/
  23.  
  24. AFILE *allfps;
  25. /*****************************************************************************/
  26. /* findfp                                                                    */
  27. /*                                                                           */
  28. /* Description:                                                              */
  29. /*   Find the AFILE structure containing an address.                         */
  30. /*   If it doesn't exist then make one.                                      */
  31. /*                                                                           */
  32. /* Parameters:                                                               */
  33. /*   instaddr   instruction address.                                         */
  34. /*                                                                           */
  35. /* Return:                                                                   */
  36. /*   fp         pointer to the AFILE structure.                              */
  37. /*   NULL       did not find a pointer.                                      */
  38. /*                                                                           */
  39. /*****************************************************************************/
  40. AFILE *findfp( ULONG instaddr )
  41. {
  42.  AFILE   *fp;
  43.  AFILE   *fplast;
  44.  ULONG    mid;
  45.  ULONG    lno;
  46.  DEBFILE *pdf;
  47.  LNOTAB  *pLnoTabEntry;
  48.  int      sfi;
  49.  
  50.  /****************************************************************************/
  51.  /* - define the context where we landed.                                    */
  52.  /****************************************************************************/
  53.  pdf = FindExeOrDllWithAddr(instaddr);
  54.  if( pdf == NULL )
  55.   return(NULL);
  56.  
  57.  lno = sfi = 0;
  58.  mid = DBMapInstAddr( instaddr, &pLnoTabEntry, pdf);
  59.  
  60.  /****************************************************************************/
  61.  /* - if the instruction address maps to hyperspace, then make a fake        */
  62.  /*   view.                                                                  */
  63.  /****************************************************************************/
  64.  if( mid == 0 )
  65.  {
  66.   ULONG segbase = 0;
  67.   ULONG segspan = 0;
  68.  
  69.   segbase = DBLsegInfo( instaddr, &segspan, pdf );
  70.   if( (segbase==0) || (segspan==0) )
  71.    return(NULL);
  72.  
  73.   for( fplast=&allfps, fp=allfps; fp != NULL; fplast=fp, fp=fp->next ){;}
  74.  
  75.   fplast->next = fp = fakefp( segbase, segspan, pdf);
  76.  }
  77.  
  78.  if( pLnoTabEntry )
  79.  {
  80.   lno = pLnoTabEntry->lno;
  81.   sfi = pLnoTabEntry->sfi;
  82.  }
  83.  /****************************************************************************/
  84.  /* - scan the views and see if we already have a view for this module.      */
  85.  /****************************************************************************/
  86.  for( fp = allfps; fp != NULL; fp = fp-> next )
  87.  {
  88.   if( (fp->mid == mid) && (fp->sfi == sfi) )
  89.   {
  90.    pagefp( fp, lno );
  91.  
  92.    fp->hotline = lno - fp->Nbias;
  93.    fp->hotaddr = instaddr;
  94.    return( fp );
  95.   }
  96.  }
  97.  
  98.  /****************************************************************************/
  99.  /* - if we don't have a view, then make one and add it to the ring.         */
  100.  /****************************************************************************/
  101.  if( fp == NULL )
  102.  {
  103.   for( fplast=&allfps, fp=allfps; fp != NULL; fplast=fp, fp=fp->next ){;}
  104.  
  105.   fplast->next = fp = makefp( pdf, mid, instaddr, NULL );
  106.  
  107.   pagefp( fp, lno );
  108.   fp->hotline = lno - fp->Nbias;
  109.   fp->hotaddr = instaddr;
  110.   return(fp);
  111.  }
  112.  
  113. #if 0
  114. /*****************************************************************************/
  115. /* The first thing we want to do is establish pdf,mid, and lno. The instaddr */
  116. /* was probably generated by a csip from a single-step or go. In this case,  */
  117. /* we can use the exec values to establish pdf, mid, and lno. If this is not */
  118. /* the case, then we have to find pdf, mid, and lno.                         */
  119. /*                                                                           */
  120. /*****************************************************************************/
  121.  
  122.     pdf=FindExeOrDllWithAddr(instaddr);                                 /*901*/
  123.     if( instaddr==GetExecAddr())
  124.     {
  125.      mid = GetExecMid();
  126.      lno = GetExecLno();
  127.     }
  128.     else                                /* else,                             */
  129.     {
  130.      if(pdf != NULL )                   /*                                101*/
  131.       mid=DBMapInstAddr(instaddr,&lno,pdf); /* find the  mid and lno.        */
  132.     }
  133.  
  134.     if(pdf)
  135.      segbase = DBLsegInfo(instaddr, &segspan , pdf);
  136.     if( pdf && (DEBFILE*)segbase != NULL )                              /*107*/
  137.     {
  138.         if( mid ){
  139.             /* Note:  The "next" field must be the 1st field in
  140.              * the AFILE struct for the following code to work.
  141.              */
  142.  
  143.             fp      = allfps ;
  144.             fplast  = (AFILE *) &allfps ;
  145.             for( ; fp != NULL; fp = (fplast = fp) -> next )
  146.             {
  147.               if( fp->mid == mid )
  148.               {
  149.                 DBMapInstAddr(instaddr,&junk,pdf); /* remove x=.          521*/
  150.                                         /* a dummy call just to find the  115*/
  151.                                         /* value of flag_src_or_asm       115*/
  152.                 if ( flag_asm_or_src == 1 )
  153.                 {
  154.                   /***********************************************************/
  155.                   /* if line not found change to asm view.                115*/
  156.                   /***********************************************************/
  157.                   fp->shower = showA;
  158.                 }
  159.                 goto ReturnFp;
  160.               }
  161.             }
  162.             fplast->next = fp = makefp( mid, NULL );                    /*505*/
  163.  
  164.         ReturnFp:
  165.             pagefp( fp, lno );
  166.             fp->hotline = lno - fp->Nbias;
  167.         ReturnAny:
  168.             fp->hotaddr = instaddr;          /*                            101*/
  169.             return( fp );
  170.         }
  171.         /* Note:  The "next" field must be the 1st field in
  172.          * the AFILE struct for the following code to work.
  173.          */
  174.         fp      = allfps ;
  175.         fplast  = (AFILE *) &allfps ;
  176.         for( ; fp != NULL; fp = (fplast = fp) -> next )
  177.             if( !fp->mid )
  178.                 goto ReturnAny;
  179.         fplast->next = fp = fakefp(segbase, segspan, pdf);              /*107*/
  180.         goto ReturnAny;
  181.     }
  182.     return(NULL);
  183. #endif
  184. }                                       /* end findfp().                     */
  185.  
  186.  
  187. /*****************************************************************************/
  188. /* DropOrMakeFakefp                                                       216*/
  189. /*                                                                        216*/
  190. /* Description:                                                           216*/
  191. /*   try to find a suitable afile for the given address and span.         216*/
  192. /*   If not found create one and return.                                  216*/
  193. /*                                                                        216*/
  194. /* Parameters:                                                            216*/
  195. /*   base       module base address.                                      216*/
  196. /*   span       length of the mdoule.                                     216*/
  197. /*   pdf        pointer to debug file.                                    216*/
  198. /*                                                                        216*/
  199. /* Return:                                                                216*/
  200. /*                                                                        216*/
  201. /*****************************************************************************/
  202.  void
  203. DropOrMakeFakefp(uint base, uint span, DEBFILE *pdf)                                               /*107*/
  204.  
  205. {
  206.   AFILE        *fp;                     /* pointer to an afile            216*/
  207.   AFILE        *fplast;                 /* temp pointer to an afile       216*/
  208.   AFILE        *fpold;                  /* -> old afile held for disposal 216*/
  209.                                         /*                                216*/
  210. /*****************************************************************************/
  211. /* First check to see if this afile exists from a prior event. If it does 216*/
  212. /* not then we build one.  If it does then we check to see if it's still  216*/
  213. /* valid then return.  If it is no longer valid then build a new one and  216*/
  214. /* dispose of the old one.                                                216*/
  215. /*****************************************************************************/
  216.  
  217.   fplast = ( AFILE *)&allfps;           /* pointer to the head node       216*/
  218.   for( fp = allfps;                     /* scan for afile with mid=FAKEMID216*/
  219.        fp != NULL &&                    /* scan if the ring is established216*/
  220.        fp->mid !=FAKEMID;               /* and  while mid != FAKEMID and  216*/
  221.        fp = (fplast = fp)->next         /* next afile in ring             216*/
  222.      ){;}                               /*                                216*/
  223.                                         /*                                216*/
  224.   if ((fp != NULL) && (fp->mid == FAKEMID)) /* found afile with fake mid? 216*/
  225.   {                                     /*                                216*/
  226.    if ( (GetExecAddr() >= fp->mseg) && (GetExecAddr() <= span) )
  227.                                         /* still ok from last time?       216*/
  228.     return;                             /* return                         216*/
  229.    fpold = fp;                          /* if it's not good then hold ->to it*/
  230.    fp = fakefp(base,span,pdf);          /* build a new one                216*/
  231.    fp->mid  = FAKEMID;                  /* give it mid id                 216*/
  232.    fp->next = fpold->next;              /* put it in the ring             216*/
  233.    fplast->next = fp;                   /*                                216*/
  234.    Tfree((void*)fpold);                  /* release the old memory     521 216*/
  235.    fp->hotaddr = GetExecAddr();         /* set current address            216*/
  236.    fp->sview  = NOSRC;                  /* assume no source disassembler view*/
  237.   }                                     /*                                   */
  238. /*****************************************************************************/
  239. /* At this point we have not found an afile with a fake mid.                 */
  240. /*****************************************************************************/
  241.   fp = fakefp(base,span,pdf);            /* build a new one               216*/
  242.   fp->next = NULL;                      /* and put it in the ring         216*/
  243.   fp->mid  = FAKEMID;                   /* give it mid id                 216*/
  244.   if ( fplast )                         /* if the ring is not empty then  216*/
  245.   {                                     /*                                216*/
  246.    fplast->next = fp;                   /* put it in the ring             216*/
  247.   }                                     /*                                216*/
  248.   else                                  /*                                216*/
  249.   {                                     /*                                216*/
  250.    allfps = fp;                         /* start the ring                 216*/
  251.   }                                     /*                                216*/
  252.   fp->hotaddr = GetExecAddr();          /* set current address            216*/
  253.   fp->topoff = base;                    /* set top offset of display      107*/
  254.   fp->flags=AF_FAKE|AF_ZOOM;            /* set appropriate flags          216*/
  255.   fp->sview  = NOSRC;                   /* assume no source disassembler view*/
  256.  }                                      /* end of dropormakefakefp()         */
  257.  
  258.  
  259.  
  260.  
  261. /*****************************************************************************/
  262. /* FindFuncOrAddr                                                            */
  263. /*                                                                           */
  264. /* Description:                                                              */
  265. /*                                                                           */
  266. /*   Find the AFILE structure containing a function name or address.         */
  267. /*                                                                           */
  268. /* Parameters:                                                               */
  269. /*                                                                           */
  270. /*   pFunctionNameOrAddr  -> to function name or address string.             */
  271. /*   IsAddr               TRUE => Address.                                   */
  272. /*                        FALSE=> Function name.                             */
  273. /*                                                                           */
  274. /* Return:                                                                   */
  275. /*                                                                           */
  276. /*   fp         pointer to the AFILE structure.                              */
  277. /*   NULL       did not find a pointer.                                      */
  278. /*                                                                           */
  279. /*****************************************************************************/
  280. AFILE *FindFuncOrAddr( uchar *pFunctionNameOrAddr , int IsAddr )
  281. {
  282.  AFILE   *fp;
  283.  ULONG    line = 0;
  284.  uint     n;
  285.  uint     addr;
  286.  uchar   *cp;
  287.  uchar    uname[MAXSYM+2];
  288.  DEBFILE *pdf;
  289.  LNOTAB  *pLnoTabEntry;
  290.  
  291.  cp   = NULL;
  292.  addr = NULL;
  293.  if( IsAddr )
  294.  {
  295.   /***************************************************************************/
  296.   /* Handle Addresses.                                                       */
  297.   /***************************************************************************/
  298.   cp = ParseExpr(pFunctionNameOrAddr,2,0,0,0);
  299.   if( cp && ( *cp=='\0') )
  300.   {
  301.    addr = ExprAddr;
  302.    pdf  = FindExeOrDllWithAddr(addr);
  303.   }
  304.  }
  305.  else
  306.  {
  307.   /***************************************************************************/
  308.   /* Handle function names.                                                  */
  309.   /*                                                                         */
  310.   /* - FindFuncOrAddr will receive the name without the "_". So build the    */
  311.   /*   name with the "_" and make in an ASCII Z string.                      */
  312.   /* - scan the list of pdfs looking through the public sections.            */
  313.   /* - look for the "_"+name followed by a search for the name.              */
  314.   /*                                                                         */
  315.   /***************************************************************************/
  316.   n = strlen(pFunctionNameOrAddr);
  317.   if( n > sizeof(uname)-2 )
  318.     n = sizeof(uname)-2;
  319.  
  320.   memcpy( uname+1, pFunctionNameOrAddr, n );
  321.   uname[0] = '_';
  322.   uname[n+1] = 0;
  323.  
  324.   pdf = pnode->ExeStruct;
  325.   for( ; pdf; pdf=pdf->next )
  326.   {
  327.    {
  328.     addr=DBPub(uname,pdf);
  329.     if( addr == NULL)
  330.     {
  331.      addr=DBPub(pFunctionNameOrAddr,pdf);
  332.     }
  333.     if( addr != NULL)
  334.      break;
  335.    }
  336.   }
  337.  }
  338.  
  339.  /****************************************************************************/
  340.  /* If can't find addr and pdf, then can't build a view.                     */
  341.  /****************************************************************************/
  342.  if( (addr == NULL) || (pdf == NULL) )
  343.   return(NULL);
  344.  
  345. /*****************************************************************************/
  346. /* At this point, we have an addr and a pdf. Now build a view.               */
  347. /*****************************************************************************/
  348.  fp = findfp( addr );
  349.  if( fp )
  350.  {
  351.   if( fp->source)
  352.   {
  353.    DBMapInstAddr( addr, &pLnoTabEntry, pdf);
  354.    if( pLnoTabEntry ) line = pLnoTabEntry->lno;
  355.    line -= fp->Nbias;                   /* removed a '+1'.                234*/
  356.    if( (line  >  0) &&                  /* removed an '='.                234*/
  357.        (line <= (int)(fp->Nlines))      /* add '=' to make '<='           234*/
  358.      )                                  /*                                   */
  359.    {                                    /*                                   */
  360.     fp->csrline = line;                 /* put the cursor on the line and    */
  361.     if( (int)(fp->topline=line-LinesPer/2) < 1 ) /* make it show up in the304*/
  362.      fp->topline = 1;                            /* middle of the page.   234*/
  363.    }                                    /* end "if we have source info"      */
  364.   }                                     /*                                   */
  365.   else
  366.    fp->topoff = addr;
  367.  }
  368.  return( fp );
  369. }
  370.  
  371.  
  372.     AFILE *
  373. mid2fp(uint  mid )
  374. {
  375.     AFILE *fp;                          /* was register.                  112*/
  376.  
  377.     /* Note:  The "next" field must be the 1st field in
  378.      * the AFILE struct for the following code to work.
  379.      */
  380.  
  381.     for(fp = allfps; fp != NULL; fp = fp->next )
  382.         if( fp->mid == mid )
  383.             break;
  384.     return( fp );
  385. }
  386.