【问题标题】:GDB print all values in char arrayGDB 打印 char 数组中的所有值
【发布时间】:2015-04-09 02:30:59
【问题描述】:

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

【问题讨论】:

  • 在您的示例中,字符串由一系列0s 分区,这与空字节不同。还是只是为了说明目的?
  • 另外,还有很多调试方法。你是如何打印你的数组的?
  • @ReticulatedSpline 它只是一个插图。我使用 memset 将它们全部设置为值 0。
  • @Brean 我只使用打印

标签: c gdb


【解决方案1】:

您可以使用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) 

【讨论】:

    【解决方案2】:

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

    (gdb) print *array@size
    

    如果我的变量 arraychar*[] 类型,如下所示

    const char *array[] = {"first","second","third"};
    

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

    (gdb) print *array@2
    $2 = { 0x..... "first", 0x..... "second"}
    

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

    (gdb) print *argv@argc
    

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

    有关 print 命令的全部魔力的文档是 here

    【讨论】:

    • 就可读性而言,我认为这比公认的答案更受欢迎。
    【解决方案3】:

    您可以尝试将数组定义为:

    char ** array;
    
    array = malloc( NUM_ROWS*sizeof char* );
    for( int i =0; i < NUM_ROWS; i++ )
    {
        *array[i] = malloc( NUM_COLUMNS )
    } 
    

    然后代码就可以了

    memset( array[x], '\0', NUM_COLUMNS );
    strncpy(array[x], myString, NUM_COLUMNS-1);
    

    其中 myString 是要放置在该行中的数据 和

    for( int i = 0; i < NUM_ROWS; i++ )
    {
        if( array[i] )
        { // only enters this code block if something placed in row
            printf( "%s\n", array[x] );
        }
    }
    

    然后对数组中的每一行使用'p array[x]'

    【讨论】:

    • 当然,代码还需要检查每个malloc的返回值以确保操作成功,并在代码的最后将每一行地址传递给free,然后将数组传递给free跨度>
    【解决方案4】:

    如果您有一个固定长度的数组并想查看其中的所有数据 - 只需要求打印该数组即可获得完整的输出,因为 GDB 知道大小。
    如果你有一个指向固定长度数组的指针,那么 GDB 会假定最常见的情况——一个 C 字符串,所以它会在第一个十六进制 null 处停止显示。要查看更多信息:取消引用并将结果转换为具有您想要查看的预期长度的 char 数组。

    #include <stdio.h>
    static char myarr[55] = "hello.txt\x00\x00\x00\x000hello2.txt";
    
    int main () {
       char *p = myarr;
       puts (p);
       return 0;
    }
    

    编译并运行为gcc -g test.c &amp;&amp; gdb -q -ex start ./a.out:

    Reading symbols from /tmp/a.out...done.
    Temporary breakpoint 1 at 0x400535: file test.c, line 5.
    Starting program: /tmp/a.out
    
    Temporary breakpoint 1, main () at test.c:5
    5          char *p = myarr;
    (gdb) step
    6          puts (p);
    (gdb) print p
    $1 = 0x601060 <myarr> "hello.txt"
    (gdb) print *p
    $2 = 104 'h'
    (gdb) print (char[20])*p
    $3 = "hello.txt\000\000\000\000hello2."
    (gdb) print (char[55])*p
    $4 = "hello.txt\000\000\000\000hello2.txt", '\000' <repeats 31 times>
    (gdb) detach
    Detaching from program: /tmp/a.out, process 456
    hello.txt
    (gdb) quit
    

    如果您希望以十六进制而不是八进制打印序列 - 请查看 54469844

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-06
      • 2012-11-11
      • 2019-03-29
      • 2015-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多