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