第一点经验:
在linux下用gdb调试C++程序时,由其是调试STL(或QT 、或ACE)的程序时,千万要记住不能使用 -O2 这个选项。
因为如果在编译时加了该选项就是使得gdb在单步调试时,在STL间跳来跳去。使程序员无法进行有效的调试工作。
第二点经验:
有时我们需要将STL中的一些容器里的值打印出来,但通常的gdb 中的 print无法直接得到我们想要的结果。所以我们需要对在gdb中定义一个宏,然后我们每次打印时只要执行该宏就可以得到想得到的值了。
具体做法是:
2. #cat dbinit_stl_views-1.03.txt >>
~/.gdbinit
3. 若正处于gdb中,运行命令:
(gdb) source ~/.gdbinit
4. 例如,如下代码:
bugging.cpp
#
include
<
vector
>
using
namespace
std
;
int
main(
)
{
vector
<
int
>
vec;
vec.
push_back(
2)
;
vec.
push_back(
3)
;
vec.
push_back(
4)
;
return
0;
}
编译:
#g+
+
-
o bugging -
g bugging.
cpp
gdb调试:
#
gdb bugging
GNU gdb 6.
8
Copyright (
C)
2008 Free
Software Foundation,
Inc.
License GPLv3+
:
GNU GPL version 3 or
later <
http:
//gnu.org/licenses/gpl.html>
This
is free
software:
you are free
to change and
redistribute it.
There is NO WARRANTY,
to the extent permitted by law.
Type "show copying"
and
"show warranty"
for
details.
This
GDB was configured as "i486-slackware-linux"
.
.
.
(
gdb)
help pvector
Prints std
:
:
vector
<
T>
information.
Syntax:
pvector <
vector
>
<
idx1>
<
idx2>
Note:
idx,
idx1 and
idx2 must be in acceptable range [
0.
.
<
vector
>
.
size(
)
-
1]
.
Examples:
pvector v -
Prints vector
content,
size,
capacity and
T typedef
pvector v 0 -
Prints element[
idx]
from vector
pvector v 1 2 -
Prints elements in range [
idx1.
.
idx2]
from vector
(
gdb)
break
main
Breakpoint 1 at 0x80485c6:
file
bugging.
cpp,
line
6.
(
gdb)
run
Starting program:
/
root/
learn/
c+
+
/
bugging
Breakpoint 1,
main (
)
at bugging.
cpp:
6
6 vector
<
int
>
vec;
(
gdb)
n
7 vec.
push_back(
2)
;
(
gdb)
8 vec.
push_back(
3)
;
(
gdb)
pvector
Prints std
:
:
vector
<
T>
information.
Syntax:
pvector <
vector
>
<
idx1>
<
idx2>
Note:
idx,
idx1 and
idx2 must be in acceptable range [
0.
.
<
vector
>
.
size(
)
-
1]
.
Examples:
pvector v -
Prints vector
content,
size,
capacity and
T typedef
pvector v 0 -
Prints element[
idx]
from vector
pvector v 1 2 -
Prints elements in range [
idx1.
.
idx2]
from vector
(
gdb)
pvector vec
elem[
0]
:
$1 =
2
Vector
size =
1
Vector
capacity =
1
Element type =
int
*
(
gdb)
n
9 vec.
push_back(
4)
;
(
gdb)
10 return
0;
(
gdb)
pvector vec
elem[
0]
:
$2 =
2
elem[
1]
:
$3 =
3
elem[
2]
:
$4 =
4
Vector
size =
3
Vector
capacity =
4
Element type =
int
*
(
gdb)
5. 默认情况下gdb不能用[]查看stl容器的数据元素,提示如下错误:
(
gdb)
print vec[
0]
One of the arguments you tried to pass to operator
[
]
could not
be converted to what
the function wants.
使用GDB的"p
variable-name"查看STL容器类,只会显示该容器的一些信息,并不能很友好的显示该容器的内容。使用stl-views.gdb这个脚本可
以很好地解决这一问题。下载stl-veiws.gdb文件,将其放到~/目录下,直接将其改名为~/.gdbinit,或者在你已有的.gdbinit
文件中用source ~/.stl-views.gdb命令将其包含。这样你就可以用下面的命令显示STL容器类了。
容器类型 GDB 命令
std::vectorpvector stl_variable
std::listplist stl_variable T
std::mappmap stl_variable
std::multimappmap stl_variable
std::setpset stl_variable T
std::multisetpset stl_variable
std::dequepdequeue stl_variable
std::stackpstack stl_variable
std::queuepqueue stl_variable
std::priority_queueppqueue stl_variable
std::bitsetpbitset stl_variable
std::string pstring stl_variable
std::widestring pwstring stl_variable
举例:
如果你的C++代码中有定义: sets;
则在GDB中可以使用如下命令查看该set的信息与内容:
pset s - 打印该集合s的定义和大小
pset s char* - 打印该集合s的大小以及该集合的所有元素
阅读(1350) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。
关于我们 | 关于IT168 | 联系方式 | 广告合作 | 法律声明 | 免费注册
Copyright 2001-2010 ChinaUnix.net All Rights Reserved 北京皓辰网域网络信息技术有限公司. 版权所有
感谢所有关心和支持过ChinaUnix的朋友们