10

typically I set a breakpoint in my Java application when I want to observe the run.

However sometimes I just want to know if a method is called or not. Therefore a breakpoint does not help me, and I insert a "systrace" statement System.out.println("method signature");

I thought it would be a nice feature If I could set a breakpoint and when the breakpoint is reached to just print out the systrace message and continue the run.

Do you know if this is possible?

matthias
  • 1,938
  • 23
  • 51
  • 1
    Why not you set a breakpoint in that method? – Tony Jul 15 '14 at 11:23
  • As @Tony said why don't you place a breakpoint in or on that method? If you place the breakpoint on the method signature it will be executed whenever that method is called independent of the line or whether there are other debug instructions in the code or not. – Thomas Jul 15 '14 at 11:28
  • 1) I don't want the program to stop – matthias Jul 15 '14 at 11:36
  • 2) I don't want to manually insert code – matthias Jul 15 '14 at 11:37
  • @Thomas Sorry, but I don't know 'other debug instructions'. Any tutorial? – Tony Jul 15 '14 at 11:53
  • @matthias if you do not want to manually insert code, another approach you may like would be to use AspectJ to insert the additional methods you need. – Thorbjørn Ravn Andersen Jul 15 '14 at 12:16
  • @Tony sorry that was a typo, I meant "other debug information", i.e. line numbers etc. which might be removed by the compiler if told so. – Thomas Jul 15 '14 at 14:00

1 Answers1

15

You have to make it a conditional breakpoint with a following condition:

System.out.printf(Thread.currentThread().getStackTrace()[1].getMethodName() + "\n") == null

Works fine in my Eclipse. I'm using printf to make printing code evaluate to boolean. Not sure if there's a way to automate inserting this code into a breakpoint.

  • Hi, thanks. This is I was looking for. However it would be nice withouth the need of an conditional breakpoint. An option like "print systrace on breakpoint" would be cool – matthias Jul 15 '14 at 11:52
  • 3
    not working with eclipse Neon: "operator == is undefined for the argument type(s) void, null". The condition should be `System.out.printf(...); return false;` – user85421 Jan 12 '17 at 14:25