use of com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters in project binnavi by google.
the class RegisterValuesParser method parse.
/**
* Parses a byte array from the debug client into usable register information.
*
* @param data Byte array from the debug client.
*
* @return Usable register information.
*
* @throws IllegalArgumentException If the data argument is null.
* @throws MessageParserException If parsing the message failed.
*/
public static RegisterValues parse(final byte[] data) throws MessageParserException {
Preconditions.checkNotNull(data, "IE01299: Data argument can not be null");
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document = builder.parse(new ByteArrayInputStream(data, 0, data.length));
final NodeList nodes = document.getFirstChild().getChildNodes();
final List<ThreadRegisters> threads = new ArrayList<>();
for (int i = 0; i < nodes.getLength(); ++i) {
final Node node = nodes.item(i);
if (node.getNodeName().equals("Thread")) {
threads.add(parseThreadNode(node));
} else {
throw new MessageParserException(String.format("IE01040: Invalid node '%s' found during register values message parsing", node.getNodeName()));
}
}
return new RegisterValues(threads);
} catch (IOException | ParserConfigurationException | SAXException exception) {
CUtilityFunctions.logException(exception);
throw new MessageParserException(exception.getLocalizedMessage());
}
}
use of com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters in project binnavi by google.
the class RegisterValuesParser method parseThreadNode.
/**
* Parses a single thread node.
*
* @param node The node to parse.
*
* @return The thread registers object created from the content of the node.
*
* @throws MessageParserException Thrown if parsing the thread node failed.
*/
private static ThreadRegisters parseThreadNode(final Node node) throws MessageParserException {
final List<RegisterValue> registerValues = new ArrayList<>();
final long tid = Long.valueOf(node.getAttributes().getNamedItem("id").getNodeValue());
final NodeList children = node.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
final Node child = children.item(i);
final String registerName = getAttribute(child, "name");
final BigInteger registerValue = new BigInteger(getAttribute(child, "value"), 16);
final byte[] memory = MemoryStringParser.parseMemoryString(getAttribute(child, "memory"));
final boolean isPc = hasAttribute(child, "pc");
final boolean isSp = hasAttribute(child, "sp");
registerValues.add(new RegisterValue(registerName, registerValue, memory, isPc, isSp));
}
return new ThreadRegisters(tid, registerValues);
}
use of com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters in project binnavi by google.
the class BreakpointHitSynchronizer method handleSuccess.
/**
* Handles incoming Breakpoint Hit replies.
*
* @param reply The incoming Breakpoint Hit reply to handle.
*/
@Override
protected void handleSuccess(final BreakpointHitReply reply) {
final ProcessManager processManager = getDebugger().getProcessManager();
// When the debug client notifies BinNavi that a
// breakpoint was hit, it is necessary to mark the
// breakpoint as hit.
// TODO: Check for success
RelocatedAddress eventAddress = null;
final RegisterValues registerValues = reply.getRegisterValues();
final long tid = reply.getThreadId();
for (final ThreadRegisters threadRegisters : registerValues) {
if (tid == threadRegisters.getTid()) {
for (final RegisterValue registerValue : threadRegisters) {
if (registerValue.isPc()) {
eventAddress = new RelocatedAddress(new CAddress(registerValue.getValue()));
}
}
}
}
if (eventAddress != null) {
updateHitBreakpoints(DebuggerHelpers.getBreakpointAddress(getDebugger(), eventAddress));
} else {
throw new IllegalStateException("IE00173: register reply did not include program counter");
}
try {
final TargetProcessThread thread = processManager.getThread(reply.getThreadId());
// Update the thread with the new register values.
for (final ThreadRegisters threadRegisters : registerValues) {
if (tid == threadRegisters.getTid()) {
thread.setRegisterValues(threadRegisters.getRegisters());
break;
}
}
processManager.setActiveThread(thread);
thread.setCurrentAddress(eventAddress);
} catch (final MaybeNullException exception) {
NaviLogger.info("Error: there is no thread with the specified thread id %d Exception: %s", reply.getThreadId(), exception);
}
}
use of com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters in project binnavi by google.
the class ReplySynchronizer method setRegisterValues.
/**
* Updates the thread the register data belongs to with the new values.
*
* @param registerValues The new register values.
*/
protected void setRegisterValues(final RegisterValues registerValues) {
Preconditions.checkNotNull(registerValues, "IE01046: Register values argument can not be null");
final ProcessManager processManager = debugger.getProcessManager();
for (final ThreadRegisters threadRegister : registerValues) {
for (final TargetProcessThread thread : processManager.getThreads()) {
if (thread.getThreadId() == threadRegister.getTid()) {
// Update the thread with the new register values.
thread.setRegisterValues(threadRegister.getRegisters());
for (final RegisterValue registerValue : threadRegister.getRegisters()) {
if (registerValue.isPc()) {
thread.setCurrentAddress(new RelocatedAddress(new CAddress(registerValue.getValue())));
}
}
}
}
}
}
use of com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters in project binnavi by google.
the class DebuggerTest method testListeners3.
@SuppressWarnings("unchecked")
@Test
public void testListeners3() throws MessageParserException, DebugExceptionWrapper, MaybeNullException {
mockDebugger.connect();
mockDebugger.getProcessManager().addThread(new TargetProcessThread(0, ThreadState.RUNNING));
mockDebugger.getProcessManager().getThread(0).setCurrentAddress(new RelocatedAddress(new CAddress(0)));
mockDebugger.connection.m_synchronizer.receivedEvent(new ResumeThreadReply(0, 0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new ResumeThreadReply(0, 1, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new SearchReply(0, 0, new CAddress(0)));
mockDebugger.connection.m_synchronizer.receivedEvent(new SearchReply(0, 1, null));
mockDebugger.connection.m_synchronizer.receivedEvent(new SelectFileReply(0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new SelectFileReply(0, 1));
mockDebugger.getProcessManager().addThread(new TargetProcessThread(0, ThreadState.RUNNING));
mockDebugger.getProcessManager().getThread(0).setCurrentAddress(new RelocatedAddress(new CAddress(0)));
mockDebugger.connection.m_synchronizer.receivedEvent(new SetRegisterReply(0, 0, 0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new SetRegisterReply(0, 1, 0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new SingleStepReply(0, 0, 0, new RelocatedAddress(new CAddress(0)), new RegisterValues(new FilledList<ThreadRegisters>())));
mockDebugger.connection.m_synchronizer.receivedEvent(new SingleStepReply(0, 1, 0, new RelocatedAddress(new CAddress(0)), new RegisterValues(new FilledList<ThreadRegisters>())));
mockDebugger.connection.m_synchronizer.receivedEvent(new StepBreakpointHitReply(0, 0, 1, new RegisterValues(Lists.<ThreadRegisters>newArrayList(new ThreadRegisters(1, Lists.newArrayList(new RegisterValue("eip", BigInteger.ONE, new byte[0], false, false)))))));
mockDebugger.connection.m_synchronizer.receivedEvent(new StepBreakpointHitReply(0, 0, 1, new RegisterValues(Lists.<ThreadRegisters>newArrayList(new ThreadRegisters(1, Lists.newArrayList(new RegisterValue("eip", BigInteger.ONE, new byte[0], true, false)))))));
mockDebugger.getBreakpointManager().addBreakpoints(BreakpointType.STEP, CommonTestObjects.BP_ADDRESS_123_SET);
mockDebugger.connection.m_synchronizer.receivedEvent(new StepBreakpointSetReply(0, 0, Lists.newArrayList(new Pair<RelocatedAddress, Integer>(CommonTestObjects.BP_ADDRESS_123_RELOC, 0))));
mockDebugger.connection.m_synchronizer.receivedEvent(new StepBreakpointSetReply(0, 1, Lists.newArrayList(new Pair<RelocatedAddress, Integer>(CommonTestObjects.BP_ADDRESS_123_RELOC, 1))));
mockDebugger.connection.m_synchronizer.receivedEvent(new StepBreakpointsRemovedReply(0, 0, Lists.newArrayList(new Pair<RelocatedAddress, Integer>(CommonTestObjects.BP_ADDRESS_123_RELOC, 0))));
mockDebugger.connection.m_synchronizer.receivedEvent(new StepBreakpointsRemovedReply(0, 1, Lists.newArrayList(new Pair<RelocatedAddress, Integer>(CommonTestObjects.BP_ADDRESS_123_RELOC, 1))));
mockDebugger.connection.m_synchronizer.receivedEvent(new SuspendThreadReply(0, 0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new SuspendThreadReply(0, 1, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new TargetInformationReply(0, 0, TargetInformationParser.parse("<foo><size>32</size><registers></registers><options></options></foo>".getBytes())));
mockDebugger.connection.m_synchronizer.receivedEvent(new TargetInformationReply(0, 1, null));
mockDebugger.getProcessManager().addThread(new TargetProcessThread(0, ThreadState.RUNNING));
mockDebugger.connection.m_synchronizer.receivedEvent(new ThreadClosedReply(0, 0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new ThreadClosedReply(0, 1, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new ThreadCreatedReply(0, 0, 0, ThreadState.RUNNING));
mockDebugger.connection.m_synchronizer.receivedEvent(new ThreadCreatedReply(0, 1, 0, null));
mockDebugger.connection.m_synchronizer.receivedEvent(new ValidateMemoryReply(0, 0, new CAddress(0), new CAddress(0)));
mockDebugger.connection.m_synchronizer.receivedEvent(new ValidateMemoryReply(0, 1, null, null));
mockDebugger.connection.m_synchronizer.receivedEvent(new WriteMemoryReply(0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new WriteMemoryReply(0, 1));
mockDebugger.connection.m_synchronizer.receivedEvent(new TerminateReply(0, 0));
mockDebugger.connection.m_synchronizer.receivedEvent(new TerminateReply(0, 1));
}
Aggregations