43

I have a multithreaded Linux application written in C/C++. I have chosen names for my threads. To aid debugging, I would like these names to be visible in GDB, "top", etc. Is this possible, and if so how?

(There are plenty of reasons to know the thread name. Right now I want to know which thread is taking up 50% CPU (as reported by 'top'). And when debugging I often need to switch to a different thread - currently I have to do "thread apply all bt" then look through pages of backtrace output to find the right thread).

The Windows solution is here; what's the Linux one?

Mark
  • 3,357
  • 30
  • 31
user9876
  • 10,954
  • 6
  • 44
  • 66
  • Related question: http://stackoverflow.com/questions/5026531/thread-name-longer-than-15-chars – user9876 May 19 '11 at 14:58
  • Somehow the `prctl()` (and `pthread_setname_np()` did not work for me. It changed the name of ALL my threads. So rather useless. Instead I had to write the name to the comm file. See: https://stackoverflow.com/questions/68676407/how-do-i-change-the-name-of-one-single-thread-in-linux/68687132#68687132 – Alexis Wilke Aug 06 '21 at 21:06

2 Answers2

44

Posix Threads?

This evidently won't compile, but it will give you an idea of where to go hunting. I'm not even sure its the right PR_ command, but i think it is. It's been a while...

  #include <sys/prctl.h>
  prctl(PR_SET_NAME,"<null> terminated string",0,0,0)
allyourcode
  • 21,871
  • 18
  • 78
  • 106
Fusspawn
  • 1,405
  • 12
  • 19
  • 6
    That worked, thanks! The documentation says PR_SET_NAME sets the process name; but that documentation is wrong - it does actually set the thread name. Now "top" and "ps -L" show the thread name. – user9876 Apr 22 '09 at 17:39
  • Glad it worked, I wasnt sure if it was even the right Constant for it, :) – Fusspawn Apr 22 '09 at 17:53
  • I've always identified threads based on the start function as shown in the thread's stack-trace. Nice to know there's a clean alternative. – veefu Apr 23 '09 at 00:20
  • 2
    Further investigation shows that busybox's "top" and "ps" don't report thread names. You need the full versions from the "procps" package. – user9876 Aug 06 '09 at 13:17
  • you can force "ps" to show that name with the c switch (ps Hcx for example). – Sam Liao Apr 01 '11 at 03:40
  • @arsane not the busybox's one. Busybox tools are very limited, sometimes have a different args. And e.g. its `free` just give wrong results. – Hi-Angel Mar 24 '15 at 10:01
14

If you are using a library like ACE the Thread has a way to specify the thread name when creating a new thread.

BSD Unix has also a pthread_set_name_np call.

Otherwise you can use prctl as mentioned by Fusspawn.

lothar
  • 19,853
  • 5
  • 45
  • 59
  • 11
    On Linux it's called `pthread_setname_np` and it was added in glibc 2.11.2. – Nemo Aug 26 '11 at 17:41
  • 2
    [This](http://stackoverflow.com/questions/2369738/can-i-set-the-name-of-a-thread-in-pthreads-linux) is, probably, the most-detailed answer on the subject. – Mikhail T. Jan 11 '13 at 00:11