openharmony 在linux gdb/gdbserver调试
gdb作为c/c++开发的重要性不亚于庖丁的解牛刀,鲁班手上的斧头,是理解代码,定位问题,分析问题的不二之选,可以快速帮助开发者熟悉代码,以及解决异常问题。本文目的在于介绍一种在openharmony上使用gdb/gdbserver进行远程调试环境搭建的过程。根据本文方法可以调试几乎大部分的用户态用户程序,对于xts问题定位,崩溃,代码理解,内核问题辅助都有非常直接的帮助。
1 调试环境准备
硬件:
整个调试全部基于linux系统+openharmony开发板,不支持windows系统,所以要想使用此方法的用户最好按照openharmony linux开发环境 这个文档搭建开发环境。
软件:
linux开发主机,配置好gdb交叉调试环境,开发板需要支持gdbserver,为了方便最好使用静态编译的gdbserver。
网络:
由于是远程调试,所以需要网络,推荐使用hdc端口转发,如果不行也可以用网线,保证网络能够通畅。
2 准备调试工具
由于鸿蒙使用的编译器为clang,随着版本的更新有些linux自带的gdb-multiarch可能无法直接使用,可能需要自己准备新版本的gdb工具。
gdb-multiarch exe.unstripped/multimedia/media_library/scanner
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
BFD: /home/ubuntu/openharmony/out/rk3568/exe.unstripped/multimedia/media_library/scanner: unknown type [0x13] section `.relr.dyn'
Reading symbols from exe.unstripped/multimedia/media_library/scanner...
如果出现上述unknown type [0x13] section `.relr.dyn'问题就是因为你使用的gdb版本过低导致。这个时候可以删除build/config/compiler/BUILD.gn和arkcompiler/toolchain/build/config/compiler/BUILD.gn文件中的--pack-dyn-relocs=android+relr 选项就可以使用低版本使用。
但是推荐使用新版本的gdb工具,进行调试,如果你的gdb-multiarch没有问题,那么恭喜你,你可以直接使用了跳过准备调试工具这一章节了。
3 编译调试工具
推荐使用buildroot去编译,可以直接 git clone https://gitee.com/qq2820/buildroot-tool-ohos.git,然后make即可。
目前此项目使用的静态编译,包含了一些常用的工具,这些工具都可以直接在鸿蒙上使用,编译完整可以验证一下。
file output/target/usr/bin/gdbserver
output/target/usr/bin/gdbserver: ELF 64-bit LSB executable, ARM aarch64, version 1 (SYSV), statically linked, stripped
./output/host/bin/aarch64-linux-gdb ~/openharmony/out/rk3568/exe.unstripped/graphic/graphic_2d/bootanimation
GNU gdb (GDB) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "--host=x86_64-pc-linux-gnu --target=aarch64-buildroot-linux-musl".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /home/ubuntu/openharmony/out/rk3568/exe.unstripped/graphic/graphic_2d/bootanimation...
可以看到该程序是arm64的静态编译,注意的是这个静态编译的64是可以在32位编译的rk3568上运行的。aarch64-linux-gdb也可以成功加载符号,没有出现之前的unknown type [0x13] section `.relr.dyn'错误信息。
4 上传资源到开发板
hdc shell mount -o rw,remount /
hdc file send output/target/usr/bin/gdbserver /system/bin/
hdc shell chmod +x /system/bin/gdbserver
此外对于要调试的应用或者动态库需要复制带符号的到开发板替换原始文件。最好确认一下是不是带符号的。
find -iname bootanimation
./graphic/graphic_2d/bootanimation
./gen/foundation/graphic/graphic_2d/frameworks/bootanimation
./gen/device/board/hihope/rk3568/bootanimation
./packages/phone/system/bin/bootanimation
./tests/unittest/graphic_2d/bootanimation
./exe.unstripped/graphic/graphic_2d/bootanimation
./obj/foundation/graphic/graphic_2d/frameworks/bootanimation
./obj/foundation/graphic/graphic_2d/frameworks/bootanimation/src/bootanimation
./obj/device/board/hihope/rk3568/bootanimation
./obj/device/board/hihope/rk3568/bootanimation/device/board/hihope/rk3568/bootanimation
./module_list_files/graphic_2d/bootanimation
file ./graphic/graphic_2d/bootanimation
./graphic/graphic_2d/bootanimation: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-arm.so.1, BuildID[md5/uuid]=4f9a9924d6a0d71a9276f68d85209dc0, stripped
file ./exe.unstripped/graphic/graphic_2d/bootanimation
./exe.unstripped/graphic/graphic_2d/bootanimation: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-arm.so.1, BuildID[md5/uuid]=4f9a9924d6a0d71a9276f68d85209dc0, with debug_info, not stripped
可以看到带有debug_info,带符号的都在exe.unstripped和lib.unstripped目录中。
5开始调试
一下以bootanimation进程为例
调试之前需要注意的是openharmony目前的代码是用O2甚至O3来编译的,而且不管是不是debug状态,这个状态下可能会出现gdb无法识别符号,或者是找不到变量值的问题,这是正常的。如果这个影响到问题定位,可以临时使用编译宏来禁用局部代码优化来解决
#pragma clang optimize push
#pragma clang optimize O0
// 这里的代码将使用O0编译方便使用gdb定位问题
#pragma clang optimize pop
使用O0可能会出现编译失败,这类问题可能是目前有部分代码没有严格按照c++标准来写,建议只在关键代码处使用。
如果是已经运行的程序可以使用gdbserver 0.0.0.0:1234 --attach 800 此命令为调试800号进程,
具体的gdb使用常用技巧可以自己去寻找相关资料。
更多推荐
所有评论(0)