1e5dd7070Spatrick#!/usr/bin/env python 2e5dd7070Spatrick# 3e5dd7070Spatrick#===- exploded-graph-rewriter.py - ExplodedGraph dump tool -----*- python -*--# 4e5dd7070Spatrick# 5e5dd7070Spatrick# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 6e5dd7070Spatrick# See https://llvm.org/LICENSE.txt for license information. 7e5dd7070Spatrick# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 8e5dd7070Spatrick# 9e5dd7070Spatrick#===-----------------------------------------------------------------------===# 10e5dd7070Spatrick 11e5dd7070Spatrick 12e5dd7070Spatrickfrom __future__ import print_function 13e5dd7070Spatrick 14e5dd7070Spatrickimport argparse 15e5dd7070Spatrickimport collections 16e5dd7070Spatrickimport difflib 17e5dd7070Spatrickimport json 18e5dd7070Spatrickimport logging 19e5dd7070Spatrickimport os 20e5dd7070Spatrickimport re 21e5dd7070Spatrick 22e5dd7070Spatrick 23e5dd7070Spatrick#===-----------------------------------------------------------------------===# 24e5dd7070Spatrick# These data structures represent a deserialized ExplodedGraph. 25e5dd7070Spatrick#===-----------------------------------------------------------------------===# 26e5dd7070Spatrick 27e5dd7070Spatrick 28e5dd7070Spatrick# A helper function for finding the difference between two dictionaries. 29e5dd7070Spatrickdef diff_dicts(curr, prev): 30e5dd7070Spatrick removed = [k for k in prev if k not in curr or curr[k] != prev[k]] 31e5dd7070Spatrick added = [k for k in curr if k not in prev or curr[k] != prev[k]] 32e5dd7070Spatrick return (removed, added) 33e5dd7070Spatrick 34e5dd7070Spatrick 35e5dd7070Spatrick# Represents any program state trait that is a dictionary of key-value pairs. 36ec727ea7Spatrickclass GenericMap: 37e5dd7070Spatrick def __init__(self, items): 38e5dd7070Spatrick self.generic_map = collections.OrderedDict(items) 39e5dd7070Spatrick 40e5dd7070Spatrick def diff(self, prev): 41e5dd7070Spatrick return diff_dicts(self.generic_map, prev.generic_map) 42e5dd7070Spatrick 43e5dd7070Spatrick def is_different(self, prev): 44e5dd7070Spatrick removed, added = self.diff(prev) 45e5dd7070Spatrick return len(removed) != 0 or len(added) != 0 46e5dd7070Spatrick 47e5dd7070Spatrick 48e5dd7070Spatrick# A deserialized source location. 49ec727ea7Spatrickclass SourceLocation: 50e5dd7070Spatrick def __init__(self, json_loc): 51e5dd7070Spatrick logging.debug('json: %s' % json_loc) 52e5dd7070Spatrick self.line = json_loc['line'] 53e5dd7070Spatrick self.col = json_loc['column'] 54e5dd7070Spatrick self.filename = os.path.basename(json_loc['file']) \ 55e5dd7070Spatrick if 'file' in json_loc else '(main file)' 56e5dd7070Spatrick self.spelling = SourceLocation(json_loc['spelling']) \ 57e5dd7070Spatrick if 'spelling' in json_loc else None 58e5dd7070Spatrick 59e5dd7070Spatrick def is_macro(self): 60e5dd7070Spatrick return self.spelling is not None 61e5dd7070Spatrick 62e5dd7070Spatrick 63e5dd7070Spatrick# A deserialized program point. 64ec727ea7Spatrickclass ProgramPoint: 65e5dd7070Spatrick def __init__(self, json_pp): 66e5dd7070Spatrick self.kind = json_pp['kind'] 67e5dd7070Spatrick self.tag = json_pp['tag'] 68e5dd7070Spatrick self.node_id = json_pp['node_id'] 69e5dd7070Spatrick self.is_sink = bool(json_pp['is_sink']) 70e5dd7070Spatrick self.has_report = bool(json_pp['has_report']) 71e5dd7070Spatrick if self.kind == 'Edge': 72e5dd7070Spatrick self.src_id = json_pp['src_id'] 73e5dd7070Spatrick self.dst_id = json_pp['dst_id'] 74e5dd7070Spatrick elif self.kind == 'Statement': 75e5dd7070Spatrick logging.debug(json_pp) 76e5dd7070Spatrick self.stmt_kind = json_pp['stmt_kind'] 77e5dd7070Spatrick self.cast_kind = json_pp['cast_kind'] \ 78e5dd7070Spatrick if 'cast_kind' in json_pp else None 79e5dd7070Spatrick self.stmt_point_kind = json_pp['stmt_point_kind'] 80e5dd7070Spatrick self.stmt_id = json_pp['stmt_id'] 81e5dd7070Spatrick self.pointer = json_pp['pointer'] 82e5dd7070Spatrick self.pretty = json_pp['pretty'] 83e5dd7070Spatrick self.loc = SourceLocation(json_pp['location']) \ 84e5dd7070Spatrick if json_pp['location'] is not None else None 85e5dd7070Spatrick elif self.kind == 'BlockEntrance': 86e5dd7070Spatrick self.block_id = json_pp['block_id'] 87e5dd7070Spatrick 88e5dd7070Spatrick 89e5dd7070Spatrick# A single expression acting as a key in a deserialized Environment. 90ec727ea7Spatrickclass EnvironmentBindingKey: 91e5dd7070Spatrick def __init__(self, json_ek): 92e5dd7070Spatrick # CXXCtorInitializer is not a Stmt! 93e5dd7070Spatrick self.stmt_id = json_ek['stmt_id'] if 'stmt_id' in json_ek \ 94e5dd7070Spatrick else json_ek['init_id'] 95e5dd7070Spatrick self.pretty = json_ek['pretty'] 96e5dd7070Spatrick self.kind = json_ek['kind'] if 'kind' in json_ek else None 97e5dd7070Spatrick 98e5dd7070Spatrick def _key(self): 99e5dd7070Spatrick return self.stmt_id 100e5dd7070Spatrick 101e5dd7070Spatrick def __eq__(self, other): 102e5dd7070Spatrick return self._key() == other._key() 103e5dd7070Spatrick 104e5dd7070Spatrick def __hash__(self): 105e5dd7070Spatrick return hash(self._key()) 106e5dd7070Spatrick 107e5dd7070Spatrick 108e5dd7070Spatrick# Deserialized description of a location context. 109ec727ea7Spatrickclass LocationContext: 110e5dd7070Spatrick def __init__(self, json_frame): 111e5dd7070Spatrick self.lctx_id = json_frame['lctx_id'] 112e5dd7070Spatrick self.caption = json_frame['location_context'] 113e5dd7070Spatrick self.decl = json_frame['calling'] 114e5dd7070Spatrick self.loc = SourceLocation(json_frame['location']) \ 115e5dd7070Spatrick if json_frame['location'] is not None else None 116e5dd7070Spatrick 117e5dd7070Spatrick def _key(self): 118e5dd7070Spatrick return self.lctx_id 119e5dd7070Spatrick 120e5dd7070Spatrick def __eq__(self, other): 121e5dd7070Spatrick return self._key() == other._key() 122e5dd7070Spatrick 123e5dd7070Spatrick def __hash__(self): 124e5dd7070Spatrick return hash(self._key()) 125e5dd7070Spatrick 126e5dd7070Spatrick 127e5dd7070Spatrick# A group of deserialized Environment bindings that correspond to a specific 128e5dd7070Spatrick# location context. 129ec727ea7Spatrickclass EnvironmentFrame: 130e5dd7070Spatrick def __init__(self, json_frame): 131e5dd7070Spatrick self.location_context = LocationContext(json_frame) 132e5dd7070Spatrick self.bindings = collections.OrderedDict( 133e5dd7070Spatrick [(EnvironmentBindingKey(b), 134e5dd7070Spatrick b['value']) for b in json_frame['items']] 135e5dd7070Spatrick if json_frame['items'] is not None else []) 136e5dd7070Spatrick 137e5dd7070Spatrick def diff_bindings(self, prev): 138e5dd7070Spatrick return diff_dicts(self.bindings, prev.bindings) 139e5dd7070Spatrick 140e5dd7070Spatrick def is_different(self, prev): 141e5dd7070Spatrick removed, added = self.diff_bindings(prev) 142e5dd7070Spatrick return len(removed) != 0 or len(added) != 0 143e5dd7070Spatrick 144e5dd7070Spatrick 145e5dd7070Spatrick# A deserialized Environment. This class can also hold other entities that 146*12c85518Srobert# are similar to Environment, such as Objects Under Construction or 147*12c85518Srobert# Indices Of Elements Under Construction. 148ec727ea7Spatrickclass GenericEnvironment: 149e5dd7070Spatrick def __init__(self, json_e): 150e5dd7070Spatrick self.frames = [EnvironmentFrame(f) for f in json_e] 151e5dd7070Spatrick 152e5dd7070Spatrick def diff_frames(self, prev): 153e5dd7070Spatrick # TODO: It's difficult to display a good diff when frame numbers shift. 154e5dd7070Spatrick if len(self.frames) != len(prev.frames): 155e5dd7070Spatrick return None 156e5dd7070Spatrick 157e5dd7070Spatrick updated = [] 158e5dd7070Spatrick for i in range(len(self.frames)): 159e5dd7070Spatrick f = self.frames[i] 160e5dd7070Spatrick prev_f = prev.frames[i] 161e5dd7070Spatrick if f.location_context == prev_f.location_context: 162e5dd7070Spatrick if f.is_different(prev_f): 163e5dd7070Spatrick updated.append(i) 164e5dd7070Spatrick else: 165e5dd7070Spatrick # We have the whole frame replaced with another frame. 166e5dd7070Spatrick # TODO: Produce a nice diff. 167e5dd7070Spatrick return None 168e5dd7070Spatrick 169e5dd7070Spatrick # TODO: Add support for added/removed. 170e5dd7070Spatrick return updated 171e5dd7070Spatrick 172e5dd7070Spatrick def is_different(self, prev): 173e5dd7070Spatrick updated = self.diff_frames(prev) 174e5dd7070Spatrick return updated is None or len(updated) > 0 175e5dd7070Spatrick 176e5dd7070Spatrick 177e5dd7070Spatrick# A single binding key in a deserialized RegionStore cluster. 178ec727ea7Spatrickclass StoreBindingKey: 179e5dd7070Spatrick def __init__(self, json_sk): 180e5dd7070Spatrick self.kind = json_sk['kind'] 181e5dd7070Spatrick self.offset = json_sk['offset'] 182e5dd7070Spatrick 183e5dd7070Spatrick def _key(self): 184e5dd7070Spatrick return (self.kind, self.offset) 185e5dd7070Spatrick 186e5dd7070Spatrick def __eq__(self, other): 187e5dd7070Spatrick return self._key() == other._key() 188e5dd7070Spatrick 189e5dd7070Spatrick def __hash__(self): 190e5dd7070Spatrick return hash(self._key()) 191e5dd7070Spatrick 192e5dd7070Spatrick 193e5dd7070Spatrick# A single cluster of the deserialized RegionStore. 194ec727ea7Spatrickclass StoreCluster: 195e5dd7070Spatrick def __init__(self, json_sc): 196e5dd7070Spatrick self.base_region = json_sc['cluster'] 197e5dd7070Spatrick self.bindings = collections.OrderedDict( 198e5dd7070Spatrick [(StoreBindingKey(b), b['value']) for b in json_sc['items']]) 199e5dd7070Spatrick 200e5dd7070Spatrick def diff_bindings(self, prev): 201e5dd7070Spatrick return diff_dicts(self.bindings, prev.bindings) 202e5dd7070Spatrick 203e5dd7070Spatrick def is_different(self, prev): 204e5dd7070Spatrick removed, added = self.diff_bindings(prev) 205e5dd7070Spatrick return len(removed) != 0 or len(added) != 0 206e5dd7070Spatrick 207e5dd7070Spatrick 208e5dd7070Spatrick# A deserialized RegionStore. 209ec727ea7Spatrickclass Store: 210e5dd7070Spatrick def __init__(self, json_s): 211e5dd7070Spatrick self.ptr = json_s['pointer'] 212e5dd7070Spatrick self.clusters = collections.OrderedDict( 213e5dd7070Spatrick [(c['pointer'], StoreCluster(c)) for c in json_s['items']]) 214e5dd7070Spatrick 215e5dd7070Spatrick def diff_clusters(self, prev): 216e5dd7070Spatrick removed = [k for k in prev.clusters if k not in self.clusters] 217e5dd7070Spatrick added = [k for k in self.clusters if k not in prev.clusters] 218e5dd7070Spatrick updated = [k for k in prev.clusters if k in self.clusters 219e5dd7070Spatrick and prev.clusters[k].is_different(self.clusters[k])] 220e5dd7070Spatrick return (removed, added, updated) 221e5dd7070Spatrick 222e5dd7070Spatrick def is_different(self, prev): 223e5dd7070Spatrick removed, added, updated = self.diff_clusters(prev) 224e5dd7070Spatrick return len(removed) != 0 or len(added) != 0 or len(updated) != 0 225e5dd7070Spatrick 226e5dd7070Spatrick 227e5dd7070Spatrick# Deserialized messages from a single checker in a single program state. 228e5dd7070Spatrick# Basically a list of raw strings. 229ec727ea7Spatrickclass CheckerLines: 230e5dd7070Spatrick def __init__(self, json_lines): 231e5dd7070Spatrick self.lines = json_lines 232e5dd7070Spatrick 233e5dd7070Spatrick def diff_lines(self, prev): 234e5dd7070Spatrick lines = difflib.ndiff(prev.lines, self.lines) 235e5dd7070Spatrick return [l.strip() for l in lines 236e5dd7070Spatrick if l.startswith('+') or l.startswith('-')] 237e5dd7070Spatrick 238e5dd7070Spatrick def is_different(self, prev): 239e5dd7070Spatrick return len(self.diff_lines(prev)) > 0 240e5dd7070Spatrick 241e5dd7070Spatrick 242e5dd7070Spatrick# Deserialized messages of all checkers, separated by checker. 243ec727ea7Spatrickclass CheckerMessages: 244e5dd7070Spatrick def __init__(self, json_m): 245e5dd7070Spatrick self.items = collections.OrderedDict( 246e5dd7070Spatrick [(m['checker'], CheckerLines(m['messages'])) for m in json_m]) 247e5dd7070Spatrick 248e5dd7070Spatrick def diff_messages(self, prev): 249e5dd7070Spatrick removed = [k for k in prev.items if k not in self.items] 250e5dd7070Spatrick added = [k for k in self.items if k not in prev.items] 251e5dd7070Spatrick updated = [k for k in prev.items if k in self.items 252e5dd7070Spatrick and prev.items[k].is_different(self.items[k])] 253e5dd7070Spatrick return (removed, added, updated) 254e5dd7070Spatrick 255e5dd7070Spatrick def is_different(self, prev): 256e5dd7070Spatrick removed, added, updated = self.diff_messages(prev) 257e5dd7070Spatrick return len(removed) != 0 or len(added) != 0 or len(updated) != 0 258e5dd7070Spatrick 259e5dd7070Spatrick 260e5dd7070Spatrick# A deserialized program state. 261ec727ea7Spatrickclass ProgramState: 262e5dd7070Spatrick def __init__(self, state_id, json_ps): 263e5dd7070Spatrick logging.debug('Adding ProgramState ' + str(state_id)) 264e5dd7070Spatrick 265*12c85518Srobert store_key = 'store' 266*12c85518Srobert env_key = 'environment' 267*12c85518Srobert constraints_key = 'constraints' 268*12c85518Srobert dyn_ty_key = 'dynamic_types' 269*12c85518Srobert ctor_key = 'constructing_objects' 270*12c85518Srobert ind_key = 'index_of_element' 271*12c85518Srobert init_loop_key = 'pending_init_loops' 272*12c85518Srobert dtor_key = 'pending_destructors' 273*12c85518Srobert msg_key = 'checker_messages' 274*12c85518Srobert 275e5dd7070Spatrick if json_ps is None: 276e5dd7070Spatrick json_ps = { 277*12c85518Srobert store_key: None, 278*12c85518Srobert env_key: None, 279*12c85518Srobert constraints_key: None, 280*12c85518Srobert dyn_ty_key: None, 281*12c85518Srobert ctor_key: None, 282*12c85518Srobert ind_key: None, 283*12c85518Srobert init_loop_key: None, 284*12c85518Srobert dtor_key: None, 285*12c85518Srobert msg_key: None 286e5dd7070Spatrick } 287e5dd7070Spatrick 288e5dd7070Spatrick self.state_id = state_id 289e5dd7070Spatrick 290*12c85518Srobert self.store = Store(json_ps[store_key]) \ 291*12c85518Srobert if json_ps[store_key] is not None else None 292e5dd7070Spatrick 293e5dd7070Spatrick self.environment = \ 294*12c85518Srobert GenericEnvironment(json_ps[env_key]['items']) \ 295*12c85518Srobert if json_ps[env_key] is not None else None 296e5dd7070Spatrick 297e5dd7070Spatrick self.constraints = GenericMap([ 298*12c85518Srobert (c['symbol'], c['range']) for c in json_ps[constraints_key] 299*12c85518Srobert ]) if json_ps[constraints_key] is not None else None 300e5dd7070Spatrick 301e5dd7070Spatrick self.dynamic_types = GenericMap([ 302e5dd7070Spatrick (t['region'], '%s%s' % (t['dyn_type'], 303e5dd7070Spatrick ' (or a sub-class)' 304e5dd7070Spatrick if t['sub_classable'] else '')) 305*12c85518Srobert for t in json_ps[dyn_ty_key]]) \ 306*12c85518Srobert if json_ps[dyn_ty_key] is not None else None 307*12c85518Srobert 308*12c85518Srobert self.checker_messages = CheckerMessages(json_ps[msg_key]) \ 309*12c85518Srobert if json_ps[msg_key] is not None else None 310*12c85518Srobert 311*12c85518Srobert # State traits 312*12c85518Srobert # 313*12c85518Srobert # For traits we always check if a key exists because if a trait 314*12c85518Srobert # has no imformation, nothing will be printed in the .dot file 315*12c85518Srobert # we parse. 316e5dd7070Spatrick 317e5dd7070Spatrick self.constructing_objects = \ 318*12c85518Srobert GenericEnvironment(json_ps[ctor_key]) \ 319*12c85518Srobert if ctor_key in json_ps and json_ps[ctor_key] is not None else None 320e5dd7070Spatrick 321*12c85518Srobert self.index_of_element = \ 322*12c85518Srobert GenericEnvironment(json_ps[ind_key]) \ 323*12c85518Srobert if ind_key in json_ps and json_ps[ind_key] is not None else None 324*12c85518Srobert 325*12c85518Srobert self.pending_init_loops = \ 326*12c85518Srobert GenericEnvironment(json_ps[init_loop_key]) \ 327*12c85518Srobert if init_loop_key in json_ps and json_ps[init_loop_key] is not None else None 328*12c85518Srobert 329*12c85518Srobert self.pending_destructors = \ 330*12c85518Srobert GenericEnvironment(json_ps[dtor_key]) \ 331*12c85518Srobert if dtor_key in json_ps and json_ps[dtor_key] is not None else None 332e5dd7070Spatrick 333e5dd7070Spatrick 334e5dd7070Spatrick# A deserialized exploded graph node. Has a default constructor because it 335e5dd7070Spatrick# may be referenced as part of an edge before its contents are deserialized, 336e5dd7070Spatrick# and in this moment we already need a room for predecessors and successors. 337ec727ea7Spatrickclass ExplodedNode: 338e5dd7070Spatrick def __init__(self): 339e5dd7070Spatrick self.predecessors = [] 340e5dd7070Spatrick self.successors = [] 341e5dd7070Spatrick 342e5dd7070Spatrick def construct(self, node_id, json_node): 343e5dd7070Spatrick logging.debug('Adding ' + node_id) 344e5dd7070Spatrick self.ptr = node_id[4:] 345e5dd7070Spatrick self.points = [ProgramPoint(p) for p in json_node['program_points']] 346e5dd7070Spatrick self.node_id = self.points[-1].node_id 347e5dd7070Spatrick self.state = ProgramState(json_node['state_id'], 348e5dd7070Spatrick json_node['program_state'] 349e5dd7070Spatrick if json_node['program_state'] is not None else None); 350e5dd7070Spatrick 351e5dd7070Spatrick assert self.node_name() == node_id 352e5dd7070Spatrick 353e5dd7070Spatrick def node_name(self): 354e5dd7070Spatrick return 'Node' + self.ptr 355e5dd7070Spatrick 356e5dd7070Spatrick 357e5dd7070Spatrick# A deserialized ExplodedGraph. Constructed by consuming a .dot file 358e5dd7070Spatrick# line-by-line. 359ec727ea7Spatrickclass ExplodedGraph: 360e5dd7070Spatrick # Parse .dot files with regular expressions. 361e5dd7070Spatrick node_re = re.compile( 362e5dd7070Spatrick '^(Node0x[0-9a-f]*) \\[shape=record,.*label="{(.*)\\\\l}"\\];$') 363e5dd7070Spatrick edge_re = re.compile( 364e5dd7070Spatrick '^(Node0x[0-9a-f]*) -> (Node0x[0-9a-f]*);$') 365e5dd7070Spatrick 366e5dd7070Spatrick def __init__(self): 367e5dd7070Spatrick self.nodes = collections.defaultdict(ExplodedNode) 368e5dd7070Spatrick self.root_id = None 369e5dd7070Spatrick self.incomplete_line = '' 370e5dd7070Spatrick 371e5dd7070Spatrick def add_raw_line(self, raw_line): 372e5dd7070Spatrick if raw_line.startswith('//'): 373e5dd7070Spatrick return 374e5dd7070Spatrick 375e5dd7070Spatrick # Allow line breaks by waiting for ';'. This is not valid in 376e5dd7070Spatrick # a .dot file, but it is useful for writing tests. 377e5dd7070Spatrick if len(raw_line) > 0 and raw_line[-1] != ';': 378e5dd7070Spatrick self.incomplete_line += raw_line 379e5dd7070Spatrick return 380e5dd7070Spatrick raw_line = self.incomplete_line + raw_line 381e5dd7070Spatrick self.incomplete_line = '' 382e5dd7070Spatrick 383e5dd7070Spatrick # Apply regexps one by one to see if it's a node or an edge 384e5dd7070Spatrick # and extract contents if necessary. 385e5dd7070Spatrick logging.debug('Line: ' + raw_line) 386e5dd7070Spatrick result = self.edge_re.match(raw_line) 387e5dd7070Spatrick if result is not None: 388e5dd7070Spatrick logging.debug('Classified as edge line.') 389e5dd7070Spatrick pred = result.group(1) 390e5dd7070Spatrick succ = result.group(2) 391e5dd7070Spatrick self.nodes[pred].successors.append(succ) 392e5dd7070Spatrick self.nodes[succ].predecessors.append(pred) 393e5dd7070Spatrick return 394e5dd7070Spatrick result = self.node_re.match(raw_line) 395e5dd7070Spatrick if result is not None: 396e5dd7070Spatrick logging.debug('Classified as node line.') 397e5dd7070Spatrick node_id = result.group(1) 398e5dd7070Spatrick if len(self.nodes) == 0: 399e5dd7070Spatrick self.root_id = node_id 400e5dd7070Spatrick # Note: when writing tests you don't need to escape everything, 401e5dd7070Spatrick # even though in a valid dot file everything is escaped. 402ec727ea7Spatrick node_label = result.group(2).replace(' ', '') \ 403e5dd7070Spatrick .replace('\\"', '"') \ 404e5dd7070Spatrick .replace('\\{', '{') \ 405e5dd7070Spatrick .replace('\\}', '}') \ 406e5dd7070Spatrick .replace('\\\\', '\\') \ 407e5dd7070Spatrick .replace('\\|', '|') \ 408e5dd7070Spatrick .replace('\\<', '\\\\<') \ 409e5dd7070Spatrick .replace('\\>', '\\\\>') \ 410e5dd7070Spatrick .rstrip(',') 411ec727ea7Spatrick # Handle `\l` separately because a string literal can be in code 412ec727ea7Spatrick # like "string\\literal" with the `\l` inside. 413ec727ea7Spatrick # Also on Windows macros __FILE__ produces specific delimiters `\` 414ec727ea7Spatrick # and a directory or file may starts with the letter `l`. 415ec727ea7Spatrick # Find all `\l` (like `,\l`, `}\l`, `[\l`) except `\\l`, 416*12c85518Srobert # because the literal as a rule contains multiple `\` before `\l`. 417ec727ea7Spatrick node_label = re.sub(r'(?<!\\)\\l', '', node_label) 418e5dd7070Spatrick logging.debug(node_label) 419e5dd7070Spatrick json_node = json.loads(node_label) 420e5dd7070Spatrick self.nodes[node_id].construct(node_id, json_node) 421e5dd7070Spatrick return 422e5dd7070Spatrick logging.debug('Skipping.') 423e5dd7070Spatrick 424e5dd7070Spatrick 425e5dd7070Spatrick#===-----------------------------------------------------------------------===# 426e5dd7070Spatrick# Visitors traverse a deserialized ExplodedGraph and do different things 427e5dd7070Spatrick# with every node and edge. 428e5dd7070Spatrick#===-----------------------------------------------------------------------===# 429e5dd7070Spatrick 430e5dd7070Spatrick 431e5dd7070Spatrick# A visitor that dumps the ExplodedGraph into a DOT file with fancy HTML-based 432e5dd7070Spatrick# syntax highlighing. 433ec727ea7Spatrickclass DotDumpVisitor: 434e5dd7070Spatrick def __init__(self, do_diffs, dark_mode, gray_mode, 435e5dd7070Spatrick topo_mode, dump_dot_only): 436e5dd7070Spatrick self._do_diffs = do_diffs 437e5dd7070Spatrick self._dark_mode = dark_mode 438e5dd7070Spatrick self._gray_mode = gray_mode 439e5dd7070Spatrick self._topo_mode = topo_mode 440e5dd7070Spatrick self._dump_dot_only = dump_dot_only 441e5dd7070Spatrick self._output = [] 442e5dd7070Spatrick 443e5dd7070Spatrick def _dump_raw(self, s): 444e5dd7070Spatrick if self._dump_dot_only: 445e5dd7070Spatrick print(s, end='') 446e5dd7070Spatrick else: 447e5dd7070Spatrick self._output.append(s) 448e5dd7070Spatrick 449e5dd7070Spatrick def output(self): 450e5dd7070Spatrick assert not self._dump_dot_only 451e5dd7070Spatrick return ''.join(self._output) 452e5dd7070Spatrick 453e5dd7070Spatrick def _dump(self, s): 454e5dd7070Spatrick s = s.replace('&', '&') \ 455e5dd7070Spatrick .replace('{', '\\{') \ 456e5dd7070Spatrick .replace('}', '\\}') \ 457e5dd7070Spatrick .replace('\\<', '<') \ 458e5dd7070Spatrick .replace('\\>', '>') \ 459e5dd7070Spatrick .replace('|', '\\|') 460ec727ea7Spatrick s = re.sub(r'(?<!\\)\\l', '<br />', s) 461e5dd7070Spatrick if self._gray_mode: 462e5dd7070Spatrick s = re.sub(r'<font color="[a-z0-9]*">', '', s) 463e5dd7070Spatrick s = re.sub(r'</font>', '', s) 464e5dd7070Spatrick self._dump_raw(s) 465e5dd7070Spatrick 466e5dd7070Spatrick @staticmethod 467e5dd7070Spatrick def _diff_plus_minus(is_added): 468e5dd7070Spatrick if is_added is None: 469e5dd7070Spatrick return '' 470e5dd7070Spatrick if is_added: 471e5dd7070Spatrick return '<font color="forestgreen">+</font>' 472e5dd7070Spatrick return '<font color="red">-</font>' 473e5dd7070Spatrick 474e5dd7070Spatrick @staticmethod 475e5dd7070Spatrick def _short_pretty(s): 476e5dd7070Spatrick if s is None: 477e5dd7070Spatrick return None 478e5dd7070Spatrick if len(s) < 20: 479e5dd7070Spatrick return s 480e5dd7070Spatrick left = s.find('{') 481e5dd7070Spatrick right = s.rfind('}') 482e5dd7070Spatrick if left == -1 or right == -1 or left >= right: 483e5dd7070Spatrick return s 484e5dd7070Spatrick candidate = s[0:left + 1] + ' ... ' + s[right:] 485e5dd7070Spatrick if len(candidate) >= len(s): 486e5dd7070Spatrick return s 487e5dd7070Spatrick return candidate 488e5dd7070Spatrick 489e5dd7070Spatrick @staticmethod 490e5dd7070Spatrick def _make_sloc(loc): 491e5dd7070Spatrick if loc is None: 492e5dd7070Spatrick return '<i>Invalid Source Location</i>' 493e5dd7070Spatrick 494e5dd7070Spatrick def make_plain_loc(loc): 495e5dd7070Spatrick return '%s:<b>%s</b>:<b>%s</b>' \ 496e5dd7070Spatrick % (loc.filename, loc.line, loc.col) 497e5dd7070Spatrick 498e5dd7070Spatrick if loc.is_macro(): 499e5dd7070Spatrick return '%s <font color="royalblue1">' \ 500e5dd7070Spatrick '(<i>spelling at </i> %s)</font>' \ 501e5dd7070Spatrick % (make_plain_loc(loc), make_plain_loc(loc.spelling)) 502e5dd7070Spatrick 503e5dd7070Spatrick return make_plain_loc(loc) 504e5dd7070Spatrick 505e5dd7070Spatrick def visit_begin_graph(self, graph): 506e5dd7070Spatrick self._graph = graph 507e5dd7070Spatrick self._dump_raw('digraph "ExplodedGraph" {\n') 508e5dd7070Spatrick if self._dark_mode: 509e5dd7070Spatrick self._dump_raw('bgcolor="gray10";\n') 510e5dd7070Spatrick self._dump_raw('label="";\n') 511e5dd7070Spatrick 512e5dd7070Spatrick def visit_program_point(self, p): 513e5dd7070Spatrick if p.kind in ['Edge', 'BlockEntrance', 'BlockExit']: 514e5dd7070Spatrick color = 'gold3' 515e5dd7070Spatrick elif p.kind in ['PreStmtPurgeDeadSymbols', 516e5dd7070Spatrick 'PostStmtPurgeDeadSymbols']: 517e5dd7070Spatrick color = 'red' 518e5dd7070Spatrick elif p.kind in ['CallEnter', 'CallExitBegin', 'CallExitEnd']: 519e5dd7070Spatrick color = 'dodgerblue' if self._dark_mode else 'blue' 520e5dd7070Spatrick elif p.kind in ['Statement']: 521e5dd7070Spatrick color = 'cyan4' 522e5dd7070Spatrick else: 523e5dd7070Spatrick color = 'forestgreen' 524e5dd7070Spatrick 525e5dd7070Spatrick self._dump('<tr><td align="left">%s.</td>' % p.node_id) 526e5dd7070Spatrick 527e5dd7070Spatrick if p.kind == 'Statement': 528e5dd7070Spatrick # This avoids pretty-printing huge statements such as CompoundStmt. 529e5dd7070Spatrick # Such statements show up only at [Pre|Post]StmtPurgeDeadSymbols 530e5dd7070Spatrick skip_pretty = 'PurgeDeadSymbols' in p.stmt_point_kind 531e5dd7070Spatrick stmt_color = 'cyan3' 532e5dd7070Spatrick self._dump('<td align="left" width="0">%s:</td>' 533e5dd7070Spatrick '<td align="left" width="0"><font color="%s">' 534e5dd7070Spatrick '%s</font> </td>' 535e5dd7070Spatrick '<td align="left"><i>S%s</i></td>' 536e5dd7070Spatrick '<td align="left"><font color="%s">%s</font></td>' 537e5dd7070Spatrick '<td align="left">%s</td></tr>' 538e5dd7070Spatrick % (self._make_sloc(p.loc), color, 539e5dd7070Spatrick '%s (%s)' % (p.stmt_kind, p.cast_kind) 540e5dd7070Spatrick if p.cast_kind is not None else p.stmt_kind, 541e5dd7070Spatrick p.stmt_id, stmt_color, p.stmt_point_kind, 542e5dd7070Spatrick self._short_pretty(p.pretty) 543e5dd7070Spatrick if not skip_pretty else '')) 544e5dd7070Spatrick elif p.kind == 'Edge': 545e5dd7070Spatrick self._dump('<td width="0"></td>' 546e5dd7070Spatrick '<td align="left" width="0">' 547e5dd7070Spatrick '<font color="%s">%s</font></td><td align="left">' 548e5dd7070Spatrick '[B%d] -\\> [B%d]</td></tr>' 549e5dd7070Spatrick % (color, 'BlockEdge', p.src_id, p.dst_id)) 550e5dd7070Spatrick elif p.kind == 'BlockEntrance': 551e5dd7070Spatrick self._dump('<td width="0"></td>' 552e5dd7070Spatrick '<td align="left" width="0">' 553e5dd7070Spatrick '<font color="%s">%s</font></td>' 554e5dd7070Spatrick '<td align="left">[B%d]</td></tr>' 555e5dd7070Spatrick % (color, p.kind, p.block_id)) 556e5dd7070Spatrick else: 557e5dd7070Spatrick # TODO: Print more stuff for other kinds of points. 558e5dd7070Spatrick self._dump('<td width="0"></td>' 559e5dd7070Spatrick '<td align="left" width="0" colspan="2">' 560e5dd7070Spatrick '<font color="%s">%s</font></td></tr>' 561e5dd7070Spatrick % (color, p.kind)) 562e5dd7070Spatrick 563e5dd7070Spatrick if p.tag is not None: 564e5dd7070Spatrick self._dump('<tr><td width="0"></td><td width="0"></td>' 565e5dd7070Spatrick '<td colspan="3" align="left">' 566e5dd7070Spatrick '<b>Tag: </b> <font color="crimson">' 567e5dd7070Spatrick '%s</font></td></tr>' % p.tag) 568e5dd7070Spatrick 569e5dd7070Spatrick if p.has_report: 570e5dd7070Spatrick self._dump('<tr><td width="0"></td><td width="0"></td>' 571e5dd7070Spatrick '<td colspan="3" align="left">' 572e5dd7070Spatrick '<font color="red"><b>Bug Report Attached' 573e5dd7070Spatrick '</b></font></td></tr>') 574e5dd7070Spatrick if p.is_sink: 575e5dd7070Spatrick self._dump('<tr><td width="0"></td><td width="0"></td>' 576e5dd7070Spatrick '<td colspan="3" align="left">' 577e5dd7070Spatrick '<font color="cornflowerblue"><b>Sink Node' 578e5dd7070Spatrick '</b></font></td></tr>') 579e5dd7070Spatrick 580e5dd7070Spatrick def visit_environment(self, e, prev_e=None): 581e5dd7070Spatrick self._dump('<table border="0">') 582e5dd7070Spatrick 583e5dd7070Spatrick def dump_location_context(lc, is_added=None): 584e5dd7070Spatrick self._dump('<tr><td>%s</td>' 585e5dd7070Spatrick '<td align="left"><b>%s</b></td>' 586e5dd7070Spatrick '<td align="left" colspan="2">' 587e5dd7070Spatrick '<font color="gray60">%s </font>' 588e5dd7070Spatrick '%s</td></tr>' 589e5dd7070Spatrick % (self._diff_plus_minus(is_added), 590e5dd7070Spatrick lc.caption, lc.decl, 591e5dd7070Spatrick ('(%s)' % self._make_sloc(lc.loc)) 592e5dd7070Spatrick if lc.loc is not None else '')) 593e5dd7070Spatrick 594e5dd7070Spatrick def dump_binding(f, b, is_added=None): 595e5dd7070Spatrick self._dump('<tr><td>%s</td>' 596e5dd7070Spatrick '<td align="left"><i>S%s</i></td>' 597e5dd7070Spatrick '%s' 598e5dd7070Spatrick '<td align="left">%s</td>' 599e5dd7070Spatrick '<td align="left">%s</td></tr>' 600e5dd7070Spatrick % (self._diff_plus_minus(is_added), 601e5dd7070Spatrick b.stmt_id, 602e5dd7070Spatrick '<td align="left"><font color="%s"><i>' 603e5dd7070Spatrick '%s</i></font></td>' % ( 604e5dd7070Spatrick 'lavender' if self._dark_mode else 'darkgreen', 605e5dd7070Spatrick ('(%s)' % b.kind) if b.kind is not None else ' ' 606e5dd7070Spatrick ), 607e5dd7070Spatrick self._short_pretty(b.pretty), f.bindings[b])) 608e5dd7070Spatrick 609e5dd7070Spatrick frames_updated = e.diff_frames(prev_e) if prev_e is not None else None 610e5dd7070Spatrick if frames_updated: 611e5dd7070Spatrick for i in frames_updated: 612e5dd7070Spatrick f = e.frames[i] 613e5dd7070Spatrick prev_f = prev_e.frames[i] 614e5dd7070Spatrick dump_location_context(f.location_context) 615e5dd7070Spatrick bindings_removed, bindings_added = f.diff_bindings(prev_f) 616e5dd7070Spatrick for b in bindings_removed: 617e5dd7070Spatrick dump_binding(prev_f, b, False) 618e5dd7070Spatrick for b in bindings_added: 619e5dd7070Spatrick dump_binding(f, b, True) 620e5dd7070Spatrick else: 621e5dd7070Spatrick for f in e.frames: 622e5dd7070Spatrick dump_location_context(f.location_context) 623e5dd7070Spatrick for b in f.bindings: 624e5dd7070Spatrick dump_binding(f, b) 625e5dd7070Spatrick 626e5dd7070Spatrick self._dump('</table>') 627e5dd7070Spatrick 628e5dd7070Spatrick def visit_environment_in_state(self, selector, title, s, prev_s=None): 629e5dd7070Spatrick e = getattr(s, selector) 630e5dd7070Spatrick prev_e = getattr(prev_s, selector) if prev_s is not None else None 631e5dd7070Spatrick if e is None and prev_e is None: 632e5dd7070Spatrick return 633e5dd7070Spatrick 634e5dd7070Spatrick self._dump('<hr /><tr><td align="left"><b>%s: </b>' % title) 635e5dd7070Spatrick if e is None: 636e5dd7070Spatrick self._dump('<i> Nothing!</i>') 637e5dd7070Spatrick else: 638e5dd7070Spatrick if prev_e is not None: 639e5dd7070Spatrick if e.is_different(prev_e): 640e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 641e5dd7070Spatrick self.visit_environment(e, prev_e) 642e5dd7070Spatrick else: 643e5dd7070Spatrick self._dump('<i> No changes!</i>') 644e5dd7070Spatrick else: 645e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 646e5dd7070Spatrick self.visit_environment(e) 647e5dd7070Spatrick 648e5dd7070Spatrick self._dump('</td></tr>') 649e5dd7070Spatrick 650e5dd7070Spatrick def visit_store(self, s, prev_s=None): 651e5dd7070Spatrick self._dump('<table border="0">') 652e5dd7070Spatrick 653e5dd7070Spatrick def dump_binding(s, c, b, is_added=None): 654e5dd7070Spatrick self._dump('<tr><td>%s</td>' 655e5dd7070Spatrick '<td align="left">%s</td>' 656e5dd7070Spatrick '<td align="left">%s</td>' 657e5dd7070Spatrick '<td align="left">%s</td>' 658e5dd7070Spatrick '<td align="left">%s</td></tr>' 659e5dd7070Spatrick % (self._diff_plus_minus(is_added), 660e5dd7070Spatrick s.clusters[c].base_region, b.offset, 661e5dd7070Spatrick '(<i>Default</i>)' if b.kind == 'Default' 662e5dd7070Spatrick else '', 663e5dd7070Spatrick s.clusters[c].bindings[b])) 664e5dd7070Spatrick 665e5dd7070Spatrick if prev_s is not None: 666e5dd7070Spatrick clusters_removed, clusters_added, clusters_updated = \ 667e5dd7070Spatrick s.diff_clusters(prev_s) 668e5dd7070Spatrick for c in clusters_removed: 669e5dd7070Spatrick for b in prev_s.clusters[c].bindings: 670e5dd7070Spatrick dump_binding(prev_s, c, b, False) 671e5dd7070Spatrick for c in clusters_updated: 672e5dd7070Spatrick bindings_removed, bindings_added = \ 673e5dd7070Spatrick s.clusters[c].diff_bindings(prev_s.clusters[c]) 674e5dd7070Spatrick for b in bindings_removed: 675e5dd7070Spatrick dump_binding(prev_s, c, b, False) 676e5dd7070Spatrick for b in bindings_added: 677e5dd7070Spatrick dump_binding(s, c, b, True) 678e5dd7070Spatrick for c in clusters_added: 679e5dd7070Spatrick for b in s.clusters[c].bindings: 680e5dd7070Spatrick dump_binding(s, c, b, True) 681e5dd7070Spatrick else: 682e5dd7070Spatrick for c in s.clusters: 683e5dd7070Spatrick for b in s.clusters[c].bindings: 684e5dd7070Spatrick dump_binding(s, c, b) 685e5dd7070Spatrick 686e5dd7070Spatrick self._dump('</table>') 687e5dd7070Spatrick 688e5dd7070Spatrick def visit_store_in_state(self, s, prev_s=None): 689e5dd7070Spatrick st = s.store 690e5dd7070Spatrick prev_st = prev_s.store if prev_s is not None else None 691e5dd7070Spatrick if st is None and prev_st is None: 692e5dd7070Spatrick return 693e5dd7070Spatrick 694e5dd7070Spatrick self._dump('<hr /><tr><td align="left"><b>Store: </b>') 695e5dd7070Spatrick if st is None: 696e5dd7070Spatrick self._dump('<i> Nothing!</i>') 697e5dd7070Spatrick else: 698e5dd7070Spatrick if self._dark_mode: 699e5dd7070Spatrick self._dump(' <font color="gray30">(%s)</font>' % st.ptr) 700e5dd7070Spatrick else: 701e5dd7070Spatrick self._dump(' <font color="gray">(%s)</font>' % st.ptr) 702e5dd7070Spatrick if prev_st is not None: 703e5dd7070Spatrick if s.store.is_different(prev_st): 704e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 705e5dd7070Spatrick self.visit_store(st, prev_st) 706e5dd7070Spatrick else: 707e5dd7070Spatrick self._dump('<i> No changes!</i>') 708e5dd7070Spatrick else: 709e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 710e5dd7070Spatrick self.visit_store(st) 711e5dd7070Spatrick self._dump('</td></tr>') 712e5dd7070Spatrick 713e5dd7070Spatrick def visit_generic_map(self, m, prev_m=None): 714e5dd7070Spatrick self._dump('<table border="0">') 715e5dd7070Spatrick 716e5dd7070Spatrick def dump_pair(m, k, is_added=None): 717e5dd7070Spatrick self._dump('<tr><td>%s</td>' 718e5dd7070Spatrick '<td align="left">%s</td>' 719e5dd7070Spatrick '<td align="left">%s</td></tr>' 720e5dd7070Spatrick % (self._diff_plus_minus(is_added), 721e5dd7070Spatrick k, m.generic_map[k])) 722e5dd7070Spatrick 723e5dd7070Spatrick if prev_m is not None: 724e5dd7070Spatrick removed, added = m.diff(prev_m) 725e5dd7070Spatrick for k in removed: 726e5dd7070Spatrick dump_pair(prev_m, k, False) 727e5dd7070Spatrick for k in added: 728e5dd7070Spatrick dump_pair(m, k, True) 729e5dd7070Spatrick else: 730e5dd7070Spatrick for k in m.generic_map: 731e5dd7070Spatrick dump_pair(m, k, None) 732e5dd7070Spatrick 733e5dd7070Spatrick self._dump('</table>') 734e5dd7070Spatrick 735e5dd7070Spatrick def visit_generic_map_in_state(self, selector, title, s, prev_s=None): 736e5dd7070Spatrick m = getattr(s, selector) 737e5dd7070Spatrick prev_m = getattr(prev_s, selector) if prev_s is not None else None 738e5dd7070Spatrick if m is None and prev_m is None: 739e5dd7070Spatrick return 740e5dd7070Spatrick 741e5dd7070Spatrick self._dump('<hr />') 742e5dd7070Spatrick self._dump('<tr><td align="left">' 743e5dd7070Spatrick '<b>%s: </b>' % title) 744e5dd7070Spatrick if m is None: 745e5dd7070Spatrick self._dump('<i> Nothing!</i>') 746e5dd7070Spatrick else: 747e5dd7070Spatrick if prev_m is not None: 748e5dd7070Spatrick if m.is_different(prev_m): 749e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 750e5dd7070Spatrick self.visit_generic_map(m, prev_m) 751e5dd7070Spatrick else: 752e5dd7070Spatrick self._dump('<i> No changes!</i>') 753e5dd7070Spatrick else: 754e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 755e5dd7070Spatrick self.visit_generic_map(m) 756e5dd7070Spatrick 757e5dd7070Spatrick self._dump('</td></tr>') 758e5dd7070Spatrick 759e5dd7070Spatrick def visit_checker_messages(self, m, prev_m=None): 760e5dd7070Spatrick self._dump('<table border="0">') 761e5dd7070Spatrick 762e5dd7070Spatrick def dump_line(l, is_added=None): 763e5dd7070Spatrick self._dump('<tr><td>%s</td>' 764e5dd7070Spatrick '<td align="left">%s</td></tr>' 765e5dd7070Spatrick % (self._diff_plus_minus(is_added), l)) 766e5dd7070Spatrick 767e5dd7070Spatrick def dump_chk(chk, is_added=None): 768e5dd7070Spatrick dump_line('<i>%s</i>:' % chk, is_added) 769e5dd7070Spatrick 770e5dd7070Spatrick if prev_m is not None: 771e5dd7070Spatrick removed, added, updated = m.diff_messages(prev_m) 772e5dd7070Spatrick for chk in removed: 773e5dd7070Spatrick dump_chk(chk, False) 774e5dd7070Spatrick for l in prev_m.items[chk].lines: 775e5dd7070Spatrick dump_line(l, False) 776e5dd7070Spatrick for chk in updated: 777e5dd7070Spatrick dump_chk(chk) 778e5dd7070Spatrick for l in m.items[chk].diff_lines(prev_m.items[chk]): 779e5dd7070Spatrick dump_line(l[1:], l.startswith('+')) 780e5dd7070Spatrick for chk in added: 781e5dd7070Spatrick dump_chk(chk, True) 782e5dd7070Spatrick for l in m.items[chk].lines: 783e5dd7070Spatrick dump_line(l, True) 784e5dd7070Spatrick else: 785e5dd7070Spatrick for chk in m.items: 786e5dd7070Spatrick dump_chk(chk) 787e5dd7070Spatrick for l in m.items[chk].lines: 788e5dd7070Spatrick dump_line(l) 789e5dd7070Spatrick 790e5dd7070Spatrick self._dump('</table>') 791e5dd7070Spatrick 792e5dd7070Spatrick def visit_checker_messages_in_state(self, s, prev_s=None): 793e5dd7070Spatrick m = s.checker_messages 794e5dd7070Spatrick prev_m = prev_s.checker_messages if prev_s is not None else None 795e5dd7070Spatrick if m is None and prev_m is None: 796e5dd7070Spatrick return 797e5dd7070Spatrick 798e5dd7070Spatrick self._dump('<hr />') 799e5dd7070Spatrick self._dump('<tr><td align="left">' 800e5dd7070Spatrick '<b>Checker State: </b>') 801e5dd7070Spatrick if m is None: 802e5dd7070Spatrick self._dump('<i> Nothing!</i>') 803e5dd7070Spatrick else: 804e5dd7070Spatrick if prev_m is not None: 805e5dd7070Spatrick if m.is_different(prev_m): 806e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 807e5dd7070Spatrick self.visit_checker_messages(m, prev_m) 808e5dd7070Spatrick else: 809e5dd7070Spatrick self._dump('<i> No changes!</i>') 810e5dd7070Spatrick else: 811e5dd7070Spatrick self._dump('</td></tr><tr><td align="left">') 812e5dd7070Spatrick self.visit_checker_messages(m) 813e5dd7070Spatrick 814e5dd7070Spatrick self._dump('</td></tr>') 815e5dd7070Spatrick 816e5dd7070Spatrick def visit_state(self, s, prev_s): 817e5dd7070Spatrick self.visit_store_in_state(s, prev_s) 818e5dd7070Spatrick self.visit_environment_in_state('environment', 'Expressions', 819e5dd7070Spatrick s, prev_s) 820e5dd7070Spatrick self.visit_generic_map_in_state('constraints', 'Ranges', 821e5dd7070Spatrick s, prev_s) 822e5dd7070Spatrick self.visit_generic_map_in_state('dynamic_types', 'Dynamic Types', 823e5dd7070Spatrick s, prev_s) 824e5dd7070Spatrick self.visit_environment_in_state('constructing_objects', 825e5dd7070Spatrick 'Objects Under Construction', 826e5dd7070Spatrick s, prev_s) 827*12c85518Srobert self.visit_environment_in_state('index_of_element', 828*12c85518Srobert 'Indices Of Elements Under Construction', 829*12c85518Srobert s, prev_s) 830*12c85518Srobert self.visit_environment_in_state('pending_init_loops', 831*12c85518Srobert 'Pending Array Init Loop Expressions', 832*12c85518Srobert s, prev_s) 833*12c85518Srobert self.visit_environment_in_state('pending_destructors', 834*12c85518Srobert 'Indices of Elements Under Destruction', 835*12c85518Srobert s, prev_s) 836e5dd7070Spatrick self.visit_checker_messages_in_state(s, prev_s) 837e5dd7070Spatrick 838e5dd7070Spatrick def visit_node(self, node): 839e5dd7070Spatrick self._dump('%s [shape=record,' 840e5dd7070Spatrick % (node.node_name())) 841e5dd7070Spatrick if self._dark_mode: 842e5dd7070Spatrick self._dump('color="white",fontcolor="gray80",') 843e5dd7070Spatrick self._dump('label=<<table border="0">') 844e5dd7070Spatrick 845e5dd7070Spatrick self._dump('<tr><td bgcolor="%s"><b>State %s</b></td></tr>' 846e5dd7070Spatrick % ("gray20" if self._dark_mode else "gray70", 847e5dd7070Spatrick node.state.state_id 848e5dd7070Spatrick if node.state is not None else 'Unspecified')) 849e5dd7070Spatrick if not self._topo_mode: 850e5dd7070Spatrick self._dump('<tr><td align="left" width="0">') 851e5dd7070Spatrick if len(node.points) > 1: 852e5dd7070Spatrick self._dump('<b>Program points:</b></td></tr>') 853e5dd7070Spatrick else: 854e5dd7070Spatrick self._dump('<b>Program point:</b></td></tr>') 855e5dd7070Spatrick self._dump('<tr><td align="left" width="0">' 856e5dd7070Spatrick '<table border="0" align="left" width="0">') 857e5dd7070Spatrick for p in node.points: 858e5dd7070Spatrick self.visit_program_point(p) 859e5dd7070Spatrick self._dump('</table></td></tr>') 860e5dd7070Spatrick 861e5dd7070Spatrick if node.state is not None and not self._topo_mode: 862e5dd7070Spatrick prev_s = None 863e5dd7070Spatrick # Do diffs only when we have a unique predecessor. 864e5dd7070Spatrick # Don't do diffs on the leaf nodes because they're 865e5dd7070Spatrick # the important ones. 866e5dd7070Spatrick if self._do_diffs and len(node.predecessors) == 1 \ 867e5dd7070Spatrick and len(node.successors) > 0: 868e5dd7070Spatrick prev_s = self._graph.nodes[node.predecessors[0]].state 869e5dd7070Spatrick self.visit_state(node.state, prev_s) 870e5dd7070Spatrick self._dump_raw('</table>>];\n') 871e5dd7070Spatrick 872e5dd7070Spatrick def visit_edge(self, pred, succ): 873e5dd7070Spatrick self._dump_raw('%s -> %s%s;\n' % ( 874e5dd7070Spatrick pred.node_name(), succ.node_name(), 875e5dd7070Spatrick ' [color="white"]' if self._dark_mode else '' 876e5dd7070Spatrick )) 877e5dd7070Spatrick 878e5dd7070Spatrick def visit_end_of_graph(self): 879e5dd7070Spatrick self._dump_raw('}\n') 880e5dd7070Spatrick 881e5dd7070Spatrick if not self._dump_dot_only: 882e5dd7070Spatrick import sys 883e5dd7070Spatrick import tempfile 884e5dd7070Spatrick 885*12c85518Srobert def write_temp_file(suffix, prefix, data): 886*12c85518Srobert fd, filename = tempfile.mkstemp(suffix, prefix, '.', True) 887e5dd7070Spatrick print('Writing "%s"...' % filename) 888e5dd7070Spatrick with os.fdopen(fd, 'w') as fp: 889e5dd7070Spatrick fp.write(data) 890e5dd7070Spatrick print('Done! Please remember to remove the file.') 891e5dd7070Spatrick return filename 892e5dd7070Spatrick 893e5dd7070Spatrick try: 894e5dd7070Spatrick import graphviz 895e5dd7070Spatrick except ImportError: 896e5dd7070Spatrick # The fallback behavior if graphviz is not installed! 897e5dd7070Spatrick print('Python graphviz not found. Please invoke') 898e5dd7070Spatrick print(' $ pip install graphviz') 899e5dd7070Spatrick print('in order to enable automatic conversion to HTML.') 900e5dd7070Spatrick print() 901e5dd7070Spatrick print('You may also convert DOT to SVG manually via') 902e5dd7070Spatrick print(' $ dot -Tsvg input.dot -o output.svg') 903e5dd7070Spatrick print() 904*12c85518Srobert write_temp_file('.dot', 'egraph-', self.output()) 905e5dd7070Spatrick return 906e5dd7070Spatrick 907*12c85518Srobert svg = graphviz.pipe('dot', 'svg', self.output().encode()).decode() 908e5dd7070Spatrick 909e5dd7070Spatrick filename = write_temp_file( 910*12c85518Srobert '.html', 'egraph-', '<html><body bgcolor="%s">%s</body></html>' % ( 911e5dd7070Spatrick '#1a1a1a' if self._dark_mode else 'white', svg)) 912e5dd7070Spatrick if sys.platform == 'win32': 913e5dd7070Spatrick os.startfile(filename) 914e5dd7070Spatrick elif sys.platform == 'darwin': 915e5dd7070Spatrick os.system('open "%s"' % filename) 916e5dd7070Spatrick else: 917e5dd7070Spatrick os.system('xdg-open "%s"' % filename) 918e5dd7070Spatrick 919e5dd7070Spatrick 920e5dd7070Spatrick#===-----------------------------------------------------------------------===# 921e5dd7070Spatrick# Explorers know how to traverse the ExplodedGraph in a certain order. 922e5dd7070Spatrick# They would invoke a Visitor on every node or edge they encounter. 923e5dd7070Spatrick#===-----------------------------------------------------------------------===# 924e5dd7070Spatrick 925e5dd7070Spatrick 926e5dd7070Spatrick# BasicExplorer explores the whole graph in no particular order. 927ec727ea7Spatrickclass BasicExplorer: 928e5dd7070Spatrick def explore(self, graph, visitor): 929e5dd7070Spatrick visitor.visit_begin_graph(graph) 930e5dd7070Spatrick for node in sorted(graph.nodes): 931e5dd7070Spatrick logging.debug('Visiting ' + node) 932e5dd7070Spatrick visitor.visit_node(graph.nodes[node]) 933e5dd7070Spatrick for succ in sorted(graph.nodes[node].successors): 934e5dd7070Spatrick logging.debug('Visiting edge: %s -> %s ' % (node, succ)) 935e5dd7070Spatrick visitor.visit_edge(graph.nodes[node], graph.nodes[succ]) 936e5dd7070Spatrick visitor.visit_end_of_graph() 937e5dd7070Spatrick 938e5dd7070Spatrick 939e5dd7070Spatrick#===-----------------------------------------------------------------------===# 940e5dd7070Spatrick# Trimmers cut out parts of the ExplodedGraph so that to focus on other parts. 941e5dd7070Spatrick# Trimmers can be combined together by applying them sequentially. 942e5dd7070Spatrick#===-----------------------------------------------------------------------===# 943e5dd7070Spatrick 944e5dd7070Spatrick 945e5dd7070Spatrick# SinglePathTrimmer keeps only a single path - the leftmost path from the root. 946e5dd7070Spatrick# Useful when the trimmed graph is still too large. 947ec727ea7Spatrickclass SinglePathTrimmer: 948e5dd7070Spatrick def trim(self, graph): 949e5dd7070Spatrick visited_nodes = set() 950e5dd7070Spatrick node_id = graph.root_id 951e5dd7070Spatrick while True: 952e5dd7070Spatrick visited_nodes.add(node_id) 953e5dd7070Spatrick node = graph.nodes[node_id] 954e5dd7070Spatrick if len(node.successors) > 0: 955e5dd7070Spatrick succ_id = node.successors[0] 956e5dd7070Spatrick succ = graph.nodes[succ_id] 957e5dd7070Spatrick node.successors = [succ_id] 958e5dd7070Spatrick succ.predecessors = [node_id] 959e5dd7070Spatrick if succ_id in visited_nodes: 960e5dd7070Spatrick break 961e5dd7070Spatrick node_id = succ_id 962e5dd7070Spatrick else: 963e5dd7070Spatrick break 964e5dd7070Spatrick graph.nodes = {node_id: graph.nodes[node_id] 965e5dd7070Spatrick for node_id in visited_nodes} 966e5dd7070Spatrick 967e5dd7070Spatrick 968e5dd7070Spatrick# TargetedTrimmer keeps paths that lead to specific nodes and discards all 969e5dd7070Spatrick# other paths. Useful when you cannot use -trim-egraph (e.g. when debugging 970e5dd7070Spatrick# a crash). 971ec727ea7Spatrickclass TargetedTrimmer: 972e5dd7070Spatrick def __init__(self, target_nodes): 973e5dd7070Spatrick self._target_nodes = target_nodes 974e5dd7070Spatrick 975e5dd7070Spatrick @staticmethod 976e5dd7070Spatrick def parse_target_node(node, graph): 977e5dd7070Spatrick if node.startswith('0x'): 978e5dd7070Spatrick ret = 'Node' + node 979e5dd7070Spatrick assert ret in graph.nodes 980e5dd7070Spatrick return ret 981e5dd7070Spatrick else: 982e5dd7070Spatrick for other_id in graph.nodes: 983e5dd7070Spatrick other = graph.nodes[other_id] 984e5dd7070Spatrick if other.node_id == int(node): 985e5dd7070Spatrick return other_id 986e5dd7070Spatrick 987e5dd7070Spatrick @staticmethod 988e5dd7070Spatrick def parse_target_nodes(target_nodes, graph): 989e5dd7070Spatrick return [TargetedTrimmer.parse_target_node(node, graph) 990e5dd7070Spatrick for node in target_nodes.split(',')] 991e5dd7070Spatrick 992e5dd7070Spatrick def trim(self, graph): 993e5dd7070Spatrick queue = self._target_nodes 994e5dd7070Spatrick visited_nodes = set() 995e5dd7070Spatrick 996e5dd7070Spatrick while len(queue) > 0: 997e5dd7070Spatrick node_id = queue.pop() 998e5dd7070Spatrick visited_nodes.add(node_id) 999e5dd7070Spatrick node = graph.nodes[node_id] 1000e5dd7070Spatrick for pred_id in node.predecessors: 1001e5dd7070Spatrick if pred_id not in visited_nodes: 1002e5dd7070Spatrick queue.append(pred_id) 1003e5dd7070Spatrick graph.nodes = {node_id: graph.nodes[node_id] 1004e5dd7070Spatrick for node_id in visited_nodes} 1005e5dd7070Spatrick for node_id in graph.nodes: 1006e5dd7070Spatrick node = graph.nodes[node_id] 1007e5dd7070Spatrick node.successors = [succ_id for succ_id in node.successors 1008e5dd7070Spatrick if succ_id in visited_nodes] 1009e5dd7070Spatrick node.predecessors = [succ_id for succ_id in node.predecessors 1010e5dd7070Spatrick if succ_id in visited_nodes] 1011e5dd7070Spatrick 1012e5dd7070Spatrick 1013e5dd7070Spatrick#===-----------------------------------------------------------------------===# 1014e5dd7070Spatrick# The entry point to the script. 1015e5dd7070Spatrick#===-----------------------------------------------------------------------===# 1016e5dd7070Spatrick 1017e5dd7070Spatrick 1018e5dd7070Spatrickdef main(): 1019e5dd7070Spatrick parser = argparse.ArgumentParser( 1020e5dd7070Spatrick description='Display and manipulate Exploded Graph dumps.') 1021e5dd7070Spatrick parser.add_argument('filename', type=str, 1022e5dd7070Spatrick help='the .dot file produced by the Static Analyzer') 1023e5dd7070Spatrick parser.add_argument('-v', '--verbose', action='store_const', 1024e5dd7070Spatrick dest='loglevel', const=logging.DEBUG, 1025e5dd7070Spatrick default=logging.WARNING, 1026e5dd7070Spatrick help='enable info prints') 1027e5dd7070Spatrick parser.add_argument('-d', '--diff', action='store_const', dest='diff', 1028e5dd7070Spatrick const=True, default=False, 1029e5dd7070Spatrick help='display differences between states') 1030e5dd7070Spatrick parser.add_argument('-t', '--topology', action='store_const', 1031e5dd7070Spatrick dest='topology', const=True, default=False, 1032e5dd7070Spatrick help='only display program points, omit states') 1033e5dd7070Spatrick parser.add_argument('-s', '--single-path', action='store_const', 1034e5dd7070Spatrick dest='single_path', const=True, default=False, 1035e5dd7070Spatrick help='only display the leftmost path in the graph ' 1036e5dd7070Spatrick '(useful for trimmed graphs that still ' 1037e5dd7070Spatrick 'branch too much)') 1038e5dd7070Spatrick parser.add_argument('--to', type=str, default=None, 1039e5dd7070Spatrick help='only display execution paths from the root ' 1040e5dd7070Spatrick 'to the given comma-separated list of nodes ' 1041e5dd7070Spatrick 'identified by a pointer or a stable ID; ' 1042e5dd7070Spatrick 'compatible with --single-path') 1043e5dd7070Spatrick parser.add_argument('--dark', action='store_const', dest='dark', 1044e5dd7070Spatrick const=True, default=False, 1045e5dd7070Spatrick help='dark mode') 1046e5dd7070Spatrick parser.add_argument('--gray', action='store_const', dest='gray', 1047e5dd7070Spatrick const=True, default=False, 1048e5dd7070Spatrick help='black-and-white mode') 1049e5dd7070Spatrick parser.add_argument('--dump-dot-only', action='store_const', 1050e5dd7070Spatrick dest='dump_dot_only', const=True, default=False, 1051e5dd7070Spatrick help='instead of writing an HTML file and immediately ' 1052e5dd7070Spatrick 'displaying it, dump the rewritten dot file ' 1053e5dd7070Spatrick 'to stdout') 1054e5dd7070Spatrick args = parser.parse_args() 1055e5dd7070Spatrick logging.basicConfig(level=args.loglevel) 1056e5dd7070Spatrick 1057e5dd7070Spatrick graph = ExplodedGraph() 1058e5dd7070Spatrick with open(args.filename) as fd: 1059e5dd7070Spatrick for raw_line in fd: 1060e5dd7070Spatrick raw_line = raw_line.strip() 1061e5dd7070Spatrick graph.add_raw_line(raw_line) 1062e5dd7070Spatrick 1063e5dd7070Spatrick trimmers = [] 1064e5dd7070Spatrick if args.to is not None: 1065e5dd7070Spatrick trimmers.append(TargetedTrimmer( 1066e5dd7070Spatrick TargetedTrimmer.parse_target_nodes(args.to, graph))) 1067e5dd7070Spatrick if args.single_path: 1068e5dd7070Spatrick trimmers.append(SinglePathTrimmer()) 1069e5dd7070Spatrick 1070e5dd7070Spatrick explorer = BasicExplorer() 1071e5dd7070Spatrick 1072e5dd7070Spatrick visitor = DotDumpVisitor(args.diff, args.dark, args.gray, args.topology, 1073e5dd7070Spatrick args.dump_dot_only) 1074e5dd7070Spatrick 1075e5dd7070Spatrick for trimmer in trimmers: 1076e5dd7070Spatrick trimmer.trim(graph) 1077e5dd7070Spatrick 1078e5dd7070Spatrick explorer.explore(graph, visitor) 1079e5dd7070Spatrick 1080e5dd7070Spatrick 1081e5dd7070Spatrickif __name__ == '__main__': 1082e5dd7070Spatrick main() 1083