diff options
| -rw-r--r-- | miasm/core/graph.py | 59 | ||||
| -rw-r--r-- | miasm/core/parse_asm.py | 15 | ||||
| -rw-r--r-- | miasm/ir/ir.py | 65 |
3 files changed, 60 insertions, 79 deletions
diff --git a/miasm/core/graph.py b/miasm/core/graph.py index 8bb4371d..1ee30c9f 100644 --- a/miasm/core/graph.py +++ b/miasm/core/graph.py @@ -293,6 +293,65 @@ class DiGraph(object): out.append("}") return '\n'.join(out) + + def graphviz(self): + try: + import re + import graphviz + + + self.gv = graphviz.Digraph('html_table') + self._dot_offset = False + escape_chars = re.compile('[' + re.escape('{}') + '&|<>' + ']') + td_attr = {'align': 'left'} + nodes_attr = {'shape': 'Mrecord', + 'fontname': 'Courier New'} + + for node in self.nodes(): + elements = [x for x in self.node2lines(node)] + node_id = self.nodeid(node) + out_node = '<<table border="0" cellborder="0" cellpadding="3">' + + node_html_lines = [] + for lineDesc in elements: + out_render = "" + if isinstance(lineDesc, self.DotCellDescription): + lineDesc = [lineDesc] + for col in lineDesc: + out_render += "<td %s>%s</td>" % ( + self._attr2str(td_attr, col.attr), + escape_chars.sub(self._fix_chars, str(col.text))) + node_html_lines.append(out_render) + + node_html_lines = ('<tr>' + + ('</tr><tr>').join(node_html_lines) + + '</tr>') + + out_node += node_html_lines + "</table>>" + attrs = dict(nodes_attr) + attrs.update(self.node_attr(node)) + self.gv.node( + "%s" % node_id, + out_node, + attrs, + ) + + + for src, dst in self.edges(): + attrs = self.edge_attr(src, dst) + self.gv.edge( + str(self.nodeid(src)), + str(self.nodeid(dst)), + "", + attrs, + ) + + return self.gv + except ImportError: + # Skip as graphviz is not installed + return None + + @staticmethod def _reachable_nodes(head, next_cb): """Generic algorithm to compute all nodes reachable from/to node diff --git a/miasm/core/parse_asm.py b/miasm/core/parse_asm.py index 509f0483..19bf4415 100644 --- a/miasm/core/parse_asm.py +++ b/miasm/core/parse_asm.py @@ -61,19 +61,6 @@ class DirectiveDontSplit(Directive): pass -def guess_next_new_label(loc_db): - """Generate a new label - @loc_db: the LocationDB instance""" - i = 0 - gen_name = "loc_%.8X" - while True: - name = gen_name % i - label = loc_db.get_name_location(name) - if label is None: - return loc_db.add_location(name) - i += 1 - - STATE_NO_BLOC = 0 STATE_IN_BLOC = 1 @@ -227,7 +214,7 @@ def parse_txt(mnemo, attrib, txt, loc_db): elif not isinstance(line, LocKey): # First line must be a label. If it's not the case, generate # it. - loc = guess_next_new_label(loc_db) + loc = loc_db.add_location() cur_block = asmblock.AsmBlock(loc_db, loc, alignment=mnemo.alignment) else: cur_block = asmblock.AsmBlock(loc_db, line, alignment=mnemo.alignment) diff --git a/miasm/ir/ir.py b/miasm/ir/ir.py index 9c9a1384..aed5cb65 100644 --- a/miasm/ir/ir.py +++ b/miasm/ir/ir.py @@ -921,71 +921,6 @@ class IntermediateRepresentation(object): return new_irblocks -try: - import re - import graphviz - - class IRCfgGraphviz(IRCFG): - @classmethod - def from_ircfg(cls, ircfg): - new_ircfg = IRCfgGraphviz(ircfg.IRDst, ircfg.loc_db, blocks=ircfg.blocks) - for node in ircfg.nodes(): - new_ircfg.add_node(node) - for src, dst in ircfg.edges(): - new_ircfg.add_uniq_edge(src, dst) - return new_ircfg - - def graphviz(self): - self.gv = graphviz.Digraph('html_table') - self._dot_offset = False - escape_chars = re.compile('[' + re.escape('{}') + '&|<>' + ']') - td_attr = {'align': 'left'} - nodes_attr = {'shape': 'Mrecord', - 'fontname': 'Courier New'} - - for node in self.nodes(): - elements = [x for x in self.node2lines(node)] - node_id = self.nodeid(node) - out_node = '<<table border="0" cellborder="0" cellpadding="3">' - - node_html_lines = [] - for lineDesc in elements: - out_render = "" - if isinstance(lineDesc, self.DotCellDescription): - lineDesc = [lineDesc] - for col in lineDesc: - out_render += "<td %s>%s</td>" % ( - self._attr2str(td_attr, col.attr), - escape_chars.sub(self._fix_chars, str(col.text))) - node_html_lines.append(out_render) - - node_html_lines = ('<tr>' + - ('</tr><tr>').join(node_html_lines) + - '</tr>') - - out_node += node_html_lines + "</table>>" - self.gv.node( - "%s" % node_id, - label=out_node, - shape="Mrecord" - ) - - - for src, dst in self.edges(): - attrs = self.edge_attr(src, dst) - self.gv.edge( - str(self.nodeid(src)), - str(self.nodeid(dst)), - "", - attrs, - ) - - return self.gv -except ImportError: - # Skip as graphviz is not installed - pass - - class ir(IntermediateRepresentation): """ DEPRECATED object |