当前位置: 首页 > article >正文

Android 原生程序使用gdb, addr2line, readelf调试

Platform: RK3368

OS: Android 6.0

Kernel: 3.10.0


文章目录

  • 一 gdb
    • 1. 原生程序添加调试符号
    • 2. 主机上adb push 编译好的原生程序到设备
    • 3. 设备上使用gdbserver运行原生程序
    • 4. 主机上设置adb端口转发
    • 5. 主机上运行gdb调试
  • 二 addr2line
  • 三 readelf

一 gdb

GDB(GNU Debugger)是GNU项目调试器,是一种强大的程序调试工具,可以用于调试C、C++、Fortran等多种编程语言编写的程序。它允许程序员在程序运行时监视程序的内部状态和程序的控制流程。

1. 原生程序添加调试符号

在原生程序的Android.mk中添加以下内容:

# 添加调试符号  
LOCAL_CFLAGS += -g -O0  
 
# 不剥离符号  
LOCAL_STRIP_MODULE := false  

2. 主机上adb push 编译好的原生程序到设备

$ adb push out/target/product/$YOUR_DEVICE/system/bin/$YOUR_NATIVE_PROCESS

3. 设备上使用gdbserver运行原生程序

$ gdbserver :9090 /system/bin/$YOUR_NATIVE_PROCESS

4. 主机上设置adb端口转发

表示将本地9090端口转发到设备9090端口:

$ adb forward tcp:9090 tcp:9090

5. 主机上运行gdb调试

$ gdb out/target/product/$YOUR_DEVICE/system/bin/$YOUR_NATIVE_PROCESS
GNU gdb (Ubuntu 15.0.50.20240403-0ubuntu1) 15.0.50.20240403-git
Copyright (C) 2024 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:
<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 out/target/product/***/system/bin/****...
(gdb) target remote :9090
Remote debugging using :9090
warning: while parsing target description (at line 1): Target description specified unknown architecture "arm"
warning: Could not load XML target description; ignoring
Reading /system/bin/linker64 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initializers
and track explicitly loaded dynamic code.
0x00000000 in ?? ()

二 addr2line

addr2line用于将程序的地址(例如,崩溃报告中提供的地址)转换为文件名和行号。这对于调试和定位程序中的错误非常有用,尤其是在处理崩溃或异常行为时。

一般在Android源码中source build/envsetup.sh和lunch以后, 就会有很多工具可以直接使用了, 例如:

$ arm-linux-androideabi-addr2line -f -e $YOUR_NATIVE_PROCESS

如果是64位程序需要使用aarch64下面的工具链, 否则会出现"File format not recognized"

$ /prebuilts/gcc/linux-x86/aarch64/aarch64-linux-android-4.9/bin/aarch64-linux-android-addr2line -e $YOUR_NATIVE_PROCESS

三 readelf

readelf是一个在Unix和类Unix系统上用于查看ELF(Executable and Linkable Format)文件信息的命令行工具。readelf能够显示ELF文件的各种信息,包括但不限于:

  • 文件头:描述ELF文件的总体信息,包括系统相关、类型相关、加载相关和链接相关等。
  • 程序头:包含程序执行时所需的各种段(segment)的信息。
  • 节头:描述ELF文件中各个节(section)的信息,节是ELF文件的基本组成部分。
  • 符号表:包含程序中定义的符号和引用的外部符号的信息。
    此外,readelf还支持查看动态节、调试信息、版本信息等。

例如查看符号表信息:

$ arm-linux-androideabi-readelf -s $YOUR_NATIVE_PROCESS

http://www.kler.cn/news/361051.html

相关文章:

  • CTFHUB技能树之XSS——DOM反射
  • JMeter详细介绍和相关概念
  • LLM----BERT+HuggingFace理论及基础入门实战
  • 【Flutter】基础入门:自定义Widget
  • 空文件夹,python项目来启动
  • 《Python基础教程》笔记(ch0-1)
  • 2024年软件设计师中级(软考中级)详细笔记【7】面向对象技术(下)23种设计模式(分值10+)
  • js读取.txt文件内容
  • RIP协议
  • 刷爆leetccode Day5
  • 7-1 大写字母转换为小写字母(C++;cin;cout)
  • 数据库表的创建
  • Spring配置/管理bean-IOC(控制反转) 非常详细!基于XML及其注解!案例分析! 建议复习收藏!
  • 开源图像超分ECBSR项目源码分析
  • L1练习-鸢尾花数据集处理(分类/聚类)
  • STM32CubeIDE(Eclipse)Post-build steps添加带参.exe实现全流程(2):带参调用.exe的几种方法
  • 搭建知识库 | 知识管理在汽车行业中的作用
  • 缓存框架JetCache源码解析-缓存变更通知机制
  • 建库建表练习
  • c语言 atoi 函数的功能和模拟实现