Add a new system call to xv6: traceme
Moderate
xv6 book Sect 2.3 Kernel organization
xv6 book Sect 2.5 Process overview
In the previous exercise, you have learned step-by-step how to implement a system call. Now you will independently add the new system call void traceme(int enable).
What is traceme?
traceme is a system call that enables system call tracing for a process. When tracing is enabled, every system call made by the process is logged to the console, providing visibility into the process’s interactions with the kernel. This is particularly useful for debugging and understanding program behavior at the system level.
When a process calls traceme with enable set to true, every system call of the process is printed to the console, along with the pid of the process in the form: [{pid}] syscall {num}. The following examples illustrate the behavior of traceme:
$ ./trace hello
[3] syscall 7
[3] syscall 16
Hello world
[3] syscall 24
[3] syscall 2
In this example, the trace program enables tracing for the hello executable. Each line prefixed with [3] shows a system call made by process 3 (the hello process), with the number indicating which system call was invoked. For instance, [3] syscall 7 means process 3 called system call number 7.
The trace output appears interleaved with the program’s normal output (“Hello world”), showing the sequence of system calls before and after the program executes its main logic.
Task: Implementing traceme
- Add the new system call
void traceme(int enable)with number 25. - Use the kernel version of
printf. - Use
argintto request a syscall argument. - Create a user-space program
tracethat takes an executable as an argument and calls this executable with the traceme functionality enabled.
Hints:
- Your
trace.cprogram should be placed in theuser/directory - Use
tracemesyscall to enable tracing - Use
execto run the target program. The first argument toexecshould be the program name, and you need to pass the remaining command-line arguments - Don’t forget to add
$U/_trace\to theUPROGSlist in theMakefileso the program gets compiled