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