开发者

DebugBreak equivalent in Java?

开发者 https://www.devze.com 2023-03-25 13:56 出处:网络
I\'m looking for a way to break into the debugger from 开发者_如何学运维Java code, without setting a breakpoint in the IDE.

I'm looking for a way to break into the debugger from 开发者_如何学运维Java code, without setting a breakpoint in the IDE.

In Win32 there was DebugBreak(), in C# there's DebugBreak() Equivalent in C#, but I can't find anything in Java.

What I'm trying to do: say I have a wait with a 30s timeout, but in normal conditions that wait should always be <1s. I'd like to use ByteMan or something similar to wait with 1s timeout first, and break into the debugger if that wait timed out.


Not a direct answer to your question but in most IDE's you can set conditional breakpoints.

This would require you to set a timestamp variable in your code before the timeout and test its value in your breakpoint, after the timeout. You would trigger your breakpoint conditional if the delta is greater than your threshold.


The poster of this question has done exactly what I was looking for: Secure Debugging for Production JVMs

It reproduces the best feature of DebugBreak, that is you can attach the IDE to your program only after the "breakpoint" is hit.


I created this class

import com.sun.jna.Library;

public interface Kernel32 extends Library {

    public void OutputDebugStringA(String Text);
    public void DebugBreak();    
}

then used it like this:

Kernel32 lib = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);
lib.OutputDebugStringA("just before DebugBreak\n");
lib.DebugBreak();

and that's it. It calls native function void WINAPI DebugBreak(void); inside Kernel32.dll

You also need the jar file jna-4.4.0.jar


for easier situation inside IDE use also this example with breakpoint in body, poor mans conditional... (sorry for c# format)

        if (lineNumber==6502)
        {
            System.out.println("BREAK"); //breakpoint here
        }


Currently (as of 2021) you cannot accomplish this in Java in a way as convenient as DebugBreak is in C#.

The best you can do in Java is throw a custom runtime exception (say, class DebugBreakException extends RuntimeException) and pray that you have also remembered to add an "exception breakpoint" which breaks whenever this exception is thrown, even if the exception is handled.

Immediately after your code throws this exception, it catches it and dumps a message saying that this particular exception occurred.

If this message ever appears in the log, and yet your debugger did not break, then you know you forgot to add the necessary "exception breakpoint." (In other words, you have to pray that if this message ever gets logged, it will be noticed by someone.)

You can also be extra smart about it and add some code which checks how many milliseconds have elapsed from the moment the exception was thrown until it was caught; if more than, say, 10 milliseconds elapsed, then most probably there was a break into the debugger, so the logging of the message can be skipped. If the exception was caught within fewer than 10 milliseconds, then the debugger probably did not break, so you can proceed with logging the message.

It goes without saying that besides simply logging a message you can try to take as many additional measures as you can, like produce a beep, turn on a lamp, send an email to yourself, bang a gong, fry a motherboard, whatever it takes.

0

精彩评论

暂无评论...
验证码 换一张
取 消