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