manifold-systems / manifold

Manifold is a Java compiler plugin, its features include Metaprogramming, Properties, Extension Methods, Operator Overloading, Templates, a Preprocessor, and more.
http://manifold.systems/
Apache License 2.0
2.43k stars 125 forks source link

Question: Manifold preprocessor: Can it detect if a Debugger is attached? #317

Closed schittli closed 3 years ago

schittli commented 3 years ago

Good evening Scott

I hope you're doing well.

I have a question, maybe an idea: Java has since its beginning the problem that it can not detect if a debugger is attached at runtime, therefore there is no (easy) code like Debugger.IsAttached(), unfortunately.

Would there be an elegant way to use the Manifold preprocessor to detect the attached debugger? Maybe you have an idea :-)

Thanks a lot, kind regards, Thomas

rsmckinney commented 3 years ago

Hi Thomas. (still need to get back to the other issue!)

I don't have any immediate ideas, however if knowing at runtime whether the JVM is in debug mode is sufficient, you can do this:

      boolean isDebugMode() {
        List<String> values = java.lang.management.ManagementFactory.getRuntimeMXBean().getInputArguments();
        for(String value : values) {
          if(value.startsWith( "-Xrunjdwp:") || value.startsWith("-agentlib:jdwp=")) {
            return true;
          }
        }
        return false;
      }
    }

I do this, for example, to conditionally change caching strategies to make it easier to catch certain classes of bugs.

schittli commented 3 years ago

Hello Scott

thanks a lot for your help!, your isDebugMode() method works great. I am surprised that not many Java developers miss this kind of information.

On the other issue, yes it is perfectly OK and important to prioritize, I keep my eyes open and test every Manifold update, maybe it gets solved on the fly 😃