Lab 4
Lab 4
● Most OSes provide a useful and easy to use tools for debugging.
● In Unix-Like OSes the following commands is very useful in debugging:
○ strace: trace system calls and signals.
○ top: display the current running processes in Linux.
○ htop: the same as previous but in enhanced view for system resource
usage, and you can filter, search, and interact with processes more easily.
○ gdb: we will see more about this later.
○ dmesg: used to display all messages from the kernel.
○ Checking Logs in /var/log/: some services and kernel messages are
logged in various log files located in the /var/log/ directory.
DEBUGGING Unix-Like OSes (cont'd)
$ top
$ htop
DEBUGGING Unix-Like OSes (cont'd)
void f(void) {
int *a = malloc(16);
int *b;
a = (int *) a + 1;
b = (int *) ((char *) a + 1);
printf("2: a = %p, b = %p\n", a, b);
}
int
main(int ac, char **av)
{
f();
return 0;
}
What is GDB?
● GDB can do four main kinds of things (plus other things in support of
these) to help you catch bugs in the act:
○ Start your program, specifying anything that might affect its
behavior.
○ Make your program stop on specified conditions.
○ Examine what has happened, when your program has stopped.
○ Change things in your program, so you can experiment with
correcting the effects of one bug and go on to learn about
another.
GDB in Xv6
$ make qemu-gdb
Let's DEBUG Like a Detective (cont'd)
● You can use the following command to split the terminal in two,
showing where gdb is in the source code:
(gdb) layout src
● Now, you can run the xv6 until the point in which you put the
breakpoint using the following:
(gdb) cont
● You can use the following command to print the stack of backtrace:
(gdb) backtrace
Your Detective Sense Should
Create a Comfortable Environment Like This
Edit your code here. The source code being
executed appears here.
(gdb) print /x *p
● What is the value of p->trapframe->a7 and what does that value
represent? Ans: 0x7 and represents the syscall number.
● What is the name of process being run and what is the process
id? Ans: initcode and process id is 1.
Task
1. Write a program in user-space and debug it. Hint: Consult your TA for details on
the program and debug requirements. Your grade (4 marks) will be based on the
quality of your questions.
2. Setting your debugging environment in a correct way. (1 mark)
3. Set a breakpoint to investigate the error. (1 mark)
4. Backtrace the stack of error. (1 mark)
5. Find and fix the error. ((Min(p->pid, 2) - 0.5) bonus marks)
Thank you!
Do you have any questions?