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