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

  1. /*****************************************************************************/
  2. /* File:                                             IBM INTERNAL USE ONLY   */
  3. /*   makefp.c                                                                */
  4. /*                                                                           */
  5. /* Description:                                                              */
  6. /*                                                                           */
  7. /*   Make a view.                                                            */
  8. /*                                                                           */
  9. /* History:                                                                  */
  10. /*                                                                           */
  11. /*   02/21/96 Modified.                                                      */
  12. /*                                                                           */
  13. /*****************************************************************************/
  14.  
  15. #define INCL_16
  16. #include "all.h"
  17.  
  18. extern PROCESS_NODE *pnode;
  19.  
  20. /*****************************************************************************/
  21. /* makefp                                                                    */
  22. /*                                                                           */
  23. /* Description:                                                              */
  24. /*                                                                           */
  25. /*   Creates an AFILE block for the specified module.                        */
  26. /*                                                                           */
  27. /* Parameters:                                                               */
  28. /*                                                                           */
  29. /*   mid         the module id we're making this view for.                   */
  30. /*   instaddr    build the view around this address.                         */
  31. /*   fname       z-string filename from GetFile.                             */
  32. /*                                                                           */
  33. /* Return:                                                                   */
  34. /*                                                                           */
  35. /*   AFILE *fp   -> to afile view structure.                                 */
  36. /*                                                                           */
  37. /* Assumptions:                                                              */
  38. /*                                                                           */
  39. /*****************************************************************************/
  40. AFILE *makefp(DEBFILE *pdf, ULONG mid, ULONG instaddr, UCHAR *fname)
  41. {
  42.  uchar    *cp = NULL;
  43.  uint      segbase;
  44.  uchar    *srcbuf;
  45.  uint      Nlines;
  46.  uint      Tlines;
  47.  SEL       srcsel;
  48.  SEL       offsel;
  49.  
  50.  ushort    srcseglen;
  51.  ushort   *offtab;
  52.  AFILE    *fp;
  53.  int       ftype;
  54.  int       OffsetTableBufSize;
  55.  char      FileNameBuffer[CCHMAXPATH];
  56.  char      FileSpecBuffer[CCHMAXPATH];
  57.  UCHAR     FileNameLength;
  58.  LNOTAB   *pLnoTabEntry;
  59.  int       sfi;
  60.  MODULE   *pModule;
  61.  CSECT    *pCsect;
  62.  UCHAR    *pFileName;
  63.  UCHAR    *fn;
  64.  BOOL      found;
  65.  ULONG     junk;
  66.  int       lno;
  67.  
  68.  if( fname )
  69.  {
  70.   /***************************************************************************/
  71.   /* - if fname is specified, then the view is to be build using the source  */
  72.   /*   file name supplied by the user as in the case of getfile.             */
  73.   /* - we need to find an sfi to associate with the file.                    */
  74.   /* - build a length-prefixed z-string from the supplied name.              */
  75.   /***************************************************************************/
  76.   fn         = strrchr(fname, '\\');
  77.   fn         = (fn) ? (fn + 1) : fname ;
  78.   pFileName  = Talloc( strlen(fn) + 2 );
  79.   *pFileName = (UCHAR)strlen(fn);
  80.   strcpy( pFileName+1, fn );
  81.  
  82.   found = FALSE;
  83.   for( sfi=mid=0, pdf = pnode->ExeStruct; pdf != NULL; pdf=pdf->next )
  84.   {
  85.    mid = MapSourceFileToMidSfi( pFileName, &sfi, pdf );
  86.    if( mid && sfi )
  87.    {
  88.     found = TRUE;
  89.     memset(FileNameBuffer, 0, sizeof(FileNameBuffer) );
  90.     strcpy(FileNameBuffer+1, fname );
  91.     FileNameBuffer[0] = (UCHAR)strlen(fname);
  92.     break;
  93.    }
  94.   }
  95.  
  96.   Tfree( pFileName );
  97.  
  98.   if( found == FALSE )
  99.    return(NULL);
  100.  
  101.   instaddr = DBMapLno(mid, 0, sfi, &junk , pdf );
  102.   DBMapInstAddr(instaddr, &pLnoTabEntry, pdf);
  103.   lno = 0;
  104.   if( pLnoTabEntry )
  105.    lno = pLnoTabEntry->lno;
  106.  }
  107.  else
  108.  {
  109.   /***************************************************************************/
  110.   /* - we're going to build the view from an instruction address.            */
  111.   /***************************************************************************/
  112.   if ( pdf->SrcOrAsm == SOURCE_LEVEL )
  113.   {
  114.    mid = DBMapInstAddr(instaddr, &pLnoTabEntry, pdf);
  115.    lno = sfi = 0;
  116.    if( pLnoTabEntry )
  117.    {
  118.     lno = pLnoTabEntry->lno;
  119.     sfi = pLnoTabEntry->sfi;
  120.    }
  121.    if( (pLnoTabEntry != NULL) && (sfi != 0) )
  122.    {
  123.     memset(FileNameBuffer, 0, sizeof(FileNameBuffer) );
  124.     cp = GetFileName( mid, sfi );
  125.     if( cp )
  126.      strcpy(FileNameBuffer, cp);
  127.    }
  128.   }
  129.  }
  130.  
  131.  if(ftype)
  132.  {
  133.   findsrc(FileNameBuffer+1,FileSpecBuffer+1,sizeof(FileSpecBuffer) );
  134.   FileSpecBuffer[0] = (UCHAR)strlen(FileSpecBuffer+1);
  135.   memcpy(FileNameBuffer,FileSpecBuffer,sizeof(FileSpecBuffer));
  136.  }
  137.  
  138.  FileNameLength = FileNameBuffer[0];
  139.  
  140.  fp = (AFILE*) Talloc(SizeOfAFILE(FileNameLength));
  141.  memcpy( fp->filename, FileNameBuffer, FileNameLength+1);
  142.  
  143.  /****************************************************************************/
  144.  /* - allocate 64k for the source buffer.                                    */
  145.  /* - allocate 20k for the offset buffer.                                    */
  146.  /* - load source file into a buffer and define:                             */
  147.  /*    - srcsel    =  source buffer selector.                                */
  148.  /*    - offsel    =  offset buffer selector.                                */
  149.  /*    - 0         =  number of lines to skip at the beginning of the file.  */
  150.  /*    - srcseglen =  source buffer bytes actually used by the load.         */
  151.  /*    - Nlines    =  number of source lines in the buffer.                  */
  152.  /*    - Tlines    =  number of source lines in the entire file.             */
  153.  /* - reallocate the source buffer to size really needed.                    */
  154.  /****************************************************************************/
  155.  Nlines = srcsel = offsel = 0;
  156.  if( !DosAllocSeg(0,(PSEL)&srcsel,0) &&
  157.      !DosAllocSeg(20*1024,(PSEL)&offsel, 0) )
  158.  {
  159.   LoadSource( fp->filename+1, (UCHAR *)Sel2Flat(srcsel),
  160.              (USHORT *)Sel2Flat(offsel), 0, &srcseglen, &Nlines, &Tlines);
  161.   if( Nlines )
  162.    DosReallocSeg(srcseglen, srcsel);
  163.  }
  164.  
  165.  /****************************************************************************/
  166.  /* - now, define the view structure.                                        */
  167.  /****************************************************************************/
  168.  fp->shower   = showA;
  169.  fp->pdf      = pdf;
  170.  fp->mid      = mid;
  171.  fp->sfi      = sfi;
  172.  fp->mseg     = segbase;
  173.  fp->Tlines   = Tlines;                 /* number of lines in file.          */
  174.  fp->Nlines   = Nlines;                 /* number of lines in source buffer. */
  175.  fp->Nbias    = 0;                      /* number of lines skipped in source.*/
  176.  fp->topline  = 1;                      /* init to 1st line in the file.     */
  177.  fp->csrline  = lno;                    /*  " "                              */
  178.  fp->hotline  = lno;                    /*  " "                              */
  179.  fp->hotaddr  = instaddr;
  180.  fp->skipcols = 0;                      /* columns skipped on left.          */
  181.  fp->Nshown   = 0;
  182.  fp->topoff   = instaddr;
  183.  fp->csr.row  = 0;
  184.  fp->csr.col  = 0;
  185.  fp->csr.mode = CSR_NORMAL;
  186.  fp->sview    = NOSRC;                  /* assume no source disassembler view*/
  187.  fp->flags    = ASM_VIEW_NEW;
  188.  
  189.  if( Nlines )
  190.  {
  191.   srcbuf = (uchar*)Sel2Flat(srcsel);
  192.   fp->source = srcbuf;
  193.  
  194.   /**************************************************************************/
  195.   /* Allocate the offtab[] buffer to hold Nlines + 1 offsets. We add the 1  */
  196.   /* so that we can make the offset table indices line up with the          */
  197.   /* source line numbers.                                                   */
  198.   /**************************************************************************/
  199.   OffsetTableBufSize = (Nlines+1)*sizeof(USHORT);
  200.   offtab             = (USHORT*) Talloc(OffsetTableBufSize);
  201.  
  202.   memcpy(offtab + 1, (uchar*)Sel2Flat(offsel), Nlines*sizeof(USHORT) );
  203.  
  204.   fp->offtab = offtab;
  205.   fp->flags  = 0;                      /* clear asm flag.                   */
  206.  
  207.   if( Tlines > Nlines )                /* does compressed source exceed 64k?*/
  208.    fp->flags |= AF_HUGE;               /* mark afile with huge file flag    */
  209.  
  210.   fp->shower = showC;                  /*  display source                   */
  211.   fp->sview  = MIXEDN;                 /* assume no source disassembler view*/
  212.  
  213. /*
  214.   Flag all text lines for which a (line #, offset) pair exists.
  215. */
  216.  
  217.   pModule = GetModuleWithAddr( instaddr, pdf );
  218.   if( pModule )
  219.   {
  220.    for(pCsect = pModule->pCsects; pCsect != NULL; pCsect=pCsect->next )
  221.    {
  222.     int NumEntries;
  223.  
  224.     NumEntries   = pCsect->NumEntries;
  225.     pLnoTabEntry = pCsect->pLnoTab;
  226.  
  227.     if( (pLnoTabEntry != NULL) && ( NumEntries > 0 ) )
  228.     {
  229.      for( ; NumEntries; pLnoTabEntry++, NumEntries-- )
  230.      {
  231.       if( pLnoTabEntry->sfi == sfi )
  232.       {
  233.        int lno;
  234.  
  235.        lno = pLnoTabEntry->lno;
  236.        if( (lno != 0) && (lno <= Nlines) )
  237.        {
  238.         *(srcbuf + offtab[lno] - 1) |= LINE_OK;
  239.        }
  240.       }
  241.      }
  242.     }
  243.    }
  244.   }
  245.   MarkLineBRKs( fp );                   /* mark the active breakpoints       */
  246.  }
  247.  
  248.  if( offsel )                           /* if there was an offset segment    */
  249.   DosFreeSeg(offsel);                   /* allocated then free it up         */
  250.  if( !Nlines && srcsel )                /* if there was no source then       */
  251.   DosFreeSeg(srcsel);                   /* free up the temp source buffer    */
  252.  
  253.  return( fp );
  254. }
  255.  
  256.     AFILE *
  257. fakefp(uint base,uint span,DEBFILE *pdf)
  258. {
  259.     uint n, mlen;
  260.     uchar modname[CCHMAXPATH];
  261.     AFILE *fp;
  262.  
  263.     strcpy(modname,pdf->DebFilePtr->fn);
  264.  
  265.     mlen = sprintf( modname, "Code Segment %04X (%04X thru %04X)",
  266.             SelOf(base), OffOf(base), OffOf(base) + (uint)span - 1 );
  267.     n = SizeOfAFILE(mlen);
  268.     fp = (AFILE *) Talloc(n);
  269.     fp->pdf = pdf;
  270.     fp->mseg = base;
  271.     fp->topoff=base;                  /* off of base for top disasm disp     */
  272.     fp->mid = FAKEMID;
  273.     fp->shower = showA;
  274.     memcpy( fp->filename + 1, modname,
  275.            (fp->filename[0] = ( uchar )mlen) + 1 );
  276.  
  277.     return( fp );
  278. }
  279.