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