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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [gdb/] [procfs.c] - Blame information for rev 853

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

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

powered by: WebSVN 2.1.0

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