Any way for executing code to know it's running in the eclipse debugger?

*爱你&永不变心* 提交于 2019-12-11 06:59:36

问题


I have some Pax Exam tests. To execute the test normally, I just run the JUnit class in Eclipse. If I want to step through the code in the Eclipse debugger, I have to make it set the debug options, including the flag to make it wait for the debugger connection, which is a separate process I have to run. I'm presently having this code check for a "debug" system property to enable this, but that's sort of annoying.

It would be really nice if the @Configuration method can look at a system property or some other condition that will always be true if the code is executing in the debugger, so I could use that as a trigger to enable those flags, instead of a manually set "debug" system property.

I've already tried setting a breakpoint at the top of this method and inspecting all the system properties for something that might be set while in the debugger, but I didn't see anything.

Update:

Just to be clear, I need to point out some details about how Pax Exam tests work, to better explain why I'm looking for a way to improve this process. When the test runs, it forks a Karaf container to run the test in. In order to run a test in the debugger, you have to force the code that runs in the container to set the "suspend=y" flag, which will wait for a debugger connection. You definitely don't want to do that if you're not debugging.

After starting the pax exam test running, you then have to run another debug configuration, to make a remote connection to the karaf container. Technically, the run configuration for the unit test itself doesn't need to be a debug configuration.

So, the easiest way to make this happen is to have the the code that initiates the container check for a "debug" system property (or whatever you want to call it), and when that is set, to set the debugger port and the "suspend=y" flag. If the property is not set, it doesn't do that.

So, if you're running the test without debug, you have to make sure that system property is not set. If you're debugging, you have to make sure it's set. It's an annoyance to have to edit the run configuration each time you need to go back and forth.

So, what I was intending was to start the unit test run configuration as a debug configuration (even though it doesn't need to be), and for the code that starts the karaf container to detect that it's being run as a debug configuration, and set the "suspend=y" flag in that case.

I've concluded that there is no way for the code itself to detect this, but I'll detail in my own answer how I get the debugger to help me a bit.


回答1:


I'm going to self-answer to address my original problem, although it isn't quite the answer to my original question, which the first answer does attempt to address. That answer doesn't help me, however.

My real need was to able to run my Pax Exam test so that when I first run the unit test, which is running the "server" portion of the Pax Exam test, it will know to provide the correct "-Xdebug" parameters to the server if I'm going to be using the debugger, and NOT to if I'm not using the debugger. I have code that checks for the "debug" system property and uses that to set the correct "-Xdebug" parameters, but I don't want to have to manually add or remove that parameter from the run configuration if I need to change how I'm running the test (going between debugging and not debugging).

So, as far as I can see, the best I can do is make it so that when I run the "server" portion of the unit test in the debugger (which otherwise doesn't actually have to be in the debugger, as it's only the client side that needs it), this will cause the system property I'm checking for to be set, so it will set the correct flags.

I'm not aware of any feature in Eclipse that lets me run particular predefined snippets of code when I start any debugging session (I mean "any", not a "particular" debugging session), but there is something that comes close, even though it's a bit of a hack.

What I did was set a breakpoint at the top of the method that sets the karaf configuration to be started, and I made the breakpoint conditional, with the following expression:

(System.setProperty("debug", "true") != null) && false

This will set the system property I need, but then not stop, as the final expression will be false.

Technically, it doesn't even need to be in this method, it just has to be hit before the karaf options are set.

This stays as a workspace setting, so I don't need to re-add this every time I start Eclipse.

Update:

With the upgrade to Oxygen, this can be slightly simpler, with the new "tracepoints" feature (https://www.eclipse.org/eclipse/news/4.7/jdt.php#toggle-trace-point). Just "toggle tracepoint" and set the expression to 'System.setProperty("debug", "true")'




回答2:


If you are debugging eclipse application there is a way, you can use Platform.isDebug() to check whether your application is using debug or run lanuch configuration.

In your case i.e checking in Junit test run, I am not aware of how to detect whether we are using run/debug launch config. But strongly believe that there must a way to find it out with code like what you suggested using System Class.

Why can't you pass your own argument/environment variable in Junit debug launch config(With -D= in Arguments tab or in Environment tab? and use it in the test code to detect?



来源:https://stackoverflow.com/questions/43125798/any-way-for-executing-code-to-know-its-running-in-the-eclipse-debugger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!