Skip to content

Commit

Permalink
Put the task pid in the thread name
Browse files Browse the repository at this point in the history
  • Loading branch information
saagarjha committed Oct 11, 2020
1 parent a9d70a1 commit bf1603e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
6 changes: 1 addition & 5 deletions kernel/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,11 +594,7 @@ int __do_execve(const char *file, struct exec_args argv, struct exec_args envp)
strncpy(current->comm, basename, sizeof(current->comm));
unlock(&current->general_lock);

// set the thread name
char threadname[16];
strncpy(threadname, current->comm, sizeof(threadname)-1);
threadname[15] = '\0';
set_thread_name(threadname);
update_thread_name();

// cloexec
// consider putting this in fd.c?
Expand Down
15 changes: 14 additions & 1 deletion kernel/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void task_run_current() {

static void *task_thread(void *task) {
current = task;
set_thread_name(current->comm);
update_thread_name();
task_run_current();
die("task_thread returned"); // above function call should never return
}
Expand All @@ -125,3 +125,16 @@ int_t sys_sched_yield() {
sched_yield();
return 0;
}

void update_thread_name() {
char name[16]; // As long as Linux will let us make this
snprintf(name, sizeof(name), "-%d", current->pid);
size_t pid_width = strlen(name);
size_t name_width = snprintf(name, sizeof(name), "%s", current->comm);
sprintf(name + (name_width < sizeof(name) - 1 - pid_width ? name_width : sizeof(name) - 1 - pid_width), "-%d", current->pid);
#if __APPLE__
pthread_setname_np(name);
#else
pthread_setname_np(pthread_self(), name);
#endif
}
4 changes: 4 additions & 0 deletions kernel/task.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,8 @@ extern void (*exit_hook)(struct task *task, int code);

#define superuser() (current != NULL && current->euid == 0)

// Update the thread name to match the current task, in the format "comm-pid".
// Will ensure that the -pid part always fits, then will fit as much of comm as possible.
void update_thread_name(void);

#endif
6 changes: 0 additions & 6 deletions util/sync.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,4 @@ static inline void sigunwind_end() {
should_unwind = false;
}

#if __APPLE__
#define set_thread_name(threadname) pthread_setname_np(threadname)
#else
#define set_thread_name(threadname) pthread_setname_np(pthread_self(), threadname)
#endif

#endif

0 comments on commit bf1603e

Please sign in to comment.