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