Class CodeManager

java.lang.Object
ghidra.program.database.code.CodeManager
All Implemented Interfaces:
ErrorHandler, ManagerDB

public class CodeManager extends Object implements ErrorHandler, ManagerDB
Class to manage database tables for data and instructions.
  • Constructor Details

    • CodeManager

      public CodeManager(DBHandle handle, AddressMap addrMap, OpenMode openMode, Lock lock, TaskMonitor monitor) throws VersionException, CancelledException, IOException
      Constructs a new CodeManager for a program.
      Parameters:
      handle - handle to database
      addrMap - addressMap to convert between addresses and long values.
      openMode - either READ_ONLY, UPDATE, or UPGRADE
      lock - the program synchronization lock
      monitor - the task monitor use while upgrading.
      Throws:
      VersionException - if the database is incompatible with the current schema
      IOException - if a database io error occurs
      CancelledException - if the user cancels the upgrade operation
  • Method Details

    • setProgram

      public void setProgram(ProgramDB program)
      Set the program after all the managers have been created.
      Specified by:
      setProgram in interface ManagerDB
      Parameters:
      program - The program object that this manager belongs to.
    • programReady

      public void programReady(OpenMode openMode, int currentRevision, TaskMonitor monitor) throws IOException, CancelledException
      Description copied from interface: ManagerDB
      Callback from program made to each manager after the program has completed initialization. This method may be used by managers to perform additional upgrading which may have been deferred.
      Specified by:
      programReady in interface ManagerDB
      Parameters:
      openMode - the mode that the program is being opened.
      currentRevision - current program revision. If openMode is UPGRADE, this value reflects the pre-upgrade value.
      monitor - the task monitor to use in any upgrade operations.
      Throws:
      IOException - if a database io error occurs.
      CancelledException - if the user cancelled the operation via the task monitor.
    • activateContextLocking

      public void activateContextLocking()
    • dbError

      public void dbError(IOException e)
      Description copied from interface: ErrorHandler
      Notification that an IO exception occurred.
      Specified by:
      dbError in interface ErrorHandler
      Parameters:
      e - IOException which was cause of error
    • addInstructions

      public AddressSetView addInstructions(InstructionSet instructionSet, boolean overwrite)
      Creates a complete set of instructions. A preliminary pass will be made checking for code unit conflicts which will be marked within the instructionSet causing dependent blocks to get pruned.
      Parameters:
      instructionSet - the set of instructions to be added. All code unit conflicts will be marked within the instructionSet and associated blocks.
      overwrite - if true, overwrites existing code units.
      Returns:
      the set of addresses over which instructions were actually added to the program. This may differ from the InstructionSet address set if conflict errors occurred. Such conflict errors will be recorded within the InstructionSet and its InstructionBlocks.
    • createCodeUnit

      public Instruction createCodeUnit(Address address, InstructionPrototype prototype, MemBuffer memBuf, ProcessorContextView context, int length) throws CodeUnitInsertionException
      Creates an instruction at the specified address.
      Parameters:
      address - start address of instruction
      prototype - instruction definition object
      memBuf - the MemBuffer to use to get the bytes from memory
      context - object that has the state of all the registers.
      length - instruction byte-length (must be in the range 0..prototype.getLength()). If smaller than the prototype length it must have a value no greater than 7, otherwise an error will be thrown. A value of 0 or greater-than-or-equal the prototype length will be ignored and not impose and override length. The length value must be a multiple of the instruction alignment .
      Returns:
      the newly created instruction.
      Throws:
      CodeUnitInsertionException - thrown if the new Instruction would overlap and existing CodeUnit or the specified length is unsupported.
      IllegalArgumentException - if a negative length is specified.
    • deleteAddressRange

      public void deleteAddressRange(Address start, Address end, TaskMonitor monitor) throws CancelledException
      Removes the block of defined bytes from the listing. All necessary checks will be made by listing before this method is called, so just do the work.
      Specified by:
      deleteAddressRange in interface ManagerDB
      Parameters:
      start - the first address in the range.
      end - the last address in the range.
      monitor - the TaskMonitor that tracks progress and is used to tell if the user cancels the operation.
      Throws:
      CancelledException - if the user cancels the operation.
    • moveAddressRange

      public void moveAddressRange(Address fromAddr, Address toAddr, long length, TaskMonitor monitor) throws CancelledException
      Move a block of code from one address to a new address.

      Updates all property managers, symbols, and references.

      Specified by:
      moveAddressRange in interface ManagerDB
      Parameters:
      fromAddr - the first address in the range to be moved.
      toAddr - the address to move to.
      length - the number of addresses to move.
      monitor - the TaskMonitor that tracks progress and is used to tell if the user cancels the operation.
      Throws:
      CancelledException - if the user cancels the operation.
    • getCodeUnitAt

      public CodeUnit getCodeUnitAt(Address address)
      Returns the code unit whose min address equals the specified address.
      Parameters:
      address - the min address of the code unit to return
      Returns:
      CodeUnit the code unit at the specified address, or null if a code unit does not exist
    • getCodeUnitAfter

      public CodeUnit getCodeUnitAfter(Address addr)
      Returns the next code unit whose min address is greater than the specified address.
      Parameters:
      addr - the address to look after
      Returns:
      CodeUnit the code unit after the specified address, or null if a code unit does not exist
    • getUserDefinedProperties

      public Iterator<String> getUserDefinedProperties()
      Returns an iterator over all user-defined properties.
      Returns:
      Iterator an iterator over all user-defined properties
    • removeUserDefinedProperty

      public void removeUserDefinedProperty(String propertyName)
      Removes the user-defined property with the specified property name.
      Parameters:
      propertyName - the name of the user-defined property to remove
    • getPropertyMap

      public PropertyMap getPropertyMap(String propertyName)
      Returns the property map object that is associated with the specified property name.
      Parameters:
      propertyName - the name of the property
      Returns:
      the property map object associated to the property name
    • getCodeUnitBefore

      public CodeUnit getCodeUnitBefore(Address address)
      Returns the next code unit whose min address is closest to and less than the specified address.
      Parameters:
      address - the address to look before
      Returns:
      CodeUnit the code unit before the specified address, or null if a code unit does not exist
    • getCodeUnitContaining

      public CodeUnit getCodeUnitContaining(Address address)
      Returns the code unit whose min address is less than or equal to the specified address and whose max address is greater than or equal to the specified address.
       codeunit.minAddress() <= addr <= codeunit.maxAddress()
       
      Parameters:
      address - the address for which to find the code containing it.
      Returns:
      CodeUnit the code unit containing the specified address, or null if a code unit does not exist.
    • getCodeUnitIterator

      public CodeUnitIterator getCodeUnitIterator(String property, Address address, boolean forward)
      Get an iterator that contains the code units which have the specified property type defined. Only code units at an address greater than or equal to the specified start address will be returned by the iterator. If the start address is null then check the entire program.

      Standard property types are defined in the CodeUnit class. The property types are:

      • COMMENT_PROPERTY
      • INSTRUCTION_PROPERTY
      • DEFINED_DATA_PROPERTY
      Property types can also be user defined.
      Parameters:
      property - the name of the user defined property type or special standard name from above.
      address - the address to start the iterator, or null to iterator the entire program
      forward - true means get iterator in the forward direction
      Returns:
      a CodeUnitIterator that returns all code units from the indicated start address that have the specified property type defined.
    • getCodeUnitIterator

      public CodeUnitIterator getCodeUnitIterator(String property, AddressSetView addrSetView, boolean forward)
      Get an iterator that contains the code units which have the specified property type defined. Only code units starting within the address set specified will be returned by the iterator. If the address set is null then check the entire program.
      Standard property types are defined in the CodeUnit class. The property types are:
      • REFERENCE_PROPERTY
      • INSTRUCTION_PROPERTY
      • DEFINED_DATA_PROPERTY
      Property types can also be user defined.
      Parameters:
      property - the name of the property type, or this can be user defined.
      addrSetView - the address set to iterate, or null to iterate over the entire program
      forward - true means the iterator is in the forward direction
      Returns:
      a CodeUnitIterator that returns all code units from the indicated address set that have the specified property type defined.
    • getCommentCodeUnitIterator

      public CodeUnitIterator getCommentCodeUnitIterator(int commentType, AddressSetView set)
      Get a forward iterator over code units that have comments of the given type.
      Parameters:
      commentType - comment type defined in CodeUnit
      set - address set (null for all defined memory)
      Returns:
      code unit iterator
    • getCommentAddressIterator

      public AddressIterator getCommentAddressIterator(int commentType, AddressSetView set, boolean forward)
      Get a forward iterator over addresses that have comments of the given type.
      Parameters:
      commentType - comment type defined in CodeUnit
      set - address set (null for all defined memory)
      forward - true to iterate in the direction of increasing addresses.
      Returns:
      address iterator
    • getCommentAddressIterator

      public AddressIterator getCommentAddressIterator(AddressSetView addrSet, boolean forward)
      Get an iterator over addresses that have comments of any type.
      Parameters:
      addrSet - address set containing the comment addresses to iterate over.
      forward - true to iterate in the direction of increasing addresses.
      Returns:
      the iterator
    • getInstructionAt

      public Instruction getInstructionAt(Address address)
      Returns the instruction whose min address equals the specified address or null if the address is not the beginning address of some code unit.
      Parameters:
      address - the min address of the instruction to return
      Returns:
      the instruction at the specified address, or null if a instruction does not exist starting at the given address.
    • getDefinedDataAt

      public Data getDefinedDataAt(Address address)
      Returns the defined data whose min address equals the specified address.
      Parameters:
      address - the min address of the data defined to return
      Returns:
      the defined data at the specified address, or null if a defined data does not exist
    • getInstructionBefore

      public Instruction getInstructionBefore(Address addr)
      Returns the next instruction whose min address is closest to and less than the specified address.
      Parameters:
      addr - the address to look before
      Returns:
      the instruction before the specified address, or null if a instruction does not exist
    • getInstructionAfter

      public Instruction getInstructionAfter(Address addr)
      Returns the next instruction whose min address is greater than the specified address.
      Parameters:
      addr - the address to look after
      Returns:
      the instruction after the specified address, or null if a instruction does not exist
    • getInstructionContaining

      public Instruction getInstructionContaining(Address address, boolean usePrototypeLength)
      Returns an instruction whose min address is less than or equal to the specified address and whose max address is greater than or equal to the specified address. If usePrototypeLength==true
       instruction.getMinAddress() <= addr <= 
          instruction.getMinAddress().add(instruction.getPrototype().getLength() - 1)
       
      If usePrototypeLength==false
          instruction.getMinAddress() <= addr <= instruction.getMaxAddress()
       
      The use of the prototype length is required when guarding against memory modifications. If a length-override is present only one of the entangled instructions will be returned and is intended to simply indicate the presence of a conflict.
      Parameters:
      address - the address to be contained
      usePrototypeLength - if actual prototype length should be considered when identifying a conflict (required when checking for memory modification conflicts), otherwise code unit length is used. These lengths can vary when a length-override is in affect for an instruction.
      Returns:
      the instruction containing the specified address, or null if a instruction does not exist
    • getDataAt

      public Data getDataAt(Address address)
      Returns the data whose min address equals the specified address.
      Parameters:
      address - the min address of the data to return
      Returns:
      the data at the specified address, or null if data does not exist
    • getDataBefore

      public Data getDataBefore(Address addr)
      Returns the next data whose min address is closest to and less than the specified address.
      Parameters:
      addr - the address to look before
      Returns:
      the data before the specified address, or null if a data does not exist
    • getDataAfter

      public Data getDataAfter(Address addr)
      Returns the next data whose min address is greater than the specified address.
      Parameters:
      addr - the address to look after
      Returns:
      the data after the specified address, or null if a data does not exist
    • getDataContaining

      public Data getDataContaining(Address addr)
      Returns the data whose min address is less than or equal to the specified address and whose max address is greater than or equal to the specified address.
       data.minAddress() <= addr <= data.maxAddress()
       
      Parameters:
      addr - the address to be contained
      Returns:
      the data containing the specified address, or null if a data does not exist that starts at that address.
    • getDefinedDataAfter

      public Data getDefinedDataAfter(Address addr)
      Returns the next defined data whose min address is greater than the specified address.
      Parameters:
      addr - the address to look after
      Returns:
      the defined data after the specified address, null if a defined data does not exist
    • getDefinedDataBefore

      public Data getDefinedDataBefore(Address addr)
      Returns the next defined data whose min address is closest to and less than the specified address.
      Parameters:
      addr - the address to look before
      Returns:
      the defined data before the specified address, null if a defined data does not exist
    • getDefinedDataContaining

      public Data getDefinedDataContaining(Address addr)
      Returns the defined data whose min address is less than or equal to the specified address and whose max address is greater than or equal to the specified address.
       data.minAddress() <= addr <= data.maxAddress()
       
      Parameters:
      addr - the address to be contained
      Returns:
      the defined data containing the address, null if a defined data does not exist
    • getUndefinedRanges

      public AddressSetView getUndefinedRanges(AddressSetView set, boolean initializedMemoryOnly, TaskMonitor monitor) throws CancelledException
      Throws:
      CancelledException
    • getUndefinedAt

      public Data getUndefinedAt(Address address)
      Returns the undefined data whose min address equals the specified address.
      Parameters:
      address - the min address of the undefined data to return
      Returns:
      Data the undefined data at the address, null if undefined data does not exist
    • getFirstUndefinedDataAfter

      public Data getFirstUndefinedDataAfter(Address addr, TaskMonitor monitor)
      Returns the next undefined data whose min address is greater than the specified address.
      Parameters:
      addr - the address to look after
      monitor - the task monitor
      Returns:
      Data the undefined data after the address, null if a undefined data does not exist
    • getFirstUndefinedData

      public Data getFirstUndefinedData(AddressSetView set, TaskMonitor monitor)
      Returns the next undefined data whose min address falls within the address set searching in the forward direction (e.g., 0 -> 0xfff).
      Parameters:
      set - the address set to look within (required).
      monitor - the current monitor.
      Returns:
      the first undefined data within the address set, or null if there is none.
    • getFirstUndefinedDataBefore

      public Data getFirstUndefinedDataBefore(Address addr, TaskMonitor monitor)
      Returns the next undefined data whose min address is closest to and less than the specified address.
      Parameters:
      addr - the address to look before
      monitor - the task monitor
      Returns:
      the undefined data before the address, null if a undefined data does not exist
    • createCodeUnit

      public Data createCodeUnit(Address addr, DataType dataType, int length) throws CodeUnitInsertionException
      Creates a data at the specified address.
      Parameters:
      addr - Starting address of code unit
      dataType - data prototype for the code unit
      length - the data length
      Returns:
      the data
      Throws:
      CodeUnitInsertionException - if the code unit overlaps with an existing code unit
    • updateDataReferences

      public void updateDataReferences(Data data)
      Update the data references on this data item. Get rid of any references first, then add in any new ones.
      Parameters:
      data - the data object to be updated
    • clearComments

      public void clearComments(Address start, Address end)
      Clears all comments in the given range (inclusive). The specified start and end addresses must form a valid range within a single AddressSpace.
      Parameters:
      start - the start address of the range to clear
      end - the end address of the range to clear
    • clearProperties

      public void clearProperties(Address start, Address end, TaskMonitor monitor) throws CancelledException
      Clears the properties in the given range (inclusive). The specified start and end addresses must form a valid range within a single AddressSpace.
      Parameters:
      start - the start address of the range to clear
      end - the end address of the range to clear
      monitor - the task monitor
      Throws:
      CancelledException - if cancelled
    • clearCodeUnits

      public void clearCodeUnits(Address start, Address end, boolean clearContext, TaskMonitor monitor) throws CancelledException
      Remove code units, symbols, equates, and references to code units in the given range (inclusive). Comments and comment history will be retained. The specified start and end addresses must form a valid range within a single AddressSpace.
      Parameters:
      start - the start address of the range to clear
      end - the end address of the range to clear
      clearContext - if true all context-register values will be cleared over range
      monitor - the task monitor
      Throws:
      CancelledException - if cancelled
    • clearAll

      public void clearAll(boolean clearContext, TaskMonitor monitor)
      Clear all code units in the program.
      Parameters:
      clearContext - true to clear the context
      monitor - the task monitor
    • getNumInstructions

      public int getNumInstructions()
      Returns the number of instructions in the program.
      Returns:
      the number of instructions in the program.
    • getNumDefinedData

      public int getNumDefinedData()
      Returns the number of defined data in the program.
      Returns:
      the number of defined data in the program.
    • getCompositeData

      public DataIterator getCompositeData(Address start, boolean forward)
      Returns a composite data iterator beginning at the specified start address.
      Parameters:
      start - the address to begin iterator
      forward - true means get iterator in forward direction
      Returns:
      the composite data iterator
    • getCompositeData

      public DataIterator getCompositeData(AddressSetView addrSet, boolean forward)
      Returns a composite data iterator limited to the addresses in the specified address set.
      Parameters:
      addrSet - the address set to limit the iterator
      forward - determines if the iterator will go from the lowest address to the highest or the other way around.
      Returns:
      DataIterator the composite data iterator
    • getCodeUnits

      public CodeUnitIterator getCodeUnits(Address start, boolean forward)
      Returns an iterator over all codeUnits in the program from the given start address to either the end address or the start address, depending if the iterator is forward or not.
      Parameters:
      start - the starting address for the iterator.
      forward - if true the iterator returns all codeUnits from the given start address to the end of the program, otherwise it returns all codeUnits from the given start address to the start of the program.
      Returns:
      code unit iterator
    • getCodeUnits

      public CodeUnitIterator getCodeUnits(AddressSetView set, boolean forward)
      Returns an iterator over all codeUnits in the given addressSet. The iterator will go from the lowest address to the largest or from the largest to the lowest depending on the forward parameter.
      Parameters:
      set - the memory address set over which code units should be iterated (required)
      forward - determines if the iterator goes from lowest address to highest or the other way around.
      Returns:
      code unit iterator
    • getInstructions

      public InstructionIterator getInstructions(Address address, boolean forward)
      Returns an iterator over all instructions in the program from the given start address to either the end address or the start address, depending if the iterator is forward or not.
      Parameters:
      address - the starting address for the iterator.
      forward - if true the iterator returns all instructions from the given start address to the end of the program, otherwise it returns all instructions from the given start address to the start of the program.
      Returns:
      the iterator
    • getDefinedData

      public DataIterator getDefinedData(Address address, boolean forward)
      Returns an iterator over all defined data in the program from the given start address to either the end address or the start address, depending if the iterator is forward or not.
      Parameters:
      address - the starting address for the iterator.
      forward - if true the iterator returns all defined data from the given start address to the end of the program, otherwise it returns all defined data from the given start address to the start of the program.
      Returns:
      the iterator
    • getInstructions

      public InstructionIterator getInstructions(AddressSetView set, boolean forward)
      Returns an iterator over all instructions in the given addressSet. The iterator will go from the lowest address to the largest or from the largest to the lowest depending on the forward parameter.
      Parameters:
      set - restrict the returned instructions to these addresses
      forward - determines if the iterator goes from lowest address to highest or the other way around.
      Returns:
      the iterator
    • getData

      public DataIterator getData(Address start, boolean forward)
      Returns an iterator over all data in the program from the given start address to either the end address or the start address, depending if the iterator is forward or not.
      Parameters:
      start - the starting address for the iterator.
      forward - if true the iterator returns all data from the given start address to the end of the program, otherwise it returns all data from the given start address to the start of the program.
      Returns:
      the iterator
    • getData

      public DataIterator getData(AddressSetView set, boolean forward)
      Returns an iterator over all data in the given addressSet. The iterator will go from the lowest address to the largest or from the largest to the lowest depending on the forward parameter.
      Parameters:
      set - restrict the returned instructions to these addresses
      forward - determines if the iterator goes from lowest address to highest or the other way around.
      Returns:
      the iterator
    • getDefinedData

      public DataIterator getDefinedData(AddressSetView set, boolean forward)
      Returns an iterator over all defined data in the given addressSet. The iterator will go from the lowest address to the largest or from the largest to the lowest depending on the forward parameter.
      Parameters:
      set - restrict the returned instructions to these addresses
      forward - determines if the iterator goes from lowest address to highest or the other way around.
      Returns:
      the iterator
    • checkContextWrite

      public void checkContextWrite(Address start, Address end) throws ContextChangeException
      Check if any instruction intersects the specified address range. The specified start and end addresses must form a valid range within a single AddressSpace.
      Parameters:
      start - start of range
      end - end of range
      Throws:
      ContextChangeException - if there is a context register change conflict
    • isUndefined

      public boolean isUndefined(Address start, Address end)
      Checks if all the addresses from start to end have undefined data.
      Parameters:
      start - the first address in the range to check.
      end - the last address in the range to check.
      Returns:
      true if all the addresses in the range have undefined data.
    • isUndefined

      protected boolean isUndefined(Address address, long addr)
    • clearData

      public void clearData(Set<Long> dataTypeIDs, TaskMonitor monitor) throws CancelledException
      Removes any data objects that have dataTypes matching the given dataType ids.
      Parameters:
      dataTypeIDs - the set of DataType IDs that have been deleted.
      monitor - the task monitor.
      Throws:
      CancelledException - if cancelled
    • getReferenceMgr

      public ReferenceManager getReferenceMgr()
      Returns the reference manager being used by this code manager.
      Returns:
      ReferenceManager the reference manager being used by this code manager
    • getInstructionRecord

      protected DBRecord getInstructionRecord(long addr)
    • invalidateCache

      public void invalidateCache(boolean all)
      Description copied from interface: ManagerDB
      Clears all data caches.
      Specified by:
      invalidateCache in interface ManagerDB
      Parameters:
      all - if false, some managers may not need to update their cache if they can tell that its not necessary. If this flag is true, then all managers should clear their cache no matter what.
    • invalidateCodeUnitCache

      public void invalidateCodeUnitCache()
      Invalidates the cache for the code units.
    • memoryChanged

      public void memoryChanged(Address addr, Address end)
      Notification that memory has changed, so clear the cache for the affected code units.
      Parameters:
      addr - start of change
      end - end address of change
    • fallThroughChanged

      public void fallThroughChanged(Address fromAddr, Reference newFallThroughRef)
      Callback from ReferenceManager when a new fall-through reference is set.
      Parameters:
      fromAddr - fall-through from location
      newFallThroughRef - new fallthrough reference or null if removed
    • getComment

      public String getComment(int commentType, Address address)
      Get the comment for the given type at the specified address.
      Parameters:
      commentType - either EOL_COMMENT, PRE_COMMENT, POST_COMMENT, PLATE_COMMENT, or REPEATABLE_COMMENT
      address - the address of the comment.
      Returns:
      the comment string of the appropriate type or null if no comment of that type exists for this code unit
      Throws:
      IllegalArgumentException - if type is not one of the types of comments supported
    • setComment

      public void setComment(Address address, int commentType, String comment)
      Set the comment for the given comment type at the specified address.
      Parameters:
      address - the address of the comment.
      commentType - either EOL_COMMENT, PRE_COMMENT, POST_COMMENT, PLATE_COMMENT, or REPEATABLE_COMMENT
      comment - comment to set at the address
      Throws:
      IllegalArgumentException - if type is not one of the types of comments supported
    • getCommentHistory

      public CommentHistory[] getCommentHistory(Address addr, int commentType)
      Get the comment history for the comment type at the given address
      Parameters:
      addr - address for the comment history
      commentType - comment type
      Returns:
      zero length array if no history exists
    • replaceDataTypes

      public void replaceDataTypes(Map<Long,Long> dataTypeReplacementMap)
    • reDisassembleAllInstructions

      public void reDisassembleAllInstructions(TaskMonitor monitor) throws IOException, CancelledException
      Complete language transformation of all instructions. All existing prototypes will be discarded and all instructions re-disassembled following flow and adjusting context as needed. Instructions which fail to re-disassemble will be marked - since only one byte will be skipped, such bad instruction disassembly may cause subsequent errors due to possible instruction shift.

      This method is only intended for use by the ProgramDB setLanguage method which must ensure that the context has been properly initialized.

      Parameters:
      monitor - task monitor
      Throws:
      IOException - if IO error occurs
      CancelledException - if the operation is canceled.