2014-09-29 64 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。