U-boot下进行gdb 调试

1、编译选项设置

添加调试选项-g 保留调试信息,否则不能使用GDB进行调试。
例如,对test.c 文件进行调试编译
test.c :

int main()
{
    return 0;
};
gcc test.c -o test -g

在 u-boot 中需要在Makefile 中打开 -g 编译选项
在这里插入图片描述

生成的debug 信息如下, 可以通过 readelf -S u-boot|grep debug
来查看。其中 .debug_xxxx 存放的就是调试信息, .rela.debug_xxxx 是记录debug段的动态链接重定位信息
在这里插入图片描述

2、指定源码路径

在查看源码之前,首先要确保我们的程序能够关联到源码,一般来说,我们在自己的机器上加上-g参数编译完之后,使用gdb都能查看到源码, 但是如果源代码和gdb调试的平台不是同一个怎么办呢?

由于gcc -g 编译的时候,将绝对路径或者相对路径,保存在.debug_str中,如果源文件的路径和.debug_str中的路径不一致,就无法显示code

可以通过如下命令查看绝对路径:

readelf  xxxx  -p .debug_str 

在这里插入图片描述

可以看到里面保存是完整的绝对路径/home/code/demo/uboo
GDB提供一个方法,可以让你修改源码搜索路径,把源码绝对路径里面的一个path映射到另一个path上去,这样即使你 .debug_str 里面的是绝对路径,源码也可以放到另外的目录
命令如下:

set substitute-path  将原来的路径替换为新的路径

如果原来的路径/home/code/demo/uboo(.debug_str),新的路径为/home/code/demo/uboo/temp

(gdb) set substitute-path //home/code/demo/uboo  /home/code/demo/uboo/temp
(gdb) show substitute-path
List of all source path substitution rules:
  `/home/code/demo/uboo' -> `/home/code/demo/uboo/temp'.

3、u-boot 重定向的问题

当你完成上面操作, 如果是普通代码,就已经ok了, 但是如果是u-boot 那么执行下来会显示 ?????, 具体原因就是u-boot 进行了重定向。

所谓的relocation,就是重定位,uboot运行后会将自身代码拷贝到sdram的另一个位置继续运行,这个在uboot启动流程分析中说过。

一个完整可运行的bin文件,link时指定的链接地址,load时的加载地址,运行时的运行地址,这3个地址应该是一致的。(裸机的情况下)

新版uboot不管uboot的load addr(entry pointer)在哪里,启动后会计算出一个靠近sdram顶端的地址,将自身代码拷贝到该地址,继续运行。

一是为kernel腾出低端空间,防止kernel解压覆盖uboot,二是对于由静态存储器(spiflash nandflash)启动,这个relocation是必须的。

add-symbol-file filename address

The add-symbol-file command reads additional symbol table information
from the file filename. You would use this command when filename has
been dynamically loaded (by some other means) into the program that is
running. address should be the memory address at which the file has
been loaded;

例如, u-boot 文件被重定向到0xf5000000的位置, 那么实现如下:

(gdb) add-symbol-file u-boot 0xf5000000

这个0x0xf5000000地址需要由u-boot入口地址+relocate偏移地址组成

猜你喜欢

转载自blog.csdn.net/shenjin_s/article/details/124749657
今日推荐