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