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