Debug tricks
These are basic commands and techniques I learned while conducting some teaching operating system experiments, mainly focusing on debugging.
xv6
# b9e416502121 => docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b9e416502121 ubuntu23-cs144 "/bin/bash" 13 hours ago Up 13 hours hardcore_wescoff
c1b373cdc67a ubuntu23-cs144 "/bin/bash" 27 hours ago Up 17 hours reverent_maxwell
# Start Kernel
make qemu-gdb
gdb-multiarch kernel/kernel
set architecture riscv:rv64
target remote localhost:25000
# split-screen
layout split
Pintos
# Start Kernel
pintos --qemu --gdb --run user_program_name
----
# Find the function you want to Debug, for example
grep -rn "timer_sleep"
...tests/threads/alarm-simultaneous.c:90: timer_sleep (sleep_until - timer_ticks ());
tests/threads/mlfqs-fair.c:101: timer_sleep (40 * TIMER_FREQ);
tests/threads/mlfqs-fair.c:116: timer_sleep (sleep_time - timer_elapsed (ti->start_time));
grep: threads/build/tests/threads/alarm-zero.o: binary file matches
grep: threads/build/tests/threads/alarm-simultaneous.o: binary file matches
grep: threads/build/tests/threads/mlfqs-fair.o: binary file matches
# We choose one arbitrarily, for example, alarm-zero
pintos --qemu --gdb -- run alarm-zero
---- cd build
# Enter gdb
pintos-gdb kernel.o
(gdb)set architecture i386
(gdb)target remote localhost:1234
# For Example
(gdb)break devices/timer.c:96
(gdb)c
(gdb)step...
# If qemu freezes, execute this to find the PID
ps aux | grep qemu
root 32 0.0 0.0 288508 5640 pts/1 S+ 03:51 0:00 make qemu-gdb
root 53 0.5 2.4 2016128 194744 pts/1 Sl+ 03:51 0:07 qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -global virtio-mmio.force-legacy=false -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0 -S -gdb tcp::25000
root 84 60.0 0.0 288516 4704 pts/2 S+ 04:14 0:00 grep --color=auto qemu
# kill process
kill -9 32 53
Start qemu
pintos --qemu --run alarm-multiple
GDB with Pintos
# Set breakpoint
break filename:line
# For Example
break devices/timer.c:92
# Continue running the program until the next breakpoint
continue | c
# Step through the code, entering the function
step | s
# Step through the code, but do not enter the function
next | n
# Execute the current function and return to the calling function
finish
# Run to the specified line number
until <line>
# Jump directly to the specified line number
jump <line>
# List all breakpoints
info breakpoints
# Display the contents of all registers
info registers
# Display the current stack
info frame
# Display all threads
info threads
# Display all variables in the current function
info locals
# Delete a breakpoint
delete <breakpoint_number>
# Print the variable value
print <variable>
# Display the current program code
list | l
# Display the current function call stack
backtrace | bt
# Quit
Count rows
#ubuntu/debian
apt install diffstat
#Macos
brew install diffstat
# For Example
git diff | diffstat
#Commit
git show <commit_id> | diffstat
# Or
git diff <old_commit> <new_commit> | diffstat
# Git diff
git diff --stat
Enjoy Reading This Article?
Here are some more articles you might like to read next: