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