Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you detect in Java if the code is being debugged? [duplicate]

Tags:

java

debugging

Possible Duplicate:
How to find out if “debug mode” is enabled

Coming from C#, I cannot believe that Java has no way to execute code only when the program is being debugged. Somehow, Log4J seems to be able to do it. Does anyone know a possibility for this? I'm thinking of something like this:

#if DEBUG
    executedCode();
#endif

Or something like this:

if (Java.isDebugging())
    executeCode();

Ideas?

EDIT: Thanks to Matt Ball, the code in this possible duplicate works:

public static boolean debugging = java.lang.management.ManagementFactory.getRuntimeMXBean().
        getInputArguments().toString().indexOf("-agentlib:jdwp") > 0;
like image 815
Akku Avatar asked Jul 17 '12 14:07

Akku


People also ask

What is the purpose of debug?

The purpose of debugging is to locate and fix the mistake. The testing process does not help the developer figure out what the coding mistake is -- it simply reveals what effects the coding error has on the program.

What is a breakpoint in debugging?

Breakpoints are one of the most important debugging techniques in your developer's toolbox. You set breakpoints wherever you want to pause debugger execution. For example, you may want to see the state of code variables or look at the call stack at a certain breakpoint.

What is Java debugging and why is it important?

One of the biggest nightmares for all developers is to debug their code. Debugging code written in Java is a tough task. Debugging is the process of determining and fixing bugs or errors present in the code, project, or application. Debugging your application helps you improve the quality of the code.

How can a Java program find out if it is running debug?

How can a Java program find out if it is running in debug mode? The application should behave a bit different in regular “full speed” mode than in “debug mode” (when a debugger is attached, when running in debug mode). The application communicates over TCP with either another computer, another process, or within itself.

How do I debug a class in Java?

Right-click on the class name from the project explorer and click on Debug As -> Java Application. Once you run the application in debug mode, the following window will show up. You can see, the line on which the breakpoint is applied is highlighted and the execution of code has stopped at that point.

How to debug Java code before deployment?

If an application crashes in a production environment, it can badly impact your business. You can prevent crashes and errors by spending time testing and debugging your code before deploying it. There are several ways to debug Java code. When developing, you can use the tools available in your IDE or debugger.


2 Answers

I think you are conflating a debug build of a program with running under the debugger (unless C# is rather strange).

Many systems - including lots of C/C++ build environments, and I expect C#, given the way Microsoft has historically designed the Visual Studio build processes - have two different types of builds you can do:

  • Release build, with only info/error/warning logging, no debug symbols, extra optimizations, etc.
  • Debug build, with extra logging enabled, debug symbols, possibly tuned-down optimizations.

You use debug builds while developing and debugging your program, and release builds for final QA and release. The debug builds, however, are independent of being run under a debugger - they're normal programs, and if you run them the run and still output their extra logging information. This can be very useful.

Running them under a debugger is completely orthogonal. You can run the release build under a debugger (although, lacking debug symbols, it might not be very useful). In fact, I would submit that you do not want code to change based on whether you are running under a debugger - it makes it rather difficult to debug the code being actually run in the application.

And unlike C, C++, and C#, Java has no facility for compile-time conditionals, so everything is enabled in the program. You typically deal with this by having a very fast way to check if the debugging message is to be emitted, and doing as little work as possible in the case where it is not.

like image 76
Michael Ekstrand Avatar answered Oct 14 '22 13:10

Michael Ekstrand


C doesn't execute code only when it is debugging, what you're showing is a conditional compile directive, the code only gets compiled when the flag value is true. The running code has no idea when it is being debugged. Likewise with Log4j it is reading from its configuration what level to use, it has no idea when it is being debugged.

Conditional compile flags were deliberately left out of Java, they wanted to avoid complexity. C# has a different philosophy.

like image 40
Nathan Hughes Avatar answered Oct 14 '22 14:10

Nathan Hughes