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