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