home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / mint / mint095s / debugold.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-03  |  6.6 KB  |  373 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 (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.     while (*s) {
  223.         (void)Bconout(out_device, *s);
  224.         if (*s == '\n' && out_device != 0 && Bconstat(out_device)) {
  225.             (void)Bconin(out_device);
  226.             while (!Bconstat(out_device)) ;
  227.             (void)Bconin(out_device);
  228.         }
  229.         s++;
  230.     }
  231. }
  232.  
  233. static void
  234. VDEBUGOUT(s, args)
  235.     char *s;
  236.     va_list args;
  237. {
  238.     char buf[SPRINTF_MAX];
  239.  
  240.     ksprintf(buf, "pid %d (%s): ", curproc->pid, curproc->name);
  241.     debug_ws(buf);
  242.     vksprintf(buf, s, args);
  243.     debug_ws(buf);
  244.     debug_ws("\r\n");
  245. }
  246.  
  247. void TRACE(s)
  248.     char *s;
  249. {
  250.     va_list args;
  251.  
  252.     if (debug_level > 1) {
  253.         va_start(args, s);
  254.         VDEBUGOUT(s, args);
  255.         va_end(args);
  256.     }
  257. }
  258.  
  259. void DEBUG(s)
  260.     char *s;
  261. {
  262.     va_list args;
  263.  
  264.     if (debug_level) {
  265.         va_start(args, s);
  266.         VDEBUGOUT(s, args);
  267.         va_end(args);
  268.     }
  269. }
  270.  
  271. void ALERT(s)
  272.     char *s;
  273. {
  274.     va_list args;
  275.  
  276.     va_start(args, s);
  277.     VDEBUGOUT(s, args);
  278.     va_end(args);
  279. }
  280.  
  281. EXITING
  282. void FATAL(s)
  283.     char *s;
  284. {
  285.     va_list args;
  286.  
  287.     va_start(args, s);
  288.     VDEBUGOUT(s, args);
  289.     va_end(args);
  290.     HALT();
  291. }
  292.  
  293.  
  294. EXITING 
  295. void HALT()
  296. {
  297.     long r;
  298.     extern long tosssp;    /* in main.c */
  299.  
  300.     restr_intr();    /* restore interrupts to normal */
  301.     debug_ws("Fatal MiNT error: adjust debug level and hit a key...\r\n");
  302.     sys_q[READY_Q] = 0;    /* prevent context switches */
  303.  
  304.     for(;;) {
  305.         r = Bconin(2) & 0x00ff0000;
  306.         if (r == 0x3b0000)
  307.             debug_level++;
  308.         else if (r == 0x3c0000)
  309.             --debug_level;
  310.         else if (r == 0x3d0000)        /* F3: cycle to next device */
  311.             out_device = out_next[out_device];
  312.         else if (r == 0x3e0000)
  313.             out_device = 2;        /* F4: reset to console */
  314.         else if (r == 0x3f0000)
  315.             DUMPMEM(core);        /* F5: dump memory */
  316.         else if (r == 0x400000)    
  317.             DUMPPROC();
  318.         else
  319.             break;
  320.     }
  321.     for(;;) {
  322.     debug_ws("System halted. Press 'x' to exit, or else reboot\r\n");
  323.         r = Bconin(2);
  324.  
  325.         if ( (r & 0x0ff) == 'x' ) {
  326.             close_filesys();
  327.             (void)Super(tosssp);
  328.             zeroexit();
  329.         }
  330.     }
  331. }
  332.  
  333. /* some key definitions */
  334. #define CTRLALT 0xc
  335. #define DEL 0x53    /* scan code of delete key */
  336. #define UNDO 0x61    /* scan code of undo key */
  337.  
  338. void
  339. do_func_key(scan)
  340.     int scan;
  341. {
  342.     extern struct tty con_tty;
  343.  
  344.     switch (scan) {
  345.     case DEL:
  346.         reboot();
  347.         break;
  348.     case UNDO:
  349.         killgroup(con_tty.pgrp, SIGQUIT);
  350.         break;
  351.     case 0x3b:        /* F1 */
  352.         debug_level++;
  353.         break;
  354.     case 0x3c:        /* F2 */
  355.         if (debug_level > 0)
  356.             --debug_level;
  357.         break;
  358.     case 0x3d:        /* F3 */
  359.         out_device = out_next[out_device];
  360.         break;
  361.     case 0x3e:        /* F4 */
  362.         out_device = 2;
  363.         break;
  364.     case 0x3f:        /* F5 */
  365.         DUMPMEM(core);
  366.         break;
  367.     case 0x40:        /* F6 */
  368.         DUMPPROC();
  369.         break;
  370.     }
  371. }
  372.  
  373.