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