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