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