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