Android C++ 开发调试 LLDB 工具的使用

发布于:2024-05-05 ⋅ 阅读:(33) ⋅ 点赞:(0)


Android 中在进行 NDK 开发的时候,我们经常需要进行 C++ 代码调试,这里对调试工具 LLDB (Low Lever Debugger)的使用进行介绍。
在 lldb 的官方文档中对 LLDB 的介绍如下:

LLDB is the default debugger in Xcode on macOS and supports debugging C, Objective-C and C++ on the desktop and iOS devices and simulator.

但是到目前为止,Android 上的 NDK 开发也可以使用 LLDB 进行调试,这里记录这么强大且通用的工具相关用法,后续会持续补充。

调试环境准备

因为自己主要做 Android ,因此自己这里使用 Android NDK 项目学习使用 LLDB 调试 c++ 。这里自己使用 github 上的 google ndk jni callback sample 项目。项目跑起来以后,我们在 c++ 代码中加断点,然后就可以看到底部有一个 LLDB 窗口会出来,然后我们就可以在这个窗口输入命令:
在这里插入图片描述

基础命令

最基础的命令 lldb help 查看命令使用 :

help <command> <subcommand>

这里我们输入

help breakpoint # 使用该命令我们可以查看 breakpoint 的用法
help breakpoint set # 使用该命令我们可以查看 breakpoint 的子命令 set 的用法,同样也可以再增加 set 的 subcommand 查看用法

查看官网的文档中的 GDB to LLDB command map ,在使用 LLDB 命令时,可以将命令主要分为下面几类(将日常用使用不到的命令暂时忽略,等有需要再去查看用法):

Breakpoint Commands

设置断点

指定方法名称设置断点:Set a breakpoint at all functions named main

# 给所有的方法名称为 main 的函数设置断点
(lldb) breakpoint set --name main
(lldb) br s -n main
(lldb) b main

指定文件名和行号设置断点:Set a breakpoint in file test.c at line 12

# 指定 test.c 文件,在文件的第 12 行设置
(lldb) breakpoint set --file test.c --line 12
(lldb) br s -f test.c -l 12
(lldb) b test.c:12

设置断点的时候增加条件:Set a conditional breakpoint

(lldb) breakpoint set --name foo --condition '(int)strcmp(y,"hello") == 0'
(lldb) br s -n foo -c '(int)strcmp(y,"hello") == 0'

列出当前的所有断点:List all breakpoints

(lldb) breakpoint list
(lldb) br l

删除断点:Delete a breakpoint

(lldb) breakpoint delete 1 #(这里的数字通过 br l 查看所有的断点信息,最前面的数字可以写到这里)
(lldb) br del 1

禁用或者启用断点:Disable/Enable a breakpoint

(lldb) breakpoint disable/enable 1
(lldb) br dis/en 1
Watchpoint Commands

自己在 android 上 lldb 测试,这个命令不生效,(日常也用这种调试方式)

Examining Variables

显示当前 frame 中的局部变量和参数 :
Show the arguments and local variables for the current frame

frame variable
fr v

显示 frame 中的变量的内容:Show the contents of local variable var

frame variable var

显示全局变量中的内容 : Show the contents of global variable

(lldb) target variable xxx
(lldb) ta v xxx

打印数组的内容:Print an array in memory, assuming we have a pointer like int/float/** *ptr

# int arr[] = {10, 20, 30, 40, 50}; // 声明并初始化一个整数数组    int *ptr; // 声明一个指向int的指针
# ptr = arr; // 让ptr指向arr数组的首元素;等同于 ptr = &arr[0];

# float myArray[5] = {1.2f, 2.0f, 3.0f, 4.0f, 5.0f};
# float* fMyArray = myArray;
# parray <COUNT> <EXPRESSION>
(lldb) parray 5 fMyArray
Evaluating Expressions

打印 c/c++/oc 对象的描述:Printing the ObjC “description” of an object

(lldb) po [SomeClass returnAnObject] # po + 变量名称
Examining Thread State

列出当前 program 中的线程:List the threads in your program

(lldb) thread list

切换后续命令操作的线程:Select thread 1 as the default thread for subsequent commands

(lldb) thread select 1
(lldb) t 1

展示当前线程的调用 tace:Show the stack backtrace for the current thread

(lldb) thread backtrace
(lldb) bt

从给定的地址开始读取 内存

(lldb) memory read `argv[0]`

从指定的变量开始读取指定字节的内存,结果输出到文本文件中:

memory read --outfile /Users/lxd/Desktop/mem.txt --count 512 fMyArray

将文件保存为二进制文件:Save binary memory data starting at 0x1000 and ending at 0x2000 to a file

(lldb) memory read --outfile /Users/lxd/Desktop/mem.bin --binary 0x1000 0x2000
(lldb) me r -o /tmp/mem.bin -b 0x1000 0x2000
Executable and Shared Library Query Commands

这个自己暂时用不上

参考:

链接:https://lldb.llvm.org/use/map.html#execution-commands