lab1之backtrace函数

来源:互联网 发布:淘宝联盟微信自动发单 编辑:程序博客网 时间:2024/05/20 23:40


The backtrace function should display a listing of function call frames in the following format:
Stack backtrace:
ebp f0109e58 eip f0100a62 args 00000001 f0109e80 f0109e98 f0100ed2 00000031
ebp f0109ed8 eip f01000d6 args 00000000 00000000 f0100058 f0109f28 00000061
...

函数如下:


int
mon_backtrace(int argc, char **argv, struct Trapframe *tf)
{
    // Your code here.
    int i;
    uint32_t ebp;
        __asm __volatile("movl %%ebp,%0" : "=r" (ebp));//read ebp
        uint32_t eip;
        uint32_t a,b,c,d,e;
            
        cprintf("Stack backtrace:/n");
    

//内联函数实现
         while(ebp!=0x0){
            //get eip
           __asm __volatile( "movl %1,%%eax /n/t"      
                  "movl 4(%%eax),%0 /n/t"
                  :"=r"(eip)
                  :"r"(ebp)
                  );
            //get args
        __asm __volatile( "movl  %5,%%eax /n/t"
                  "movl  8(%%eax),%0 /n/t"
                  "movl 12(%%eax),%1 /n/t"
                  "movl 16(%%eax),%2 /n/t"
                  "movl 20(%%eax),%3 /n/t"
                  "movl 24(%%eax),%4 /n/t"
                 :"=r"(a),"=r"(b),"=r"(c),"=r"(d),"=r"(e)
                  :"r"(ebp)
                  );
            //print
        cprintf("ebp %08x  eip %08x args %08x %08x %08x %08x %08x/n",ebp,eip,a,b,c,d,e);
            //get new ebp
           __asm __volatile( "movl %0,%%eax /n/t"
                  "movl (%%eax), %0 /n/t"
                  :"=r"(ebp)
                  );
        
        }
            
         //c语言实现 
     /*
        while (ebp!=0x0){
            cprintf("ebp %08x eip %08x args ",ebp,*(int*)(ebp+4));
            for(i=1;i<=5;i++)
                if(*(int*)ebp!=((ebp+4)+i*4))
                cprintf("%08x ",*(int*)((ebp+4)+i*4));
                cprintf("/n");
            //    ebp=(*(int*)((ebp+4)+i*4));
                ebp=(*(int*)(ebp));
        }
        */
    return 0;
}



最后得到的结果:

Stack backtrace:
ebp f010ef18  eip f0100124 args 00000000 ffffffff ffffffff ffffffff ffffffff
ebp f010ef38  eip f0100106 args 00000000 ffffffff ffffffff ffffffff ffffffff
ebp f010ef58  eip f0100106 args 00000001 ffffffff ffffffff ffffffff ffffffff
ebp f010ef78  eip f0100106 args 00000002 ffffffff ffffffff ffffffff ffffffff
ebp f010ef98  eip f0100106 args 00000003 ffffffff ffffffff ffffffff ffffffff
ebp f010efb8  eip f0100106 args 00000004 ffffffff ffffffff ffffffff ffffffff
ebp f010efd8  eip f0100187 args 00000005 ffffffff ffffffff ffffffff ffffffff
ebp f010eff8  eip f010003d args 00000000 ffffffff ffffffff ffffffff ffffffff


在lab1目录下执行:

$make grade

make all
make[1]: 正在进入目录 `/study/MIT OS/lab1'
make[1]:正在离开目录 `/study/MIT OS/lab1'
make[1]: 正在进入目录 `/study/MIT OS/lab1'
+ as kern/entry.S
+ cc kern/init.c
+ cc kern/console.c
+ cc kern/monitor.c
+ cc kern/printf.c
+ cc lib/printfmt.c
+ cc lib/readline.c
+ cc lib/string.c
+ ld obj/kern/kernel
+ as boot/boot.S
+ cc -Os boot/main.c
+ ld boot/boot
boot block is 411 bytes (max 510)
+ mk obj/kern/bochs.img
make[1]:正在离开目录 `/study/MIT OS/lab1'
sh ./grade.sh
./grade.sh: 56: gmake: not found
Printf: OK (.5s)
Backtrace: Count OK, Args OK (.5s)
Score: 50/50


函数编写完成。


对于question:Why can't the backtrace code detect how many arguments there actually are? How could this limitation be fixed?,我还没有想到,忘高人指点