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