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