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