home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d01xx / d0128.lha / PrtDriver / debug.c < prev    next >
C/C++ Source or Header  |  1988-01-02  |  4KB  |  167 lines

  1. /*    DEBUGGING-- This is Matt Dillon's debugger code,
  2.  *    which is *real* handy when you're paranoid about what
  3.  *    you can and can't do because of your task/process state
  4.  *
  5.  *    It's even better than kprintf :-)
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/nodes.h>
  10. #include <exec/lists.h>
  11. #include <exec/memory.h>
  12. #include <devices/printer.h>
  13. #include <devices/prtbase.h>
  14.  
  15. #ifndef DEBUG
  16.  
  17. dbprintf() {}    /* empty function declaration */
  18.  
  19. #else
  20.  
  21. #define CTOB(x) (void *)(((long)(x))>>2)
  22.  
  23. struct Task *FindTask();
  24. struct MsgPort *CreatePort();
  25. struct FileHandle *Open();
  26. struct Message *GetMsg();
  27.  
  28.  
  29. struct MsgPort  *Dbport;    /*    owned by the debug process    */
  30. struct MsgPort  *Dback;        /*    owned by the DOS device driver    */
  31. short DBDisable;
  32. struct Message  DummyMsg;
  33.  
  34. void dbstart()
  35. {
  36.  
  37.     DBDisable = 0;
  38.     Dbport = Dback = NULL;
  39.  
  40.     /* need DosBase and SysBase to use debugger */
  41.     /*    DEBUGGING   */
  42.  
  43.     dbinit();    /* to close off debugger call dbuninit() */
  44. }
  45.  
  46. /*
  47.  *  DEBUGGING CODE.    You cannot make DOS library calls that access other
  48.  *  devices from within a DOS device driver because they use the same
  49.  *  message port as the driver.  If you need to make such calls you must
  50.  *  create a port and construct the DOS messages yourself.  I do not
  51.  *  do this.  To get debugging info out another PROCESS is created to which
  52.  *  debugging messages can be sent.
  53.  *
  54.  *  You want the priority of the debug process to be larger than the
  55.  *  priority of your DOS handler.  This is so if your DOS handler crashes
  56.  *  you have a better idea of where it died from the debugging messages
  57.  *  (remember that the two processes are asyncronous from each other).
  58.  */
  59.  
  60. extern void debugproc();
  61.  
  62. dbinit()
  63. {
  64.     struct Task *task = FindTask(NULL);
  65.  
  66.     Dback = CreatePort(NULL,NULL);
  67.     CreateProc("DEV_DB", task->tc_Node.ln_Pri+1, CTOB(debugproc), 4096);
  68.     WaitPort(Dback);                /* handshake startup    */
  69.     GetMsg(Dback);                /* remove dummy msg     */
  70.     dbprintf("Debugger running V1.00\n");
  71. }
  72.  
  73. dbuninit()
  74. {
  75.     struct Message killmsg;
  76.  
  77.     if (Dbport) {
  78.     killmsg.mn_Length = 0;        /*    0 means die        */
  79.     PutMsg(Dbport,&killmsg);
  80.     WaitPort(Dback);        /*    He's dead jim!      */
  81.     GetMsg(Dback);
  82.     DeletePort(Dback);
  83.  
  84.     /*
  85.      *  Since the debug process is running at a greater priority, I
  86.      *  am pretty sure that it is guarenteed to be completely removed
  87.      *  before this task gets control again.  Still, it doesn't hurt...
  88.      */
  89.  
  90.     /* Delay(50); */        /*    ensure he's dead    */
  91.     }
  92. }
  93.  
  94. dbprintf(a,b,c,d,e,f,g,h,i,j)
  95. {
  96.     static char buf[256];
  97.     struct Message *msg;
  98.  
  99.     if (Dbport && !DBDisable) {
  100.     sprintf(buf,a,b,c,d,e,f,g,h,i,j);
  101.     msg = AllocMem(sizeof(struct Message)+strlen(buf)+1,
  102.         MEMF_PUBLIC|MEMF_CLEAR);
  103.     msg->mn_Length = strlen(buf)+1;     /*    Length NEVER 0    */
  104.     strcpy(msg+1,buf);
  105.     PutMsg(Dbport,msg);
  106.     }
  107. }
  108.  
  109. /*
  110.  *  BTW, the DOS library used by debugmain() was actually openned by
  111.  *  the _Init routine.    Note: DummyMsg cannot be on debugmain()'s stack
  112.  *  since debugmain() goes away on the final handshake.
  113.  */
  114.  
  115. debugmain()
  116. {
  117.     
  118.     struct Message *msg;
  119.     short len;
  120.     void *fh;
  121.  
  122.     Dbport = CreatePort(NULL,NULL);
  123.     fh = Open("con:0/0/640/100/debugwindow", 1006);
  124.     PutMsg(Dback, &DummyMsg);
  125.     for (;;) {
  126.     WaitPort(Dbport);
  127.     msg = GetMsg(Dbport);
  128.     len = msg->mn_Length;
  129.     if (len == 0)
  130.         break;
  131.     --len;                /*    Fix length up    */
  132.     Write(fh, msg+1, len);
  133.     FreeMem(msg,sizeof(struct Message)+len+1);
  134.     }
  135.     Close(fh);
  136.     DeletePort(Dbport);
  137.     PutMsg(Dback,&DummyMsg);          /*  Kill handshake  */
  138. }
  139.  
  140. /*
  141.  *  The assembly tag for the DOS process:  CNOP causes alignment problems
  142.  *  with the Aztec assembler for some reason.  I assume then, that the
  143.  *  alignment is unknown.  Since the BCPL conversion basically zero's the
  144.  *  lower two bits of the address the actual code may start anywhere
  145.  *  within 8 bytes of address (remember the first longword is a segment
  146.  *  pointer and skipped).  Sigh....  (see CreatProc() above).
  147.  */
  148.  
  149. #asm
  150.     public    _debugproc
  151.     public    _debugmain
  152.  
  153.     cseg
  154. _debugproc:
  155.     nop
  156.     nop
  157.     nop
  158.     nop
  159.     nop
  160.     movem.l D2-D7/A2-A6,-(sp)
  161.     jsr    _debugmain
  162.     movem.l (sp)+,D2-D7/A2-A6
  163.     rts
  164. #endasm
  165.  
  166. #endif
  167.