GP-3405: Emit events and sync for dynamic loc/sel/hl.

This commit is contained in:
Dan 2025-04-11 16:43:49 +00:00
parent b5e404f499
commit da89fe5fe9
19 changed files with 549 additions and 322 deletions

View file

@ -0,0 +1,38 @@
/* ###
* 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.app.plugin.core.debug.event;
import java.util.Objects;
import ghidra.app.events.AbstractHighlightPluginEvent;
import ghidra.program.util.ProgramSelection;
import ghidra.trace.model.program.TraceProgramView;
public class TraceHighlightPluginEvent extends AbstractHighlightPluginEvent {
public static final String NAME = "TraceHighlight";
private final TraceProgramView view;
public TraceHighlightPluginEvent(String src, ProgramSelection highlight,
TraceProgramView view) {
super(src, NAME, highlight, view);
this.view = Objects.requireNonNull(view);
}
public TraceProgramView getTraceProgramView() {
return view;
}
}

View file

@ -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,34 +15,21 @@
*/
package ghidra.app.plugin.core.debug.event;
import java.util.Objects;
import ghidra.framework.plugintool.PluginEvent;
import ghidra.app.events.AbstractLocationPluginEvent;
import ghidra.program.util.ProgramLocation;
import ghidra.trace.model.program.TraceProgramView;
public class TraceLocationPluginEvent extends PluginEvent {
public class TraceLocationPluginEvent extends AbstractLocationPluginEvent {
public static final String NAME = "TraceLocation";
private final ProgramLocation loc;
private final TraceProgramView view;
public TraceLocationPluginEvent(String src, ProgramLocation loc) {
super(src, NAME);
this.loc = Objects.requireNonNull(loc);
super(src, NAME, loc, loc.getProgram());
this.view = (TraceProgramView) loc.getProgram();
}
public ProgramLocation getLocation() {
return loc;
}
public TraceProgramView getTraceProgramView() {
return view;
}
@Override
protected String getDetails() {
return loc.getClass() + " addr==> " + loc.getAddress();
}
}

View file

@ -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.
@ -17,33 +17,22 @@ package ghidra.app.plugin.core.debug.event;
import java.util.Objects;
import ghidra.framework.plugintool.PluginEvent;
import ghidra.app.events.AbstractSelectionPluginEvent;
import ghidra.program.util.ProgramSelection;
import ghidra.trace.model.program.TraceProgramView;
public class TraceSelectionPluginEvent extends PluginEvent {
public class TraceSelectionPluginEvent extends AbstractSelectionPluginEvent {
public static final String NAME = "TraceSelection";
private ProgramSelection selection;
private TraceProgramView view;
private final TraceProgramView view;
public TraceSelectionPluginEvent(String src, ProgramSelection sel, TraceProgramView view) {
super(src, NAME);
this.selection = Objects.requireNonNull(sel);
public TraceSelectionPluginEvent(String src, ProgramSelection selection,
TraceProgramView view) {
super(src, NAME, selection, view);
this.view = Objects.requireNonNull(view);
}
public ProgramSelection getSelection() {
return selection;
}
public TraceProgramView getTraceProgramView() {
return view;
}
@Override
protected String getDetails() {
return getClass() + " ==> " + selection;
}
}

View file

@ -0,0 +1,34 @@
/* ###
* 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.app.plugin.core.debug.event;
import ghidra.debug.api.action.LocationTrackingSpec;
import ghidra.framework.plugintool.PluginEvent;
public class TrackingChangedPluginEvent extends PluginEvent {
public static final String NAME = "TrackingChanged";
private final LocationTrackingSpec spec;
public TrackingChangedPluginEvent(String sourceName, LocationTrackingSpec spec) {
super(sourceName, NAME);
this.spec = spec;
}
public LocationTrackingSpec getLocationTrackingSpec() {
return spec;
}
}

View file

@ -62,7 +62,6 @@ import utilities.util.SuppressableCallback.Suppression;
packageName = DebuggerPluginPackage.NAME,
status = PluginStatus.RELEASED,
eventsConsumed = {
// ProgramHighlightPluginEvent.class, // TODO: Later or remove
ProgramOpenedPluginEvent.class, // For auto-open log cleanup
ProgramClosedPluginEvent.class, // For marker set cleanup
ProgramActivatedPluginEvent.class, // To track the static program for sync
@ -70,19 +69,25 @@ import utilities.util.SuppressableCallback.Suppression;
ProgramSelectionPluginEvent.class, // For static listing sync
TraceActivatedPluginEvent.class, // Trace/thread activation and register tracking
TraceClosedPluginEvent.class,
TraceLocationPluginEvent.class,
TraceSelectionPluginEvent.class,
TraceHighlightPluginEvent.class,
TrackingChangedPluginEvent.class,
},
eventsProduced = {
ProgramLocationPluginEvent.class,
ProgramSelectionPluginEvent.class,
TraceLocationPluginEvent.class,
TraceSelectionPluginEvent.class
TraceSelectionPluginEvent.class,
TraceHighlightPluginEvent.class,
TrackingChangedPluginEvent.class,
},
servicesRequired = {
DebuggerStaticMappingService.class, // For static listing sync. TODO: Optional?
DebuggerEmulationService.class, // TODO: Optional?
ProgramManager.class, // For static listing sync
ClipboardService.class,
MarkerService.class // TODO: Make optional?
MarkerService.class, // TODO: Make optional?
},
servicesProvided = {
DebuggerListingService.class,
@ -208,11 +213,16 @@ public class DebuggerListingPlugin extends AbstractCodeBrowserPlugin<DebuggerLis
public void locationChanged(CodeViewerProvider provider, ProgramLocation location) {
// TODO: Fix cursor?
// Do not fire ProgramLocationPluginEvent.
firePluginEvent(new TraceLocationPluginEvent(getName(), location));
if (provider == connectedProvider) {
firePluginEvent(new TraceLocationPluginEvent(getName(), location));
}
}
@Override
public void selectionChanged(CodeViewerProvider provider, ProgramSelection selection) {
if (provider != connectedProvider) {
return;
}
TraceProgramView view = current.getView();
if (view == null) {
return;
@ -222,9 +232,16 @@ public class DebuggerListingPlugin extends AbstractCodeBrowserPlugin<DebuggerLis
}
@Override
public void highlightChanged(CodeViewerProvider codeViewerProvider,
ProgramSelection highlight) {
// TODO Nothing, yet
public void highlightChanged(CodeViewerProvider provider, ProgramSelection highlight) {
if (provider != connectedProvider) {
return;
}
TraceProgramView view = current.getView();
if (view == null) {
return;
}
// Do not fire ProgramHighlightPluginEvent
firePluginEvent(new TraceHighlightPluginEvent(getName(), highlight, view));
}
protected boolean heedLocationEvent(PluginEvent ev) {
@ -259,39 +276,50 @@ public class DebuggerListingPlugin extends AbstractCodeBrowserPlugin<DebuggerLis
@Override
public void processEvent(PluginEvent event) {
if (event instanceof ProgramLocationPluginEvent ev) {
cbProgramLocationEvents.invoke(() -> {
switch (event) {
case ProgramLocationPluginEvent ev -> cbProgramLocationEvents.invoke(() -> {
if (heedLocationEvent(ev)) {
connectedProvider.staticProgramLocationChanged(ev.getLocation());
}
});
}
if (event instanceof ProgramSelectionPluginEvent ev) {
cbProgramSelectionEvents.invoke(() -> {
case ProgramSelectionPluginEvent ev -> cbProgramSelectionEvents.invoke(() -> {
if (heedSelectionEvent(ev)) {
connectedProvider.staticProgramSelectionChanged(ev.getProgram(),
ev.getSelection());
}
});
}
if (event instanceof ProgramOpenedPluginEvent ev) {
allProviders(p -> p.programOpened(ev.getProgram()));
}
if (event instanceof ProgramClosedPluginEvent ev) {
allProviders(p -> p.programClosed(ev.getProgram()));
}
if (event instanceof ProgramActivatedPluginEvent ev) {
allProviders(p -> p.staticProgramActivated(ev.getActiveProgram()));
}
if (event instanceof TraceActivatedPluginEvent ev) {
current = ev.getActiveCoordinates();
allProviders(p -> p.coordinatesActivated(current));
}
if (event instanceof TraceClosedPluginEvent ev) {
if (current.getTrace() == ev.getTrace()) {
current = DebuggerCoordinates.NOWHERE;
case ProgramOpenedPluginEvent ev -> allProviders(p -> p.programOpened(ev.getProgram()));
case ProgramClosedPluginEvent ev -> allProviders(p -> p.programClosed(ev.getProgram()));
case ProgramActivatedPluginEvent ev -> allProviders(
p -> p.staticProgramActivated(ev.getActiveProgram()));
case TraceActivatedPluginEvent ev -> {
current = ev.getActiveCoordinates();
allProviders(p -> p.coordinatesActivated(current));
}
case TraceClosedPluginEvent ev -> {
if (current.getTrace() == ev.getTrace()) {
current = DebuggerCoordinates.NOWHERE;
}
allProviders(p -> p.traceClosed(ev.getTrace()));
}
case TraceLocationPluginEvent ev -> {
// For those comparing to CodeBrowserPlugin, there is no "viewManager" here.
connectedProvider.goTo(ev.getTraceProgramView(), ev.getLocation());
}
case TraceSelectionPluginEvent ev -> {
if (ev.getTraceProgramView() == current.getView()) {
connectedProvider.setSelection(ev.getSelection());
}
}
case TraceHighlightPluginEvent ev -> {
if (ev.getTraceProgramView() == current.getView()) {
connectedProvider.setHighlight(ev.getHighlight());
}
}
case TrackingChangedPluginEvent ev -> connectedProvider
.setTrackingSpec(ev.getLocationTrackingSpec());
default -> {
}
allProviders(p -> p.traceClosed(ev.getTrace()));
}
}

View file

@ -52,6 +52,7 @@ import ghidra.app.plugin.core.codebrowser.MarkerServiceBackgroundColorModel;
import ghidra.app.plugin.core.debug.disassemble.CurrentPlatformTraceDisassembleCommand;
import ghidra.app.plugin.core.debug.disassemble.CurrentPlatformTraceDisassembleCommand.Reqs;
import ghidra.app.plugin.core.debug.disassemble.DebuggerDisassemblerPlugin;
import ghidra.app.plugin.core.debug.event.TrackingChangedPluginEvent;
import ghidra.app.plugin.core.debug.gui.*;
import ghidra.app.plugin.core.debug.gui.DebuggerResources.FollowsCurrentThreadAction;
import ghidra.app.plugin.core.debug.gui.DebuggerResources.OpenProgramAction;
@ -240,6 +241,9 @@ public class DebuggerListingProvider extends CodeViewerProvider {
@Override
protected void specChanged(LocationTrackingSpec spec) {
if (isMainListing()) {
plugin.firePluginEvent(new TrackingChangedPluginEvent(getName(), spec));
}
updateTitle();
trackingLabel.setText("");
trackingLabel.setToolTipText("");

View file

@ -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.
@ -22,10 +22,9 @@ import java.util.stream.Collectors;
import org.jdom.Element;
import docking.action.DockingAction;
import ghidra.app.events.ProgramLocationPluginEvent;
import ghidra.app.events.ProgramSelectionPluginEvent;
import ghidra.app.plugin.PluginCategoryNames;
import ghidra.app.plugin.core.byteviewer.*;
import ghidra.app.plugin.core.byteviewer.AbstractByteViewerPlugin;
import ghidra.app.plugin.core.byteviewer.ByteViewerComponentProvider;
import ghidra.app.plugin.core.debug.DebuggerPluginPackage;
import ghidra.app.plugin.core.debug.event.*;
import ghidra.app.plugin.core.debug.gui.DebuggerResources.NewMemoryAction;
@ -37,8 +36,8 @@ import ghidra.framework.options.SaveState;
import ghidra.framework.plugintool.*;
import ghidra.framework.plugintool.annotation.AutoServiceConsumed;
import ghidra.framework.plugintool.util.PluginStatus;
import ghidra.program.model.listing.Program;
import ghidra.program.util.ProgramSelection;
import ghidra.trace.model.program.TraceProgramView;
@PluginInfo(
shortDescription = "View bytes of trace (possibly live) memory",
@ -53,10 +52,16 @@ import ghidra.program.util.ProgramSelection;
// ProgramHighlightPluginEvent.class, // TODO: Later or remove
TraceActivatedPluginEvent.class, // Trace/thread activation and register tracking
TraceClosedPluginEvent.class,
TraceLocationPluginEvent.class,
TraceSelectionPluginEvent.class,
TraceHighlightPluginEvent.class,
TrackingChangedPluginEvent.class,
},
eventsProduced = {
TraceLocationPluginEvent.class,
TraceSelectionPluginEvent.class,
TraceHighlightPluginEvent.class,
TrackingChangedPluginEvent.class,
},
servicesRequired = {
ClipboardService.class,
@ -116,29 +121,12 @@ public class DebuggerMemoryBytesPlugin
}
}
@Override
protected void updateLocation(
ProgramByteViewerComponentProvider programByteViewerComponentProvider,
ProgramLocationPluginEvent event, boolean export) {
// TODO
}
@Override
protected void fireProgramLocationPluginEvent(
ProgramByteViewerComponentProvider programByteViewerComponentProvider,
ProgramLocationPluginEvent pluginEvent) {
// TODO
}
@Override
public void updateSelection(ByteViewerComponentProvider provider,
ProgramSelectionPluginEvent event, Program program) {
// TODO
}
@Override
public void highlightChanged(ByteViewerComponentProvider provider, ProgramSelection highlight) {
// TODO
if (provider == connectedProvider) {
tool.firePluginEvent(new TraceHighlightPluginEvent(getName(), highlight,
(TraceProgramView) connectedProvider.getProgram()));
}
}
protected void allProviders(Consumer<DebuggerMemoryBytesProvider> action) {
@ -150,17 +138,22 @@ public class DebuggerMemoryBytesPlugin
@Override
public void processEvent(PluginEvent event) {
// do not delegate to super
if (event instanceof TraceActivatedPluginEvent ev) {
current = ev.getActiveCoordinates();
allProviders(p -> p.coordinatesActivated(current));
}
if (event instanceof TraceClosedPluginEvent ev) {
else if (event instanceof TraceClosedPluginEvent ev) {
if (current.getTrace() == ev.getTrace()) {
current = DebuggerCoordinates.NOWHERE;
}
allProviders(p -> p.traceClosed(ev.getTrace()));
}
// TODO: Sync among dynamic providers?
else if (event instanceof TraceLocationPluginEvent ev) {
currentLocation = ev.getLocation();
}
connectedProvider.doHandleTraceEvent(event);
}
@AutoServiceConsumed

View file

@ -36,13 +36,15 @@ import docking.action.ToggleDockingAction;
import docking.menu.MultiStateDockingAction;
import docking.widgets.fieldpanel.support.ViewerPosition;
import generic.theme.GThemeDefaults.Colors;
import ghidra.app.events.AbstractLocationPluginEvent;
import ghidra.app.events.AbstractSelectionPluginEvent;
import ghidra.app.plugin.core.byteviewer.*;
import ghidra.app.plugin.core.debug.event.*;
import ghidra.app.plugin.core.debug.gui.*;
import ghidra.app.plugin.core.debug.gui.DebuggerResources.FollowsCurrentThreadAction;
import ghidra.app.plugin.core.debug.gui.action.*;
import ghidra.app.plugin.core.debug.gui.action.AutoReadMemorySpec.AutoReadMemorySpecConfigFieldCodec;
import ghidra.app.plugin.core.format.ByteBlock;
import ghidra.app.plugin.core.format.ByteBlockAccessException;
import ghidra.app.plugin.core.format.*;
import ghidra.app.services.*;
import ghidra.app.services.DebuggerControlService.ControlModeChangeListener;
import ghidra.debug.api.action.GoToInput;
@ -133,18 +135,21 @@ public class DebuggerMemoryBytesProvider extends ProgramByteViewerComponentProvi
DebuggerMemoryBytesProvider.this);
}
@Override
protected void locationTracked() {
doGoToTracked();
}
@Override
protected void specChanged(LocationTrackingSpec spec) {
if (isMainViewer()) {
plugin.firePluginEvent(new TrackingChangedPluginEvent(getName(), spec));
}
updateTitle();
trackingLabel.setText("");
trackingLabel.setToolTipText("");
trackingLabel.setForeground(Colors.FOREGROUND);
}
@Override
protected void locationTracked() {
doGoToTracked();
}
}
protected class ForMemoryBytesReadsMemoryTrait extends DebuggerReadsMemoryTrait {
@ -380,15 +385,32 @@ public class DebuggerMemoryBytesProvider extends ProgramByteViewerComponentProvi
}
class TargetByteBlockSet extends ProgramByteBlockSet {
private final DebuggerMemoryBytesProvider provider;
protected TargetByteBlockSet(ByteBlockChangeManager changeManager) {
super(DebuggerMemoryBytesProvider.this, DebuggerMemoryBytesProvider.this.program,
changeManager);
this.provider = DebuggerMemoryBytesProvider.this;
}
@Override
protected MemoryByteBlock newMemoryByteBlock(Memory memory, MemoryBlock memBlock) {
return new TargetByteBlock(program, memory, memBlock);
}
@Override
public AbstractLocationPluginEvent getPluginEvent(String source, ByteBlock block,
BigInteger offset, int column) {
ProgramLocation loc = provider.getLocation(block, offset, column);
return new TraceLocationPluginEvent(source, loc);
}
@Override
public AbstractSelectionPluginEvent getPluginEvent(String source,
ByteBlockSelection selection) {
ProgramSelection pSel = convertSelection(selection);
return new TraceSelectionPluginEvent(source, pSel, (TraceProgramView) program);
}
}
@Override
@ -604,6 +626,20 @@ public class DebuggerMemoryBytesProvider extends ProgramByteViewerComponentProvi
}
}
void doHandleTraceEvent(PluginEvent event) {
if (getByteBlocks() == null) {
return;
}
switch (event) {
case TraceLocationPluginEvent ev -> processLocationEvent(ev);
case TraceSelectionPluginEvent ev -> processSelectionEvent(ev);
case TraceHighlightPluginEvent ev -> processHighlightEvent(ev);
case TrackingChangedPluginEvent ev -> setTrackingSpec(ev.getLocationTrackingSpec());
default -> {
}
}
}
public void setFollowsCurrentThread(boolean follows) {
if (isMainViewer()) {
throw new IllegalStateException(