您可以使用 lldb 的用户定义变量。来自help expression:
用户定义变量:
You can define your own variables for convenience or to be used in subsequent
expressions. You define them the same way you would define variables in C.
If the first character of your user defined variable is a $, then the
variable's value will be available in future expressions, otherwise it will
just be available in the current expression.
在您的示例中,您将创建一个名为$thread 的用户定义变量以供以后使用。然后运行finish(又名thread step-out)来完成pthread_create。此时,运行p $thread 将显示该函数创建的线程。
生成的断点命令如下所示:
p pthread_t *$thread = (pthread_t *)$arg1
finish
p *$thread
bt
continue
$arg1 是用于传递第一个参数的寄存器的 lldb 别名。
更新
正如评论中所指出的,在断点命令中使用 finish 会导致“错误:在命令 #2 之后中止读取命令:‘完成’继续目标”。
一种解决方案是使用 Python 创建一个自定义的finish 命令来避免该问题。此函数运行StepOutOfFrame(),但在此之前它将 lldb 置于同步模式。我已经使用 Xcode 11.3.1 中的 lldb-1100.0.30.12 对此进行了测试。
import lldb
@lldb.command("Finish")
def finish(debugger, expression, context, result, _internal):
is_async = debugger.GetAsync()
debugger.SetAsync(False)
context.thread.StepOutOfFrame(context.frame)
debugger.SetAsync(is_async)
在您的~/.lldbinit 中,导入此脚本:
command script import path/to/finish.py
现在使用自定义的Finish 命令代替断点命令中的finish(大写名称不会覆盖内置的finish)。