Update copyright to LGPL on all files
[dyninst.git] / dyninstAPI / h / BPatch_addressSpace.h
1 /*
2  * Copyright (c) 1996-2009 Barton P. Miller
3  * 
4  * We provide the Paradyn Parallel Performance Tools (below
5  * described as "Paradyn") on an AS IS basis, and do not warrant its
6  * validity or performance.  We reserve the right to update, modify,
7  * or discontinue this software at any time.  We shall have no
8  * obligation to supply such updates or modifications or any other
9  * form of support to you.
10  * 
11  * By your use of Paradyn, you understand and agree that we (or any
12  * other person or entity with proprietary rights in Paradyn) are
13  * under no obligation to provide either maintenance services,
14  * update services, notices of latent defects, or correction of
15  * defects for Paradyn.
16  * 
17  * This library is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU Lesser General Public
19  * License as published by the Free Software Foundation; either
20  * version 2.1 of the License, or (at your option) any later version.
21  * 
22  * This library is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25  * Lesser General Public License for more details.
26  * 
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30  */
31
32 #ifndef _BPatch_addressSpace_h_
33 #define _BPatch_addressSpace_h_
34
35 #include "BPatch_snippet.h"
36 #include "BPatch_dll.h"
37 #include "BPatch_Vector.h"
38 #include "BPatch_eventLock.h"
39 #include "BPatch_point.h"
40
41 #include "BPatch_instruction.h" // for register type
42
43 #include "BPatch_callbacks.h"
44
45 #include <vector>
46
47 #include <stdio.h>
48 #include <signal.h>
49
50 class AddressSpace;
51 class miniTrampHandle;
52 class miniTramp;
53 class BPatch;
54
55 class int_function;
56 struct batchInsertionRecord;
57
58 typedef enum{
59   TRADITIONAL_PROCESS, STATIC_EDITOR
60 } processType;
61
62
63 #ifdef DYNINST_CLASS_NAME
64 #undef DYNINST_CLASS_NAME
65 #endif
66 #define DYNINST_CLASS_NAME BPatchSnippetHandle
67 class BPATCH_DLL_EXPORT BPatchSnippetHandle : public BPatch_eventLock {
68     friend class BPatch_point;
69     friend class BPatch_image;
70     friend class BPatch_process;
71     friend class BPatch_binaryEdit;
72     friend class BPatch_addressSpace;
73     friend class BPatch_thread;
74
75 private:    
76     // Address Space snippet belogns to
77     BPatch_addressSpace *addSpace_;
78
79     // low-level mappings for removal
80     BPatch_Vector<miniTramp *> mtHandles_;
81
82     //  a flag for catchuo
83     bool catchupNeeded;
84     //  and a list of threads to apply catchup to
85     BPatch_Vector<BPatch_thread *> catchup_threads;
86     
87     BPatchSnippetHandle(BPatch_addressSpace * addSpace);
88
89     void addMiniTramp(miniTramp *m) { mtHandles_.push_back(m); }
90     
91 public:
92
93     API_EXPORT_DTOR(_dtor, (),
94     ~,BPatchSnippetHandle,());
95
96     // Returns whether the installed miniTramps use traps.
97     // Not 100% accurate due to internal Dyninst design; we can
98     // have multiple instances of instrumentation due to function
99     // relocation.
100     API_EXPORT(Int, (), bool, usesTrap, ());
101
102     API_EXPORT(Int, (),
103     BPatch_addressSpace *, getAddressSpace, ());
104
105     API_EXPORT(Int, (),
106     BPatch_process *, getProcess, ());
107     
108     API_EXPORT(Int, (),
109     BPatch_Vector<BPatch_thread *> &, getCatchupThreads, ());
110    
111 };
112
113 #ifdef DYNINST_CLASS_NAME
114 #undef DYNINST_CLASS_NAME
115 #endif
116 #define DYNINST_CLASS_NAME BPatch_addressSpace
117
118 class BPATCH_DLL_EXPORT BPatch_addressSpace : public BPatch_eventLock {
119     friend class BPatch;
120     friend class BPatch_image;
121     friend class BPatch_function;
122     friend class BPatch_frame;
123     friend class BPatch_module;
124     friend class BPatch_basicBlock;
125     friend class BPatch_flowGraph;
126     friend class BPatch_loopTreeNode;
127     friend class BPatch_point;
128     friend class BPatch_funcCallExpr;
129     friend class BPatch_eventMailbox;
130     friend class BPatch_instruction;
131   
132  public:
133     
134   BPatch_function *findOrCreateBPFunc(int_function *ifunc, 
135                                       BPatch_module *bpmod);
136
137   BPatch_point *findOrCreateBPPoint(BPatch_function *bpfunc, 
138                                     instPoint *ip,
139                                     BPatch_procedureLocation pointType = BPatch_locUnknownLocation);
140
141   BPatch_variableExpr *findOrCreateVariable(int_variable *v,
142                                             BPatch_type *type = NULL);
143
144  protected:
145   
146   
147   // These callbacks are triggered by lower-level code and forward
148   // calls up to the findOrCreate functions.
149   static BPatch_function *createBPFuncCB(AddressSpace *p, int_function *f);
150   static BPatch_point *createBPPointCB(AddressSpace *p, int_function *f,
151                                        instPoint *ip, int type);
152
153   BPatch_Vector<batchInsertionRecord *> *pendingInsertions;
154
155   BPatch_image *image;
156
157   //  AddressSpace * as;
158   
159   std::vector<BPatch_register> registers_;
160
161  protected:
162   virtual void getAS(std::vector<AddressSpace *> &as) = 0;
163
164  public:
165
166   BPatch_addressSpace();
167
168
169   virtual ~BPatch_addressSpace();
170
171   // Distinguishes between BPatch_process and BPatch_binaryEdit
172   virtual bool getType() = 0;  
173
174   // Returns back bools for variables that are BPatch_process member variables,
175   //   the return value is hardcoded for BPatch_binaryEdit
176   virtual bool getTerminated() = 0;
177   virtual bool getMutationsActive() = 0;
178
179   //  BPatch_addressSpace::insertSnippet
180   //  
181   //  Insert new code into the mutatee
182   API_EXPORT_VIRT(Int, (expr, point, order),
183                   BPatchSnippetHandle *,insertSnippet,(const BPatch_snippet &expr, 
184                                                        BPatch_point &point,
185                                                        BPatch_snippetOrder order = BPatch_firstSnippet));
186   
187     //BPatch_addressSpace::insertSnippet
188       
189     //Insert new code into the mutatee, specifying "when" (before/after point)
190
191     API_EXPORT_VIRT(When, (expr, point, when, order),
192                     BPatchSnippetHandle *,insertSnippet,(const BPatch_snippet &expr, 
193                                                          BPatch_point &point,
194                                                          BPatch_callWhen when,
195                                                          BPatch_snippetOrder order = BPatch_firstSnippet));
196     
197     //BPatch_addressSpace::insertSnippet
198       
199     //Insert new code into the mutatee at multiple points
200
201     API_EXPORT_VIRT(AtPoints, (expr, points, order),
202                     BPatchSnippetHandle *,insertSnippet,(const BPatch_snippet &expr,
203                                                          const BPatch_Vector<BPatch_point *> &points,
204                                                          BPatch_snippetOrder order = BPatch_firstSnippet));
205     
206       // BPatch_addressSpace::insertSnippet
207       
208       //Insert new code into the mutatee at multiple points, specifying "when"
209
210     API_EXPORT_VIRT(AtPointsWhen, (expr, points, when, order),
211                     BPatchSnippetHandle *,insertSnippet,(const BPatch_snippet &expr,
212                                                          const BPatch_Vector<BPatch_point *> &points,
213                                                          BPatch_callWhen when,
214                                                          BPatch_snippetOrder order = BPatch_firstSnippet));
215
216
217
218   
219   virtual void beginInsertionSet() = 0;
220
221   virtual bool finalizeInsertionSet(bool atomic, bool *modified) = 0;
222  
223
224   //  BPatch_addressSpace::deleteSnippet
225   //  
226   //  Remove instrumentation from the mutatee process
227
228   API_EXPORT(Int, (handle),
229              bool,deleteSnippet,(BPatchSnippetHandle *handle));
230
231   //  BPatch_addressSpace::replaceCode
232   //
233   //  Replace a point (must be an instruction...) with a given BPatch_snippet
234
235     API_EXPORT(Int, (point, snippet), 
236     bool, replaceCode, (BPatch_point *point, BPatch_snippet *snippet));
237
238     //  BPatch_addressSpace::replaceFunctionCall
239     //  
240     //  Replace function call at one point with another
241
242     API_EXPORT(Int, (point, newFunc),
243     bool,replaceFunctionCall,(BPatch_point &point, BPatch_function &newFunc));
244
245     //  BPatch_addressSpace::removeFunctionCall
246     //  
247     //  Remove function call at one point 
248
249     API_EXPORT(Int, (point),
250     bool,removeFunctionCall,(BPatch_point &point));
251
252     //  BPatch_addressSpace::replaceFunction
253     //  
254     //  Replace all calls to a function with calls to another
255
256     API_EXPORT(Int, (oldFunc, newFunc),
257     bool,replaceFunction,(BPatch_function &oldFunc, BPatch_function &newFunc));
258
259     //  BPatch_addressSpace::getSourceLines
260     //  
261     //  Method that retrieves the line number and file name corresponding 
262     //  to an address
263
264     API_EXPORT(Int, (addr, lines),
265     bool,getSourceLines,(unsigned long addr, BPatch_Vector< BPatch_statement > & lines ));
266     
267     // BPatch_addressSpace::getAddressRanges
268     //
269     // Method that retrieves address range(s) for a given filename and line number.
270     
271     API_EXPORT(Int, (fileName, lineNo, ranges),
272     bool,getAddressRanges,(const char * fileName, unsigned int lineNo, std::vector< std::pair< unsigned long, unsigned long > > & ranges ));
273         
274     //  BPatch_addressSpace::findFunctionByAddr
275     //  
276     //  Returns the function containing an address
277
278     API_EXPORT(Int, (addr),
279     BPatch_function *,findFunctionByAddr,(void *addr));
280
281      //  BPatch_addressSpace::getImage
282     //
283     //  Obtain BPatch_image associated with this BPatch_addressSpace
284
285     API_EXPORT(Int, (),
286     BPatch_image *,getImage,());
287
288
289     //  BPatch_addressSpace::malloc
290     //  
291     //  Allocate memory for a new variable in the mutatee process
292
293     API_EXPORT(Int, (n),
294     BPatch_variableExpr *,malloc,(int n));
295
296     //  BPatch_addressSpace::malloc
297     //  
298     //  Allocate memory for a new variable in the mutatee process
299
300     API_EXPORT(ByType, (type),
301     BPatch_variableExpr *,malloc,(const BPatch_type &type));
302
303     API_EXPORT(Int, (at_addr, type, var_name, in_module),
304     BPatch_variableExpr *, createVariable,(Dyninst::Address at_addr, 
305                                            BPatch_type *type,
306                                            std::string var_name = std::string(""),
307                                            BPatch_module *in_module = NULL));
308
309     //  BPatch_addressSpace::free
310     //  
311     //  Free memory allocated by Dyninst in the mutatee process
312
313     API_EXPORT(Int, (ptr),
314     bool,free,(BPatch_variableExpr &ptr));
315
316     // BPatch_addressSpace::createVariable
317     // 
318     // Wrap an existing piece of allocated memory with a BPatch_variableExpr.
319     // Used (for instance) by the shared memory library to wrap its externally
320     // allocated memory for use by BPatch.
321     
322     API_EXPORT(Int, (name, addr, type),
323                BPatch_variableExpr *, createVariable, 
324                (std::string name, Dyninst::Address addr, BPatch_type *type = NULL));
325
326     API_EXPORT(Int, (regs),
327                bool, getRegisters, (std::vector<BPatch_register> &regs));
328
329     API_EXPORT(Int, (regName, reg),
330     bool, createRegister_NP, (std::string regName, BPatch_register &reg)); 
331
332     API_EXPORT_V(Int, (allowtraps),
333                void, allowTraps, (bool allowtraps));
334
335     //  BPatch_addressSpace::loadLibrary
336     //  
337     //  Load a shared library into the mutatee's address space
338     //  Returns true if successful
339     //
340     //  the reload argument is used by save the world to determine
341     //  if this library should be reloaded by the mutated binary
342     //  when it starts up. this is up to the user because loading
343     //  an extra shared library could hide access to the 'correct'
344     //  function by redefining a function  
345
346     API_EXPORT_VIRT(Int, (libname, reload),
347     bool, loadLibrary,(const char *libname, bool reload = false));
348
349 };
350
351 #endif