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