可以使用getTool().getService(DebuggerService.class)
方法获取到DebuggerService实例,然后通过该实例获取到当前所选中的调试器。修改代码如下:
import ghidra.app.script.GhidraScript;
import ghidra.program.model.listing.Program;
import ghidra.util.task.TaskMonitor;
import com.google.gson.Gson;
import java.io.FileWriter;
import java.io.IOException;
public class ExtractDebugInfoScript extends GhidraScript {
public void run() throws Exception {
// 获取当前打开的程序
Program program = getCurrentProgram();
// 创建一个用于保存调试信息的对象(自定义结构)
DebugInfo debugInfo = new DebugInfo();
// 提取调试信息
// 这里只是一个示例,你需要根据具体需求修改这部分逻辑
debugInfo.setFileName(program.getName());
debugInfo.setEntryPoint(program.getExecutablePath());
DebuggerService debuggerService = getTool().getService(DebuggerService.class);
if (debuggerService != null) {
String debuggerName = debuggerService.getActiveDebugger().getClass().getSimpleName();
debugInfo.setDebugger(debuggerName);
} else {
debugInfo.setDebugger("No active debugger");
}
// 将调试信息转换为JSON字符串
Gson gson = new Gson();
String json = gson.toJson(debugInfo);
// 将JSON字符串保存为文件
saveToJsonFile(json, "debug_info.json");
}
private void saveToJsonFile(String json, String fileName) throws IOException {
try (FileWriter writer = new FileWriter(fileName)) {
writer.write(json);
writer.flush();
println("调试信息已保存为JSON文件:" + fileName);
} catch (IOException e) {
println("保存JSON文件时出错:" + e.getMessage());
}
}
// 自定义调试信息结构
private static class DebugInfo {
private String fileName;
private String entryPoint;
private String debugger;
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setEntryPoint(String entryPoint) {
this.entryPoint = entryPoint;
}
public void setDebugger(String debugger) {
this.debugger = debugger;
}
}
}
这样,就可以获取到当前活动的调试器的类名了。
内容由零声教学AI助手提供,问题来源于学员提问