如何以编程方式检测当前 JVM 是否已被远程调试器连接?

发布于 2025-01-10 19:32:15 字数 166 浏览 1 评论 0原文

我面临的情况是,当我在子线程中调试代码时,其包装 future 有超时,我总是在外部 future.get(timeout) 上得到 TimeoutException,我的想法如果我知道调试器已连接,我可以动态放大 future.get() 的 timeout 参数

The situation that I'm facing is when I debug my code in a sub-thread, whose wrapping future has a timeout, I always get a TimeoutException on the outter future.get(timeout), my idea is if I can know that a debugger is connected, I can dynamically enlarge the timeout parameter of the future.get()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就是爱搞怪 2025-01-17 19:32:15

查找是否已附加调试器的一种选择是检查名为“JDWP Command Reader”的线程是否正在运行:

public static boolean isDebuggerPresent() {
    ThreadInfo[] infos = ((com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean())
            .dumpAllThreads(false, false, 0);
    for (ThreadInfo info : infos) {
        if ("JDWP Command Reader".equals(info.getThreadName())) {
            return true;
        }
    }
    return false;
}

但是,我建议不要以编程方式检测调试器。一般来说,应用程序行为永远不应该依赖于调试器的存在,否则它会破坏调试真实应用程序的整个想法。最好从外部显式配置timeout

One option to find if debugger is attached is to check whether a thread named "JDWP Command Reader" is running:

public static boolean isDebuggerPresent() {
    ThreadInfo[] infos = ((com.sun.management.ThreadMXBean) ManagementFactory.getThreadMXBean())
            .dumpAllThreads(false, false, 0);
    for (ThreadInfo info : infos) {
        if ("JDWP Command Reader".equals(info.getThreadName())) {
            return true;
        }
    }
    return false;
}

However, I'd suggest against detecting debugger programmatically. In general, application behavior should never depend on the presence of debugger, otherwise it ruins the entire idea of debugging the real application. It's probably better to make timeout explicitly configurable from outside.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文