Add BPatch_thread::oneTimeCodeAsync and other changes to support it.
[dyninst.git] / dyninstAPI / h / BPatch_thread.h
1 /*
2  * Copyright (c) 1996 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  * This license is for research uses.  For such uses, there is no
12  * charge. We define "research use" to mean you may freely use it
13  * inside your organization for whatever purposes you see fit. But you
14  * may not re-distribute Paradyn or parts of Paradyn, in any form
15  * source or binary (including derivatives), electronic or otherwise,
16  * to any other organization or entity without our permission.
17  * 
18  * (for other uses, please contact us at paradyn@cs.wisc.edu)
19  * 
20  * All warranties, including without limitation, any warranty of
21  * merchantability or fitness for a particular purpose, are hereby
22  * excluded.
23  * 
24  * By your use of Paradyn, you understand and agree that we (or any
25  * other person or entity with proprietary rights in Paradyn) are
26  * under no obligation to provide either maintenance services,
27  * update services, notices of latent defects, or correction of
28  * defects for Paradyn.
29  * 
30  * Even if advised of the possibility of such damages, under no
31  * circumstances shall we (or any other person or entity with
32  * proprietary rights in the software licensed hereunder) be liable
33  * to you or any third party for direct, indirect, or consequential
34  * damages of any character regardless of type of action, including,
35  * without limitation, loss of profits, loss of use, loss of good
36  * will, or computer failure or malfunction.  You agree to indemnify
37  * us (and any other person or entity with proprietary rights in the
38  * software licensed hereunder) for any and all liability it may
39  * incur to third parties resulting from your use of Paradyn.
40  */
41
42 #ifndef _BPatch_thread_h_
43 #define _BPatch_thread_h_
44
45
46 /*
47  * The following is a kludge so that the functions that refer to signals (such
48  * as BPatch_thread::stopSignal) can emulate the Unix behavior on Windows NT.
49  */
50 #include <signal.h>
51 #ifndef SIGTRAP
52 #define SIGTRAP         5
53 #endif
54
55 #include "BPatch_dll.h"
56 #include "BPatch_Vector.h"
57 #include "BPatch_image.h"
58 #include "BPatch_snippet.h"
59
60 class process;
61 class miniTrampHandle;
62 class BPatch;
63 class BPatch_thread;
64
65 // BPatch_callWhen is defined in BPatch_point.h
66
67 /*
68  * Used to specify whether a snippet should be installed before other snippets
69  * that have previously been inserted at the same point, or after.
70  */
71 typedef enum {
72     BPatch_firstSnippet,
73     BPatch_lastSnippet
74 } BPatch_snippetOrder;
75
76
77 /*
78  * Contains information about the code that was inserted by an earlier call to
79  * Bpatch_thread::insertSnippet.
80  */
81 class BPATCH_DLL_EXPORT BPatchSnippetHandle {
82     friend class BPatch_thread;
83 private:
84     BPatch_Vector<miniTrampHandle *> mtHandles;
85       
86 public:
87     process *proc;
88
89     BPatchSnippetHandle(process *_proc) : proc(_proc) {};
90     ~BPatchSnippetHandle();
91
92     void add(miniTrampHandle *pointInstance);
93     void getMiniTrampHandles(BPatch_Vector<miniTrampHandle *> *save_mtHandles) {
94       for(unsigned i=0; i<mtHandles.size(); i++)
95         (*save_mtHandles).push_back(mtHandles[i]);
96     }
97 };
98
99 /*
100  * Represents a thread of execution.
101  */
102 class BPATCH_DLL_EXPORT BPatch_thread {
103     friend class BPatch;
104     friend class BPatch_image;
105     friend class BPatch_function;
106     friend class process;
107     friend bool pollForStatusChange();
108
109     process             *proc;
110     BPatch_image        *image;
111     int                 lastSignal;
112     int                 exitCode;
113     bool                mutationsActive;
114     bool                createdViaAttach;
115     bool                detached;
116
117     bool                unreportedStop;
118     bool                unreportedTermination;
119
120     void                setUnreportedStop(bool new_value)
121                                 { unreportedStop = new_value; }
122     void                setUnreportedTermination(bool new_value)
123                                 { unreportedTermination = new_value; }
124
125     bool                pendingUnreportedStop()
126                                 { return unreportedStop; }
127     bool                pendingUnreportedTermination()
128                                 { return unreportedTermination; }
129
130     bool                statusIsStopped();
131     bool                statusIsTerminated();
132
133     static void         oneTimeCodeCallbackDispatch(process *theProc,
134                                                     void *userData,
135                                                     void *returnValue);
136
137     void                *oneTimeCodeInternal(const BPatch_snippet &expr,
138                                              void *userData,
139                                              bool synchronous);
140
141 protected:
142     // for creating a process
143     BPatch_thread(char *path, char *argv[], char *envp[] = NULL, int stdin_fd = 0,
144         int stdout_fd = 1, int stderr_fd = 2);
145     // for attaching
146     BPatch_thread(char *path, int pid); 
147
148     // for forking
149     BPatch_thread(int childPid, process *proc);
150
151 public:
152     ~BPatch_thread();
153
154     BPatch_image *getImage() { return image; }
155
156     int         getPid();
157
158     bool        stopExecution();
159     bool        continueExecution();
160     bool        terminateExecution();
161
162     bool        isStopped();
163     int         stopSignal();
164     bool        isTerminated();
165     int         terminationStatus();
166
167     void        detach(bool cont);
168
169     bool        dumpCore(const char *file, bool terminate);
170     bool        dumpImage(const char *file);
171     char*       dumpPatchedImage(const char* file);//ccw 28 oct 2001
172
173     BPatch_variableExpr *malloc(int n);
174     BPatch_variableExpr *malloc(const BPatch_type &type);
175     void        free(const BPatch_variableExpr &ptr);
176     BPatch_variableExpr *getInheritedVariable(const BPatch_variableExpr &pVar);
177     BPatchSnippetHandle *getInheritedSnippet(
178                                           BPatchSnippetHandle &parentSnippet);
179
180
181     // to provide backward compatiblity 
182     BPatchSnippetHandle *insertSnippet(
183                             const BPatch_snippet &expr,
184                             BPatch_point &point,
185                             BPatch_snippetOrder order = BPatch_firstSnippet);
186
187     BPatchSnippetHandle *insertSnippet(
188                             const BPatch_snippet &expr,
189                             BPatch_point &point,
190                             BPatch_callWhen when,
191                             BPatch_snippetOrder order = BPatch_firstSnippet);
192
193     BPatchSnippetHandle *insertSnippet(
194                             const BPatch_snippet &expr,
195                             const BPatch_Vector<BPatch_point *> &points,
196                             BPatch_snippetOrder order = BPatch_firstSnippet);
197
198     BPatchSnippetHandle *insertSnippet(
199                             const BPatch_snippet &expr,
200                             const BPatch_Vector<BPatch_point *> &points,
201                             BPatch_callWhen when,
202                             BPatch_snippetOrder order = BPatch_firstSnippet);
203
204     bool        deleteSnippet(BPatchSnippetHandle *handle);
205
206     void        setMutationsActive(bool activate);
207
208     bool        replaceFunctionCall(BPatch_point &point,
209                                     BPatch_function &newFunc);
210     bool        removeFunctionCall(BPatch_point &point);
211     bool        replaceFunction(BPatch_function &oldFunc,
212                                 BPatch_function &newFunc);
213
214     void        oneTimeCode(const BPatch_snippet &expr) {
215                         oneTimeCodeInternal(expr, NULL, true);
216                     };
217
218     void        oneTimeCodeAsync(const BPatch_snippet &expr, void *userData = NULL) {
219                         oneTimeCodeInternal(expr, userData, false);
220                     };
221
222         //the reload argument is used by save the world to determine
223         //if this library should be reloaded by the mutated binary
224         //when it starts up. this is up to the user because loading
225         //an extra shared library could hide access to the 'correct'
226         //function by redefining a function  
227     bool        loadLibrary(char *libname, bool reload = false); 
228
229     //method that retrieves the line number and file name corresponding 
230     //to an address
231     bool getLineAndFile(unsigned long addr,unsigned short& lineNo,
232                         char* fileName,int length);
233
234         void startSaveWorld();//ccw 23 jan 2002
235 #ifdef IBM_BPATCH_COMPAT
236     bool isThreaded() { return false; }
237     bool addSharedObject(const char *name, const unsigned long loadaddr);
238 #endif
239
240 };
241
242 #endif /* BPatch_thread_h_ */