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