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