yale_OS(2)——OS-xv6的源代码的调试

xv6源代码的调试(源代码的下载,编译,运行如:yale_OS(1)——OS-xv6的源代码的下载,编译和运行所示)

在QEMU下远程调试xv6.(学习课程地址如下:http://zoo.cs.yale.edu/classes/cs422/2011/lec/l2-hw)
1.打开一个终端,进入xv6目录下,运行如下命令:make qemu-gdb
  此时qemu进入等待状态,如下:
  administrator@ubuntu:~/xv6$ make qemu-gdb
  *** Now run 'gdb'.
  qemu -serial mon:stdio -hdb fs.img xv6.img -smp 2 -S -gdb tcp::26000
  等待gdb远程连接到该qemu.


2.然后打开另一个终端,进入相同的目录,运行如下命令:gdb kernel
  此时进入了调试状态,如下:
  administrator@ubuntu:~/xv6$ gdb kernel
  GNU gdb (GDB) 7.1-ubuntu
  Copyright (C) 2010 Free Software Foundation, Inc.
  License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  This is free software: you are free to change and redistribute it.
  There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
  and "show warranty" for details.
  This GDB was configured as "x86_64-linux-gnu".
  For bug reporting instructions, please see:
  <http://www.gnu.org/software/gdb/bugs/>...
  Reading symbols from /home/administrator/xv6/kernel...done.
  + target remote localhost:26000
  The target architecture is assumed to be i8086
  [f000:fff0]    0xffff0:    ljmp   $0xf000,$0xe05b
  0x0000fff0 in ?? ()
  + symbol-file kernel
  (gdb)

3.一旦GDB成功连接上QEMU的远程调试,它将会显示远程的QEMU程序停在何处。
  如下所示:
  The target architecture is assumed to be i8086
  [f000:fff0]    0xffff0:    ljmp   $0xf000,$0xe05b
  0x0000fff0 in ?? ()

4.在exec处设置断点,命令如下:b exec

5.然后继续运行,继续的命令如下:c
  运行结果如下:
  (gdb) c
  Continuing.
  [New Thread 2]
  [Switching to Thread 2]
  The target architecture is assumed to be i386
  => 0x100930 <exec>:    push   %ebp

  Breakpoint 1, exec (path=0x1c "/init", argv=0xff1ee4) at exec.c:11
  11    {
  (gdb)

  在此处,机器正运行在32bit模式下,xv6已经进行了自身的初始化,其将加载和运行它的第一个用户模式下的进程——/init程序,

6.然后继续运行,继续的命令如下:c
  (gdb) c
  Continuing.
  [Switching to Thread 1]
  => 0x100930 <exec>:    push   %ebp

  Breakpoint 1, exec (path=0x83c "sh", argv=0xff0ee4) at exec.c:11
  11    {
  (gdb)

  此处第二次调用exec函数,

7.当继续在gdb下敲入命令:c,此时在QEMU下出现等待输入命令状态,此时,在QEMU下输入如下命令:
  $ cat README
  此处gdb调试终端下出现如下状态:
  (gdb) c
  Continuing.
  [Switching to Thread 2]
  => 0x100930 <exec>:    push   %ebp

  Breakpoint 1, exec (path=0x1460 "cat", argv=0xfe4ee4) at exec.c:11
  11    {
  (gdb)

  通过如下命令来检查exec函数调用时的参数:
  1)p argv[0]
    结果为:
    (gdb) p argv[0]
    $1 = 0x1460 "cat"

  2)p argv[1]
    结果为:
    (gdb) p argv[1]
    $2 = 0x1464 "README"

  3)p argv[2]
    结果为:
    (gdb) p argv[2]
    $3 = 0x0

8.在gdb调试终端下,输入命令:up,可以检查上下文哪个函数调用了exec
  如下:
  (gdb) up
  #1  0x001041a8 in sys_exec () at sysfile.c:366
  366      return exec(path, argv);
  (gdb) up
  #2  0x00103f86 in syscall () at syscall.c:133
  133        proc->tf->eax = syscalls[num]();
  (gdb) up
  #3  0x00104e2d in trap (tf=0xfe4fb4) at trap.c:41
  41        syscall();

9.使用gdb中的list或l命令,可以查看当下调用exec函数的上下文的源代码。








你可能感兴趣的:(thread,ubuntu,OS,Path,远程连接,终端)