Search in sources :

Example 6 with ThreadRegisters

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());
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) RegisterValues(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValues) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ThreadRegisters(com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters)

Example 7 with ThreadRegisters

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);
}
Also used : RegisterValue(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValue) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) BigInteger(java.math.BigInteger) ThreadRegisters(com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters)

Example 8 with ThreadRegisters

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);
    }
}
Also used : RegisterValue(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValue) TargetProcessThread(com.google.security.zynamics.binnavi.debug.models.processmanager.TargetProcessThread) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) RelocatedAddress(com.google.security.zynamics.binnavi.disassembly.RelocatedAddress) ThreadRegisters(com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters) ProcessManager(com.google.security.zynamics.binnavi.debug.models.processmanager.ProcessManager) RegisterValues(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValues) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress)

Example 9 with ThreadRegisters

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())));
                    }
                }
            }
        }
    }
}
Also used : RegisterValue(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValue) TargetProcessThread(com.google.security.zynamics.binnavi.debug.models.processmanager.TargetProcessThread) RelocatedAddress(com.google.security.zynamics.binnavi.disassembly.RelocatedAddress) ThreadRegisters(com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters) ProcessManager(com.google.security.zynamics.binnavi.debug.models.processmanager.ProcessManager) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress)

Example 10 with ThreadRegisters

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));
}
Also used : SingleStepReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.SingleStepReply) SetRegisterReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.SetRegisterReply) ThreadClosedReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.ThreadClosedReply) ValidateMemoryReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.ValidateMemoryReply) StepBreakpointSetReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.StepBreakpointSetReply) TargetProcessThread(com.google.security.zynamics.binnavi.debug.models.processmanager.TargetProcessThread) SearchReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.SearchReply) TerminateReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.TerminateReply) RelocatedAddress(com.google.security.zynamics.binnavi.disassembly.RelocatedAddress) StepBreakpointHitReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.StepBreakpointHitReply) SelectFileReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.SelectFileReply) SuspendThreadReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.SuspendThreadReply) CAddress(com.google.security.zynamics.zylib.disassembly.CAddress) RegisterValues(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValues) BigInteger(java.math.BigInteger) RegisterValue(com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValue) StepBreakpointsRemovedReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.StepBreakpointsRemovedReply) ResumeThreadReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.ResumeThreadReply) WriteMemoryReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.WriteMemoryReply) TargetInformationReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.TargetInformationReply) ThreadRegisters(com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters) ThreadCreatedReply(com.google.security.zynamics.binnavi.debug.connection.packets.replies.ThreadCreatedReply) Test(org.junit.Test)

Aggregations

ThreadRegisters (com.google.security.zynamics.binnavi.debug.models.targetinformation.ThreadRegisters)16 RegisterValue (com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValue)14 RegisterValues (com.google.security.zynamics.binnavi.debug.models.targetinformation.RegisterValues)11 RelocatedAddress (com.google.security.zynamics.binnavi.disassembly.RelocatedAddress)10 Test (org.junit.Test)10 TargetProcessThread (com.google.security.zynamics.binnavi.debug.models.processmanager.TargetProcessThread)9 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)9 BigInteger (java.math.BigInteger)7 EchoBreakpointHitReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.EchoBreakpointHitReply)5 StepBreakpointHitReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.StepBreakpointHitReply)5 StepBreakpointSetReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.StepBreakpointSetReply)5 EchoBreakpointSetReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.EchoBreakpointSetReply)4 EchoBreakpointsRemovedReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.EchoBreakpointsRemovedReply)4 StepBreakpointsRemovedReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.StepBreakpointsRemovedReply)4 BreakpointConditionSetReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.BreakpointConditionSetReply)3 Address (com.google.security.zynamics.binnavi.API.disassembly.Address)2 Trace (com.google.security.zynamics.binnavi.API.disassembly.Trace)2 TracePoint (com.google.security.zynamics.binnavi.API.disassembly.TracePoint)2 ExceptionOccurredReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.ExceptionOccurredReply)2 HaltReply (com.google.security.zynamics.binnavi.debug.connection.packets.replies.HaltReply)2