forked from moosetechnology/JavaTraceExtractor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVmManager.java
More file actions
82 lines (69 loc) · 2.18 KB
/
Copy pathVmManager.java
File metadata and controls
82 lines (69 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package app;
import com.sun.jdi.ThreadReference;
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.event.BreakpointEvent;
import com.sun.jdi.event.Event;
import com.sun.jdi.event.EventSet;
import com.sun.jdi.event.VMDeathEvent;
import com.sun.jdi.event.VMDisconnectEvent;
import app.breakpoint.BreakpointWrapper;
public class VmManager {
VirtualMachine vm;
public VmManager(VirtualMachine vm) {
this.vm = vm;
}
public void resumeThread(String threadName) {
this.getThreadNamed(threadName).resume();
}
/**
* Returns the thread with the chosen name if one exist in the given VM
*
* @param threadName name of the searched thread
* @return the thread with the chosen name if one exist in the given VM
* @throws IllegalStateException if no thread can be found
*/
public ThreadReference getThreadNamed(String threadName) {
ThreadReference main = null;
for (ThreadReference thread : vm.allThreads()) {
if (thread.name().equals(threadName)) {
main = thread;
break;
}
}
if (main == null) {
throw new IllegalStateException("No thread named " + threadName + "was found");
}
return main;
}
/**
* Waiting for the vm to stop at a break point
*
* @param bkWrap the wrapper for the breakpoint searched
* @throws InterruptedException
*/
public void waitForBreakpoint(BreakpointWrapper bkWrap) throws InterruptedException {
EventSet eventSet;
boolean stop = false;
while ((!stop && (eventSet = vm.eventQueue().remove()) != null)) {
for (Event event : eventSet) {
if (event instanceof VMDeathEvent) {
throw new IllegalStateException("Thread has terminated without encountering the wanted breakpoint");
} else if (event instanceof VMDisconnectEvent) {
throw new IllegalStateException("VM has been disconnected");
} else if (event instanceof BreakpointEvent && (stop = bkWrap.shouldStopAt(event))) {
// BreakPoint attained we can stop here
break;
} else {
// Not noticeable now
// TODO by looking at the type hierarchy of Event it could be possible to take steps into accounts
}
}
}
}
/**
* Dispose of the VM to make sure the vm disconnect properly
*/
public void disposeVM() {
vm.dispose();
}
}