GDB查看STL容器数据

转载自:http://blog.chinaunix.net/uid-20594049-id-1620139.html

1. 下载 http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt

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调试:

(gdb) print vec[0]
One of the arguments you tried to pass to operator[] could not be converted to what the function wants.

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gdb是一款用于调试程序的工具,可以通过它来查看和分析程序运行时的状态。而core文件是在程序出现严重错误或崩溃时自动生成的,包含了程序在崩溃时的内存状态和调用栈信息。 使用gdb查看core文件可以帮助我们定位程序崩溃或错误的原因。首先,我们需要确保程序编译时开启了core文件生成的选项,可以通过在编译时使用"-g"参数来开启。当程序崩溃时,系统会生成一个core文件。 要使用gdb查看core文件,首先需要打开终端,并在终端中输入"gdb"命令以启动gdb。然后,使用"gdb <可执行文件路径> <core文件路径>"这样的命令来加载core文件。这样gdb就会将core文件和可执行文件关联起来,方便我们进行调试。 加载core文件后,我们可以使用一系列的gdb命令来查看和分析core文件。例如,使用"backtrace"命令可以查看崩溃时的调用栈信息。使用"up"和"down"命令可以在不同的栈帧之间切换,查看各个函数的调用关系。使用"print"命令可以查看变量的值。还可以使用"run"命令来重新运行程序,并在崩溃的位置设置断点以进行调试。 通过使用以上的命令和调试技巧,我们可以有效地利用gdb查看和分析core文件,找出程序崩溃或错误的原因,并进行修复和调试。这对于开发人员来说非常重要,可以帮助他们快速定位和解决问题,提高程序的稳定性和可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值