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