每年都要 repost 一下,希望同學們都能花一些時間學一下 debugger,
不要再用 cout 硬撐了啦!!
※ 引述《ric2k1 (Ric)》之銘言:
有鑑於許多同學遇到程式的 bug 但確不一定清楚如何使用 debugger,
這裡寫一個 quick reference 希望同學們可以多多使用 debugger.
**************************
* gdb/ddd survival guide *
**************************
[Commonly used commands]
========================
r [options]: run the program with [options]
n: execute the next line of the program
s: step into the function under current line
b [funcName | lineNo]: set break point on <funcName> or <lineNo>, break on
current line if no parameter is specified.
e.g.
b BSTree::insert
b adtTest.h:106
del <breakPointId>: delete the break point by ID
c: continue the execution to the next break or watch point
p <var>: print the content of the variable
p* <var>: print the content of the memory pointed by the varaible
display <var>: display the content of the variable
display* <var>: display the content of the memory pointed by the varaible
undisplay <displayId>: undisplay by ID
up: move up one level of source code (for viewing; won't affect the execution)
down: move down one level of source code (for viewing)
where: show the calling trace to the current line
dir <dirName>: add <dirName> for the look-up of the source code
<Ctrl-c>: force to break the execution
q: quit the debugger
[Less common commands]
======================
call <funcName>: call the function
set <varName> = <value>: force to set the variable value
watch <expression>: set the watch point (used with 'continue')
e.g. watch (idx = 10) // note: not "=="
tbreak [funcName | lineNo]: set the temporary break point. Voided after the
first encounter.
b if <expression>: conditional break point
[Note]
======
You can add a number to most of the commands to specify the "repeating times".
For example, "n 10" will execute the next 10 lines of codes. "c 100" means
you will ignore the next 99 break points and stop at the 100th one.