Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5637: More improvements to the PyGhidra API

Ryan Kurtz657ec392010f
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/AutoAnalysisManagerListener.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/AutoAnalysisManagerListener.java
index 66f78dd86c..957bacf6c2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/AutoAnalysisManagerListener.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/analysis/AutoAnalysisManagerListener.java
@@ -4,9 +4,9 @@
* 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.
@@ -15,6 +15,7 @@
*/
package ghidra.app.plugin.core.analysis;
+@FunctionalInterface
public interface AutoAnalysisManagerListener {
public void analysisEnded(AutoAnalysisManager manager, boolean isCancelled);
diff --git a/Ghidra/Features/PyGhidra/src/main/py/README.md b/Ghidra/Features/PyGhidra/src/main/py/README.md
index b3bd5f82c2..9c61a8deca 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/README.md
+++ b/Ghidra/Features/PyGhidra/src/main/py/README.md
@@ -141,11 +141,15 @@ def program_context(
### pyghidra.analyze()
```python
-def analyze(program: "Program"):
+def analyze(
+ program: "Program",
+ monitor: Optional["TaskMonitor"] = None
+ ) -> str:
"""
Analyzes the given program.
:param program: The Ghidra program to analyze.
+ :return: The analysis log.
"""
```
diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/api.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/api.py
index 0bb887e6fd..2f0a4dc033 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/api.py
+++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/api.py
@@ -162,11 +162,12 @@ def program_context(
def analyze(
program: "Program",
monitor: Optional["TaskMonitor"] = None
- ):
+ ) -> str:
"""
Analyzes the given program.
:param program: The Ghidra program to analyze.
+ :return: The analysis log.
"""
from ghidra.app.script import GhidraScriptUtil
from ghidra.program.util import GhidraProgramUtilities
@@ -181,12 +182,16 @@ def analyze(
mgr: AutoAnalysisManager = AutoAnalysisManager.getAnalysisManager(program);
mgr.initializeOptions();
mgr.reAnalyzeAll(None);
- analysisTool = mgr.getAnalysisTool();
- if analysisTool is None or analysisTool.threadIsBackgroundTaskThread():
- mgr.startAnalysis(monitor, True); # yields to analysis
- else:
- mgr.waitForAnalysis(None, monitor); # waits for all analysis to complete
+ mgr_log = ""
+ def get_log(manager, _is_cancelled):
+ nonlocal mgr_log
+ mgr_log += manager.getMessageLog().toString()
+ mgr.addListener(get_log)
+ mgr.startAnalysis(monitor, True); # yields to analysis
+ if not monitor.isCancelled():
+ monitor.cancel()
GhidraProgramUtilities.markProgramAnalyzed(program)
+ return mgr_log
finally:
GhidraScriptUtil.releaseBundleHostReference()