[英]lldb/Xcode: how to print thread index, id or name?
问题:我想在断点的帮助下使用Xcode中的动作来跟踪程序,所以我感兴趣的是我的函数是否总是在一个线程中执行。
有手册: http : //lldb.llvm.org/formats.html ,它具有所有必需的变量,但是由于某些原因,它们在p / expr命令中不起作用。
所以我想要像p $ {thread.id}或expr-thread.id之类的东西,但是我没有运气。
我知道不好的方法是:
p / x(长)pthread_self()
并获得名称:
p new char [256] //它将返回合适的指针,例如$ 3 = 0x000000007480840
p(int)pthread_getname_np((pthread_t)yourId,$ 3,(size_t)256)//将线程名写入缓冲区
p $ 3 //您将看到它的名字
p delete $ 3 //如果担心内存泄漏
但它看起来是一个不好的解决方法,并且不太适合断点。
“ formats.html”页面中提到的格式不是用于表达式,而是用于在lldb打印时打印线程和框架信息的方式。 例如,我有这个:
settings set thread-format thread #${thread.index}: tid = ${thread.id}{, name = ${thread.name}}{, function: ${function.name}} {, stop reason = ${thread.stop-reason}}{, return = ${thread.return-value}}\n
在我的.lldbinit中,这样我可以在停止时看到线程ID和名称。
如果您在Xcode中运行,则通常不会在停止处看到线程信息,因为Xcode不会在每次停止时都向Xcode控制台回显。 但是您仍然可以使用“线程信息”命令来调用其中一些信息:
(lldb) thread info
thread #1: tid = 0x34ca69, name = A_Cool_Thread, function: -[SKTGraphicView alignLeftEdges:] , stop reason = breakpoint 2.1
因此,出于您的目的,您可以在所需的断点上放置一个breakpoint命令,并使该命令为“线程信息”。 然后,每站都会显示ID和名称以及其他内容。
请注意,另一种执行相同操作的方法是使用Python断点命令,例如:
(lldb) breakpoint command add -s python <BPNO>
Enter your Python command(s). Type 'DONE' to end.
def function (frame, bp_loc, internal_dict):
"""frame: the lldb.SBFrame for the location at which you stopped
bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
internal_dict: an LLDB support object not to be used"""
print "Thread ID is: ", frame.thread.GetThreadID(), " and name: ", frame.thread.GetName()
DONE
(lldb)
然后,每次您到达断点时,您都会看到类似以下内容的内容:
Thread id is: 3459689 and name: A_Cool_Thread
顺便说一句,您没有说您使用的是什么系统,但是在Mac OS X上,此处列出的线程ID不是pthread ID。 pthread ID仅保证在程序中在给定时间存在的所有线程都是唯一的,因此,尽管程序在给定时间的每个线程将具有不同的pthread ID,但不能保证在不同时间有两个线程会具有不同的pthread ID。 Mac OS X有一个“全局唯一的线程ID”,但是,在程序运行期间它是唯一的。 这就是该线程ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.