This commit allows us to force the relocation of functions being
[dyninst.git] / dyninstAPI / src / BPatch.C
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 // $Id: BPatch.C,v 1.36 2001/07/11 21:19:57 gurari Exp $
43
44 #include <stdio.h>
45 #include <assert.h>
46 #include <signal.h>
47
48 #define BPATCH_FILE
49 #include "BPatch.h"
50 #include "BPatch_libInfo.h"
51 #include "process.h"
52 #include "BPatch_collections.h"
53 #include "common/h/timing.h"
54
55 #ifdef i386_unknown_nt4_0
56 #include "nt_signal_emul.h"
57 #endif
58
59 extern bool dyninstAPI_init();
60 extern int handleSigChild(int pid, int status);
61
62
63 BPatch *BPatch::bpatch = NULL;
64
65 /*
66  * BPatch::BPatch
67  *
68  * Constructor for BPatch.  Performs one-time initialization needed by the
69  * library.
70  */
71 BPatch::BPatch()
72   : info(NULL),
73     errorHandler(NULL),
74     dynLibraryCallback(NULL),
75     typeCheckOn(true),
76     lastError(0),
77     debugParseOn(true),
78     trampRecursiveOn(false),
79     forceRelocation_NP(false),
80     builtInTypes(NULL),
81     stdTypes(NULL),
82     type_Error(NULL),
83     type_Untyped(NULL)
84 {
85     extern bool init();
86
87     // Save a pointer to the one-and-only bpatch object.
88     if (bpatch == NULL)
89         bpatch = this;
90     /* XXX else
91      *  (indicate an error somehow)
92      */
93
94     // XXX dyninstAPI_init returns success/failure -- should pass on somehow
95     dyninstAPI_init();
96     initCyclesPerSecond();
97
98     /*
99      * Create the library private info object.
100      */
101     info = new BPatch_libInfo;
102
103     /*
104      * Create the "error" and "untyped" types.
105      */
106     type_Error   = new BPatch_type("<error>", true);
107     type_Untyped = new BPatch_type("<no type>", true);
108
109     /*
110      * Initialize hash table of standard types.
111      */
112     stdTypes = new BPatch_typeCollection;
113     stdTypes->addType(new BPatch_type("int",-1, BPatch_scalar, sizeof(int)));
114     stdTypes->addType(new BPatch_type("char *",-3, BPatch_scalar, sizeof(char*)));
115     BPatch_type *voidType = new BPatch_type("void",-11, BPatch_scalar, 0);
116     stdTypes->addType(voidType);
117     stdTypes->addType(new BPatch_type("void *",-4, BPatch_pointer, voidType));
118     stdTypes->addType(new BPatch_type("float",-12, BPatch_scalar, sizeof(float)));
119
120     /*
121      * Initialize hash table of API types.
122      */
123     APITypes = new BPatch_typeCollection;
124
125     /*
126      *  Initialize hash table of Built-in types.
127      *  Negative type numbers defined in the gdb stab-docs
128      */
129      
130     builtInTypes = new BPatch_builtInTypeCollection;
131     
132     // NOTE: integral type  mean twos-complement
133     // -1  int, 32 bit signed integral type
134     // in stab document, size specified in bits, system size is in bytes
135     builtInTypes->addBuiltInType(new BPatch_type("int",-1, BPatch_built_inType,
136                                                  4));
137     // -2  char, 8 bit type holding a character. GDB & dbx(AIX) treat as signed
138     builtInTypes->addBuiltInType(new BPatch_type("char",-2,
139                                                  BPatch_built_inType, 1));
140     // -3  short, 16 bit signed integral type
141     builtInTypes->addBuiltInType(new BPatch_type("short",-3,
142                                                  BPatch_built_inType, 2));
143     // -4  long, 32/64 bit signed integral type
144     builtInTypes->addBuiltInType(new BPatch_type("long",-4,
145                                                  BPatch_built_inType, 
146                                                  sizeof(long)));
147     // -5  unsigned char, 8 bit unsigned integral type
148     builtInTypes->addBuiltInType(new BPatch_type("unsigned char",-5,
149                                                  BPatch_built_inType, 1));
150     // -6  signed char, 8 bit signed integral type
151     builtInTypes->addBuiltInType(new BPatch_type("signed char",-6,
152                                                  BPatch_built_inType, 1));
153     // -7  unsigned short, 16 bit unsigned integral type
154     builtInTypes->addBuiltInType(new BPatch_type("unsigned short",-7,
155                                                  BPatch_built_inType, 2));
156     // -8  unsigned int, 32 bit unsigned integral type
157     builtInTypes->addBuiltInType(new BPatch_type("unsigned int",-8,
158                                                  BPatch_built_inType, 4));
159     // -9  unsigned, 32 bit unsigned integral type
160     builtInTypes->addBuiltInType(new BPatch_type("unsigned",-9,
161                                                  BPatch_built_inType,4));
162     // -10 unsigned long, 32 bit unsigned integral type
163     builtInTypes->addBuiltInType(new BPatch_type("unsigned long",-10,
164                                                  BPatch_built_inType, 
165                                                  sizeof(unsigned long)));
166     // -11 void, type indicating the lack of a value
167     //  XXX-size may not be correct jdd 4/22/99
168     builtInTypes->addBuiltInType(new BPatch_type("void",-11,
169                                                  BPatch_built_inType,
170                                                  0));
171     // -12 float, IEEE single precision
172     builtInTypes->addBuiltInType(new BPatch_type("float",-12,
173                                                  BPatch_built_inType,
174                                                  sizeof(float)));
175     // -13 double, IEEE double precision
176     builtInTypes->addBuiltInType(new BPatch_type("double",-13,
177                                                  BPatch_built_inType,
178                                                  sizeof(double)));
179     // -14 long double, IEEE double precision, size may increase in future
180     builtInTypes->addBuiltInType(new BPatch_type("long double",-14,
181                                                  BPatch_built_inType,
182                                                  sizeof(long double)));
183     // -15 integer, 32 bit signed integral type
184     builtInTypes->addBuiltInType(new BPatch_type("integer",-15,
185                                                  BPatch_built_inType, 4));
186     // -16 boolean, 32 bit type. GDB/GCC 0=False, 1=True, all other values
187     //     have unspecified meaning
188     builtInTypes->addBuiltInType(new BPatch_type("boolean",-16,
189                                                  BPatch_built_inType, 4));
190     // -17 short real, IEEE single precision
191     //  XXX-size may not be correct jdd 4/22/99
192     builtInTypes->addBuiltInType(new BPatch_type("short real",-17,
193                                                  BPatch_built_inType,
194                                                  sizeof(float)));
195     // -18 real, IEEE double precision XXX-size may not be correct jdd 4/22/99 
196     builtInTypes->addBuiltInType(new BPatch_type("real",-18,
197                                                  BPatch_built_inType,
198                                                  sizeof(double)));
199     // -19 stringptr XXX- size of void * -- jdd 4/22/99
200     builtInTypes->addBuiltInType(new BPatch_type("stringptr",-19,
201                                                  BPatch_built_inType,
202                                                  sizeof(void *)));
203     // -20 character, 8 bit unsigned character type
204     builtInTypes->addBuiltInType(new BPatch_type("character",-20,
205                                                  BPatch_built_inType, 1));
206     // -21 logical*1, 8 bit type (Fortran, used for boolean or unsigned int)
207     builtInTypes->addBuiltInType(new BPatch_type("logical*1",-21,
208                                                  BPatch_built_inType, 1));
209     // -22 logical*2, 16 bit type (Fortran, some for boolean or unsigned int)
210     builtInTypes->addBuiltInType(new BPatch_type("logical*2",-22,
211                                                  BPatch_built_inType, 2));
212     // -23 logical*4, 32 bit type (Fortran, some for boolean or unsigned int)
213     builtInTypes->addBuiltInType(new BPatch_type("logical*4",-23,
214                                                  BPatch_built_inType, 4));
215     // -24 logical, 32 bit type (Fortran, some for boolean or unsigned int)
216     builtInTypes->addBuiltInType(new BPatch_type("logical",-24,
217                                                  BPatch_built_inType, 4));
218     // -25 complex, consists of 2 IEEE single-precision floating point values
219     builtInTypes->addBuiltInType(new BPatch_type("complex",-25,
220                                                  BPatch_built_inType,
221                                                  (sizeof(float)*2)));
222     // -26 complex, consists of 2 IEEE double-precision floating point values
223     builtInTypes->addBuiltInType(new BPatch_type("complex",-26,
224                                                  BPatch_built_inType,
225                                                  (sizeof(double)*2)));
226     // -27 integer*1, 8 bit signed integral type
227     builtInTypes->addBuiltInType(new BPatch_type("integer*1",-27,
228                                                  BPatch_built_inType, 1));
229     // -28 integer*2, 16 bit signed integral type
230     builtInTypes->addBuiltInType(new BPatch_type("integer*2",-28,
231                                                  BPatch_built_inType, 2));
232     // -29 integer*4, 32 bit signed integral type
233     builtInTypes->addBuiltInType(new BPatch_type("integer*4",-29,
234                                                  BPatch_built_inType, 4));
235     // -30 wchar, Wide character, 16 bits wide, unsigned (unknown format)
236     builtInTypes->addBuiltInType(new BPatch_type("wchar",-30,
237                                                  BPatch_built_inType, 2));
238     // -31 long long, 64 bit signed integral type
239     builtInTypes->addBuiltInType(new BPatch_type("long long",-31,
240                                                  BPatch_built_inType, 8));
241     // -32 unsigned long long, 64 bit unsigned integral type
242     builtInTypes->addBuiltInType(new BPatch_type("unsigned long long", -32,
243                                                  BPatch_built_inType, 8));
244     // -33 logical*8, 64 bit unsigned integral type
245     builtInTypes->addBuiltInType(new BPatch_type("logical*8",-33,
246                                                  BPatch_built_inType, 8));
247     // -34 integer*8, 64 bit signed integral type
248     builtInTypes->addBuiltInType(new BPatch_type("integer*8",-34,
249                                                  BPatch_built_inType, 8));
250
251     // default callbacks are null
252     postForkCallback = NULL;
253     preForkCallback = NULL;
254     errorHandler = NULL;
255     dynLibraryCallback = NULL;
256     execCallback = NULL;
257     exitCallback = NULL;
258
259 #ifdef DETACH_ON_THE_FLY
260     // Register handler for notification from detached inferiors
261     extern void initDetachOnTheFly();
262     initDetachOnTheFly();
263 #endif
264 }
265
266
267 /*
268  * BPatch::~BPatch
269  *
270  * Destructor for BPatch.  Free allocated memory.
271  */
272 BPatch::~BPatch()
273 {
274     delete info;
275
276     delete type_Error;
277     delete type_Untyped;
278
279     delete stdTypes;
280
281     bpatch = NULL;
282 }
283
284
285 /*
286  * BPatch::registerErrorCallback
287  *
288  * Registers a function that is to be called by the library when an error
289  * occurs or when there is status to report.  Returns the address of the
290  * previously registered error callback function.
291  *
292  * function     The function to be called.
293  */
294 BPatchErrorCallback BPatch::registerErrorCallback(BPatchErrorCallback function)
295 {
296     BPatchErrorCallback ret;
297
298     ret = errorHandler;
299     errorHandler = function;
300
301     return ret;
302 }
303
304
305 /*
306  * BPatch::registerPostForkCallback
307  *
308  * Registers a function that is to be called by the library when a new
309  * process has been forked off by an mutatee process.
310  *
311  * function     The function to be called.
312  */
313 BPatchForkCallback BPatch::registerPostForkCallback(BPatchForkCallback func)
314 {
315
316 #if !defined(sparc_sun_solaris2_4) && \
317     !defined(i386_unknown_solaris2_5) && \
318     !defined(alpha_dec_osf4_0) && \
319     !defined(mips_sgi_irix6_4)
320     reportError(BPatchWarning, 0,
321         "postfork callbacks not implemented on this platform\n");
322     return NULL;
323 #else
324     BPatchForkCallback ret;
325
326     ret = postForkCallback;
327     postForkCallback = func;
328
329     return ret;
330 #endif
331 }
332
333 /*
334  * BPatch::registerPreForkCallback
335  *
336  * Registers a function that is to be called by the library when a process
337  * is about to fork a new process
338  *
339  * function     The function to be called.
340  */
341 BPatchForkCallback BPatch::registerPreForkCallback(BPatchForkCallback func)
342 {
343 #if !defined(sparc_sun_solaris2_4) && \
344     !defined(i386_unknown_solaris2_5) &&\
345     !defined(alpha_dec_osf4_0) && \
346     !defined(mips_sgi_irix6_4)
347     reportError(BPatchWarning, 0,
348         "prefork callbacks not implemented on this platform\n");
349     return NULL;
350 #else
351     BPatchForkCallback ret;
352
353     ret = preForkCallback;
354     preForkCallback = func;
355
356     return ret;
357 #endif
358 }
359
360 /*
361  * BPatch::registerExecCallback
362  *
363  * Registers a function that is to be called by the library when a 
364  * process has just completed an exec* call
365  *
366  * func The function to be called.
367  */
368 BPatchExecCallback BPatch::registerExecCallback(BPatchExecCallback func)
369 {
370
371 #if !defined(sparc_sun_solaris2_4) && \
372     !defined(i386_unknown_solaris2_5) &&\
373     !defined(alpha_dec_osf4_0) && \
374     !defined(mips_sgi_irix6_4)
375     reportError(BPatchWarning, 0,
376         "exec callbacks not implemented on this platform\n");
377     return NULL;
378 #else
379     BPatchExecCallback ret;
380
381     ret = execCallback;
382     execCallback = func;
383
384     return ret;
385 #endif
386 }
387
388 /*
389  * BPatch::registerExecCallback
390  *
391  * Registers a function that is to be called by the library when a 
392  * process has just called the exit system call
393  *
394  * func The function to be called.
395  */
396 BPatchExitCallback BPatch::registerExitCallback(BPatchExitCallback func)
397 {
398
399 #if !defined(sparc_sun_solaris2_4) && \
400     !defined(i386_unknown_solaris2_5) &&\
401     !defined(alpha_dec_osf4_0) && \
402     !defined(mips_sgi_irix6_4)
403     reportError(BPatchWarning, 0,
404         "exit callbacks not implemented on this platform\n");
405     return NULL;
406 #else
407     BPatchExitCallback ret;
408
409     ret = exitCallback;
410     exitCallback = func;
411
412     return ret;
413 #endif
414 }
415
416 /*
417  * BPatch::registerDynLibraryCallback
418  *
419  * Registers a function that is to be called by the library when a dynamically
420  * loaded library is loaded or unloaded by a process under the API's control.
421  * Returns the address of the previously registered callback function.
422  *
423  * function     The function to be called.
424  */
425 BPatchDynLibraryCallback
426 BPatch::registerDynLibraryCallback(BPatchDynLibraryCallback function)
427 {
428     BPatchDynLibraryCallback ret;
429
430     ret = dynLibraryCallback;
431     dynLibraryCallback = function;
432
433     return ret;
434 }
435
436
437 /*
438  * BPatch::getEnglishErrorString
439  *
440  * Returns the descriptive error string for the passed error number.
441  *
442  * number       The number that identifies the error.
443  */
444 const char *BPatch::getEnglishErrorString(int /* number */)
445 {
446     return "%s";
447 }
448
449
450 /*
451  * BPatch::reportError
452  *
453  * Report an error using the callback mechanism.
454  *
455  * severity     The severity level of the error.
456  * number       Identifies the error.
457  * str          A string to pass as the first element of the list of strings
458  *              given to the callback function.
459  */
460 void BPatch::reportError(BPatchErrorLevel severity, int number, const char *str)
461 {
462     assert(bpatch != NULL);
463   
464         // don't log BPatchWarning or BPatchInfo messages as "errors"
465     if ((severity == BPatchFatal) || (severity == BPatchSerious))
466         bpatch->lastError = number;
467
468     if (bpatch->errorHandler != NULL) {
469         bpatch->errorHandler(severity, number, &str);
470     }
471 }
472
473
474 /*
475  * BPatch::formatErrorString
476  *
477  * Takes a format string with an error message (obtained from
478  * getEnglishErrorString) and an array of parameters that were passed to an
479  * error callback function, and creates a string with the parameters
480  * substituted into it.
481  *
482  * dst          The address into which the formatted string should be copied.
483  * size         If the formatted string is equal to or longer than this number
484  *              of characters, then it will be truncated to size-1 characters
485  *              and terminated with a nul ('\0').
486  * fmt          The format string (returned by a function such as
487  *              getEnglishErrorString).
488  * params       The array of parameters that were passed to an error callback
489  *              function.
490  */
491 void BPatch::formatErrorString(char *dst, int size,
492                                const char *fmt, const char **params)
493 {
494     int cur_param = 0;
495
496     while (size > 1 && *fmt) {
497         if (*fmt == '%') {
498             if (fmt[1] == '\0') {
499                 break;
500             } else if (fmt[1] == '%') {
501                 *dst++ = '%';
502                 size--;
503             } else if (fmt[1] == 's') {
504                 char *p = const_cast<char *>(params[cur_param++]);
505                 while (size > 1 && *p) {
506                     *dst++ = *p++;
507                     size--;
508                 }
509             } else {
510                 // Illegal specifier
511                 *dst++ = fmt[0];
512                 *dst++ = fmt[1];
513                 size -= 2;
514             }
515             fmt += 2;
516         } else {
517             *dst++ = *fmt++;
518             size--;
519         }
520     }
521     if (size > 0)
522         *dst = '\0';
523 }
524
525
526 /*
527  * BPatch::getThreadByPid
528  *
529  * Given a process ID, this function returns a pointer to the associated
530  * BPatch_thread object (or NULL if there is none).  Since a process may be
531  * registered provisionally with a thread object pointer of NULL, the boolean
532  * pointed to by the parameter "exists" is set to true if the pid exists in
533  * the table of processes, and false if it does not.
534  *
535  * pid          The pid to look up.
536  * exists       A pointer to a boolean to fill in with true if the pid exists
537  *              in the table and false if it does not.  NULL may be passed in
538  *              if this information is not required.
539  */
540 BPatch_thread *BPatch::getThreadByPid(int pid, bool *exists)
541 {
542     if (info->threadsByPid.defines(pid)) {
543         if (exists) *exists = true;
544         return info->threadsByPid[pid];
545     } else {
546         if (exists) *exists = false;
547         return NULL;
548     }
549 }
550
551
552 /*
553  * BPatch::getThreads
554  *
555  * Returns a vector of all threads that are currently defined.  Includes
556  * threads created directly using the library and those created with UNIX fork
557  * or Windows NT spawn system calls.  The caller is responsible for deleting
558  * the vector when it is no longer needed.
559  */
560 BPatch_Vector<BPatch_thread *> *BPatch::getThreads()
561 {
562     BPatch_Vector<BPatch_thread *> *result = new BPatch_Vector<BPatch_thread *>;
563
564     dictionary_hash_iter<int, BPatch_thread *> ti(info->threadsByPid);
565
566     int pid;
567     BPatch_thread *thread;
568
569     while (ti.next(pid, thread))
570         result->push_back(thread);
571
572     return result;
573 }
574
575
576 /*
577  * BPatch::registerProvisionalThread
578  *
579  * Register a new process that is not yet associated with a thread.
580  * (this function is called only by createProcess).
581  *
582  * pid          The pid of the process to register.
583  */
584 void BPatch::registerProvisionalThread(int pid)
585 {
586     assert(!info->threadsByPid.defines(pid));
587     info->threadsByPid[pid] = NULL;
588 }
589
590
591 /*
592  * BPatch::registerForkedThread
593  *
594  * Register a new process that is not yet associated with a thread.
595  * (this function is an upcall when a new process is created).
596  *
597  * parentPid            the pid of the parent process.
598  * childPid             The pid of the process to register.
599  * proc                 lower lever handle to process specific stuff
600  *
601  */
602 void BPatch::registerForkedThread(int parentPid, int childPid, process *proc)
603 {
604     assert(!info->threadsByPid.defines(childPid));
605
606     BPatch_thread *parent = info->threadsByPid[parentPid];
607
608     assert(parent);
609     info->threadsByPid[childPid] = new BPatch_thread(childPid, proc);
610
611     if (postForkCallback) {
612         postForkCallback(parent, info->threadsByPid[childPid]);
613     }
614 }
615
616
617 /*
618  * BPatch::registerExec
619  *
620  * Register a process that has just done an exec call.
621  *
622  * thread       thread that has just performed the exec
623  *
624  */
625 void BPatch::registerExec(BPatch_thread *thread)
626 {
627     // build a new BPatch_image for this one
628     thread->image = new BPatch_image(thread->proc);
629
630     if (execCallback) {
631         execCallback(thread);
632     }
633 }
634
635
636 /*
637  * BPatch::registerExit
638  *
639  * Register a process that has just done an exit call.
640  *
641  * thread       thread that has just performed the exec
642  * code         the exit status code
643  *
644  */
645 void BPatch::registerExit(BPatch_thread *thread, int code)
646 {
647     if (exitCallback) {
648         exitCallback(thread, code);
649     }
650 }
651
652
653 /*
654  * BPatch::registerThread
655  *
656  * Register a new BPatch_thread object with the BPatch library (this function
657  * is called only by the constructor for BPatch_thread).
658  *
659  * thread       A pointer to the thread to register.
660  */
661 void BPatch::registerThread(BPatch_thread *thread)
662 {
663     assert(!info->threadsByPid.defines(thread->getPid()) ||
664             info->threadsByPid[thread->getPid()] == NULL);
665     info->threadsByPid[thread->getPid()] = thread;
666 }
667
668
669 /*
670  * BPatch::unRegisterThread
671  *
672  * Remove the BPatch_thread associated with a given pid from the list of
673  * threads being managed by the library.
674  *
675  * pid          The pid of the thread to be removed.
676  */
677 void BPatch::unRegisterThread(int pid)
678 {
679     assert(info->threadsByPid.defines(pid));
680     info->threadsByPid.undef(pid);      
681 }
682
683
684 /*
685  * BPatch::createProcess
686  *
687  * Create a process and return a BPatch_thread representing it.
688  * Returns NULL upon failure.
689  *
690  * path         The pathname of the executable for the new process.
691  * argv         A list of the arguments for the new process, terminated by a
692  *              NULL.
693  * envp         A list of values that make up the environment for the new
694  *              process, terminated by a NULL.  If envp is NULL, the new
695  *              new process will inherit the environemnt of the parent.
696  * stdin_fd     file descriptor to use for stdin for the application
697  * stdout_fd    file descriptor to use for stdout for the application
698  * stderr_fd    file descriptor to use for stderr for the application
699
700  */
701 BPatch_thread *BPatch::createProcess(char *path, char *argv[], 
702         char *envp[], int stdin_fd, int stdout_fd, int stderr_fd)
703 {
704     clearError();
705
706     BPatch_thread *ret = 
707         new BPatch_thread(path, argv, envp, stdin_fd, stdout_fd, stderr_fd);
708
709     if (!ret->proc ||
710        (ret->proc->status() != stopped) ||
711        !ret->proc->isBootstrappedYet()) {
712         delete ret;
713         return NULL;
714     }
715
716     return ret;
717 }
718
719
720 /*
721  * BPatch::attachProcess
722  *
723  * Attach to a running process and return a BPatch_thread representing it.
724  * Returns NULL upon failure.
725  *
726  * path         The pathname of the executable for the process.
727  * pid          The id of the process to attach to.
728  */
729 BPatch_thread *BPatch::attachProcess(char *path, int pid)
730 {
731     clearError();
732
733     BPatch_thread *ret = new BPatch_thread(path, pid);
734
735     if (!ret->proc ||
736        (ret->proc->status() != stopped) ||
737        !ret->proc->isBootstrappedYet()) {
738         // It would be considerate to (attempt to) leave the process running
739         // at this point (*before* deleting the BPatch_thread handle for it!),
740         // even though it might be in bad shape after the attempted attach.
741         char msg[256];
742         sprintf(msg,"attachProcess failed: process %d may now be killed!",pid);
743         reportError(BPatchWarning, 26, msg);
744         delete ret;
745         return NULL;
746     }
747
748     return ret;
749 }
750
751
752 /*
753  * getThreadEvent
754  *
755  * Checks for changes in any child process, and optionally blocks until such a
756  * change has occurred.  Also updates the process object representing each
757  * process for which a change is detected.  The return value is true if a
758  * change was detected, otherwise it is false.
759  *
760  * block        Set this parameter to true to block waiting for a change,
761  *              set to false to poll and return immediately, whether or not a
762  *              change occurred.
763  */
764 bool BPatch::getThreadEvent(bool block)
765 {
766     bool        result = false;
767     int         pid, status;
768
769     // while ((pid = process::waitProcs(&status, block)) > 0) {
770     if ((pid = process::waitProcs(&status, block)) > 0) {
771         // There's been a change in a child process
772         result = true;
773         // Since we found something, we don't want to block anymore
774         block = false;
775
776         bool exists;
777         BPatch_thread *thread = getThreadByPid(pid, &exists);
778         if (thread == NULL) {
779             if (exists) {
780                 if (WIFSIGNALED(status) || WIFEXITED(status))
781                     unRegisterThread(pid);
782             } else {
783                 fprintf(stderr, "Warning - wait returned status of an unknown process (%d)\n", pid);
784             }
785         }
786         if (thread != NULL) {
787             if (WIFSTOPPED(status)) {
788                 thread->lastSignal = WSTOPSIG(status);
789                 thread->setUnreportedStop(true);
790             } else if (WIFSIGNALED(status)) {
791                 thread->lastSignal = WTERMSIG(status);
792                 thread->setUnreportedTermination(true);
793             } else if (WIFEXITED(status)) {
794 #ifndef i386_unknown_nt4_0
795                 thread->proc->exitCode_ = WEXITSTATUS(status);
796 #endif
797                 thread->exitCode = thread->proc->exitCode();
798                 thread->lastSignal = 0; /* XXX Make into some constant */
799                 thread->setUnreportedTermination(true);
800             }
801         }
802 #ifndef i386_unknown_nt4_0
803         handleSigChild(pid, status);
804 #ifdef notdef
805         if (thread->lastSignal == SIGSTOP) {
806             // need to continue process after initial sigstop
807             // thread->continueExecution();
808             printf("BPatch past handleSigChild for SIGSTOP\n");
809             if (thread->proc->wasCreatedViaFork()) {
810                 printf("marking forked process stopped\n");
811                 thread->proc->status_ = stopped;
812                 // thread->lastSignal = SIGSTOP;
813                 // thread->setUnreportedStop(true);
814                 // thread->proc->continueProc();
815             }
816         }
817 #endif
818 #endif
819     }
820
821     return result;
822 }
823
824
825 /*
826  * havePendingEvent
827  *
828  * Returns true if any thread has stopped or terminated and that fact hasn't
829  * been reported to the user of the library.  Otherwise, returns false.
830  */
831 bool BPatch::havePendingEvent()
832 {
833 #ifdef i386_unknown_nt4_0
834     // On NT, we need to poll for events as often as possible, so that we can
835     // handle traps.
836     if (getThreadEvent(false))
837         return true;
838 #endif
839
840     // For now, we'll do it by iterating over the threads and checking them,
841     // and we'll change it to something more efficient later on.
842     dictionary_hash_iter<int, BPatch_thread *> ti(info->threadsByPid);
843
844     int pid;
845     BPatch_thread *thread;
846
847     while (ti.next(pid, thread)) {
848         if (thread != NULL &&
849             (thread->pendingUnreportedStop() ||
850              thread->pendingUnreportedTermination())) {
851             return true;
852         }
853     }
854
855     return false;
856 }
857
858
859 /*
860  * pollForStatusChange
861  *
862  * Checks for unreported changes to the status of any child process, and
863  * returns true if any are detected.  Returns false otherwise.
864  *
865  * This function is declared as a friend of BPatch_thread so that it can use
866  * the BPatch_thread::getThreadEvent call to check for status changes.
867  */
868 bool BPatch::pollForStatusChange()
869 {
870     if (havePendingEvent())
871         return true;
872   
873     // No changes were previously detected, so check for new changes
874     return getThreadEvent(false);
875 }
876
877
878 /*
879  * waitForStatusChange
880  *
881  * Blocks waiting for a change to occur in the running status of a child
882  * process.  Returns true upon success, false upon failure.
883  *
884  * This function is declared as a friend of BPatch_thread so that it can use
885  * the BPatch_thread::getThreadEvent call to check for status changes.
886  */
887 bool BPatch::waitForStatusChange()
888 {
889     if (havePendingEvent())
890         return true;
891
892     // No changes were previously detected, so wait for a new change
893     return getThreadEvent(true);
894 }
895
896 /*
897  * createEnum
898  *
899  * This function is a wrapper for the BPatch_type constructors for API/User
900  * created types.
901  *
902  * It returns a pointer to a BPatch_type that was added to the APITypes
903  * collection.
904  */
905 BPatch_type * BPatch::createEnum( const char * name, 
906                                   BPatch_Vector<char *> elementNames,
907                                   BPatch_Vector<int> elementIds)
908 {
909
910     if (elementNames.size() != elementIds.size()) {
911       return NULL;
912     }
913
914     BPatch_type * newType = new BPatch_type( name, BPatch_enumerated );
915     if (!newType) return NULL;
916     
917     APITypes->addType(newType);
918
919     // ADD components to type
920     for (int i=0; i < elementNames.size(); i++) {
921         newType->addField(elementNames[i], BPatch_scalar, elementIds[i]);
922     }
923
924     return(newType);
925 }
926
927
928 /*
929  * createEnum
930  *
931  * This function is a wrapper for the BPatch_type constructors for API/User
932  * created types.  The user has left element id specification to us
933  *
934  * It returns a pointer to a BPatch_type that was added to the APITypes
935  * collection.
936  */
937 BPatch_type * BPatch::createEnum( const char * name, 
938                                   BPatch_Vector<char *> elementNames)
939 {
940     BPatch_type * newType = new BPatch_type( name, BPatch_enumerated );
941
942     if (!newType) return NULL;
943     
944     APITypes->addType(newType);
945
946     // ADD components to type
947     for (int i=0; i < elementNames.size(); i++) {
948         newType->addField(elementNames[i], BPatch_scalar, i);
949     }
950
951     return(newType);
952 }
953
954 /*
955  * createStructs
956  *
957  * This function is a wrapper for the BPatch_type constructors for API/User
958  * created types.
959  *
960  * It returns a pointer to a BPatch_type that was added to the APITypes
961  * collection.
962  */
963
964 BPatch_type * BPatch::createStruct( const char * name,
965                                     BPatch_Vector<char *> fieldNames,
966                                     BPatch_Vector<BPatch_type *> fieldTypes)
967 {
968     int i;
969     int offset, size;
970
971     offset = size = 0;
972     if (fieldNames.size() != fieldTypes.size()) {
973       return NULL;
974     }
975
976     //Compute the size of the struct
977     for (i=0; i < fieldNames.size(); i++) {
978         BPatch_type *type = fieldTypes[i];
979         size = type->getSize();
980         size += size;
981     }
982   
983     BPatch_type *newType = new BPatch_type(name, BPatch_structure, size);
984     if (!newType) return NULL;
985     
986     APITypes->addType(newType);
987
988     // ADD components to type
989     size = 0;
990     for (i=0; i < fieldNames.size(); i++) {
991         BPatch_type *type = fieldTypes[i];
992         size = type->getSize();
993         newType->addField(fieldNames[i], type->getDataClass(), type, offset, size);
994   
995         // Calculate next offset (in bits) into the struct
996         offset += (size * 8);
997     }
998
999     return(newType);
1000 }
1001
1002 /*
1003  * createUnions
1004  *
1005  * This function is a wrapper for the BPatch_type constructors for API/User
1006  * created types.
1007  *
1008  * It returns a pointer to a BPatch_type that was added to the APITypes
1009  * collection.
1010  */
1011
1012 BPatch_type * BPatch::createUnion( const char * name, 
1013                                    BPatch_Vector<char *> fieldNames,
1014                                    BPatch_Vector<BPatch_type *> fieldTypes)
1015 {
1016     int i;
1017     int offset, size, newsize;
1018     offset = size = newsize = 0;
1019
1020     if (fieldNames.size() != fieldTypes.size()) {
1021         return NULL;
1022     }
1023
1024     // Compute the size of the union
1025     for (i=0; i < fieldTypes.size(); i++) {
1026         BPatch_type *type = fieldTypes[i];
1027         newsize = type->getSize();
1028         if(size < newsize) size = newsize;
1029     }
1030   
1031     BPatch_type * newType = new BPatch_type(name, BPatch_union, size);
1032     if (!newType) return NULL;
1033
1034     APITypes->addType(newType);
1035
1036     // ADD components to type
1037     for (i=0; i < fieldNames.size(); i++) {
1038         BPatch_type *type = fieldTypes[i];
1039         size = type->getSize();
1040         newType->addField(fieldNames[i], type->getDataClass(), type, offset, size);
1041     }  
1042     return(newType);
1043 }
1044
1045
1046 /*
1047  * createArray for Arrays and SymTypeRanges
1048  *
1049  * This function is a wrapper for the BPatch_type constructors for API/User
1050  * created types.
1051  *
1052  * It returns a pointer to a BPatch_type that was added to the APITypes
1053  * collection.
1054  */
1055 BPatch_type * BPatch::createArray( const char * name, BPatch_type * ptr,
1056                                    unsigned int low, unsigned int hi)
1057 {
1058
1059     BPatch_type * newType;
1060
1061     if (!ptr) {
1062         return NULL;
1063     } else {
1064         newType = new BPatch_type(name, BPatch_array , ptr, low, hi);
1065         if (!newType) return NULL;
1066     }
1067
1068     APITypes->addType(newType);
1069
1070     return newType;
1071 }
1072
1073 /*
1074  * createPointer for BPatch_pointers
1075  *
1076  * This function is a wrapper for the BPatch_type constructors for API/User
1077  * created types.
1078  *
1079  * It returns a pointer to a BPatch_type that was added to the APITypes
1080  * collection.
1081  */
1082 BPatch_type * BPatch::createPointer(const char * name, BPatch_type * ptr,
1083                                     int size)
1084 {
1085
1086     BPatch_type * newType = new BPatch_type(name, ptr, size);
1087     if(!newType) return NULL;
1088
1089     APITypes->addType(newType);
1090   
1091     return newType;
1092 }
1093
1094 /*
1095  * createScalar for scalars with a size and no range
1096  *
1097  * This function is a wrapper for the BPatch_type constructors for API/User
1098  * created types.
1099  *
1100  * It returns a pointer to a BPatch_type that was added to the APITypes
1101  * collection.
1102  */
1103
1104 BPatch_type * BPatch::createScalar( const char * name, int size)
1105 {
1106     BPatch_type * newType = new BPatch_type(name, BPatch_scalar, size);
1107     if (!newType) return NULL;
1108
1109     APITypes->addType(newType);
1110  
1111     return newType;
1112 }
1113
1114 /*
1115  * createType for typedefs
1116  *
1117  * This function is a wrapper for the BPatch_type constructors for API/User
1118  * created types.
1119  *
1120  * It returns a pointer to a BPatch_type that was added to the APITypes
1121  * collection.
1122  */
1123 BPatch_type * BPatch::createTypedef( const char * name, BPatch_type * ptr)
1124 {
1125     BPatch_type * newType = new BPatch_type(name, ptr);
1126
1127     if (!newType) return NULL;
1128
1129     APITypes->addType(newType);
1130   
1131     return newType;
1132 }