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