Ghidra is a software reverse engineering (SRE) framework
GP-5084 Adding ability to clear just instructions or data when using clear with options
ghidragon3cc34cf6a978
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/Clear.htm b/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/Clear.htmindex bf801c6999..fa83e140fa 100644--- a/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/Clear.htm+++ b/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/Clear.htm@@ -1,261 +1,327 @@<!DOCTYPE doctype PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN">-<html>-<head>- <title>Clear Code Bytes</title>- <meta http-equiv="content-type"- content="text/html; charset=windows-1252">- <link rel="stylesheet" type="text/css" href="help/shared/DefaultStyle.css">-</head>-<body>-<h1><a name="Clear_Code_Bytes"></a>Clear</h1>-<h2>Clear Code Bytes</h2>-<blockquote>- <p>Clear Code Bytes reverts <a- href="help/topics/DisassemblerPlugin/Disassembly.htm">disassembled</a>-instructions and <a href="help/topics/DataPlugin/Data.htm"> defined data</a> to-their undefined state. It can also be used to clear the components of-an applied structure directly from the browser.<br>- </p>-</blockquote>-<blockquote>- <p>To Clear a single instruction,</p>- <blockquote>- <ol type="1">- <li>Position the cursor on a defined instruction or data code unit</li>- <li>From the right-mouse pop-up menu over the Code Browser-window, select <i> Clear Code Bytes</i><br>-Alternately: press the 'C' key</li>- </ol>- </blockquote>- <p>To Clear an area,</p>- <blockquote>- <ol type="1">- <li>Select the defined code units (data and instructions)</li>- <li>From the right-mouse pop-up menu over the Code Browser-window, select <i> Clear Code Bytes</i><br>-Alternately: press the 'C' key</li>- </ol>- </blockquote>- <p>To Clear components of a structure directly in the browser,</p>- <blockquote>- <ol type="1">- <li>Select the structure elements in the open structure</li>- <li>From the right-mouse pop-up menu over the Code Browser-window, select <i> Clear Code Bytes</i><br>-Alternately: press the 'C' key</li>- <br>- </ol>- </blockquote>- <p> <img src="help/shared/note.png" x-maintain-ratio="TRUE"- border="0"> Clearing bytes inside a structure-from the CodeBrowser, changes the definition of the structure and will-affect all occurences of the structure. </p>-</blockquote>-<h2><a name="Clear_With_Options"></a>Clear With Options</h2>-<blockquote>- <p><i>Clear With Options</i> more selectively clears information-defined in the program. While <i>Clear Code Bytes</i> only clears code-units and their associated data, Clear With Options can selectively-remove additional information, e.g., symbols, references, functions,-etc.</p>- <p>The <i>Clear</i> dialog has check boxes for each type of-information that can be cleared. If the checkbox is selected, that item-will be removed everywhere within the selection when the OK button is-pressed.</p>-</blockquote>-<div style="text-align: center;"><img alt=""- src="images/ClearWithOptions.png"><br>-</div>-<blockquote>- <p>To Clear With Options,</p>- <ol>- <ol>- <li>Create a selection in the Code Browser containing instruction-and/or defined data to be cleared.</li>- <li>From the right-mouse pop-up menu over the Code Browser-window, select<i> Clear With Options...</i></li>- <li>De-selected the check boxes from the items that should not be-cleared.</li>- <li>Click <i> OK</i>.</li>- </ol>- </ol>- <blockquote>- <p> <i><img src="help/shared/note.png" x-maintain-ratio="TRUE"- border="0"> </i>You can undo clearing of code-units if it has an undesired effect.</p>- </blockquote>-</blockquote>-<blockquote>- <p>The following paragraphs describe each option.</p>- <h3>Symbols</h3>- <blockquote>- <p>Any User defined <a href="help/topics/SymbolTablePlugin/symbol_table.htm">-symbol</a> in the program. Automatically generated symbols won't be-cleared if references still exist to the symbol's defined address.</p>- <blockquote>- <p> <img src="help/shared/note.png" x-maintain-ratio="TRUE"- border="0"> This option will <b>not</b> clear function names. To delete these you must- select the <a href=#functions>functions</a> option. </p>- </blockquote>- </blockquote>- <h3>Comments</h3>- <blockquote>- <p>Clears Pre, Post, End of Line (EOL), Plate <a- href="help/topics/CommentsPlugin/Comments.htm">comments</a>. It will not clear-comments on functions or function variables.</p>- <blockquote>- <p> <img src="help/shared/note.png" x-maintain-ratio="TRUE"- border="0"> This option will <b>not</b> clear-repeatable or automatic comments. To delete those comments you must-delete the associated reference. </p>- </blockquote>- </blockquote>- <h3>Properties</h3>- <blockquote>- <p>Properties are placed at addresses in a program by plugins to-store information. This information is usually only understood by the-plugin that placed it there or other cooperating plugins.</p>- <p><img src="help/shared/tip.png" border="0">-Select the <a href="help/topics/PropertyManagerPlugin/Property_Viewer.htm">properties</a>-icon (<img src="images/document-properties.png" border="0">-) on the tool bar to display all the properties that are currently in-the program.</p>- </blockquote>- <h3>Code</h3>- <blockquote>- <p>Data and Instructions are cleared.</p>- </blockquote>- <h3>User-defined References</h3>- <blockquote>- <p>Any references added by the user are cleared.</p>- </blockquote>- <h3>Analysis References</h3>- <blockquote>- <p>Any references created by analysis tasks are cleared.</p>- </blockquote>- <h3>Import References</h3>- <blockquote>- <p>Any references created during the import process are cleared,- such as calls to external library functions.</p>- </blockquote>- <h3>Default References</h3>- <blockquote>- <p>References automatically created by Ghidra during-disassembly are cleared. This includes references to stack-variables within the body of a function.</p>- </blockquote>- <h3><a name="functions">Functions</a></h3>- <blockquote>- <p>Defined functions are cleared, including comments and any-defined variables. Any references to stack variables within the body of-the function will be cleared as well.</p>- </blockquote>- <h3>Registers</h3>- <blockquote>- <p>Registers within the selection with a defined value will be-cleared and set to undefined. Registers can be set to a value using Set-Register Values.</p>- </blockquote>- <h3>Equates</h3>- <blockquote>- <p>Instructions with Scalar Operands set to display an alternate-string with an <a href="help/topics/EquatePlugin/Equates.htm">Equate</a> are-cleared.</p>- </blockquote>- <h3>Bookmarks </h3>- <blockquote>- <p>All types of <a href="help/topics/BookmarkPlugin/Bookmarks.htm">Bookmarks</a>-are cleared.<br>- </p>- </blockquote>-</blockquote>-<h2><a name="Clear_Flow_and_Repair"></a>Clear Flow and Repair</h2>-<blockquote>- <p><i>Clear Flow and Repair</i> is intended to be used to clear and-optionally repair code which was produced in error. The duration of-this action-will vary depending on the extent of the instruction flow. If good code-with extensive flow is encountered, the action may take a long time to-complete. If the flow analysis is lengthy, a task dialog will be-displayed-with a <i>Cancel </i>button. The <i>Cancel </i>button may be-pressed-to terminate the action. </p>- <blockquote>- <p><img src="help/shared/note.png" alt="Note" border="0"> You can undo clearing of code units-if it has an undesired effect</p>- </blockquote>- <p>The <i>Clear Flow</i> options dialog has check boxes to control-its-behavior. Pressing the <i>OK </i>button will begin the clear process-using-the selected options. </p>-</blockquote>-<table x-use-null-cells="" width="100%">- <tbody>- <tr>- <td align="center" width="100%"> <img src="images/ClearFlow.png"- alt=""> <br>- </td>- </tr>- </tbody>-</table>-<blockquote>- <p>To Clear instructions produced by an invalid fall-through or bad-code produced by a data reference:</p>- <ol>- <ol>- <li>Click on the first bad instruction</li>- <li>From the right-mouse pop-up menu over the Code Browser-window, select<i> Clear Flow and Repair...</i></li>- <li>Choose the desired <i>Clear Flow Options</i>.</li>- <li>Click <i> OK</i>.</li>- </ol>- </ol>- <p>To Clear instructions referenced by one or more pointers :</p>- <ol>- <ol>- <li>Select all pointer data units</li>- <li>From the right-mouse pop-up menu over the Code Browser-window, select<i> Clear Flow and Repair...</i></li>- <li>Choose the desired <i>Clear Flow Options</i>.</li>- <li>Click <i> OK</i>.</li>- </ol>- </ol>- <p>Note that clearing pointer referenced code will also clear all-computed flow references to the address(es) referenced by the selected-pointer(s).<br>- </p>- <p>The following paragraphs describe each option.</p>- <h3>Clear Symbols</h3>- <blockquote>- <p>All non-default <a href="help/topics/SymbolTablePlugin/symbol_table.htm">symbols</a>-at cleared code unit locations will be removed. <br>- </p>- </blockquote>- <h3>Clear Data</h3>- <blockquote>- <p>All data whoose only references were from cleared code unit-locations will also be cleared.</p>- </blockquote>- <h3>Repair</h3>- <blockquote>- <p>Following the clearing of the flow, attempt to repair the-disassembly of references into the cleared region.</p>- </blockquote>-</blockquote>-<p class="providedbyplugin"><br>-Provided By: <i> ClearPlugin</i></p>-<p class="relatedtopic">Related Topics:</p>-<ul>- <li><a href="help/topics/DisassemblerPlugin/Disassembly.htm">Disassembly</a></li>- <li><a href="help/topics/ImporterPlugin/importer.htm">Importing Files</a></li>- <li><a href="help/topics/PropertyManagerPlugin/Property_Viewer.htm">Property-Viewer</a></li>-</ul>-<br>-<br>-<br>-<br>-</body>-</html>++<HTML>+ <HEAD>+ <META name="generator" content=+ "HTML Tidy for Java (vers. 2009-12-01), see jtidy.sourceforge.net">++ <TITLE>Clear Code Bytes</TITLE>+ <META http-equiv="content-type" content="text/html; charset=windows-1252">+ <LINK rel="stylesheet" type="text/css" href="help/shared/DefaultStyle.css">+ </HEAD>++ <BODY>+ <H1><A name="Clear_Code_Bytes"></A>Clear</H1>++ <H2>Clear Code Bytes</H2>++ <BLOCKQUOTE>+ <P>Clear Code Bytes reverts <A href=+ "help/topics/DisassemblerPlugin/Disassembly.htm">disassembled</A> instructions and <A href=+ "help/topics/DataPlugin/Data.htm">defined data</A> to their undefined state. It can also be+ used to clear the components of an applied structure directly from the browser.<BR>+ </P>+ </BLOCKQUOTE>++ <BLOCKQUOTE>+ <P>To Clear a single instruction,</P>++ <BLOCKQUOTE>+ <OL type="1">+ <LI>Position the cursor on a defined instruction or data code unit</LI>++ <LI>From the right-mouse pop-up menu over the Code Browser window, select <I>Clear Code+ Bytes</I><BR>+ Alternately: press the 'C' key</LI>+ </OL>+ </BLOCKQUOTE>++ <P>To Clear an area,</P>++ <BLOCKQUOTE>+ <OL type="1">+ <LI>Select the defined code units (data and instructions)</LI>++ <LI>From the right-mouse pop-up menu over the Code Browser window, select <I>Clear Code+ Bytes</I><BR>+ Alternately: press the 'C' key</LI>+ </OL>+ </BLOCKQUOTE>++ <P>To Clear components of a structure directly in the browser,</P>++ <BLOCKQUOTE>+ <OL type="1">+ <LI>Select the structure elements in the open structure</LI>++ <LI>From the right-mouse pop-up menu over the Code Browser window, select <I>Clear Code+ Bytes</I><BR>+ Alternately: press the 'C' key</LI>++ <LI style="list-style: none"><BR>+ </LI>+ </OL>+ </BLOCKQUOTE>++ <P><IMG alt="" src="help/shared/note.png" x-maintain-ratio="TRUE" border="0"> Clearing bytes+ inside a structure from the CodeBrowser, changes the definition of the structure and will+ affect all occurences of the structure.</P>+ </BLOCKQUOTE>++ <H2><A name="Clear_With_Options"></A>Clear With Options</H2>++ <BLOCKQUOTE>+ <P><I>Clear With Options</I> more selectively clears information defined in the program.+ While <I>Clear Code Bytes</I> only clears instructions and data, Clear With+ Options can selectively choose either instructions or data and additionally can remove+ other program information, e.g., symbols, references, functions, etc.</P>++ <P>The <I>Clear</I> dialog has check boxes for each type of information that can be cleared.+ If the check box is selected, that item will be removed everywhere within the selection when+ the OK button is pressed.</P>+ </BLOCKQUOTE>++ <DIV style="text-align: center;">+ <IMG alt="" src="images/ClearWithOptions.png"><BR>+ </DIV>++ <BLOCKQUOTE>+ <P>To Clear With Options,</P>++ <DIV style="margin-left: 2em">+ <OL>+ <LI>Create a selection in the Code Browser containing instruction and/or defined data to+ be cleared.</LI>++ <LI>From the right-mouse pop-up menu over the Code Browser window, select <I>Clear With+ Options...</I></LI>++ <LI>De-selected the check boxes from the items that should not be cleared.</LI>++ <LI>Click <I>OK</I>.</LI>+ </OL>+ </DIV>++ <BLOCKQUOTE>+ <P><I><IMG alt="" src="help/shared/note.png" x-maintain-ratio="TRUE" border="0"></I> You+ can undo clearing of code units if it has an undesired effect.</P>+ </BLOCKQUOTE>+ </BLOCKQUOTE>++ <BLOCKQUOTE>+ <P>The following paragraphs describe each option.</P>++ <H3>Symbols</H3>++ <BLOCKQUOTE>+ <P>Any User defined <A href="help/topics/SymbolTablePlugin/symbol_table.htm">symbol</A> in+ the program. Automatically generated symbols won't be cleared if references still exist to+ the symbol's defined address.</P>++ <BLOCKQUOTE>+ <P><IMG alt="" src="help/shared/note.png" x-maintain-ratio="TRUE" border="0"> This option+ will <B>not</B> clear function names. To delete these you must select the <A href=+ "#functions">functions</A> option.</P>+ </BLOCKQUOTE>+ </BLOCKQUOTE>++ <H3>Comments</H3>++ <BLOCKQUOTE>+ <P>Clears Pre, Post, End of Line (EOL), Plate <A href=+ "help/topics/CommentsPlugin/Comments.htm">comments</A>. It will not clear comments on+ functions or function variables.</P>++ <BLOCKQUOTE>+ <P><IMG alt="" src="help/shared/note.png" x-maintain-ratio="TRUE" border="0"> This option+ will <B>not</B> clear repeatable or automatic comments. To delete those comments you must+ delete the associated reference.</P>+ </BLOCKQUOTE>+ </BLOCKQUOTE>++ <H3>Properties</H3>++ <BLOCKQUOTE>+ <P>Properties are placed at addresses in a program by plugins to store information. This+ information is usually only understood by the plugin that placed it there or other+ cooperating plugins.</P>++ <P><IMG alt="" src="help/shared/tip.png" border="0"> Select the <A href=+ "help/topics/PropertyManagerPlugin/Property_Viewer.htm">properties</A> icon (<IMG alt=""+ src="images/document-properties.png" border="0"> ) on the tool bar to display all the+ properties that are currently in the program.</P>+ </BLOCKQUOTE>++ <H3>Instructions</H3>++ <BLOCKQUOTE>+ <P>Instructions are cleared.</P>+ </BLOCKQUOTE>++ <H3>Data</H3>++ <BLOCKQUOTE>+ <P>Defined Data elements are cleared.</P>+ </BLOCKQUOTE>++ <H3>User-defined References</H3>++ <BLOCKQUOTE>+ <P>Any references added by the user are cleared.</P>+ </BLOCKQUOTE>++ <H3>Analysis References</H3>++ <BLOCKQUOTE>+ <P>Any references created by analysis tasks are cleared.</P>+ </BLOCKQUOTE>++ <H3>Import References</H3>++ <BLOCKQUOTE>+ <P>Any references created during the import process are cleared, such as calls to external+ library functions.</P>+ </BLOCKQUOTE>++ <H3>Default References</H3>++ <BLOCKQUOTE>+ <P>References automatically created by Ghidra during disassembly are cleared. This includes+ references to stack variables within the body of a function.</P>+ </BLOCKQUOTE>++ <H3><A name="functions">Functions</A></H3>++ <BLOCKQUOTE>+ <P>Defined functions are cleared, including comments and any defined variables. Any+ references to stack variables within the body of the function will be cleared as well.</P>+ </BLOCKQUOTE>++ <H3>Registers</H3>++ <BLOCKQUOTE>+ <P>Registers within the selection with a defined value will be cleared and set to+ undefined. Registers can be set to a value using Set Register Values.</P>+ </BLOCKQUOTE>++ <H3>Equates</H3>++ <BLOCKQUOTE>+ <P>Instructions with Scalar Operands set to display an alternate string with an <A href=+ "help/topics/EquatePlugin/Equates.htm">Equate</A> are cleared.</P>+ </BLOCKQUOTE>++ <H3>Bookmarks</H3>++ <BLOCKQUOTE>+ <P>All types of <A href="help/topics/BookmarkPlugin/Bookmarks.htm">Bookmarks</A> are+ cleared.<BR>+ </P>+ </BLOCKQUOTE>+ </BLOCKQUOTE>++ <H2><A name="Clear_Flow_and_Repair"></A>Clear Flow and Repair</H2>++ <BLOCKQUOTE>+ <P><I>Clear Flow and Repair</I> is intended to be used to clear and optionally repair code+ which was produced in error. The duration of this action will vary depending on the extent of+ the instruction flow. If good code with extensive flow is encountered, the action may take a+ long time to complete. If the flow analysis is lengthy, a task dialog will be displayed with+ a <I>Cancel</I> button. The <I>Cancel</I> button may be pressed to terminate the action.</P>++ <BLOCKQUOTE>+ <P><IMG src="help/shared/note.png" alt="Note" border="0"> You can undo clearing of code+ units if it has an undesired effect</P>+ </BLOCKQUOTE>++ <P>The <I>Clear Flow</I> options dialog has check boxes to control its behavior. Pressing the+ <I>OK</I> button will begin the clear process using the selected options.</P>+ </BLOCKQUOTE>++ <TABLE x-use-null-cells="" width="100%">+ <TBODY>+ <TR>+ <TD align="center" width="100%"><IMG src="images/ClearFlow.png" alt=""><BR>+ </TD>+ </TR>+ </TBODY>+ </TABLE>++ <BLOCKQUOTE>+ <P>To Clear instructions produced by an invalid fall-through or bad code produced by a data+ reference:</P>++ <DIV style="margin-left: 2em">+ <OL>+ <LI>Click on the first bad instruction</LI>++ <LI>From the right-mouse pop-up menu over the Code Browser window, select <I>Clear Flow+ and Repair...</I></LI>++ <LI>Choose the desired <I>Clear Flow Options</I>.</LI>++ <LI>Click <I>OK</I>.</LI>+ </OL>+ </DIV>++ <P>To Clear instructions referenced by one or more pointers :</P>++ <DIV style="margin-left: 2em">+ <OL>+ <LI>Select all pointer data units</LI>++ <LI>From the right-mouse pop-up menu over the Code Browser window, select <I>Clear Flow+ and Repair...</I></LI>++ <LI>Choose the desired <I>Clear Flow Options</I>.</LI>++ <LI>Click <I>OK</I>.</LI>+ </OL>+ </DIV>++ <P>Note that clearing pointer referenced code will also clear all computed flow references to+ the address(es) referenced by the selected pointer(s).<BR>+ </P>++ <P>The following paragraphs describe each option.</P>++ <H3>Clear Symbols</H3>++ <BLOCKQUOTE>+ <P>All non-default <A href="help/topics/SymbolTablePlugin/symbol_table.htm">symbols</A> at+ cleared code unit locations will be removed.<BR>+ </P>+ </BLOCKQUOTE>++ <H3>Clear Data</H3>++ <BLOCKQUOTE>+ <P>All data whoose only references were from cleared code unit locations will also be+ cleared.</P>+ </BLOCKQUOTE>++ <H3>Repair</H3>++ <BLOCKQUOTE>+ <P>Following the clearing of the flow, attempt to repair the disassembly of references into+ the cleared region.</P>+ </BLOCKQUOTE>+ </BLOCKQUOTE>++ <P class="providedbyplugin"><BR>+ Provided By: <I>ClearPlugin</I></P>++ <P class="relatedtopic">Related Topics:</P>++ <UL>+ <LI><A href="help/topics/DisassemblerPlugin/Disassembly.htm">Disassembly</A></LI>++ <LI><A href="help/topics/ImporterPlugin/importer.htm">Importing Files</A></LI>++ <LI><A href="help/topics/PropertyManagerPlugin/Property_Viewer.htm">Property Viewer</A></LI>+ </UL><BR>+ <BR>+ <BR>+ <BR>+ </BODY>+</HTML>diff --git a/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/images/ClearWithOptions.png b/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/images/ClearWithOptions.pngindex 7498f84191..abe95ab32d 100644Binary files a/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/images/ClearWithOptions.png and b/Ghidra/Features/Base/src/main/help/help/topics/ClearPlugin/images/ClearWithOptions.png differdiff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearCmd.javaindex f3b1b08bec..54da29d0b3 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearCmd.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearCmd.java@@ -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,6 +15,8 @@*/package ghidra.app.plugin.core.clear;+import static ghidra.app.plugin.core.clear.ClearOptions.ClearType.*;+import java.util.Iterator;import java.util.Set;@@ -35,6 +37,9 @@ public class ClearCmd extends BackgroundCommand<Program> {private ClearOptions options;private boolean sendIndividualEvents = false;+ private TaskMonitor monitor;+ private Program program;+/*** A convenience constructor to clear a single code unit.*@@ -71,25 +76,34 @@ public class ClearCmd extends BackgroundCommand<Program> {this.view = view;this.options = options;this.sendIndividualEvents = sendIndividualEvents;++ if (options == null) {+ options = new ClearOptions(false);+ options.setShouldClear(INSTRUCTIONS, true);+ options.setShouldClear(DATA, true);+ }}@Override- public boolean applyTo(Program program, TaskMonitor monitor) {-+ public boolean applyTo(Program p, TaskMonitor taskMonitor) {+ this.monitor = taskMonitor;+ this.program = p;boolean wasEabled = program.isSendingEvents();try {program.setEventsEnabled(sendIndividualEvents);- return doApplyTo(program, monitor);+ return doApplyTo();}finally {program.setEventsEnabled(wasEabled);+ program = null;+ monitor = null;}}- private boolean doApplyTo(Program program, TaskMonitor monitor) {+ private boolean doApplyTo() {try {- doApplyWithCancel(program, monitor);+ doApplyWithCancel();monitor.setMessage("Clear completed");return true;}@@ -98,50 +112,51 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private boolean doApplyWithCancel(Program program, TaskMonitor monitor)+ private boolean doApplyWithCancel()throws CancelledException {if (monitor == null) {monitor = TaskMonitor.DUMMY;}- if (options == null) {- clearCode(program, view, monitor);- return true;+ if (options.shouldClear(EQUATES)) {+ clearEquates(view);}-- if (options.clearEquates()) {- clearEquates(program, view, monitor);+ if (options.shouldClear(INSTRUCTIONS) || options.shouldClear(DATA)) {+ clearInstructionsAndOrData(view);}- if (options.clearCode()) {- clearCode(program, view, monitor);+ if (options.shouldClear(COMMENTS)) {+ clearComments(view);}- if (options.clearComments()) {- clearComments(program, view, monitor);+ if (options.shouldClear(FUNCTIONS)) {+ clearFunctions(view);}- if (options.clearFunctions()) {- clearFunctions(program, view, monitor);+ if (options.shouldClear(SYMBOLS)) {+ clearSymbols(view);}- if (options.clearSymbols()) {- clearSymbols(program, view, monitor);+ if (options.shouldClear(PROPERTIES)) {+ clearProperties(view);}- if (options.clearProperties()) {- clearProperties(program, view, monitor);+ if (options.shouldClear(REGISTERS)) {+ clearRegisters(view);}- if (options.clearRegisters()) {- clearRegisters(program, view, monitor);+ if (options.shouldClear(BOOKMARKS)) {+ clearBookmarks(view);}- Set<SourceType> referenceSourceTypesToClear = options.getReferenceSourceTypesToClear();- if (!options.clearCode() && !referenceSourceTypesToClear.isEmpty()) {- clearReferences(program, view, referenceSourceTypesToClear, monitor);++ // if clearing instructions and data, no need to handle references separately+ if (options.shouldClear(INSTRUCTIONS) && options.shouldClear(DATA)) {+ return true;}- if (options.clearBookmarks()) {- clearBookmarks(program, view, monitor);++ Set<SourceType> referenceSourceTypesToClear = options.getReferenceSourceTypesToClear();+ if (!referenceSourceTypesToClear.isEmpty()) {+ clearReferences(view, referenceSourceTypesToClear);}return true;}- private void clearSymbols(Program program, AddressSetView clearView, TaskMonitor monitor)+ private void clearSymbols(AddressSetView clearView)throws CancelledException {if (clearView.isEmpty()) {@@ -187,7 +202,7 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void clearComments(Program program, AddressSetView clearView, TaskMonitor monitor)+ private void clearComments(AddressSetView clearView)throws CancelledException {monitor.initialize(clearView.getNumAddresses());@@ -206,7 +221,7 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void clearProperties(Program program, AddressSetView clearView, TaskMonitor monitor)+ private void clearProperties(AddressSetView clearView)throws CancelledException {monitor.initialize(clearView.getNumAddresses());@@ -223,7 +238,7 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void clearFunctions(Program program, AddressSetView clearView, TaskMonitor monitor)+ private void clearFunctions(AddressSetView clearView)throws CancelledException {FunctionManager manager = program.getFunctionManager();@@ -242,7 +257,7 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void clearRegisters(Program program, AddressSetView clearView, TaskMonitor monitor)+ private void clearRegisters(AddressSetView clearView)throws CancelledException {monitor.initialize(clearView.getNumAddresses());@@ -251,16 +266,16 @@ public class ClearCmd extends BackgroundCommand<Program> {AddressRangeIterator iter = clearView.getAddressRanges();while (iter.hasNext()) {AddressRange range = iter.next();- removeRegisters(pc, range, monitor);+ removeRegisters(pc, range);}}- private void clearEquates(Program p, AddressSetView clearView, TaskMonitor monitor)+ private void clearEquates(AddressSetView clearView)throws CancelledException {monitor.initialize(100);monitor.setMessage("Starting to clear equates...");- EquateTable eqtbl = p.getEquateTable();+ EquateTable eqtbl = program.getEquateTable();Iterator<Equate> iter = eqtbl.getEquates();while (iter.hasNext()) {@@ -280,33 +295,76 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void clearCode(Program program, AddressSetView clearView, TaskMonitor monitor)+ private void clearInstructionsAndOrData(AddressSetView clearView)throws CancelledException {- Listing listing = program.getListing();+ boolean clearInstructions = options.shouldClear(INSTRUCTIONS);+ boolean clearData = options.shouldClear(DATA);monitor.initialize(clearView.getNumAddresses());- monitor.setMessage("Starting to clear code...");-- AddressRangeIterator it = clearView.getAddressRanges();- while (it.hasNext()) {+ monitor.setMessage(getMessage(clearInstructions, clearData));- AddressRange currentRange = it.next();- Address start = currentRange.getMinAddress();- Address end = currentRange.getMaxAddress();- clearAddresses(monitor, listing, start, end);+ for (AddressRange range : clearView.getAddressRanges()) {+ clearCode(range, clearInstructions, clearData);}}- private void clearAddresses(TaskMonitor monitor, Listing listing, Address start, Address end)+ private void clearCode(AddressRange range, boolean clearInstructions, boolean clearData)throws CancelledException {- boolean clearContext = options != null && options.clearRegisters();- AddressRangeChunker chunker = new AddressRangeChunker(start, end, 10000);- for (AddressRange range : chunker) {+ if (clearData && clearInstructions) {+ clearCodeUnits(range);+ return;+ }++ AddressSet set = clearInstructions ? getInstructionRanges(range) : getDataRanges(range);++ for (AddressRange r : set.getAddressRanges()) {+ clearCodeUnits(r);+ }++ // also increment monitor for skipped address+ monitor.incrementProgress(range.getLength() - set.getNumAddresses());+ }++ private AddressSet getInstructionRanges(AddressRange range) {+ AddressSet addresses = new AddressSet();+ Listing listing = program.getListing();+ Address end = range.getMaxAddress();++ for (Instruction inst : listing.getInstructions(range.getMinAddress(), true)) {+ if (inst.getMinAddress().compareTo(end) > 0) {+ break;+ }+ addresses.add(inst.getMinAddress(), inst.getMaxAddress());+ }+ return addresses;+ }++ private AddressSet getDataRanges(AddressRange range) {+ AddressSet addresses = new AddressSet();+ Listing listing = program.getListing();+ Address end = range.getMaxAddress();++ for (Data data : listing.getDefinedData(range.getMinAddress(), true)) {+ if (data.getMinAddress().compareTo(end) > 0) {+ break;+ }+ addresses.add(data.getMinAddress(), data.getMaxAddress());+ }+ return addresses;+ }- Address min = range.getMinAddress();- Address max = range.getMaxAddress();+ private void clearCodeUnits(AddressRange range) throws CancelledException {++ Listing listing = program.getListing();++ boolean clearContext = options.shouldClear(REGISTERS);+ AddressRangeChunker chunker = new AddressRangeChunker(range, 10000);+ for (AddressRange chunk : chunker) {++ Address min = chunk.getMinAddress();+ Address max = chunk.getMaxAddress();monitor.setMessage("Clearing code at " + min);@@ -320,8 +378,8 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void clearReferences(Program program, AddressSetView clearView,- Set<SourceType> sourceTypesToClear, TaskMonitor monitor) throws CancelledException {+ private void clearReferences(AddressSetView clearView, Set<SourceType> sourceTypesToClear)+ throws CancelledException {if (clearView.isEmpty()) {return;@@ -330,13 +388,12 @@ public class ClearCmd extends BackgroundCommand<Program> {monitor.initialize(clearView.getNumAddresses());monitor.setMessage("Clearing references...");- ReferenceManager refMgr = program.getReferenceManager();- AddressIterator it = refMgr.getReferenceSourceIterator(clearView, true);- removeRefs(refMgr, it, sourceTypesToClear, monitor);+ ReferenceManager referenceManager = program.getReferenceManager();+ AddressIterator it = referenceManager.getReferenceSourceIterator(clearView, true);+ removeRefs(it, sourceTypesToClear);}- private void clearBookmarks(Program program, AddressSetView clearView, TaskMonitor monitor)- throws CancelledException {+ private void clearBookmarks(AddressSetView clearView) throws CancelledException {if (clearView.isEmpty()) {return;@@ -348,7 +405,7 @@ public class ClearCmd extends BackgroundCommand<Program> {bookmarkMgr.removeBookmarks(clearView, monitor);}- private void removeRegisters(ProgramContext pc, AddressRange range, TaskMonitor monitor)+ private void removeRegisters(ProgramContext pc, AddressRange range)throws CancelledException {for (Register reg : pc.getRegistersWithValues()) {monitor.checkCancelled();@@ -365,24 +422,36 @@ public class ClearCmd extends BackgroundCommand<Program> {}}- private void removeRefs(ReferenceManager refMgr, AddressIterator iter,- Set<SourceType> sourceTypesToClear, TaskMonitor monitor) throws CancelledException {+ private void removeRefs(AddressIterator iter, Set<SourceType> sourceTypesToClear)+ throws CancelledException {++ ReferenceManager referenceManager = program.getReferenceManager();while (iter.hasNext()) {monitor.checkCancelled();Address addr = iter.next();- Reference[] refs = refMgr.getReferencesFrom(addr);+ Reference[] refs = referenceManager.getReferencesFrom(addr);for (Reference ref : refs) {if (monitor.isCancelled()) {break;}SourceType source = ref.getSource();if (sourceTypesToClear.contains(source)) {- refMgr.delete(ref);+ referenceManager.delete(ref);}}monitor.incrementProgress(1);}}++ private String getMessage(boolean clearInstructions, boolean clearData) {+ if (!clearData) {+ return "Clearing Instructions...";+ }+ if (!clearInstructions) {+ return "Clearing Data...";+ }+ return "Clearing Instructions and Data...";+ }}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearDialog.javaindex 62df84aef2..b33ad40f9a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearDialog.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearDialog.java@@ -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.@@ -25,9 +25,9 @@ import javax.swing.*;import docking.DialogComponentProvider;import docking.widgets.checkbox.GCheckBox;-import docking.widgets.checkbox.GHtmlCheckBox;import docking.widgets.label.GLabel;import ghidra.app.context.ListingActionContext;+import ghidra.app.plugin.core.clear.ClearOptions.ClearType;import ghidra.util.HelpLocation;/**@@ -41,7 +41,8 @@ public class ClearDialog extends DialogComponentProvider {private JCheckBox symbolsCb;private JCheckBox commentsCb;private JCheckBox propertiesCb;- private JCheckBox codeCb;+ private JCheckBox instructionsCb;+ private JCheckBox dataCb;private JCheckBox functionsCb;private JCheckBox registersCb;private JCheckBox equatesCb;@@ -72,22 +73,23 @@ public class ClearDialog extends DialogComponentProvider {public void okCallback() {close();- ClearOptions opts = new ClearOptions();+ ClearOptions options = new ClearOptions();- opts.setClearCode(codeCb.isSelected());- opts.setClearSymbols(symbolsCb.isSelected());- opts.setClearComments(commentsCb.isSelected());- opts.setClearProperties(propertiesCb.isSelected());- opts.setClearFunctions(functionsCb.isSelected());- opts.setClearRegisters(registersCb.isSelected());- opts.setClearEquates(equatesCb.isSelected());- opts.setClearUserReferences(userReferencesCb.isSelected());- opts.setClearAnalysisReferences(analysisReferencesCb.isSelected());- opts.setClearImportReferences(importReferencesCb.isSelected());- opts.setClearDefaultReferences(systemReferencesCb.isSelected());- opts.setClearBookmarks(bookmarksCb.isSelected());+ options.setShouldClear(ClearType.INSTRUCTIONS, instructionsCb.isSelected());+ options.setShouldClear(ClearType.DATA, dataCb.isSelected());+ options.setShouldClear(ClearType.SYMBOLS, symbolsCb.isSelected());+ options.setShouldClear(ClearType.COMMENTS, commentsCb.isSelected());+ options.setShouldClear(ClearType.PROPERTIES, propertiesCb.isSelected());+ options.setShouldClear(ClearType.FUNCTIONS, functionsCb.isSelected());+ options.setShouldClear(ClearType.REGISTERS, registersCb.isSelected());+ options.setShouldClear(ClearType.EQUATES, equatesCb.isSelected());+ options.setShouldClear(ClearType.USER_REFERENCES, userReferencesCb.isSelected());+ options.setShouldClear(ClearType.ANALYSIS_REFERENCES, analysisReferencesCb.isSelected());+ options.setShouldClear(ClearType.IMPORT_REFERENCES, importReferencesCb.isSelected());+ options.setShouldClear(ClearType.DEFAULT_REFERENCES, systemReferencesCb.isSelected());+ options.setShouldClear(ClearType.BOOKMARKS, bookmarksCb.isSelected());- plugin.clear(opts, context);+ plugin.clear(options, context);}/**@@ -123,11 +125,12 @@ public class ClearDialog extends DialogComponentProvider {cbPanel.setLayout(bl);symbolsCb = new GCheckBox("Symbols");- commentsCb = new GHtmlCheckBox(- "<html>Comments <FONT SIZE=\"2\">(does not affect automatic comments)</FONT>");+ commentsCb = new GCheckBox("Comments");+ commentsCb.setToolTipText("Does not clear automatic comments.");commentsCb.setVerticalTextPosition(SwingConstants.TOP);propertiesCb = new GCheckBox("Properties");- codeCb = new GCheckBox("Code");+ instructionsCb = new GCheckBox("Instructions");+ dataCb = new GCheckBox("Data");functionsCb = new GCheckBox("Functions");registersCb = new GCheckBox("Registers");equatesCb = new GCheckBox("Equates");@@ -143,8 +146,10 @@ public class ClearDialog extends DialogComponentProvider {commentsCb.addKeyListener(listener);propertiesCb.setSelected(true);propertiesCb.addKeyListener(listener);- codeCb.setSelected(true);- codeCb.addKeyListener(listener);+ instructionsCb.setSelected(true);+ instructionsCb.addKeyListener(listener);+ dataCb.setSelected(true);+ dataCb.addKeyListener(listener);functionsCb.setSelected(true);functionsCb.addKeyListener(listener);registersCb.setSelected(true);@@ -165,7 +170,8 @@ public class ClearDialog extends DialogComponentProvider {cbPanel.add(symbolsCb);cbPanel.add(commentsCb);cbPanel.add(propertiesCb);- cbPanel.add(codeCb);+ cbPanel.add(instructionsCb);+ cbPanel.add(dataCb);cbPanel.add(userReferencesCb);cbPanel.add(analysisReferencesCb);cbPanel.add(importReferencesCb);@@ -175,12 +181,12 @@ public class ClearDialog extends DialogComponentProvider {cbPanel.add(equatesCb);cbPanel.add(bookmarksCb);- // if a user clears the code, then we will force them+ // if a user clears instructions and data, then we will force them// to clear all user references...- codeCb.addItemListener(new ItemListener() {+ ItemListener itemListener = new ItemListener() {@Overridepublic void itemStateChanged(ItemEvent e) {- if (codeCb.isSelected()) {+ if (instructionsCb.isSelected() && dataCb.isSelected()) {userReferencesCb.setSelected(true);userReferencesCb.setEnabled(false);analysisReferencesCb.setSelected(true);@@ -197,7 +203,9 @@ public class ClearDialog extends DialogComponentProvider {systemReferencesCb.setEnabled(true);}}- });+ };+ instructionsCb.addItemListener(itemListener);+ dataCb.addItemListener(itemListener);userReferencesCb.setEnabled(false);analysisReferencesCb.setEnabled(false);@@ -209,7 +217,8 @@ public class ClearDialog extends DialogComponentProvider {checkBoxList.add(symbolsCb);checkBoxList.add(commentsCb);checkBoxList.add(propertiesCb);- checkBoxList.add(codeCb);+ checkBoxList.add(instructionsCb);+ checkBoxList.add(dataCb);checkBoxList.add(userReferencesCb);checkBoxList.add(analysisReferencesCb);checkBoxList.add(importReferencesCb);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearFlowAndRepairCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearFlowAndRepairCmd.javaindex 8cc840ba4b..b263b48e6a 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearFlowAndRepairCmd.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearFlowAndRepairCmd.java@@ -20,6 +20,7 @@ import java.util.*;import ghidra.app.cmd.disassemble.DisassembleCommand;import ghidra.app.cmd.function.CreateFunctionCmd;import ghidra.app.plugin.core.analysis.AutoAnalysisManager;+import ghidra.app.plugin.core.clear.ClearOptions.ClearType;import ghidra.framework.cmd.BackgroundCommand;import ghidra.program.database.function.OverlappingFunctionException;import ghidra.program.disassemble.Disassembler;@@ -203,7 +204,7 @@ public class ClearFlowAndRepairCmd extends BackgroundCommand<Program> {clearSet.delete(protectedSet);ClearOptions opts = new ClearOptions(true);- opts.setClearSymbols(clearLabels);+ opts.setShouldClear(ClearType.SYMBOLS, clearLabels);ClearCmd clear = new ClearCmd(clearSet, opts);clear.applyTo(program, monitor);@@ -318,7 +319,8 @@ public class ClearFlowAndRepairCmd extends BackgroundCommand<Program> {continue; // don't add to clear set}// if defined data is anything other than Undefined1,2... or a pointer- if (data.isDefined() && !(data.getDataType() instanceof Undefined) && !(data.isPointer())) {+ if (data.isDefined() && !(data.getDataType() instanceof Undefined) &&+ !(data.isPointer())) {continue; // don't add to clear set}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearOptions.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearOptions.javaindex 33b7ea8fde..34621f167b 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearOptions.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearOptions.java@@ -1,13 +1,12 @@/* ###* IP: GHIDRA- * REVIEWED: YES** 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.@@ -16,170 +15,76 @@*/package ghidra.app.plugin.core.clear;-import ghidra.program.model.symbol.SourceType;-import java.util.HashSet;import java.util.Set;+import ghidra.program.model.symbol.SourceType;+public class ClearOptions {+ public enum ClearType {+ INSTRUCTIONS,+ DATA,+ SYMBOLS,+ COMMENTS,+ PROPERTIES,+ FUNCTIONS,+ REGISTERS,+ EQUATES,+ USER_REFERENCES,+ ANALYSIS_REFERENCES,+ IMPORT_REFERENCES,+ DEFAULT_REFERENCES,+ BOOKMARKS+ }- private boolean code;- private boolean symbols;- private boolean comments;- private boolean properties;- private boolean functions;- private boolean registers;- private boolean equates;- private boolean userReferences;- private boolean analysisReferences;- private boolean importReferences;- private boolean defaultReferences;- private boolean bookmarks;+ private Set<ClearType> typesToClearSet = new HashSet<>();/*** Default constructor that will clear everything!*/public ClearOptions() {- this(true);+ this(true);}- public ClearOptions( boolean defaultClearValue ) {- this(defaultClearValue, defaultClearValue, defaultClearValue, defaultClearValue,- defaultClearValue, defaultClearValue, defaultClearValue, defaultClearValue,- defaultClearValue, defaultClearValue, defaultClearValue, defaultClearValue);+ public ClearOptions(boolean defaultClearState) {+ if (defaultClearState) {+ for (ClearType type : ClearType.values()) {+ typesToClearSet.add(type);+ }+ }}-- private ClearOptions(boolean code,- boolean symbols,- boolean comments,- boolean properties,- boolean functions,- boolean registers,- boolean equates,- boolean userReferences,- boolean analysisReferences,- boolean importReferences,- boolean defaultReferences,- boolean bookmarks) {- this.code = code;- this.symbols = symbols;- this.comments = comments;- this.properties = properties;- this.functions = functions;- this.registers = registers;- this.equates = equates;- this.userReferences = userReferences;- this.analysisReferences = analysisReferences;- this.importReferences = importReferences;- this.defaultReferences = defaultReferences;- this.bookmarks = bookmarks;- }-- public void setClearCode( boolean code ) {- this.code = code;- }-- public void setClearSymbols( boolean symbols ) {- this.symbols = symbols;- }-- public void setClearComments( boolean comments ) {- this.comments = comments;- }-- public void setClearProperties( boolean properties ) {- this.properties = properties;- }- public void setClearFunctions( boolean functions ) {- this.functions = functions;- }-- public void setClearRegisters( boolean registers ) {- this.registers = registers;- }-- public void setClearEquates( boolean equates ) {- this.equates = equates;- }-- public void setClearUserReferences( boolean userReferences ) {- this.userReferences = userReferences;- }-- public void setClearAnalysisReferences( boolean analysisReferences ) {- this.analysisReferences = analysisReferences;- }-- public void setClearImportReferences( boolean importReferences ) {- this.importReferences = importReferences;- }-- public void setClearDefaultReferences( boolean defaultReferences ) {- this.defaultReferences = defaultReferences;- }+ public void setShouldClear(ClearType type, boolean shouldClear) {+ if (shouldClear) {+ typesToClearSet.add(type);+ }+ else {+ typesToClearSet.remove(type);+ }+ }- public void setClearBookmarks( boolean bookmarks ) {- this.bookmarks = bookmarks;- }-- boolean clearCode() {- return code;- }- boolean clearComments() {- return comments;- }- boolean clearProperties() {- return properties;- }- boolean clearSymbols() {- return symbols;- }- boolean clearFunctions() {- return functions;- }- boolean clearRegisters() {- return registers;- }- boolean clearEquates() {- return equates;- }- boolean clearUserReferences() {- return userReferences;- }- boolean clearAnalysisReferences() {- return analysisReferences;- }- boolean clearImportReferences() {- return importReferences;- }- boolean clearDefaultReferences() {- return defaultReferences;- }- boolean clearBookmarks() {- return bookmarks;- }+ public boolean shouldClear(ClearType type) {+ return typesToClearSet.contains(type);+ }- Set<SourceType> getReferenceSourceTypesToClear() {- HashSet<SourceType> sourceTypesToClear = new HashSet<SourceType>();- if (clearUserReferences()) {- sourceTypesToClear.add(SourceType.USER_DEFINED);- }- if (clearDefaultReferences()) {- sourceTypesToClear.add(SourceType.DEFAULT);- }- if (clearImportReferences()) {- sourceTypesToClear.add(SourceType.IMPORTED);- }- if (clearAnalysisReferences()) {- sourceTypesToClear.add(SourceType.ANALYSIS);- }- return sourceTypesToClear;- }+ Set<SourceType> getReferenceSourceTypesToClear() {+ HashSet<SourceType> sourceTypesToClear = new HashSet<SourceType>();+ if (shouldClear(ClearType.USER_REFERENCES)) {+ sourceTypesToClear.add(SourceType.USER_DEFINED);+ }+ if (shouldClear(ClearType.DEFAULT_REFERENCES)) {+ sourceTypesToClear.add(SourceType.DEFAULT);+ }+ if (shouldClear(ClearType.IMPORT_REFERENCES)) {+ sourceTypesToClear.add(SourceType.IMPORTED);+ }+ if (shouldClear(ClearType.ANALYSIS_REFERENCES)) {+ sourceTypesToClear.add(SourceType.ANALYSIS);+ }+ return sourceTypesToClear;+ }- boolean clearAny() {- return code || symbols || comments || properties || functions- || registers || equates || userReferences || analysisReferences- || importReferences || defaultReferences || bookmarks;- }+ boolean clearAny() {+ return !typesToClearSet.isEmpty();+ }}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearPlugin.javaindex 5207b5e0e8..bdd9ab1002 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearPlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/clear/ClearPlugin.java@@ -15,6 +15,8 @@*/package ghidra.app.plugin.core.clear;+import static ghidra.app.plugin.core.clear.ClearOptions.ClearType.*;+import docking.action.builder.ActionBuilder;import docking.tool.ToolConstants;import ghidra.app.CorePluginPackage;@@ -226,22 +228,16 @@ public class ClearPlugin extends Plugin {}private void clearCodeBytes(ListingActionContext context) {- ClearOptions opts = new ClearOptions();+ ClearOptions options = new ClearOptions(false);- opts.setClearCode(true);- opts.setClearSymbols(false);- opts.setClearComments(false);- opts.setClearProperties(false);- opts.setClearFunctions(false);- opts.setClearRegisters(false);- opts.setClearEquates(false);- opts.setClearUserReferences(true);- opts.setClearAnalysisReferences(true);- opts.setClearImportReferences(true);- opts.setClearDefaultReferences(false);- opts.setClearBookmarks(false);+ options.setShouldClear(INSTRUCTIONS, true);+ options.setShouldClear(DATA, true);+ options.setShouldClear(USER_REFERENCES, true);+ options.setShouldClear(ANALYSIS_REFERENCES, true);+ options.setShouldClear(IMPORT_REFERENCES, true);+ options.setShouldClear(DEFAULT_REFERENCES, true);- clear(opts, context);+ clear(options, context);}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/UninitializedBlockCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/UninitializedBlockCmd.javaindex 21ac69cf8a..940e63bc1f 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/UninitializedBlockCmd.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/memory/UninitializedBlockCmd.java@@ -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,6 +15,8 @@*/package ghidra.app.plugin.core.memory;+import static ghidra.app.plugin.core.clear.ClearOptions.ClearType.*;+import ghidra.app.plugin.core.clear.ClearCmd;import ghidra.app.plugin.core.clear.ClearOptions;import ghidra.framework.cmd.BackgroundCommand;@@ -36,12 +38,14 @@ public class UninitializedBlockCmd extends BackgroundCommand<Program> {@Overridepublic boolean applyTo(Program program, TaskMonitor monitor) throws RuntimeException {ClearOptions clearOptions = new ClearOptions(false);- clearOptions.setClearCode(true);- clearOptions.setClearDefaultReferences(true);- clearOptions.setClearFunctions(true);- clearOptions.setClearAnalysisReferences(true);- clearOptions.setClearImportReferences(true);- clearOptions.setClearUserReferences(true);+ clearOptions.setShouldClear(INSTRUCTIONS, true);+ clearOptions.setShouldClear(DATA, true);+ clearOptions.setShouldClear(FUNCTIONS, true);+ clearOptions.setShouldClear(DEFAULT_REFERENCES, true);+ clearOptions.setShouldClear(ANALYSIS_REFERENCES, true);+ clearOptions.setShouldClear(IMPORT_REFERENCES, true);+ clearOptions.setShouldClear(USER_REFERENCES, true);+ClearCmd clearCmd =new ClearCmd(new AddressSet(block.getStart(), block.getEnd()), clearOptions);if (!clearCmd.applyTo(program, monitor)) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java b/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.javaindex 421e58b883..dfbb1b3916 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/flatapi/FlatProgramAPI.java@@ -15,6 +15,8 @@*/package ghidra.program.flatapi;+import static ghidra.app.plugin.core.clear.ClearOptions.ClearType.*;+import java.io.*;import java.text.SimpleDateFormat;import java.util.*;@@ -295,19 +297,48 @@ public class FlatProgramAPI {boolean equates, boolean userReferences, boolean analysisReferences,boolean importReferences, boolean defaultReferences, boolean bookmarks) {+ return this.clearListing(set, code, code, symbols, comments, properties, functions,+ registers, equates, userReferences, analysisReferences, importReferences,+ defaultReferences, bookmarks);+ }++ /**+ * Clears the listing in the specified address set.+ * @param set the address set where to clear+ * @param instructions true if instructions should be cleared+ * @param data true if defined data should be cleared+ * @param symbols true if symbols should be cleared+ * @param comments true if comments should be cleared+ * @param properties true if properties should be cleared+ * @param functions true if functions should be cleared+ * @param registers true if registers should be cleared+ * @param equates true if equates should be cleared+ * @param userReferences true if user references should be cleared+ * @param analysisReferences true if analysis references should be cleared+ * @param importReferences true if import references should be cleared+ * @param defaultReferences true if default references should be cleared+ * @param bookmarks true if bookmarks should be cleared+ * @return true if the address set was successfully cleared+ */+ public final boolean clearListing(AddressSetView set, boolean instructions,+ boolean data, boolean symbols, boolean comments, boolean properties, boolean functions,+ boolean registers, boolean equates, boolean userReferences, boolean analysisReferences,+ boolean importReferences, boolean defaultReferences, boolean bookmarks) {+ClearOptions options = new ClearOptions();- options.setClearCode(code);- options.setClearSymbols(symbols);- options.setClearComments(comments);- options.setClearProperties(properties);- options.setClearFunctions(functions);- options.setClearRegisters(registers);- options.setClearEquates(equates);- options.setClearUserReferences(userReferences);- options.setClearAnalysisReferences(analysisReferences);- options.setClearImportReferences(importReferences);- options.setClearDefaultReferences(defaultReferences);- options.setClearBookmarks(bookmarks);+ options.setShouldClear(INSTRUCTIONS, instructions);+ options.setShouldClear(DATA, data);+ options.setShouldClear(SYMBOLS, symbols);+ options.setShouldClear(COMMENTS, comments);+ options.setShouldClear(PROPERTIES, properties);+ options.setShouldClear(FUNCTIONS, functions);+ options.setShouldClear(REGISTERS, registers);+ options.setShouldClear(EQUATES, equates);+ options.setShouldClear(USER_REFERENCES, userReferences);+ options.setShouldClear(ANALYSIS_REFERENCES, analysisReferences);+ options.setShouldClear(IMPORT_REFERENCES, importReferences);+ options.setShouldClear(DEFAULT_REFERENCES, defaultReferences);+ options.setShouldClear(BOOKMARKS, bookmarks);ClearCmd cmd = new ClearCmd(set, options);return cmd.applyTo(currentProgram, monitor);diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/data/DataReferencesTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/data/DataReferencesTest.javaindex 0c7eee4789..e1a22dd4a2 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/data/DataReferencesTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/data/DataReferencesTest.java@@ -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,6 +15,7 @@*/package ghidra.app.plugin.core.data;+import static ghidra.app.plugin.core.clear.ClearOptions.ClearType.*;import static org.junit.Assert.*;import org.junit.*;@@ -583,9 +584,9 @@ public class DataReferencesTest extends AbstractGhidraHeadedIntegrationTest {private void clearSymbols(Address start, Address end) {ClearOptions options = new ClearOptions(false);- options.setClearSymbols(true);+ options.setShouldClear(SYMBOLS, true);- Command cmd = new ClearCmd(new AddressSet(start, end), options);+ ClearCmd cmd = new ClearCmd(new AddressSet(start, end), options);cmd.applyTo(program);}diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/disassembler/ClearTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/disassembler/ClearTest.javaindex 4a07d26553..e3e785dcb0 100644--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/disassembler/ClearTest.java+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/disassembler/ClearTest.java@@ -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.@@ -51,9 +51,6 @@ import ghidra.test.TestEnv;public class ClearTest extends AbstractGhidraHeadedIntegrationTest {- private static final String COMMENTS_CHECK_BOX_TEXT =- "<html>Comments <FONT SIZE=\"2\">(does not affect automatic comments)</FONT>";-private TestEnv env;private PluginTool tool;private AddressFactory addrFactory;@@ -369,9 +366,10 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Functions", cd);turnOffOption("Registers", cd);turnOffOption("Equates", cd);@@ -397,7 +395,7 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Functions", cd);turnOffOption("Registers", cd);@@ -429,10 +427,11 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Functions", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Registers", cd);turnOffOption("Equates", cd);turnOffOption("Bookmarks", cd);@@ -484,10 +483,11 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Functions", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Registers", cd);turnOffOption("Equates", cd);turnOffOption("Bookmarks", cd);@@ -518,10 +518,11 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Functions", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Registers", cd);turnOffOption("Equates", cd);turnOffOption("Bookmarks", cd);@@ -572,7 +573,8 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);okOnClearDialog();@@ -596,12 +598,13 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Functions", cd);turnOffOption("Registers", cd);turnOffOption("Equates", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Symbols", cd);okOnClearDialog();@@ -625,12 +628,13 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Bookmarks", cd);turnOffOption("Registers", cd);turnOffOption("Equates", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Symbols", cd);okOnClearDialog();@@ -647,6 +651,76 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {}+ @Test+ public void testClearJustInstructions() throws Exception {+ Listing listing = program.getListing();++ long numInstructions = listing.getNumInstructions();+ long numDefinedData = listing.getNumDefinedData();++ assertTrue(numInstructions > 0);+ assertTrue(numDefinedData > 0);++ DockingActionIf action = getAction(tool, "Select All");+ performAction(action, cb.getProvider(), true);++ performAction(clearWithOptionsAction, cb.getProvider(), false);+ ClearDialog cd = waitForDialogComponent(ClearDialog.class);+ turnOffOption("Comments", cd);+ turnOffOption("Properties", cd);+ turnOffOption("Bookmarks", cd);+ turnOffOption("Registers", cd);+ turnOffOption("Equates", cd);+ turnOffOption("Data", cd);+ turnOffOption("Symbols", cd);+ turnOffOption("Functions", cd);++ okOnClearDialog();++ assertEquals(0, listing.getNumInstructions());+ assertEquals(numDefinedData, listing.getNumDefinedData());++ undo(program);+ assertEquals(numInstructions, listing.getNumInstructions());+ assertEquals(numDefinedData, listing.getNumDefinedData());++ }++ @Test+ public void testClearJustDefinedData() throws Exception {+ Listing listing = program.getListing();++ long numInstructions = listing.getNumInstructions();+ long numDefinedData = listing.getNumDefinedData();++ assertTrue(numInstructions > 0);+ assertTrue(numDefinedData > 0);++ DockingActionIf action = getAction(tool, "Select All");+ performAction(action, cb.getProvider(), true);++ performAction(clearWithOptionsAction, cb.getProvider(), false);+ ClearDialog cd = waitForDialogComponent(ClearDialog.class);+ turnOffOption("Comments", cd);+ turnOffOption("Properties", cd);+ turnOffOption("Bookmarks", cd);+ turnOffOption("Registers", cd);+ turnOffOption("Equates", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Symbols", cd);+ turnOffOption("Functions", cd);++ okOnClearDialog();++ assertEquals(numInstructions, listing.getNumInstructions());+ assertEquals(0, listing.getNumDefinedData());++ undo(program);+ assertEquals(numInstructions, listing.getNumInstructions());+ assertEquals(numDefinedData, listing.getNumDefinedData());++ }+@Testpublic void testClearRegisters() throws Exception {@@ -664,12 +738,13 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Bookmarks", cd);turnOffOption("Functions", cd);turnOffOption("Equates", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Symbols", cd);okOnClearDialog();@@ -695,12 +770,13 @@ public class ClearTest extends AbstractGhidraHeadedIntegrationTest {performAction(clearWithOptionsAction, cb.getProvider(), false);ClearDialog cd = waitForDialogComponent(ClearDialog.class);- turnOffOption(COMMENTS_CHECK_BOX_TEXT, cd);+ turnOffOption("Comments", cd);turnOffOption("Properties", cd);turnOffOption("Bookmarks", cd);turnOffOption("Functions", cd);turnOffOption("Registers", cd);- turnOffOption("Code", cd);+ turnOffOption("Instructions", cd);+ turnOffOption("Data", cd);turnOffOption("Symbols", cd);okOnClearDialog();