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