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