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