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