tty info (^T): Add optional kernel stack(9) traces

It is often useful for developers and administrators to determine a running
thread's stack for debugging purposes.  With this feature, using ^T will
print that information

For now, the feature is disabled by default.  Enable with sysctl
kern.tty_info_kstacks=1.

Discussed with:	markj
Reviewed by:	oshogbo
Relnotes:	yes
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D17621
This commit is contained in:
Conrad Meyer 2018-10-20 18:42:28 +00:00
parent 1563a27e1f
commit d7aa89c363

View File

@ -49,11 +49,14 @@ __FBSDID("$FreeBSD$");
#include <sys/cons.h>
#include <sys/kdb.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/resourcevar.h>
#include <sys/sbuf.h>
#include <sys/sched.h>
#include <sys/stack.h>
#include <sys/sysctl.h>
#include <sys/systm.h>
#include <sys/tty.h>
@ -233,6 +236,11 @@ sbuf_tty_drain(void *a, const char *d, int len)
return (-ENXIO);
}
static bool tty_info_kstacks = false;
SYSCTL_BOOL(_kern, OID_AUTO, tty_info_kstacks, CTLFLAG_RWTUN,
&tty_info_kstacks, 0,
"Enable printing kernel stack(9) traces on ^T (tty info)");
/*
* Report on state of foreground process group.
*/
@ -240,12 +248,13 @@ void
tty_info(struct tty *tp)
{
struct timeval rtime, utime, stime;
struct stack stack;
struct proc *p, *ppick;
struct thread *td, *tdpick;
const char *stateprefix, *state;
struct sbuf sb;
long rss;
int load, pctcpu;
int load, pctcpu, sterr;
pid_t pid;
char comm[MAXCOMLEN + 1];
struct rusage ru;
@ -320,6 +329,15 @@ tty_info(struct tty *tp)
else
state = "unknown";
pctcpu = (sched_pctcpu(td) * 10000 + FSCALE / 2) >> FSHIFT;
if (tty_info_kstacks) {
stack_zero(&stack);
if (TD_IS_SWAPPED(td) || TD_IS_RUNNING(td))
sterr = stack_save_td_running(&stack, td);
else {
stack_save_td(&stack, td);
sterr = 0;
}
}
thread_unlock(td);
if (p->p_state == PRS_NEW || p->p_state == PRS_ZOMBIE)
rss = 0;
@ -341,6 +359,9 @@ tty_info(struct tty *tp)
(long)stime.tv_sec, stime.tv_usec / 10000,
pctcpu / 100, rss);
if (tty_info_kstacks && sterr == 0)
stack_sbuf_print_flags(&sb, &stack, M_NOWAIT);
out:
sbuf_finish(&sb);
sbuf_delete(&sb);