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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [gdb/] [procfs.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* Machine independent support for SVR4 /proc (process file system) for GDB.
2
   Copyright 1999, 2000, 2001 Free Software Foundation, Inc.
3
   Written by Michael Snyder at Cygnus Solutions.
4
   Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
5
 
6
This file is part of GDB.
7
 
8
This program is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 2 of the License, or
11
(at your option) any later version.
12
 
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with this program; if not, write to the Free Software Foundation,
20
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
 
22
#include "defs.h"
23
#include "inferior.h"
24
#include "target.h"
25
#include "gdbcore.h"
26
#include "gdbcmd.h"
27
#include "gdbthread.h"
28
 
29
#if defined (NEW_PROC_API)
30
#define _STRUCTURED_PROC 1      /* Should be done by configure script. */
31
#endif
32
 
33
#include <sys/procfs.h>
34
#ifdef HAVE_SYS_FAULT_H
35
#include <sys/fault.h>
36
#endif
37
#ifdef HAVE_SYS_SYSCALL_H
38
#include <sys/syscall.h>
39
#endif
40
#include <sys/errno.h>
41
#include <sys/wait.h>
42
#include <signal.h>
43
#include <ctype.h>
44
 
45
/*
46
 * PROCFS.C
47
 *
48
 * This module provides the interface between GDB and the
49
 * /proc file system, which is used on many versions of Unix
50
 * as a means for debuggers to control other processes.
51
 * Examples of the systems that use this interface are:
52
 *   Irix
53
 *   Solaris
54
 *   OSF
55
 *   Unixware
56
 *   AIX5
57
 *
58
 * /proc works by immitating a file system: you open a simulated file
59
 * that represents the process you wish to interact with, and
60
 * perform operations on that "file" in order to examine or change
61
 * the state of the other process.
62
 *
63
 * The most important thing to know about /proc and this module
64
 * is that there are two very different interfaces to /proc:
65
 *   One that uses the ioctl system call, and
66
 *   another that uses read and write system calls.
67
 * This module has to support both /proc interfaces.  This means
68
 * that there are two different ways of doing every basic operation.
69
 *
70
 * In order to keep most of the code simple and clean, I have
71
 * defined an interface "layer" which hides all these system calls.
72
 * An ifdef (NEW_PROC_API) determines which interface we are using,
73
 * and most or all occurrances of this ifdef should be confined to
74
 * this interface layer.
75
 */
76
 
77
 
78
/* Determine which /proc API we are using:
79
   The ioctl API defines PIOCSTATUS, while
80
   the read/write (multiple fd) API never does.  */
81
 
82
#ifdef NEW_PROC_API
83
#include <sys/types.h>
84
#include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */
85
#endif
86
 
87
#include <fcntl.h>      /* for O_RDONLY */
88
#include <unistd.h>     /* for "X_OK" */
89
#include "gdb_stat.h"   /* for struct stat */
90
 
91
/* Note: procfs-utils.h must be included after the above system header
92
   files, because it redefines various system calls using macros.
93
   This may be incompatible with the prototype declarations.  */
94
 
95
#include "proc-utils.h"
96
 
97
/* Prototypes for supply_gregset etc. */
98
#include "gregset.h"
99
 
100
/* =================== TARGET_OPS "MODULE" =================== */
101
 
102
/*
103
 * This module defines the GDB target vector and its methods.
104
 */
105
 
106
static void procfs_open (char *, int);
107
static void procfs_attach (char *, int);
108
static void procfs_detach (char *, int);
109
static void procfs_resume (ptid_t, int, enum target_signal);
110
static int procfs_can_run (void);
111
static void procfs_stop (void);
112
static void procfs_files_info (struct target_ops *);
113
static void procfs_fetch_registers (int);
114
static void procfs_store_registers (int);
115
static void procfs_notice_signals (ptid_t);
116
static void procfs_prepare_to_store (void);
117
static void procfs_kill_inferior (void);
118
static void procfs_mourn_inferior (void);
119
static void procfs_create_inferior (char *, char *, char **);
120
static ptid_t procfs_wait (ptid_t, struct target_waitstatus *);
121
static int procfs_xfer_memory (CORE_ADDR, char *, int, int,
122
                               struct mem_attrib *attrib,
123
                               struct target_ops *);
124
 
125
static int procfs_thread_alive (ptid_t);
126
 
127
void procfs_find_new_threads (void);
128
char *procfs_pid_to_str (ptid_t);
129
 
130
struct target_ops procfs_ops;           /* the target vector */
131
 
132
static void
133
init_procfs_ops (void)
134
{
135
  procfs_ops.to_shortname          = "procfs";
136
  procfs_ops.to_longname           = "Unix /proc child process";
137
  procfs_ops.to_doc                =
138
    "Unix /proc child process (started by the \"run\" command).";
139
  procfs_ops.to_open               = procfs_open;
140
  procfs_ops.to_can_run            = procfs_can_run;
141
  procfs_ops.to_create_inferior    = procfs_create_inferior;
142
  procfs_ops.to_kill               = procfs_kill_inferior;
143
  procfs_ops.to_mourn_inferior     = procfs_mourn_inferior;
144
  procfs_ops.to_attach             = procfs_attach;
145
  procfs_ops.to_detach             = procfs_detach;
146
  procfs_ops.to_wait               = procfs_wait;
147
  procfs_ops.to_resume             = procfs_resume;
148
  procfs_ops.to_prepare_to_store   = procfs_prepare_to_store;
149
  procfs_ops.to_fetch_registers    = procfs_fetch_registers;
150
  procfs_ops.to_store_registers    = procfs_store_registers;
151
  procfs_ops.to_xfer_memory        = procfs_xfer_memory;
152
  procfs_ops.to_insert_breakpoint  =  memory_insert_breakpoint;
153
  procfs_ops.to_remove_breakpoint  =  memory_remove_breakpoint;
154
  procfs_ops.to_notice_signals     = procfs_notice_signals;
155
  procfs_ops.to_files_info         = procfs_files_info;
156
  procfs_ops.to_stop               = procfs_stop;
157
 
158
  procfs_ops.to_terminal_init      = terminal_init_inferior;
159
  procfs_ops.to_terminal_inferior  = terminal_inferior;
160
  procfs_ops.to_terminal_ours_for_output = terminal_ours_for_output;
161
  procfs_ops.to_terminal_ours      = terminal_ours;
162
  procfs_ops.to_terminal_info      = child_terminal_info;
163
 
164
  procfs_ops.to_find_new_threads   = procfs_find_new_threads;
165
  procfs_ops.to_thread_alive       = procfs_thread_alive;
166
  procfs_ops.to_pid_to_str         = procfs_pid_to_str;
167
 
168
  procfs_ops.to_has_all_memory     = 1;
169
  procfs_ops.to_has_memory         = 1;
170
  procfs_ops.to_has_execution      = 1;
171
  procfs_ops.to_has_stack          = 1;
172
  procfs_ops.to_has_registers      = 1;
173
  procfs_ops.to_stratum            = process_stratum;
174
  procfs_ops.to_has_thread_control = tc_schedlock;
175
  procfs_ops.to_magic              = OPS_MAGIC;
176
}
177
 
178
/* =================== END, TARGET_OPS "MODULE" =================== */
179
 
180
/*
181
 * World Unification:
182
 *
183
 * Put any typedefs, defines etc. here that are required for
184
 * the unification of code that handles different versions of /proc.
185
 */
186
 
187
#ifdef NEW_PROC_API             /* Solaris 7 && 8 method for watchpoints */
188
#ifdef WA_READ
189
     enum { READ_WATCHFLAG  = WA_READ,
190
            WRITE_WATCHFLAG = WA_WRITE,
191
            EXEC_WATCHFLAG  = WA_EXEC,
192
            AFTER_WATCHFLAG = WA_TRAPAFTER
193
     };
194
#endif
195
#else                           /* Irix method for watchpoints */
196
     enum { READ_WATCHFLAG  = MA_READ,
197
            WRITE_WATCHFLAG = MA_WRITE,
198
            EXEC_WATCHFLAG  = MA_EXEC,
199
            AFTER_WATCHFLAG = 0          /* trapafter not implemented */
200
     };
201
#endif
202
 
203
/* gdb_sigset_t */
204
#ifdef HAVE_PR_SIGSET_T
205
typedef pr_sigset_t gdb_sigset_t;
206
#else
207
typedef sigset_t gdb_sigset_t;
208
#endif
209
 
210
/* sigaction */
211
#ifdef HAVE_PR_SIGACTION64_T
212
typedef pr_sigaction64_t gdb_sigaction_t;
213
#else
214
typedef struct sigaction gdb_sigaction_t;
215
#endif
216
 
217
/* siginfo */
218
#ifdef HAVE_PR_SIGINFO64_T
219
typedef pr_siginfo64_t gdb_siginfo_t;
220
#else
221
typedef struct siginfo gdb_siginfo_t;
222
#endif
223
 
224
/* gdb_premptysysset */
225
#ifdef premptysysset
226
#define gdb_premptysysset premptysysset
227
#else
228
#define gdb_premptysysset premptyset
229
#endif
230
 
231
/* praddsysset */
232
#ifdef praddsysset
233
#define gdb_praddsysset praddsysset
234
#else
235
#define gdb_praddsysset praddset
236
#endif
237
 
238
/* prdelsysset */
239
#ifdef prdelsysset
240
#define gdb_prdelsysset prdelsysset
241
#else
242
#define gdb_prdelsysset prdelset
243
#endif
244
 
245
/* prissyssetmember */
246
#ifdef prissyssetmember
247
#define gdb_pr_issyssetmember prissyssetmember
248
#else
249
#define gdb_pr_issyssetmember prismember
250
#endif
251
 
252
/* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't
253
   as intuitively descriptive as it could be, so we'll define
254
   DYNAMIC_SYSCALLS to mean the same thing.  Anyway, at the time of
255
   this writing, this feature is only found on AIX5 systems and
256
   basically means that the set of syscalls is not fixed.  I.e,
257
   there's no nice table that one can #include to get all of the
258
   syscall numbers.  Instead, they're stored in /proc/PID/sysent
259
   for each process.  We are at least guaranteed that they won't
260
   change over the lifetime of the process.  But each process could
261
   (in theory) have different syscall numbers.
262
*/
263
#ifdef HAVE_PRSYSENT_T
264
#define DYNAMIC_SYSCALLS
265
#endif
266
 
267
 
268
 
269
/* =================== STRUCT PROCINFO "MODULE" =================== */
270
 
271
     /* FIXME: this comment will soon be out of date W.R.T. threads.  */
272
 
273
/* The procinfo struct is a wrapper to hold all the state information
274
   concerning a /proc process.  There should be exactly one procinfo
275
   for each process, and since GDB currently can debug only one
276
   process at a time, that means there should be only one procinfo.
277
   All of the LWP's of a process can be accessed indirectly thru the
278
   single process procinfo.
279
 
280
   However, against the day when GDB may debug more than one process,
281
   this data structure is kept in a list (which for now will hold no
282
   more than one member), and many functions will have a pointer to a
283
   procinfo as an argument.
284
 
285
   There will be a separate procinfo structure for use by the (not yet
286
   implemented) "info proc" command, so that we can print useful
287
   information about any random process without interfering with the
288
   inferior's procinfo information. */
289
 
290
#ifdef NEW_PROC_API
291
/* format strings for /proc paths */
292
# ifndef CTL_PROC_NAME_FMT
293
#  define MAIN_PROC_NAME_FMT   "/proc/%d"
294
#  define CTL_PROC_NAME_FMT    "/proc/%d/ctl"
295
#  define AS_PROC_NAME_FMT     "/proc/%d/as"
296
#  define MAP_PROC_NAME_FMT    "/proc/%d/map"
297
#  define STATUS_PROC_NAME_FMT "/proc/%d/status"
298
#  define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus")
299
# endif
300
/* the name of the proc status struct depends on the implementation */
301
typedef pstatus_t   gdb_prstatus_t;
302
typedef lwpstatus_t gdb_lwpstatus_t;
303
#else /* ! NEW_PROC_API */
304
/* format strings for /proc paths */
305
# ifndef CTL_PROC_NAME_FMT
306
#  define MAIN_PROC_NAME_FMT   "/proc/%05d"
307
#  define CTL_PROC_NAME_FMT    "/proc/%05d"
308
#  define AS_PROC_NAME_FMT     "/proc/%05d"
309
#  define MAP_PROC_NAME_FMT    "/proc/%05d"
310
#  define STATUS_PROC_NAME_FMT "/proc/%05d"
311
#  define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp")
312
# endif
313
/* the name of the proc status struct depends on the implementation */
314
typedef prstatus_t gdb_prstatus_t;
315
typedef prstatus_t gdb_lwpstatus_t;
316
#endif /* NEW_PROC_API */
317
 
318
typedef struct procinfo {
319
  struct procinfo *next;
320
  int pid;                      /* Process ID    */
321
  int tid;                      /* Thread/LWP id */
322
 
323
  /* process state */
324
  int was_stopped;
325
  int ignore_next_sigstop;
326
 
327
  /* The following four fd fields may be identical, or may contain
328
     several different fd's, depending on the version of /proc
329
     (old ioctl or new read/write).  */
330
 
331
  int ctl_fd;                   /* File descriptor for /proc control file */
332
  /*
333
   * The next three file descriptors are actually only needed in the
334
   * read/write, multiple-file-descriptor implemenation (NEW_PROC_API).
335
   * However, to avoid a bunch of #ifdefs in the code, we will use
336
   * them uniformly by (in the case of the ioctl single-file-descriptor
337
   * implementation) filling them with copies of the control fd.
338
   */
339
  int status_fd;                /* File descriptor for /proc status file */
340
  int as_fd;                    /* File descriptor for /proc as file */
341
 
342
  char pathname[MAX_PROC_NAME_SIZE];    /* Pathname to /proc entry */
343
 
344
  fltset_t saved_fltset;        /* Saved traced hardware fault set */
345
  gdb_sigset_t saved_sigset;    /* Saved traced signal set */
346
  gdb_sigset_t saved_sighold;   /* Saved held signal set */
347
  sysset_t *saved_exitset;      /* Saved traced system call exit set */
348
  sysset_t *saved_entryset;     /* Saved traced system call entry set */
349
 
350
  gdb_prstatus_t prstatus;      /* Current process status info */
351
 
352
#ifndef NEW_PROC_API
353
  gdb_fpregset_t fpregset;      /* Current floating point registers */
354
#endif
355
 
356
#ifdef DYNAMIC_SYSCALLS
357
  int num_syscalls;             /* Total number of syscalls */
358
  char **syscall_names;         /* Syscall number to name map */
359
#endif
360
 
361
  struct procinfo *thread_list;
362
 
363
  int status_valid : 1;
364
  int gregs_valid  : 1;
365
  int fpregs_valid : 1;
366
  int threads_valid: 1;
367
} procinfo;
368
 
369
static char errmsg[128];        /* shared error msg buffer */
370
 
371
/* Function prototypes for procinfo module: */
372
 
373
static procinfo *find_procinfo_or_die (int pid, int tid);
374
static procinfo *find_procinfo (int pid, int tid);
375
static procinfo *create_procinfo (int pid, int tid);
376
static void destroy_procinfo (procinfo * p);
377
static void do_destroy_procinfo_cleanup (void *);
378
static void dead_procinfo (procinfo * p, char *msg, int killp);
379
static int open_procinfo_files (procinfo * p, int which);
380
static void close_procinfo_files (procinfo * p);
381
static int sysset_t_size (procinfo *p);
382
static sysset_t *sysset_t_alloc (procinfo * pi);
383
#ifdef DYNAMIC_SYSCALLS
384
static void load_syscalls (procinfo *pi);
385
static void free_syscalls (procinfo *pi);
386
static int find_syscall (procinfo *pi, char *name);
387
#endif /* DYNAMIC_SYSCALLS */
388
 
389
/* The head of the procinfo list: */
390
static procinfo * procinfo_list;
391
 
392
/*
393
 * Function: find_procinfo
394
 *
395
 * Search the procinfo list.
396
 *
397
 * Returns: pointer to procinfo, or NULL if not found.
398
 */
399
 
400
static procinfo *
401
find_procinfo (int pid, int tid)
402
{
403
  procinfo *pi;
404
 
405
  for (pi = procinfo_list; pi; pi = pi->next)
406
    if (pi->pid == pid)
407
      break;
408
 
409
  if (pi)
410
    if (tid)
411
      {
412
        /* Don't check threads_valid.  If we're updating the
413
           thread_list, we want to find whatever threads are already
414
           here.  This means that in general it is the caller's
415
           responsibility to check threads_valid and update before
416
           calling find_procinfo, if the caller wants to find a new
417
           thread. */
418
 
419
        for (pi = pi->thread_list; pi; pi = pi->next)
420
          if (pi->tid == tid)
421
            break;
422
      }
423
 
424
  return pi;
425
}
426
 
427
/*
428
 * Function: find_procinfo_or_die
429
 *
430
 * Calls find_procinfo, but errors on failure.
431
 */
432
 
433
static procinfo *
434
find_procinfo_or_die (int pid, int tid)
435
{
436
  procinfo *pi = find_procinfo (pid, tid);
437
 
438
  if (pi == NULL)
439
    {
440
      if (tid)
441
        error ("procfs: couldn't find pid %d (kernel thread %d) in procinfo list.",
442
               pid, tid);
443
      else
444
        error ("procfs: couldn't find pid %d in procinfo list.", pid);
445
    }
446
  return pi;
447
}
448
 
449
/* open_with_retry() is a wrapper for open().  The appropriate
450
   open() call is attempted; if unsuccessful, it will be retried as
451
   many times as needed for the EAGAIN and EINTR conditions.
452
 
453
   For other conditions, open_with_retry() will retry the open() a
454
   limited number of times.  In addition, a short sleep is imposed
455
   prior to retrying the open().  The reason for this sleep is to give
456
   the kernel a chance to catch up and create the file in question in
457
   the event that GDB "wins" the race to open a file before the kernel
458
   has created it.  */
459
 
460
static int
461
open_with_retry (const char *pathname, int flags)
462
{
463
  int retries_remaining, status;
464
 
465
  retries_remaining = 2;
466
 
467
  while (1)
468
    {
469
      status = open (pathname, flags);
470
 
471
      if (status >= 0 || retries_remaining == 0)
472
        break;
473
      else if (errno != EINTR && errno != EAGAIN)
474
        {
475
          retries_remaining--;
476
          sleep (1);
477
        }
478
    }
479
 
480
  return status;
481
}
482
 
483
/*
484
 * Function: open_procinfo_files
485
 *
486
 * Open the file descriptor for the process or LWP.
487
 * ifdef NEW_PROC_API, we only open the control file descriptor;
488
 * the others are opened lazily as needed.
489
 * else (if not NEW_PROC_API), there is only one real
490
 * file descriptor, but we keep multiple copies of it so that
491
 * the code that uses them does not have to be #ifdef'd.
492
 *
493
 * Return: file descriptor, or zero for failure.
494
 */
495
 
496
enum { FD_CTL, FD_STATUS, FD_AS };
497
 
498
static int
499
open_procinfo_files (procinfo *pi, int which)
500
{
501
#ifdef NEW_PROC_API
502
  char tmp[MAX_PROC_NAME_SIZE];
503
#endif
504
  int  fd;
505
 
506
  /*
507
   * This function is getting ALMOST long enough to break up into several.
508
   * Here is some rationale:
509
   *
510
   * NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware):
511
   *   There are several file descriptors that may need to be open
512
   *   for any given process or LWP.  The ones we're intereted in are:
513
   *     - control       (ctl)    write-only    change the state
514
   *     - status        (status) read-only     query the state
515
   *     - address space (as)     read/write    access memory
516
   *     - map           (map)    read-only     virtual addr map
517
   *   Most of these are opened lazily as they are needed.
518
   *   The pathnames for the 'files' for an LWP look slightly
519
   *   different from those of a first-class process:
520
   *     Pathnames for a process (<proc-id>):
521
   *       /proc/<proc-id>/ctl
522
   *       /proc/<proc-id>/status
523
   *       /proc/<proc-id>/as
524
   *       /proc/<proc-id>/map
525
   *     Pathnames for an LWP (lwp-id):
526
   *       /proc/<proc-id>/lwp/<lwp-id>/lwpctl
527
   *       /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
528
   *   An LWP has no map or address space file descriptor, since
529
   *   the memory map and address space are shared by all LWPs.
530
   *
531
   * Everyone else (Solaris 2.5, Irix, OSF)
532
   *   There is only one file descriptor for each process or LWP.
533
   *   For convenience, we copy the same file descriptor into all
534
   *   three fields of the procinfo struct (ctl_fd, status_fd, and
535
   *   as_fd, see NEW_PROC_API above) so that code that uses them
536
   *   doesn't need any #ifdef's.
537
   *     Pathname for all:
538
   *       /proc/<proc-id>
539
   *
540
   *   Solaris 2.5 LWP's:
541
   *     Each LWP has an independent file descriptor, but these
542
   *     are not obtained via the 'open' system call like the rest:
543
   *     instead, they're obtained thru an ioctl call (PIOCOPENLWP)
544
   *     to the file descriptor of the parent process.
545
   *
546
   *   OSF threads:
547
   *     These do not even have their own independent file descriptor.
548
   *     All operations are carried out on the file descriptor of the
549
   *     parent process.  Therefore we just call open again for each
550
   *     thread, getting a new handle for the same 'file'.
551
   */
552
 
553
#ifdef NEW_PROC_API
554
  /*
555
   * In this case, there are several different file descriptors that
556
   * we might be asked to open.  The control file descriptor will be
557
   * opened early, but the others will be opened lazily as they are
558
   * needed.
559
   */
560
 
561
  strcpy (tmp, pi->pathname);
562
  switch (which) {      /* which file descriptor to open? */
563
  case FD_CTL:
564
    if (pi->tid)
565
      strcat (tmp, "/lwpctl");
566
    else
567
      strcat (tmp, "/ctl");
568
    fd = open_with_retry (tmp, O_WRONLY);
569
    if (fd <= 0)
570
      return 0;          /* fail */
571
    pi->ctl_fd = fd;
572
    break;
573
  case FD_AS:
574
    if (pi->tid)
575
      return 0;          /* there is no 'as' file descriptor for an lwp */
576
    strcat (tmp, "/as");
577
    fd = open_with_retry (tmp, O_RDWR);
578
    if (fd <= 0)
579
      return 0;          /* fail */
580
    pi->as_fd = fd;
581
    break;
582
  case FD_STATUS:
583
    if (pi->tid)
584
      strcat (tmp, "/lwpstatus");
585
    else
586
      strcat (tmp, "/status");
587
    fd = open_with_retry (tmp, O_RDONLY);
588
    if (fd <= 0)
589
      return 0;          /* fail */
590
    pi->status_fd = fd;
591
    break;
592
  default:
593
    return 0;            /* unknown file descriptor */
594
  }
595
#else  /* not NEW_PROC_API */
596
  /*
597
   * In this case, there is only one file descriptor for each procinfo
598
   * (ie. each process or LWP).  In fact, only the file descriptor for
599
   * the process can actually be opened by an 'open' system call.
600
   * The ones for the LWPs have to be obtained thru an IOCTL call
601
   * on the process's file descriptor.
602
   *
603
   * For convenience, we copy each procinfo's single file descriptor
604
   * into all of the fields occupied by the several file descriptors
605
   * of the NEW_PROC_API implementation.  That way, the code that uses
606
   * them can be written without ifdefs.
607
   */
608
 
609
 
610
#ifdef PIOCTSTATUS      /* OSF */
611
  /* Only one FD; just open it. */
612
  if ((fd = open_with_retry (pi->pathname, O_RDWR)) == 0)
613
    return 0;
614
#else                   /* Sol 2.5, Irix, other? */
615
  if (pi->tid == 0)      /* Master procinfo for the process */
616
    {
617
      fd = open_with_retry (pi->pathname, O_RDWR);
618
      if (fd <= 0)
619
        return 0;        /* fail */
620
    }
621
  else                  /* LWP thread procinfo */
622
    {
623
#ifdef PIOCOPENLWP      /* Sol 2.5, thread/LWP */
624
      procinfo *process;
625
      int lwpid = pi->tid;
626
 
627
      /* Find the procinfo for the entire process. */
628
      if ((process = find_procinfo (pi->pid, 0)) == NULL)
629
        return 0;        /* fail */
630
 
631
      /* Now obtain the file descriptor for the LWP. */
632
      if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) <= 0)
633
        return 0;        /* fail */
634
#else                   /* Irix, other? */
635
      return 0;          /* Don't know how to open threads */
636
#endif  /* Sol 2.5 PIOCOPENLWP */
637
    }
638
#endif  /* OSF     PIOCTSTATUS */
639
  pi->ctl_fd = pi->as_fd = pi->status_fd = fd;
640
#endif  /* NEW_PROC_API */
641
 
642
  return 1;             /* success */
643
}
644
 
645
/*
646
 * Function: create_procinfo
647
 *
648
 * Allocate a data structure and link it into the procinfo list.
649
 * (First tries to find a pre-existing one (FIXME: why?)
650
 *
651
 * Return: pointer to new procinfo struct.
652
 */
653
 
654
static procinfo *
655
create_procinfo (int pid, int tid)
656
{
657
  procinfo *pi, *parent;
658
 
659
  if ((pi = find_procinfo (pid, tid)))
660
    return pi;                  /* Already exists, nothing to do. */
661
 
662
  /* find parent before doing malloc, to save having to cleanup */
663
  if (tid != 0)
664
    parent = find_procinfo_or_die (pid, 0);      /* FIXME: should I
665
                                                   create it if it
666
                                                   doesn't exist yet? */
667
 
668
  pi = (procinfo *) xmalloc (sizeof (procinfo));
669
  memset (pi, 0, sizeof (procinfo));
670
  pi->pid = pid;
671
  pi->tid = tid;
672
 
673
#ifdef DYNAMIC_SYSCALLS
674
  load_syscalls (pi);
675
#endif
676
 
677
  /* Chain into list.  */
678
  if (tid == 0)
679
    {
680
      sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
681
      pi->next = procinfo_list;
682
      procinfo_list = pi;
683
    }
684
  else
685
    {
686
#ifdef NEW_PROC_API
687
      sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid);
688
#else
689
      sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid);
690
#endif
691
      pi->next = parent->thread_list;
692
      parent->thread_list = pi;
693
    }
694
  return pi;
695
}
696
 
697
/*
698
 * Function: close_procinfo_files
699
 *
700
 * Close all file descriptors associated with the procinfo
701
 */
702
 
703
static void
704
close_procinfo_files (procinfo *pi)
705
{
706
  if (pi->ctl_fd > 0)
707
    close (pi->ctl_fd);
708
#ifdef NEW_PROC_API
709
  if (pi->as_fd > 0)
710
    close (pi->as_fd);
711
  if (pi->status_fd > 0)
712
    close (pi->status_fd);
713
#endif
714
  pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
715
}
716
 
717
/*
718
 * Function: destroy_procinfo
719
 *
720
 * Destructor function.  Close, unlink and deallocate the object.
721
 */
722
 
723
static void
724
destroy_one_procinfo (procinfo **list, procinfo *pi)
725
{
726
  procinfo *ptr;
727
 
728
  /* Step one: unlink the procinfo from its list */
729
  if (pi == *list)
730
    *list = pi->next;
731
  else
732
    for (ptr = *list; ptr; ptr = ptr->next)
733
      if (ptr->next == pi)
734
        {
735
          ptr->next =  pi->next;
736
          break;
737
        }
738
 
739
  /* Step two: close any open file descriptors */
740
  close_procinfo_files (pi);
741
 
742
  /* Step three: free the memory. */
743
#ifdef DYNAMIC_SYSCALLS
744
  free_syscalls (pi);
745
#endif
746
  xfree (pi);
747
}
748
 
749
static void
750
destroy_procinfo (procinfo *pi)
751
{
752
  procinfo *tmp;
753
 
754
  if (pi->tid != 0)      /* destroy a thread procinfo */
755
    {
756
      tmp = find_procinfo (pi->pid, 0);  /* find the parent process */
757
      destroy_one_procinfo (&tmp->thread_list, pi);
758
    }
759
  else                  /* destroy a process procinfo and all its threads */
760
    {
761
      /* First destroy the children, if any; */
762
      while (pi->thread_list != NULL)
763
        destroy_one_procinfo (&pi->thread_list, pi->thread_list);
764
      /* Then destroy the parent.  Genocide!!!  */
765
      destroy_one_procinfo (&procinfo_list, pi);
766
    }
767
}
768
 
769
static void
770
do_destroy_procinfo_cleanup (void *pi)
771
{
772
  destroy_procinfo (pi);
773
}
774
 
775
enum { NOKILL, KILL };
776
 
777
/*
778
 * Function: dead_procinfo
779
 *
780
 * To be called on a non_recoverable error for a procinfo.
781
 * Prints error messages, optionally sends a SIGKILL to the process,
782
 * then destroys the data structure.
783
 */
784
 
785
static void
786
dead_procinfo (procinfo *pi, char *msg, int kill_p)
787
{
788
  char procfile[80];
789
 
790
  if (pi->pathname)
791
    {
792
      print_sys_errmsg (pi->pathname, errno);
793
    }
794
  else
795
    {
796
      sprintf (procfile, "process %d", pi->pid);
797
      print_sys_errmsg (procfile, errno);
798
    }
799
  if (kill_p == KILL)
800
    kill (pi->pid, SIGKILL);
801
 
802
  destroy_procinfo (pi);
803
  error (msg);
804
}
805
 
806
/*
807
 * Function: sysset_t_size
808
 *
809
 * Returns the (complete) size of a sysset_t struct.  Normally, this
810
 * is just sizeof (syset_t), but in the case of Monterey/64, the actual
811
 * size of sysset_t isn't known until runtime.
812
 */
813
 
814
static int
815
sysset_t_size (procinfo * pi)
816
{
817
#ifndef DYNAMIC_SYSCALLS
818
  return sizeof (sysset_t);
819
#else
820
  return sizeof (sysset_t) - sizeof (uint64_t)
821
    + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
822
                           / (8 * sizeof (uint64_t)));
823
#endif
824
}
825
 
826
/* Function: sysset_t_alloc
827
 
828
   Allocate and (partially) initialize a sysset_t struct.  */
829
 
830
static sysset_t *
831
sysset_t_alloc (procinfo * pi)
832
{
833
  sysset_t *ret;
834
  int size = sysset_t_size (pi);
835
  ret = xmalloc (size);
836
#ifdef DYNAMIC_SYSCALLS
837
  ret->pr_size = (pi->num_syscalls + (8 * sizeof (uint64_t) - 1))
838
                 / (8 * sizeof (uint64_t));
839
#endif
840
  return ret;
841
}
842
 
843
#ifdef DYNAMIC_SYSCALLS
844
 
845
/* Function: load_syscalls
846
 
847
   Extract syscall numbers and names from /proc/<pid>/sysent.  Initialize
848
   pi->num_syscalls with the number of syscalls and pi->syscall_names
849
   with the names.  (Certain numbers may be skipped in which case the
850
   names for these numbers will be left as NULL.) */
851
 
852
#define MAX_SYSCALL_NAME_LENGTH 256
853
#define MAX_SYSCALLS 65536
854
 
855
static void
856
load_syscalls (procinfo *pi)
857
{
858
  char pathname[MAX_PROC_NAME_SIZE];
859
  int sysent_fd;
860
  prsysent_t header;
861
  prsyscall_t *syscalls;
862
  int i, size, maxcall;
863
 
864
  pi->num_syscalls = 0;
865
  pi->syscall_names = 0;
866
 
867
  /* Open the file descriptor for the sysent file */
868
  sprintf (pathname, "/proc/%d/sysent", pi->pid);
869
  sysent_fd = open_with_retry (pathname, O_RDONLY);
870
  if (sysent_fd < 0)
871
    {
872
      error ("load_syscalls: Can't open /proc/%d/sysent", pi->pid);
873
    }
874
 
875
  size = sizeof header - sizeof (prsyscall_t);
876
  if (read (sysent_fd, &header, size) != size)
877
    {
878
      error ("load_syscalls: Error reading /proc/%d/sysent", pi->pid);
879
    }
880
 
881
  if (header.pr_nsyscalls == 0)
882
    {
883
      error ("load_syscalls: /proc/%d/sysent contains no syscalls!", pi->pid);
884
    }
885
 
886
  size = header.pr_nsyscalls * sizeof (prsyscall_t);
887
  syscalls = xmalloc (size);
888
 
889
  if (read (sysent_fd, syscalls, size) != size)
890
    {
891
      xfree (syscalls);
892
      error ("load_syscalls: Error reading /proc/%d/sysent", pi->pid);
893
    }
894
 
895
  /* Find maximum syscall number.  This may not be the same as
896
     pr_nsyscalls since that value refers to the number of entries
897
     in the table.  (Also, the docs indicate that some system
898
     call numbers may be skipped.) */
899
 
900
  maxcall = syscalls[0].pr_number;
901
 
902
  for (i = 1; i <  header.pr_nsyscalls; i++)
903
    if (syscalls[i].pr_number > maxcall
904
        && syscalls[i].pr_nameoff > 0
905
        && syscalls[i].pr_number < MAX_SYSCALLS)
906
      maxcall = syscalls[i].pr_number;
907
 
908
  pi->num_syscalls = maxcall+1;
909
  pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *));
910
 
911
  for (i = 0; i < pi->num_syscalls; i++)
912
    pi->syscall_names[i] = NULL;
913
 
914
  /* Read the syscall names in */
915
  for (i = 0; i < header.pr_nsyscalls; i++)
916
    {
917
      char namebuf[MAX_SYSCALL_NAME_LENGTH];
918
      int nread;
919
      int callnum;
920
 
921
      if (syscalls[i].pr_number >= MAX_SYSCALLS
922
          || syscalls[i].pr_number < 0
923
          || syscalls[i].pr_nameoff <= 0
924
          || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET)
925
                                       != (off_t) syscalls[i].pr_nameoff))
926
        continue;
927
 
928
      nread = read (sysent_fd, namebuf, sizeof namebuf);
929
      if (nread <= 0)
930
        continue;
931
 
932
      callnum = syscalls[i].pr_number;
933
 
934
      if (pi->syscall_names[callnum] != NULL)
935
        {
936
          /* FIXME: Generate warning */
937
          continue;
938
        }
939
 
940
      namebuf[nread-1] = '\0';
941
      size = strlen (namebuf) + 1;
942
      pi->syscall_names[callnum] = xmalloc (size);
943
      strncpy (pi->syscall_names[callnum], namebuf, size-1);
944
      pi->syscall_names[callnum][size-1] = '\0';
945
    }
946
 
947
  close (sysent_fd);
948
  xfree (syscalls);
949
}
950
 
951
/* Function: free_syscalls
952
 
953
   Free the space allocated for the syscall names from the procinfo
954
   structure.  */
955
 
956
static void
957
free_syscalls (procinfo *pi)
958
{
959
  if (pi->syscall_names)
960
    {
961
      int i;
962
 
963
      for (i = 0; i < pi->num_syscalls; i++)
964
        if (pi->syscall_names[i] != NULL)
965
          xfree (pi->syscall_names[i]);
966
 
967
      xfree (pi->syscall_names);
968
      pi->syscall_names = 0;
969
    }
970
}
971
 
972
/* Function: find_syscall
973
 
974
   Given a name, look up (and return) the corresponding syscall number.
975
   If no match is found, return -1.  */
976
 
977
static int
978
find_syscall (procinfo *pi, char *name)
979
{
980
  int i;
981
  for (i = 0; i < pi->num_syscalls; i++)
982
    {
983
      if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0)
984
        return i;
985
    }
986
  return -1;
987
}
988
#endif
989
 
990
/* =================== END, STRUCT PROCINFO "MODULE" =================== */
991
 
992
/* ===================  /proc  "MODULE" =================== */
993
 
994
/*
995
 * This "module" is the interface layer between the /proc system API
996
 * and the gdb target vector functions.  This layer consists of
997
 * access functions that encapsulate each of the basic operations
998
 * that we need to use from the /proc API.
999
 *
1000
 * The main motivation for this layer is to hide the fact that
1001
 * there are two very different implementations of the /proc API.
1002
 * Rather than have a bunch of #ifdefs all thru the gdb target vector
1003
 * functions, we do our best to hide them all in here.
1004
 */
1005
 
1006
int proc_get_status (procinfo * pi);
1007
long proc_flags (procinfo * pi);
1008
int proc_why (procinfo * pi);
1009
int proc_what (procinfo * pi);
1010
int proc_set_run_on_last_close (procinfo * pi);
1011
int proc_unset_run_on_last_close (procinfo * pi);
1012
int proc_set_inherit_on_fork (procinfo * pi);
1013
int proc_unset_inherit_on_fork (procinfo * pi);
1014
int proc_set_async (procinfo * pi);
1015
int proc_unset_async (procinfo * pi);
1016
int proc_stop_process (procinfo * pi);
1017
int proc_trace_signal (procinfo * pi, int signo);
1018
int proc_ignore_signal (procinfo * pi, int signo);
1019
int proc_clear_current_fault (procinfo * pi);
1020
int proc_set_current_signal (procinfo * pi, int signo);
1021
int proc_clear_current_signal (procinfo * pi);
1022
int proc_set_gregs (procinfo * pi);
1023
int proc_set_fpregs (procinfo * pi);
1024
int proc_wait_for_stop (procinfo * pi);
1025
int proc_run_process (procinfo * pi, int step, int signo);
1026
int proc_kill (procinfo * pi, int signo);
1027
int proc_parent_pid (procinfo * pi);
1028
int proc_get_nthreads (procinfo * pi);
1029
int proc_get_current_thread (procinfo * pi);
1030
int proc_set_held_signals (procinfo * pi, gdb_sigset_t * sighold);
1031
int proc_set_traced_sysexit (procinfo * pi, sysset_t * sysset);
1032
int proc_set_traced_sysentry (procinfo * pi, sysset_t * sysset);
1033
int proc_set_traced_faults (procinfo * pi, fltset_t * fltset);
1034
int proc_set_traced_signals (procinfo * pi, gdb_sigset_t * sigset);
1035
 
1036
int proc_update_threads (procinfo * pi);
1037
int proc_iterate_over_threads (procinfo * pi,
1038
                               int (*func) (procinfo *, procinfo *, void *),
1039
                               void *ptr);
1040
 
1041
gdb_gregset_t *proc_get_gregs (procinfo * pi);
1042
gdb_fpregset_t *proc_get_fpregs (procinfo * pi);
1043
sysset_t *proc_get_traced_sysexit (procinfo * pi, sysset_t * save);
1044
sysset_t *proc_get_traced_sysentry (procinfo * pi, sysset_t * save);
1045
fltset_t *proc_get_traced_faults (procinfo * pi, fltset_t * save);
1046
gdb_sigset_t *proc_get_traced_signals (procinfo * pi, gdb_sigset_t * save);
1047
gdb_sigset_t *proc_get_held_signals (procinfo * pi, gdb_sigset_t * save);
1048
gdb_sigset_t *proc_get_pending_signals (procinfo * pi, gdb_sigset_t * save);
1049
gdb_sigaction_t *proc_get_signal_actions (procinfo * pi, gdb_sigaction_t *save);
1050
 
1051
void proc_warn (procinfo * pi, char *func, int line);
1052
void proc_error (procinfo * pi, char *func, int line);
1053
 
1054
void
1055
proc_warn (procinfo *pi, char *func, int line)
1056
{
1057
  sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1058
  print_sys_errmsg (errmsg, errno);
1059
}
1060
 
1061
void
1062
proc_error (procinfo *pi, char *func, int line)
1063
{
1064
  sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname);
1065
  perror_with_name (errmsg);
1066
}
1067
 
1068
/*
1069
 * Function: proc_get_status
1070
 *
1071
 * Updates the status struct in the procinfo.
1072
 * There is a 'valid' flag, to let other functions know when
1073
 * this function needs to be called (so the status is only
1074
 * read when it is needed).  The status file descriptor is
1075
 * also only opened when it is needed.
1076
 *
1077
 * Return: non-zero for success, zero for failure.
1078
 */
1079
 
1080
int
1081
proc_get_status (procinfo *pi)
1082
{
1083
  /* Status file descriptor is opened "lazily" */
1084
  if (pi->status_fd == 0 &&
1085
      open_procinfo_files (pi, FD_STATUS) == 0)
1086
    {
1087
      pi->status_valid = 0;
1088
      return 0;
1089
    }
1090
 
1091
#ifdef NEW_PROC_API
1092
  if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
1093
    pi->status_valid = 0;                        /* fail */
1094
  else
1095
    {
1096
      /* Sigh... I have to read a different data structure,
1097
         depending on whether this is a main process or an LWP. */
1098
      if (pi->tid)
1099
        pi->status_valid = (read (pi->status_fd,
1100
                                  (char *) &pi->prstatus.pr_lwp,
1101
                                  sizeof (lwpstatus_t))
1102
                            == sizeof (lwpstatus_t));
1103
      else
1104
        {
1105
          pi->status_valid = (read (pi->status_fd,
1106
                                    (char *) &pi->prstatus,
1107
                                    sizeof (gdb_prstatus_t))
1108
                              == sizeof (gdb_prstatus_t));
1109
#if 0 /*def UNIXWARE*/
1110
          if (pi->status_valid &&
1111
              (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) &&
1112
              pi->prstatus.pr_lwp.pr_why == PR_REQUESTED)
1113
            /* Unixware peculiarity -- read the damn thing again! */
1114
            pi->status_valid = (read (pi->status_fd,
1115
                                      (char *) &pi->prstatus,
1116
                                      sizeof (gdb_prstatus_t))
1117
                                == sizeof (gdb_prstatus_t));
1118
#endif /* UNIXWARE */
1119
        }
1120
    }
1121
#else   /* ioctl method */
1122
#ifdef PIOCTSTATUS      /* osf */
1123
  if (pi->tid == 0)      /* main process */
1124
    {
1125
      /* Just read the danged status.  Now isn't that simple? */
1126
      pi->status_valid =
1127
        (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1128
    }
1129
  else
1130
    {
1131
      int win;
1132
      struct {
1133
        long pr_count;
1134
        tid_t pr_error_thread;
1135
        struct prstatus status;
1136
      } thread_status;
1137
 
1138
      thread_status.pr_count = 1;
1139
      thread_status.status.pr_tid = pi->tid;
1140
      win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0);
1141
      if (win)
1142
        {
1143
          memcpy (&pi->prstatus, &thread_status.status,
1144
                  sizeof (pi->prstatus));
1145
          pi->status_valid = 1;
1146
        }
1147
    }
1148
#else
1149
  /* Just read the danged status.  Now isn't that simple? */
1150
  pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0);
1151
#endif
1152
#endif
1153
 
1154
  if (pi->status_valid)
1155
    {
1156
      PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1157
                                proc_why (pi),
1158
                                proc_what (pi),
1159
                                proc_get_current_thread (pi));
1160
    }
1161
 
1162
  /* The status struct includes general regs, so mark them valid too */
1163
  pi->gregs_valid  = pi->status_valid;
1164
#ifdef NEW_PROC_API
1165
  /* In the read/write multiple-fd model,
1166
     the status struct includes the fp regs too, so mark them valid too */
1167
  pi->fpregs_valid = pi->status_valid;
1168
#endif
1169
  return pi->status_valid;      /* True if success, false if failure. */
1170
}
1171
 
1172
/*
1173
 * Function: proc_flags
1174
 *
1175
 * returns the process flags (pr_flags field).
1176
 */
1177
 
1178
long
1179
proc_flags (procinfo *pi)
1180
{
1181
  if (!pi->status_valid)
1182
    if (!proc_get_status (pi))
1183
      return 0;  /* FIXME: not a good failure value (but what is?) */
1184
 
1185
#ifdef NEW_PROC_API
1186
# ifdef UNIXWARE
1187
  /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in
1188
     pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t.
1189
     The two sets of flags don't overlap. */
1190
  return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags;
1191
# else
1192
  return pi->prstatus.pr_lwp.pr_flags;
1193
# endif
1194
#else
1195
  return pi->prstatus.pr_flags;
1196
#endif
1197
}
1198
 
1199
/*
1200
 * Function: proc_why
1201
 *
1202
 * returns the pr_why field (why the process stopped).
1203
 */
1204
 
1205
int
1206
proc_why (procinfo *pi)
1207
{
1208
  if (!pi->status_valid)
1209
    if (!proc_get_status (pi))
1210
      return 0;  /* FIXME: not a good failure value (but what is?) */
1211
 
1212
#ifdef NEW_PROC_API
1213
  return pi->prstatus.pr_lwp.pr_why;
1214
#else
1215
  return pi->prstatus.pr_why;
1216
#endif
1217
}
1218
 
1219
/*
1220
 * Function: proc_what
1221
 *
1222
 * returns the pr_what field (details of why the process stopped).
1223
 */
1224
 
1225
int
1226
proc_what (procinfo *pi)
1227
{
1228
  if (!pi->status_valid)
1229
    if (!proc_get_status (pi))
1230
      return 0;  /* FIXME: not a good failure value (but what is?) */
1231
 
1232
#ifdef NEW_PROC_API
1233
  return pi->prstatus.pr_lwp.pr_what;
1234
#else
1235
  return pi->prstatus.pr_what;
1236
#endif
1237
}
1238
 
1239
#ifndef PIOCSSPCACT     /* The following is not supported on OSF.  */
1240
/*
1241
 * Function: proc_nsysarg
1242
 *
1243
 * returns the pr_nsysarg field (number of args to the current syscall).
1244
 */
1245
 
1246
int
1247
proc_nsysarg (procinfo *pi)
1248
{
1249
  if (!pi->status_valid)
1250
    if (!proc_get_status (pi))
1251
      return 0;
1252
 
1253
#ifdef NEW_PROC_API
1254
  return pi->prstatus.pr_lwp.pr_nsysarg;
1255
#else
1256
  return pi->prstatus.pr_nsysarg;
1257
#endif
1258
}
1259
 
1260
/*
1261
 * Function: proc_sysargs
1262
 *
1263
 * returns the pr_sysarg field (pointer to the arguments of current syscall).
1264
 */
1265
 
1266
long *
1267
proc_sysargs (procinfo *pi)
1268
{
1269
  if (!pi->status_valid)
1270
    if (!proc_get_status (pi))
1271
      return NULL;
1272
 
1273
#ifdef NEW_PROC_API
1274
  return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
1275
#else
1276
  return (long *) &pi->prstatus.pr_sysarg;
1277
#endif
1278
}
1279
 
1280
/*
1281
 * Function: proc_syscall
1282
 *
1283
 * returns the pr_syscall field (id of current syscall if we are in one).
1284
 */
1285
 
1286
int
1287
proc_syscall (procinfo *pi)
1288
{
1289
  if (!pi->status_valid)
1290
    if (!proc_get_status (pi))
1291
      return 0;
1292
 
1293
#ifdef NEW_PROC_API
1294
  return pi->prstatus.pr_lwp.pr_syscall;
1295
#else
1296
  return pi->prstatus.pr_syscall;
1297
#endif
1298
}
1299
#endif /* PIOCSSPCACT */
1300
 
1301
/*
1302
 * Function: proc_cursig:
1303
 *
1304
 * returns the pr_cursig field (current signal).
1305
 */
1306
 
1307
long
1308
proc_cursig (struct procinfo *pi)
1309
{
1310
  if (!pi->status_valid)
1311
    if (!proc_get_status (pi))
1312
      return 0;  /* FIXME: not a good failure value (but what is?) */
1313
 
1314
#ifdef NEW_PROC_API
1315
  return pi->prstatus.pr_lwp.pr_cursig;
1316
#else
1317
  return pi->prstatus.pr_cursig;
1318
#endif
1319
}
1320
 
1321
/*
1322
 * Function: proc_modify_flag
1323
 *
1324
 *  === I appologize for the messiness of this function.
1325
 *  === This is an area where the different versions of
1326
 *  === /proc are more inconsistent than usual.     MVS
1327
 *
1328
 * Set or reset any of the following process flags:
1329
 *    PR_FORK   -- forked child will inherit trace flags
1330
 *    PR_RLC    -- traced process runs when last /proc file closed.
1331
 *    PR_KLC    -- traced process is killed when last /proc file closed.
1332
 *    PR_ASYNC  -- LWP's get to run/stop independently.
1333
 *
1334
 * There are three methods for doing this function:
1335
 * 1) Newest: read/write [PCSET/PCRESET/PCUNSET]
1336
 *    [Sol6, Sol7, UW]
1337
 * 2) Middle: PIOCSET/PIOCRESET
1338
 *    [Irix, Sol5]
1339
 * 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC
1340
 *    [OSF, Sol5]
1341
 *
1342
 * Note: Irix does not define PR_ASYNC.
1343
 * Note: OSF  does not define PR_KLC.
1344
 * Note: OSF  is the only one that can ONLY use the oldest method.
1345
 *
1346
 * Arguments:
1347
 *    pi   -- the procinfo
1348
 *    flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
1349
 *    mode -- 1 for set, 0 for reset.
1350
 *
1351
 * Returns non-zero for success, zero for failure.
1352
 */
1353
 
1354
enum { FLAG_RESET, FLAG_SET };
1355
 
1356
static int
1357
proc_modify_flag (procinfo *pi, long flag, long mode)
1358
{
1359
  long win = 0;          /* default to fail */
1360
 
1361
  /*
1362
   * These operations affect the process as a whole, and applying
1363
   * them to an individual LWP has the same meaning as applying them
1364
   * to the main process.  Therefore, if we're ever called with a
1365
   * pointer to an LWP's procinfo, let's substitute the process's
1366
   * procinfo and avoid opening the LWP's file descriptor
1367
   * unnecessarily.
1368
   */
1369
 
1370
  if (pi->pid != 0)
1371
    pi = find_procinfo_or_die (pi->pid, 0);
1372
 
1373
#ifdef NEW_PROC_API     /* Newest method: UnixWare and newer Solarii */
1374
  /* First normalize the PCUNSET/PCRESET command opcode
1375
     (which for no obvious reason has a different definition
1376
     from one operating system to the next...)  */
1377
#ifdef  PCUNSET
1378
#define GDBRESET PCUNSET
1379
#else
1380
#ifdef  PCRESET
1381
#define GDBRESET PCRESET
1382
#endif
1383
#endif
1384
  {
1385
    procfs_ctl_t arg[2];
1386
 
1387
    if (mode == FLAG_SET)       /* Set the flag (RLC, FORK, or ASYNC) */
1388
      arg[0] = PCSET;
1389
    else                        /* Reset the flag */
1390
      arg[0] = GDBRESET;
1391
 
1392
    arg[1] = flag;
1393
    win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1394
  }
1395
#else
1396
#ifdef PIOCSET          /* Irix/Sol5 method */
1397
  if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC) */
1398
    {
1399
      win = (ioctl (pi->ctl_fd, PIOCSET, &flag)   >= 0);
1400
    }
1401
  else                  /* Reset the flag */
1402
    {
1403
      win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0);
1404
    }
1405
 
1406
#else
1407
#ifdef PIOCSRLC         /* Oldest method: OSF */
1408
  switch (flag) {
1409
  case PR_RLC:
1410
    if (mode == FLAG_SET)       /* Set run-on-last-close */
1411
      {
1412
        win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0);
1413
      }
1414
    else                        /* Clear run-on-last-close */
1415
      {
1416
        win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0);
1417
      }
1418
    break;
1419
  case PR_FORK:
1420
    if (mode == FLAG_SET)       /* Set inherit-on-fork */
1421
      {
1422
        win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0);
1423
      }
1424
    else                        /* Clear inherit-on-fork */
1425
      {
1426
        win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0);
1427
      }
1428
    break;
1429
  default:
1430
    win = 0;             /* fail -- unknown flag (can't do PR_ASYNC) */
1431
    break;
1432
  }
1433
#endif
1434
#endif
1435
#endif
1436
#undef GDBRESET
1437
  /* The above operation renders the procinfo's cached pstatus obsolete. */
1438
  pi->status_valid = 0;
1439
 
1440
  if (!win)
1441
    warning ("procfs: modify_flag failed to turn %s %s",
1442
             flag == PR_FORK  ? "PR_FORK"  :
1443
             flag == PR_RLC   ? "PR_RLC"   :
1444
#ifdef PR_ASYNC
1445
             flag == PR_ASYNC ? "PR_ASYNC" :
1446
#endif
1447
#ifdef PR_KLC
1448
             flag == PR_KLC   ? "PR_KLC"   :
1449
#endif
1450
             "<unknown flag>",
1451
             mode == FLAG_RESET ? "off" : "on");
1452
 
1453
  return win;
1454
}
1455
 
1456
/*
1457
 * Function: proc_set_run_on_last_close
1458
 *
1459
 * Set the run_on_last_close flag.
1460
 * Process with all threads will become runnable
1461
 * when debugger closes all /proc fds.
1462
 *
1463
 * Returns non-zero for success, zero for failure.
1464
 */
1465
 
1466
int
1467
proc_set_run_on_last_close (procinfo *pi)
1468
{
1469
  return proc_modify_flag (pi, PR_RLC, FLAG_SET);
1470
}
1471
 
1472
/*
1473
 * Function: proc_unset_run_on_last_close
1474
 *
1475
 * Reset the run_on_last_close flag.
1476
 * Process will NOT become runnable
1477
 * when debugger closes its file handles.
1478
 *
1479
 * Returns non-zero for success, zero for failure.
1480
 */
1481
 
1482
int
1483
proc_unset_run_on_last_close (procinfo *pi)
1484
{
1485
  return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
1486
}
1487
 
1488
#ifdef PR_KLC
1489
/*
1490
 * Function: proc_set_kill_on_last_close
1491
 *
1492
 * Set the kill_on_last_close flag.
1493
 * Process with all threads will be killed when debugger
1494
 * closes all /proc fds (or debugger exits or dies).
1495
 *
1496
 * Returns non-zero for success, zero for failure.
1497
 */
1498
 
1499
int
1500
proc_set_kill_on_last_close (procinfo *pi)
1501
{
1502
  return proc_modify_flag (pi, PR_KLC, FLAG_SET);
1503
}
1504
 
1505
/*
1506
 * Function: proc_unset_kill_on_last_close
1507
 *
1508
 * Reset the kill_on_last_close flag.
1509
 * Process will NOT be killed when debugger
1510
 * closes its file handles (or exits or dies).
1511
 *
1512
 * Returns non-zero for success, zero for failure.
1513
 */
1514
 
1515
int
1516
proc_unset_kill_on_last_close (procinfo *pi)
1517
{
1518
  return proc_modify_flag (pi, PR_KLC, FLAG_RESET);
1519
}
1520
#endif /* PR_KLC */
1521
 
1522
/*
1523
 * Function: proc_set_inherit_on_fork
1524
 *
1525
 * Set inherit_on_fork flag.
1526
 * If the process forks a child while we are registered for events
1527
 * in the parent, then we will also recieve events from the child.
1528
 *
1529
 * Returns non-zero for success, zero for failure.
1530
 */
1531
 
1532
int
1533
proc_set_inherit_on_fork (procinfo *pi)
1534
{
1535
  return proc_modify_flag (pi, PR_FORK, FLAG_SET);
1536
}
1537
 
1538
/*
1539
 * Function: proc_unset_inherit_on_fork
1540
 *
1541
 * Reset inherit_on_fork flag.
1542
 * If the process forks a child while we are registered for events
1543
 * in the parent, then we will NOT recieve events from the child.
1544
 *
1545
 * Returns non-zero for success, zero for failure.
1546
 */
1547
 
1548
int
1549
proc_unset_inherit_on_fork (procinfo *pi)
1550
{
1551
  return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
1552
}
1553
 
1554
#ifdef PR_ASYNC
1555
/*
1556
 * Function: proc_set_async
1557
 *
1558
 * Set PR_ASYNC flag.
1559
 * If one LWP stops because of a debug event (signal etc.),
1560
 * the remaining LWPs will continue to run.
1561
 *
1562
 * Returns non-zero for success, zero for failure.
1563
 */
1564
 
1565
int
1566
proc_set_async (procinfo *pi)
1567
{
1568
  return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
1569
}
1570
 
1571
/*
1572
 * Function: proc_unset_async
1573
 *
1574
 * Reset PR_ASYNC flag.
1575
 * If one LWP stops because of a debug event (signal etc.),
1576
 * then all other LWPs will stop as well.
1577
 *
1578
 * Returns non-zero for success, zero for failure.
1579
 */
1580
 
1581
int
1582
proc_unset_async (procinfo *pi)
1583
{
1584
  return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
1585
}
1586
#endif /* PR_ASYNC */
1587
 
1588
/*
1589
 * Function: proc_stop_process
1590
 *
1591
 * Request the process/LWP to stop.  Does not wait.
1592
 * Returns non-zero for success, zero for failure.
1593
 */
1594
 
1595
int
1596
proc_stop_process (procinfo *pi)
1597
{
1598
  int win;
1599
 
1600
  /*
1601
   * We might conceivably apply this operation to an LWP, and
1602
   * the LWP's ctl file descriptor might not be open.
1603
   */
1604
 
1605
  if (pi->ctl_fd == 0 &&
1606
      open_procinfo_files (pi, FD_CTL) == 0)
1607
    return 0;
1608
  else
1609
    {
1610
#ifdef NEW_PROC_API
1611
      procfs_ctl_t cmd = PCSTOP;
1612
      win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1613
#else   /* ioctl method */
1614
      win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0);
1615
      /* Note: the call also reads the prstatus.  */
1616
      if (win)
1617
        {
1618
          pi->status_valid = 1;
1619
          PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1620
                                    proc_why (pi),
1621
                                    proc_what (pi),
1622
                                    proc_get_current_thread (pi));
1623
        }
1624
#endif
1625
    }
1626
 
1627
  return win;
1628
}
1629
 
1630
/*
1631
 * Function: proc_wait_for_stop
1632
 *
1633
 * Wait for the process or LWP to stop (block until it does).
1634
 * Returns non-zero for success, zero for failure.
1635
 */
1636
 
1637
int
1638
proc_wait_for_stop (procinfo *pi)
1639
{
1640
  int win;
1641
 
1642
  /*
1643
   * We should never have to apply this operation to any procinfo
1644
   * except the one for the main process.  If that ever changes
1645
   * for any reason, then take out the following clause and
1646
   * replace it with one that makes sure the ctl_fd is open.
1647
   */
1648
 
1649
  if (pi->tid != 0)
1650
    pi = find_procinfo_or_die (pi->pid, 0);
1651
 
1652
#ifdef NEW_PROC_API
1653
  {
1654
    procfs_ctl_t cmd = PCWSTOP;
1655
    win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1656
    /* We been runnin' and we stopped -- need to update status.  */
1657
    pi->status_valid = 0;
1658
  }
1659
#else   /* ioctl method */
1660
  win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0);
1661
  /* Above call also refreshes the prstatus.  */
1662
  if (win)
1663
    {
1664
      pi->status_valid = 1;
1665
      PROC_PRETTYFPRINT_STATUS (proc_flags (pi),
1666
                                proc_why (pi),
1667
                                proc_what (pi),
1668
                                proc_get_current_thread (pi));
1669
    }
1670
#endif
1671
 
1672
  return win;
1673
}
1674
 
1675
/*
1676
 * Function: proc_run_process
1677
 *
1678
 * Make the process or LWP runnable.
1679
 * Options (not all are implemented):
1680
 *   - single-step
1681
 *   - clear current fault
1682
 *   - clear current signal
1683
 *   - abort the current system call
1684
 *   - stop as soon as finished with system call
1685
 *   - (ioctl): set traced signal set
1686
 *   - (ioctl): set held   signal set
1687
 *   - (ioctl): set traced fault  set
1688
 *   - (ioctl): set start pc (vaddr)
1689
 * Always clear the current fault.
1690
 * Clear the current signal if 'signo' is zero.
1691
 *
1692
 * Arguments:
1693
 *   pi         the process or LWP to operate on.
1694
 *   step       if true, set the process or LWP to trap after one instr.
1695
 *   signo      if zero, clear the current signal if any.
1696
 *              if non-zero, set the current signal to this one.
1697
 *
1698
 * Returns non-zero for success, zero for failure.
1699
 */
1700
 
1701
int
1702
proc_run_process (procinfo *pi, int step, int signo)
1703
{
1704
  int win;
1705
  int runflags;
1706
 
1707
  /*
1708
   * We will probably have to apply this operation to individual threads,
1709
   * so make sure the control file descriptor is open.
1710
   */
1711
 
1712
  if (pi->ctl_fd == 0 &&
1713
      open_procinfo_files (pi, FD_CTL) == 0)
1714
    {
1715
      return 0;
1716
    }
1717
 
1718
  runflags    = PRCFAULT;       /* always clear current fault  */
1719
  if (step)
1720
    runflags |= PRSTEP;
1721
  if (signo == 0)
1722
    runflags |= PRCSIG;
1723
  else if (signo != -1)         /* -1 means do nothing W.R.T. signals */
1724
    proc_set_current_signal (pi, signo);
1725
 
1726
#ifdef NEW_PROC_API
1727
  {
1728
    procfs_ctl_t cmd[2];
1729
 
1730
    cmd[0]  = PCRUN;
1731
    cmd[1]  = runflags;
1732
    win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1733
  }
1734
#else   /* ioctl method */
1735
  {
1736
    prrun_t prrun;
1737
 
1738
    memset (&prrun, 0, sizeof (prrun));
1739
    prrun.pr_flags  = runflags;
1740
    win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0);
1741
  }
1742
#endif
1743
 
1744
  return win;
1745
}
1746
 
1747
/*
1748
 * Function: proc_set_traced_signals
1749
 *
1750
 * Register to trace signals in the process or LWP.
1751
 * Returns non-zero for success, zero for failure.
1752
 */
1753
 
1754
int
1755
proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset)
1756
{
1757
  int win;
1758
 
1759
  /*
1760
   * We should never have to apply this operation to any procinfo
1761
   * except the one for the main process.  If that ever changes
1762
   * for any reason, then take out the following clause and
1763
   * replace it with one that makes sure the ctl_fd is open.
1764
   */
1765
 
1766
  if (pi->tid != 0)
1767
    pi = find_procinfo_or_die (pi->pid, 0);
1768
 
1769
#ifdef NEW_PROC_API
1770
  {
1771
    struct {
1772
      procfs_ctl_t cmd;
1773
      /* Use char array to avoid alignment issues.  */
1774
      char sigset[sizeof (gdb_sigset_t)];
1775
    } arg;
1776
 
1777
    arg.cmd = PCSTRACE;
1778
    memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t));
1779
 
1780
    win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1781
  }
1782
#else   /* ioctl method */
1783
  win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0);
1784
#endif
1785
  /* The above operation renders the procinfo's cached pstatus obsolete. */
1786
  pi->status_valid = 0;
1787
 
1788
  if (!win)
1789
    warning ("procfs: set_traced_signals failed");
1790
  return win;
1791
}
1792
 
1793
/*
1794
 * Function: proc_set_traced_faults
1795
 *
1796
 * Register to trace hardware faults in the process or LWP.
1797
 * Returns non-zero for success, zero for failure.
1798
 */
1799
 
1800
int
1801
proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
1802
{
1803
  int win;
1804
 
1805
  /*
1806
   * We should never have to apply this operation to any procinfo
1807
   * except the one for the main process.  If that ever changes
1808
   * for any reason, then take out the following clause and
1809
   * replace it with one that makes sure the ctl_fd is open.
1810
   */
1811
 
1812
  if (pi->tid != 0)
1813
    pi = find_procinfo_or_die (pi->pid, 0);
1814
 
1815
#ifdef NEW_PROC_API
1816
  {
1817
    struct {
1818
      procfs_ctl_t cmd;
1819
      /* Use char array to avoid alignment issues.  */
1820
      char fltset[sizeof (fltset_t)];
1821
    } arg;
1822
 
1823
    arg.cmd = PCSFAULT;
1824
    memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1825
 
1826
    win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1827
  }
1828
#else   /* ioctl method */
1829
  win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0);
1830
#endif
1831
  /* The above operation renders the procinfo's cached pstatus obsolete. */
1832
  pi->status_valid = 0;
1833
 
1834
  return win;
1835
}
1836
 
1837
/*
1838
 * Function: proc_set_traced_sysentry
1839
 *
1840
 * Register to trace entry to system calls in the process or LWP.
1841
 * Returns non-zero for success, zero for failure.
1842
 */
1843
 
1844
int
1845
proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1846
{
1847
  int win;
1848
 
1849
  /*
1850
   * We should never have to apply this operation to any procinfo
1851
   * except the one for the main process.  If that ever changes
1852
   * for any reason, then take out the following clause and
1853
   * replace it with one that makes sure the ctl_fd is open.
1854
   */
1855
 
1856
  if (pi->tid != 0)
1857
    pi = find_procinfo_or_die (pi->pid, 0);
1858
 
1859
#ifdef NEW_PROC_API
1860
  {
1861
    struct gdb_proc_ctl_pcsentry {
1862
      procfs_ctl_t cmd;
1863
      /* Use char array to avoid alignment issues.  */
1864
      char sysset[sizeof (sysset_t)];
1865
    } *argp;
1866
    int argp_size = sizeof (struct gdb_proc_ctl_pcsentry)
1867
                  - sizeof (sysset_t)
1868
                  + sysset_t_size (pi);
1869
 
1870
    argp = xmalloc (argp_size);
1871
 
1872
    argp->cmd = PCSENTRY;
1873
    memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1874
 
1875
    win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1876
    xfree (argp);
1877
  }
1878
#else   /* ioctl method */
1879
  win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0);
1880
#endif
1881
  /* The above operation renders the procinfo's cached pstatus obsolete. */
1882
  pi->status_valid = 0;
1883
 
1884
  return win;
1885
}
1886
 
1887
/*
1888
 * Function: proc_set_traced_sysexit
1889
 *
1890
 * Register to trace exit from system calls in the process or LWP.
1891
 * Returns non-zero for success, zero for failure.
1892
 */
1893
 
1894
int
1895
proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1896
{
1897
  int win;
1898
 
1899
  /*
1900
   * We should never have to apply this operation to any procinfo
1901
   * except the one for the main process.  If that ever changes
1902
   * for any reason, then take out the following clause and
1903
   * replace it with one that makes sure the ctl_fd is open.
1904
   */
1905
 
1906
  if (pi->tid != 0)
1907
    pi = find_procinfo_or_die (pi->pid, 0);
1908
 
1909
#ifdef NEW_PROC_API
1910
  {
1911
    struct gdb_proc_ctl_pcsexit {
1912
      procfs_ctl_t cmd;
1913
      /* Use char array to avoid alignment issues.  */
1914
      char sysset[sizeof (sysset_t)];
1915
    } *argp;
1916
    int argp_size = sizeof (struct gdb_proc_ctl_pcsexit)
1917
                  - sizeof (sysset_t)
1918
                  + sysset_t_size (pi);
1919
 
1920
    argp = xmalloc (argp_size);
1921
 
1922
    argp->cmd = PCSEXIT;
1923
    memcpy (&argp->sysset, sysset, sysset_t_size (pi));
1924
 
1925
    win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size);
1926
    xfree (argp);
1927
  }
1928
#else   /* ioctl method */
1929
  win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0);
1930
#endif
1931
  /* The above operation renders the procinfo's cached pstatus obsolete. */
1932
  pi->status_valid = 0;
1933
 
1934
  return win;
1935
}
1936
 
1937
/*
1938
 * Function: proc_set_held_signals
1939
 *
1940
 * Specify the set of blocked / held signals in the process or LWP.
1941
 * Returns non-zero for success, zero for failure.
1942
 */
1943
 
1944
int
1945
proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold)
1946
{
1947
  int win;
1948
 
1949
  /*
1950
   * We should never have to apply this operation to any procinfo
1951
   * except the one for the main process.  If that ever changes
1952
   * for any reason, then take out the following clause and
1953
   * replace it with one that makes sure the ctl_fd is open.
1954
   */
1955
 
1956
  if (pi->tid != 0)
1957
    pi = find_procinfo_or_die (pi->pid, 0);
1958
 
1959
#ifdef NEW_PROC_API
1960
  {
1961
    struct {
1962
      procfs_ctl_t cmd;
1963
      /* Use char array to avoid alignment issues.  */
1964
      char hold[sizeof (gdb_sigset_t)];
1965
    } arg;
1966
 
1967
    arg.cmd  = PCSHOLD;
1968
    memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t));
1969
    win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1970
  }
1971
#else
1972
  win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0);
1973
#endif
1974
  /* The above operation renders the procinfo's cached pstatus obsolete. */
1975
  pi->status_valid = 0;
1976
 
1977
  return win;
1978
}
1979
 
1980
/*
1981
 * Function: proc_get_pending_signals
1982
 *
1983
 * returns the set of signals that are pending in the process or LWP.
1984
 * Will also copy the sigset if 'save' is non-zero.
1985
 */
1986
 
1987
gdb_sigset_t *
1988
proc_get_pending_signals (procinfo *pi, gdb_sigset_t *save)
1989
{
1990
  gdb_sigset_t *ret = NULL;
1991
 
1992
  /*
1993
   * We should never have to apply this operation to any procinfo
1994
   * except the one for the main process.  If that ever changes
1995
   * for any reason, then take out the following clause and
1996
   * replace it with one that makes sure the ctl_fd is open.
1997
   */
1998
 
1999
  if (pi->tid != 0)
2000
    pi = find_procinfo_or_die (pi->pid, 0);
2001
 
2002
  if (!pi->status_valid)
2003
    if (!proc_get_status (pi))
2004
      return NULL;
2005
 
2006
#ifdef NEW_PROC_API
2007
  ret = &pi->prstatus.pr_lwp.pr_lwppend;
2008
#else
2009
  ret = &pi->prstatus.pr_sigpend;
2010
#endif
2011
  if (save && ret)
2012
    memcpy (save, ret, sizeof (gdb_sigset_t));
2013
 
2014
  return ret;
2015
}
2016
 
2017
/*
2018
 * Function: proc_get_signal_actions
2019
 *
2020
 * returns the set of signal actions.
2021
 * Will also copy the sigactionset if 'save' is non-zero.
2022
 */
2023
 
2024
gdb_sigaction_t *
2025
proc_get_signal_actions (procinfo *pi, gdb_sigaction_t *save)
2026
{
2027
  gdb_sigaction_t *ret = NULL;
2028
 
2029
  /*
2030
   * We should never have to apply this operation to any procinfo
2031
   * except the one for the main process.  If that ever changes
2032
   * for any reason, then take out the following clause and
2033
   * replace it with one that makes sure the ctl_fd is open.
2034
   */
2035
 
2036
  if (pi->tid != 0)
2037
    pi = find_procinfo_or_die (pi->pid, 0);
2038
 
2039
  if (!pi->status_valid)
2040
    if (!proc_get_status (pi))
2041
      return NULL;
2042
 
2043
#ifdef NEW_PROC_API
2044
  ret = &pi->prstatus.pr_lwp.pr_action;
2045
#else
2046
  ret = &pi->prstatus.pr_action;
2047
#endif
2048
  if (save && ret)
2049
    memcpy (save, ret, sizeof (gdb_sigaction_t));
2050
 
2051
  return ret;
2052
}
2053
 
2054
/*
2055
 * Function: proc_get_held_signals
2056
 *
2057
 * returns the set of signals that are held / blocked.
2058
 * Will also copy the sigset if 'save' is non-zero.
2059
 */
2060
 
2061
gdb_sigset_t *
2062
proc_get_held_signals (procinfo *pi, gdb_sigset_t *save)
2063
{
2064
  gdb_sigset_t *ret = NULL;
2065
 
2066
  /*
2067
   * We should never have to apply this operation to any procinfo
2068
   * except the one for the main process.  If that ever changes
2069
   * for any reason, then take out the following clause and
2070
   * replace it with one that makes sure the ctl_fd is open.
2071
   */
2072
 
2073
  if (pi->tid != 0)
2074
    pi = find_procinfo_or_die (pi->pid, 0);
2075
 
2076
#ifdef NEW_PROC_API
2077
  if (!pi->status_valid)
2078
    if (!proc_get_status (pi))
2079
      return NULL;
2080
 
2081
#ifdef UNIXWARE
2082
  ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask;
2083
#else
2084
  ret = &pi->prstatus.pr_lwp.pr_lwphold;
2085
#endif /* UNIXWARE */
2086
#else  /* not NEW_PROC_API */
2087
  {
2088
    static gdb_sigset_t sigheld;
2089
 
2090
    if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0)
2091
      ret = &sigheld;
2092
  }
2093
#endif /* NEW_PROC_API */
2094
  if (save && ret)
2095
    memcpy (save, ret, sizeof (gdb_sigset_t));
2096
 
2097
  return ret;
2098
}
2099
 
2100
/*
2101
 * Function: proc_get_traced_signals
2102
 *
2103
 * returns the set of signals that are traced / debugged.
2104
 * Will also copy the sigset if 'save' is non-zero.
2105
 */
2106
 
2107
gdb_sigset_t *
2108
proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save)
2109
{
2110
  gdb_sigset_t *ret = NULL;
2111
 
2112
  /*
2113
   * We should never have to apply this operation to any procinfo
2114
   * except the one for the main process.  If that ever changes
2115
   * for any reason, then take out the following clause and
2116
   * replace it with one that makes sure the ctl_fd is open.
2117
   */
2118
 
2119
  if (pi->tid != 0)
2120
    pi = find_procinfo_or_die (pi->pid, 0);
2121
 
2122
#ifdef NEW_PROC_API
2123
  if (!pi->status_valid)
2124
    if (!proc_get_status (pi))
2125
      return NULL;
2126
 
2127
  ret = &pi->prstatus.pr_sigtrace;
2128
#else
2129
  {
2130
    static gdb_sigset_t sigtrace;
2131
 
2132
    if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0)
2133
      ret = &sigtrace;
2134
  }
2135
#endif
2136
  if (save && ret)
2137
    memcpy (save, ret, sizeof (gdb_sigset_t));
2138
 
2139
  return ret;
2140
}
2141
 
2142
/*
2143
 * Function: proc_trace_signal
2144
 *
2145
 * Add 'signo' to the set of signals that are traced.
2146
 * Returns non-zero for success, zero for failure.
2147
 */
2148
 
2149
int
2150
proc_trace_signal (procinfo *pi, int signo)
2151
{
2152
  gdb_sigset_t temp;
2153
 
2154
  /*
2155
   * We should never have to apply this operation to any procinfo
2156
   * except the one for the main process.  If that ever changes
2157
   * for any reason, then take out the following clause and
2158
   * replace it with one that makes sure the ctl_fd is open.
2159
   */
2160
 
2161
  if (pi->tid != 0)
2162
    pi = find_procinfo_or_die (pi->pid, 0);
2163
 
2164
  if (pi)
2165
    {
2166
      if (proc_get_traced_signals (pi, &temp))
2167
        {
2168
          praddset (&temp, signo);
2169
          return proc_set_traced_signals (pi, &temp);
2170
        }
2171
    }
2172
 
2173
  return 0;      /* failure */
2174
}
2175
 
2176
/*
2177
 * Function: proc_ignore_signal
2178
 *
2179
 * Remove 'signo' from the set of signals that are traced.
2180
 * Returns non-zero for success, zero for failure.
2181
 */
2182
 
2183
int
2184
proc_ignore_signal (procinfo *pi, int signo)
2185
{
2186
  gdb_sigset_t temp;
2187
 
2188
  /*
2189
   * We should never have to apply this operation to any procinfo
2190
   * except the one for the main process.  If that ever changes
2191
   * for any reason, then take out the following clause and
2192
   * replace it with one that makes sure the ctl_fd is open.
2193
   */
2194
 
2195
  if (pi->tid != 0)
2196
    pi = find_procinfo_or_die (pi->pid, 0);
2197
 
2198
  if (pi)
2199
    {
2200
      if (proc_get_traced_signals (pi, &temp))
2201
        {
2202
          prdelset (&temp, signo);
2203
          return proc_set_traced_signals (pi, &temp);
2204
        }
2205
    }
2206
 
2207
  return 0;      /* failure */
2208
}
2209
 
2210
/*
2211
 * Function: proc_get_traced_faults
2212
 *
2213
 * returns the set of hardware faults that are traced /debugged.
2214
 * Will also copy the faultset if 'save' is non-zero.
2215
 */
2216
 
2217
fltset_t *
2218
proc_get_traced_faults (procinfo *pi, fltset_t *save)
2219
{
2220
  fltset_t *ret = NULL;
2221
 
2222
  /*
2223
   * We should never have to apply this operation to any procinfo
2224
   * except the one for the main process.  If that ever changes
2225
   * for any reason, then take out the following clause and
2226
   * replace it with one that makes sure the ctl_fd is open.
2227
   */
2228
 
2229
  if (pi->tid != 0)
2230
    pi = find_procinfo_or_die (pi->pid, 0);
2231
 
2232
#ifdef NEW_PROC_API
2233
  if (!pi->status_valid)
2234
    if (!proc_get_status (pi))
2235
      return NULL;
2236
 
2237
  ret = &pi->prstatus.pr_flttrace;
2238
#else
2239
  {
2240
    static fltset_t flttrace;
2241
 
2242
    if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0)
2243
      ret = &flttrace;
2244
  }
2245
#endif
2246
  if (save && ret)
2247
    memcpy (save, ret, sizeof (fltset_t));
2248
 
2249
  return ret;
2250
}
2251
 
2252
/*
2253
 * Function: proc_get_traced_sysentry
2254
 *
2255
 * returns the set of syscalls that are traced /debugged on entry.
2256
 * Will also copy the syscall set if 'save' is non-zero.
2257
 */
2258
 
2259
sysset_t *
2260
proc_get_traced_sysentry (procinfo *pi, sysset_t *save)
2261
{
2262
  sysset_t *ret = NULL;
2263
 
2264
  /*
2265
   * We should never have to apply this operation to any procinfo
2266
   * except the one for the main process.  If that ever changes
2267
   * for any reason, then take out the following clause and
2268
   * replace it with one that makes sure the ctl_fd is open.
2269
   */
2270
 
2271
  if (pi->tid != 0)
2272
    pi = find_procinfo_or_die (pi->pid, 0);
2273
 
2274
#ifdef NEW_PROC_API
2275
  if (!pi->status_valid)
2276
    if (!proc_get_status (pi))
2277
      return NULL;
2278
 
2279
#ifndef DYNAMIC_SYSCALLS
2280
  ret = &pi->prstatus.pr_sysentry;
2281
#else /* DYNAMIC_SYSCALLS */
2282
  {
2283
    static sysset_t *sysentry;
2284
    size_t size;
2285
 
2286
    if (!sysentry)
2287
      sysentry = sysset_t_alloc (pi);
2288
    ret = sysentry;
2289
    if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2290
      return NULL;
2291
    if (pi->prstatus.pr_sysentry_offset == 0)
2292
      {
2293
        gdb_premptysysset (sysentry);
2294
      }
2295
    else
2296
      {
2297
        int rsize;
2298
 
2299
        if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset,
2300
                   SEEK_SET)
2301
            != (off_t) pi->prstatus.pr_sysentry_offset)
2302
          return NULL;
2303
        size = sysset_t_size (pi);
2304
        gdb_premptysysset (sysentry);
2305
        rsize = read (pi->status_fd, sysentry, size);
2306
        if (rsize < 0)
2307
          return NULL;
2308
      }
2309
  }
2310
#endif /* DYNAMIC_SYSCALLS */
2311
#else /* !NEW_PROC_API */
2312
  {
2313
    static sysset_t sysentry;
2314
 
2315
    if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0)
2316
      ret = &sysentry;
2317
  }
2318
#endif /* NEW_PROC_API */
2319
  if (save && ret)
2320
    memcpy (save, ret, sysset_t_size (pi));
2321
 
2322
  return ret;
2323
}
2324
 
2325
/*
2326
 * Function: proc_get_traced_sysexit
2327
 *
2328
 * returns the set of syscalls that are traced /debugged on exit.
2329
 * Will also copy the syscall set if 'save' is non-zero.
2330
 */
2331
 
2332
sysset_t *
2333
proc_get_traced_sysexit (procinfo *pi, sysset_t *save)
2334
{
2335
  sysset_t * ret = NULL;
2336
 
2337
  /*
2338
   * We should never have to apply this operation to any procinfo
2339
   * except the one for the main process.  If that ever changes
2340
   * for any reason, then take out the following clause and
2341
   * replace it with one that makes sure the ctl_fd is open.
2342
   */
2343
 
2344
  if (pi->tid != 0)
2345
    pi = find_procinfo_or_die (pi->pid, 0);
2346
 
2347
#ifdef NEW_PROC_API
2348
  if (!pi->status_valid)
2349
    if (!proc_get_status (pi))
2350
      return NULL;
2351
 
2352
#ifndef DYNAMIC_SYSCALLS
2353
  ret = &pi->prstatus.pr_sysexit;
2354
#else /* DYNAMIC_SYSCALLS */
2355
  {
2356
    static sysset_t *sysexit;
2357
    size_t size;
2358
 
2359
    if (!sysexit)
2360
      sysexit = sysset_t_alloc (pi);
2361
    ret = sysexit;
2362
    if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
2363
      return NULL;
2364
    if (pi->prstatus.pr_sysexit_offset == 0)
2365
      {
2366
        gdb_premptysysset (sysexit);
2367
      }
2368
    else
2369
      {
2370
        int rsize;
2371
 
2372
        if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, SEEK_SET)
2373
            != (off_t) pi->prstatus.pr_sysexit_offset)
2374
          return NULL;
2375
        size = sysset_t_size (pi);
2376
        gdb_premptysysset (sysexit);
2377
        rsize = read (pi->status_fd, sysexit, size);
2378
        if (rsize < 0)
2379
          return NULL;
2380
      }
2381
  }
2382
#endif /* DYNAMIC_SYSCALLS */
2383
#else
2384
  {
2385
    static sysset_t sysexit;
2386
 
2387
    if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0)
2388
      ret = &sysexit;
2389
  }
2390
#endif
2391
  if (save && ret)
2392
    memcpy (save, ret, sysset_t_size (pi));
2393
 
2394
  return ret;
2395
}
2396
 
2397
/*
2398
 * Function: proc_clear_current_fault
2399
 *
2400
 * The current fault (if any) is cleared; the associated signal
2401
 * will not be sent to the process or LWP when it resumes.
2402
 * Returns non-zero for success,  zero for failure.
2403
 */
2404
 
2405
int
2406
proc_clear_current_fault (procinfo *pi)
2407
{
2408
  int win;
2409
 
2410
  /*
2411
   * We should never have to apply this operation to any procinfo
2412
   * except the one for the main process.  If that ever changes
2413
   * for any reason, then take out the following clause and
2414
   * replace it with one that makes sure the ctl_fd is open.
2415
   */
2416
 
2417
  if (pi->tid != 0)
2418
    pi = find_procinfo_or_die (pi->pid, 0);
2419
 
2420
#ifdef NEW_PROC_API
2421
  {
2422
    procfs_ctl_t cmd = PCCFAULT;
2423
    win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
2424
  }
2425
#else
2426
  win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0);
2427
#endif
2428
 
2429
  return win;
2430
}
2431
 
2432
/*
2433
 * Function: proc_set_current_signal
2434
 *
2435
 * Set the "current signal" that will be delivered next to the process.
2436
 * NOTE: semantics are different from those of KILL.
2437
 * This signal will be delivered to the process or LWP
2438
 * immediately when it is resumed (even if the signal is held/blocked);
2439
 * it will NOT immediately cause another event of interest, and will NOT
2440
 * first trap back to the debugger.
2441
 *
2442
 * Returns non-zero for success,  zero for failure.
2443
 */
2444
 
2445
int
2446
proc_set_current_signal (procinfo *pi, int signo)
2447
{
2448
  int win;
2449
  struct {
2450
    procfs_ctl_t cmd;
2451
    /* Use char array to avoid alignment issues.  */
2452
    char sinfo[sizeof (gdb_siginfo_t)];
2453
  } arg;
2454
  gdb_siginfo_t *mysinfo;
2455
 
2456
  /*
2457
   * We should never have to apply this operation to any procinfo
2458
   * except the one for the main process.  If that ever changes
2459
   * for any reason, then take out the following clause and
2460
   * replace it with one that makes sure the ctl_fd is open.
2461
   */
2462
 
2463
  if (pi->tid != 0)
2464
    pi = find_procinfo_or_die (pi->pid, 0);
2465
 
2466
#ifdef PROCFS_DONT_PIOCSSIG_CURSIG
2467
  /* With Alpha OSF/1 procfs, the kernel gets really confused if it
2468
   * receives a PIOCSSIG with a signal identical to the current signal,
2469
   * it messes up the current signal. Work around the kernel bug.
2470
   */
2471
  if (signo > 0 &&
2472
      signo == proc_cursig (pi))
2473
    return 1;           /* I assume this is a success? */
2474
#endif
2475
 
2476
  /* The pointer is just a type alias.  */
2477
  mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2478
  mysinfo->si_signo = signo;
2479
  mysinfo->si_code  = 0;
2480
  mysinfo->si_pid   = getpid ();       /* ?why? */
2481
  mysinfo->si_uid   = getuid ();       /* ?why? */
2482
 
2483
#ifdef NEW_PROC_API
2484
  arg.cmd = PCSSIG;
2485
  win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg))  == sizeof (arg));
2486
#else
2487
  win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0);
2488
#endif
2489
 
2490
  return win;
2491
}
2492
 
2493
/*
2494
 * Function: proc_clear_current_signal
2495
 *
2496
 * The current signal (if any) is cleared, and
2497
 * is not sent to the process or LWP when it resumes.
2498
 * Returns non-zero for success,  zero for failure.
2499
 */
2500
 
2501
int
2502
proc_clear_current_signal (procinfo *pi)
2503
{
2504
  int win;
2505
 
2506
  /*
2507
   * We should never have to apply this operation to any procinfo
2508
   * except the one for the main process.  If that ever changes
2509
   * for any reason, then take out the following clause and
2510
   * replace it with one that makes sure the ctl_fd is open.
2511
   */
2512
 
2513
  if (pi->tid != 0)
2514
    pi = find_procinfo_or_die (pi->pid, 0);
2515
 
2516
#ifdef NEW_PROC_API
2517
  {
2518
    struct {
2519
      procfs_ctl_t cmd;
2520
      /* Use char array to avoid alignment issues.  */
2521
      char sinfo[sizeof (gdb_siginfo_t)];
2522
    } arg;
2523
    gdb_siginfo_t *mysinfo;
2524
 
2525
    arg.cmd = PCSSIG;
2526
    /* The pointer is just a type alias.  */
2527
    mysinfo = (gdb_siginfo_t *) &arg.sinfo;
2528
    mysinfo->si_signo = 0;
2529
    mysinfo->si_code  = 0;
2530
    mysinfo->si_errno = 0;
2531
    mysinfo->si_pid   = getpid ();       /* ?why? */
2532
    mysinfo->si_uid   = getuid ();       /* ?why? */
2533
 
2534
    win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2535
  }
2536
#else
2537
  win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0);
2538
#endif
2539
 
2540
  return win;
2541
}
2542
 
2543
/*
2544
 * Function: proc_get_gregs
2545
 *
2546
 * Get the general registers for the process or LWP.
2547
 * Returns non-zero for success, zero for failure.
2548
 */
2549
 
2550
gdb_gregset_t *
2551
proc_get_gregs (procinfo *pi)
2552
{
2553
  if (!pi->status_valid || !pi->gregs_valid)
2554
    if (!proc_get_status (pi))
2555
      return NULL;
2556
 
2557
  /*
2558
   * OK, sorry about the ifdef's.
2559
   * There's three cases instead of two, because
2560
   * in this instance Unixware and Solaris/RW differ.
2561
   */
2562
 
2563
#ifdef NEW_PROC_API
2564
#ifdef UNIXWARE         /* ugh, a true architecture dependency */
2565
  return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs;
2566
#else   /* not Unixware */
2567
  return &pi->prstatus.pr_lwp.pr_reg;
2568
#endif  /* Unixware */
2569
#else   /* not NEW_PROC_API */
2570
  return &pi->prstatus.pr_reg;
2571
#endif  /* NEW_PROC_API */
2572
}
2573
 
2574
/*
2575
 * Function: proc_get_fpregs
2576
 *
2577
 * Get the floating point registers for the process or LWP.
2578
 * Returns non-zero for success, zero for failure.
2579
 */
2580
 
2581
gdb_fpregset_t *
2582
proc_get_fpregs (procinfo *pi)
2583
{
2584
#ifdef NEW_PROC_API
2585
  if (!pi->status_valid || !pi->fpregs_valid)
2586
    if (!proc_get_status (pi))
2587
      return NULL;
2588
 
2589
#ifdef UNIXWARE         /* a true architecture dependency */
2590
  return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs;
2591
#else
2592
  return &pi->prstatus.pr_lwp.pr_fpreg;
2593
#endif  /* Unixware */
2594
 
2595
#else   /* not NEW_PROC_API */
2596
  if (pi->fpregs_valid)
2597
    return &pi->fpregset;       /* already got 'em */
2598
  else
2599
    {
2600
      if (pi->ctl_fd == 0 &&
2601
          open_procinfo_files (pi, FD_CTL) == 0)
2602
        {
2603
          return NULL;
2604
        }
2605
      else
2606
        {
2607
#ifdef PIOCTGFPREG
2608
          struct {
2609
            long pr_count;
2610
            tid_t pr_error_thread;
2611
            tfpregset_t thread_1;
2612
          } thread_fpregs;
2613
 
2614
          thread_fpregs.pr_count = 1;
2615
          thread_fpregs.thread_1.tid = pi->tid;
2616
 
2617
          if (pi->tid == 0 &&
2618
              ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2619
            {
2620
              pi->fpregs_valid = 1;
2621
              return &pi->fpregset;     /* got 'em now! */
2622
            }
2623
          else if (pi->tid != 0 &&
2624
                   ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0)
2625
            {
2626
              memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs,
2627
                      sizeof (pi->fpregset));
2628
              pi->fpregs_valid = 1;
2629
              return &pi->fpregset;     /* got 'em now! */
2630
            }
2631
          else
2632
            {
2633
              return NULL;
2634
            }
2635
#else
2636
          if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0)
2637
            {
2638
              pi->fpregs_valid = 1;
2639
              return &pi->fpregset;     /* got 'em now! */
2640
            }
2641
          else
2642
            {
2643
              return NULL;
2644
            }
2645
#endif
2646
        }
2647
    }
2648
#endif
2649
}
2650
 
2651
/*
2652
 * Function: proc_set_gregs
2653
 *
2654
 * Write the general registers back to the process or LWP.
2655
 * Returns non-zero for success, zero for failure.
2656
 */
2657
 
2658
int
2659
proc_set_gregs (procinfo *pi)
2660
{
2661
  gdb_gregset_t *gregs;
2662
  int win;
2663
 
2664
  if ((gregs = proc_get_gregs (pi)) == NULL)
2665
    return 0;    /* get_regs has already warned */
2666
 
2667
  if (pi->ctl_fd == 0 &&
2668
      open_procinfo_files (pi, FD_CTL) == 0)
2669
    {
2670
      return 0;
2671
    }
2672
  else
2673
    {
2674
#ifdef NEW_PROC_API
2675
      struct {
2676
        procfs_ctl_t cmd;
2677
        /* Use char array to avoid alignment issues.  */
2678
        char gregs[sizeof (gdb_gregset_t)];
2679
      } arg;
2680
 
2681
      arg.cmd   = PCSREG;
2682
      memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
2683
      win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2684
#else
2685
      win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0);
2686
#endif
2687
    }
2688
 
2689
  /* Policy: writing the regs invalidates our cache. */
2690
  pi->gregs_valid = 0;
2691
  return win;
2692
}
2693
 
2694
/*
2695
 * Function: proc_set_fpregs
2696
 *
2697
 * Modify the floating point register set of the process or LWP.
2698
 * Returns non-zero for success, zero for failure.
2699
 */
2700
 
2701
int
2702
proc_set_fpregs (procinfo *pi)
2703
{
2704
  gdb_fpregset_t *fpregs;
2705
  int win;
2706
 
2707
  if ((fpregs = proc_get_fpregs (pi)) == NULL)
2708
    return 0;            /* get_fpregs has already warned */
2709
 
2710
  if (pi->ctl_fd == 0 &&
2711
      open_procinfo_files (pi, FD_CTL) == 0)
2712
    {
2713
      return 0;
2714
    }
2715
  else
2716
    {
2717
#ifdef NEW_PROC_API
2718
      struct {
2719
        procfs_ctl_t cmd;
2720
        /* Use char array to avoid alignment issues.  */
2721
        char fpregs[sizeof (gdb_fpregset_t)];
2722
      } arg;
2723
 
2724
      arg.cmd   = PCSFPREG;
2725
      memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
2726
      win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
2727
#else
2728
#ifdef PIOCTSFPREG
2729
      if (pi->tid == 0)
2730
        win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2731
      else
2732
        {
2733
          struct {
2734
            long pr_count;
2735
            tid_t pr_error_thread;
2736
            tfpregset_t thread_1;
2737
          } thread_fpregs;
2738
 
2739
          thread_fpregs.pr_count = 1;
2740
          thread_fpregs.thread_1.tid = pi->tid;
2741
          memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs,
2742
                  sizeof (*fpregs));
2743
          win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0);
2744
        }
2745
#else
2746
      win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0);
2747
#endif  /* osf PIOCTSFPREG */
2748
#endif  /* NEW_PROC_API */
2749
    }
2750
 
2751
  /* Policy: writing the regs invalidates our cache. */
2752
  pi->fpregs_valid = 0;
2753
  return win;
2754
}
2755
 
2756
/*
2757
 * Function: proc_kill
2758
 *
2759
 * Send a signal to the proc or lwp with the semantics of "kill()".
2760
 * Returns non-zero for success,  zero for failure.
2761
 */
2762
 
2763
int
2764
proc_kill (procinfo *pi, int signo)
2765
{
2766
  int win;
2767
 
2768
  /*
2769
   * We might conceivably apply this operation to an LWP, and
2770
   * the LWP's ctl file descriptor might not be open.
2771
   */
2772
 
2773
  if (pi->ctl_fd == 0 &&
2774
      open_procinfo_files (pi, FD_CTL) == 0)
2775
    {
2776
      return 0;
2777
    }
2778
  else
2779
    {
2780
#ifdef NEW_PROC_API
2781
      procfs_ctl_t cmd[2];
2782
 
2783
      cmd[0] = PCKILL;
2784
      cmd[1] = signo;
2785
      win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
2786
#else   /* ioctl method */
2787
      /* FIXME: do I need the Alpha OSF fixups present in
2788
         procfs.c/unconditionally_kill_inferior?  Perhaps only for SIGKILL? */
2789
      win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0);
2790
#endif
2791
  }
2792
 
2793
  return win;
2794
}
2795
 
2796
/*
2797
 * Function: proc_parent_pid
2798
 *
2799
 * Find the pid of the process that started this one.
2800
 * Returns the parent process pid, or zero.
2801
 */
2802
 
2803
int
2804
proc_parent_pid (procinfo *pi)
2805
{
2806
  /*
2807
   * We should never have to apply this operation to any procinfo
2808
   * except the one for the main process.  If that ever changes
2809
   * for any reason, then take out the following clause and
2810
   * replace it with one that makes sure the ctl_fd is open.
2811
   */
2812
 
2813
  if (pi->tid != 0)
2814
    pi = find_procinfo_or_die (pi->pid, 0);
2815
 
2816
  if (!pi->status_valid)
2817
    if (!proc_get_status (pi))
2818
      return 0;
2819
 
2820
  return pi->prstatus.pr_ppid;
2821
}
2822
 
2823
 
2824
/*
2825
 * Function: proc_set_watchpoint
2826
 *
2827
 */
2828
 
2829
int
2830
proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
2831
{
2832
#if !defined (TARGET_HAS_HARDWARE_WATCHPOINTS)  
2833
  return 0;
2834
#else
2835
/* Horrible hack!  Detect Solaris 2.5, because this doesn't work on 2.5 */
2836
#if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out */
2837
  return 0;
2838
#else
2839
  struct {
2840
    procfs_ctl_t cmd;
2841
    char watch[sizeof (prwatch_t)];
2842
  } arg;
2843
  prwatch_t *pwatch;
2844
 
2845
  pwatch            = (prwatch_t *) &arg.watch;
2846
  pwatch->pr_vaddr  = address_to_host_pointer (addr);
2847
  pwatch->pr_size   = len;
2848
  pwatch->pr_wflags = wflags;
2849
#if defined(NEW_PROC_API) && defined (PCWATCH)
2850
  arg.cmd = PCWATCH;
2851
  return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
2852
#else
2853
#if defined (PIOCSWATCH)
2854
  return (ioctl (pi->ctl_fd, PIOCSWATCH, pwatch) >= 0);
2855
#else
2856
  return 0;      /* Fail */
2857
#endif
2858
#endif
2859
#endif
2860
#endif
2861
}
2862
 
2863
/*
2864
 * Function: proc_iterate_over_mappings
2865
 *
2866
 * Given a pointer to a function, call that function once for every
2867
 * mapped address space in the process.  The callback function
2868
 * receives an open file descriptor for the file corresponding to
2869
 * that mapped address space (if there is one), and the base address
2870
 * of the mapped space.  Quit when the callback function returns a
2871
 * nonzero value, or at teh end of the mappings.
2872
 *
2873
 * Returns: the first non-zero return value of the callback function,
2874
 * or zero.
2875
 */
2876
 
2877
/* FIXME: it's probably a waste to cache this FD.
2878
   It doesn't get called that often... and if I open it
2879
   every time, I don't need to lseek it.  */
2880
int
2881
proc_iterate_over_mappings (int (*func) (int, CORE_ADDR))
2882
{
2883
  struct prmap *map;
2884
  procinfo *pi;
2885
#ifndef NEW_PROC_API    /* avoid compiler warning */
2886
  int nmaps = 0;
2887
  int i;
2888
#else
2889
  int map_fd;
2890
  char pathname[MAX_PROC_NAME_SIZE];
2891
#endif
2892
  int funcstat = 0;
2893
  int fd;
2894
 
2895
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
2896
 
2897
#ifdef NEW_PROC_API
2898
  /* Open map fd.  */
2899
  sprintf (pathname, "/proc/%d/map", pi->pid);
2900
  if ((map_fd = open_with_retry (pathname, O_RDONLY)) < 0)
2901
    proc_error (pi, "proc_iterate_over_mappings (open)", __LINE__);
2902
 
2903
  /* Make sure it gets closed again.  */
2904
  make_cleanup_close (map_fd);
2905
 
2906
  /* Allocate space for mapping (lifetime only for this function). */
2907
  map = alloca (sizeof (struct prmap));
2908
 
2909
  /* Now read the mappings from the file,
2910
     open a file descriptor for those that have a name,
2911
     and call the callback function.  */
2912
  while (read (map_fd,
2913
               (void *) map,
2914
               sizeof (struct prmap)) == sizeof (struct prmap))
2915
    {
2916
      char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
2917
 
2918
      if (map->pr_vaddr == 0 && map->pr_size == 0)
2919
        break;          /* sanity */
2920
 
2921
      if (map->pr_mapname[0] == 0)
2922
        {
2923
          fd = -1;      /* no map file */
2924
        }
2925
      else
2926
        {
2927
          sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
2928
          /* Note: caller's responsibility to close this fd!  */
2929
          fd = open_with_retry (name, O_RDONLY);
2930
          /* Note: we don't test the above call for failure;
2931
             we just pass the FD on as given.  Sometimes there is
2932
             no file, so the ioctl may return failure, but that's
2933
             not a problem.  */
2934
        }
2935
 
2936
      /* Stop looping if the callback returns non-zero.  */
2937
      if ((funcstat = (*func) (fd, (CORE_ADDR) map->pr_vaddr)) != 0)
2938
        break;
2939
    }
2940
#else
2941
  /* Get the number of mapping entries.  */
2942
  if (ioctl (pi->ctl_fd, PIOCNMAP, &nmaps) < 0)
2943
    proc_error (pi, "proc_iterate_over_mappings (PIOCNMAP)", __LINE__);
2944
 
2945
  /* Allocate space for mappings (lifetime only this function).  */
2946
  map = (struct prmap *) alloca ((nmaps + 1) * sizeof (struct prmap));
2947
 
2948
  /* Read in all the mappings.  */
2949
  if (ioctl (pi->ctl_fd, PIOCMAP, map) < 0)
2950
    proc_error (pi, "proc_iterate_over_mappings (PIOCMAP)", __LINE__);
2951
 
2952
  /* Now loop through the mappings, open an fd for each, and
2953
     call the callback function.  */
2954
  for (i = 0;
2955
       i < nmaps && map[i].pr_size != 0;
2956
       i++)
2957
    {
2958
      /* Note: caller's responsibility to close this fd!  */
2959
      fd = ioctl (pi->ctl_fd, PIOCOPENM, &map[i].pr_vaddr);
2960
      /* Note: we don't test the above call for failure;
2961
         we just pass the FD on as given.  Sometimes there is
2962
         no file, so the ioctl may return failure, but that's
2963
         not a problem.  */
2964
 
2965
      /* Stop looping if the callback returns non-zero.  */
2966
      funcstat = (*func) (fd, host_pointer_to_address (map[i].pr_vaddr));
2967
      if (funcstat != 0)
2968
        break;
2969
    }
2970
#endif
2971
 
2972
  return funcstat;
2973
}
2974
 
2975
#ifdef TM_I386SOL2_H            /* Is it hokey to use this? */
2976
 
2977
#include <sys/sysi86.h>
2978
 
2979
/*
2980
 * Function: proc_get_LDT_entry
2981
 *
2982
 * Inputs:
2983
 *   procinfo *pi;
2984
 *   int key;
2985
 *
2986
 * The 'key' is actually the value of the lower 16 bits of
2987
 * the GS register for the LWP that we're interested in.
2988
 *
2989
 * Return: matching ssh struct (LDT entry).
2990
 */
2991
 
2992
struct ssd *
2993
proc_get_LDT_entry (procinfo *pi, int key)
2994
{
2995
  static struct ssd *ldt_entry = NULL;
2996
#ifdef NEW_PROC_API
2997
  char pathname[MAX_PROC_NAME_SIZE];
2998
  struct cleanup *old_chain = NULL;
2999
  int  fd;
3000
 
3001
  /* Allocate space for one LDT entry.
3002
     This alloc must persist, because we return a pointer to it.  */
3003
  if (ldt_entry == NULL)
3004
    ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd));
3005
 
3006
  /* Open the file descriptor for the LDT table.  */
3007
  sprintf (pathname, "/proc/%d/ldt", pi->pid);
3008
  if ((fd = open_with_retry (pathname, O_RDONLY)) < 0)
3009
    {
3010
      proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
3011
      return NULL;
3012
    }
3013
  /* Make sure it gets closed again! */
3014
  old_chain = make_cleanup_close (fd);
3015
 
3016
  /* Now 'read' thru the table, find a match and return it.  */
3017
  while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd))
3018
    {
3019
      if (ldt_entry->sel == 0 &&
3020
          ldt_entry->bo  == 0 &&
3021
          ldt_entry->acc1 == 0 &&
3022
          ldt_entry->acc2 == 0)
3023
        break;  /* end of table */
3024
      /* If key matches, return this entry. */
3025
      if (ldt_entry->sel == key)
3026
        return ldt_entry;
3027
    }
3028
  /* Loop ended, match not found. */
3029
  return NULL;
3030
#else
3031
  int nldt, i;
3032
  static int nalloc = 0;
3033
 
3034
  /* Get the number of LDT entries.  */
3035
  if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0)
3036
    {
3037
      proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__);
3038
      return NULL;
3039
    }
3040
 
3041
  /* Allocate space for the number of LDT entries. */
3042
  /* This alloc has to persist, 'cause we return a pointer to it. */
3043
  if (nldt > nalloc)
3044
    {
3045
      ldt_entry = (struct ssd *)
3046
        xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd));
3047
      nalloc = nldt;
3048
    }
3049
 
3050
  /* Read the whole table in one gulp.  */
3051
  if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0)
3052
    {
3053
      proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__);
3054
      return NULL;
3055
    }
3056
 
3057
  /* Search the table and return the (first) entry matching 'key'. */
3058
  for (i = 0; i < nldt; i++)
3059
    if (ldt_entry[i].sel == key)
3060
      return &ldt_entry[i];
3061
 
3062
  /* Loop ended, match not found. */
3063
  return NULL;
3064
#endif
3065
}
3066
 
3067
#endif /* TM_I386SOL2_H */
3068
 
3069
/* =============== END, non-thread part of /proc  "MODULE" =============== */
3070
 
3071
/* =================== Thread "MODULE" =================== */
3072
 
3073
/* NOTE: you'll see more ifdefs and duplication of functions here,
3074
   since there is a different way to do threads on every OS.  */
3075
 
3076
/*
3077
 * Function: proc_get_nthreads
3078
 *
3079
 * Return the number of threads for the process
3080
 */
3081
 
3082
#if defined (PIOCNTHR) && defined (PIOCTLIST)
3083
/*
3084
 * OSF version
3085
 */
3086
int
3087
proc_get_nthreads (procinfo *pi)
3088
{
3089
  int nthreads = 0;
3090
 
3091
  if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0)
3092
    proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__);
3093
 
3094
  return nthreads;
3095
}
3096
 
3097
#else
3098
#if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3099
/*
3100
 * Solaris and Unixware version
3101
 */
3102
int
3103
proc_get_nthreads (procinfo *pi)
3104
{
3105
  if (!pi->status_valid)
3106
    if (!proc_get_status (pi))
3107
      return 0;
3108
 
3109
  /*
3110
   * NEW_PROC_API: only works for the process procinfo,
3111
   * because the LWP procinfos do not get prstatus filled in.
3112
   */
3113
#ifdef NEW_PROC_API  
3114
  if (pi->tid != 0)      /* find the parent process procinfo */
3115
    pi = find_procinfo_or_die (pi->pid, 0);
3116
#endif
3117
  return pi->prstatus.pr_nlwp;
3118
}
3119
 
3120
#else
3121
/*
3122
 * Default version
3123
 */
3124
int
3125
proc_get_nthreads (procinfo *pi)
3126
{
3127
  return 0;
3128
}
3129
#endif
3130
#endif
3131
 
3132
/*
3133
 * Function: proc_get_current_thread (LWP version)
3134
 *
3135
 * Return the ID of the thread that had an event of interest.
3136
 * (ie. the one that hit a breakpoint or other traced event).
3137
 * All other things being equal, this should be the ID of a
3138
 * thread that is currently executing.
3139
 */
3140
 
3141
#if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */
3142
/*
3143
 * Solaris and Unixware version
3144
 */
3145
int
3146
proc_get_current_thread (procinfo *pi)
3147
{
3148
  /*
3149
   * Note: this should be applied to the root procinfo for the process,
3150
   * not to the procinfo for an LWP.  If applied to the procinfo for
3151
   * an LWP, it will simply return that LWP's ID.  In that case,
3152
   * find the parent process procinfo.
3153
   */
3154
 
3155
  if (pi->tid != 0)
3156
    pi = find_procinfo_or_die (pi->pid, 0);
3157
 
3158
  if (!pi->status_valid)
3159
    if (!proc_get_status (pi))
3160
      return 0;
3161
 
3162
#ifdef NEW_PROC_API
3163
  return pi->prstatus.pr_lwp.pr_lwpid;
3164
#else
3165
  return pi->prstatus.pr_who;
3166
#endif
3167
}
3168
 
3169
#else
3170
#if defined (PIOCNTHR) && defined (PIOCTLIST)
3171
/*
3172
 * OSF version
3173
 */
3174
int
3175
proc_get_current_thread (procinfo *pi)
3176
{
3177
#if 0   /* FIXME: not ready for prime time? */
3178
  return pi->prstatus.pr_tid;
3179
#else
3180
  return 0;
3181
#endif
3182
}
3183
 
3184
#else
3185
/*
3186
 * Default version
3187
 */
3188
int
3189
proc_get_current_thread (procinfo *pi)
3190
{
3191
  return 0;
3192
}
3193
 
3194
#endif
3195
#endif
3196
 
3197
/*
3198
 * Function: proc_update_threads
3199
 *
3200
 * Discover the IDs of all the threads within the process, and
3201
 * create a procinfo for each of them (chained to the parent).
3202
 *
3203
 * This unfortunately requires a different method on every OS.
3204
 *
3205
 * Return: non-zero for success, zero for failure.
3206
 */
3207
 
3208
int
3209
proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
3210
{
3211
  if (thread && parent) /* sanity */
3212
    {
3213
      thread->status_valid = 0;
3214
      if (!proc_get_status (thread))
3215
        destroy_one_procinfo (&parent->thread_list, thread);
3216
    }
3217
  return 0;      /* keep iterating */
3218
}
3219
 
3220
#if defined (PIOCLSTATUS)
3221
/*
3222
 * Solaris 2.5 (ioctl) version
3223
 */
3224
int
3225
proc_update_threads (procinfo *pi)
3226
{
3227
  gdb_prstatus_t *prstatus;
3228
  struct cleanup *old_chain = NULL;
3229
  procinfo *thread;
3230
  int nlwp, i;
3231
 
3232
  /*
3233
   * We should never have to apply this operation to any procinfo
3234
   * except the one for the main process.  If that ever changes
3235
   * for any reason, then take out the following clause and
3236
   * replace it with one that makes sure the ctl_fd is open.
3237
   */
3238
 
3239
  if (pi->tid != 0)
3240
    pi = find_procinfo_or_die (pi->pid, 0);
3241
 
3242
  proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3243
 
3244
  if ((nlwp = proc_get_nthreads (pi)) <= 1)
3245
    return 1;   /* Process is not multi-threaded; nothing to do.  */
3246
 
3247
  prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1));
3248
 
3249
  old_chain = make_cleanup (xfree, prstatus);
3250
  if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0)
3251
    proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__);
3252
 
3253
  /* Skip element zero, which represents the process as a whole. */
3254
  for (i = 1; i < nlwp + 1; i++)
3255
    {
3256
      if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL)
3257
        proc_error (pi, "update_threads, create_procinfo", __LINE__);
3258
 
3259
      memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus));
3260
      thread->status_valid = 1;
3261
    }
3262
  pi->threads_valid = 1;
3263
  do_cleanups (old_chain);
3264
  return 1;
3265
}
3266
#else
3267
#ifdef NEW_PROC_API
3268
/*
3269
 * Unixware and Solaris 6 (and later) version
3270
 */
3271
static void
3272
do_closedir_cleanup (void *dir)
3273
{
3274
  closedir (dir);
3275
}
3276
 
3277
int
3278
proc_update_threads (procinfo *pi)
3279
{
3280
  char pathname[MAX_PROC_NAME_SIZE + 16];
3281
  struct dirent *direntry;
3282
  struct cleanup *old_chain = NULL;
3283
  procinfo *thread;
3284
  DIR *dirp;
3285
  int lwpid;
3286
 
3287
  /*
3288
   * We should never have to apply this operation to any procinfo
3289
   * except the one for the main process.  If that ever changes
3290
   * for any reason, then take out the following clause and
3291
   * replace it with one that makes sure the ctl_fd is open.
3292
   */
3293
 
3294
  if (pi->tid != 0)
3295
    pi = find_procinfo_or_die (pi->pid, 0);
3296
 
3297
  proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3298
 
3299
  /*
3300
   * Unixware
3301
   *
3302
   * Note: this brute-force method is the only way I know of
3303
   * to accomplish this task on Unixware.  This method will
3304
   * also work on Solaris 2.6 and 2.7.  There is a much simpler
3305
   * and more elegant way to do this on Solaris, but the margins
3306
   * of this manuscript are too small to write it here...  ;-)
3307
   */
3308
 
3309
  strcpy (pathname, pi->pathname);
3310
  strcat (pathname, "/lwp");
3311
  if ((dirp = opendir (pathname)) == NULL)
3312
    proc_error (pi, "update_threads, opendir", __LINE__);
3313
 
3314
  old_chain = make_cleanup (do_closedir_cleanup, dirp);
3315
  while ((direntry = readdir (dirp)) != NULL)
3316
    if (direntry->d_name[0] != '.')              /* skip '.' and '..' */
3317
      {
3318
        lwpid = atoi (&direntry->d_name[0]);
3319
        if ((thread = create_procinfo (pi->pid, lwpid)) == NULL)
3320
          proc_error (pi, "update_threads, create_procinfo", __LINE__);
3321
      }
3322
  pi->threads_valid = 1;
3323
  do_cleanups (old_chain);
3324
  return 1;
3325
}
3326
#else
3327
#ifdef PIOCTLIST
3328
/*
3329
 * OSF version
3330
 */
3331
int
3332
proc_update_threads (procinfo *pi)
3333
{
3334
  int nthreads, i;
3335
  tid_t *threads;
3336
 
3337
  /*
3338
   * We should never have to apply this operation to any procinfo
3339
   * except the one for the main process.  If that ever changes
3340
   * for any reason, then take out the following clause and
3341
   * replace it with one that makes sure the ctl_fd is open.
3342
   */
3343
 
3344
  if (pi->tid != 0)
3345
    pi = find_procinfo_or_die (pi->pid, 0);
3346
 
3347
  proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL);
3348
 
3349
  nthreads = proc_get_nthreads (pi);
3350
  if (nthreads < 2)
3351
    return 0;            /* nothing to do for 1 or fewer threads */
3352
 
3353
  threads = xmalloc (nthreads * sizeof (tid_t));
3354
 
3355
  if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0)
3356
    proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__);
3357
 
3358
  for (i = 0; i < nthreads; i++)
3359
    {
3360
      if (!find_procinfo (pi->pid, threads[i]))
3361
        if (!create_procinfo  (pi->pid, threads[i]))
3362
          proc_error (pi, "update_threads, create_procinfo", __LINE__);
3363
    }
3364
  pi->threads_valid = 1;
3365
  return 1;
3366
}
3367
#else
3368
/*
3369
 * Default version
3370
 */
3371
int
3372
proc_update_threads (procinfo *pi)
3373
{
3374
  return 0;
3375
}
3376
#endif  /* OSF PIOCTLIST */
3377
#endif  /* NEW_PROC_API   */
3378
#endif  /* SOL 2.5 PIOCLSTATUS */
3379
 
3380
/*
3381
 * Function: proc_iterate_over_threads
3382
 *
3383
 * Description:
3384
 *   Given a pointer to a function, call that function once
3385
 *   for each lwp in the procinfo list, until the function
3386
 *   returns non-zero, in which event return the value
3387
 *   returned by the function.
3388
 *
3389
 * Note: this function does NOT call update_threads.
3390
 * If you want to discover new threads first, you must
3391
 * call that function explicitly.  This function just makes
3392
 * a quick pass over the currently-known procinfos.
3393
 *
3394
 * Arguments:
3395
 *   pi         - parent process procinfo
3396
 *   func       - per-thread function
3397
 *   ptr        - opaque parameter for function.
3398
 *
3399
 * Return:
3400
 *   First non-zero return value from the callee, or zero.
3401
 */
3402
 
3403
int
3404
proc_iterate_over_threads (procinfo *pi,
3405
                           int (*func) (procinfo *, procinfo *, void *),
3406
                           void *ptr)
3407
{
3408
  procinfo *thread, *next;
3409
  int retval = 0;
3410
 
3411
  /*
3412
   * We should never have to apply this operation to any procinfo
3413
   * except the one for the main process.  If that ever changes
3414
   * for any reason, then take out the following clause and
3415
   * replace it with one that makes sure the ctl_fd is open.
3416
   */
3417
 
3418
  if (pi->tid != 0)
3419
    pi = find_procinfo_or_die (pi->pid, 0);
3420
 
3421
  for (thread = pi->thread_list; thread != NULL; thread = next)
3422
    {
3423
      next = thread->next;      /* in case thread is destroyed */
3424
      if ((retval = (*func) (pi, thread, ptr)) != 0)
3425
        break;
3426
    }
3427
 
3428
  return retval;
3429
}
3430
 
3431
/* =================== END, Thread "MODULE" =================== */
3432
 
3433
/* =================== END, /proc  "MODULE" =================== */
3434
 
3435
/* ===================  GDB  "MODULE" =================== */
3436
 
3437
/*
3438
 * Here are all of the gdb target vector functions and their friends.
3439
 */
3440
 
3441
static ptid_t do_attach (ptid_t ptid);
3442
static void do_detach (int signo);
3443
static int register_gdb_signals (procinfo *, gdb_sigset_t *);
3444
 
3445
/*
3446
 * Function: procfs_debug_inferior
3447
 *
3448
 * Sets up the inferior to be debugged.
3449
 * Registers to trace signals, hardware faults, and syscalls.
3450
 * Note: does not set RLC flag: caller may want to customize that.
3451
 *
3452
 * Returns: zero for success (note! unlike most functions in this module)
3453
 *   On failure, returns the LINE NUMBER where it failed!
3454
 */
3455
 
3456
static int
3457
procfs_debug_inferior (procinfo *pi)
3458
{
3459
  fltset_t traced_faults;
3460
  gdb_sigset_t traced_signals;
3461
  sysset_t *traced_syscall_entries;
3462
  sysset_t *traced_syscall_exits;
3463
  int status;
3464
 
3465
#ifdef PROCFS_DONT_TRACE_FAULTS
3466
  /* On some systems (OSF), we don't trace hardware faults.
3467
     Apparently it's enough that we catch them as signals.
3468
     Wonder why we don't just do that in general? */
3469
  premptyset (&traced_faults);          /* don't trace faults. */
3470
#else
3471
  /* Register to trace hardware faults in the child. */
3472
  prfillset (&traced_faults);           /* trace all faults... */
3473
  prdelset  (&traced_faults, FLTPAGE);  /* except page fault.  */
3474
#endif
3475
  if (!proc_set_traced_faults  (pi, &traced_faults))
3476
    return __LINE__;
3477
 
3478
  /* Register to trace selected signals in the child. */
3479
  premptyset (&traced_signals);
3480
  if (!register_gdb_signals (pi, &traced_signals))
3481
    return __LINE__;
3482
 
3483
 
3484
  /* Register to trace the 'exit' system call (on entry).  */
3485
  traced_syscall_entries = sysset_t_alloc (pi);
3486
  gdb_premptysysset (traced_syscall_entries);
3487
#ifdef SYS_exit
3488
  gdb_praddsysset (traced_syscall_entries, SYS_exit);
3489
#endif
3490
#ifdef SYS_lwpexit
3491
  gdb_praddsysset (traced_syscall_entries, SYS_lwpexit);        /* And _lwp_exit... */
3492
#endif
3493
#ifdef SYS_lwp_exit
3494
  gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit);
3495
#endif
3496
#ifdef DYNAMIC_SYSCALLS
3497
  {
3498
    int callnum = find_syscall (pi, "_exit");
3499
    if (callnum >= 0)
3500
      gdb_praddsysset (traced_syscall_entries, callnum);
3501
  }
3502
#endif
3503
 
3504
  status = proc_set_traced_sysentry (pi, traced_syscall_entries);
3505
  xfree (traced_syscall_entries);
3506
  if (!status)
3507
    return __LINE__;
3508
 
3509
#ifdef PRFS_STOPEXEC    /* defined on OSF */
3510
  /* OSF method for tracing exec syscalls.  Quoting:
3511
     Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
3512
     exits from exec system calls because of the user level loader.  */
3513
  /* FIXME: make nice and maybe move into an access function. */
3514
  {
3515
    int prfs_flags;
3516
 
3517
    if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
3518
      return __LINE__;
3519
 
3520
    prfs_flags |= PRFS_STOPEXEC;
3521
 
3522
    if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
3523
      return __LINE__;
3524
  }
3525
#else /* not PRFS_STOPEXEC */
3526
  /* Everyone else's (except OSF) method for tracing exec syscalls */
3527
  /* GW: Rationale...
3528
     Not all systems with /proc have all the exec* syscalls with the same
3529
     names.  On the SGI, for example, there is no SYS_exec, but there
3530
     *is* a SYS_execv.  So, we try to account for that. */
3531
 
3532
  traced_syscall_exits = sysset_t_alloc (pi);
3533
  gdb_premptysysset (traced_syscall_exits);
3534
#ifdef SYS_exec
3535
  gdb_praddsysset (traced_syscall_exits, SYS_exec);
3536
#endif
3537
#ifdef SYS_execve
3538
  gdb_praddsysset (traced_syscall_exits, SYS_execve);
3539
#endif
3540
#ifdef SYS_execv
3541
  gdb_praddsysset (traced_syscall_exits, SYS_execv);
3542
#endif
3543
 
3544
#ifdef SYS_lwpcreate
3545
  gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate);
3546
  gdb_praddsysset (traced_syscall_exits, SYS_lwpexit);
3547
#endif
3548
 
3549
#ifdef SYS_lwp_create   /* FIXME: once only, please */
3550
  gdb_praddsysset (traced_syscall_exits, SYS_lwp_create);
3551
  gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit);
3552
#endif
3553
 
3554
#ifdef DYNAMIC_SYSCALLS
3555
  {
3556
    int callnum = find_syscall (pi, "execve");
3557
    if (callnum >= 0)
3558
      gdb_praddsysset (traced_syscall_exits, callnum);
3559
    callnum = find_syscall (pi, "ra_execve");
3560
    if (callnum >= 0)
3561
      gdb_praddsysset (traced_syscall_exits, callnum);
3562
  }
3563
#endif
3564
 
3565
  status = proc_set_traced_sysexit (pi, traced_syscall_exits);
3566
  xfree (traced_syscall_exits);
3567
  if (!status)
3568
    return __LINE__;
3569
 
3570
#endif /* PRFS_STOPEXEC */
3571
  return 0;
3572
}
3573
 
3574
static void
3575
procfs_attach (char *args, int from_tty)
3576
{
3577
  char *exec_file;
3578
  int   pid;
3579
 
3580
  if (!args)
3581
    error_no_arg ("process-id to attach");
3582
 
3583
  pid = atoi (args);
3584
  if (pid == getpid ())
3585
    error ("Attaching GDB to itself is not a good idea...");
3586
 
3587
  if (from_tty)
3588
    {
3589
      exec_file = get_exec_file (0);
3590
 
3591
      if (exec_file)
3592
        printf_filtered ("Attaching to program `%s', %s\n",
3593
                         exec_file, target_pid_to_str (pid_to_ptid (pid)));
3594
      else
3595
        printf_filtered ("Attaching to %s\n",
3596
                         target_pid_to_str (pid_to_ptid (pid)));
3597
 
3598
      fflush (stdout);
3599
    }
3600
  inferior_ptid = do_attach (pid_to_ptid (pid));
3601
  push_target (&procfs_ops);
3602
}
3603
 
3604
static void
3605
procfs_detach (char *args, int from_tty)
3606
{
3607
  char *exec_file;
3608
  int   signo = 0;
3609
 
3610
  if (from_tty)
3611
    {
3612
      exec_file = get_exec_file (0);
3613
      if (exec_file == 0)
3614
        exec_file = "";
3615
      printf_filtered ("Detaching from program: %s %s\n",
3616
              exec_file, target_pid_to_str (inferior_ptid));
3617
      fflush (stdout);
3618
    }
3619
  if (args)
3620
    signo = atoi (args);
3621
 
3622
  do_detach (signo);
3623
  inferior_ptid = null_ptid;
3624
  unpush_target (&procfs_ops);          /* Pop out of handling an inferior */
3625
}
3626
 
3627
static ptid_t
3628
do_attach (ptid_t ptid)
3629
{
3630
  procinfo *pi;
3631
  int fail;
3632
 
3633
  if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
3634
    perror ("procfs: out of memory in 'attach'");
3635
 
3636
  if (!open_procinfo_files (pi, FD_CTL))
3637
    {
3638
      fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3639
      sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
3640
               PIDGET (ptid));
3641
      dead_procinfo (pi, errmsg, NOKILL);
3642
    }
3643
 
3644
  /* Stop the process (if it isn't already stopped).  */
3645
  if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3646
    {
3647
      pi->was_stopped = 1;
3648
      proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
3649
    }
3650
  else
3651
    {
3652
      pi->was_stopped = 0;
3653
      /* Set the process to run again when we close it.  */
3654
      if (!proc_set_run_on_last_close (pi))
3655
        dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3656
 
3657
      /* Now stop the process. */
3658
      if (!proc_stop_process (pi))
3659
        dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3660
      pi->ignore_next_sigstop = 1;
3661
    }
3662
  /* Save some of the /proc state to be restored if we detach.  */
3663
  if (!proc_get_traced_faults   (pi, &pi->saved_fltset))
3664
    dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3665
  if (!proc_get_traced_signals  (pi, &pi->saved_sigset))
3666
    dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
3667
  if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
3668
    dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3669
                   NOKILL);
3670
  if (!proc_get_traced_sysexit  (pi, pi->saved_exitset))
3671
    dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3672
                   NOKILL);
3673
  if (!proc_get_held_signals    (pi, &pi->saved_sighold))
3674
    dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3675
 
3676
  if ((fail = procfs_debug_inferior (pi)) != 0)
3677
    dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3678
 
3679
  /* Let GDB know that the inferior was attached.  */
3680
  attach_flag = 1;
3681
  return MERGEPID (pi->pid, proc_get_current_thread (pi));
3682
}
3683
 
3684
static void
3685
do_detach (int signo)
3686
{
3687
  procinfo *pi;
3688
 
3689
  /* Find procinfo for the main process */
3690
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
3691
  if (signo)
3692
    if (!proc_set_current_signal (pi, signo))
3693
      proc_warn (pi, "do_detach, set_current_signal", __LINE__);
3694
 
3695
  if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3696
    proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
3697
 
3698
  if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3699
    proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3700
 
3701
  if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
3702
    proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3703
 
3704
  if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
3705
    proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3706
 
3707
  if (!proc_set_held_signals (pi, &pi->saved_sighold))
3708
    proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3709
 
3710
  if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3711
    if (signo || !(pi->was_stopped) ||
3712
        query ("Was stopped when attached, make it runnable again? "))
3713
      {
3714
        /* Clear any pending signal.  */
3715
        if (!proc_clear_current_fault (pi))
3716
          proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3717
 
3718
        if (!proc_set_run_on_last_close (pi))
3719
          proc_warn (pi, "do_detach, set_rlc", __LINE__);
3720
      }
3721
 
3722
  attach_flag = 0;
3723
  destroy_procinfo (pi);
3724
}
3725
 
3726
/*
3727
 * fetch_registers
3728
 *
3729
 * Since the /proc interface cannot give us individual registers,
3730
 * we pay no attention to the (regno) argument, and just fetch them all.
3731
 * This results in the possibility that we will do unnecessarily many
3732
 * fetches, since we may be called repeatedly for individual registers.
3733
 * So we cache the results, and mark the cache invalid when the process
3734
 * is resumed.
3735
 */
3736
 
3737
static void
3738
procfs_fetch_registers (int regno)
3739
{
3740
  gdb_fpregset_t *fpregs;
3741
  gdb_gregset_t  *gregs;
3742
  procinfo       *pi;
3743
  int            pid;
3744
  int            tid;
3745
 
3746
  pid = PIDGET (inferior_ptid);
3747
  tid = TIDGET (inferior_ptid);
3748
 
3749
  /* First look up procinfo for the main process. */
3750
  pi  = find_procinfo_or_die (pid, 0);
3751
 
3752
  /* If the event thread is not the same as GDB's requested thread
3753
     (ie. inferior_ptid), then look up procinfo for the requested
3754
     thread.  */
3755
  if ((tid != 0) &&
3756
      (tid != proc_get_current_thread (pi)))
3757
    pi = find_procinfo_or_die (pid, tid);
3758
 
3759
  if (pi == NULL)
3760
    error ("procfs: fetch_registers failed to find procinfo for %s",
3761
           target_pid_to_str (inferior_ptid));
3762
 
3763
  if ((gregs = proc_get_gregs (pi)) == NULL)
3764
    proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3765
 
3766
  supply_gregset (gregs);
3767
 
3768
  if (FP0_REGNUM >= 0)   /* need floating point? */
3769
    {
3770
      if ((regno >= 0 && regno < FP0_REGNUM) ||
3771
          regno == PC_REGNUM  ||
3772
          (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3773
          regno == FP_REGNUM  ||
3774
          regno == SP_REGNUM)
3775
        return;                 /* not a floating point register */
3776
 
3777
      if ((fpregs = proc_get_fpregs (pi)) == NULL)
3778
        proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
3779
 
3780
      supply_fpregset (fpregs);
3781
    }
3782
}
3783
 
3784
/* Get ready to modify the registers array.  On machines which store
3785
   individual registers, this doesn't need to do anything.  On
3786
   machines which store all the registers in one fell swoop, such as
3787
   /proc, this makes sure that registers contains all the registers
3788
   from the program being debugged.  */
3789
 
3790
static void
3791
procfs_prepare_to_store (void)
3792
{
3793
#ifdef CHILD_PREPARE_TO_STORE
3794
  CHILD_PREPARE_TO_STORE ();
3795
#endif
3796
}
3797
 
3798
/*
3799
 * store_registers
3800
 *
3801
 * Since the /proc interface will not read individual registers,
3802
 * we will cache these requests until the process is resumed, and
3803
 * only then write them back to the inferior process.
3804
 *
3805
 * FIXME: is that a really bad idea?  Have to think about cases
3806
 * where writing one register might affect the value of others, etc.
3807
 */
3808
 
3809
static void
3810
procfs_store_registers (int regno)
3811
{
3812
  gdb_fpregset_t *fpregs;
3813
  gdb_gregset_t  *gregs;
3814
  procinfo       *pi;
3815
  int            pid;
3816
  int            tid;
3817
 
3818
  pid = PIDGET (inferior_ptid);
3819
  tid = TIDGET (inferior_ptid);
3820
 
3821
  /* First find procinfo for main process */
3822
  pi  = find_procinfo_or_die (pid, 0);
3823
 
3824
  /* If current lwp for process is not the same as requested thread
3825
     (ie. inferior_ptid), then find procinfo for the requested thread.  */
3826
 
3827
  if ((tid != 0) &&
3828
      (tid != proc_get_current_thread (pi)))
3829
    pi = find_procinfo_or_die (pid, tid);
3830
 
3831
  if (pi == NULL)
3832
    error ("procfs: store_registers: failed to find procinfo for %s",
3833
           target_pid_to_str (inferior_ptid));
3834
 
3835
  if ((gregs = proc_get_gregs (pi)) == NULL)
3836
    proc_error (pi, "store_registers, get_gregs", __LINE__);
3837
 
3838
  fill_gregset (gregs, regno);
3839
  if (!proc_set_gregs (pi))
3840
    proc_error (pi, "store_registers, set_gregs", __LINE__);
3841
 
3842
  if (FP0_REGNUM >= 0)           /* need floating point? */
3843
    {
3844
      if ((regno >= 0 && regno < FP0_REGNUM) ||
3845
          regno == PC_REGNUM  ||
3846
          (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3847
          regno == FP_REGNUM  ||
3848
          regno == SP_REGNUM)
3849
        return;                 /* not a floating point register */
3850
 
3851
      if ((fpregs = proc_get_fpregs (pi)) == NULL)
3852
        proc_error (pi, "store_registers, get_fpregs", __LINE__);
3853
 
3854
      fill_fpregset (fpregs, regno);
3855
      if (!proc_set_fpregs (pi))
3856
        proc_error (pi, "store_registers, set_fpregs", __LINE__);
3857
    }
3858
}
3859
 
3860
static int
3861
syscall_is_lwp_exit (procinfo *pi, int scall)
3862
{
3863
 
3864
#ifdef SYS_lwp_exit
3865
  if (scall == SYS_lwp_exit)
3866
    return 1;
3867
#endif
3868
#ifdef SYS_lwpexit
3869
  if (scall == SYS_lwpexit)
3870
    return 1;
3871
#endif
3872
  return 0;
3873
}
3874
 
3875
static int
3876
syscall_is_exit (procinfo *pi, int scall)
3877
{
3878
#ifdef SYS_exit
3879
  if (scall == SYS_exit)
3880
    return 1;
3881
#endif
3882
#ifdef DYNAMIC_SYSCALLS
3883
  if (find_syscall (pi, "_exit") == scall)
3884
    return 1;
3885
#endif
3886
  return 0;
3887
}
3888
 
3889
static int
3890
syscall_is_exec (procinfo *pi, int scall)
3891
{
3892
#ifdef SYS_exec
3893
  if (scall == SYS_exec)
3894
    return 1;
3895
#endif
3896
#ifdef SYS_execv
3897
  if (scall == SYS_execv)
3898
    return 1;
3899
#endif
3900
#ifdef SYS_execve
3901
  if (scall == SYS_execve)
3902
    return 1;
3903
#endif
3904
#ifdef DYNAMIC_SYSCALLS
3905
  if (find_syscall (pi, "_execve"))
3906
    return 1;
3907
  if (find_syscall (pi, "ra_execve"))
3908
    return 1;
3909
#endif
3910
  return 0;
3911
}
3912
 
3913
static int
3914
syscall_is_lwp_create (procinfo *pi, int scall)
3915
{
3916
#ifdef SYS_lwp_create
3917
  if (scall == SYS_lwp_create)
3918
    return 1;
3919
#endif
3920
#ifdef SYS_lwpcreate
3921
  if (scall == SYS_lwpcreate)
3922
    return 1;
3923
#endif
3924
  return 0;
3925
}
3926
 
3927
/*
3928
 * Function: target_wait
3929
 *
3930
 * Retrieve the next stop event from the child process.
3931
 * If child has not stopped yet, wait for it to stop.
3932
 * Translate /proc eventcodes (or possibly wait eventcodes)
3933
 * into gdb internal event codes.
3934
 *
3935
 * Return: id of process (and possibly thread) that incurred the event.
3936
 *         event codes are returned thru a pointer parameter.
3937
 */
3938
 
3939
static ptid_t
3940
procfs_wait (ptid_t ptid, struct target_waitstatus *status)
3941
{
3942
  /* First cut: loosely based on original version 2.1 */
3943
  procinfo *pi;
3944
  int       wstat;
3945
  int       temp_tid;
3946
  ptid_t    retval, temp_ptid;
3947
  int       why, what, flags;
3948
  int       retry = 0;
3949
 
3950
wait_again:
3951
 
3952
  retry++;
3953
  wstat    = 0;
3954
  retval   = pid_to_ptid (-1);
3955
 
3956
  /* Find procinfo for main process */
3957
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
3958
  if (pi)
3959
    {
3960
      /* We must assume that the status is stale now... */
3961
      pi->status_valid = 0;
3962
      pi->gregs_valid  = 0;
3963
      pi->fpregs_valid = 0;
3964
 
3965
#if 0   /* just try this out... */
3966
      flags = proc_flags (pi);
3967
      why   = proc_why (pi);
3968
      if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3969
        pi->status_valid = 0;    /* re-read again, IMMEDIATELY... */
3970
#endif
3971
      /* If child is not stopped, wait for it to stop.  */
3972
      if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3973
          !proc_wait_for_stop (pi))
3974
        {
3975
          /* wait_for_stop failed: has the child terminated? */
3976
          if (errno == ENOENT)
3977
            {
3978
              int wait_retval;
3979
 
3980
              /* /proc file not found; presumably child has terminated. */
3981
              wait_retval = wait (&wstat); /* "wait" for the child's exit  */
3982
 
3983
              if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
3984
                error ("procfs: couldn't stop process %d: wait returned %d\n",
3985
                       PIDGET (inferior_ptid), wait_retval);
3986
              /* FIXME: might I not just use waitpid?
3987
                 Or try find_procinfo to see if I know about this child? */
3988
              retval = pid_to_ptid (wait_retval);
3989
            }
3990
          else if (errno == EINTR)
3991
            goto wait_again;
3992
          else
3993
            {
3994
              /* Unknown error from wait_for_stop. */
3995
              proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
3996
            }
3997
        }
3998
      else
3999
        {
4000
          /* This long block is reached if either:
4001
             a) the child was already stopped, or
4002
             b) we successfully waited for the child with wait_for_stop.
4003
             This block will analyze the /proc status, and translate it
4004
             into a waitstatus for GDB.
4005
 
4006
             If we actually had to call wait because the /proc file
4007
             is gone (child terminated), then we skip this block,
4008
             because we already have a waitstatus.  */
4009
 
4010
          flags = proc_flags (pi);
4011
          why   = proc_why (pi);
4012
          what  = proc_what (pi);
4013
 
4014
          if (flags & (PR_STOPPED | PR_ISTOP))
4015
            {
4016
#ifdef PR_ASYNC
4017
              /* If it's running async (for single_thread control),
4018
                 set it back to normal again.  */
4019
              if (flags & PR_ASYNC)
4020
                if (!proc_unset_async (pi))
4021
                  proc_error (pi, "target_wait, unset_async", __LINE__);
4022
#endif
4023
 
4024
              if (info_verbose)
4025
                proc_prettyprint_why (why, what, 1);
4026
 
4027
              /* The 'pid' we will return to GDB is composed of
4028
                 the process ID plus the lwp ID.  */
4029
              retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
4030
 
4031
              switch (why) {
4032
              case PR_SIGNALLED:
4033
                wstat = (what << 8) | 0177;
4034
                break;
4035
              case PR_SYSENTRY:
4036
                if (syscall_is_lwp_exit (pi, what))
4037
                  {
4038
                    printf_filtered ("[%s exited]\n",
4039
                                     target_pid_to_str (retval));
4040
                    delete_thread (retval);
4041
                    status->kind = TARGET_WAITKIND_SPURIOUS;
4042
                    return retval;
4043
                  }
4044
                else if (syscall_is_exit (pi, what))
4045
                  {
4046
                    /* Handle SYS_exit call only */
4047
                    /* Stopped at entry to SYS_exit.
4048
                       Make it runnable, resume it, then use
4049
                       the wait system call to get its exit code.
4050
                       Proc_run_process always clears the current
4051
                       fault and signal.
4052
                       Then return its exit status.  */
4053
                    pi->status_valid = 0;
4054
                    wstat = 0;
4055
                    /* FIXME: what we should do is return
4056
                       TARGET_WAITKIND_SPURIOUS.  */
4057
                    if (!proc_run_process (pi, 0, 0))
4058
                      proc_error (pi, "target_wait, run_process", __LINE__);
4059
                    if (attach_flag)
4060
                      {
4061
                        /* Don't call wait: simulate waiting for exit,
4062
                           return a "success" exit code.  Bogus: what if
4063
                           it returns something else?  */
4064
                        wstat = 0;
4065
                        retval = inferior_ptid;  /* ? ? ? */
4066
                      }
4067
                    else
4068
                      {
4069
                        int temp = wait (&wstat);
4070
 
4071
                        /* FIXME: shouldn't I make sure I get the right
4072
                           event from the right process?  If (for
4073
                           instance) I have killed an earlier inferior
4074
                           process but failed to clean up after it
4075
                           somehow, I could get its termination event
4076
                           here.  */
4077
 
4078
                        /* If wait returns -1, that's what we return to GDB. */
4079
                        if (temp < 0)
4080
                          retval = pid_to_ptid (temp);
4081
                      }
4082
                  }
4083
                else
4084
                  {
4085
                    printf_filtered ("procfs: trapped on entry to ");
4086
                    proc_prettyprint_syscall (proc_what (pi), 0);
4087
                    printf_filtered ("\n");
4088
#ifndef PIOCSSPCACT
4089
                    {
4090
                      long i, nsysargs, *sysargs;
4091
 
4092
                      if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4093
                          (sysargs  = proc_sysargs (pi)) != NULL)
4094
                        {
4095
                          printf_filtered ("%ld syscall arguments:\n", nsysargs);
4096
                          for (i = 0; i < nsysargs; i++)
4097
                            printf_filtered ("#%ld: 0x%08lx\n",
4098
                                             i, sysargs[i]);
4099
                        }
4100
 
4101
                    }
4102
#endif
4103
                    if (status)
4104
                      {
4105
                        /* How to exit gracefully, returning "unknown event" */
4106
                        status->kind = TARGET_WAITKIND_SPURIOUS;
4107
                        return inferior_ptid;
4108
                      }
4109
                    else
4110
                      {
4111
                        /* How to keep going without returning to wfi: */
4112
                        target_resume (ptid, 0, TARGET_SIGNAL_0);
4113
                        goto wait_again;
4114
                      }
4115
                  }
4116
                break;
4117
              case PR_SYSEXIT:
4118
                if (syscall_is_exec (pi, what))
4119
                  {
4120
                    /* Hopefully this is our own "fork-child" execing
4121
                       the real child.  Hoax this event into a trap, and
4122
                       GDB will see the child about to execute its start
4123
                       address. */
4124
                    wstat = (SIGTRAP << 8) | 0177;
4125
                  }
4126
                else if (syscall_is_lwp_create (pi, what))
4127
                  {
4128
                    /*
4129
                     * This syscall is somewhat like fork/exec.
4130
                     * We will get the event twice: once for the parent LWP,
4131
                     * and once for the child.  We should already know about
4132
                     * the parent LWP, but the child will be new to us.  So,
4133
                     * whenever we get this event, if it represents a new
4134
                     * thread, simply add the thread to the list.
4135
                     */
4136
 
4137
                    /* If not in procinfo list, add it.  */
4138
                    temp_tid = proc_get_current_thread (pi);
4139
                    if (!find_procinfo (pi->pid, temp_tid))
4140
                      create_procinfo  (pi->pid, temp_tid);
4141
 
4142
                    temp_ptid = MERGEPID (pi->pid, temp_tid);
4143
                    /* If not in GDB's thread list, add it.  */
4144
                    if (!in_thread_list (temp_ptid))
4145
                      {
4146
                        printf_filtered ("[New %s]\n",
4147
                                         target_pid_to_str (temp_ptid));
4148
                        add_thread (temp_ptid);
4149
                      }
4150
                    /* Return to WFI, but tell it to immediately resume. */
4151
                    status->kind = TARGET_WAITKIND_SPURIOUS;
4152
                    return inferior_ptid;
4153
                  }
4154
                else if (syscall_is_lwp_exit (pi, what))
4155
                  {
4156
                    printf_filtered ("[%s exited]\n",
4157
                                     target_pid_to_str (retval));
4158
                    delete_thread (retval);
4159
                    status->kind = TARGET_WAITKIND_SPURIOUS;
4160
                    return retval;
4161
                  }
4162
                else if (0)
4163
                  {
4164
                    /* FIXME:  Do we need to handle SYS_sproc,
4165
                       SYS_fork, or SYS_vfork here?  The old procfs
4166
                       seemed to use this event to handle threads on
4167
                       older (non-LWP) systems, where I'm assuming
4168
                       that threads were actually separate processes.
4169
                       Irix, maybe?  Anyway, low priority for now.  */
4170
                  }
4171
                else
4172
                  {
4173
                    printf_filtered ("procfs: trapped on exit from ");
4174
                    proc_prettyprint_syscall (proc_what (pi), 0);
4175
                    printf_filtered ("\n");
4176
#ifndef PIOCSSPCACT
4177
                    {
4178
                      long i, nsysargs, *sysargs;
4179
 
4180
                      if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4181
                          (sysargs  = proc_sysargs (pi)) != NULL)
4182
                        {
4183
                          printf_filtered ("%ld syscall arguments:\n", nsysargs);
4184
                          for (i = 0; i < nsysargs; i++)
4185
                            printf_filtered ("#%ld: 0x%08lx\n",
4186
                                             i, sysargs[i]);
4187
                        }
4188
                    }
4189
#endif
4190
                    status->kind = TARGET_WAITKIND_SPURIOUS;
4191
                    return inferior_ptid;
4192
                  }
4193
                break;
4194
              case PR_REQUESTED:
4195
#if 0   /* FIXME */
4196
                wstat = (SIGSTOP << 8) | 0177;
4197
                break;
4198
#else
4199
                if (retry < 5)
4200
                  {
4201
                    printf_filtered ("Retry #%d:\n", retry);
4202
                    pi->status_valid = 0;
4203
                    goto wait_again;
4204
                  }
4205
                else
4206
                  {
4207
                    /* If not in procinfo list, add it.  */
4208
                    temp_tid = proc_get_current_thread (pi);
4209
                    if (!find_procinfo (pi->pid, temp_tid))
4210
                      create_procinfo  (pi->pid, temp_tid);
4211
 
4212
                    /* If not in GDB's thread list, add it.  */
4213
                    temp_ptid = MERGEPID (pi->pid, temp_tid);
4214
                    if (!in_thread_list (temp_ptid))
4215
                      {
4216
                        printf_filtered ("[New %s]\n",
4217
                                         target_pid_to_str (temp_ptid));
4218
                        add_thread (temp_ptid);
4219
                      }
4220
 
4221
                    status->kind = TARGET_WAITKIND_STOPPED;
4222
                    status->value.sig = 0;
4223
                    return retval;
4224
                  }
4225
#endif
4226
              case PR_JOBCONTROL:
4227
                wstat = (what << 8) | 0177;
4228
                break;
4229
              case PR_FAULTED:
4230
                switch (what) { /* FIXME: FAULTED_USE_SIGINFO */
4231
#ifdef FLTWATCH
4232
                case FLTWATCH:
4233
                  wstat = (SIGTRAP << 8) | 0177;
4234
                  break;
4235
#endif
4236
#ifdef FLTKWATCH
4237
                case FLTKWATCH:
4238
                  wstat = (SIGTRAP << 8) | 0177;
4239
                  break;
4240
#endif
4241
                  /* FIXME: use si_signo where possible. */
4242
                case FLTPRIV:
4243
#if (FLTILL != FLTPRIV)         /* avoid "duplicate case" error */
4244
                case FLTILL:
4245
#endif
4246
                  wstat = (SIGILL << 8) | 0177;
4247
                  break;
4248
                case FLTBPT:
4249
#if (FLTTRACE != FLTBPT)        /* avoid "duplicate case" error */
4250
                case FLTTRACE:
4251
#endif
4252
                  wstat = (SIGTRAP << 8) | 0177;
4253
                  break;
4254
                case FLTSTACK:
4255
                case FLTACCESS:
4256
#if (FLTBOUNDS != FLTSTACK)     /* avoid "duplicate case" error */
4257
                case FLTBOUNDS:
4258
#endif
4259
                  wstat = (SIGSEGV << 8) | 0177;
4260
                  break;
4261
                case FLTIOVF:
4262
                case FLTIZDIV:
4263
#if (FLTFPE != FLTIOVF)         /* avoid "duplicate case" error */
4264
                case FLTFPE:
4265
#endif
4266
                  wstat = (SIGFPE << 8) | 0177;
4267
                  break;
4268
                case FLTPAGE:           /* Recoverable page fault */
4269
                default:         /* FIXME: use si_signo if possible for fault */
4270
                  retval = pid_to_ptid (-1);
4271
                  printf_filtered ("procfs:%d -- ", __LINE__);
4272
                  printf_filtered ("child stopped for unknown reason:\n");
4273
                  proc_prettyprint_why (why, what, 1);
4274
                  error ("... giving up...");
4275
                  break;
4276
                }
4277
                break;  /* case PR_FAULTED: */
4278
              default:  /* switch (why) unmatched */
4279
                printf_filtered ("procfs:%d -- ", __LINE__);
4280
                printf_filtered ("child stopped for unknown reason:\n");
4281
                proc_prettyprint_why (why, what, 1);
4282
                error ("... giving up...");
4283
                break;
4284
              }
4285
              /*
4286
               * Got this far without error:
4287
               * If retval isn't in the threads database, add it.
4288
               */
4289
              if (PIDGET (retval) > 0 &&
4290
                  !ptid_equal (retval, inferior_ptid) &&
4291
                  !in_thread_list (retval))
4292
                {
4293
                  /*
4294
                   * We have a new thread.
4295
                   * We need to add it both to GDB's list and to our own.
4296
                   * If we don't create a procinfo, resume may be unhappy
4297
                   * later.
4298
                   */
4299
                  printf_filtered ("[New %s]\n", target_pid_to_str (retval));
4300
                  add_thread (retval);
4301
                  if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4302
                    create_procinfo (PIDGET (retval), TIDGET (retval));
4303
 
4304
                  /* In addition, it's possible that this is the first
4305
                   * new thread we've seen, in which case we may not
4306
                   * have created entries for inferior_ptid yet.
4307
                   */
4308
                  if (TIDGET (inferior_ptid) != 0)
4309
                    {
4310
                      if (!in_thread_list (inferior_ptid))
4311
                        add_thread (inferior_ptid);
4312
                      if (find_procinfo (PIDGET (inferior_ptid),
4313
                                         TIDGET (inferior_ptid)) == NULL)
4314
                        create_procinfo (PIDGET (inferior_ptid),
4315
                                         TIDGET (inferior_ptid));
4316
                    }
4317
                }
4318
            }
4319
          else  /* flags do not indicate STOPPED */
4320
            {
4321
              /* surely this can't happen... */
4322
              printf_filtered ("procfs:%d -- process not stopped.\n",
4323
                               __LINE__);
4324
              proc_prettyprint_flags (flags, 1);
4325
              error ("procfs: ...giving up...");
4326
            }
4327
        }
4328
 
4329
      if (status)
4330
        store_waitstatus (status, wstat);
4331
    }
4332
 
4333
  return retval;
4334
}
4335
 
4336
/* Transfer LEN bytes between GDB address MYADDR and target address
4337
   MEMADDR.  If DOWRITE is non-zero, transfer them to the target,
4338
   otherwise transfer them from the target.  TARGET is unused.
4339
 
4340
   The return value is 0 if an error occurred or no bytes were
4341
   transferred.  Otherwise, it will be a positive value which
4342
   indicates the number of bytes transferred between gdb and the
4343
   target.  (Note that the interface also makes provisions for
4344
   negative values, but this capability isn't implemented here.) */
4345
 
4346
static int
4347
procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
4348
                    struct mem_attrib *attrib, struct target_ops *target)
4349
{
4350
  procinfo *pi;
4351
  int nbytes = 0;
4352
 
4353
  /* Find procinfo for main process */
4354
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4355
  if (pi->as_fd == 0 &&
4356
      open_procinfo_files (pi, FD_AS) == 0)
4357
    {
4358
      proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4359
      return 0;
4360
    }
4361
 
4362
  if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
4363
    {
4364
      if (dowrite)
4365
        {
4366
#ifdef NEW_PROC_API
4367
          PROCFS_NOTE ("write memory: ");
4368
#else
4369
          PROCFS_NOTE ("write memory: \n");
4370
#endif
4371
          nbytes = write (pi->as_fd, myaddr, len);
4372
        }
4373
      else
4374
        {
4375
          PROCFS_NOTE ("read  memory: \n");
4376
          nbytes = read (pi->as_fd, myaddr, len);
4377
        }
4378
      if (nbytes < 0)
4379
        {
4380
          nbytes = 0;
4381
        }
4382
    }
4383
  return nbytes;
4384
}
4385
 
4386
/*
4387
 * Function: invalidate_cache
4388
 *
4389
 * Called by target_resume before making child runnable.
4390
 * Mark cached registers and status's invalid.
4391
 * If there are "dirty" caches that need to be written back
4392
 * to the child process, do that.
4393
 *
4394
 * File descriptors are also cached.
4395
 * As they are a limited resource, we cannot hold onto them indefinitely.
4396
 * However, as they are expensive to open, we don't want to throw them
4397
 * away indescriminately either.  As a compromise, we will keep the
4398
 * file descriptors for the parent process, but discard any file
4399
 * descriptors we may have accumulated for the threads.
4400
 *
4401
 * Return value:
4402
 * As this function is called by iterate_over_threads, it always
4403
 * returns zero (so that iterate_over_threads will keep iterating).
4404
 */
4405
 
4406
 
4407
static int
4408
invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
4409
{
4410
  /*
4411
   * About to run the child; invalidate caches and do any other cleanup.
4412
   */
4413
 
4414
#if 0
4415
  if (pi->gregs_dirty)
4416
    if (parent == NULL ||
4417
        proc_get_current_thread (parent) != pi->tid)
4418
      if (!proc_set_gregs (pi)) /* flush gregs cache */
4419
        proc_warn (pi, "target_resume, set_gregs",
4420
                   __LINE__);
4421
  if (FP0_REGNUM >= 0)
4422
    if (pi->fpregs_dirty)
4423
      if (parent == NULL ||
4424
          proc_get_current_thread (parent) != pi->tid)
4425
        if (!proc_set_fpregs (pi))      /* flush fpregs cache */
4426
          proc_warn (pi, "target_resume, set_fpregs",
4427
                     __LINE__);
4428
#endif
4429
 
4430
  if (parent != NULL)
4431
    {
4432
      /* The presence of a parent indicates that this is an LWP.
4433
         Close any file descriptors that it might have open.
4434
         We don't do this to the master (parent) procinfo.  */
4435
 
4436
      close_procinfo_files (pi);
4437
    }
4438
  pi->gregs_valid   = 0;
4439
  pi->fpregs_valid  = 0;
4440
#if 0
4441
  pi->gregs_dirty   = 0;
4442
  pi->fpregs_dirty  = 0;
4443
#endif
4444
  pi->status_valid  = 0;
4445
  pi->threads_valid = 0;
4446
 
4447
  return 0;
4448
}
4449
 
4450
#if 0
4451
/*
4452
 * Function: make_signal_thread_runnable
4453
 *
4454
 * A callback function for iterate_over_threads.
4455
 * Find the asynchronous signal thread, and make it runnable.
4456
 * See if that helps matters any.
4457
 */
4458
 
4459
static int
4460
make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
4461
{
4462
#ifdef PR_ASLWP
4463
  if (proc_flags (pi) & PR_ASLWP)
4464
    {
4465
      if (!proc_run_process (pi, 0, -1))
4466
        proc_error (pi, "make_signal_thread_runnable", __LINE__);
4467
      return 1;
4468
    }
4469
#endif
4470
  return 0;
4471
}
4472
#endif
4473
 
4474
/*
4475
 * Function: target_resume
4476
 *
4477
 * Make the child process runnable.  Normally we will then call
4478
 * procfs_wait and wait for it to stop again (unles gdb is async).
4479
 *
4480
 * Arguments:
4481
 *  step:  if true, then arrange for the child to stop again
4482
 *         after executing a single instruction.
4483
 *  signo: if zero, then cancel any pending signal.
4484
 *         If non-zero, then arrange for the indicated signal
4485
 *         to be delivered to the child when it runs.
4486
 *  pid:   if -1, then allow any child thread to run.
4487
 *         if non-zero, then allow only the indicated thread to run.
4488
 *******   (not implemented yet)
4489
 */
4490
 
4491
static void
4492
procfs_resume (ptid_t ptid, int step, enum target_signal signo)
4493
{
4494
  procinfo *pi, *thread;
4495
  int native_signo;
4496
 
4497
  /* 2.1:
4498
     prrun.prflags |= PRSVADDR;
4499
     prrun.pr_vaddr = $PC;         set resume address
4500
     prrun.prflags |= PRSTRACE;    trace signals in pr_trace (all)
4501
     prrun.prflags |= PRSFAULT;    trace faults in pr_fault (all but PAGE)
4502
     prrun.prflags |= PRCFAULT;    clear current fault.
4503
 
4504
     PRSTRACE and PRSFAULT can be done by other means
4505
        (proc_trace_signals, proc_trace_faults)
4506
     PRSVADDR is unnecessary.
4507
     PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4508
     This basically leaves PRSTEP and PRCSIG.
4509
     PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4510
     So basically PR_STEP is the sole argument that must be passed
4511
     to proc_run_process (for use in the prrun struct by ioctl). */
4512
 
4513
  /* Find procinfo for main process */
4514
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
4515
 
4516
  /* First cut: ignore pid argument */
4517
  errno = 0;
4518
 
4519
  /* Convert signal to host numbering.  */
4520
  if (signo == 0 ||
4521
      (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
4522
    native_signo = 0;
4523
  else
4524
    native_signo = target_signal_to_host (signo);
4525
 
4526
  pi->ignore_next_sigstop = 0;
4527
 
4528
  /* Running the process voids all cached registers and status. */
4529
  /* Void the threads' caches first */
4530
  proc_iterate_over_threads (pi, invalidate_cache, NULL);
4531
  /* Void the process procinfo's caches.  */
4532
  invalidate_cache (NULL, pi, NULL);
4533
 
4534
  if (PIDGET (ptid) != -1)
4535
    {
4536
      /* Resume a specific thread, presumably suppressing the others. */
4537
      thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
4538
      if (thread != NULL)
4539
        {
4540
          if (thread->tid != 0)
4541
            {
4542
              /* We're to resume a specific thread, and not the others.
4543
               * Set the child process's PR_ASYNC flag.
4544
               */
4545
#ifdef PR_ASYNC
4546
              if (!proc_set_async (pi))
4547
                proc_error (pi, "target_resume, set_async", __LINE__);
4548
#endif
4549
#if 0
4550
              proc_iterate_over_threads (pi,
4551
                                         make_signal_thread_runnable,
4552
                                         NULL);
4553
#endif
4554
              pi = thread;      /* substitute the thread's procinfo for run */
4555
            }
4556
        }
4557
    }
4558
 
4559
  if (!proc_run_process (pi, step, native_signo))
4560
    {
4561
      if (errno == EBUSY)
4562
        warning ("resume: target already running.  Pretend to resume, and hope for the best!\n");
4563
      else
4564
        proc_error (pi, "target_resume", __LINE__);
4565
    }
4566
}
4567
 
4568
/*
4569
 * Function: register_gdb_signals
4570
 *
4571
 * Traverse the list of signals that GDB knows about
4572
 * (see "handle" command), and arrange for the target
4573
 * to be stopped or not, according to these settings.
4574
 *
4575
 * Returns non-zero for success, zero for failure.
4576
 */
4577
 
4578
static int
4579
register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
4580
{
4581
  int signo;
4582
 
4583
  for (signo = 0; signo < NSIG; signo ++)
4584
    if (signal_stop_state  (target_signal_from_host (signo)) == 0 &&
4585
        signal_print_state (target_signal_from_host (signo)) == 0 &&
4586
        signal_pass_state  (target_signal_from_host (signo)) == 1)
4587
      prdelset (signals, signo);
4588
    else
4589
      praddset (signals, signo);
4590
 
4591
  return proc_set_traced_signals (pi, signals);
4592
}
4593
 
4594
/*
4595
 * Function: target_notice_signals
4596
 *
4597
 * Set up to trace signals in the child process.
4598
 */
4599
 
4600
static void
4601
procfs_notice_signals (ptid_t ptid)
4602
{
4603
  gdb_sigset_t signals;
4604
  procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
4605
 
4606
  if (proc_get_traced_signals (pi, &signals) &&
4607
      register_gdb_signals    (pi, &signals))
4608
    return;
4609
  else
4610
    proc_error (pi, "notice_signals", __LINE__);
4611
}
4612
 
4613
/*
4614
 * Function: target_files_info
4615
 *
4616
 * Print status information about the child process.
4617
 */
4618
 
4619
static void
4620
procfs_files_info (struct target_ops *ignore)
4621
{
4622
  printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4623
                   attach_flag? "attached": "child",
4624
                   target_pid_to_str (inferior_ptid));
4625
}
4626
 
4627
/*
4628
 * Function: target_open
4629
 *
4630
 * A dummy: you don't open procfs.
4631
 */
4632
 
4633
static void
4634
procfs_open (char *args, int from_tty)
4635
{
4636
  error ("Use the \"run\" command to start a Unix child process.");
4637
}
4638
 
4639
/*
4640
 * Function: target_can_run
4641
 *
4642
 * This tells GDB that this target vector can be invoked
4643
 * for "run" or "attach".
4644
 */
4645
 
4646
int procfs_suppress_run = 0;     /* Non-zero if procfs should pretend not to
4647
                                   be a runnable target.  Used by targets
4648
                                   that can sit atop procfs, such as solaris
4649
                                   thread support.  */
4650
 
4651
 
4652
static int
4653
procfs_can_run (void)
4654
{
4655
  /* This variable is controlled by modules that sit atop procfs that
4656
     may layer their own process structure atop that provided here.
4657
     sol-thread.c does this because of the Solaris two-level thread
4658
     model.  */
4659
 
4660
  /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
4661
 
4662
  return !procfs_suppress_run;
4663
}
4664
 
4665
/*
4666
 * Function: target_stop
4667
 *
4668
 * Stop the child process asynchronously, as when the
4669
 * gdb user types control-c or presses a "stop" button.
4670
 *
4671
 * Works by sending kill(SIGINT) to the child's process group.
4672
 */
4673
 
4674
static void
4675
procfs_stop (void)
4676
{
4677
  extern pid_t inferior_process_group;
4678
 
4679
  kill (-inferior_process_group, SIGINT);
4680
}
4681
 
4682
/*
4683
 * Function: unconditionally_kill_inferior
4684
 *
4685
 * Make it die.  Wait for it to die.  Clean up after it.
4686
 * Note: this should only be applied to the real process,
4687
 * not to an LWP, because of the check for parent-process.
4688
 * If we need this to work for an LWP, it needs some more logic.
4689
 */
4690
 
4691
static void
4692
unconditionally_kill_inferior (procinfo *pi)
4693
{
4694
  int parent_pid;
4695
 
4696
  parent_pid = proc_parent_pid (pi);
4697
#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4698
  /* FIXME: use access functions */
4699
  /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4700
     before the PIOCKILL, otherwise it might generate a corrupted core
4701
     file for the inferior.  */
4702
  if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4703
    {
4704
      printf_filtered ("unconditionally_kill: SSIG failed!\n");
4705
    }
4706
#endif
4707
#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4708
  /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4709
     to kill the inferior, otherwise it might remain stopped with a
4710
     pending SIGKILL.
4711
     We do not check the result of the PIOCSSIG, the inferior might have
4712
     died already.  */
4713
  {
4714
    gdb_siginfo_t newsiginfo;
4715
 
4716
    memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4717
    newsiginfo.si_signo = SIGKILL;
4718
    newsiginfo.si_code = 0;
4719
    newsiginfo.si_errno = 0;
4720
    newsiginfo.si_pid = getpid ();
4721
    newsiginfo.si_uid = getuid ();
4722
    /* FIXME: use proc_set_current_signal */
4723
    ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4724
  }
4725
#else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4726
  if (!proc_kill (pi, SIGKILL))
4727
    proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
4728
#endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4729
  destroy_procinfo (pi);
4730
 
4731
  /* If pi is GDB's child, wait for it to die.  */
4732
  if (parent_pid == getpid ())
4733
    /* FIXME: should we use waitpid to make sure we get the right event?
4734
       Should we check the returned event?  */
4735
    {
4736
#if 0
4737
      int status, ret;
4738
 
4739
      ret = waitpid (pi->pid, &status, 0);
4740
#else
4741
      wait (NULL);
4742
#endif
4743
    }
4744
}
4745
 
4746
/*
4747
 * Function: target_kill_inferior
4748
 *
4749
 * We're done debugging it, and we want it to go away.
4750
 * Then we want GDB to forget all about it.
4751
 */
4752
 
4753
static void
4754
procfs_kill_inferior (void)
4755
{
4756
  if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
4757
    {
4758
      /* Find procinfo for main process */
4759
      procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
4760
 
4761
      if (pi)
4762
        unconditionally_kill_inferior (pi);
4763
      target_mourn_inferior ();
4764
    }
4765
}
4766
 
4767
/*
4768
 * Function: target_mourn_inferior
4769
 *
4770
 * Forget we ever debugged this thing!
4771
 */
4772
 
4773
static void
4774
procfs_mourn_inferior (void)
4775
{
4776
  procinfo *pi;
4777
 
4778
  if (!ptid_equal (inferior_ptid, null_ptid))
4779
    {
4780
      /* Find procinfo for main process */
4781
      pi = find_procinfo (PIDGET (inferior_ptid), 0);
4782
      if (pi)
4783
        destroy_procinfo (pi);
4784
    }
4785
  unpush_target (&procfs_ops);
4786
  generic_mourn_inferior ();
4787
}
4788
 
4789
/*
4790
 * Function: init_inferior
4791
 *
4792
 * When GDB forks to create a runnable inferior process,
4793
 * this function is called on the parent side of the fork.
4794
 * It's job is to do whatever is necessary to make the child
4795
 * ready to be debugged, and then wait for the child to synchronize.
4796
 */
4797
 
4798
static void
4799
procfs_init_inferior (int pid)
4800
{
4801
  procinfo *pi;
4802
  gdb_sigset_t signals;
4803
  int fail;
4804
 
4805
  /* This routine called on the parent side (GDB side)
4806
     after GDB forks the inferior.  */
4807
 
4808
  push_target (&procfs_ops);
4809
 
4810
  if ((pi = create_procinfo (pid, 0)) == NULL)
4811
    perror ("procfs: out of memory in 'init_inferior'");
4812
 
4813
  if (!open_procinfo_files (pi, FD_CTL))
4814
    proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4815
 
4816
  /*
4817
    xmalloc                     // done
4818
    open_procinfo_files         // done
4819
    link list                   // done
4820
    prfillset (trace)
4821
    procfs_notice_signals
4822
    prfillset (fault)
4823
    prdelset (FLTPAGE)
4824
    PIOCWSTOP
4825
    PIOCSFAULT
4826
    */
4827
 
4828
  /* If not stopped yet, wait for it to stop. */
4829
  if (!(proc_flags (pi) & PR_STOPPED) &&
4830
      !(proc_wait_for_stop (pi)))
4831
    dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4832
 
4833
  /* Save some of the /proc state to be restored if we detach.  */
4834
  /* FIXME: Why?  In case another debugger was debugging it?
4835
     We're it's parent, for Ghu's sake! */
4836
  if (!proc_get_traced_signals  (pi, &pi->saved_sigset))
4837
    proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4838
  if (!proc_get_held_signals    (pi, &pi->saved_sighold))
4839
    proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4840
  if (!proc_get_traced_faults   (pi, &pi->saved_fltset))
4841
    proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
4842
  if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
4843
    proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
4844
  if (!proc_get_traced_sysexit  (pi, pi->saved_exitset))
4845
    proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4846
 
4847
  /* Register to trace selected signals in the child. */
4848
  prfillset (&signals);
4849
  if (!register_gdb_signals (pi, &signals))
4850
    proc_error (pi, "init_inferior, register_signals", __LINE__);
4851
 
4852
  if ((fail = procfs_debug_inferior (pi)) != 0)
4853
    proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4854
 
4855
  /* FIXME: logically, we should really be turning OFF run-on-last-close,
4856
     and possibly even turning ON kill-on-last-close at this point.  But
4857
     I can't make that change without careful testing which I don't have
4858
     time to do right now...  */
4859
  /* Turn on run-on-last-close flag so that the child
4860
     will die if GDB goes away for some reason.  */
4861
  if (!proc_set_run_on_last_close (pi))
4862
    proc_error (pi, "init_inferior, set_RLC", __LINE__);
4863
 
4864
  /* The 'process ID' we return to GDB is composed of
4865
     the actual process ID plus the lwp ID. */
4866
  inferior_ptid = MERGEPID (pi->pid, proc_get_current_thread (pi));
4867
 
4868
#ifdef START_INFERIOR_TRAPS_EXPECTED
4869
  startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4870
#else
4871
  /* One trap to exec the shell, one to exec the program being debugged.  */
4872
  startup_inferior (2);
4873
#endif /* START_INFERIOR_TRAPS_EXPECTED */
4874
}
4875
 
4876
/*
4877
 * Function: set_exec_trap
4878
 *
4879
 * When GDB forks to create a new process, this function is called
4880
 * on the child side of the fork before GDB exec's the user program.
4881
 * Its job is to make the child minimally debuggable, so that the
4882
 * parent GDB process can connect to the child and take over.
4883
 * This function should do only the minimum to make that possible,
4884
 * and to synchronize with the parent process.  The parent process
4885
 * should take care of the details.
4886
 */
4887
 
4888
static void
4889
procfs_set_exec_trap (void)
4890
{
4891
  /* This routine called on the child side (inferior side)
4892
     after GDB forks the inferior.  It must use only local variables,
4893
     because it may be sharing data space with its parent.  */
4894
 
4895
  procinfo *pi;
4896
  sysset_t *exitset;
4897
 
4898
  if ((pi = create_procinfo (getpid (), 0)) == NULL)
4899
    perror_with_name ("procfs: create_procinfo failed in child.");
4900
 
4901
  if (open_procinfo_files (pi, FD_CTL) == 0)
4902
    {
4903
      proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4904
      gdb_flush (gdb_stderr);
4905
      /* no need to call "dead_procinfo", because we're going to exit. */
4906
      _exit (127);
4907
    }
4908
 
4909
#ifdef PRFS_STOPEXEC    /* defined on OSF */
4910
  /* OSF method for tracing exec syscalls.  Quoting:
4911
     Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4912
     exits from exec system calls because of the user level loader.  */
4913
  /* FIXME: make nice and maybe move into an access function. */
4914
  {
4915
    int prfs_flags;
4916
 
4917
    if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4918
      {
4919
        proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4920
        gdb_flush (gdb_stderr);
4921
        _exit (127);
4922
      }
4923
    prfs_flags |= PRFS_STOPEXEC;
4924
 
4925
    if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4926
      {
4927
        proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4928
        gdb_flush (gdb_stderr);
4929
        _exit (127);
4930
      }
4931
  }
4932
#else /* not PRFS_STOPEXEC */
4933
  /* Everyone else's (except OSF) method for tracing exec syscalls */
4934
  /* GW: Rationale...
4935
     Not all systems with /proc have all the exec* syscalls with the same
4936
     names.  On the SGI, for example, there is no SYS_exec, but there
4937
     *is* a SYS_execv.  So, we try to account for that. */
4938
 
4939
  exitset = sysset_t_alloc (pi);
4940
  gdb_premptysysset (exitset);
4941
#ifdef SYS_exec
4942
  gdb_praddsysset (exitset, SYS_exec);
4943
#endif
4944
#ifdef SYS_execve
4945
  gdb_praddsysset (exitset, SYS_execve);
4946
#endif
4947
#ifdef SYS_execv
4948
  gdb_praddsysset (exitset, SYS_execv);
4949
#endif
4950
#ifdef DYNAMIC_SYSCALLS
4951
  {
4952
    int callnum = find_syscall (pi, "execve");
4953
 
4954
    if (callnum >= 0)
4955
      gdb_praddsysset (exitset, callnum);
4956
 
4957
    callnum = find_syscall (pi, "ra_execve");
4958
    if (callnum >= 0)
4959
      gdb_praddsysset (exitset, callnum);
4960
  }
4961
#endif /* DYNAMIC_SYSCALLS */
4962
 
4963
  if (!proc_set_traced_sysexit (pi, exitset))
4964
    {
4965
      proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4966
      gdb_flush (gdb_stderr);
4967
      _exit (127);
4968
    }
4969
#endif /* PRFS_STOPEXEC */
4970
 
4971
  /* FIXME: should this be done in the parent instead? */
4972
  /* Turn off inherit on fork flag so that all grand-children
4973
     of gdb start with tracing flags cleared.  */
4974
  if (!proc_unset_inherit_on_fork (pi))
4975
    proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4976
 
4977
  /* Turn off run on last close flag, so that the child process
4978
     cannot run away just because we close our handle on it.
4979
     We want it to wait for the parent to attach.  */
4980
  if (!proc_unset_run_on_last_close (pi))
4981
    proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4982
 
4983
  /* FIXME: No need to destroy the procinfo --
4984
     we have our own address space, and we're about to do an exec! */
4985
  /*destroy_procinfo (pi);*/
4986
}
4987
 
4988
/*
4989
 * Function: create_inferior
4990
 *
4991
 * This function is called BEFORE gdb forks the inferior process.
4992
 * Its only real responsibility is to set things up for the fork,
4993
 * and tell GDB which two functions to call after the fork (one
4994
 * for the parent, and one for the child).
4995
 *
4996
 * This function does a complicated search for a unix shell program,
4997
 * which it then uses to parse arguments and environment variables
4998
 * to be sent to the child.  I wonder whether this code could not
4999
 * be abstracted out and shared with other unix targets such as
5000
 * infptrace?
5001
 */
5002
 
5003
static void
5004
procfs_create_inferior (char *exec_file, char *allargs, char **env)
5005
{
5006
  char *shell_file = getenv ("SHELL");
5007
  char *tryname;
5008
  if (shell_file != NULL && strchr (shell_file, '/') == NULL)
5009
    {
5010
 
5011
      /* We will be looking down the PATH to find shell_file.  If we
5012
         just do this the normal way (via execlp, which operates by
5013
         attempting an exec for each element of the PATH until it
5014
         finds one which succeeds), then there will be an exec for
5015
         each failed attempt, each of which will cause a PR_SYSEXIT
5016
         stop, and we won't know how to distinguish the PR_SYSEXIT's
5017
         for these failed execs with the ones for successful execs
5018
         (whether the exec has succeeded is stored at that time in the
5019
         carry bit or some such architecture-specific and
5020
         non-ABI-specified place).
5021
 
5022
         So I can't think of anything better than to search the PATH
5023
         now.  This has several disadvantages: (1) There is a race
5024
         condition; if we find a file now and it is deleted before we
5025
         exec it, we lose, even if the deletion leaves a valid file
5026
         further down in the PATH, (2) there is no way to know exactly
5027
         what an executable (in the sense of "capable of being
5028
         exec'd") file is.  Using access() loses because it may lose
5029
         if the caller is the superuser; failing to use it loses if
5030
         there are ACLs or some such.  */
5031
 
5032
      char *p;
5033
      char *p1;
5034
      /* FIXME-maybe: might want "set path" command so user can change what
5035
         path is used from within GDB.  */
5036
      char *path = getenv ("PATH");
5037
      int len;
5038
      struct stat statbuf;
5039
 
5040
      if (path == NULL)
5041
        path = "/bin:/usr/bin";
5042
 
5043
      tryname = alloca (strlen (path) + strlen (shell_file) + 2);
5044
      for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
5045
        {
5046
          p1 = strchr (p, ':');
5047
          if (p1 != NULL)
5048
            len = p1 - p;
5049
          else
5050
            len = strlen (p);
5051
          strncpy (tryname, p, len);
5052
          tryname[len] = '\0';
5053
          strcat (tryname, "/");
5054
          strcat (tryname, shell_file);
5055
          if (access (tryname, X_OK) < 0)
5056
            continue;
5057
          if (stat (tryname, &statbuf) < 0)
5058
            continue;
5059
          if (!S_ISREG (statbuf.st_mode))
5060
            /* We certainly need to reject directories.  I'm not quite
5061
               as sure about FIFOs, sockets, etc., but I kind of doubt
5062
               that people want to exec() these things.  */
5063
            continue;
5064
          break;
5065
        }
5066
      if (p == NULL)
5067
        /* Not found.  This must be an error rather than merely passing
5068
           the file to execlp(), because execlp() would try all the
5069
           exec()s, causing GDB to get confused.  */
5070
        error ("procfs:%d -- Can't find shell %s in PATH",
5071
               __LINE__, shell_file);
5072
 
5073
      shell_file = tryname;
5074
    }
5075
 
5076
  fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5077
                 procfs_init_inferior, NULL, shell_file);
5078
 
5079
  /* We are at the first instruction we care about.  */
5080
  /* Pedal to the metal... */
5081
 
5082
  proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
5083
}
5084
 
5085
/*
5086
 * Function: notice_thread
5087
 *
5088
 * Callback for find_new_threads.
5089
 * Calls "add_thread".
5090
 */
5091
 
5092
static int
5093
procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
5094
{
5095
  ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
5096
 
5097
  if (!in_thread_list (gdb_threadid))
5098
    add_thread (gdb_threadid);
5099
 
5100
  return 0;
5101
}
5102
 
5103
/*
5104
 * Function: target_find_new_threads
5105
 *
5106
 * Query all the threads that the target knows about,
5107
 * and give them back to GDB to add to its list.
5108
 */
5109
 
5110
void
5111
procfs_find_new_threads (void)
5112
{
5113
  procinfo *pi;
5114
 
5115
  /* Find procinfo for main process */
5116
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5117
  proc_update_threads (pi);
5118
  proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
5119
}
5120
 
5121
/*
5122
 * Function: target_thread_alive
5123
 *
5124
 * Return true if the thread is still 'alive'.
5125
 *
5126
 * This guy doesn't really seem to be doing his job.
5127
 * Got to investigate how to tell when a thread is really gone.
5128
 */
5129
 
5130
static int
5131
procfs_thread_alive (ptid_t ptid)
5132
{
5133
  int proc, thread;
5134
  procinfo *pi;
5135
 
5136
  proc    = PIDGET (ptid);
5137
  thread  = TIDGET (ptid);
5138
  /* If I don't know it, it ain't alive! */
5139
  if ((pi = find_procinfo (proc, thread)) == NULL)
5140
    return 0;
5141
 
5142
  /* If I can't get its status, it ain't alive!
5143
     What's more, I need to forget about it!  */
5144
  if (!proc_get_status (pi))
5145
    {
5146
      destroy_procinfo (pi);
5147
      return 0;
5148
    }
5149
  /* I couldn't have got its status if it weren't alive, so it's alive.  */
5150
  return 1;
5151
}
5152
 
5153
/*
5154
 * Function: target_pid_to_str
5155
 *
5156
 * Return a string to be used to identify the thread in
5157
 * the "info threads" display.
5158
 */
5159
 
5160
char *
5161
procfs_pid_to_str (ptid_t ptid)
5162
{
5163
  static char buf[80];
5164
  int proc, thread;
5165
  procinfo *pi;
5166
 
5167
  proc    = PIDGET (ptid);
5168
  thread  = TIDGET (ptid);
5169
  pi      = find_procinfo (proc, thread);
5170
 
5171
  if (thread == 0)
5172
    sprintf (buf, "Process %d", proc);
5173
  else
5174
    sprintf (buf, "LWP %d", thread);
5175
  return &buf[0];
5176
}
5177
 
5178
/*
5179
 * Function: procfs_set_watchpoint
5180
 * Insert a watchpoint
5181
 */
5182
 
5183
int
5184
procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5185
                       int after)
5186
{
5187
#ifndef UNIXWARE
5188
#ifndef AIX5
5189
  int       pflags = 0;
5190
  procinfo *pi;
5191
 
5192
  pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5193
                             PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5194
 
5195
  /* Translate from GDB's flags to /proc's */
5196
  if (len > 0)   /* len == 0 means delete watchpoint */
5197
    {
5198
      switch (rwflag) {         /* FIXME: need an enum! */
5199
      case hw_write:            /* default watchpoint (write) */
5200
        pflags = WRITE_WATCHFLAG;
5201
        break;
5202
      case hw_read:             /* read watchpoint */
5203
        pflags = READ_WATCHFLAG;
5204
        break;
5205
      case hw_access:           /* access watchpoint */
5206
        pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5207
        break;
5208
      case hw_execute:          /* execution HW breakpoint */
5209
        pflags = EXEC_WATCHFLAG;
5210
        break;
5211
      default:                  /* Something weird.  Return error. */
5212
        return -1;
5213
      }
5214
      if (after)                /* Stop after r/w access is completed. */
5215
        pflags |= AFTER_WATCHFLAG;
5216
    }
5217
 
5218
  if (!proc_set_watchpoint (pi, addr, len, pflags))
5219
    {
5220
      if (errno == E2BIG)       /* Typical error for no resources */
5221
        return -1;              /* fail */
5222
      /* GDB may try to remove the same watchpoint twice.
5223
         If a remove request returns no match, don't error.  */
5224
      if (errno == ESRCH && len == 0)
5225
        return 0;                /* ignore */
5226
      proc_error (pi, "set_watchpoint", __LINE__);
5227
    }
5228
#endif /* AIX5 */
5229
#endif /* UNIXWARE */
5230
  return 0;
5231
}
5232
 
5233
/*
5234
 * Function: stopped_by_watchpoint
5235
 *
5236
 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5237
 * else returns zero.
5238
 */
5239
 
5240
int
5241
procfs_stopped_by_watchpoint (ptid_t ptid)
5242
{
5243
  procinfo *pi;
5244
 
5245
  pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5246
                             PIDGET (inferior_ptid) : PIDGET (ptid), 0);
5247
 
5248
  if (!pi)      /* If no process, then not stopped by watchpoint!  */
5249
    return 0;
5250
 
5251
  if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
5252
    {
5253
      if (proc_why (pi) == PR_FAULTED)
5254
        {
5255
#ifdef FLTWATCH
5256
          if (proc_what (pi) == FLTWATCH)
5257
            return 1;
5258
#endif
5259
#ifdef FLTKWATCH
5260
          if (proc_what (pi) == FLTKWATCH)
5261
            return 1;
5262
#endif
5263
        }
5264
    }
5265
  return 0;
5266
}
5267
 
5268
#ifdef TM_I386SOL2_H
5269
/*
5270
 * Function: procfs_find_LDT_entry
5271
 *
5272
 * Input:
5273
 *   ptid_t ptid;       // The GDB-style pid-plus-LWP.
5274
 *
5275
 * Return:
5276
 *   pointer to the corresponding LDT entry.
5277
 */
5278
 
5279
struct ssd *
5280
procfs_find_LDT_entry (ptid_t ptid)
5281
{
5282
  gdb_gregset_t *gregs;
5283
  int            key;
5284
  procinfo      *pi;
5285
 
5286
  /* Find procinfo for the lwp. */
5287
  if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
5288
    {
5289
      warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5290
               PIDGET (ptid), TIDGET (ptid));
5291
      return NULL;
5292
    }
5293
  /* get its general registers. */
5294
  if ((gregs = proc_get_gregs (pi)) == NULL)
5295
    {
5296
      warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5297
               PIDGET (ptid), TIDGET (ptid));
5298
      return NULL;
5299
    }
5300
  /* Now extract the GS register's lower 16 bits. */
5301
  key = (*gregs)[GS] & 0xffff;
5302
 
5303
  /* Find the matching entry and return it. */
5304
  return proc_get_LDT_entry (pi, key);
5305
}
5306
#endif /* TM_I386SOL2_H */
5307
 
5308
 
5309
 
5310
static void
5311
info_proc_cmd (char *args, int from_tty)
5312
{
5313
  struct cleanup *old_chain;
5314
  procinfo *process = NULL;
5315
  procinfo *thread  = NULL;
5316
  char    **argv    = NULL;
5317
  char     *tmp     = NULL;
5318
  int       pid     = 0;
5319
  int       tid     = 0;
5320
 
5321
  old_chain = make_cleanup (null_cleanup, 0);
5322
  if (args)
5323
    {
5324
      if ((argv = buildargv (args)) == NULL)
5325
        nomem (0);
5326
      else
5327
        make_cleanup_freeargv (argv);
5328
    }
5329
  while (argv != NULL && *argv != NULL)
5330
    {
5331
      if (isdigit (argv[0][0]))
5332
        {
5333
          pid = strtoul (argv[0], &tmp, 10);
5334
          if (*tmp == '/')
5335
            tid = strtoul (++tmp, NULL, 10);
5336
        }
5337
      else if (argv[0][0] == '/')
5338
        {
5339
          tid = strtoul (argv[0] + 1, NULL, 10);
5340
        }
5341
      else
5342
        {
5343
          /* [...] */
5344
        }
5345
      argv++;
5346
    }
5347
  if (pid == 0)
5348
    pid = PIDGET (inferior_ptid);
5349
  if (pid == 0)
5350
    error ("No current process: you must name one.");
5351
  else
5352
    {
5353
      /* Have pid, will travel.
5354
         First see if it's a process we're already debugging. */
5355
      process = find_procinfo (pid, 0);
5356
       if (process == NULL)
5357
         {
5358
           /* No.  So open a procinfo for it, but
5359
              remember to close it again when finished.  */
5360
           process = create_procinfo (pid, 0);
5361
           make_cleanup (do_destroy_procinfo_cleanup, process);
5362
           if (!open_procinfo_files (process, FD_CTL))
5363
             proc_error (process, "info proc, open_procinfo_files", __LINE__);
5364
         }
5365
    }
5366
  if (tid != 0)
5367
    thread = create_procinfo (pid, tid);
5368
 
5369
  if (process)
5370
    {
5371
      printf_filtered ("process %d flags:\n", process->pid);
5372
      proc_prettyprint_flags (proc_flags (process), 1);
5373
      if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5374
        proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5375
      if (proc_get_nthreads (process) > 1)
5376
        printf_filtered ("Process has %d threads.\n",
5377
                         proc_get_nthreads (process));
5378
    }
5379
  if (thread)
5380
    {
5381
      printf_filtered ("thread %d flags:\n", thread->tid);
5382
      proc_prettyprint_flags (proc_flags (thread), 1);
5383
      if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5384
        proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5385
    }
5386
 
5387
  do_cleanups (old_chain);
5388
}
5389
 
5390
static void
5391
proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
5392
{
5393
  procinfo *pi;
5394
  sysset_t *sysset;
5395
  int       syscallnum = 0;
5396
 
5397
  if (PIDGET (inferior_ptid) <= 0)
5398
    error ("you must be debugging a process to use this command.");
5399
 
5400
  if (args == NULL || args[0] == 0)
5401
    error_no_arg ("system call to trace");
5402
 
5403
  pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5404
  if (isdigit (args[0]))
5405
    {
5406
      syscallnum = atoi (args);
5407
      if (entry_or_exit == PR_SYSENTRY)
5408
        sysset = proc_get_traced_sysentry (pi, NULL);
5409
      else
5410
        sysset = proc_get_traced_sysexit (pi, NULL);
5411
 
5412
      if (sysset == NULL)
5413
        proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
5414
 
5415
      if (mode == FLAG_SET)
5416
        gdb_praddsysset (sysset, syscallnum);
5417
      else
5418
        gdb_prdelsysset (sysset, syscallnum);
5419
 
5420
      if (entry_or_exit == PR_SYSENTRY)
5421
        {
5422
          if (!proc_set_traced_sysentry (pi, sysset))
5423
            proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5424
        }
5425
      else
5426
        {
5427
          if (!proc_set_traced_sysexit (pi, sysset))
5428
            proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5429
        }
5430
    }
5431
}
5432
 
5433
static void
5434
proc_trace_sysentry_cmd (char *args, int from_tty)
5435
{
5436
  proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5437
}
5438
 
5439
static void
5440
proc_trace_sysexit_cmd (char *args, int from_tty)
5441
{
5442
  proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
5443
}
5444
 
5445
static void
5446
proc_untrace_sysentry_cmd (char *args, int from_tty)
5447
{
5448
  proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5449
}
5450
 
5451
static void
5452
proc_untrace_sysexit_cmd (char *args, int from_tty)
5453
{
5454
  proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5455
}
5456
 
5457
 
5458
void
5459
_initialize_procfs (void)
5460
{
5461
  init_procfs_ops ();
5462
  add_target (&procfs_ops);
5463
  add_info ("proc", info_proc_cmd,
5464
            "Show /proc process information about any running process.\
5465
Default is the process being debugged.");
5466
  add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5467
           "Give a trace of entries into the syscall.");
5468
  add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5469
           "Give a trace of exits from the syscall.");
5470
  add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5471
           "Cancel a trace of entries into the syscall.");
5472
  add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5473
           "Cancel a trace of exits from the syscall.");
5474
}
5475
 
5476
/* =================== END, GDB  "MODULE" =================== */
5477
 
5478
 
5479
 
5480
/* miscelaneous stubs:                                             */
5481
/* The following satisfy a few random symbols mostly created by    */
5482
/* the solaris threads implementation, which I will chase down     */
5483
/* later.        */
5484
 
5485
/*
5486
 * Return a pid for which we guarantee
5487
 * we will be able to find a 'live' procinfo.
5488
 */
5489
 
5490
ptid_t
5491
procfs_first_available (void)
5492
{
5493
  return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
5494
}

powered by: WebSVN 2.1.0

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