Last active
February 28, 2020 15:15
-
-
Save henryw374/b525dc4f41c667c9272e1773c689066f to your computer and use it in GitHub Desktop.
thread dump clojure programmatic jstack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)] | |
(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