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