简体   繁体   English

lldb / Xcode:如何打印线程索引,ID或名称?

[英]lldb/Xcode: how to print thread index, id or name?

Problem: I want to trace a program with the help of breakpoints with action in Xcode, so what I am interested in is if my function is always executed in one thread. 问题:我想在断点的帮助下使用Xcode中的动作来跟踪程序,所以我感兴趣的是我的函数是否总是在一个线程中执行。

There is a manual: http://lldb.llvm.org/formats.html which has all the required variables but they don't work for some reason with p/expr commands. 有手册: http : //lldb.llvm.org/formats.html ,它具有所有必需的变量,但是由于某些原因,它们在p / expr命令中不起作用。

So I'd want something like p ${thread.id} or expr -- thread.id but I've had no luck with them. 所以我想要像p $ {thread.id}或expr-thread.id之类的东西,但是我没有运气。

The method I know is bad is: 我知道不好的方法是:

p/x (long)pthread_self() p / x(长)pthread_self()

and to get name: 并获得名称:

p new char[256] //it will return suitable pointer like $3 = 0x000000007480840 p new char [256] //它将返回合适的指针,例如$ 3 = 0x000000007480840

p (int)pthread_getname_np( (pthread_t)yourId, $3, (size_t)256 ) //it writes thread name to the buffer p(int)pthread_getname_np((pthread_t)yourId,$ 3,(size_t)256)//将线程名写入缓冲区

p $3 //you will see its name p $ 3 //您将看到它的名字

p delete $3 //if you worry about memory leak p delete $ 3 //如果担心内存泄漏

but it looks rather a bad workaround, and doesn't fit well for breakpoints. 但它看起来是一个不好的解决方法,并且不太适合断点。

The formats mentioned in the "formats.html" page are used not for expressions, but for the way that the thread and frame info is printed whenever lldb prints it. “ formats.html”页面中提到的格式不是用于表达式,而是用于在lldb打印时打印线程和框架信息的方式。 For instance, I have this: 例如,我有这个:

    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

in my .lldbinit, so I can see the thread ID & name when I stop. 在我的.lldbinit中,这样我可以在停止时看到线程ID和名称。

If you are running in Xcode, you won't generally see the thread info printed at stop, because Xcode doesn't echo every stop to the Xcode console. 如果您在Xcode中运行,则通常不会在停止处看到线程信息,因为Xcode不会在每次停止时都向Xcode控制台回显。 But you can still call up some of this information with the "thread info" command: 但是您仍然可以使用“线程信息”命令来调用其中一些信息:

    (lldb) thread info
    thread #1: tid = 0x34ca69, name = A_Cool_Thread, function: -[SKTGraphicView alignLeftEdges:] , stop reason = breakpoint 2.1

So for your purposes, you can put a breakpoint command on the breakpoints you care about, and have the command be "thread info". 因此,出于您的目的,您可以在所需的断点上放置一个breakpoint命令,并使该命令为“线程信息”。 Then every stop will show you the ID and the name, among other things. 然后,每站都会显示ID和名称以及其他内容。

Note, another way to do the same thing would be to use Python breakpoint commands, for instance: 请注意,另一种执行相同操作的方法是使用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)

Then every time you hit breakpoint you'll see something like: 然后,每次您到达断点时,您都​​会看到类似以下内容的内容:

Thread id is:  3459689  and name:  A_Cool_Thread

BTW, you didn't say what system you were on, but on Mac OS X the thread ID that is listed here is not the pthread ID. 顺便说一句,您没有说您使用的是什么系统,但是在Mac OS X上,此处列出的线程ID不是pthread ID。 The pthread id is only guaranteed to be unique for all the threads that exist at a given time in the program, so while every thread in the program at a given time will have different pthread ID's, there's no guarantee that two threads at different times will have different pthread ID's. pthread ID仅保证在程序中在给定时间存在的所有线程都是唯一的,因此,尽管程序在给定时间的每个线程将具有不同的pthread ID,但不能保证在不同时间有两个线程会具有不同的pthread ID。 Mac OS X has a "globally unique thread ID", however, which is unique across the running of the program. Mac OS X有一个“全局唯一的线程ID”,但是,在程序运行期间它是唯一的。 That's what this thread ID is. 这就是该线程ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM