diff --git a/kern/Makefrag b/kern/Makefrag index 3b2982e..458dd57 100644 --- a/kern/Makefrag +++ b/kern/Makefrag @@ -17,6 +17,7 @@ KERN_SRCFILES := kern/entry.S \ kern/entrypgdir.c \ kern/init.c \ kern/console.c \ + kern/ansi.c \ kern/monitor.c \ kern/pmap.c \ kern/env.c \ diff --git a/kern/ansi.c b/kern/ansi.c new file mode 100644 index 0000000..c90057e --- /dev/null +++ b/kern/ansi.c @@ -0,0 +1,49 @@ +#include + +uint8_t vga_colors[8] = { + [0] = 0, + [1] = 4, + [2] = 2, + [3] = 6, + [4] = 1, + [5] = 5, + [6] = 3, + [7] = 7 +}; + +void +ansi_step(void (*putcf)(int), int c, struct AttrState* s) { + switch(s->state) { + case NORMAL: + if(c != 27) putcf(c | (s->cattrs << 8)); + else s->state = ESC; + return; + case ESC: + s->state = (c == '[') ? BRACKET : NORMAL; + return; + case BRACKET: + if(c == '3') { + s->state = COLOR_FG; + } else if(c == '0') { + s->state = COLOR; + s->attrs = 0; + } else { + s->state = NORMAL; + } + return; + case COLOR_FG: + if(c >= '0' && c <= '9') { + s->attrs = (s->attrs & ~(0x07)) | vga_colors[c - '0']; + c |= ((c - '0') << 8); + s->state = COLOR; + } else { + s->state = NORMAL; + } + return; + case COLOR: + s->state = (c == ';') ? BRACKET : NORMAL; + if(c == 'm') s->cattrs = s->attrs; + return; + } +} + diff --git a/kern/ansi.h b/kern/ansi.h new file mode 100644 index 0000000..970f9c6 --- /dev/null +++ b/kern/ansi.h @@ -0,0 +1,20 @@ +#ifndef _ANSI_H_ +#define _ANSI_H_ + +#include + +struct AttrState { + uint8_t cattrs; + uint8_t attrs; + enum { + NORMAL, + ESC, + BRACKET, + COLOR_FG, + COLOR + } state; +}; + +void ansi_step(void (*putcf)(int), int c, struct AttrState* s); + +#endif diff --git a/kern/console.c b/kern/console.c index 7d312a7..8019dad 100644 --- a/kern/console.c +++ b/kern/console.c @@ -7,6 +7,7 @@ #include #include +#include static void cons_intr(int (*proc)(void)); static void cons_putc(int c); @@ -128,6 +129,7 @@ lpt_putc(int c) static unsigned addr_6845; static uint16_t *crt_buf; static uint16_t crt_pos; +static struct AttrState attst; static void cga_init(void) @@ -136,6 +138,10 @@ cga_init(void) uint16_t was; unsigned pos; + attst.cattrs = 0; + attst.attrs = 0; + attst.state = NORMAL; + cp = (uint16_t*) (KERNBASE + CGA_BUF); was = *cp; *cp = (uint16_t) 0xA55A; @@ -157,10 +163,15 @@ cga_init(void) crt_pos = pos; } - +static void cga_putc_internal(int c); static void -cga_putc(int c) +cga_putc(int c){ + ansi_step(cga_putc_internal, c, &attst); +} + +static void +cga_putc_internal(int c) { // if no attribute given, then use black on white if (!(c & ~0xFF)) diff --git a/kern/init.c b/kern/init.c index 6bffa80..88736d0 100644 --- a/kern/init.c +++ b/kern/init.c @@ -29,6 +29,14 @@ i386_init(void) // Lab 2 memory management initialization functions mem_init(); + // Test ANSI color escape codes + cprintf("\33[31m" "C" + "\33[33m" "o" + "\33[32m" "l" + "\33[36m" "o" + "\33[34m" "r" + "\33[0m" " Works!" "\n"); + // Drop into the kernel monitor. while (1) monitor(NULL); diff --git a/kern/kdebug.c b/kern/kdebug.c index 9547143..cc59033 100644 --- a/kern/kdebug.c +++ b/kern/kdebug.c @@ -171,7 +171,8 @@ debuginfo_eip(uintptr_t addr, struct Eipdebuginfo *info) // Search within [lline, rline] for the line number stab. - // If found, set info->eip_line to the right line number. + // If found, set info->eip_line to the correct line number. + // e.g., info->eip_line = stabs[lline].n_desc // If not found, return -1. // // Hint: @@ -179,6 +180,12 @@ debuginfo_eip(uintptr_t addr, struct Eipdebuginfo *info) // Look at the STABS documentation and to find // which one. // Your code here. + stab_binsearch(stabs, &lline, &rline, N_SLINE, addr); + if(lline <= rline) { + info->eip_line = stabs[lline].n_desc; + } else { + info->eip_line = -1; + } // Search backwards from the line number for the relevant filename diff --git a/kern/monitor.c b/kern/monitor.c index ee25809..c97da78 100644 --- a/kern/monitor.c +++ b/kern/monitor.c @@ -25,6 +25,7 @@ struct Command { static struct Command commands[] = { { "help", "Display this list of commands", mon_help }, { "kerninfo", "Display information about the kernel", mon_kerninfo }, + { "backtrace", "Display current backtrace", mon_backtrace } }; /***** Implementations of basic kernel monitor commands *****/ @@ -61,6 +62,25 @@ mon_backtrace(int argc, char **argv, struct Trapframe *tf) // LAB 1: Your code here. // HINT 1: use read_ebp(). // HINT 2: print the current ebp on the first line (not current_ebp[0]) + uint32_t* ebp; + uint32_t eip; + struct Eipdebuginfo info; + + ebp = (uint32_t*) read_ebp(); + cprintf("Stack backtrace:\n"); + while(ebp) { + eip = ebp[1]; + cprintf(" ebp %08x eip %08x args %08x %08x %08x %08x %08x\n", + ebp, eip, ebp[2], ebp[3], ebp[4], ebp[5], ebp[6]); + ebp = (uint32_t*) ebp[0]; + + debuginfo_eip(eip, &info); + cprintf(" %s:%d: %.*s+%d\n", + info.eip_file, info.eip_line, + info.eip_fn_namelen, info.eip_fn_name, + eip - info.eip_fn_addr); + }; + return 0; } diff --git a/lib/printfmt.c b/lib/printfmt.c index 802adcd..b1de635 100644 --- a/lib/printfmt.c +++ b/lib/printfmt.c @@ -205,11 +205,9 @@ vprintfmt(void (*putch)(int, void*), void *putdat, const char *fmt, va_list ap) // (unsigned) octal case 'o': - // LAB 1: Replace this with your code. - putch('X', putdat); - putch('X', putdat); - putch('X', putdat); - break; + num = getuint(&ap, lflag); + base = 8; + goto number; // pointer case 'p': diff --git a/student.info b/student.info index f3ede52..7014618 100644 --- a/student.info +++ b/student.info @@ -1,5 +1,5 @@ -OSU ID (xxx-yyy-zzz) : 933456789 -FLIP ID (e.g., jangye) : jangye -Name : Yeongjin Jang +OSU ID (xxx-yyy-zzz) : 932700867 +FLIP ID (e.g., jangye) : fedorind +Name : Danila Fedorin CS 444/544 ? : 444 -Lab Class # : Lab 1 +Lab Class # : Lab 2