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