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