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