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