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