如何传递参数并将stdin从一个文件重定向到gdb中运行的程序?

时间:2021-03-15 23:18:46

I usually run a program as :

我通常以以下方式运行一个程序:

./a.out arg1 arg2 <file

I would like to debug it using gdb.

我想使用gdb调试它。

I am aware of the set args functionality, but that only works from the gdb prompt.

我知道set args功能,但它只能在gdb提示符下工作。

4 个解决方案

#1


130  

Pass the arguments to the run command from within gdb.

将参数从gdb内部传递给run命令。

$ gdb ./a.out
(gdb) r < t
Starting program: /dir/a.out < t

#2


406  

You can do this:

你可以这样做:

gdb --args path/to/executable -every -arg you can=think < of

The magic bit being --args.

神奇的是,args。

Just type run in the gdb command console to start debugging.

只需在gdb命令控制台中键入run以启动调试。

#3


3  

If you want to have bare run command in gdb to execute your program with redirections and arguments, you can use set args:

如果您想在gdb中使用裸运行命令来执行带有重定向和参数的程序,您可以使用set args:

% gdb ./a.out
(gdb) set args arg1 arg2 <file
(gdb) run

I was unable to achieve the same behaviour with --args parameter, gdb fiercely escapes the redirections, i.e.

我无法用-args参数实现同样的行为,gdb强烈地逃避重定向,即。

% gdb --args echo 1 2 "<file"
(gdb) show args
Argument list to give program being debugged when it is started is "1 2 \<file".
(gdb) run
...
1 2 <file
...

This one actually redirects the input of gdb itself, not what we really want here

这个实际上重定向gdb本身的输入,而不是我们真正想要的

% gdb --args echo 1 2 <file
zsh: no such file or directory: file

#4


1  

Start GDB on your project.

在项目上启动GDB。

  1. Go to project directory, where you've already compiled the project executable. Issue the command gdb and the name of the executable as below:

    转到项目目录,在那里您已经编译了项目可执行文件。发出命令gdb和可执行文件的名称如下:

    gdb projectExecutablename

    gdb projectExecutablename

This starts up gdb, prints the following: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. ................................................. Type "apropos word" to search for commands related to "word"... Reading symbols from projectExecutablename...done. (gdb)

这个启动gdb,打印以下:GNU gdb(Ubuntu 7.11.1-0ubuntu1 ~ 16.04)7.11.1版权(C)2016*软件基金会,公司.................................................输入“aprops word”来搜索与“word”相关的命令。从projectExecutablename…完成读取符号。(gdb)

  1. Before you start your program running, you want to set up your breakpoints. The break command allows you to do so. To set a breakpoint at the beginning of the function named main:

    在开始运行程序之前,您需要设置断点。break命令允许您这样做。在名为main的函数的开头设置断点:

    (gdb) b main

    主要(gdb)b

  2. Once you've have the (gdb) prompt, the run command starts the executable running. If the program you are debugging requires any command-line arguments, you specify them to the run command. If you wanted to run my program on the "xfiles" file (which is in a folder "mulder" in the project directory), you'd do the following:

    一旦有了(gdb)提示符,run命令将启动可执行文件的运行。如果正在调试的程序需要任何命令行参数,则将它们指定为run命令。如果您想在“xfiles”文件(项目目录中的“mulder”文件夹中)上运行我的程序,请执行以下操作:

    (gdb) r mulder/xfiles

    (gdb)r穆德/ xfiles

Hope this helps.

希望这个有帮助。

Disclaimer: This solution is not mine, it is adapted from https://web.stanford.edu/class/cs107/guide_gdb.html This short guide to gdb was, most probably, developed at Stanford University.

声明:这个解决方案不是我的,它改编自https://web.stanford.edu/class/cs107/guide_gdb.html。

#1


130  

Pass the arguments to the run command from within gdb.

将参数从gdb内部传递给run命令。

$ gdb ./a.out
(gdb) r < t
Starting program: /dir/a.out < t

#2


406  

You can do this:

你可以这样做:

gdb --args path/to/executable -every -arg you can=think < of

The magic bit being --args.

神奇的是,args。

Just type run in the gdb command console to start debugging.

只需在gdb命令控制台中键入run以启动调试。

#3


3  

If you want to have bare run command in gdb to execute your program with redirections and arguments, you can use set args:

如果您想在gdb中使用裸运行命令来执行带有重定向和参数的程序,您可以使用set args:

% gdb ./a.out
(gdb) set args arg1 arg2 <file
(gdb) run

I was unable to achieve the same behaviour with --args parameter, gdb fiercely escapes the redirections, i.e.

我无法用-args参数实现同样的行为,gdb强烈地逃避重定向,即。

% gdb --args echo 1 2 "<file"
(gdb) show args
Argument list to give program being debugged when it is started is "1 2 \<file".
(gdb) run
...
1 2 <file
...

This one actually redirects the input of gdb itself, not what we really want here

这个实际上重定向gdb本身的输入,而不是我们真正想要的

% gdb --args echo 1 2 <file
zsh: no such file or directory: file

#4


1  

Start GDB on your project.

在项目上启动GDB。

  1. Go to project directory, where you've already compiled the project executable. Issue the command gdb and the name of the executable as below:

    转到项目目录,在那里您已经编译了项目可执行文件。发出命令gdb和可执行文件的名称如下:

    gdb projectExecutablename

    gdb projectExecutablename

This starts up gdb, prints the following: GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1 Copyright (C) 2016 Free Software Foundation, Inc. ................................................. Type "apropos word" to search for commands related to "word"... Reading symbols from projectExecutablename...done. (gdb)

这个启动gdb,打印以下:GNU gdb(Ubuntu 7.11.1-0ubuntu1 ~ 16.04)7.11.1版权(C)2016*软件基金会,公司.................................................输入“aprops word”来搜索与“word”相关的命令。从projectExecutablename…完成读取符号。(gdb)

  1. Before you start your program running, you want to set up your breakpoints. The break command allows you to do so. To set a breakpoint at the beginning of the function named main:

    在开始运行程序之前,您需要设置断点。break命令允许您这样做。在名为main的函数的开头设置断点:

    (gdb) b main

    主要(gdb)b

  2. Once you've have the (gdb) prompt, the run command starts the executable running. If the program you are debugging requires any command-line arguments, you specify them to the run command. If you wanted to run my program on the "xfiles" file (which is in a folder "mulder" in the project directory), you'd do the following:

    一旦有了(gdb)提示符,run命令将启动可执行文件的运行。如果正在调试的程序需要任何命令行参数,则将它们指定为run命令。如果您想在“xfiles”文件(项目目录中的“mulder”文件夹中)上运行我的程序,请执行以下操作:

    (gdb) r mulder/xfiles

    (gdb)r穆德/ xfiles

Hope this helps.

希望这个有帮助。

Disclaimer: This solution is not mine, it is adapted from https://web.stanford.edu/class/cs107/guide_gdb.html This short guide to gdb was, most probably, developed at Stanford University.

声明:这个解决方案不是我的,它改编自https://web.stanford.edu/class/cs107/guide_gdb.html。