在运行我的一些二进制文件时,我发现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) 使用“打印帐户”命令,我期望GDB像在其他可执行文件中一样,转储数组的全部内容。为什么会发生这种事?
发布于 2016-09-13 19:50:10
我还没有完全的答案,但我怀疑如果您共享更多的代码,我们可以解决它。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
发布于 2016-09-16 10:20:00
您还可以使用“@”手动告诉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>
}}如果你只对索引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>
}}https://stackoverflow.com/questions/39475537
复制相似问题