Skip to content

Ghidra is a software reverse engineering (SRE) framework

1.9 KB
Raw Blame
/* ###
 * IP: GHIDRA
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//Decompile the function at the cursor and its callees, then output facts files corresponding to the pcodes
//@category PCode

import java.io.File;

import ghidra.app.plugin.core.decompiler.export.ExportDecompilationTask;
import ghidra.app.script.GhidraScript;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionManager;
import ghidra.util.task.TaskLauncher;

public class ExportFunctionPcodeViaDecoderScript extends GhidraScript {

	File outputDirectory;

	@Override
	protected void run() throws Exception {

		String[] args = getScriptArgs();
		PluginTool tool = state.getTool();
		if (tool == null) {
			println("Script is not running in GUI");
		}
		if (args.length >= 1) {
			outputDirectory = new File(args[0]);
		}
		else {
			outputDirectory = askDirectory("Select Directory for Results", "OK");
		}

		long startTime = System.nanoTime();

		FunctionManager functionManager = currentProgram.getFunctionManager();
		Function currentFunction = functionManager.getFunctionContaining(currentAddress);
		ExportDecompilationTask task =
			new ExportDecompilationTask("Export Pcode", currentProgram, currentFunction,
				outputDirectory);
		TaskLauncher.launch(task);

		long endTime = System.nanoTime();
		long duration = (endTime - startTime) / 1000000;
		println("total duration: " + Long.toString(duration));
	}

}