1

I'm trying to debug some Java code involving complicated mutually recursive functions, and wishing for a debugging facility I have only encountered in Lisp, and wondering if there is a way to implement an equivalent of it.

In Lisp, you can mark a function for tracing, and then each time it is called, a note will be printed to the console along with the argument values. (Okay, this much is easy enough to hack in any language.) Each time it returns, a note will be printed along with the return value. (This is more difficult and more valuable.) Finally, part of what makes it really valuable, recursive calls are marked with indentation. An example from http://clhs.lisp.se/Body/m_tracec.htm if the factorial function FACT is defined recursively:

;; Of course, the format of traced output is implementation-dependent.
 (fact 3)
>>  1 Enter FACT 3
>>  | 2 Enter FACT 2
>>  |   3 Enter FACT 1
>>  |   | 4 Enter FACT 0
>>  |   | 4 Exit FACT 1
>>  |   3 Exit FACT 1
>>  | 2 Exit FACT 2
>>  1 Exit FACT 6
=>  6

Is there a way to do something like this in Java?

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
rwallace
  • 31,405
  • 40
  • 123
  • 242
  • Looks like, you might be looking for some `Logging` specific qualities! – Naman Nov 25 '18 at 04:32
  • @nullpointer Skimming the documentation on Java logging frameworks, I don't see how they address this use case. What am I missing? – rwallace Nov 25 '18 at 04:39
  • 1
    Java is not LISP (notably Java objects are "closed") - although there are some AOP frameworks that allow similar. Logging can do this. A profiler is a better bet. Or a debugger. – Elliott Frisch Nov 25 '18 at 04:47
  • @ElliottFrisch I'm not asking for it to be done in the same way. A debugger does not do this; it just lets you peek at the current value of variables in a given frame. The overview documentation on logging doesn't say anything about it doing anything like this. What am I missing? – rwallace Nov 25 '18 at 04:50
  • 1
    Nothing I know specifically implements the indented log lines though, and it would be a little tricky to add. It might be possible to add to a log viewer program, if both entry and exit of a function was logged. – markspace Nov 25 '18 at 04:50
  • @markspace Okay, so is there a way to log function exit? That is, an easier way than going around and manually editing every return statement in the function? – rwallace Nov 25 '18 at 04:55
  • @rwallace AOP and [Cross Cutting Concern](https://stackoverflow.com/a/25779679/2970947). Although, you might have better luck with [clojure](https://clojure.org/). Or [Armed Bears](https://common-lisp.net/project/armedbear/). – Elliott Frisch Nov 25 '18 at 04:57
  • 2
    No it's pretty much manually logging every return statement. That's why most of us just use a debugger, or make do without the indentation. – markspace Nov 25 '18 at 05:00
  • @markspace Fair enough, not the answer I was hoping for, but a clear answer nonetheless. Thanks! – rwallace Nov 25 '18 at 05:01

0 Answers0