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