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