MisOS Lab02
Published:
代码参考链接:https://github.com/ChenYuHengSJTU/MIT_OSLab/tree/syscall
System call tracing(moderate)
TODO
In this assignment you will add a system call tracing feature that may help you when debugging later labs. You’ll create a new trace system call that will control tracing. It should take one argument, an integer “mask”, whose bits specify which system calls to trace. For example, to trace the fork system call, a program calls trace(1 « SYS_fork), where SYS_fork is a syscall number from kernel/syscall.h. You have to modify the xv6 kernel to print out a line when each system call is about to return, if the system call’s number is set in the mask. The line should contain the process id, the name of the system call and the return value; you don’t need to print the system call arguments. The trace system call should enable tracing for the process that calls it and any children that it subsequently forks, but should not affect other processes.
We provide a trace user-level program that runs another program with tracing enabled (see
user/trace.c
). When you’re done, you should see output like this:
$ trace 32 grep hello README
3: syscall read -> 1023
3: syscall read -> 966
3: syscall read -> 70
3: syscall read -> 0
$
$ trace 2147483647 grep hello README
4: syscall trace -> 0
4: syscall exec -> 3
4: syscall open -> 3
4: syscall read -> 1023
4: syscall read -> 966
4: syscall read -> 70
4: syscall read -> 0
4: syscall close -> 0
$
$ grep hello README
$
$ trace 2 usertests forkforkfork
usertests starting
test forkforkfork: 407: syscall fork -> 408
408: syscall fork -> 409
409: syscall fork -> 410
410: syscall fork -> 411
409: syscall fork -> 412
410: syscall fork -> 413
409: syscall fork -> 414
411: syscall fork -> 415
...
- In the first example above, trace invokes grep tracing just the read system call. The 32 is 1«SYS_read. In the second example, trace runs grep while tracing all system calls; the 2147583647 has all 31 low bits set. In the third example, the program isn’t traced, so no trace output is printed. In the fourth example, the fork system calls of all the descendants of the forkforkfork test in usertests are being traced. Your solution is correct if your program behaves as shown above (though the process IDs may be different).
Hints
- Add
$U/_trace
toUPROGS
in Makefile - Run make qemu and you will see that the compiler cannot compile
user/trace.c
, because the user-space stubs for the system call don’t exist yet: add a prototype for the system call to user/user.h, a stub touser/usys.pl
, and a syscall number tokernel/syscall.h
. The Makefile invokes the perl scriptuser/usys.pl
, which produces user/usys.S, the actual system call stubs, which use the RISC-V ecall instruction to transition to the kernel. Once you fix the compilation issues, run trace 32 grep hello README; it will fail because you haven’t implemented the system call in the kernel yet. - Add a
sys_trace()
function inkernel/sysproc.c
that implements the new system call by remembering its argument in a new variable in the proc structure (seekernel/proc.h
). The functions to retrieve system call arguments from user space are inkernel/syscall.c
, and you can see examples of their use inkernel/sysproc.c
. - Modify fork() (see
kernel/proc.c
) to copy the trace mask from the parent to the child process. - Modify the
syscall()
function inkernel/syscall.c
to print the trace output. You will need to add an array of syscall names to index into.
Sysinfo(moderate)
TODO
- In this assignment you will add a system call,
sysinfo
, that collects information about the running system. The system call takes one argument: a pointer to astruct sysinfo
(seekernel/sysinfo.
h). The kernel should fill out the fields of this struct: the freemem field should be set to the number of bytes of free memory, and the nproc field should be set to the number of processes whose state is not UNUSED. We provide a test programsysinfotest
; you pass this assignment if it prints “sysinfotest: OK”.
Hints
Add
$U/_sysinfotest
toUPROGS
in MakefileRun
make qemu
;user/sysinfotest.c
will fail to compile. Add the system call sysinfo, following the same steps as in the previous assignment. To declare the prototype forsysinfo()
inuser/user.h
you need predeclare the existence of struct sysinfo:
struct sysinfo;
int sysinfo(struct sysinfo *);
Once you fix the compilation issues, run sysinfotest; it will fail because you haven’t implemented the system call in the kernel yet.
sysinfo needs to copy a struct sysinfo back to user space; see
sys_fstat()
(kernel/sysfile.c
) andfilestat()
(kernel/file.c
) for examples of how to do that using copyout().To collect the amount of free memory, add a function to
kernel/kalloc.c
To collect the number of processes, add a function to
kernel/proc.c