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