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