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