Merge remote-tracking branch 'origin/GT-3078_ghizard_use_SetCommentCmd_over_SetCommentsCmd'

This commit is contained in:
Ryan Kurtz 2019-08-13 13:31:42 -04:00
commit 6b49a80dde
8 changed files with 105 additions and 156 deletions

View file

@ -75,8 +75,7 @@ public class SetCommentCmd implements Command {
public boolean applyTo(DomainObject obj) {
CodeUnit cu = getCodeUnit((Program) obj);
if (cu == null) {
message =
"No Instruction or Data found for address " + address.toString() +
message = "No Instruction or Data found for address " + address.toString() +
" Is this address valid?";
return false;
}
@ -113,4 +112,21 @@ public class SetCommentCmd implements Command {
return message;
}
/**
* Creates the specified comment of the specified type at address. The current comment of
* this commentType will be cleared.
*
* @param program the program being analyzed
* @param addr the address where data is created
* @param comment the comment about the data
* @param commentType the type of comment ({@link CodeUnit#PLATE_COMMENT},
* {@link CodeUnit#PRE_COMMENT}, {@link CodeUnit#EOL_COMMENT}, {@link CodeUnit#POST_COMMENT},
* {@link CodeUnit#REPEATABLE_COMMENT})
*/
public static void createComment(Program program, Address addr, String comment,
int commentType) {
SetCommentCmd commentCmd = new SetCommentCmd(addr, commentType, comment);
commentCmd.applyTo(program);
}
}

View file

@ -65,8 +65,8 @@ public class SetCommentsCmd implements Command {
/**
* return true if the newValue and oldValue are different
* @param newValue
* @param oldValue
* @param newValue the value that we desire to set
* @param oldValue the existing value
* @return boolean
*/
private boolean commentChanged(String newValue, String oldValue) {
@ -141,40 +141,4 @@ public class SetCommentsCmd implements Command {
return msg;
}
/**
* Creates the specified comment of the specified type at address.
*
* @param program the program being analyzed
* @param addr the address where data is created
* @param comment the comment about the data
* @param commentType the type of comment ({@link CodeUnit#PLATE_COMMENT},
* {@link CodeUnit#PRE_COMMENT}, {@link CodeUnit#EOL_COMMENT}, {@link CodeUnit#POST_COMMENT},
* {@link CodeUnit#REPEATABLE_COMMENT})
*/
public static void createComment(Program program, Address addr, String comment,
int commentType) {
SetCommentsCmd commentCmd = null;
switch (commentType) {
case CodeUnit.PRE_COMMENT:
commentCmd = new SetCommentsCmd(addr, comment, null, null, null, null);
break;
case CodeUnit.POST_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, comment, null, null, null);
break;
case CodeUnit.EOL_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, null, comment, null, null);
break;
case CodeUnit.PLATE_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, null, null, comment, null);
break;
case CodeUnit.REPEATABLE_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, null, null, null, comment);
break;
default:
commentCmd = new SetCommentsCmd(addr, null, null, comment, null, null);
}
commentCmd.applyTo(program);
}
}

View file

@ -15,6 +15,10 @@
*/
package ghidra.app.plugin.core.comments;
import java.awt.event.KeyEvent;
import docking.ActionContext;
import docking.action.*;
import ghidra.app.CorePluginPackage;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.cmd.comments.SetCommentsCmd;
@ -30,11 +34,6 @@ import ghidra.program.model.listing.Program;
import ghidra.program.util.*;
import ghidra.util.HelpLocation;
import java.awt.event.KeyEvent;
import docking.ActionContext;
import docking.action.*;
/**
* Class to handle end comments for a code unit in a program.
*/
@ -88,8 +87,8 @@ public class CommentsPlugin extends Plugin implements OptionsChangeListener {
HelpLocation helpLocation = new HelpLocation(getName(), "Comments_Option");
options.setOptionsHelpLocation(helpLocation);
options.registerOption(OPTION_NAME, dialog.getEnterMode(), helpLocation,
"Toggle for whether pressing the <Enter> key causes the comment to be entered,"
+ " versus adding a new line character in the comment.");
"Toggle for whether pressing the <Enter> key causes the comment to be entered," +
" versus adding a new line character in the comment.");
setOptions(options);
options.addOptionsChangeListener(this);
@ -115,15 +114,16 @@ public class CommentsPlugin extends Plugin implements OptionsChangeListener {
plateComment = (plateComment.length() == 0) ? null : plateComment;
repeatableComment = (repeatableComment.length() == 0) ? null : repeatableComment;
Command cmd =
new SetCommentsCmd(cu.getMinAddress(), preComment, postComment, eolComment,
Command cmd = new SetCommentsCmd(cu.getMinAddress(), preComment, postComment, eolComment,
plateComment, repeatableComment);
tool.execute(cmd, cu.getProgram());
}
/**
* Called by the deleteAction to delete a comment.
* Called by the deleteAction to delete an end-of-line comment.
* @param program the {@link Program} for which to act
* @param loc the {@link ProgramLocation} for which to delete the comment
*/
void deleteComments(Program program, ProgramLocation loc) {
int commentType = CommentType.getCommentType(null, loc, CodeUnit.EOL_COMMENT);
@ -147,29 +147,24 @@ public class CommentsPlugin extends Plugin implements OptionsChangeListener {
editAction = CommentsActionFactory.getEditCommentsAction(dialog, name);
tool.addAction(editAction);
preCommentEditAction =
CommentsActionFactory.getSetCommentsAction(dialog, name, "Set Pre Comment",
CodeUnit.PRE_COMMENT);
preCommentEditAction = CommentsActionFactory.getSetCommentsAction(dialog, name,
"Set Pre Comment", CodeUnit.PRE_COMMENT);
tool.addAction(preCommentEditAction);
postCommentEditAction =
CommentsActionFactory.getSetCommentsAction(dialog, name, "Set Post Comment",
CodeUnit.POST_COMMENT);
postCommentEditAction = CommentsActionFactory.getSetCommentsAction(dialog, name,
"Set Post Comment", CodeUnit.POST_COMMENT);
tool.addAction(postCommentEditAction);
plateCommentEditAction =
CommentsActionFactory.getSetCommentsAction(dialog, name, "Set Plate Comment",
CodeUnit.PLATE_COMMENT);
plateCommentEditAction = CommentsActionFactory.getSetCommentsAction(dialog, name,
"Set Plate Comment", CodeUnit.PLATE_COMMENT);
tool.addAction(plateCommentEditAction);
eolCommentEditAction =
CommentsActionFactory.getSetCommentsAction(dialog, name, "Set EOL Comment",
CodeUnit.EOL_COMMENT);
eolCommentEditAction = CommentsActionFactory.getSetCommentsAction(dialog, name,
"Set EOL Comment", CodeUnit.EOL_COMMENT);
tool.addAction(eolCommentEditAction);
repeatableCommentEditAction =
CommentsActionFactory.getSetCommentsAction(dialog, name, "Set Repeatable Comment",
CodeUnit.REPEATABLE_COMMENT);
repeatableCommentEditAction = CommentsActionFactory.getSetCommentsAction(dialog, name,
"Set Repeatable Comment", CodeUnit.REPEATABLE_COMMENT);
tool.addAction(repeatableCommentEditAction);
deleteAction = new ListingContextAction("Delete Comments", pluginName) {

View file

@ -15,7 +15,7 @@
*/
package ghidra.app.plugin.exceptionhandlers.gcc;
import ghidra.app.cmd.comments.SetCommentsCmd;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.cmd.data.CreateDataCmd;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.*;
@ -59,7 +59,7 @@ public abstract class GccAnalysisClass {
* @param program the program being analyzed
*/
protected void init(Program program) {
initPointerInfo(program);
initPointerInfo();
dwordDT = new DWordDataType();
}
@ -67,10 +67,8 @@ public abstract class GccAnalysisClass {
* Method that initializes information about the
* program's pointer size and creates an appropriate
* pointer data type (i.e. ptrDT)
*
* @param program the program being analyzed
*/
private void initPointerInfo(Program program) {
private void initPointerInfo() {
ptrDT = PointerDataType.getPointer(null, -1);
}
@ -102,7 +100,7 @@ public abstract class GccAnalysisClass {
protected static void createAndCommentData(Program program, Address addr, DataType dt,
String comment, int commentType) {
createData(program, addr, dt);
SetCommentsCmd.createComment(program, addr, comment, commentType);
SetCommentCmd.createComment(program, addr, comment, commentType);
}
}

View file

@ -1,6 +1,5 @@
/* ###
* 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.
@ -20,7 +19,6 @@ import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.cmd.comments.SetCommentsCmd;
import ghidra.app.cmd.data.CreateArrayCmd;
import ghidra.app.plugin.exceptionhandlers.gcc.*;
import ghidra.app.plugin.exceptionhandlers.gcc.datatype.*;
@ -213,8 +211,8 @@ public class Cie extends GccAnalysisClass {
createAndCommentData(program, addr, new StringDataType(), comment, CodeUnit.EOL_COMMENT);
Data dataAt = program.getListing().getDataAt(addr);
if (dataAt == null) {
throw new ExceptionHandlerFrameException("Couldn't process augmentation string @ " +
addr + ".");
throw new ExceptionHandlerFrameException(
"Couldn't process augmentation string @ " + addr + ".");
}
augmentationString = (String) dataAt.getValue();
curSize += augmentationString.length() + 1; // Add 1 for the NUL byte
@ -435,14 +433,14 @@ public class Cie extends GccAnalysisClass {
*/
private Address processInitialInstructions(Address addr) throws MemoryAccessException {
CreateArrayCmd arrayCmd = null;
SetCommentsCmd commentCmd = null;
// Create initial instructions array with remaining bytes
initialInstructionCount = intLength - curSize;
arrayCmd = new CreateArrayCmd(addr, initialInstructionCount, new ByteDataType(), BYTE_LEN);
commentCmd = new SetCommentsCmd(addr, null, null, "(CIE) Initial Instructions", null, null);
arrayCmd.applyTo(program);
commentCmd.applyTo(program);
SetCommentCmd.createComment(program, addr, "(CIE) Initial Instructions",
CodeUnit.EOL_COMMENT);
initialInstructions = new byte[initialInstructionCount];
int numBytesRead = program.getMemory().getBytes(addr, initialInstructions);
@ -469,8 +467,8 @@ public class Cie extends GccAnalysisClass {
* @throws MemoryAccessException if memory for the CIE couldn't be read.
* @throws ExceptionHandlerFrameException if some of the CIE information couldn't be created.
*/
public void create(Address cieAddress) throws MemoryAccessException,
ExceptionHandlerFrameException {
public void create(Address cieAddress)
throws MemoryAccessException, ExceptionHandlerFrameException {
if (cieAddress == null || monitor.isCancelled()) {
return;
@ -605,9 +603,8 @@ public class Cie extends GccAnalysisClass {
case 'P':
DwarfEHDecoder personalityDecoder =
processPersonalityEncoding(augmentationDataAddr, augmentationDataIndex,
augData);
DwarfEHDecoder personalityDecoder = processPersonalityEncoding(
augmentationDataAddr, augmentationDataIndex, augData);
augmentationDataIndex++;
DwarfDecodeContext personalityDecodeContext =
@ -669,8 +666,9 @@ public class Cie extends GccAnalysisClass {
DataType prnsFuncPtrDt = personalityDecoder.getDataType(program);
createAndCommentData(program, augmentationDataAddr.add(augmentationDataIndex),
prnsFuncPtrDt, "(CIE Augmentation Data) Personality Function Pointer (" +
personalityFuncAddr + ")", CodeUnit.EOL_COMMENT);
prnsFuncPtrDt,
"(CIE Augmentation Data) Personality Function Pointer (" + personalityFuncAddr + ")",
CodeUnit.EOL_COMMENT);
program.getReferenceManager().addMemoryReference(
augmentationDataAddr.add(augmentationDataIndex), personalityFuncAddr, RefType.DATA,

View file

@ -18,7 +18,7 @@
*/
package ghidra.app.plugin.exceptionhandlers.gcc.structures.ehFrame;
import ghidra.app.cmd.comments.SetCommentsCmd;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.plugin.exceptionhandlers.gcc.GccAnalysisUtils;
import ghidra.program.model.address.Address;
import ghidra.program.model.listing.CodeUnit;
@ -187,8 +187,8 @@ public class DwarfCallFrameOpcodeParser {
operand2Len = GccAnalysisUtils.getULEB128Length(program, curr);
curr = curr.add(operand2Len);
sb.append("DW_CFA_offset_extended reg[" + operand1 + "] reg[" + operand2 +
"]");
sb.append(
"DW_CFA_offset_extended reg[" + operand1 + "] reg[" + operand2 + "]");
break;
case DW_CFA_restore_extended:
@ -370,35 +370,10 @@ public class DwarfCallFrameOpcodeParser {
}
}
setComment(instrAddr, sb.toString(), CodeUnit.EOL_COMMENT);
SetCommentCmd.createComment(program, instrAddr, sb.toString(), CodeUnit.EOL_COMMENT);
Msg.info(this, sb.toString());
}
}
private void setComment(Address addr, String comment, int commentType) {
SetCommentsCmd commentCmd = null;
switch (commentType) {
case CodeUnit.PRE_COMMENT:
commentCmd = new SetCommentsCmd(addr, comment, null, null, null, null);
break;
case CodeUnit.POST_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, comment, null, null, null);
break;
case CodeUnit.EOL_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, null, comment, null, null);
break;
case CodeUnit.PLATE_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, null, null, comment, null);
break;
case CodeUnit.REPEATABLE_COMMENT:
commentCmd = new SetCommentsCmd(addr, null, null, null, null, comment);
break;
default:
commentCmd = new SetCommentsCmd(addr, null, null, comment, null, null);
}
commentCmd.applyTo(program);
}
}

View file

@ -16,7 +16,6 @@
package ghidra.app.plugin.exceptionhandlers.gcc.structures.ehFrame;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.cmd.comments.SetCommentsCmd;
import ghidra.app.cmd.data.CreateArrayCmd;
import ghidra.app.cmd.function.CreateFunctionCmd;
import ghidra.app.plugin.exceptionhandlers.gcc.*;
@ -113,7 +112,7 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
* @param monitor a status monitor for tracking progress and allowing cancelling when creating
* an FDE.
* @param program the program where this will create an FDE.
* @param cie the call frame information entry for this FDE.
* @param cieSource the call frame information entry for this FDE.
*/
public FrameDescriptionEntry(TaskMonitor monitor, Program program, CieSource cieSource) {
super(monitor, program);
@ -157,8 +156,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
case 8:
return new QWordDataType();
default:
throw new IllegalArgumentException("Unhandled pointer size -- " +
pointerDecodeSize + " bytes");
throw new IllegalArgumentException(
"Unhandled pointer size -- " + pointerDecodeSize + " bytes");
}
}
@ -190,8 +189,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
* @throws MemoryAccessException if the required memory can't be read
* @throws ExceptionHandlerFrameException if there is an error creating the information.
*/
private Address createCiePointer(Address addr) throws MemoryAccessException,
ExceptionHandlerFrameException {
private Address createCiePointer(Address addr)
throws MemoryAccessException, ExceptionHandlerFrameException {
/*
* Create a new CIE Pointer field at the specified address and sets an
* appropriate comment for the new structure.
@ -210,16 +209,16 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
if (isInDebugFrame(addr)) {
if (intPtr == -1) {
throw new ExceptionHandlerFrameException("Invalid CIE Reference Pointer (0x" +
Integer.toHexString(intPtr) + ")");
throw new ExceptionHandlerFrameException(
"Invalid CIE Reference Pointer (0x" + Integer.toHexString(intPtr) + ")");
}
cieAddr = addr.getNewAddress(intPtr); // absolute ref
}
else {
if (intPtr == 0) {
throw new ExceptionHandlerFrameException("Invalid CIE Reference Pointer (0x" +
Integer.toHexString(intPtr) + ")");
throw new ExceptionHandlerFrameException(
"Invalid CIE Reference Pointer (0x" + Integer.toHexString(intPtr) + ")");
}
cieAddr = addr.subtract(intPtr); // relative ref
}
@ -282,8 +281,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
* @throws ExceptionHandlerFrameException if there is an error creating the information.
* @throws MemoryAccessException if the required memory can't be read
*/
private Address createPcRange(Address addr) throws ExceptionHandlerFrameException,
MemoryAccessException {
private Address createPcRange(Address addr)
throws ExceptionHandlerFrameException, MemoryAccessException {
/*
* Create a new pcRange field at the specified address
@ -362,14 +361,11 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
* @throws MemoryAccessException if the required memory can't be read
*/
private Address createAugmentationData(Address addr) throws MemoryAccessException {
SetCommentsCmd commentCmd = null;
/*
* Create a new Augmentation Data field at the specified address
* and sets an appropriate comment for the new structure.
*/
commentCmd = new SetCommentsCmd(addr, null, null, "(FDE) Augmentation Data", null, null);
commentCmd.applyTo(program);
SetCommentCmd.createComment(program, addr, "(FDE) Augmentation Data", CodeUnit.EOL_COMMENT);
this.augmentationData = new byte[intAugmentationDataLength];
program.getMemory().getBytes(addr, augmentationData);
@ -387,16 +383,14 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
*/
private Address createCallFrameInstructions(Address addr) throws MemoryAccessException {
CreateArrayCmd arrayCmd = null;
SetCommentsCmd commentCmd = null;
// Create initial instructions array with remaining bytes.
int instructionLength = intLength - curSize;
arrayCmd = new CreateArrayCmd(addr, instructionLength, new ByteDataType(), BYTE_LEN);
arrayCmd.applyTo(program);
commentCmd =
new SetCommentsCmd(addr, null, null, "(FDE) Call Frame Instructions", null, null);
commentCmd.applyTo(program);
SetCommentCmd.createComment(program, addr, "(FDE) Call Frame Instructions",
CodeUnit.EOL_COMMENT);
callFrameInstructions = new byte[instructionLength];
program.getMemory().getBytes(addr, callFrameInstructions);
@ -428,8 +422,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
* @throws MemoryAccessException if memory for the FDE or its associated data can't be accessed
* @throws ExceptionHandlerFrameException if there is an error creating the FDE information.
*/
public RegionDescriptor create(Address fdeBaseAddress) throws MemoryAccessException,
ExceptionHandlerFrameException {
public RegionDescriptor create(Address fdeBaseAddress)
throws MemoryAccessException, ExceptionHandlerFrameException {
if (fdeBaseAddress == null || monitor.isCancelled()) {
return null;
@ -464,8 +458,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
createFuncCmd.applyTo(program);
}
catch (AddressOutOfBoundsException e) {
throw new ExceptionHandlerFrameException(e.getMessage() + ": " +
pcBeginAddr.toString() + " + " + intPcRange);
throw new ExceptionHandlerFrameException(
e.getMessage() + ": " + pcBeginAddr.toString() + " + " + intPcRange);
}
// If some FDE data remains, then it is the augmentation fields or call frame instructions.
@ -572,9 +566,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
DwarfEHDecoder decoder = cie.getLSDADecoder();
DwarfDecodeContext ctx =
new DwarfDecodeContext(program, augmentationDataAddr, region.getEHMemoryBlock()
.getStart());
DwarfDecodeContext ctx = new DwarfDecodeContext(program, augmentationDataAddr,
region.getEHMemoryBlock().getStart());
Address potentialAugmentationDataExAddr = decoder.decodeAddress(ctx);
@ -588,8 +581,7 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
try {
String label =
"eh_augmentation_" + pcBeginAddr + ".." + pcEndAddr + "_" +
String label = "eh_augmentation_" + pcBeginAddr + ".." + pcEndAddr + "_" +
augmentationDataExAddr;
program.getSymbolTable().createLabel(augmentationDataExAddr, label,
@ -600,9 +592,8 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
}
}
else {
CreateArrayCmd arrayCmd =
new CreateArrayCmd(augmentationDataAddr, intAugmentationDataLength,
new ByteDataType(), BYTE_LEN);
CreateArrayCmd arrayCmd = new CreateArrayCmd(augmentationDataAddr,
intAugmentationDataLength, new ByteDataType(), BYTE_LEN);
arrayCmd.applyTo(program);
}
}
@ -635,8 +626,7 @@ public class FrameDescriptionEntry extends GccAnalysisClass {
if (!program.getMemory().getAllInitializedAddressSet().contains(lsdaAddr)) {
String errorMessage =
"Can't create LSDA data @ " + lsdaAddr +
String errorMessage = "Can't create LSDA data @ " + lsdaAddr +
". The address is not in the program's initialized memory! CIE @ " +
cie.getAddress() + " FDE @ " + baseAddress;

View file

@ -15,7 +15,7 @@
*/
package ghidra.app.util.bin.format.pdb;
import ghidra.app.cmd.comments.SetCommentsCmd;
import ghidra.app.cmd.comments.SetCommentCmd;
import ghidra.app.util.PseudoDisassembler;
import ghidra.app.util.PseudoInstruction;
import ghidra.program.model.address.Address;
@ -29,6 +29,9 @@ final class PdbUtil {
/**
* Returns an address using the relative offset + image base.
* @param program the {@link Program} for which to act
* @param relativeOffset the relative offset
* @return the calculated {@link Address}
*/
final static Address reladdr(Program program, int relativeOffset) {
return reladdr(program, relativeOffset & Conv.INT_MASK);
@ -36,6 +39,9 @@ final class PdbUtil {
/**
* Returns an address using the relative offset + image base.
* @param program the {@link Program} for which to act
* @param relativeOffset the relative offset
* @return the calculated {@link Address}
*/
final static Address reladdr(Program program, long relativeOffset) {
return program.getImageBase().add(relativeOffset);
@ -59,13 +65,18 @@ final class PdbUtil {
text = comment + "\n" + text;
}
SetCommentsCmd.createComment(program, address, text, commentType);
SetCommentCmd.createComment(program, address, text, commentType);
}
/**
* Returns true is this symbol represents a function.
* For example, "FunctionName@4" or "MyFunction@22".
* @param program the {@link Program} for which to check
* @param symbol the symbol to check
* @param addr {@link Address} of the symbol
* @param length the length for the check
* @return {@code true} upon success
*/
final static boolean isFunction(Program program, String symbol, Address addr, int length) {
int atpos = symbol.lastIndexOf('@');
@ -143,6 +154,8 @@ final class PdbUtil {
* ...
* 23rd pass
* etc.
* @param pass the number value of the pass to make pretty
* @return the string result
*/
final static String getPass(int pass) {
if (pass > 20) {