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