为什么GDB不打印阵列?

为什么GDB不打印阵列?,gdb,Gdb,当运行我的一些二进制文件时,我发现GDB没有转储数组很奇怪,我不知道为什么这只发生在一些可执行文件中 宣言非常简单: tbl_account_t accounts[MAX_ACCOUNTS]; 当我打印数组(只是任何数组)时,我得到以下结果: (gdb) print accounts $16 = 0x618d20 <accounts> 我确实有数据在里面: (gdb) print accounts[1] $18 = {email_len = 16, passwor

当运行我的一些二进制文件时,我发现GDB没有转储数组很奇怪,我不知道为什么这只发生在一些可执行文件中

宣言非常简单:

 tbl_account_t       accounts[MAX_ACCOUNTS]; 
当我打印数组(只是任何数组)时,我得到以下结果:

(gdb) print accounts
$16 = 0x618d20 <accounts>
我确实有数据在里面:

(gdb) print accounts[1]
$18 = {email_len = 16, password_len = 3, auto_log_in = 0 '\000', reserved_char = "\000\000", reserved_int = 0, email = "abra@cadabra.com", '\000' <repeats 47 times>, 
  password = "123", '\000' <repeats 21 times>}
(gdb) 
(gdb)打印帐户[1]
$18={email\u len=16,password\u len=3,auto\u log\u in=0'\000',reserved\u char=“\000\000”,reserved\u int=0,email=”abra@cadabra.com", '\000' , 
password=“123”,'\000'}
(gdb)

使用“print accounts”命令,我希望GDB像在其他可执行文件中一样转储整个数组的内容。为什么会发生这种情况

我没有完整的答案,但我怀疑如果你分享更多的代码,我们就能找到答案。gdb似乎不知道数组的长度

以这个项目为例

struct tbl_account {
    unsigned short email_len;
    unsigned short password_len;
    char auto_log_in;
    char reserved_char[3];
    int reserved_int;
    char email[64];
    char password[25];
};


int main(int argc, char* agv[])
{

tbl_account table[10];
tbl_account* table_ptr = table;
return 0;
}
现在是gdb会话

(gdb) b main
Breakpoint 1 at 0x4006e5: file junk.cpp, line 22.
(gdb) r
Starting program: /tmp/a.out 

Breakpoint 1, main (argc=1, agv=0x7fffffffdfe8) at junk.cpp:22
22  tbl_account* table_ptr = table;
(gdb) n
23  return 0;
(gdb) ptype table_ptr
type = struct tbl_account {
    unsigned short email_len;
    unsigned short password_len;
    char auto_log_in;
    char reserved_char[3];
    int reserved_int;
    char email[64];
    char password[25];
} *
(gdb) ptype table
type = struct tbl_account {
    unsigned short email_len;
    unsigned short password_len;
    char auto_log_in;
    char reserved_char[3];
    int reserved_int;
    char email[64];
    char password[25];
} [10]
请注意指针的ptype是如何由“*”表示的。我们还可以看到,数组类型由[10]表示。在我的示例中,包含了数组大小,gdb将打印每个元素。出于某种原因,在您的示例中,不包括数组的大小。也许它是一个
extern
变量

由于gdb不确定数组的大小,所以它只是将其作为指针值打印出来。作为一个工作环境,请尝试铸造


(gdb)p(tbl_账户[10])账户

您还可以使用“@”手动告诉gdb要打印多少条记录。 如果您的数组不是静态分配的,或者您只想检查数组中的特定项集,那么这种方法会更好

struct tbl_account {
    int id;
    char email[64];
    char password[25];
};

int main(void)
{
    struct tbl_account table[] = {
            {1, "abc", "1234"}, 
            {2, "def", "5678"}, 
            {3, "ghi", "1011"}, 
            {4, "jkl", "1213"}};

    return 0;
}
然后告诉GDB打印数组中的前3个条目

(gdb) p table[0]@3
$3 = {{
    id = 1, 
    email = "abc", '\000' <repeats 60 times>, 
    password = "1234", '\000' <repeats 20 times>
  }, {
    id = 2, 
    email = "def", '\000' <repeats 60 times>, 
    password = "5678", '\000' <repeats 20 times>
  }, {
    id = 3, 
    email = "ghi", '\000' <repeats 60 times>, 
    password = "1011", '\000' <repeats 20 times>
  }}
(gdb)p表[0]@3
$3 = {{
id=1,
email=“abc”、“\000”,
password=“1234”,“\000”
}, {
id=2,
email=“def”、“\000”,
password=“5678”,“\000”
}, {
id=3,
email=“ghi”、“\000”,
password=“1011”,“\000”
}}
如果您只对例如的索引2-3感兴趣,您可以这样做-

(gdb) p table[2]@2
$5 = {{
    id = 3, 
    email = "ghi", '\000' <repeats 60 times>, 
    password = "1011", '\000' <repeats 20 times>
  }, {
    id = 4, 
    email = "jkl", '\000' <repeats 60 times>, 
    password = "1213", '\000' <repeats 20 times>
  }}
(gdb)p表[2]@2
$5 = {{
id=3,
email=“ghi”、“\000”,
password=“1011”,“\000”
}, {
id=4,
email=“jkl”、“\000”,
密码=“1213”,“\000”
}}

非常感谢,您帮助我找到了问题所在。我没有在main.c中包含标题“constants.h”,这是在我的源代码中声明MAX_ACCOUNTS的地方。对于GCC,这不是问题,因此没有编译错误。但显然,GDB并没有在包含的较低层次上查找符号定义。通过将#include“constants.h”添加到main.c中,GDB可以找到accounts[]数组的大小,现在它可以很好地打印出来。我找到了这一问题的真正原因。它不是包含“常数.h”的方法。碰巧我在一个库函数(我自己的库,但它是一个完全不同的共享对象)中跟踪代码,并且该库使用以下声明:“extern tbl_account_t accounts[]”,这就是为什么只打印指针。哈!用外人的猜测确定了它
(gdb) p table[2]@2
$5 = {{
    id = 3, 
    email = "ghi", '\000' <repeats 60 times>, 
    password = "1011", '\000' <repeats 20 times>
  }, {
    id = 4, 
    email = "jkl", '\000' <repeats 60 times>, 
    password = "1213", '\000' <repeats 20 times>
  }}