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