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