Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-2732: Forcibly lock trace during auto-save

Dan1c27c34e669b
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/model/DebuggerConnectDialog.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/model/DebuggerConnectDialog.java
index ba0de5f927..ed3fb4b896 100644
--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/model/DebuggerConnectDialog.java
+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/model/DebuggerConnectDialog.java
@@ -241,7 +241,8 @@ public class DebuggerConnectDialog extends DialogComponentProvider
synchronized (this) {
futureConnect = factory.build();
}
- futureConnect.thenAcceptAsync(m -> {
+ futureConnect.thenCompose(m -> m.fetchModelRoot()).thenAcceptAsync(r -> {
+ DebuggerObjectModel m = r.getModel();
modelService.addModel(m);
setStatusText("");
close();
diff --git a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java
index 3a81e82f92..273db9727d 100644
--- a/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java
+++ b/Ghidra/Debug/Debugger/src/main/java/ghidra/app/plugin/core/debug/service/tracemgr/DebuggerTraceManagerServicePlugin.java
@@ -60,6 +60,7 @@ import ghidra.trace.model.thread.TraceThread;
import ghidra.trace.model.time.TraceSnapshot;
import ghidra.trace.model.time.schedule.TraceSchedule;
import ghidra.util.*;
+import ghidra.util.database.DomainObjectLockHold;
import ghidra.util.datastruct.CollectionChangeListener;
import ghidra.util.exception.*;
import ghidra.util.task.*;
@@ -86,9 +87,11 @@ import ghidra.util.task.*;
})
public class DebuggerTraceManagerServicePlugin extends Plugin
implements DebuggerTraceManagerService {
- private static final AutoConfigState.ClassHandler<DebuggerTraceManagerServicePlugin> CONFIG_STATE_HANDLER =
- AutoConfigState.wireHandler(DebuggerTraceManagerServicePlugin.class,
- MethodHandles.lookup());
+
+ private static final AutoConfigState.ClassHandler<DebuggerTraceManagerServicePlugin> //
+ CONFIG_STATE_HANDLER = AutoConfigState.wireHandler(DebuggerTraceManagerServicePlugin.class,
+ MethodHandles.lookup());
+
private static final String KEY_TRACE_COUNT = "NUM_TRACES";
private static final String PREFIX_OPEN_TRACE = "OPEN_TRACE_";
private static final String KEY_CURRENT_COORDS = "CURRENT_COORDS";
@@ -861,7 +864,14 @@ public class DebuggerTraceManagerServicePlugin extends Plugin
}
}
- public static CompletableFuture<Void> saveTrace(PluginTool tool, Trace trace) {
+ protected static DomainObjectLockHold maybeLock(Trace trace, boolean lock) {
+ if (!lock) {
+ return null;
+ }
+ return DomainObjectLockHold.forceLock(trace, false, "Auto Save");
+ }
+
+ public static CompletableFuture<Void> saveTrace(PluginTool tool, Trace trace, boolean force) {
tool.prepareToSave(trace);
CompletableFuture<Void> future = new CompletableFuture<>();
// TODO: Get all the nuances for this correct...
@@ -870,7 +880,7 @@ public class DebuggerTraceManagerServicePlugin extends Plugin
new TaskLauncher(new Task("Save Trace", true, true, true) {
@Override
public void run(TaskMonitor monitor) throws CancelledException {
- try {
+ try (DomainObjectLockHold hold = maybeLock(trace, force)) {
trace.getDomainFile().save(monitor);
future.complete(null);
}
@@ -912,10 +922,9 @@ public class DebuggerTraceManagerServicePlugin extends Plugin
final String finalFilename = filename;
new TaskLauncher(new Task("Save New Trace", true, true, true) {
-
@Override
public void run(TaskMonitor monitor) throws CancelledException {
- try {
+ try (DomainObjectLockHold hold = maybeLock(trace, force)) {
traces.createFile(finalFilename, trace, monitor);
trace.save("Initial save", monitor);
future.complete(null);
@@ -945,19 +954,22 @@ public class DebuggerTraceManagerServicePlugin extends Plugin
future.completeExceptionally(e);
}
}
-
});
}
return future;
}
- @Override
- public CompletableFuture<Void> saveTrace(Trace trace) {
+ public CompletableFuture<Void> saveTrace(Trace trace, boolean force) {
if (isDisposed()) {
Msg.error(this, "Cannot save trace after manager disposal! Data may have been lost.");
return AsyncUtils.NIL;
}
- return saveTrace(tool, trace);
+ return saveTrace(tool, trace, force);
+ }
+
+ @Override
+ public CompletableFuture<Void> saveTrace(Trace trace) {
+ return saveTrace(trace, false);
}
protected void doTraceClosed(Trace trace) {
diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DomainObjectLockHold.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DomainObjectLockHold.java
new file mode 100644
index 0000000000..9ff1ec70c1
--- /dev/null
+++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DomainObjectLockHold.java
@@ -0,0 +1,77 @@
+/* ###
+ * 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.
+ */
+package ghidra.util.database;
+
+import ghidra.framework.model.DomainObject;
+import ghidra.framework.model.DomainObjectLockedException;
+
+/**
+ * A hold on the lock for a domain object, obtained via {@link #lock(DomainObject, String)} or
+ * {@link #forceLock(DomainObject, boolean, String)}
+ *
+ * <p>
+ * This is designed for use in a {@code try-with-resources} block to ensure the timely release of
+ * the lock even in exceptional conditions, as in:
+ *
+ * <pre>
+ * try (DomainObjectLockHold hold = DomainObjectLockHold.lock("Demonstration")) {
+ * // Do stuff while holding the lock
+ * }
+ * </pre>
+ */
+public interface DomainObjectLockHold extends AutoCloseable {
+
+ /**
+ * Wrapper for {@link DomainObject#lock(String)}
+ *
+ * @param object the object
+ * @param reason as in {@link DomainObject#lock(String)}
+ * @return the hold, which should be used in a {@code try-with-resources} block
+ * @throws DomainObjectLockedException if the lock could not be obtained
+ */
+ static DomainObjectLockHold lock(DomainObject object, String reason) {
+ if (object.lock(reason)) {
+ return new DefaultHold(object);
+ }
+ throw new DomainObjectLockedException("Could not get lock");
+ }
+
+ /**
+ * Wrapper for {@link DomainObject#forceLock(boolean, String)}
+ *
+ * @param object the object
+ * @param rollback as in {@link DomainObject#forceLock(boolean, String)}
+ * @param reason as in {@link DomainObject#forceLock(boolean, String)}
+ * @return the hold, which should be used in a {@code try-with-resources} block
+ */
+ static DomainObjectLockHold forceLock(DomainObject object, boolean rollback, String reason) {
+ object.forceLock(rollback, reason);
+ return new DefaultHold(object);
+ }
+
+ class DefaultHold implements DomainObjectLockHold {
+ final DomainObject object;
+
+ public DefaultHold(DomainObject object) {
+ this.object = object;
+ }
+
+ @Override
+ public void close() throws Exception {
+ object.unlock();
+ }
+ }
+}