Thread name longer than 15 chars?
Asked Answered
S

2

24

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

Salaam answered 17/2, 2011 at 8:21 Comment(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.Fungus
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.Salaam
P
28

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.

Pevzner answered 17/2, 2011 at 9:17 Comment(1)
Recompiling the kernel won't help you: code.woboq.org/userspace/glibc/sysdeps/unix/sysv/linux/…Woolly
M
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.

Midshipman answered 17/2, 2011 at 11:43 Comment(2)
@dsvensson, indeed. I don't believe there's any equivalent for threads. Feel free to propose a patch on lkml! :)Midshipman
Furthermore, the task_struct comm is wholly separate from argv.Squeamish

© 2022 - 2024 — McMap. All rights reserved.