mods/additions largely under the USE_STL_VECTOR and IBM_BPATCH_COMPAT flags
[dyninst.git] / dyninstAPI / h / BPatch.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_h_
43 #define _BPatch_h_
44
45 #include <stdio.h>
46 #include "BPatch_dll.h"
47 #include "BPatch_Vector.h"
48 #include "BPatch_thread.h"
49 #include "BPatch_type.h"
50
51 #ifdef mips_unknown_ce2_11 //ccw 28 july 2000
52 #include "remoteDevice.h"
53 #endif
54
55 class BPatch_typeCollection;
56 class BPatch_libInfo;
57 class BPatch_module;
58
59 typedef enum {
60     BPatchFatal, BPatchSerious, BPatchWarning, BPatchInfo
61 } BPatchErrorLevel;
62
63 typedef void (*BPatchErrorCallback)(BPatchErrorLevel severity,
64                                     int number,
65                                     const char **params);
66
67 typedef void (*BPatchDynLibraryCallback)(BPatch_thread *thr,
68                                          BPatch_module *mod,
69                                          bool load);
70
71 typedef void (*BPatchForkCallback)(BPatch_thread *parent, BPatch_thread *child);
72
73 typedef void (*BPatchExecCallback)(BPatch_thread *proc);
74
75 typedef void (*BPatchExitCallback)(BPatch_thread *proc, int code);
76
77 typedef void (*BPatchSignalCallback)(BPatch_thread *proc, int sigNum);
78
79
80 #ifdef IBM_BPATCH_COMPAT
81 typedef void *BPatch_Address;
82 typedef void (*BPatchLoggingCallback)(char *msg, int);
83 extern void setLogging_NP(BPatchLoggingCallback func, int);
84
85 typedef void (*BPatchThreadEventCallback)(BPatch_thread *thr, void *arg1, void *arg2);
86
87 #define BP_OK                   0
88 #define BP_Pending              1
89 #define BP_Delayed              2
90 #define BP_child                3
91 #define BP_state                4
92 #define BP_pthdbBadContext      5
93 #define BP_lastCode             6
94
95 extern int eCodes[BP_lastCode];
96
97 #endif
98
99 class BPATCH_DLL_EXPORT BPatch {
100     friend class BPatch_thread;
101     friend class process;
102
103     BPatch_libInfo      *info;
104
105     BPatchErrorCallback         errorHandler;
106     BPatchDynLibraryCallback    dynLibraryCallback;
107     BPatchForkCallback          postForkCallback;
108     BPatchForkCallback          preForkCallback;
109     BPatchExecCallback          execCallback;
110     BPatchExitCallback          exitCallback;
111     bool        typeCheckOn;
112     int         lastError;
113     bool        debugParseOn;
114     bool        baseTrampDeletionOn;
115
116     bool        getThreadEvent(bool block);
117     bool        havePendingEvent();
118
119     /* If true, trampolines can recurse to their heart's content.
120        Defaults to false */
121     bool        trampRecursiveOn;
122     bool        forceRelocation_NP;
123
124 public:
125     static BPatch                *bpatch;
126
127     BPatch_builtInTypeCollection *builtInTypes;
128     BPatch_typeCollection        *stdTypes;
129     BPatch_typeCollection        *APITypes; //API/User defined types
130     BPatch_type                  *type_Error;
131     BPatch_type                  *type_Untyped;
132 #ifdef mips_unknown_ce2_11 //ccw 28 july 2000
133         remoteDevice *rDevice;  //the ctor sets up the connection here and
134                                 //gets the tramptemplate from the CE device.
135 #endif
136
137  
138     bool isTypeChecked() { return typeCheckOn; }
139     bool parseDebugInfo() { return debugParseOn; }
140     bool baseTrampDeletion() { return baseTrampDeletionOn; }
141     bool isTrampRecursive() { return trampRecursiveOn; }
142     bool hasForcedRelocation_NP() { return forceRelocation_NP; }
143
144     // The following are only to be called by the library:
145     void registerProvisionalThread(int pid);
146     void registerForkedThread(int parentPid, int childPid, process *proc);
147     void registerExec(BPatch_thread *thread);
148     void registerExit(BPatch_thread *thread, int code);
149     void registerThread(BPatch_thread *thread);
150     void unRegisterThread(int pid);
151
152     BPatch_thread *getThreadByPid(int pid, bool *exists = NULL);
153
154     static void reportError(BPatchErrorLevel severity, int number, const char *str);
155
156     void clearError() { lastError = 0; }
157     int getLastError() { return lastError; }
158     // End of functions that are for internal use only
159
160 #ifdef IBM_BPATCH_COMPAT
161     int getLastErrorCode() { return lastError; }
162
163     BPatchThreadEventCallback registerDetachDoneCallback(BPatchThreadEventCallback) { return NULL; }
164     BPatchThreadEventCallback registerSnippetRemovedCallback(BPatchThreadEventCallback) { return NULL; }
165     
166     BPatchExitCallback registerSignalCallback(BPatchThreadEventCallback func, int sigNum) {return NULL;}
167     BPatchExitCallback registerExitCallback(BPatchThreadEventCallback func);
168
169     BPatchThreadEventCallback registerRPCTerminationCallback(BPatchThreadEventCallback);
170     BPatchThreadEventCallback           RPCdoneCallback;
171 #endif
172
173     BPatch();
174     ~BPatch();
175
176     static const char *getEnglishErrorString(int number);
177     static void formatErrorString(char *dst, int size,
178                                   const char *fmt, const char **params);
179     BPatchErrorCallback registerErrorCallback(BPatchErrorCallback function);
180     BPatchDynLibraryCallback registerDynLibraryCallback(BPatchDynLibraryCallback function);
181     BPatchForkCallback registerPostForkCallback(BPatchForkCallback func);
182     BPatchForkCallback registerPreForkCallback(BPatchForkCallback func);
183     BPatchExecCallback registerExecCallback(BPatchExecCallback func);
184     BPatchExitCallback registerExitCallback(BPatchExitCallback func);
185
186     BPatch_Vector<BPatch_thread*> *getThreads();
187
188     void setDebugParsing(bool x) { debugParseOn = x; }
189     void setBaseTrampDeletion(bool x) { baseTrampDeletionOn = x; }
190     void setTypeChecking(bool x) { typeCheckOn = x; }
191     void setTrampRecursive(bool x) { trampRecursiveOn = x; }
192     void setForcedRelocation_NP(bool x) { forceRelocation_NP = x; }
193
194     BPatch_thread *createProcess(char *path, char *argv[], 
195         char *envp[] = NULL, int stdin_fd=0, int stdout_fd=1, int stderr_fd=2);
196     BPatch_thread *attachProcess(char *path, int pid);
197
198     // Create Enum types. 
199     BPatch_type * createEnum(const char * name, 
200         BPatch_Vector<char *> elementNames,
201         BPatch_Vector<int> elementIds);
202     
203     // API selects elemetIds
204     BPatch_type * createEnum(const char * name, 
205         BPatch_Vector<char *> elementNames);
206
207     // Create Struct types. 
208     BPatch_type * createStruct( const char * name,
209                                 BPatch_Vector<char *> fieldNames,
210                                 BPatch_Vector<BPatch_type *> fieldTypes);
211
212     // Create Union types. 
213     BPatch_type * createUnion( const char * name, 
214                                 BPatch_Vector<char *> fieldNames,
215                                 BPatch_Vector<BPatch_type *> fieldTypes);
216  
217     // Creates BPatch_array type or symtyperanges ( scalars with upper and
218    //lower bound).
219     BPatch_type * createArray( const char * name, BPatch_type * ptr,
220                                unsigned int low, unsigned int hi );
221
222     // Creates BPatch_pointer types      
223     BPatch_type * createPointer( const char * name, BPatch_type * ptr,
224                                  int size = sizeof(void *));
225
226     // Creates BPatch_scalar types
227     BPatch_type * createScalar( const char * name, int size );
228     
229     // Creates typedefs.
230     BPatch_type * createTypedef( const char * name, BPatch_type * ptr );
231          
232     bool        pollForStatusChange();
233     bool        waitForStatusChange();
234
235     bool waitUntilStopped(BPatch_thread *appThread);
236 };
237
238 #endif /* _BPatch_h_ */