2 * Copyright (c) 1996 Barton P. Miller
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.
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.
18 * (for other uses, please contact us at paradyn@cs.wisc.edu)
20 * All warranties, including without limitation, any warranty of
21 * merchantability or fitness for a particular purpose, are hereby
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.
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.
48 #include "BPatch_type.h"
49 #include "BPatch_libInfo.h"
52 extern bool dyninstAPI_init();
53 extern int dyninstAPI_handleSigChild(int pid, int status);
56 BPatch *BPatch::bpatch = NULL;
62 * Constructor for BPatch. Performs one-time initialization needed by the
65 BPatch::BPatch() : errorHandler(NULL), typeCheckOn(true)
68 extern double cyclesPerSecond;
69 extern double timing_loop(const unsigned, const unsigned);
71 // Save a pointer to the one-and-only bpatch object.
75 * (indicate an error somehow)
78 // XXX dyninstAPI_init returns success/failure -- should pass on somehow
80 cyclesPerSecond = timing_loop(1, 100000) * 1000000;
83 * Create the library private info object.
85 info = new BPatch_libInfo;
88 * Create the "error" and "untyped" types.
90 type_Error = new BPatch_type("<error>", true);
91 type_Untyped = new BPatch_type("<no type>", true);
94 * Initialize hash table of standard types.
96 stdTypes = new BPatch_typeCollection;
97 stdTypes->addType(new BPatch_type("int"));
98 stdTypes->addType(new BPatch_type("char *"));
105 * Destructor for BPatch. Free allocated memory.
119 * BPatch::registerErrorCallback
121 * Registers a function that is to be called by the library when an error
122 * occurs or when there is status to report. Returns the address of the
123 * previously registered error callback function.
125 * function The function to be called.
127 BPatchErrorCallback BPatch::registerErrorCallback(BPatchErrorCallback function)
129 BPatchErrorCallback ret;
132 errorHandler = function;
139 * BPatch::getEnglishErrorString
141 * Returns the descriptive error string for the passed error number.
143 * number The number that identifies the error.
145 const char *BPatch::getEnglishErrorString(int /* number */)
152 * BPatch::reportError
154 * Report an error using the callback mechanism.
156 * severity The severity level of the error.
157 * number Identifies the error.
158 * str A string to pass as the first element of the list of strings
159 * given to the callback function.
161 void BPatch::reportError(BPatchErrorLevel severity, int number, const char *str)
163 if (errorHandler != NULL) {
164 errorHandler(severity, number, &str);
170 * BPatch::formatErrorString
172 * Takes a format string with an error message (obtained from
173 * getEnglishErrorString) and an array of parameters that were passed to an
174 * error callback function, and creates a string with the parameters
175 * substituted into it.
177 * dst The address into which the formatted string should be copied.
178 * size If the formatted string is equal to or longer than this number
179 * of characters, then it will be truncated to size-1 characters
180 * and terminated with a nul ('\0').
181 * fmt The format string (returned by a function such as
182 * getEnglishErrorString).
183 * params The array of parameters that were passed to an error callback
186 void BPatch::formatErrorString(char *dst, int size,
187 const char *fmt, const char **params)
191 while (size > 1 && *fmt) {
193 if (fmt[1] == '\0') {
195 } else if (fmt[1] == '%') {
198 } else if (fmt[1] == 's') {
199 char *p = (char *)params[cur_param++];
200 while (size > 1 && *p) {
222 * BPatch::pidToThread
224 * Given a process ID, this function returns a pointer to the associated
225 * BPatch_thread object (or NULL if there is none).
227 BPatch_thread *BPatch::pidToThread(int pid)
229 if (info->threadsByPid.defines(pid))
230 return info->threadsByPid[pid];
239 * Returns a vector of all threads that are currently defined. Includes
240 * threads created directly using the library and those created with UNIX fork
241 * or Windows NT spawn system calls. The caller is responsible for deleting
242 * the vector when it is no longer needed.
244 BPatch_Vector<BPatch_thread *> *BPatch::getThreads()
246 BPatch_Vector<BPatch_thread *> *result = new BPatch_Vector<BPatch_thread *>;
248 dictionary_hash_iter<int, BPatch_thread *> ti(info->threadsByPid);
251 BPatch_thread *thread;
253 while (ti.next(pid, thread))
254 result->push_back(thread);
261 * pollForStatusChange
263 * Checks for changes in the state of any child process, and returns true if
264 * it discovers any such changes. Also updates the process object
265 * representing each process for which a change is detected.
267 * This function is declared as a friend of BPatch_thread so that it can use
268 * the BPatch_thread::pidToThread call and so that it can set the lastSignal
269 * member of a BPatch_thread object.
271 bool pollForStatusChange()
276 while ((pid = process::waitProcs(&status)) > 0) {
277 // There's been a change in a child process
279 assert(BPatch::bpatch != NULL);
280 BPatch_thread *thread = BPatch::bpatch->pidToThread(pid);
281 if (thread == NULL) {
282 fprintf(stderr, "Warning - wait returned status of an unknown process (%d)\n", pid);
284 if (thread != NULL) {
285 if (WIFSTOPPED(status))
286 thread->lastSignal = WSTOPSIG(status);
287 else if (WIFSIGNALED(status))
288 thread->lastSignal = WTERMSIG(status);
289 else if (WIFEXITED(status))
290 thread->lastSignal = 0; /* XXX Make into some constant */
292 dyninstAPI_handleSigChild(pid, status);
299 * BPatch::registerThread
301 * Register a new BPatch_thread object with the BPatch library (this function
302 * is called only by the constructor for BPatch_thread).
304 * thread A pointer to the thread to register.
306 void BPatch::registerThread(BPatch_thread *thread)
308 assert(!info->threadsByPid.defines(thread->getPid()));
309 info->threadsByPid[thread->getPid()] = thread;
314 * BPatch::unRegisterThread
316 * Remove the BPatch_thread associated with a given pid from the list of
317 * threads being managed by the library.
319 * pid The pid of the thread to be removed.
321 void BPatch::unRegisterThread(int pid)
323 assert(info->threadsByPid.defines(pid));
324 info->threadsByPid.undef(pid);