Skip to content

Commit

Permalink
GP-4636: Use Debug Console for initial mapping failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsadeveloper789 committed May 30, 2024
1 parent 3543461 commit 1db04f8
Show file tree
Hide file tree
Showing 8 changed files with 435 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.app.plugin.core.debug.gui.modules;
package ghidra.debug.api.modules;

import java.util.Objects;

Expand Down Expand Up @@ -43,10 +43,9 @@ public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof DebuggerMissingModuleActionContext)) {
if (!(obj instanceof DebuggerMissingModuleActionContext that)) {
return false;
}
DebuggerMissingModuleActionContext that = (DebuggerMissingModuleActionContext) obj;
if (!this.module.equals(that.module)) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* ###
* 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:https://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.debug.api.modules;

import java.util.Objects;

import docking.DefaultActionContext;
import ghidra.program.model.address.*;
import ghidra.program.model.listing.InstructionIterator;
import ghidra.program.model.listing.Program;
import ghidra.trace.model.Trace;

public class DebuggerMissingProgramActionContext extends DefaultActionContext {

public static Address getMappingProbeAddress(Program program) {
if (program == null) {
return null;
}
AddressIterator eepi = program.getSymbolTable().getExternalEntryPointIterator();
if (eepi.hasNext()) {
return eepi.next();
}
InstructionIterator ii = program.getListing().getInstructions(true);
if (ii.hasNext()) {
return ii.next().getAddress();
}
AddressSetView es = program.getMemory().getExecuteSet();
if (!es.isEmpty()) {
return es.getMinAddress();
}
if (!program.getMemory().isEmpty()) {
return program.getMinAddress();
}
return null;
}

private final Trace trace;
private final Program program;
private final int hashCode;

private Address probe;

public DebuggerMissingProgramActionContext(Trace trace, Program program) {
this.trace = Objects.requireNonNull(trace);
this.program = Objects.requireNonNull(program);
this.hashCode = Objects.hash(getClass(), trace, program);
}

public Trace getTrace() {
return trace;
}

public Program getProgram() {
return program;
}

@Override
public int hashCode() {
return hashCode;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof DebuggerMissingProgramActionContext that)) {
return false;
}
if (!this.trace.equals(that.trace)) {
return false;
}
if (!this.program.equals(that.program)) {
return false;
}
return true;
}

public Address getMappingProbeAddress() {
if (probe == null) {
probe = getMappingProbeAddress(program);
}
return probe;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@
import ghidra.framework.options.SaveState;
import ghidra.framework.plugintool.AutoConfigState.ConfigStateField;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.address.*;
import ghidra.program.model.listing.InstructionIterator;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.Program;
import ghidra.program.util.ProgramLocation;
import ghidra.pty.*;
import ghidra.trace.model.Trace;
import ghidra.trace.model.TraceLocation;
import ghidra.trace.model.modules.TraceModule;
import ghidra.util.MessageType;
import ghidra.util.Msg;
import ghidra.util.*;
import ghidra.util.exception.CancelledException;
import ghidra.util.task.Task;
import ghidra.util.task.TaskMonitor;
Expand Down Expand Up @@ -169,25 +167,8 @@ public Icon getIcon() {
}

protected Address getMappingProbeAddress() {
if (program == null) {
return null;
}
AddressIterator eepi = program.getSymbolTable().getExternalEntryPointIterator();
if (eepi.hasNext()) {
return eepi.next();
}
InstructionIterator ii = program.getListing().getInstructions(true);
if (ii.hasNext()) {
return ii.next().getAddress();
}
AddressSetView es = program.getMemory().getExecuteSet();
if (!es.isEmpty()) {
return es.getMinAddress();
}
if (!program.getMemory().isEmpty()) {
return program.getMinAddress();
}
return null; // I guess we won't wait for a mapping, then
// May be null, in which case, we won't wait for a mapping
return DebuggerMissingProgramActionContext.getMappingProbeAddress(program);
}

protected CompletableFuture<Void> listenForMapping(DebuggerStaticMappingService mappingService,
Expand Down Expand Up @@ -641,6 +622,22 @@ public LaunchResult launchProgram(TaskMonitor monitor, LaunchConfigurator config
}
return new LaunchResult(program, Map.of(), null, null, null, lastExc);
}
catch (NoStaticMappingException e) {
DebuggerConsoleService consoleService =
tool.getService(DebuggerConsoleService.class);
if (consoleService == null) {
Msg.error(this, e.getMessage());
}
else {
consoleService.log(DebuggerResources.ICON_MODULES,
"<html>The trace <b>%s</b> has no mapping to its program <b>%s</b></html>"
.formatted(
HTMLUtilities.escapeHTML(trace.getDomainFile().getName()),
HTMLUtilities.escapeHTML(program.getDomainFile().getName())),
new DebuggerMissingProgramActionContext(trace, program));
}
return new LaunchResult(program, sessions, acceptor, connection, trace, e);
}
catch (Exception e) {
DebuggerConsoleService consoleService =
tool.getService(DebuggerConsoleService.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,51 @@ <H3><A name="map_section_to"></A>Map Section to Current Block</H3>
<H3><A name="import_missing_module"></A><IMG alt="" src="icon.debugger.import"> Import Missing
Module</H3>

<P>This action is offered to resolve a "missing module" console message. It is equivalent to <A
href="#import_from_fs">Import From File System</A> on the missing module.</P>
<P>This action is offered to resolve a "missing module" console message. Such a message is
reported in the <A href="help/topics/DebuggerConsolePlugin/DebuggerConsolePlugin.html">Debug
Console</A> when the cursor in the <A href=
"help/topics/DebuggerListingPlugin/DebuggerListingPlugin.html">Dynamic Listing</A> cannot be
synchronized to the static listing for lack of a module mapping. This action is equivalent to
<A href="#import_from_fs">Import From File System</A> on the missing module.</P>

<H3><A name="map_missing_module"></A><IMG alt="" src="icon.debugger.map.modules"> Map Missing
Module</H3>

<P>This action is offered to resolve a "missing module" console message. It is equivalent to <A
href="#map_module_to">Map Module To</A> on the missing module.</P>
<P>This action is offered to resolve a "missing module" console message. Such a message is
reported in the <A href="help/topics/DebuggerConsolePlugin/DebuggerConsolePlugin.html">Debug
Console</A> when the cursor in the <A href=
"help/topics/DebuggerListingPlugin/DebuggerListingPlugin.html">Dynamic Listing</A> cannot be
synchronized to the static listing for lack of a module mapping. This action is equivalent to
<A href="#map_module_to">Map Module To</A> on the missing module.</P>

<H3><A name="map_missing_program_retry"></A><IMG alt="" src="icon.debugger.map.auto"> Retry Map
Missing Program</H3>

<P>This action is offered to resolve a "missing program" console message. Such a message is
reported in the <A href="help/topics/DebuggerConsolePlugin/DebuggerConsolePlugin.html">Debug
Console</A> when the launcher fails to map the current program database to the launched trace.
This action is equivalent to <A href="#map_modules">Map Modules</A>, but considering only the
missing program and launched trace.</P>

<H3><A name="map_missing_program_current"></A><IMG alt="" src="icon.debugger.map.modules"> Map
Missing Program to Current Module</H3>

<P>This action is offered to resolve a "missing program" console message. Such a message is
reported in the <A href="help/topics/DebuggerConsolePlugin/DebuggerConsolePlugin.html">Debug
Console</A> when the launcher fails to map the current program database to the launched trace.
This action is only available when the current trace is the launched trace. It finds the module
containing the cursor in the <A href=
"help/topics/DebuggerListingPlugin/DebuggerListingPlugin.html">Dynamic Listing</A> and proposes
to map it to the missing program.</P>

<H3><A name="map_missing_program_identically"></A><IMG alt="" src=
"icon.debugger.map.identically"> Map Missing Program Identically</H3>

<P>This action is offered to resolve a "missing program" console message. Such a message is
reported in the <A href="help/topics/DebuggerConsolePlugin/DebuggerConsolePlugin.html">Debug
Console</A> when the launcher fails to map the current program database to the launched trace.
This action is equivalent to <A href="#map_identically">Map Identically</A>, but for the
missing program and launched trace.</P>

<H3><A name="show_sections_table"></A><IMG alt="" src="icon.debugger.modules.table.sections">
Show Sections Table</H3>
Expand All @@ -179,12 +216,15 @@ <H3><A name="select_addresses"></A><IMG alt="" src="icon.debugger.select.address
Addresses</H3>

<P>This action is available when at least one module or section is selected. It selects all
addresses in the dynamic listing contained by the selected modules or sections.</P>
addresses in the <A href="help/topics/DebuggerListingPlugin/DebuggerListingPlugin.html">Dynamic
Listing</A> contained by the selected modules or sections.</P>

<H3><A name="select_rows"></A><IMG alt="" src="icon.debugger.select.rows"> Select Rows</H3>

<P>This action is available when the dynamic listing's cursor is at a valid location. It
selects the module and section, if applicable, containing that cursor. If the dynamic listing
has a selection, it selects all modules and sections intersecting that selection.</P>
<P>This action is available when the <A href=
"help/topics/DebuggerListingPlugin/DebuggerListingPlugin.html">Dynamic Listing</A>'s cursor is
at a valid location. It selects the module and section, if applicable, containing that cursor.
If the dynamic listing has a selection, it selects all modules and sections intersecting that
selection.</P>
</BODY>
</HTML>
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import ghidra.app.plugin.core.debug.gui.DebuggerResources.FollowsCurrentThreadAction;
import ghidra.app.plugin.core.debug.gui.DebuggerResources.OpenProgramAction;
import ghidra.app.plugin.core.debug.gui.action.*;
import ghidra.app.plugin.core.debug.gui.modules.DebuggerMissingModuleActionContext;
import ghidra.app.plugin.core.debug.gui.thread.DebuggerTraceFileActionContext;
import ghidra.app.plugin.core.debug.gui.trace.DebuggerTraceTabPanel;
import ghidra.app.plugin.core.debug.utils.ProgramLocationUtils;
Expand All @@ -74,6 +73,7 @@
import ghidra.debug.api.action.LocationTrackingSpec;
import ghidra.debug.api.control.ControlMode;
import ghidra.debug.api.listing.MultiBlendedListingBackgroundColorModel;
import ghidra.debug.api.modules.DebuggerMissingModuleActionContext;
import ghidra.debug.api.modules.DebuggerStaticMappingChangeListener;
import ghidra.debug.api.tracemgr.DebuggerCoordinates;
import ghidra.framework.model.DomainFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ghidra.app.plugin.core.debug.AbstractDebuggerPlugin;
import ghidra.app.plugin.core.debug.DebuggerPluginPackage;
import ghidra.app.plugin.core.debug.event.TraceActivatedPluginEvent;
import ghidra.app.plugin.core.debug.event.TraceClosedPluginEvent;
import ghidra.app.services.*;
import ghidra.framework.options.SaveState;
import ghidra.framework.plugintool.*;
Expand All @@ -37,6 +38,7 @@
ProgramLocationPluginEvent.class,
ProgramClosedPluginEvent.class,
TraceActivatedPluginEvent.class,
TraceClosedPluginEvent.class,
},
servicesRequired = {
DebuggerStaticMappingService.class,
Expand Down Expand Up @@ -81,6 +83,9 @@ else if (event instanceof ProgramClosedPluginEvent ev) {
else if (event instanceof TraceActivatedPluginEvent ev) {
provider.coordinatesActivated(ev.getActiveCoordinates());
}
else if (event instanceof TraceClosedPluginEvent ev) {
provider.traceClosed(ev.getTrace());
}
}

@Override
Expand Down
Loading

0 comments on commit 1db04f8

Please sign in to comment.