gdb的run命令

一 格式

run arg1 arg2

二 传参数给程序并运行程序

1 源代码

#include 
using namespace std;
int main(int argc, char *argv[])
{     
    int i;
    if(argc==3)
    {
        cout<<"argc="<

2 编译

[root@localhost test]# g++ test2.cpp -g -o test2

3 用gdb加载test2,并运行

[root@localhost test]# gdb test2
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7_4.1
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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".
For bug reporting instructions, please see:
...
Reading symbols from /root/C++/ch02/2.5/test/test2...done.
(gdb) run boy girl
Starting program: /root/C++/ch02/2.5/test/test2 boy girl
argc=3
argv[0]=/root/C++/ch02/2.5/test/test2
argv[1]=boy
argv[2]=girl
[Inferior 1 (process 1033) exited normally]
Missing separate debuginfos, use: debuginfo-install glibc-2.17-196.el7_4.2.x86_64 libgcc-4.8.5-16.el7_4.1.x86_64 libstdc++-4.8.5-16.el7_4.1.x86_64

4 显示传给main的参数

(gdb) show args
Argument list to give program being debugged when it is started is "boy girl".

5 重写设置传给main的参数

(gdb) run
Starting program: /root/C++/ch02/2.5/test/test2 boy girl
argc=3
argv[0]=/root/C++/ch02/2.5/test/test2
argv[1]=boy
argv[2]=girl
[Inferior 1 (process 1038) exited normally]
(gdb) set args dad mum
(gdb) run
Starting program: /root/C++/ch02/2.5/test/test2 dad mum
argc=3
argv[0]=/root/C++/ch02/2.5/test/test2
argv[1]=dad
argv[2]=mum
[Inferior 1 (process 1039) exited normally]

设置新参数dad和mum,然后运行,就可以看到程序输出了新传入的参数。

你可能感兴趣的:(C++)