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