Note: I will post an answer, actually, but any other suggestion is welcome!
This question needs a bit of context, to prevent comments questioning my goal… 🙂
Where I work, we use lot of debug, mostly done with Eclipse.
I know some people question the practice of debugging, asserting that good design and analysis and appropriate unit tests makes easy to dissect a bug.
Well, I work on a large code base (Java / Swing) of a legacy application designed when Java 1.2 (or even 1.1) was the latest version, and the code has lot of cruft, and even after years of working on it, I can’t pretend to know more than 10 % of it!
And the JUnit tests we have covers perhaps 1 % of the code, at best (it is a recent practice here…).
So using a good debugger is important to examine classes, the data inside them, the behavior of the software with the kind of data we provide, etc.
Yet, step-by-step has its limits: it can be slow, we can miss stuff out, and it is impractical in some cases, like looking at a paint() method or with multithreaded parts.
So I took the habit, in these difficult cases, to sprinkle some System.out.println() statements here and there, to see what methods are called, with what parameters, in what order, etc.
I could use logs, but it is somehow simpler this way, and I remove these lines after the problem has been solved.
One problem with that practice is that we have to scrutenize the diffs to eliminate these lines before committing.
Another problem is that we use Perforce as VCS, which is fine, but requires to check out explicitly a file (changing its state from read-only to writable) to add such trace. It is slow (with the Eclipse P4 plug-in) and cumbersome .
So, my question is: is it possible to add contextual trace to some points of code, without altering the source code itself?
Something that can log “I was here and I saw that” each time a line is ran.
I initially searched an Eclipse plugin, found only a proposal for the C debugger, and found out (in comment in SO, I think, but I lost the reference, alas) a solution which I will post below, in the hope it can be useful to somebody else.
As said above, any alternative is good to take.
No need for a plugin in Eclipse: add a breakpoint to the line where you want a trace, make it conditional (eg. Ctrl+double click on the breakpoint icon in the margin; alternatively by right-clicking on the BP line in the BP list and selecting
Breakpoint Properties) by selecting theConditionalcheck box and keeping theSuspend when 'true'option.Then paste something like:
Contextual auto-completion works (reminder:
sysoutfollowed by Ctrl+space expands toSystem.out.println()), you can print out the current thread name, and other useful information.The
return false;line tells Eclipse not to stop on this breakpoint, actually. So the “conditional” code is run, and Eclipse continues to run the code.I was fearing it would slow down the code, but with a modern computer, it isn’t perceptible at human level.
Note: for some reason, when I type the semi-colon after
false, the Perforce plugin wants to check out the current file. You can cancel this, of course. And if you paste the line, it doesn’t react…