11

Xcode's "thread list" pane shows English-like names for several special threads: com.apple.main-thread, com.apple.libdispatch-manager, com.dispatchfractal.opencl, com.dispatchfractal.opengl, com.apple.root.low-priority,... But for user-created threads that field is just blank.

Is there any way to set that "thread name" field programmatically from my application? For example, if I've got a thread devoted to network I/O, I'd like it to show up as "com.example.network-io" in the debugger; if I spawn five worker threads I'd like to be able to name them "worker A", "worker B", etc. Does Xcode pull its thread-names from some API that I could hook into myself? Maybe something like CFAssignDebuggerNameToCurrentThread? :)

enter image description here

Quuxplusone
  • 23,928
  • 8
  • 94
  • 159

4 Answers4

12

Probably not exactly what you want but NSThread has setName: method that allows you to set thread's name, you can attach meaningful name to the thread so you'll get the following in debugger:

[[NSThread mainThread] setName:@"That is main thread!"];

enter image description here

Remember also that some apis (like grand central dispatch) operate with thread pools so you are not guaranteed on what thread your operation will be performed

Edit: com.apple.main-thread, com.apple.libdispatch-manager etc are labels of corresponding dispatch queues. You can set label value when queue is created with dispatch_queue_create function and later get it from the queue using dispatch_queue_get_label function.

It seems there's no API to change label value of the existing dispatch queue and I would not advise to change labels of the system queues anyway.

Vladimir
  • 170,431
  • 36
  • 387
  • 313
  • Not *exactly* what I was asking for, but it serves the same purpose. I'll accept this answer unless someone posts a better idea. – Quuxplusone Sep 24 '12 at 18:10
  • And FYI, I'm not worried about GCD thread pools. My particular application manages all its own worker threads outside of GCD, for reasons that are out of the scope of this question. – Quuxplusone Sep 24 '12 at 18:11
4

If you can get a reference to the thread whose name you want to change, you can change it in the debugger console. Two ways to do that for the current thread:

  • (lldb) po [[NSThread currentThread] setName:@"foo"]

  • (lldb) expression (void)[(NSThread*)[NSThread currentThread] setName:@"foo"];

I'd guess you could do the same from a breakpoint that has an associated expression. If you have a method that you know will run in the thread that you're interested in, you could set a breakpoint containing one of the above commands and have it automatically continue after running the command. That'd have the effect of automatically setting the name of the thread every time you run the code, which might be handy for debugging.

Caleb
  • 124,013
  • 19
  • 183
  • 272
  • I think I'd just do this in the application itself. +1 for `po` and `expression`, though; I don't know nearly enough about what's possible in lldb. – Quuxplusone Sep 24 '12 at 19:27
3

I use the following code in Swift 5 to rename the current thread:

    pthread_setname_np("myThreadName")
j.s.com
  • 1,422
  • 14
  • 24
1

pesudo code for -[NSThread setName:]

- (void) setName:(NSString*)thname {
    if (self == [NSThread currentThread])
    {
        pthread_setname_np([thname UTF8String]);
    }
    else { ... }
    ...
}

So, the easist way to set name is calling pthread_setname_np.

user2902980
  • 447
  • 4
  • 6