home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mint96sb.zoo / src / debugold.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-05  |  6.9 KB  |  391 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith. All rights reserved.
  3. */
  4.  
  5. /* MiNT debugging output routines */
  6. /* also, ksprintf is put here, for lack of any better place to put it */
  7.  
  8. #include "mint.h"
  9. #include <stdarg.h>
  10.  
  11. static void VDEBUGOUT P_((char *, va_list));
  12.  
  13. /*
  14.  * ksprintf implements a very crude sprintf() function that provides only
  15.  * what MiNT needs...
  16.  *
  17.  * NOTE: this sprintf probably doesn't conform to any standard at
  18.  * all. It's only use in life is that it won't overflow fixed
  19.  * size buffers (i.e. it won't try to write more than SPRINTF_MAX
  20.  * characters into a string)
  21.  */
  22.  
  23. static int
  24. PUTC(p, c, cnt, width)
  25.     char *p;
  26.     int c;
  27.     int *cnt;
  28.     int width;
  29. {
  30.     int put = 1;
  31.  
  32.     if (*cnt <= 0) return 0;
  33.     *p++ = c;
  34.     *cnt -= 1;
  35.     while (*cnt > 0 && --width > 0) {
  36.         *p++ = ' ';
  37.         *cnt -= 1;
  38.         put++;
  39.     }
  40.     return put;
  41. }
  42.  
  43. static int
  44. PUTS(p, s, cnt, width)
  45.     char *p, *s;
  46.     int *cnt;
  47.     int width;
  48. {
  49.     int put = 0;
  50.  
  51.     while (*cnt > 0 && *s) {
  52.         *p++ = *s++;
  53.         put++;
  54.         *cnt -= 1;
  55.         width--;
  56.     }
  57.     while (width-- > 0 && *cnt > 0) {
  58.         *p++ = ' ';
  59.         put++;
  60.         *cnt -= 1;
  61.     }
  62.     return put;
  63. }
  64.  
  65. static int
  66. PUTL(p, u, base, cnt, width, fill_char)
  67.     char *p;
  68.     unsigned long u;
  69.     int base;
  70.     int *cnt;
  71.     int width;
  72.     int fill_char;
  73. {
  74.     int put = 0;
  75.     static char obuf[32];
  76.     char *t;
  77.  
  78.     t = obuf;
  79.  
  80.     do {
  81.         *t++ = "0123456789abcdef"[u % base];
  82.         u /= base;
  83.         width--;
  84.     } while (u > 0);
  85.  
  86.     while (width-- > 0 && *cnt > 0) {
  87.         *p++ = fill_char;
  88.         put++;
  89.         *cnt -= 1;
  90.     }
  91.     while (*cnt > 0 && t != obuf) {
  92.         *p++ = *--t;
  93.         put++;
  94.         *cnt -= 1;
  95.     }
  96.     return put;
  97. }
  98.  
  99. int
  100. vksprintf(buf, fmt, args)
  101.     char *buf;
  102.     const char *fmt;
  103.     va_list args;
  104. {
  105.     char *p = buf, c, fill_char;
  106.     char *s_arg;
  107.     int i_arg;
  108.     long l_arg;
  109.     int cnt;
  110.     int width, long_flag;
  111.  
  112.     cnt = SPRINTF_MAX - 1;
  113.     while( (c = *fmt++) != 0 ) {
  114.         if (c != '%') {
  115.             p += PUTC(p, c, &cnt, 1);
  116.             continue;
  117.         }
  118.         c = *fmt++;
  119.         width = 0;
  120.         long_flag = 0;
  121.         fill_char = ' ';
  122.         if (c == '0') fill_char = '0';
  123.         while (c && isdigit(c)) {
  124.             width = 10*width + (c-'0');
  125.             c = *fmt++;
  126.         }
  127.         if (c == 'l' || c == 'L') {
  128.             long_flag = 1;
  129.             c = *fmt++;
  130.         }
  131.         if (!c) break;
  132.  
  133.         switch (c) {
  134.         case '%':
  135.             p += PUTC(p, c, &cnt, width);
  136.             break;
  137.         case 'c':
  138.             i_arg = va_arg(args, int);
  139.             p += PUTC(p, i_arg, &cnt, width);
  140.             break;
  141.         case 's':
  142.             s_arg = va_arg(args, char *);
  143.             p += PUTS(p, s_arg, &cnt, width);
  144.             break;
  145.         case 'd':
  146.             if (long_flag) {
  147.                 l_arg = va_arg(args, long);
  148.             } else {
  149.                 l_arg = va_arg(args, int);
  150.             }
  151.             if (l_arg < 0) {
  152.                 p += PUTC(p, '-', &cnt, 1);
  153.                 width--;
  154.                 l_arg = -l_arg;
  155.             }
  156.             p += PUTL(p, l_arg, 10, &cnt, width, fill_char);
  157.             break;
  158.         case 'o':
  159.             if (long_flag) {
  160.                 l_arg = va_arg(args, long);
  161.             } else {
  162.                 l_arg = va_arg(args, unsigned int);
  163.             }
  164.             p += PUTL(p, l_arg, 8, &cnt, width, fill_char);
  165.             break;
  166.         case 'x':
  167.             if (long_flag) {
  168.                 l_arg = va_arg(args, long);
  169.             } else {
  170.                 l_arg = va_arg(args, unsigned int);
  171.             }
  172.             p += PUTL(p, l_arg, 16, &cnt, width, fill_char);
  173.             break;
  174.         case 'u':
  175.             if (long_flag) {
  176.                 l_arg = va_arg(args, long);
  177.             } else {
  178.                 l_arg = va_arg(args, unsigned int);
  179.             }
  180.             p += PUTL(p, l_arg, 10, &cnt, width, fill_char);
  181.             break;
  182.  
  183.         }
  184.     }
  185.     *p = 0;
  186.     return (int)(p - buf);
  187. }
  188.  
  189. int
  190. ksprintf(buf, fmt)
  191.     char *buf;
  192.     const char *fmt;
  193. {
  194.     va_list args;
  195.     int foo;
  196.  
  197.     va_start(args, fmt);
  198.     foo = vksprintf(buf, fmt, args);    
  199.     va_end(args);
  200.     return foo;
  201. }
  202.  
  203. int debug_level = 0;    /* how much debugging info should we print? */
  204. int out_device = 2;    /* BIOS device to write errors to */
  205.  
  206. /*
  207.  * out_next[i] is the out_device value to use when the current
  208.  * device is i and the user hits F3.
  209.  * Cycle is CON -> PRN -> AUX -> MIDI -> 6 -> 7 -> 8 -> 9 -> CON
  210.  * (Note: BIOS devices 6-8 exist on Mega STe and TT, 9 on TT.)
  211.  *
  212.  * out_device and this table are exported to bios.c and used here in HALT().
  213.  */
  214.  
  215. /*            0  1  2  3  4  5  6  7  8  9 */
  216. char out_next[] = { 1, 3, 0, 6, 0, 0, 7, 8, 9, 2 };
  217.  
  218. void
  219. debug_ws(s)
  220.     char *s;
  221. {
  222.     long r;
  223.  
  224.     while (*s) {
  225.         (void)Bconout(out_device, *s);
  226.         if (*s == '\n' && out_device != 0 && Bconstat(out_device)) {
  227.             r = Bconin(out_device) & 0x00ff0000L;
  228.             if (r == 0x3b0000L)
  229.                 debug_level++;
  230.             else if (r == 0x3c0000L)
  231.                 --debug_level;
  232.             else if (r == 0x400000L)
  233.                 DUMPPROC();
  234.             else if (r == 0x620000L) {
  235.                 do {
  236.                     r = Bconin(out_device) & 0x00ff0000L;
  237.                 } while (r != 0x610000L);
  238.             }
  239.         }
  240.         s++;
  241.     }
  242. }
  243.  
  244. static void
  245. VDEBUGOUT(s, args)
  246.     char *s;
  247.     va_list args;
  248. {
  249.     char buf[SPRINTF_MAX];
  250.  
  251.     ksprintf(buf, "pid %d (%s): ", curproc->pid, curproc->name);
  252.     debug_ws(buf);
  253.     vksprintf(buf, s, args);
  254.     debug_ws(buf);
  255.     debug_ws("\r\n");
  256. }
  257.  
  258. void Trace(s)
  259.     char *s;
  260. {
  261.     va_list args;
  262.  
  263.     if (debug_level > 1) {
  264.         va_start(args, s);
  265.         VDEBUGOUT(s, args);
  266.         va_end(args);
  267.     }
  268. }
  269.  
  270. void Debug(s)
  271.     char *s;
  272. {
  273.     va_list args;
  274.  
  275.     if (debug_level) {
  276.         va_start(args, s);
  277.         VDEBUGOUT(s, args);
  278.         va_end(args);
  279.     }
  280. }
  281.  
  282. void ALERT(s)
  283.     char *s;
  284. {
  285.     va_list args;
  286.  
  287.     va_start(args, s);
  288.     VDEBUGOUT(s, args);
  289.     va_end(args);
  290. }
  291.  
  292. EXITING
  293. void FATAL(s)
  294.     char *s;
  295. {
  296.     va_list args;
  297.  
  298.     va_start(args, s);
  299.     VDEBUGOUT(s, args);
  300.     va_end(args);
  301.     HALT();
  302. }
  303.  
  304.  
  305. EXITING 
  306. void HALT()
  307. {
  308.     long r;
  309.     extern long tosssp;    /* in main.c */
  310.  
  311.     restr_intr();    /* restore interrupts to normal */
  312.     debug_ws("Fatal MiNT error: adjust debug level and hit a key...\r\n");
  313.     sys_q[READY_Q] = 0;    /* prevent context switches */
  314.  
  315.     for(;;) {
  316.         r = Bconin(2) & 0x00ff0000L;
  317.         if (r == 0x3b0000L)
  318.             debug_level++;
  319.         else if (r == 0x3c0000L)
  320.             --debug_level;
  321.         else if (r == 0x3d0000L)        /* F3: cycle to next device */
  322.             out_device = out_next[out_device];
  323.         else if (r == 0x3e0000L)
  324.             out_device = 2;        /* F4: reset to console */
  325.         else if (r == 0x3f0000L) {
  326.             DUMPMEM(core);        /* F5: dump memory */
  327.             DUMPMEM(alt);
  328.         }
  329.         else if (r == 0x400000L)    
  330.             DUMPPROC();
  331.         else
  332.             break;
  333.     }
  334.     for(;;) {
  335.     debug_ws("System halted. Press 'x' to exit, or else reboot\r\n");
  336.         r = Bconin(2);
  337.  
  338.         if ( (r & 0x0ff) == 'x' ) {
  339.             close_filesys();
  340.             (void)Super(tosssp);
  341. #ifdef PROFILING
  342.             _exit(0);
  343. #else
  344.             Pterm0();
  345. #endif
  346.         }
  347.     }
  348. }
  349.  
  350. /* some key definitions */
  351. #define CTRLALT 0xc
  352. #define DEL 0x53    /* scan code of delete key */
  353. #define UNDO 0x61    /* scan code of undo key */
  354.  
  355. void
  356. do_func_key(scan)
  357.     int scan;
  358. {
  359.     extern struct tty con_tty;
  360.  
  361.     switch (scan) {
  362.     case DEL:
  363.         reboot();
  364.         break;
  365.     case UNDO:
  366.         killgroup(con_tty.pgrp, SIGQUIT);
  367.         break;
  368.     case 0x3b:        /* F1 */
  369.         debug_level++;
  370.         break;
  371.     case 0x3c:        /* F2 */
  372.         if (debug_level > 0)
  373.             --debug_level;
  374.         break;
  375.     case 0x3d:        /* F3 */
  376.         out_device = out_next[out_device];
  377.         break;
  378.     case 0x3e:        /* F4 */
  379.         out_device = 2;
  380.         break;
  381.     case 0x3f:        /* F5 */
  382.         DUMPMEM(core);
  383.         DUMPMEM(alt);
  384.         break;
  385.     case 0x40:        /* F6 */
  386.         DUMPPROC();
  387.         break;
  388.     }
  389. }
  390.  
  391.