2014-09-29 57 views
0

问题:我想用Xcode中的断点帮助跟踪程序,所以我感兴趣的是如果我的函数总是在一个线程中执行。lldb/Xcode:如何打印线索索引,id或名字?

有一个手册:http://lldb.llvm.org/formats.html它具有所有必需的变量,但它们由于某种原因与p/expr命令不起作用。

所以我想要像p $ {thread.id}或expr - thread.id这样的东西,但我没有和他们一起运气。

我知道的方法是坏的是:

P/X(长)pthread_self()

,并获得名字:

p新的char [256] //它会返回合适的指针,例如$ 3 = 0x000000007480840

p(int)pthread_getname_np((pthread_t)yourId,$ 3,(size_t)256)// i牛逼写线程名到缓冲区

P $ // 3,你会看到它的名字

p删除$ // 3,如果你担心内存泄漏

,但它看起来相当不好的解决办法,并不适合断点。

+0

已知这些变量不适用于表达式,它完全由设计决定。如果你想在表达式中检索线程信息,你需要像在代码中那样做。 – 2014-09-29 18:01:21

回答

2

“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 

因此,对于你的目的,你可以把一个断点命令你关心的断点,并有命令是“线程信息”。然后每一站都会告诉你身份证和姓名等。

注意,另一种方式做同样的事情将是使用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不是p线程ID。 pthread id只能保证在程序中给定时间存在的所有线程都是唯一的,所以当给定时间的程序中的每个线程都具有不同的pthread ID时,不能保证在不同时间的两个线程将会有不同的线程ID。但是,Mac OS X具有“全局唯一的线程ID”,这在程序运行过程中是唯一的。这就是这个线程ID。