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