如何使用 jdb 调试 lambda 表达式

How to debug a lambda expression using jdb

我在集群生产服务器中遇到问题,我设法将单个实例与用户隔离开来,因此我可以使用它进行调试,我正在使用 jdb 执行此操作。长介绍结束。我的问题是我需要将代码调试为 lambda 表达式

public void method(){
    this.privateField = Util.methodCall(); // Here the breakpoint works
    Clazz.staticMethod(() -> {
       Integer x = 1;
       Long y = 2; 
       y = x * y; // I need a Break point here
       /* 
         And a lot of non related code
       */
       });
}

当我在该行的 jdb 中创建断点时,它会忽略我的断点。我很确定调用该方法是因为断点到达了方法的第一行,但只是忽略了另一行,我正在使用 nextstep 命令,但不起作用。那么我的过程可能有什么问题呢?如何使用 jdb 调试 lambda 表达式?

看起来 jdb 上次得到一些爱是在 Java 6...

替代方案:IntelliJ 支持此功能(lambda 表达式内的断点)

这里是为登陆这里并感兴趣的人提供的答案。

创建class

import java.util.concurrent.Callable;
public class Clazz {

    public static void main(String[] args) throws Exception {
        new Clazz().method();
    }

    public void method() throws Exception {
        Clazz.staticMethod(() -> {
            Integer x = 1;
            Long y = 2L;
            y = x * y; // I need a Break point here
            return y;
        });
    }

    private static void staticMethod(Callable i) throws Exception {
        System.out.println("i = " + i.call());
    }
}

编译它

javac Clazz.java

为此启动 jdb class

jdb Clazz
Initializing jdb ...

使用 stop

在 main 方法中设置断点
> stop in Clazz.main
Deferring breakpoint Clazz.main.
It will be set after the class is loaded.

使用 run

启动调试会话
> run
run Clazz
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
> 
VM Started: Set deferred breakpoint Clazz.main

Breakpoint hit: "thread=main", Clazz.main(), line=6 bci=0
6            new Clazz().method();

现在调试器在 main 方法中停止,就在调用 new Clazz().method();.

之前

要找到我们感兴趣的行,我们 list 来源

main[1] list
2    
3    public class Clazz {
4    
5        public static void main(String[] args) throws Exception {
6 =>         new Clazz().method();
7        }
8        
9        public void method() throws Exception {
10            Clazz.staticMethod(() -> {
11                Integer x = 1;
main[1] list 12
8        
9        public void method() throws Exception {
10            Clazz.staticMethod(() -> {
11                Integer x = 1;
12 =>             Long y = 2L;
13                y = x * y; // I need a Break point here
14                return y;
15            });
16        }
17    

命令list 12 需要列出以下行。在输出中我们可以看到我们想要在行 13 处停止。因此,让我们使用 stop 命令

在那里设置一个新断点
main[1] stop at Clazz:13
Set breakpoint Clazz:13

继续执行直到下一个断点发出命令cont

main[1] cont
> 
Breakpoint hit: "thread=main", Clazz.lambda$method[=17=](), line=13 bci=12
13                y = x * y; // I need a Break point here

我们不在线 13 例如可以 dump xy 的值。

main[1] dump x
 x = {
    MIN_VALUE: -2147483648
    MAX_VALUE: 2147483647
    TYPE: instance of java.lang.Class(reflected class=int, id=568)
    digits: instance of char[36] (id=569)
    DigitTens: instance of char[100] (id=570)
    DigitOnes: instance of char[100] (id=571)
    sizeTable: instance of int[10] (id=572)
    value: 1
    SIZE: 32
    BYTES: 4
    serialVersionUID: 1360826667806852920
    java.lang.Number.serialVersionUID: -8742448824652078965
}
main[1] dump y
 y = {
    MIN_VALUE: -9223372036854775808
    MAX_VALUE: 9223372036854775807
    TYPE: instance of java.lang.Class(reflected class=long, id=574)
    value: 2
    SIZE: 64
    BYTES: 8
    serialVersionUID: 4290774380558885855
    java.lang.Number.serialVersionUID: -8742448824652078965
}

更进一步step

main[1] step
> 
Step completed: "thread=main", Clazz.lambda$method[=19=](), line=14 bci=26
14    

我们现在可以再次 dump y

的值
main[1] dump y
 y = {
    MIN_VALUE: -9223372036854775808
    MAX_VALUE: 9223372036854775807
    TYPE: instance of java.lang.Class(reflected class=long, id=574)
    value: 2
    SIZE: 64
    BYTES: 8
    serialVersionUID: 4290774380558885855
    java.lang.Number.serialVersionUID: -8742448824652078965
}