Like task_set_name, we use the same size as the task name and will inherit the task name, whenever it exists. This will be used to implement pthread_setname_np. --- ddb/db_print.c | 9 +++++---- include/mach/gnumach.defs | 8 ++++++++ kern/thread.c | 20 ++++++++++++++++++++ kern/thread.h | 8 ++++++++ 4 files changed, 41 insertions(+), 4 deletions(-)
diff --git a/ddb/db_print.c b/ddb/db_print.c index c8d85d2..f08dd6c 100644 --- a/ddb/db_print.c +++ b/ddb/db_print.c @@ -222,10 +222,11 @@ db_print_thread( } } else { if (flag & OPTION_INDENT) - db_printf(" %3d (%0*X) ", thread_id, - 2*sizeof(vm_offset_t), thread); - else - db_printf("(%0*X) ", 2*sizeof(vm_offset_t), thread); + db_printf(" %3d ", thread_id); + if (thread->name[0] && + strncmp (thread->name, thread->task->name, THREAD_NAME_SIZE)) + db_printf("%s ", thread->name); + db_printf("(%0*X) ", 2*sizeof(vm_offset_t), thread); char status[8]; db_printf("%s", db_thread_stat(thread, status)); if (thread->state & TH_SWAPPED) { diff --git a/include/mach/gnumach.defs b/include/mach/gnumach.defs index 6252de9..7ecf74d 100644 --- a/include/mach/gnumach.defs +++ b/include/mach/gnumach.defs @@ -207,3 +207,11 @@ routine vm_pages_phys( vaddr : vm_address_t; size : vm_size_t; out pages : rpc_phys_addr_array_t); + +/* + * Set the name of thread THREAD to NAME. This is a debugging aid. + * NAME will be used in error messages printed by the kernel. + */ +simpleroutine thread_set_name( + thread : thread_t; + name : kernel_debug_name_t); diff --git a/kern/thread.c b/kern/thread.c index 23ee8b0..2eab1ca 100644 --- a/kern/thread.c +++ b/kern/thread.c @@ -46,6 +46,7 @@ #include <kern/counters.h> #include <kern/debug.h> #include <kern/eventcount.h> +#include <kern/gnumach.server.h> #include <kern/ipc_mig.h> #include <kern/ipc_tt.h> #include <kern/mach_debug.server.h> @@ -551,6 +552,10 @@ kern_return_t thread_create( #endif /* MACH_PCSAMPLE */ new_thread->pc_sample.buffer = 0; + + /* Inherit the task name as the thread name. */ + memcpy (new_thread->name, parent_task->name, THREAD_NAME_SIZE); + /* * Add the thread to the task`s list of threads. * The new thread holds another reference to the task. @@ -2624,3 +2629,18 @@ thread_stats(void) printf("%d using rpc_reply.\n", rpcreply); } #endif /* MACH_DEBUG */ + +/* + * thread_set_name + * + * Set the name of thread THREAD to NAME. + */ +kern_return_t +thread_set_name( + thread_t thread, + const_kernel_debug_name_t name) +{ + strncpy(thread->name, name, sizeof thread->name - 1); + thread->name[sizeof thread->name - 1] = '\0'; + return KERN_SUCCESS; +} diff --git a/kern/thread.h b/kern/thread.h index 7bfe2e8..21b2503 100644 --- a/kern/thread.h +++ b/kern/thread.h @@ -54,6 +54,12 @@ #include <machine/thread.h> #include <ipc/ipc_kmsg_queue.h> +/* + * Thread name buffer size. Use the same size as the task so + * the thread can inherit the task's name. + */ +#define THREAD_NAME_SIZE TASK_NAME_SIZE + struct thread { /* Run queues */ queue_chain_t links; /* current run queue links */ @@ -232,6 +238,8 @@ struct thread { #if MACH_LOCK_MON unsigned lock_stack; #endif + + char name[THREAD_NAME_SIZE]; }; #include <kern/cpu_number.h> -- 2.39.2