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