GDB打印char数组中的所有值

mrQ*_*RTY 8 c gdb

我在我的数组中存储各种文件名,这些文件名由空字节分区.调试时,我只能看到第一个文件名.所以,例如,如果我的数组是这样的:hello.txt00000hello2.txt我只能看到hello.txt.如何打印整个阵列?我无法在其他地方找到这样的命令.

Cro*_*man 15

您可以使用x/999bc,999例如,数组的大小在哪里:

paul@thoth:~/src/sandbox$ gdb ./str
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 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".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/sandbox/str...done.
(gdb) list
1   int main(void) {
2       char * p = "hello\0world\0hahaha";
3       return 0;
4   }
5   
(gdb) b 3
Breakpoint 1 at 0x4004b8: file str.c, line 3.
(gdb) run
Starting program: /home/paul/src/sandbox/str 

Breakpoint 1, main () at str.c:3
3       return 0;
(gdb) print p
$1 = 0x40056c "hello"
(gdb) x/19bc p
0x40056c:   104 'h' 101 'e' 108 'l' 108 'l' 111 'o' 0 '\000'    119 'w' 111 'o'
0x400574:   114 'r' 108 'l' 100 'd' 0 '\000'    104 'h' 97 'a'  104 'h' 97 'a'
0x40057c:   104 'h' 97 'a'  0 '\000'
(gdb) 
Run Code Online (Sandbox Code Playgroud)


sbz*_*sbz 9

使用 gdb,您可以使用以下命令打印数组元素:

(gdb) print *array@size
Run Code Online (Sandbox Code Playgroud)

如果我的变量数组char*[]如下类型

const char *array[] = {"first","second","third"};
Run Code Online (Sandbox Code Playgroud)

然后我可以使用以下方法显示char*数组的第 2 个条目:

(gdb) print *array@2
$2 = { 0x..... "first", 0x..... "second"}
Run Code Online (Sandbox Code Playgroud)

使用它来显示程序的参数非常方便:

(gdb) print *argv@argc
Run Code Online (Sandbox Code Playgroud)

也可以使用x命令执行相同的操作x/Ns *argv,其中Nargc的整数值(即对于 argc = 2, x/2s *argv)

打印命令的全部魔法的文档在这里