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