当运行我的一些二进制文件时,我发现奇怪的是GDB不会转储数组,我不知道为什么这只会发生在某些可执行文件中。
声明非常简单:
tbl_account_t accounts[MAX_ACCOUNTS];
当我打印数组(只是任何数组)时,我得到了这个:
(gdb) print accounts
$16 = 0x618d20 <accounts>
确认变量类型,一切正常:
(gdb) ptype accounts
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)
我确实有数据:
(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)
使用“print accounts”命令,我希望GDB能够转储数组的整个内容,就像在其他可执行文件中一样。为什么会这样?
答案 0 :(得分:2)
我没有完整的答案,但我怀疑如果你分享更多的代码,我们可以弄清楚。看来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_account[10])accounts
答案 1 :(得分:1)
您还可以手动告诉GDB使用&#39; @&#39;打印多少条目。 如果您的数组未静态分配,或者您只想检查数组中的特定条目集,则效果会更好。
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>
}}
如果您只对索引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>
}}