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