gdb guide

Quick Guide to Gdb

The dynamic debugger utility, gdb, has a large number of capabilities. This quick guide lists a small but useful subset of the gdb commands.

Index

Preparation
Setting Breakpoints
Running the Program being Debugged
Examining Variables
List Source Code and the Next Statement
Help and Quitting Gdb
Summary of Commands

Preparation

Compile with the -g option.
Example. Compile the program printch.cpp:

hawk% g++ -g printch.cpp -o printch
Start gdb and set the number of source lines to list.
Example. Run gdb on the printch program and set the number of source lines to list at a time to 28.

hawk% gdb -xdb -tui printch
GNU gdb 4.17.1
Copyright 1998 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License
(gdb) set listsize 28

Setting Breakpoints

Breakpoints are points in your code at which gdb will stop and allow executing other gdb commands.

Set a breakpoint at the beginning of a function.
Example. Set a breakpoint at the beginning of main.

(gdb) b main

Set a breakpoint at a line of the current file during debugging.
Example. Set a breakpoint at line 35 while in file printch.cpp.

(gdb) b 35

Set a breakpoint at the beginning of a class member function.
Example. Set a breakpoint at the beginning of member function erase of the class list.

(gdb) b list::erase

Listing breakpoints.
Example. List all breakpoints which have been set so far in a debugging session.

(gdb) info b
Num Type           Disp Enb Address    What
1   breakpoint     keep y   0x0040104f in main at printch.cpp:27
2   breakpoint     keep y   0x004010a7 in main at printch.cpp:35
Deleting a breakpoint.
Example. Delete the breakpoint at line 35.

(gdb) delete 2

Running the Program being Debugged

Start the program being debugged.
Example 1. The program is printch, which can take an optional command line argument. Start it running with no command line argument.

(gdb) r

Example 2. Start printch running with command line argument A.

(gdb) r A

Execute a single statement. If the statement is a function call, just single step into the function.

(gdb) s

Execute a single statement. If the statement is a function call, execute the entire function and return to the statement just after the call; that is, step over the function.

(gdb) n

Execute from the current point up to the next breakpoint if there is one, otherwise execute until the program terminates.

(gdb) c

Execute the rest of the current function; that is, step out of the function.
(gdb) finish

Examining Variables

Print the value of a variable or expression.
Example 1. Print the value of a variable count

(gdb) p count

Example 2. Print the value of the expression fname[i+1]

(gdb) p fname[i+1]

List Source Code and the Next Statement

List lines of source code.
Example. List the next listsize number of lines of code. Note that listsize’s value can be change with the set command.

(gdb) l

List lines of source code centered around a particular line.
Example. List the lines centered around line 41.

(gdb) l 41

Show the next statement that will be executed.

(gdb) where
#0  mystrcpy (copyto=0x259fc6c "*", copyfrom=0x259fddc "ABC") at printch.cpp:27
#1  0x4010c8 in main (argc=3, argv=0x25b0cb8) at printch.cpp:40
The statement at line 27 of the function mystrcpy is the next statement and the function mystrcpy was called by main.

Help and Quitting Gdb

There is a help command, h and the command to quit gdb is q.

Summary of Commands

Gdb Command Description

set listsize n	Set the number of lines listed by the list command to n [set listsize]
b function	Set a breakpoint at the beginning of function [break]
b line number	Set a breakpoint at line number of the current file. [break]
info b	List all breakpoints [info]
delete n	Delete breakpoint number n [delete]
r args	Start the program being debugged, possibly with command line arguments args. [run]
s count	Single step the next count statments (default is 1). Step into functions. [step]
n count	Single step the next count statments (default is 1). Step over functions. [next]
finish	Execute the rest of the current function. Step out of the current function. [finish]
c	Continue execution up to the next breakpoint or until termination if no breakpoints are encountered. [continue]
p expression	print the value of expression [print]
l optional_line	List next listsize lines. If optional_line is given, list the lines centered around optional_line. [list]
where	Display the current line and function and the stack of calls that got you there. [where]
h optional_topic	help or help on optional_topic [help]
q	quit gdb [quit]

egr. [run with command]

#lan_circ eth1.4061 0 192.168.100.2 255.255.255.0 -r isis
#gdb lan_circ
(gdb)b FuncA
(gdb)run eth1.4061 0 192.168.100.2 255.255.255.0 -r isis
(gdb) p *circ
$5 = {type = priv_lan, NI = 0xfffffffffaf0, IP = 0xfffffffffab0, 
  OSI = 0xfffffffffa90, OSPF = 0xfffffffffa10, RIP = 0x0, 
  ISIS = 0xfffffffffa88}
(gdb) p *(circ->ISIS)
$6 = {passive = 144 '\220', enable = 1 '\001'}
(gdb) p *(circ->NI->ni.eth)
$3 = {mac_address = "\000\000\000\000\000", proxy_arp = 0 '\000', 
  if_name = "eth1.4061\000\000\000\000\000\000", admin_status = 1}
(gdb) p/x *(circ->NI->ni.eth)
$4 = {mac_address = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, proxy_arp = 0x0, 
  if_name = {0x65, 0x74, 0x68, 0x31, 0x2e, 0x34, 0x30, 0x36, 0x31, 0x0, 0x0, 
    0x0, 0x0, 0x0, 0x0, 0x0}, admin_status = 0x1}
(gdb) 

[1.]http://condor.depaul.edu/glancast/373class/docs/gdb.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值