Florian Reisinger
Florian Reisinger

Reputation: 3088

Java debugger interface (JDI) - How to set a breakpoint

I am asking because I am unable to find anything using Google.

I want to debug org.Example. I want to set an breakpoint and then resume the VM to execute till the breakpoint. org.Example is just a dummy main function.

So I do have my VM, but I didn't find a way to get to a Locationobject. I reduced the problem to not being able to get a ReferenceTypeobject.

My problem: I have a simple command line debugger with a launching connector

I set the classpath to "<Project_DIR>\out\artifacts\javaDebugger_jar\*" for the VM, where the program to debug should run. In the javaDebugger_jar folder are multiple JAR files, one of those containing the org/Example.class file.

If I call "vm.resume()" the program will execute. I guess before the "resume" the Main Class is not loaded.

So what is the procedure here? How to make sure the class is loaded or do I need to set a breakpoint on method entry for a specific class (which I know how) and then in set the breakpoint as on method entry in class "Example" the breakpoint can be set.

I am asking because that seems to be so stupid. Is there a better way?

Upvotes: 0

Views: 627

Answers (2)

davmac
davmac

Reputation: 20631

You can create a "class prepare request" via the virtual machine's event request manager to get notified when classes are prepared:

    EventRequestManager erm = machine.eventRequestManager();
    erm.createClassPrepareRequest().enable();

This will give you an event when any class is prepared on the VM, regardless of how it is loaded. You can set a filter so that it only notifies for specific classes, if you like. You may wish to set the suspend policy if you rely on this in order to set a breakpoint when the class is loaded.

Upvotes: 0

Florian Reisinger
Florian Reisinger

Reputation: 3088

So after thinking for a while this behaviour is expected.

I am trying to set a breakpoint before any (non jdk) classes are loaded. This simply is not possible.

I need to watch for the methd entry of the main method ans then set the breakpoints. This means:

org.Example is loaded. All class files on which this class depends are loaded.

I am aware this is only true for classes which are directly dependant. I am not sure this is working for JDBC drivers, which are loaded via a Class.forName or other things.

For me this solution is sufficient. If you have an addition how such classes (classes which are dynamically loaded) are found, please answer this question as well.

Upvotes: 0

Related Questions