all remaining *.c *.h files from hp merge.
[deliverable/binutils-gdb.git] / gdb / infttrace.c
1 /* Low level Unix child interface to ttrace, for GDB when running under HP-UX.
2 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "defs.h"
22 #include "frame.h"
23 #include "inferior.h"
24 #include "target.h"
25 #include "gdb_string.h"
26 #include "wait.h"
27 #include "command.h"
28
29 /* Some hackery to work around a use of the #define name NO_FLAGS
30 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
31 */
32 #ifdef NO_FLAGS
33 #define INFTTRACE_TEMP_HACK NO_FLAGS
34 #undef NO_FLAGS
35 #endif
36
37 #ifdef USG
38 #include <sys/types.h>
39 #endif
40
41 #include <sys/param.h>
42 #include <sys/dir.h>
43 #include <signal.h>
44 #include <sys/ioctl.h>
45
46 #include <sys/ttrace.h>
47 #include <sys/unistd.h>
48 #include <sys/mman.h>
49
50 #ifndef NO_PTRACE_H
51 #ifdef PTRACE_IN_WRONG_PLACE
52 #include <ptrace.h>
53 #else
54 #include <sys/ptrace.h>
55 #endif
56 #endif /* NO_PTRACE_H */
57
58 /* Second half of the hackery above. Non-ANSI C, so
59 * we can't use "#error", alas.
60 */
61 #ifdef NO_FLAGS
62 #if (NO_FLAGS != INFTTRACE_TEMP_HACK )
63 /* #error "Hackery to remove warning didn't work right" */
64 #else
65 /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
66 #endif
67 #else
68 /* #error "Didn't get expected re-definition of NO_FLAGS" */
69 #define NO_FLAGS INFTTRACE_TEMP_HACK
70 #endif
71
72 #if !defined (PT_SETTRC)
73 #define PT_SETTRC 0 /* Make process traceable by parent */
74 #endif
75 #if !defined (PT_READ_I)
76 #define PT_READ_I 1 /* Read word from text space */
77 #endif
78 #if !defined (PT_READ_D)
79 #define PT_READ_D 2 /* Read word from data space */
80 #endif
81 #if !defined (PT_READ_U)
82 #define PT_READ_U 3 /* Read word from kernel user struct */
83 #endif
84 #if !defined (PT_WRITE_I)
85 #define PT_WRITE_I 4 /* Write word to text space */
86 #endif
87 #if !defined (PT_WRITE_D)
88 #define PT_WRITE_D 5 /* Write word to data space */
89 #endif
90 #if !defined (PT_WRITE_U)
91 #define PT_WRITE_U 6 /* Write word to kernel user struct */
92 #endif
93 #if !defined (PT_CONTINUE)
94 #define PT_CONTINUE 7 /* Continue after signal */
95 #endif
96 #if !defined (PT_STEP)
97 #define PT_STEP 9 /* Set flag for single stepping */
98 #endif
99 #if !defined (PT_KILL)
100 #define PT_KILL 8 /* Send child a SIGKILL signal */
101 #endif
102
103 #ifndef PT_ATTACH
104 #define PT_ATTACH PTRACE_ATTACH
105 #endif
106 #ifndef PT_DETACH
107 #define PT_DETACH PTRACE_DETACH
108 #endif
109
110 #include "gdbcore.h"
111 #ifndef NO_SYS_FILE
112 #include <sys/file.h>
113 #endif
114
115 /* This semaphore is used to coordinate the child and parent processes
116 after a fork(), and before an exec() by the child. See parent_attach_all
117 for details.
118 */
119 typedef struct {
120 int parent_channel[2]; /* Parent "talks" to [1], child "listens" to [0] */
121 int child_channel[2]; /* Child "talks" to [1], parent "listens" to [0] */
122 } startup_semaphore_t;
123
124 #define SEM_TALK (1)
125 #define SEM_LISTEN (0)
126
127 static startup_semaphore_t startup_semaphore;
128
129 /* See can_touch_threads_of_process for details. */
130 static int vforking_child_pid = 0;
131 static int vfork_in_flight = 0;
132
133 /* To support PREPARE_TO_PROCEED (hppa_prepare_to_proceed).
134 */
135 static pid_t old_gdb_pid = 0;
136 static pid_t reported_pid = 0;
137 static int reported_bpt = 0;
138
139 /* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
140 */
141 #define TT_OK( _status, _errno ) \
142 (((_status) == 1) && ((_errno) == 0))
143
144 #define TTRACE_ARG_TYPE uint64_t
145
146 /* When supplied as the "addr" operand, ttrace interprets this
147 to mean, "from the current address".
148 */
149 #define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)
150
151 /* When supplied as the "addr", "data" or "addr2" operand for most
152 requests, ttrace interprets this to mean, "pay no heed to this
153 argument".
154 */
155 #define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)
156
157 /* This is capable of holding the value of a 32-bit register. The
158 value is always left-aligned in the buffer; i.e., [0] contains
159 the most-significant byte of the register's value, and [sizeof(reg)]
160 contains the least-significant value.
161
162 ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
163 that registers are 32-bits on HP-UX. The latter assumption changes
164 with PA2.0.
165 */
166 typedef int register_value_t;
167
168 /********************************************************************
169
170 How this works:
171
172 1. Thread numbers
173
174 The rest of GDB sees threads as being things with different
175 "pid" (process id) values. See "thread.c" for details. The
176 separate threads will be seen and reacted to if infttrace passes
177 back different pid values (for _events_). See wait_for_inferior
178 in inftarg.c.
179
180 So infttrace is going to use thread ids externally, pretending
181 they are process ids, and keep track internally so that it can
182 use the real process id (and thread id) when calling ttrace.
183
184 The data structure that supports this is a linked list of the
185 current threads. Since at some date infttrace will have to
186 deal with multiple processes, each list element records its
187 corresponding pid, rather than having a single global.
188
189 Note that the list is only approximately current; that's ok, as
190 it's up to date when we need it (we hope!). Also, it can contain
191 dead threads, as there's no harm if it does.
192
193 The approach taken here is to bury the translation from external
194 to internal inside "call_ttrace" and a few other places.
195
196 There are some wrinkles:
197
198 o When GDB forks itself to create the debug target process,
199 there's only a pid of 0 around in the child, so the
200 TT_PROC_SETTRC operation uses a more direct call to ttrace;
201 Similiarly, the initial setting of the event mask happens
202 early as well, and so is also special-cased, and an attach
203 uses a real pid;
204
205 o We define an unthreaded application as having a "pseudo"
206 thread;
207
208 o To keep from confusing the rest of GDB, we don't switch
209 the PID for the pseudo thread to a TID. A table will help:
210
211 Rest of GDB sees these PIDs: pid tid1 tid2 tid3 ...
212
213 Our thread list stores: pid pid pid pid ...
214 tid0 tid1 tid2 tid3
215
216 Ttrace sees these TIDS: tid0 tid1 tid2 tid3 ...
217
218 Both pid and tid0 will map to tid0, as there are infttrace.c-internal
219 calls to ttrace using tid0.
220
221 2. Step and Continue
222
223 Since we're implementing the "stop the world" model, sub-model
224 "other threads run during step", we have some stuff to do:
225
226 o User steps require continuing all threads other than the
227 one the user is stepping;
228
229 o Internal debugger steps (such as over a breakpoint or watchpoint,
230 but not out of a library load thunk) require stepping only
231 the selected thread; this means that we have to report the
232 step finish on that thread, which can lead to complications;
233
234 o When a thread is created, it is created running, rather
235 than stopped--so we have to stop it.
236
237 The OS doesn't guarantee the stopped thread list will be stable,
238 no does it guarantee where on the stopped thread list a thread
239 that is single-stepped will wind up: it's possible that it will
240 be off the list for a while, it's possible the step will complete
241 and it will be re-posted to the end...
242
243 This means we have to scan the stopped thread list, build up
244 a work-list, and then run down the work list; we can't do the
245 step/continue during the scan.
246
247 3. Buffering events
248
249 Then there's the issue of waiting for an event. We do this by
250 noticing how many events are reported at the end of each wait.
251 From then on, we "fake" all resumes and steps, returning instantly,
252 and don't do another wait. Once all pending events are reported,
253 we can really resume again.
254
255 To keep this hidden, all the routines which know about tids and
256 pids or real events and simulated ones are static (file-local).
257
258 This code can make lots of calls to ttrace, in particular it
259 can spin down the list of thread states more than once. If this
260 becomes a performance hit, the spin could be done once and the
261 various "tsp" blocks saved, keeping all later spins in this
262 process.
263
264 The O/S doesn't promise to keep the list straight, and so we must
265 re-scan a lot. By observation, it looks like a single-step/wait
266 puts the stepped thread at the end of the list but doesn't change
267 it otherwise.
268
269 ****************************************************************
270 */
271
272 /* Uncomment these to turn on various debugging output */
273 /* #define THREAD_DEBUG */
274 /* #define WAIT_BUFFER_DEBUG */
275 /* #define PARANOIA */
276
277
278 #define INFTTRACE_ALL_THREADS (-1)
279 #define INFTTRACE_STEP (1)
280 #define INFTTRACE_CONTINUE (0)
281
282 /* FIX: this is used in inftarg.c/child_wait, in a hack.
283 */
284 extern int not_same_real_pid;
285
286 /* This is used to count buffered events.
287 */
288 static unsigned int more_events_left = 0;
289
290 /* Process state.
291 */
292 typedef enum process_state_enum {
293 STOPPED,
294 FAKE_STEPPING,
295 FAKE_CONTINUE, /* For later use */
296 RUNNING,
297 FORKING,
298 VFORKING
299 } process_state_t;
300
301 static process_state_t process_state = STOPPED;
302
303 /* User-specified stepping modality.
304 */
305 typedef enum stepping_mode_enum {
306 DO_DEFAULT, /* ...which is a continue! */
307 DO_STEP,
308 DO_CONTINUE
309 } stepping_mode_t;
310
311 /* Action to take on an attach, depends on
312 * what kind (user command, fork, vfork).
313 *
314 * At the moment, this is either:
315 *
316 * o continue with a SIGTRAP signal, or
317 *
318 * o leave stopped.
319 */
320 typedef enum attach_continue_enum {
321 DO_ATTACH_CONTINUE,
322 DONT_ATTACH_CONTINUE
323 } attach_continue_t;
324
325 /* This flag is true if we are doing a step-over-bpt
326 * with buffered events. We will have to be sure to
327 * report the right thread, as otherwise the spaghetti
328 * code in "infrun.c/wait_for_inferior" will get
329 * confused.
330 */
331 static int doing_fake_step = 0;
332 static lwpid_t fake_step_tid = 0;
333
334 \f
335 /****************************************************
336 * Thread information structure routines and types. *
337 ****************************************************
338 */
339 typedef
340 struct thread_info_struct
341 {
342 int am_pseudo; /* This is a pseudo-thread for the process. */
343 int pid; /* Process ID */
344 lwpid_t tid; /* Thread ID */
345 int handled; /* 1 if a buffered event was handled. */
346 int seen; /* 1 if this thread was seen on a traverse. */
347 int terminated; /* 1 if thread has terminated. */
348 int have_signal; /* 1 if signal to be sent */
349 enum target_signal signal_value; /* Signal to send */
350 int have_start; /* 1 if alternate starting address */
351 stepping_mode_t stepping_mode; /* Whether to step or continue */
352 CORE_ADDR start; /* Where to start */
353 int have_state; /* 1 if the event state has been set */
354 ttstate_t last_stop_state;/* The most recently-waited event for this thread. */
355 struct thread_info_struct
356 *next; /* All threads are linked via this field. */
357 struct thread_info_struct
358 *next_pseudo; /* All pseudo-threads are linked via this field. */
359 } thread_info;
360
361 typedef
362 struct thread_info_header_struct
363 {
364 int count;
365 thread_info *head;
366 thread_info *head_pseudo;
367
368 } thread_info_header;
369
370 static thread_info_header thread_head = { 0, NULL, NULL };
371 static thread_info_header deleted_threads = { 0, NULL, NULL };
372
373 static saved_real_pid = 0;
374
375 \f
376 /*************************************************
377 * Debugging support functions *
378 *************************************************
379 */
380 CORE_ADDR
381 get_raw_pc( ttid )
382 lwpid_t ttid;
383 {
384 unsigned long pc_val;
385 int offset;
386 int res;
387
388 offset = register_addr( PC_REGNUM, U_REGS_OFFSET );
389 res = read_from_register_save_state(
390 ttid,
391 (TTRACE_ARG_TYPE) offset,
392 (char *) &pc_val,
393 sizeof( pc_val ));
394 if( res <= 0 ) {
395 return (CORE_ADDR) pc_val;
396 }
397 else {
398 return (CORE_ADDR) 0;
399 }
400 }
401
402 static char *
403 get_printable_name_of_stepping_mode( mode )
404 stepping_mode_t mode;
405 {
406 switch( mode ) {
407 case DO_DEFAULT: return "DO_DEFAULT";
408 case DO_STEP: return "DO_STEP";
409 case DO_CONTINUE: return "DO_CONTINUE";
410 default: return "?unknown mode?";
411 }
412 }
413
414 /* This function returns a pointer to a string describing the
415 * ttrace event being reported.
416 */
417 char *
418 get_printable_name_of_ttrace_event (event)
419 ttevents_t event;
420 {
421 /* This enumeration is "gappy", so don't use a table. */
422 switch (event) {
423
424 case TTEVT_NONE:
425 return "TTEVT_NONE";
426 case TTEVT_SIGNAL:
427 return "TTEVT_SIGNAL";
428 case TTEVT_FORK:
429 return "TTEVT_FORK";
430 case TTEVT_EXEC:
431 return "TTEVT_EXEC";
432 case TTEVT_EXIT:
433 return "TTEVT_EXIT";
434 case TTEVT_VFORK:
435 return "TTEVT_VFORK";
436 case TTEVT_SYSCALL_RETURN:
437 return "TTEVT_SYSCALL_RETURN";
438 case TTEVT_LWP_CREATE:
439 return "TTEVT_LWP_CREATE";
440 case TTEVT_LWP_TERMINATE:
441 return "TTEVT_LWP_TERMINATE";
442 case TTEVT_LWP_EXIT:
443 return "TTEVT_LWP_EXIT";
444 case TTEVT_LWP_ABORT_SYSCALL:
445 return "TTEVT_LWP_ABORT_SYSCALL";
446 case TTEVT_SYSCALL_ENTRY:
447 return "TTEVT_SYSCALL_ENTRY";
448 case TTEVT_SYSCALL_RESTART:
449 return "TTEVT_SYSCALL_RESTART";
450 default :
451 return "?new event?";
452 }
453 }
454
455 \f
456 /* This function translates the ttrace request enumeration into
457 * a character string that is its printable (aka "human readable")
458 * name.
459 */
460 char *
461 get_printable_name_of_ttrace_request (request)
462 ttreq_t request;
463 {
464 if (!IS_TTRACE_REQ (request))
465 return "?bad req?";
466
467 /* This enumeration is "gappy", so don't use a table. */
468 switch (request) {
469 case TT_PROC_SETTRC :
470 return "TT_PROC_SETTRC";
471 case TT_PROC_ATTACH :
472 return "TT_PROC_ATTACH";
473 case TT_PROC_DETACH :
474 return "TT_PROC_DETACH";
475 case TT_PROC_RDTEXT :
476 return "TT_PROC_RDTEXT";
477 case TT_PROC_WRTEXT :
478 return "TT_PROC_WRTEXT";
479 case TT_PROC_RDDATA :
480 return "TT_PROC_RDDATA";
481 case TT_PROC_WRDATA :
482 return "TT_PROC_WRDATA";
483 case TT_PROC_STOP :
484 return "TT_PROC_STOP";
485 case TT_PROC_CONTINUE :
486 return "TT_PROC_CONTINUE";
487 case TT_PROC_GET_PATHNAME :
488 return "TT_PROC_GET_PATHNAME";
489 case TT_PROC_GET_EVENT_MASK :
490 return "TT_PROC_GET_EVENT_MASK";
491 case TT_PROC_SET_EVENT_MASK :
492 return "TT_PROC_SET_EVENT_MASK";
493 case TT_PROC_GET_FIRST_LWP_STATE :
494 return "TT_PROC_GET_FIRST_LWP_STATE";
495 case TT_PROC_GET_NEXT_LWP_STATE :
496 return "TT_PROC_GET_NEXT_LWP_STATE";
497 case TT_PROC_EXIT :
498 return "TT_PROC_EXIT";
499 case TT_PROC_GET_MPROTECT :
500 return "TT_PROC_GET_MPROTECT";
501 case TT_PROC_SET_MPROTECT :
502 return "TT_PROC_SET_MPROTECT";
503 case TT_PROC_SET_SCBM :
504 return "TT_PROC_SET_SCBM";
505 case TT_LWP_STOP :
506 return "TT_LWP_STOP";
507 case TT_LWP_CONTINUE :
508 return "TT_LWP_CONTINUE";
509 case TT_LWP_SINGLE :
510 return "TT_LWP_SINGLE";
511 case TT_LWP_RUREGS :
512 return "TT_LWP_RUREGS";
513 case TT_LWP_WUREGS :
514 return "TT_LWP_WUREGS";
515 case TT_LWP_GET_EVENT_MASK :
516 return "TT_LWP_GET_EVENT_MASK";
517 case TT_LWP_SET_EVENT_MASK :
518 return "TT_LWP_SET_EVENT_MASK";
519 case TT_LWP_GET_STATE :
520 return "TT_LWP_GET_STATE";
521 default :
522 return "?new req?";
523 }
524 }
525
526 \f
527 /* This function translates the process state enumeration into
528 * a character string that is its printable (aka "human readable")
529 * name.
530 */
531 static char *
532 get_printable_name_of_process_state (process_state)
533 process_state_t process_state;
534 {
535 switch (process_state) {
536 case STOPPED:
537 return "STOPPED";
538 case FAKE_STEPPING:
539 return "FAKE_STEPPING";
540 case RUNNING:
541 return "RUNNING";
542 case FORKING:
543 return "FORKING";
544 case VFORKING:
545 return "VFORKING";
546 default:
547 return "?some unknown state?";
548 }
549 }
550
551 /* Set a ttrace thread state to a safe, initial state.
552 */
553 static void
554 clear_ttstate_t (tts)
555 ttstate_t * tts;
556 {
557 tts->tts_pid = 0;
558 tts->tts_lwpid = 0;
559 tts->tts_user_tid = 0;
560 tts->tts_event = TTEVT_NONE;
561 }
562
563 /* Copy ttrace thread state TTS_FROM into TTS_TO.
564 */
565 static void
566 copy_ttstate_t (tts_to, tts_from)
567 ttstate_t * tts_to;
568 ttstate_t * tts_from;
569 {
570 memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
571 }
572
573 /* Are there any live threads we know about?
574 */
575 static int
576 any_thread_records()
577 {
578 return( thread_head.count > 0 );
579 }
580
581 /* Create, fill in and link in a thread descriptor.
582 */
583 static thread_info *
584 create_thread_info (pid, tid)
585 int pid;
586 lwpid_t tid;
587 {
588 thread_info * new_p;
589 thread_info * p;
590 int thread_count_of_pid;
591
592 new_p = malloc( sizeof( thread_info ));
593 new_p->pid = pid;
594 new_p->tid = tid;
595 new_p->have_signal = 0;
596 new_p->have_start = 0;
597 new_p->have_state = 0;
598 clear_ttstate_t( &new_p->last_stop_state );
599 new_p->am_pseudo = 0;
600 new_p->handled = 0;
601 new_p->seen = 0;
602 new_p->terminated = 0;
603 new_p->next = NULL;
604 new_p->next_pseudo = NULL;
605 new_p->stepping_mode = DO_DEFAULT;
606
607 if( 0 == thread_head.count ) {
608 #ifdef THREAD_DEBUG
609 if( debug_on )
610 printf( "First thread, pid %d tid %d!\n", pid, tid );
611 #endif
612 saved_real_pid = inferior_pid;
613 }
614 else {
615 #ifdef THREAD_DEBUG
616 if( debug_on )
617 printf( "Subsequent thread, pid %d tid %d\n", pid, tid );
618 #endif
619 }
620
621 /* Another day, another thread...
622 */
623 thread_head.count++;
624
625 /* The new thread always goes at the head of the list.
626 */
627 new_p->next = thread_head.head;
628 thread_head.head = new_p;
629
630 /* Is this the "pseudo" thread of a process? It is if there's
631 * no other thread for this process on the list. (Note that this
632 * accomodates multiple processes, such as we see even for simple
633 * cases like forking "non-threaded" programs.)
634 */
635 p = thread_head.head;
636 thread_count_of_pid = 0;
637 while (p)
638 {
639 if (p->pid == new_p->pid)
640 thread_count_of_pid++;
641 p = p->next;
642 }
643
644 /* Did we see any other threads for this pid? (Recall that we just
645 * added this thread to the list...)
646 */
647 if (thread_count_of_pid == 1)
648 {
649 new_p->am_pseudo = 1;
650 new_p->next_pseudo = thread_head.head_pseudo;
651 thread_head.head_pseudo = new_p;
652 }
653
654 return new_p;
655 }
656
657 /* Get rid of our thread info.
658 */
659 static void
660 clear_thread_info ()
661 {
662 thread_info *p;
663 thread_info *q;
664
665 #ifdef THREAD_DEBUG
666 if( debug_on )
667 printf( "Clearing all thread info\n" );
668 #endif
669
670 p = thread_head.head;
671 while( p ) {
672 q = p;
673 p = p->next;
674 free( q );
675 }
676
677 thread_head.head = NULL;
678 thread_head.head_pseudo = NULL;
679 thread_head.count = 0;
680
681 p = deleted_threads.head;
682 while( p ) {
683 q = p;
684 p = p->next;
685 free( q );
686 }
687
688 deleted_threads.head = NULL;
689 deleted_threads.head_pseudo = NULL;
690 deleted_threads.count = 0;
691
692 /* No threads, so can't have pending events.
693 */
694 more_events_left = 0;
695 }
696
697 /* Given a tid, find the thread block for it.
698 */
699 static thread_info *
700 find_thread_info (tid)
701 lwpid_t tid;
702 {
703 thread_info *p;
704
705 for( p = thread_head.head; p; p = p->next ) {
706 if( p->tid == tid ) {
707 return p;
708 }
709 }
710
711 for( p = deleted_threads.head; p; p = p->next ) {
712 if( p->tid == tid ) {
713 return p;
714 }
715 }
716
717 return NULL;
718 }
719
720 /* For any but the pseudo thread, this maps to the
721 * thread ID. For the pseudo thread, if you pass either
722 * the thread id or the PID, you get the pseudo thread ID.
723 *
724 * We have to be prepared for core gdb to ask about
725 * deleted threads. We do the map, but we don't like it.
726 */
727 static lwpid_t
728 map_from_gdb_tid( gdb_tid )
729 lwpid_t gdb_tid;
730 {
731 thread_info *p;
732
733 /* First assume gdb_tid really is a tid, and try to find a
734 * matching entry on the threads list.
735 */
736 for( p = thread_head.head; p; p = p->next ) {
737 if( p->tid == gdb_tid )
738 return gdb_tid;
739 }
740
741 /* It doesn't appear to be a tid; perhaps it's really a pid?
742 * Try to find a "pseudo" thread entry on the threads list.
743 */
744 for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
745 {
746 if (p->pid == gdb_tid)
747 return p->tid;
748 }
749
750 /* Perhaps it's the tid of a deleted thread we may still
751 * have some knowledge of?
752 */
753 for( p = deleted_threads.head; p; p = p-> next ) {
754 if( p->tid == gdb_tid )
755 return gdb_tid;
756 }
757
758 /* Or perhaps it's the pid of a deleted process we may still
759 * have knowledge of?
760 */
761 for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
762 {
763 if (p->pid == gdb_tid)
764 return p->tid;
765 }
766
767 return 0; /* Error? */
768 }
769
770 /* Map the other way: from a real tid to the
771 * "pid" known by core gdb. This tid may be
772 * for a thread that just got deleted, so we
773 * also need to consider deleted threads.
774 */
775 static lwpid_t
776 map_to_gdb_tid( real_tid )
777 lwpid_t real_tid;
778 {
779 thread_info *p;
780
781 for( p = thread_head.head; p; p = p->next ) {
782 if( p->tid == real_tid ) {
783 if( p->am_pseudo )
784 return p->pid;
785 else
786 return real_tid;
787 }
788 }
789
790 for( p = deleted_threads.head; p; p = p-> next ) {
791 if( p->tid == real_tid )
792 if( p->am_pseudo )
793 return p->pid; /* Error? */
794 else
795 return real_tid;
796 }
797
798 return 0; /* Error? Never heard of this thread! */
799 }
800
801 /* Do any threads have saved signals?
802 */
803 static int
804 saved_signals_exist ()
805 {
806 thread_info *p;
807
808 for( p = thread_head.head; p; p = p->next ) {
809 if( p->have_signal ) {
810 return 1;
811 }
812 }
813
814 return 0;
815 }
816
817 /* Is this the tid for the zero-th thread?
818 */
819 static int
820 is_pseudo_thread (tid)
821 lwpid_t tid;
822 {
823 thread_info *p = find_thread_info( tid );
824 if( NULL == p || p->terminated )
825 return 0;
826 else
827 return p->am_pseudo;
828 }
829
830 /* Is this thread terminated?
831 */
832 static int
833 is_terminated (tid)
834 lwpid_t tid;
835 {
836 thread_info *p = find_thread_info( tid );
837
838 if( NULL != p )
839 return p->terminated;
840
841 return 0;
842 }
843
844 /* Is this pid a real PID or a TID?
845 */
846 static int
847 is_process_id (pid)
848 int pid;
849 {
850 lwpid_t tid;
851 thread_info * tinfo;
852 pid_t this_pid;
853 int this_pid_count;
854
855 /* What does PID really represent?
856 */
857 tid = map_from_gdb_tid (pid);
858 if (tid <= 0)
859 return 0; /* Actually, is probably an error... */
860
861 tinfo = find_thread_info (tid);
862
863 /* Does it appear to be a true thread?
864 */
865 if (! tinfo->am_pseudo)
866 return 0;
867
868 /* Else, it looks like it may be a process. See if there's any other
869 * threads with the same process ID, though. If there are, then TID
870 * just happens to be the first thread of several for this process.
871 */
872 this_pid = tinfo->pid;
873 this_pid_count = 0;
874 for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
875 {
876 if (tinfo->pid == this_pid)
877 this_pid_count++;
878 }
879
880 return (this_pid_count == 1);
881 }
882
883
884 /* Add a thread to our info. Prevent duplicate entries.
885 */
886 static thread_info *
887 add_tthread (pid, tid)
888 int pid;
889 lwpid_t tid;
890 {
891 thread_info *p;
892
893 p = find_thread_info( tid );
894 if( NULL == p )
895 p = create_thread_info( pid, tid );
896
897 return p;
898 }
899
900 /* Notice that a thread was deleted.
901 */
902 static void
903 del_tthread (tid)
904 lwpid_t tid;
905 {
906 thread_info *p;
907 thread_info *chase;
908
909 if( thread_head.count <= 0 ) {
910 error( "Internal error in thread database." );
911 return;
912 }
913
914 chase = NULL;
915 for( p = thread_head.head; p; p = p->next ) {
916 if( p->tid == tid ) {
917
918 #ifdef THREAD_DEBUG
919 if( debug_on )
920 printf( "Delete here: %d \n", tid );
921 #endif
922
923 if( p->am_pseudo ) {
924 /*
925 * Deleting a main thread is ok if we're doing
926 * a parent-follow on a child; this is odd but
927 * not wrong. It apparently _doesn't_ happen
928 * on the child-follow, as we don't just delete
929 * the pseudo while keeping the rest of the
930 * threads around--instead, we clear out the whole
931 * thread list at once.
932 */
933 thread_info *q;
934 thread_info *q_chase;
935
936 q_chase = NULL;
937 for( q = thread_head.head_pseudo; q; q = q -> next ) {
938 if( q == p ) {
939 /* Remove from pseudo list.
940 */
941 if( q_chase == NULL )
942 thread_head.head_pseudo = p->next_pseudo;
943 else
944 q_chase-> next = p->next_pseudo;
945 }
946 else
947 q_chase = q;
948 }
949 }
950
951 /* Remove from live list.
952 */
953 thread_head.count--;
954
955 if( NULL == chase )
956 thread_head.head = p->next;
957 else
958 chase->next = p->next;
959
960 /* Add to deleted thread list.
961 */
962 p->next = deleted_threads.head;
963 deleted_threads.head = p;
964 deleted_threads.count++;
965 if( p->am_pseudo ) {
966 p->next_pseudo = deleted_threads.head_pseudo;
967 deleted_threads.head_pseudo = p;
968 }
969 p->terminated = 1;
970
971 return;
972 }
973
974 else
975 chase = p;
976 }
977 }
978
979 /* Get the pid for this tid. (Has to be a real TID!).
980 */
981 static int
982 get_pid_for (tid)
983 lwpid_t tid;
984 {
985 thread_info *p;
986
987 for( p = thread_head.head; p; p = p->next ) {
988 if( p->tid == tid ) {
989 return p->pid;
990 }
991 }
992
993 for( p = deleted_threads.head; p; p = p->next ) {
994 if( p->tid == tid ) {
995 return p->pid;
996 }
997 }
998
999 return 0;
1000 }
1001
1002 /* Note that this thread's current event has been handled.
1003 */
1004 static void
1005 set_handled( pid, tid )
1006 int pid;
1007 lwpid_t tid;
1008 {
1009 thread_info *p;
1010
1011 p = find_thread_info( tid );
1012 if( NULL == p )
1013 p = add_tthread( pid, tid );
1014
1015 p->handled = 1;
1016 }
1017
1018 /* Was this thread's current event handled?
1019 */
1020 static int
1021 was_handled( tid )
1022 lwpid_t tid;
1023 {
1024 thread_info *p;
1025
1026 p = find_thread_info( tid );
1027 if( NULL != p )
1028 return p->handled;
1029
1030 return 0; /* New threads have not been handled */
1031 }
1032
1033 /* Set this thread to unhandled.
1034 */
1035 static void
1036 clear_handled( tid )
1037 lwpid_t tid;
1038 {
1039 thread_info * p;
1040
1041 #ifdef WAIT_BUFFER_DEBUG
1042 if( debug_on )
1043 printf( "clear_handled %d\n", (int) tid );
1044 #endif
1045
1046 p = find_thread_info (tid);
1047 if (p == NULL)
1048 error ("Internal error: No thread state to clear?");
1049
1050 p->handled = 0;
1051 }
1052
1053 /* Set all threads to unhandled.
1054 */
1055 static void
1056 clear_all_handled ()
1057 {
1058 thread_info *p;
1059
1060 #ifdef WAIT_BUFFER_DEBUG
1061 if( debug_on )
1062 printf( "clear_all_handled\n" );
1063 #endif
1064
1065 for( p = thread_head.head; p; p = p->next ) {
1066 p->handled = 0;
1067 }
1068
1069 for( p = deleted_threads.head; p; p = p->next ) {
1070 p->handled = 0;
1071 }
1072 }
1073
1074 /* Set this thread to default stepping mode.
1075 */
1076 static void
1077 clear_stepping_mode( tid )
1078 lwpid_t tid;
1079 {
1080 thread_info * p;
1081
1082 #ifdef WAIT_BUFFER_DEBUG
1083 if( debug_on )
1084 printf( "clear_stepping_mode %d\n", (int) tid );
1085 #endif
1086
1087 p = find_thread_info (tid);
1088 if (p == NULL)
1089 error ("Internal error: No thread state to clear?");
1090
1091 p->stepping_mode = DO_DEFAULT;
1092 }
1093
1094 /* Set all threads to do default continue on resume.
1095 */
1096 static void
1097 clear_all_stepping_mode ()
1098 {
1099 thread_info *p;
1100
1101 #ifdef WAIT_BUFFER_DEBUG
1102 if( debug_on )
1103 printf( "clear_all_stepping_mode\n" );
1104 #endif
1105
1106 for( p = thread_head.head; p; p = p->next ) {
1107 p->stepping_mode = DO_DEFAULT;
1108 }
1109
1110 for( p = deleted_threads.head; p; p = p->next ) {
1111 p->stepping_mode = DO_DEFAULT;
1112 }
1113 }
1114
1115 /* Set all threads to unseen on this pass.
1116 */
1117 static void
1118 set_all_unseen ()
1119 {
1120 thread_info *p;
1121
1122 for( p = thread_head.head; p; p = p->next ) {
1123 p->seen = 0;
1124 }
1125 }
1126
1127 #if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
1128 /* debugging routine.
1129 */
1130 static void
1131 print_tthread (p)
1132 thread_info * p;
1133 {
1134 printf( " Thread pid %d, tid %d", p->pid, p->tid );
1135 if( p->have_state )
1136 printf( ", event is %s",
1137 get_printable_name_of_ttrace_event( p->last_stop_state.tts_event ));
1138
1139 if( p->am_pseudo )
1140 printf( ", pseudo thread" );
1141
1142 if( p->have_signal )
1143 printf( ", have signal 0x%x", p->signal_value );
1144
1145 if( p->have_start )
1146 printf( ", have start at 0x%x", p->start );
1147
1148 printf( ", step is %s", get_printable_name_of_stepping_mode( p->stepping_mode ));
1149
1150 if( p->handled )
1151 printf( ", handled" );
1152 else
1153 printf( ", not handled" );
1154
1155 if( p->seen )
1156 printf( ", seen" );
1157 else
1158 printf( ", not seen" );
1159
1160 printf( "\n" );
1161 }
1162
1163 static void
1164 print_tthreads ()
1165 {
1166 thread_info *p;
1167
1168 if( thread_head.count == 0 )
1169 printf( "Thread list is empty\n" );
1170 else {
1171 printf( "Thread list has " );
1172 if( thread_head.count == 1 )
1173 printf( "1 entry:\n" );
1174 else
1175 printf( "%d entries:\n", thread_head.count );
1176 for( p = thread_head.head; p; p = p->next ) {
1177 print_tthread (p);
1178 }
1179 }
1180
1181 if( deleted_threads.count == 0 )
1182 printf( "Deleted thread list is empty\n" );
1183 else {
1184 printf( "Deleted thread list has " );
1185 if( deleted_threads.count == 1 )
1186 printf( "1 entry:\n" );
1187 else
1188 printf( "%d entries:\n", deleted_threads.count );
1189
1190 for( p = deleted_threads.head; p; p = p->next ) {
1191 print_tthread (p);
1192 }
1193 }
1194 }
1195 #endif
1196
1197 /* Update the thread list based on the "seen" bits.
1198 */
1199 static void
1200 update_thread_list ()
1201 {
1202 thread_info *p;
1203 thread_info *chase;
1204
1205 chase = NULL;
1206 for( p = thread_head.head; p; p = p->next ) {
1207 /* Is this an "unseen" thread which really happens to be a process?
1208 If so, is it inferior_pid and is a vfork in flight? If yes to
1209 all, then DON'T REMOVE IT! We're in the midst of moving a vfork
1210 operation, which is a multiple step thing, to the point where we
1211 can touch the parent again. We've most likely stopped to examine
1212 the child at a late stage in the vfork, and if we're not following
1213 the child, we'd best not treat the parent as a dead "thread"...
1214 */
1215 if( (!p->seen) && p->am_pseudo && vfork_in_flight
1216 && (p->pid != vforking_child_pid))
1217 p->seen = 1;
1218
1219 if( !p->seen ) {
1220 /* Remove this one
1221 */
1222
1223 #ifdef THREAD_DEBUG
1224 if( debug_on )
1225 printf( "Delete unseen thread: %d \n", p->tid );
1226 #endif
1227 del_tthread( p->tid );
1228 }
1229 }
1230 }
1231
1232
1233 \f
1234 /************************************************
1235 * O/S call wrappers *
1236 ************************************************
1237 */
1238
1239 /* This function simply calls ttrace with the given arguments.
1240 * It exists so that all calls to ttrace are isolated. All
1241 * parameters should be as specified by "man 2 ttrace".
1242 *
1243 * No other "raw" calls to ttrace should exist in this module.
1244 */
1245 static int
1246 call_real_ttrace( request, pid, tid, addr, data, addr2 )
1247 ttreq_t request;
1248 pid_t pid;
1249 lwpid_t tid;
1250 TTRACE_ARG_TYPE addr, data, addr2;
1251 {
1252 int tt_status;
1253
1254 errno = 0;
1255 tt_status = ttrace( request, pid, tid, addr, data, addr2 );
1256
1257 #ifdef THREAD_DEBUG
1258 if (errno) {
1259 /* Don't bother for a known benign error: if you ask for the
1260 * first thread state, but there is only one thread and it's
1261 * not stopped, ttrace complains.
1262 *
1263 * We have this inside the #ifdef because our caller will do
1264 * this check for real.
1265 */
1266 if( request != TT_PROC_GET_FIRST_LWP_STATE
1267 || errno != EPROTO ) {
1268 if( debug_on )
1269 printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
1270 get_printable_name_of_ttrace_request (request),
1271 pid, tid, tt_status );
1272 }
1273 }
1274 #endif
1275
1276 #if 0
1277 /* ??rehrauer: It would probably be most robust to catch and report
1278 * failed requests here. However, some clients of this interface
1279 * seem to expect to catch & deal with them, so we'd best not.
1280 */
1281 if (errno) {
1282 strcpy (reason_for_failure, "ttrace (");
1283 strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
1284 strcat (reason_for_failure, ")");
1285 printf( "ttrace error, errno = %d\n", errno );
1286 perror_with_name (reason_for_failure);
1287 }
1288 #endif
1289
1290 return tt_status;
1291 }
1292
1293 \f
1294 /* This function simply calls ttrace_wait with the given arguments.
1295 * It exists so that all calls to ttrace_wait are isolated.
1296 *
1297 * No "raw" calls to ttrace_wait should exist elsewhere.
1298 */
1299 static int
1300 call_real_ttrace_wait( pid, tid, option, tsp, tsp_size )
1301 int pid;
1302 lwpid_t tid;
1303 ttwopt_t option;
1304 ttstate_t *tsp;
1305 size_t tsp_size;
1306 {
1307 int ttw_status;
1308 thread_info * tinfo = NULL;
1309
1310 errno = 0;
1311 ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);
1312
1313 if (errno) {
1314 #ifdef THREAD_DEBUG
1315 if( debug_on )
1316 printf( "TW fail with pid %d, tid %d \n", pid, tid );
1317 #endif
1318
1319 perror_with_name ("ttrace wait");
1320 }
1321
1322 return ttw_status;
1323 }
1324
1325 \f
1326 /* A process may have one or more kernel threads, of which all or
1327 none may be stopped. This function returns the ID of the first
1328 kernel thread in a stopped state, or 0 if none are stopped.
1329
1330 This function can be used with get_process_next_stopped_thread_id
1331 to iterate over the IDs of all stopped threads of this process.
1332 */
1333 static lwpid_t
1334 get_process_first_stopped_thread_id (pid, thread_state)
1335 int pid;
1336 ttstate_t * thread_state;
1337 {
1338 int tt_status;
1339
1340 tt_status = call_real_ttrace (
1341 TT_PROC_GET_FIRST_LWP_STATE,
1342 (pid_t) pid,
1343 (lwpid_t) TT_NIL,
1344 (TTRACE_ARG_TYPE) thread_state,
1345 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1346 TT_NIL);
1347
1348 if (errno) {
1349 if( errno == EPROTO) {
1350 /* This is an error we can handle: there isn't any stopped
1351 * thread. This happens when we're re-starting the application
1352 * and it has only one thread. GET_NEXT handles the case of
1353 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1354 * "feature".)
1355 */
1356 tt_status = 1;
1357 errno = 0;
1358 return 0;
1359 }
1360 else
1361 perror_with_name ("ttrace");
1362 }
1363
1364 if( tt_status < 0 )
1365 /* Failed somehow.
1366 */
1367 return 0;
1368
1369 return thread_state->tts_lwpid;
1370 }
1371
1372 \f
1373 /* This function returns the ID of the "next" kernel thread in a
1374 stopped state, or 0 if there are none. "Next" refers to the
1375 thread following that of the last successful call to this
1376 function or to get_process_first_stopped_thread_id, using
1377 the value of thread_state returned by that call.
1378
1379 This function can be used with get_process_first_stopped_thread_id
1380 to iterate over the IDs of all stopped threads of this process.
1381 */
1382 static lwpid_t
1383 get_process_next_stopped_thread_id (pid, thread_state)
1384 int pid;
1385 ttstate_t * thread_state;
1386 {
1387 int tt_status;
1388
1389 tt_status = call_real_ttrace (
1390 TT_PROC_GET_NEXT_LWP_STATE,
1391 (pid_t) pid,
1392 (lwpid_t) TT_NIL,
1393 (TTRACE_ARG_TYPE) thread_state,
1394 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1395 TT_NIL);
1396 if (errno)
1397 perror_with_name ("ttrace");
1398
1399 if (tt_status < 0)
1400 /* Failed
1401 */
1402 return 0;
1403
1404 else if( tt_status == 0 ) {
1405 /* End of list, no next state. Don't return the
1406 * tts_lwpid, as it's a meaningless "240".
1407 *
1408 * This is an HPUX "feature".
1409 */
1410 return 0;
1411 }
1412
1413 return thread_state->tts_lwpid;
1414 }
1415
1416 /* ??rehrauer: Eventually this function perhaps should be calling
1417 pid_to_thread_id. However, that function currently does nothing
1418 for HP-UX. Even then, I'm not clear whether that function
1419 will return a "kernel" thread ID, or a "user" thread ID. If
1420 the former, we can just call it here. If the latter, we must
1421 map from the "user" tid to a "kernel" tid.
1422
1423 NOTE: currently not called.
1424 */
1425 static lwpid_t
1426 get_active_tid_of_pid (pid)
1427 int pid;
1428 {
1429 ttstate_t thread_state;
1430
1431 return get_process_first_stopped_thread_id (pid, &thread_state);
1432 }
1433
1434 /* This function returns 1 if tt_request is a ttrace request that
1435 * operates upon all threads of a (i.e., the entire) process.
1436 */
1437 int
1438 is_process_ttrace_request (tt_request)
1439 ttreq_t tt_request;
1440 {
1441 return IS_TTRACE_PROCREQ (tt_request);
1442 }
1443
1444 \f
1445 /* This function translates a thread ttrace request into
1446 * the equivalent process request for a one-thread process.
1447 */
1448 static ttreq_t
1449 make_process_version( request )
1450 ttreq_t request;
1451 {
1452 if (!IS_TTRACE_REQ (request)) {
1453 error( "Internal error, bad ttrace request made\n" );
1454 return -1;
1455 }
1456
1457 switch (request) {
1458 case TT_LWP_STOP :
1459 return TT_PROC_STOP;
1460
1461 case TT_LWP_CONTINUE :
1462 return TT_PROC_CONTINUE;
1463
1464 case TT_LWP_GET_EVENT_MASK :
1465 return TT_PROC_GET_EVENT_MASK;
1466
1467 case TT_LWP_SET_EVENT_MASK :
1468 return TT_PROC_SET_EVENT_MASK;
1469
1470 case TT_LWP_SINGLE :
1471 case TT_LWP_RUREGS :
1472 case TT_LWP_WUREGS :
1473 case TT_LWP_GET_STATE :
1474 return -1; /* No equivalent */
1475
1476 default :
1477 return request;
1478 }
1479 }
1480
1481 \f
1482 /* This function translates the "pid" used by the rest of
1483 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1484 * with the given arguments.
1485 *
1486 * In general, other parts of this module should call this
1487 * function when they are dealing with external users, who only
1488 * have tids to pass (but they call it "pid" for historical
1489 * reasons).
1490 */
1491 static int
1492 call_ttrace( request, gdb_tid, addr, data, addr2 )
1493 ttreq_t request;
1494 int gdb_tid;
1495 TTRACE_ARG_TYPE addr, data, addr2;
1496 {
1497 lwpid_t real_tid;
1498 int real_pid;
1499 ttreq_t new_request;
1500 int tt_status;
1501 char reason_for_failure [100]; /* Arbitrary size, should be big enough. */
1502
1503 #ifdef THREAD_DEBUG
1504 int is_interesting = 0;
1505
1506 if( TT_LWP_RUREGS == request ) {
1507 is_interesting = 1; /* Adjust code here as desired */
1508 }
1509
1510 if( is_interesting && 0 && debug_on ) {
1511 if( !is_process_ttrace_request( request )) {
1512 printf( "TT: Thread request, tid is %d", gdb_tid );
1513 printf( "== SINGLE at %x", addr );
1514 }
1515 else {
1516 printf( "TT: Process request, tid is %d\n", gdb_tid );
1517 printf( "==! SINGLE at %x", addr );
1518 }
1519 }
1520 #endif
1521
1522 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1523 * which happen before any threads get set up) should go
1524 * directly to "call_real_ttrace", so they don't happen here.
1525 *
1526 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1527 * rule them out....
1528 */
1529 #ifdef THREAD_DEBUG
1530 if( request == TT_PROC_SETTRC && debug_on )
1531 printf( "Unexpected call for TT_PROC_SETTRC\n" );
1532 #endif
1533
1534 /* Sometimes we get called with a bogus tid (e.g., if a
1535 * thread has terminated, we return 0; inftarg later asks
1536 * whether the thread has exited/forked/vforked).
1537 */
1538 if( gdb_tid == 0 )
1539 {
1540 errno = ESRCH; /* ttrace's response would probably be "No such process". */
1541 return -1;
1542 }
1543
1544 /* All other cases should be able to expect that there are
1545 * thread records.
1546 */
1547 if( !any_thread_records()) {
1548 #ifdef THREAD_DEBUG
1549 if( debug_on )
1550 warning ("No thread records for ttrace call");
1551 #endif
1552 errno = ESRCH; /* ttrace's response would be "No such process". */
1553 return -1;
1554 }
1555
1556 /* OK, now the task is to translate the incoming tid into
1557 * a pid/tid pair.
1558 */
1559 real_tid = map_from_gdb_tid( gdb_tid );
1560 real_pid = get_pid_for( real_tid );
1561
1562 /* Now check the result. "Real_pid" is NULL if our list
1563 * didn't find it. We have some tricks we can play to fix
1564 * this, however.
1565 */
1566 if( 0 == real_pid ) {
1567 ttstate_t thread_state;
1568
1569 #ifdef THREAD_DEBUG
1570 if( debug_on )
1571 printf( "No saved pid for tid %d\n", gdb_tid );
1572 #endif
1573
1574 if( is_process_ttrace_request( request )) {
1575
1576 /* Ok, we couldn't get a tid. Try to translate to
1577 * the equivalent process operation. We expect this
1578 * NOT to happen, so this is a desparation-type
1579 * move. It can happen if there is an internal
1580 * error and so no "wait()" call is ever done.
1581 */
1582 new_request = make_process_version( request );
1583 if( new_request == -1 ) {
1584
1585 #ifdef THREAD_DEBUG
1586 if( debug_on )
1587 printf( "...and couldn't make process version of thread operation\n" );
1588 #endif
1589
1590 /* Use hacky saved pid, which won't always be correct
1591 * in the multi-process future. Use tid as thread,
1592 * probably dooming this to failure. FIX!
1593 */
1594 if( saved_real_pid != 0 ) {
1595 #ifdef THREAD_DEBUG
1596 if( debug_on )
1597 printf( "...using saved pid %d\n", saved_real_pid );
1598 #endif
1599
1600 real_pid = saved_real_pid;
1601 real_tid = gdb_tid;
1602 }
1603
1604 else
1605 error( "Unable to perform thread operation" );
1606 }
1607
1608 else {
1609 /* Sucessfully translated this to a process request,
1610 * which needs no thread value.
1611 */
1612 real_pid = gdb_tid;
1613 real_tid = 0;
1614 request = new_request;
1615
1616 #ifdef THREAD_DEBUG
1617 if( debug_on ) {
1618 printf( "Translated thread request to process request\n" );
1619 if( saved_real_pid == 0 )
1620 printf( "...but there's no saved pid\n" );
1621
1622 else {
1623 if( gdb_tid != saved_real_pid )
1624 printf( "...but have the wrong pid (%d rather than %d)\n",
1625 gdb_tid, saved_real_pid );
1626 }
1627 }
1628 #endif
1629 } /* Translated to a process request */
1630 } /* Is a process request */
1631
1632 else {
1633 /* We have to have a thread. Ooops.
1634 */
1635 error( "Thread request with no threads (%s)",
1636 get_printable_name_of_ttrace_request( request ));
1637 }
1638 }
1639
1640 /* Ttrace doesn't like to see tid values on process requests,
1641 * even if we have the right one.
1642 */
1643 if (is_process_ttrace_request (request)) {
1644 real_tid = 0;
1645 }
1646
1647 #ifdef THREAD_DEBUG
1648 if( is_interesting && 0 && debug_on ) {
1649 printf( " now tid %d, pid %d\n", real_tid, real_pid );
1650 printf( " request is %s\n", get_printable_name_of_ttrace_request (request));
1651 }
1652 #endif
1653
1654 /* Finally, the (almost) real call.
1655 */
1656 tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1657
1658 #ifdef THREAD_DEBUG
1659 if(is_interesting && debug_on ) {
1660 if( !TT_OK( tt_status, errno )
1661 && !(tt_status == 0 & errno == 0))
1662 printf( " got error (errno==%d, status==%d)\n", errno, tt_status );
1663 }
1664 #endif
1665
1666 return tt_status;
1667 }
1668
1669
1670 /* Stop all the threads of a process.
1671 *
1672 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1673 * to get a TTEVT_NONE event, discarding the old event. Be
1674 * very careful, and only call TT_PROC_STOP when you mean it!
1675 */
1676 static void
1677 stop_all_threads_of_process( real_pid )
1678 pid_t real_pid;
1679 {
1680 int ttw_status;
1681
1682 ttw_status = call_real_ttrace (TT_PROC_STOP,
1683 (pid_t) real_pid,
1684 (lwpid_t) TT_NIL,
1685 (TTRACE_ARG_TYPE) TT_NIL,
1686 (TTRACE_ARG_TYPE) TT_NIL,
1687 TT_NIL );
1688 if (errno)
1689 perror_with_name ("ttrace stop of other threads");
1690 }
1691
1692
1693 /* Under some circumstances, it's unsafe to attempt to stop, or even
1694 query the state of, a process' threads.
1695
1696 In ttrace-based HP-UX, an example is a vforking child process. The
1697 vforking parent and child are somewhat fragile, w/r/t what we can do
1698 what we can do to them with ttrace, until after the child exits or
1699 execs, or until the parent's vfork event is delivered. Until that
1700 time, we must not try to stop the process' threads, or inquire how
1701 many there are, or even alter its data segments, or it typically dies
1702 with a SIGILL. Sigh.
1703
1704 This function returns 1 if this stopped process, and the event that
1705 we're told was responsible for its current stopped state, cannot safely
1706 have its threads examined.
1707 */
1708 #define CHILD_VFORKED(evt,pid) \
1709 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1710 #define CHILD_URPED(evt,pid) \
1711 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1712 #define PARENT_VFORKED(evt,pid) \
1713 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1714
1715 static int
1716 can_touch_threads_of_process (pid, stopping_event)
1717 int pid;
1718 ttevents_t stopping_event;
1719 {
1720 if (CHILD_VFORKED (stopping_event, pid))
1721 {
1722 vforking_child_pid = pid;
1723 vfork_in_flight = 1;
1724 }
1725
1726 else if (vfork_in_flight &&
1727 (PARENT_VFORKED (stopping_event, pid) ||
1728 CHILD_URPED (stopping_event, pid)))
1729 {
1730 vfork_in_flight = 0;
1731 vforking_child_pid = 0;
1732 }
1733
1734 return ! vfork_in_flight;
1735 }
1736
1737
1738 /* If we can find an as-yet-unhandled thread state of a
1739 * stopped thread of this process return 1 and set "tsp".
1740 * Return 0 if we can't.
1741 *
1742 * If this function is used when the threads of PIS haven't
1743 * been stopped, undefined behaviour is guaranteed!
1744 */
1745 static int
1746 select_stopped_thread_of_process (pid, tsp)
1747 int pid;
1748 ttstate_t * tsp;
1749 {
1750 lwpid_t candidate_tid, tid;
1751 ttstate_t candidate_tstate, tstate;
1752
1753 /* If we're not allowed to touch the process now, then just
1754 * return the current value of *TSP.
1755 *
1756 * This supports "vfork". It's ok, really, to double the
1757 * current event (the child EXEC, we hope!).
1758 */
1759 if (! can_touch_threads_of_process (pid, tsp->tts_event))
1760 return 1;
1761
1762 /* Decide which of (possibly more than one) events to
1763 * return as the first one. We scan them all so that
1764 * we always return the result of a fake-step first.
1765 */
1766 candidate_tid = 0;
1767 for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1768 tid != 0;
1769 tid = get_process_next_stopped_thread_id (pid, &tstate))
1770 {
1771 /* TTEVT_NONE events are uninteresting to our clients. They're
1772 * an artifact of our "stop the world" model--the thread is
1773 * stopped because we stopped it.
1774 */
1775 if (tstate.tts_event == TTEVT_NONE) {
1776 set_handled( pid, tstate.tts_lwpid );
1777 }
1778
1779 /* Did we just single-step a single thread, without letting any
1780 * of the others run? Is this an event for that thread?
1781 *
1782 * If so, we believe our client would prefer to see this event
1783 * over any others. (Typically the client wants to just push
1784 * one thread a little farther forward, and then go around
1785 * checking for what all threads are doing.)
1786 */
1787 else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1788 {
1789 #ifdef WAIT_BUFFER_DEBUG
1790 /* It's possible here to see either a SIGTRAP (due to
1791 * successful completion of a step) or a SYSCALL_ENTRY
1792 * (due to a step completion with active hardware
1793 * watchpoints).
1794 */
1795 if( debug_on )
1796 printf( "Ending fake step with tid %d, state %s\n",
1797 tstate.tts_lwpid,
1798 get_printable_name_of_ttrace_event( tstate.tts_event ));
1799 #endif
1800
1801 /* Remember this one, and throw away any previous
1802 * candidate.
1803 */
1804 candidate_tid = tstate.tts_lwpid;
1805 candidate_tstate = tstate;
1806 }
1807
1808 #ifdef FORGET_DELETED_BPTS
1809
1810 /* We can't just do this, as if we do, and then wind
1811 * up the loop with no unhandled events, we need to
1812 * handle that case--the appropriate reaction is to
1813 * just continue, but there's no easy way to do that.
1814 *
1815 * Better to put this in the ttrace_wait call--if, when
1816 * we fake a wait, we update our events based on the
1817 * breakpoint_here_pc call and find there are no more events,
1818 * then we better continue and so on.
1819 *
1820 * Or we could put it in the next/continue fake.
1821 * But it has to go in the buffering code, not in the
1822 * real go/wait code.
1823 */
1824 else if( (TTEVT_SIGNAL == tstate.tts_event)
1825 && (5 == tstate.tts_u.tts_signal.tts_signo)
1826 && (0 != get_raw_pc( tstate.tts_lwpid ))
1827 && ! breakpoint_here_p( get_raw_pc( tstate.tts_lwpid )) ) {
1828 /*
1829 * If the user deleted a breakpoint while this
1830 * breakpoint-hit event was buffered, we can forget
1831 * it now.
1832 */
1833 #ifdef WAIT_BUFFER_DEBUG
1834 if( debug_on )
1835 printf( "Forgetting deleted bp hit for thread %d\n",
1836 tstate.tts_lwpid );
1837 #endif
1838
1839 set_handled( pid, tstate.tts_lwpid );
1840 }
1841 #endif
1842
1843 /* Else, is this the first "unhandled" event? If so,
1844 * we believe our client wants to see it (if we don't
1845 * see a fake-step later on in the scan).
1846 */
1847 else if( !was_handled( tstate.tts_lwpid ) && candidate_tid == 0 ) {
1848 candidate_tid = tstate.tts_lwpid;
1849 candidate_tstate = tstate;
1850 }
1851
1852 /* This is either an event that has already been "handled",
1853 * and thus we believe is uninteresting to our client, or we
1854 * already have a candidate event. Ignore it...
1855 */
1856 }
1857
1858 /* What do we report?
1859 */
1860 if( doing_fake_step ) {
1861 if( candidate_tid == fake_step_tid ) {
1862 /* Fake step.
1863 */
1864 tstate = candidate_tstate;
1865 }
1866 else {
1867 warning( "Internal error: fake-step failed to complete." );
1868 return 0;
1869 }
1870 }
1871 else if( candidate_tid != 0 ) {
1872 /* Found a candidate unhandled event.
1873 */
1874 tstate = candidate_tstate;
1875 }
1876 else if( tid != 0 ) {
1877 warning( "Internal error in call of ttrace_wait." );
1878 return 0;
1879 }
1880 else {
1881 warning ("Internal error: no unhandled thread event to select");
1882 return 0;
1883 }
1884
1885 copy_ttstate_t (tsp, &tstate);
1886 return 1;
1887 } /* End of select_stopped_thread_of_process */
1888
1889 #ifdef PARANOIA
1890 /* Check our internal thread data against the real thing.
1891 */
1892 static void
1893 check_thread_consistency( real_pid )
1894 pid_t real_pid;
1895 {
1896 int tid; /* really lwpid_t */
1897 ttstate_t tstate;
1898 thread_info *p;
1899
1900 /* Spin down the O/S list of threads, checking that they
1901 * match what we've got.
1902 */
1903 for (tid = get_process_first_stopped_thread_id( real_pid, &tstate );
1904 tid != 0;
1905 tid = get_process_next_stopped_thread_id( real_pid, &tstate )) {
1906
1907 p = find_thread_info( tid );
1908
1909 if( NULL == p ) {
1910 warning( "No internal thread data for thread %d.", tid );
1911 continue;
1912 }
1913
1914 if( !p->seen ) {
1915 warning( "Inconsistent internal thread data for thread %d.", tid );
1916 }
1917
1918 if( p->terminated ) {
1919 warning( "Thread %d is not terminated, internal error.", tid );
1920 continue;
1921 }
1922
1923
1924 #define TT_COMPARE( fld ) \
1925 tstate.fld != p->last_stop_state.fld
1926
1927 if( p->have_state ) {
1928 if( TT_COMPARE( tts_pid )
1929 || TT_COMPARE( tts_lwpid )
1930 || TT_COMPARE( tts_user_tid )
1931 || TT_COMPARE( tts_event )
1932 || TT_COMPARE( tts_flags )
1933 || TT_COMPARE( tts_scno )
1934 || TT_COMPARE( tts_scnargs )) {
1935 warning( "Internal thread data for thread %d is wrong.", tid );
1936 continue;
1937 }
1938 }
1939 }
1940 }
1941 #endif /* PARANOIA */
1942
1943 \f
1944 /* This function wraps calls to "call_real_ttrace_wait" so
1945 * that a actual wait is only done when all pending events
1946 * have been reported.
1947 *
1948 * Note that typically it is called with a pid of "0", i.e.
1949 * the "don't care" value.
1950 *
1951 * Return value is the status of the pseudo wait.
1952 */
1953 static int
1954 call_ttrace_wait( pid, option, tsp, tsp_size )
1955 int pid;
1956 ttwopt_t option;
1957 ttstate_t *tsp;
1958 size_t tsp_size;
1959 {
1960 /* This holds the actual, for-real, true process ID.
1961 */
1962 static int real_pid;
1963
1964 /* As an argument to ttrace_wait, zero pid
1965 * means "Any process", and zero tid means
1966 * "Any thread of the specified process".
1967 */
1968 int wait_pid = 0;
1969 lwpid_t wait_tid = 0;
1970 lwpid_t real_tid;
1971
1972 int ttw_status = 0; /* To be returned */
1973
1974 thread_info * tinfo = NULL;
1975
1976 if( pid != 0 ) {
1977 /* Unexpected case.
1978 */
1979 #ifdef THREAD_DEBUG
1980 if( debug_on )
1981 printf( "TW: Pid to wait on is %d\n", pid );
1982 #endif
1983
1984 if( !any_thread_records())
1985 error( "No thread records for ttrace call w. specific pid" );
1986
1987 /* OK, now the task is to translate the incoming tid into
1988 * a pid/tid pair.
1989 */
1990 real_tid = map_from_gdb_tid( pid );
1991 real_pid = get_pid_for( real_tid );
1992 #ifdef THREAD_DEBUG
1993 if( debug_on )
1994 printf( "==TW: real pid %d, real tid %d\n", real_pid, real_tid );
1995 #endif
1996 }
1997
1998
1999 /* Sanity checks and set-up.
2000 * Process State
2001 *
2002 * Stopped Running Fake-step (v)Fork
2003 * \________________________________________
2004 * |
2005 * No buffered events | error wait wait wait
2006 * |
2007 * Buffered events | debuffer error wait debuffer (?)
2008 *
2009 */
2010 if( more_events_left == 0 ) {
2011
2012 if( process_state == RUNNING ) {
2013 /* OK--normal call of ttrace_wait with no buffered events.
2014 */
2015 ;
2016 }
2017 else if( process_state == FAKE_STEPPING ) {
2018 /* Ok--call of ttrace_wait to support
2019 * fake stepping with no buffered events.
2020 *
2021 * But we better be fake-stepping!
2022 */
2023 if( !doing_fake_step ) {
2024 warning( "Inconsistent thread state." );
2025 }
2026 }
2027 else if( (process_state == FORKING)
2028 || (process_state == VFORKING)) {
2029 /* Ok--there are two processes, so waiting
2030 * for the second while the first is stopped
2031 * is ok. Handled bits stay as they were.
2032 */
2033 ;
2034 }
2035 else if( process_state == STOPPED ) {
2036 warning( "Process not running at wait call." );
2037 }
2038 else
2039 /* No known state.
2040 */
2041 warning( "Inconsistent process state." );
2042 }
2043
2044 else {
2045 /* More events left
2046 */
2047 if( process_state == STOPPED ) {
2048 /* OK--buffered events being unbuffered.
2049 */
2050 ;
2051 }
2052 else if( process_state == RUNNING ) {
2053 /* An error--shouldn't have buffered events
2054 * when running.
2055 */
2056 warning( "Trying to continue with buffered events:" );
2057 }
2058 else if( process_state == FAKE_STEPPING ) {
2059 /*
2060 * Better be fake-stepping!
2061 */
2062 if( !doing_fake_step ) {
2063 warning( "Losing buffered thread events!\n" );
2064 }
2065 }
2066 else if( (process_state == FORKING)
2067 || (process_state == VFORKING)) {
2068 /* Ok--there are two processes, so waiting
2069 * for the second while the first is stopped
2070 * is ok. Handled bits stay as they were.
2071 */
2072 ;
2073 }
2074 else
2075 warning( "Process in unknown state with buffered events." );
2076 }
2077
2078 /* Sometimes we have to wait for a particular thread
2079 * (if we're stepping over a bpt). In that case, we
2080 * _know_ it's going to complete the single-step we
2081 * asked for (because we're only doing the step under
2082 * certain very well-understood circumstances), so it
2083 * can't block.
2084 */
2085 if( doing_fake_step ) {
2086 wait_tid = fake_step_tid;
2087 wait_pid = get_pid_for( fake_step_tid );
2088
2089 #ifdef WAIT_BUFFER_DEBUG
2090 if( debug_on )
2091 printf( "Doing a wait after a fake-step for %d, pid %d\n",
2092 wait_tid, wait_pid );
2093 #endif
2094 }
2095
2096 if( more_events_left == 0 /* No buffered events, need real ones. */
2097 || process_state != STOPPED ) {
2098 /* If there are no buffered events, and so we need
2099 * real ones, or if we are FORKING, VFORKING,
2100 * FAKE_STEPPING or RUNNING, and thus have to do
2101 * a real wait, then do a real wait.
2102 */
2103
2104 #ifdef WAIT_BUFFER_DEBUG
2105 /* Normal case... */
2106 if( debug_on )
2107 printf( "TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid );
2108 #endif
2109
2110 /* The actual wait call.
2111 */
2112 ttw_status = call_real_ttrace_wait( wait_pid, wait_tid, option, tsp, tsp_size);
2113
2114 /* Note that the routines we'll call will be using "call_real_ttrace",
2115 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2116 * the rest of the world uses (which is actually the tid).
2117 */
2118 real_pid = tsp->tts_pid;
2119
2120 /* For most events: Stop the world!
2121 *
2122 * It's sometimes not safe to stop all threads of a process.
2123 * Sometimes it's not even safe to ask for the thread state
2124 * of a process!
2125 */
2126 if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2127 {
2128 /* If we're really only stepping a single thread, then don't
2129 * try to stop all the others -- we only do this single-stepping
2130 * business when all others were already stopped...and the stop
2131 * would mess up other threads' events.
2132 *
2133 * Similiarly, if there are other threads with events,
2134 * don't do the stop.
2135 */
2136 if( !doing_fake_step ) {
2137 if( more_events_left > 0 )
2138 warning( "Internal error in stopping process" );
2139
2140 stop_all_threads_of_process (real_pid);
2141
2142 /* At this point, we could scan and update_thread_list(),
2143 * and only use the local list for the rest of the
2144 * module! We'd get rid of the scans in the various
2145 * continue routines (adding one in attach). It'd
2146 * be great--UPGRADE ME!
2147 */
2148 }
2149 }
2150
2151 #ifdef PARANOIA
2152 else if( debug_on ) {
2153 if( more_events_left > 0 )
2154 printf( "== Can't stop process; more events!\n" );
2155 else
2156 printf( "== Can't stop process!\n" );
2157 }
2158 #endif
2159
2160 process_state = STOPPED;
2161
2162 #ifdef WAIT_BUFFER_DEBUG
2163 if( debug_on )
2164 printf( "Process set to STOPPED\n" );
2165 #endif
2166 }
2167
2168 else {
2169 /* Fake a call to ttrace_wait. The process must be
2170 * STOPPED, as we aren't going to do any wait.
2171 */
2172 #ifdef WAIT_BUFFER_DEBUG
2173 if( debug_on )
2174 printf( "TW: fake it\n" );
2175 #endif
2176
2177 if( process_state != STOPPED ) {
2178 warning( "Process not stopped at wait call, in state '%s'.\n",
2179 get_printable_name_of_process_state( process_state ));
2180 }
2181
2182 if( doing_fake_step )
2183 error( "Internal error in stepping over breakpoint" );
2184
2185 ttw_status = 0; /* Faking it is always successful! */
2186 } /* End of fake or not? if */
2187
2188 /* Pick an event to pass to our caller. Be paranoid.
2189 */
2190 if( !select_stopped_thread_of_process( real_pid, tsp ))
2191 warning( "Can't find event, using previous event." );
2192
2193 else if( tsp->tts_event == TTEVT_NONE )
2194 warning( "Internal error: no thread has a real event." );
2195
2196 else if( doing_fake_step ) {
2197 if( fake_step_tid != tsp->tts_lwpid )
2198 warning( "Internal error in stepping over breakpoint." );
2199
2200 /* This wait clears the (current) fake-step if there was one.
2201 */
2202 doing_fake_step = 0;
2203 fake_step_tid = 0;
2204 }
2205
2206 /* We now have a correct tsp and ttw_status for the thread
2207 * which we want to report. So it's "handled"! This call
2208 * will add it to our list if it's not there already.
2209 */
2210 set_handled( real_pid, tsp->tts_lwpid );
2211
2212 /* Save a copy of the ttrace state of this thread, in our local
2213 thread descriptor.
2214
2215 This caches the state. The implementation of queries like
2216 target_has_execd can then use this cached state, rather than
2217 be forced to make an explicit ttrace call to get it.
2218
2219 (Guard against the condition that this is the first time we've
2220 waited on, i.e., seen this thread, and so haven't yet entered
2221 it into our list of threads.)
2222 */
2223 tinfo = find_thread_info (tsp->tts_lwpid);
2224 if (tinfo != NULL) {
2225 copy_ttstate_t (&tinfo->last_stop_state, tsp);
2226 tinfo->have_state = 1;
2227 }
2228
2229 return ttw_status;
2230 } /* call_ttrace_wait */
2231
2232 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2233 int
2234 child_reported_exec_events_per_exec_call ()
2235 {
2236 return 1; /* ttrace reports the event once per call. */
2237 }
2238 #endif
2239
2240
2241 \f
2242 /* Our implementation of hardware watchpoints involves making memory
2243 pages write-protected. We must remember a page's original permissions,
2244 and we must also know when it is appropriate to restore a page's
2245 permissions to its original state.
2246
2247 We use a "dictionary" of hardware-watched pages to do this. Each
2248 hardware-watched page is recorded in the dictionary. Each page's
2249 dictionary entry contains the original permissions and a reference
2250 count. Pages are hashed into the dictionary by their start address.
2251
2252 When hardware watchpoint is set on page X for the first time, page X
2253 is added to the dictionary with a reference count of 1. If other
2254 hardware watchpoints are subsequently set on page X, its reference
2255 count is incremented. When hardware watchpoints are removed from
2256 page X, its reference count is decremented. If a page's reference
2257 count drops to 0, it's permissions are restored and the page's entry
2258 is thrown out of the dictionary.
2259 */
2260 typedef struct memory_page {
2261 CORE_ADDR page_start;
2262 int reference_count;
2263 int original_permissions;
2264 struct memory_page * next;
2265 struct memory_page * previous;
2266 } memory_page_t;
2267
2268 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2269
2270 static struct {
2271 LONGEST page_count;
2272 int page_size;
2273 int page_protections_allowed;
2274 /* These are just the heads of chains of actual page descriptors. */
2275 memory_page_t buckets [MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2276 } memory_page_dictionary;
2277
2278
2279 static void
2280 require_memory_page_dictionary ()
2281 {
2282 int i;
2283
2284 /* Is the memory page dictionary ready for use? If so, we're done. */
2285 if (memory_page_dictionary.page_count >= (LONGEST) 0)
2286 return;
2287
2288 /* Else, initialize it. */
2289 memory_page_dictionary.page_count = (LONGEST) 0;
2290
2291 for (i=0; i<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2292 {
2293 memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2294 memory_page_dictionary.buckets[i].reference_count = 0;
2295 memory_page_dictionary.buckets[i].next = NULL;
2296 memory_page_dictionary.buckets[i].previous = NULL;
2297 }
2298 }
2299
2300
2301 static void
2302 retire_memory_page_dictionary ()
2303 {
2304 memory_page_dictionary.page_count = (LONGEST) -1;
2305 }
2306
2307
2308 /* Write-protect the memory page that starts at this address.
2309
2310 Returns the original permissions of the page.
2311 */
2312 static int
2313 write_protect_page (pid, page_start)
2314 int pid;
2315 CORE_ADDR page_start;
2316 {
2317 int tt_status;
2318 int original_permissions;
2319 int new_permissions;
2320
2321 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2322 pid,
2323 (TTRACE_ARG_TYPE) page_start,
2324 TT_NIL,
2325 (TTRACE_ARG_TYPE) &original_permissions);
2326 if (errno || (tt_status < 0))
2327 {
2328 return 0; /* What else can we do? */
2329 }
2330
2331 /* We'll also write-protect the page now, if that's allowed. */
2332 if (memory_page_dictionary.page_protections_allowed)
2333 {
2334 new_permissions = original_permissions & ~PROT_WRITE;
2335 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2336 pid,
2337 (TTRACE_ARG_TYPE) page_start,
2338 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2339 (TTRACE_ARG_TYPE) new_permissions);
2340 if (errno || (tt_status < 0))
2341 {
2342 return 0; /* What else can we do? */
2343 }
2344 }
2345
2346 return original_permissions;
2347 }
2348
2349
2350 /* Unwrite-protect the memory page that starts at this address, restoring
2351 (what we must assume are) its original permissions.
2352 */
2353 static void
2354 unwrite_protect_page (pid, page_start, original_permissions)
2355 int pid;
2356 CORE_ADDR page_start;
2357 int original_permissions;
2358 {
2359 int tt_status;
2360
2361 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2362 pid,
2363 (TTRACE_ARG_TYPE) page_start,
2364 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2365 (TTRACE_ARG_TYPE) original_permissions);
2366 if (errno || (tt_status < 0))
2367 {
2368 return; /* What else can we do? */
2369 }
2370 }
2371
2372
2373 /* Memory page-protections are used to implement "hardware" watchpoints
2374 on HP-UX.
2375
2376 For every memory page that is currently being watched (i.e., that
2377 presently should be write-protected), write-protect it.
2378 */
2379 void
2380 hppa_enable_page_protection_events (pid)
2381 int pid;
2382 {
2383 int bucket;
2384
2385 memory_page_dictionary.page_protections_allowed = 1;
2386
2387 for (bucket=0; bucket<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2388 {
2389 memory_page_t * page;
2390
2391 page = memory_page_dictionary.buckets[bucket].next;
2392 while (page != NULL)
2393 {
2394 page->original_permissions = write_protect_page (pid, page->page_start);
2395 page = page->next;
2396 }
2397 }
2398 }
2399
2400
2401 /* Memory page-protections are used to implement "hardware" watchpoints
2402 on HP-UX.
2403
2404 For every memory page that is currently being watched (i.e., that
2405 presently is or should be write-protected), un-write-protect it.
2406 */
2407 void
2408 hppa_disable_page_protection_events (pid)
2409 int pid;
2410 {
2411 int bucket;
2412
2413 for (bucket=0; bucket<MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2414 {
2415 memory_page_t * page;
2416
2417 page = memory_page_dictionary.buckets[bucket].next;
2418 while (page != NULL)
2419 {
2420 unwrite_protect_page (pid, page->page_start, page->original_permissions);
2421 page = page->next;
2422 }
2423 }
2424
2425 memory_page_dictionary.page_protections_allowed = 0;
2426 }
2427
2428 /* Count the number of outstanding events. At this
2429 * point, we have selected one thread and its event
2430 * as the one to be "reported" upwards to core gdb.
2431 * That thread is already marked as "handled".
2432 *
2433 * Note: we could just scan our own thread list. FIXME!
2434 */
2435 static int
2436 count_unhandled_events( real_pid, real_tid )
2437 int real_pid;
2438 lwpid_t real_tid;
2439 {
2440 ttstate_t tstate;
2441 lwpid_t ttid;
2442 int events_left;
2443
2444 /* Ok, find out how many threads have real events to report.
2445 */
2446 events_left = 0;
2447 ttid = get_process_first_stopped_thread_id( real_pid, &tstate );
2448
2449 #ifdef THREAD_DEBUG
2450 if( debug_on ) {
2451 if( ttid == 0 )
2452 printf( "Process %d has no threads\n", real_pid );
2453 else
2454 printf( "Process %d has these threads:\n", real_pid );
2455 }
2456 #endif
2457
2458 while (ttid > 0 ) {
2459 if( tstate.tts_event != TTEVT_NONE
2460 && !was_handled( ttid )) {
2461 /* TTEVT_NONE implies we just stopped it ourselves
2462 * because we're the stop-the-world guys, so it's
2463 * not an event from our point of view.
2464 *
2465 * If "was_handled" is true, this is an event we
2466 * already handled, so don't count it.
2467 *
2468 * Note that we don't count the thread with the
2469 * currently-reported event, as it's already marked
2470 * as handled.
2471 */
2472 events_left++;
2473 }
2474
2475 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2476 if( debug_on ) {
2477 if( ttid == real_tid )
2478 printf( "*" ); /* Thread we're reporting */
2479 else
2480 printf( " " );
2481
2482 if( tstate.tts_event != TTEVT_NONE )
2483 printf( "+" ); /* Thread with a real event */
2484 else
2485 printf( " " );
2486
2487 if( was_handled( ttid ))
2488 printf( "h" ); /* Thread has been handled */
2489 else
2490 printf( " " );
2491
2492 printf( " %d, with event %s", ttid,
2493 get_printable_name_of_ttrace_event( tstate.tts_event ));
2494
2495 if( tstate.tts_event == TTEVT_SIGNAL
2496 && 5 == tstate.tts_u.tts_signal.tts_signo ) {
2497 CORE_ADDR pc_val;
2498
2499 pc_val = get_raw_pc( ttid );
2500
2501 if( pc_val > 0 )
2502 printf( " breakpoint at 0x%x\n", pc_val );
2503 else
2504 printf( " bpt, can't fetch pc.\n" );
2505 }
2506 else
2507 printf( "\n" );
2508 }
2509 #endif
2510
2511 ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2512 }
2513
2514 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2515 if( debug_on )
2516 if( events_left > 0 )
2517 printf( "There are thus %d pending events\n", events_left );
2518 #endif
2519
2520 return events_left;
2521 }
2522
2523 /* This function is provided as a sop to clients that are calling
2524 * proc_wait to wait for a process to stop. (see the
2525 * implementation of child_wait.) Return value is the pid for
2526 * the event that ended the wait.
2527 *
2528 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2529 */
2530 int
2531 proc_wait (pid, status)
2532 int pid;
2533 int *status;
2534 {
2535 ttstate_t tsp;
2536 int ttwait_return;
2537 int real_pid;
2538 ttstate_t state;
2539 lwpid_t real_tid;
2540 int return_pid;
2541
2542 /* The ptrace implementation of this also ignores pid.
2543 */
2544 *status = 0;
2545
2546 ttwait_return = call_ttrace_wait( 0, TTRACE_WAITOK, &tsp, sizeof (tsp) );
2547 if (ttwait_return < 0)
2548 {
2549 /* ??rehrauer: It appears that if our inferior exits and we
2550 haven't asked for exit events, that we're not getting any
2551 indication save a negative return from ttrace_wait and an
2552 errno set to ESRCH?
2553 */
2554 if (errno == ESRCH)
2555 {
2556 *status = 0; /* WIFEXITED */
2557 return inferior_pid;
2558 }
2559
2560 warning( "Call of ttrace_wait returned with errno %d.",
2561 errno );
2562 *status = ttwait_return;
2563 return inferior_pid;
2564 }
2565
2566 real_pid = tsp.tts_pid;
2567 real_tid = tsp.tts_lwpid;
2568
2569 /* One complication is that the "tts_event" structure has
2570 * a set of flags, and more than one can be set. So we
2571 * either have to force an order (as we do here), or handle
2572 * more than one flag at a time.
2573 */
2574 if (tsp.tts_event & TTEVT_LWP_CREATE) {
2575
2576 /* Unlike what you might expect, this event is reported in
2577 * the _creating_ thread, and the _created_ thread (whose tid
2578 * we have) is still running. So we have to stop it. This
2579 * has already been done in "call_ttrace_wait", but should we
2580 * ever abandon the "stop-the-world" model, here's the command
2581 * to use:
2582 *
2583 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2584 *
2585 * Note that this would depend on being called _after_ "add_tthread"
2586 * below for the tid-to-pid translation to be done in "call_ttrace".
2587 */
2588
2589 #ifdef THREAD_DEBUG
2590 if( debug_on )
2591 printf( "New thread: pid %d, tid %d, creator tid %d\n",
2592 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2593 real_tid );
2594 #endif
2595
2596 /* Now we have to return the tid of the created thread, not
2597 * the creating thread, or "wait_for_inferior" won't know we
2598 * have a new "process" (thread). Plus we should record it
2599 * right, too.
2600 */
2601 real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2602
2603 add_tthread( real_pid, real_tid );
2604 }
2605
2606 else if( (tsp.tts_event & TTEVT_LWP_TERMINATE )
2607 || (tsp.tts_event & TTEVT_LWP_EXIT) ) {
2608
2609 #ifdef THREAD_DEBUG
2610 if( debug_on )
2611 printf( "Thread dies: %d\n", real_tid );
2612 #endif
2613
2614 del_tthread( real_tid );
2615 }
2616
2617 else if (tsp.tts_event & TTEVT_EXEC) {
2618
2619 #ifdef THREAD_DEBUG
2620 if( debug_on )
2621 printf( "Pid %d has zero'th thread %d; inferior pid is %d\n",
2622 real_pid, real_tid, inferior_pid );
2623 #endif
2624
2625 add_tthread( real_pid, real_tid );
2626 }
2627
2628 #ifdef THREAD_DEBUG
2629 else if( debug_on ) {
2630 printf( "Process-level event %s, using tid %d\n",
2631 get_printable_name_of_ttrace_event( tsp.tts_event ),
2632 real_tid );
2633
2634 /* OK to do this, as "add_tthread" won't add
2635 * duplicate entries. Also OK not to do it,
2636 * as this event isn't one which can change the
2637 * thread state.
2638 */
2639 add_tthread( real_pid, real_tid );
2640 }
2641 #endif
2642
2643
2644 /* How many events are left to report later?
2645 * In a non-stop-the-world model, this isn't needed.
2646 *
2647 * Note that it's not always safe to query the thread state of a process,
2648 * which is what count_unhandled_events does. (If unsafe, we're left with
2649 * no other resort than to assume that no more events remain...)
2650 */
2651 if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2652 more_events_left = count_unhandled_events( real_pid, real_tid );
2653
2654 else {
2655 if( more_events_left > 0 )
2656 warning( "Vfork or fork causing loss of %d buffered events.",
2657 more_events_left );
2658
2659 more_events_left = 0;
2660 }
2661
2662 /* Attempt to translate the ttrace_wait-returned status into the
2663 ptrace equivalent.
2664
2665 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2666 clients that expect to pick apart a ptrace wait status, to use
2667 something a little more abstract.
2668 */
2669 if ( (tsp.tts_event & TTEVT_EXEC)
2670 || (tsp.tts_event & TTEVT_FORK)
2671 || (tsp.tts_event & TTEVT_VFORK))
2672 {
2673 /* Forks come in pairs (parent and child), so core gdb
2674 * will do two waits. Be ready to notice this.
2675 */
2676 if (tsp.tts_event & TTEVT_FORK)
2677 {
2678 process_state = FORKING;
2679
2680 #ifdef WAIT_BUFFER_DEBUG
2681 if( debug_on )
2682 printf( "Process set to FORKING\n" );
2683 #endif
2684 }
2685 else if (tsp.tts_event & TTEVT_VFORK)
2686 {
2687 process_state = VFORKING;
2688
2689 #ifdef WAIT_BUFFER_DEBUG
2690 if( debug_on )
2691 printf( "Process set to VFORKING\n" );
2692 #endif
2693 }
2694
2695 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2696 but I don't think non HP-UX-specific clients really carefully
2697 inspect the first events they get after inferior startup, so
2698 it probably almost doesn't matter what we claim this is.
2699 */
2700
2701 #ifdef THREAD_DEBUG
2702 if( debug_on )
2703 printf( "..a process 'event'\n" );
2704 #endif
2705
2706 /* Also make fork and exec events look like bpts, so they can be caught.
2707 */
2708 *status = 0177 | (_SIGTRAP << 8);
2709 }
2710
2711 /* Special-cases: We ask for syscall entry and exit events to implement
2712 "fast" (aka "hardware") watchpoints.
2713
2714 When we get a syscall entry, we want to disable page-protections,
2715 and resume the inferior; this isn't an event we wish for
2716 wait_for_inferior to see. Note that we must resume ONLY the
2717 thread that reported the syscall entry; we don't want to allow
2718 other threads to run with the page protections off, as they might
2719 then be able to write to watch memory without it being caught.
2720
2721 When we get a syscall exit, we want to reenable page-protections,
2722 but we don't want to resume the inferior; this is an event we wish
2723 wait_for_inferior to see. Make it look like the signal we normally
2724 get for a single-step completion. This should cause wait_for_inferior
2725 to evaluate whether any watchpoint triggered.
2726
2727 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2728 due to some HP-UX "features". Some syscalls have problems with
2729 write-protections on some pages, and some syscalls seem to have
2730 pending writes to those pages at the time we're getting the return
2731 event. So, we'll single-step the inferior to get out of the syscall,
2732 and then reenable protections.
2733
2734 Note that we're intentionally allowing the syscall exit case to
2735 fall through into the succeeding cases, as sometimes we single-
2736 step out of one syscall only to immediately enter another...
2737 */
2738 else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2739 || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2740 {
2741 /* Make a syscall event look like a breakpoint. Same comments
2742 as for exec & fork events.
2743 */
2744 #ifdef THREAD_DEBUG
2745 if( debug_on )
2746 printf( "..a syscall 'event'\n" );
2747 #endif
2748
2749 /* Also make syscall events look like bpts, so they can be caught.
2750 */
2751 *status = 0177 | (_SIGTRAP << 8);
2752 }
2753
2754 else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2755 || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2756 || (tsp.tts_event & TTEVT_LWP_EXIT))
2757 {
2758 /* Make a thread event look like a breakpoint. Same comments
2759 * as for exec & fork events.
2760 */
2761 #ifdef THREAD_DEBUG
2762 if( debug_on )
2763 printf( "..a thread 'event'\n" );
2764 #endif
2765
2766 /* Also make thread events look like bpts, so they can be caught.
2767 */
2768 *status = 0177 | (_SIGTRAP << 8);
2769 }
2770
2771 else if ((tsp.tts_event & TTEVT_EXIT))
2772 { /* WIFEXITED */
2773
2774 #ifdef THREAD_DEBUG
2775 if( debug_on )
2776 printf( "..an exit\n" );
2777 #endif
2778
2779 /* Prevent rest of gdb from thinking this is
2780 * a new thread if for some reason it's never
2781 * seen the main thread before.
2782 */
2783 inferior_pid = map_to_gdb_tid( real_tid ); /* HACK, FIX */
2784
2785 *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2786 }
2787
2788 else if (tsp.tts_event & TTEVT_SIGNAL)
2789 { /* WIFSTOPPED */
2790 #ifdef THREAD_DEBUG
2791 if( debug_on )
2792 printf( "..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo );
2793 #endif
2794
2795 *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2796 }
2797
2798 else
2799 { /* !WIFSTOPPED */
2800
2801 /* This means the process or thread terminated. But we should've
2802 caught an explicit exit/termination above. So warn (this is
2803 really an internal error) and claim the process or thread
2804 terminated with a SIGTRAP.
2805 */
2806
2807 warning ("process_wait: unknown process state");
2808
2809 #ifdef THREAD_DEBUG
2810 if( debug_on )
2811 printf( "Process-level event %s, using tid %d\n",
2812 get_printable_name_of_ttrace_event( tsp.tts_event ),
2813 real_tid );
2814 #endif
2815
2816 *status = _SIGTRAP;
2817 }
2818
2819 target_post_wait (tsp.tts_pid, *status);
2820
2821
2822 #ifdef THREAD_DEBUG
2823 if( debug_on )
2824 printf( "Done waiting, pid is %d, tid %d\n", real_pid, real_tid );
2825 #endif
2826
2827 /* All code external to this module uses the tid, but calls
2828 * it "pid". There's some tweaking so that the outside sees
2829 * the first thread as having the same number as the starting
2830 * pid.
2831 */
2832 return_pid = map_to_gdb_tid( real_tid );
2833
2834 /* Remember this for later use in "hppa_prepare_to_proceed".
2835 */
2836 old_gdb_pid = inferior_pid;
2837 reported_pid = return_pid;
2838 reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2839
2840 if( real_tid == 0 || return_pid == 0 ) {
2841 warning( "Internal error: process-wait failed." );
2842 }
2843
2844 return return_pid;
2845 }
2846
2847 \f
2848 /* This function causes the caller's process to be traced by its
2849 parent. This is intended to be called after GDB forks itself,
2850 and before the child execs the target. Despite the name, it
2851 is called by the child.
2852
2853 Note that HP-UX ttrace is rather funky in how this is done.
2854 If the parent wants to get the initial exec event of a child,
2855 it must set the ttrace event mask of the child to include execs.
2856 (The child cannot do this itself.) This must be done after the
2857 child is forked, but before it execs.
2858
2859 To coordinate the parent and child, we implement a semaphore using
2860 pipes. After SETTRC'ing itself, the child tells the parent that
2861 it is now traceable by the parent, and waits for the parent's
2862 acknowledgement. The parent can then set the child's event mask,
2863 and notify the child that it can now exec.
2864
2865 (The acknowledgement by parent happens as a result of a call to
2866 child_acknowledge_created_inferior.)
2867 */
2868 int
2869 parent_attach_all ()
2870 {
2871 int tt_status;
2872
2873 /* We need a memory home for a constant, to pass it to ttrace.
2874 The value of the constant is arbitrary, so long as both
2875 parent and child use the same value. Might as well use the
2876 "magic" constant provided by ttrace...
2877 */
2878 uint64_t tc_magic_child = TT_VERSION;
2879 uint64_t tc_magic_parent = 0;
2880
2881 tt_status = call_real_ttrace (
2882 TT_PROC_SETTRC,
2883 (int) TT_NIL,
2884 (lwpid_t) TT_NIL,
2885 TT_NIL,
2886 (TTRACE_ARG_TYPE) TT_VERSION,
2887 TT_NIL );
2888
2889 if (tt_status < 0)
2890 return tt_status;
2891
2892 /* Notify the parent that we're potentially ready to exec(). */
2893 write (startup_semaphore.child_channel[SEM_TALK],
2894 &tc_magic_child,
2895 sizeof (tc_magic_child));
2896
2897 /* Wait for acknowledgement from the parent. */
2898 read (startup_semaphore.parent_channel[SEM_LISTEN],
2899 &tc_magic_parent,
2900 sizeof (tc_magic_parent));
2901
2902 if (tc_magic_child != tc_magic_parent)
2903 warning ("mismatched semaphore magic");
2904
2905 /* Discard our copy of the semaphore. */
2906 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
2907 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
2908 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
2909 (void) close (startup_semaphore.child_channel[SEM_TALK]);
2910
2911 return tt_status;
2912 }
2913
2914 /* Despite being file-local, this routine is dealing with
2915 * actual process IDs, not thread ids. That's because it's
2916 * called before the first "wait" call, and there's no map
2917 * yet from tids to pids.
2918 *
2919 * When it is called, a forked child is running, but waiting on
2920 * the semaphore. If you stop the child and re-start it,
2921 * things get confused, so don't do that! An attached child is
2922 * stopped.
2923 *
2924 * Since this is called after either attach or run, we
2925 * have to be the common part of both.
2926 */
2927 static void
2928 require_notification_of_events ( real_pid )
2929 int real_pid;
2930 {
2931 int tt_status;
2932 ttevent_t notifiable_events;
2933
2934 lwpid_t tid;
2935 ttstate_t thread_state;
2936
2937 #ifdef THREAD_DEBUG
2938 if( debug_on )
2939 printf( "Require notif, pid is %d\n", real_pid );
2940 #endif
2941
2942 /* Temporary HACK: tell inftarg.c/child_wait to not
2943 * loop until pids are the same.
2944 */
2945 not_same_real_pid = 0;
2946
2947 sigemptyset (&notifiable_events.tte_signals);
2948 notifiable_events.tte_opts = TTEO_NONE;
2949
2950 /* This ensures that forked children inherit their parent's
2951 * event mask, which we're setting here.
2952 *
2953 * NOTE: if you debug gdb with itself, then the ultimate
2954 * debuggee gets flags set by the outermost gdb, as
2955 * a child of a child will still inherit.
2956 */
2957 notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
2958
2959 notifiable_events.tte_events = TTEVT_DEFAULT;
2960 notifiable_events.tte_events |= TTEVT_SIGNAL;
2961 notifiable_events.tte_events |= TTEVT_EXEC;
2962 notifiable_events.tte_events |= TTEVT_EXIT;
2963 notifiable_events.tte_events |= TTEVT_FORK;
2964 notifiable_events.tte_events |= TTEVT_VFORK;
2965 notifiable_events.tte_events |= TTEVT_LWP_CREATE;
2966 notifiable_events.tte_events |= TTEVT_LWP_EXIT;
2967 notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
2968
2969 tt_status = call_real_ttrace (
2970 TT_PROC_SET_EVENT_MASK,
2971 real_pid,
2972 (lwpid_t) TT_NIL,
2973 (TTRACE_ARG_TYPE) &notifiable_events,
2974 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
2975 TT_NIL);
2976 }
2977
2978 static void
2979 require_notification_of_exec_events ( real_pid )
2980 int real_pid;
2981 {
2982 int tt_status;
2983 ttevent_t notifiable_events;
2984
2985 lwpid_t tid;
2986 ttstate_t thread_state;
2987
2988 #ifdef THREAD_DEBUG
2989 if( debug_on )
2990 printf( "Require notif, pid is %d\n", real_pid );
2991 #endif
2992
2993 /* Temporary HACK: tell inftarg.c/child_wait to not
2994 * loop until pids are the same.
2995 */
2996 not_same_real_pid = 0;
2997
2998 sigemptyset (&notifiable_events.tte_signals);
2999 notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3000
3001 /* This ensures that forked children don't inherit their parent's
3002 * event mask, which we're setting here.
3003 */
3004 notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3005
3006 notifiable_events.tte_events = TTEVT_DEFAULT;
3007 notifiable_events.tte_events |= TTEVT_EXEC;
3008 notifiable_events.tte_events |= TTEVT_EXIT;
3009
3010 tt_status = call_real_ttrace (
3011 TT_PROC_SET_EVENT_MASK,
3012 real_pid,
3013 (lwpid_t) TT_NIL,
3014 (TTRACE_ARG_TYPE) &notifiable_events,
3015 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3016 TT_NIL);
3017 }
3018
3019 \f
3020 /* This function is called by the parent process, with pid being the
3021 * ID of the child process, after the debugger has forked.
3022 */
3023 void
3024 child_acknowledge_created_inferior (pid)
3025 int pid;
3026 {
3027 /* We need a memory home for a constant, to pass it to ttrace.
3028 The value of the constant is arbitrary, so long as both
3029 parent and child use the same value. Might as well use the
3030 "magic" constant provided by ttrace...
3031 */
3032 uint64_t tc_magic_parent = TT_VERSION;
3033 uint64_t tc_magic_child = 0;
3034
3035 /* Wait for the child to tell us that it has forked. */
3036 read (startup_semaphore.child_channel[SEM_LISTEN],
3037 &tc_magic_child,
3038 sizeof(tc_magic_child));
3039
3040 /* Clear thread info now. We'd like to do this in
3041 * "require...", but that messes up attach.
3042 */
3043 clear_thread_info();
3044
3045 /* Tell the "rest of gdb" that the initial thread exists.
3046 * This isn't really a hack. Other thread-based versions
3047 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3048 *
3049 * Q: Why don't we also add this thread to the local
3050 * list via "add_tthread"?
3051 *
3052 * A: Because we don't know the tid, and can't stop the
3053 * the process safely to ask what it is. Anyway, we'll
3054 * add it when it gets the EXEC event.
3055 */
3056 add_thread( pid ); /* in thread.c */
3057
3058 /* We can now set the child's ttrace event mask.
3059 */
3060 require_notification_of_exec_events (pid);
3061
3062 /* Tell ourselves that the process is running.
3063 */
3064 process_state = RUNNING;
3065
3066 /* Notify the child that it can exec. */
3067 write (startup_semaphore.parent_channel[SEM_TALK],
3068 &tc_magic_parent,
3069 sizeof (tc_magic_parent));
3070
3071 /* Discard our copy of the semaphore. */
3072 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3073 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3074 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3075 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3076 }
3077
3078
3079 /*
3080 * arrange for notification of all events by
3081 * calling require_notification_of_events.
3082 */
3083 void
3084 child_post_startup_inferior ( real_pid)
3085 int real_pid;
3086 {
3087 require_notification_of_events (real_pid);
3088 }
3089
3090 /* From here on, we should expect tids rather than pids.
3091 */
3092 static void
3093 hppa_enable_catch_fork (tid)
3094 int tid;
3095 {
3096 int tt_status;
3097 ttevent_t ttrace_events;
3098
3099 /* Get the set of events that are currently enabled.
3100 */
3101 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3102 tid,
3103 (TTRACE_ARG_TYPE) &ttrace_events,
3104 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3105 TT_NIL );
3106 if (errno)
3107 perror_with_name ("ttrace");
3108
3109 /* Add forks to that set. */
3110 ttrace_events.tte_events |= TTEVT_FORK;
3111
3112 #ifdef THREAD_DEBUG
3113 if( debug_on )
3114 printf( "enable fork, tid is %d\n", tid );
3115 #endif
3116
3117 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3118 tid,
3119 (TTRACE_ARG_TYPE) &ttrace_events,
3120 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3121 TT_NIL);
3122 if (errno)
3123 perror_with_name ("ttrace");
3124 }
3125
3126
3127 static void
3128 hppa_disable_catch_fork (tid)
3129 int tid;
3130 {
3131 int tt_status;
3132 ttevent_t ttrace_events;
3133
3134 /* Get the set of events that are currently enabled.
3135 */
3136 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3137 tid,
3138 (TTRACE_ARG_TYPE) &ttrace_events,
3139 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3140 TT_NIL);
3141
3142 if (errno)
3143 perror_with_name ("ttrace");
3144
3145 /* Remove forks from that set. */
3146 ttrace_events.tte_events &= ~TTEVT_FORK;
3147
3148 #ifdef THREAD_DEBUG
3149 if( debug_on )
3150 printf("disable fork, tid is %d\n", tid );
3151 #endif
3152
3153 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3154 tid,
3155 (TTRACE_ARG_TYPE) &ttrace_events,
3156 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3157 TT_NIL);
3158
3159 if (errno)
3160 perror_with_name ("ttrace");
3161 }
3162
3163
3164 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3165 int
3166 child_insert_fork_catchpoint (tid)
3167 int tid;
3168 {
3169 /* Enable reporting of fork events from the kernel. */
3170 /* ??rehrauer: For the moment, we're always enabling these events,
3171 and just ignoring them if there's no catchpoint to catch them.
3172 */
3173 return 0;
3174 }
3175 #endif
3176
3177
3178 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3179 int
3180 child_remove_fork_catchpoint (tid)
3181 int tid;
3182 {
3183 /* Disable reporting of fork events from the kernel. */
3184 /* ??rehrauer: For the moment, we're always enabling these events,
3185 and just ignoring them if there's no catchpoint to catch them.
3186 */
3187 return 0;
3188 }
3189 #endif
3190
3191
3192 static void
3193 hppa_enable_catch_vfork (tid)
3194 int tid;
3195 {
3196 int tt_status;
3197 ttevent_t ttrace_events;
3198
3199 /* Get the set of events that are currently enabled.
3200 */
3201 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3202 tid,
3203 (TTRACE_ARG_TYPE) &ttrace_events,
3204 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3205 TT_NIL);
3206
3207 if (errno)
3208 perror_with_name ("ttrace");
3209
3210 /* Add vforks to that set. */
3211 ttrace_events.tte_events |= TTEVT_VFORK;
3212
3213 #ifdef THREAD_DEBUG
3214 if( debug_on )
3215 printf("enable vfork, tid is %d\n", tid );
3216 #endif
3217
3218 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3219 tid,
3220 (TTRACE_ARG_TYPE) &ttrace_events,
3221 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3222 TT_NIL);
3223
3224 if (errno)
3225 perror_with_name ("ttrace");
3226 }
3227
3228
3229 static void
3230 hppa_disable_catch_vfork (tid)
3231 int tid;
3232 {
3233 int tt_status;
3234 ttevent_t ttrace_events;
3235
3236 /* Get the set of events that are currently enabled. */
3237 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3238 tid,
3239 (TTRACE_ARG_TYPE) &ttrace_events,
3240 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3241 TT_NIL);
3242
3243 if (errno)
3244 perror_with_name ("ttrace");
3245
3246 /* Remove vforks from that set. */
3247 ttrace_events.tte_events &= ~TTEVT_VFORK;
3248
3249 #ifdef THREAD_DEBUG
3250 if( debug_on )
3251 printf("disable vfork, tid is %d\n", tid );
3252 #endif
3253 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3254 tid,
3255 (TTRACE_ARG_TYPE) &ttrace_events,
3256 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3257 TT_NIL);
3258
3259 if (errno)
3260 perror_with_name ("ttrace");
3261 }
3262
3263
3264 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3265 int
3266 child_insert_vfork_catchpoint (tid)
3267 int tid;
3268 {
3269 /* Enable reporting of vfork events from the kernel. */
3270 /* ??rehrauer: For the moment, we're always enabling these events,
3271 and just ignoring them if there's no catchpoint to catch them.
3272 */
3273 return 0;
3274 }
3275 #endif
3276
3277
3278 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3279 int
3280 child_remove_vfork_catchpoint (tid)
3281 int tid;
3282 {
3283 /* Disable reporting of vfork events from the kernel. */
3284 /* ??rehrauer: For the moment, we're always enabling these events,
3285 and just ignoring them if there's no catchpoint to catch them.
3286 */
3287 return 0;
3288 }
3289 #endif
3290
3291 #if defined(CHILD_HAS_FORKED)
3292
3293 /* Q: Do we need to map the returned process ID to a thread ID?
3294 *
3295 * A: I don't think so--here we want a _real_ pid. Any later
3296 * operations will call "require_notification_of_events" and
3297 * start the mapping.
3298 */
3299 int
3300 child_has_forked (tid, childpid)
3301 int tid;
3302 int *childpid;
3303 {
3304 int tt_status;
3305 ttstate_t ttrace_state;
3306 thread_info * tinfo;
3307
3308 /* Do we have cached thread state that we can consult? If so, use it. */
3309 tinfo = find_thread_info (map_from_gdb_tid (tid));
3310 if (tinfo != NULL) {
3311 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3312 }
3313
3314 /* Nope, must read the thread's current state */
3315 else
3316 {
3317 tt_status = call_ttrace (TT_LWP_GET_STATE,
3318 tid,
3319 (TTRACE_ARG_TYPE) &ttrace_state,
3320 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3321 TT_NIL);
3322
3323 if (errno)
3324 perror_with_name ("ttrace");
3325
3326 if (tt_status < 0)
3327 return 0;
3328 }
3329
3330 if (ttrace_state.tts_event & TTEVT_FORK)
3331 {
3332 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3333 return 1;
3334 }
3335
3336 return 0;
3337 }
3338 #endif
3339
3340
3341 #if defined(CHILD_HAS_VFORKED)
3342
3343 /* See child_has_forked for pid discussion.
3344 */
3345 int
3346 child_has_vforked (tid, childpid)
3347 int tid;
3348 int * childpid;
3349 {
3350 int tt_status;
3351 ttstate_t ttrace_state;
3352 thread_info * tinfo;
3353
3354 /* Do we have cached thread state that we can consult? If so, use it. */
3355 tinfo = find_thread_info (map_from_gdb_tid (tid));
3356 if (tinfo != NULL)
3357 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3358
3359 /* Nope, must read the thread's current state */
3360 else
3361 {
3362 tt_status = call_ttrace (TT_LWP_GET_STATE,
3363 tid,
3364 (TTRACE_ARG_TYPE) &ttrace_state,
3365 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3366 TT_NIL);
3367
3368 if (errno)
3369 perror_with_name ("ttrace");
3370
3371 if (tt_status < 0)
3372 return 0;
3373 }
3374
3375 if (ttrace_state.tts_event & TTEVT_VFORK)
3376 {
3377 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3378 return 1;
3379 }
3380
3381 return 0;
3382 }
3383 #endif
3384
3385
3386 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3387 int
3388 child_can_follow_vfork_prior_to_exec ()
3389 {
3390 /* ttrace does allow this.
3391
3392 ??rehrauer: However, I had major-league problems trying to
3393 convince wait_for_inferior to handle that case. Perhaps when
3394 it is rewritten to grok multiple processes in an explicit way...
3395 */
3396 return 0;
3397 }
3398 #endif
3399
3400
3401 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3402 int
3403 child_insert_exec_catchpoint (tid)
3404 int tid;
3405 {
3406 /* Enable reporting of exec events from the kernel. */
3407 /* ??rehrauer: For the moment, we're always enabling these events,
3408 and just ignoring them if there's no catchpoint to catch them.
3409 */
3410 return 0;
3411 }
3412 #endif
3413
3414
3415 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3416 int
3417 child_remove_exec_catchpoint (tid)
3418 int tid;
3419 {
3420 /* Disable reporting of execevents from the kernel. */
3421 /* ??rehrauer: For the moment, we're always enabling these events,
3422 and just ignoring them if there's no catchpoint to catch them.
3423 */
3424 return 0;
3425 }
3426 #endif
3427
3428
3429 #if defined(CHILD_HAS_EXECD)
3430 int
3431 child_has_execd (tid, execd_pathname)
3432 int tid;
3433 char ** execd_pathname;
3434 {
3435 int tt_status;
3436 ttstate_t ttrace_state;
3437 thread_info * tinfo;
3438
3439 /* Do we have cached thread state that we can consult? If so, use it. */
3440 tinfo = find_thread_info (map_from_gdb_tid (tid));
3441 if (tinfo != NULL)
3442 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3443
3444 /* Nope, must read the thread's current state */
3445 else
3446 {
3447 tt_status = call_ttrace (TT_LWP_GET_STATE,
3448 tid,
3449 (TTRACE_ARG_TYPE) &ttrace_state,
3450 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3451 TT_NIL);
3452
3453 if (errno)
3454 perror_with_name ("ttrace");
3455
3456 if (tt_status < 0)
3457 return 0;
3458 }
3459
3460 if (ttrace_state.tts_event & TTEVT_EXEC)
3461 {
3462 /* See child_pid_to_exec_file in this file: this is a macro.
3463 */
3464 char * exec_file = target_pid_to_exec_file (tid);
3465
3466 *execd_pathname = savestring (exec_file, strlen (exec_file));
3467 return 1;
3468 }
3469
3470 return 0;
3471 }
3472 #endif
3473
3474
3475 #if defined(CHILD_HAS_SYSCALL_EVENT)
3476 int
3477 child_has_syscall_event (pid, kind, syscall_id)
3478 int pid;
3479 enum target_waitkind * kind;
3480 int * syscall_id;
3481 {
3482 int tt_status;
3483 ttstate_t ttrace_state;
3484 thread_info * tinfo;
3485
3486 /* Do we have cached thread state that we can consult? If so, use it. */
3487 tinfo = find_thread_info (map_from_gdb_tid (pid));
3488 if (tinfo != NULL)
3489 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3490
3491 /* Nope, must read the thread's current state */
3492 else
3493 {
3494 tt_status = call_ttrace (TT_LWP_GET_STATE,
3495 pid,
3496 (TTRACE_ARG_TYPE) &ttrace_state,
3497 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3498 TT_NIL);
3499
3500 if (errno)
3501 perror_with_name ("ttrace");
3502
3503 if (tt_status < 0)
3504 return 0;
3505 }
3506
3507 *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */
3508 *syscall_id = -1;
3509
3510 if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3511 *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3512 else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3513 *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3514 else
3515 return 0;
3516
3517 *syscall_id = ttrace_state.tts_scno;
3518 return 1;
3519 }
3520 #endif
3521
3522
3523 \f
3524 #if defined(CHILD_THREAD_ALIVE)
3525
3526 /* Check to see if the given thread is alive.
3527 *
3528 * We'll trust the thread list, as the more correct
3529 * approach of stopping the process and spinning down
3530 * the OS's thread list is _very_ expensive.
3531 *
3532 * May need a FIXME for that reason.
3533 */
3534 int
3535 child_thread_alive (gdb_tid)
3536 lwpid_t gdb_tid;
3537 {
3538 lwpid_t tid;
3539
3540 /* This spins down the lists twice.
3541 * Possible peformance improvement here!
3542 */
3543 tid = map_from_gdb_tid( gdb_tid );
3544 return !is_terminated( tid );
3545 }
3546
3547 #endif
3548
3549
3550 \f
3551 /* This function attempts to read the specified number of bytes from the
3552 save_state_t that is our view into the hardware registers, starting at
3553 ss_offset, and ending at ss_offset + sizeof_buf - 1
3554
3555 If this function succeeds, it deposits the fetched bytes into buf,
3556 and returns 0.
3557
3558 If it fails, it returns a negative result. The contents of buf are
3559 undefined it this function fails.
3560 */
3561 int
3562 read_from_register_save_state (tid, ss_offset, buf, sizeof_buf)
3563 int tid;
3564 TTRACE_ARG_TYPE ss_offset;
3565 char * buf;
3566 int sizeof_buf;
3567 {
3568 int tt_status;
3569 register_value_t register_value = 0;
3570
3571 tt_status = call_ttrace (TT_LWP_RUREGS,
3572 tid,
3573 ss_offset,
3574 (TTRACE_ARG_TYPE) sizeof_buf,
3575 (TTRACE_ARG_TYPE) buf);
3576
3577 if( tt_status == 1 )
3578 /* Map ttrace's version of success to our version.
3579 * Sometime ttrace returns 0, but that's ok here.
3580 */
3581 return 0;
3582
3583 return tt_status;
3584 }
3585
3586 \f
3587 /* This function attempts to write the specified number of bytes to the
3588 save_state_t that is our view into the hardware registers, starting at
3589 ss_offset, and ending at ss_offset + sizeof_buf - 1
3590
3591 If this function succeeds, it deposits the bytes in buf, and returns 0.
3592
3593 If it fails, it returns a negative result. The contents of the save_state_t
3594 are undefined it this function fails.
3595 */
3596 int
3597 write_to_register_save_state (tid, ss_offset, buf, sizeof_buf)
3598 int tid;
3599 TTRACE_ARG_TYPE ss_offset;
3600 char * buf;
3601 int sizeof_buf;
3602 {
3603 int tt_status;
3604 register_value_t register_value = 0;
3605
3606 tt_status = call_ttrace (TT_LWP_WUREGS,
3607 tid,
3608 ss_offset,
3609 (TTRACE_ARG_TYPE) sizeof_buf,
3610 (TTRACE_ARG_TYPE) buf);
3611 return tt_status;
3612 }
3613
3614 \f
3615 /* This function is a sop to the largeish number of direct calls
3616 to call_ptrace that exist in other files. Rather than create
3617 functions whose name abstracts away from ptrace, and change all
3618 the present callers of call_ptrace, we'll do the expedient (and
3619 perhaps only practical) thing.
3620
3621 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3622 process. Thus, we must translate all ptrace requests into their
3623 process-specific, ttrace equivalents.
3624 */
3625 int
3626 call_ptrace (pt_request, gdb_tid, addr, data)
3627 int pt_request;
3628 int gdb_tid;
3629 PTRACE_ARG3_TYPE addr;
3630 int data;
3631 {
3632 ttreq_t tt_request;
3633 TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3634 TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3635 TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3636 int tt_status;
3637 register_value_t register_value;
3638 int read_buf;
3639
3640 /* Perform the necessary argument translation. Note that some
3641 cases are funky enough in the ttrace realm that we handle them
3642 very specially.
3643 */
3644 switch (pt_request) {
3645 /* The following cases cannot conveniently be handled conveniently
3646 by merely adjusting the ptrace arguments and feeding into the
3647 generic call to ttrace at the bottom of this function.
3648
3649 Note that because all branches of this switch end in "return",
3650 there's no need for any "break" statements.
3651 */
3652 case PT_SETTRC :
3653 return parent_attach_all ();
3654
3655 case PT_RUREGS :
3656 tt_status = read_from_register_save_state (gdb_tid,
3657 tt_addr,
3658 &register_value,
3659 sizeof (register_value));
3660 if (tt_status < 0)
3661 return tt_status;
3662 return register_value;
3663
3664 case PT_WUREGS :
3665 register_value = (int) tt_data;
3666 tt_status = write_to_register_save_state (gdb_tid,
3667 tt_addr,
3668 &register_value,
3669 sizeof (register_value));
3670 return tt_status;
3671 break;
3672
3673 case PT_READ_I :
3674 tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */
3675 gdb_tid,
3676 tt_addr,
3677 (TTRACE_ARG_TYPE) 4,
3678 (TTRACE_ARG_TYPE) &read_buf);
3679 if (tt_status < 0)
3680 return tt_status;
3681 return read_buf;
3682
3683 case PT_READ_D :
3684 tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */
3685 gdb_tid,
3686 tt_addr,
3687 (TTRACE_ARG_TYPE) 4,
3688 (TTRACE_ARG_TYPE) &read_buf);
3689 if (tt_status < 0)
3690 return tt_status;
3691 return read_buf;
3692
3693 case PT_ATTACH :
3694 tt_status = call_real_ttrace (TT_PROC_ATTACH,
3695 map_from_gdb_tid (gdb_tid),
3696 (lwpid_t) TT_NIL,
3697 tt_addr,
3698 (TTRACE_ARG_TYPE) TT_VERSION,
3699 tt_addr2);
3700 if (tt_status < 0)
3701 return tt_status;
3702 return tt_status;
3703
3704 /* The following cases are handled by merely adjusting the ptrace
3705 arguments and feeding into the generic call to ttrace.
3706 */
3707 case PT_DETACH :
3708 tt_request = TT_PROC_DETACH;
3709 break;
3710
3711 case PT_WRITE_I :
3712 tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */
3713 tt_data = 4; /* This many bytes. */
3714 tt_addr2 = (TTRACE_ARG_TYPE) &data; /* Address of xfer source. */
3715 break;
3716
3717 case PT_WRITE_D :
3718 tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */
3719 tt_data = 4; /* This many bytes. */
3720 tt_addr2 = (TTRACE_ARG_TYPE) &data; /* Address of xfer source. */
3721 break;
3722
3723 case PT_RDTEXT :
3724 tt_request = TT_PROC_RDTEXT;
3725 break;
3726
3727 case PT_RDDATA :
3728 tt_request = TT_PROC_RDDATA;
3729 break;
3730
3731 case PT_WRTEXT :
3732 tt_request = TT_PROC_WRTEXT;
3733 break;
3734
3735 case PT_WRDATA :
3736 tt_request = TT_PROC_WRDATA;
3737 break;
3738
3739 case PT_CONTINUE :
3740 tt_request = TT_PROC_CONTINUE;
3741 break;
3742
3743 case PT_STEP :
3744 tt_request = TT_LWP_SINGLE; /* Should not be making this request? */
3745 break;
3746
3747 case PT_KILL :
3748 tt_request = TT_PROC_EXIT;
3749 break;
3750
3751 case PT_GET_PROCESS_PATHNAME :
3752 tt_request = TT_PROC_GET_PATHNAME;
3753 break;
3754
3755 default :
3756 tt_request = pt_request; /* Let ttrace be the one to complain. */
3757 break;
3758 }
3759
3760 return call_ttrace (tt_request,
3761 gdb_tid,
3762 tt_addr,
3763 tt_data,
3764 tt_addr2);
3765 }
3766
3767 /* Kill that pesky process!
3768 */
3769 void
3770 kill_inferior ()
3771 {
3772 int tid;
3773 int wait_status;
3774 thread_info * t;
3775 thread_info **paranoia;
3776 int para_count, i;
3777
3778 if (inferior_pid == 0)
3779 return;
3780
3781 /* Walk the list of "threads", some of which are "pseudo threads",
3782 aka "processes". For each that is NOT inferior_pid, stop it,
3783 and detach it.
3784
3785 You see, we may not have just a single process to kill. If we're
3786 restarting or quitting or detaching just after the inferior has
3787 forked, then we've actually two processes to clean up.
3788
3789 But we can't just call target_mourn_inferior() for each, since that
3790 zaps the target vector.
3791 */
3792
3793 paranoia = (thread_info **) malloc( thread_head.count *
3794 sizeof(thread_info *));
3795 para_count = 0;
3796
3797 t = thread_head.head;
3798 while (t) {
3799
3800 paranoia[ para_count ] = t;
3801 for( i = 0; i < para_count; i++ ){
3802 if( t->next == paranoia[i] ) {
3803 warning( "Bad data in gdb's thread data; repairing." );
3804 t->next = 0;
3805 }
3806 }
3807 para_count++;
3808
3809 if (t->am_pseudo && (t->pid != inferior_pid))
3810 {
3811 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3812 * generates no event.
3813 */
3814 call_ttrace (TT_PROC_STOP,
3815 t->pid,
3816 TT_NIL,
3817 TT_NIL,
3818 TT_NIL);
3819
3820 call_ttrace (TT_PROC_DETACH,
3821 t->pid,
3822 TT_NIL,
3823 (TTRACE_ARG_TYPE) TARGET_SIGNAL_0,
3824 TT_NIL);
3825 }
3826 t = t->next;
3827 }
3828
3829 free( paranoia );
3830
3831 call_ttrace (TT_PROC_STOP,
3832 inferior_pid,
3833 TT_NIL,
3834 TT_NIL,
3835 TT_NIL);
3836 target_mourn_inferior ();
3837 clear_thread_info();
3838 }
3839
3840
3841 #ifndef CHILD_RESUME
3842
3843 /* Sanity check a thread about to be continued.
3844 */
3845 static void
3846 thread_dropping_event_check( p )
3847 thread_info *p;
3848 {
3849 if( !p->handled ) {
3850 /*
3851 * This seems to happen when we "next" over a
3852 * "fork()" while following the parent. If it's
3853 * the FORK event, that's ok. If it's a SIGNAL
3854 * in the unfollowed child, that's ok to--but
3855 * how can we know that's what's going on?
3856 *
3857 * FIXME!
3858 */
3859 if( p->have_state ) {
3860 if( p->last_stop_state.tts_event == TTEVT_FORK ) {
3861 /* Ok */
3862 ;
3863 }
3864 else if( p->last_stop_state.tts_event == TTEVT_SIGNAL ) {
3865 /* Ok, close eyes and let it happen.
3866 */
3867 ;
3868 }
3869 else {
3870 /* This shouldn't happen--we're dropping a
3871 * real event.
3872 */
3873 warning( "About to continue process %d, thread %d with unhandled event %s.",
3874 p->pid, p->tid,
3875 get_printable_name_of_ttrace_event(
3876 p->last_stop_state.tts_event ));
3877
3878 #ifdef PARANOIA
3879 if( debug_on )
3880 print_tthread( p );
3881 #endif
3882 }
3883 }
3884 else {
3885 /* No saved state, have to assume it failed.
3886 */
3887 warning( "About to continue process %d, thread %d with unhandled event.",
3888 p->pid, p->tid );
3889 #ifdef PARANOIA
3890 if( debug_on )
3891 print_tthread( p );
3892 #endif
3893 }
3894 }
3895
3896 } /* thread_dropping_event_check */
3897
3898 /* Use a loop over the threads to continue all the threads but
3899 * the one specified, which is to be stepped.
3900 */
3901 static void
3902 threads_continue_all_but_one( gdb_tid, signal )
3903 lwpid_t gdb_tid;
3904 int signal;
3905 {
3906 thread_info *p;
3907 int thread_signal;
3908 lwpid_t real_tid;
3909 lwpid_t scan_tid;
3910 ttstate_t state;
3911 int real_pid;
3912
3913 #ifdef THREAD_DEBUG
3914 if( debug_on )
3915 printf( "Using loop over threads to step/resume with signals\n" );
3916 #endif
3917
3918 /* First update the thread list.
3919 */
3920 set_all_unseen();
3921 real_tid = map_from_gdb_tid( gdb_tid );
3922 real_pid = get_pid_for( real_tid );
3923
3924 scan_tid = get_process_first_stopped_thread_id( real_pid, &state );
3925 while ( 0 != scan_tid ) {
3926
3927 #ifdef THREAD_DEBUG
3928 /* FIX: later should check state is stopped;
3929 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
3930 */
3931 if( debug_on )
3932 if( state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED )
3933 printf( "About to continue non-stopped thread %d\n", scan_tid );
3934 #endif
3935
3936 p = find_thread_info( scan_tid );
3937 if( NULL == p ) {
3938 add_tthread( real_pid, scan_tid );
3939 p = find_thread_info( scan_tid );
3940
3941 /* This is either a newly-created thread or the
3942 * result of a fork; in either case there's no
3943 * actual event to worry about.
3944 */
3945 p->handled = 1;
3946
3947 if( state.tts_event != TTEVT_NONE ) {
3948 /* Oops, do need to worry!
3949 */
3950 warning( "Unexpected thread with \"%s\" event.",
3951 get_printable_name_of_ttrace_event( state.tts_event ));
3952 }
3953 }
3954 else if( scan_tid != p->tid )
3955 error( "Bad data in thread database." );
3956
3957 #ifdef THREAD_DEBUG
3958 if( debug_on )
3959 if( p->terminated )
3960 printf( "Why are we continuing a dead thread?\n" );
3961 #endif
3962
3963 p->seen = 1;
3964
3965 scan_tid = get_process_next_stopped_thread_id( real_pid, &state );
3966 }
3967
3968 /* Remove unseen threads.
3969 */
3970 update_thread_list();
3971
3972 /* Now run down the thread list and continue or step.
3973 */
3974 for( p = thread_head.head; p; p = p->next ) {
3975
3976 /* Sanity check.
3977 */
3978 thread_dropping_event_check( p );
3979
3980 /* Pass the correct signals along.
3981 */
3982 if( p->have_signal ) {
3983 thread_signal = p->signal_value;
3984 p->have_signal = 0;
3985 }
3986 else
3987 thread_signal = 0;
3988
3989 if( p->tid != real_tid ) {
3990 /*
3991 * Not the thread of interest, so continue it
3992 * as the user expects.
3993 */
3994 if( p->stepping_mode == DO_STEP ) {
3995 /* Just step this thread.
3996 */
3997 call_ttrace(
3998 TT_LWP_SINGLE,
3999 p->tid,
4000 TT_USE_CURRENT_PC,
4001 (TTRACE_ARG_TYPE) target_signal_to_host( signal ),
4002 TT_NIL );
4003 }
4004 else {
4005 /* Regular continue (default case).
4006 */
4007 call_ttrace(
4008 TT_LWP_CONTINUE,
4009 p->tid,
4010 TT_USE_CURRENT_PC,
4011 (TTRACE_ARG_TYPE) target_signal_to_host( thread_signal ),
4012 TT_NIL );
4013 }
4014 }
4015 else {
4016 /* Step the thread of interest.
4017 */
4018 call_ttrace(
4019 TT_LWP_SINGLE,
4020 real_tid,
4021 TT_USE_CURRENT_PC,
4022 (TTRACE_ARG_TYPE) target_signal_to_host( signal ),
4023 TT_NIL );
4024 }
4025 } /* Loop over threads */
4026 } /* End threads_continue_all_but_one */
4027
4028 /* Use a loop over the threads to continue all the threads.
4029 * This is done when a signal must be sent to any of the threads.
4030 */
4031 static void
4032 threads_continue_all_with_signals( gdb_tid, signal )
4033 lwpid_t gdb_tid;
4034 int signal;
4035 {
4036 thread_info *p;
4037 int thread_signal;
4038 lwpid_t real_tid;
4039 lwpid_t scan_tid;
4040 ttstate_t state;
4041 int real_pid;
4042
4043 #ifdef THREAD_DEBUG
4044 if( debug_on )
4045 printf( "Using loop over threads to resume with signals\n" );
4046 #endif
4047
4048 /* Scan and update thread list.
4049 */
4050 set_all_unseen();
4051 real_tid = map_from_gdb_tid( gdb_tid );
4052 real_pid = get_pid_for( real_tid );
4053
4054 scan_tid = get_process_first_stopped_thread_id( real_pid, &state );
4055 while ( 0 != scan_tid ) {
4056
4057 #ifdef THREAD_DEBUG
4058 if( debug_on )
4059 if( state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED )
4060 warning( "About to continue non-stopped thread %d\n", scan_tid );
4061 #endif
4062
4063 p = find_thread_info( scan_tid );
4064 if( NULL == p ) {
4065 add_tthread( real_pid, scan_tid );
4066 p = find_thread_info( scan_tid );
4067
4068 /* This is either a newly-created thread or the
4069 * result of a fork; in either case there's no
4070 * actual event to worry about.
4071 */
4072 p->handled = 1;
4073
4074 if( state.tts_event != TTEVT_NONE ) {
4075 /* Oops, do need to worry!
4076 */
4077 warning( "Unexpected thread with \"%s\" event.",
4078 get_printable_name_of_ttrace_event( state.tts_event ));
4079 }
4080 }
4081
4082 #ifdef THREAD_DEBUG
4083 if( debug_on )
4084 if( p->terminated )
4085 printf( "Why are we continuing a dead thread? (1)\n" );
4086 #endif
4087
4088 p->seen = 1;
4089
4090 scan_tid = get_process_next_stopped_thread_id( real_pid, &state );
4091 }
4092
4093 /* Remove unseen threads from our list.
4094 */
4095 update_thread_list();
4096
4097 /* Continue the threads.
4098 */
4099 for( p = thread_head.head; p; p = p->next ) {
4100
4101 /* Sanity check.
4102 */
4103 thread_dropping_event_check( p );
4104
4105 /* Pass the correct signals along.
4106 */
4107 if( p->tid == real_tid ) {
4108 thread_signal = signal;
4109 p->have_signal = 0;
4110 }
4111 else if( p->have_signal ) {
4112 thread_signal = p->signal_value;
4113 p->have_signal = 0;
4114 }
4115 else
4116 thread_signal = 0;
4117
4118 if( p->stepping_mode == DO_STEP ) {
4119 call_ttrace(
4120 TT_LWP_SINGLE,
4121 p->tid,
4122 TT_USE_CURRENT_PC,
4123 (TTRACE_ARG_TYPE) target_signal_to_host( signal ),
4124 TT_NIL );
4125 }
4126 else {
4127 /* Continue this thread (default case).
4128 */
4129 call_ttrace(
4130 TT_LWP_CONTINUE,
4131 p->tid,
4132 TT_USE_CURRENT_PC,
4133 (TTRACE_ARG_TYPE) target_signal_to_host( thread_signal ),
4134 TT_NIL );
4135 }
4136 }
4137 } /* End threads_continue_all_with_signals */
4138
4139 /* Step one thread only.
4140 */
4141 static void
4142 thread_fake_step( tid, signal )
4143 lwpid_t tid;
4144 enum target_signal signal;
4145 {
4146 thread_info *p;
4147
4148 #ifdef THREAD_DEBUG
4149 if( debug_on ) {
4150 printf( "Doing a fake-step over a bpt, etc. for %d\n", tid );
4151
4152 if( is_terminated( tid ))
4153 printf( "Why are we continuing a dead thread? (4)\n" );
4154 }
4155 #endif
4156
4157 if( doing_fake_step )
4158 warning( "Step while step already in progress." );
4159
4160 /* See if there's a saved signal value for this
4161 * thread to be passed on, but no current signal.
4162 */
4163 p = find_thread_info( tid );
4164 if( p != NULL ) {
4165 if( p->have_signal && signal == NULL ) {
4166 /* Pass on a saved signal.
4167 */
4168 signal = p->signal_value;
4169 }
4170
4171 p->have_signal = 0;
4172 }
4173
4174 if( !p->handled )
4175 warning( "Internal error: continuing unhandled thread." );
4176
4177 call_ttrace( TT_LWP_SINGLE,
4178 tid,
4179 TT_USE_CURRENT_PC,
4180 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4181 TT_NIL );
4182
4183 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4184 * for this thread only, and clear any saved signal info.
4185 */
4186 doing_fake_step = 1;
4187 fake_step_tid = tid;
4188
4189 } /* End thread_fake_step */
4190
4191 /* Continue one thread when a signal must be sent to it.
4192 */
4193 static void
4194 threads_continue_one_with_signal( gdb_tid, signal )
4195 lwpid_t gdb_tid;
4196 int signal;
4197 {
4198 thread_info *p;
4199 lwpid_t real_tid;
4200 int real_pid;
4201
4202 #ifdef THREAD_DEBUG
4203 if( debug_on )
4204 printf( "Continuing one thread with a signal\n" );
4205 #endif
4206
4207 real_tid = map_from_gdb_tid( gdb_tid );
4208 real_pid = get_pid_for( real_tid );
4209
4210 p = find_thread_info( real_tid );
4211 if( NULL == p ) {
4212 add_tthread( real_pid, real_tid );
4213 }
4214
4215 #ifdef THREAD_DEBUG
4216 if( debug_on )
4217 if( p->terminated )
4218 printf( "Why are we continuing a dead thread? (2)\n" );
4219 #endif
4220
4221 if( !p->handled )
4222 warning( "Internal error: continuing unhandled thread." );
4223
4224 p->have_signal = 0;
4225
4226 call_ttrace( TT_LWP_CONTINUE,
4227 gdb_tid,
4228 TT_USE_CURRENT_PC,
4229 (TTRACE_ARG_TYPE) target_signal_to_host( signal ),
4230 TT_NIL );
4231 }
4232 #endif
4233
4234 #ifndef CHILD_RESUME
4235
4236 /* Resume execution of the inferior process.
4237 *
4238 * This routine is in charge of setting the "handled" bits.
4239 *
4240 * If STEP is zero, continue it.
4241 * If STEP is nonzero, single-step it.
4242 *
4243 * If SIGNAL is nonzero, give it that signal.
4244 *
4245 * If TID is -1, apply to all threads.
4246 * If TID is not -1, apply to specified thread.
4247 *
4248 * STEP
4249 * \ !0 0
4250 * TID \________________________________________________
4251 * |
4252 * -1 | Step current Continue all threads
4253 * | thread and (but which gets any
4254 * | continue others signal?--We look at
4255 * | "inferior_pid")
4256 * |
4257 * N | Step _this_ thread Continue _this_ thread
4258 * | and leave others and leave others
4259 * | stopped; internally stopped; used only for
4260 * | used by gdb, never hardware watchpoints
4261 * | a user command. and attach, never a
4262 * | user command.
4263 */
4264 void
4265 child_resume( gdb_tid, step, signal )
4266 lwpid_t gdb_tid;
4267 int step;
4268 enum target_signal signal;
4269 {
4270 int resume_all_threads;
4271 lwpid_t tid;
4272 process_state_t new_process_state;
4273
4274 resume_all_threads =
4275 (gdb_tid == INFTTRACE_ALL_THREADS) ||
4276 (vfork_in_flight);
4277
4278 if (resume_all_threads) {
4279 /* Resume all threads, but first pick a tid value
4280 * so we can get the pid when in call_ttrace doing
4281 * the map.
4282 */
4283 if (vfork_in_flight)
4284 tid = vforking_child_pid;
4285 else
4286 tid = map_from_gdb_tid( inferior_pid );
4287 }
4288 else
4289 tid = map_from_gdb_tid( gdb_tid );
4290
4291 #ifdef THREAD_DEBUG
4292 if( debug_on ) {
4293 if( more_events_left )
4294 printf( "More events; " );
4295
4296 if( signal != 0 )
4297 printf( "Sending signal %d; ", signal );
4298
4299 if( resume_all_threads ) {
4300 if( step == 0 )
4301 printf( "Continue process %d\n", tid );
4302 else
4303 printf( "Step/continue thread %d\n", tid );
4304 }
4305 else {
4306 if( step == 0 )
4307 printf( "Continue thread %d\n", tid );
4308 else
4309 printf( "Step just thread %d\n", tid );
4310 }
4311
4312 if( vfork_in_flight )
4313 printf( "Vfork in flight\n" );
4314 }
4315 #endif
4316
4317 if( process_state == RUNNING )
4318 warning( "Internal error in resume logic; doing resume or step anyway." );
4319
4320 if( !step /* Asked to continue... */
4321 && resume_all_threads /* whole process.. */
4322 && signal != 0 /* with a signal... */
4323 && more_events_left > 0 ) { /* but we can't yet--save it! */
4324
4325 /* Continue with signal means we have to set the pending
4326 * signal value for this thread.
4327 */
4328 thread_info *k;
4329
4330 #ifdef THREAD_DEBUG
4331 if( debug_on )
4332 printf( "Saving signal %d for thread %d\n", signal, tid );
4333 #endif
4334
4335 k = find_thread_info( tid );
4336 if( k != NULL ) {
4337 k->have_signal = 1;
4338 k->signal_value = signal;
4339
4340 #ifdef THREAD_DEBUG
4341 if( debug_on )
4342 if( k->terminated )
4343 printf( "Why are we continuing a dead thread? (3)\n" );
4344 #endif
4345
4346 }
4347
4348 #ifdef THREAD_DEBUG
4349 else if( debug_on ) {
4350 printf( "No thread info for tid %d\n", tid );
4351 }
4352 #endif
4353 }
4354
4355 /* Are we faking this "continue" or "step"?
4356 *
4357 * We used to do steps by continuing all the threads for
4358 * which the events had been handled already. While
4359 * conceptually nicer (hides it all in a lower level), this
4360 * can lead to starvation and a hang (e.g. all but one thread
4361 * are unhandled at a breakpoint just before a "join" operation,
4362 * and one thread is in the join, and the user wants to step that
4363 * thread).
4364 */
4365 if( resume_all_threads /* Whole process, therefore user command */
4366 && more_events_left > 0 ) { /* But we can't do this yet--fake it! */
4367 thread_info *p;
4368
4369 if( !step ) {
4370 /* No need to do any notes on a per-thread
4371 * basis--we're done!
4372 */
4373 #ifdef WAIT_BUFFER_DEBUG
4374 if( debug_on )
4375 printf( "Faking a process resume.\n" );
4376 #endif
4377
4378 return;
4379 }
4380 else {
4381
4382 #ifdef WAIT_BUFFER_DEBUG
4383 if( debug_on )
4384 printf( "Faking a process step.\n" );
4385 #endif
4386
4387 }
4388
4389 p = find_thread_info( tid );
4390 if( p == NULL ) {
4391 warning( "No thread information for tid %d, 'next' command ignored.\n", tid );
4392 return;
4393 }
4394 else {
4395
4396 #ifdef THREAD_DEBUG
4397 if( debug_on )
4398 if( p->terminated )
4399 printf( "Why are we continuing a dead thread? (3.5)\n" );
4400 #endif
4401
4402 if( p->stepping_mode != DO_DEFAULT ) {
4403 warning( "Step or continue command applied to thread which is already stepping or continuing; command ignored." );
4404
4405 return;
4406 }
4407
4408 if( step )
4409 p->stepping_mode = DO_STEP;
4410 else
4411 p->stepping_mode = DO_CONTINUE;
4412
4413 return;
4414 } /* Have thread info */
4415 } /* Must fake step or go */
4416
4417 /* Execept for fake-steps, from here on we know we are
4418 * going to wind up with a running process which will
4419 * need a real wait.
4420 */
4421 new_process_state = RUNNING;
4422
4423 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4424 * it was. (If GDB wanted it to start some other way, we have already
4425 * written a new PC value to the child.)
4426 *
4427 * If this system does not support PT_STEP, a higher level function will
4428 * have called single_step() to transmute the step request into a
4429 * continue request (by setting breakpoints on all possible successor
4430 * instructions), so we don't have to worry about that here.
4431 */
4432 if (step) {
4433 if( resume_all_threads ) {
4434 /*
4435 * Regular user step: other threads get a "continue".
4436 */
4437 threads_continue_all_but_one( tid, signal );
4438 clear_all_handled();
4439 clear_all_stepping_mode();
4440 }
4441
4442 else {
4443 /* "Fake step": gdb is stepping one thread over a
4444 * breakpoint, watchpoint, or out of a library load
4445 * event, etc. The rest just stay where they are.
4446 *
4447 * Also used when there are pending events: we really
4448 * step the current thread, but leave the rest stopped.
4449 * Users can't request this, but "wait_for_inferior"
4450 * does--a lot!
4451 */
4452 thread_fake_step( tid, signal );
4453
4454 /* Clear the "handled" state of this thread, because
4455 * we'll soon get a new event for it. Other events
4456 * stay as they were.
4457 */
4458 clear_handled( tid );
4459 clear_stepping_mode( tid );
4460 new_process_state = FAKE_STEPPING;
4461 }
4462 }
4463
4464 else {
4465 /* TT_LWP_CONTINUE can pass signals to threads,
4466 * TT_PROC_CONTINUE can't. So if there are any
4467 * signals to pass, we have to use the (slower)
4468 * loop over the stopped threads.
4469 *
4470 * Equally, if we have to not continue some threads,
4471 * due to saved events, we have to use the loop.
4472 */
4473 if( (signal != 0) || saved_signals_exist()) {
4474 if( resume_all_threads ) {
4475
4476 #ifdef THREAD_DEBUG
4477 if( debug_on )
4478 printf( "Doing a continue by loop of all threads\n" );
4479 #endif
4480
4481 threads_continue_all_with_signals( tid, signal );
4482
4483 clear_all_handled();
4484 clear_all_stepping_mode();
4485 }
4486
4487 else {
4488 #ifdef THREAD_DEBUG
4489 printf( "Doing a continue w/signal of just thread %d\n", tid );
4490 #endif
4491
4492 threads_continue_one_with_signal( tid, signal );
4493
4494 /* Clear the "handled" state of this thread, because
4495 * we'll soon get a new event for it. Other events
4496 * can stay as they were.
4497 */
4498 clear_handled( tid );
4499 clear_stepping_mode( tid );
4500 }
4501 }
4502
4503 else {
4504 /* No signals to send.
4505 */
4506 if( resume_all_threads ) {
4507 #ifdef THREAD_DEBUG
4508 if( debug_on )
4509 printf( "Doing a continue by process of process %d\n", tid );
4510 #endif
4511
4512 if( more_events_left > 0 ) {
4513 warning( "Losing buffered events on continue." );
4514 more_events_left = 0;
4515 }
4516
4517 call_ttrace( TT_PROC_CONTINUE,
4518 tid,
4519 TT_NIL,
4520 TT_NIL,
4521 TT_NIL );
4522
4523 clear_all_handled();
4524 clear_all_stepping_mode();
4525 }
4526
4527 else {
4528 #ifdef THREAD_DEBUG
4529 if( debug_on ) {
4530 printf( "Doing a continue of just thread %d\n", tid );
4531 if( is_terminated( tid ))
4532 printf( "Why are we continuing a dead thread? (5)\n" );
4533 }
4534 #endif
4535
4536 call_ttrace( TT_LWP_CONTINUE,
4537 tid,
4538 TT_NIL,
4539 TT_NIL,
4540 TT_NIL );
4541
4542 /* Clear the "handled" state of this thread, because
4543 * we'll soon get a new event for it. Other events
4544 * can stay as they were.
4545 */
4546 clear_handled( tid );
4547 clear_stepping_mode( tid );
4548 }
4549 }
4550 }
4551
4552 process_state = new_process_state;
4553
4554 #ifdef WAIT_BUFFER_DEBUG
4555 if( debug_on )
4556 printf( "Process set to %s\n",
4557 get_printable_name_of_process_state (process_state) );
4558 #endif
4559
4560 }
4561 #endif /* CHILD_RESUME */
4562
4563 \f
4564 #ifdef ATTACH_DETACH
4565 /*
4566 * Like it says.
4567 *
4568 * One worry is that we may not be attaching to "inferior_pid"
4569 * and thus may not want to clear out our data. FIXME?
4570 *
4571 */
4572 static void
4573 update_thread_state_after_attach( pid, kind_of_go )
4574 int pid;
4575 attach_continue_t kind_of_go;
4576 {
4577 int tt_status;
4578 ttstate_t thread_state;
4579 lwpid_t a_thread;
4580 lwpid_t tid;
4581
4582 /* The process better be stopped.
4583 */
4584 if( process_state != STOPPED
4585 && process_state != VFORKING )
4586 warning( "Internal error attaching." );
4587
4588 /* Clear out old tthread info and start over. This has the
4589 * side effect of ensuring that the TRAP is reported as being
4590 * in the right thread (re-mapped from tid to pid).
4591 *
4592 * It's because we need to add the tthread _now_ that we
4593 * need to call "clear_thread_info" _now_, and that's why
4594 * "require_notification_of_events" doesn't clear the thread
4595 * info (it's called later than this routine).
4596 */
4597 clear_thread_info();
4598 a_thread = 0;
4599
4600 for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4601 tid != 0;
4602 tid = get_process_next_stopped_thread_id (pid, &thread_state))
4603 {
4604 thread_info *p;
4605
4606 if (a_thread == 0)
4607 {
4608 a_thread = tid;
4609 #ifdef THREAD_DEBUG
4610 if( debug_on )
4611 printf( "Attaching to process %d, thread %d\n",
4612 pid, a_thread );
4613 #endif
4614 }
4615
4616 /* Tell ourselves and the "rest of gdb" that this thread
4617 * exists.
4618 *
4619 * This isn't really a hack. Other thread-based versions
4620 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4621 *
4622 * We don't need to do mapping here, as we know this
4623 * is the first thread and thus gets the real pid
4624 * (and is "inferior_pid").
4625 *
4626 * NOTE: it probably isn't the originating thread,
4627 * but that doesn't matter (we hope!).
4628 */
4629 add_tthread( pid, tid );
4630 p = find_thread_info( tid );
4631 if( NULL == p ) /* ?We just added it! */
4632 error( "Internal error adding a thread on attach." );
4633
4634 copy_ttstate_t( &p->last_stop_state, thread_state );
4635 p->have_state = 1;
4636
4637 if( DO_ATTACH_CONTINUE == kind_of_go ) {
4638 /*
4639 * If we are going to CONTINUE afterwards,
4640 * raising a SIGTRAP, don't bother trying to
4641 * handle this event. But check first!
4642 */
4643 switch( p->last_stop_state.tts_event ) {
4644
4645 case TTEVT_NONE:
4646 /* Ok to set this handled.
4647 */
4648 break;
4649
4650 default:
4651 warning( "Internal error; skipping event %s on process %d, thread %d.",
4652 get_printable_name_of_ttrace_event(
4653 p->last_stop_state.tts_event ),
4654 p->pid, p->tid);
4655 }
4656
4657 set_handled( pid, tid );
4658
4659 }
4660 else {
4661 /* There will be no "continue" opertion, so the
4662 * process remains stopped. Don't set any events
4663 * handled except the "gimmies".
4664 */
4665 switch( p->last_stop_state.tts_event ) {
4666
4667 case TTEVT_NONE:
4668 /* Ok to ignore this.
4669 */
4670 set_handled( pid, tid );
4671 break;
4672
4673 case TTEVT_EXEC:
4674 case TTEVT_FORK:
4675 /* Expected "other" FORK or EXEC event from a
4676 * fork or vfork.
4677 */
4678 break;
4679
4680 default:
4681 printf( "Internal error: failed to handle event %s on process %d, thread %d.",
4682 get_printable_name_of_ttrace_event(
4683 p->last_stop_state.tts_event ),
4684 p->pid, p->tid);
4685 }
4686 }
4687
4688 add_thread( tid ); /* in thread.c */
4689 }
4690
4691 #ifdef PARANOIA
4692 if( debug_on )
4693 print_tthreads();
4694 #endif
4695
4696 /* One mustn't call ttrace_wait() after attaching via ttrace,
4697 'cause the process is stopped already.
4698
4699 However, the upper layers of gdb's execution control will
4700 want to wait after attaching (but not after forks, in
4701 which case they will be doing a "target_resume", anticipating
4702 a later TTEVT_EXEC or TTEVT_FORK event).
4703
4704 To make this attach() implementation more compatible with
4705 others, we'll make the attached-to process raise a SIGTRAP.
4706
4707 Issue: this continues only one thread. That could be
4708 dangerous if the thread is blocked--the process won't run
4709 and no trap will be raised. FIX! (check state.tts_flags?
4710 need one that's either TTS_WASRUNNING--but we've stopped
4711 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4712 */
4713 if( DO_ATTACH_CONTINUE == kind_of_go ) {
4714 tt_status = call_real_ttrace(
4715 TT_LWP_CONTINUE,
4716 pid,
4717 a_thread,
4718 TT_USE_CURRENT_PC,
4719 (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4720 TT_NIL);
4721 if (errno)
4722 perror_with_name ("ttrace");
4723
4724 clear_handled( a_thread ); /* So TRAP will be reported. */
4725
4726 /* Now running.
4727 */
4728 process_state = RUNNING;
4729 }
4730
4731 attach_flag = 1;
4732 }
4733 #endif /* ATTACH_DETACH */
4734
4735 \f
4736 #ifdef ATTACH_DETACH
4737 /* Start debugging the process whose number is PID.
4738 * (A _real_ pid).
4739 */
4740 int
4741 attach( pid )
4742 int pid;
4743 {
4744 int tt_status;
4745
4746 tt_status = call_real_ttrace (
4747 TT_PROC_ATTACH,
4748 pid,
4749 (lwpid_t) TT_NIL,
4750 TT_NIL,
4751 (TTRACE_ARG_TYPE) TT_VERSION,
4752 TT_NIL);
4753 if (errno)
4754 perror_with_name ("ttrace attach");
4755
4756 /* If successful, the process is now stopped.
4757 */
4758 process_state = STOPPED;
4759
4760 /* Our caller ("attach_command" in "infcmd.c")
4761 * expects to do a "wait_for_inferior" after
4762 * the attach, so make sure the inferior is
4763 * running when we're done.
4764 */
4765 update_thread_state_after_attach( pid, DO_ATTACH_CONTINUE );
4766
4767 return pid;
4768 }
4769
4770
4771 #if defined(CHILD_POST_ATTACH)
4772 void
4773 child_post_attach (pid)
4774 int pid;
4775 {
4776 #ifdef THREAD_DEBUG
4777 if( debug_on )
4778 printf( "child-post-attach call\n" );
4779 #endif
4780
4781 require_notification_of_events (pid);
4782 }
4783 #endif
4784
4785
4786 /* Stop debugging the process whose number is PID
4787 and continue it with signal number SIGNAL.
4788 SIGNAL = 0 means just continue it.
4789 */
4790 void
4791 detach( signal )
4792 int signal;
4793 {
4794 errno = 0;
4795 call_ttrace (TT_PROC_DETACH,
4796 inferior_pid,
4797 TT_NIL,
4798 (TTRACE_ARG_TYPE) signal,
4799 TT_NIL);
4800 attach_flag = 0;
4801
4802 clear_thread_info();
4803
4804 /* Process-state? */
4805 }
4806 #endif /* ATTACH_DETACH */
4807
4808 \f
4809 /* Default the type of the ttrace transfer to int. */
4810 #ifndef TTRACE_XFER_TYPE
4811 #define TTRACE_XFER_TYPE int
4812 #endif
4813
4814 void
4815 _initialize_kernel_u_addr ()
4816 {
4817 }
4818
4819 #if !defined (CHILD_XFER_MEMORY)
4820 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
4821 in the NEW_SUN_TTRACE case.
4822 It ought to be straightforward. But it appears that writing did
4823 not write the data that I specified. I cannot understand where
4824 it got the data that it actually did write. */
4825
4826 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
4827 to debugger memory starting at MYADDR. Copy to inferior if
4828 WRITE is nonzero.
4829
4830 Returns the length copied, which is either the LEN argument or zero.
4831 This xfer function does not do partial moves, since child_ops
4832 doesn't allow memory operations to cross below us in the target stack
4833 anyway. */
4834
4835 int
4836 child_xfer_memory (memaddr, myaddr, len, write, target)
4837 CORE_ADDR memaddr;
4838 char *myaddr;
4839 int len;
4840 int write;
4841 struct target_ops *target; /* ignored */
4842 {
4843 register int i;
4844 /* Round starting address down to longword boundary. */
4845 register CORE_ADDR addr = memaddr & - sizeof (TTRACE_XFER_TYPE);
4846 /* Round ending address up; get number of longwords that makes. */
4847 register int count
4848 = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
4849 / sizeof (TTRACE_XFER_TYPE);
4850 /* Allocate buffer of that many longwords. */
4851 register TTRACE_XFER_TYPE *buffer
4852 = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
4853
4854 if (write)
4855 {
4856 /* Fill start and end extra bytes of buffer with existing memory data. */
4857
4858 if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE)) {
4859 /* Need part of initial word -- fetch it. */
4860 buffer[0] = call_ttrace (TT_LWP_RDTEXT,
4861 inferior_pid,
4862 (TTRACE_ARG_TYPE) addr,
4863 TT_NIL,
4864 TT_NIL);
4865 }
4866
4867 if (count > 1) /* FIXME, avoid if even boundary */
4868 {
4869 buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
4870 inferior_pid,
4871 ((TTRACE_ARG_TYPE)
4872 (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
4873 TT_NIL,
4874 TT_NIL);
4875 }
4876
4877 /* Copy data to be written over corresponding part of buffer */
4878
4879 memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
4880 myaddr,
4881 len);
4882
4883 /* Write the entire buffer. */
4884
4885 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4886 {
4887 errno = 0;
4888 call_ttrace (TT_LWP_WRDATA,
4889 inferior_pid,
4890 (TTRACE_ARG_TYPE) addr,
4891 (TTRACE_ARG_TYPE) buffer[i],
4892 TT_NIL);
4893 if (errno)
4894 {
4895 /* Using the appropriate one (I or D) is necessary for
4896 Gould NP1, at least. */
4897 errno = 0;
4898 call_ttrace (TT_LWP_WRTEXT,
4899 inferior_pid,
4900 (TTRACE_ARG_TYPE) addr,
4901 (TTRACE_ARG_TYPE) buffer[i],
4902 TT_NIL);
4903 }
4904 if (errno)
4905 return 0;
4906 }
4907 }
4908 else
4909 {
4910 /* Read all the longwords */
4911 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
4912 {
4913 errno = 0;
4914 buffer[i] = call_ttrace (TT_LWP_RDTEXT,
4915 inferior_pid,
4916 (TTRACE_ARG_TYPE) addr,
4917 TT_NIL,
4918 TT_NIL);
4919 if (errno)
4920 return 0;
4921 QUIT;
4922 }
4923
4924 /* Copy appropriate bytes out of the buffer. */
4925 memcpy (myaddr,
4926 (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
4927 len);
4928 }
4929 return len;
4930 }
4931
4932 \f
4933 static void
4934 udot_info ()
4935 {
4936 int udot_off; /* Offset into user struct */
4937 int udot_val; /* Value from user struct at udot_off */
4938 char mess[128]; /* For messages */
4939
4940 if (!target_has_execution)
4941 {
4942 error ("The program is not being run.");
4943 }
4944
4945 #if !defined (KERNEL_U_SIZE)
4946
4947 /* Adding support for this command is easy. Typically you just add a
4948 routine, called "kernel_u_size" that returns the size of the user
4949 struct, to the appropriate *-nat.c file and then add to the native
4950 config file "#define KERNEL_U_SIZE kernel_u_size()" */
4951 error ("Don't know how large ``struct user'' is in this version of gdb.");
4952
4953 #else
4954
4955 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
4956 {
4957 if ((udot_off % 24) == 0)
4958 {
4959 if (udot_off > 0)
4960 {
4961 printf_filtered ("\n");
4962 }
4963 printf_filtered ("%04x:", udot_off);
4964 }
4965 udot_val = call_ttrace (TT_LWP_RUREGS,
4966 inferior_pid,
4967 (TTRACE_ARG_TYPE) udot_off,
4968 TT_NIL,
4969 TT_NIL);
4970 if (errno != 0)
4971 {
4972 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
4973 perror_with_name (mess);
4974 }
4975 /* Avoid using nonportable (?) "*" in print specs */
4976 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
4977 }
4978 printf_filtered ("\n");
4979
4980 #endif
4981 }
4982 #endif /* !defined (CHILD_XFER_MEMORY). */
4983
4984 /* TTrace version of "target_pid_to_exec_file"
4985 */
4986 char *
4987 child_pid_to_exec_file (tid)
4988 int tid;
4989 {
4990 static char exec_file_buffer[1024];
4991 int tt_status;
4992 CORE_ADDR top_of_stack;
4993 char four_chars[4];
4994 int name_index;
4995 int i;
4996 int done;
4997 int saved_inferior_pid;
4998
4999 /* As of 10.x HP-UX, there's an explicit request to get the
5000 *pathname.
5001 */
5002 tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5003 tid,
5004 (TTRACE_ARG_TYPE) exec_file_buffer,
5005 (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
5006 TT_NIL);
5007 if (tt_status >= 0)
5008 return exec_file_buffer;
5009
5010 /* ??rehrauer: The above request may or may not be broken. It
5011 doesn't seem to work when I use it. But, it may be designed
5012 to only work immediately after an exec event occurs. (I'm
5013 waiting for COSL to explain.)
5014
5015 In any case, if it fails, try a really, truly amazingly gross
5016 hack that DDE uses, of pawing through the process' data
5017 segment to find the pathname.
5018 */
5019 top_of_stack = 0x7b03a000;
5020 name_index = 0;
5021 done = 0;
5022
5023 /* On the chance that pid != inferior_pid, set inferior_pid
5024 to pid, so that (grrrr!) implicit uses of inferior_pid get
5025 the right id.
5026 */
5027 saved_inferior_pid = inferior_pid;
5028 inferior_pid = tid;
5029
5030 /* Try to grab a null-terminated string. */
5031 while (! done) {
5032 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
5033 {
5034 inferior_pid = saved_inferior_pid;
5035 return NULL;
5036 }
5037 for (i = 0; i < 4; i++) {
5038 exec_file_buffer[name_index++] = four_chars[i];
5039 done = (four_chars[i] == '\0');
5040 if (done)
5041 break;
5042 }
5043 top_of_stack += 4;
5044 }
5045
5046 if (exec_file_buffer[0] == '\0')
5047 {
5048 inferior_pid = saved_inferior_pid;
5049 return NULL;
5050 }
5051
5052 inferior_pid = saved_inferior_pid;
5053 return exec_file_buffer;
5054 }
5055
5056
5057 void
5058 pre_fork_inferior ()
5059 {
5060 int status;
5061
5062 status = pipe (startup_semaphore.parent_channel);
5063 if (status < 0) {
5064 warning ("error getting parent pipe for startup semaphore");
5065 return;
5066 }
5067
5068 status = pipe (startup_semaphore.child_channel);
5069 if (status < 0) {
5070 warning ("error getting child pipe for startup semaphore");
5071 return;
5072 }
5073 }
5074
5075 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5076 * ultimately from "follow_inferior_fork" in infrun.c,
5077 * itself called from "resume".
5078 *
5079 * This seems to be intended to attach after a fork or
5080 * vfork, while "attach" is used to attach to a pid
5081 * given by the user. The check for an existing attach
5082 * seems odd--it always fails in our test system.
5083 */
5084 int
5085 hppa_require_attach (pid)
5086 int pid;
5087 {
5088 int tt_status;
5089 CORE_ADDR pc;
5090 CORE_ADDR pc_addr;
5091 unsigned int regs_offset;
5092 process_state_t old_process_state = process_state;
5093
5094 /* Are we already attached? There appears to be no explicit
5095 * way to answer this via ttrace, so we try something which
5096 * should be innocuous if we are attached. If that fails,
5097 * then we assume we're not attached, and so attempt to make
5098 * it so.
5099 */
5100 errno = 0;
5101 tt_status = call_real_ttrace (TT_PROC_STOP,
5102 pid,
5103 (lwpid_t) TT_NIL,
5104 (TTRACE_ARG_TYPE) TT_NIL,
5105 (TTRACE_ARG_TYPE) TT_NIL,
5106 TT_NIL);
5107
5108 if (errno)
5109 {
5110 /* No change to process-state!
5111 */
5112 errno = 0;
5113 pid = attach (pid);
5114 }
5115 else
5116 {
5117 /* If successful, the process is now stopped. But if
5118 * we're VFORKING, the parent is still running, so don't
5119 * change the process state.
5120 */
5121 if( process_state != VFORKING )
5122 process_state = STOPPED;
5123
5124 /* If we were already attached, you'd think that we
5125 * would need to start going again--but you'd be wrong,
5126 * as the fork-following code is actually in the middle
5127 * of the "resume" routine in in "infrun.c" and so
5128 * will (almost) immediately do a resume.
5129 *
5130 * On the other hand, if we are VFORKING, which means
5131 * that the child and the parent share a process for a
5132 * while, we know that "resume" won't be resuming
5133 * until the child EXEC event is seen. But we still
5134 * don't want to continue, as the event is already
5135 * there waiting.
5136 */
5137 update_thread_state_after_attach( pid, DONT_ATTACH_CONTINUE );
5138 } /* STOP succeeded */
5139
5140 return pid;
5141 }
5142
5143 int
5144 hppa_require_detach (pid, signal)
5145 int pid;
5146 int signal;
5147 {
5148 int tt_status;
5149
5150 /* If signal is non-zero, we must pass the signal on to the active
5151 thread prior to detaching. We do this by continuing the threads
5152 with the signal.
5153 */
5154 if (signal != 0)
5155 {
5156 errno = 0;
5157 threads_continue_all_with_signals( pid, signal );
5158 }
5159
5160 errno = 0;
5161 tt_status = call_ttrace (TT_PROC_DETACH,
5162 pid,
5163 TT_NIL,
5164 TT_NIL,
5165 TT_NIL);
5166
5167 errno = 0; /* Ignore any errors. */
5168
5169 /* process_state? */
5170
5171 return pid;
5172 }
5173
5174 #if defined(HPPA_GET_PROCESS_EVENTS)
5175 process_event_vector
5176 hppa_get_process_events (pid, wait_status, must_continue_pid_after)
5177 int pid;
5178 int wait_status;
5179 int * must_continue_pid_after;
5180 {
5181 int tt_status;
5182 ttstate_t ttrace_state;
5183 process_event_vector events = PEVT_NONE;
5184
5185 /* This is always 1 with ptrace. */
5186 *must_continue_pid_after = 0;
5187
5188 errno = 0;
5189 tt_status = call_ttrace (TT_LWP_GET_STATE,
5190 pid,
5191 (TTRACE_ARG_TYPE) &ttrace_state,
5192 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
5193 TT_NIL);
5194 if (errno)
5195 perror_with_name ("ttrace");
5196 if (tt_status < 0)
5197 return 0;
5198
5199 if (ttrace_state.tts_event & TTEVT_SIGNAL)
5200 events |= PEVT_SIGNAL;
5201 if (ttrace_state.tts_event & TTEVT_FORK)
5202 events |= PEVT_FORK;
5203 if (ttrace_state.tts_event & TTEVT_VFORK)
5204 events |= PEVT_VFORK;
5205 if (ttrace_state.tts_event & TTEVT_EXEC)
5206 events |= PEVT_EXEC;
5207 if (ttrace_state.tts_event & TTEVT_EXIT)
5208 events |= PEVT_EXIT;
5209
5210 return events;
5211 }
5212 #endif /* HPPA_GET_PROCESS_EVENTS */
5213
5214
5215 /* Given the starting address of a memory page, hash it to a bucket in
5216 the memory page dictionary.
5217 */
5218 static int
5219 get_dictionary_bucket_of_page (page_start)
5220 CORE_ADDR page_start;
5221 {
5222 int hash;
5223
5224 hash = (page_start / memory_page_dictionary.page_size);
5225 hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5226
5227 return hash;
5228 }
5229
5230
5231 /* Given a memory page's starting address, get (i.e., find an existing
5232 or create a new) dictionary entry for the page. The page will be
5233 write-protected when this function returns, but may have a reference
5234 count of 0 (if the page was newly-added to the dictionary).
5235 */
5236 static memory_page_t *
5237 get_dictionary_entry_of_page (pid, page_start)
5238 int pid;
5239 CORE_ADDR page_start;
5240 {
5241 int bucket;
5242 memory_page_t * page = NULL;
5243 memory_page_t * previous_page = NULL;
5244
5245 /* We're going to be using the dictionary now, than-kew. */
5246 require_memory_page_dictionary (pid);
5247
5248 /* Try to find an existing dictionary entry for this page. Hash
5249 on the page's starting address.
5250 */
5251 bucket = get_dictionary_bucket_of_page (page_start);
5252 page = &memory_page_dictionary.buckets[bucket];
5253 while (page != NULL)
5254 {
5255 if (page->page_start == page_start)
5256 break;
5257 previous_page = page;
5258 page = page->next;
5259 }
5260
5261 /* Did we find a dictionary entry for this page? If not, then
5262 add it to the dictionary now.
5263 */
5264 if (page == NULL)
5265 {
5266 /* Create a new entry. */
5267 page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5268 page->page_start = page_start;
5269 page->reference_count = 0;
5270 page->next = NULL;
5271 page->previous = NULL;
5272
5273 /* We'll write-protect the page now, if that's allowed. */
5274 page->original_permissions = write_protect_page (pid, page_start);
5275
5276 /* Add the new entry to the dictionary. */
5277 page->previous = previous_page;
5278 previous_page->next = page;
5279
5280 memory_page_dictionary.page_count++;
5281 }
5282
5283 return page;
5284 }
5285
5286
5287 static void
5288 remove_dictionary_entry_of_page (pid, page)
5289 int pid;
5290 memory_page_t * page;
5291 {
5292 /* Restore the page's original permissions. */
5293 unwrite_protect_page (pid, page->page_start, page->original_permissions);
5294
5295 /* Kick the page out of the dictionary. */
5296 if (page->previous != NULL)
5297 page->previous->next = page->next;
5298 if (page->next != NULL)
5299 page->next->previous = page->previous;
5300
5301 /* Just in case someone retains a handle to this after it's freed. */
5302 page->page_start = (CORE_ADDR) 0;
5303
5304 memory_page_dictionary.page_count--;
5305
5306 free (page);
5307 }
5308
5309
5310 static void
5311 hppa_enable_syscall_events (pid)
5312 int pid;
5313 {
5314 int tt_status;
5315 ttevent_t ttrace_events;
5316
5317 /* Get the set of events that are currently enabled. */
5318 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5319 pid,
5320 (TTRACE_ARG_TYPE) &ttrace_events,
5321 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5322 TT_NIL);
5323 if (errno)
5324 perror_with_name ("ttrace");
5325
5326 /* Add syscall events to that set. */
5327 ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5328 ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5329
5330 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5331 pid,
5332 (TTRACE_ARG_TYPE) &ttrace_events,
5333 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5334 TT_NIL);
5335 if (errno)
5336 perror_with_name ("ttrace");
5337 }
5338
5339
5340 static void
5341 hppa_disable_syscall_events (pid)
5342 int pid;
5343 {
5344 int tt_status;
5345 ttevent_t ttrace_events;
5346
5347 /* Get the set of events that are currently enabled. */
5348 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5349 pid,
5350 (TTRACE_ARG_TYPE) &ttrace_events,
5351 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5352 TT_NIL);
5353 if (errno)
5354 perror_with_name ("ttrace");
5355
5356 /* Remove syscall events from that set. */
5357 ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5358 ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5359
5360 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5361 pid,
5362 (TTRACE_ARG_TYPE) &ttrace_events,
5363 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5364 TT_NIL);
5365 if (errno)
5366 perror_with_name ("ttrace");
5367 }
5368
5369
5370 /* The address range beginning with START and ending with START+LEN-1
5371 (inclusive) is to be watched via page-protection by a new watchpoint.
5372 Set protection for all pages that overlap that range.
5373
5374 Note that our caller sets TYPE to:
5375 0 for a bp_hardware_watchpoint,
5376 1 for a bp_read_watchpoint,
5377 2 for a bp_access_watchpoint
5378
5379 (Yes, this is intentionally (though lord only knows why) different
5380 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5381 */
5382 int
5383 hppa_insert_hw_watchpoint (pid, start, len, type)
5384 int pid;
5385 CORE_ADDR start;
5386 LONGEST len;
5387 int type;
5388 {
5389 CORE_ADDR page_start;
5390 int dictionary_was_empty;
5391 int page_size;
5392 int page_id;
5393 LONGEST range_size_in_pages;
5394
5395 if (type != 0)
5396 error ("read or access hardware watchpoints not supported on HP-UX");
5397
5398 /* Examine all pages in the address range. */
5399 require_memory_page_dictionary ();
5400
5401 dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5402
5403 page_size = memory_page_dictionary.page_size;
5404 page_start = (start / page_size) * page_size;
5405 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5406
5407 for (page_id=0; page_id < range_size_in_pages; page_id++, page_start+=page_size)
5408 {
5409 memory_page_t * page;
5410
5411 /* This gets the page entered into the dictionary if it was
5412 not already entered.
5413 */
5414 page = get_dictionary_entry_of_page (pid, page_start);
5415 page->reference_count++;
5416 }
5417
5418 /* Our implementation depends on seeing calls to kernel code, for the
5419 following reason. Here we ask to be notified of syscalls.
5420
5421 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5422 Fine.
5423
5424 But when kernel code accesses the page, it doesn't give a SIGBUS.
5425 Rather, the system call that touched the page fails, with errno=EFAULT.
5426 Not good for us.
5427
5428 We could accomodate this "feature" by asking to be notified of syscall
5429 entries & exits; upon getting an entry event, disabling page-protections;
5430 upon getting an exit event, reenabling page-protections and then checking
5431 if any watchpoints triggered.
5432
5433 However, this turns out to be a real performance loser. syscalls are
5434 usually a frequent occurrence. Having to unprotect-reprotect all watched
5435 pages, and also to then read all watched memory locations and compare for
5436 triggers, can be quite expensive.
5437
5438 Instead, we'll only ask to be notified of syscall exits. When we get
5439 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5440 we can just continue the inferior.
5441
5442 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5443 hackish stuff to determine whether the failure really was due to a
5444 page-protect trap on a watched location.
5445 */
5446 if (dictionary_was_empty)
5447 hppa_enable_syscall_events (pid);
5448 }
5449
5450
5451 /* The address range beginning with START and ending with START+LEN-1
5452 (inclusive) was being watched via page-protection by a watchpoint
5453 which has been removed. Remove protection for all pages that
5454 overlap that range, which are not also being watched by other
5455 watchpoints.
5456 */
5457 int
5458 hppa_remove_hw_watchpoint (pid, start, len, type)
5459 int pid;
5460 CORE_ADDR start;
5461 LONGEST len;
5462 enum bptype type;
5463 {
5464 CORE_ADDR page_start;
5465 int dictionary_is_empty;
5466 int page_size;
5467 int page_id;
5468 LONGEST range_size_in_pages;
5469
5470 if (type != 0)
5471 error ("read or access hardware watchpoints not supported on HP-UX");
5472
5473 /* Examine all pages in the address range. */
5474 require_memory_page_dictionary ();
5475
5476 page_size = memory_page_dictionary.page_size;
5477 page_start = (start / page_size) * page_size;
5478 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5479
5480 for (page_id=0; page_id < range_size_in_pages; page_id++, page_start+=page_size)
5481 {
5482 memory_page_t * page;
5483
5484 page = get_dictionary_entry_of_page (pid, page_start);
5485 page->reference_count--;
5486
5487 /* Was this the last reference of this page? If so, then we
5488 must scrub the entry from the dictionary, and also restore
5489 the page's original permissions.
5490 */
5491 if (page->reference_count == 0)
5492 remove_dictionary_entry_of_page (pid, page);
5493 }
5494
5495 dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5496
5497 /* If write protections are currently disallowed, then that implies that
5498 wait_for_inferior believes that the inferior is within a system call.
5499 Since we want to see both syscall entry and return, it's clearly not
5500 good to disable syscall events in this state!
5501
5502 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5503 "inferior is between syscall events now". Oh well.
5504 */
5505 if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5506 hppa_disable_syscall_events (pid);
5507 }
5508
5509
5510 /* Could we implement a watchpoint of this type via our available
5511 hardware support?
5512
5513 This query does not consider whether a particular address range
5514 could be so watched, but just whether support is generally available
5515 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5516 query that answers whether a particular range should be watched via
5517 hardware support.
5518 */
5519 int
5520 hppa_can_use_hw_watchpoint (type, cnt, ot)
5521 enum bptype type;
5522 int cnt;
5523 enum bptype ot;
5524 {
5525 return (type == bp_hardware_watchpoint);
5526 }
5527
5528
5529 /* Assuming we could set a hardware watchpoint on this address, do
5530 we think it would be profitable ("a good idea") to do so? If not,
5531 we can always set a regular (aka single-step & test) watchpoint
5532 on the address...
5533 */
5534 int
5535 hppa_range_profitable_for_hw_watchpoint (pid, start, len)
5536 int pid;
5537 CORE_ADDR start;
5538 LONGEST len;
5539 {
5540 int range_is_stack_based;
5541 int range_is_accessible;
5542 CORE_ADDR page_start;
5543 int page_size;
5544 int page;
5545 LONGEST range_size_in_pages;
5546
5547 /* ??rehrauer: For now, say that all addresses are potentially
5548 profitable. Possibly later we'll want to test the address
5549 for "stackness"?
5550 */
5551 range_is_stack_based = 0;
5552
5553 /* If any page in the range is inaccessible, then we cannot
5554 really use hardware watchpointing, even though our client
5555 thinks we can. In that case, it's actually an error to
5556 attempt to use hw watchpoints, so we'll tell our client
5557 that the range is "unprofitable", and hope that they listen...
5558 */
5559 range_is_accessible = 1; /* Until proven otherwise. */
5560
5561 /* Examine all pages in the address range. */
5562 errno = 0;
5563 page_size = sysconf (_SC_PAGE_SIZE);
5564
5565 /* If we can't determine page size, we're hosed. Tell our
5566 client it's unprofitable to use hw watchpoints for this
5567 range.
5568 */
5569 if (errno || (page_size <= 0))
5570 {
5571 errno = 0;
5572 return 0;
5573 }
5574
5575 page_start = (start / page_size) * page_size;
5576 range_size_in_pages = len / (LONGEST)page_size;
5577
5578 for (page=0; page < range_size_in_pages; page++, page_start+=page_size)
5579 {
5580 int tt_status;
5581 int page_permissions;
5582
5583 /* Is this page accessible? */
5584 errno = 0;
5585 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5586 pid,
5587 (TTRACE_ARG_TYPE) page_start,
5588 TT_NIL,
5589 (TTRACE_ARG_TYPE) &page_permissions);
5590 if (errno || (tt_status < 0))
5591 {
5592 errno = 0;
5593 range_is_accessible = 0;
5594 break;
5595 }
5596
5597 /* Yes, go for another... */
5598 }
5599
5600 return (! range_is_stack_based && range_is_accessible);
5601 }
5602
5603
5604 char *
5605 hppa_pid_or_tid_to_str (id)
5606 pid_t id;
5607 {
5608 static char buf[100]; /* Static because address returned. */
5609
5610 /* Does this appear to be a process? If so, print it that way. */
5611 if (is_process_id (id))
5612 return hppa_pid_to_str (id);
5613
5614 /* Else, print both the GDB thread number and the system thread id. */
5615 sprintf (buf, "thread %d (", pid_to_thread_id (id));
5616 strcat (buf, hppa_tid_to_str (id));
5617 strcat (buf, ")\0");
5618
5619 return buf;
5620 }
5621
5622 \f
5623 /* If the current pid is not the pid this module reported
5624 * from "proc_wait" with the most recent event, then the
5625 * user has switched threads.
5626 *
5627 * If the last reported event was a breakpoint, then return
5628 * the old thread id, else return 0.
5629 */
5630 pid_t
5631 hppa_switched_threads( gdb_pid )
5632 pid_t gdb_pid;
5633 {
5634 if( gdb_pid == old_gdb_pid ) {
5635 /*
5636 * Core gdb is working with the same pid that it
5637 * was before we reported the last event. This
5638 * is ok: e.g. we reported hitting a thread-specific
5639 * breakpoint, but we were reporting the wrong
5640 * thread, so the core just ignored the event.
5641 *
5642 * No thread switch has happened.
5643 */
5644 return (pid_t) 0;
5645 }
5646 else if( gdb_pid == reported_pid ) {
5647 /*
5648 * Core gdb is working with the pid we reported, so
5649 * any continue or step will be able to figure out
5650 * that it needs to step over any hit breakpoints
5651 * without our (i.e. PREPARE_TO_PROCEED's) help.
5652 */
5653 return (pid_t) 0;
5654 }
5655 else if( !reported_bpt ) {
5656 /*
5657 * The core switched, but we didn't just report a
5658 * breakpoint, so there's no just-hit breakpoint
5659 * instruction at "reported_pid"'s PC, and thus there
5660 * is no need to step over it.
5661 */
5662 return (pid_t) 0;
5663 }
5664 else {
5665 /* There's been a real switch, and we reported
5666 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5667 * know, so it can see whether the breakpoint is
5668 * still active.
5669 */
5670 return reported_pid;
5671 }
5672
5673 /* Keep compiler happy with an obvious return at the end.
5674 */
5675 return (pid_t) 0;
5676 }
5677
5678 void
5679 hppa_ensure_vforking_parent_remains_stopped (pid)
5680 int pid;
5681 {
5682 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5683 must do real work.
5684 */
5685 }
5686
5687
5688 int
5689 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5690 {
5691 return 0; /* No, the parent vfork is available now. */
5692 }
5693
5694
5695 \f
5696 void
5697 _initialize_infttrace ()
5698 {
5699 /* Initialize the ttrace-based hardware watchpoint implementation. */
5700 memory_page_dictionary.page_count = (LONGEST) -1;
5701 memory_page_dictionary.page_protections_allowed = 1;
5702
5703 errno = 0;
5704 memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5705
5706 if (errno || (memory_page_dictionary.page_size <= 0))
5707 perror_with_name ("sysconf");
5708 }
5709
This page took 0.147591 seconds and 4 git commands to generate.