Instructions
gdb ./program_name
. Start the debugger.
disass Function
. Show the disassemble code of Function.
b x
. Set a breakpoint at line x.
b Function
. Set a breakpoint at the beginning of function.
info b
. List the information of breakpoints.
info r
. List all values of registers.
list Function
. Show the source code of function.
p currentThread
. Show the address of the main thread.
run
. Run from the beginning.
c
. Continue running.
ni/si
. Run a step.
x *address
. Show the function in address.
Get started
In threads directory:
gdb ./nachos |
Then, show the disassemble code of main function:
disass main |
We can see a few addresses of functions:
0x00001404 <+23>: call 0x12f0 <__x86.get_pc_thunk.bx> |
Start from initialize . disass Initialize
but nothing we are looking for.
In ThreadTest()
:
0x00003343 <+117>: call 0x3273 <SimpleThread(int)> |
The address of SimpleThread(int)
is 0x3273
.
In SynchTest()
, nothing.
Oh, just get started… Now dig into the questions.
Find the address of Funtion
Use b function
to set breakpoint, then it returns the address.
(gdb) b InterruptEnable |
Find the address of threads (both main and forked)
Let’s look into the main function, in the first few lines, we can see there is a Initialize function call. Look into it, there seems to be nothing major. Then look down, we see ThreadTest()
, then look into it. It calls SimpleThread()
in threadtest.cc
:
void SimpleThread(_int which) { |
We can see a Yield()
function, which makes the current thread to give up CPU to another thread, so we make a breakpoint here. When it yields, it’s obviously another thread running, so we can see the address of thread.
(gdb) list SimpleThread |
We can see the thread switch and also the address of threads using p currentThread
So the main thread of the Nachos is 0x56563ca0
, and the forked thread created by the main thread is 0x56563d00
.
Digging into SWITCH()
When the main thread executes
SWITCH()
function for the first time, to what address the CPU returns when it executes the last instructionret
ofSWITCH()
? What location in the program that address is referred to?When the forked thread executes
SWITCH()
function for the first time, to what address the CPU returns when it executes the last instructionret
ofSWITCH()
? What location in the program that address is referred to?
Looking through all files in the directory, one thing is sure that the SWITCH()
function is written in disassemble language.
(gdb) disass SWITCH |
In line <+80>, make a breakpoint, run
, and look into registers using info r
, then show the location by x address
.
(gdb) b *0x56559ed4 |
The CPU returns to address 0x56559e76
, which located to <ThreadRoot>
when it executes the last instruction ret
of SWITCH()
for the first time when the main thread executes SWITCH()
function, and the second time it returns to 0x56556a24
locating to <Scheduler::Run(Thread*)+144>
.