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