1 #ifndef ROSE_BinaryAnalysis_Partitioner2_GraphViz_H
2 #define ROSE_BinaryAnalysis_Partitioner2_GraphViz_H
3 #include <featureTests.h>
4 #ifdef ROSE_ENABLE_BINARY_ANALYSIS
6 #include <Rose/BinaryAnalysis/NoOperation.h>
7 #include <Rose/BinaryAnalysis/Partitioner2/ControlFlowGraph.h>
8 #include <Rose/BinaryAnalysis/Partitioner2/FunctionCallGraph.h>
9 #include <Rose/BinaryAnalysis/SourceLocations.h>
10 #include <Rose/Color.h>
12 #include <boost/regex.hpp>
16 namespace BinaryAnalysis {
17 namespace Partitioner2 {
28 ROSE_DLL_API std::string
toString(
const Attributes&);
31 ROSE_DLL_API std::string
quotedEscape(
const std::string&);
34 ROSE_DLL_API std::string
htmlEscape(
const std::string&);
39 ROSE_DLL_API std::string
escape(
const std::string&);
47 ROSE_DLL_API std::string
concatenate(
const std::string &oldStuff,
const std::string &newStuff,
const std::string &separator=
"");
52 ROSE_DLL_API
bool isId(
const std::string &s);
55 ROSE_DLL_API
extern const size_t NO_ID;
68 Attributes attributes_;
69 std::string subgraph_;
78 void select(
bool b=
true) { isSelected_ = b; }
93 const std::string &
name()
const {
return name_; }
94 void name(
const std::string &s) { name_ = s; }
104 static std::string empty =
"\"\"";
105 return label_.empty() ? empty : label_;
107 void label(
const std::string &s) { label_ = s; }
126 const std::string&
subgraph()
const {
return subgraph_; }
127 void subgraph(
const std::string &s) { subgraph_ = s; }
155 typename G::ConstVertexIterator src, dst;
157 Attributes attributes;
158 PseudoEdge(
const typename G::ConstVertexIterator &src,
const typename G::ConstVertexIterator &dst,
159 const std::string &label)
160 : src(src), dst(dst), label(label) {}
167 VertexOrganization vertexOrganization_;
168 EdgeOrganization edgeOrganization_;
169 SubgraphOrganization subgraphOrganization_;
170 Attributes defaultGraphAttributes_;
171 Attributes defaultNodeAttributes_;
172 Attributes defaultEdgeAttributes_;
173 std::list<PseudoEdge> pseudoEdges_;
187 : subgraphColor_(0, 0, 0.95) {
194 vertexOrganization_.
clear();
195 vertexOrganization_.resize(g.
nVertices());
196 edgeOrganization_.clear();
197 edgeOrganization_.resize(g.
nEdges());
198 subgraphOrganization_.
clear();
199 pseudoEdges_.clear();
208 return defaultGraphAttributes_;
211 return defaultGraphAttributes_;
221 return defaultNodeAttributes_;
224 return defaultNodeAttributes_;
234 return defaultEdgeAttributes_;
237 return defaultEdgeAttributes_;
259 return vertexOrganization_;
262 return vertexOrganization_;
265 ASSERT_require(vertexId < vertexOrganization_.size());
266 return vertexOrganization_[vertexId];
269 ASSERT_require(vertexId < vertexOrganization_.size());
270 return vertexOrganization_[vertexId];
297 return edgeOrganization_;
300 return edgeOrganization_;
303 ASSERT_require(edgeId < edgeOrganization_.size());
304 return edgeOrganization_[edgeId];
307 ASSERT_require(edgeId < edgeOrganization_.size());
308 return edgeOrganization_[edgeId];
335 return subgraphOrganization_;
338 return subgraphOrganization_;
359 pseudoEdges_.clear();
391 virtual void emit(std::ostream&)
const;
397 size_t emitVertex(std::ostream&,
const typename G::ConstVertexIterator&,
const Organization&,
const VMap&)
const;
400 void emitEdge(std::ostream&,
const typename G::ConstEdgeIterator&,
const Organization&,
const VMap&)
const;
437 bool useFunctionSubgraphs_;
438 bool showReturnEdges_;
439 bool showInstructions_;
440 bool showInstructionAddresses_;
441 bool showInstructionStackDeltas_;
442 bool showInNeighbors_;
443 bool showOutNeighbors_;
444 bool strikeNoopSequences_;
449 static unsigned long versionDate_;
625 void deselectReturnEdges();
628 void deselectUnusedVertex(ControlFlowGraph::ConstVertexIterator);
636 void selectNeighbors(
bool selectInEdges=
true,
bool selectOutEdges=
true);
669 selectWholeGraph().emit(out);
676 selectFunctionGraph(
function).emit(out);
683 selectIntervalGraph(interval).emit(out);
696 static bool isInterFunctionEdge(
const ControlFlowGraph::Edge&);
706 static Function::Ptr firstOwningFunction(
const ControlFlowGraph::Vertex&);
708 return firstOwningFunction(*v);
718 static FunctionSet owningFunctions(
const ControlFlowGraph::Vertex&);
726 void assignFunctionSubgraphs();
736 virtual std::string sourceLocation(
const ControlFlowGraph::ConstVertexIterator&)
const;
744 virtual std::string vertexLabel(
const ControlFlowGraph::ConstVertexIterator&)
const;
745 std::string vertexLabel(
const ControlFlowGraph::Vertex&)
const;
755 virtual std::string vertexLabelDetailed(
const ControlFlowGraph::ConstVertexIterator&)
const;
756 std::string vertexLabelDetailed(
const ControlFlowGraph::Vertex&)
const;
762 virtual Attributes vertexAttributes(
const ControlFlowGraph::ConstVertexIterator&)
const;
763 Attributes vertexAttributes(
const ControlFlowGraph::Vertex&)
const;
771 virtual std::string edgeLabel(
const ControlFlowGraph::ConstEdgeIterator&)
const;
772 std::string edgeLabel(
const ControlFlowGraph::Edge&)
const;
778 virtual Attributes edgeAttributes(
const ControlFlowGraph::ConstEdgeIterator&)
const;
779 Attributes edgeAttributes(
const ControlFlowGraph::Edge&)
const;
785 virtual std::string functionLabel(
const Function::Ptr&)
const;
788 virtual Attributes functionAttributes(
const Function::Ptr&)
const;
808 boost::regex highlightNameMatcher_;
812 virtual std::string functionLabel(
const Function::Ptr&)
const;
813 virtual Attributes functionAttributes(
const Function::Ptr&)
const;
814 virtual void emitCallGraph(std::ostream &out)
const;
817 virtual void highlight(
const boost::regex&);
836 boost::regex nameMatcher_;
837 typedef std::vector<Function::Ptr> InlinedFunctions;
843 virtual const FunctionCallGraph& callGraph()
const override {
return CgEmitter::callGraph(); }
845 virtual std::string functionLabel(
const Function::Ptr&)
const override;
902 std::string name = org.
name();
905 out <<name <<
" [ label=" <<org.
label() <<
" ";
914 const VMap &vmap)
const {
915 ASSERT_require2(vmap.
exists(edge->source()->id()),
"edge source vertex has not yet been emitted");
916 ASSERT_require2(vmap.
exists(edge->target()->id()),
"edge target vertex has not yet been emitted");
918 size_t sourceId = edge->source()->id();
919 std::string sourceName = vertexOrganization(sourceId).name();
920 if (sourceName.empty())
923 size_t targetId = edge->target()->id();
924 std::string targetName = vertexOrganization(targetId).name();
925 if (targetName.empty())
928 out <<sourceName <<
" -> " <<targetName <<
" [ label=" <<org.
label() <<
" " <<
toString(org.
attributes()) <<
" ];\n";
936 out <<
"digraph CFG {\n";
937 out <<
" graph [ " <<
toString(defaultGraphAttributes_) <<
" ];\n";
938 out <<
" node [ " <<
toString(defaultNodeAttributes_) <<
" ];\n";
939 out <<
" edge [ " <<
toString(defaultEdgeAttributes_) <<
" ];\n";
941 typedef std::map<std::string , std::string> Subgraphs;
945 for (
typename G::ConstVertexIterator vertex=graph_.vertices().begin(); vertex!=graph_.vertices().end(); ++vertex) {
948 std::ostringstream ss;
949 size_t gvid = emitVertex(ss, vertex, org, vmap);
950 vmap.
insert(vertex->id(), gvid);
951 subgraphs[org.
subgraph()] += ss.str();
956 for (
typename G::ConstEdgeIterator edge=graph_.edges().begin(); edge!=graph_.edges().end(); ++edge) {
959 vertexOrganization(edge->source()).isSelected() && vertexOrganization(edge->target()).isSelected()) {
960 std::ostringstream ss;
961 emitEdge(ss, edge, org, vmap);
962 subgraphs[org.
subgraph()] += ss.str();
967 for (
const Subgraphs::value_type &node: subgraphs) {
968 const std::string &subgraphName = node.first;
969 const std::string &subgraphContent = node.second;
970 if (!subgraphName.empty()) {
971 out <<
"\nsubgraph cluster_" <<subgraphName <<
" {"
972 <<
" label=" <<subgraphOrganization(subgraphName).label() <<
" "
973 <<
toString(subgraphOrganization(subgraphName).attributes()) <<
"\n"
980 Subgraphs::iterator unnamedSubgraph = subgraphs.find(
"");
981 if (unnamedSubgraph != subgraphs.end())
982 out <<unnamedSubgraph->second;
986 if (vertexOrganization(edge.src).isSelected() && vertexOrganization(edge.dst).isSelected()) {
987 std::string sourceName = vertexOrganization(edge.src).name();
988 std::string targetName = vertexOrganization(edge.dst).name();
992 <<
" [ label=" <<
escape(edge.label) <<
" ];\n";
1002 for (
const typename G::Vertex &src: graph_.vertices()) {
1003 if (vertexOrganization(src).isSelected()) {
1004 std::set<size_t> targets;
1005 for (
const typename G::Edge &edge: src.outEdges()) {
1006 if (edgeOrganization(edge).isSelected() && !targets.insert(edge.target()->id()).second)
1007 edgeOrganization(edge).select(
false);
size_t nVertices() const
Total number of vertices.
ROSE_DLL_API const size_t NO_ID
An invalid identification number.
void funcReturnColor(const Color::HSV &bg)
Property: color to use for background of function return nodes.
double x
Distance from left.
const Color::HSV & warningColor() const
Property: color to use for background of special nodes and for warnings.
const VertexOrganization & vertexOrganization() const
Property: Controls which vertices are to appear in the output, and how.
const Organization & vertexOrganization(size_t vertexId) const
Property: Controls which vertices are to appear in the output, and how.
void showInstructionStackDeltas(bool b)
Property: show instruction stack deltas.
const Organization & edgeOrganization(const typename G::ConstEdgeIterator &edge) const
Property: Controls which edges are to appear in the output, and how.
PositionGraph readPositions(std::istream &)
Constructs graph positions from a file.
Organization & subgraphOrganization(const std::string &name)
Property: Controls which subgraphs appear in the output, and how.
Coordinate center
Center of vertex in display plane units.
Organization()
Default constructor.
bool exists(const Key &key) const
Determine if a key exists.
ROSE_UTIL_API std::string numberToString(long long)
Convert an integer to a string.
Optional< Value > getOptional(const Key &key) const
Lookup and return a value or nothing.
size_t nEdges() const
Total number of edges.
SourceLocations & srcMapper()
Property: Address-to-source mapping.
BaseEmitter()
Default constructor.
Sawyer::Container::Map< std::string, Organization > SubgraphOrganization
Organizational information for subgraphs.
const Attributes & defaultNodeAttributes() const
Property: default graph node attributes.
void graph(const Graph &g)
Reset the graph.
Base class for generating GraphViz output.
const SubgraphOrganization & subgraphOrganization() const
Property: Controls which subgraphs appear in the output, and how.
const Attributes & defaultGraphAttributes() const
Property: default graph attributes.
void selectAll(bool b=true)
Causes all vertices and edges to be selected.
Organization & edgeOrganization(size_t edgeId)
Property: Controls which edges are to appear in the output, and how.
void useFunctionSubgraphs(bool b)
Property: use function subgraphs.
void label(const std::string &s)
Label for object.
Bidirectional mapping between addresses and source locations.
ROSE_DLL_API std::string toString(const Attributes &)
Convert attributes to GraphViz language string.
const std::string & name() const
Name for object.
size_t emitVertex(std::ostream &, const typename G::ConstVertexIterator &, const Organization &, const VMap &) const
Emit a single vertex if it hasn't been emitted already.
const Attributes & defaultEdgeAttributes() const
Property: default graph edge attributes.
ROSE_DLL_API std::string htmlEscape(const std::string &)
Escape characters that need to be escaped within GraphViz HTML literals.
ROSE_DLL_API std::string quotedEscape(const std::string &)
Escape characters that need to be escaped within GraphViz double quoted literals. ...
bool useFunctionSubgraphs() const
Property: use function subgraphs.
void emitWholeGraph(std::ostream &out)
Dump entire control flow graph.
ROSE_DLL_API std::string escape(const std::string &)
Escape some value for GraphViz.
const Attributes & attributes() const
Attributes for object.
Value & insertMaybeDefault(const Key &key)
Conditionally insert a new key with default value.
void emitEdge(std::ostream &, const typename G::ConstEdgeIterator &, const Organization &, const VMap &) const
Emit a single edge.
const Color::HSV & subgraphColor() const
Property: color to use for function subgraph background.
bool showInstructionAddresses() const
Property: show instruction addresses.
Creates GraphViz files from Partitioner data.
Main namespace for the ROSE library.
bool isSelected() const
Determines whether an object is selected.
const Organization & edgeOrganization(const typename G::Edge &edge) const
Property: Controls which edges are to appear in the output, and how.
const EdgeOrganization & edgeOrganization() const
Property: Controls which edges are to appear in the output, and how.
void subgraph(const std::string &s)
Subgraph for object.
Map & clear()
Remove all nodes.
Organization & edgeOrganization(const typename G::ConstEdgeIterator &edge)
Property: Controls which edges are to appear in the output, and how.
void showReturnEdges(bool b)
Property: show function return edges.
VertexType
Partitioner control flow vertex types.
Sawyer::Container::Graph< VertexPosition, EdgePosition > PositionGraph
A graph with positioned vertices and edges.
Organization & vertexOrganization(size_t vertexId)
Property: Controls which vertices are to appear in the output, and how.
void funcEnterColor(const Color::HSV &bg)
Property: color to use for background of function entrance nodes.
Function call information.
Emits a modified function call graph.
Sawyer::Container::Map< std::string, std::string > Attributes
GraphViz attributes.
bool showReturnEdges() const
Property: show function return edges.
Emits a function call graph.
Organization & vertexOrganization(const typename G::ConstVertexIterator &vertex)
Property: Controls which vertices are to appear in the output, and how.
const SourceLocations & srcMapper() const
Property: Address-to-source mapping.
const Organization & vertexOrganization(const typename G::Vertex &vertex) const
Property: Controls which vertices are to appear in the output, and how.
VertexOrganization & vertexOrganization()
Property: Controls which vertices are to appear in the output, and how.
std::vector< Organization > EdgeOrganization
Organizational information for edges.
virtual void emit(std::ostream &) const
Dump selected vertices, edges, and subgraphs.
bool showInstructionStackDeltas() const
Property: show instruction stack deltas.
void srcMapper(const SourceLocations &mapper)
Property: Address-to-source mapping.
bool showInNeighbors() const
Property: show incoming edges from neighbor vertices.
const std::string & label() const
Label for object.
double y
Distance from up.
Attributes & attributes()
Attributes for object.
ROSE_DLL_API std::string concatenate(const std::string &oldStuff, const std::string &newStuff, const std::string &separator="")
Append a value to an existing string.
Attributes & defaultNodeAttributes()
Property: default graph node attributes.
void subgraphColor(const Color::HSV &bg)
Property: color to use for function subgraph background.
void deselectParallelEdges()
Deselect all but one parallel edge.
const Partitioner & partitioner()
Property: partitioner.
double height
Vertical size of vertex.
const Organization & vertexOrganization(const typename G::ConstVertexIterator &vertex) const
Property: Controls which vertices are to appear in the output, and how.
const Color::HSV & funcEnterColor() const
Property: color to use for background of function entrance nodes.
bool showOutNeighbors() const
Property: show outgoing edges to neighbor vertices.
bool strikeNoopSequences() const
Property: strike no-op sequences.
void emitIntervalGraph(std::ostream &out, const AddressInterval &interval)
Dump control flow graph for some address interval.
void clear()
Remove all vertices and edges.
void select(bool b=true)
Select or deselect object.
void showOutNeighbors(bool b)
Property: show outgoing edges to neighbor vertices.
const Color::HSV & funcReturnColor() const
Property: color to use for background of function return nodes.
void selectAllEdges(bool b=true)
Causes all edges to be selected.
ROSE_DLL_API bool isId(const std::string &s)
Determins if a string is a valid GraphViz ID.
const Value & getOrDefault(const Key &key) const
Lookup and return a value or a default.
Attributes & defaultGraphAttributes()
Property: default graph attributes.
void name(const std::string &s)
Name for object.
double width
Horizontal size of vertex.
Two dimensional display plane coordinate.
void warningColor(const Color::HSV &bg)
Property: color to use for background of special nodes and for warnings.
const Organization & edgeOrganization(size_t edgeId) const
Property: Controls which edges are to appear in the output, and how.
SubgraphOrganization & subgraphOrganization()
Property: Controls which subgraphs appear in the output, and how.
std::vector< Organization > VertexOrganization
Organizational information for vertices.
void showInstructions(bool b)
Property: show basic block instructions.
Map & insert(const Key &key, const Value &value)
Insert or update a key/value pair.
void showInNeighbors(bool b)
Property: show incoming edges from neighbor vertices.
void selectNone()
Deselects all vertices and edges.
Organization & vertexOrganization(const typename G::Vertex &vertex)
Property: Controls which vertices are to appear in the output, and how.
const Organization & subgraphOrganization(const std::string &name) const
Property: Controls which subgraphs appear in the output, and how.
void attributes(const Attributes &a)
Attributes for object.
void strikeNoopSequences(bool b)
Property: strike no-op sequences.
static Function::Ptr firstOwningFunction(const ControlFlowGraph::ConstVertexIterator &v)
First function that owns a vertex.
static bool isInterFunctionEdge(const ControlFlowGraph::ConstEdgeIterator &e)
Returns true if the edge spans two different functions.
void selectAllVertices(bool b=true)
Causes all vertices to be selected.
std::string name
Name of vertex as known to GraphViz.
Partitions instructions into basic blocks and functions.
Attributes & defaultEdgeAttributes()
Property: default graph edge attributes.
Analysis that looks for no-op equivalents.
void showInstructionAddresses(bool b)
Property: show instruction addresses.
size_t size() const
Number of nodes, keys, or values in this container.
BaseEmitter(const Graph &g)
Constructor.
const std::string & subgraph() const
Subgraph for object.
EdgeOrganization & edgeOrganization()
Property: Controls which edges are to appear in the output, and how.
std::vector< Coordinate > spline
Control points for the edge B-spline.
Organization & edgeOrganization(const typename G::Edge &edge)
Property: Controls which edges are to appear in the output, and how.
static FunctionSet owningFunctions(const ControlFlowGraph::ConstVertexIterator &v)
Functions that own a vertex.
Organizational information.
void emitFunctionGraph(std::ostream &out, const Function::Ptr &function)
Dump control flow graph for one function.
bool showInstructions() const
Property: show basic block instructions.