Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracepoints in Eclipse

There seem to be no support for tracepoints in Eclipse (JDT) debug mode.

This is really a bummer, since you may sometime want to print a lot of debug infomation without cluttering your code with hardcoded println that you are likely to forget later on.

I found a simple workaround for that and wanted to share with SO.

like image 963
Raphael Jolivet Avatar asked Feb 18 '13 13:02

Raphael Jolivet


People also ask

How do I enable break points in eclipse?

Breakpoints To define a breakpoint in your source code, right-click in the left margin in the Java editor and select Toggle Breakpoint. Alternatively, you can double-click on this position. The Breakpoints view allows you to delete and deactivate Breakpoints and modify their properties.

How do I go back in debug mode in Eclipse?

Open Debug Configuration -> Debugger -> Enable Reverse Debugging at startup . Than you can press shift+F5 or shift+F6 for step back like F5 or F6 for step forward.

How do I debug a .class file in eclipse?

There are two ways to debug a class file. The first way is to set the decompiler preference, and to realign the line number. The second way is to check the debug mode in the decompiler menu bar. When your Eclipse workspace is in debug perspective, the debug mode becomes the default.


2 Answers

Eclipse supports conditional breakpoints.

All you have to do, is provide it with a boolean expression, and it'll be happy. In this case, a static method that also prints useful stuff as a side effect is fine.

public class TracePointUtils {

  static private Logger logger = org.slf4j.LoggerFactory.getLogger(TracePointUtils.class);

  /** Produce a WARN message in SL4J logger */
  public static boolean trace(String msg, Object ... args) {
    logger.warn(format(msg, args));
    // Do not stop
    return false;
  }
}

Here, I use SL4J as a logger, but a plain println would be fine.

Then, add a breakpoint in your code, and call this static method as the condition: tracepoints screenshot

Et voila, you can enjoy nice traces in your console.

like image 162
Raphael Jolivet Avatar answered Oct 14 '22 09:10

Raphael Jolivet


Just for the record, Eclipse Oxygen provides support for tracepoints:

http://www.eclipse.org/oxygen/noteworthy/#conditional-watchpoint

Clicking around in the menu seems tedious, I think it's best to create a key mapping for the Toggle Tracepoint command.

like image 32
Csákvári Dávid Avatar answered Oct 14 '22 07:10

Csákvári Dávid