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