Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Debugger: Is it possible selectively suspend threads?

In my past life as a C/C++ programmer, it was possible on some platforms & debugger combinations to selectively suspend threads. After hitting a breakpoint, one could issue commands (or click stuff in the GUI) to ice/de-ice (suspend / wake) a thread. Upon further step/next/run/continue commands, suspended threads would not execute any instructions.

Is this possible with any JVM or Java IDE today? I use IntelliJ and I don't see the feature, nor can I find any information via Google.

To clarify:

  1. I am not asking about the thread policy for a breakpoint, e.g, suspend current thread or all threads. I am aware of that JVM debugger feature.
  2. The dev environment is vanilla: I use IntelliJ Java IDE, and I have access to the source code in question.

Finally, I realise the "Java debugger" is a somewhat vague term. AFAIK, Sun published a debug-on-the-wire format that debuggers use to communicate with the JVM. Perhaps each JVM (IBM vs Sun vs IcedTea vs ???) supports different debug capabilities, but I am only familiar with the Sun/Oracle JVM.

like image 765
kevinarpe Avatar asked Oct 01 '14 08:10

kevinarpe


2 Answers

You can definitely suspend a single thread in jdb (see below). I love IntelliJ as a Java editor. On the other side its debugger is not terrible, but close.

Initializing jdb ...
> run
Nothing suspended.
> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x141 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x140  Finalizer         cond. waiting
  (java.lang.Thread)0x13f                         Signal Dispatcher running
  (java.lang.Thread)0x401                         Java2D Disposer   cond. waiting
  (java.lang.Thread)0x804                         TimerQueue        cond. waiting
Group main:
  (java.lang.Thread)0x322                         AWT-AppKit        running
  (java.lang.Thread)0x323                         AWT-Shutdown      cond. waiting
  (java.awt.EventDispatchThread)0x5ec             AWT-EventQueue-0  cond. waiting
  (java.util.TimerThread)0x750                    Timer-0           cond. waiting
  (java.lang.Thread)0x7fd                         DestroyJavaVM     running
> suspend 0x5ec             
> threads
Group system:
  (java.lang.ref.Reference$ReferenceHandler)0x141 Reference Handler cond. waiting
  (java.lang.ref.Finalizer$FinalizerThread)0x140  Finalizer         cond. waiting
  (java.lang.Thread)0x13f                         Signal Dispatcher running
  (java.lang.Thread)0x401                         Java2D Disposer   cond. waiting
  (java.lang.Thread)0x804                         TimerQueue        cond. waiting
Group main:
  (java.lang.Thread)0x322                         AWT-AppKit        running
  (java.lang.Thread)0x323                         AWT-Shutdown      cond. waiting
  (java.awt.EventDispatchThread)0x5ec             AWT-EventQueue-0  waiting in a monitor
  (java.util.TimerThread)0x750                    Timer-0           cond. waiting
  (java.lang.Thread)0x7fd                         DestroyJavaVM     running
> where 0x5ec
  [1] java.lang.Object.wait (native method)
  [2] java.lang.Object.wait (Object.java:485)
  [3] java.awt.EventQueue.getNextEvent (EventQueue.java:558)
  [4] java.awt.EventDispatchThread.pumpOneEventForFilters (EventDispatchThread.java:263)
  [5] java.awt.EventDispatchThread.pumpEventsForFilter (EventDispatchThread.java:211)
  [6] java.awt.EventDispatchThread.pumpEventsForHierarchy (EventDispatchThread.java:201)
  [7] java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:196)
  [8] java.awt.EventDispatchThread.pumpEvents (EventDispatchThread.java:188)
  [9] java.awt.EventDispatchThread.run (EventDispatchThread.java:122)
AWT-EventQueue-0[1] where 0x322
Current thread isn't suspended.
AWT-AppKit[1] resume 0x5ec
AWT-AppKit[1] where 0x5ec
Current thread isn't suspended.
AWT-EventQueue-0[1]
like image 66
jdb Avatar answered Oct 15 '22 13:10

jdb


IntelliJ IDEA has it now (I'm looking at v15). The Debug window has a Threads panel, that allows suspending and resuming any thread.

https://www.jetbrains.com/idea/help/debug-tool-window-threads.html

Screenshot of Threads panel

like image 23
OrangeDog Avatar answered Oct 15 '22 13:10

OrangeDog