After tested under windows.
[dyninst.git] / patchAPI / h / PatchCFG.h
1 /* Public Interface */
2
3 #ifndef _PATCHAPI_DYNINST_CFG_H_
4 #define _PATCHAPI_DYNINST_CFG_H_
5
6 #include "common.h"
7 #include "PatchObject.h"
8
9 namespace Dyninst {
10 namespace PatchAPI {
11
12 class PatchEdge;
13 class PatchBlock;
14 class PatchFunction;
15 //class PatchObject;
16
17 class PatchEdge {
18    friend class PatchBlock;
19    friend class PatchFunction;
20    friend class PatchObject;
21
22   public:
23    PATCHAPI_EXPORT static PatchEdge *create(ParseAPI::Edge *,
24                                             PatchBlock *src,
25                                             PatchBlock *trg);
26    PATCHAPI_EXPORT PatchEdge(ParseAPI::Edge *internalEdge,
27              PatchBlock *source,
28              PatchBlock *target);
29    PATCHAPI_EXPORT PatchEdge(const PatchEdge *parent,
30              PatchBlock *child_src,
31              PatchBlock *child_trg);
32    PATCHAPI_EXPORT virtual ~PatchEdge();
33
34    // Getters
35    PATCHAPI_EXPORT ParseAPI::Edge *edge() const;
36    PATCHAPI_EXPORT PatchBlock *source();
37    PATCHAPI_EXPORT PatchBlock *target();
38    PATCHAPI_EXPORT ParseAPI::EdgeTypeEnum type() const;
39    PATCHAPI_EXPORT bool sinkEdge() const;
40    PATCHAPI_EXPORT bool interproc() const;
41
42  protected:
43     ParseAPI::Edge *edge_;
44     PatchBlock *src_;
45     PatchBlock *trg_;
46 };
47
48 class PatchBlock {
49   friend class PatchEdge;
50   friend class PatchFunction;
51   friend class PatchObject;
52
53   public:
54     typedef std::map<Address, InstructionAPI::Instruction::Ptr> Insns;
55     typedef std::vector<PatchEdge*> edgelist;
56
57     PATCHAPI_EXPORT static PatchBlock *create(ParseAPI::Block *, PatchFunction *);
58     PATCHAPI_EXPORT PatchBlock(const PatchBlock *parblk, PatchObject *child);
59     PATCHAPI_EXPORT PatchBlock(ParseAPI::Block *block, PatchObject *obj);
60     PATCHAPI_EXPORT virtual ~PatchBlock();
61
62     // Getters
63     PATCHAPI_EXPORT Address start() const;
64     PATCHAPI_EXPORT Address end() const;
65     PATCHAPI_EXPORT Address last() const;
66     PATCHAPI_EXPORT Address size() const;
67
68     PATCHAPI_EXPORT PatchFunction* getFunction(ParseAPI::Function*);
69     PATCHAPI_EXPORT bool isShared();
70     PATCHAPI_EXPORT int containingFuncs() const;
71     PATCHAPI_EXPORT void getInsns(Insns &insns) const;
72     PATCHAPI_EXPORT InstructionAPI::Instruction::Ptr getInsn(Address a) const;
73     PATCHAPI_EXPORT std::string disassemble() const;
74     PATCHAPI_EXPORT bool containsCall();
75     PATCHAPI_EXPORT bool containsDynamicCall();
76     PATCHAPI_EXPORT std::string format() const;
77
78     // Difference between this layer and ParseAPI: per-function blocks.
79     PATCHAPI_EXPORT PatchFunction *function() const;
80     PATCHAPI_EXPORT ParseAPI::Block *block() const;
81     PATCHAPI_EXPORT PatchObject* object() const;
82     PATCHAPI_EXPORT edgelist &getSources();
83     PATCHAPI_EXPORT edgelist &getTargets();
84
85     template <class OutputIterator>
86     void getFunctions(OutputIterator result);
87
88   protected:
89     typedef enum {
90       backwards,
91       forwards } Direction;
92
93     void removeSourceEdge(PatchEdge *e);
94     void removeTargetEdge(PatchEdge *e);
95
96     ParseAPI::Block *block_;
97     PatchFunction *function_;
98     edgelist srclist_;
99     edgelist trglist_;
100     PatchObject* obj_;
101 };
102
103
104 /* PatchAPI Function */
105 class PatchFunction {
106    friend class PatchEdge;
107    friend class PatchBlock;
108    friend class PatchObject;
109
110    public:
111      typedef std::vector<PatchBlock *> blocklist;
112
113      PATCHAPI_EXPORT static PatchFunction *create(ParseAPI::Function *, PatchObject*);
114      PATCHAPI_EXPORT PatchFunction(ParseAPI::Function *f, PatchObject* o);
115      PATCHAPI_EXPORT PatchFunction(const PatchFunction* parFunc, PatchObject* child);
116      PATCHAPI_EXPORT virtual ~PatchFunction();
117
118      const string &name() { return func_->name(); }
119      Address addr() const { return addr_;  }
120      ParseAPI::Function *function() { return func_; }
121      PatchObject* object() { return obj_; }
122
123      PATCHAPI_EXPORT const blocklist &getAllBlocks();
124      PATCHAPI_EXPORT PatchBlock *getEntryBlock();
125      PATCHAPI_EXPORT const blocklist &getExitBlocks();
126      PATCHAPI_EXPORT const blocklist &getCallBlocks();
127
128    protected:
129      ParseAPI::Function *func_;
130      PatchObject* obj_;
131      Address addr_;
132
133      blocklist all_blocks_;
134      blocklist exit_blocks_;
135      blocklist call_blocks_;
136 };
137
138 template <class OutputIterator>
139 void PatchBlock::getFunctions(OutputIterator result) {
140   std::vector<ParseAPI::Function *> pFuncs;
141   block()->getFuncs(pFuncs);
142   for (unsigned i = 0; i < pFuncs.size(); ++i) {
143     //PatchFunction *func = function()->object()->getFunction(pFuncs[i]);
144     PatchFunction *func = getFunction(pFuncs[i]);
145     *result = func;
146     ++result;
147   }
148 }
149
150 };
151 };
152
153
154 #endif /* _PATCHAPI_DYNINST_CFG_H_ */