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