Search in sources :

Example 16 with INaviInstruction

use of com.google.security.zynamics.binnavi.disassembly.INaviInstruction in project binnavi by google.

the class PostgreSQLNodeSaver method saveCodeNodes.

/**
   * Saves the code nodes to the database.
   *
   * @param provider The connection to the database.
   * @param nodes The nodes to save.
   * @param firstNode The database index of the first node.
   * @param codeNodeIndices Index into the nodes list that identifies the code nodes.
   *
   * @throws SQLException Thrown if saving the code node instructions failed.
   */
protected static void saveCodeNodes(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
    if (!codeNodeIndices.isEmpty()) {
        final List<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = PostgreSQLNodeSaver.saveCodeNodeInstructions(provider, nodes, firstNode, codeNodeIndices);
        final String query = "INSERT INTO " + CTableNames.CODE_NODES_TABLE + "(module_id, node_id, parent_function, comment_id) VALUES (?, ?, ?, ?)";
        final ArrayList<INaviCodeNode> codeNodesWithUnsavedComments = new ArrayList<INaviCodeNode>();
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        try {
            for (final int index : codeNodeIndices) {
                final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
                codeNode.setId(firstNode + index);
                INaviFunction function = null;
                try {
                    function = codeNode.getParentFunction();
                } catch (final MaybeNullException e) {
                }
                final int moduleId = Iterables.getLast(codeNode.getInstructions()).getModule().getConfiguration().getId();
                final List<IComment> comment = codeNode.getComments().getLocalCodeNodeComment();
                final Integer commentId = comment == null ? null : comment.size() == 0 ? null : Iterables.getLast(comment).getId();
                if ((comment != null) && (comment.size() != 0) && (commentId == null)) {
                    codeNodesWithUnsavedComments.add(codeNode);
                }
                preparedStatement.setInt(1, moduleId);
                preparedStatement.setInt(2, firstNode + index);
                if (function == null) {
                    preparedStatement.setNull(3, Types.BIGINT);
                } else {
                    preparedStatement.setObject(3, function.getAddress().toBigInteger(), Types.BIGINT);
                }
                if (commentId == null) {
                    preparedStatement.setNull(4, Types.INTEGER);
                } else {
                    preparedStatement.setInt(4, commentId);
                }
                preparedStatement.addBatch();
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        // implementation.
        for (final INaviCodeNode codeNode : codeNodesWithUnsavedComments) {
            final ArrayList<IComment> codeNodecomments = new ArrayList<IComment>();
            for (final IComment comment : codeNode.getComments().getLocalCodeNodeComment()) {
                try {
                    final Integer commentId = PostgreSQLNodeFunctions.appendLocalCodeNodeComment(provider, codeNode, comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    codeNodecomments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            codeNode.getComments().initializeLocalCodeNodeComment(codeNodecomments);
        }
        // implementation.
        for (final Pair<INaviCodeNode, INaviInstruction> pair : instructionsWithUnsavedLocalComments) {
            final ArrayList<IComment> localInstructionComments = new ArrayList<IComment>();
            for (final IComment comment : pair.first().getComments().getLocalInstructionComment(pair.second())) {
                try {
                    final int commentId = PostgreSQLInstructionFunctions.appendLocalInstructionComment(provider, pair.first(), pair.second(), comment.getComment(), comment.getUser().getUserId());
                    final IComment newComment = new CComment(commentId, comment.getUser(), comment.getParent(), comment.getComment());
                    localInstructionComments.add(newComment);
                } catch (final CouldntSaveDataException exception) {
                    CUtilityFunctions.logException(exception);
                }
            }
            pair.first().getComments().initializeLocalInstructionComment(pair.second(), localInstructionComments);
        }
    }
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) MaybeNullException(com.google.security.zynamics.binnavi.Exceptions.MaybeNullException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) CComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.CComment) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) Pair(com.google.security.zynamics.zylib.general.Pair) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 17 with INaviInstruction

use of com.google.security.zynamics.binnavi.disassembly.INaviInstruction in project binnavi by google.

the class PostgreSQLNodeSaver method saveCodeNodeInstructions.

/**
   * Saves the mapping between code nodes and their instructions to the database.
   *
   * @param provider The provider used to access the database.
   * @param nodes The nodes to save.
   * @param firstNode The database index of the first node.
   * @param codeNodeIndices Index into the nodes list that identifies the code nodes.
   *
   * @throws SQLException Thrown if saving the code node instructions failed.
   */
protected static ArrayList<Pair<INaviCodeNode, INaviInstruction>> saveCodeNodeInstructions(final SQLProvider provider, final List<INaviViewNode> nodes, final int firstNode, final List<Integer> codeNodeIndices) throws SQLException {
    if (!nodes.isEmpty()) {
        final Set<INaviInstruction> unsavedInstructions = new HashSet<INaviInstruction>();
        for (final int index : codeNodeIndices) {
            final CCodeNode node = (CCodeNode) nodes.get(index);
            final Iterable<INaviInstruction> instructions = node.getInstructions();
            for (final INaviInstruction instruction : instructions) {
                if (!(instruction.isStored())) {
                    unsavedInstructions.add(instruction);
                }
            }
        }
        PostgreSQLInstructionFunctions.createInstructions(provider, unsavedInstructions);
        final String query = "INSERT INTO " + CTableNames.CODENODE_INSTRUCTIONS_TABLE + " (module_id, node_id, position, address, comment_id) VALUES (?, ?, ?, ?, ?)";
        final PreparedStatement preparedStatement = provider.getConnection().getConnection().prepareStatement(query);
        final ArrayList<Pair<INaviCodeNode, INaviInstruction>> instructionsWithUnsavedLocalComments = new ArrayList<Pair<INaviCodeNode, INaviInstruction>>();
        try {
            for (final Integer index : codeNodeIndices) {
                final INaviCodeNode codeNode = (INaviCodeNode) nodes.get(index);
                int position = 0;
                for (final INaviInstruction instruction : codeNode.getInstructions()) {
                    final List<IComment> comments = codeNode.getComments().getLocalInstructionComment(instruction);
                    final Integer commentId = comments == null ? null : comments.size() == 0 ? null : Iterables.getLast(comments).getId();
                    if ((comments != null) && (comments.size() != 0) && (commentId == null)) {
                        instructionsWithUnsavedLocalComments.add(new Pair<INaviCodeNode, INaviInstruction>(codeNode, instruction));
                    }
                    final int moduleId = instruction.getModule().getConfiguration().getId();
                    preparedStatement.setInt(1, moduleId);
                    preparedStatement.setInt(2, firstNode + index);
                    preparedStatement.setInt(3, position);
                    preparedStatement.setObject(4, instruction.getAddress().toBigInteger(), Types.BIGINT);
                    if (commentId == null) {
                        preparedStatement.setNull(5, Types.INTEGER);
                    } else {
                        preparedStatement.setInt(5, commentId);
                    }
                    position++;
                    preparedStatement.addBatch();
                }
            }
            preparedStatement.executeBatch();
        } finally {
            preparedStatement.close();
        }
        return instructionsWithUnsavedLocalComments;
    }
    return null;
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) CCodeNode(com.google.security.zynamics.binnavi.disassembly.CCodeNode) HashSet(java.util.HashSet) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction) Pair(com.google.security.zynamics.zylib.general.Pair)

Example 18 with INaviInstruction

use of com.google.security.zynamics.binnavi.disassembly.INaviInstruction in project binnavi by google.

the class InternalNodeCallBack method zoomToAddress.

/**
   * Zooms to the first occurrence of an address in a graph.
   *
   * @param graph The graph where the zoom operation takes place.
   * @param address The address to zoom to.
   */
public static void zoomToAddress(final ZyGraph graph, final IAddress address) {
    graph.iterate(new INodeCallback<NaviNode>() {

        @Override
        public IterationMode next(final NaviNode node) {
            if (node.getRawNode() instanceof INaviCodeNode) {
                final INaviCodeNode codeNode = (INaviCodeNode) node.getRawNode();
                for (final INaviInstruction instruction : codeNode.getInstructions()) {
                    if (instruction.getAddress().equals(address)) {
                        uncollapseParents(codeNode);
                        graph.showNode(node, true);
                        ZoomFunctions.zoomToNode(graph, node);
                        return IterationMode.STOP;
                    }
                }
            } else if (node.getRawNode() instanceof INaviFunctionNode) {
                final INaviFunctionNode functionNode = (INaviFunctionNode) node.getRawNode();
                if (functionNode.getFunction().getAddress().equals(address)) {
                    uncollapseParents(functionNode);
                    graph.showNode(node, true);
                    ZoomFunctions.zoomToNode(graph, node);
                    return IterationMode.STOP;
                }
            }
            return IterationMode.CONTINUE;
        }
    });
}
Also used : INaviCodeNode(com.google.security.zynamics.binnavi.disassembly.INaviCodeNode) NaviNode(com.google.security.zynamics.binnavi.yfileswrap.zygraph.NaviNode) INaviFunctionNode(com.google.security.zynamics.binnavi.disassembly.INaviFunctionNode) IterationMode(com.google.security.zynamics.zylib.types.common.IterationMode) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 19 with INaviInstruction

use of com.google.security.zynamics.binnavi.disassembly.INaviInstruction in project binnavi by google.

the class CBreakpointPainter method paintBreakpoints.

/**
   * Paints breakpoints into code nodes.
   * 
   * @param manager The breakpoint manager that provides breakpoint information.
   * @param node The visible BinNavi node where the breakpoint is painted.
   * @param codeNode The code node that contains the raw data for the BinNavi node.
   */
public static void paintBreakpoints(final BreakpointManager manager, final NaviNode node, final INaviCodeNode codeNode) {
    Preconditions.checkNotNull(manager, "IE02171: Manager argument can not be null");
    Preconditions.checkNotNull(node, "IE02172: Node argument can not be null");
    Preconditions.checkNotNull(codeNode, "IE02173: Code node argument can not be null");
    for (final INaviInstruction instruction : codeNode.getInstructions()) {
        final BreakpointAddress address = new BreakpointAddress(instruction.getModule(), new UnrelocatedAddress(instruction.getAddress()));
        final int line = CCodeNodeHelpers.instructionToLine(codeNode, instruction);
        if (manager.hasBreakpoint(BreakpointType.REGULAR, address)) {
            // Only the address is highlighted when breakpoints are set on instructions
            // visible in code nodes.
            final int addressCharacters = address.getAddress().getAddress().toHexString().length();
            node.setHighlighting(CHighlightLayers.BREAKPOINT_LAYER, line, 0, addressCharacters, BreakpointManager.getBreakpointColor(manager.getBreakpointStatus(address, BreakpointType.REGULAR)));
        } else {
            // If there is no breakpoint, clear potential older breakpoint line.
            node.clearHighlighting(CHighlightLayers.BREAKPOINT_LAYER, line);
        }
    }
}
Also used : UnrelocatedAddress(com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress) BreakpointAddress(com.google.security.zynamics.binnavi.debug.models.breakpoints.BreakpointAddress) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Example 20 with INaviInstruction

use of com.google.security.zynamics.binnavi.disassembly.INaviInstruction in project binnavi by google.

the class ZyCodeNodeBuilder method createLines.

/**
   * Creates the instructions lines for a code node.
   * 
   * @param node The node that provides the instructions.
   * @param lines The created lines will be stored here.
   * @param commentsToLineAssociation Information about the required comments is stored here.
   * @param graphSettings Provides settings that influence node formatting.
   * @param modifier Calculates the address strings. This argument can be null.
   * 
   * @return The maximum size in characters of all lines put into the lines list.
   */
private static int createLines(final INaviCodeNode node, final List<Pair<String, List<CStyleRunData>>> lines, final HashMap<Pair<String, List<CStyleRunData>>, ArrayList<CommentContainer>> commentsToLineAssociation, final ZyGraphViewSettings graphSettings, final INodeModifier modifier) {
    int maxLineWidth = 0;
    final Map<INaviInstruction, INaviFunction> instructionToFunctionReferences = CReferenceFinder.getCodeReferenceMap(node);
    for (final INaviInstruction instruction : node.getInstructions()) {
        final Pair<String, List<CStyleRunData>> zyLineContent = ZyInstructionBuilder.buildInstructionLine(instruction, graphSettings, modifier);
        final ArrayList<CommentContainer> commentLineContainerList = new ArrayList<CommentContainer>();
        final List<IComment> localComments = node.getComments().getLocalInstructionComment(instruction);
        if (localComments != null) {
            for (final IComment localComment : localComments) {
                commentLineContainerList.add(new CommentContainer(localComment));
            }
        }
        final List<IComment> globalComments = instruction.getGlobalComment();
        if (globalComments != null) {
            for (final IComment globalComment : globalComments) {
                commentLineContainerList.add(new CommentContainer(globalComment));
            }
        }
        final List<IComment> functionComments = instructionToFunctionReferences.get(instruction) == null ? null : instructionToFunctionReferences.get(instruction).getGlobalComment();
        if (functionComments != null) {
            for (final IComment functionComment : functionComments) {
                commentLineContainerList.add(new CommentContainer(functionComment));
            }
        }
        commentsToLineAssociation.put(zyLineContent, commentLineContainerList);
        final int lineWidth = zyLineContent.first().length();
        if (lineWidth > maxLineWidth) {
            maxLineWidth = lineWidth;
        }
        lines.add(zyLineContent);
    }
    return maxLineWidth;
}
Also used : IComment(com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) INaviFunction(com.google.security.zynamics.binnavi.disassembly.INaviFunction) INaviInstruction(com.google.security.zynamics.binnavi.disassembly.INaviInstruction)

Aggregations

INaviInstruction (com.google.security.zynamics.binnavi.disassembly.INaviInstruction)82 Test (org.junit.Test)27 INaviCodeNode (com.google.security.zynamics.binnavi.disassembly.INaviCodeNode)26 CAddress (com.google.security.zynamics.zylib.disassembly.CAddress)24 ArrayList (java.util.ArrayList)24 INaviFunction (com.google.security.zynamics.binnavi.disassembly.INaviFunction)20 INaviModule (com.google.security.zynamics.binnavi.disassembly.INaviModule)18 CCodeNode (com.google.security.zynamics.binnavi.disassembly.CCodeNode)16 COperandTree (com.google.security.zynamics.binnavi.disassembly.COperandTree)15 IAddress (com.google.security.zynamics.zylib.disassembly.IAddress)15 MockSqlProvider (com.google.security.zynamics.binnavi.Database.MockClasses.MockSqlProvider)10 INaviView (com.google.security.zynamics.binnavi.disassembly.views.INaviView)10 IComment (com.google.security.zynamics.binnavi.Gui.GraphWindows.CommentDialogs.Interfaces.IComment)9 IBlockNode (com.google.security.zynamics.binnavi.disassembly.IBlockNode)9 INaviOperandTreeNode (com.google.security.zynamics.binnavi.disassembly.INaviOperandTreeNode)9 INaviViewNode (com.google.security.zynamics.binnavi.disassembly.INaviViewNode)9 UnrelocatedAddress (com.google.security.zynamics.binnavi.disassembly.UnrelocatedAddress)9 MockModule (com.google.security.zynamics.binnavi.disassembly.Modules.MockModule)8 ExpensiveBaseTest (com.google.security.zynamics.binnavi.disassembly.types.ExpensiveBaseTest)8 MaybeNullException (com.google.security.zynamics.binnavi.Exceptions.MaybeNullException)7