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