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