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