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