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