From 289d9bce9d19919c03eb9e2bb2f696731756dcc0 Mon Sep 17 00:00:00 2001 From: vangyzen Date: Thu, 15 Dec 2016 16:52:17 +0000 Subject: [PATCH] MFC r309676 Export the whole thread name in kinfo_proc kinfo_proc::ki_tdname is three characters shorter than thread::td_name. Add a ki_moretdname field for these three extra characters. Add the new field to kinfo_proc32, as well. Update all in-tree consumers to read the new field and assemble the full name, except for lldb's HostThreadFreeBSD.cpp, which I will handle separately. Bump __FreeBSD_version. Sponsored by: Dell EMC git-svn-id: svn://svn.freebsd.org/base/stable/10@310121 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- bin/ps/print.c | 16 ++++++++++------ lib/libkvm/kvm_proc.c | 2 -- sys/compat/freebsd32/freebsd32.h | 3 ++- sys/kern/kern_proc.c | 10 +++++++++- sys/sys/param.h | 2 +- sys/sys/user.h | 3 ++- usr.bin/procstat/procstat.c | 16 ++++++++++++++++ usr.bin/procstat/procstat.h | 1 + usr.bin/procstat/procstat_cs.c | 8 +++----- usr.bin/procstat/procstat_kstack.c | 8 +++----- usr.bin/procstat/procstat_threads.c | 8 +++----- usr.bin/top/machine.c | 15 +++++++++------ 12 files changed, 59 insertions(+), 33 deletions(-) diff --git a/bin/ps/print.c b/bin/ps/print.c index bcd451e1f..a1d5b2b49 100644 --- a/bin/ps/print.c +++ b/bin/ps/print.c @@ -119,11 +119,12 @@ command(KINFO *k, VARENT *ve) if (cflag) { /* If it is the last field, then don't pad */ if (STAILQ_NEXT(ve, next_ve) == NULL) { - asprintf(&str, "%s%s%s%s", + asprintf(&str, "%s%s%s%s%s", k->ki_d.prefix ? k->ki_d.prefix : "", k->ki_p->ki_comm, (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "", - (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : ""); + (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "", + (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_moretdname : ""); } else str = strdup(k->ki_p->ki_comm); @@ -171,14 +172,16 @@ ucomm(KINFO *k, VARENT *ve) char *str; if (STAILQ_NEXT(ve, next_ve) == NULL) { /* last field, don't pad */ - asprintf(&str, "%s%s%s%s", + asprintf(&str, "%s%s%s%s%s", k->ki_d.prefix ? k->ki_d.prefix : "", k->ki_p->ki_comm, (showthreads && k->ki_p->ki_numthreads > 1) ? "/" : "", - (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : ""); + (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_tdname : "", + (showthreads && k->ki_p->ki_numthreads > 1) ? k->ki_p->ki_moretdname : ""); } else { if (showthreads && k->ki_p->ki_numthreads > 1) - asprintf(&str, "%s/%s", k->ki_p->ki_comm, k->ki_p->ki_tdname); + asprintf(&str, "%s/%s%s", k->ki_p->ki_comm, + k->ki_p->ki_tdname, k->ki_p->ki_moretdname); else str = strdup(k->ki_p->ki_comm); } @@ -191,7 +194,8 @@ tdnam(KINFO *k, VARENT *ve __unused) char *str; if (showthreads && k->ki_p->ki_numthreads > 1) - str = strdup(k->ki_p->ki_tdname); + asprintf(&str, "%s%s", k->ki_p->ki_tdname, + k->ki_p->ki_moretdname); else str = strdup(" "); diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c index f8e152e4b..ed34a9c14 100644 --- a/lib/libkvm/kvm_proc.c +++ b/lib/libkvm/kvm_proc.c @@ -424,8 +424,6 @@ nopgrp: kp->ki_pri.pri_native = mtd.td_base_pri; kp->ki_lastcpu = mtd.td_lastcpu; kp->ki_wchan = mtd.td_wchan; - if (mtd.td_name[0] != 0) - strlcpy(kp->ki_tdname, mtd.td_name, MAXCOMLEN); kp->ki_oncpu = mtd.td_oncpu; if (mtd.td_name[0] != '\0') strlcpy(kp->ki_tdname, mtd.td_name, sizeof(kp->ki_tdname)); diff --git a/sys/compat/freebsd32/freebsd32.h b/sys/compat/freebsd32/freebsd32.h index bbe41cf71..ccac785fe 100644 --- a/sys/compat/freebsd32/freebsd32.h +++ b/sys/compat/freebsd32/freebsd32.h @@ -315,7 +315,8 @@ struct kinfo_proc32 { char ki_comm[COMMLEN+1]; char ki_emul[KI_EMULNAMELEN+1]; char ki_loginclass[LOGINCLASSLEN+1]; - char ki_sparestrings[50]; + char ki_moretdname[MAXCOMLEN-TDNAMLEN+1]; + char ki_sparestrings[46]; int ki_spareints[KI_NSPARE_INT]; int ki_flag2; int ki_fibnum; diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 0f6055315..475895cba 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -959,7 +959,14 @@ fill_kinfo_thread(struct thread *td, struct kinfo_proc *kp, int preferthread) strlcpy(kp->ki_wmesg, td->td_wmesg, sizeof(kp->ki_wmesg)); else bzero(kp->ki_wmesg, sizeof(kp->ki_wmesg)); - strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)); + if (strlcpy(kp->ki_tdname, td->td_name, sizeof(kp->ki_tdname)) >= + sizeof(kp->ki_tdname)) { + strlcpy(kp->ki_moretdname, + td->td_name + sizeof(kp->ki_tdname) - 1, + sizeof(kp->ki_moretdname)); + } else { + bzero(kp->ki_moretdname, sizeof(kp->ki_moretdname)); + } if (TD_ON_LOCK(td)) { kp->ki_kiflag |= KI_LOCKBLOCK; strlcpy(kp->ki_lockname, td->td_lockname, @@ -1180,6 +1187,7 @@ freebsd32_kinfo_proc_out(const struct kinfo_proc *ki, struct kinfo_proc32 *ki32) bcopy(ki->ki_comm, ki32->ki_comm, COMMLEN + 1); bcopy(ki->ki_emul, ki32->ki_emul, KI_EMULNAMELEN + 1); bcopy(ki->ki_loginclass, ki32->ki_loginclass, LOGINCLASSLEN + 1); + bcopy(ki->ki_moretdname, ki32->ki_moretdname, MAXCOMLEN - TDNAMLEN + 1); CP(*ki, *ki32, ki_flag2); CP(*ki, *ki32, ki_fibnum); CP(*ki, *ki32, ki_cr_flags); diff --git a/sys/sys/param.h b/sys/sys/param.h index 97e660d5a..6b3873deb 100644 --- a/sys/sys/param.h +++ b/sys/sys/param.h @@ -58,7 +58,7 @@ * in the range 5 to 9. */ #undef __FreeBSD_version -#define __FreeBSD_version 1003510 /* Master, propagated to newvers */ +#define __FreeBSD_version 1003511 /* Master, propagated to newvers */ /* * __FreeBSD_kernel__ indicates that this system uses the kernel of FreeBSD, diff --git a/sys/sys/user.h b/sys/sys/user.h index 3bd27728a..5b2d8d21a 100644 --- a/sys/sys/user.h +++ b/sys/sys/user.h @@ -180,12 +180,13 @@ struct kinfo_proc { char ki_comm[COMMLEN+1]; /* command name */ char ki_emul[KI_EMULNAMELEN+1]; /* emulation name */ char ki_loginclass[LOGINCLASSLEN+1]; /* login class */ + char ki_moretdname[MAXCOMLEN-TDNAMLEN+1]; /* more thread name */ /* * When adding new variables, take space for char-strings from the * front of ki_sparestrings, and ints from the end of ki_spareints. * That way the spare room from both arrays will remain contiguous. */ - char ki_sparestrings[50]; /* spare string space */ + char ki_sparestrings[46]; /* spare string space */ int ki_spareints[KI_NSPARE_INT]; /* spare room for growth */ int ki_flag2; /* P2_* flags */ int ki_fibnum; /* Default FIB number */ diff --git a/usr.bin/procstat/procstat.c b/usr.bin/procstat/procstat.c index 48a213518..a6774e51f 100644 --- a/usr.bin/procstat/procstat.c +++ b/usr.bin/procstat/procstat.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -115,6 +116,21 @@ kinfo_proc_sort(struct kinfo_proc *kipp, int count) qsort(kipp, count, sizeof(*kipp), kinfo_proc_compare); } +const char * +kinfo_proc_thread_name(const struct kinfo_proc *kipp) +{ + static char name[MAXCOMLEN+1]; + + strlcpy(name, kipp->ki_tdname, sizeof(name)); + strlcat(name, kipp->ki_moretdname, sizeof(name)); + if (name[0] == '\0' || strcmp(kipp->ki_comm, name) == 0) { + name[0] = '-'; + name[1] = '\0'; + } + + return (name); +} + int main(int argc, char *argv[]) { diff --git a/usr.bin/procstat/procstat.h b/usr.bin/procstat/procstat.h index 507952191..4c5242a6e 100644 --- a/usr.bin/procstat/procstat.h +++ b/usr.bin/procstat/procstat.h @@ -33,6 +33,7 @@ extern int hflag, nflag, Cflag, Hflag; struct kinfo_proc; void kinfo_proc_sort(struct kinfo_proc *kipp, int count); +const char * kinfo_proc_thread_name(const struct kinfo_proc *kipp); void procstat_args(struct procstat *prstat, struct kinfo_proc *kipp); void procstat_auxv(struct procstat *prstat, struct kinfo_proc *kipp); diff --git a/usr.bin/procstat/procstat_cs.c b/usr.bin/procstat/procstat_cs.c index 8ccf1ea82..4af29ca78 100644 --- a/usr.bin/procstat/procstat_cs.c +++ b/usr.bin/procstat/procstat_cs.c @@ -50,7 +50,7 @@ procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp) int once, twice, lastcpu, cpu; if (!hflag) - printf("%5s %6s %-16s %-16s %2s %4s %-7s\n", "PID", + printf("%5s %6s %-19s %-19s %2s %4s %-7s\n", "PID", "TID", "COMM", "TDNAME", "CPU", "CSID", "CPU MASK"); kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, @@ -62,11 +62,9 @@ procstat_cs(struct procstat *procstat, struct kinfo_proc *kipp) kipp = &kip[i]; printf("%5d ", kipp->ki_pid); printf("%6d ", kipp->ki_tid); - printf("%-16s ", strlen(kipp->ki_comm) ? + printf("%-19s ", strlen(kipp->ki_comm) ? kipp->ki_comm : "-"); - printf("%-16s ", (strlen(kipp->ki_tdname) && - (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? - kipp->ki_tdname : "-"); + printf("%-19s ", kinfo_proc_thread_name(kipp)); if (kipp->ki_oncpu != 255) printf("%3d ", kipp->ki_oncpu); else if (kipp->ki_lastcpu != 255) diff --git a/usr.bin/procstat/procstat_kstack.c b/usr.bin/procstat/procstat_kstack.c index 8ffa4f98a..10cb5cf8d 100644 --- a/usr.bin/procstat/procstat_kstack.c +++ b/usr.bin/procstat/procstat_kstack.c @@ -134,7 +134,7 @@ procstat_kstack(struct procstat *procstat, struct kinfo_proc *kipp, int kflag) unsigned int kip_count, kstk_count; if (!hflag) - printf("%5s %6s %-16s %-16s %-29s\n", "PID", "TID", "COMM", + printf("%5s %6s %-19s %-19s %-29s\n", "PID", "TID", "COMM", "TDNAME", "KSTACK"); kkstp = kkstp_free = procstat_getkstack(procstat, kipp, &kstk_count); @@ -171,10 +171,8 @@ procstat_kstack(struct procstat *procstat, struct kinfo_proc *kipp, int kflag) printf("%5d ", kipp->ki_pid); printf("%6d ", kkstp->kkst_tid); - printf("%-16s ", kipp->ki_comm); - printf("%-16s ", (strlen(kipp->ki_tdname) && - (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? - kipp->ki_tdname : "-"); + printf("%-19s ", kipp->ki_comm); + printf("%-19s ", kinfo_proc_thread_name(kipp)); switch (kkstp->kkst_state) { case KKST_STATE_RUNNING: diff --git a/usr.bin/procstat/procstat_threads.c b/usr.bin/procstat/procstat_threads.c index 6bd88da50..f81ec74fa 100644 --- a/usr.bin/procstat/procstat_threads.c +++ b/usr.bin/procstat/procstat_threads.c @@ -47,7 +47,7 @@ procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp) const char *str; if (!hflag) - printf("%5s %6s %-16s %-16s %2s %4s %-7s %-9s\n", "PID", + printf("%5s %6s %-19s %-19s %2s %4s %-7s %-9s\n", "PID", "TID", "COMM", "TDNAME", "CPU", "PRI", "STATE", "WCHAN"); kip = procstat_getprocs(procstat, KERN_PROC_PID | KERN_PROC_INC_THREAD, @@ -59,11 +59,9 @@ procstat_threads(struct procstat *procstat, struct kinfo_proc *kipp) kipp = &kip[i]; printf("%5d ", kipp->ki_pid); printf("%6d ", kipp->ki_tid); - printf("%-16s ", strlen(kipp->ki_comm) ? + printf("%-19s ", strlen(kipp->ki_comm) ? kipp->ki_comm : "-"); - printf("%-16s ", (strlen(kipp->ki_tdname) && - (strcmp(kipp->ki_comm, kipp->ki_tdname) != 0)) ? - kipp->ki_tdname : "-"); + printf("%-19s ", kinfo_proc_thread_name(kipp)); if (kipp->ki_oncpu != 255) printf("%3d ", kipp->ki_oncpu); else if (kipp->ki_lastcpu != 255) diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index cf4714c70..69780606b 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -889,8 +889,8 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) if (!(flags & FMT_SHOWARGS)) { if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) { - snprintf(cmdbuf, cmdlen, "%s{%s}", pp->ki_comm, - pp->ki_tdname); + snprintf(cmdbuf, cmdlen, "%s{%s%s}", pp->ki_comm, + pp->ki_tdname, pp->ki_moretdname); } else { snprintf(cmdbuf, cmdlen, "%s", pp->ki_comm); } @@ -902,7 +902,8 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) { snprintf(cmdbuf, cmdlen, - "[%s{%s}]", pp->ki_comm, pp->ki_tdname); + "[%s{%s%s}]", pp->ki_comm, pp->ki_tdname, + pp->ki_moretdname); } else { snprintf(cmdbuf, cmdlen, "[%s]", pp->ki_comm); @@ -950,8 +951,9 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) snprintf(cmdbuf, cmdlen, - "%s (%s){%s}", argbuf, pp->ki_comm, - pp->ki_tdname); + "%s (%s){%s%s}", argbuf, + pp->ki_comm, pp->ki_tdname, + pp->ki_moretdname); else snprintf(cmdbuf, cmdlen, "%s (%s)", argbuf, pp->ki_comm); @@ -959,7 +961,8 @@ format_next_process(caddr_t handle, char *(*get_userid)(int), int flags) if (ps.thread && pp->ki_flag & P_HADTHREADS && pp->ki_tdname[0]) snprintf(cmdbuf, cmdlen, - "%s{%s}", argbuf, pp->ki_tdname); + "%s{%s%s}", argbuf, pp->ki_tdname, + pp->ki_moretdname); else strlcpy(cmdbuf, argbuf, cmdlen); } -- 2.42.0