本文所有代码均为JOS内核源代码,可以从MIT 6.828课程网站下载。
概述:所有向屏幕输出的过程,一定是经过参数处理,最后组织成一个字符数组(BUFFER),这个数组(一段内存)会在虚拟地址空间映射到显示硬件的那一部分空间里(memory mapped I/O 的思想)。这段空间是内核才能读写的,所以屏幕输出会进行系统调用。
C语言函数在调用printf时,会根据输出特点调整实际调用的函数,比如一般向屏幕输出,会调用cprintf(),它除了名字和printf完全一致。因此,本文介绍从cprintf开始。
向控制台输出(打印到屏幕)的函数是在kern/printf.c中的cprintf():
int
cprintf(const char *fmt, ...)
{
va_list ap;
int cnt;
va_start(ap, fmt);
cnt = vcprintf(fmt, ap);
va_end(ap);
return cnt;
}
函数预处理变长参数,然后调用同一文件下的vcprintf():
int
vcprintf(const char *fmt, va_list ap)
{
int cnt = 0;
vprintfmt((void*)putch, &cnt, fmt, ap);
return cnt;
}
该函数将调用vprintfmt(),并且指定vprintfmt的字符输出函数putch()。
在lib/printfmt.c中可以找到vprintfmt函数的实现:
void
vprintfmt(void (*putch)(int, void*), void *putdat, const char *fmt, va_list ap)
{
register const char *p;
register int ch, err;
unsigned long long num;
int base, lflag, width, precision, altflag, precedeflag;
char padc;
while (1) {
while ((ch = *(unsigned char *) fmt++) != '%') {
if (ch == '\0')
return;
putch(ch, putdat);
}
// Process a %-escape sequence
padc = ' ';
width = -1;
precision = -1;
lflag = 0;
altflag = 0;
precedeflag = 0;
reswitch:
switch (ch = *(unsigned char *) fmt++) {
//plus sign
case '+':
precedeflag = 1;
// flag to pad on the right
case '-':
padc = '-';
goto reswitch;
// flag to pad with 0's instead of spaces
case '0':
padc = '0';
goto reswitch;
// width field
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
for (precision = 0; ; ++fmt) {
precision = precision * 10 + ch - '0';
ch = *fmt;
if (ch < '0' || ch > '9')
break;
}
goto process_precision;
case '*':
precision = va_arg(ap, int);
goto proce