Graph.h

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 // Graph class
00032 
00033 
00034 #if !defined(GRAPH_H)
00035 #define GRAPH_H
00036 
00037 #include "dyntypes.h"
00038 #include "boost/shared_ptr.hpp"
00039 #include <set>
00040 #include <list>
00041 #include <queue>
00042 #include <map>
00043 
00044 #include "Annotatable.h"
00045 #include "Node.h"
00046 
00047 #if defined(_MSC_VER)
00048 #pragma warning(disable:4251)
00049 #endif
00050 
00051 namespace Dyninst {
00052 class Edge;
00053 class Graph;
00054 class Node;
00055 class NodeIterator;
00056 class EdgeIterator;
00057     
00058 class COMMON_EXPORT Graph : public AnnotatableSparse {
00059     friend class Edge;
00060     friend class Node;
00061     friend class Creator;
00062     friend class Iterator;
00063     
00064  protected:
00065 
00066     typedef boost::shared_ptr<Node> NodePtr;
00067     typedef boost::shared_ptr<Edge> EdgePtr;
00068 
00069     typedef std::set<NodePtr> NodeSet;
00070     typedef std::map<Address, NodeSet> NodeMap;
00071 
00072  public:    
00073     typedef boost::shared_ptr<Graph> Ptr;
00074 
00075     // Interface class for predicate-based searches. Users
00076     // can inherit this class to specify the functor to use
00077     // as a predicate...
00078     class NodePredicate {
00079 
00080     public:
00081         typedef boost::shared_ptr<NodePredicate> Ptr;
00082         virtual ~NodePredicate() {};
00083         virtual bool predicate(const NodePtr &node) = 0;
00084         static Ptr getPtr(NodePredicate *p) { 
00085             return Ptr(p);
00086         }
00087     };
00088 
00089     typedef bool (*NodePredicateFunc)(const NodePtr &node, void *user_arg);
00090     
00091     // If you want to traverse the graph start here.
00092     virtual void entryNodes(NodeIterator &begin, NodeIterator &end);
00093 
00094     // If you want to traverse the graph backwards start here.
00095     virtual void exitNodes(NodeIterator &begin, NodeIterator &end);
00096     
00097     // Get all nodes in the graph
00098     virtual void allNodes(NodeIterator &begin, NodeIterator &end);
00099 
00100     // Get all nodes with a provided address
00101     virtual bool find(Address addr, NodeIterator &begin, NodeIterator &end);
00102 
00103     // Get all nodes that satisfy the provided predicate
00104     virtual bool find(NodePredicate::Ptr, NodeIterator &begin, NodeIterator &end);
00105     virtual bool find(NodePredicateFunc, void *user_arg, NodeIterator &begin, NodeIterator &end);
00106 
00107     bool printDOT(const std::string& fileName);
00108 
00109     virtual ~Graph() {};
00110     
00111     // We create an empty graph and then add nodes and edges.
00112     static Ptr createGraph();
00113     
00114     void insertPair(NodePtr source, NodePtr target, EdgePtr edge = EdgePtr());
00115 
00116     virtual void insertEntryNode(NodePtr entry);
00117     virtual void insertExitNode(NodePtr exit);
00118 
00119     virtual void markAsEntryNode(NodePtr entry);
00120     virtual void markAsExitNode(NodePtr exit);
00121 
00122     void deleteNode(NodePtr node);
00123 
00124     void addNode(NodePtr node);
00125 
00126     virtual void removeAnnotation() {};
00127 
00128     bool isEntryNode(NodePtr node);
00129     bool isExitNode(NodePtr node);
00130 
00131     unsigned size() const;
00132 
00133  protected:
00134      
00135     static const Address INITIAL_ADDR;
00136     
00137     // Create graph, add nodes.
00138     Graph();
00139     
00140     // We also need to point to all Nodes to keep them alive; we can't 
00141     // pervasively use shared_ptr within the graph because we're likely
00142     // to have cycles.
00143     NodeSet nodes_;
00144     
00145     NodeMap nodesByAddr_;
00146 
00147     // May be overridden by children; don't assume it exists.
00148     // Arguably should be removed entirely.
00149     NodeSet entryNodes_;
00150 
00151     // See the above ;)
00152     NodeSet exitNodes_;
00153 };
00154 
00155 }
00156 #endif
00157 
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1