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