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