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