Phase 1 of the ptid_t changes.
[deliverable/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);
39f77062 109static void procfs_resume (ptid_t, int, enum target_signal);
a14ed312
KB
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);
39f77062 115static void procfs_notice_signals (ptid_t);
a14ed312
KB
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 **);
39f77062 120static ptid_t procfs_wait (ptid_t, 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 124
39f77062 125static int procfs_thread_alive (ptid_t);
a14ed312
KB
126
127void procfs_find_new_threads (void);
39f77062 128char *procfs_pid_to_str (ptid_t);
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
39f77062 2907 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 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
39f77062 3453static ptid_t do_attach (ptid_t ptid);
a14ed312 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",
39f77062 3605 exec_file, target_pid_to_str (pid_to_ptid (pid)));
c3f6f71d 3606 else
39f77062
KB
3607 printf_filtered ("Attaching to %s\n",
3608 target_pid_to_str (pid_to_ptid (pid)));
c3f6f71d
JM
3609
3610 fflush (stdout);
c906108c 3611 }
39f77062 3612 inferior_ptid = do_attach (pid_to_ptid (pid));
c3f6f71d
JM
3613 push_target (&procfs_ops);
3614}
3615
3616static void
fba45db2 3617procfs_detach (char *args, int from_tty)
c3f6f71d
JM
3618{
3619 char *exec_file;
3620 int signo = 0;
3621
3622 if (from_tty)
c906108c 3623 {
c3f6f71d
JM
3624 exec_file = get_exec_file (0);
3625 if (exec_file == 0)
3626 exec_file = "";
3627 printf_filtered ("Detaching from program: %s %s\n",
39f77062 3628 exec_file, target_pid_to_str (inferior_ptid));
c3f6f71d 3629 fflush (stdout);
c906108c 3630 }
c3f6f71d
JM
3631 if (args)
3632 signo = atoi (args);
3633
3634 do_detach (signo);
39f77062 3635 inferior_ptid = null_ptid;
c3f6f71d 3636 unpush_target (&procfs_ops); /* Pop out of handling an inferior */
c906108c
SS
3637}
3638
39f77062
KB
3639static ptid_t
3640do_attach (ptid_t ptid)
c906108c 3641{
c3f6f71d
JM
3642 procinfo *pi;
3643 int fail;
3644
39f77062 3645 if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL)
c3f6f71d
JM
3646 perror ("procfs: out of memory in 'attach'");
3647
3648 if (!open_procinfo_files (pi, FD_CTL))
3649 {
3650 fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__);
3651 sprintf (errmsg, "do_attach: couldn't open /proc file for process %d",
39f77062 3652 PIDGET (ptid));
c3f6f71d
JM
3653 dead_procinfo (pi, errmsg, NOKILL);
3654 }
c906108c 3655
c3f6f71d
JM
3656 /* Stop the process (if it isn't already stopped). */
3657 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
c906108c 3658 {
c3f6f71d
JM
3659 pi->was_stopped = 1;
3660 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
c906108c
SS
3661 }
3662 else
3663 {
c3f6f71d
JM
3664 pi->was_stopped = 0;
3665 /* Set the process to run again when we close it. */
3666 if (!proc_set_run_on_last_close (pi))
3667 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
3668
3669 /* Now stop the process. */
3670 if (!proc_stop_process (pi))
3671 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
3672 pi->ignore_next_sigstop = 1;
c906108c 3673 }
c3f6f71d
JM
3674 /* Save some of the /proc state to be restored if we detach. */
3675 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
3676 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
3677 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
3678 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
37de36c6 3679 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
c3f6f71d
JM
3680 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
3681 NOKILL);
37de36c6 3682 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
c3f6f71d
JM
3683 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
3684 NOKILL);
3685 if (!proc_get_held_signals (pi, &pi->saved_sighold))
3686 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
3687
3688 if ((fail = procfs_debug_inferior (pi)) != 0)
3689 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
3690
3691 /* Let GDB know that the inferior was attached. */
3692 attach_flag = 1;
3693 return MERGEPID (pi->pid, proc_get_current_thread (pi));
c906108c
SS
3694}
3695
3696static void
fba45db2 3697do_detach (int signo)
c906108c 3698{
c3f6f71d 3699 procinfo *pi;
c906108c 3700
c3f6f71d 3701 /* Find procinfo for the main process */
39f77062 3702 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */
c3f6f71d
JM
3703 if (signo)
3704 if (!proc_set_current_signal (pi, signo))
3705 proc_warn (pi, "do_detach, set_current_signal", __LINE__);
c5aa993b 3706
c3f6f71d
JM
3707 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
3708 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
c906108c 3709
c3f6f71d
JM
3710 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
3711 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
3712
37de36c6 3713 if (!proc_set_traced_sysentry (pi, pi->saved_entryset))
c3f6f71d
JM
3714 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
3715
37de36c6 3716 if (!proc_set_traced_sysexit (pi, pi->saved_exitset))
c3f6f71d
JM
3717 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
3718
3719 if (!proc_set_held_signals (pi, &pi->saved_sighold))
3720 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
3721
3722 if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)))
3723 if (signo || !(pi->was_stopped) ||
3724 query ("Was stopped when attached, make it runnable again? "))
3725 {
3726 /* Clear any pending signal. */
3727 if (!proc_clear_current_fault (pi))
3728 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
3729
3730 if (!proc_set_run_on_last_close (pi))
3731 proc_warn (pi, "do_detach, set_rlc", __LINE__);
3732 }
3733
3734 attach_flag = 0;
3735 destroy_procinfo (pi);
c906108c
SS
3736}
3737
c3f6f71d
JM
3738/*
3739 * fetch_registers
3740 *
3741 * Since the /proc interface cannot give us individual registers,
3742 * we pay no attention to the (regno) argument, and just fetch them all.
3743 * This results in the possibility that we will do unnecessarily many
3744 * fetches, since we may be called repeatedly for individual registers.
3745 * So we cache the results, and mark the cache invalid when the process
3746 * is resumed.
3747 */
3748
c906108c 3749static void
fba45db2 3750procfs_fetch_registers (int regno)
c906108c 3751{
c3f6f71d
JM
3752 gdb_fpregset_t *fpregs;
3753 gdb_gregset_t *gregs;
3754 procinfo *pi;
3755 int pid;
3756 int tid;
c906108c 3757
39f77062
KB
3758 pid = PIDGET (inferior_ptid);
3759 tid = TIDGET (inferior_ptid);
c3f6f71d
JM
3760
3761 /* First look up procinfo for the main process. */
3762 pi = find_procinfo_or_die (pid, 0);
3763
3764 /* If the event thread is not the same as GDB's requested thread
39f77062 3765 (ie. inferior_ptid), then look up procinfo for the requested
c3f6f71d
JM
3766 thread. */
3767 if ((tid != 0) &&
3768 (tid != proc_get_current_thread (pi)))
3769 pi = find_procinfo_or_die (pid, tid);
3770
3771 if (pi == NULL)
3772 error ("procfs: fetch_registers failed to find procinfo for %s",
39f77062 3773 target_pid_to_str (inferior_ptid));
c3f6f71d
JM
3774
3775 if ((gregs = proc_get_gregs (pi)) == NULL)
3776 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
3777
3778 supply_gregset (gregs);
3779
60054393
MS
3780 if (FP0_REGNUM >= 0) /* need floating point? */
3781 {
3782 if ((regno >= 0 && regno < FP0_REGNUM) ||
3783 regno == PC_REGNUM ||
3784 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3785 regno == FP_REGNUM ||
3786 regno == SP_REGNUM)
3787 return; /* not a floating point register */
c5aa993b 3788
60054393
MS
3789 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3790 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
c906108c 3791
60054393
MS
3792 supply_fpregset (fpregs);
3793 }
c906108c
SS
3794}
3795
c3f6f71d
JM
3796/* Get ready to modify the registers array. On machines which store
3797 individual registers, this doesn't need to do anything. On
3798 machines which store all the registers in one fell swoop, such as
3799 /proc, this makes sure that registers contains all the registers
3800 from the program being debugged. */
3801
c906108c 3802static void
fba45db2 3803procfs_prepare_to_store (void)
c906108c 3804{
c3f6f71d
JM
3805#ifdef CHILD_PREPARE_TO_STORE
3806 CHILD_PREPARE_TO_STORE ();
c906108c 3807#endif
c906108c
SS
3808}
3809
3810/*
c3f6f71d
JM
3811 * store_registers
3812 *
3813 * Since the /proc interface will not read individual registers,
3814 * we will cache these requests until the process is resumed, and
3815 * only then write them back to the inferior process.
3816 *
3817 * FIXME: is that a really bad idea? Have to think about cases
3818 * where writing one register might affect the value of others, etc.
3819 */
c906108c 3820
c3f6f71d 3821static void
fba45db2 3822procfs_store_registers (int regno)
c3f6f71d
JM
3823{
3824 gdb_fpregset_t *fpregs;
3825 gdb_gregset_t *gregs;
3826 procinfo *pi;
3827 int pid;
3828 int tid;
c906108c 3829
39f77062
KB
3830 pid = PIDGET (inferior_ptid);
3831 tid = TIDGET (inferior_ptid);
c906108c 3832
c3f6f71d
JM
3833 /* First find procinfo for main process */
3834 pi = find_procinfo_or_die (pid, 0);
3835
3836 /* If current lwp for process is not the same as requested thread
39f77062 3837 (ie. inferior_ptid), then find procinfo for the requested thread. */
c3f6f71d
JM
3838
3839 if ((tid != 0) &&
3840 (tid != proc_get_current_thread (pi)))
3841 pi = find_procinfo_or_die (pid, tid);
3842
3843 if (pi == NULL)
3844 error ("procfs: store_registers: failed to find procinfo for %s",
39f77062 3845 target_pid_to_str (inferior_ptid));
c906108c 3846
c3f6f71d
JM
3847 if ((gregs = proc_get_gregs (pi)) == NULL)
3848 proc_error (pi, "store_registers, get_gregs", __LINE__);
c906108c 3849
c3f6f71d
JM
3850 fill_gregset (gregs, regno);
3851 if (!proc_set_gregs (pi))
3852 proc_error (pi, "store_registers, set_gregs", __LINE__);
c906108c 3853
60054393
MS
3854 if (FP0_REGNUM >= 0) /* need floating point? */
3855 {
3856 if ((regno >= 0 && regno < FP0_REGNUM) ||
3857 regno == PC_REGNUM ||
3858 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
3859 regno == FP_REGNUM ||
3860 regno == SP_REGNUM)
3861 return; /* not a floating point register */
3862
3863 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3864 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3865
3866 fill_fpregset (fpregs, regno);
3867 if (!proc_set_fpregs (pi))
3868 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3869 }
c3f6f71d 3870}
c906108c 3871
37de36c6
KB
3872static int
3873syscall_is_lwp_exit (procinfo *pi, int scall)
3874{
3875
3876#ifdef SYS_lwp_exit
3877 if (scall == SYS_lwp_exit)
3878 return 1;
3879#endif
3880#ifdef SYS_lwpexit
3881 if (scall == SYS_lwpexit)
3882 return 1;
3883#endif
3884 return 0;
3885}
3886
3887static int
3888syscall_is_exit (procinfo *pi, int scall)
3889{
3890#ifdef SYS_exit
3891 if (scall == SYS_exit)
3892 return 1;
3893#endif
3894#ifdef DYNAMIC_SYSCALLS
3895 if (find_syscall (pi, "_exit") == scall)
3896 return 1;
3897#endif
3898 return 0;
3899}
3900
3901static int
3902syscall_is_exec (procinfo *pi, int scall)
3903{
3904#ifdef SYS_exec
3905 if (scall == SYS_exec)
3906 return 1;
3907#endif
3908#ifdef SYS_execv
3909 if (scall == SYS_execv)
3910 return 1;
3911#endif
3912#ifdef SYS_execve
3913 if (scall == SYS_execve)
3914 return 1;
3915#endif
3916#ifdef DYNAMIC_SYSCALLS
3917 if (find_syscall (pi, "_execve"))
3918 return 1;
3919 if (find_syscall (pi, "ra_execve"))
3920 return 1;
3921#endif
3922 return 0;
3923}
3924
3925static int
3926syscall_is_lwp_create (procinfo *pi, int scall)
3927{
3928#ifdef SYS_lwp_create
3929 if (scall == SYS_lwp_create)
3930 return 1;
3931#endif
3932#ifdef SYS_lwpcreate
3933 if (scall == SYS_lwpcreate)
3934 return 1;
3935#endif
3936 return 0;
3937}
3938
c3f6f71d
JM
3939/*
3940 * Function: target_wait
3941 *
3942 * Retrieve the next stop event from the child process.
3943 * If child has not stopped yet, wait for it to stop.
3944 * Translate /proc eventcodes (or possibly wait eventcodes)
3945 * into gdb internal event codes.
3946 *
3947 * Return: id of process (and possibly thread) that incurred the event.
3948 * event codes are returned thru a pointer parameter.
c906108c
SS
3949 */
3950
39f77062
KB
3951static ptid_t
3952procfs_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 3953{
c3f6f71d
JM
3954 /* First cut: loosely based on original version 2.1 */
3955 procinfo *pi;
39f77062
KB
3956 int wstat;
3957 int temp_tid;
3958 ptid_t retval, temp_ptid;
c3f6f71d
JM
3959 int why, what, flags;
3960 int retry = 0;
c906108c 3961
c3f6f71d 3962wait_again:
c906108c 3963
c3f6f71d
JM
3964 retry++;
3965 wstat = 0;
39f77062 3966 retval = pid_to_ptid (-1);
c906108c 3967
c3f6f71d 3968 /* Find procinfo for main process */
39f77062 3969 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d 3970 if (pi)
c906108c 3971 {
c3f6f71d
JM
3972 /* We must assume that the status is stale now... */
3973 pi->status_valid = 0;
3974 pi->gregs_valid = 0;
3975 pi->fpregs_valid = 0;
3976
3977#if 0 /* just try this out... */
3978 flags = proc_flags (pi);
3979 why = proc_why (pi);
3980 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3981 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3982#endif
3983 /* If child is not stopped, wait for it to stop. */
3984 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3985 !proc_wait_for_stop (pi))
c906108c 3986 {
c3f6f71d
JM
3987 /* wait_for_stop failed: has the child terminated? */
3988 if (errno == ENOENT)
c906108c 3989 {
39f77062
KB
3990 int wait_retval;
3991
c3f6f71d 3992 /* /proc file not found; presumably child has terminated. */
39f77062 3993 wait_retval = wait (&wstat); /* "wait" for the child's exit */
c3f6f71d 3994
39f77062 3995 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
c3f6f71d 3996 error ("procfs: couldn't stop process %d: wait returned %d\n",
39f77062 3997 PIDGET (inferior_ptid), wait_retval);
c3f6f71d
JM
3998 /* FIXME: might I not just use waitpid?
3999 Or try find_procinfo to see if I know about this child? */
39f77062 4000 retval = pid_to_ptid (wait_retval);
c906108c 4001 }
d1566ff5
FN
4002 else if (errno == EINTR)
4003 goto wait_again;
c3f6f71d 4004 else
c906108c 4005 {
c3f6f71d
JM
4006 /* Unknown error from wait_for_stop. */
4007 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
c906108c 4008 }
c3f6f71d
JM
4009 }
4010 else
4011 {
4012 /* This long block is reached if either:
4013 a) the child was already stopped, or
4014 b) we successfully waited for the child with wait_for_stop.
4015 This block will analyze the /proc status, and translate it
4016 into a waitstatus for GDB.
4017
4018 If we actually had to call wait because the /proc file
4019 is gone (child terminated), then we skip this block,
4020 because we already have a waitstatus. */
4021
4022 flags = proc_flags (pi);
4023 why = proc_why (pi);
4024 what = proc_what (pi);
4025
c3f6f71d 4026 if (flags & (PR_STOPPED | PR_ISTOP))
c906108c 4027 {
c3f6f71d
JM
4028#ifdef PR_ASYNC
4029 /* If it's running async (for single_thread control),
4030 set it back to normal again. */
4031 if (flags & PR_ASYNC)
4032 if (!proc_unset_async (pi))
4033 proc_error (pi, "target_wait, unset_async", __LINE__);
4034#endif
4035
4036 if (info_verbose)
4037 proc_prettyprint_why (why, what, 1);
4038
4039 /* The 'pid' we will return to GDB is composed of
4040 the process ID plus the lwp ID. */
4041 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
4042
4043 switch (why) {
4044 case PR_SIGNALLED:
4045 wstat = (what << 8) | 0177;
4046 break;
4047 case PR_SYSENTRY:
37de36c6 4048 if (syscall_is_lwp_exit (pi, what))
c3f6f71d 4049 {
37de36c6
KB
4050 printf_filtered ("[%s exited]\n",
4051 target_pid_to_str (retval));
4052 delete_thread (retval);
4053 status->kind = TARGET_WAITKIND_SPURIOUS;
4054 return retval;
4055 }
4056 else if (syscall_is_exit (pi, what))
4057 {
4058 /* Handle SYS_exit call only */
4059 /* Stopped at entry to SYS_exit.
4060 Make it runnable, resume it, then use
4061 the wait system call to get its exit code.
4062 Proc_run_process always clears the current
4063 fault and signal.
4064 Then return its exit status. */
4065 pi->status_valid = 0;
4066 wstat = 0;
4067 /* FIXME: what we should do is return
4068 TARGET_WAITKIND_SPURIOUS. */
4069 if (!proc_run_process (pi, 0, 0))
4070 proc_error (pi, "target_wait, run_process", __LINE__);
4071 if (attach_flag)
c3f6f71d 4072 {
37de36c6
KB
4073 /* Don't call wait: simulate waiting for exit,
4074 return a "success" exit code. Bogus: what if
4075 it returns something else? */
4076 wstat = 0;
39f77062 4077 retval = inferior_ptid; /* ? ? ? */
37de36c6
KB
4078 }
4079 else
4080 {
4081 int temp = wait (&wstat);
4082
4083 /* FIXME: shouldn't I make sure I get the right
4084 event from the right process? If (for
4085 instance) I have killed an earlier inferior
4086 process but failed to clean up after it
4087 somehow, I could get its termination event
4088 here. */
4089
4090 /* If wait returns -1, that's what we return to GDB. */
4091 if (temp < 0)
39f77062 4092 retval = pid_to_ptid (temp);
c3f6f71d 4093 }
c3f6f71d 4094 }
37de36c6
KB
4095 else
4096 {
4097 printf_filtered ("procfs: trapped on entry to ");
4098 proc_prettyprint_syscall (proc_what (pi), 0);
4099 printf_filtered ("\n");
4100#ifndef PIOCSSPCACT
c3f6f71d 4101 {
37de36c6 4102 long i, nsysargs, *sysargs;
c3f6f71d 4103
37de36c6
KB
4104 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4105 (sysargs = proc_sysargs (pi)) != NULL)
4106 {
4107 printf_filtered ("%ld syscall arguments:\n", nsysargs);
4108 for (i = 0; i < nsysargs; i++)
4109 printf_filtered ("#%ld: 0x%08lx\n",
4110 i, sysargs[i]);
4111 }
c3f6f71d 4112
c3f6f71d 4113 }
c3f6f71d 4114#endif
37de36c6
KB
4115 if (status)
4116 {
4117 /* How to exit gracefully, returning "unknown event" */
4118 status->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 4119 return inferior_ptid;
37de36c6
KB
4120 }
4121 else
4122 {
4123 /* How to keep going without returning to wfi: */
39f77062 4124 target_resume (ptid, 0, TARGET_SIGNAL_0);
37de36c6
KB
4125 goto wait_again;
4126 }
4127 }
4128 break;
4129 case PR_SYSEXIT:
4130 if (syscall_is_exec (pi, what))
c3f6f71d 4131 {
37de36c6
KB
4132 /* Hopefully this is our own "fork-child" execing
4133 the real child. Hoax this event into a trap, and
4134 GDB will see the child about to execute its start
4135 address. */
4136 wstat = (SIGTRAP << 8) | 0177;
4137 }
4138 else if (syscall_is_lwp_create (pi, what))
4139 {
4140 /*
4141 * This syscall is somewhat like fork/exec.
4142 * We will get the event twice: once for the parent LWP,
4143 * and once for the child. We should already know about
4144 * the parent LWP, but the child will be new to us. So,
4145 * whenever we get this event, if it represents a new
4146 * thread, simply add the thread to the list.
4147 */
c3f6f71d 4148
37de36c6 4149 /* If not in procinfo list, add it. */
39f77062
KB
4150 temp_tid = proc_get_current_thread (pi);
4151 if (!find_procinfo (pi->pid, temp_tid))
4152 create_procinfo (pi->pid, temp_tid);
37de36c6 4153
39f77062 4154 temp_ptid = MERGEPID (pi->pid, temp_tid);
37de36c6 4155 /* If not in GDB's thread list, add it. */
39f77062 4156 if (!in_thread_list (temp_ptid))
c3f6f71d 4157 {
39f77062
KB
4158 printf_filtered ("[New %s]\n",
4159 target_pid_to_str (temp_ptid));
4160 add_thread (temp_ptid);
c3f6f71d 4161 }
37de36c6
KB
4162 /* Return to WFI, but tell it to immediately resume. */
4163 status->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 4164 return inferior_ptid;
37de36c6
KB
4165 }
4166 else if (syscall_is_lwp_exit (pi, what))
4167 {
4168 printf_filtered ("[%s exited]\n",
4169 target_pid_to_str (retval));
4170 delete_thread (retval);
4171 status->kind = TARGET_WAITKIND_SPURIOUS;
4172 return retval;
c3f6f71d 4173 }
37de36c6
KB
4174 else if (0)
4175 {
4176 /* FIXME: Do we need to handle SYS_sproc,
4177 SYS_fork, or SYS_vfork here? The old procfs
4178 seemed to use this event to handle threads on
4179 older (non-LWP) systems, where I'm assuming
4180 that threads were actually separate processes.
4181 Irix, maybe? Anyway, low priority for now. */
4182 }
4183 else
4184 {
4185 printf_filtered ("procfs: trapped on exit from ");
4186 proc_prettyprint_syscall (proc_what (pi), 0);
4187 printf_filtered ("\n");
4188#ifndef PIOCSSPCACT
4189 {
4190 long i, nsysargs, *sysargs;
4191
4192 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4193 (sysargs = proc_sysargs (pi)) != NULL)
4194 {
4195 printf_filtered ("%ld syscall arguments:\n", nsysargs);
4196 for (i = 0; i < nsysargs; i++)
4197 printf_filtered ("#%ld: 0x%08lx\n",
4198 i, sysargs[i]);
4199 }
4200 }
c3f6f71d 4201#endif
37de36c6 4202 status->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 4203 return inferior_ptid;
37de36c6 4204 }
c3f6f71d
JM
4205 break;
4206 case PR_REQUESTED:
4207#if 0 /* FIXME */
4208 wstat = (SIGSTOP << 8) | 0177;
4209 break;
4210#else
4211 if (retry < 5)
4212 {
4213 printf_filtered ("Retry #%d:\n", retry);
4214 pi->status_valid = 0;
4215 goto wait_again;
4216 }
4217 else
4218 {
4219 /* If not in procinfo list, add it. */
39f77062
KB
4220 temp_tid = proc_get_current_thread (pi);
4221 if (!find_procinfo (pi->pid, temp_tid))
4222 create_procinfo (pi->pid, temp_tid);
c3f6f71d
JM
4223
4224 /* If not in GDB's thread list, add it. */
39f77062
KB
4225 temp_ptid = MERGEPID (pi->pid, temp_tid);
4226 if (!in_thread_list (temp_ptid))
c3f6f71d 4227 {
0d06e24b 4228 printf_filtered ("[New %s]\n",
39f77062
KB
4229 target_pid_to_str (temp_ptid));
4230 add_thread (temp_ptid);
c3f6f71d
JM
4231 }
4232
4233 status->kind = TARGET_WAITKIND_STOPPED;
4234 status->value.sig = 0;
4235 return retval;
4236 }
4237#endif
4238 case PR_JOBCONTROL:
4239 wstat = (what << 8) | 0177;
4240 break;
4241 case PR_FAULTED:
4242 switch (what) { /* FIXME: FAULTED_USE_SIGINFO */
4243#ifdef FLTWATCH
4244 case FLTWATCH:
4245 wstat = (SIGTRAP << 8) | 0177;
4246 break;
4247#endif
4248#ifdef FLTKWATCH
4249 case FLTKWATCH:
4250 wstat = (SIGTRAP << 8) | 0177;
4251 break;
4252#endif
4253 /* FIXME: use si_signo where possible. */
4254 case FLTPRIV:
4255#if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4256 case FLTILL:
4257#endif
4258 wstat = (SIGILL << 8) | 0177;
4259 break;
4260 case FLTBPT:
4261#if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4262 case FLTTRACE:
4263#endif
4264 wstat = (SIGTRAP << 8) | 0177;
4265 break;
4266 case FLTSTACK:
4267 case FLTACCESS:
4268#if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4269 case FLTBOUNDS:
4270#endif
4271 wstat = (SIGSEGV << 8) | 0177;
4272 break;
4273 case FLTIOVF:
4274 case FLTIZDIV:
4275#if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4276 case FLTFPE:
4277#endif
4278 wstat = (SIGFPE << 8) | 0177;
4279 break;
4280 case FLTPAGE: /* Recoverable page fault */
4281 default: /* FIXME: use si_signo if possible for fault */
39f77062 4282 retval = pid_to_ptid (-1);
c3f6f71d
JM
4283 printf_filtered ("procfs:%d -- ", __LINE__);
4284 printf_filtered ("child stopped for unknown reason:\n");
4285 proc_prettyprint_why (why, what, 1);
4286 error ("... giving up...");
4287 break;
4288 }
4289 break; /* case PR_FAULTED: */
4290 default: /* switch (why) unmatched */
4291 printf_filtered ("procfs:%d -- ", __LINE__);
4292 printf_filtered ("child stopped for unknown reason:\n");
4293 proc_prettyprint_why (why, what, 1);
4294 error ("... giving up...");
4295 break;
4296 }
4297 /*
4298 * Got this far without error:
4299 * If retval isn't in the threads database, add it.
4300 */
39f77062
KB
4301 if (PIDGET (retval) > 0 &&
4302 !ptid_equal (retval, inferior_ptid) &&
c3f6f71d 4303 !in_thread_list (retval))
c906108c 4304 {
c3f6f71d
JM
4305 /*
4306 * We have a new thread.
4307 * We need to add it both to GDB's list and to our own.
4308 * If we don't create a procinfo, resume may be unhappy
4309 * later.
4310 */
4311 printf_filtered ("[New %s]\n", target_pid_to_str (retval));
4312 add_thread (retval);
4313 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4314 create_procinfo (PIDGET (retval), TIDGET (retval));
4315
4316 /* In addition, it's possible that this is the first
4317 * new thread we've seen, in which case we may not
39f77062 4318 * have created entries for inferior_ptid yet.
c3f6f71d 4319 */
39f77062 4320 if (TIDGET (inferior_ptid) != 0)
c3f6f71d 4321 {
39f77062
KB
4322 if (!in_thread_list (inferior_ptid))
4323 add_thread (inferior_ptid);
4324 if (find_procinfo (PIDGET (inferior_ptid),
4325 TIDGET (inferior_ptid)) == NULL)
4326 create_procinfo (PIDGET (inferior_ptid),
4327 TIDGET (inferior_ptid));
c3f6f71d 4328 }
c906108c 4329 }
c906108c 4330 }
c3f6f71d 4331 else /* flags do not indicate STOPPED */
c906108c 4332 {
c3f6f71d
JM
4333 /* surely this can't happen... */
4334 printf_filtered ("procfs:%d -- process not stopped.\n",
4335 __LINE__);
4336 proc_prettyprint_flags (flags, 1);
4337 error ("procfs: ...giving up...");
c906108c 4338 }
c906108c 4339 }
c906108c 4340
c3f6f71d
JM
4341 if (status)
4342 store_waitstatus (status, wstat);
c906108c
SS
4343 }
4344
c3f6f71d
JM
4345 return retval;
4346}
c906108c 4347
d0849a9a
KB
4348/* Transfer LEN bytes between GDB address MYADDR and target address
4349 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4350 otherwise transfer them from the target. TARGET is unused.
4351
4352 The return value is 0 if an error occurred or no bytes were
4353 transferred. Otherwise, it will be a positive value which
4354 indicates the number of bytes transferred between gdb and the
4355 target. (Note that the interface also makes provisions for
4356 negative values, but this capability isn't implemented here.) */
4357
c3f6f71d 4358static int
d0849a9a 4359procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
37de36c6 4360 struct mem_attrib *attrib, struct target_ops *target)
c3f6f71d
JM
4361{
4362 procinfo *pi;
4363 int nbytes = 0;
c906108c 4364
c3f6f71d 4365 /* Find procinfo for main process */
39f77062 4366 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
4367 if (pi->as_fd == 0 &&
4368 open_procinfo_files (pi, FD_AS) == 0)
c906108c 4369 {
c3f6f71d
JM
4370 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4371 return 0;
c906108c 4372 }
c906108c 4373
c3f6f71d 4374 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
c906108c 4375 {
c3f6f71d 4376 if (dowrite)
c906108c 4377 {
c3f6f71d
JM
4378#ifdef NEW_PROC_API
4379 PROCFS_NOTE ("write memory: ");
c906108c 4380#else
c3f6f71d 4381 PROCFS_NOTE ("write memory: \n");
c906108c 4382#endif
c3f6f71d 4383 nbytes = write (pi->as_fd, myaddr, len);
c906108c 4384 }
c3f6f71d 4385 else
c906108c 4386 {
c3f6f71d
JM
4387 PROCFS_NOTE ("read memory: \n");
4388 nbytes = read (pi->as_fd, myaddr, len);
c906108c 4389 }
c3f6f71d 4390 if (nbytes < 0)
c906108c 4391 {
c3f6f71d 4392 nbytes = 0;
c906108c 4393 }
c906108c 4394 }
c3f6f71d 4395 return nbytes;
c906108c
SS
4396}
4397
4398/*
c3f6f71d
JM
4399 * Function: invalidate_cache
4400 *
4401 * Called by target_resume before making child runnable.
4402 * Mark cached registers and status's invalid.
4403 * If there are "dirty" caches that need to be written back
4404 * to the child process, do that.
4405 *
4406 * File descriptors are also cached.
4407 * As they are a limited resource, we cannot hold onto them indefinitely.
4408 * However, as they are expensive to open, we don't want to throw them
4409 * away indescriminately either. As a compromise, we will keep the
4410 * file descriptors for the parent process, but discard any file
4411 * descriptors we may have accumulated for the threads.
4412 *
4413 * Return value:
4414 * As this function is called by iterate_over_threads, it always
4415 * returns zero (so that iterate_over_threads will keep iterating).
c906108c
SS
4416 */
4417
c3f6f71d
JM
4418
4419static int
fba45db2 4420invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
c906108c 4421{
c3f6f71d
JM
4422 /*
4423 * About to run the child; invalidate caches and do any other cleanup.
4424 */
c906108c 4425
c3f6f71d
JM
4426#if 0
4427 if (pi->gregs_dirty)
4428 if (parent == NULL ||
4429 proc_get_current_thread (parent) != pi->tid)
4430 if (!proc_set_gregs (pi)) /* flush gregs cache */
4431 proc_warn (pi, "target_resume, set_gregs",
4432 __LINE__);
60054393
MS
4433 if (FP0_REGNUM >= 0)
4434 if (pi->fpregs_dirty)
4435 if (parent == NULL ||
4436 proc_get_current_thread (parent) != pi->tid)
4437 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4438 proc_warn (pi, "target_resume, set_fpregs",
4439 __LINE__);
c906108c 4440#endif
c906108c 4441
c3f6f71d 4442 if (parent != NULL)
c906108c 4443 {
c3f6f71d
JM
4444 /* The presence of a parent indicates that this is an LWP.
4445 Close any file descriptors that it might have open.
4446 We don't do this to the master (parent) procinfo. */
4447
4448 close_procinfo_files (pi);
c906108c 4449 }
c3f6f71d
JM
4450 pi->gregs_valid = 0;
4451 pi->fpregs_valid = 0;
4452#if 0
4453 pi->gregs_dirty = 0;
4454 pi->fpregs_dirty = 0;
c906108c 4455#endif
c3f6f71d
JM
4456 pi->status_valid = 0;
4457 pi->threads_valid = 0;
c906108c 4458
c3f6f71d 4459 return 0;
c906108c
SS
4460}
4461
0fda6bd2 4462#if 0
c906108c 4463/*
c3f6f71d
JM
4464 * Function: make_signal_thread_runnable
4465 *
4466 * A callback function for iterate_over_threads.
4467 * Find the asynchronous signal thread, and make it runnable.
4468 * See if that helps matters any.
c906108c
SS
4469 */
4470
c3f6f71d 4471static int
fba45db2 4472make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
c906108c 4473{
c3f6f71d
JM
4474#ifdef PR_ASLWP
4475 if (proc_flags (pi) & PR_ASLWP)
c906108c 4476 {
c3f6f71d
JM
4477 if (!proc_run_process (pi, 0, -1))
4478 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4479 return 1;
c906108c 4480 }
c906108c 4481#endif
c3f6f71d 4482 return 0;
c906108c 4483}
0fda6bd2 4484#endif
c906108c
SS
4485
4486/*
c3f6f71d
JM
4487 * Function: target_resume
4488 *
4489 * Make the child process runnable. Normally we will then call
4490 * procfs_wait and wait for it to stop again (unles gdb is async).
4491 *
4492 * Arguments:
4493 * step: if true, then arrange for the child to stop again
4494 * after executing a single instruction.
4495 * signo: if zero, then cancel any pending signal.
4496 * If non-zero, then arrange for the indicated signal
4497 * to be delivered to the child when it runs.
4498 * pid: if -1, then allow any child thread to run.
4499 * if non-zero, then allow only the indicated thread to run.
4500 ******* (not implemented yet)
c906108c
SS
4501 */
4502
4503static void
39f77062 4504procfs_resume (ptid_t ptid, int step, enum target_signal signo)
c906108c 4505{
c3f6f71d
JM
4506 procinfo *pi, *thread;
4507 int native_signo;
4508
4509 /* 2.1:
4510 prrun.prflags |= PRSVADDR;
4511 prrun.pr_vaddr = $PC; set resume address
4512 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4513 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4514 prrun.prflags |= PRCFAULT; clear current fault.
4515
4516 PRSTRACE and PRSFAULT can be done by other means
4517 (proc_trace_signals, proc_trace_faults)
4518 PRSVADDR is unnecessary.
4519 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4520 This basically leaves PRSTEP and PRCSIG.
4521 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4522 So basically PR_STEP is the sole argument that must be passed
4523 to proc_run_process (for use in the prrun struct by ioctl). */
4524
4525 /* Find procinfo for main process */
39f77062 4526 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
4527
4528 /* First cut: ignore pid argument */
4529 errno = 0;
c906108c 4530
c3f6f71d
JM
4531 /* Convert signal to host numbering. */
4532 if (signo == 0 ||
0fda6bd2 4533 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
c3f6f71d
JM
4534 native_signo = 0;
4535 else
4536 native_signo = target_signal_to_host (signo);
c906108c 4537
c3f6f71d 4538 pi->ignore_next_sigstop = 0;
c906108c 4539
c3f6f71d
JM
4540 /* Running the process voids all cached registers and status. */
4541 /* Void the threads' caches first */
4542 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4543 /* Void the process procinfo's caches. */
4544 invalidate_cache (NULL, pi, NULL);
c906108c 4545
39f77062 4546 if (PIDGET (ptid) != -1)
c906108c 4547 {
c3f6f71d 4548 /* Resume a specific thread, presumably suppressing the others. */
39f77062 4549 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
c3f6f71d 4550 if (thread == NULL)
39f77062
KB
4551 warning ("procfs: resume can't find thread %ld -- resuming all.",
4552 TIDGET (ptid));
c3f6f71d 4553 else
c906108c 4554 {
c3f6f71d
JM
4555 if (thread->tid != 0)
4556 {
4557 /* We're to resume a specific thread, and not the others.
4558 * Set the child process's PR_ASYNC flag.
4559 */
4560#ifdef PR_ASYNC
4561 if (!proc_set_async (pi))
4562 proc_error (pi, "target_resume, set_async", __LINE__);
4563#endif
4564#if 0
4565 proc_iterate_over_threads (pi,
4566 make_signal_thread_runnable,
4567 NULL);
4568#endif
4569 pi = thread; /* substitute the thread's procinfo for run */
4570 }
c906108c
SS
4571 }
4572 }
c906108c 4573
c3f6f71d 4574 if (!proc_run_process (pi, step, native_signo))
c906108c 4575 {
c3f6f71d
JM
4576 if (errno == EBUSY)
4577 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4578 else
4579 proc_error (pi, "target_resume", __LINE__);
c906108c 4580 }
c3f6f71d 4581}
c906108c 4582
c3f6f71d
JM
4583/*
4584 * Function: register_gdb_signals
4585 *
4586 * Traverse the list of signals that GDB knows about
4587 * (see "handle" command), and arrange for the target
4588 * to be stopped or not, according to these settings.
4589 *
4590 * Returns non-zero for success, zero for failure.
4591 */
c906108c 4592
c3f6f71d 4593static int
37de36c6 4594register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
c3f6f71d
JM
4595{
4596 int signo;
c906108c 4597
c3f6f71d
JM
4598 for (signo = 0; signo < NSIG; signo ++)
4599 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4600 signal_print_state (target_signal_from_host (signo)) == 0 &&
4601 signal_pass_state (target_signal_from_host (signo)) == 1)
4602 prdelset (signals, signo);
4603 else
4604 praddset (signals, signo);
c906108c 4605
c3f6f71d 4606 return proc_set_traced_signals (pi, signals);
c906108c
SS
4607}
4608
4609/*
c3f6f71d
JM
4610 * Function: target_notice_signals
4611 *
4612 * Set up to trace signals in the child process.
4613 */
c906108c 4614
c3f6f71d 4615static void
39f77062 4616procfs_notice_signals (ptid_t ptid)
c3f6f71d 4617{
37de36c6 4618 gdb_sigset_t signals;
39f77062 4619 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
c906108c 4620
c3f6f71d
JM
4621 if (proc_get_traced_signals (pi, &signals) &&
4622 register_gdb_signals (pi, &signals))
4623 return;
4624 else
4625 proc_error (pi, "notice_signals", __LINE__);
4626}
c906108c 4627
c3f6f71d
JM
4628/*
4629 * Function: target_files_info
4630 *
4631 * Print status information about the child process.
4632 */
c906108c 4633
c3f6f71d 4634static void
fba45db2 4635procfs_files_info (struct target_ops *ignore)
c3f6f71d
JM
4636{
4637 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4638 attach_flag? "attached": "child",
39f77062 4639 target_pid_to_str (inferior_ptid));
c3f6f71d 4640}
c906108c 4641
c3f6f71d
JM
4642/*
4643 * Function: target_open
4644 *
4645 * A dummy: you don't open procfs.
c906108c
SS
4646 */
4647
4648static void
fba45db2 4649procfs_open (char *args, int from_tty)
c906108c 4650{
c3f6f71d
JM
4651 error ("Use the \"run\" command to start a Unix child process.");
4652}
c906108c 4653
c3f6f71d
JM
4654/*
4655 * Function: target_can_run
4656 *
4657 * This tells GDB that this target vector can be invoked
4658 * for "run" or "attach".
4659 */
c906108c 4660
c3f6f71d
JM
4661int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
4662 be a runnable target. Used by targets
4663 that can sit atop procfs, such as solaris
4664 thread support. */
c906108c 4665
c906108c 4666
c3f6f71d 4667static int
fba45db2 4668procfs_can_run (void)
c3f6f71d
JM
4669{
4670 /* This variable is controlled by modules that sit atop procfs that
4671 may layer their own process structure atop that provided here.
4672 sol-thread.c does this because of the Solaris two-level thread
4673 model. */
4674
4675 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
c906108c 4676
c3f6f71d
JM
4677 return !procfs_suppress_run;
4678}
c906108c 4679
c3f6f71d
JM
4680/*
4681 * Function: target_stop
4682 *
4683 * Stop the child process asynchronously, as when the
4684 * gdb user types control-c or presses a "stop" button.
4685 *
4686 * Works by sending kill(SIGINT) to the child's process group.
4687 */
c906108c 4688
c3f6f71d 4689static void
fba45db2 4690procfs_stop (void)
c3f6f71d
JM
4691{
4692 extern pid_t inferior_process_group;
c906108c 4693
c3f6f71d 4694 kill (-inferior_process_group, SIGINT);
c906108c
SS
4695}
4696
c906108c 4697/*
c3f6f71d
JM
4698 * Function: unconditionally_kill_inferior
4699 *
4700 * Make it die. Wait for it to die. Clean up after it.
4701 * Note: this should only be applied to the real process,
4702 * not to an LWP, because of the check for parent-process.
4703 * If we need this to work for an LWP, it needs some more logic.
4704 */
c906108c 4705
c3f6f71d 4706static void
fba45db2 4707unconditionally_kill_inferior (procinfo *pi)
c3f6f71d
JM
4708{
4709 int parent_pid;
c906108c 4710
c3f6f71d
JM
4711 parent_pid = proc_parent_pid (pi);
4712#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4713 /* FIXME: use access functions */
4714 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4715 before the PIOCKILL, otherwise it might generate a corrupted core
4716 file for the inferior. */
4717 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4718 {
4719 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4720 }
4721#endif
4722#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4723 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4724 to kill the inferior, otherwise it might remain stopped with a
4725 pending SIGKILL.
4726 We do not check the result of the PIOCSSIG, the inferior might have
4727 died already. */
4728 {
37de36c6 4729 gdb_siginfo_t newsiginfo;
c906108c 4730
c3f6f71d
JM
4731 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4732 newsiginfo.si_signo = SIGKILL;
4733 newsiginfo.si_code = 0;
4734 newsiginfo.si_errno = 0;
4735 newsiginfo.si_pid = getpid ();
4736 newsiginfo.si_uid = getuid ();
4737 /* FIXME: use proc_set_current_signal */
4738 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4739 }
4740#else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4741 if (!proc_kill (pi, SIGKILL))
103b3ef5 4742 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
c3f6f71d
JM
4743#endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4744 destroy_procinfo (pi);
c906108c 4745
c3f6f71d
JM
4746 /* If pi is GDB's child, wait for it to die. */
4747 if (parent_pid == getpid ())
4748 /* FIXME: should we use waitpid to make sure we get the right event?
4749 Should we check the returned event? */
4750 {
0d06e24b 4751#if 0
c3f6f71d 4752 int status, ret;
c906108c 4753
c3f6f71d
JM
4754 ret = waitpid (pi->pid, &status, 0);
4755#else
4756 wait (NULL);
4757#endif
4758 }
4759}
c906108c 4760
c3f6f71d
JM
4761/*
4762 * Function: target_kill_inferior
4763 *
4764 * We're done debugging it, and we want it to go away.
4765 * Then we want GDB to forget all about it.
c906108c
SS
4766 */
4767
c3f6f71d 4768static void
fba45db2 4769procfs_kill_inferior (void)
c906108c 4770{
39f77062 4771 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
c3f6f71d
JM
4772 {
4773 /* Find procinfo for main process */
39f77062 4774 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
c906108c 4775
c3f6f71d
JM
4776 if (pi)
4777 unconditionally_kill_inferior (pi);
4778 target_mourn_inferior ();
c906108c 4779 }
c3f6f71d
JM
4780}
4781
4782/*
4783 * Function: target_mourn_inferior
4784 *
4785 * Forget we ever debugged this thing!
4786 */
c906108c 4787
c3f6f71d 4788static void
fba45db2 4789procfs_mourn_inferior (void)
c3f6f71d
JM
4790{
4791 procinfo *pi;
c906108c 4792
39f77062 4793 if (!ptid_equal (inferior_ptid, null_ptid))
c3f6f71d
JM
4794 {
4795 /* Find procinfo for main process */
39f77062 4796 pi = find_procinfo (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
4797 if (pi)
4798 destroy_procinfo (pi);
c906108c 4799 }
c3f6f71d
JM
4800 unpush_target (&procfs_ops);
4801 generic_mourn_inferior ();
4802}
c906108c 4803
c3f6f71d
JM
4804/*
4805 * Function: init_inferior
4806 *
4807 * When GDB forks to create a runnable inferior process,
4808 * this function is called on the parent side of the fork.
4809 * It's job is to do whatever is necessary to make the child
4810 * ready to be debugged, and then wait for the child to synchronize.
4811 */
c906108c 4812
c3f6f71d 4813static void
fba45db2 4814procfs_init_inferior (int pid)
c3f6f71d
JM
4815{
4816 procinfo *pi;
37de36c6 4817 gdb_sigset_t signals;
c3f6f71d 4818 int fail;
c906108c 4819
c3f6f71d
JM
4820 /* This routine called on the parent side (GDB side)
4821 after GDB forks the inferior. */
c906108c 4822
c3f6f71d 4823 push_target (&procfs_ops);
c906108c 4824
c3f6f71d
JM
4825 if ((pi = create_procinfo (pid, 0)) == NULL)
4826 perror ("procfs: out of memory in 'init_inferior'");
4827
4828 if (!open_procinfo_files (pi, FD_CTL))
4829 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4830
4831 /*
4832 xmalloc // done
4833 open_procinfo_files // done
4834 link list // done
4835 prfillset (trace)
4836 procfs_notice_signals
4837 prfillset (fault)
4838 prdelset (FLTPAGE)
4839 PIOCWSTOP
4840 PIOCSFAULT
4841 */
4842
4843 /* If not stopped yet, wait for it to stop. */
4844 if (!(proc_flags (pi) & PR_STOPPED) &&
4845 !(proc_wait_for_stop (pi)))
4846 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4847
4848 /* Save some of the /proc state to be restored if we detach. */
4849 /* FIXME: Why? In case another debugger was debugging it?
4850 We're it's parent, for Ghu's sake! */
4851 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4852 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4853 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4854 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4855 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4856 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
37de36c6 4857 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
c3f6f71d 4858 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
37de36c6 4859 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
c3f6f71d
JM
4860 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4861
4862 /* Register to trace selected signals in the child. */
4863 prfillset (&signals);
4864 if (!register_gdb_signals (pi, &signals))
4865 proc_error (pi, "init_inferior, register_signals", __LINE__);
4866
4867 if ((fail = procfs_debug_inferior (pi)) != 0)
4868 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4869
0d06e24b
JM
4870 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4871 and possibly even turning ON kill-on-last-close at this point. But
4872 I can't make that change without careful testing which I don't have
4873 time to do right now... */
c3f6f71d
JM
4874 /* Turn on run-on-last-close flag so that the child
4875 will die if GDB goes away for some reason. */
4876 if (!proc_set_run_on_last_close (pi))
4877 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4878
4879 /* The 'process ID' we return to GDB is composed of
4880 the actual process ID plus the lwp ID. */
39f77062 4881 inferior_ptid = MERGEPID (pi->pid, proc_get_current_thread (pi));
c906108c 4882
c3f6f71d
JM
4883#ifdef START_INFERIOR_TRAPS_EXPECTED
4884 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4885#else
4886 /* One trap to exec the shell, one to exec the program being debugged. */
4887 startup_inferior (2);
0d06e24b 4888#endif /* START_INFERIOR_TRAPS_EXPECTED */
c3f6f71d 4889}
c906108c 4890
c3f6f71d
JM
4891/*
4892 * Function: set_exec_trap
4893 *
4894 * When GDB forks to create a new process, this function is called
4895 * on the child side of the fork before GDB exec's the user program.
4896 * Its job is to make the child minimally debuggable, so that the
4897 * parent GDB process can connect to the child and take over.
4898 * This function should do only the minimum to make that possible,
4899 * and to synchronize with the parent process. The parent process
4900 * should take care of the details.
4901 */
4902
4903static void
fba45db2 4904procfs_set_exec_trap (void)
c3f6f71d
JM
4905{
4906 /* This routine called on the child side (inferior side)
4907 after GDB forks the inferior. It must use only local variables,
4908 because it may be sharing data space with its parent. */
c906108c 4909
c3f6f71d 4910 procinfo *pi;
37de36c6 4911 sysset_t *exitset;
c906108c 4912
c3f6f71d
JM
4913 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4914 perror_with_name ("procfs: create_procinfo failed in child.");
c906108c 4915
c3f6f71d
JM
4916 if (open_procinfo_files (pi, FD_CTL) == 0)
4917 {
4918 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4919 gdb_flush (gdb_stderr);
4920 /* no need to call "dead_procinfo", because we're going to exit. */
4921 _exit (127);
4922 }
c906108c 4923
c3f6f71d
JM
4924#ifdef PRFS_STOPEXEC /* defined on OSF */
4925 /* OSF method for tracing exec syscalls. Quoting:
4926 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4927 exits from exec system calls because of the user level loader. */
4928 /* FIXME: make nice and maybe move into an access function. */
4929 {
4930 int prfs_flags;
c906108c 4931
c3f6f71d
JM
4932 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4933 {
4934 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4935 gdb_flush (gdb_stderr);
4936 _exit (127);
4937 }
4938 prfs_flags |= PRFS_STOPEXEC;
c906108c 4939
c3f6f71d
JM
4940 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4941 {
4942 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4943 gdb_flush (gdb_stderr);
4944 _exit (127);
4945 }
4946 }
4947#else /* not PRFS_STOPEXEC */
4948 /* Everyone else's (except OSF) method for tracing exec syscalls */
4949 /* GW: Rationale...
4950 Not all systems with /proc have all the exec* syscalls with the same
4951 names. On the SGI, for example, there is no SYS_exec, but there
4952 *is* a SYS_execv. So, we try to account for that. */
c906108c 4953
37de36c6
KB
4954 exitset = sysset_t_alloc (pi);
4955 gdb_premptysysset (exitset);
c3f6f71d 4956#ifdef SYS_exec
37de36c6 4957 gdb_praddsysset (exitset, SYS_exec);
c3f6f71d
JM
4958#endif
4959#ifdef SYS_execve
37de36c6 4960 gdb_praddsysset (exitset, SYS_execve);
c3f6f71d
JM
4961#endif
4962#ifdef SYS_execv
37de36c6 4963 gdb_praddsysset (exitset, SYS_execv);
c906108c 4964#endif
37de36c6
KB
4965#ifdef DYNAMIC_SYSCALLS
4966 {
4967 int callnum = find_syscall (pi, "execve");
4968
4969 if (callnum >= 0)
4970 gdb_praddsysset (exitset, callnum);
c906108c 4971
37de36c6
KB
4972 callnum = find_syscall (pi, "ra_execve");
4973 if (callnum >= 0)
4974 gdb_praddsysset (exitset, callnum);
4975 }
4976#endif /* DYNAMIC_SYSCALLS */
4977
4978 if (!proc_set_traced_sysexit (pi, exitset))
c906108c 4979 {
c3f6f71d
JM
4980 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4981 gdb_flush (gdb_stderr);
4982 _exit (127);
c906108c 4983 }
c3f6f71d
JM
4984#endif /* PRFS_STOPEXEC */
4985
4986 /* FIXME: should this be done in the parent instead? */
4987 /* Turn off inherit on fork flag so that all grand-children
4988 of gdb start with tracing flags cleared. */
4989 if (!proc_unset_inherit_on_fork (pi))
4990 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4991
4992 /* Turn off run on last close flag, so that the child process
4993 cannot run away just because we close our handle on it.
4994 We want it to wait for the parent to attach. */
4995 if (!proc_unset_run_on_last_close (pi))
4996 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4997
4998 /* FIXME: No need to destroy the procinfo --
4999 we have our own address space, and we're about to do an exec! */
5000 /*destroy_procinfo (pi);*/
c906108c 5001}
c906108c 5002
c3f6f71d
JM
5003/*
5004 * Function: create_inferior
5005 *
5006 * This function is called BEFORE gdb forks the inferior process.
5007 * Its only real responsibility is to set things up for the fork,
5008 * and tell GDB which two functions to call after the fork (one
5009 * for the parent, and one for the child).
5010 *
5011 * This function does a complicated search for a unix shell program,
5012 * which it then uses to parse arguments and environment variables
5013 * to be sent to the child. I wonder whether this code could not
5014 * be abstracted out and shared with other unix targets such as
5015 * infptrace?
5016 */
c906108c
SS
5017
5018static void
fba45db2 5019procfs_create_inferior (char *exec_file, char *allargs, char **env)
c906108c
SS
5020{
5021 char *shell_file = getenv ("SHELL");
5022 char *tryname;
5023 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
5024 {
5025
5026 /* We will be looking down the PATH to find shell_file. If we
c3f6f71d
JM
5027 just do this the normal way (via execlp, which operates by
5028 attempting an exec for each element of the PATH until it
5029 finds one which succeeds), then there will be an exec for
5030 each failed attempt, each of which will cause a PR_SYSEXIT
5031 stop, and we won't know how to distinguish the PR_SYSEXIT's
5032 for these failed execs with the ones for successful execs
5033 (whether the exec has succeeded is stored at that time in the
5034 carry bit or some such architecture-specific and
5035 non-ABI-specified place).
5036
5037 So I can't think of anything better than to search the PATH
5038 now. This has several disadvantages: (1) There is a race
5039 condition; if we find a file now and it is deleted before we
5040 exec it, we lose, even if the deletion leaves a valid file
5041 further down in the PATH, (2) there is no way to know exactly
5042 what an executable (in the sense of "capable of being
5043 exec'd") file is. Using access() loses because it may lose
5044 if the caller is the superuser; failing to use it loses if
5045 there are ACLs or some such. */
c906108c
SS
5046
5047 char *p;
5048 char *p1;
5049 /* FIXME-maybe: might want "set path" command so user can change what
c3f6f71d 5050 path is used from within GDB. */
c906108c
SS
5051 char *path = getenv ("PATH");
5052 int len;
5053 struct stat statbuf;
5054
5055 if (path == NULL)
5056 path = "/bin:/usr/bin";
5057
5058 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
c3f6f71d 5059 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
c906108c
SS
5060 {
5061 p1 = strchr (p, ':');
5062 if (p1 != NULL)
5063 len = p1 - p;
5064 else
5065 len = strlen (p);
5066 strncpy (tryname, p, len);
5067 tryname[len] = '\0';
5068 strcat (tryname, "/");
5069 strcat (tryname, shell_file);
5070 if (access (tryname, X_OK) < 0)
5071 continue;
5072 if (stat (tryname, &statbuf) < 0)
5073 continue;
5074 if (!S_ISREG (statbuf.st_mode))
5075 /* We certainly need to reject directories. I'm not quite
5076 as sure about FIFOs, sockets, etc., but I kind of doubt
5077 that people want to exec() these things. */
5078 continue;
5079 break;
5080 }
5081 if (p == NULL)
5082 /* Not found. This must be an error rather than merely passing
5083 the file to execlp(), because execlp() would try all the
5084 exec()s, causing GDB to get confused. */
c3f6f71d
JM
5085 error ("procfs:%d -- Can't find shell %s in PATH",
5086 __LINE__, shell_file);
c906108c
SS
5087
5088 shell_file = tryname;
5089 }
5090
c3f6f71d
JM
5091 fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5092 procfs_init_inferior, NULL, shell_file);
c906108c
SS
5093
5094 /* We are at the first instruction we care about. */
5095 /* Pedal to the metal... */
5096
2acceee2 5097 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
c906108c
SS
5098}
5099
c3f6f71d
JM
5100/*
5101 * Function: notice_thread
5102 *
5103 * Callback for find_new_threads.
5104 * Calls "add_thread".
5105 */
c906108c 5106
c3f6f71d 5107static int
fba45db2 5108procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
c906108c 5109{
39f77062 5110 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
c906108c 5111
c3f6f71d
JM
5112 if (!in_thread_list (gdb_threadid))
5113 add_thread (gdb_threadid);
c906108c 5114
c3f6f71d
JM
5115 return 0;
5116}
5117
5118/*
5119 * Function: target_find_new_threads
5120 *
5121 * Query all the threads that the target knows about,
5122 * and give them back to GDB to add to its list.
5123 */
5124
5125void
fba45db2 5126procfs_find_new_threads (void)
c3f6f71d
JM
5127{
5128 procinfo *pi;
5129
5130 /* Find procinfo for main process */
39f77062 5131 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
5132 proc_update_threads (pi);
5133 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
c906108c
SS
5134}
5135
c3f6f71d
JM
5136/*
5137 * Function: target_thread_alive
5138 *
5139 * Return true if the thread is still 'alive'.
5140 *
5141 * This guy doesn't really seem to be doing his job.
5142 * Got to investigate how to tell when a thread is really gone.
5143 */
c906108c 5144
c906108c 5145static int
39f77062 5146procfs_thread_alive (ptid_t ptid)
c906108c 5147{
c3f6f71d
JM
5148 int proc, thread;
5149 procinfo *pi;
c906108c 5150
39f77062
KB
5151 proc = PIDGET (ptid);
5152 thread = TIDGET (ptid);
c3f6f71d
JM
5153 /* If I don't know it, it ain't alive! */
5154 if ((pi = find_procinfo (proc, thread)) == NULL)
5155 return 0;
5156
5157 /* If I can't get its status, it ain't alive!
5158 What's more, I need to forget about it! */
5159 if (!proc_get_status (pi))
5160 {
5161 destroy_procinfo (pi);
5162 return 0;
5163 }
5164 /* I couldn't have got its status if it weren't alive, so it's alive. */
5165 return 1;
c906108c 5166}
c3f6f71d
JM
5167
5168/*
5169 * Function: target_pid_to_str
5170 *
5171 * Return a string to be used to identify the thread in
5172 * the "info threads" display.
5173 */
5174
5175char *
39f77062 5176procfs_pid_to_str (ptid_t ptid)
c3f6f71d
JM
5177{
5178 static char buf[80];
5179 int proc, thread;
5180 procinfo *pi;
5181
39f77062
KB
5182 proc = PIDGET (ptid);
5183 thread = TIDGET (ptid);
c3f6f71d
JM
5184 pi = find_procinfo (proc, thread);
5185
5186 if (thread == 0)
5187 sprintf (buf, "Process %d", proc);
5188 else
5189 sprintf (buf, "LWP %d", thread);
5190 return &buf[0];
5191}
5192
5193/*
5194 * Function: procfs_set_watchpoint
5195 * Insert a watchpoint
5196 */
5197
5198int
39f77062
KB
5199procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5200 int after)
c906108c 5201{
c3f6f71d 5202#ifndef UNIXWARE
37de36c6 5203#ifndef AIX5
c3f6f71d
JM
5204 int pflags = 0;
5205 procinfo *pi;
5206
39f77062
KB
5207 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5208 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
c3f6f71d
JM
5209
5210 /* Translate from GDB's flags to /proc's */
5211 if (len > 0) /* len == 0 means delete watchpoint */
c906108c 5212 {
c3f6f71d
JM
5213 switch (rwflag) { /* FIXME: need an enum! */
5214 case hw_write: /* default watchpoint (write) */
5215 pflags = WRITE_WATCHFLAG;
5216 break;
5217 case hw_read: /* read watchpoint */
5218 pflags = READ_WATCHFLAG;
5219 break;
5220 case hw_access: /* access watchpoint */
5221 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5222 break;
5223 case hw_execute: /* execution HW breakpoint */
5224 pflags = EXEC_WATCHFLAG;
5225 break;
5226 default: /* Something weird. Return error. */
c906108c 5227 return -1;
c3f6f71d
JM
5228 }
5229 if (after) /* Stop after r/w access is completed. */
5230 pflags |= AFTER_WATCHFLAG;
5231 }
5232
5233 if (!proc_set_watchpoint (pi, addr, len, pflags))
5234 {
5235 if (errno == E2BIG) /* Typical error for no resources */
5236 return -1; /* fail */
5237 /* GDB may try to remove the same watchpoint twice.
5238 If a remove request returns no match, don't error. */
c906108c 5239 if (errno == ESRCH && len == 0)
c3f6f71d
JM
5240 return 0; /* ignore */
5241 proc_error (pi, "set_watchpoint", __LINE__);
c906108c 5242 }
37de36c6
KB
5243#endif /* AIX5 */
5244#endif /* UNIXWARE */
c906108c
SS
5245 return 0;
5246}
5247
c3f6f71d
JM
5248/*
5249 * Function: stopped_by_watchpoint
5250 *
5251 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5252 * else returns zero.
5253 */
5254
c906108c 5255int
39f77062 5256procfs_stopped_by_watchpoint (ptid_t ptid)
c906108c 5257{
c3f6f71d 5258 procinfo *pi;
c906108c 5259
39f77062
KB
5260 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5261 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
aaeb7efa
MS
5262
5263 if (!pi) /* If no process, then not stopped by watchpoint! */
5264 return 0;
5265
c3f6f71d 5266 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
c906108c 5267 {
c3f6f71d
JM
5268 if (proc_why (pi) == PR_FAULTED)
5269 {
c906108c 5270#ifdef FLTWATCH
c3f6f71d
JM
5271 if (proc_what (pi) == FLTWATCH)
5272 return 1;
c906108c
SS
5273#endif
5274#ifdef FLTKWATCH
c3f6f71d
JM
5275 if (proc_what (pi) == FLTKWATCH)
5276 return 1;
c906108c 5277#endif
c3f6f71d 5278 }
c906108c
SS
5279 }
5280 return 0;
5281}
c906108c 5282
c3f6f71d
JM
5283#ifdef TM_I386SOL2_H
5284/*
5285 * Function: procfs_find_LDT_entry
5286 *
5287 * Input:
39f77062 5288 * ptid_t ptid; // The GDB-style pid-plus-LWP.
c3f6f71d
JM
5289 *
5290 * Return:
5291 * pointer to the corresponding LDT entry.
5292 */
c906108c 5293
c3f6f71d 5294struct ssd *
39f77062 5295procfs_find_LDT_entry (ptid_t ptid)
c906108c 5296{
c3f6f71d
JM
5297 gdb_gregset_t *gregs;
5298 int key;
5299 procinfo *pi;
c906108c 5300
c3f6f71d 5301 /* Find procinfo for the lwp. */
39f77062 5302 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
c906108c 5303 {
39f77062
KB
5304 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5305 PIDGET (ptid), TIDGET (ptid));
c3f6f71d 5306 return NULL;
c906108c 5307 }
c3f6f71d
JM
5308 /* get its general registers. */
5309 if ((gregs = proc_get_gregs (pi)) == NULL)
5310 {
39f77062
KB
5311 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5312 PIDGET (ptid), TIDGET (ptid));
c3f6f71d
JM
5313 return NULL;
5314 }
5315 /* Now extract the GS register's lower 16 bits. */
5316 key = (*gregs)[GS] & 0xffff;
5317
5318 /* Find the matching entry and return it. */
5319 return proc_get_LDT_entry (pi, key);
c906108c 5320}
c3f6f71d 5321#endif /* TM_I386SOL2_H */
c906108c 5322
c3f6f71d
JM
5323
5324
5325static void
fba45db2 5326info_proc_cmd (char *args, int from_tty)
c906108c 5327{
c3f6f71d
JM
5328 struct cleanup *old_chain;
5329 procinfo *process = NULL;
5330 procinfo *thread = NULL;
5331 char **argv = NULL;
5332 char *tmp = NULL;
5333 int pid = 0;
5334 int tid = 0;
c906108c 5335
c3f6f71d
JM
5336 old_chain = make_cleanup (null_cleanup, 0);
5337 if (args)
0fda6bd2
JM
5338 {
5339 if ((argv = buildargv (args)) == NULL)
5340 nomem (0);
5341 else
004527cb 5342 make_cleanup_freeargv (argv);
0fda6bd2 5343 }
c3f6f71d
JM
5344 while (argv != NULL && *argv != NULL)
5345 {
5346 if (isdigit (argv[0][0]))
5347 {
5348 pid = strtoul (argv[0], &tmp, 10);
5349 if (*tmp == '/')
5350 tid = strtoul (++tmp, NULL, 10);
5351 }
5352 else if (argv[0][0] == '/')
5353 {
5354 tid = strtoul (argv[0] + 1, NULL, 10);
5355 }
5356 else
5357 {
5358 /* [...] */
5359 }
5360 argv++;
5361 }
5362 if (pid == 0)
39f77062 5363 pid = PIDGET (inferior_ptid);
c3f6f71d
JM
5364 if (pid == 0)
5365 error ("No current process: you must name one.");
5366 else
c906108c 5367 {
c3f6f71d
JM
5368 /* Have pid, will travel.
5369 First see if it's a process we're already debugging. */
5370 process = find_procinfo (pid, 0);
5371 if (process == NULL)
5372 {
5373 /* No. So open a procinfo for it, but
5374 remember to close it again when finished. */
5375 process = create_procinfo (pid, 0);
004527cb 5376 make_cleanup (do_destroy_procinfo_cleanup, process);
c3f6f71d
JM
5377 if (!open_procinfo_files (process, FD_CTL))
5378 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5379 }
c906108c 5380 }
c3f6f71d
JM
5381 if (tid != 0)
5382 thread = create_procinfo (pid, tid);
5383
5384 if (process)
5385 {
5386 printf_filtered ("process %d flags:\n", process->pid);
5387 proc_prettyprint_flags (proc_flags (process), 1);
5388 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5389 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5390 if (proc_get_nthreads (process) > 1)
5391 printf_filtered ("Process has %d threads.\n",
5392 proc_get_nthreads (process));
5393 }
5394 if (thread)
5395 {
5396 printf_filtered ("thread %d flags:\n", thread->tid);
5397 proc_prettyprint_flags (proc_flags (thread), 1);
5398 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5399 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5400 }
5401
5402 do_cleanups (old_chain);
c906108c
SS
5403}
5404
c3f6f71d 5405static void
fba45db2 5406proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
c906108c 5407{
c3f6f71d
JM
5408 procinfo *pi;
5409 sysset_t *sysset;
5410 int syscallnum = 0;
c906108c 5411
39f77062 5412 if (PIDGET (inferior_ptid) <= 0)
c3f6f71d 5413 error ("you must be debugging a process to use this command.");
c906108c 5414
c3f6f71d
JM
5415 if (args == NULL || args[0] == 0)
5416 error_no_arg ("system call to trace");
5417
39f77062 5418 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
5419 if (isdigit (args[0]))
5420 {
5421 syscallnum = atoi (args);
5422 if (entry_or_exit == PR_SYSENTRY)
5423 sysset = proc_get_traced_sysentry (pi, NULL);
5424 else
5425 sysset = proc_get_traced_sysexit (pi, NULL);
c906108c 5426
c3f6f71d
JM
5427 if (sysset == NULL)
5428 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
c906108c 5429
c3f6f71d 5430 if (mode == FLAG_SET)
37de36c6 5431 gdb_praddsysset (sysset, syscallnum);
c3f6f71d 5432 else
37de36c6 5433 gdb_prdelsysset (sysset, syscallnum);
c906108c 5434
c3f6f71d
JM
5435 if (entry_or_exit == PR_SYSENTRY)
5436 {
5437 if (!proc_set_traced_sysentry (pi, sysset))
5438 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5439 }
5440 else
5441 {
5442 if (!proc_set_traced_sysexit (pi, sysset))
5443 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5444 }
5445 }
5446}
5447
5448static void
fba45db2 5449proc_trace_sysentry_cmd (char *args, int from_tty)
c906108c 5450{
c3f6f71d
JM
5451 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5452}
c906108c 5453
c3f6f71d 5454static void
fba45db2 5455proc_trace_sysexit_cmd (char *args, int from_tty)
c3f6f71d
JM
5456{
5457 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
c906108c 5458}
c906108c 5459
c3f6f71d 5460static void
fba45db2 5461proc_untrace_sysentry_cmd (char *args, int from_tty)
c3f6f71d
JM
5462{
5463 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5464}
5465
5466static void
fba45db2 5467proc_untrace_sysexit_cmd (char *args, int from_tty)
c906108c 5468{
c3f6f71d
JM
5469 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5470}
c906108c 5471
c906108c 5472
c906108c 5473void
fba45db2 5474_initialize_procfs (void)
c906108c 5475{
c906108c
SS
5476 init_procfs_ops ();
5477 add_target (&procfs_ops);
c3f6f71d
JM
5478 add_info ("proc", info_proc_cmd,
5479 "Show /proc process information about any running process.\
5480Default is the process being debugged.");
5481 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5482 "Give a trace of entries into the syscall.");
5483 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5484 "Give a trace of exits from the syscall.");
5485 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5486 "Cancel a trace of entries into the syscall.");
5487 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5488 "Cancel a trace of exits from the syscall.");
c3f6f71d
JM
5489}
5490
5491/* =================== END, GDB "MODULE" =================== */
5492
5493
5494
5495/* miscelaneous stubs: */
5496/* The following satisfy a few random symbols mostly created by */
5497/* the solaris threads implementation, which I will chase down */
5498/* later. */
5499
5500/*
5501 * Return a pid for which we guarantee
5502 * we will be able to find a 'live' procinfo.
5503 */
5504
39f77062 5505ptid_t
fba45db2 5506procfs_first_available (void)
c3f6f71d 5507{
39f77062 5508 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
c3f6f71d 5509}
This page took 0.421622 seconds and 4 git commands to generate.