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