mods/additions largely under the USE_STL_VECTOR and IBM_BPATCH_COMPAT flags
[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 instInstance;
62 class BPatch;
63 class BPatch_thread;
64
65
66 /*
67  * Used to specify whether a snippet is to be called before the instructions
68  * at the point where it is inserted, or after.
69  */
70 typedef enum {
71     BPatch_callBefore,
72     BPatch_callAfter
73 } BPatch_callWhen;
74
75
76 /*
77  * Used to specify whether a snippet should be installed before other snippets
78  * that have previously been inserted at the same point, or after.
79  */
80 typedef enum {
81     BPatch_firstSnippet,
82     BPatch_lastSnippet
83 } BPatch_snippetOrder;
84
85
86 /*
87  * Contains information about the code that was inserted by an earlier call to
88  * Bpatch_thread::insertSnippet.
89  */
90 class BPATCH_DLL_EXPORT BPatchSnippetHandle {
91     friend class BPatch_thread;
92 private:
93     BPatch_Vector<instInstance *> instance;
94 public:
95     process *proc;
96
97     BPatchSnippetHandle(process *_proc) : proc(_proc) {};
98     ~BPatchSnippetHandle();
99
100     void add(instInstance *pointInstance);
101 };
102
103 /*
104  * Represents a thread of execution.
105  */
106 class BPATCH_DLL_EXPORT BPatch_thread {
107     friend class BPatch;
108     friend class BPatch_image;
109     friend class BPatch_function;
110     friend class process;
111     friend bool pollForStatusChange();
112
113     process             *proc;
114     BPatch_image        *image;
115     int                 lastSignal;
116     int                 exitCode;
117     bool                mutationsActive;
118     bool                createdViaAttach;
119     bool                detached;
120
121     bool                waitingForOneTimeCode;
122     void                *lastOneTimeCodeReturnValue;
123
124     bool                unreportedStop;
125     bool                unreportedTermination;
126
127     void                setUnreportedStop(bool new_value)
128                                 { unreportedStop = new_value; }
129     void                setUnreportedTermination(bool new_value)
130                                 { unreportedTermination = new_value; }
131
132     bool                pendingUnreportedStop()
133                                 { return unreportedStop; }
134     bool                pendingUnreportedTermination()
135                                 { return unreportedTermination; }
136
137     bool                statusIsStopped();
138     bool                statusIsTerminated();
139
140     static void         oneTimeCodeCallbackDispatch(process *theProc,
141                                                     void *userData,
142                                                     void *returnValue);
143
144     void                oneTimeCodeCallback(void *userData,
145                                             void *returnValue);
146
147     void                *oneTimeCodeInternal(const BPatch_snippet &expr);
148
149 protected:
150     // for creating a process
151     BPatch_thread(char *path, char *argv[], char *envp[] = NULL, int stdin_fd = 0,
152         int stdout_fd = 1, int stderr_fd = 2);
153     // for attaching
154     BPatch_thread(char *path, int pid); 
155
156     // for forking
157     BPatch_thread(int childPid, process *proc);
158
159 public:
160     ~BPatch_thread();
161
162     BPatch_image *getImage() { return image; }
163
164     int         getPid();
165
166     bool        stopExecution();
167     bool        continueExecution();
168     bool        terminateExecution();
169
170     bool        isStopped();
171     int         stopSignal();
172     bool        isTerminated();
173     int         terminationStatus();
174
175     void        detach(bool cont);
176
177     bool        dumpCore(const char *file, bool terminate);
178     bool        dumpImage(const char *file);
179     char*       dumpPatchedImage(const char* file);//ccw 28 oct 2001
180
181     BPatch_variableExpr *malloc(int n);
182     BPatch_variableExpr *malloc(const BPatch_type &type);
183     void        free(const BPatch_variableExpr &ptr);
184
185     // to provide backward compatiblity 
186     BPatchSnippetHandle *insertSnippet(
187                             const BPatch_snippet &expr,
188                             BPatch_point &point,
189                             BPatch_snippetOrder order = BPatch_firstSnippet);
190
191     BPatchSnippetHandle *insertSnippet(
192                             const BPatch_snippet &expr,
193                             BPatch_point &point,
194                             BPatch_callWhen when,
195                             BPatch_snippetOrder order);
196
197     BPatchSnippetHandle *insertSnippet(
198                             const BPatch_snippet &expr,
199                             const BPatch_Vector<BPatch_point *> &points,
200                             BPatch_snippetOrder order = BPatch_firstSnippet);
201
202     BPatchSnippetHandle *insertSnippet(
203                             const BPatch_snippet &expr,
204                             const BPatch_Vector<BPatch_point *> &points,
205                             BPatch_callWhen when,
206                             BPatch_snippetOrder order = BPatch_firstSnippet);
207
208     bool        deleteSnippet(BPatchSnippetHandle *handle);
209
210     void        setMutationsActive(bool activate);
211
212     bool        replaceFunctionCall(BPatch_point &point,
213                                     BPatch_function &newFunc);
214     bool        removeFunctionCall(BPatch_point &point);
215     bool        replaceFunction(BPatch_function &oldFunc,
216                                 BPatch_function &newFunc);
217
218     void        oneTimeCode(const BPatch_snippet &expr) {
219                         oneTimeCodeInternal(expr);
220                     };
221
222     bool        loadLibrary(char *libname);
223
224     //method that retrieves the line number and file name corresponding 
225     //to an address
226     bool getLineAndFile(unsigned long addr,unsigned short& lineNo,
227                         char* fileName,int length);
228
229         void startSaveWorld();//ccw 23 jan 2002
230 #ifdef IBM_BPATCH_COMPAT
231     bool isThreaded() { return false; }
232     bool addSharedObject(const char *name, const unsigned long loadaddr);
233 #endif
234
235 };
236
237 #endif /* BPatch_thread_h_ */