Ghidra is a software reverse engineering (SRE) framework
GP-1530: Registers viewer can now modify values for emulation
Dan583c6eca6081
diff --git a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerRegistersPlugin/DebuggerRegistersPlugin.html b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerRegistersPlugin/DebuggerRegistersPlugin.htmlindex 8b59011a9e..00c9beb5c4 100644--- a/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerRegistersPlugin/DebuggerRegistersPlugin.html+++ b/Ghidra/Debug/Debugger/src/main/help/help/topics/DebuggerRegistersPlugin/DebuggerRegistersPlugin.html@@ -45,9 +45,10 @@<LI>Value - the value of the register as recorded in the trace. When the value refers to avalid memory offset, right-clicking the row allows the user to navigate to that offset in a- selected memory space. This field is user modifiable when the target is alive, the trace is- "at the present," and the <B>Enable Edits</B> toggle is on. Changes to the register's value- are sent to the target. Values changed by the last event are displayed in <FONT color=+ selected memory space. This field is user modifiable when the <B>Enable Edits</B> toggle is+ on, and the register is not part of <CODE>contextreg</CODE>. Changes are sent to the target+ if the trace is live and "at the present." Otherwise, the change is materialized via+ emulation. Values changed by the last event are displayed in <FONT color="red">red</FONT>.</LI><LI>Type - the type of the register as marked up in the trace. There is generally no default@@ -91,10 +92,15 @@<H3><A name="enable_edits"></A>Enable Edits</H3>- <P>This toggle is a write protector for live registers. To modify live register values, this- toggle must be enabled, and the trace must be live and "at the present." Note that editing- recorded historical values is not permitted, regardless of this toggle, but can be accomplished- via watches or scripts.</P>+ <P>This toggle is a write protector for target machine state. To modify register values, this+ toggle must be enabled. Editing a value when the trace is live and "at the present" will cause+ the value to be modified on the target. Editing emulated values is permitted, but ity has no+ effect on the target. Editing historical values is not permitted. All edits to non-live trace+ values are performed in emulation. Specifically, it appends a patch command to the current+ emulation schedule. This keeps trace history in tact, and it allows patched emulated states to+ be annotated and recalled later, since they are stored in the trace's scratch space. Note that+ only the raw "Value" column can be edited directory. The "Repr" column cannot be edited,+ yet.</P><H3><A name="snapshot_window"></A>Snapshot Window</H3>diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProvider.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProvider.javaindex efe6646cd1..a5edc68609 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProvider.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProvider.java@@ -72,6 +72,7 @@ import ghidra.trace.model.memory.TraceMemoryRegisterSpace;import ghidra.trace.model.memory.TraceMemoryState;import ghidra.trace.model.program.TraceProgramView;import ghidra.trace.model.thread.TraceThread;+import ghidra.trace.model.time.schedule.TraceSchedule;import ghidra.trace.util.*;import ghidra.util.Msg;import ghidra.util.Swing;@@ -755,8 +756,21 @@ public class DebuggerRegistersProvider extends ComponentProviderAdapterreturn true;}+ boolean canWriteRegister(Register register) {+ if (!isEditsEnabled()) {+ return false;+ }+ if (register.isProcessorContext()) {+ return false; // TODO: Limitation from using Sleigh for patching+ }+ return true;+ }+boolean canWriteTargetRegister(Register register) {- if (!computeEditsEnabled()) {+ if (!isEditsEnabled()) {+ return false;+ }+ if (!canWriteTarget()) {return false;}return current.getRecorder().isRegisterOnTarget(current.getThread(), register);@@ -775,23 +789,32 @@ public class DebuggerRegistersProvider extends ComponentProviderAdapter}void writeRegisterValue(RegisterValue rv) {- rv = combineWithTraceBaseRegisterValue(rv);- CompletableFuture<Void> future = current.getRecorder()- .writeThreadRegisters(current.getThread(), current.getFrame(),- Map.of(rv.getRegister(), rv));- future.exceptionally(ex -> {- ex = AsyncUtils.unwrapThrowable(ex);- if (ex instanceof DebuggerModelAccessException) {- Msg.error(this, "Could not write target register", ex);- plugin.getTool()- .setStatusInfo("Could not write target register: " + ex.getMessage());- }- else {- Msg.showError(this, getComponent(), "Edit Register",- "Could not write target register", ex);- }- return null;- });+ if (canWriteTargetRegister(rv.getRegister())) {+ rv = combineWithTraceBaseRegisterValue(rv);+ CompletableFuture<Void> future = current.getRecorder()+ .writeThreadRegisters(current.getThread(), current.getFrame(),+ Map.of(rv.getRegister(), rv));+ future.exceptionally(ex -> {+ ex = AsyncUtils.unwrapThrowable(ex);+ if (ex instanceof DebuggerModelAccessException) {+ Msg.error(this, "Could not write target register", ex);+ plugin.getTool()+ .setStatusInfo("Could not write target register: " + ex.getMessage());+ }+ else {+ Msg.showError(this, getComponent(), "Edit Register",+ "Could not write target register", ex);+ }+ return null;+ });+ return;+ }+ TraceSchedule time = current.getTime().patched(current.getThread(), generateSleigh(rv));+ traceManager.activateTime(time);+ }++ protected String generateSleigh(RegisterValue rv) {+ return String.format("%s=0x%s", rv.getRegister(), rv.getUnsignedValue().toString(16));}private RegisterValue combineWithTraceBaseRegisterValue(RegisterValue rv) {@@ -886,11 +909,8 @@ public class DebuggerRegistersProvider extends ComponentProviderAdapterreturn !Objects.equals(curRegVal, prevRegVal);}- private boolean computeEditsEnabled() {- if (!actionEnableEdits.isSelected()) {- return false;- }- return canWriteTarget();+ private boolean isEditsEnabled() {+ return actionEnableEdits.isSelected();}/**diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/RegisterRow.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/RegisterRow.javaindex 9f42e86b0e..f68db9bc9c 100644--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/RegisterRow.java+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/gui/register/RegisterRow.java@@ -62,7 +62,7 @@ public class RegisterRow {}public boolean isValueEditable() {- return provider.canWriteTargetRegister(register);+ return provider.canWriteRegister(register);}public void setValue(BigInteger value) {diff --git a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProviderTest.java b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProviderTest.javaindex b38cd4c323..e891e9c03c 100644--- a/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProviderTest.java+++ b/Ghidra/Debug/Debugger/src/test/java/ghidra/app/plugin/core/debug/gui/register/DebuggerRegistersProviderTest.java@@ -59,6 +59,7 @@ public class DebuggerRegistersProviderTest extends AbstractGhidraHeadedDebuggerGprotected Register r0;protected Register pc;protected Register sp;+ protected Register contextreg;protected Register r0h;protected Register r0l;@@ -79,6 +80,7 @@ public class DebuggerRegistersProviderTest extends AbstractGhidraHeadedDebuggerGr0 = tb.language.getRegister("r0");pc = tb.language.getProgramCounter();sp = tb.language.getDefaultCompilerSpec().getStackPointer();+ contextreg = tb.language.getContextBaseRegister();pch = tb.language.getRegister("pch");pcl = tb.language.getRegister("pcl");@@ -381,6 +383,38 @@ public class DebuggerRegistersProviderTest extends AbstractGhidraHeadedDebuggerGassertR0RowTypePopulated();}+ // TODO: Test that contextreg cannot be modified+ // TODO: Make contextreg modifiable by Registers window++ @Test+ public void testDeadModifyValueEmulates() throws Exception {+ traceManager.openTrace(tb.trace);++ TraceThread thread = addThread();+ traceManager.activateThread(thread);+ waitForSwing();++ assertTrue(registersProvider.actionEnableEdits.isEnabled());+ performAction(registersProvider.actionEnableEdits);++ addRegisterValues(thread);+ waitForDomainObject(tb.trace);++ TraceMemoryRegisterSpace regVals =+ tb.trace.getMemoryManager().getMemoryRegisterSpace(thread, false);++ RegisterRow row = findRegisterRow(r0);++ setRowText(row, "1234");+ waitForSwing();+ waitForPass(() -> {+ long viewSnap = traceManager.getCurrent().getViewSnap();+ assertEquals(BigInteger.valueOf(0x1234),+ regVals.getValue(viewSnap, r0).getUnsignedValue());+ assertEquals(BigInteger.valueOf(0x1234), row.getValue());+ });+ }+@Testpublic void testLiveModifyValueAffectsTarget() throws Exception {TraceRecorder recorder = recordAndWaitSync();