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);
}
}
}
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;
}
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;
}
});
}
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);
}
}
}
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;
}
Aggregations