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