daily update
[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 {
3706 if ((regno >= 0 && regno < FP0_REGNUM) ||
3707 regno == PC_REGNUM ||
3708 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
0ba6dca9 3709 regno == DEPRECATED_FP_REGNUM ||
60054393
MS
3710 regno == SP_REGNUM)
3711 return; /* not a floating point register */
c5aa993b 3712
60054393
MS
3713 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3714 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
c906108c 3715
60054393
MS
3716 supply_fpregset (fpregs);
3717 }
c906108c
SS
3718}
3719
c3f6f71d
JM
3720/* Get ready to modify the registers array. On machines which store
3721 individual registers, this doesn't need to do anything. On
3722 machines which store all the registers in one fell swoop, such as
3723 /proc, this makes sure that registers contains all the registers
3724 from the program being debugged. */
3725
c906108c 3726static void
fba45db2 3727procfs_prepare_to_store (void)
c906108c 3728{
c3f6f71d
JM
3729#ifdef CHILD_PREPARE_TO_STORE
3730 CHILD_PREPARE_TO_STORE ();
c906108c 3731#endif
c906108c
SS
3732}
3733
3734/*
c3f6f71d
JM
3735 * store_registers
3736 *
3737 * Since the /proc interface will not read individual registers,
3738 * we will cache these requests until the process is resumed, and
3739 * only then write them back to the inferior process.
3740 *
3741 * FIXME: is that a really bad idea? Have to think about cases
3742 * where writing one register might affect the value of others, etc.
3743 */
c906108c 3744
c3f6f71d 3745static void
fba45db2 3746procfs_store_registers (int regno)
c3f6f71d
JM
3747{
3748 gdb_fpregset_t *fpregs;
3749 gdb_gregset_t *gregs;
3750 procinfo *pi;
3751 int pid;
3752 int tid;
c906108c 3753
39f77062
KB
3754 pid = PIDGET (inferior_ptid);
3755 tid = TIDGET (inferior_ptid);
c906108c 3756
c3f6f71d
JM
3757 /* First find procinfo for main process */
3758 pi = find_procinfo_or_die (pid, 0);
3759
3760 /* If current lwp for process is not the same as requested thread
39f77062 3761 (ie. inferior_ptid), then find procinfo for the requested thread. */
c3f6f71d
JM
3762
3763 if ((tid != 0) &&
3764 (tid != proc_get_current_thread (pi)))
3765 pi = find_procinfo_or_die (pid, tid);
3766
3767 if (pi == NULL)
3768 error ("procfs: store_registers: failed to find procinfo for %s",
39f77062 3769 target_pid_to_str (inferior_ptid));
c906108c 3770
c3f6f71d
JM
3771 if ((gregs = proc_get_gregs (pi)) == NULL)
3772 proc_error (pi, "store_registers, get_gregs", __LINE__);
c906108c 3773
c3f6f71d
JM
3774 fill_gregset (gregs, regno);
3775 if (!proc_set_gregs (pi))
3776 proc_error (pi, "store_registers, set_gregs", __LINE__);
c906108c 3777
60054393
MS
3778 if (FP0_REGNUM >= 0) /* need floating point? */
3779 {
3780 if ((regno >= 0 && regno < FP0_REGNUM) ||
3781 regno == PC_REGNUM ||
3782 (NPC_REGNUM >= 0 && regno == NPC_REGNUM) ||
0ba6dca9 3783 regno == DEPRECATED_FP_REGNUM ||
60054393
MS
3784 regno == SP_REGNUM)
3785 return; /* not a floating point register */
3786
3787 if ((fpregs = proc_get_fpregs (pi)) == NULL)
3788 proc_error (pi, "store_registers, get_fpregs", __LINE__);
3789
3790 fill_fpregset (fpregs, regno);
3791 if (!proc_set_fpregs (pi))
3792 proc_error (pi, "store_registers, set_fpregs", __LINE__);
3793 }
c3f6f71d 3794}
c906108c 3795
37de36c6
KB
3796static int
3797syscall_is_lwp_exit (procinfo *pi, int scall)
3798{
3799
3800#ifdef SYS_lwp_exit
3801 if (scall == SYS_lwp_exit)
3802 return 1;
3803#endif
3804#ifdef SYS_lwpexit
3805 if (scall == SYS_lwpexit)
3806 return 1;
3807#endif
3808 return 0;
3809}
3810
3811static int
3812syscall_is_exit (procinfo *pi, int scall)
3813{
3814#ifdef SYS_exit
3815 if (scall == SYS_exit)
3816 return 1;
3817#endif
3818#ifdef DYNAMIC_SYSCALLS
3819 if (find_syscall (pi, "_exit") == scall)
3820 return 1;
3821#endif
3822 return 0;
3823}
3824
3825static int
3826syscall_is_exec (procinfo *pi, int scall)
3827{
3828#ifdef SYS_exec
3829 if (scall == SYS_exec)
3830 return 1;
3831#endif
3832#ifdef SYS_execv
3833 if (scall == SYS_execv)
3834 return 1;
3835#endif
3836#ifdef SYS_execve
3837 if (scall == SYS_execve)
3838 return 1;
3839#endif
3840#ifdef DYNAMIC_SYSCALLS
3841 if (find_syscall (pi, "_execve"))
3842 return 1;
3843 if (find_syscall (pi, "ra_execve"))
3844 return 1;
3845#endif
3846 return 0;
3847}
3848
3849static int
3850syscall_is_lwp_create (procinfo *pi, int scall)
3851{
3852#ifdef SYS_lwp_create
3853 if (scall == SYS_lwp_create)
3854 return 1;
3855#endif
3856#ifdef SYS_lwpcreate
3857 if (scall == SYS_lwpcreate)
3858 return 1;
3859#endif
3860 return 0;
3861}
3862
c3f6f71d
JM
3863/*
3864 * Function: target_wait
3865 *
3866 * Retrieve the next stop event from the child process.
3867 * If child has not stopped yet, wait for it to stop.
3868 * Translate /proc eventcodes (or possibly wait eventcodes)
3869 * into gdb internal event codes.
3870 *
3871 * Return: id of process (and possibly thread) that incurred the event.
3872 * event codes are returned thru a pointer parameter.
c906108c
SS
3873 */
3874
39f77062
KB
3875static ptid_t
3876procfs_wait (ptid_t ptid, struct target_waitstatus *status)
c906108c 3877{
c3f6f71d
JM
3878 /* First cut: loosely based on original version 2.1 */
3879 procinfo *pi;
39f77062
KB
3880 int wstat;
3881 int temp_tid;
3882 ptid_t retval, temp_ptid;
c3f6f71d
JM
3883 int why, what, flags;
3884 int retry = 0;
c906108c 3885
c3f6f71d 3886wait_again:
c906108c 3887
c3f6f71d
JM
3888 retry++;
3889 wstat = 0;
39f77062 3890 retval = pid_to_ptid (-1);
c906108c 3891
c3f6f71d 3892 /* Find procinfo for main process */
39f77062 3893 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d 3894 if (pi)
c906108c 3895 {
c3f6f71d
JM
3896 /* We must assume that the status is stale now... */
3897 pi->status_valid = 0;
3898 pi->gregs_valid = 0;
3899 pi->fpregs_valid = 0;
3900
3901#if 0 /* just try this out... */
3902 flags = proc_flags (pi);
3903 why = proc_why (pi);
3904 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
3905 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
3906#endif
3907 /* If child is not stopped, wait for it to stop. */
3908 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) &&
3909 !proc_wait_for_stop (pi))
c906108c 3910 {
c3f6f71d
JM
3911 /* wait_for_stop failed: has the child terminated? */
3912 if (errno == ENOENT)
c906108c 3913 {
39f77062
KB
3914 int wait_retval;
3915
c3f6f71d 3916 /* /proc file not found; presumably child has terminated. */
39f77062 3917 wait_retval = wait (&wstat); /* "wait" for the child's exit */
c3f6f71d 3918
39f77062 3919 if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */
c3f6f71d 3920 error ("procfs: couldn't stop process %d: wait returned %d\n",
39f77062 3921 PIDGET (inferior_ptid), wait_retval);
c3f6f71d
JM
3922 /* FIXME: might I not just use waitpid?
3923 Or try find_procinfo to see if I know about this child? */
39f77062 3924 retval = pid_to_ptid (wait_retval);
c906108c 3925 }
d1566ff5
FN
3926 else if (errno == EINTR)
3927 goto wait_again;
c3f6f71d 3928 else
c906108c 3929 {
c3f6f71d
JM
3930 /* Unknown error from wait_for_stop. */
3931 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
c906108c 3932 }
c3f6f71d
JM
3933 }
3934 else
3935 {
3936 /* This long block is reached if either:
3937 a) the child was already stopped, or
3938 b) we successfully waited for the child with wait_for_stop.
3939 This block will analyze the /proc status, and translate it
3940 into a waitstatus for GDB.
3941
3942 If we actually had to call wait because the /proc file
3943 is gone (child terminated), then we skip this block,
3944 because we already have a waitstatus. */
3945
3946 flags = proc_flags (pi);
3947 why = proc_why (pi);
3948 what = proc_what (pi);
3949
c3f6f71d 3950 if (flags & (PR_STOPPED | PR_ISTOP))
c906108c 3951 {
c3f6f71d
JM
3952#ifdef PR_ASYNC
3953 /* If it's running async (for single_thread control),
3954 set it back to normal again. */
3955 if (flags & PR_ASYNC)
3956 if (!proc_unset_async (pi))
3957 proc_error (pi, "target_wait, unset_async", __LINE__);
3958#endif
3959
3960 if (info_verbose)
3961 proc_prettyprint_why (why, what, 1);
3962
3963 /* The 'pid' we will return to GDB is composed of
3964 the process ID plus the lwp ID. */
3965 retval = MERGEPID (pi->pid, proc_get_current_thread (pi));
3966
3967 switch (why) {
3968 case PR_SIGNALLED:
3969 wstat = (what << 8) | 0177;
3970 break;
3971 case PR_SYSENTRY:
37de36c6 3972 if (syscall_is_lwp_exit (pi, what))
c3f6f71d 3973 {
37de36c6
KB
3974 printf_filtered ("[%s exited]\n",
3975 target_pid_to_str (retval));
3976 delete_thread (retval);
3977 status->kind = TARGET_WAITKIND_SPURIOUS;
3978 return retval;
3979 }
3980 else if (syscall_is_exit (pi, what))
3981 {
3982 /* Handle SYS_exit call only */
3983 /* Stopped at entry to SYS_exit.
3984 Make it runnable, resume it, then use
3985 the wait system call to get its exit code.
3986 Proc_run_process always clears the current
3987 fault and signal.
3988 Then return its exit status. */
3989 pi->status_valid = 0;
3990 wstat = 0;
3991 /* FIXME: what we should do is return
3992 TARGET_WAITKIND_SPURIOUS. */
3993 if (!proc_run_process (pi, 0, 0))
3994 proc_error (pi, "target_wait, run_process", __LINE__);
3995 if (attach_flag)
c3f6f71d 3996 {
37de36c6
KB
3997 /* Don't call wait: simulate waiting for exit,
3998 return a "success" exit code. Bogus: what if
3999 it returns something else? */
4000 wstat = 0;
39f77062 4001 retval = inferior_ptid; /* ? ? ? */
37de36c6
KB
4002 }
4003 else
4004 {
4005 int temp = wait (&wstat);
4006
4007 /* FIXME: shouldn't I make sure I get the right
4008 event from the right process? If (for
4009 instance) I have killed an earlier inferior
4010 process but failed to clean up after it
4011 somehow, I could get its termination event
4012 here. */
4013
4014 /* If wait returns -1, that's what we return to GDB. */
4015 if (temp < 0)
39f77062 4016 retval = pid_to_ptid (temp);
c3f6f71d 4017 }
c3f6f71d 4018 }
37de36c6
KB
4019 else
4020 {
4021 printf_filtered ("procfs: trapped on entry to ");
4022 proc_prettyprint_syscall (proc_what (pi), 0);
4023 printf_filtered ("\n");
4024#ifndef PIOCSSPCACT
c3f6f71d 4025 {
37de36c6 4026 long i, nsysargs, *sysargs;
c3f6f71d 4027
37de36c6
KB
4028 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4029 (sysargs = proc_sysargs (pi)) != NULL)
4030 {
4031 printf_filtered ("%ld syscall arguments:\n", nsysargs);
4032 for (i = 0; i < nsysargs; i++)
4033 printf_filtered ("#%ld: 0x%08lx\n",
4034 i, sysargs[i]);
4035 }
c3f6f71d 4036
c3f6f71d 4037 }
c3f6f71d 4038#endif
37de36c6
KB
4039 if (status)
4040 {
4041 /* How to exit gracefully, returning "unknown event" */
4042 status->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 4043 return inferior_ptid;
37de36c6
KB
4044 }
4045 else
4046 {
4047 /* How to keep going without returning to wfi: */
39f77062 4048 target_resume (ptid, 0, TARGET_SIGNAL_0);
37de36c6
KB
4049 goto wait_again;
4050 }
4051 }
4052 break;
4053 case PR_SYSEXIT:
4054 if (syscall_is_exec (pi, what))
c3f6f71d 4055 {
37de36c6
KB
4056 /* Hopefully this is our own "fork-child" execing
4057 the real child. Hoax this event into a trap, and
4058 GDB will see the child about to execute its start
4059 address. */
4060 wstat = (SIGTRAP << 8) | 0177;
4061 }
4062 else if (syscall_is_lwp_create (pi, what))
4063 {
4064 /*
4065 * This syscall is somewhat like fork/exec.
4066 * We will get the event twice: once for the parent LWP,
4067 * and once for the child. We should already know about
4068 * the parent LWP, but the child will be new to us. So,
4069 * whenever we get this event, if it represents a new
4070 * thread, simply add the thread to the list.
4071 */
c3f6f71d 4072
37de36c6 4073 /* If not in procinfo list, add it. */
39f77062
KB
4074 temp_tid = proc_get_current_thread (pi);
4075 if (!find_procinfo (pi->pid, temp_tid))
4076 create_procinfo (pi->pid, temp_tid);
37de36c6 4077
39f77062 4078 temp_ptid = MERGEPID (pi->pid, temp_tid);
37de36c6 4079 /* If not in GDB's thread list, add it. */
39f77062 4080 if (!in_thread_list (temp_ptid))
c3f6f71d 4081 {
39f77062
KB
4082 printf_filtered ("[New %s]\n",
4083 target_pid_to_str (temp_ptid));
4084 add_thread (temp_ptid);
c3f6f71d 4085 }
37de36c6
KB
4086 /* Return to WFI, but tell it to immediately resume. */
4087 status->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 4088 return inferior_ptid;
37de36c6
KB
4089 }
4090 else if (syscall_is_lwp_exit (pi, what))
4091 {
4092 printf_filtered ("[%s exited]\n",
4093 target_pid_to_str (retval));
4094 delete_thread (retval);
4095 status->kind = TARGET_WAITKIND_SPURIOUS;
4096 return retval;
c3f6f71d 4097 }
37de36c6
KB
4098 else if (0)
4099 {
4100 /* FIXME: Do we need to handle SYS_sproc,
4101 SYS_fork, or SYS_vfork here? The old procfs
4102 seemed to use this event to handle threads on
4103 older (non-LWP) systems, where I'm assuming
4104 that threads were actually separate processes.
4105 Irix, maybe? Anyway, low priority for now. */
4106 }
4107 else
4108 {
4109 printf_filtered ("procfs: trapped on exit from ");
4110 proc_prettyprint_syscall (proc_what (pi), 0);
4111 printf_filtered ("\n");
4112#ifndef PIOCSSPCACT
4113 {
4114 long i, nsysargs, *sysargs;
4115
4116 if ((nsysargs = proc_nsysarg (pi)) > 0 &&
4117 (sysargs = proc_sysargs (pi)) != NULL)
4118 {
4119 printf_filtered ("%ld syscall arguments:\n", nsysargs);
4120 for (i = 0; i < nsysargs; i++)
4121 printf_filtered ("#%ld: 0x%08lx\n",
4122 i, sysargs[i]);
4123 }
4124 }
c3f6f71d 4125#endif
37de36c6 4126 status->kind = TARGET_WAITKIND_SPURIOUS;
39f77062 4127 return inferior_ptid;
37de36c6 4128 }
c3f6f71d
JM
4129 break;
4130 case PR_REQUESTED:
4131#if 0 /* FIXME */
4132 wstat = (SIGSTOP << 8) | 0177;
4133 break;
4134#else
4135 if (retry < 5)
4136 {
4137 printf_filtered ("Retry #%d:\n", retry);
4138 pi->status_valid = 0;
4139 goto wait_again;
4140 }
4141 else
4142 {
4143 /* If not in procinfo list, add it. */
39f77062
KB
4144 temp_tid = proc_get_current_thread (pi);
4145 if (!find_procinfo (pi->pid, temp_tid))
4146 create_procinfo (pi->pid, temp_tid);
c3f6f71d
JM
4147
4148 /* If not in GDB's thread list, add it. */
39f77062
KB
4149 temp_ptid = MERGEPID (pi->pid, temp_tid);
4150 if (!in_thread_list (temp_ptid))
c3f6f71d 4151 {
0d06e24b 4152 printf_filtered ("[New %s]\n",
39f77062
KB
4153 target_pid_to_str (temp_ptid));
4154 add_thread (temp_ptid);
c3f6f71d
JM
4155 }
4156
4157 status->kind = TARGET_WAITKIND_STOPPED;
4158 status->value.sig = 0;
4159 return retval;
4160 }
4161#endif
4162 case PR_JOBCONTROL:
4163 wstat = (what << 8) | 0177;
4164 break;
4165 case PR_FAULTED:
4166 switch (what) { /* FIXME: FAULTED_USE_SIGINFO */
4167#ifdef FLTWATCH
4168 case FLTWATCH:
4169 wstat = (SIGTRAP << 8) | 0177;
4170 break;
4171#endif
4172#ifdef FLTKWATCH
4173 case FLTKWATCH:
4174 wstat = (SIGTRAP << 8) | 0177;
4175 break;
4176#endif
4177 /* FIXME: use si_signo where possible. */
4178 case FLTPRIV:
4179#if (FLTILL != FLTPRIV) /* avoid "duplicate case" error */
4180 case FLTILL:
4181#endif
4182 wstat = (SIGILL << 8) | 0177;
4183 break;
4184 case FLTBPT:
4185#if (FLTTRACE != FLTBPT) /* avoid "duplicate case" error */
4186 case FLTTRACE:
4187#endif
4188 wstat = (SIGTRAP << 8) | 0177;
4189 break;
4190 case FLTSTACK:
4191 case FLTACCESS:
4192#if (FLTBOUNDS != FLTSTACK) /* avoid "duplicate case" error */
4193 case FLTBOUNDS:
4194#endif
4195 wstat = (SIGSEGV << 8) | 0177;
4196 break;
4197 case FLTIOVF:
4198 case FLTIZDIV:
4199#if (FLTFPE != FLTIOVF) /* avoid "duplicate case" error */
4200 case FLTFPE:
4201#endif
4202 wstat = (SIGFPE << 8) | 0177;
4203 break;
4204 case FLTPAGE: /* Recoverable page fault */
4205 default: /* FIXME: use si_signo if possible for fault */
39f77062 4206 retval = pid_to_ptid (-1);
c3f6f71d
JM
4207 printf_filtered ("procfs:%d -- ", __LINE__);
4208 printf_filtered ("child stopped for unknown reason:\n");
4209 proc_prettyprint_why (why, what, 1);
4210 error ("... giving up...");
4211 break;
4212 }
4213 break; /* case PR_FAULTED: */
4214 default: /* switch (why) unmatched */
4215 printf_filtered ("procfs:%d -- ", __LINE__);
4216 printf_filtered ("child stopped for unknown reason:\n");
4217 proc_prettyprint_why (why, what, 1);
4218 error ("... giving up...");
4219 break;
4220 }
4221 /*
4222 * Got this far without error:
4223 * If retval isn't in the threads database, add it.
4224 */
39f77062
KB
4225 if (PIDGET (retval) > 0 &&
4226 !ptid_equal (retval, inferior_ptid) &&
c3f6f71d 4227 !in_thread_list (retval))
c906108c 4228 {
c3f6f71d
JM
4229 /*
4230 * We have a new thread.
4231 * We need to add it both to GDB's list and to our own.
4232 * If we don't create a procinfo, resume may be unhappy
4233 * later.
4234 */
4235 printf_filtered ("[New %s]\n", target_pid_to_str (retval));
4236 add_thread (retval);
4237 if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL)
4238 create_procinfo (PIDGET (retval), TIDGET (retval));
4239
4240 /* In addition, it's possible that this is the first
4241 * new thread we've seen, in which case we may not
39f77062 4242 * have created entries for inferior_ptid yet.
c3f6f71d 4243 */
39f77062 4244 if (TIDGET (inferior_ptid) != 0)
c3f6f71d 4245 {
39f77062
KB
4246 if (!in_thread_list (inferior_ptid))
4247 add_thread (inferior_ptid);
4248 if (find_procinfo (PIDGET (inferior_ptid),
4249 TIDGET (inferior_ptid)) == NULL)
4250 create_procinfo (PIDGET (inferior_ptid),
4251 TIDGET (inferior_ptid));
c3f6f71d 4252 }
c906108c 4253 }
c906108c 4254 }
c3f6f71d 4255 else /* flags do not indicate STOPPED */
c906108c 4256 {
c3f6f71d
JM
4257 /* surely this can't happen... */
4258 printf_filtered ("procfs:%d -- process not stopped.\n",
4259 __LINE__);
4260 proc_prettyprint_flags (flags, 1);
4261 error ("procfs: ...giving up...");
c906108c 4262 }
c906108c 4263 }
c906108c 4264
c3f6f71d
JM
4265 if (status)
4266 store_waitstatus (status, wstat);
c906108c
SS
4267 }
4268
c3f6f71d
JM
4269 return retval;
4270}
c906108c 4271
d0849a9a
KB
4272/* Transfer LEN bytes between GDB address MYADDR and target address
4273 MEMADDR. If DOWRITE is non-zero, transfer them to the target,
4274 otherwise transfer them from the target. TARGET is unused.
4275
4276 The return value is 0 if an error occurred or no bytes were
4277 transferred. Otherwise, it will be a positive value which
4278 indicates the number of bytes transferred between gdb and the
4279 target. (Note that the interface also makes provisions for
4280 negative values, but this capability isn't implemented here.) */
4281
c3f6f71d 4282static int
d0849a9a 4283procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int dowrite,
37de36c6 4284 struct mem_attrib *attrib, struct target_ops *target)
c3f6f71d
JM
4285{
4286 procinfo *pi;
4287 int nbytes = 0;
c906108c 4288
c3f6f71d 4289 /* Find procinfo for main process */
39f77062 4290 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
4291 if (pi->as_fd == 0 &&
4292 open_procinfo_files (pi, FD_AS) == 0)
c906108c 4293 {
c3f6f71d
JM
4294 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
4295 return 0;
c906108c 4296 }
c906108c 4297
c3f6f71d 4298 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr)
c906108c 4299 {
c3f6f71d 4300 if (dowrite)
c906108c 4301 {
c3f6f71d
JM
4302#ifdef NEW_PROC_API
4303 PROCFS_NOTE ("write memory: ");
c906108c 4304#else
c3f6f71d 4305 PROCFS_NOTE ("write memory: \n");
c906108c 4306#endif
c3f6f71d 4307 nbytes = write (pi->as_fd, myaddr, len);
c906108c 4308 }
c3f6f71d 4309 else
c906108c 4310 {
c3f6f71d
JM
4311 PROCFS_NOTE ("read memory: \n");
4312 nbytes = read (pi->as_fd, myaddr, len);
c906108c 4313 }
c3f6f71d 4314 if (nbytes < 0)
c906108c 4315 {
c3f6f71d 4316 nbytes = 0;
c906108c 4317 }
c906108c 4318 }
c3f6f71d 4319 return nbytes;
c906108c
SS
4320}
4321
4322/*
c3f6f71d
JM
4323 * Function: invalidate_cache
4324 *
4325 * Called by target_resume before making child runnable.
4326 * Mark cached registers and status's invalid.
4327 * If there are "dirty" caches that need to be written back
4328 * to the child process, do that.
4329 *
4330 * File descriptors are also cached.
4331 * As they are a limited resource, we cannot hold onto them indefinitely.
4332 * However, as they are expensive to open, we don't want to throw them
4333 * away indescriminately either. As a compromise, we will keep the
4334 * file descriptors for the parent process, but discard any file
4335 * descriptors we may have accumulated for the threads.
4336 *
4337 * Return value:
4338 * As this function is called by iterate_over_threads, it always
4339 * returns zero (so that iterate_over_threads will keep iterating).
c906108c
SS
4340 */
4341
c3f6f71d
JM
4342
4343static int
fba45db2 4344invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
c906108c 4345{
c3f6f71d
JM
4346 /*
4347 * About to run the child; invalidate caches and do any other cleanup.
4348 */
c906108c 4349
c3f6f71d
JM
4350#if 0
4351 if (pi->gregs_dirty)
4352 if (parent == NULL ||
4353 proc_get_current_thread (parent) != pi->tid)
4354 if (!proc_set_gregs (pi)) /* flush gregs cache */
4355 proc_warn (pi, "target_resume, set_gregs",
4356 __LINE__);
60054393
MS
4357 if (FP0_REGNUM >= 0)
4358 if (pi->fpregs_dirty)
4359 if (parent == NULL ||
4360 proc_get_current_thread (parent) != pi->tid)
4361 if (!proc_set_fpregs (pi)) /* flush fpregs cache */
4362 proc_warn (pi, "target_resume, set_fpregs",
4363 __LINE__);
c906108c 4364#endif
c906108c 4365
c3f6f71d 4366 if (parent != NULL)
c906108c 4367 {
c3f6f71d
JM
4368 /* The presence of a parent indicates that this is an LWP.
4369 Close any file descriptors that it might have open.
4370 We don't do this to the master (parent) procinfo. */
4371
4372 close_procinfo_files (pi);
c906108c 4373 }
c3f6f71d
JM
4374 pi->gregs_valid = 0;
4375 pi->fpregs_valid = 0;
4376#if 0
4377 pi->gregs_dirty = 0;
4378 pi->fpregs_dirty = 0;
c906108c 4379#endif
c3f6f71d
JM
4380 pi->status_valid = 0;
4381 pi->threads_valid = 0;
c906108c 4382
c3f6f71d 4383 return 0;
c906108c
SS
4384}
4385
0fda6bd2 4386#if 0
c906108c 4387/*
c3f6f71d
JM
4388 * Function: make_signal_thread_runnable
4389 *
4390 * A callback function for iterate_over_threads.
4391 * Find the asynchronous signal thread, and make it runnable.
4392 * See if that helps matters any.
c906108c
SS
4393 */
4394
c3f6f71d 4395static int
fba45db2 4396make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr)
c906108c 4397{
c3f6f71d
JM
4398#ifdef PR_ASLWP
4399 if (proc_flags (pi) & PR_ASLWP)
c906108c 4400 {
c3f6f71d
JM
4401 if (!proc_run_process (pi, 0, -1))
4402 proc_error (pi, "make_signal_thread_runnable", __LINE__);
4403 return 1;
c906108c 4404 }
c906108c 4405#endif
c3f6f71d 4406 return 0;
c906108c 4407}
0fda6bd2 4408#endif
c906108c
SS
4409
4410/*
c3f6f71d
JM
4411 * Function: target_resume
4412 *
4413 * Make the child process runnable. Normally we will then call
4414 * procfs_wait and wait for it to stop again (unles gdb is async).
4415 *
4416 * Arguments:
4417 * step: if true, then arrange for the child to stop again
4418 * after executing a single instruction.
4419 * signo: if zero, then cancel any pending signal.
4420 * If non-zero, then arrange for the indicated signal
4421 * to be delivered to the child when it runs.
4422 * pid: if -1, then allow any child thread to run.
4423 * if non-zero, then allow only the indicated thread to run.
4424 ******* (not implemented yet)
c906108c
SS
4425 */
4426
4427static void
39f77062 4428procfs_resume (ptid_t ptid, int step, enum target_signal signo)
c906108c 4429{
c3f6f71d
JM
4430 procinfo *pi, *thread;
4431 int native_signo;
4432
4433 /* 2.1:
4434 prrun.prflags |= PRSVADDR;
4435 prrun.pr_vaddr = $PC; set resume address
4436 prrun.prflags |= PRSTRACE; trace signals in pr_trace (all)
4437 prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE)
4438 prrun.prflags |= PRCFAULT; clear current fault.
4439
4440 PRSTRACE and PRSFAULT can be done by other means
4441 (proc_trace_signals, proc_trace_faults)
4442 PRSVADDR is unnecessary.
4443 PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault)
4444 This basically leaves PRSTEP and PRCSIG.
4445 PRCSIG is like PIOCSSIG (proc_clear_current_signal).
4446 So basically PR_STEP is the sole argument that must be passed
4447 to proc_run_process (for use in the prrun struct by ioctl). */
4448
4449 /* Find procinfo for main process */
39f77062 4450 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
4451
4452 /* First cut: ignore pid argument */
4453 errno = 0;
c906108c 4454
c3f6f71d
JM
4455 /* Convert signal to host numbering. */
4456 if (signo == 0 ||
0fda6bd2 4457 (signo == TARGET_SIGNAL_STOP && pi->ignore_next_sigstop))
c3f6f71d
JM
4458 native_signo = 0;
4459 else
4460 native_signo = target_signal_to_host (signo);
c906108c 4461
c3f6f71d 4462 pi->ignore_next_sigstop = 0;
c906108c 4463
c3f6f71d
JM
4464 /* Running the process voids all cached registers and status. */
4465 /* Void the threads' caches first */
4466 proc_iterate_over_threads (pi, invalidate_cache, NULL);
4467 /* Void the process procinfo's caches. */
4468 invalidate_cache (NULL, pi, NULL);
c906108c 4469
39f77062 4470 if (PIDGET (ptid) != -1)
c906108c 4471 {
c3f6f71d 4472 /* Resume a specific thread, presumably suppressing the others. */
39f77062 4473 thread = find_procinfo (PIDGET (ptid), TIDGET (ptid));
7de45904 4474 if (thread != NULL)
c906108c 4475 {
c3f6f71d
JM
4476 if (thread->tid != 0)
4477 {
4478 /* We're to resume a specific thread, and not the others.
4479 * Set the child process's PR_ASYNC flag.
4480 */
4481#ifdef PR_ASYNC
4482 if (!proc_set_async (pi))
4483 proc_error (pi, "target_resume, set_async", __LINE__);
4484#endif
4485#if 0
4486 proc_iterate_over_threads (pi,
4487 make_signal_thread_runnable,
4488 NULL);
4489#endif
4490 pi = thread; /* substitute the thread's procinfo for run */
4491 }
c906108c
SS
4492 }
4493 }
c906108c 4494
c3f6f71d 4495 if (!proc_run_process (pi, step, native_signo))
c906108c 4496 {
c3f6f71d
JM
4497 if (errno == EBUSY)
4498 warning ("resume: target already running. Pretend to resume, and hope for the best!\n");
4499 else
4500 proc_error (pi, "target_resume", __LINE__);
c906108c 4501 }
c3f6f71d 4502}
c906108c 4503
c3f6f71d
JM
4504/*
4505 * Function: register_gdb_signals
4506 *
4507 * Traverse the list of signals that GDB knows about
4508 * (see "handle" command), and arrange for the target
4509 * to be stopped or not, according to these settings.
4510 *
4511 * Returns non-zero for success, zero for failure.
4512 */
c906108c 4513
c3f6f71d 4514static int
37de36c6 4515register_gdb_signals (procinfo *pi, gdb_sigset_t *signals)
c3f6f71d
JM
4516{
4517 int signo;
c906108c 4518
c3f6f71d
JM
4519 for (signo = 0; signo < NSIG; signo ++)
4520 if (signal_stop_state (target_signal_from_host (signo)) == 0 &&
4521 signal_print_state (target_signal_from_host (signo)) == 0 &&
4522 signal_pass_state (target_signal_from_host (signo)) == 1)
4523 prdelset (signals, signo);
4524 else
4525 praddset (signals, signo);
c906108c 4526
c3f6f71d 4527 return proc_set_traced_signals (pi, signals);
c906108c
SS
4528}
4529
4530/*
c3f6f71d
JM
4531 * Function: target_notice_signals
4532 *
4533 * Set up to trace signals in the child process.
4534 */
c906108c 4535
c3f6f71d 4536static void
39f77062 4537procfs_notice_signals (ptid_t ptid)
c3f6f71d 4538{
37de36c6 4539 gdb_sigset_t signals;
39f77062 4540 procinfo *pi = find_procinfo_or_die (PIDGET (ptid), 0);
c906108c 4541
c3f6f71d
JM
4542 if (proc_get_traced_signals (pi, &signals) &&
4543 register_gdb_signals (pi, &signals))
4544 return;
4545 else
4546 proc_error (pi, "notice_signals", __LINE__);
4547}
c906108c 4548
c3f6f71d
JM
4549/*
4550 * Function: target_files_info
4551 *
4552 * Print status information about the child process.
4553 */
c906108c 4554
c3f6f71d 4555static void
fba45db2 4556procfs_files_info (struct target_ops *ignore)
c3f6f71d
JM
4557{
4558 printf_filtered ("\tUsing the running image of %s %s via /proc.\n",
4559 attach_flag? "attached": "child",
39f77062 4560 target_pid_to_str (inferior_ptid));
c3f6f71d 4561}
c906108c 4562
c3f6f71d
JM
4563/*
4564 * Function: target_open
4565 *
4566 * A dummy: you don't open procfs.
c906108c
SS
4567 */
4568
4569static void
fba45db2 4570procfs_open (char *args, int from_tty)
c906108c 4571{
c3f6f71d
JM
4572 error ("Use the \"run\" command to start a Unix child process.");
4573}
c906108c 4574
c3f6f71d
JM
4575/*
4576 * Function: target_can_run
4577 *
4578 * This tells GDB that this target vector can be invoked
4579 * for "run" or "attach".
4580 */
c906108c 4581
c3f6f71d
JM
4582int procfs_suppress_run = 0; /* Non-zero if procfs should pretend not to
4583 be a runnable target. Used by targets
4584 that can sit atop procfs, such as solaris
4585 thread support. */
c906108c 4586
c906108c 4587
c3f6f71d 4588static int
fba45db2 4589procfs_can_run (void)
c3f6f71d
JM
4590{
4591 /* This variable is controlled by modules that sit atop procfs that
4592 may layer their own process structure atop that provided here.
4593 sol-thread.c does this because of the Solaris two-level thread
4594 model. */
4595
4596 /* NOTE: possibly obsolete -- use the thread_stratum approach instead. */
c906108c 4597
c3f6f71d
JM
4598 return !procfs_suppress_run;
4599}
c906108c 4600
c3f6f71d
JM
4601/*
4602 * Function: target_stop
4603 *
4604 * Stop the child process asynchronously, as when the
4605 * gdb user types control-c or presses a "stop" button.
4606 *
4607 * Works by sending kill(SIGINT) to the child's process group.
4608 */
c906108c 4609
c3f6f71d 4610static void
fba45db2 4611procfs_stop (void)
c3f6f71d 4612{
c3f6f71d 4613 kill (-inferior_process_group, SIGINT);
c906108c
SS
4614}
4615
c906108c 4616/*
c3f6f71d
JM
4617 * Function: unconditionally_kill_inferior
4618 *
4619 * Make it die. Wait for it to die. Clean up after it.
4620 * Note: this should only be applied to the real process,
4621 * not to an LWP, because of the check for parent-process.
4622 * If we need this to work for an LWP, it needs some more logic.
4623 */
c906108c 4624
c3f6f71d 4625static void
fba45db2 4626unconditionally_kill_inferior (procinfo *pi)
c3f6f71d
JM
4627{
4628 int parent_pid;
c906108c 4629
c3f6f71d
JM
4630 parent_pid = proc_parent_pid (pi);
4631#ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL
4632 /* FIXME: use access functions */
4633 /* Alpha OSF/1-3.x procfs needs a clear of the current signal
4634 before the PIOCKILL, otherwise it might generate a corrupted core
4635 file for the inferior. */
4636 if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0)
4637 {
4638 printf_filtered ("unconditionally_kill: SSIG failed!\n");
4639 }
4640#endif
4641#ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL
4642 /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal
4643 to kill the inferior, otherwise it might remain stopped with a
4644 pending SIGKILL.
4645 We do not check the result of the PIOCSSIG, the inferior might have
4646 died already. */
4647 {
37de36c6 4648 gdb_siginfo_t newsiginfo;
c906108c 4649
c3f6f71d
JM
4650 memset ((char *) &newsiginfo, 0, sizeof (newsiginfo));
4651 newsiginfo.si_signo = SIGKILL;
4652 newsiginfo.si_code = 0;
4653 newsiginfo.si_errno = 0;
4654 newsiginfo.si_pid = getpid ();
4655 newsiginfo.si_uid = getuid ();
4656 /* FIXME: use proc_set_current_signal */
4657 ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo);
4658 }
4659#else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4660 if (!proc_kill (pi, SIGKILL))
103b3ef5 4661 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
c3f6f71d
JM
4662#endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */
4663 destroy_procinfo (pi);
c906108c 4664
c3f6f71d
JM
4665 /* If pi is GDB's child, wait for it to die. */
4666 if (parent_pid == getpid ())
4667 /* FIXME: should we use waitpid to make sure we get the right event?
4668 Should we check the returned event? */
4669 {
0d06e24b 4670#if 0
c3f6f71d 4671 int status, ret;
c906108c 4672
c3f6f71d
JM
4673 ret = waitpid (pi->pid, &status, 0);
4674#else
4675 wait (NULL);
4676#endif
4677 }
4678}
c906108c 4679
c3f6f71d
JM
4680/*
4681 * Function: target_kill_inferior
4682 *
4683 * We're done debugging it, and we want it to go away.
4684 * Then we want GDB to forget all about it.
c906108c
SS
4685 */
4686
c3f6f71d 4687static void
fba45db2 4688procfs_kill_inferior (void)
c906108c 4689{
39f77062 4690 if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */
c3f6f71d
JM
4691 {
4692 /* Find procinfo for main process */
39f77062 4693 procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0);
c906108c 4694
c3f6f71d
JM
4695 if (pi)
4696 unconditionally_kill_inferior (pi);
4697 target_mourn_inferior ();
c906108c 4698 }
c3f6f71d
JM
4699}
4700
4701/*
4702 * Function: target_mourn_inferior
4703 *
4704 * Forget we ever debugged this thing!
4705 */
c906108c 4706
c3f6f71d 4707static void
fba45db2 4708procfs_mourn_inferior (void)
c3f6f71d
JM
4709{
4710 procinfo *pi;
c906108c 4711
39f77062 4712 if (!ptid_equal (inferior_ptid, null_ptid))
c3f6f71d
JM
4713 {
4714 /* Find procinfo for main process */
39f77062 4715 pi = find_procinfo (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
4716 if (pi)
4717 destroy_procinfo (pi);
c906108c 4718 }
c3f6f71d
JM
4719 unpush_target (&procfs_ops);
4720 generic_mourn_inferior ();
4721}
c906108c 4722
c3f6f71d
JM
4723/*
4724 * Function: init_inferior
4725 *
4726 * When GDB forks to create a runnable inferior process,
4727 * this function is called on the parent side of the fork.
4728 * It's job is to do whatever is necessary to make the child
4729 * ready to be debugged, and then wait for the child to synchronize.
4730 */
c906108c 4731
c3f6f71d 4732static void
fba45db2 4733procfs_init_inferior (int pid)
c3f6f71d
JM
4734{
4735 procinfo *pi;
37de36c6 4736 gdb_sigset_t signals;
c3f6f71d 4737 int fail;
c906108c 4738
c3f6f71d
JM
4739 /* This routine called on the parent side (GDB side)
4740 after GDB forks the inferior. */
c906108c 4741
c3f6f71d 4742 push_target (&procfs_ops);
c906108c 4743
c3f6f71d
JM
4744 if ((pi = create_procinfo (pid, 0)) == NULL)
4745 perror ("procfs: out of memory in 'init_inferior'");
4746
4747 if (!open_procinfo_files (pi, FD_CTL))
4748 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
4749
4750 /*
4751 xmalloc // done
4752 open_procinfo_files // done
4753 link list // done
4754 prfillset (trace)
4755 procfs_notice_signals
4756 prfillset (fault)
4757 prdelset (FLTPAGE)
4758 PIOCWSTOP
4759 PIOCSFAULT
4760 */
4761
4762 /* If not stopped yet, wait for it to stop. */
4763 if (!(proc_flags (pi) & PR_STOPPED) &&
4764 !(proc_wait_for_stop (pi)))
4765 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
4766
4767 /* Save some of the /proc state to be restored if we detach. */
4768 /* FIXME: Why? In case another debugger was debugging it?
4769 We're it's parent, for Ghu's sake! */
4770 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
4771 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
4772 if (!proc_get_held_signals (pi, &pi->saved_sighold))
4773 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
4774 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
4775 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
37de36c6 4776 if (!proc_get_traced_sysentry (pi, pi->saved_entryset))
c3f6f71d 4777 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
37de36c6 4778 if (!proc_get_traced_sysexit (pi, pi->saved_exitset))
c3f6f71d
JM
4779 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
4780
4781 /* Register to trace selected signals in the child. */
4782 prfillset (&signals);
4783 if (!register_gdb_signals (pi, &signals))
4784 proc_error (pi, "init_inferior, register_signals", __LINE__);
4785
4786 if ((fail = procfs_debug_inferior (pi)) != 0)
4787 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
4788
0d06e24b
JM
4789 /* FIXME: logically, we should really be turning OFF run-on-last-close,
4790 and possibly even turning ON kill-on-last-close at this point. But
4791 I can't make that change without careful testing which I don't have
4792 time to do right now... */
c3f6f71d
JM
4793 /* Turn on run-on-last-close flag so that the child
4794 will die if GDB goes away for some reason. */
4795 if (!proc_set_run_on_last_close (pi))
4796 proc_error (pi, "init_inferior, set_RLC", __LINE__);
4797
4798 /* The 'process ID' we return to GDB is composed of
4799 the actual process ID plus the lwp ID. */
39f77062 4800 inferior_ptid = MERGEPID (pi->pid, proc_get_current_thread (pi));
c906108c 4801
c3f6f71d
JM
4802#ifdef START_INFERIOR_TRAPS_EXPECTED
4803 startup_inferior (START_INFERIOR_TRAPS_EXPECTED);
4804#else
4805 /* One trap to exec the shell, one to exec the program being debugged. */
4806 startup_inferior (2);
0d06e24b 4807#endif /* START_INFERIOR_TRAPS_EXPECTED */
c3f6f71d 4808}
c906108c 4809
c3f6f71d
JM
4810/*
4811 * Function: set_exec_trap
4812 *
4813 * When GDB forks to create a new process, this function is called
4814 * on the child side of the fork before GDB exec's the user program.
4815 * Its job is to make the child minimally debuggable, so that the
4816 * parent GDB process can connect to the child and take over.
4817 * This function should do only the minimum to make that possible,
4818 * and to synchronize with the parent process. The parent process
4819 * should take care of the details.
4820 */
4821
4822static void
fba45db2 4823procfs_set_exec_trap (void)
c3f6f71d
JM
4824{
4825 /* This routine called on the child side (inferior side)
4826 after GDB forks the inferior. It must use only local variables,
4827 because it may be sharing data space with its parent. */
c906108c 4828
c3f6f71d 4829 procinfo *pi;
37de36c6 4830 sysset_t *exitset;
c906108c 4831
c3f6f71d
JM
4832 if ((pi = create_procinfo (getpid (), 0)) == NULL)
4833 perror_with_name ("procfs: create_procinfo failed in child.");
c906108c 4834
c3f6f71d
JM
4835 if (open_procinfo_files (pi, FD_CTL) == 0)
4836 {
4837 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
4838 gdb_flush (gdb_stderr);
4839 /* no need to call "dead_procinfo", because we're going to exit. */
4840 _exit (127);
4841 }
c906108c 4842
c3f6f71d
JM
4843#ifdef PRFS_STOPEXEC /* defined on OSF */
4844 /* OSF method for tracing exec syscalls. Quoting:
4845 Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace
4846 exits from exec system calls because of the user level loader. */
4847 /* FIXME: make nice and maybe move into an access function. */
4848 {
4849 int prfs_flags;
c906108c 4850
c3f6f71d
JM
4851 if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0)
4852 {
4853 proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__);
4854 gdb_flush (gdb_stderr);
4855 _exit (127);
4856 }
4857 prfs_flags |= PRFS_STOPEXEC;
c906108c 4858
c3f6f71d
JM
4859 if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0)
4860 {
4861 proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__);
4862 gdb_flush (gdb_stderr);
4863 _exit (127);
4864 }
4865 }
4866#else /* not PRFS_STOPEXEC */
4867 /* Everyone else's (except OSF) method for tracing exec syscalls */
4868 /* GW: Rationale...
4869 Not all systems with /proc have all the exec* syscalls with the same
4870 names. On the SGI, for example, there is no SYS_exec, but there
4871 *is* a SYS_execv. So, we try to account for that. */
c906108c 4872
37de36c6
KB
4873 exitset = sysset_t_alloc (pi);
4874 gdb_premptysysset (exitset);
c3f6f71d 4875#ifdef SYS_exec
37de36c6 4876 gdb_praddsysset (exitset, SYS_exec);
c3f6f71d
JM
4877#endif
4878#ifdef SYS_execve
37de36c6 4879 gdb_praddsysset (exitset, SYS_execve);
c3f6f71d
JM
4880#endif
4881#ifdef SYS_execv
37de36c6 4882 gdb_praddsysset (exitset, SYS_execv);
c906108c 4883#endif
37de36c6
KB
4884#ifdef DYNAMIC_SYSCALLS
4885 {
4886 int callnum = find_syscall (pi, "execve");
4887
4888 if (callnum >= 0)
4889 gdb_praddsysset (exitset, callnum);
c906108c 4890
37de36c6
KB
4891 callnum = find_syscall (pi, "ra_execve");
4892 if (callnum >= 0)
4893 gdb_praddsysset (exitset, callnum);
4894 }
4895#endif /* DYNAMIC_SYSCALLS */
4896
4897 if (!proc_set_traced_sysexit (pi, exitset))
c906108c 4898 {
c3f6f71d
JM
4899 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
4900 gdb_flush (gdb_stderr);
4901 _exit (127);
c906108c 4902 }
c3f6f71d
JM
4903#endif /* PRFS_STOPEXEC */
4904
4905 /* FIXME: should this be done in the parent instead? */
4906 /* Turn off inherit on fork flag so that all grand-children
4907 of gdb start with tracing flags cleared. */
4908 if (!proc_unset_inherit_on_fork (pi))
4909 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
4910
4911 /* Turn off run on last close flag, so that the child process
4912 cannot run away just because we close our handle on it.
4913 We want it to wait for the parent to attach. */
4914 if (!proc_unset_run_on_last_close (pi))
4915 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
4916
4917 /* FIXME: No need to destroy the procinfo --
4918 we have our own address space, and we're about to do an exec! */
4919 /*destroy_procinfo (pi);*/
c906108c 4920}
c906108c 4921
c3f6f71d
JM
4922/*
4923 * Function: create_inferior
4924 *
4925 * This function is called BEFORE gdb forks the inferior process.
4926 * Its only real responsibility is to set things up for the fork,
4927 * and tell GDB which two functions to call after the fork (one
4928 * for the parent, and one for the child).
4929 *
4930 * This function does a complicated search for a unix shell program,
4931 * which it then uses to parse arguments and environment variables
4932 * to be sent to the child. I wonder whether this code could not
4933 * be abstracted out and shared with other unix targets such as
4934 * infptrace?
4935 */
c906108c
SS
4936
4937static void
fba45db2 4938procfs_create_inferior (char *exec_file, char *allargs, char **env)
c906108c
SS
4939{
4940 char *shell_file = getenv ("SHELL");
4941 char *tryname;
4942 if (shell_file != NULL && strchr (shell_file, '/') == NULL)
4943 {
4944
4945 /* We will be looking down the PATH to find shell_file. If we
c3f6f71d
JM
4946 just do this the normal way (via execlp, which operates by
4947 attempting an exec for each element of the PATH until it
4948 finds one which succeeds), then there will be an exec for
4949 each failed attempt, each of which will cause a PR_SYSEXIT
4950 stop, and we won't know how to distinguish the PR_SYSEXIT's
4951 for these failed execs with the ones for successful execs
4952 (whether the exec has succeeded is stored at that time in the
4953 carry bit or some such architecture-specific and
4954 non-ABI-specified place).
4955
4956 So I can't think of anything better than to search the PATH
4957 now. This has several disadvantages: (1) There is a race
4958 condition; if we find a file now and it is deleted before we
4959 exec it, we lose, even if the deletion leaves a valid file
4960 further down in the PATH, (2) there is no way to know exactly
4961 what an executable (in the sense of "capable of being
4962 exec'd") file is. Using access() loses because it may lose
4963 if the caller is the superuser; failing to use it loses if
4964 there are ACLs or some such. */
c906108c
SS
4965
4966 char *p;
4967 char *p1;
4968 /* FIXME-maybe: might want "set path" command so user can change what
c3f6f71d 4969 path is used from within GDB. */
c906108c
SS
4970 char *path = getenv ("PATH");
4971 int len;
4972 struct stat statbuf;
4973
4974 if (path == NULL)
4975 path = "/bin:/usr/bin";
4976
4977 tryname = alloca (strlen (path) + strlen (shell_file) + 2);
c3f6f71d 4978 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
c906108c
SS
4979 {
4980 p1 = strchr (p, ':');
4981 if (p1 != NULL)
4982 len = p1 - p;
4983 else
4984 len = strlen (p);
4985 strncpy (tryname, p, len);
4986 tryname[len] = '\0';
4987 strcat (tryname, "/");
4988 strcat (tryname, shell_file);
4989 if (access (tryname, X_OK) < 0)
4990 continue;
4991 if (stat (tryname, &statbuf) < 0)
4992 continue;
4993 if (!S_ISREG (statbuf.st_mode))
4994 /* We certainly need to reject directories. I'm not quite
4995 as sure about FIFOs, sockets, etc., but I kind of doubt
4996 that people want to exec() these things. */
4997 continue;
4998 break;
4999 }
5000 if (p == NULL)
5001 /* Not found. This must be an error rather than merely passing
5002 the file to execlp(), because execlp() would try all the
5003 exec()s, causing GDB to get confused. */
c3f6f71d
JM
5004 error ("procfs:%d -- Can't find shell %s in PATH",
5005 __LINE__, shell_file);
c906108c
SS
5006
5007 shell_file = tryname;
5008 }
5009
c3f6f71d
JM
5010 fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
5011 procfs_init_inferior, NULL, shell_file);
c906108c
SS
5012
5013 /* We are at the first instruction we care about. */
5014 /* Pedal to the metal... */
5015
2acceee2 5016 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
c906108c
SS
5017}
5018
c3f6f71d
JM
5019/*
5020 * Function: notice_thread
5021 *
5022 * Callback for find_new_threads.
5023 * Calls "add_thread".
5024 */
c906108c 5025
c3f6f71d 5026static int
fba45db2 5027procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
c906108c 5028{
39f77062 5029 ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid);
c906108c 5030
c3f6f71d
JM
5031 if (!in_thread_list (gdb_threadid))
5032 add_thread (gdb_threadid);
c906108c 5033
c3f6f71d
JM
5034 return 0;
5035}
5036
5037/*
5038 * Function: target_find_new_threads
5039 *
5040 * Query all the threads that the target knows about,
5041 * and give them back to GDB to add to its list.
5042 */
5043
5044void
fba45db2 5045procfs_find_new_threads (void)
c3f6f71d
JM
5046{
5047 procinfo *pi;
5048
5049 /* Find procinfo for main process */
39f77062 5050 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
5051 proc_update_threads (pi);
5052 proc_iterate_over_threads (pi, procfs_notice_thread, NULL);
c906108c
SS
5053}
5054
c3f6f71d
JM
5055/*
5056 * Function: target_thread_alive
5057 *
5058 * Return true if the thread is still 'alive'.
5059 *
5060 * This guy doesn't really seem to be doing his job.
5061 * Got to investigate how to tell when a thread is really gone.
5062 */
c906108c 5063
c906108c 5064static int
39f77062 5065procfs_thread_alive (ptid_t ptid)
c906108c 5066{
c3f6f71d
JM
5067 int proc, thread;
5068 procinfo *pi;
c906108c 5069
39f77062
KB
5070 proc = PIDGET (ptid);
5071 thread = TIDGET (ptid);
c3f6f71d
JM
5072 /* If I don't know it, it ain't alive! */
5073 if ((pi = find_procinfo (proc, thread)) == NULL)
5074 return 0;
5075
5076 /* If I can't get its status, it ain't alive!
5077 What's more, I need to forget about it! */
5078 if (!proc_get_status (pi))
5079 {
5080 destroy_procinfo (pi);
5081 return 0;
5082 }
5083 /* I couldn't have got its status if it weren't alive, so it's alive. */
5084 return 1;
c906108c 5085}
c3f6f71d
JM
5086
5087/*
5088 * Function: target_pid_to_str
5089 *
5090 * Return a string to be used to identify the thread in
5091 * the "info threads" display.
5092 */
5093
5094char *
39f77062 5095procfs_pid_to_str (ptid_t ptid)
c3f6f71d
JM
5096{
5097 static char buf[80];
5098 int proc, thread;
5099 procinfo *pi;
5100
39f77062
KB
5101 proc = PIDGET (ptid);
5102 thread = TIDGET (ptid);
c3f6f71d
JM
5103 pi = find_procinfo (proc, thread);
5104
5105 if (thread == 0)
5106 sprintf (buf, "Process %d", proc);
5107 else
5108 sprintf (buf, "LWP %d", thread);
5109 return &buf[0];
5110}
5111
5112/*
5113 * Function: procfs_set_watchpoint
5114 * Insert a watchpoint
5115 */
5116
5117int
39f77062
KB
5118procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
5119 int after)
c906108c 5120{
c3f6f71d 5121#ifndef UNIXWARE
37de36c6 5122#ifndef AIX5
c3f6f71d
JM
5123 int pflags = 0;
5124 procinfo *pi;
5125
39f77062
KB
5126 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5127 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
c3f6f71d
JM
5128
5129 /* Translate from GDB's flags to /proc's */
5130 if (len > 0) /* len == 0 means delete watchpoint */
c906108c 5131 {
c3f6f71d
JM
5132 switch (rwflag) { /* FIXME: need an enum! */
5133 case hw_write: /* default watchpoint (write) */
5134 pflags = WRITE_WATCHFLAG;
5135 break;
5136 case hw_read: /* read watchpoint */
5137 pflags = READ_WATCHFLAG;
5138 break;
5139 case hw_access: /* access watchpoint */
5140 pflags = READ_WATCHFLAG | WRITE_WATCHFLAG;
5141 break;
5142 case hw_execute: /* execution HW breakpoint */
5143 pflags = EXEC_WATCHFLAG;
5144 break;
5145 default: /* Something weird. Return error. */
c906108c 5146 return -1;
c3f6f71d
JM
5147 }
5148 if (after) /* Stop after r/w access is completed. */
5149 pflags |= AFTER_WATCHFLAG;
5150 }
5151
5152 if (!proc_set_watchpoint (pi, addr, len, pflags))
5153 {
5154 if (errno == E2BIG) /* Typical error for no resources */
5155 return -1; /* fail */
5156 /* GDB may try to remove the same watchpoint twice.
5157 If a remove request returns no match, don't error. */
c906108c 5158 if (errno == ESRCH && len == 0)
c3f6f71d
JM
5159 return 0; /* ignore */
5160 proc_error (pi, "set_watchpoint", __LINE__);
c906108c 5161 }
37de36c6
KB
5162#endif /* AIX5 */
5163#endif /* UNIXWARE */
c906108c
SS
5164 return 0;
5165}
5166
1e03ad20
KB
5167/* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
5168 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
5169 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
5170 far.
5171
5172 Note: procfs_can_use_hw_breakpoint() is not yet used by all
5173 procfs.c targets due to the fact that some of them still define
5174 TARGET_CAN_USE_HARDWARE_WATCHPOINT. */
5175
5176static int
5177procfs_can_use_hw_breakpoint (int type, int cnt, int othertype)
5178{
5179#ifndef TARGET_HAS_HARDWARE_WATCHPOINTS
5180 return 0;
5181#else
5182 /* Due to the way that proc_set_watchpoint() is implemented, host
5183 and target pointers must be of the same size. If they are not,
5184 we can't use hardware watchpoints. This limitation is due to the
9a043c1d
AC
5185 fact that proc_set_watchpoint() calls
5186 procfs_address_to_host_pointer(); a close inspection of
5187 procfs_address_to_host_pointer will reveal that an internal error
5188 will be generated when the host and target pointer sizes are
5189 different. */
1e03ad20
KB
5190 if (sizeof (void *) != TYPE_LENGTH (builtin_type_void_data_ptr))
5191 return 0;
5192
5193 /* Other tests here??? */
5194
5195 return 1;
5196#endif
5197}
5198
c3f6f71d
JM
5199/*
5200 * Function: stopped_by_watchpoint
5201 *
5202 * Returns non-zero if process is stopped on a hardware watchpoint fault,
5203 * else returns zero.
5204 */
5205
c906108c 5206int
39f77062 5207procfs_stopped_by_watchpoint (ptid_t ptid)
c906108c 5208{
c3f6f71d 5209 procinfo *pi;
c906108c 5210
39f77062
KB
5211 pi = find_procinfo_or_die (PIDGET (ptid) == -1 ?
5212 PIDGET (inferior_ptid) : PIDGET (ptid), 0);
aaeb7efa
MS
5213
5214 if (!pi) /* If no process, then not stopped by watchpoint! */
5215 return 0;
5216
c3f6f71d 5217 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
c906108c 5218 {
c3f6f71d
JM
5219 if (proc_why (pi) == PR_FAULTED)
5220 {
c906108c 5221#ifdef FLTWATCH
c3f6f71d
JM
5222 if (proc_what (pi) == FLTWATCH)
5223 return 1;
c906108c
SS
5224#endif
5225#ifdef FLTKWATCH
c3f6f71d
JM
5226 if (proc_what (pi) == FLTKWATCH)
5227 return 1;
c906108c 5228#endif
c3f6f71d 5229 }
c906108c
SS
5230 }
5231 return 0;
5232}
c906108c 5233
c3f6f71d
JM
5234#ifdef TM_I386SOL2_H
5235/*
5236 * Function: procfs_find_LDT_entry
5237 *
5238 * Input:
39f77062 5239 * ptid_t ptid; // The GDB-style pid-plus-LWP.
c3f6f71d
JM
5240 *
5241 * Return:
5242 * pointer to the corresponding LDT entry.
5243 */
c906108c 5244
c3f6f71d 5245struct ssd *
39f77062 5246procfs_find_LDT_entry (ptid_t ptid)
c906108c 5247{
c3f6f71d
JM
5248 gdb_gregset_t *gregs;
5249 int key;
5250 procinfo *pi;
c906108c 5251
c3f6f71d 5252 /* Find procinfo for the lwp. */
39f77062 5253 if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL)
c906108c 5254 {
39f77062
KB
5255 warning ("procfs_find_LDT_entry: could not find procinfo for %d:%d.",
5256 PIDGET (ptid), TIDGET (ptid));
c3f6f71d 5257 return NULL;
c906108c 5258 }
c3f6f71d
JM
5259 /* get its general registers. */
5260 if ((gregs = proc_get_gregs (pi)) == NULL)
5261 {
39f77062
KB
5262 warning ("procfs_find_LDT_entry: could not read gregs for %d:%d.",
5263 PIDGET (ptid), TIDGET (ptid));
c3f6f71d
JM
5264 return NULL;
5265 }
5266 /* Now extract the GS register's lower 16 bits. */
5267 key = (*gregs)[GS] & 0xffff;
5268
5269 /* Find the matching entry and return it. */
5270 return proc_get_LDT_entry (pi, key);
c906108c 5271}
c3f6f71d 5272#endif /* TM_I386SOL2_H */
c906108c 5273
831e682e
MS
5274/*
5275 * Memory Mappings Functions:
5276 */
5277
5278/*
5279 * Function: iterate_over_mappings
5280 *
5281 * Call a callback function once for each mapping, passing it the mapping,
5282 * an optional secondary callback function, and some optional opaque data.
5283 * Quit and return the first non-zero value returned from the callback.
5284 *
5285 * Arguments:
5286 * pi -- procinfo struct for the process to be mapped.
5287 * func -- callback function to be called by this iterator.
5288 * data -- optional opaque data to be passed to the callback function.
5289 * child_func -- optional secondary function pointer to be passed
5290 * to the child function.
5291 *
5292 * Return: First non-zero return value from the callback function,
5293 * or zero.
5294 */
5295
5296static int
5297iterate_over_mappings (procinfo *pi, int (*child_func) (), void *data,
5298 int (*func) (struct prmap *map,
5299 int (*child_func) (),
5300 void *data))
5301{
5302 char pathname[MAX_PROC_NAME_SIZE];
5303 struct prmap *prmaps;
5304 struct prmap *prmap;
5305 int funcstat;
5306 int map_fd;
5307 int nmap;
5308#ifdef NEW_PROC_API
5309 struct stat sbuf;
5310#endif
5311
5312 /* Get the number of mappings, allocate space,
5313 and read the mappings into prmaps. */
5314#ifdef NEW_PROC_API
5315 /* Open map fd. */
5316 sprintf (pathname, "/proc/%d/map", pi->pid);
5317 if ((map_fd = open (pathname, O_RDONLY)) < 0)
5318 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
5319
5320 /* Make sure it gets closed again. */
5321 make_cleanup_close (map_fd);
5322
5323 /* Use stat to determine the file size, and compute
5324 the number of prmap_t objects it contains. */
5325 if (fstat (map_fd, &sbuf) != 0)
5326 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
5327
5328 nmap = sbuf.st_size / sizeof (prmap_t);
5329 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5330 if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps))
5331 != (nmap * sizeof (*prmaps)))
5332 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
5333#else
5334 /* Use ioctl command PIOCNMAP to get number of mappings. */
5335 if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0)
5336 proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__);
5337
5338 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
5339 if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0)
5340 proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__);
5341#endif
5342
5343 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
5344 if ((funcstat = (*func) (prmap, child_func, data)) != 0)
5345 return funcstat;
5346
5347 return 0;
5348}
5349
5350/*
5351 * Function: solib_mappings_callback
5352 *
5353 * Calls the supplied callback function once for each mapped address
5354 * space in the process. The callback function receives an open
5355 * file descriptor for the file corresponding to that mapped
5356 * address space (if there is one), and the base address of the
5357 * mapped space. Quit when the callback function returns a
5358 * nonzero value, or at teh end of the mappings.
5359 *
5360 * Returns: the first non-zero return value of the callback function,
5361 * or zero.
5362 */
5363
5364int solib_mappings_callback (struct prmap *map,
5365 int (*func) (int, CORE_ADDR),
5366 void *data)
5367{
5368 procinfo *pi = data;
5369 int fd;
5370
5371#ifdef NEW_PROC_API
5372 char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)];
5373
5374 if (map->pr_vaddr == 0 && map->pr_size == 0)
5375 return -1; /* sanity */
5376
5377 if (map->pr_mapname[0] == 0)
5378 {
5379 fd = -1; /* no map file */
5380 }
5381 else
5382 {
5383 sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname);
5384 /* Note: caller's responsibility to close this fd! */
5385 fd = open_with_retry (name, O_RDONLY);
5386 /* Note: we don't test the above call for failure;
5387 we just pass the FD on as given. Sometimes there is
5388 no file, so the open may return failure, but that's
5389 not a problem. */
5390 }
5391#else
5392 fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr);
5393 /* Note: we don't test the above call for failure;
5394 we just pass the FD on as given. Sometimes there is
5395 no file, so the ioctl may return failure, but that's
5396 not a problem. */
5397#endif
5398 return (*func) (fd, (CORE_ADDR) map->pr_vaddr);
5399}
5400
5401/*
5402 * Function: proc_iterate_over_mappings
5403 *
5404 * Uses the unified "iterate_over_mappings" function
5405 * to implement the exported interface to solib-svr4.c.
5406 *
5407 * Given a pointer to a function, call that function once for every
5408 * mapped address space in the process. The callback function
5409 * receives an open file descriptor for the file corresponding to
5410 * that mapped address space (if there is one), and the base address
5411 * of the mapped space. Quit when the callback function returns a
5412 * nonzero value, or at teh end of the mappings.
5413 *
5414 * Returns: the first non-zero return value of the callback function,
5415 * or zero.
5416 */
5417
5418int
5419proc_iterate_over_mappings (int (*func) (int, CORE_ADDR))
5420{
5421 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5422
5423 return iterate_over_mappings (pi, func, pi, solib_mappings_callback);
5424}
5425
be4d1333
MS
5426/*
5427 * Function: find_memory_regions_callback
5428 *
5429 * Implements the to_find_memory_regions method.
5430 * Calls an external function for each memory region.
5431 * External function will have the signiture:
5432 *
5433 * int callback (CORE_ADDR vaddr,
5434 * unsigned long size,
5435 * int read, int write, int execute,
5436 * void *data);
5437 *
5438 * Returns the integer value returned by the callback.
5439 */
5440
5441static int
5442find_memory_regions_callback (struct prmap *map,
5443 int (*func) (CORE_ADDR,
5444 unsigned long,
5445 int, int, int,
5446 void *),
5447 void *data)
5448{
6dbdc4a3 5449 return (*func) ((CORE_ADDR) map->pr_vaddr,
be4d1333
MS
5450 map->pr_size,
5451 (map->pr_mflags & MA_READ) != 0,
5452 (map->pr_mflags & MA_WRITE) != 0,
5453 (map->pr_mflags & MA_EXEC) != 0,
5454 data);
5455}
5456
5457/*
5458 * Function: proc_find_memory_regions
5459 *
5460 * External interface. Calls a callback function once for each
5461 * mapped memory region in the child process, passing as arguments
5462 * CORE_ADDR virtual_address,
5463 * unsigned long size,
5464 * int read, TRUE if region is readable by the child
5465 * int write, TRUE if region is writable by the child
5466 * int execute TRUE if region is executable by the child.
5467 *
5468 * Stops iterating and returns the first non-zero value
5469 * returned by the callback.
5470 */
5471
5472static int
5473proc_find_memory_regions (int (*func) (CORE_ADDR,
5474 unsigned long,
5475 int, int, int,
5476 void *),
5477 void *data)
5478{
5479 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5480
5481 return iterate_over_mappings (pi, func, data,
5482 find_memory_regions_callback);
5483}
5484
388faa48
MS
5485/*
5486 * Function: mappingflags
5487 *
5488 * Returns an ascii representation of a memory mapping's flags.
5489 */
c3f6f71d 5490
388faa48 5491static char *
5ae5f592 5492mappingflags (long flags)
388faa48
MS
5493{
5494 static char asciiflags[8];
5495
5496 strcpy (asciiflags, "-------");
5497#if defined (MA_PHYS)
5498 if (flags & MA_PHYS)
5499 asciiflags[0] = 'd';
5500#endif
5501 if (flags & MA_STACK)
5502 asciiflags[1] = 's';
5503 if (flags & MA_BREAK)
5504 asciiflags[2] = 'b';
5505 if (flags & MA_SHARED)
5506 asciiflags[3] = 's';
5507 if (flags & MA_READ)
5508 asciiflags[4] = 'r';
5509 if (flags & MA_WRITE)
5510 asciiflags[5] = 'w';
5511 if (flags & MA_EXEC)
5512 asciiflags[6] = 'x';
5513 return (asciiflags);
5514}
5515
831e682e
MS
5516/*
5517 * Function: info_mappings_callback
5518 *
5519 * Callback function, does the actual work for 'info proc mappings'.
5520 */
5521
5522/* ARGSUSED */
5523static int
5524info_mappings_callback (struct prmap *map, int (*ignore) (), void *unused)
5525{
5526 char *data_fmt_string;
5527
5528 if (TARGET_ADDR_BIT == 32)
5529 data_fmt_string = "\t%#10lx %#10lx %#10x %#10x %7s\n";
5530 else
5531 data_fmt_string = " %#18lx %#18lx %#10x %#10x %7s\n";
5532
5533 printf_filtered (data_fmt_string,
5534 (unsigned long) map->pr_vaddr,
5535 (unsigned long) map->pr_vaddr + map->pr_size - 1,
5536 map->pr_size,
5537#ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */
5538 (unsigned int) map->pr_offset,
5539#else
5540 map->pr_off,
5541#endif
5542 mappingflags (map->pr_mflags));
5543
5544 return 0;
5545}
5546
388faa48
MS
5547/*
5548 * Function: info_proc_mappings
5549 *
5550 * Implement the "info proc mappings" subcommand.
5551 */
5552
5553static void
5554info_proc_mappings (procinfo *pi, int summary)
5555{
831e682e 5556 char *header_fmt_string;
388faa48
MS
5557
5558 if (TARGET_PTR_BIT == 32)
831e682e 5559 header_fmt_string = "\t%10s %10s %10s %10s %7s\n";
388faa48 5560 else
831e682e 5561 header_fmt_string = " %18s %18s %10s %10s %7s\n";
388faa48
MS
5562
5563 if (summary)
5564 return; /* No output for summary mode. */
5565
388faa48
MS
5566 printf_filtered ("Mapped address spaces:\n\n");
5567 printf_filtered (header_fmt_string,
5568 "Start Addr",
5569 " End Addr",
5570 " Size",
5571 " Offset",
5572 "Flags");
5573
831e682e 5574 iterate_over_mappings (pi, NULL, NULL, info_mappings_callback);
388faa48
MS
5575 printf_filtered ("\n");
5576}
5577
5578/*
5579 * Function: info_proc_cmd
5580 *
5581 * Implement the "info proc" command.
5582 */
c3f6f71d
JM
5583
5584static void
fba45db2 5585info_proc_cmd (char *args, int from_tty)
c906108c 5586{
c3f6f71d 5587 struct cleanup *old_chain;
388faa48
MS
5588 procinfo *process = NULL;
5589 procinfo *thread = NULL;
5590 char **argv = NULL;
5591 char *tmp = NULL;
5592 int pid = 0;
5593 int tid = 0;
5594 int mappings = 0;
c906108c 5595
c3f6f71d
JM
5596 old_chain = make_cleanup (null_cleanup, 0);
5597 if (args)
0fda6bd2
JM
5598 {
5599 if ((argv = buildargv (args)) == NULL)
5600 nomem (0);
5601 else
004527cb 5602 make_cleanup_freeargv (argv);
0fda6bd2 5603 }
c3f6f71d
JM
5604 while (argv != NULL && *argv != NULL)
5605 {
5606 if (isdigit (argv[0][0]))
5607 {
5608 pid = strtoul (argv[0], &tmp, 10);
5609 if (*tmp == '/')
5610 tid = strtoul (++tmp, NULL, 10);
5611 }
5612 else if (argv[0][0] == '/')
5613 {
5614 tid = strtoul (argv[0] + 1, NULL, 10);
5615 }
388faa48
MS
5616 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
5617 {
5618 mappings = 1;
5619 }
c3f6f71d
JM
5620 else
5621 {
5622 /* [...] */
5623 }
5624 argv++;
5625 }
5626 if (pid == 0)
39f77062 5627 pid = PIDGET (inferior_ptid);
c3f6f71d
JM
5628 if (pid == 0)
5629 error ("No current process: you must name one.");
5630 else
c906108c 5631 {
c3f6f71d
JM
5632 /* Have pid, will travel.
5633 First see if it's a process we're already debugging. */
5634 process = find_procinfo (pid, 0);
5635 if (process == NULL)
5636 {
5637 /* No. So open a procinfo for it, but
5638 remember to close it again when finished. */
5639 process = create_procinfo (pid, 0);
004527cb 5640 make_cleanup (do_destroy_procinfo_cleanup, process);
c3f6f71d
JM
5641 if (!open_procinfo_files (process, FD_CTL))
5642 proc_error (process, "info proc, open_procinfo_files", __LINE__);
5643 }
c906108c 5644 }
c3f6f71d
JM
5645 if (tid != 0)
5646 thread = create_procinfo (pid, tid);
5647
5648 if (process)
5649 {
5650 printf_filtered ("process %d flags:\n", process->pid);
5651 proc_prettyprint_flags (proc_flags (process), 1);
5652 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
5653 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
5654 if (proc_get_nthreads (process) > 1)
5655 printf_filtered ("Process has %d threads.\n",
5656 proc_get_nthreads (process));
5657 }
5658 if (thread)
5659 {
5660 printf_filtered ("thread %d flags:\n", thread->tid);
5661 proc_prettyprint_flags (proc_flags (thread), 1);
5662 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
5663 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
5664 }
5665
388faa48
MS
5666 if (mappings)
5667 {
5668 info_proc_mappings (process, 0);
5669 }
5670
c3f6f71d 5671 do_cleanups (old_chain);
c906108c
SS
5672}
5673
c3f6f71d 5674static void
fba45db2 5675proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode)
c906108c 5676{
c3f6f71d
JM
5677 procinfo *pi;
5678 sysset_t *sysset;
5679 int syscallnum = 0;
c906108c 5680
39f77062 5681 if (PIDGET (inferior_ptid) <= 0)
c3f6f71d 5682 error ("you must be debugging a process to use this command.");
c906108c 5683
c3f6f71d
JM
5684 if (args == NULL || args[0] == 0)
5685 error_no_arg ("system call to trace");
5686
39f77062 5687 pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
c3f6f71d
JM
5688 if (isdigit (args[0]))
5689 {
5690 syscallnum = atoi (args);
5691 if (entry_or_exit == PR_SYSENTRY)
5692 sysset = proc_get_traced_sysentry (pi, NULL);
5693 else
5694 sysset = proc_get_traced_sysexit (pi, NULL);
c906108c 5695
c3f6f71d
JM
5696 if (sysset == NULL)
5697 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
c906108c 5698
c3f6f71d 5699 if (mode == FLAG_SET)
37de36c6 5700 gdb_praddsysset (sysset, syscallnum);
c3f6f71d 5701 else
37de36c6 5702 gdb_prdelsysset (sysset, syscallnum);
c906108c 5703
c3f6f71d
JM
5704 if (entry_or_exit == PR_SYSENTRY)
5705 {
5706 if (!proc_set_traced_sysentry (pi, sysset))
5707 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
5708 }
5709 else
5710 {
5711 if (!proc_set_traced_sysexit (pi, sysset))
5712 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
5713 }
5714 }
5715}
5716
5717static void
fba45db2 5718proc_trace_sysentry_cmd (char *args, int from_tty)
c906108c 5719{
c3f6f71d
JM
5720 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
5721}
c906108c 5722
c3f6f71d 5723static void
fba45db2 5724proc_trace_sysexit_cmd (char *args, int from_tty)
c3f6f71d
JM
5725{
5726 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
c906108c 5727}
c906108c 5728
c3f6f71d 5729static void
fba45db2 5730proc_untrace_sysentry_cmd (char *args, int from_tty)
c3f6f71d
JM
5731{
5732 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
5733}
5734
5735static void
fba45db2 5736proc_untrace_sysexit_cmd (char *args, int from_tty)
c906108c 5737{
c3f6f71d
JM
5738 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
5739}
c906108c 5740
c906108c 5741
c906108c 5742void
fba45db2 5743_initialize_procfs (void)
c906108c 5744{
c906108c
SS
5745 init_procfs_ops ();
5746 add_target (&procfs_ops);
c3f6f71d 5747 add_info ("proc", info_proc_cmd,
388faa48
MS
5748 "Show /proc process information about any running process.\n\
5749Specify process id, or use the program being debugged by default.\n\
5750Specify keyword 'mappings' for detailed info on memory mappings.");
c3f6f71d
JM
5751 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
5752 "Give a trace of entries into the syscall.");
5753 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
5754 "Give a trace of exits from the syscall.");
5755 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
5756 "Cancel a trace of entries into the syscall.");
5757 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
5758 "Cancel a trace of exits from the syscall.");
c3f6f71d
JM
5759}
5760
5761/* =================== END, GDB "MODULE" =================== */
5762
5763
5764
65554fef 5765/* miscellaneous stubs: */
c3f6f71d
JM
5766/* The following satisfy a few random symbols mostly created by */
5767/* the solaris threads implementation, which I will chase down */
5768/* later. */
5769
5770/*
5771 * Return a pid for which we guarantee
5772 * we will be able to find a 'live' procinfo.
5773 */
5774
39f77062 5775ptid_t
fba45db2 5776procfs_first_available (void)
c3f6f71d 5777{
39f77062 5778 return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1);
c3f6f71d 5779}
be4d1333
MS
5780
5781/* =================== GCORE .NOTE "MODULE" =================== */
65554fef
MS
5782#if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT)
5783/* gcore only implemented on solaris and unixware (so far) */
be4d1333
MS
5784
5785static char *
5786procfs_do_thread_registers (bfd *obfd, ptid_t ptid,
5787 char *note_data, int *note_size)
5788{
5789 gdb_gregset_t gregs;
5790 gdb_fpregset_t fpregs;
5791 unsigned long merged_pid;
5792
5793 merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid);
5794
5795 fill_gregset (&gregs, -1);
65554fef
MS
5796#if defined (UNIXWARE)
5797 note_data = (char *) elfcore_write_lwpstatus (obfd,
5798 note_data,
5799 note_size,
5800 merged_pid,
5801 stop_signal,
5802 &gregs);
5803#else
be4d1333 5804 note_data = (char *) elfcore_write_prstatus (obfd,
65554fef
MS
5805 note_data,
5806 note_size,
be4d1333
MS
5807 merged_pid,
5808 stop_signal,
65554fef
MS
5809 &gregs);
5810#endif
be4d1333
MS
5811 fill_fpregset (&fpregs, -1);
5812 note_data = (char *) elfcore_write_prfpreg (obfd,
5813 note_data,
5814 note_size,
5815 &fpregs,
5816 sizeof (fpregs));
5817 return note_data;
5818}
5819
5820struct procfs_corefile_thread_data {
5821 bfd *obfd;
5822 char *note_data;
5823 int *note_size;
5824};
5825
5826static int
65554fef 5827procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data)
be4d1333
MS
5828{
5829 struct procfs_corefile_thread_data *args = data;
be4d1333 5830
65554fef 5831 if (pi != NULL && thread->tid != 0)
be4d1333
MS
5832 {
5833 ptid_t saved_ptid = inferior_ptid;
65554fef
MS
5834 inferior_ptid = MERGEPID (pi->pid, thread->tid);
5835 args->note_data = procfs_do_thread_registers (args->obfd, inferior_ptid,
be4d1333
MS
5836 args->note_data,
5837 args->note_size);
5838 inferior_ptid = saved_ptid;
5839 }
5840 return 0;
5841}
5842
5843static char *
5844procfs_make_note_section (bfd *obfd, int *note_size)
5845{
5846 struct cleanup *old_chain;
5847 gdb_gregset_t gregs;
5848 gdb_fpregset_t fpregs;
5849 char fname[16] = {'\0'};
5850 char psargs[80] = {'\0'};
5851 procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0);
5852 char *note_data = NULL;
6dbdc4a3 5853 char *inf_args;
be4d1333
MS
5854 struct procfs_corefile_thread_data thread_args;
5855
5856 if (get_exec_file (0))
5857 {
5858 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
5859 strncpy (psargs, get_exec_file (0),
5860 sizeof (psargs));
6dbdc4a3
MS
5861
5862 inf_args = get_inferior_args ();
5863 if (inf_args && *inf_args &&
5864 strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs)))
be4d1333
MS
5865 {
5866 strncat (psargs, " ",
5867 sizeof (psargs) - strlen (psargs));
6dbdc4a3 5868 strncat (psargs, inf_args,
be4d1333
MS
5869 sizeof (psargs) - strlen (psargs));
5870 }
5871 }
5872
5873 note_data = (char *) elfcore_write_prpsinfo (obfd,
5874 note_data,
5875 note_size,
5876 fname,
5877 psargs);
5878
65554fef
MS
5879#ifdef UNIXWARE
5880 fill_gregset (&gregs, -1);
5881 note_data = elfcore_write_pstatus (obfd, note_data, note_size,
5882 PIDGET (inferior_ptid),
5883 stop_signal, &gregs);
5884#endif
5885
be4d1333
MS
5886 thread_args.obfd = obfd;
5887 thread_args.note_data = note_data;
5888 thread_args.note_size = note_size;
65554fef
MS
5889 proc_iterate_over_threads (pi, procfs_corefile_thread_callback, &thread_args);
5890
be4d1333
MS
5891 if (thread_args.note_data == note_data)
5892 {
5893 /* iterate_over_threads didn't come up with any threads;
5894 just use inferior_ptid. */
5895 note_data = procfs_do_thread_registers (obfd, inferior_ptid,
5896 note_data, note_size);
5897 }
5898 else
5899 {
5900 note_data = thread_args.note_data;
5901 }
5902
5903 make_cleanup (xfree, note_data);
5904 return note_data;
5905}
65554fef
MS
5906#else /* !(Solaris or Unixware) */
5907static char *
5908procfs_make_note_section (bfd *obfd, int *note_size)
5909{
5910 error ("gcore not implemented for this host.");
5911 return NULL; /* lint */
5912}
5913#endif /* Solaris or Unixware */
be4d1333 5914/* =================== END GCORE .NOTE "MODULE" =================== */
This page took 0.827093 seconds and 4 git commands to generate.