]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/infttrace.c
import gdb-1999-07-07 post reformat
[thirdparty/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 (
1407 TT_PROC_GET_FIRST_LWP_STATE,
1408 (pid_t) pid,
1409 (lwpid_t) TT_NIL,
1410 (TTRACE_ARG_TYPE) thread_state,
1411 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1412 TT_NIL);
1413
1414 if (errno)
1415 {
1416 if (errno == EPROTO)
1417 {
1418 /* This is an error we can handle: there isn't any stopped
1419 * thread. This happens when we're re-starting the application
1420 * and it has only one thread. GET_NEXT handles the case of
1421 * no more stopped threads well; GET_FIRST doesn't. (A ttrace
1422 * "feature".)
1423 */
1424 tt_status = 1;
1425 errno = 0;
1426 return 0;
1427 }
1428 else
1429 perror_with_name ("ttrace");
1430 }
1431
1432 if (tt_status < 0)
1433 /* Failed somehow.
1434 */
1435 return 0;
1436
1437 return thread_state->tts_lwpid;
1438 }
1439 \f
1440
1441 /* This function returns the ID of the "next" kernel thread in a
1442 stopped state, or 0 if there are none. "Next" refers to the
1443 thread following that of the last successful call to this
1444 function or to get_process_first_stopped_thread_id, using
1445 the value of thread_state returned by that call.
1446
1447 This function can be used with get_process_first_stopped_thread_id
1448 to iterate over the IDs of all stopped threads of this process.
1449 */
1450 static lwpid_t
1451 get_process_next_stopped_thread_id (pid, thread_state)
1452 int pid;
1453 ttstate_t *thread_state;
1454 {
1455 int tt_status;
1456
1457 tt_status = call_real_ttrace (
1458 TT_PROC_GET_NEXT_LWP_STATE,
1459 (pid_t) pid,
1460 (lwpid_t) TT_NIL,
1461 (TTRACE_ARG_TYPE) thread_state,
1462 (TTRACE_ARG_TYPE) sizeof (*thread_state),
1463 TT_NIL);
1464 if (errno)
1465 perror_with_name ("ttrace");
1466
1467 if (tt_status < 0)
1468 /* Failed
1469 */
1470 return 0;
1471
1472 else if (tt_status == 0)
1473 {
1474 /* End of list, no next state. Don't return the
1475 * tts_lwpid, as it's a meaningless "240".
1476 *
1477 * This is an HPUX "feature".
1478 */
1479 return 0;
1480 }
1481
1482 return thread_state->tts_lwpid;
1483 }
1484
1485 /* ??rehrauer: Eventually this function perhaps should be calling
1486 pid_to_thread_id. However, that function currently does nothing
1487 for HP-UX. Even then, I'm not clear whether that function
1488 will return a "kernel" thread ID, or a "user" thread ID. If
1489 the former, we can just call it here. If the latter, we must
1490 map from the "user" tid to a "kernel" tid.
1491
1492 NOTE: currently not called.
1493 */
1494 static lwpid_t
1495 get_active_tid_of_pid (pid)
1496 int pid;
1497 {
1498 ttstate_t thread_state;
1499
1500 return get_process_first_stopped_thread_id (pid, &thread_state);
1501 }
1502
1503 /* This function returns 1 if tt_request is a ttrace request that
1504 * operates upon all threads of a (i.e., the entire) process.
1505 */
1506 int
1507 is_process_ttrace_request (tt_request)
1508 ttreq_t tt_request;
1509 {
1510 return IS_TTRACE_PROCREQ (tt_request);
1511 }
1512 \f
1513
1514 /* This function translates a thread ttrace request into
1515 * the equivalent process request for a one-thread process.
1516 */
1517 static ttreq_t
1518 make_process_version (request)
1519 ttreq_t request;
1520 {
1521 if (!IS_TTRACE_REQ (request))
1522 {
1523 error ("Internal error, bad ttrace request made\n");
1524 return -1;
1525 }
1526
1527 switch (request)
1528 {
1529 case TT_LWP_STOP:
1530 return TT_PROC_STOP;
1531
1532 case TT_LWP_CONTINUE:
1533 return TT_PROC_CONTINUE;
1534
1535 case TT_LWP_GET_EVENT_MASK:
1536 return TT_PROC_GET_EVENT_MASK;
1537
1538 case TT_LWP_SET_EVENT_MASK:
1539 return TT_PROC_SET_EVENT_MASK;
1540
1541 case TT_LWP_SINGLE:
1542 case TT_LWP_RUREGS:
1543 case TT_LWP_WUREGS:
1544 case TT_LWP_GET_STATE:
1545 return -1; /* No equivalent */
1546
1547 default:
1548 return request;
1549 }
1550 }
1551 \f
1552
1553 /* This function translates the "pid" used by the rest of
1554 * gdb to a real pid and a tid. It then calls "call_real_ttrace"
1555 * with the given arguments.
1556 *
1557 * In general, other parts of this module should call this
1558 * function when they are dealing with external users, who only
1559 * have tids to pass (but they call it "pid" for historical
1560 * reasons).
1561 */
1562 static int
1563 call_ttrace (request, gdb_tid, addr, data, addr2)
1564 ttreq_t request;
1565 int gdb_tid;
1566 TTRACE_ARG_TYPE addr, data, addr2;
1567 {
1568 lwpid_t real_tid;
1569 int real_pid;
1570 ttreq_t new_request;
1571 int tt_status;
1572 char reason_for_failure[100]; /* Arbitrary size, should be big enough. */
1573
1574 #ifdef THREAD_DEBUG
1575 int is_interesting = 0;
1576
1577 if (TT_LWP_RUREGS == request)
1578 {
1579 is_interesting = 1; /* Adjust code here as desired */
1580 }
1581
1582 if (is_interesting && 0 && debug_on)
1583 {
1584 if (!is_process_ttrace_request (request))
1585 {
1586 printf ("TT: Thread request, tid is %d", gdb_tid);
1587 printf ("== SINGLE at %x", addr);
1588 }
1589 else
1590 {
1591 printf ("TT: Process request, tid is %d\n", gdb_tid);
1592 printf ("==! SINGLE at %x", addr);
1593 }
1594 }
1595 #endif
1596
1597 /* The initial SETTRC and SET_EVENT_MASK calls (and all others
1598 * which happen before any threads get set up) should go
1599 * directly to "call_real_ttrace", so they don't happen here.
1600 *
1601 * But hardware watchpoints do a SET_EVENT_MASK, so we can't
1602 * rule them out....
1603 */
1604 #ifdef THREAD_DEBUG
1605 if (request == TT_PROC_SETTRC && debug_on)
1606 printf ("Unexpected call for TT_PROC_SETTRC\n");
1607 #endif
1608
1609 /* Sometimes we get called with a bogus tid (e.g., if a
1610 * thread has terminated, we return 0; inftarg later asks
1611 * whether the thread has exited/forked/vforked).
1612 */
1613 if (gdb_tid == 0)
1614 {
1615 errno = ESRCH; /* ttrace's response would probably be "No such process". */
1616 return -1;
1617 }
1618
1619 /* All other cases should be able to expect that there are
1620 * thread records.
1621 */
1622 if (!any_thread_records ())
1623 {
1624 #ifdef THREAD_DEBUG
1625 if (debug_on)
1626 warning ("No thread records for ttrace call");
1627 #endif
1628 errno = ESRCH; /* ttrace's response would be "No such process". */
1629 return -1;
1630 }
1631
1632 /* OK, now the task is to translate the incoming tid into
1633 * a pid/tid pair.
1634 */
1635 real_tid = map_from_gdb_tid (gdb_tid);
1636 real_pid = get_pid_for (real_tid);
1637
1638 /* Now check the result. "Real_pid" is NULL if our list
1639 * didn't find it. We have some tricks we can play to fix
1640 * this, however.
1641 */
1642 if (0 == real_pid)
1643 {
1644 ttstate_t thread_state;
1645
1646 #ifdef THREAD_DEBUG
1647 if (debug_on)
1648 printf ("No saved pid for tid %d\n", gdb_tid);
1649 #endif
1650
1651 if (is_process_ttrace_request (request))
1652 {
1653
1654 /* Ok, we couldn't get a tid. Try to translate to
1655 * the equivalent process operation. We expect this
1656 * NOT to happen, so this is a desparation-type
1657 * move. It can happen if there is an internal
1658 * error and so no "wait()" call is ever done.
1659 */
1660 new_request = make_process_version (request);
1661 if (new_request == -1)
1662 {
1663
1664 #ifdef THREAD_DEBUG
1665 if (debug_on)
1666 printf ("...and couldn't make process version of thread operation\n");
1667 #endif
1668
1669 /* Use hacky saved pid, which won't always be correct
1670 * in the multi-process future. Use tid as thread,
1671 * probably dooming this to failure. FIX!
1672 */
1673 if (saved_real_pid != 0)
1674 {
1675 #ifdef THREAD_DEBUG
1676 if (debug_on)
1677 printf ("...using saved pid %d\n", saved_real_pid);
1678 #endif
1679
1680 real_pid = saved_real_pid;
1681 real_tid = gdb_tid;
1682 }
1683
1684 else
1685 error ("Unable to perform thread operation");
1686 }
1687
1688 else
1689 {
1690 /* Sucessfully translated this to a process request,
1691 * which needs no thread value.
1692 */
1693 real_pid = gdb_tid;
1694 real_tid = 0;
1695 request = new_request;
1696
1697 #ifdef THREAD_DEBUG
1698 if (debug_on)
1699 {
1700 printf ("Translated thread request to process request\n");
1701 if (saved_real_pid == 0)
1702 printf ("...but there's no saved pid\n");
1703
1704 else
1705 {
1706 if (gdb_tid != saved_real_pid)
1707 printf ("...but have the wrong pid (%d rather than %d)\n",
1708 gdb_tid, saved_real_pid);
1709 }
1710 }
1711 #endif
1712 } /* Translated to a process request */
1713 } /* Is a process request */
1714
1715 else
1716 {
1717 /* We have to have a thread. Ooops.
1718 */
1719 error ("Thread request with no threads (%s)",
1720 get_printable_name_of_ttrace_request (request));
1721 }
1722 }
1723
1724 /* Ttrace doesn't like to see tid values on process requests,
1725 * even if we have the right one.
1726 */
1727 if (is_process_ttrace_request (request))
1728 {
1729 real_tid = 0;
1730 }
1731
1732 #ifdef THREAD_DEBUG
1733 if (is_interesting && 0 && debug_on)
1734 {
1735 printf (" now tid %d, pid %d\n", real_tid, real_pid);
1736 printf (" request is %s\n", get_printable_name_of_ttrace_request (request));
1737 }
1738 #endif
1739
1740 /* Finally, the (almost) real call.
1741 */
1742 tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);
1743
1744 #ifdef THREAD_DEBUG
1745 if (is_interesting && debug_on)
1746 {
1747 if (!TT_OK (tt_status, errno)
1748 && !(tt_status == 0 & errno == 0))
1749 printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
1750 }
1751 #endif
1752
1753 return tt_status;
1754 }
1755
1756
1757 /* Stop all the threads of a process.
1758
1759 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
1760 * to get a TTEVT_NONE event, discarding the old event. Be
1761 * very careful, and only call TT_PROC_STOP when you mean it!
1762 */
1763 static void
1764 stop_all_threads_of_process (real_pid)
1765 pid_t real_pid;
1766 {
1767 int ttw_status;
1768
1769 ttw_status = call_real_ttrace (TT_PROC_STOP,
1770 (pid_t) real_pid,
1771 (lwpid_t) TT_NIL,
1772 (TTRACE_ARG_TYPE) TT_NIL,
1773 (TTRACE_ARG_TYPE) TT_NIL,
1774 TT_NIL);
1775 if (errno)
1776 perror_with_name ("ttrace stop of other threads");
1777 }
1778
1779
1780 /* Under some circumstances, it's unsafe to attempt to stop, or even
1781 query the state of, a process' threads.
1782
1783 In ttrace-based HP-UX, an example is a vforking child process. The
1784 vforking parent and child are somewhat fragile, w/r/t what we can do
1785 what we can do to them with ttrace, until after the child exits or
1786 execs, or until the parent's vfork event is delivered. Until that
1787 time, we must not try to stop the process' threads, or inquire how
1788 many there are, or even alter its data segments, or it typically dies
1789 with a SIGILL. Sigh.
1790
1791 This function returns 1 if this stopped process, and the event that
1792 we're told was responsible for its current stopped state, cannot safely
1793 have its threads examined.
1794 */
1795 #define CHILD_VFORKED(evt,pid) \
1796 (((evt) == TTEVT_VFORK) && ((pid) != inferior_pid))
1797 #define CHILD_URPED(evt,pid) \
1798 ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
1799 #define PARENT_VFORKED(evt,pid) \
1800 (((evt) == TTEVT_VFORK) && ((pid) == inferior_pid))
1801
1802 static int
1803 can_touch_threads_of_process (pid, stopping_event)
1804 int pid;
1805 ttevents_t stopping_event;
1806 {
1807 if (CHILD_VFORKED (stopping_event, pid))
1808 {
1809 vforking_child_pid = pid;
1810 vfork_in_flight = 1;
1811 }
1812
1813 else if (vfork_in_flight &&
1814 (PARENT_VFORKED (stopping_event, pid) ||
1815 CHILD_URPED (stopping_event, pid)))
1816 {
1817 vfork_in_flight = 0;
1818 vforking_child_pid = 0;
1819 }
1820
1821 return !vfork_in_flight;
1822 }
1823
1824
1825 /* If we can find an as-yet-unhandled thread state of a
1826 * stopped thread of this process return 1 and set "tsp".
1827 * Return 0 if we can't.
1828 *
1829 * If this function is used when the threads of PIS haven't
1830 * been stopped, undefined behaviour is guaranteed!
1831 */
1832 static int
1833 select_stopped_thread_of_process (pid, tsp)
1834 int pid;
1835 ttstate_t *tsp;
1836 {
1837 lwpid_t candidate_tid, tid;
1838 ttstate_t candidate_tstate, tstate;
1839
1840 /* If we're not allowed to touch the process now, then just
1841 * return the current value of *TSP.
1842 *
1843 * This supports "vfork". It's ok, really, to double the
1844 * current event (the child EXEC, we hope!).
1845 */
1846 if (!can_touch_threads_of_process (pid, tsp->tts_event))
1847 return 1;
1848
1849 /* Decide which of (possibly more than one) events to
1850 * return as the first one. We scan them all so that
1851 * we always return the result of a fake-step first.
1852 */
1853 candidate_tid = 0;
1854 for (tid = get_process_first_stopped_thread_id (pid, &tstate);
1855 tid != 0;
1856 tid = get_process_next_stopped_thread_id (pid, &tstate))
1857 {
1858 /* TTEVT_NONE events are uninteresting to our clients. They're
1859 * an artifact of our "stop the world" model--the thread is
1860 * stopped because we stopped it.
1861 */
1862 if (tstate.tts_event == TTEVT_NONE)
1863 {
1864 set_handled (pid, tstate.tts_lwpid);
1865 }
1866
1867 /* Did we just single-step a single thread, without letting any
1868 * of the others run? Is this an event for that thread?
1869 *
1870 * If so, we believe our client would prefer to see this event
1871 * over any others. (Typically the client wants to just push
1872 * one thread a little farther forward, and then go around
1873 * checking for what all threads are doing.)
1874 */
1875 else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
1876 {
1877 #ifdef WAIT_BUFFER_DEBUG
1878 /* It's possible here to see either a SIGTRAP (due to
1879 * successful completion of a step) or a SYSCALL_ENTRY
1880 * (due to a step completion with active hardware
1881 * watchpoints).
1882 */
1883 if (debug_on)
1884 printf ("Ending fake step with tid %d, state %s\n",
1885 tstate.tts_lwpid,
1886 get_printable_name_of_ttrace_event (tstate.tts_event));
1887 #endif
1888
1889 /* Remember this one, and throw away any previous
1890 * candidate.
1891 */
1892 candidate_tid = tstate.tts_lwpid;
1893 candidate_tstate = tstate;
1894 }
1895
1896 #ifdef FORGET_DELETED_BPTS
1897
1898 /* We can't just do this, as if we do, and then wind
1899 * up the loop with no unhandled events, we need to
1900 * handle that case--the appropriate reaction is to
1901 * just continue, but there's no easy way to do that.
1902 *
1903 * Better to put this in the ttrace_wait call--if, when
1904 * we fake a wait, we update our events based on the
1905 * breakpoint_here_pc call and find there are no more events,
1906 * then we better continue and so on.
1907 *
1908 * Or we could put it in the next/continue fake.
1909 * But it has to go in the buffering code, not in the
1910 * real go/wait code.
1911 */
1912 else if ((TTEVT_SIGNAL == tstate.tts_event)
1913 && (5 == tstate.tts_u.tts_signal.tts_signo)
1914 && (0 != get_raw_pc (tstate.tts_lwpid))
1915 && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
1916 {
1917 /*
1918 * If the user deleted a breakpoint while this
1919 * breakpoint-hit event was buffered, we can forget
1920 * it now.
1921 */
1922 #ifdef WAIT_BUFFER_DEBUG
1923 if (debug_on)
1924 printf ("Forgetting deleted bp hit for thread %d\n",
1925 tstate.tts_lwpid);
1926 #endif
1927
1928 set_handled (pid, tstate.tts_lwpid);
1929 }
1930 #endif
1931
1932 /* Else, is this the first "unhandled" event? If so,
1933 * we believe our client wants to see it (if we don't
1934 * see a fake-step later on in the scan).
1935 */
1936 else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
1937 {
1938 candidate_tid = tstate.tts_lwpid;
1939 candidate_tstate = tstate;
1940 }
1941
1942 /* This is either an event that has already been "handled",
1943 * and thus we believe is uninteresting to our client, or we
1944 * already have a candidate event. Ignore it...
1945 */
1946 }
1947
1948 /* What do we report?
1949 */
1950 if (doing_fake_step)
1951 {
1952 if (candidate_tid == fake_step_tid)
1953 {
1954 /* Fake step.
1955 */
1956 tstate = candidate_tstate;
1957 }
1958 else
1959 {
1960 warning ("Internal error: fake-step failed to complete.");
1961 return 0;
1962 }
1963 }
1964 else if (candidate_tid != 0)
1965 {
1966 /* Found a candidate unhandled event.
1967 */
1968 tstate = candidate_tstate;
1969 }
1970 else if (tid != 0)
1971 {
1972 warning ("Internal error in call of ttrace_wait.");
1973 return 0;
1974 }
1975 else
1976 {
1977 warning ("Internal error: no unhandled thread event to select");
1978 return 0;
1979 }
1980
1981 copy_ttstate_t (tsp, &tstate);
1982 return 1;
1983 } /* End of select_stopped_thread_of_process */
1984
1985 #ifdef PARANOIA
1986 /* Check our internal thread data against the real thing.
1987 */
1988 static void
1989 check_thread_consistency (real_pid)
1990 pid_t real_pid;
1991 {
1992 int tid; /* really lwpid_t */
1993 ttstate_t tstate;
1994 thread_info *p;
1995
1996 /* Spin down the O/S list of threads, checking that they
1997 * match what we've got.
1998 */
1999 for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
2000 tid != 0;
2001 tid = get_process_next_stopped_thread_id (real_pid, &tstate))
2002 {
2003
2004 p = find_thread_info (tid);
2005
2006 if (NULL == p)
2007 {
2008 warning ("No internal thread data for thread %d.", tid);
2009 continue;
2010 }
2011
2012 if (!p->seen)
2013 {
2014 warning ("Inconsistent internal thread data for thread %d.", tid);
2015 }
2016
2017 if (p->terminated)
2018 {
2019 warning ("Thread %d is not terminated, internal error.", tid);
2020 continue;
2021 }
2022
2023
2024 #define TT_COMPARE( fld ) \
2025 tstate.fld != p->last_stop_state.fld
2026
2027 if (p->have_state)
2028 {
2029 if (TT_COMPARE (tts_pid)
2030 || TT_COMPARE (tts_lwpid)
2031 || TT_COMPARE (tts_user_tid)
2032 || TT_COMPARE (tts_event)
2033 || TT_COMPARE (tts_flags)
2034 || TT_COMPARE (tts_scno)
2035 || TT_COMPARE (tts_scnargs))
2036 {
2037 warning ("Internal thread data for thread %d is wrong.", tid);
2038 continue;
2039 }
2040 }
2041 }
2042 }
2043 #endif /* PARANOIA */
2044 \f
2045
2046 /* This function wraps calls to "call_real_ttrace_wait" so
2047 * that a actual wait is only done when all pending events
2048 * have been reported.
2049 *
2050 * Note that typically it is called with a pid of "0", i.e.
2051 * the "don't care" value.
2052 *
2053 * Return value is the status of the pseudo wait.
2054 */
2055 static int
2056 call_ttrace_wait (pid, option, tsp, tsp_size)
2057 int pid;
2058 ttwopt_t option;
2059 ttstate_t *tsp;
2060 size_t tsp_size;
2061 {
2062 /* This holds the actual, for-real, true process ID.
2063 */
2064 static int real_pid;
2065
2066 /* As an argument to ttrace_wait, zero pid
2067 * means "Any process", and zero tid means
2068 * "Any thread of the specified process".
2069 */
2070 int wait_pid = 0;
2071 lwpid_t wait_tid = 0;
2072 lwpid_t real_tid;
2073
2074 int ttw_status = 0; /* To be returned */
2075
2076 thread_info *tinfo = NULL;
2077
2078 if (pid != 0)
2079 {
2080 /* Unexpected case.
2081 */
2082 #ifdef THREAD_DEBUG
2083 if (debug_on)
2084 printf ("TW: Pid to wait on is %d\n", pid);
2085 #endif
2086
2087 if (!any_thread_records ())
2088 error ("No thread records for ttrace call w. specific pid");
2089
2090 /* OK, now the task is to translate the incoming tid into
2091 * a pid/tid pair.
2092 */
2093 real_tid = map_from_gdb_tid (pid);
2094 real_pid = get_pid_for (real_tid);
2095 #ifdef THREAD_DEBUG
2096 if (debug_on)
2097 printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
2098 #endif
2099 }
2100
2101
2102 /* Sanity checks and set-up.
2103 * Process State
2104 *
2105 * Stopped Running Fake-step (v)Fork
2106 * \________________________________________
2107 * |
2108 * No buffered events | error wait wait wait
2109 * |
2110 * Buffered events | debuffer error wait debuffer (?)
2111 *
2112 */
2113 if (more_events_left == 0)
2114 {
2115
2116 if (process_state == RUNNING)
2117 {
2118 /* OK--normal call of ttrace_wait with no buffered events.
2119 */
2120 ;
2121 }
2122 else if (process_state == FAKE_STEPPING)
2123 {
2124 /* Ok--call of ttrace_wait to support
2125 * fake stepping with no buffered events.
2126 *
2127 * But we better be fake-stepping!
2128 */
2129 if (!doing_fake_step)
2130 {
2131 warning ("Inconsistent thread state.");
2132 }
2133 }
2134 else if ((process_state == FORKING)
2135 || (process_state == VFORKING))
2136 {
2137 /* Ok--there are two processes, so waiting
2138 * for the second while the first is stopped
2139 * is ok. Handled bits stay as they were.
2140 */
2141 ;
2142 }
2143 else if (process_state == STOPPED)
2144 {
2145 warning ("Process not running at wait call.");
2146 }
2147 else
2148 /* No known state.
2149 */
2150 warning ("Inconsistent process state.");
2151 }
2152
2153 else
2154 {
2155 /* More events left
2156 */
2157 if (process_state == STOPPED)
2158 {
2159 /* OK--buffered events being unbuffered.
2160 */
2161 ;
2162 }
2163 else if (process_state == RUNNING)
2164 {
2165 /* An error--shouldn't have buffered events
2166 * when running.
2167 */
2168 warning ("Trying to continue with buffered events:");
2169 }
2170 else if (process_state == FAKE_STEPPING)
2171 {
2172 /*
2173 * Better be fake-stepping!
2174 */
2175 if (!doing_fake_step)
2176 {
2177 warning ("Losing buffered thread events!\n");
2178 }
2179 }
2180 else if ((process_state == FORKING)
2181 || (process_state == VFORKING))
2182 {
2183 /* Ok--there are two processes, so waiting
2184 * for the second while the first is stopped
2185 * is ok. Handled bits stay as they were.
2186 */
2187 ;
2188 }
2189 else
2190 warning ("Process in unknown state with buffered events.");
2191 }
2192
2193 /* Sometimes we have to wait for a particular thread
2194 * (if we're stepping over a bpt). In that case, we
2195 * _know_ it's going to complete the single-step we
2196 * asked for (because we're only doing the step under
2197 * certain very well-understood circumstances), so it
2198 * can't block.
2199 */
2200 if (doing_fake_step)
2201 {
2202 wait_tid = fake_step_tid;
2203 wait_pid = get_pid_for (fake_step_tid);
2204
2205 #ifdef WAIT_BUFFER_DEBUG
2206 if (debug_on)
2207 printf ("Doing a wait after a fake-step for %d, pid %d\n",
2208 wait_tid, wait_pid);
2209 #endif
2210 }
2211
2212 if (more_events_left == 0 /* No buffered events, need real ones. */
2213 || process_state != STOPPED)
2214 {
2215 /* If there are no buffered events, and so we need
2216 * real ones, or if we are FORKING, VFORKING,
2217 * FAKE_STEPPING or RUNNING, and thus have to do
2218 * a real wait, then do a real wait.
2219 */
2220
2221 #ifdef WAIT_BUFFER_DEBUG
2222 /* Normal case... */
2223 if (debug_on)
2224 printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
2225 #endif
2226
2227 /* The actual wait call.
2228 */
2229 ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);
2230
2231 /* Note that the routines we'll call will be using "call_real_ttrace",
2232 * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
2233 * the rest of the world uses (which is actually the tid).
2234 */
2235 real_pid = tsp->tts_pid;
2236
2237 /* For most events: Stop the world!
2238
2239 * It's sometimes not safe to stop all threads of a process.
2240 * Sometimes it's not even safe to ask for the thread state
2241 * of a process!
2242 */
2243 if (can_touch_threads_of_process (real_pid, tsp->tts_event))
2244 {
2245 /* If we're really only stepping a single thread, then don't
2246 * try to stop all the others -- we only do this single-stepping
2247 * business when all others were already stopped...and the stop
2248 * would mess up other threads' events.
2249 *
2250 * Similiarly, if there are other threads with events,
2251 * don't do the stop.
2252 */
2253 if (!doing_fake_step)
2254 {
2255 if (more_events_left > 0)
2256 warning ("Internal error in stopping process");
2257
2258 stop_all_threads_of_process (real_pid);
2259
2260 /* At this point, we could scan and update_thread_list(),
2261 * and only use the local list for the rest of the
2262 * module! We'd get rid of the scans in the various
2263 * continue routines (adding one in attach). It'd
2264 * be great--UPGRADE ME!
2265 */
2266 }
2267 }
2268
2269 #ifdef PARANOIA
2270 else if (debug_on)
2271 {
2272 if (more_events_left > 0)
2273 printf ("== Can't stop process; more events!\n");
2274 else
2275 printf ("== Can't stop process!\n");
2276 }
2277 #endif
2278
2279 process_state = STOPPED;
2280
2281 #ifdef WAIT_BUFFER_DEBUG
2282 if (debug_on)
2283 printf ("Process set to STOPPED\n");
2284 #endif
2285 }
2286
2287 else
2288 {
2289 /* Fake a call to ttrace_wait. The process must be
2290 * STOPPED, as we aren't going to do any wait.
2291 */
2292 #ifdef WAIT_BUFFER_DEBUG
2293 if (debug_on)
2294 printf ("TW: fake it\n");
2295 #endif
2296
2297 if (process_state != STOPPED)
2298 {
2299 warning ("Process not stopped at wait call, in state '%s'.\n",
2300 get_printable_name_of_process_state (process_state));
2301 }
2302
2303 if (doing_fake_step)
2304 error ("Internal error in stepping over breakpoint");
2305
2306 ttw_status = 0; /* Faking it is always successful! */
2307 } /* End of fake or not? if */
2308
2309 /* Pick an event to pass to our caller. Be paranoid.
2310 */
2311 if (!select_stopped_thread_of_process (real_pid, tsp))
2312 warning ("Can't find event, using previous event.");
2313
2314 else if (tsp->tts_event == TTEVT_NONE)
2315 warning ("Internal error: no thread has a real event.");
2316
2317 else if (doing_fake_step)
2318 {
2319 if (fake_step_tid != tsp->tts_lwpid)
2320 warning ("Internal error in stepping over breakpoint.");
2321
2322 /* This wait clears the (current) fake-step if there was one.
2323 */
2324 doing_fake_step = 0;
2325 fake_step_tid = 0;
2326 }
2327
2328 /* We now have a correct tsp and ttw_status for the thread
2329 * which we want to report. So it's "handled"! This call
2330 * will add it to our list if it's not there already.
2331 */
2332 set_handled (real_pid, tsp->tts_lwpid);
2333
2334 /* Save a copy of the ttrace state of this thread, in our local
2335 thread descriptor.
2336
2337 This caches the state. The implementation of queries like
2338 target_has_execd can then use this cached state, rather than
2339 be forced to make an explicit ttrace call to get it.
2340
2341 (Guard against the condition that this is the first time we've
2342 waited on, i.e., seen this thread, and so haven't yet entered
2343 it into our list of threads.)
2344 */
2345 tinfo = find_thread_info (tsp->tts_lwpid);
2346 if (tinfo != NULL)
2347 {
2348 copy_ttstate_t (&tinfo->last_stop_state, tsp);
2349 tinfo->have_state = 1;
2350 }
2351
2352 return ttw_status;
2353 } /* call_ttrace_wait */
2354
2355 #if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
2356 int
2357 child_reported_exec_events_per_exec_call ()
2358 {
2359 return 1; /* ttrace reports the event once per call. */
2360 }
2361 #endif
2362 \f
2363
2364
2365 /* Our implementation of hardware watchpoints involves making memory
2366 pages write-protected. We must remember a page's original permissions,
2367 and we must also know when it is appropriate to restore a page's
2368 permissions to its original state.
2369
2370 We use a "dictionary" of hardware-watched pages to do this. Each
2371 hardware-watched page is recorded in the dictionary. Each page's
2372 dictionary entry contains the original permissions and a reference
2373 count. Pages are hashed into the dictionary by their start address.
2374
2375 When hardware watchpoint is set on page X for the first time, page X
2376 is added to the dictionary with a reference count of 1. If other
2377 hardware watchpoints are subsequently set on page X, its reference
2378 count is incremented. When hardware watchpoints are removed from
2379 page X, its reference count is decremented. If a page's reference
2380 count drops to 0, it's permissions are restored and the page's entry
2381 is thrown out of the dictionary.
2382 */
2383 typedef struct memory_page
2384 {
2385 CORE_ADDR page_start;
2386 int reference_count;
2387 int original_permissions;
2388 struct memory_page *next;
2389 struct memory_page *previous;
2390 }
2391 memory_page_t;
2392
2393 #define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT 128
2394
2395 static struct
2396 {
2397 LONGEST page_count;
2398 int page_size;
2399 int page_protections_allowed;
2400 /* These are just the heads of chains of actual page descriptors. */
2401 memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
2402 }
2403 memory_page_dictionary;
2404
2405
2406 static void
2407 require_memory_page_dictionary ()
2408 {
2409 int i;
2410
2411 /* Is the memory page dictionary ready for use? If so, we're done. */
2412 if (memory_page_dictionary.page_count >= (LONGEST) 0)
2413 return;
2414
2415 /* Else, initialize it. */
2416 memory_page_dictionary.page_count = (LONGEST) 0;
2417
2418 for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
2419 {
2420 memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
2421 memory_page_dictionary.buckets[i].reference_count = 0;
2422 memory_page_dictionary.buckets[i].next = NULL;
2423 memory_page_dictionary.buckets[i].previous = NULL;
2424 }
2425 }
2426
2427
2428 static void
2429 retire_memory_page_dictionary ()
2430 {
2431 memory_page_dictionary.page_count = (LONGEST) - 1;
2432 }
2433
2434
2435 /* Write-protect the memory page that starts at this address.
2436
2437 Returns the original permissions of the page.
2438 */
2439 static int
2440 write_protect_page (pid, page_start)
2441 int pid;
2442 CORE_ADDR page_start;
2443 {
2444 int tt_status;
2445 int original_permissions;
2446 int new_permissions;
2447
2448 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
2449 pid,
2450 (TTRACE_ARG_TYPE) page_start,
2451 TT_NIL,
2452 (TTRACE_ARG_TYPE) & original_permissions);
2453 if (errno || (tt_status < 0))
2454 {
2455 return 0; /* What else can we do? */
2456 }
2457
2458 /* We'll also write-protect the page now, if that's allowed. */
2459 if (memory_page_dictionary.page_protections_allowed)
2460 {
2461 new_permissions = original_permissions & ~PROT_WRITE;
2462 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2463 pid,
2464 (TTRACE_ARG_TYPE) page_start,
2465 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2466 (TTRACE_ARG_TYPE) new_permissions);
2467 if (errno || (tt_status < 0))
2468 {
2469 return 0; /* What else can we do? */
2470 }
2471 }
2472
2473 return original_permissions;
2474 }
2475
2476
2477 /* Unwrite-protect the memory page that starts at this address, restoring
2478 (what we must assume are) its original permissions.
2479 */
2480 static void
2481 unwrite_protect_page (pid, page_start, original_permissions)
2482 int pid;
2483 CORE_ADDR page_start;
2484 int original_permissions;
2485 {
2486 int tt_status;
2487
2488 tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
2489 pid,
2490 (TTRACE_ARG_TYPE) page_start,
2491 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
2492 (TTRACE_ARG_TYPE) original_permissions);
2493 if (errno || (tt_status < 0))
2494 {
2495 return; /* What else can we do? */
2496 }
2497 }
2498
2499
2500 /* Memory page-protections are used to implement "hardware" watchpoints
2501 on HP-UX.
2502
2503 For every memory page that is currently being watched (i.e., that
2504 presently should be write-protected), write-protect it.
2505 */
2506 void
2507 hppa_enable_page_protection_events (pid)
2508 int pid;
2509 {
2510 int bucket;
2511
2512 memory_page_dictionary.page_protections_allowed = 1;
2513
2514 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2515 {
2516 memory_page_t *page;
2517
2518 page = memory_page_dictionary.buckets[bucket].next;
2519 while (page != NULL)
2520 {
2521 page->original_permissions = write_protect_page (pid, page->page_start);
2522 page = page->next;
2523 }
2524 }
2525 }
2526
2527
2528 /* Memory page-protections are used to implement "hardware" watchpoints
2529 on HP-UX.
2530
2531 For every memory page that is currently being watched (i.e., that
2532 presently is or should be write-protected), un-write-protect it.
2533 */
2534 void
2535 hppa_disable_page_protection_events (pid)
2536 int pid;
2537 {
2538 int bucket;
2539
2540 for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
2541 {
2542 memory_page_t *page;
2543
2544 page = memory_page_dictionary.buckets[bucket].next;
2545 while (page != NULL)
2546 {
2547 unwrite_protect_page (pid, page->page_start, page->original_permissions);
2548 page = page->next;
2549 }
2550 }
2551
2552 memory_page_dictionary.page_protections_allowed = 0;
2553 }
2554
2555 /* Count the number of outstanding events. At this
2556 * point, we have selected one thread and its event
2557 * as the one to be "reported" upwards to core gdb.
2558 * That thread is already marked as "handled".
2559 *
2560 * Note: we could just scan our own thread list. FIXME!
2561 */
2562 static int
2563 count_unhandled_events (real_pid, real_tid)
2564 int real_pid;
2565 lwpid_t real_tid;
2566 {
2567 ttstate_t tstate;
2568 lwpid_t ttid;
2569 int events_left;
2570
2571 /* Ok, find out how many threads have real events to report.
2572 */
2573 events_left = 0;
2574 ttid = get_process_first_stopped_thread_id (real_pid, &tstate);
2575
2576 #ifdef THREAD_DEBUG
2577 if (debug_on)
2578 {
2579 if (ttid == 0)
2580 printf ("Process %d has no threads\n", real_pid);
2581 else
2582 printf ("Process %d has these threads:\n", real_pid);
2583 }
2584 #endif
2585
2586 while (ttid > 0)
2587 {
2588 if (tstate.tts_event != TTEVT_NONE
2589 && !was_handled (ttid))
2590 {
2591 /* TTEVT_NONE implies we just stopped it ourselves
2592 * because we're the stop-the-world guys, so it's
2593 * not an event from our point of view.
2594 *
2595 * If "was_handled" is true, this is an event we
2596 * already handled, so don't count it.
2597 *
2598 * Note that we don't count the thread with the
2599 * currently-reported event, as it's already marked
2600 * as handled.
2601 */
2602 events_left++;
2603 }
2604
2605 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2606 if (debug_on)
2607 {
2608 if (ttid == real_tid)
2609 printf ("*"); /* Thread we're reporting */
2610 else
2611 printf (" ");
2612
2613 if (tstate.tts_event != TTEVT_NONE)
2614 printf ("+"); /* Thread with a real event */
2615 else
2616 printf (" ");
2617
2618 if (was_handled (ttid))
2619 printf ("h"); /* Thread has been handled */
2620 else
2621 printf (" ");
2622
2623 printf (" %d, with event %s", ttid,
2624 get_printable_name_of_ttrace_event (tstate.tts_event));
2625
2626 if (tstate.tts_event == TTEVT_SIGNAL
2627 && 5 == tstate.tts_u.tts_signal.tts_signo)
2628 {
2629 CORE_ADDR pc_val;
2630
2631 pc_val = get_raw_pc (ttid);
2632
2633 if (pc_val > 0)
2634 printf (" breakpoint at 0x%x\n", pc_val);
2635 else
2636 printf (" bpt, can't fetch pc.\n");
2637 }
2638 else
2639 printf ("\n");
2640 }
2641 #endif
2642
2643 ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
2644 }
2645
2646 #if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
2647 if (debug_on)
2648 if (events_left > 0)
2649 printf ("There are thus %d pending events\n", events_left);
2650 #endif
2651
2652 return events_left;
2653 }
2654
2655 /* This function is provided as a sop to clients that are calling
2656 * ptrace_wait to wait for a process to stop. (see the
2657 * implementation of child_wait.) Return value is the pid for
2658 * the event that ended the wait.
2659 *
2660 * Note: used by core gdb and so uses the pseudo-pid (really tid).
2661 */
2662 int
2663 ptrace_wait (pid, status)
2664 int pid;
2665 int *status;
2666 {
2667 ttstate_t tsp;
2668 int ttwait_return;
2669 int real_pid;
2670 ttstate_t state;
2671 lwpid_t real_tid;
2672 int return_pid;
2673
2674 /* The ptrace implementation of this also ignores pid.
2675 */
2676 *status = 0;
2677
2678 ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
2679 if (ttwait_return < 0)
2680 {
2681 /* ??rehrauer: It appears that if our inferior exits and we
2682 haven't asked for exit events, that we're not getting any
2683 indication save a negative return from ttrace_wait and an
2684 errno set to ESRCH?
2685 */
2686 if (errno == ESRCH)
2687 {
2688 *status = 0; /* WIFEXITED */
2689 return inferior_pid;
2690 }
2691
2692 warning ("Call of ttrace_wait returned with errno %d.",
2693 errno);
2694 *status = ttwait_return;
2695 return inferior_pid;
2696 }
2697
2698 real_pid = tsp.tts_pid;
2699 real_tid = tsp.tts_lwpid;
2700
2701 /* One complication is that the "tts_event" structure has
2702 * a set of flags, and more than one can be set. So we
2703 * either have to force an order (as we do here), or handle
2704 * more than one flag at a time.
2705 */
2706 if (tsp.tts_event & TTEVT_LWP_CREATE)
2707 {
2708
2709 /* Unlike what you might expect, this event is reported in
2710 * the _creating_ thread, and the _created_ thread (whose tid
2711 * we have) is still running. So we have to stop it. This
2712 * has already been done in "call_ttrace_wait", but should we
2713 * ever abandon the "stop-the-world" model, here's the command
2714 * to use:
2715 *
2716 * call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
2717 *
2718 * Note that this would depend on being called _after_ "add_tthread"
2719 * below for the tid-to-pid translation to be done in "call_ttrace".
2720 */
2721
2722 #ifdef THREAD_DEBUG
2723 if (debug_on)
2724 printf ("New thread: pid %d, tid %d, creator tid %d\n",
2725 real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
2726 real_tid);
2727 #endif
2728
2729 /* Now we have to return the tid of the created thread, not
2730 * the creating thread, or "wait_for_inferior" won't know we
2731 * have a new "process" (thread). Plus we should record it
2732 * right, too.
2733 */
2734 real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;
2735
2736 add_tthread (real_pid, real_tid);
2737 }
2738
2739 else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
2740 || (tsp.tts_event & TTEVT_LWP_EXIT))
2741 {
2742
2743 #ifdef THREAD_DEBUG
2744 if (debug_on)
2745 printf ("Thread dies: %d\n", real_tid);
2746 #endif
2747
2748 del_tthread (real_tid);
2749 }
2750
2751 else if (tsp.tts_event & TTEVT_EXEC)
2752 {
2753
2754 #ifdef THREAD_DEBUG
2755 if (debug_on)
2756 printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
2757 real_pid, real_tid, inferior_pid);
2758 #endif
2759
2760 add_tthread (real_pid, real_tid);
2761 }
2762
2763 #ifdef THREAD_DEBUG
2764 else if (debug_on)
2765 {
2766 printf ("Process-level event %s, using tid %d\n",
2767 get_printable_name_of_ttrace_event (tsp.tts_event),
2768 real_tid);
2769
2770 /* OK to do this, as "add_tthread" won't add
2771 * duplicate entries. Also OK not to do it,
2772 * as this event isn't one which can change the
2773 * thread state.
2774 */
2775 add_tthread (real_pid, real_tid);
2776 }
2777 #endif
2778
2779
2780 /* How many events are left to report later?
2781 * In a non-stop-the-world model, this isn't needed.
2782 *
2783 * Note that it's not always safe to query the thread state of a process,
2784 * which is what count_unhandled_events does. (If unsafe, we're left with
2785 * no other resort than to assume that no more events remain...)
2786 */
2787 if (can_touch_threads_of_process (real_pid, tsp.tts_event))
2788 more_events_left = count_unhandled_events (real_pid, real_tid);
2789
2790 else
2791 {
2792 if (more_events_left > 0)
2793 warning ("Vfork or fork causing loss of %d buffered events.",
2794 more_events_left);
2795
2796 more_events_left = 0;
2797 }
2798
2799 /* Attempt to translate the ttrace_wait-returned status into the
2800 ptrace equivalent.
2801
2802 ??rehrauer: This is somewhat fragile. We really ought to rewrite
2803 clients that expect to pick apart a ptrace wait status, to use
2804 something a little more abstract.
2805 */
2806 if ((tsp.tts_event & TTEVT_EXEC)
2807 || (tsp.tts_event & TTEVT_FORK)
2808 || (tsp.tts_event & TTEVT_VFORK))
2809 {
2810 /* Forks come in pairs (parent and child), so core gdb
2811 * will do two waits. Be ready to notice this.
2812 */
2813 if (tsp.tts_event & TTEVT_FORK)
2814 {
2815 process_state = FORKING;
2816
2817 #ifdef WAIT_BUFFER_DEBUG
2818 if (debug_on)
2819 printf ("Process set to FORKING\n");
2820 #endif
2821 }
2822 else if (tsp.tts_event & TTEVT_VFORK)
2823 {
2824 process_state = VFORKING;
2825
2826 #ifdef WAIT_BUFFER_DEBUG
2827 if (debug_on)
2828 printf ("Process set to VFORKING\n");
2829 #endif
2830 }
2831
2832 /* Make an exec or fork look like a breakpoint. Definitely a hack,
2833 but I don't think non HP-UX-specific clients really carefully
2834 inspect the first events they get after inferior startup, so
2835 it probably almost doesn't matter what we claim this is.
2836 */
2837
2838 #ifdef THREAD_DEBUG
2839 if (debug_on)
2840 printf ("..a process 'event'\n");
2841 #endif
2842
2843 /* Also make fork and exec events look like bpts, so they can be caught.
2844 */
2845 *status = 0177 | (_SIGTRAP << 8);
2846 }
2847
2848 /* Special-cases: We ask for syscall entry and exit events to implement
2849 "fast" (aka "hardware") watchpoints.
2850
2851 When we get a syscall entry, we want to disable page-protections,
2852 and resume the inferior; this isn't an event we wish for
2853 wait_for_inferior to see. Note that we must resume ONLY the
2854 thread that reported the syscall entry; we don't want to allow
2855 other threads to run with the page protections off, as they might
2856 then be able to write to watch memory without it being caught.
2857
2858 When we get a syscall exit, we want to reenable page-protections,
2859 but we don't want to resume the inferior; this is an event we wish
2860 wait_for_inferior to see. Make it look like the signal we normally
2861 get for a single-step completion. This should cause wait_for_inferior
2862 to evaluate whether any watchpoint triggered.
2863
2864 Or rather, that's what we'd LIKE to do for syscall exit; we can't,
2865 due to some HP-UX "features". Some syscalls have problems with
2866 write-protections on some pages, and some syscalls seem to have
2867 pending writes to those pages at the time we're getting the return
2868 event. So, we'll single-step the inferior to get out of the syscall,
2869 and then reenable protections.
2870
2871 Note that we're intentionally allowing the syscall exit case to
2872 fall through into the succeeding cases, as sometimes we single-
2873 step out of one syscall only to immediately enter another...
2874 */
2875 else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
2876 || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
2877 {
2878 /* Make a syscall event look like a breakpoint. Same comments
2879 as for exec & fork events.
2880 */
2881 #ifdef THREAD_DEBUG
2882 if (debug_on)
2883 printf ("..a syscall 'event'\n");
2884 #endif
2885
2886 /* Also make syscall events look like bpts, so they can be caught.
2887 */
2888 *status = 0177 | (_SIGTRAP << 8);
2889 }
2890
2891 else if ((tsp.tts_event & TTEVT_LWP_CREATE)
2892 || (tsp.tts_event & TTEVT_LWP_TERMINATE)
2893 || (tsp.tts_event & TTEVT_LWP_EXIT))
2894 {
2895 /* Make a thread event look like a breakpoint. Same comments
2896 * as for exec & fork events.
2897 */
2898 #ifdef THREAD_DEBUG
2899 if (debug_on)
2900 printf ("..a thread 'event'\n");
2901 #endif
2902
2903 /* Also make thread events look like bpts, so they can be caught.
2904 */
2905 *status = 0177 | (_SIGTRAP << 8);
2906 }
2907
2908 else if ((tsp.tts_event & TTEVT_EXIT))
2909 { /* WIFEXITED */
2910
2911 #ifdef THREAD_DEBUG
2912 if (debug_on)
2913 printf ("..an exit\n");
2914 #endif
2915
2916 /* Prevent rest of gdb from thinking this is
2917 * a new thread if for some reason it's never
2918 * seen the main thread before.
2919 */
2920 inferior_pid = map_to_gdb_tid (real_tid); /* HACK, FIX */
2921
2922 *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
2923 }
2924
2925 else if (tsp.tts_event & TTEVT_SIGNAL)
2926 { /* WIFSTOPPED */
2927 #ifdef THREAD_DEBUG
2928 if (debug_on)
2929 printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
2930 #endif
2931
2932 *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
2933 }
2934
2935 else
2936 { /* !WIFSTOPPED */
2937
2938 /* This means the process or thread terminated. But we should've
2939 caught an explicit exit/termination above. So warn (this is
2940 really an internal error) and claim the process or thread
2941 terminated with a SIGTRAP.
2942 */
2943
2944 warning ("process_wait: unknown process state");
2945
2946 #ifdef THREAD_DEBUG
2947 if (debug_on)
2948 printf ("Process-level event %s, using tid %d\n",
2949 get_printable_name_of_ttrace_event (tsp.tts_event),
2950 real_tid);
2951 #endif
2952
2953 *status = _SIGTRAP;
2954 }
2955
2956 target_post_wait (tsp.tts_pid, *status);
2957
2958
2959 #ifdef THREAD_DEBUG
2960 if (debug_on)
2961 printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
2962 #endif
2963
2964 /* All code external to this module uses the tid, but calls
2965 * it "pid". There's some tweaking so that the outside sees
2966 * the first thread as having the same number as the starting
2967 * pid.
2968 */
2969 return_pid = map_to_gdb_tid (real_tid);
2970
2971 /* Remember this for later use in "hppa_prepare_to_proceed".
2972 */
2973 old_gdb_pid = inferior_pid;
2974 reported_pid = return_pid;
2975 reported_bpt = ((tsp.tts_event & TTEVT_SIGNAL) && (5 == tsp.tts_u.tts_signal.tts_signo));
2976
2977 if (real_tid == 0 || return_pid == 0)
2978 {
2979 warning ("Internal error: process-wait failed.");
2980 }
2981
2982 return return_pid;
2983 }
2984 \f
2985
2986 /* This function causes the caller's process to be traced by its
2987 parent. This is intended to be called after GDB forks itself,
2988 and before the child execs the target. Despite the name, it
2989 is called by the child.
2990
2991 Note that HP-UX ttrace is rather funky in how this is done.
2992 If the parent wants to get the initial exec event of a child,
2993 it must set the ttrace event mask of the child to include execs.
2994 (The child cannot do this itself.) This must be done after the
2995 child is forked, but before it execs.
2996
2997 To coordinate the parent and child, we implement a semaphore using
2998 pipes. After SETTRC'ing itself, the child tells the parent that
2999 it is now traceable by the parent, and waits for the parent's
3000 acknowledgement. The parent can then set the child's event mask,
3001 and notify the child that it can now exec.
3002
3003 (The acknowledgement by parent happens as a result of a call to
3004 child_acknowledge_created_inferior.)
3005 */
3006 int
3007 parent_attach_all ()
3008 {
3009 int tt_status;
3010
3011 /* We need a memory home for a constant, to pass it to ttrace.
3012 The value of the constant is arbitrary, so long as both
3013 parent and child use the same value. Might as well use the
3014 "magic" constant provided by ttrace...
3015 */
3016 uint64_t tc_magic_child = TT_VERSION;
3017 uint64_t tc_magic_parent = 0;
3018
3019 tt_status = call_real_ttrace (
3020 TT_PROC_SETTRC,
3021 (int) TT_NIL,
3022 (lwpid_t) TT_NIL,
3023 TT_NIL,
3024 (TTRACE_ARG_TYPE) TT_VERSION,
3025 TT_NIL);
3026
3027 if (tt_status < 0)
3028 return tt_status;
3029
3030 /* Notify the parent that we're potentially ready to exec(). */
3031 write (startup_semaphore.child_channel[SEM_TALK],
3032 &tc_magic_child,
3033 sizeof (tc_magic_child));
3034
3035 /* Wait for acknowledgement from the parent. */
3036 read (startup_semaphore.parent_channel[SEM_LISTEN],
3037 &tc_magic_parent,
3038 sizeof (tc_magic_parent));
3039
3040 if (tc_magic_child != tc_magic_parent)
3041 warning ("mismatched semaphore magic");
3042
3043 /* Discard our copy of the semaphore. */
3044 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3045 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3046 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3047 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3048
3049 return tt_status;
3050 }
3051
3052 /* Despite being file-local, this routine is dealing with
3053 * actual process IDs, not thread ids. That's because it's
3054 * called before the first "wait" call, and there's no map
3055 * yet from tids to pids.
3056 *
3057 * When it is called, a forked child is running, but waiting on
3058 * the semaphore. If you stop the child and re-start it,
3059 * things get confused, so don't do that! An attached child is
3060 * stopped.
3061 *
3062 * Since this is called after either attach or run, we
3063 * have to be the common part of both.
3064 */
3065 static void
3066 require_notification_of_events (real_pid)
3067 int real_pid;
3068 {
3069 int tt_status;
3070 ttevent_t notifiable_events;
3071
3072 lwpid_t tid;
3073 ttstate_t thread_state;
3074
3075 #ifdef THREAD_DEBUG
3076 if (debug_on)
3077 printf ("Require notif, pid is %d\n", real_pid);
3078 #endif
3079
3080 /* Temporary HACK: tell inftarg.c/child_wait to not
3081 * loop until pids are the same.
3082 */
3083 not_same_real_pid = 0;
3084
3085 sigemptyset (&notifiable_events.tte_signals);
3086 notifiable_events.tte_opts = TTEO_NONE;
3087
3088 /* This ensures that forked children inherit their parent's
3089 * event mask, which we're setting here.
3090 *
3091 * NOTE: if you debug gdb with itself, then the ultimate
3092 * debuggee gets flags set by the outermost gdb, as
3093 * a child of a child will still inherit.
3094 */
3095 notifiable_events.tte_opts |= TTEO_PROC_INHERIT;
3096
3097 notifiable_events.tte_events = TTEVT_DEFAULT;
3098 notifiable_events.tte_events |= TTEVT_SIGNAL;
3099 notifiable_events.tte_events |= TTEVT_EXEC;
3100 notifiable_events.tte_events |= TTEVT_EXIT;
3101 notifiable_events.tte_events |= TTEVT_FORK;
3102 notifiable_events.tte_events |= TTEVT_VFORK;
3103 notifiable_events.tte_events |= TTEVT_LWP_CREATE;
3104 notifiable_events.tte_events |= TTEVT_LWP_EXIT;
3105 notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;
3106
3107 tt_status = call_real_ttrace (
3108 TT_PROC_SET_EVENT_MASK,
3109 real_pid,
3110 (lwpid_t) TT_NIL,
3111 (TTRACE_ARG_TYPE) & notifiable_events,
3112 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3113 TT_NIL);
3114 }
3115
3116 static void
3117 require_notification_of_exec_events (real_pid)
3118 int real_pid;
3119 {
3120 int tt_status;
3121 ttevent_t notifiable_events;
3122
3123 lwpid_t tid;
3124 ttstate_t thread_state;
3125
3126 #ifdef THREAD_DEBUG
3127 if (debug_on)
3128 printf ("Require notif, pid is %d\n", real_pid);
3129 #endif
3130
3131 /* Temporary HACK: tell inftarg.c/child_wait to not
3132 * loop until pids are the same.
3133 */
3134 not_same_real_pid = 0;
3135
3136 sigemptyset (&notifiable_events.tte_signals);
3137 notifiable_events.tte_opts = TTEO_NOSTRCCHLD;
3138
3139 /* This ensures that forked children don't inherit their parent's
3140 * event mask, which we're setting here.
3141 */
3142 notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;
3143
3144 notifiable_events.tte_events = TTEVT_DEFAULT;
3145 notifiable_events.tte_events |= TTEVT_EXEC;
3146 notifiable_events.tte_events |= TTEVT_EXIT;
3147
3148 tt_status = call_real_ttrace (
3149 TT_PROC_SET_EVENT_MASK,
3150 real_pid,
3151 (lwpid_t) TT_NIL,
3152 (TTRACE_ARG_TYPE) & notifiable_events,
3153 (TTRACE_ARG_TYPE) sizeof (notifiable_events),
3154 TT_NIL);
3155 }
3156 \f
3157
3158 /* This function is called by the parent process, with pid being the
3159 * ID of the child process, after the debugger has forked.
3160 */
3161 void
3162 child_acknowledge_created_inferior (pid)
3163 int pid;
3164 {
3165 /* We need a memory home for a constant, to pass it to ttrace.
3166 The value of the constant is arbitrary, so long as both
3167 parent and child use the same value. Might as well use the
3168 "magic" constant provided by ttrace...
3169 */
3170 uint64_t tc_magic_parent = TT_VERSION;
3171 uint64_t tc_magic_child = 0;
3172
3173 /* Wait for the child to tell us that it has forked. */
3174 read (startup_semaphore.child_channel[SEM_LISTEN],
3175 &tc_magic_child,
3176 sizeof (tc_magic_child));
3177
3178 /* Clear thread info now. We'd like to do this in
3179 * "require...", but that messes up attach.
3180 */
3181 clear_thread_info ();
3182
3183 /* Tell the "rest of gdb" that the initial thread exists.
3184 * This isn't really a hack. Other thread-based versions
3185 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
3186 *
3187 * Q: Why don't we also add this thread to the local
3188 * list via "add_tthread"?
3189 *
3190 * A: Because we don't know the tid, and can't stop the
3191 * the process safely to ask what it is. Anyway, we'll
3192 * add it when it gets the EXEC event.
3193 */
3194 add_thread (pid); /* in thread.c */
3195
3196 /* We can now set the child's ttrace event mask.
3197 */
3198 require_notification_of_exec_events (pid);
3199
3200 /* Tell ourselves that the process is running.
3201 */
3202 process_state = RUNNING;
3203
3204 /* Notify the child that it can exec. */
3205 write (startup_semaphore.parent_channel[SEM_TALK],
3206 &tc_magic_parent,
3207 sizeof (tc_magic_parent));
3208
3209 /* Discard our copy of the semaphore. */
3210 (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
3211 (void) close (startup_semaphore.parent_channel[SEM_TALK]);
3212 (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
3213 (void) close (startup_semaphore.child_channel[SEM_TALK]);
3214 }
3215
3216
3217 /*
3218 * arrange for notification of all events by
3219 * calling require_notification_of_events.
3220 */
3221 void
3222 child_post_startup_inferior (real_pid)
3223 int real_pid;
3224 {
3225 require_notification_of_events (real_pid);
3226 }
3227
3228 /* From here on, we should expect tids rather than pids.
3229 */
3230 static void
3231 hppa_enable_catch_fork (tid)
3232 int tid;
3233 {
3234 int tt_status;
3235 ttevent_t ttrace_events;
3236
3237 /* Get the set of events that are currently enabled.
3238 */
3239 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3240 tid,
3241 (TTRACE_ARG_TYPE) & ttrace_events,
3242 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3243 TT_NIL);
3244 if (errno)
3245 perror_with_name ("ttrace");
3246
3247 /* Add forks to that set. */
3248 ttrace_events.tte_events |= TTEVT_FORK;
3249
3250 #ifdef THREAD_DEBUG
3251 if (debug_on)
3252 printf ("enable fork, tid is %d\n", tid);
3253 #endif
3254
3255 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3256 tid,
3257 (TTRACE_ARG_TYPE) & ttrace_events,
3258 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3259 TT_NIL);
3260 if (errno)
3261 perror_with_name ("ttrace");
3262 }
3263
3264
3265 static void
3266 hppa_disable_catch_fork (tid)
3267 int tid;
3268 {
3269 int tt_status;
3270 ttevent_t ttrace_events;
3271
3272 /* Get the set of events that are currently enabled.
3273 */
3274 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3275 tid,
3276 (TTRACE_ARG_TYPE) & ttrace_events,
3277 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3278 TT_NIL);
3279
3280 if (errno)
3281 perror_with_name ("ttrace");
3282
3283 /* Remove forks from that set. */
3284 ttrace_events.tte_events &= ~TTEVT_FORK;
3285
3286 #ifdef THREAD_DEBUG
3287 if (debug_on)
3288 printf ("disable fork, tid is %d\n", tid);
3289 #endif
3290
3291 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3292 tid,
3293 (TTRACE_ARG_TYPE) & ttrace_events,
3294 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3295 TT_NIL);
3296
3297 if (errno)
3298 perror_with_name ("ttrace");
3299 }
3300
3301
3302 #if defined(CHILD_INSERT_FORK_CATCHPOINT)
3303 int
3304 child_insert_fork_catchpoint (tid)
3305 int tid;
3306 {
3307 /* Enable reporting of fork events from the kernel. */
3308 /* ??rehrauer: For the moment, we're always enabling these events,
3309 and just ignoring them if there's no catchpoint to catch them.
3310 */
3311 return 0;
3312 }
3313 #endif
3314
3315
3316 #if defined(CHILD_REMOVE_FORK_CATCHPOINT)
3317 int
3318 child_remove_fork_catchpoint (tid)
3319 int tid;
3320 {
3321 /* Disable reporting of fork events from the kernel. */
3322 /* ??rehrauer: For the moment, we're always enabling these events,
3323 and just ignoring them if there's no catchpoint to catch them.
3324 */
3325 return 0;
3326 }
3327 #endif
3328
3329
3330 static void
3331 hppa_enable_catch_vfork (tid)
3332 int tid;
3333 {
3334 int tt_status;
3335 ttevent_t ttrace_events;
3336
3337 /* Get the set of events that are currently enabled.
3338 */
3339 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3340 tid,
3341 (TTRACE_ARG_TYPE) & ttrace_events,
3342 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3343 TT_NIL);
3344
3345 if (errno)
3346 perror_with_name ("ttrace");
3347
3348 /* Add vforks to that set. */
3349 ttrace_events.tte_events |= TTEVT_VFORK;
3350
3351 #ifdef THREAD_DEBUG
3352 if (debug_on)
3353 printf ("enable vfork, tid is %d\n", tid);
3354 #endif
3355
3356 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3357 tid,
3358 (TTRACE_ARG_TYPE) & ttrace_events,
3359 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3360 TT_NIL);
3361
3362 if (errno)
3363 perror_with_name ("ttrace");
3364 }
3365
3366
3367 static void
3368 hppa_disable_catch_vfork (tid)
3369 int tid;
3370 {
3371 int tt_status;
3372 ttevent_t ttrace_events;
3373
3374 /* Get the set of events that are currently enabled. */
3375 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
3376 tid,
3377 (TTRACE_ARG_TYPE) & ttrace_events,
3378 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3379 TT_NIL);
3380
3381 if (errno)
3382 perror_with_name ("ttrace");
3383
3384 /* Remove vforks from that set. */
3385 ttrace_events.tte_events &= ~TTEVT_VFORK;
3386
3387 #ifdef THREAD_DEBUG
3388 if (debug_on)
3389 printf ("disable vfork, tid is %d\n", tid);
3390 #endif
3391 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
3392 tid,
3393 (TTRACE_ARG_TYPE) & ttrace_events,
3394 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
3395 TT_NIL);
3396
3397 if (errno)
3398 perror_with_name ("ttrace");
3399 }
3400
3401
3402 #if defined(CHILD_INSERT_VFORK_CATCHPOINT)
3403 int
3404 child_insert_vfork_catchpoint (tid)
3405 int tid;
3406 {
3407 /* Enable reporting of vfork events from the kernel. */
3408 /* ??rehrauer: For the moment, we're always enabling these events,
3409 and just ignoring them if there's no catchpoint to catch them.
3410 */
3411 return 0;
3412 }
3413 #endif
3414
3415
3416 #if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
3417 int
3418 child_remove_vfork_catchpoint (tid)
3419 int tid;
3420 {
3421 /* Disable reporting of vfork events from the kernel. */
3422 /* ??rehrauer: For the moment, we're always enabling these events,
3423 and just ignoring them if there's no catchpoint to catch them.
3424 */
3425 return 0;
3426 }
3427 #endif
3428
3429 #if defined(CHILD_HAS_FORKED)
3430
3431 /* Q: Do we need to map the returned process ID to a thread ID?
3432
3433 * A: I don't think so--here we want a _real_ pid. Any later
3434 * operations will call "require_notification_of_events" and
3435 * start the mapping.
3436 */
3437 int
3438 child_has_forked (tid, childpid)
3439 int tid;
3440 int *childpid;
3441 {
3442 int tt_status;
3443 ttstate_t ttrace_state;
3444 thread_info *tinfo;
3445
3446 /* Do we have cached thread state that we can consult? If so, use it. */
3447 tinfo = find_thread_info (map_from_gdb_tid (tid));
3448 if (tinfo != NULL)
3449 {
3450 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3451 }
3452
3453 /* Nope, must read the thread's current state */
3454 else
3455 {
3456 tt_status = call_ttrace (TT_LWP_GET_STATE,
3457 tid,
3458 (TTRACE_ARG_TYPE) & ttrace_state,
3459 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3460 TT_NIL);
3461
3462 if (errno)
3463 perror_with_name ("ttrace");
3464
3465 if (tt_status < 0)
3466 return 0;
3467 }
3468
3469 if (ttrace_state.tts_event & TTEVT_FORK)
3470 {
3471 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3472 return 1;
3473 }
3474
3475 return 0;
3476 }
3477 #endif
3478
3479
3480 #if defined(CHILD_HAS_VFORKED)
3481
3482 /* See child_has_forked for pid discussion.
3483 */
3484 int
3485 child_has_vforked (tid, childpid)
3486 int tid;
3487 int *childpid;
3488 {
3489 int tt_status;
3490 ttstate_t ttrace_state;
3491 thread_info *tinfo;
3492
3493 /* Do we have cached thread state that we can consult? If so, use it. */
3494 tinfo = find_thread_info (map_from_gdb_tid (tid));
3495 if (tinfo != NULL)
3496 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3497
3498 /* Nope, must read the thread's current state */
3499 else
3500 {
3501 tt_status = call_ttrace (TT_LWP_GET_STATE,
3502 tid,
3503 (TTRACE_ARG_TYPE) & ttrace_state,
3504 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3505 TT_NIL);
3506
3507 if (errno)
3508 perror_with_name ("ttrace");
3509
3510 if (tt_status < 0)
3511 return 0;
3512 }
3513
3514 if (ttrace_state.tts_event & TTEVT_VFORK)
3515 {
3516 *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
3517 return 1;
3518 }
3519
3520 return 0;
3521 }
3522 #endif
3523
3524
3525 #if defined(CHILD_CAN_FOLLOW_VFORK_PRIOR_TO_EXEC)
3526 int
3527 child_can_follow_vfork_prior_to_exec ()
3528 {
3529 /* ttrace does allow this.
3530
3531 ??rehrauer: However, I had major-league problems trying to
3532 convince wait_for_inferior to handle that case. Perhaps when
3533 it is rewritten to grok multiple processes in an explicit way...
3534 */
3535 return 0;
3536 }
3537 #endif
3538
3539
3540 #if defined(CHILD_INSERT_EXEC_CATCHPOINT)
3541 int
3542 child_insert_exec_catchpoint (tid)
3543 int tid;
3544 {
3545 /* Enable reporting of exec events from the kernel. */
3546 /* ??rehrauer: For the moment, we're always enabling these events,
3547 and just ignoring them if there's no catchpoint to catch them.
3548 */
3549 return 0;
3550 }
3551 #endif
3552
3553
3554 #if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
3555 int
3556 child_remove_exec_catchpoint (tid)
3557 int tid;
3558 {
3559 /* Disable reporting of execevents from the kernel. */
3560 /* ??rehrauer: For the moment, we're always enabling these events,
3561 and just ignoring them if there's no catchpoint to catch them.
3562 */
3563 return 0;
3564 }
3565 #endif
3566
3567
3568 #if defined(CHILD_HAS_EXECD)
3569 int
3570 child_has_execd (tid, execd_pathname)
3571 int tid;
3572 char **execd_pathname;
3573 {
3574 int tt_status;
3575 ttstate_t ttrace_state;
3576 thread_info *tinfo;
3577
3578 /* Do we have cached thread state that we can consult? If so, use it. */
3579 tinfo = find_thread_info (map_from_gdb_tid (tid));
3580 if (tinfo != NULL)
3581 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3582
3583 /* Nope, must read the thread's current state */
3584 else
3585 {
3586 tt_status = call_ttrace (TT_LWP_GET_STATE,
3587 tid,
3588 (TTRACE_ARG_TYPE) & ttrace_state,
3589 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3590 TT_NIL);
3591
3592 if (errno)
3593 perror_with_name ("ttrace");
3594
3595 if (tt_status < 0)
3596 return 0;
3597 }
3598
3599 if (ttrace_state.tts_event & TTEVT_EXEC)
3600 {
3601 /* See child_pid_to_exec_file in this file: this is a macro.
3602 */
3603 char *exec_file = target_pid_to_exec_file (tid);
3604
3605 *execd_pathname = savestring (exec_file, strlen (exec_file));
3606 return 1;
3607 }
3608
3609 return 0;
3610 }
3611 #endif
3612
3613
3614 #if defined(CHILD_HAS_SYSCALL_EVENT)
3615 int
3616 child_has_syscall_event (pid, kind, syscall_id)
3617 int pid;
3618 enum target_waitkind *kind;
3619 int *syscall_id;
3620 {
3621 int tt_status;
3622 ttstate_t ttrace_state;
3623 thread_info *tinfo;
3624
3625 /* Do we have cached thread state that we can consult? If so, use it. */
3626 tinfo = find_thread_info (map_from_gdb_tid (pid));
3627 if (tinfo != NULL)
3628 copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
3629
3630 /* Nope, must read the thread's current state */
3631 else
3632 {
3633 tt_status = call_ttrace (TT_LWP_GET_STATE,
3634 pid,
3635 (TTRACE_ARG_TYPE) & ttrace_state,
3636 (TTRACE_ARG_TYPE) sizeof (ttrace_state),
3637 TT_NIL);
3638
3639 if (errno)
3640 perror_with_name ("ttrace");
3641
3642 if (tt_status < 0)
3643 return 0;
3644 }
3645
3646 *kind = TARGET_WAITKIND_SPURIOUS; /* Until proven otherwise... */
3647 *syscall_id = -1;
3648
3649 if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
3650 *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
3651 else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
3652 *kind = TARGET_WAITKIND_SYSCALL_RETURN;
3653 else
3654 return 0;
3655
3656 *syscall_id = ttrace_state.tts_scno;
3657 return 1;
3658 }
3659 #endif
3660 \f
3661
3662
3663 #if defined(CHILD_THREAD_ALIVE)
3664
3665 /* Check to see if the given thread is alive.
3666
3667 * We'll trust the thread list, as the more correct
3668 * approach of stopping the process and spinning down
3669 * the OS's thread list is _very_ expensive.
3670 *
3671 * May need a FIXME for that reason.
3672 */
3673 int
3674 child_thread_alive (gdb_tid)
3675 lwpid_t gdb_tid;
3676 {
3677 lwpid_t tid;
3678
3679 /* This spins down the lists twice.
3680 * Possible peformance improvement here!
3681 */
3682 tid = map_from_gdb_tid (gdb_tid);
3683 return !is_terminated (tid);
3684 }
3685
3686 #endif
3687 \f
3688
3689
3690 /* This function attempts to read the specified number of bytes from the
3691 save_state_t that is our view into the hardware registers, starting at
3692 ss_offset, and ending at ss_offset + sizeof_buf - 1
3693
3694 If this function succeeds, it deposits the fetched bytes into buf,
3695 and returns 0.
3696
3697 If it fails, it returns a negative result. The contents of buf are
3698 undefined it this function fails.
3699 */
3700 int
3701 read_from_register_save_state (tid, ss_offset, buf, sizeof_buf)
3702 int tid;
3703 TTRACE_ARG_TYPE ss_offset;
3704 char *buf;
3705 int sizeof_buf;
3706 {
3707 int tt_status;
3708 register_value_t register_value = 0;
3709
3710 tt_status = call_ttrace (TT_LWP_RUREGS,
3711 tid,
3712 ss_offset,
3713 (TTRACE_ARG_TYPE) sizeof_buf,
3714 (TTRACE_ARG_TYPE) buf);
3715
3716 if (tt_status == 1)
3717 /* Map ttrace's version of success to our version.
3718 * Sometime ttrace returns 0, but that's ok here.
3719 */
3720 return 0;
3721
3722 return tt_status;
3723 }
3724 \f
3725
3726 /* This function attempts to write the specified number of bytes to the
3727 save_state_t that is our view into the hardware registers, starting at
3728 ss_offset, and ending at ss_offset + sizeof_buf - 1
3729
3730 If this function succeeds, it deposits the bytes in buf, and returns 0.
3731
3732 If it fails, it returns a negative result. The contents of the save_state_t
3733 are undefined it this function fails.
3734 */
3735 int
3736 write_to_register_save_state (tid, ss_offset, buf, sizeof_buf)
3737 int tid;
3738 TTRACE_ARG_TYPE ss_offset;
3739 char *buf;
3740 int sizeof_buf;
3741 {
3742 int tt_status;
3743 register_value_t register_value = 0;
3744
3745 tt_status = call_ttrace (TT_LWP_WUREGS,
3746 tid,
3747 ss_offset,
3748 (TTRACE_ARG_TYPE) sizeof_buf,
3749 (TTRACE_ARG_TYPE) buf);
3750 return tt_status;
3751 }
3752 \f
3753
3754 /* This function is a sop to the largeish number of direct calls
3755 to call_ptrace that exist in other files. Rather than create
3756 functions whose name abstracts away from ptrace, and change all
3757 the present callers of call_ptrace, we'll do the expedient (and
3758 perhaps only practical) thing.
3759
3760 Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
3761 process. Thus, we must translate all ptrace requests into their
3762 process-specific, ttrace equivalents.
3763 */
3764 int
3765 call_ptrace (pt_request, gdb_tid, addr, data)
3766 int pt_request;
3767 int gdb_tid;
3768 PTRACE_ARG3_TYPE addr;
3769 int data;
3770 {
3771 ttreq_t tt_request;
3772 TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
3773 TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
3774 TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
3775 int tt_status;
3776 register_value_t register_value;
3777 int read_buf;
3778
3779 /* Perform the necessary argument translation. Note that some
3780 cases are funky enough in the ttrace realm that we handle them
3781 very specially.
3782 */
3783 switch (pt_request)
3784 {
3785 /* The following cases cannot conveniently be handled conveniently
3786 by merely adjusting the ptrace arguments and feeding into the
3787 generic call to ttrace at the bottom of this function.
3788
3789 Note that because all branches of this switch end in "return",
3790 there's no need for any "break" statements.
3791 */
3792 case PT_SETTRC:
3793 return parent_attach_all ();
3794
3795 case PT_RUREGS:
3796 tt_status = read_from_register_save_state (gdb_tid,
3797 tt_addr,
3798 &register_value,
3799 sizeof (register_value));
3800 if (tt_status < 0)
3801 return tt_status;
3802 return register_value;
3803
3804 case PT_WUREGS:
3805 register_value = (int) tt_data;
3806 tt_status = write_to_register_save_state (gdb_tid,
3807 tt_addr,
3808 &register_value,
3809 sizeof (register_value));
3810 return tt_status;
3811 break;
3812
3813 case PT_READ_I:
3814 tt_status = call_ttrace (TT_PROC_RDTEXT, /* Implicit 4-byte xfer becomes block-xfer. */
3815 gdb_tid,
3816 tt_addr,
3817 (TTRACE_ARG_TYPE) 4,
3818 (TTRACE_ARG_TYPE) & read_buf);
3819 if (tt_status < 0)
3820 return tt_status;
3821 return read_buf;
3822
3823 case PT_READ_D:
3824 tt_status = call_ttrace (TT_PROC_RDDATA, /* Implicit 4-byte xfer becomes block-xfer. */
3825 gdb_tid,
3826 tt_addr,
3827 (TTRACE_ARG_TYPE) 4,
3828 (TTRACE_ARG_TYPE) & read_buf);
3829 if (tt_status < 0)
3830 return tt_status;
3831 return read_buf;
3832
3833 case PT_ATTACH:
3834 tt_status = call_real_ttrace (TT_PROC_ATTACH,
3835 map_from_gdb_tid (gdb_tid),
3836 (lwpid_t) TT_NIL,
3837 tt_addr,
3838 (TTRACE_ARG_TYPE) TT_VERSION,
3839 tt_addr2);
3840 if (tt_status < 0)
3841 return tt_status;
3842 return tt_status;
3843
3844 /* The following cases are handled by merely adjusting the ptrace
3845 arguments and feeding into the generic call to ttrace.
3846 */
3847 case PT_DETACH:
3848 tt_request = TT_PROC_DETACH;
3849 break;
3850
3851 case PT_WRITE_I:
3852 tt_request = TT_PROC_WRTEXT; /* Translates 4-byte xfer to block-xfer. */
3853 tt_data = 4; /* This many bytes. */
3854 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3855 break;
3856
3857 case PT_WRITE_D:
3858 tt_request = TT_PROC_WRDATA; /* Translates 4-byte xfer to block-xfer. */
3859 tt_data = 4; /* This many bytes. */
3860 tt_addr2 = (TTRACE_ARG_TYPE) & data; /* Address of xfer source. */
3861 break;
3862
3863 case PT_RDTEXT:
3864 tt_request = TT_PROC_RDTEXT;
3865 break;
3866
3867 case PT_RDDATA:
3868 tt_request = TT_PROC_RDDATA;
3869 break;
3870
3871 case PT_WRTEXT:
3872 tt_request = TT_PROC_WRTEXT;
3873 break;
3874
3875 case PT_WRDATA:
3876 tt_request = TT_PROC_WRDATA;
3877 break;
3878
3879 case PT_CONTINUE:
3880 tt_request = TT_PROC_CONTINUE;
3881 break;
3882
3883 case PT_STEP:
3884 tt_request = TT_LWP_SINGLE; /* Should not be making this request? */
3885 break;
3886
3887 case PT_KILL:
3888 tt_request = TT_PROC_EXIT;
3889 break;
3890
3891 case PT_GET_PROCESS_PATHNAME:
3892 tt_request = TT_PROC_GET_PATHNAME;
3893 break;
3894
3895 default:
3896 tt_request = pt_request; /* Let ttrace be the one to complain. */
3897 break;
3898 }
3899
3900 return call_ttrace (tt_request,
3901 gdb_tid,
3902 tt_addr,
3903 tt_data,
3904 tt_addr2);
3905 }
3906
3907 /* Kill that pesky process!
3908 */
3909 void
3910 kill_inferior ()
3911 {
3912 int tid;
3913 int wait_status;
3914 thread_info *t;
3915 thread_info **paranoia;
3916 int para_count, i;
3917
3918 if (inferior_pid == 0)
3919 return;
3920
3921 /* Walk the list of "threads", some of which are "pseudo threads",
3922 aka "processes". For each that is NOT inferior_pid, stop it,
3923 and detach it.
3924
3925 You see, we may not have just a single process to kill. If we're
3926 restarting or quitting or detaching just after the inferior has
3927 forked, then we've actually two processes to clean up.
3928
3929 But we can't just call target_mourn_inferior() for each, since that
3930 zaps the target vector.
3931 */
3932
3933 paranoia = (thread_info **) malloc (thread_head.count *
3934 sizeof (thread_info *));
3935 para_count = 0;
3936
3937 t = thread_head.head;
3938 while (t)
3939 {
3940
3941 paranoia[para_count] = t;
3942 for (i = 0; i < para_count; i++)
3943 {
3944 if (t->next == paranoia[i])
3945 {
3946 warning ("Bad data in gdb's thread data; repairing.");
3947 t->next = 0;
3948 }
3949 }
3950 para_count++;
3951
3952 if (t->am_pseudo && (t->pid != inferior_pid))
3953 {
3954 /* TT_PROC_STOP doesn't require a subsequent ttrace_wait, as it
3955 * generates no event.
3956 */
3957 call_ttrace (TT_PROC_STOP,
3958 t->pid,
3959 TT_NIL,
3960 TT_NIL,
3961 TT_NIL);
3962
3963 call_ttrace (TT_PROC_DETACH,
3964 t->pid,
3965 TT_NIL,
3966 (TTRACE_ARG_TYPE) TARGET_SIGNAL_0,
3967 TT_NIL);
3968 }
3969 t = t->next;
3970 }
3971
3972 free (paranoia);
3973
3974 call_ttrace (TT_PROC_STOP,
3975 inferior_pid,
3976 TT_NIL,
3977 TT_NIL,
3978 TT_NIL);
3979 target_mourn_inferior ();
3980 clear_thread_info ();
3981 }
3982
3983
3984 #ifndef CHILD_RESUME
3985
3986 /* Sanity check a thread about to be continued.
3987 */
3988 static void
3989 thread_dropping_event_check (p)
3990 thread_info *p;
3991 {
3992 if (!p->handled)
3993 {
3994 /*
3995 * This seems to happen when we "next" over a
3996 * "fork()" while following the parent. If it's
3997 * the FORK event, that's ok. If it's a SIGNAL
3998 * in the unfollowed child, that's ok to--but
3999 * how can we know that's what's going on?
4000 *
4001 * FIXME!
4002 */
4003 if (p->have_state)
4004 {
4005 if (p->last_stop_state.tts_event == TTEVT_FORK)
4006 {
4007 /* Ok */
4008 ;
4009 }
4010 else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
4011 {
4012 /* Ok, close eyes and let it happen.
4013 */
4014 ;
4015 }
4016 else
4017 {
4018 /* This shouldn't happen--we're dropping a
4019 * real event.
4020 */
4021 warning ("About to continue process %d, thread %d with unhandled event %s.",
4022 p->pid, p->tid,
4023 get_printable_name_of_ttrace_event (
4024 p->last_stop_state.tts_event));
4025
4026 #ifdef PARANOIA
4027 if (debug_on)
4028 print_tthread (p);
4029 #endif
4030 }
4031 }
4032 else
4033 {
4034 /* No saved state, have to assume it failed.
4035 */
4036 warning ("About to continue process %d, thread %d with unhandled event.",
4037 p->pid, p->tid);
4038 #ifdef PARANOIA
4039 if (debug_on)
4040 print_tthread (p);
4041 #endif
4042 }
4043 }
4044
4045 } /* thread_dropping_event_check */
4046
4047 /* Use a loop over the threads to continue all the threads but
4048 * the one specified, which is to be stepped.
4049 */
4050 static void
4051 threads_continue_all_but_one (gdb_tid, signal)
4052 lwpid_t gdb_tid;
4053 int signal;
4054 {
4055 thread_info *p;
4056 int thread_signal;
4057 lwpid_t real_tid;
4058 lwpid_t scan_tid;
4059 ttstate_t state;
4060 int real_pid;
4061
4062 #ifdef THREAD_DEBUG
4063 if (debug_on)
4064 printf ("Using loop over threads to step/resume with signals\n");
4065 #endif
4066
4067 /* First update the thread list.
4068 */
4069 set_all_unseen ();
4070 real_tid = map_from_gdb_tid (gdb_tid);
4071 real_pid = get_pid_for (real_tid);
4072
4073 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4074 while (0 != scan_tid)
4075 {
4076
4077 #ifdef THREAD_DEBUG
4078 /* FIX: later should check state is stopped;
4079 * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
4080 */
4081 if (debug_on)
4082 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4083 printf ("About to continue non-stopped thread %d\n", scan_tid);
4084 #endif
4085
4086 p = find_thread_info (scan_tid);
4087 if (NULL == p)
4088 {
4089 add_tthread (real_pid, scan_tid);
4090 p = find_thread_info (scan_tid);
4091
4092 /* This is either a newly-created thread or the
4093 * result of a fork; in either case there's no
4094 * actual event to worry about.
4095 */
4096 p->handled = 1;
4097
4098 if (state.tts_event != TTEVT_NONE)
4099 {
4100 /* Oops, do need to worry!
4101 */
4102 warning ("Unexpected thread with \"%s\" event.",
4103 get_printable_name_of_ttrace_event (state.tts_event));
4104 }
4105 }
4106 else if (scan_tid != p->tid)
4107 error ("Bad data in thread database.");
4108
4109 #ifdef THREAD_DEBUG
4110 if (debug_on)
4111 if (p->terminated)
4112 printf ("Why are we continuing a dead thread?\n");
4113 #endif
4114
4115 p->seen = 1;
4116
4117 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4118 }
4119
4120 /* Remove unseen threads.
4121 */
4122 update_thread_list ();
4123
4124 /* Now run down the thread list and continue or step.
4125 */
4126 for (p = thread_head.head; p; p = p->next)
4127 {
4128
4129 /* Sanity check.
4130 */
4131 thread_dropping_event_check (p);
4132
4133 /* Pass the correct signals along.
4134 */
4135 if (p->have_signal)
4136 {
4137 thread_signal = p->signal_value;
4138 p->have_signal = 0;
4139 }
4140 else
4141 thread_signal = 0;
4142
4143 if (p->tid != real_tid)
4144 {
4145 /*
4146 * Not the thread of interest, so continue it
4147 * as the user expects.
4148 */
4149 if (p->stepping_mode == DO_STEP)
4150 {
4151 /* Just step this thread.
4152 */
4153 call_ttrace (
4154 TT_LWP_SINGLE,
4155 p->tid,
4156 TT_USE_CURRENT_PC,
4157 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4158 TT_NIL);
4159 }
4160 else
4161 {
4162 /* Regular continue (default case).
4163 */
4164 call_ttrace (
4165 TT_LWP_CONTINUE,
4166 p->tid,
4167 TT_USE_CURRENT_PC,
4168 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4169 TT_NIL);
4170 }
4171 }
4172 else
4173 {
4174 /* Step the thread of interest.
4175 */
4176 call_ttrace (
4177 TT_LWP_SINGLE,
4178 real_tid,
4179 TT_USE_CURRENT_PC,
4180 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4181 TT_NIL);
4182 }
4183 } /* Loop over threads */
4184 } /* End threads_continue_all_but_one */
4185
4186 /* Use a loop over the threads to continue all the threads.
4187 * This is done when a signal must be sent to any of the threads.
4188 */
4189 static void
4190 threads_continue_all_with_signals (gdb_tid, signal)
4191 lwpid_t gdb_tid;
4192 int signal;
4193 {
4194 thread_info *p;
4195 int thread_signal;
4196 lwpid_t real_tid;
4197 lwpid_t scan_tid;
4198 ttstate_t state;
4199 int real_pid;
4200
4201 #ifdef THREAD_DEBUG
4202 if (debug_on)
4203 printf ("Using loop over threads to resume with signals\n");
4204 #endif
4205
4206 /* Scan and update thread list.
4207 */
4208 set_all_unseen ();
4209 real_tid = map_from_gdb_tid (gdb_tid);
4210 real_pid = get_pid_for (real_tid);
4211
4212 scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
4213 while (0 != scan_tid)
4214 {
4215
4216 #ifdef THREAD_DEBUG
4217 if (debug_on)
4218 if (state.tts_flags & TTS_STATEMASK != TTS_WASSUSPENDED)
4219 warning ("About to continue non-stopped thread %d\n", scan_tid);
4220 #endif
4221
4222 p = find_thread_info (scan_tid);
4223 if (NULL == p)
4224 {
4225 add_tthread (real_pid, scan_tid);
4226 p = find_thread_info (scan_tid);
4227
4228 /* This is either a newly-created thread or the
4229 * result of a fork; in either case there's no
4230 * actual event to worry about.
4231 */
4232 p->handled = 1;
4233
4234 if (state.tts_event != TTEVT_NONE)
4235 {
4236 /* Oops, do need to worry!
4237 */
4238 warning ("Unexpected thread with \"%s\" event.",
4239 get_printable_name_of_ttrace_event (state.tts_event));
4240 }
4241 }
4242
4243 #ifdef THREAD_DEBUG
4244 if (debug_on)
4245 if (p->terminated)
4246 printf ("Why are we continuing a dead thread? (1)\n");
4247 #endif
4248
4249 p->seen = 1;
4250
4251 scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
4252 }
4253
4254 /* Remove unseen threads from our list.
4255 */
4256 update_thread_list ();
4257
4258 /* Continue the threads.
4259 */
4260 for (p = thread_head.head; p; p = p->next)
4261 {
4262
4263 /* Sanity check.
4264 */
4265 thread_dropping_event_check (p);
4266
4267 /* Pass the correct signals along.
4268 */
4269 if (p->tid == real_tid)
4270 {
4271 thread_signal = signal;
4272 p->have_signal = 0;
4273 }
4274 else if (p->have_signal)
4275 {
4276 thread_signal = p->signal_value;
4277 p->have_signal = 0;
4278 }
4279 else
4280 thread_signal = 0;
4281
4282 if (p->stepping_mode == DO_STEP)
4283 {
4284 call_ttrace (
4285 TT_LWP_SINGLE,
4286 p->tid,
4287 TT_USE_CURRENT_PC,
4288 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4289 TT_NIL);
4290 }
4291 else
4292 {
4293 /* Continue this thread (default case).
4294 */
4295 call_ttrace (
4296 TT_LWP_CONTINUE,
4297 p->tid,
4298 TT_USE_CURRENT_PC,
4299 (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
4300 TT_NIL);
4301 }
4302 }
4303 } /* End threads_continue_all_with_signals */
4304
4305 /* Step one thread only.
4306 */
4307 static void
4308 thread_fake_step (tid, signal)
4309 lwpid_t tid;
4310 enum target_signal signal;
4311 {
4312 thread_info *p;
4313
4314 #ifdef THREAD_DEBUG
4315 if (debug_on)
4316 {
4317 printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);
4318
4319 if (is_terminated (tid))
4320 printf ("Why are we continuing a dead thread? (4)\n");
4321 }
4322 #endif
4323
4324 if (doing_fake_step)
4325 warning ("Step while step already in progress.");
4326
4327 /* See if there's a saved signal value for this
4328 * thread to be passed on, but no current signal.
4329 */
4330 p = find_thread_info (tid);
4331 if (p != NULL)
4332 {
4333 if (p->have_signal && signal == NULL)
4334 {
4335 /* Pass on a saved signal.
4336 */
4337 signal = p->signal_value;
4338 }
4339
4340 p->have_signal = 0;
4341 }
4342
4343 if (!p->handled)
4344 warning ("Internal error: continuing unhandled thread.");
4345
4346 call_ttrace (TT_LWP_SINGLE,
4347 tid,
4348 TT_USE_CURRENT_PC,
4349 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4350 TT_NIL);
4351
4352 /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
4353 * for this thread only, and clear any saved signal info.
4354 */
4355 doing_fake_step = 1;
4356 fake_step_tid = tid;
4357
4358 } /* End thread_fake_step */
4359
4360 /* Continue one thread when a signal must be sent to it.
4361 */
4362 static void
4363 threads_continue_one_with_signal (gdb_tid, signal)
4364 lwpid_t gdb_tid;
4365 int signal;
4366 {
4367 thread_info *p;
4368 lwpid_t real_tid;
4369 int real_pid;
4370
4371 #ifdef THREAD_DEBUG
4372 if (debug_on)
4373 printf ("Continuing one thread with a signal\n");
4374 #endif
4375
4376 real_tid = map_from_gdb_tid (gdb_tid);
4377 real_pid = get_pid_for (real_tid);
4378
4379 p = find_thread_info (real_tid);
4380 if (NULL == p)
4381 {
4382 add_tthread (real_pid, real_tid);
4383 }
4384
4385 #ifdef THREAD_DEBUG
4386 if (debug_on)
4387 if (p->terminated)
4388 printf ("Why are we continuing a dead thread? (2)\n");
4389 #endif
4390
4391 if (!p->handled)
4392 warning ("Internal error: continuing unhandled thread.");
4393
4394 p->have_signal = 0;
4395
4396 call_ttrace (TT_LWP_CONTINUE,
4397 gdb_tid,
4398 TT_USE_CURRENT_PC,
4399 (TTRACE_ARG_TYPE) target_signal_to_host (signal),
4400 TT_NIL);
4401 }
4402 #endif
4403
4404 #ifndef CHILD_RESUME
4405
4406 /* Resume execution of the inferior process.
4407
4408 * This routine is in charge of setting the "handled" bits.
4409 *
4410 * If STEP is zero, continue it.
4411 * If STEP is nonzero, single-step it.
4412 *
4413 * If SIGNAL is nonzero, give it that signal.
4414 *
4415 * If TID is -1, apply to all threads.
4416 * If TID is not -1, apply to specified thread.
4417 *
4418 * STEP
4419 * \ !0 0
4420 * TID \________________________________________________
4421 * |
4422 * -1 | Step current Continue all threads
4423 * | thread and (but which gets any
4424 * | continue others signal?--We look at
4425 * | "inferior_pid")
4426 * |
4427 * N | Step _this_ thread Continue _this_ thread
4428 * | and leave others and leave others
4429 * | stopped; internally stopped; used only for
4430 * | used by gdb, never hardware watchpoints
4431 * | a user command. and attach, never a
4432 * | user command.
4433 */
4434 void
4435 child_resume (gdb_tid, step, signal)
4436 lwpid_t gdb_tid;
4437 int step;
4438 enum target_signal signal;
4439 {
4440 int resume_all_threads;
4441 lwpid_t tid;
4442 process_state_t new_process_state;
4443
4444 resume_all_threads =
4445 (gdb_tid == INFTTRACE_ALL_THREADS) ||
4446 (vfork_in_flight);
4447
4448 if (resume_all_threads)
4449 {
4450 /* Resume all threads, but first pick a tid value
4451 * so we can get the pid when in call_ttrace doing
4452 * the map.
4453 */
4454 if (vfork_in_flight)
4455 tid = vforking_child_pid;
4456 else
4457 tid = map_from_gdb_tid (inferior_pid);
4458 }
4459 else
4460 tid = map_from_gdb_tid (gdb_tid);
4461
4462 #ifdef THREAD_DEBUG
4463 if (debug_on)
4464 {
4465 if (more_events_left)
4466 printf ("More events; ");
4467
4468 if (signal != 0)
4469 printf ("Sending signal %d; ", signal);
4470
4471 if (resume_all_threads)
4472 {
4473 if (step == 0)
4474 printf ("Continue process %d\n", tid);
4475 else
4476 printf ("Step/continue thread %d\n", tid);
4477 }
4478 else
4479 {
4480 if (step == 0)
4481 printf ("Continue thread %d\n", tid);
4482 else
4483 printf ("Step just thread %d\n", tid);
4484 }
4485
4486 if (vfork_in_flight)
4487 printf ("Vfork in flight\n");
4488 }
4489 #endif
4490
4491 if (process_state == RUNNING)
4492 warning ("Internal error in resume logic; doing resume or step anyway.");
4493
4494 if (!step /* Asked to continue... */
4495 && resume_all_threads /* whole process.. */
4496 && signal != 0 /* with a signal... */
4497 && more_events_left > 0)
4498 { /* but we can't yet--save it! */
4499
4500 /* Continue with signal means we have to set the pending
4501 * signal value for this thread.
4502 */
4503 thread_info *k;
4504
4505 #ifdef THREAD_DEBUG
4506 if (debug_on)
4507 printf ("Saving signal %d for thread %d\n", signal, tid);
4508 #endif
4509
4510 k = find_thread_info (tid);
4511 if (k != NULL)
4512 {
4513 k->have_signal = 1;
4514 k->signal_value = signal;
4515
4516 #ifdef THREAD_DEBUG
4517 if (debug_on)
4518 if (k->terminated)
4519 printf ("Why are we continuing a dead thread? (3)\n");
4520 #endif
4521
4522 }
4523
4524 #ifdef THREAD_DEBUG
4525 else if (debug_on)
4526 {
4527 printf ("No thread info for tid %d\n", tid);
4528 }
4529 #endif
4530 }
4531
4532 /* Are we faking this "continue" or "step"?
4533
4534 * We used to do steps by continuing all the threads for
4535 * which the events had been handled already. While
4536 * conceptually nicer (hides it all in a lower level), this
4537 * can lead to starvation and a hang (e.g. all but one thread
4538 * are unhandled at a breakpoint just before a "join" operation,
4539 * and one thread is in the join, and the user wants to step that
4540 * thread).
4541 */
4542 if (resume_all_threads /* Whole process, therefore user command */
4543 && more_events_left > 0)
4544 { /* But we can't do this yet--fake it! */
4545 thread_info *p;
4546
4547 if (!step)
4548 {
4549 /* No need to do any notes on a per-thread
4550 * basis--we're done!
4551 */
4552 #ifdef WAIT_BUFFER_DEBUG
4553 if (debug_on)
4554 printf ("Faking a process resume.\n");
4555 #endif
4556
4557 return;
4558 }
4559 else
4560 {
4561
4562 #ifdef WAIT_BUFFER_DEBUG
4563 if (debug_on)
4564 printf ("Faking a process step.\n");
4565 #endif
4566
4567 }
4568
4569 p = find_thread_info (tid);
4570 if (p == NULL)
4571 {
4572 warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
4573 return;
4574 }
4575 else
4576 {
4577
4578 #ifdef THREAD_DEBUG
4579 if (debug_on)
4580 if (p->terminated)
4581 printf ("Why are we continuing a dead thread? (3.5)\n");
4582 #endif
4583
4584 if (p->stepping_mode != DO_DEFAULT)
4585 {
4586 warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");
4587
4588 return;
4589 }
4590
4591 if (step)
4592 p->stepping_mode = DO_STEP;
4593 else
4594 p->stepping_mode = DO_CONTINUE;
4595
4596 return;
4597 } /* Have thread info */
4598 } /* Must fake step or go */
4599
4600 /* Execept for fake-steps, from here on we know we are
4601 * going to wind up with a running process which will
4602 * need a real wait.
4603 */
4604 new_process_state = RUNNING;
4605
4606 /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
4607 * it was. (If GDB wanted it to start some other way, we have already
4608 * written a new PC value to the child.)
4609 *
4610 * If this system does not support PT_STEP, a higher level function will
4611 * have called single_step() to transmute the step request into a
4612 * continue request (by setting breakpoints on all possible successor
4613 * instructions), so we don't have to worry about that here.
4614 */
4615 if (step)
4616 {
4617 if (resume_all_threads)
4618 {
4619 /*
4620 * Regular user step: other threads get a "continue".
4621 */
4622 threads_continue_all_but_one (tid, signal);
4623 clear_all_handled ();
4624 clear_all_stepping_mode ();
4625 }
4626
4627 else
4628 {
4629 /* "Fake step": gdb is stepping one thread over a
4630 * breakpoint, watchpoint, or out of a library load
4631 * event, etc. The rest just stay where they are.
4632 *
4633 * Also used when there are pending events: we really
4634 * step the current thread, but leave the rest stopped.
4635 * Users can't request this, but "wait_for_inferior"
4636 * does--a lot!
4637 */
4638 thread_fake_step (tid, signal);
4639
4640 /* Clear the "handled" state of this thread, because
4641 * we'll soon get a new event for it. Other events
4642 * stay as they were.
4643 */
4644 clear_handled (tid);
4645 clear_stepping_mode (tid);
4646 new_process_state = FAKE_STEPPING;
4647 }
4648 }
4649
4650 else
4651 {
4652 /* TT_LWP_CONTINUE can pass signals to threads,
4653 * TT_PROC_CONTINUE can't. So if there are any
4654 * signals to pass, we have to use the (slower)
4655 * loop over the stopped threads.
4656 *
4657 * Equally, if we have to not continue some threads,
4658 * due to saved events, we have to use the loop.
4659 */
4660 if ((signal != 0) || saved_signals_exist ())
4661 {
4662 if (resume_all_threads)
4663 {
4664
4665 #ifdef THREAD_DEBUG
4666 if (debug_on)
4667 printf ("Doing a continue by loop of all threads\n");
4668 #endif
4669
4670 threads_continue_all_with_signals (tid, signal);
4671
4672 clear_all_handled ();
4673 clear_all_stepping_mode ();
4674 }
4675
4676 else
4677 {
4678 #ifdef THREAD_DEBUG
4679 printf ("Doing a continue w/signal of just thread %d\n", tid);
4680 #endif
4681
4682 threads_continue_one_with_signal (tid, signal);
4683
4684 /* Clear the "handled" state of this thread, because
4685 * we'll soon get a new event for it. Other events
4686 * can stay as they were.
4687 */
4688 clear_handled (tid);
4689 clear_stepping_mode (tid);
4690 }
4691 }
4692
4693 else
4694 {
4695 /* No signals to send.
4696 */
4697 if (resume_all_threads)
4698 {
4699 #ifdef THREAD_DEBUG
4700 if (debug_on)
4701 printf ("Doing a continue by process of process %d\n", tid);
4702 #endif
4703
4704 if (more_events_left > 0)
4705 {
4706 warning ("Losing buffered events on continue.");
4707 more_events_left = 0;
4708 }
4709
4710 call_ttrace (TT_PROC_CONTINUE,
4711 tid,
4712 TT_NIL,
4713 TT_NIL,
4714 TT_NIL);
4715
4716 clear_all_handled ();
4717 clear_all_stepping_mode ();
4718 }
4719
4720 else
4721 {
4722 #ifdef THREAD_DEBUG
4723 if (debug_on)
4724 {
4725 printf ("Doing a continue of just thread %d\n", tid);
4726 if (is_terminated (tid))
4727 printf ("Why are we continuing a dead thread? (5)\n");
4728 }
4729 #endif
4730
4731 call_ttrace (TT_LWP_CONTINUE,
4732 tid,
4733 TT_NIL,
4734 TT_NIL,
4735 TT_NIL);
4736
4737 /* Clear the "handled" state of this thread, because
4738 * we'll soon get a new event for it. Other events
4739 * can stay as they were.
4740 */
4741 clear_handled (tid);
4742 clear_stepping_mode (tid);
4743 }
4744 }
4745 }
4746
4747 process_state = new_process_state;
4748
4749 #ifdef WAIT_BUFFER_DEBUG
4750 if (debug_on)
4751 printf ("Process set to %s\n",
4752 get_printable_name_of_process_state (process_state));
4753 #endif
4754
4755 }
4756 #endif /* CHILD_RESUME */
4757 \f
4758
4759 #ifdef ATTACH_DETACH
4760 /*
4761 * Like it says.
4762 *
4763 * One worry is that we may not be attaching to "inferior_pid"
4764 * and thus may not want to clear out our data. FIXME?
4765 *
4766 */
4767 static void
4768 update_thread_state_after_attach (pid, kind_of_go)
4769 int pid;
4770 attach_continue_t kind_of_go;
4771 {
4772 int tt_status;
4773 ttstate_t thread_state;
4774 lwpid_t a_thread;
4775 lwpid_t tid;
4776
4777 /* The process better be stopped.
4778 */
4779 if (process_state != STOPPED
4780 && process_state != VFORKING)
4781 warning ("Internal error attaching.");
4782
4783 /* Clear out old tthread info and start over. This has the
4784 * side effect of ensuring that the TRAP is reported as being
4785 * in the right thread (re-mapped from tid to pid).
4786 *
4787 * It's because we need to add the tthread _now_ that we
4788 * need to call "clear_thread_info" _now_, and that's why
4789 * "require_notification_of_events" doesn't clear the thread
4790 * info (it's called later than this routine).
4791 */
4792 clear_thread_info ();
4793 a_thread = 0;
4794
4795 for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
4796 tid != 0;
4797 tid = get_process_next_stopped_thread_id (pid, &thread_state))
4798 {
4799 thread_info *p;
4800
4801 if (a_thread == 0)
4802 {
4803 a_thread = tid;
4804 #ifdef THREAD_DEBUG
4805 if (debug_on)
4806 printf ("Attaching to process %d, thread %d\n",
4807 pid, a_thread);
4808 #endif
4809 }
4810
4811 /* Tell ourselves and the "rest of gdb" that this thread
4812 * exists.
4813 *
4814 * This isn't really a hack. Other thread-based versions
4815 * of gdb (e.g. gnu-nat.c) seem to do the same thing.
4816 *
4817 * We don't need to do mapping here, as we know this
4818 * is the first thread and thus gets the real pid
4819 * (and is "inferior_pid").
4820 *
4821 * NOTE: it probably isn't the originating thread,
4822 * but that doesn't matter (we hope!).
4823 */
4824 add_tthread (pid, tid);
4825 p = find_thread_info (tid);
4826 if (NULL == p) /* ?We just added it! */
4827 error ("Internal error adding a thread on attach.");
4828
4829 copy_ttstate_t (&p->last_stop_state, thread_state);
4830 p->have_state = 1;
4831
4832 if (DO_ATTACH_CONTINUE == kind_of_go)
4833 {
4834 /*
4835 * If we are going to CONTINUE afterwards,
4836 * raising a SIGTRAP, don't bother trying to
4837 * handle this event. But check first!
4838 */
4839 switch (p->last_stop_state.tts_event)
4840 {
4841
4842 case TTEVT_NONE:
4843 /* Ok to set this handled.
4844 */
4845 break;
4846
4847 default:
4848 warning ("Internal error; skipping event %s on process %d, thread %d.",
4849 get_printable_name_of_ttrace_event (
4850 p->last_stop_state.tts_event),
4851 p->pid, p->tid);
4852 }
4853
4854 set_handled (pid, tid);
4855
4856 }
4857 else
4858 {
4859 /* There will be no "continue" opertion, so the
4860 * process remains stopped. Don't set any events
4861 * handled except the "gimmies".
4862 */
4863 switch (p->last_stop_state.tts_event)
4864 {
4865
4866 case TTEVT_NONE:
4867 /* Ok to ignore this.
4868 */
4869 set_handled (pid, tid);
4870 break;
4871
4872 case TTEVT_EXEC:
4873 case TTEVT_FORK:
4874 /* Expected "other" FORK or EXEC event from a
4875 * fork or vfork.
4876 */
4877 break;
4878
4879 default:
4880 printf ("Internal error: failed to handle event %s on process %d, thread %d.",
4881 get_printable_name_of_ttrace_event (
4882 p->last_stop_state.tts_event),
4883 p->pid, p->tid);
4884 }
4885 }
4886
4887 add_thread (tid); /* in thread.c */
4888 }
4889
4890 #ifdef PARANOIA
4891 if (debug_on)
4892 print_tthreads ();
4893 #endif
4894
4895 /* One mustn't call ttrace_wait() after attaching via ttrace,
4896 'cause the process is stopped already.
4897
4898 However, the upper layers of gdb's execution control will
4899 want to wait after attaching (but not after forks, in
4900 which case they will be doing a "target_resume", anticipating
4901 a later TTEVT_EXEC or TTEVT_FORK event).
4902
4903 To make this attach() implementation more compatible with
4904 others, we'll make the attached-to process raise a SIGTRAP.
4905
4906 Issue: this continues only one thread. That could be
4907 dangerous if the thread is blocked--the process won't run
4908 and no trap will be raised. FIX! (check state.tts_flags?
4909 need one that's either TTS_WASRUNNING--but we've stopped
4910 it and made it TTS_WASSUSPENDED. Hum...FIXME!)
4911 */
4912 if (DO_ATTACH_CONTINUE == kind_of_go)
4913 {
4914 tt_status = call_real_ttrace (
4915 TT_LWP_CONTINUE,
4916 pid,
4917 a_thread,
4918 TT_USE_CURRENT_PC,
4919 (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
4920 TT_NIL);
4921 if (errno)
4922 perror_with_name ("ttrace");
4923
4924 clear_handled (a_thread); /* So TRAP will be reported. */
4925
4926 /* Now running.
4927 */
4928 process_state = RUNNING;
4929 }
4930
4931 attach_flag = 1;
4932 }
4933 #endif /* ATTACH_DETACH */
4934 \f
4935
4936 #ifdef ATTACH_DETACH
4937 /* Start debugging the process whose number is PID.
4938 * (A _real_ pid).
4939 */
4940 int
4941 attach (pid)
4942 int pid;
4943 {
4944 int tt_status;
4945
4946 tt_status = call_real_ttrace (
4947 TT_PROC_ATTACH,
4948 pid,
4949 (lwpid_t) TT_NIL,
4950 TT_NIL,
4951 (TTRACE_ARG_TYPE) TT_VERSION,
4952 TT_NIL);
4953 if (errno)
4954 perror_with_name ("ttrace attach");
4955
4956 /* If successful, the process is now stopped.
4957 */
4958 process_state = STOPPED;
4959
4960 /* Our caller ("attach_command" in "infcmd.c")
4961 * expects to do a "wait_for_inferior" after
4962 * the attach, so make sure the inferior is
4963 * running when we're done.
4964 */
4965 update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);
4966
4967 return pid;
4968 }
4969
4970
4971 #if defined(CHILD_POST_ATTACH)
4972 void
4973 child_post_attach (pid)
4974 int pid;
4975 {
4976 #ifdef THREAD_DEBUG
4977 if (debug_on)
4978 printf ("child-post-attach call\n");
4979 #endif
4980
4981 require_notification_of_events (pid);
4982 }
4983 #endif
4984
4985
4986 /* Stop debugging the process whose number is PID
4987 and continue it with signal number SIGNAL.
4988 SIGNAL = 0 means just continue it.
4989 */
4990 void
4991 detach (signal)
4992 int signal;
4993 {
4994 errno = 0;
4995 call_ttrace (TT_PROC_DETACH,
4996 inferior_pid,
4997 TT_NIL,
4998 (TTRACE_ARG_TYPE) signal,
4999 TT_NIL);
5000 attach_flag = 0;
5001
5002 clear_thread_info ();
5003
5004 /* Process-state? */
5005 }
5006 #endif /* ATTACH_DETACH */
5007 \f
5008
5009 /* Default the type of the ttrace transfer to int. */
5010 #ifndef TTRACE_XFER_TYPE
5011 #define TTRACE_XFER_TYPE int
5012 #endif
5013
5014 void
5015 _initialize_kernel_u_addr ()
5016 {
5017 }
5018
5019 #if !defined (CHILD_XFER_MEMORY)
5020 /* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
5021 in the NEW_SUN_TTRACE case.
5022 It ought to be straightforward. But it appears that writing did
5023 not write the data that I specified. I cannot understand where
5024 it got the data that it actually did write. */
5025
5026 /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
5027 to debugger memory starting at MYADDR. Copy to inferior if
5028 WRITE is nonzero.
5029
5030 Returns the length copied, which is either the LEN argument or zero.
5031 This xfer function does not do partial moves, since child_ops
5032 doesn't allow memory operations to cross below us in the target stack
5033 anyway. */
5034
5035 int
5036 child_xfer_memory (memaddr, myaddr, len, write, target)
5037 CORE_ADDR memaddr;
5038 char *myaddr;
5039 int len;
5040 int write;
5041 struct target_ops *target; /* ignored */
5042 {
5043 register int i;
5044 /* Round starting address down to longword boundary. */
5045 register CORE_ADDR addr = memaddr & -sizeof (TTRACE_XFER_TYPE);
5046 /* Round ending address up; get number of longwords that makes. */
5047 register int count
5048 = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
5049 / sizeof (TTRACE_XFER_TYPE);
5050 /* Allocate buffer of that many longwords. */
5051 register TTRACE_XFER_TYPE *buffer
5052 = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));
5053
5054 if (write)
5055 {
5056 /* Fill start and end extra bytes of buffer with existing memory data. */
5057
5058 if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
5059 {
5060 /* Need part of initial word -- fetch it. */
5061 buffer[0] = call_ttrace (TT_LWP_RDTEXT,
5062 inferior_pid,
5063 (TTRACE_ARG_TYPE) addr,
5064 TT_NIL,
5065 TT_NIL);
5066 }
5067
5068 if (count > 1) /* FIXME, avoid if even boundary */
5069 {
5070 buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
5071 inferior_pid,
5072 ((TTRACE_ARG_TYPE)
5073 (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
5074 TT_NIL,
5075 TT_NIL);
5076 }
5077
5078 /* Copy data to be written over corresponding part of buffer */
5079
5080 memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5081 myaddr,
5082 len);
5083
5084 /* Write the entire buffer. */
5085
5086 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5087 {
5088 errno = 0;
5089 call_ttrace (TT_LWP_WRDATA,
5090 inferior_pid,
5091 (TTRACE_ARG_TYPE) addr,
5092 (TTRACE_ARG_TYPE) buffer[i],
5093 TT_NIL);
5094 if (errno)
5095 {
5096 /* Using the appropriate one (I or D) is necessary for
5097 Gould NP1, at least. */
5098 errno = 0;
5099 call_ttrace (TT_LWP_WRTEXT,
5100 inferior_pid,
5101 (TTRACE_ARG_TYPE) addr,
5102 (TTRACE_ARG_TYPE) buffer[i],
5103 TT_NIL);
5104 }
5105 if (errno)
5106 return 0;
5107 }
5108 }
5109 else
5110 {
5111 /* Read all the longwords */
5112 for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
5113 {
5114 errno = 0;
5115 buffer[i] = call_ttrace (TT_LWP_RDTEXT,
5116 inferior_pid,
5117 (TTRACE_ARG_TYPE) addr,
5118 TT_NIL,
5119 TT_NIL);
5120 if (errno)
5121 return 0;
5122 QUIT;
5123 }
5124
5125 /* Copy appropriate bytes out of the buffer. */
5126 memcpy (myaddr,
5127 (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
5128 len);
5129 }
5130 return len;
5131 }
5132 \f
5133
5134 static void
5135 udot_info ()
5136 {
5137 int udot_off; /* Offset into user struct */
5138 int udot_val; /* Value from user struct at udot_off */
5139 char mess[128]; /* For messages */
5140
5141 if (!target_has_execution)
5142 {
5143 error ("The program is not being run.");
5144 }
5145
5146 #if !defined (KERNEL_U_SIZE)
5147
5148 /* Adding support for this command is easy. Typically you just add a
5149 routine, called "kernel_u_size" that returns the size of the user
5150 struct, to the appropriate *-nat.c file and then add to the native
5151 config file "#define KERNEL_U_SIZE kernel_u_size()" */
5152 error ("Don't know how large ``struct user'' is in this version of gdb.");
5153
5154 #else
5155
5156 for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
5157 {
5158 if ((udot_off % 24) == 0)
5159 {
5160 if (udot_off > 0)
5161 {
5162 printf_filtered ("\n");
5163 }
5164 printf_filtered ("%04x:", udot_off);
5165 }
5166 udot_val = call_ttrace (TT_LWP_RUREGS,
5167 inferior_pid,
5168 (TTRACE_ARG_TYPE) udot_off,
5169 TT_NIL,
5170 TT_NIL);
5171 if (errno != 0)
5172 {
5173 sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
5174 perror_with_name (mess);
5175 }
5176 /* Avoid using nonportable (?) "*" in print specs */
5177 printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
5178 }
5179 printf_filtered ("\n");
5180
5181 #endif
5182 }
5183 #endif /* !defined (CHILD_XFER_MEMORY). */
5184
5185 /* TTrace version of "target_pid_to_exec_file"
5186 */
5187 char *
5188 child_pid_to_exec_file (tid)
5189 int tid;
5190 {
5191 static char exec_file_buffer[1024];
5192 int tt_status;
5193 CORE_ADDR top_of_stack;
5194 char four_chars[4];
5195 int name_index;
5196 int i;
5197 int done;
5198 int saved_inferior_pid;
5199
5200 /* As of 10.x HP-UX, there's an explicit request to get the
5201 *pathname.
5202 */
5203 tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
5204 tid,
5205 (TTRACE_ARG_TYPE) exec_file_buffer,
5206 (TTRACE_ARG_TYPE) sizeof (exec_file_buffer) - 1,
5207 TT_NIL);
5208 if (tt_status >= 0)
5209 return exec_file_buffer;
5210
5211 /* ??rehrauer: The above request may or may not be broken. It
5212 doesn't seem to work when I use it. But, it may be designed
5213 to only work immediately after an exec event occurs. (I'm
5214 waiting for COSL to explain.)
5215
5216 In any case, if it fails, try a really, truly amazingly gross
5217 hack that DDE uses, of pawing through the process' data
5218 segment to find the pathname.
5219 */
5220 top_of_stack = 0x7b03a000;
5221 name_index = 0;
5222 done = 0;
5223
5224 /* On the chance that pid != inferior_pid, set inferior_pid
5225 to pid, so that (grrrr!) implicit uses of inferior_pid get
5226 the right id.
5227 */
5228 saved_inferior_pid = inferior_pid;
5229 inferior_pid = tid;
5230
5231 /* Try to grab a null-terminated string. */
5232 while (!done)
5233 {
5234 if (target_read_memory (top_of_stack, four_chars, 4) != 0)
5235 {
5236 inferior_pid = saved_inferior_pid;
5237 return NULL;
5238 }
5239 for (i = 0; i < 4; i++)
5240 {
5241 exec_file_buffer[name_index++] = four_chars[i];
5242 done = (four_chars[i] == '\0');
5243 if (done)
5244 break;
5245 }
5246 top_of_stack += 4;
5247 }
5248
5249 if (exec_file_buffer[0] == '\0')
5250 {
5251 inferior_pid = saved_inferior_pid;
5252 return NULL;
5253 }
5254
5255 inferior_pid = saved_inferior_pid;
5256 return exec_file_buffer;
5257 }
5258
5259
5260 void
5261 pre_fork_inferior ()
5262 {
5263 int status;
5264
5265 status = pipe (startup_semaphore.parent_channel);
5266 if (status < 0)
5267 {
5268 warning ("error getting parent pipe for startup semaphore");
5269 return;
5270 }
5271
5272 status = pipe (startup_semaphore.child_channel);
5273 if (status < 0)
5274 {
5275 warning ("error getting child pipe for startup semaphore");
5276 return;
5277 }
5278 }
5279
5280 /* Called via #define REQUIRE_ATTACH from inftarg.c,
5281 * ultimately from "follow_inferior_fork" in infrun.c,
5282 * itself called from "resume".
5283 *
5284 * This seems to be intended to attach after a fork or
5285 * vfork, while "attach" is used to attach to a pid
5286 * given by the user. The check for an existing attach
5287 * seems odd--it always fails in our test system.
5288 */
5289 int
5290 hppa_require_attach (pid)
5291 int pid;
5292 {
5293 int tt_status;
5294 CORE_ADDR pc;
5295 CORE_ADDR pc_addr;
5296 unsigned int regs_offset;
5297 process_state_t old_process_state = process_state;
5298
5299 /* Are we already attached? There appears to be no explicit
5300 * way to answer this via ttrace, so we try something which
5301 * should be innocuous if we are attached. If that fails,
5302 * then we assume we're not attached, and so attempt to make
5303 * it so.
5304 */
5305 errno = 0;
5306 tt_status = call_real_ttrace (TT_PROC_STOP,
5307 pid,
5308 (lwpid_t) TT_NIL,
5309 (TTRACE_ARG_TYPE) TT_NIL,
5310 (TTRACE_ARG_TYPE) TT_NIL,
5311 TT_NIL);
5312
5313 if (errno)
5314 {
5315 /* No change to process-state!
5316 */
5317 errno = 0;
5318 pid = attach (pid);
5319 }
5320 else
5321 {
5322 /* If successful, the process is now stopped. But if
5323 * we're VFORKING, the parent is still running, so don't
5324 * change the process state.
5325 */
5326 if (process_state != VFORKING)
5327 process_state = STOPPED;
5328
5329 /* If we were already attached, you'd think that we
5330 * would need to start going again--but you'd be wrong,
5331 * as the fork-following code is actually in the middle
5332 * of the "resume" routine in in "infrun.c" and so
5333 * will (almost) immediately do a resume.
5334 *
5335 * On the other hand, if we are VFORKING, which means
5336 * that the child and the parent share a process for a
5337 * while, we know that "resume" won't be resuming
5338 * until the child EXEC event is seen. But we still
5339 * don't want to continue, as the event is already
5340 * there waiting.
5341 */
5342 update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
5343 } /* STOP succeeded */
5344
5345 return pid;
5346 }
5347
5348 int
5349 hppa_require_detach (pid, signal)
5350 int pid;
5351 int signal;
5352 {
5353 int tt_status;
5354
5355 /* If signal is non-zero, we must pass the signal on to the active
5356 thread prior to detaching. We do this by continuing the threads
5357 with the signal.
5358 */
5359 if (signal != 0)
5360 {
5361 errno = 0;
5362 threads_continue_all_with_signals (pid, signal);
5363 }
5364
5365 errno = 0;
5366 tt_status = call_ttrace (TT_PROC_DETACH,
5367 pid,
5368 TT_NIL,
5369 TT_NIL,
5370 TT_NIL);
5371
5372 errno = 0; /* Ignore any errors. */
5373
5374 /* process_state? */
5375
5376 return pid;
5377 }
5378
5379 /* Given the starting address of a memory page, hash it to a bucket in
5380 the memory page dictionary.
5381 */
5382 static int
5383 get_dictionary_bucket_of_page (page_start)
5384 CORE_ADDR page_start;
5385 {
5386 int hash;
5387
5388 hash = (page_start / memory_page_dictionary.page_size);
5389 hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;
5390
5391 return hash;
5392 }
5393
5394
5395 /* Given a memory page's starting address, get (i.e., find an existing
5396 or create a new) dictionary entry for the page. The page will be
5397 write-protected when this function returns, but may have a reference
5398 count of 0 (if the page was newly-added to the dictionary).
5399 */
5400 static memory_page_t *
5401 get_dictionary_entry_of_page (pid, page_start)
5402 int pid;
5403 CORE_ADDR page_start;
5404 {
5405 int bucket;
5406 memory_page_t *page = NULL;
5407 memory_page_t *previous_page = NULL;
5408
5409 /* We're going to be using the dictionary now, than-kew. */
5410 require_memory_page_dictionary (pid);
5411
5412 /* Try to find an existing dictionary entry for this page. Hash
5413 on the page's starting address.
5414 */
5415 bucket = get_dictionary_bucket_of_page (page_start);
5416 page = &memory_page_dictionary.buckets[bucket];
5417 while (page != NULL)
5418 {
5419 if (page->page_start == page_start)
5420 break;
5421 previous_page = page;
5422 page = page->next;
5423 }
5424
5425 /* Did we find a dictionary entry for this page? If not, then
5426 add it to the dictionary now.
5427 */
5428 if (page == NULL)
5429 {
5430 /* Create a new entry. */
5431 page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
5432 page->page_start = page_start;
5433 page->reference_count = 0;
5434 page->next = NULL;
5435 page->previous = NULL;
5436
5437 /* We'll write-protect the page now, if that's allowed. */
5438 page->original_permissions = write_protect_page (pid, page_start);
5439
5440 /* Add the new entry to the dictionary. */
5441 page->previous = previous_page;
5442 previous_page->next = page;
5443
5444 memory_page_dictionary.page_count++;
5445 }
5446
5447 return page;
5448 }
5449
5450
5451 static void
5452 remove_dictionary_entry_of_page (pid, page)
5453 int pid;
5454 memory_page_t *page;
5455 {
5456 /* Restore the page's original permissions. */
5457 unwrite_protect_page (pid, page->page_start, page->original_permissions);
5458
5459 /* Kick the page out of the dictionary. */
5460 if (page->previous != NULL)
5461 page->previous->next = page->next;
5462 if (page->next != NULL)
5463 page->next->previous = page->previous;
5464
5465 /* Just in case someone retains a handle to this after it's freed. */
5466 page->page_start = (CORE_ADDR) 0;
5467
5468 memory_page_dictionary.page_count--;
5469
5470 free (page);
5471 }
5472
5473
5474 static void
5475 hppa_enable_syscall_events (pid)
5476 int pid;
5477 {
5478 int tt_status;
5479 ttevent_t ttrace_events;
5480
5481 /* Get the set of events that are currently enabled. */
5482 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5483 pid,
5484 (TTRACE_ARG_TYPE) & ttrace_events,
5485 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5486 TT_NIL);
5487 if (errno)
5488 perror_with_name ("ttrace");
5489
5490 /* Add syscall events to that set. */
5491 ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
5492 ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;
5493
5494 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5495 pid,
5496 (TTRACE_ARG_TYPE) & ttrace_events,
5497 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5498 TT_NIL);
5499 if (errno)
5500 perror_with_name ("ttrace");
5501 }
5502
5503
5504 static void
5505 hppa_disable_syscall_events (pid)
5506 int pid;
5507 {
5508 int tt_status;
5509 ttevent_t ttrace_events;
5510
5511 /* Get the set of events that are currently enabled. */
5512 tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
5513 pid,
5514 (TTRACE_ARG_TYPE) & ttrace_events,
5515 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5516 TT_NIL);
5517 if (errno)
5518 perror_with_name ("ttrace");
5519
5520 /* Remove syscall events from that set. */
5521 ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
5522 ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;
5523
5524 tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
5525 pid,
5526 (TTRACE_ARG_TYPE) & ttrace_events,
5527 (TTRACE_ARG_TYPE) sizeof (ttrace_events),
5528 TT_NIL);
5529 if (errno)
5530 perror_with_name ("ttrace");
5531 }
5532
5533
5534 /* The address range beginning with START and ending with START+LEN-1
5535 (inclusive) is to be watched via page-protection by a new watchpoint.
5536 Set protection for all pages that overlap that range.
5537
5538 Note that our caller sets TYPE to:
5539 0 for a bp_hardware_watchpoint,
5540 1 for a bp_read_watchpoint,
5541 2 for a bp_access_watchpoint
5542
5543 (Yes, this is intentionally (though lord only knows why) different
5544 from the TYPE that is passed to hppa_remove_hw_watchpoint.)
5545 */
5546 int
5547 hppa_insert_hw_watchpoint (pid, start, len, type)
5548 int pid;
5549 CORE_ADDR start;
5550 LONGEST len;
5551 int type;
5552 {
5553 CORE_ADDR page_start;
5554 int dictionary_was_empty;
5555 int page_size;
5556 int page_id;
5557 LONGEST range_size_in_pages;
5558
5559 if (type != 0)
5560 error ("read or access hardware watchpoints not supported on HP-UX");
5561
5562 /* Examine all pages in the address range. */
5563 require_memory_page_dictionary ();
5564
5565 dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5566
5567 page_size = memory_page_dictionary.page_size;
5568 page_start = (start / page_size) * page_size;
5569 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5570
5571 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5572 {
5573 memory_page_t *page;
5574
5575 /* This gets the page entered into the dictionary if it was
5576 not already entered.
5577 */
5578 page = get_dictionary_entry_of_page (pid, page_start);
5579 page->reference_count++;
5580 }
5581
5582 /* Our implementation depends on seeing calls to kernel code, for the
5583 following reason. Here we ask to be notified of syscalls.
5584
5585 When a protected page is accessed by user code, HP-UX raises a SIGBUS.
5586 Fine.
5587
5588 But when kernel code accesses the page, it doesn't give a SIGBUS.
5589 Rather, the system call that touched the page fails, with errno=EFAULT.
5590 Not good for us.
5591
5592 We could accomodate this "feature" by asking to be notified of syscall
5593 entries & exits; upon getting an entry event, disabling page-protections;
5594 upon getting an exit event, reenabling page-protections and then checking
5595 if any watchpoints triggered.
5596
5597 However, this turns out to be a real performance loser. syscalls are
5598 usually a frequent occurrence. Having to unprotect-reprotect all watched
5599 pages, and also to then read all watched memory locations and compare for
5600 triggers, can be quite expensive.
5601
5602 Instead, we'll only ask to be notified of syscall exits. When we get
5603 one, we'll check whether errno is set. If not, or if it's not EFAULT,
5604 we can just continue the inferior.
5605
5606 If errno is set upon syscall exit to EFAULT, we must perform some fairly
5607 hackish stuff to determine whether the failure really was due to a
5608 page-protect trap on a watched location.
5609 */
5610 if (dictionary_was_empty)
5611 hppa_enable_syscall_events (pid);
5612
5613 return 1;
5614 }
5615
5616
5617 /* The address range beginning with START and ending with START+LEN-1
5618 (inclusive) was being watched via page-protection by a watchpoint
5619 which has been removed. Remove protection for all pages that
5620 overlap that range, which are not also being watched by other
5621 watchpoints.
5622 */
5623 int
5624 hppa_remove_hw_watchpoint (pid, start, len, type)
5625 int pid;
5626 CORE_ADDR start;
5627 LONGEST len;
5628 enum bptype type;
5629 {
5630 CORE_ADDR page_start;
5631 int dictionary_is_empty;
5632 int page_size;
5633 int page_id;
5634 LONGEST range_size_in_pages;
5635
5636 if (type != 0)
5637 error ("read or access hardware watchpoints not supported on HP-UX");
5638
5639 /* Examine all pages in the address range. */
5640 require_memory_page_dictionary ();
5641
5642 page_size = memory_page_dictionary.page_size;
5643 page_start = (start / page_size) * page_size;
5644 range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;
5645
5646 for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
5647 {
5648 memory_page_t *page;
5649
5650 page = get_dictionary_entry_of_page (pid, page_start);
5651 page->reference_count--;
5652
5653 /* Was this the last reference of this page? If so, then we
5654 must scrub the entry from the dictionary, and also restore
5655 the page's original permissions.
5656 */
5657 if (page->reference_count == 0)
5658 remove_dictionary_entry_of_page (pid, page);
5659 }
5660
5661 dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);
5662
5663 /* If write protections are currently disallowed, then that implies that
5664 wait_for_inferior believes that the inferior is within a system call.
5665 Since we want to see both syscall entry and return, it's clearly not
5666 good to disable syscall events in this state!
5667
5668 ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
5669 "inferior is between syscall events now". Oh well.
5670 */
5671 if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
5672 hppa_disable_syscall_events (pid);
5673
5674 return 1;
5675 }
5676
5677
5678 /* Could we implement a watchpoint of this type via our available
5679 hardware support?
5680
5681 This query does not consider whether a particular address range
5682 could be so watched, but just whether support is generally available
5683 for such things. See hppa_range_profitable_for_hw_watchpoint for a
5684 query that answers whether a particular range should be watched via
5685 hardware support.
5686 */
5687 int
5688 hppa_can_use_hw_watchpoint (type, cnt, ot)
5689 enum bptype type;
5690 int cnt;
5691 enum bptype ot;
5692 {
5693 return (type == bp_hardware_watchpoint);
5694 }
5695
5696
5697 /* Assuming we could set a hardware watchpoint on this address, do
5698 we think it would be profitable ("a good idea") to do so? If not,
5699 we can always set a regular (aka single-step & test) watchpoint
5700 on the address...
5701 */
5702 int
5703 hppa_range_profitable_for_hw_watchpoint (pid, start, len)
5704 int pid;
5705 CORE_ADDR start;
5706 LONGEST len;
5707 {
5708 int range_is_stack_based;
5709 int range_is_accessible;
5710 CORE_ADDR page_start;
5711 int page_size;
5712 int page;
5713 LONGEST range_size_in_pages;
5714
5715 /* ??rehrauer: For now, say that all addresses are potentially
5716 profitable. Possibly later we'll want to test the address
5717 for "stackness"?
5718 */
5719 range_is_stack_based = 0;
5720
5721 /* If any page in the range is inaccessible, then we cannot
5722 really use hardware watchpointing, even though our client
5723 thinks we can. In that case, it's actually an error to
5724 attempt to use hw watchpoints, so we'll tell our client
5725 that the range is "unprofitable", and hope that they listen...
5726 */
5727 range_is_accessible = 1; /* Until proven otherwise. */
5728
5729 /* Examine all pages in the address range. */
5730 errno = 0;
5731 page_size = sysconf (_SC_PAGE_SIZE);
5732
5733 /* If we can't determine page size, we're hosed. Tell our
5734 client it's unprofitable to use hw watchpoints for this
5735 range.
5736 */
5737 if (errno || (page_size <= 0))
5738 {
5739 errno = 0;
5740 return 0;
5741 }
5742
5743 page_start = (start / page_size) * page_size;
5744 range_size_in_pages = len / (LONGEST) page_size;
5745
5746 for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
5747 {
5748 int tt_status;
5749 int page_permissions;
5750
5751 /* Is this page accessible? */
5752 errno = 0;
5753 tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
5754 pid,
5755 (TTRACE_ARG_TYPE) page_start,
5756 TT_NIL,
5757 (TTRACE_ARG_TYPE) & page_permissions);
5758 if (errno || (tt_status < 0))
5759 {
5760 errno = 0;
5761 range_is_accessible = 0;
5762 break;
5763 }
5764
5765 /* Yes, go for another... */
5766 }
5767
5768 return (!range_is_stack_based && range_is_accessible);
5769 }
5770
5771
5772 char *
5773 hppa_pid_or_tid_to_str (id)
5774 pid_t id;
5775 {
5776 static char buf[100]; /* Static because address returned. */
5777
5778 /* Does this appear to be a process? If so, print it that way. */
5779 if (is_process_id (id))
5780 return hppa_pid_to_str (id);
5781
5782 /* Else, print both the GDB thread number and the system thread id. */
5783 sprintf (buf, "thread %d (", pid_to_thread_id (id));
5784 strcat (buf, hppa_tid_to_str (id));
5785 strcat (buf, ")\0");
5786
5787 return buf;
5788 }
5789 \f
5790
5791 /* If the current pid is not the pid this module reported
5792 * from "ptrace_wait" with the most recent event, then the
5793 * user has switched threads.
5794 *
5795 * If the last reported event was a breakpoint, then return
5796 * the old thread id, else return 0.
5797 */
5798 pid_t
5799 hppa_switched_threads (gdb_pid)
5800 pid_t gdb_pid;
5801 {
5802 if (gdb_pid == old_gdb_pid)
5803 {
5804 /*
5805 * Core gdb is working with the same pid that it
5806 * was before we reported the last event. This
5807 * is ok: e.g. we reported hitting a thread-specific
5808 * breakpoint, but we were reporting the wrong
5809 * thread, so the core just ignored the event.
5810 *
5811 * No thread switch has happened.
5812 */
5813 return (pid_t) 0;
5814 }
5815 else if (gdb_pid == reported_pid)
5816 {
5817 /*
5818 * Core gdb is working with the pid we reported, so
5819 * any continue or step will be able to figure out
5820 * that it needs to step over any hit breakpoints
5821 * without our (i.e. PREPARE_TO_PROCEED's) help.
5822 */
5823 return (pid_t) 0;
5824 }
5825 else if (!reported_bpt)
5826 {
5827 /*
5828 * The core switched, but we didn't just report a
5829 * breakpoint, so there's no just-hit breakpoint
5830 * instruction at "reported_pid"'s PC, and thus there
5831 * is no need to step over it.
5832 */
5833 return (pid_t) 0;
5834 }
5835 else
5836 {
5837 /* There's been a real switch, and we reported
5838 * a hit breakpoint. Let "hppa_prepare_to_proceed"
5839 * know, so it can see whether the breakpoint is
5840 * still active.
5841 */
5842 return reported_pid;
5843 }
5844
5845 /* Keep compiler happy with an obvious return at the end.
5846 */
5847 return (pid_t) 0;
5848 }
5849
5850 void
5851 hppa_ensure_vforking_parent_remains_stopped (pid)
5852 int pid;
5853 {
5854 /* Nothing to do when using ttrace. Only the ptrace-based implementation
5855 must do real work.
5856 */
5857 }
5858
5859
5860 int
5861 hppa_resume_execd_vforking_child_to_get_parent_vfork ()
5862 {
5863 return 0; /* No, the parent vfork is available now. */
5864 }
5865 \f
5866
5867
5868 void
5869 _initialize_infttrace ()
5870 {
5871 /* Initialize the ttrace-based hardware watchpoint implementation. */
5872 memory_page_dictionary.page_count = (LONGEST) - 1;
5873 memory_page_dictionary.page_protections_allowed = 1;
5874
5875 errno = 0;
5876 memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);
5877
5878 if (errno || (memory_page_dictionary.page_size <= 0))
5879 perror_with_name ("sysconf");
5880 }