kernel/printf.c
151 lines
1//2// formatted console output -- printf, panic.3//45#include <stdarg.h>67#include "types.h"8#include "param.h"9#include "spinlock.h"10#include "sleeplock.h"11#include "fs.h"12#include "file.h"13#include "memlayout.h"14#include "riscv.h"15#include "defs.h"16#include "proc.h"1718volatile int panicking = 0; // printing a panic message19volatile int panicked = 0; // spinning forever at end of a panic2021// lock to avoid interleaving concurrent printf's.22static struct {23 struct spinlock lock;24} pr;2526static char digits[] = "0123456789abcdef";2728static void29printint(long long xx, int base, int sign)30{31 char buf[20];32 int i;33 unsigned long long x;3435 if(sign && (sign = (xx < 0)))36 x = -xx;37 else38 x = xx;3940 i = 0;41 do {42 buf[i++] = digits[x % base];43 } while((x /= base) != 0);4445 if(sign)46 buf[i++] = '-';4748 while(--i >= 0)49 consputc(buf[i]);50}5152static void53printptr(uint64 x)54{55 int i;56 consputc('0');57 consputc('x');58 for (i = 0; i < (sizeof(uint64) * 2); i++, x <<= 4)59 consputc(digits[x >> (sizeof(uint64) * 8 - 4)]);60}6162// Print to the console.63int64printf(char *fmt, ...)65{66 va_list ap;67 int i, cx, c0, c1, c2;68 char *s;6970 if(panicking == 0)71 acquire(&pr.lock);7273 va_start(ap, fmt);74 for(i = 0; (cx = fmt[i] & 0xff) != 0; i++){75 if(cx != '%'){76 consputc(cx);77 continue;78 }79 i++;80 c0 = fmt[i+0] & 0xff;81 c1 = c2 = 0;82 if(c0) c1 = fmt[i+1] & 0xff;83 if(c1) c2 = fmt[i+2] & 0xff;84 if(c0 == 'd'){85 printint(va_arg(ap, int), 10, 1);86 } else if(c0 == 'l' && c1 == 'd'){87 printint(va_arg(ap, uint64), 10, 1);88 i += 1;89 } else if(c0 == 'l' && c1 == 'l' && c2 == 'd'){90 printint(va_arg(ap, uint64), 10, 1);91 i += 2;92 } else if(c0 == 'u'){93 printint(va_arg(ap, uint32), 10, 0);94 } else if(c0 == 'l' && c1 == 'u'){95 printint(va_arg(ap, uint64), 10, 0);96 i += 1;97 } else if(c0 == 'l' && c1 == 'l' && c2 == 'u'){98 printint(va_arg(ap, uint64), 10, 0);99 i += 2;100 } else if(c0 == 'x'){101 printint(va_arg(ap, uint32), 16, 0);102 } else if(c0 == 'l' && c1 == 'x'){103 printint(va_arg(ap, uint64), 16, 0);104 i += 1;105 } else if(c0 == 'l' && c1 == 'l' && c2 == 'x'){106 printint(va_arg(ap, uint64), 16, 0);107 i += 2;108 } else if(c0 == 'p'){109 printptr(va_arg(ap, uint64));110 } else if(c0 == 'c'){111 consputc(va_arg(ap, uint));112 } else if(c0 == 's'){113 if((s = va_arg(ap, char*)) == 0)114 s = "(null)";115 for(; *s; s++)116 consputc(*s);117 } else if(c0 == '%'){118 consputc('%');119 } else if(c0 == 0){120 break;121 } else {122 // Print unknown % sequence to draw attention.123 consputc('%');124 consputc(c0);125 }126127 }128 va_end(ap);129130 if(panicking == 0)131 release(&pr.lock);132133 return 0;134}135136void137panic(char *s)138{139 panicking = 1;140 printf("panic: ");141 printf("%s\n", s);142 panicked = 1; // freeze uart output from other CPUs143 for(;;)144 ;145}146147void148printfinit(void)149{150 initlock(&pr.lock, "pr");151}152