macOS上的Eclipse CDT有一个错误,迫使我使用.gdbinit文件来指定C程序进行调试的命令行参数(请参阅https://bugs.eclipse.org/bugs/show_bug.cgi?id=516027)。在macOS上通常使用gdb时,还有一个问题需要设置“ start-with-shell off”选项(请参见Unknown ending signal when using debugger gdb)。只要命令行选项中没有空格,此方法就可以正常工作。
设置“ startup-with-shell off”后,如何在gdb命令文件中引用“ set args”的字符串?
特定示例:我想将两个参数传递给C程序,参数“ a b”包含一个空白:
a b
c
写入.gdbinit文件的语法是什么,以便gdb调用的程序获得两个参数“ a b”和“ c”(不带引号)。
我尝试了以下操作:
set args "a b" c
结果:程序获取三个参数,包括文字引号:
"a
b"
c
set args a\ b c
结果:程序获得三个参数,包括文字反斜杠:a\
b
c
复制步骤:
$ cat setargs.gdb
set startup-with-shell off
set args "a b" c
run
$ cat showargs.c
#include <stdio.h>
int
main(int argc, char *argv[])
{
int i;
printf("argc: %d\n", argc);
for (i = 0; i < argc; i += 1)
{
printf("arg %d: '%s'\n", i, argv[i]);
}
return 0;
}
$ gcc -o showargs showargs.c
$ gdb -x setargs.gdb showargs
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from showargs...(no debugging symbols found)...done.
argc: 4
arg 0: '/home_disk/stm/CTEST/showargs'
arg 1: '"a'
arg 2: 'b"'
arg 3: 'c'
[Inferior 1 (process 22518) exited normally]
(gdb) quit