Author: cem
Date: Sat Oct 20 18:42:28 2018
New Revision: 339471
URL: https://svnweb.freebsd.org/changeset/base/339471

Log:
  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

Modified:
  head/sys/kern/tty_info.c

Modified: head/sys/kern/tty_info.c
==============================================================================
--- head/sys/kern/tty_info.c    Sat Oct 20 18:37:21 2018        (r339470)
+++ head/sys/kern/tty_info.c    Sat Oct 20 18:42:28 2018        (r339471)
@@ -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;
@@ -340,6 +358,9 @@ tty_info(struct tty *tp)
            (long)utime.tv_sec, utime.tv_usec / 10000,
            (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);
_______________________________________________
[email protected] mailing list
https://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "[email protected]"

Reply via email to