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