6.828: Operating System Engineering (65)

LAB4 の Homework なんですがいきなり make 失敗。

$ make
gcc -m32 -Werror -Wall -o mkfs mkfs.c
In file included from mkfs.c:9:
stat.h:5: error: redefinition of ‘struct stat’
make: *** [mkfs] Error 1
$

最初訳が分からなかったのですが、どうも標準ヘッダのどこかで struct stat が定義されてる箇所があるのでしょうね。

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include "types.h"
#include "fs.h"
#include "stat.h"

仕方が無いのでここだけ以下な形にして

//#include "stat.h"
#define T_DIR  1   // Directory
#define T_FILE 2   // File
#define T_DEV  3   // Special device

相当無理やり make を通しました。こんなことして良いのかなぁ。まさかねーって言いつつ標準ヘッダを include してるソレを確認してみたら、この mkfs.c のみでした。

gdb 起動

ええと、grep main kernel.asm で

001028a0 <main>:
void mainc(void);
main(void)

なあたりが breakpoint なのかな、と言いつつヤッてみます。

$ make qemu-gdb
*** Now run 'gdb'.
qemu -serial mon:stdio -hdb fs.img xv6.img -smp 2 -S -gdb tcp::26000

で、もいっこの端末で gdb 起動。

$ gdb
GNU gdb (GDB) 7.2-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/>.
+ target remote localhost:26000
The target architecture is assumed to be i8086
[f000:fff0]    0xffff0: ljmp   $0xf000,$0xe05b
0x0000fff0 in ?? ()
+ symbol-file kernel
(gdb) b * 0x001028a0
Breakpoint 1 at 0x1028a0: file main.c, line 16.
(gdb) c
Continuing.

あら、止まらない。再度 kernel.asm を確認してみたら br な値が違ってました。

止まらん

gdb 起動して list 見てみたら main 手続きのソレが出力されたので

(gdb) list
17        mpinit();        // collect info about this machine
18        lapicinit(mpbcpu());
19        ksegment();      // set up segments
20        picinit();       // interrupt controller
21        ioapicinit();    // another interrupt controller
22        consoleinit();   // I/O devices & their interrupts
23        uartinit();      // serial port
24        kinit();         // initialize memory allocator
25        jkstack();       // call mainc() on a properly-allocated stack 
26      }
(gdb) b 17
Breakpoint 1 at 0x1028a9: file main.c, line 17.

して continue したのですがやはり止まらず。テキスト見たら -no-kqemu が云々とあるので、Makefile に手を入れてみます。

gcc -fno-pic -static -fno-builtin -fno-strict-aliasing -O2 -Wall -MD -ggdb -m32 -Werror -no-kqemu -fno-stack-protector   -c -o cat.o cat.c
gcc: unrecognized option '-no-kqemu'

とほほ。man 見てもそんなモノは見当たりません。よくよく考えたら 6.828 の lab で使っていたのは自前の qemu だったな、と言いつつ GNUmakefile の中を確認したらありました。

QEMU := /opt/bin/qemu

成程、ということで xv6-rev4 の Makefile も上記を使う形で修正を盛り込んで make qemu-gdb してみたところ、動きました。

(gdb) b *0x00102750
Breakpoint 1 at 0x102750: file main.c, line 42.
(gdb) c
Continuing.
The target architecture is assumed to be i386
=> 0x102750 <mainc>:    push   %ebp

Breakpoint 1, mainc () at main.c:42
42      {
(gdb) info reg
eax            0x1000000        16777216
ecx            0x1      1
edx            0x0      0
ebx            0x10094  65684
esp            0xfffffc 0xfffffc
ebp            0x7b98   0x7b98
esi            0x0      0
edi            0x10d824 1103908
eip            0x102750 0x102750 <mainc>
eflags         0x6      [ PF ]
cs             0x8      8
ss             0x10     16
ds             0x10     16
es             0x10     16
fs             0x0      0
gs             0x18     24
(gdb) x/24x $esp
0xfffffc:       0x0010287b      0x00000000      0x00000000      0x00000000
0x100000c:      0x00000000      0x00000000      0x00000000      0x00000000
0x100001c:      0x00000000      0x00000000      0x00000000      0x00000000
0x100002c:      0x00000000      0x00000000      0x00000000      0x00000000
0x100003c:      0x00000000      0x00000000      0x00000000      0x00000000
0x100004c:      0x00000000      0x00000000      0x00000000      0x00000000
(gdb)

上記出力がどーゆー意味なのか、については別途確認します。