Skip to content

Instantly share code, notes, and snippets.

@henryw374
Last active February 28, 2020 15:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henryw374/b525dc4f41c667c9272e1773c689066f to your computer and use it in GitHub Desktop.
Save henryw374/b525dc4f41c667c9272e1773c689066f to your computer and use it in GitHub Desktop.
thread dump clojure programmatic jstack
(ns thread-dump
(:require [clojure.stacktrace :as st])
(:import [java.lang.management ManagementFactory]))
(defn jstack [n]
(let [threadMXBean (ManagementFactory/getThreadMXBean)
info (.getThreadInfo threadMXBean (.getAllThreadIds threadMXBean) 100)]
(print
(for [threadInfo info]
(str
(newline)
(.getThreadName threadInfo)
"\n" (.getThreadState threadInfo)
"\n"
(with-out-str
(let [st (.getStackTrace threadInfo)]
(newline)
(print " at ")
(if-let [e (first st)]
(st/print-trace-element e)
(print "[empty stack trace]"))
(newline)
(doseq [e (if (nil? n)
(rest st)
(take (dec n) (rest st)))]
(print " ")
(st/print-trace-element e)
(newline)))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment