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