Doz*_*arp 4 java gdb jvm inject
目前我有一个简单的Java程序:
public class Test {
public static void main(String[] args) {
boolean test = true;
while (test) {
System.out.println("Hello World");
try { Thread.sleep(1000); } catch (Exception e) {}
}
System.out.println("Bye-bye");
}
}
Run Code Online (Sandbox Code Playgroud)
它每秒打印“Hello World”。我想使用 gdb 附加到进程并制作一个内存补丁来停止它并打印“Bye-bye”。
我知道 GDB 可以从它的控制台获取创建的 VM (JNI_GetCreatedVMs),env 对象也可以通过 GetEnv 的 API 获得。如何test在JVM中找到变量地址并将其设置为false(这是可选的)以使程序正常退出?不确定像 AttachCurrentThread 这样的 API、像 HotSpotVirtualMachine 这样的类、像 jmap 或 jstack 这样的工具是否可以提供帮助?
并且没有调试选项,假设在生产中运行的简单程序使用java -cp . Test.
在此先感谢您的任何指导。:)
jmap -dump:file=hex <pid> && jhat hex并浏览http://localhost:7000;找不到对test(它不是一个对象,只是一个实例class Z)的任何引用jstack <pid>可以得到主线程的tid(0x7fa412002000),并且jhat hex拥有main的java.lang.Thread对象(0x76ab05c40)java.lang.Thread有一个本地方法start0调用JVM_StartThread(hotspot/src/share/vm/prims/jvm.cpp)的热点方法,有一个类JavaThread可能包含线程堆栈中局部变量的内存结构。private static boolean test = true;; 那么JNI_GetCreatedJavaVMs ==> jvm, jvm->jvm_api->AttachCurrentThread ==> env,env->env_api->(FindClass, GetStaticFieldID, SetStaticBooleanField) ==> test[true ==> false]在某些情况下,可以使用 HotSpot Serviceability Agent 获取局部变量地址。我制作了一个示例代理,它打印带有局部变量信息的扩展堆栈跟踪:
import sun.jvm.hotspot.code.Location;
import sun.jvm.hotspot.code.LocationValue;
import sun.jvm.hotspot.code.NMethod;
import sun.jvm.hotspot.code.ScopeValue;
import sun.jvm.hotspot.code.VMRegImpl;
import sun.jvm.hotspot.debugger.Address;
import sun.jvm.hotspot.debugger.OopHandle;
import sun.jvm.hotspot.interpreter.OopMapCacheEntry;
import sun.jvm.hotspot.oops.Method;
import sun.jvm.hotspot.oops.Oop;
import sun.jvm.hotspot.runtime.CompiledVFrame;
import sun.jvm.hotspot.runtime.InterpretedVFrame;
import sun.jvm.hotspot.runtime.JavaThread;
import sun.jvm.hotspot.runtime.JavaVFrame;
import sun.jvm.hotspot.runtime.VM;
import sun.jvm.hotspot.runtime.VMReg;
import sun.jvm.hotspot.tools.Tool;
import java.util.List;
public class Frames extends Tool {
@Override
public void run() {
for (JavaThread thread = VM.getVM().getThreads().first(); thread != null; thread = thread.next()) {
System.out.println(thread.getThreadName() + ", id = " + thread.getOSThread().threadId());
for (JavaVFrame vf = thread.getLastJavaVFrameDbg(); vf != null; vf = vf.javaSender()) {
dumpFrame(vf);
}
System.out.println();
}
}
private void dumpFrame(JavaVFrame vf) {
Method method = vf.getMethod();
String className = method.getMethodHolder().getName().asString().replace('/', '.');
String methodName = method.getName().asString() + method.getSignature().asString();
System.out.println(" # " + className + '.' + methodName + " @ " + vf.getBCI());
if (vf.isCompiledFrame()) {
dumpCompiledFrame(((CompiledVFrame) vf));
} else {
dumpInterpretedFrame(((InterpretedVFrame) vf));
}
}
private void dumpCompiledFrame(CompiledVFrame vf) {
if (vf.getScope() == null) {
return;
}
NMethod nm = vf.getCode();
System.out.println(" * code=[" + nm.codeBegin() + "-" + nm.codeEnd() + "], pc=" + vf.getFrame().getPC());
List locals = vf.getScope().getLocals();
for (int i = 0; i < locals.size(); i++) {
ScopeValue sv = (ScopeValue) locals.get(i);
if (!sv.isLocation()) continue;
Location loc = ((LocationValue) sv).getLocation();
Address addr = null;
String regName = "";
if (loc.isRegister()) {
int reg = loc.getRegisterNumber();
addr = vf.getRegisterMap().getLocation(new VMReg(reg));
regName = ":" + VMRegImpl.getRegisterName(reg);
} else if (loc.isStack() && !loc.isIllegal()) {
addr = vf.getFrame().getUnextendedSP().addOffsetTo(loc.getStackOffset());
}
String value = getValue(addr, loc.getType());
System.out.println(" [" + i + "] " + addr + regName + " = " + value);
}
}
private void dumpInterpretedFrame(InterpretedVFrame vf) {
Method method = vf.getMethod();
int locals = (int) (method.isNative() ? method.getSizeOfParameters() : method.getMaxLocals());
OopMapCacheEntry oopMask = method.getMaskFor(vf.getBCI());
for (int i = 0; i < locals; i++) {
Address addr = vf.getFrame().addressOfInterpreterFrameLocal(i);
String value = getValue(addr, oopMask.isOop(i) ? Location.Type.OOP : Location.Type.NORMAL);
System.out.println(" [" + i + "] " + addr + " = " + value);
}
}
private String getValue(Address addr, Location.Type type) {
if (type == Location.Type.INVALID || addr == null) {
return "(invalid)";
} else if (type == Location.Type.OOP) {
return "(oop) " + getOopName(addr.getOopHandleAt(0));
} else if (type == Location.Type.NARROWOOP) {
return "(narrow_oop) " + getOopName(addr.getCompOopHandleAt(0));
} else if (type == Location.Type.NORMAL) {
return "(int) 0x" + Integer.toHexString(addr.getJIntAt(0));
} else {
return "(" + type + ") 0x" + Long.toHexString(addr.getJLongAt(0));
}
}
private String getOopName(OopHandle hadle) {
if (hadle == null) {
return "null";
}
Oop oop = VM.getVM().getObjectHeap().newOop(hadle);
return oop.getKlass().getName().asString();
}
public static void main(String[] args) throws Exception {
new Frames().execute(args);
}
}
Run Code Online (Sandbox Code Playgroud)
运行它:
java -cp $JAVA_HOME/lib/sa-jdi.jar:. Frames PID
Run Code Online (Sandbox Code Playgroud)
这将附加到 Java 进程PID并打印堆栈跟踪,如
main, id = 30920
# java.lang.Thread.sleep(J)V @ 0
# Test.main([Ljava/lang/String;)V @ 15
[0] 0x00007f075a857918 = (oop) [Ljava/lang/String;
[1] 0x00007f075a857910 = (int) 0x1
[2] 0x00007f075a857908 = (int) 0x0
Run Code Online (Sandbox Code Playgroud)
这main是Java线程名称;30920是本机线程 ID;@ 15是字节码索引。
该行[1] 0x00007f075a857910 = (int) 0x1表示局部变量 #1 位于地址 0x00007f075a857910 且值为 1。这正是您感兴趣的变量。
局部变量信息对于解释方法是可靠的,但对于编译方法并不总是可靠的。但是,已编译的方法将有一个额外的代码行,其中包含代码地址,因此您可以在 gdb 中反汇编和检查它。