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