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