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