OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [gdb-5.0/] [gdb/] [infttrace.c] - Blame information for rev 105

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.