1*2303Stoy /* subr_prf.c 4.5 01/28/81 */ 231Sbill 331Sbill #include "../h/param.h" 431Sbill #include "../h/systm.h" 531Sbill #include "../h/seg.h" 631Sbill #include "../h/buf.h" 731Sbill #include "../h/conf.h" 8285Sbill #include "../h/mtpr.h" 91184Sbill #include "../h/reboot.h" 102172Swnj #include "../h/vm.h" 112172Swnj #include "../h/msgbuf.h" 1231Sbill 13285Sbill #ifdef TRACE 14285Sbill #define TRCBUFS 4096 15285Sbill char trcbuf[TRCBUFS]; 16285Sbill char *trcbufp = trcbuf; 17285Sbill int trcwrap; 18285Sbill int trcprt = TRCBUFS; 19285Sbill #endif 20285Sbill 2131Sbill /* 2231Sbill * In case console is off, 2331Sbill * panicstr contains argument to last 2431Sbill * call to panic. 2531Sbill */ 2631Sbill 2731Sbill char *panicstr; 2831Sbill 2931Sbill /* 3031Sbill * Scaled down version of C Library printf. 3131Sbill * Only %s %u %d (==%u) %o %x %D are recognized. 3231Sbill * Used to print diagnostic information 3331Sbill * directly on console tty. 3431Sbill * Since it is not interrupt driven, 3531Sbill * all system activities are pretty much 3631Sbill * suspended. 3731Sbill * Printf should not be used for chit-chat. 3831Sbill */ 3931Sbill /*VARARGS1*/ 4031Sbill printf(fmt, x1) 4131Sbill register char *fmt; 4231Sbill unsigned x1; 4331Sbill { 44285Sbill 45285Sbill prf(fmt, &x1, 0); 46285Sbill } 47285Sbill 48285Sbill #ifdef TRACE 49285Sbill trace(fmt, x1) 50285Sbill register char *fmt; 51285Sbill unsigned x1; 52285Sbill { 53285Sbill 54285Sbill prf(fmt, &x1, 1); 55285Sbill } 56285Sbill 57285Sbill #endif 58285Sbill 59285Sbill prf(fmt, adx, trace) 60285Sbill register char *fmt; 61285Sbill register unsigned int *adx; 62285Sbill { 6331Sbill register c; 6431Sbill char *s; 6531Sbill 6631Sbill loop: 6731Sbill while((c = *fmt++) != '%') { 6831Sbill if(c == '\0') 6931Sbill return; 70285Sbill putchar(c, trace); 7131Sbill } 7231Sbill c = *fmt++; 73285Sbill if (c == 'X') 74285Sbill printx((long)*adx, trace); 75285Sbill else if (c == 'd' || c == 'u' || c == 'o' || c == 'x') 76285Sbill printn((long)*adx, c=='o'? 8: (c=='x'? 16:10), trace); 77285Sbill else if (c == 's') { 7831Sbill s = (char *)*adx; 79285Sbill while (c = *s++) 80285Sbill #ifdef TRACE 81285Sbill if (trace) { 82285Sbill *trcbufp++ = c; 83285Sbill if (trcbufp >= &trcbuf[TRCBUFS]) { 84285Sbill trcbufp = trcbuf; 85285Sbill trcwrap = 1; 86285Sbill } 87285Sbill } else 88285Sbill #endif 89285Sbill putchar(c, trace); 9031Sbill } else if (c == 'D') { 91285Sbill printn(*(long *)adx, 10, trace); 9231Sbill adx += (sizeof(long) / sizeof(int)) - 1; 9331Sbill } 9431Sbill adx++; 9531Sbill goto loop; 9631Sbill } 9731Sbill 98285Sbill printx(x, trace) 9931Sbill long x; 10031Sbill { 10131Sbill int i; 10231Sbill 10331Sbill for (i = 0; i < 8; i++) 104285Sbill putchar("0123456789ABCDEF"[(x>>((7-i)*4))&0xf], trace); 10531Sbill } 10631Sbill 10731Sbill /* 108*2303Stoy * Print an integer in base b. If the base is ten it is condidered a 109*2303Stoy * signed integer otherwise it is treated as unsigned. 11031Sbill */ 111285Sbill printn(n, b, trace) 112*2303Stoy unsigned long n; 11331Sbill { 114*2303Stoy register unsigned long a; 115*2303Stoy register long a1 = n; 11631Sbill 117*2303Stoy if (b == 10 && a1 < 0) { 118285Sbill putchar('-', trace); 119*2303Stoy n = -a1; 12031Sbill } 12131Sbill if(a = n/b) 122285Sbill printn(a, b, trace); 123285Sbill putchar("0123456789ABCDEF"[(int)(n%b)], trace); 12431Sbill } 12531Sbill 12631Sbill /* 1271184Sbill * Panic is called on unresolvable fatal errors. 1281184Sbill * It syncs, prints "panic: mesg", and then reboots. 12931Sbill */ 13031Sbill panic(s) 13131Sbill char *s; 13231Sbill { 13331Sbill panicstr = s; 13431Sbill printf("panic: %s\n", s); 1351787Sbill (void) spl0(); 13631Sbill for(;;) 1371401Sbill boot(RB_PANIC, RB_AUTOBOOT); 13831Sbill } 13931Sbill 14031Sbill /* 14131Sbill * prdev prints a warning message of the 14231Sbill * form "mesg on dev x/y". 14331Sbill * x and y are the major and minor parts of 14431Sbill * the device argument. 14531Sbill */ 14631Sbill prdev(str, dev) 14731Sbill char *str; 14831Sbill dev_t dev; 14931Sbill { 15031Sbill 15131Sbill printf("%s on dev %u/%u\n", str, major(dev), minor(dev)); 15231Sbill } 15331Sbill 15431Sbill /* 15531Sbill * deverr prints a diagnostic from 15631Sbill * a device driver. 15731Sbill * It prints the device, block number, 15831Sbill * and an octal word (usually some error 15931Sbill * status register) passed as argument. 16031Sbill */ 16131Sbill deverror(bp, o1, o2) 16231Sbill register struct buf *bp; 16331Sbill { 16431Sbill 16531Sbill prdev("err", bp->b_dev); 16631Sbill printf("bn=%d er=%x,%x\n", bp->b_blkno, o1,o2); 16731Sbill } 168285Sbill 169285Sbill #ifdef TRACE 170285Sbill dumptrc() 171285Sbill { 172285Sbill register char *cp; 173285Sbill register int pos, nch; 174285Sbill 175285Sbill nch = trcprt; 176285Sbill if (nch < 0 || nch > TRCBUFS) 177285Sbill nch = TRCBUFS; 178285Sbill pos = (trcbufp - trcbuf) - nch; 179285Sbill if (pos < 0) 180285Sbill if (trcwrap) 181285Sbill pos += TRCBUFS; 182285Sbill else { 183285Sbill nch += pos; 184285Sbill pos = 0; 185285Sbill } 186285Sbill for (cp = &trcbuf[pos]; nch > 0; nch--) { 187285Sbill putchar(*cp++, 0); 188285Sbill if (cp >= &trcbuf[TRCBUFS]) 189285Sbill cp = trcbuf; 190285Sbill } 191285Sbill } 192285Sbill #else 193285Sbill /*ARGSUSED*/ 194285Sbill dumptrc(nch) 195285Sbill int nch; 196285Sbill { 197285Sbill 198285Sbill } 199285Sbill #endif 200285Sbill 201285Sbill /* 202285Sbill * Print a character on console or in internal trace buffer. 203285Sbill * If destination is console then the last MSGBUFS characters 204285Sbill * are saved in msgbuf for inspection later. 205285Sbill */ 2061785Sbill /*ARGSUSED*/ 207285Sbill putchar(c, trace) 208285Sbill register c; 209285Sbill { 210285Sbill 211285Sbill #ifdef TRACE 212285Sbill if (trace) { 213285Sbill *trcbufp++ = c; 214285Sbill if (trcbufp >= &trcbuf[TRCBUFS]) { 215285Sbill trcbufp = trcbuf; 216285Sbill trcwrap = 1; 217285Sbill } 218285Sbill return; 219285Sbill } 220285Sbill #endif 221285Sbill if (c != '\0' && c != '\r' && c != 0177) { 2222172Swnj if (msgbuf.msg_magic != MSG_MAGIC) { 2232172Swnj msgbuf.msg_bufx = 0; 2242172Swnj msgbuf.msg_magic = MSG_MAGIC; 2252172Swnj } 2262172Swnj if (msgbuf.msg_bufx < 0 || msgbuf.msg_bufx >= MSG_BSIZE) 2272172Swnj msgbuf.msg_bufx = 0; 2282172Swnj msgbuf.msg_bufc[msgbuf.msg_bufx++] = c; 229285Sbill } 230285Sbill if (c == 0) 231285Sbill return; 232285Sbill cnputc(c); 233285Sbill } 234