Debugging any reasonably sized program with the following JDI client results, eventually, in a hang.
import com.sun.jdi.*;
import com.sun.jdi.connect.*;
import com.sun.jdi.request.*;
import com.sun.jdi.event.*;
import java.util.*;
import java.io.IOException;
class Dbg {
private String mainClass = null;
// possible values for Connector's startMode parameter
private static final String sInterrupted = "interrupted";
private static final String sRunning = "running";
private VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
private LaunchingConnector conn = vmm.defaultConnector();
private Map args = conn.defaultArguments();
private VirtualMachine vm = null;
private void printArgs(Map args) {
Iterator is = args.values().iterator();
while (is.hasNext()) {
Connector.Argument arg = (Connector.Argument) is.next();
System.out.println(" Name: " + arg.name() );
System.out.println(" Must Specify: " + arg.mustSpecify() );
System.out.println(" Description: " + arg.description() );
System.out.println(" Value: " + arg.value() );
System.out.println("======================================");
}
}
private void setArgs() {
Connector.Argument main = (Connector.Argument)
args.get("main");
Connector.Argument startMode = (Connector.Argument)
args.get("startMode");
if ((main == null) || (startMode == null)) {
Dbg.FATAL("Unable to understand default args...");
}
main.setValue(mainClass);
startMode.setValue(sInterrupted);
//startMode.setValue(sRunning);
}
public static void FATAL(String s) {
System.out.println(s);
System.exit(1);
}
void go() {
setArgs();
printArgs(args);
try {
vm = conn.launch(args);
}
catch (IllegalConnectorArgumentsException e) {
Dbg.FATAL("Error: " + e);
}
catch (VMStartException e) {
Dbg.FATAL("Error: " + e);
}
catch (IOException e) {
Dbg.FATAL("Error: " + e);
}
prepareStep();
while (true) {
doStep();
}
}
void prepareStep() {
List threads = vm.allThreads();
Iterator ts = threads.iterator();
while (ts.hasNext()) {
ThreadReference t = (ThreadReference) ts.next();
requestStep(t);
}
}
void requestStep(ThreadReference t) {
EventRequestManager erm = vm.eventRequestManager();
StepRequest request =
erm.createStepRequest(t,StepRequest.STEP_LINE,StepRequest.STEP_INTO);
//request.addCountFilter(1);
request.enable();
}
void doStep() {
vm.resume();
EventQueue eq = vm.eventQueue();
EventSet es = null;
try {
es = eq.remove();
} catch (InterruptedException e) {
Dbg.FATAL("Interrupted: " + e);
}
EventIterator ei = es.eventIterator();
while (ei.hasNext()) {
Event event = ei.nextEvent();
if (event instanceof StepEvent) {
StepEvent se = (StepEvent) event;
Location l = se.location();
StepRequest request = (StepRequest) se.request();
String src = "<unknown>";
try { src = l.sourceName(); } catch (AbsentInformationException e) {}
int n = l.lineNumber();
String name = l.method().name();
System.out.println("File: " + src + " Line: " + n + " Method: " +
name);
// requeue the request
//requestStep(request.thread());
} else {
System.out.println(event);
}
}
}
Dbg(String mainClass) {
this.mainClass = mainClass;
}
////////////////////////////////////////////////////////
public static void usage() {
System.out.println("Usage:");
System.out.println(" ");
System.out.println(" java Dbg <classname> <args>");
System.out.println(" ");
System.out.println(" ");
}
public static void main(String[] args) {
if (args.length < 1) {
usage();
}
String mainClass = args[0];
System.out.println("class = " + mainClass);
Dbg dbg = new Dbg(mainClass);
dbg.go();
}
}
import com.sun.jdi.*;
import com.sun.jdi.connect.*;
import com.sun.jdi.request.*;
import com.sun.jdi.event.*;
import java.util.*;
import java.io.IOException;
class Dbg {
private String mainClass = null;
// possible values for Connector's startMode parameter
private static final String sInterrupted = "interrupted";
private static final String sRunning = "running";
private VirtualMachineManager vmm = Bootstrap.virtualMachineManager();
private LaunchingConnector conn = vmm.defaultConnector();
private Map args = conn.defaultArguments();
private VirtualMachine vm = null;
private void printArgs(Map args) {
Iterator is = args.values().iterator();
while (is.hasNext()) {
Connector.Argument arg = (Connector.Argument) is.next();
System.out.println(" Name: " + arg.name() );
System.out.println(" Must Specify: " + arg.mustSpecify() );
System.out.println(" Description: " + arg.description() );
System.out.println(" Value: " + arg.value() );
System.out.println("======================================");
}
}
private void setArgs() {
Connector.Argument main = (Connector.Argument)
args.get("main");
Connector.Argument startMode = (Connector.Argument)
args.get("startMode");
if ((main == null) || (startMode == null)) {
Dbg.FATAL("Unable to understand default args...");
}
main.setValue(mainClass);
startMode.setValue(sInterrupted);
//startMode.setValue(sRunning);
}
public static void FATAL(String s) {
System.out.println(s);
System.exit(1);
}
void go() {
setArgs();
printArgs(args);
try {
vm = conn.launch(args);
}
catch (IllegalConnectorArgumentsException e) {
Dbg.FATAL("Error: " + e);
}
catch (VMStartException e) {
Dbg.FATAL("Error: " + e);
}
catch (IOException e) {
Dbg.FATAL("Error: " + e);
}
prepareStep();
while (true) {
doStep();
}
}
void prepareStep() {
List threads = vm.allThreads();
Iterator ts = threads.iterator();
while (ts.hasNext()) {
ThreadReference t = (ThreadReference) ts.next();
requestStep(t);
}
}
void requestStep(ThreadReference t) {
EventRequestManager erm = vm.eventRequestManager();
StepRequest request =
erm.createStepRequest(t,StepRequest.STEP_LINE,StepRequest.STEP_INTO);
//request.addCountFilter(1);
request.enable();
}
void doStep() {
vm.resume();
EventQueue eq = vm.eventQueue();
EventSet es = null;
try {
es = eq.remove();
} catch (InterruptedException e) {
Dbg.FATAL("Interrupted: " + e);
}
EventIterator ei = es.eventIterator();
while (ei.hasNext()) {
Event event = ei.nextEvent();
if (event instanceof StepEvent) {
StepEvent se = (StepEvent) event;
Location l = se.location();
StepRequest request = (StepRequest) se.request();
String src = "<unknown>";
try { src = l.sourceName(); } catch (AbsentInformationException e) {}
int n = l.lineNumber();
String name = l.method().name();
System.out.println("File: " + src + " Line: " + n + " Method: " +
name);
// requeue the request
//requestStep(request.thread());
} else {
System.out.println(event);
}
}
}
Dbg(String mainClass) {
this.mainClass = mainClass;
}
////////////////////////////////////////////////////////
public static void usage() {
System.out.println("Usage:");
System.out.println(" ");
System.out.println(" java Dbg <classname> <args>");
System.out.println(" ");
System.out.println(" ");
}
public static void main(String[] args) {
if (args.length < 1) {
usage();
}
String mainClass = args[0];
System.out.println("class = " + mainClass);
Dbg dbg = new Dbg(mainClass);
dbg.go();
}
}