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