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