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