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