0

Trying to capture thread dump to a file using kill -3

Tried

-XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=dump.log

This works but looking for something like

jstack 17264 > /tmp/threaddump.txt

Any simple way to capture thread dump using kill -3 and output to a file using command

Cork Kochi
  • 1,783
  • 6
  • 29
  • 45
  • Well, you already know how to get a thread dump with `kill -3`, and how to get a thread dump using `jstack`. What else do you miss and why? – apangin Apr 19 '21 at 13:37
  • @apangin I am looking to redirect the output to a file using the command kill -3 same like jstack example i have given – Cork Kochi Apr 19 '21 at 13:57
  • Why can't you use jstack then? JVM's SIGINT handler dumps threads to the JVM's standard output, and it has no option to redirect output other than the flag you've already mentioned. – apangin Apr 19 '21 at 14:01
  • Sounds like [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – apangin Apr 19 '21 at 14:04
  • @apangin If the app is unresponsive or sick and if I try jstack using -f then that thread dumps has its own issues, the best option maybe to use kill -3 – Cork Kochi Apr 19 '21 at 14:08
  • 1
    `jstack` (without -F) and `kill -3` use the same mechanism to get a thread dump under the hood. They both rely on the cooperation from the JVM; in both cases the threads are dumped at the global VM safepoint. If the JVM hangs, `kill -3` will not help either. – apangin Apr 19 '21 at 14:16
  • @apangin whats the best option to capture thread dumps without losing thread state, lock Ids, what app is about to get sick – Cork Kochi Apr 19 '21 at 14:28
  • 2
    There is no silver bullet. Either JVM works, and therefore it produces a nice thread dump on its own (in response to jstack or kill -3). Or it doesn't work, and the only way to get the information is through an external process that tries to read the foreign memory and reconstruct the JVM structures (this is what jstack -F does). – apangin Apr 19 '21 at 16:39
  • Here's also another great answer by @apangin which also mentions `gcore`: https://stackoverflow.com/questions/26140182/running-jmap-getting-unable-to-open-socket-file/35963059#35963059 – Juraj Martinka Apr 21 '21 at 05:59

0 Answers0