简体   繁体   English

Intelij IDEA 2022.1 无法调试 Runtime.getRuntime().addShutdownHook();

[英]Intelij IDEA 2022.1 Cannot debug Runtime.getRuntime().addShutdownHook();

I've tried to debug Runtime.getRuntime().addShutdownHook(unfortunateHook);我试图调试Runtime.getRuntime().addShutdownHook(unfortunateHook); in sample tapestry application created with Maven quickstart, but it seems that it doesn't work - Debugger doesn't stop at trap set to line set at LOGGER.debug("In the main hook!!!");在使用 Maven 快速启动创建的示例挂毯应用程序中,但它似乎不起作用 - 调试器不会停止在陷阱设置为LOGGER.debug("In the main hook!!!");

Example code:示例代码:

package org.example.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class App extends SpringBootServletInitializer {

    private static final Logger LOGGER= LoggerFactory.getLogger(App.class);

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(AppConfiguration.class);
    }

    public static void main(String[] args) throws Exception {
        Thread unfortunateHook = new Thread(() -> {
            LOGGER.debug("In the main hook!!!");
            try
            {
                Thread.sleep(5000);
            }
            catch (InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        });
        Runtime.getRuntime().addShutdownHook(unfortunateHook);
        LOGGER.debug("Simple log statement with inputs {}, {} and {}", 1,2,3);

        SpringApplication application = new SpringApplication(App.class);
        SpringApplication.run(App.class, args);
    }
}

Is it possible to debug code inside shutdown hook thread?是否可以在关闭挂钩线程中调试代码? I'm using InteliJ Idea 2022.01.我正在使用 InteliJ Idea 2022.01。 Killing application with standard Intelij Idea way (red square).使用标准 Intelij Idea 方式(红色方块)杀死应用程序。

Ok, after some more digging I have found this: https://youtrack.jetbrains.com/issue/IDEA-170313/Breakpoints-not-working-after-stop-signal and many more simmilar threads.好的,经过更多的挖掘,我发现了这个: https ://youtrack.jetbrains.com/issue/IDEA-170313/Breakpoints-not-working-after-stop-signal 和更多类似的线程。

In short, Idea is disconnecting debugger in a moment that you click red square and sends signal to app to kill itself.简而言之,在您单击红色方块并向应用程序发送信号以杀死自己的那一刻,Idea 正在断开调试器。 Because of these, traps are not working any more.因此,陷阱不再起作用。

Workaround to this is to send kill signal from external source.解决方法是从外部源发送终止信号。 In Linux it's pretty easy but in windows (which is I'm using) you should:在 Linux 中这很容易,但在 Windows(我正在使用)中,您应该:

  1. Put a break point any place in your code that will be executed在将要执行的代码中的任何位置放置一个断点
  2. When code stops at your trap, you go to "evaulate expression" (alt+f8 or from degug acctions) and write System.exit(0)当代码在您的陷阱处停止时,您转到“评估表达式”(alt+f8 或从 degug 操作)并编写System.exit(0)
  3. You breakpoint may need to be set up as a thread one - to do that, click right button on it and switch to "Thread"您的断点可能需要设置为一个线程 - 为此,请单击它的右键并切换到“线程”

Then breakpoint should work.然后断点应该起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM