PandA-2024.02
call_graph.cpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
45 #include "call_graph.hpp"
46 
47 #include "Parameter.hpp" // for OPT_dot_directory
48 #include "behavioral_helper.hpp" // for BehavioralHelper
49 #include "config_HAVE_HOST_PROFILING_BUILT.hpp"
50 #include "exceptions.hpp" // for THROW_ASSERT, THROW...
51 #include "function_behavior.hpp" // for FunctionBehavior
52 #include "graph.hpp" // for graph, Cget_edge_info
53 #include "loops.hpp" // for FunctionBehaviorRef
54 #include "string_manipulation.hpp" // for add_escape
55 #include <filesystem> // for create_directories
56 #include <ostream> // for operator<<, ostream
57 #include <utility> // for pair
58 
60 {
61 }
62 
64 
66  : graphs_collection(call_graph_info, _parameters)
67 {
68 }
69 
71 
72 CallGraph::CallGraph(const CallGraphsCollectionRef call_graphs_collection, const int _selector)
73  : graph(call_graphs_collection.get(), _selector)
74 {
75 }
76 
77 CallGraph::CallGraph(const CallGraphsCollectionRef call_graphs_collection, const int _selector,
78  const CustomUnorderedSet<vertex>& _vertices)
79  : graph(call_graphs_collection.get(), _selector, _vertices)
80 {
81 }
82 
83 CallGraph::~CallGraph() = default;
84 
85 void CallGraph::WriteDot(const std::string& file_name) const
86 {
87  const auto output_directory = collection->parameters->getOption<std::string>(OPT_dot_directory);
88  if(!std::filesystem::exists(output_directory))
89  {
90  std::filesystem::create_directories(output_directory);
91  }
92  const std::string full_name = output_directory + file_name;
93  const VertexWriterConstRef function_writer(new FunctionWriter(this));
94  const EdgeWriterConstRef function_edge_writer(new FunctionEdgeWriter(this));
95  InternalWriteDot<const FunctionWriter, const FunctionEdgeWriter>(full_name, function_writer, function_edge_writer);
96 }
97 
99  : VertexWriter(call_graph, 0), behaviors(call_graph->CGetCallGraphInfo()->behaviors)
100 {
101 }
102 
103 void FunctionWriter::operator()(std::ostream& out, const vertex& v) const
104 {
105  THROW_ASSERT(behaviors.find(Cget_node_info<FunctionInfo, graph>(v, *printing_graph)->nodeID) != behaviors.end(),
106  "Function " + std::to_string(Cget_node_info<FunctionInfo, graph>(v, *printing_graph)->nodeID) +
107  " not found");
108  const FunctionBehaviorRef FB =
109  behaviors.find(Cget_node_info<FunctionInfo, graph>(v, *printing_graph)->nodeID)->second;
110  out << "[shape=box, label=\"" << FB->CGetBehavioralHelper()->get_function_name();
111  const CustomOrderedSet<unsigned int>& mem_nodeID = FB->get_function_mem();
112  if(mem_nodeID.size())
113  {
114  out << "\\nMEMORY:";
115  for(unsigned int l : mem_nodeID)
116  {
117  std::string label = FB->CGetBehavioralHelper()->PrintVariable(l);
118  add_escape(label, "\"");
119  out << "\\n";
120  out << label;
121  }
122  }
123  out << "\"]";
124 }
125 
127  : EdgeWriter(call_graph, 0), behaviors(call_graph->CGetCallGraphInfo()->behaviors)
128 {
129 }
130 
132 
133 void FunctionEdgeWriter::operator()(std::ostream& out, const EdgeDescriptor& e) const
134 {
135  const CustomOrderedSet<unsigned int>& direct_call_points =
136  Cget_edge_info<FunctionEdgeInfo, graph>(e, *printing_graph)->direct_call_points;
137  const CustomOrderedSet<unsigned int>& indirect_call_points =
138  Cget_edge_info<FunctionEdgeInfo, graph>(e, *printing_graph)->indirect_call_points;
139  const CustomOrderedSet<unsigned int>& function_addresses =
140  Cget_edge_info<FunctionEdgeInfo, graph>(e, *printing_graph)->function_addresses;
141  std::string color;
143  {
144  color = "blue";
145  }
147  {
148  color = "red";
149  }
150  else
151  {
152  THROW_ERROR(std::string("InconsistentDataStructure"));
153  }
154 
155  out << "[color=" << color << ", label=\"";
156  if(direct_call_points.size())
157  {
158  out << "DIRECT: ";
159  for(const auto& call : direct_call_points)
160  {
161  out << "\\n" << call;
162  }
163  }
164  if(indirect_call_points.size())
165  {
166  if(direct_call_points.size())
167  {
168  out << "\\n";
169  }
170  out << "INDIRECT: ";
171  for(const auto& call : indirect_call_points)
172  {
173  out << "\\n" << call;
174  }
175  }
176  if(function_addresses.size())
177  {
178  if(direct_call_points.size() or indirect_call_points.size())
179  {
180  out << "\\n";
181  }
182  out << "TAKE ADDRESS: ";
183  for(const auto& call : function_addresses)
184  {
185  out << "\\n" << call;
186  }
187  }
188  out << "\"]";
189 }
#define STD_SELECTOR
Definition: call_graph.hpp:60
void operator()(std::ostream &out, const vertex &v) const override
operator function returning the label of the vertex
Definition: call_graph.cpp:103
int GetSelector() const
Return the selector of this graph.
Definition: graph.hpp:922
const ParameterConstRef parameters
Set of input parameters.
Definition: graph.hpp:291
const graph * printing_graph
The graph to be printed.
Definition: graph.hpp:1410
Class specification of the graph structures.
Functor used by write_graphviz to write the label of the vertices of a function graph.
Definition: call_graph.hpp:224
exceptions managed by PandA
void add_escape(std::string &ioString, const std::string &to_be_escaped)
Header include.
~FunctionEdgeWriter() override
Destructor.
Functor used by write_graphviz to write the edges of a function graph.
Definition: call_graph.hpp:248
Auxiliary methods for manipulating string.
#define FEEDBACK_SELECTOR
Definition: call_graph.hpp:61
const graph * printing_graph
The graph to be printed.
Definition: graph.hpp:1445
This class is the view of a call graph.
Definition: call_graph.hpp:160
FunctionInfo()
Constructor.
Definition: call_graph.cpp:59
Functor used to write the content of a vertex to dotty file.
Definition: graph.hpp:1406
void operator()(std::ostream &out, const EdgeDescriptor &e) const override
operator function returning the edge description
Definition: call_graph.cpp:133
CallGraphsCollection(const CallGraphInfoRef call_graph_info, const ParameterConstRef _parameters)
Constructor.
Definition: call_graph.cpp:65
boost::graph_traits< graph >::vertex_descriptor vertex
vertex definition.
Definition: graph.hpp:1303
General class used to describe a graph in PandA.
Definition: graph.hpp:771
FunctionEdgeWriter(const CallGraph *call_graph)
constructor
Definition: call_graph.cpp:126
Call graph hierarchy.
#define THROW_ERROR(str_expr)
helper function used to throw an error in a standard way
Definition: exceptions.hpp:263
FunctionWriter(const CallGraph *call_graph)
constructor
Definition: call_graph.cpp:98
~CallGraph() override
Destructor.
bulk graph.
Definition: graph.hpp:287
string full_name
Definition: test_panda.py:841
Functor used to write the content of the edges to a dotty file.
Definition: graph.hpp:1441
const std::map< unsigned int, FunctionBehaviorRef > & behaviors
reference to the behaviors
Definition: call_graph.hpp:228
void WriteDot(const std::string &file_name) const
Write the call graph in dot format.
Definition: call_graph.cpp:85
~CallGraphsCollection() override
Destructor.
CallGraph(const CallGraphsCollectionRef call_graphs_collection, const int selector)
Constructor.
Definition: call_graph.cpp:72
Template borrowed from the ANTLR library by Terence Parr (http://www.jGuru.com - Software rights: htt...
Definition: refcount.hpp:94
interface of loops finding algorithm
this class is used to manage the command-line or XML options.
graphs_collection * collection
The graph collection.
Definition: graph.hpp:846
FunctionEdgeInfo()
Constructor.
const CallGraphInfoConstRef CGetCallGraphInfo() const
Return the info associated with the call graph.
Definition: call_graph.hpp:197
A brief description of the C++ Header File.
boost::graph_traits< graph >::edge_descriptor EdgeDescriptor
edge definition.
Definition: graph.hpp:1316
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

Generated on Mon Feb 12 2024 13:02:50 for PandA-2024.02 by doxygen 1.8.13