macOS上的EclipseCdt有一个bug,它迫使我使用.gdbinit文件来指定要调试的C程序的命令行参数(请参见
https://bugs.eclipse.org/bugs/show_bug.cgi?id=516027
). 在macOS上使用gdb也有一个问题,需要设置“startup with shell off”选项(请参见
Unknown ending signal when using debugger gdb
). 只要命令行选项中没有空格,这就可以正常工作。
当设置了“startup with shell off”时,如何引用gdb命令文件中“set args”的字符串?
a b
c
写入.gdbinit文件的语法是什么,以便gdb调用的程序得到两个参数“ab”和“c”(不带引号)。
我尝试了以下方法:
set args "a b" c
结果:程序得到三个参数,包括文字引号:
"a
b"
c
set args a\ b c
结果:程序得到三个参数,包括字面反斜杠:
a\
b
复制步骤:
$ 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