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