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