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