Java Code Examples for ghidra.util.task.TaskMonitor#getProgress()

The following examples show how to use ghidra.util.task.TaskMonitor#getProgress() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GTreeExpandAllTask.java    From ghidra with Apache License 2.0 6 votes vote down vote up
protected void expandNode(GTreeNode parent, TaskMonitor monitor) throws CancelledException {
	// only expand MAX number of nodes.
	if (monitor.getProgress() >= MAX) {
		return;
	}
	if (parent.isLeaf()) {
		return;
	}
	monitor.checkCanceled();
	List<GTreeNode> allChildren = parent.getChildren();
	if (allChildren.size() == 0) {
		return;
	}
	TreePath treePath = parent.getTreePath();
	if (!jTree.isExpanded(treePath)) {
		expandPath(treePath, monitor);
	}
	for (GTreeNode child : allChildren) {
		monitor.checkCanceled();
		expandNode(child, monitor);
	}
	monitor.incrementProgress(1);
}
 
Example 2
Source File: GTreeLoadChildrenTask.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void run(TaskMonitor monitor) {
	long progressValue = monitor.getProgress();
	long maxValue = monitor.getMaximum();
	monitor.setMessage("Loading children");
	try {
		node.setChildren(node.generateChildren(monitor));
	}
	catch (CancelledException e) {
		if (!tree.isDisposed()) {
			runOnSwingThread(new Runnable() {
				@Override
				public void run() {
					tree.collapseAll(tree.getViewRoot());
				}
			});
		}
		node.unloadChildren();
	}
	finally {
		monitor.initialize(maxValue);
		monitor.setProgress(progressValue);

	}
}
 
Example 3
Source File: SelectByScopedFlowPlugin.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private List<CodeBlockVertex> createVertices(Function function, Program program,
		TaskMonitor monitor) throws CancelledException {
	List<CodeBlockVertex> vertices = new ArrayList<>();
	CodeBlockModel blockModel = new BasicBlockModel(program);

	AddressSetView addresses = function.getBody();
	CodeBlockIterator iterator = blockModel.getCodeBlocksContaining(addresses, monitor);
	monitor.initialize(addresses.getNumAddresses());

	for (; iterator.hasNext();) {
		monitor.checkCanceled();
		CodeBlock codeBlock = iterator.next();
		CodeBlockVertex vertex = new CodeBlockVertex(codeBlock);
		vertices.add(vertex);

		long blockAddressCount = codeBlock.getNumAddresses();
		long currentProgress = monitor.getProgress();
		monitor.setProgress(currentProgress + blockAddressCount);
	}

	return vertices;
}
 
Example 4
Source File: FunctionTagMerger.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Displays a conflict resolution panel for each conflict discovered during
 * {@link #autoMerge()}.
 * 
 * @param monitor the task monitor
 * @throws CancelledException
 */
private void handleConflicts(TaskMonitor monitor) throws CancelledException {
	monitor.setMessage("Resolving Function Tag conflicts");
	boolean askUser = (conflictOption == ASK_USER);

	int totalConflicts = tagConflicts.size();
	monitor.initialize(totalConflicts);
	for (long id : tagConflicts.keySet()) {
		if ((conflictChoice == ASK_USER) && askUser && mergeManager != null) {
			monitor.checkCanceled();
			currentlyMergingTagID = id;
			showMergePanel(id, monitor);
		}
		else {
			int optionToUse = (conflictChoice == ASK_USER) ? conflictOption : conflictChoice;
			currentlyMergingTagID = id;
			merge(optionToUse, monitor);
		}
		monitor.incrementProgress(1);

		int pctComplete = (int) ((monitor.getProgress() / totalConflicts) * 100);
		mergeManager.updateProgress(pctComplete);
	}
}
 
Example 5
Source File: GTreeSlowLoadingNode.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public void run(TaskMonitor monitor) {
	if (isLoaded()) {
		return;
	}

	long progressValue = monitor.getProgress();
	long maxValue = monitor.getMaximum();
	monitor.setMessage("Loading children");
	try {
		setChildren(generateChildren(monitor));
	}
	catch (CancelledException e) {
		if (!tree.isDisposed()) {
			runOnSwingThread(new Runnable() {
				@Override
				public void run() {
					tree.collapseAll(tree.getViewRoot());
				}
			});
		}
		doSetChildren(null);
	}
	finally {
		monitor.initialize(maxValue);
		monitor.setProgress(progressValue);
	}
}
 
Example 6
Source File: FunctionGraphFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private static BidiMap<CodeBlock, FGVertex> createVertices(Function function,
		final FGController controller, TaskMonitor monitor) throws CancelledException {

	BidiMap<CodeBlock, FGVertex> vertices = new DualHashBidiMap<>();
	CodeBlockModel blockModel = new BasicBlockModel(controller.getProgram());

	AddressSetView addresses = function.getBody();
	CodeBlockIterator iterator = blockModel.getCodeBlocksContaining(addresses, monitor);
	monitor.initialize(addresses.getNumAddresses());

	for (; iterator.hasNext();) {
		CodeBlock codeBlock = iterator.next();

		FlowType flowType = codeBlock.getFlowType();
		boolean isEntry = isEntry(codeBlock);
		Address cbStart = codeBlock.getFirstStartAddress();
		if (cbStart.equals(function.getEntryPoint())) {
			isEntry = true;
		}

		FGVertex vertex =
			new ListingFunctionGraphVertex(controller, codeBlock, flowType, isEntry);
		vertices.put(codeBlock, vertex);

		long blockAddressCount = codeBlock.getNumAddresses();
		long currentProgress = monitor.getProgress();
		monitor.setProgress(currentProgress + blockAddressCount);
	}

	return vertices;
}
 
Example 7
Source File: ApplyMatchTask.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void applyMatches(TaskMonitor monitor) throws CancelledException {
	monitor.setMessage("Processing matches");
	monitor.initialize(matches.size());
	for (VTMatch match : matches) {
		monitor.checkCanceled();
		VTAssociation association = match.getAssociation();
		VTAssociationStatus status = association.getStatus();
		if (!status.canApply()) {
			continue;
		}

		acceptMatch(match);

		long progress = monitor.getProgress();
		MatchInfo matchInfo = controller.getMatchInfo(match);
		Collection<VTMarkupItem> markupItems = matchInfo.getAppliableMarkupItems(monitor);
		if (markupItems == null || markupItems.size() == 0) {
			monitor.incrementProgress(1);
			continue;
		}

		monitor.setMessage("Processing matches");
		monitor.setProgress(progress);

		applyMarkupItems(monitor, markupItems);
		monitor.incrementProgress(1);
	}

	monitor.setProgress(matches.size());
}
 
Example 8
Source File: MemoryBytePatternSearcher.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
 * Search through bytes of a memory block using the finite state machine -root-
 * Apply any additional rules for matching patterns.
 * 
 * @param program is the Program being searched
 * @param block is the specific block of bytes being searched
 * 
 * @throws IOException exception during read of memory
 * @throws CancelledException canceled search
 */
private void searchBlock(SequenceSearchState rootState, Program program, MemoryBlock block,
		AddressSetView restrictSet, TaskMonitor monitor)
		throws IOException, CancelledException {

	// if no restricted set, make restrict set the full block
	AddressSet doneSet;
	if (restrictSet == null || restrictSet.isEmpty()) {
		doneSet = new AddressSet(block.getStart(), block.getEnd());
	}
	else {
		doneSet = restrictSet.intersectRange(block.getStart(), block.getEnd());
	}

	long numInDoneSet = doneSet.getNumAddresses();
	long numInBlock = block.getSize();

	Address blockStartAddr = block.getStart();

	// pull each range off the restricted set
	long progress = monitor.getProgress();
	AddressRangeIterator addressRanges = doneSet.getAddressRanges();
	long numDone = 0;
	while (addressRanges.hasNext()) {
		monitor.checkCanceled();
		monitor.setMessage(searchName + " Search");
		monitor.setProgress(progress + (long) (numInBlock * ((float) numDone / numInDoneSet)));
		AddressRange addressRange = addressRanges.next();
		long numAddressesInRange = addressRange.getLength();

		ArrayList<Match> mymatches = new ArrayList<>();

		long streamoffset = blockStartAddr.getOffset();

		// Give block a starting/ending point before this address to search
		//    patterns might start before, since they have a pre-pattern
		// TODO: this is dangerous, since pattern might be very big, but the set should be restricted
		//       normally only when we are searching for more matching patterns that had a postrule that didn't satisfy
		//       normally the whole memory blocks will get searched.
		long blockOffset = addressRange.getMinAddress().subtract(blockStartAddr);
		blockOffset = blockOffset - RESTRICTED_PATTERN_BYTE_RANGE;
		if (blockOffset <= 0) {
			// don't go before the block start
			blockOffset = 0;
		}

		// compute number of bytes in the range + 1, and don't search more than that.
		long maxBlockSearchLength =
			addressRange.getMaxAddress().subtract(blockStartAddr) - blockOffset + 1;

		InputStream data = block.getData();
		data.skip(blockOffset);

		rootState.apply(data, maxBlockSearchLength, mymatches, monitor);
		monitor.checkCanceled();

		monitor.setMessage(searchName + " (Examine Matches)");

		// TODO: DANGER there is much offset<-->address calculation here
		//       should be OK, since they are all relative to the block.
		long matchProgress = progress + (long) (numInBlock * ((float) numDone / numInDoneSet));
		for (int i = 0; i < mymatches.size(); ++i) {
			monitor.checkCanceled();
			monitor.setProgress(
				matchProgress + (long) (numAddressesInRange * ((float) i / mymatches.size())));
			Match match = mymatches.get(i);
			Address addr = blockStartAddr.add(match.getMarkOffset() + blockOffset);
			if (!match.checkPostRules(streamoffset + blockOffset)) {
				continue;
			}

			MatchAction[] matchactions = match.getMatchActions();
			preMatchApply(matchactions, addr);
			for (MatchAction matchaction : matchactions) {
				matchaction.apply(program, addr, match);
			}

			postMatchApply(matchactions, addr);
		}

		numDone += numAddressesInRange;
	}
}