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