1
0
Fork 0

Add basic printing/formatting infrastructure for debugging

This commit is contained in:
Aaron Lindsay 2012-09-07 23:53:27 -04:00
parent 84dd295956
commit 12e890bb8a
3 changed files with 127 additions and 0 deletions

7
include/print.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef PRINT_H
#define PRINT_H
int print(char *fmt, ...);
void print_init(void (*putc)(char));
#endif /* PRINT_H */

14
include/vargs.h Normal file
View File

@ -0,0 +1,14 @@
#ifndef VARGS_H
#define VARGS_H
#ifdef __GNUC__
typedef __builtin_va_list va_list;
#define va_start(v,l) __builtin_va_start(v,l)
#define va_end(v) __builtin_va_end(v)
#define va_arg(v,l) __builtin_va_arg(v,l)
#define va_copy(d,s) __builtin_va_copy(d,s)
#else
#error "Variadic functions aren't supported with any compiler other than GCC."
#endif
#endif /* VARGS_H */

106
kernel/print.c Normal file
View File

@ -0,0 +1,106 @@
#include <print.h>
#include <vargs.h>
/* This function exists solely so crashes don't happen if putc() gets
* called before it is initialized. */
void putc_initial(char c) {
(void)c;
}
void (*putc)(char) = &putc_initial;
void print_init(void (*putcfn)(char)) {
if (putcfn)
putc = putcfn;
}
void puts(const char *s)
{
while (*s) putc (*s++);
}
void puti(int i)
{
unsigned int left;
char buf[1 << (sizeof(int)*8) / 10];
char *p = buf;
unsigned char negative = i < 0;
if (!i)
putc('0');
left = negative ? -1*i : i;
while (left) {
unsigned int remainder = left % 10;
left /= 10;
*p = ('0'-0) + remainder;
p++;
}
if (negative)
putc('-');
while (p-- != buf)
putc(*p);
}
void putx(unsigned int i) {
unsigned int left = i;
char buf[1 << (sizeof(int)*8) / 16];
char *p = buf;
puts("0x");
if (!i)
putc('0');
while (left) {
unsigned int remainder = left % 16;
left /= 16;
if (remainder < 10)
*p = ('0'-0) + remainder;
else
*p = ('a'-10) + remainder;
p++;
}
while (p-- != buf)
putc(*p);
}
int print(char *fmt, ...) {
char *c;
va_list arg;
va_start(arg, fmt);
for (c = fmt; *c; c++) {
if (*c == '%') {
switch (*++c) {
case '%':
putc('%');
break;
case 's':
puts(va_arg(arg, char *));
break;
case 'c':
putc(va_arg(arg, unsigned int));
break;
case 'd':
puti(va_arg(arg, int));
break;
case 'x':
putx(va_arg(arg, unsigned int));
break;
default:
puts("\nError: print(): Invalid formatting character: '");
putc(*c);
puts("'\n");
return -1;
}
} else {
putc(*c);
}
}
va_end(arg);
return 0;
}