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