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