gdb调试

1、程序

#include <sstream>
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
    stringstream strStream;
    strStream << "argc: " << argc << "\t";
    for (int i = 0; i < argc; ++i)
    {
        strStream << "argv, " << i << ":" << argv[i] << endl;
    }
    cout << strStream.str().c_str();
    return 0;
}

 

2、编译

c++ -g -o test test.cpp

chmod 777 test

 

3、调试

1)不带参数

[aaa@tmp]$ gdb test
GNU gdb Fedora (6.8-37.el5)
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 "x86_64-redhat-linux-gnu"...
(gdb) r
Starting program: /home/aaa/tmp/test
argc: 1 argv, 0:/home/aaa/tmp/test

Program exited normally.
(gdb) quit
[aaa@tmp]$

 

2)带参数

方法1:

[aaa@tmp]$ gdb --args test 1 2 3 4 5 6 7 8 9 10
GNU gdb Fedora (6.8-37.el5)
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 "x86_64-redhat-linux-gnu"...

(gdb) r
Starting program: /home/aaa/tmp/test 1 2 3 4 5 6 7 8 9 10
argc: 11        argv, 0:/home/aaa/tmp/test
argv, 1:1
argv, 2:2
argv, 3:3
argv, 4:4
argv, 5:5
argv, 6:6
argv, 7:7
argv, 8:8
argv, 9:9
argv, 10:10

Program exited normally.
(gdb) quit
[aaa@tmp]$

方法2:

[aaa@tmp]$ gdb test
GNU gdb Fedora (6.8-37.el5)
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 "x86_64-redhat-linux-gnu"...
(gdb) set args 1 2 3 4 5 6 7 8 9 10
(gdb) r
Starting program: /home/aaa/tmp/test 1 2 3 4 5 6 7 8 9 10
argc: 11        argv, 0:/home/aaa/tmp/test
argv, 1:1
argv, 2:2
argv, 3:3
argv, 4:4
argv, 5:5
argv, 6:6
argv, 7:7
argv, 8:8
argv, 9:9
argv, 10:10

Program exited normally.
(gdb) quit
[aaa@tmp]$

 

posted on 2012-08-05 12:43  人间爱  阅读(306)  评论(0编辑  收藏  举报

导航