23

By using functions like prctl, or pthread_set_name_np it's possible to change the name of a thread. The limit both functions imposes, at least in Linux 2.6.38, is that the name cannot be longer than 15 characters (NULL termination being the 16th byte).

Where is this 15 character limit imposed, and is there any (even unorthodox) way around it?

Update:

As mentioned in the comments, this is imposed by the kernel.

The definition can be found here: http://lxr.linux.no/linux+v2.6.37/include/linux/sched.h#L245

dsvensson
  • 1,401
  • 12
  • 20
  • 2
    I imagine it's done to reduce overhead. A static array is much quicker than a dynamic one, but you don't want to increase the thread's footprint too much. You could keep a table yourself that translates long names to some 15 character string, if you really need longer names. – jswolf19 Feb 17 '11 at 08:42
  • The naming is to provide meaningful information when showing all threads in top (press 'H'), so it's easy for users to provide debugging information. – dsvensson Feb 17 '11 at 09:08

2 Answers2

27

15-char limit is enforced by the kernel:

struct task_struct::comm[TASK_COMM_LEN]

which is 16-byte wide.

You have to recompile the kernel if you want to increase that.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
adobriyan
  • 2,594
  • 16
  • 8
  • 1
    Recompiling the kernel won't help you: https://code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/pthread_setname.c.html#35 – Trass3r Aug 19 '20 at 12:05
0

Although the normal task name limit is set in the kernel, you can change your command line parameters (as shown in ps) by overwriting the memory pointed to by argv[0]. This can be used to display additional data of up to one page in size.

bdonlan
  • 224,562
  • 31
  • 268
  • 324