在Eclipse中调试:没有断点的变量快照

时间:2023-01-17 19:21:24

I am debugging a Java program in Eclipse. I would like to watch a specific variable. However, since my program uses a GUI, creating a breakpoint causes the window to freeze. This is particularly annoying when e.g. trying to right click on an item and navigate a context menu.

我正在Eclipse中调试Java程序。我想看一个特定的变量。但是,由于我的程序使用GUI,因此创建断点会导致窗口冻结。这在例如尝试右键单击某个项目并导航上下文菜单。

I don't actually want to stop the program, I just want to watch a specific variable and log its value every time a certain line is reached. At the moment, I am doing that with print statements, but I was wondering whether there was a way to do this using the debug view.

我实际上并不想停止程序,我只是想看一个特定的变量并在每次达到某一行时记录它的值。目前,我正在使用print语句执行此操作,但我想知道是否有办法使用调试视图执行此操作。

(Note: I don't want to write this to a log file. This is not a variable I need to look at long term. At the moment I just print it out, look at the values it takes on, and then delete the print statement. It would be nice to have something like this which I can keep in the debug view without having print / log statements cluttering my code).

(注意:我不想将其写入日志文件。这不是我需要长期查看的变量。目前我只是将其打印出来,查看它所采用的值,然后删除打印语句。如果有这样的东西,我可以保留在调试视图中,而不会使打印/日志语句混乱我的代码,这将是很好的。

1 个解决方案

#1


2  

You can add a conditional breakpoint that returns false. The code in the condition will be evaluated, and then you return false to indicate to the debugger that you do not want to stop execution.

您可以添加返回false的条件断点。将评估条件中的代码,然后返回false以向调试器指示您不想停止执行。

Right click on breakpoint properties, Check "Conditional" In the text field, enter the code you want to run (e.g. print x). Make sure the last statement in the condition is "return false"

右键单击断点属性,选中“条件”在文本字段中,输入要运行的代码(例如,打印x)。确保条件中的最后一个语句是“return false”

Here is my code:

这是我的代码:

public static void main(String[] args) {
        String x  = " Some value"; 
        String y  = "a value"; //breakpoint here
        System.out.println("Hello World!");
    }

Say I want to print the value of x just after it is declared. Set a breakpoint where indicated in the code, Add a condition:

假设我想在声明后立即打印x的值。设置代码中指示的断点,添加条件:

System.out.println("Value of x: " + x);
return false;

Output:

输出:

Value of x: Some Value
Hello World!

#1


2  

You can add a conditional breakpoint that returns false. The code in the condition will be evaluated, and then you return false to indicate to the debugger that you do not want to stop execution.

您可以添加返回false的条件断点。将评估条件中的代码,然后返回false以向调试器指示您不想停止执行。

Right click on breakpoint properties, Check "Conditional" In the text field, enter the code you want to run (e.g. print x). Make sure the last statement in the condition is "return false"

右键单击断点属性,选中“条件”在文本字段中,输入要运行的代码(例如,打印x)。确保条件中的最后一个语句是“return false”

Here is my code:

这是我的代码:

public static void main(String[] args) {
        String x  = " Some value"; 
        String y  = "a value"; //breakpoint here
        System.out.println("Hello World!");
    }

Say I want to print the value of x just after it is declared. Set a breakpoint where indicated in the code, Add a condition:

假设我想在声明后立即打印x的值。设置代码中指示的断点,添加条件:

System.out.println("Value of x: " + x);
return false;

Output:

输出:

Value of x: Some Value
Hello World!