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