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