blob: 5be5ebdc16e2896c52558576d61d8fca79c4e6b8 (
plain)
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
|
package moe.nea.firmament.util
import com.sun.tools.attach.VirtualMachine
import java.lang.management.ManagementFactory
import java.nio.file.Path
import kotlin.io.path.absolutePathString
object JvmUtil {
fun guessJVMPid(): String {
val name = ManagementFactory.getRuntimeMXBean().name
val pid = name.substringBefore('@')
ErrorUtil.softCheck("Not a valid PID: $pid", pid.toIntOrNull() != null)
return pid
}
fun getVM(): VirtualMachine {
return VirtualMachine.attach(guessJVMPid())
}
fun useVM(block: (VirtualMachine) -> Unit) {
val vm = getVM()
block(vm)
vm.detach()
}
fun loadAgent(jarPath: Path, options: String? = null) {
useVM {
it.loadAgent(jarPath.absolutePathString(), options)
}
}
}
|