13

I know about the (jvm) startup options to have the jvm wait until a debugger is attached - this is not what I mean here.

Is it possible from within Java code to also detect attachment of a debugger, so that I could e.g. write a "script" that is doing some stuff and then at a certain point make my app wait for the debugger?

Heiko Rupp
  • 30,426
  • 13
  • 82
  • 119

4 Answers4

6

No. The options are JVM options, and no Javacode is executed before the debugger connects. You can however let the app start, and spinloop on a getter for a variable, which you set from the debugger to let your app continue.

Daniel
  • 27,718
  • 20
  • 89
  • 133
  • I know that those are jvm options (this is what I meant with my first phrase). Interesting idea with the variable; close to what I want. – Heiko Rupp Mar 22 '11 at 15:24
  • 7
    So, there's no `Debugger.IsAttached` for Java? -- Super lame. – BrainSlugs83 Oct 05 '14 at 21:12
  • 5
    Hmmm, after some more searching, seems like for Android there is a `Debug.isDebuggerConnected()` -- too bad there's nothing for regular Java. – BrainSlugs83 Oct 05 '14 at 21:15
  • 1
    If you just want to know if a debugging is enabled in the current JVM session, then you can use `ManagementFactory.getRuntimeMXBean().getInputArguments().contains("-Xdebug")` – flungo Apr 16 '17 at 16:23
  • Please note that debugger arguments can be passed by env variable `JAVA_TOOL_OPTIONS` which will not be present in the command line – samvel1024 Oct 30 '19 at 23:36
3

Depending on what you'd like to do, it might be worthwhile investigating the onthrow JDWP sub-option. I haven't actually tried this ;-) but it seems like you could create a special exception type that you throw and catch to trigger JVM suspension. As shown in the linked examples, combining with launch can provide for some interesting alternatives. Of course, the logic/workflow is different from what you've expressed, but it's something to think about...

kschneid
  • 5,626
  • 23
  • 31
1

The consensus is: no. Java does not have the equivalent of:

But the consensus:

agrees that the best (non-minified) hack is:

public static boolean isDebuggerPresent() {
    // Get ahold of the Java Runtime Environment (JRE) management interface
    RuntimeMXBean runtime = java.lang.management.ManagementFactory.getRuntimeMXBean();

    // Get the command line arguments that we were originally passed in
    List<String> args = runtime.getInputArguments();

    // Check if the Java Debug Wire Protocol (JDWP) agent is used.
    // One of the items might contain something like "-agentlib:jdwp=transport=dt_socket,address=9009,server=y,suspend=n"
    // We're looking for the string "jdwp".
    boolean jdwpPresent = args.toString().contains("jdwp");

    return jdwpPresent;
}

Which will have to suffice until Java officially adds one.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
0

see Determine if a java application is in debug mode in Eclipse for detecting debugger

You could user TimerTask to poll for attachment

Community
  • 1
  • 1
gerardw
  • 5,822
  • 46
  • 39