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