memory & storage architecture lab. @ seoul national university gdb commands hyeon-gyu...

19
Memory & Storage Architecture Lab. @ Seoul National University GDB commands Hyeon-gyu Lee([email protected]) School of Computer Science and Engineering Seoul National University

Upload: suzanna-armstrong

Post on 27-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Memory & Storage Architecture Lab.@ Seoul National University

GDB commands

Hyeon-gyu Lee([email protected])

School of Computer Science and Engineering

Seoul National University

Memory & Storage Architecture Lab.@ Seoul National University

To Use GDB in Your Program

Additional compile option gcc –g –o prog prog.c

Execute gdb gdb prog

2

Memory & Storage Architecture Lab.@ Seoul National University

Looking the Program Source

List instruction (gdb) list

• Abbreviated to the letter ‘l’

• Shows your code of your program

3

Memory & Storage Architecture Lab.@ Seoul National University

Looking the Program Source

List instruction options (gdb) list <function>

• Shows the code of <function>

(gdb) list <file>:<function> • Shows the code of <function> in the <file>

(gdb) list <line number> • Shows the code from <line number>

(gdb) list <file>:<line number> • Shows the code from <line number> in the <file>

(gdb) list - • Shows the code right before your current position

Current unit lines of printing: 10 (gdb) set listsize 20

• Set the unit lines of printing to 20

4

Memory & Storage Architecture Lab.@ Seoul National University

Running the program

Run the program (gdb) run

• Abbreviated to the letter ‘r’

• Run your code continuously

- If confronts breakpoint, gdb stops there.

- If there are no breakpoints, gdb runs until the program terminates.

• Useful when your program has errors

(gdb) run arg1 arg2 …• Run your code continuously with arguments

5

Memory & Storage Architecture Lab.@ Seoul National University

Breakpoints

Make points to stop (gdb) break

• Abbreviated to the letter ‘b’

• Without any option, breakpoint is made at your current position

6

Memory & Storage Architecture Lab.@ Seoul National University

Making Breakpoints

Breakpoint options (gdb) break <function>

• Breakpoint at the start of the function

(gdb) break <file>:<function>• Breakpoint at the start of the function in the file

(gdb) break <line number>• Breakpoint at the designated line

(gdb) break <+/-number>• Breakpoint at the current-position-relatively designated line

(gdb) break <memory address>• Breakpoint at the instruction that pointed by the memory address

• Used at assembly debugging

Above all options can be done with conditions• e.g. (gdb) break 10 if var==0

7

Memory & Storage Architecture Lab.@ Seoul National University

Manipulating Breakpoints

Breakpoint information (gdb) info break

• Shows the information about your breakpoints

• Valid until the gdb terminates

Deleting breakpoint (gdb) clear

• Abbreviated to ‘cl’

• Clear breakpoints that you made

• Should be used with options

(gdb) clear <line number>• Delete breakpoint at that line number

8

Memory & Storage Architecture Lab.@ Seoul National University

Manipulating Breakpoints

Deleting breakpoint options (gdb) clear <line number>

• Delete breakpoint at that line number

(gdb) clear <function>• Delete breakpoint at the start of the function

(gdb) clear <file>:<function>• Delete breakpoint at the start of the function in the file

(gdb) d• Delete all breakpoints

(gdb) disable/enable br• Disable/enable all breakpoints

(gdb) disable/enable br 1 3• Disable/enable breakpoints with number 1, 3.

9

Memory & Storage Architecture Lab.@ Seoul National University

Single-stepping

Single stepping (gdb) step

• Abbreviated to the letter ‘s’

• When a function is called at the point, it goes into the function

(gdb) step <number>• Repeat ‘step’ with times of the number.

(gdb) next• Abbreviated to the letter ‘n’

• When a function is called at the point, it executes the whole function continuously

(gdb) next <number>• Repeat ‘next’ with times of the number.

10

Memory & Storage Architecture Lab.@ Seoul National University

Single-stepping

Other instructions (gdb) continue

• Abbreviated to the letter ‘c’

• Continuously execute from your current position

(gdb) advance <line number>• Continuously execute from your current position to the line number

(gdb) until• Continuously execute until current repetition finish point

• When the current position is not in a repetition, it operates like ‘next’

11

Memory & Storage Architecture Lab.@ Seoul National University

Single-stepping

Other instructions (gdb) finish

• Continuously execute from your current position until current function finish point

(gdb) return <return value>• Return without executing the remaining part of the function

• When needed, you can manually designate a return value

(gdb) set var <variable>=<value>• Change the content of the variable to the given value

12

Memory & Storage Architecture Lab.@ Seoul National University

Watchpoints

Stop when a variable is set to a specific value (gdb) watch <variable>

• Break when a value is written in the variable

(gdb) rwatch <variable>• Break when the value is read in the variable

(gdb) awatch <variable>• Break when a value is read/written in the variable

13

Memory & Storage Architecture Lab.@ Seoul National University

Displaying Values

See values in your program (gdb) info locals

• Shows values of local variables that you can access from your current position

(gdb) info variables• Shows values of global variables that you can access from your current position

(gdb) print <variable>• Shows value of the variable

• When the variable is a pointer, dereferencing is possible with an asterisk.

(gdb) print <function>• Shows address of the function

14

Memory & Storage Architecture Lab.@ Seoul National University

Displaying Values

See values in your program (gdb) print $<register>

• Shows value of the register.

(gdb) print ‘<file>’::<variable>• Shows value of the variable in the file.

(gdb) print <function>::<variable>• Shows value of the variable in the function

(gdb) whatis <variable>• Shows type of the variable

(gdb) display <variable>• Shows value of the variable automatically with progress of the program

• Enabling/disabling is possible

(gdb) undisplay <display#>• Delete display

15

Memory & Storage Architecture Lab.@ Seoul National University

Examining the Stack

Backtracing the stack (gdb) backtrace

• Abbreviated to the ‘bt’

• Shows stack trace of your current position

16

Memory & Storage Architecture Lab.@ Seoul National University

Killing the Executing Program in the GDB

Kill instruction (gdb) kill

• Kill the executing program forcefully

17

Memory & Storage Architecture Lab.@ Seoul National University

Quitting the GDB

Quit and exit to the Linux (gdb) quit

• Abbreviated to the letter ‘q’

18

Memory & Storage Architecture Lab.@ Seoul National University

References

References http://kwanseob.blogspot.kr/2012/03/gdb.html, retrieved on May 20, 2015.

http://darkdust.net/files/GDB%20Cheat%20Sheet.pdf, retrieved on May 20, 2015.

https://kldp.org/node/71806, retrieved on May 20, 2015.

19