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