Skip to contentSkip to conversation

pthread_set_name_np

Xenomai Jean-Baptiste Tredez 2 participants 3 messages Nov 6, 2012 – Nov 8, 2012

lore
Share & tools
lore
#0 lore raw

Hello,

I have some issues when I change the name of a task (created with the native skin) with the posix skin (pthread_set_name_np) on xenomai 2.6.1 (kernel 2.6.38.8). This is a task created by a external library and we can not set the right name at the first place. pthread_set_name_np seems to work only on tasks created with the posix skin.

Here is a test program (that worked with linux 2.6.38.8 + xenomai-2.6.0 and crash with linux 2.6.38 + xenomai 2.6.1) :

#include <sys/mman.h>
#include <stdio.h>
#include <native/task.h>

RT_TASK task1;

void task1_main(void* arg)

{
    pthread_set_name_np(pthread_self(), "foo");
}

int main()

{
    int ret;

    mlockall(MCL_CURRENT | MCL_FUTURE);

    ret = rt_task_create(&task1, "task1", 0, 50, T_JOINABLE);
    if( ret )
    {
        printf("rt_task_create : %i\n", errno);
        return 0;
    }

    ret = rt_task_start(&task1, &task1_main, NULL);
    if( ret )
    {
            printf("rt_task_start : %i\n", errno);
            return 0;
    }

    return 0;
}
BUG: unable to handle kernel NULL pointer dereference at 0000039c
IP: [<c10bff87>] __pthread_set_name_np+0x65/0x96
*pde = 00000000
Oops: 0000 [#3] PREEMPT SMP
last sysfs file: /sys/devices/pci0000:00/0000:00:03.0/class
Modules linked in: xeno_16550A ipv6 pcspkr uhci_hcd

Pid: 5694, comm: task1 Tainted: G D 2.6.38.8-os-0201patch04 #1 Bochs Bochs
EIP: 0060:[<c10bff87>] EFLAGS: 00010286 CPU: 0
EIP is at __pthread_set_name_np+0x65/0x96
EAX: 00000000 EBX: cea33fb4 ECX: 99f915b3 EDX: df403180
ESI: 00000000 EDI: cea96130 EBP: e082d840 ESP: cea33ee8
DS: 007b ES: 007b FS: 00d8 GS: 0033 SS: 0068
Process task1 (pid: 5694, ti=cea32000 task=cea96130 task.ti=cea32000) I-pipe domain Linux

Stack:
 006f6f66 c102da2d 00000000 00000082 c174b180 00000000 c1071458 00000046
 b7733b90 dead3540 cea33fb4 00000000 cea96130 c1090bf8 00000000 00000000
 cea33fb4 00000050 c183d080 00000000 00000038 df40cf24 c183d080 c1090a3b
Call Trace:
 [<c102da2d>] ? try_to_wake_up+0x1e7/0x2bc
 [<c1071458>] ? __ipipe_restore_pipeline_head+0x11/0x100
 [<c1090bf8>] ? hisyscall_event+0x1bd/0x30e
 [<c1090a3b>] ? hisyscall_event+0x0/0x30e
 [<c107088f>] ? __ipipe_dispatch_event+0x10d/0x3d7
 [<c1017de5>] ? __ipipe_syscall_root+0x3f/0x183
 [<c150a6dd>] ? system_call+0x2d/0x53
Code: 00 85 c0 78 e4 c6 44 24 1f 00 8b 03 89 44 24 20 64 a1 cc 94 74 c1 8b 80 c8 00 00 00 89 44 24 24 8d 44 24 20 e8 fc fb ff ff 89 c6 <8b> 98 9c 03 00 00 8d 83 d8 01 00 00 b9 10 00 00 00 89 e2 e8 e2
EIP: [<c10bff87>] __pthread_set_name_np+0x65/0x96 SS:ESP 0068:cea33ee8
CR2: 000000000000039c
---[ end trace c8148c10eacbfd85 ]---

Jean-Baptiste

#1 lore raw
Quoted 7 lines · Jean-Baptiste Tredez

Hello,

I have some issues when I change the name of a task (created with the native skin) with the posix skin (pthread_set_name_np) on xenomai 2.6.1 (kernel 2.6.38.8). This is a task created by a external library and we can not set the right name at the first place. pthread_set_name_np seems to work only on tasks created with the posix skin.

Yes, pthread_set_name_np only works for posix skin threads, I do not think we have a simple way of retrieving the native skin thread associated with a given pthread_t identifier.

We have some way of doing it that will only work for pthread_self().

Quoted 3 lines

Here is a test program (that worked with linux 2.6.38.8 + xenomai-2.6.0 and crash with linux 2.6.38 + xenomai 2.6.1) :

I doubt very much it has ever worked. It did not crash, but did not do anything (and returned ESRCH by the way). Anyway, we can try the following patch, that should restore pthread_set_name_np as a nop for non-posix threads as in 2.6.0:

ksrc/skins/posix/syscall.c +5 -3
index 9b1c266..6c78244 100644
@@ -435,9 +435,11 @@ static int __pthread_set_name_np(struct pt_regs *regs)
 	hkey.u_tid = __xn_reg_arg1(regs);
 	hkey.mm = current->mm;
 	k_tid = __pthread_find(&hkey);
-	p = xnthread_user_task(&k_tid->threadbase);
-	strncpy(p->comm, name, sizeof(p->comm));
-	p->comm[sizeof(p->comm) - 1] = '\0';
+	if (k_tid) {
+		p = xnthread_user_task(&k_tid->threadbase);
+		strncpy(p->comm, name, sizeof(p->comm));
+		p->comm[sizeof(p->comm) - 1] = '\0';
+	}
 
 	return -pthread_set_name_np(k_tid, name);
 }

If you want to set a thread name for display in top or ps, you should use prctl(PR_SET_NAME). If you want to set the thread name for display in /proc/xenomai/stat or /proc/xenomai/sched, you have no other choice than using the native API for native skin threads.

Signature · 1 line
					    Gilles.
#2 lore raw
Quoted 12 lines · Gilles Chanteperdrix
Quoted 7 lines · Jean-Baptiste Tredez

Hello,

I have some issues when I change the name of a task (created with the native skin) with the posix skin (pthread_set_name_np) on xenomai 2.6.1 (kernel 2.6.38.8). This is a task created by a external library and we can not set the right name at the first place. pthread_set_name_np seems to work only on tasks created with the posix skin.

Yes, pthread_set_name_np only works for posix skin threads, I do not think we have a simple way of retrieving the native skin thread associated with a given pthread_t identifier.

Quoted 6 lines

We have some way of doing it that will only work for pthread_self().

Quoted 2 lines

Here is a test program (that worked with linux 2.6.38.8 + xenomai-2.6.0 and crash with linux 2.6.38 + xenomai 2.6.1) :

I doubt very much it has ever worked. It did not crash, but did not do anything (and returned ESRCH by the way).

Ok, it did not crash but did nothing.

Quoted 13 lines

Anyway, we can try the
following patch, that should restore pthread_set_name_np as a nop for non-posix threads as in 2.6.0:

ksrc/skins/posix/syscall.c +0 -1
index 9b1c266..6c78244 100644
@@ -435,9 +435,11 @@ static int __pthread_set_name_np(struct pt_regs *regs)
  	hkey.u_tid = __xn_reg_arg1(regs);
  	hkey.mm = current->mm;
  	k_tid = __pthread_find(&hkey);
-	p = xnthread_user_task(&k_tid->threadbase);

ksrc/skins/posix/syscall.c static int __pthread_set_name_np(struct pt_regs *regs) · line 445

Quoted 10 lines
ksrc/skins/posix/syscall.c +5 -2
-	strncpy(p->comm, name, sizeof(p->comm));
-	p->comm[sizeof(p->comm) - 1] = '\0';
+	if (k_tid) {
+		p = xnthread_user_task(&k_tid->threadbase);
+		strncpy(p->comm, name, sizeof(p->comm));
+		p->comm[sizeof(p->comm) - 1] = '\0';
+	}
 
  	return -pthread_set_name_np(k_tid, name);
  }

This is working (nop operation).

Quoted 5 lines

If you want to set a thread name for display in top or ps, you should use prctl(PR_SET_NAME). If you want to set the thread name for display in /proc/xenomai/stat or /proc/xenomai/sched, you have no other choice than using the native API for native skin threads.