Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1773: Add 'Symbol' column to watches table.

Dan2789f3008b98
diff --git a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/DebuggerWatchesPlugin.html b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/DebuggerWatchesPlugin.html
index 2e8a244983..d3b4cb5546 100644
--- a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/DebuggerWatchesPlugin.html
+++ b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/DebuggerWatchesPlugin.html
@@ -66,6 +66,12 @@
Double-clicking this cell will navigate the primary dynamic listing to this address, if
possible.</LI>
+ <LI>Symbol - the symbol best representing the address, if applicable. When the watch has an
+ address in memory, this will search the trace for a symbol at that address, preferring the
+ primary. If no such symbol exists, it will search in the mapped static program, if there is
+ one, again preferring the primary symbol. Finally, it will search for a containing function
+ in the mapped program.</LI>
+
<LI>Value - the raw bytes of the watched buffer. If the expression is a register, then this
is its hexadecimal value. This field is user modifiable when the <B>Enable Edits</B> toggle
is on, and the <A href=
diff --git a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/images/DebuggerWatchesPlugin.png b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/images/DebuggerWatchesPlugin.png
index cc375abf7a..84da6b1669 100644
Binary files a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/images/DebuggerWatchesPlugin.png and b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerWatchesPlugin/images/DebuggerWatchesPlugin.png differ
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProvider.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProvider.java
index 76b887fe42..27186b9a49 100644
--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProvider.java
+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProvider.java
@@ -61,6 +61,7 @@ import ghidra.program.model.address.*;
import ghidra.program.model.data.DataType;
import ghidra.program.model.lang.Register;
import ghidra.program.model.listing.*;
+import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.util.CodeUnitInsertionException;
import ghidra.program.util.ProgramLocation;
import ghidra.program.util.ProgramSelection;
@@ -84,6 +85,7 @@ public class DebuggerWatchesProvider extends ComponentProviderAdapter {
protected enum WatchTableColumns implements EnumeratedTableColumn<WatchTableColumns, WatchRow> {
EXPRESSION("Expression", String.class, WatchRow::getExpression, WatchRow::setExpression),
ADDRESS("Address", Address.class, WatchRow::getAddress),
+ SYMBOL("Symbol", Symbol.class, WatchRow::getSymbol),
VALUE("Value", String.class, WatchRow::getRawValueString, WatchRow::setRawValueString, //
WatchRow::isRawValueEditable),
TYPE("Type", DataType.class, WatchRow::getDataType, WatchRow::setDataType),
@@ -255,7 +257,7 @@ public class DebuggerWatchesProvider extends ComponentProviderAdapter {
@AutoServiceConsumed
protected DebuggerStateEditingService editingService;
@AutoServiceConsumed
- private DebuggerStaticMappingService mappingService; // For listing action
+ DebuggerStaticMappingService mappingService;
@SuppressWarnings("unused")
private final AutoService.Wiring autoServiceWiring;
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/WatchRow.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/WatchRow.java
index 31c17531e5..bbe47dd173 100644
--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/WatchRow.java
+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/watch/WatchRow.java
@@ -16,11 +16,12 @@
package ghidra.app.plugin.core.debug.gui.watch;
import java.math.BigInteger;
-import java.util.Arrays;
-import java.util.Objects;
+import java.util.*;
import org.apache.commons.lang3.tuple.Pair;
+import com.google.common.collect.Range;
+
import ghidra.app.plugin.core.debug.DebuggerCoordinates;
import ghidra.app.plugin.processors.sleigh.SleighLanguage;
import ghidra.app.services.DataTypeManagerService;
@@ -35,11 +36,16 @@ import ghidra.program.model.address.*;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeEncodeException;
import ghidra.program.model.lang.Language;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.listing.Program;
import ghidra.program.model.mem.ByteMemBufferImpl;
import ghidra.program.model.mem.MemBuffer;
-import ghidra.trace.model.Trace;
+import ghidra.program.model.symbol.*;
+import ghidra.program.util.ProgramLocation;
+import ghidra.trace.model.*;
import ghidra.trace.model.memory.TraceMemorySpace;
import ghidra.trace.model.memory.TraceMemoryState;
+import ghidra.trace.model.symbol.TraceLabelSymbol;
import ghidra.trace.model.thread.TraceThread;
import ghidra.util.*;
@@ -61,6 +67,7 @@ public class WatchRow {
private PcodeExpression compiled;
private TraceMemoryState state;
private Address address;
+ private Symbol symbol;
private AddressSet reads;
private byte[] value;
private byte[] prevValue; // Value at previous coordinates
@@ -76,6 +83,7 @@ public class WatchRow {
protected void blank() {
state = null;
address = null;
+ symbol = null;
reads = null;
value = null;
valueString = null;
@@ -126,6 +134,7 @@ public class WatchRow {
error = null;
state = valueWithState.getRight();
address = valueWithAddress.getRight();
+ symbol = computeSymbol();
reads = executorWithAddress.getReads();
valueObj = parseAsDataTypeObj();
@@ -477,6 +486,47 @@ public class WatchRow {
return value == null ? 0 : value.length;
}
+ protected Symbol computeSymbol() {
+ if (address == null || !address.isMemoryAddress()) {
+ return null;
+ }
+ Collection<? extends TraceLabelSymbol> labels =
+ trace.getSymbolManager().labels().getAt(coordinates.getSnap(), null, address, false);
+ if (!labels.isEmpty()) {
+ return labels.iterator().next();
+ }
+ // TODO: Check trace functions? They don't work yet.
+ if (provider.mappingService == null) {
+ return null;
+ }
+ TraceLocation dloc =
+ new DefaultTraceLocation(trace, null, Range.singleton(coordinates.getSnap()), address);
+ ProgramLocation sloc = provider.mappingService.getOpenMappedLocation(dloc);
+ if (sloc == null) {
+ return null;
+ }
+
+ Program program = sloc.getProgram();
+ SymbolTable table = program.getSymbolTable();
+ Symbol primary = table.getPrimarySymbol(address);
+ if (primary != null) {
+ return primary;
+ }
+ SymbolIterator sit = table.getSymbolsAsIterator(sloc.getByteAddress());
+ if (sit.hasNext()) {
+ return sit.next();
+ }
+ Function function = program.getFunctionManager().getFunctionContaining(address);
+ if (function != null) {
+ return function.getSymbol();
+ }
+ return null;
+ }
+
+ public Symbol getSymbol() {
+ return symbol;
+ }
+
public String getErrorMessage() {
if (error == null) {
return "";
diff --git a/Ghidra/Debug/Debugger/src/screen/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesPluginScreenShots.java b/Ghidra/Debug/Debugger/src/screen/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesPluginScreenShots.java
index edca2e4539..3b2c331a66 100644
--- a/Ghidra/Debug/Debugger/src/screen/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesPluginScreenShots.java
+++ b/Ghidra/Debug/Debugger/src/screen/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesPluginScreenShots.java
@@ -23,6 +23,7 @@ import ghidra.pcode.exec.PcodeExecutor;
import ghidra.pcode.exec.trace.TraceSleighUtils;
import ghidra.program.model.data.FloatDataType;
import ghidra.program.model.data.LongDataType;
+import ghidra.program.model.symbol.SourceType;
import ghidra.test.ToyProgramBuilder;
import ghidra.trace.database.ToyDBTraceBuilder;
import ghidra.trace.model.thread.TraceThread;
@@ -59,6 +60,11 @@ public class DebuggerWatchesPluginScreenShots extends GhidraScreenShotGenerator
snap0 = tb.trace.getTimeManager().createSnapshot("First").getKey();
snap1 = tb.trace.getTimeManager().createSnapshot("Second").getKey();
+ tb.trace.getSymbolManager()
+ .labels()
+ .create(snap1, null, tb.addr(0x7fff0004), "fiveUp",
+ tb.trace.getSymbolManager().getGlobalNamespace(), SourceType.USER_DEFINED);
+
thread = tb.getOrAddThread("[1]", snap0);
PcodeExecutor<byte[]> executor0 =
@@ -81,11 +87,12 @@ public class DebuggerWatchesPluginScreenShots extends GhidraScreenShotGenerator
traceManager.openTrace(tb.trace);
traceManager.activateThread(thread);
waitForSwing();
+ // So that it shows changes in red, activate snaps in sequence
traceManager.activateSnap(snap0);
waitForSwing();
traceManager.activateSnap(snap1);
waitForSwing();
- captureIsolatedProvider(watchesProvider, 700, 400);
+ captureIsolatedProvider(watchesProvider, 800, 400);
}
}
diff --git a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProviderTest.java b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProviderTest.java
index cbed32fb1e..afaa2067ed 100644
--- a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProviderTest.java
+++ b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/watch/DebuggerWatchesProviderTest.java
@@ -43,6 +43,8 @@ import ghidra.program.model.data.*;
import ghidra.program.model.lang.Register;
import ghidra.program.model.lang.RegisterValue;
import ghidra.program.model.mem.Memory;
+import ghidra.program.model.symbol.SourceType;
+import ghidra.program.model.symbol.Symbol;
import ghidra.program.util.ProgramLocation;
import ghidra.trace.database.DBTraceUtils;
import ghidra.trace.model.DefaultTraceLocation;
@@ -443,7 +445,7 @@ public class DebuggerWatchesProviderTest extends AbstractGhidraHeadedDebuggerGUI
});
}
- protected WatchRow prepareTestEditTarget(String expression) throws Exception {
+ protected WatchRow prepareTestEditTarget(String expression) throws Throwable {
createTestModel();
mb.createTestProcessesAndThreads();
bank = mb.testThread1.addRegisterBank();
@@ -456,6 +458,7 @@ public class DebuggerWatchesProviderTest extends AbstractGhidraHeadedDebuggerGUI
recorder = modelService.recordTarget(mb.testProcess1,
createTargetTraceMapper(mb.testProcess1), ActionSource.AUTOMATIC);
Trace trace = recorder.getTrace();
+ waitRecorder(recorder);
TraceThread thread = waitForValue(() -> recorder.getTraceThread(mb.testThread1));
traceManager.openTrace(trace);
@@ -649,4 +652,29 @@ public class DebuggerWatchesProviderTest extends AbstractGhidraHeadedDebuggerGUI
assertEquals("r0", watch.getExpression());
assertTypeEquals(PointerDataType.dataType, watch.getDataType());
}
+
+ @Test
+ public void testSymbolColumnWithMappedProgram() throws Throwable {
+ setupMappedDataSection();
+
+ Symbol symbol;
+ try (UndoableTransaction tid = UndoableTransaction.start(program, "Add symbol", true)) {
+ symbol = program.getSymbolTable()
+ .createLabel(tb.addr(0x00601234), "my_symbol", SourceType.USER_DEFINED);
+ }
+ try (UndoableTransaction tid = tb.startTransaction()) {
+ TraceMemoryRegisterSpace regVals =
+ tb.trace.getMemoryManager().getMemoryRegisterSpace(thread, true);
+ regVals.setValue(0, new RegisterValue(r0, BigInteger.valueOf(0x55751234)));
+ }
+
+ performAction(watchesProvider.actionAdd);
+ WatchRow row = Unique.assertOne(watchesProvider.watchTableModel.getModelData());
+ row.setExpression("*:8 r0");
+
+ traceManager.activateThread(thread);
+ waitForSwing();
+
+ assertEquals(symbol, row.getSymbol());
+ }
}