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