我有一个程序在基于ARM的嵌入式设备上运行。全局可访问的某个struct
每隔一段时间就会被转储到磁盘,并表示大约 160Kb 的数据。
我需要检查此结构的内容。到目前为止,我已经将 Python 脚本与 struct
库一起使用来解析该转储的内容,但这种方法不能很好地扩展。
我认为可以使用交叉编译的GDB程序来做到这一点。我想将该文件的内容复制回结构地址的内存中。所以这就是我尝试过的:
$ arm-eabi-gdb
....
(gdb) target sim
Connected to the simulator.
(gdb) file MS.elf
Reading symbols from MS.elf...done.
(gdb) p &my_struct
$1 = (MyStruct *) 0x6801149c
(gdb) restore ~/Dumps/MS_20121128_164606 binary 0x6801149c
You can't do that without a process to debug.
这是正确的方法吗?如果是,我做错了什么?
我会写一个小程序,在结构中读取,然后在调试模式打印它。
该程序需要在具有相同功能的系统上编译作为 ARM 的体系结构特征。 即相同大小的字符、整数、短、长、浮点数、双精度和指针。此外,字节顺序需要与 ARM 上的相同。
如果您经常查看结构,那么它将是值得漂亮地打印出结构的内容而不是不断使用 gdb 来查看内容。
由于结构相当大,我也会去使用放置结构的 API 的额外步骤在浏览器中的文件夹/文档类型树中,以便您可以放大或浏览结构的不同部分。如果有人对此感兴趣,请告诉我。
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
struct Foo {
/* contents of the struct go here */
};
int
main()
{
struct Foo tmp;
int fd, r, n = sizeof(tmp);
fd = open("struct_dump", O_RDONLY);
if (fd < 0) {
printf("could not open struct_dump.n");
exit(0);
}
r = read(fd, &tmp, n);
if (r != n) {
printf("mismatched struct sizes.n");
exit(0);
}
/*
* Stop here in gdb and do 'p tmp'
*/
close(fd);
exit(0);
}