home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nasm097s.zip / OUTDBG.C < prev    next >
C/C++ Source or Header  |  1997-10-02  |  4KB  |  167 lines

  1. /* outdbg.c    output routines for the Netwide Assembler to produce
  2.  *        a debugging trace
  3.  *
  4.  * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
  5.  * Julian Hall. All rights reserved. The software is
  6.  * redistributable under the licence given in the file "Licence"
  7.  * distributed in the NASM archive.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <ctype.h>
  14.  
  15. #include "nasm.h"
  16. #include "nasmlib.h"
  17. #include "outform.h"
  18.  
  19. #ifdef OF_DBG
  20.  
  21. struct Section {
  22.     struct Section *next;
  23.     long number;
  24.     char *name;
  25. } *dbgsect;
  26.  
  27. FILE *dbgf;
  28. efunc dbgef;
  29.  
  30. static void dbg_init(FILE *fp, efunc errfunc, ldfunc ldef, evalfunc eval)
  31. {
  32.     dbgf = fp;
  33.     dbgef = errfunc;
  34.     dbgsect = NULL;
  35.     (void) ldef;
  36.     fprintf(fp,"NASM Output format debug dump\n");
  37. }
  38.  
  39. static void dbg_cleanup(void)
  40. {
  41.     while (dbgsect) {
  42.     struct Section *tmp = dbgsect;
  43.     dbgsect = dbgsect->next;
  44.     nasm_free (tmp->name);
  45.     nasm_free (tmp);
  46.     }
  47.     fclose(dbgf);
  48. }
  49.  
  50. static long dbg_section_names (char *name, int pass, int *bits)
  51. {
  52.     int seg;
  53.  
  54.     /*
  55.      * We must have an initial default: let's make it 16.
  56.      */
  57.     if (!name)
  58.     *bits = 16;
  59.  
  60.     if (!name)
  61.     fprintf(dbgf, "section_name on init: returning %d\n",
  62.         seg = seg_alloc());
  63.     else {
  64.     int n = strcspn(name, " \t");
  65.     char *sname = nasm_strndup(name, n);
  66.     struct Section *s;
  67.  
  68.     seg = NO_SEG;
  69.     for (s = dbgsect; s; s = s->next)
  70.         if (!strcmp(s->name, sname))
  71.         seg = s->number;
  72.     
  73.     if (seg == NO_SEG) {
  74.         s = nasm_malloc(sizeof(*s));
  75.         s->name = sname;
  76.         s->number = seg = seg_alloc();
  77.         s->next = dbgsect;
  78.         dbgsect = s;
  79.         fprintf(dbgf, "section_name %s (pass %d): returning %d\n",
  80.             name, pass, seg);
  81.     }
  82.     }
  83.     return seg;
  84. }
  85.  
  86. static void dbg_deflabel (char *name, long segment, long offset,
  87.               int is_global, char *special) {
  88.     fprintf(dbgf,"deflabel %s := %08lx:%08lx %s (%d)%s%s\n",
  89.         name, segment, offset,
  90.         is_global == 2 ? "common" : is_global ? "global" : "local",
  91.         is_global,
  92.         special ? ": " : "", special);
  93. }
  94.  
  95. static void dbg_out (long segto, void *data, unsigned long type,
  96.              long segment, long wrt) {
  97.     long realbytes = type & OUT_SIZMASK;
  98.     long ldata;
  99.     int id;
  100.  
  101.     type &= OUT_TYPMASK;
  102.  
  103.     fprintf(dbgf,"out to %lx, len = %ld: ",segto,realbytes);
  104.  
  105.     switch(type) {
  106.       case OUT_RESERVE:
  107.     fprintf(dbgf,"reserved.\n"); break;
  108.       case OUT_RAWDATA:
  109.     fprintf(dbgf,"raw data = ");
  110.     while (realbytes--) {
  111.         id = *(unsigned char *)data;
  112.         data = (char *)data + 1;
  113.         fprintf(dbgf,"%02x ",id);
  114.     }
  115.     fprintf(dbgf,"\n"); break;
  116.       case OUT_ADDRESS:
  117.     ldata = 0; /* placate gcc */
  118.     if (realbytes == 1)
  119.         ldata = *((char *)data);
  120.     else if (realbytes == 2)
  121.         ldata = *((short *)data);
  122.     else if (realbytes == 4)
  123.         ldata = *((long *)data);
  124.     fprintf(dbgf,"addr %08lx (seg %08lx, wrt %08lx)\n",ldata,
  125.         segment,wrt);break;
  126.       case OUT_REL2ADR:
  127.     fprintf(dbgf,"rel2adr %04x (seg %08lx)\n",(int)*(short *)data,segment);
  128.     break;
  129.       case OUT_REL4ADR:
  130.     fprintf(dbgf,"rel4adr %08lx (seg %08lx)\n",*(long *)data,segment);
  131.     break;
  132.       default:
  133.     fprintf(dbgf,"unknown\n");
  134.     break;
  135.     }
  136. }
  137.  
  138. static long dbg_segbase(long segment) {
  139.     return segment;
  140. }
  141.  
  142. static int dbg_directive (char *directive, char *value, int pass) {
  143.     fprintf(dbgf, "directive [%s] value [%s] (pass %d)\n",
  144.         directive, value, pass);
  145.     return 1;
  146. }
  147.  
  148. static void dbg_filename (char *inname, char *outname, efunc error) {
  149.     standard_extension (inname, outname, ".dbg", error);
  150. }
  151.  
  152. struct ofmt of_dbg = {
  153.     "Trace of all info passed to output stage",
  154.     "dbg",
  155.     NULL,
  156.     dbg_init,
  157.     dbg_out,
  158.     dbg_deflabel,
  159.     dbg_section_names,
  160.     dbg_segbase,
  161.     dbg_directive,
  162.     dbg_filename,
  163.     dbg_cleanup
  164. };
  165.  
  166. #endif /* OF_DBG */
  167.