GDB (xrefs)
Loading...
Searching...
No Matches
procfs.c
Go to the documentation of this file.
1/* Machine independent support for Solaris /proc (process file system) for GDB.
2
3 Copyright (C) 1999-2023 Free Software Foundation, Inc.
4
5 Written by Michael Snyder at Cygnus Solutions.
6 Based on work by Fred Fish, Stu Grossman, Geoff Noer, and others.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23#include "defs.h"
24#include "inferior.h"
25#include "infrun.h"
26#include "target.h"
27#include "gdbcore.h"
28#include "elf-bfd.h"
29#include "gdbcmd.h"
30#include "gdbthread.h"
31#include "regcache.h"
32#include "inf-child.h"
33#include "nat/fork-inferior.h"
34#include "gdbarch.h"
35
36#include <sys/procfs.h>
37#include <sys/fault.h>
38#include <sys/syscall.h>
39#include "gdbsupport/gdb_wait.h"
40#include <signal.h>
41#include <ctype.h>
42#include "gdb_bfd.h"
43#include "auxv.h"
44#include "procfs.h"
45#include "observable.h"
46#include "gdbsupport/scoped_fd.h"
47#include "gdbsupport/pathstuff.h"
48#include "gdbsupport/buildargv.h"
49#include "cli/cli-style.h"
50
51/* This module provides the interface between GDB and the
52 /proc file system, which is used on many versions of Unix
53 as a means for debuggers to control other processes.
54
55 /proc works by imitating a file system: you open a simulated file
56 that represents the process you wish to interact with, and perform
57 operations on that "file" in order to examine or change the state
58 of the other process.
59
60 The most important thing to know about /proc and this module is
61 that there are two very different interfaces to /proc:
62
63 One that uses the ioctl system call, and another that uses read
64 and write system calls.
65
66 This module supports only the Solaris version of the read/write
67 interface. */
68
69#include <sys/types.h>
70#include <dirent.h>
71
72#include <fcntl.h>
73#include <unistd.h>
74#include <sys/stat.h>
75
76/* Note: procfs-utils.h must be included after the above system header
77 files, because it redefines various system calls using macros.
78 This may be incompatible with the prototype declarations. */
79
80#include "proc-utils.h"
81
82/* Prototypes for supply_gregset etc. */
83#include "gregset.h"
84
85/* =================== TARGET_OPS "MODULE" =================== */
86
87/* This module defines the GDB target vector and its methods. */
88
89
90static enum target_xfer_status procfs_xfer_memory (gdb_byte *,
91 const gdb_byte *,
92 ULONGEST, ULONGEST,
93 ULONGEST *);
94
95class procfs_target final : public inf_child_target
96{
97public:
98 void create_inferior (const char *, const std::string &,
99 char **, int) override;
100
101 void kill () override;
102
103 void mourn_inferior () override;
104
105 void attach (const char *, int) override;
106 void detach (inferior *inf, int) override;
107
108 void resume (ptid_t, int, enum gdb_signal) override;
109 ptid_t wait (ptid_t, struct target_waitstatus *, target_wait_flags) override;
110
111 void fetch_registers (struct regcache *, int) override;
112 void store_registers (struct regcache *, int) override;
113
115 const char *annex,
116 gdb_byte *readbuf,
117 const gdb_byte *writebuf,
118 ULONGEST offset, ULONGEST len,
119 ULONGEST *xfered_len) override;
120
121 void pass_signals (gdb::array_view<const unsigned char>) override;
122
123 void files_info () override;
124
125 void update_thread_list () override;
126
127 bool thread_alive (ptid_t ptid) override;
128
129 std::string pid_to_str (ptid_t) override;
130
131 const char *pid_to_exec_file (int pid) override;
132
135
136 /* find_memory_regions support method for gcore */
138 override;
139
140 gdb::unique_xmalloc_ptr<char> make_corefile_notes (bfd *, int *) override;
141
142 bool info_proc (const char *, enum info_proc_what) override;
143
144#if PR_MODEL_NATIVE == PR_MODEL_LP64
145 int auxv_parse (const gdb_byte **readptr,
146 const gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
147 override;
148#endif
149
150 bool stopped_by_watchpoint () override;
151
152 int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
153 struct expression *) override;
154
155 int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
156 struct expression *) override;
157
158 int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
159
160 int can_use_hw_breakpoint (enum bptype, int, int) override;
161 bool stopped_data_address (CORE_ADDR *) override;
162
163 void procfs_init_inferior (int pid);
164};
165
167
168#if PR_MODEL_NATIVE == PR_MODEL_LP64
169/* When GDB is built as 64-bit application on Solaris, the auxv data
170 is presented in 64-bit format. We need to provide a custom parser
171 to handle that. */
172int
173procfs_target::auxv_parse (const gdb_byte **readptr,
174 const gdb_byte *endptr, CORE_ADDR *typep,
175 CORE_ADDR *valp)
176{
177 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
178 const gdb_byte *ptr = *readptr;
179
180 if (endptr == ptr)
181 return 0;
182
183 if (endptr - ptr < 8 * 2)
184 return -1;
185
186 *typep = extract_unsigned_integer (ptr, 4, byte_order);
187 ptr += 8;
188 /* The size of data is always 64-bit. If the application is 32-bit,
189 it will be zero extended, as expected. */
190 *valp = extract_unsigned_integer (ptr, 8, byte_order);
191 ptr += 8;
192
193 *readptr = ptr;
194 return 1;
195}
196#endif
197
198/* =================== END, TARGET_OPS "MODULE" =================== */
199
200/* =================== STRUCT PROCINFO "MODULE" =================== */
201
202 /* FIXME: this comment will soon be out of date W.R.T. threads. */
203
204/* The procinfo struct is a wrapper to hold all the state information
205 concerning a /proc process. There should be exactly one procinfo
206 for each process, and since GDB currently can debug only one
207 process at a time, that means there should be only one procinfo.
208 All of the LWP's of a process can be accessed indirectly thru the
209 single process procinfo.
210
211 However, against the day when GDB may debug more than one process,
212 this data structure is kept in a list (which for now will hold no
213 more than one member), and many functions will have a pointer to a
214 procinfo as an argument.
215
216 There will be a separate procinfo structure for use by the (not yet
217 implemented) "info proc" command, so that we can print useful
218 information about any random process without interfering with the
219 inferior's procinfo information. */
220
221/* format strings for /proc paths */
222#define CTL_PROC_NAME_FMT "/proc/%d/ctl"
223#define AS_PROC_NAME_FMT "/proc/%d/as"
224#define MAP_PROC_NAME_FMT "/proc/%d/map"
225#define STATUS_PROC_NAME_FMT "/proc/%d/status"
226#define MAX_PROC_NAME_SIZE sizeof("/proc/999999/lwp/0123456789/lwpstatus")
227
228typedef struct procinfo {
229 struct procinfo *next;
230 int pid; /* Process ID */
231 int tid; /* Thread/LWP id */
232
233 /* process state */
236
237 int ctl_fd; /* File descriptor for /proc control file */
238 int status_fd; /* File descriptor for /proc status file */
239 int as_fd; /* File descriptor for /proc as file */
240
241 char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */
242
243 fltset_t saved_fltset; /* Saved traced hardware fault set */
244 sigset_t saved_sigset; /* Saved traced signal set */
245 sigset_t saved_sighold; /* Saved held signal set */
246 sysset_t *saved_exitset; /* Saved traced system call exit set */
247 sysset_t *saved_entryset; /* Saved traced system call entry set */
248
249 pstatus_t prstatus; /* Current process status info */
250
252
254 int gregs_valid : 1;
258
259/* Function prototypes for procinfo module: */
260
261static procinfo *find_procinfo_or_die (int pid, int tid);
262static procinfo *find_procinfo (int pid, int tid);
263static procinfo *create_procinfo (int pid, int tid);
264static void destroy_procinfo (procinfo *p);
265static void dead_procinfo (procinfo *p, const char *msg, int killp);
266static int open_procinfo_files (procinfo *p, int which);
267static void close_procinfo_files (procinfo *p);
268
269static int iterate_over_mappings
270 (procinfo *pi, find_memory_region_ftype child_func, void *data,
271 int (*func) (struct prmap *map, find_memory_region_ftype child_func,
272 void *data));
273
274/* The head of the procinfo list: */
276
277/* Search the procinfo list. Return a pointer to procinfo, or NULL if
278 not found. */
279
280static procinfo *
282{
283 procinfo *pi;
284
285 for (pi = procinfo_list; pi; pi = pi->next)
286 if (pi->pid == pid)
287 break;
288
289 if (pi)
290 if (tid)
291 {
292 /* Don't check threads_valid. If we're updating the
293 thread_list, we want to find whatever threads are already
294 here. This means that in general it is the caller's
295 responsibility to check threads_valid and update before
296 calling find_procinfo, if the caller wants to find a new
297 thread. */
298
299 for (pi = pi->thread_list; pi; pi = pi->next)
300 if (pi->tid == tid)
301 break;
302 }
303
304 return pi;
305}
306
307/* Calls find_procinfo, but errors on failure. */
308
309static procinfo *
311{
312 procinfo *pi = find_procinfo (pid, tid);
313
314 if (pi == NULL)
315 {
316 if (tid)
317 error (_("procfs: couldn't find pid %d "
318 "(kernel thread %d) in procinfo list."),
319 pid, tid);
320 else
321 error (_("procfs: couldn't find pid %d in procinfo list."), pid);
322 }
323 return pi;
324}
325
326/* Wrapper for `open'. The appropriate open call is attempted; if
327 unsuccessful, it will be retried as many times as needed for the
328 EAGAIN and EINTR conditions.
329
330 For other conditions, retry the open a limited number of times. In
331 addition, a short sleep is imposed prior to retrying the open. The
332 reason for this sleep is to give the kernel a chance to catch up
333 and create the file in question in the event that GDB "wins" the
334 race to open a file before the kernel has created it. */
335
336static int
338{
339 int retries_remaining, status;
340
341 retries_remaining = 2;
342
343 while (1)
344 {
345 status = open (pathname, flags);
346
347 if (status >= 0 || retries_remaining == 0)
348 break;
349 else if (errno != EINTR && errno != EAGAIN)
350 {
351 retries_remaining--;
352 sleep (1);
353 }
354 }
355
356 return status;
357}
358
359/* Open the file descriptor for the process or LWP. We only open the
360 control file descriptor; the others are opened lazily as needed.
361 Returns the file descriptor, or zero for failure. */
362
364
365static int
367{
368 char tmp[MAX_PROC_NAME_SIZE];
369 int fd;
370
371 /* This function is getting ALMOST long enough to break up into
372 several. Here is some rationale:
373
374 There are several file descriptors that may need to be open
375 for any given process or LWP. The ones we're interested in are:
376 - control (ctl) write-only change the state
377 - status (status) read-only query the state
378 - address space (as) read/write access memory
379 - map (map) read-only virtual addr map
380 Most of these are opened lazily as they are needed.
381 The pathnames for the 'files' for an LWP look slightly
382 different from those of a first-class process:
383 Pathnames for a process (<proc-id>):
384 /proc/<proc-id>/ctl
385 /proc/<proc-id>/status
386 /proc/<proc-id>/as
387 /proc/<proc-id>/map
388 Pathnames for an LWP (lwp-id):
389 /proc/<proc-id>/lwp/<lwp-id>/lwpctl
390 /proc/<proc-id>/lwp/<lwp-id>/lwpstatus
391 An LWP has no map or address space file descriptor, since
392 the memory map and address space are shared by all LWPs. */
393
394 /* In this case, there are several different file descriptors that
395 we might be asked to open. The control file descriptor will be
396 opened early, but the others will be opened lazily as they are
397 needed. */
398
399 strcpy (tmp, pi->pathname);
400 switch (which) { /* Which file descriptor to open? */
401 case FD_CTL:
402 if (pi->tid)
403 strcat (tmp, "/lwpctl");
404 else
405 strcat (tmp, "/ctl");
406 fd = open_with_retry (tmp, O_WRONLY);
407 if (fd < 0)
408 return 0; /* fail */
409 pi->ctl_fd = fd;
410 break;
411 case FD_AS:
412 if (pi->tid)
413 return 0; /* There is no 'as' file descriptor for an lwp. */
414 strcat (tmp, "/as");
415 fd = open_with_retry (tmp, O_RDWR);
416 if (fd < 0)
417 return 0; /* fail */
418 pi->as_fd = fd;
419 break;
420 case FD_STATUS:
421 if (pi->tid)
422 strcat (tmp, "/lwpstatus");
423 else
424 strcat (tmp, "/status");
425 fd = open_with_retry (tmp, O_RDONLY);
426 if (fd < 0)
427 return 0; /* fail */
428 pi->status_fd = fd;
429 break;
430 default:
431 return 0; /* unknown file descriptor */
432 }
433
434 return 1; /* success */
435}
436
437/* Allocate a data structure and link it into the procinfo list.
438 First tries to find a pre-existing one (FIXME: why?). Returns the
439 pointer to new procinfo struct. */
440
441static procinfo *
443{
444 procinfo *pi, *parent = NULL;
445
446 pi = find_procinfo (pid, tid);
447 if (pi != NULL)
448 return pi; /* Already exists, nothing to do. */
449
450 /* Find parent before doing malloc, to save having to cleanup. */
451 if (tid != 0)
452 parent = find_procinfo_or_die (pid, 0); /* FIXME: should I
453 create it if it
454 doesn't exist yet? */
455
456 pi = XNEW (procinfo);
457 memset (pi, 0, sizeof (procinfo));
458 pi->pid = pid;
459 pi->tid = tid;
460
461 pi->saved_entryset = XNEW (sysset_t);
462 pi->saved_exitset = XNEW (sysset_t);
463
464 /* Chain into list. */
465 if (tid == 0)
466 {
467 xsnprintf (pi->pathname, sizeof (pi->pathname), "/proc/%d", pid);
468 pi->next = procinfo_list;
469 procinfo_list = pi;
470 }
471 else
472 {
473 xsnprintf (pi->pathname, sizeof (pi->pathname), "/proc/%d/lwp/%d",
474 pid, tid);
475 pi->next = parent->thread_list;
476 parent->thread_list = pi;
477 }
478 return pi;
479}
480
481/* Close all file descriptors associated with the procinfo. */
482
483static void
485{
486 if (pi->ctl_fd > 0)
487 close (pi->ctl_fd);
488 if (pi->as_fd > 0)
489 close (pi->as_fd);
490 if (pi->status_fd > 0)
491 close (pi->status_fd);
492 pi->ctl_fd = pi->as_fd = pi->status_fd = 0;
493}
494
495/* Destructor function. Close, unlink and deallocate the object. */
496
497static void
499{
500 procinfo *ptr;
501
502 /* Step one: unlink the procinfo from its list. */
503 if (pi == *list)
504 *list = pi->next;
505 else
506 for (ptr = *list; ptr; ptr = ptr->next)
507 if (ptr->next == pi)
508 {
509 ptr->next = pi->next;
510 break;
511 }
512
513 /* Step two: close any open file descriptors. */
515
516 /* Step three: free the memory. */
517 xfree (pi->saved_entryset);
518 xfree (pi->saved_exitset);
519 xfree (pi);
520}
521
522static void
524{
525 procinfo *tmp;
526
527 if (pi->tid != 0) /* Destroy a thread procinfo. */
528 {
529 tmp = find_procinfo (pi->pid, 0); /* Find the parent process. */
531 }
532 else /* Destroy a process procinfo and all its threads. */
533 {
534 /* First destroy the children, if any; */
535 while (pi->thread_list != NULL)
537 /* Then destroy the parent. Genocide!!! */
539 }
540}
541
542/* A deleter that calls destroy_procinfo. */
544{
545 void operator() (procinfo *pi) const
546 {
547 destroy_procinfo (pi);
548 }
549};
550
551typedef std::unique_ptr<procinfo, procinfo_deleter> procinfo_up;
552
553enum { NOKILL, KILL };
554
555/* To be called on a non_recoverable error for a procinfo. Prints
556 error messages, optionally sends a SIGKILL to the process, then
557 destroys the data structure. */
558
559static void
560dead_procinfo (procinfo *pi, const char *msg, int kill_p)
561{
563 if (kill_p == KILL)
564 kill (pi->pid, SIGKILL);
565
566 destroy_procinfo (pi);
567 error ("%s", msg);
568}
569
570/* =================== END, STRUCT PROCINFO "MODULE" =================== */
571
572/* =================== /proc "MODULE" =================== */
573
574/* This "module" is the interface layer between the /proc system API
575 and the gdb target vector functions. This layer consists of access
576 functions that encapsulate each of the basic operations that we
577 need to use from the /proc API.
578
579 The main motivation for this layer is to hide the fact that there
580 were two very different implementations of the /proc API. */
581
582static long proc_flags (procinfo *pi);
583static int proc_why (procinfo *pi);
584static int proc_what (procinfo *pi);
585static int proc_set_current_signal (procinfo *pi, int signo);
586static int proc_get_current_thread (procinfo *pi);
588 (procinfo *pi,
589 int (*func) (procinfo *, procinfo *, void *),
590 void *ptr);
591static void proc_resume (procinfo *pi, ptid_t scope_ptid,
592 int step, enum gdb_signal signo);
593
594static void
595proc_warn (procinfo *pi, const char *func, int line)
596{
597 int saved_errno = errno;
598 warning ("procfs: %s line %d, %ps: %s",
600 pi->pathname),
601 safe_strerror (saved_errno));
602}
603
604static void
605proc_error (procinfo *pi, const char *func, int line)
606{
607 int saved_errno = errno;
608 error ("procfs: %s line %d, %ps: %s",
610 pi->pathname),
611 safe_strerror (saved_errno));
612}
613
614/* Updates the status struct in the procinfo. There is a 'valid'
615 flag, to let other functions know when this function needs to be
616 called (so the status is only read when it is needed). The status
617 file descriptor is also only opened when it is needed. Returns
618 non-zero for success, zero for failure. */
619
620static int
622{
623 /* Status file descriptor is opened "lazily". */
624 if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0)
625 {
626 pi->status_valid = 0;
627 return 0;
628 }
629
630 if (lseek (pi->status_fd, 0, SEEK_SET) < 0)
631 pi->status_valid = 0; /* fail */
632 else
633 {
634 /* Sigh... I have to read a different data structure,
635 depending on whether this is a main process or an LWP. */
636 if (pi->tid)
637 pi->status_valid = (read (pi->status_fd,
638 (char *) &pi->prstatus.pr_lwp,
639 sizeof (lwpstatus_t))
640 == sizeof (lwpstatus_t));
641 else
642 {
643 pi->status_valid = (read (pi->status_fd,
644 (char *) &pi->prstatus,
645 sizeof (pstatus_t))
646 == sizeof (pstatus_t));
647 }
648 }
649
650 if (pi->status_valid)
651 {
653 proc_why (pi),
654 proc_what (pi),
656 }
657
658 /* The status struct includes general regs, so mark them valid too. */
659 pi->gregs_valid = pi->status_valid;
660 /* In the read/write multiple-fd model, the status struct includes
661 the fp regs too, so mark them valid too. */
662 pi->fpregs_valid = pi->status_valid;
663 return pi->status_valid; /* True if success, false if failure. */
664}
665
666/* Returns the process flags (pr_flags field). */
667
668static long
670{
671 if (!pi->status_valid)
672 if (!proc_get_status (pi))
673 return 0; /* FIXME: not a good failure value (but what is?) */
674
675 return pi->prstatus.pr_lwp.pr_flags;
676}
677
678/* Returns the pr_why field (why the process stopped). */
679
680static int
682{
683 if (!pi->status_valid)
684 if (!proc_get_status (pi))
685 return 0; /* FIXME: not a good failure value (but what is?) */
686
687 return pi->prstatus.pr_lwp.pr_why;
688}
689
690/* Returns the pr_what field (details of why the process stopped). */
691
692static int
694{
695 if (!pi->status_valid)
696 if (!proc_get_status (pi))
697 return 0; /* FIXME: not a good failure value (but what is?) */
698
699 return pi->prstatus.pr_lwp.pr_what;
700}
701
702/* This function is only called when PI is stopped by a watchpoint.
703 Assuming the OS supports it, write to *ADDR the data address which
704 triggered it and return 1. Return 0 if it is not possible to know
705 the address. */
706
707static int
708proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr)
709{
710 if (!pi->status_valid)
711 if (!proc_get_status (pi))
712 return 0;
713
714 *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch (),
716 (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr);
717 return 1;
718}
719
720/* Returns the pr_nsysarg field (number of args to the current
721 syscall). */
722
723static int
725{
726 if (!pi->status_valid)
727 if (!proc_get_status (pi))
728 return 0;
729
730 return pi->prstatus.pr_lwp.pr_nsysarg;
731}
732
733/* Returns the pr_sysarg field (pointer to the arguments of current
734 syscall). */
735
736static long *
738{
739 if (!pi->status_valid)
740 if (!proc_get_status (pi))
741 return NULL;
742
743 return (long *) &pi->prstatus.pr_lwp.pr_sysarg;
744}
745
746/* Set or reset any of the following process flags:
747 PR_FORK -- forked child will inherit trace flags
748 PR_RLC -- traced process runs when last /proc file closed.
749 PR_KLC -- traced process is killed when last /proc file closed.
750 PR_ASYNC -- LWP's get to run/stop independently.
751
752 This function is done using read/write [PCSET/PCRESET/PCUNSET].
753
754 Arguments:
755 pi -- the procinfo
756 flag -- one of PR_FORK, PR_RLC, or PR_ASYNC
757 mode -- 1 for set, 0 for reset.
758
759 Returns non-zero for success, zero for failure. */
760
762
763static int
764proc_modify_flag (procinfo *pi, long flag, long mode)
765{
766 long win = 0; /* default to fail */
767
768 /* These operations affect the process as a whole, and applying them
769 to an individual LWP has the same meaning as applying them to the
770 main process. Therefore, if we're ever called with a pointer to
771 an LWP's procinfo, let's substitute the process's procinfo and
772 avoid opening the LWP's file descriptor unnecessarily. */
773
774 if (pi->pid != 0)
775 pi = find_procinfo_or_die (pi->pid, 0);
776
777 procfs_ctl_t arg[2];
778
779 if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC). */
780 arg[0] = PCSET;
781 else /* Reset the flag. */
782 arg[0] = PCUNSET;
783
784 arg[1] = flag;
785 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
786
787 /* The above operation renders the procinfo's cached pstatus
788 obsolete. */
789 pi->status_valid = 0;
790
791 if (!win)
792 warning (_("procfs: modify_flag failed to turn %s %s"),
793 flag == PR_FORK ? "PR_FORK" :
794 flag == PR_RLC ? "PR_RLC" :
795 flag == PR_ASYNC ? "PR_ASYNC" :
796 flag == PR_KLC ? "PR_KLC" :
797 "<unknown flag>",
798 mode == FLAG_RESET ? "off" : "on");
799
800 return win;
801}
802
803/* Set the run_on_last_close flag. Process with all threads will
804 become runnable when debugger closes all /proc fds. Returns
805 non-zero for success, zero for failure. */
806
807static int
809{
810 return proc_modify_flag (pi, PR_RLC, FLAG_SET);
811}
812
813/* Reset the run_on_last_close flag. The process will NOT become
814 runnable when debugger closes its file handles. Returns non-zero
815 for success, zero for failure. */
816
817static int
819{
820 return proc_modify_flag (pi, PR_RLC, FLAG_RESET);
821}
822
823/* Reset inherit_on_fork flag. If the process forks a child while we
824 are registered for events in the parent, then we will NOT receive
825 events from the child. Returns non-zero for success, zero for
826 failure. */
827
828static int
830{
831 return proc_modify_flag (pi, PR_FORK, FLAG_RESET);
832}
833
834/* Set PR_ASYNC flag. If one LWP stops because of a debug event
835 (signal etc.), the remaining LWPs will continue to run. Returns
836 non-zero for success, zero for failure. */
837
838static int
840{
841 return proc_modify_flag (pi, PR_ASYNC, FLAG_SET);
842}
843
844/* Reset PR_ASYNC flag. If one LWP stops because of a debug event
845 (signal etc.), then all other LWPs will stop as well. Returns
846 non-zero for success, zero for failure. */
847
848static int
850{
851 return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET);
852}
853
854/* Request the process/LWP to stop. Does not wait. Returns non-zero
855 for success, zero for failure. */
856
857static int
859{
860 int win;
861
862 /* We might conceivably apply this operation to an LWP, and the
863 LWP's ctl file descriptor might not be open. */
864
865 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
866 return 0;
867 else
868 {
869 procfs_ctl_t cmd = PCSTOP;
870
871 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
872 }
873
874 return win;
875}
876
877/* Wait for the process or LWP to stop (block until it does). Returns
878 non-zero for success, zero for failure. */
879
880static int
882{
883 int win;
884
885 /* We should never have to apply this operation to any procinfo
886 except the one for the main process. If that ever changes for
887 any reason, then take out the following clause and replace it
888 with one that makes sure the ctl_fd is open. */
889
890 if (pi->tid != 0)
891 pi = find_procinfo_or_die (pi->pid, 0);
892
893 procfs_ctl_t cmd = PCWSTOP;
894
896
897 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
898
900
901 /* We been runnin' and we stopped -- need to update status. */
902 pi->status_valid = 0;
903
904 return win;
905}
906
907/* Make the process or LWP runnable.
908
909 Options (not all are implemented):
910 - single-step
911 - clear current fault
912 - clear current signal
913 - abort the current system call
914 - stop as soon as finished with system call
915
916 Always clears the current fault. PI is the process or LWP to
917 operate on. If STEP is true, set the process or LWP to trap after
918 one instruction. If SIGNO is zero, clear the current signal if
919 any; if non-zero, set the current signal to this one. Returns
920 non-zero for success, zero for failure. */
921
922static int
923proc_run_process (procinfo *pi, int step, int signo)
924{
925 int win;
926 int runflags;
927
928 /* We will probably have to apply this operation to individual
929 threads, so make sure the control file descriptor is open. */
930
931 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
932 return 0;
933
934 runflags = PRCFAULT; /* Always clear current fault. */
935 if (step)
936 runflags |= PRSTEP;
937 if (signo == 0)
938 runflags |= PRCSIG;
939 else if (signo != -1) /* -1 means do nothing W.R.T. signals. */
940 proc_set_current_signal (pi, signo);
941
942 procfs_ctl_t cmd[2];
943
944 cmd[0] = PCRUN;
945 cmd[1] = runflags;
946 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
947
948 return win;
949}
950
951/* Register to trace signals in the process or LWP. Returns non-zero
952 for success, zero for failure. */
953
954static int
955proc_set_traced_signals (procinfo *pi, sigset_t *sigset)
956{
957 int win;
958
959 /* We should never have to apply this operation to any procinfo
960 except the one for the main process. If that ever changes for
961 any reason, then take out the following clause and replace it
962 with one that makes sure the ctl_fd is open. */
963
964 if (pi->tid != 0)
965 pi = find_procinfo_or_die (pi->pid, 0);
966
967 struct {
968 procfs_ctl_t cmd;
969 /* Use char array to avoid alignment issues. */
970 char sigset[sizeof (sigset_t)];
971 } arg;
972
973 arg.cmd = PCSTRACE;
974 memcpy (&arg.sigset, sigset, sizeof (sigset_t));
975
976 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
977
978 /* The above operation renders the procinfo's cached pstatus obsolete. */
979 pi->status_valid = 0;
980
981 if (!win)
982 warning (_("procfs: set_traced_signals failed"));
983 return win;
984}
985
986/* Register to trace hardware faults in the process or LWP. Returns
987 non-zero for success, zero for failure. */
988
989static int
990proc_set_traced_faults (procinfo *pi, fltset_t *fltset)
991{
992 int win;
993
994 /* We should never have to apply this operation to any procinfo
995 except the one for the main process. If that ever changes for
996 any reason, then take out the following clause and replace it
997 with one that makes sure the ctl_fd is open. */
998
999 if (pi->tid != 0)
1000 pi = find_procinfo_or_die (pi->pid, 0);
1001
1002 struct {
1003 procfs_ctl_t cmd;
1004 /* Use char array to avoid alignment issues. */
1005 char fltset[sizeof (fltset_t)];
1006 } arg;
1007
1008 arg.cmd = PCSFAULT;
1009 memcpy (&arg.fltset, fltset, sizeof (fltset_t));
1010
1011 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1012
1013 /* The above operation renders the procinfo's cached pstatus obsolete. */
1014 pi->status_valid = 0;
1015
1016 return win;
1017}
1018
1019/* Register to trace entry to system calls in the process or LWP.
1020 Returns non-zero for success, zero for failure. */
1021
1022static int
1023proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset)
1024{
1025 int win;
1026
1027 /* We should never have to apply this operation to any procinfo
1028 except the one for the main process. If that ever changes for
1029 any reason, then take out the following clause and replace it
1030 with one that makes sure the ctl_fd is open. */
1031
1032 if (pi->tid != 0)
1033 pi = find_procinfo_or_die (pi->pid, 0);
1034
1035 struct {
1036 procfs_ctl_t cmd;
1037 /* Use char array to avoid alignment issues. */
1038 char sysset[sizeof (sysset_t)];
1039 } arg;
1040
1041 arg.cmd = PCSENTRY;
1042 memcpy (&arg.sysset, sysset, sizeof (sysset_t));
1043
1044 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1045
1046 /* The above operation renders the procinfo's cached pstatus
1047 obsolete. */
1048 pi->status_valid = 0;
1049
1050 return win;
1051}
1052
1053/* Register to trace exit from system calls in the process or LWP.
1054 Returns non-zero for success, zero for failure. */
1055
1056static int
1057proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset)
1058{
1059 int win;
1060
1061 /* We should never have to apply this operation to any procinfo
1062 except the one for the main process. If that ever changes for
1063 any reason, then take out the following clause and replace it
1064 with one that makes sure the ctl_fd is open. */
1065
1066 if (pi->tid != 0)
1067 pi = find_procinfo_or_die (pi->pid, 0);
1068
1069 struct gdb_proc_ctl_pcsexit {
1070 procfs_ctl_t cmd;
1071 /* Use char array to avoid alignment issues. */
1072 char sysset[sizeof (sysset_t)];
1073 } arg;
1074
1075 arg.cmd = PCSEXIT;
1076 memcpy (&arg.sysset, sysset, sizeof (sysset_t));
1077
1078 win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg));
1079
1080 /* The above operation renders the procinfo's cached pstatus
1081 obsolete. */
1082 pi->status_valid = 0;
1083
1084 return win;
1085}
1086
1087/* Specify the set of blocked / held signals in the process or LWP.
1088 Returns non-zero for success, zero for failure. */
1089
1090static int
1091proc_set_held_signals (procinfo *pi, sigset_t *sighold)
1092{
1093 int win;
1094
1095 /* We should never have to apply this operation to any procinfo
1096 except the one for the main process. If that ever changes for
1097 any reason, then take out the following clause and replace it
1098 with one that makes sure the ctl_fd is open. */
1099
1100 if (pi->tid != 0)
1101 pi = find_procinfo_or_die (pi->pid, 0);
1102
1103 struct {
1104 procfs_ctl_t cmd;
1105 /* Use char array to avoid alignment issues. */
1106 char hold[sizeof (sigset_t)];
1107 } arg;
1108
1109 arg.cmd = PCSHOLD;
1110 memcpy (&arg.hold, sighold, sizeof (sigset_t));
1111 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1112
1113 /* The above operation renders the procinfo's cached pstatus
1114 obsolete. */
1115 pi->status_valid = 0;
1116
1117 return win;
1118}
1119
1120/* Returns the set of signals that are held / blocked. Will also copy
1121 the sigset if SAVE is non-zero. */
1122
1123static sigset_t *
1124proc_get_held_signals (procinfo *pi, sigset_t *save)
1125{
1126 sigset_t *ret = NULL;
1127
1128 /* We should never have to apply this operation to any procinfo
1129 except the one for the main process. If that ever changes for
1130 any reason, then take out the following clause and replace it
1131 with one that makes sure the ctl_fd is open. */
1132
1133 if (pi->tid != 0)
1134 pi = find_procinfo_or_die (pi->pid, 0);
1135
1136 if (!pi->status_valid)
1137 if (!proc_get_status (pi))
1138 return NULL;
1139
1140 ret = &pi->prstatus.pr_lwp.pr_lwphold;
1141 if (save && ret)
1142 memcpy (save, ret, sizeof (sigset_t));
1143
1144 return ret;
1145}
1146
1147/* Returns the set of signals that are traced / debugged. Will also
1148 copy the sigset if SAVE is non-zero. */
1149
1150static sigset_t *
1152{
1153 sigset_t *ret = NULL;
1154
1155 /* We should never have to apply this operation to any procinfo
1156 except the one for the main process. If that ever changes for
1157 any reason, then take out the following clause and replace it
1158 with one that makes sure the ctl_fd is open. */
1159
1160 if (pi->tid != 0)
1161 pi = find_procinfo_or_die (pi->pid, 0);
1162
1163 if (!pi->status_valid)
1164 if (!proc_get_status (pi))
1165 return NULL;
1166
1167 ret = &pi->prstatus.pr_sigtrace;
1168 if (save && ret)
1169 memcpy (save, ret, sizeof (sigset_t));
1170
1171 return ret;
1172}
1173
1174/* Returns the set of hardware faults that are traced /debugged. Will
1175 also copy the faultset if SAVE is non-zero. */
1176
1177static fltset_t *
1179{
1180 fltset_t *ret = NULL;
1181
1182 /* We should never have to apply this operation to any procinfo
1183 except the one for the main process. If that ever changes for
1184 any reason, then take out the following clause and replace it
1185 with one that makes sure the ctl_fd is open. */
1186
1187 if (pi->tid != 0)
1188 pi = find_procinfo_or_die (pi->pid, 0);
1189
1190 if (!pi->status_valid)
1191 if (!proc_get_status (pi))
1192 return NULL;
1193
1194 ret = &pi->prstatus.pr_flttrace;
1195 if (save && ret)
1196 memcpy (save, ret, sizeof (fltset_t));
1197
1198 return ret;
1199}
1200
1201/* Returns the set of syscalls that are traced /debugged on entry.
1202 Will also copy the syscall set if SAVE is non-zero. */
1203
1204static sysset_t *
1206{
1207 sysset_t *ret = NULL;
1208
1209 /* We should never have to apply this operation to any procinfo
1210 except the one for the main process. If that ever changes for
1211 any reason, then take out the following clause and replace it
1212 with one that makes sure the ctl_fd is open. */
1213
1214 if (pi->tid != 0)
1215 pi = find_procinfo_or_die (pi->pid, 0);
1216
1217 if (!pi->status_valid)
1218 if (!proc_get_status (pi))
1219 return NULL;
1220
1221 ret = &pi->prstatus.pr_sysentry;
1222 if (save && ret)
1223 memcpy (save, ret, sizeof (sysset_t));
1224
1225 return ret;
1226}
1227
1228/* Returns the set of syscalls that are traced /debugged on exit.
1229 Will also copy the syscall set if SAVE is non-zero. */
1230
1231static sysset_t *
1233{
1234 sysset_t *ret = NULL;
1235
1236 /* We should never have to apply this operation to any procinfo
1237 except the one for the main process. If that ever changes for
1238 any reason, then take out the following clause and replace it
1239 with one that makes sure the ctl_fd is open. */
1240
1241 if (pi->tid != 0)
1242 pi = find_procinfo_or_die (pi->pid, 0);
1243
1244 if (!pi->status_valid)
1245 if (!proc_get_status (pi))
1246 return NULL;
1247
1248 ret = &pi->prstatus.pr_sysexit;
1249 if (save && ret)
1250 memcpy (save, ret, sizeof (sysset_t));
1251
1252 return ret;
1253}
1254
1255/* The current fault (if any) is cleared; the associated signal will
1256 not be sent to the process or LWP when it resumes. Returns
1257 non-zero for success, zero for failure. */
1258
1259static int
1261{
1262 int win;
1263
1264 /* We should never have to apply this operation to any procinfo
1265 except the one for the main process. If that ever changes for
1266 any reason, then take out the following clause and replace it
1267 with one that makes sure the ctl_fd is open. */
1268
1269 if (pi->tid != 0)
1270 pi = find_procinfo_or_die (pi->pid, 0);
1271
1272 procfs_ctl_t cmd = PCCFAULT;
1273
1274 win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd));
1275
1276 return win;
1277}
1278
1279/* Set the "current signal" that will be delivered next to the
1280 process. NOTE: semantics are different from those of KILL. This
1281 signal will be delivered to the process or LWP immediately when it
1282 is resumed (even if the signal is held/blocked); it will NOT
1283 immediately cause another event of interest, and will NOT first
1284 trap back to the debugger. Returns non-zero for success, zero for
1285 failure. */
1286
1287static int
1289{
1290 int win;
1291 struct {
1292 procfs_ctl_t cmd;
1293 /* Use char array to avoid alignment issues. */
1294 char sinfo[sizeof (siginfo_t)];
1295 } arg;
1296 siginfo_t mysinfo;
1297 process_stratum_target *wait_target;
1298 ptid_t wait_ptid;
1299 struct target_waitstatus wait_status;
1300
1301 /* We should never have to apply this operation to any procinfo
1302 except the one for the main process. If that ever changes for
1303 any reason, then take out the following clause and replace it
1304 with one that makes sure the ctl_fd is open. */
1305
1306 if (pi->tid != 0)
1307 pi = find_procinfo_or_die (pi->pid, 0);
1308
1309 /* The pointer is just a type alias. */
1310 get_last_target_status (&wait_target, &wait_ptid, &wait_status);
1311 if (wait_target == &the_procfs_target
1312 && wait_ptid == inferior_ptid
1313 && wait_status.kind () == TARGET_WAITKIND_STOPPED
1314 && wait_status.sig () == gdb_signal_from_host (signo)
1315 && proc_get_status (pi)
1316 && pi->prstatus.pr_lwp.pr_info.si_signo == signo
1317 )
1318 /* Use the siginfo associated with the signal being
1319 redelivered. */
1320 memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (siginfo_t));
1321 else
1322 {
1323 mysinfo.si_signo = signo;
1324 mysinfo.si_code = 0;
1325 mysinfo.si_pid = getpid (); /* ?why? */
1326 mysinfo.si_uid = getuid (); /* ?why? */
1327 memcpy (arg.sinfo, &mysinfo, sizeof (siginfo_t));
1328 }
1329
1330 arg.cmd = PCSSIG;
1331 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1332
1333 return win;
1334}
1335
1336/* The current signal (if any) is cleared, and is not sent to the
1337 process or LWP when it resumes. Returns non-zero for success, zero
1338 for failure. */
1339
1340static int
1342{
1343 int win;
1344
1345 /* We should never have to apply this operation to any procinfo
1346 except the one for the main process. If that ever changes for
1347 any reason, then take out the following clause and replace it
1348 with one that makes sure the ctl_fd is open. */
1349
1350 if (pi->tid != 0)
1351 pi = find_procinfo_or_die (pi->pid, 0);
1352
1353 struct {
1354 procfs_ctl_t cmd;
1355 /* Use char array to avoid alignment issues. */
1356 char sinfo[sizeof (siginfo_t)];
1357 } arg;
1358 siginfo_t mysinfo;
1359
1360 arg.cmd = PCSSIG;
1361 /* The pointer is just a type alias. */
1362 mysinfo.si_signo = 0;
1363 mysinfo.si_code = 0;
1364 mysinfo.si_errno = 0;
1365 mysinfo.si_pid = getpid (); /* ?why? */
1366 mysinfo.si_uid = getuid (); /* ?why? */
1367 memcpy (arg.sinfo, &mysinfo, sizeof (siginfo_t));
1368
1369 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1370
1371 return win;
1372}
1373
1374/* Return the general-purpose registers for the process or LWP
1375 corresponding to PI. Upon failure, return NULL. */
1376
1377static gdb_gregset_t *
1379{
1380 if (!pi->status_valid || !pi->gregs_valid)
1381 if (!proc_get_status (pi))
1382 return NULL;
1383
1384 return &pi->prstatus.pr_lwp.pr_reg;
1385}
1386
1387/* Return the general-purpose registers for the process or LWP
1388 corresponding to PI. Upon failure, return NULL. */
1389
1390static gdb_fpregset_t *
1392{
1393 if (!pi->status_valid || !pi->fpregs_valid)
1394 if (!proc_get_status (pi))
1395 return NULL;
1396
1397 return &pi->prstatus.pr_lwp.pr_fpreg;
1398}
1399
1400/* Write the general-purpose registers back to the process or LWP
1401 corresponding to PI. Return non-zero for success, zero for
1402 failure. */
1403
1404static int
1406{
1407 gdb_gregset_t *gregs;
1408 int win;
1409
1410 gregs = proc_get_gregs (pi);
1411 if (gregs == NULL)
1412 return 0; /* proc_get_regs has already warned. */
1413
1414 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
1415 return 0;
1416 else
1417 {
1418 struct {
1419 procfs_ctl_t cmd;
1420 /* Use char array to avoid alignment issues. */
1421 char gregs[sizeof (gdb_gregset_t)];
1422 } arg;
1423
1424 arg.cmd = PCSREG;
1425 memcpy (&arg.gregs, gregs, sizeof (arg.gregs));
1426 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1427 }
1428
1429 /* Policy: writing the registers invalidates our cache. */
1430 pi->gregs_valid = 0;
1431 return win;
1432}
1433
1434/* Write the floating-pointer registers back to the process or LWP
1435 corresponding to PI. Return non-zero for success, zero for
1436 failure. */
1437
1438static int
1440{
1441 gdb_fpregset_t *fpregs;
1442 int win;
1443
1444 fpregs = proc_get_fpregs (pi);
1445 if (fpregs == NULL)
1446 return 0; /* proc_get_fpregs has already warned. */
1447
1448 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
1449 return 0;
1450 else
1451 {
1452 struct {
1453 procfs_ctl_t cmd;
1454 /* Use char array to avoid alignment issues. */
1455 char fpregs[sizeof (gdb_fpregset_t)];
1456 } arg;
1457
1458 arg.cmd = PCSFPREG;
1459 memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs));
1460 win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg));
1461 }
1462
1463 /* Policy: writing the registers invalidates our cache. */
1464 pi->fpregs_valid = 0;
1465 return win;
1466}
1467
1468/* Send a signal to the proc or lwp with the semantics of "kill()".
1469 Returns non-zero for success, zero for failure. */
1470
1471static int
1472proc_kill (procinfo *pi, int signo)
1473{
1474 int win;
1475
1476 /* We might conceivably apply this operation to an LWP, and the
1477 LWP's ctl file descriptor might not be open. */
1478
1479 if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0)
1480 return 0;
1481 else
1482 {
1483 procfs_ctl_t cmd[2];
1484
1485 cmd[0] = PCKILL;
1486 cmd[1] = signo;
1487 win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd));
1488 }
1489
1490 return win;
1491}
1492
1493/* Find the pid of the process that started this one. Returns the
1494 parent process pid, or zero. */
1495
1496static int
1498{
1499 /* We should never have to apply this operation to any procinfo
1500 except the one for the main process. If that ever changes for
1501 any reason, then take out the following clause and replace it
1502 with one that makes sure the ctl_fd is open. */
1503
1504 if (pi->tid != 0)
1505 pi = find_procinfo_or_die (pi->pid, 0);
1506
1507 if (!pi->status_valid)
1508 if (!proc_get_status (pi))
1509 return 0;
1510
1511 return pi->prstatus.pr_ppid;
1512}
1513
1514/* Convert a target address (a.k.a. CORE_ADDR) into a host address
1515 (a.k.a void pointer)! */
1516
1517static void *
1519{
1520 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
1521 void *ptr;
1522
1523 gdb_assert (sizeof (ptr) == ptr_type->length ());
1525 (gdb_byte *) &ptr, addr);
1526 return ptr;
1527}
1528
1529static int
1530proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags)
1531{
1532 struct {
1533 procfs_ctl_t cmd;
1534 char watch[sizeof (prwatch_t)];
1535 } arg;
1536 prwatch_t pwatch;
1537
1538 /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to
1539 convert a target address into something that can be stored in a
1540 native data structure. */
1541 pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr);
1542 pwatch.pr_size = len;
1543 pwatch.pr_wflags = wflags;
1544 arg.cmd = PCWATCH;
1545 memcpy (arg.watch, &pwatch, sizeof (prwatch_t));
1546 return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg));
1547}
1548
1549/* =============== END, non-thread part of /proc "MODULE" =============== */
1550
1551/* =================== Thread "MODULE" =================== */
1552
1553/* Returns the number of threads for the process. */
1554
1555static int
1557{
1558 if (!pi->status_valid)
1559 if (!proc_get_status (pi))
1560 return 0;
1561
1562 /* Only works for the process procinfo, because the LWP procinfos do not
1563 get prstatus filled in. */
1564 if (pi->tid != 0) /* Find the parent process procinfo. */
1565 pi = find_procinfo_or_die (pi->pid, 0);
1566 return pi->prstatus.pr_nlwp;
1567}
1568
1569/* Return the ID of the thread that had an event of interest.
1570 (ie. the one that hit a breakpoint or other traced event). All
1571 other things being equal, this should be the ID of a thread that is
1572 currently executing. */
1573
1574static int
1576{
1577 /* Note: this should be applied to the root procinfo for the
1578 process, not to the procinfo for an LWP. If applied to the
1579 procinfo for an LWP, it will simply return that LWP's ID. In
1580 that case, find the parent process procinfo. */
1581
1582 if (pi->tid != 0)
1583 pi = find_procinfo_or_die (pi->pid, 0);
1584
1585 if (!pi->status_valid)
1586 if (!proc_get_status (pi))
1587 return 0;
1588
1589 return pi->prstatus.pr_lwp.pr_lwpid;
1590}
1591
1592/* Discover the IDs of all the threads within the process, and create
1593 a procinfo for each of them (chained to the parent). Returns
1594 non-zero for success, zero for failure. */
1595
1596static int
1597proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore)
1598{
1599 if (thread && parent) /* sanity */
1600 {
1601 thread->status_valid = 0;
1602 if (!proc_get_status (thread))
1603 destroy_one_procinfo (&parent->thread_list, thread);
1604 }
1605 return 0; /* keep iterating */
1606}
1607
1608static int
1610{
1611 char pathname[MAX_PROC_NAME_SIZE + 16];
1612 struct dirent *direntry;
1613 procinfo *thread;
1614 gdb_dir_up dirp;
1615 int lwpid;
1616
1617 /* We should never have to apply this operation to any procinfo
1618 except the one for the main process. If that ever changes for
1619 any reason, then take out the following clause and replace it
1620 with one that makes sure the ctl_fd is open. */
1621
1622 if (pi->tid != 0)
1623 pi = find_procinfo_or_die (pi->pid, 0);
1624
1626
1627 /* Note: this brute-force method was originally devised for Unixware
1628 (support removed since), and will also work on Solaris 2.6 and
1629 2.7. The original comment mentioned the existence of a much
1630 simpler and more elegant way to do this on Solaris, but didn't
1631 point out what that was. */
1632
1633 strcpy (pathname, pi->pathname);
1634 strcat (pathname, "/lwp");
1635 dirp.reset (opendir (pathname));
1636 if (dirp == NULL)
1637 proc_error (pi, "update_threads, opendir", __LINE__);
1638
1639 while ((direntry = readdir (dirp.get ())) != NULL)
1640 if (direntry->d_name[0] != '.') /* skip '.' and '..' */
1641 {
1642 lwpid = atoi (&direntry->d_name[0]);
1643 thread = create_procinfo (pi->pid, lwpid);
1644 if (thread == NULL)
1645 proc_error (pi, "update_threads, create_procinfo", __LINE__);
1646 }
1647 pi->threads_valid = 1;
1648 return 1;
1649}
1650
1651/* Given a pointer to a function, call that function once for each lwp
1652 in the procinfo list, until the function returns non-zero, in which
1653 event return the value returned by the function.
1654
1655 Note: this function does NOT call update_threads. If you want to
1656 discover new threads first, you must call that function explicitly.
1657 This function just makes a quick pass over the currently-known
1658 procinfos.
1659
1660 PI is the parent process procinfo. FUNC is the per-thread
1661 function. PTR is an opaque parameter for function. Returns the
1662 first non-zero return value from the callee, or zero. */
1663
1664static int
1666 int (*func) (procinfo *, procinfo *, void *),
1667 void *ptr)
1668{
1669 procinfo *thread, *next;
1670 int retval = 0;
1671
1672 /* We should never have to apply this operation to any procinfo
1673 except the one for the main process. If that ever changes for
1674 any reason, then take out the following clause and replace it
1675 with one that makes sure the ctl_fd is open. */
1676
1677 if (pi->tid != 0)
1678 pi = find_procinfo_or_die (pi->pid, 0);
1679
1680 for (thread = pi->thread_list; thread != NULL; thread = next)
1681 {
1682 next = thread->next; /* In case thread is destroyed. */
1683 retval = (*func) (pi, thread, ptr);
1684 if (retval != 0)
1685 break;
1686 }
1687
1688 return retval;
1689}
1690
1691/* =================== END, Thread "MODULE" =================== */
1692
1693/* =================== END, /proc "MODULE" =================== */
1694
1695/* =================== GDB "MODULE" =================== */
1696
1697/* Here are all of the gdb target vector functions and their
1698 friends. */
1699
1700static void do_attach (ptid_t ptid);
1701static void do_detach ();
1702static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum,
1703 int entry_or_exit, int mode, int from_tty);
1704
1705/* Sets up the inferior to be debugged. Registers to trace signals,
1706 hardware faults, and syscalls. Note: does not set RLC flag: caller
1707 may want to customize that. Returns zero for success (note!
1708 unlike most functions in this module); on failure, returns the LINE
1709 NUMBER where it failed! */
1710
1711static int
1713{
1714 fltset_t traced_faults;
1715 sigset_t traced_signals;
1716 sysset_t *traced_syscall_entries;
1717 sysset_t *traced_syscall_exits;
1718 int status;
1719
1720 /* Register to trace hardware faults in the child. */
1721 prfillset (&traced_faults); /* trace all faults... */
1722 prdelset (&traced_faults, FLTPAGE); /* except page fault. */
1723 if (!proc_set_traced_faults (pi, &traced_faults))
1724 return __LINE__;
1725
1726 /* Initially, register to trace all signals in the child. */
1727 prfillset (&traced_signals);
1728 if (!proc_set_traced_signals (pi, &traced_signals))
1729 return __LINE__;
1730
1731
1732 /* Register to trace the 'exit' system call (on entry). */
1733 traced_syscall_entries = XNEW (sysset_t);
1734 premptyset (traced_syscall_entries);
1735 praddset (traced_syscall_entries, SYS_exit);
1736 praddset (traced_syscall_entries, SYS_lwp_exit);
1737
1738 status = proc_set_traced_sysentry (pi, traced_syscall_entries);
1739 xfree (traced_syscall_entries);
1740 if (!status)
1741 return __LINE__;
1742
1743 /* Method for tracing exec syscalls. */
1744 traced_syscall_exits = XNEW (sysset_t);
1745 premptyset (traced_syscall_exits);
1746 praddset (traced_syscall_exits, SYS_execve);
1747 praddset (traced_syscall_exits, SYS_lwp_create);
1748 praddset (traced_syscall_exits, SYS_lwp_exit);
1749
1750 status = proc_set_traced_sysexit (pi, traced_syscall_exits);
1751 xfree (traced_syscall_exits);
1752 if (!status)
1753 return __LINE__;
1754
1755 return 0;
1756}
1757
1758void
1759procfs_target::attach (const char *args, int from_tty)
1760{
1761 int pid;
1762
1763 pid = parse_pid_to_attach (args);
1764
1765 if (pid == getpid ())
1766 error (_("Attaching GDB to itself is not a good idea..."));
1767
1768 /* Push the target if needed, ensure it gets un-pushed it if attach fails. */
1770 target_unpush_up unpusher;
1771 if (!inf->target_is_pushed (this))
1772 {
1773 inf->push_target (this);
1774 unpusher.reset (this);
1775 }
1776
1777 target_announce_attach (from_tty, pid);
1778
1779 do_attach (ptid_t (pid));
1780
1781 /* Everything went fine, keep the target pushed. */
1782 unpusher.release ();
1783}
1784
1785void
1787{
1788 target_announce_detach (from_tty);
1789
1790 do_detach ();
1791
1795}
1796
1797static void
1798do_attach (ptid_t ptid)
1799{
1800 procinfo *pi;
1801 struct inferior *inf;
1802 int fail;
1803 int lwpid;
1804
1805 pi = create_procinfo (ptid.pid (), 0);
1806 if (pi == NULL)
1807 perror (_("procfs: out of memory in 'attach'"));
1808
1809 if (!open_procinfo_files (pi, FD_CTL))
1810 {
1811 int saved_errno = errno;
1812 std::string errmsg
1813 = string_printf ("procfs:%d -- do_attach: couldn't open /proc "
1814 "file for process %d", __LINE__, ptid.pid ());
1815 errno = saved_errno;
1816 dead_procinfo (pi, errmsg.c_str (), NOKILL);
1817 }
1818
1819 /* Stop the process (if it isn't already stopped). */
1820 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
1821 {
1822 pi->was_stopped = 1;
1823 proc_prettyprint_why (proc_why (pi), proc_what (pi), 1);
1824 }
1825 else
1826 {
1827 pi->was_stopped = 0;
1828 /* Set the process to run again when we close it. */
1830 dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL);
1831
1832 /* Now stop the process. */
1833 if (!proc_stop_process (pi))
1834 dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL);
1835 pi->ignore_next_sigstop = 1;
1836 }
1837 /* Save some of the /proc state to be restored if we detach. */
1838 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
1839 dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL);
1840 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
1841 dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL);
1843 dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.",
1844 NOKILL);
1846 dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.",
1847 NOKILL);
1848 if (!proc_get_held_signals (pi, &pi->saved_sighold))
1849 dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL);
1850
1851 fail = procfs_debug_inferior (pi);
1852 if (fail != 0)
1853 dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL);
1854
1855 inf = current_inferior ();
1856 inferior_appeared (inf, pi->pid);
1857 /* Let GDB know that the inferior was attached. */
1858 inf->attach_flag = true;
1859
1860 /* Create a procinfo for the current lwp. */
1861 lwpid = proc_get_current_thread (pi);
1862 create_procinfo (pi->pid, lwpid);
1863
1864 /* Add it to gdb's thread list. */
1865 ptid = ptid_t (pi->pid, lwpid, 0);
1867 switch_to_thread (thr);
1868}
1869
1870static void
1872{
1873 procinfo *pi;
1874
1875 /* Find procinfo for the main process. */
1877 0); /* FIXME: threads */
1878
1879 if (!proc_set_traced_signals (pi, &pi->saved_sigset))
1880 proc_warn (pi, "do_detach, set_traced_signal", __LINE__);
1881
1882 if (!proc_set_traced_faults (pi, &pi->saved_fltset))
1883 proc_warn (pi, "do_detach, set_traced_faults", __LINE__);
1884
1886 proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__);
1887
1889 proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__);
1890
1891 if (!proc_set_held_signals (pi, &pi->saved_sighold))
1892 proc_warn (pi, "do_detach, set_held_signals", __LINE__);
1893
1894 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
1895 if (!(pi->was_stopped)
1896 || query (_("Was stopped when attached, make it runnable again? ")))
1897 {
1898 /* Clear any pending signal. */
1899 if (!proc_clear_current_fault (pi))
1900 proc_warn (pi, "do_detach, clear_current_fault", __LINE__);
1901
1902 if (!proc_clear_current_signal (pi))
1903 proc_warn (pi, "do_detach, clear_current_signal", __LINE__);
1904
1906 proc_warn (pi, "do_detach, set_rlc", __LINE__);
1907 }
1908
1909 destroy_procinfo (pi);
1910}
1911
1912/* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
1913 for all registers.
1914
1915 NOTE: Since the /proc interface cannot give us individual
1916 registers, we pay no attention to REGNUM, and just fetch them all.
1917 This results in the possibility that we will do unnecessarily many
1918 fetches, since we may be called repeatedly for individual
1919 registers. So we cache the results, and mark the cache invalid
1920 when the process is resumed. */
1921
1922void
1924{
1925 gdb_gregset_t *gregs;
1926 procinfo *pi;
1927 ptid_t ptid = regcache->ptid ();
1928 int pid = ptid.pid ();
1929 int tid = ptid.lwp ();
1930 struct gdbarch *gdbarch = regcache->arch ();
1931
1932 pi = find_procinfo_or_die (pid, tid);
1933
1934 if (pi == NULL)
1935 error (_("procfs: fetch_registers failed to find procinfo for %s"),
1936 target_pid_to_str (ptid).c_str ());
1937
1938 gregs = proc_get_gregs (pi);
1939 if (gregs == NULL)
1940 proc_error (pi, "fetch_registers, get_gregs", __LINE__);
1941
1942 supply_gregset (regcache, (const gdb_gregset_t *) gregs);
1943
1944 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
1945 {
1946 gdb_fpregset_t *fpregs;
1947
1948 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
1951 return; /* Not a floating point register. */
1952
1953 fpregs = proc_get_fpregs (pi);
1954 if (fpregs == NULL)
1955 proc_error (pi, "fetch_registers, get_fpregs", __LINE__);
1956
1957 supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs);
1958 }
1959}
1960
1961/* Store register REGNUM back into the inferior. If REGNUM is -1, do
1962 this for all registers.
1963
1964 NOTE: Since the /proc interface will not read individual registers,
1965 we will cache these requests until the process is resumed, and only
1966 then write them back to the inferior process.
1967
1968 FIXME: is that a really bad idea? Have to think about cases where
1969 writing one register might affect the value of others, etc. */
1970
1971void
1973{
1974 gdb_gregset_t *gregs;
1975 procinfo *pi;
1976 ptid_t ptid = regcache->ptid ();
1977 int pid = ptid.pid ();
1978 int tid = ptid.lwp ();
1979 struct gdbarch *gdbarch = regcache->arch ();
1980
1981 pi = find_procinfo_or_die (pid, tid);
1982
1983 if (pi == NULL)
1984 error (_("procfs: store_registers: failed to find procinfo for %s"),
1985 target_pid_to_str (ptid).c_str ());
1986
1987 gregs = proc_get_gregs (pi);
1988 if (gregs == NULL)
1989 proc_error (pi, "store_registers, get_gregs", __LINE__);
1990
1991 fill_gregset (regcache, gregs, regnum);
1992 if (!proc_set_gregs (pi))
1993 proc_error (pi, "store_registers, set_gregs", __LINE__);
1994
1995 if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */
1996 {
1997 gdb_fpregset_t *fpregs;
1998
1999 if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch))
2002 return; /* Not a floating point register. */
2003
2004 fpregs = proc_get_fpregs (pi);
2005 if (fpregs == NULL)
2006 proc_error (pi, "store_registers, get_fpregs", __LINE__);
2007
2008 fill_fpregset (regcache, fpregs, regnum);
2009 if (!proc_set_fpregs (pi))
2010 proc_error (pi, "store_registers, set_fpregs", __LINE__);
2011 }
2012}
2013
2014/* Retrieve the next stop event from the child process. If child has
2015 not stopped yet, wait for it to stop. Translate /proc eventcodes
2016 (or possibly wait eventcodes) into gdb internal event codes.
2017 Returns the id of process (and possibly thread) that incurred the
2018 event. Event codes are returned through a pointer parameter. */
2019
2020ptid_t
2022 target_wait_flags options)
2023{
2024 /* First cut: loosely based on original version 2.1. */
2025 procinfo *pi;
2026 int wstat;
2027 int temp_tid;
2028 ptid_t retval, temp_ptid;
2029 int why, what, flags;
2030 int retry = 0;
2031
2032wait_again:
2033
2034 retry++;
2035 wstat = 0;
2036 retval = ptid_t (-1);
2037
2038 /* Find procinfo for main process. */
2039
2040 /* procfs_target currently only supports one inferior. */
2042
2043 pi = find_procinfo_or_die (inf->pid, 0);
2044 if (pi)
2045 {
2046 /* We must assume that the status is stale now... */
2047 pi->status_valid = 0;
2048 pi->gregs_valid = 0;
2049 pi->fpregs_valid = 0;
2050
2051#if 0 /* just try this out... */
2052 flags = proc_flags (pi);
2053 why = proc_why (pi);
2054 if ((flags & PR_STOPPED) && (why == PR_REQUESTED))
2055 pi->status_valid = 0; /* re-read again, IMMEDIATELY... */
2056#endif
2057 /* If child is not stopped, wait for it to stop. */
2058 if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
2059 && !proc_wait_for_stop (pi))
2060 {
2061 /* wait_for_stop failed: has the child terminated? */
2062 if (errno == ENOENT)
2063 {
2064 int wait_retval;
2065
2066 /* /proc file not found; presumably child has terminated. */
2067 wait_retval = ::wait (&wstat); /* "wait" for the child's exit. */
2068
2069 /* Wrong child? */
2070 if (wait_retval != inf->pid)
2071 error (_("procfs: couldn't stop "
2072 "process %d: wait returned %d."),
2073 inf->pid, wait_retval);
2074 /* FIXME: might I not just use waitpid?
2075 Or try find_procinfo to see if I know about this child? */
2076 retval = ptid_t (wait_retval);
2077 }
2078 else if (errno == EINTR)
2079 goto wait_again;
2080 else
2081 {
2082 /* Unknown error from wait_for_stop. */
2083 proc_error (pi, "target_wait (wait_for_stop)", __LINE__);
2084 }
2085 }
2086 else
2087 {
2088 /* This long block is reached if either:
2089 a) the child was already stopped, or
2090 b) we successfully waited for the child with wait_for_stop.
2091 This block will analyze the /proc status, and translate it
2092 into a waitstatus for GDB.
2093
2094 If we actually had to call wait because the /proc file
2095 is gone (child terminated), then we skip this block,
2096 because we already have a waitstatus. */
2097
2098 flags = proc_flags (pi);
2099 why = proc_why (pi);
2100 what = proc_what (pi);
2101
2102 if (flags & (PR_STOPPED | PR_ISTOP))
2103 {
2104 /* If it's running async (for single_thread control),
2105 set it back to normal again. */
2106 if (flags & PR_ASYNC)
2107 if (!proc_unset_async (pi))
2108 proc_error (pi, "target_wait, unset_async", __LINE__);
2109
2110 if (info_verbose)
2111 proc_prettyprint_why (why, what, 1);
2112
2113 /* The 'pid' we will return to GDB is composed of
2114 the process ID plus the lwp ID. */
2115 retval = ptid_t (pi->pid, proc_get_current_thread (pi), 0);
2116
2117 switch (why) {
2118 case PR_SIGNALLED:
2119 wstat = (what << 8) | 0177;
2120 break;
2121 case PR_SYSENTRY:
2122 if (what == SYS_lwp_exit)
2123 {
2124 delete_thread (this->find_thread (retval));
2125 proc_resume (pi, ptid, 0, GDB_SIGNAL_0);
2126 goto wait_again;
2127 }
2128 else if (what == SYS_exit)
2129 {
2130 /* Handle SYS_exit call only. */
2131 /* Stopped at entry to SYS_exit.
2132 Make it runnable, resume it, then use
2133 the wait system call to get its exit code.
2134 Proc_run_process always clears the current
2135 fault and signal.
2136 Then return its exit status. */
2137 pi->status_valid = 0;
2138 wstat = 0;
2139 /* FIXME: what we should do is return
2140 TARGET_WAITKIND_SPURIOUS. */
2141 if (!proc_run_process (pi, 0, 0))
2142 proc_error (pi, "target_wait, run_process", __LINE__);
2143
2144 if (inf->attach_flag)
2145 {
2146 /* Don't call wait: simulate waiting for exit,
2147 return a "success" exit code. Bogus: what if
2148 it returns something else? */
2149 wstat = 0;
2150 retval = ptid_t (inf->pid); /* ? ? ? */
2151 }
2152 else
2153 {
2154 int temp = ::wait (&wstat);
2155
2156 /* FIXME: shouldn't I make sure I get the right
2157 event from the right process? If (for
2158 instance) I have killed an earlier inferior
2159 process but failed to clean up after it
2160 somehow, I could get its termination event
2161 here. */
2162
2163 /* If wait returns -1, that's what we return
2164 to GDB. */
2165 if (temp < 0)
2166 retval = ptid_t (temp);
2167 }
2168 }
2169 else
2170 {
2171 gdb_printf (_("procfs: trapped on entry to "));
2173 gdb_printf ("\n");
2174
2175 long i, nsysargs, *sysargs;
2176
2177 nsysargs = proc_nsysarg (pi);
2178 sysargs = proc_sysargs (pi);
2179
2180 if (nsysargs > 0 && sysargs != NULL)
2181 {
2182 gdb_printf (_("%ld syscall arguments:\n"),
2183 nsysargs);
2184 for (i = 0; i < nsysargs; i++)
2185 gdb_printf ("#%ld: 0x%08lx\n",
2186 i, sysargs[i]);
2187 }
2188
2189 proc_resume (pi, ptid, 0, GDB_SIGNAL_0);
2190 goto wait_again;
2191 }
2192 break;
2193 case PR_SYSEXIT:
2194 if (what == SYS_execve)
2195 {
2196 /* Hopefully this is our own "fork-child" execing
2197 the real child. Hoax this event into a trap, and
2198 GDB will see the child about to execute its start
2199 address. */
2200 wstat = (SIGTRAP << 8) | 0177;
2201 }
2202 else if (what == SYS_lwp_create)
2203 {
2204 /* This syscall is somewhat like fork/exec. We
2205 will get the event twice: once for the parent
2206 LWP, and once for the child. We should already
2207 know about the parent LWP, but the child will
2208 be new to us. So, whenever we get this event,
2209 if it represents a new thread, simply add the
2210 thread to the list. */
2211
2212 /* If not in procinfo list, add it. */
2213 temp_tid = proc_get_current_thread (pi);
2214 if (!find_procinfo (pi->pid, temp_tid))
2215 create_procinfo (pi->pid, temp_tid);
2216
2217 temp_ptid = ptid_t (pi->pid, temp_tid, 0);
2218 /* If not in GDB's thread list, add it. */
2219 if (!in_thread_list (this, temp_ptid))
2220 add_thread (this, temp_ptid);
2221
2222 proc_resume (pi, ptid, 0, GDB_SIGNAL_0);
2223 goto wait_again;
2224 }
2225 else if (what == SYS_lwp_exit)
2226 {
2227 delete_thread (this->find_thread (retval));
2228 status->set_spurious ();
2229 return retval;
2230 }
2231 else
2232 {
2233 gdb_printf (_("procfs: trapped on exit from "));
2235 gdb_printf ("\n");
2236
2237 long i, nsysargs, *sysargs;
2238
2239 nsysargs = proc_nsysarg (pi);
2240 sysargs = proc_sysargs (pi);
2241
2242 if (nsysargs > 0 && sysargs != NULL)
2243 {
2244 gdb_printf (_("%ld syscall arguments:\n"),
2245 nsysargs);
2246 for (i = 0; i < nsysargs; i++)
2247 gdb_printf ("#%ld: 0x%08lx\n",
2248 i, sysargs[i]);
2249 }
2250
2251 proc_resume (pi, ptid, 0, GDB_SIGNAL_0);
2252 goto wait_again;
2253 }
2254 break;
2255 case PR_REQUESTED:
2256#if 0 /* FIXME */
2257 wstat = (SIGSTOP << 8) | 0177;
2258 break;
2259#else
2260 if (retry < 5)
2261 {
2262 gdb_printf (_("Retry #%d:\n"), retry);
2263 pi->status_valid = 0;
2264 goto wait_again;
2265 }
2266 else
2267 {
2268 /* If not in procinfo list, add it. */
2269 temp_tid = proc_get_current_thread (pi);
2270 if (!find_procinfo (pi->pid, temp_tid))
2271 create_procinfo (pi->pid, temp_tid);
2272
2273 /* If not in GDB's thread list, add it. */
2274 temp_ptid = ptid_t (pi->pid, temp_tid, 0);
2275 if (!in_thread_list (this, temp_ptid))
2276 add_thread (this, temp_ptid);
2277
2278 status->set_stopped (GDB_SIGNAL_0);
2279 return retval;
2280 }
2281#endif
2282 case PR_JOBCONTROL:
2283 wstat = (what << 8) | 0177;
2284 break;
2285 case PR_FAULTED:
2286 {
2287 int signo = pi->prstatus.pr_lwp.pr_info.si_signo;
2288 if (signo != 0)
2289 wstat = (signo << 8) | 0177;
2290 }
2291 break;
2292 default: /* switch (why) unmatched */
2293 gdb_printf ("procfs:%d -- ", __LINE__);
2294 gdb_printf (_("child stopped for unknown reason:\n"));
2295 proc_prettyprint_why (why, what, 1);
2296 error (_("... giving up..."));
2297 break;
2298 }
2299 /* Got this far without error: If retval isn't in the
2300 threads database, add it. */
2301 if (retval.pid () > 0
2302 && !in_thread_list (this, retval))
2303 {
2304 /* We have a new thread. We need to add it both to
2305 GDB's list and to our own. If we don't create a
2306 procinfo, resume may be unhappy later. */
2307 add_thread (this, retval);
2308 if (find_procinfo (retval.pid (),
2309 retval.lwp ()) == NULL)
2310 create_procinfo (retval.pid (),
2311 retval.lwp ());
2312 }
2313 }
2314 else /* Flags do not indicate STOPPED. */
2315 {
2316 /* surely this can't happen... */
2317 gdb_printf ("procfs:%d -- process not stopped.\n",
2318 __LINE__);
2320 error (_("procfs: ...giving up..."));
2321 }
2322 }
2323
2324 if (status)
2326 }
2327
2328 return retval;
2329}
2330
2331/* Perform a partial transfer to/from the specified object. For
2332 memory transfers, fall back to the old memory xfer functions. */
2333
2336 const char *annex, gdb_byte *readbuf,
2337 const gdb_byte *writebuf, ULONGEST offset,
2338 ULONGEST len, ULONGEST *xfered_len)
2339{
2340 switch (object)
2341 {
2343 return procfs_xfer_memory (readbuf, writebuf, offset, len, xfered_len);
2344
2345 case TARGET_OBJECT_AUXV:
2346 return memory_xfer_auxv (this, object, annex, readbuf, writebuf,
2347 offset, len, xfered_len);
2348
2349 default:
2350 return this->beneath ()->xfer_partial (object, annex,
2351 readbuf, writebuf, offset, len,
2352 xfered_len);
2353 }
2354}
2355
2356/* Helper for procfs_xfer_partial that handles memory transfers.
2357 Arguments are like target_xfer_partial. */
2358
2359static enum target_xfer_status
2360procfs_xfer_memory (gdb_byte *readbuf, const gdb_byte *writebuf,
2361 ULONGEST memaddr, ULONGEST len, ULONGEST *xfered_len)
2362{
2363 procinfo *pi;
2364 int nbytes;
2365
2366 /* Find procinfo for main process. */
2367 pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
2368 if (pi->as_fd == 0 && open_procinfo_files (pi, FD_AS) == 0)
2369 {
2370 proc_warn (pi, "xfer_memory, open_proc_files", __LINE__);
2371 return TARGET_XFER_E_IO;
2372 }
2373
2374 if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) != (off_t) memaddr)
2375 return TARGET_XFER_E_IO;
2376
2377 if (writebuf != NULL)
2378 {
2379 PROCFS_NOTE ("write memory:\n");
2380 nbytes = write (pi->as_fd, writebuf, len);
2381 }
2382 else
2383 {
2384 PROCFS_NOTE ("read memory:\n");
2385 nbytes = read (pi->as_fd, readbuf, len);
2386 }
2387 if (nbytes <= 0)
2388 return TARGET_XFER_E_IO;
2389 *xfered_len = nbytes;
2390 return TARGET_XFER_OK;
2391}
2392
2393/* Called by target_resume before making child runnable. Mark cached
2394 registers and status's invalid. If there are "dirty" caches that
2395 need to be written back to the child process, do that.
2396
2397 File descriptors are also cached. As they are a limited resource,
2398 we cannot hold onto them indefinitely. However, as they are
2399 expensive to open, we don't want to throw them away
2400 indiscriminately either. As a compromise, we will keep the file
2401 descriptors for the parent process, but discard any file
2402 descriptors we may have accumulated for the threads.
2403
2404 As this function is called by iterate_over_threads, it always
2405 returns zero (so that iterate_over_threads will keep
2406 iterating). */
2407
2408static int
2409invalidate_cache (procinfo *parent, procinfo *pi, void *ptr)
2410{
2411 /* About to run the child; invalidate caches and do any other
2412 cleanup. */
2413
2414 if (parent != NULL)
2415 {
2416 /* The presence of a parent indicates that this is an LWP.
2417 Close any file descriptors that it might have open.
2418 We don't do this to the master (parent) procinfo. */
2419
2421 }
2422 pi->gregs_valid = 0;
2423 pi->fpregs_valid = 0;
2424 pi->status_valid = 0;
2425 pi->threads_valid = 0;
2426
2427 return 0;
2428}
2429
2430/* Make child process PI runnable.
2431
2432 If STEP is true, then arrange for the child to stop again after
2433 executing a single instruction. SCOPE_PTID, STEP and SIGNO are
2434 like in the target_resume interface. */
2435
2436static void
2437proc_resume (procinfo *pi, ptid_t scope_ptid, int step, enum gdb_signal signo)
2438{
2439 procinfo *thread;
2440 int native_signo;
2441
2442 /* FIXME: Check/reword. */
2443
2444 /* prrun.prflags |= PRCFAULT; clear current fault.
2445 PRCFAULT may be replaced by a PCCFAULT call (proc_clear_current_fault)
2446 This basically leaves PRSTEP and PRCSIG.
2447 PRCSIG is like PCSSIG (proc_clear_current_signal).
2448 So basically PR_STEP is the sole argument that must be passed
2449 to proc_run_process. */
2450
2451 errno = 0;
2452
2453 /* Convert signal to host numbering. */
2454 if (signo == 0 || (signo == GDB_SIGNAL_STOP && pi->ignore_next_sigstop))
2455 native_signo = 0;
2456 else
2457 native_signo = gdb_signal_to_host (signo);
2458
2459 pi->ignore_next_sigstop = 0;
2460
2461 /* Running the process voids all cached registers and status. */
2462 /* Void the threads' caches first. */
2464 /* Void the process procinfo's caches. */
2465 invalidate_cache (NULL, pi, NULL);
2466
2467 if (scope_ptid.pid () != -1)
2468 {
2469 /* Resume a specific thread, presumably suppressing the
2470 others. */
2471 thread = find_procinfo (scope_ptid.pid (), scope_ptid.lwp ());
2472 if (thread != NULL)
2473 {
2474 if (thread->tid != 0)
2475 {
2476 /* We're to resume a specific thread, and not the
2477 others. Set the child process's PR_ASYNC flag. */
2478 if (!proc_set_async (pi))
2479 proc_error (pi, "target_resume, set_async", __LINE__);
2480 pi = thread; /* Substitute the thread's procinfo
2481 for run. */
2482 }
2483 }
2484 }
2485
2486 if (!proc_run_process (pi, step, native_signo))
2487 {
2488 if (errno == EBUSY)
2489 warning (_("resume: target already running. "
2490 "Pretend to resume, and hope for the best!"));
2491 else
2492 proc_error (pi, "target_resume", __LINE__);
2493 }
2494}
2495
2496/* Implementation of target_ops::resume. */
2497
2498void
2499procfs_target::resume (ptid_t scope_ptid, int step, enum gdb_signal signo)
2500{
2501 /* Find procinfo for main process. */
2503
2504 proc_resume (pi, scope_ptid, step, signo);
2505}
2506
2507/* Set up to trace signals in the child process. */
2508
2509void
2510procfs_target::pass_signals (gdb::array_view<const unsigned char> pass_signals)
2511{
2512 sigset_t signals;
2514 int signo;
2515
2516 prfillset (&signals);
2517
2518 for (signo = 0; signo < NSIG; signo++)
2519 {
2520 int target_signo = gdb_signal_from_host (signo);
2521 if (target_signo < pass_signals.size () && pass_signals[target_signo])
2522 prdelset (&signals, signo);
2523 }
2524
2525 if (!proc_set_traced_signals (pi, &signals))
2526 proc_error (pi, "pass_signals", __LINE__);
2527}
2528
2529/* Print status information about the child process. */
2530
2531void
2533{
2534 struct inferior *inf = current_inferior ();
2535
2536 gdb_printf (_("\tUsing the running image of %s %s via /proc.\n"),
2537 inf->attach_flag? "attached": "child",
2538 target_pid_to_str (ptid_t (inf->pid)).c_str ());
2539}
2540
2541/* Make it die. Wait for it to die. Clean up after it. Note: this
2542 should only be applied to the real process, not to an LWP, because
2543 of the check for parent-process. If we need this to work for an
2544 LWP, it needs some more logic. */
2545
2546static void
2548{
2549 int parent_pid;
2550
2551 parent_pid = proc_parent_pid (pi);
2552 if (!proc_kill (pi, SIGKILL))
2553 proc_error (pi, "unconditionally_kill, proc_kill", __LINE__);
2554 destroy_procinfo (pi);
2555
2556 /* If pi is GDB's child, wait for it to die. */
2557 if (parent_pid == getpid ())
2558 /* FIXME: should we use waitpid to make sure we get the right event?
2559 Should we check the returned event? */
2560 {
2561#if 0
2562 int status, ret;
2563
2564 ret = waitpid (pi->pid, &status, 0);
2565#else
2566 wait (NULL);
2567#endif
2568 }
2569}
2570
2571/* We're done debugging it, and we want it to go away. Then we want
2572 GDB to forget all about it. */
2573
2574void
2576{
2577 if (inferior_ptid != null_ptid) /* ? */
2578 {
2579 /* Find procinfo for main process. */
2580 procinfo *pi = find_procinfo (inferior_ptid.pid (), 0);
2581
2582 if (pi)
2585 }
2586}
2587
2588/* Forget we ever debugged this thing! */
2589
2590void
2592{
2593 procinfo *pi;
2594
2595 if (inferior_ptid != null_ptid)
2596 {
2597 /* Find procinfo for main process. */
2598 pi = find_procinfo (inferior_ptid.pid (), 0);
2599 if (pi)
2600 destroy_procinfo (pi);
2601 }
2602
2604
2606}
2607
2608/* When GDB forks to create a runnable inferior process, this function
2609 is called on the parent side of the fork. It's job is to do
2610 whatever is necessary to make the child ready to be debugged, and
2611 then wait for the child to synchronize. */
2612
2613void
2615{
2616 procinfo *pi;
2617 int fail;
2618 int lwpid;
2619
2620 pi = create_procinfo (pid, 0);
2621 if (pi == NULL)
2622 perror (_("procfs: out of memory in 'init_inferior'"));
2623
2624 if (!open_procinfo_files (pi, FD_CTL))
2625 proc_error (pi, "init_inferior, open_proc_files", __LINE__);
2626
2627 /*
2628 xmalloc // done
2629 open_procinfo_files // done
2630 link list // done
2631 prfillset (trace)
2632 procfs_notice_signals
2633 prfillset (fault)
2634 prdelset (FLTPAGE)
2635 */
2636
2637 /* If not stopped yet, wait for it to stop. */
2638 if (!(proc_flags (pi) & PR_STOPPED) && !(proc_wait_for_stop (pi)))
2639 dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL);
2640
2641 /* Save some of the /proc state to be restored if we detach. */
2642 /* FIXME: Why? In case another debugger was debugging it?
2643 We're it's parent, for Ghu's sake! */
2644 if (!proc_get_traced_signals (pi, &pi->saved_sigset))
2645 proc_error (pi, "init_inferior, get_traced_signals", __LINE__);
2646 if (!proc_get_held_signals (pi, &pi->saved_sighold))
2647 proc_error (pi, "init_inferior, get_held_signals", __LINE__);
2648 if (!proc_get_traced_faults (pi, &pi->saved_fltset))
2649 proc_error (pi, "init_inferior, get_traced_faults", __LINE__);
2651 proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__);
2653 proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__);
2654
2655 fail = procfs_debug_inferior (pi);
2656 if (fail != 0)
2657 proc_error (pi, "init_inferior (procfs_debug_inferior)", fail);
2658
2659 /* FIXME: logically, we should really be turning OFF run-on-last-close,
2660 and possibly even turning ON kill-on-last-close at this point. But
2661 I can't make that change without careful testing which I don't have
2662 time to do right now... */
2663 /* Turn on run-on-last-close flag so that the child
2664 will die if GDB goes away for some reason. */
2666 proc_error (pi, "init_inferior, set_RLC", __LINE__);
2667
2668 /* We now have have access to the lwpid of the main thread/lwp. */
2669 lwpid = proc_get_current_thread (pi);
2670
2671 /* Create a procinfo for the main lwp. */
2672 create_procinfo (pid, lwpid);
2673
2674 /* We already have a main thread registered in the thread table at
2675 this point, but it didn't have any lwp info yet. Notify the core
2676 about it. This changes inferior_ptid as well. */
2677 thread_change_ptid (this, ptid_t (pid), ptid_t (pid, lwpid, 0));
2678
2680}
2681
2682/* When GDB forks to create a new process, this function is called on
2683 the child side of the fork before GDB exec's the user program. Its
2684 job is to make the child minimally debuggable, so that the parent
2685 GDB process can connect to the child and take over. This function
2686 should do only the minimum to make that possible, and to
2687 synchronize with the parent process. The parent process should
2688 take care of the details. */
2689
2690static void
2692{
2693 /* This routine called on the child side (inferior side)
2694 after GDB forks the inferior. It must use only local variables,
2695 because it may be sharing data space with its parent. */
2696
2697 procinfo *pi;
2698 sysset_t *exitset;
2699
2700 pi = create_procinfo (getpid (), 0);
2701 if (pi == NULL)
2702 perror_with_name (_("procfs: create_procinfo failed in child"));
2703
2704 if (open_procinfo_files (pi, FD_CTL) == 0)
2705 {
2706 proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__);
2708 /* No need to call "dead_procinfo", because we're going to
2709 exit. */
2710 _exit (127);
2711 }
2712
2713 exitset = XNEW (sysset_t);
2714 premptyset (exitset);
2715 praddset (exitset, SYS_execve);
2716
2717 if (!proc_set_traced_sysexit (pi, exitset))
2718 {
2719 proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__);
2721 _exit (127);
2722 }
2723
2724 /* FIXME: should this be done in the parent instead? */
2725 /* Turn off inherit on fork flag so that all grand-children
2726 of gdb start with tracing flags cleared. */
2728 proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__);
2729
2730 /* Turn off run on last close flag, so that the child process
2731 cannot run away just because we close our handle on it.
2732 We want it to wait for the parent to attach. */
2734 proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__);
2735
2736 /* FIXME: No need to destroy the procinfo --
2737 we have our own address space, and we're about to do an exec! */
2738 /*destroy_procinfo (pi);*/
2739}
2740
2741/* Dummy function to be sure fork_inferior uses fork(2) and not vfork(2).
2742 This avoids a possible deadlock gdb and its vfork'ed child. */
2743static void
2745{
2746}
2747
2748/* This function is called BEFORE gdb forks the inferior process. Its
2749 only real responsibility is to set things up for the fork, and tell
2750 GDB which two functions to call after the fork (one for the parent,
2751 and one for the child).
2752
2753 This function does a complicated search for a unix shell program,
2754 which it then uses to parse arguments and environment variables to
2755 be sent to the child. I wonder whether this code could not be
2756 abstracted out and shared with other unix targets such as
2757 inf-ptrace? */
2758
2759void
2760procfs_target::create_inferior (const char *exec_file,
2761 const std::string &allargs,
2762 char **env, int from_tty)
2763{
2764 const char *shell_file = get_shell ();
2765 char *tryname;
2766 int pid;
2767
2768 if (strchr (shell_file, '/') == NULL)
2769 {
2770
2771 /* We will be looking down the PATH to find shell_file. If we
2772 just do this the normal way (via execlp, which operates by
2773 attempting an exec for each element of the PATH until it
2774 finds one which succeeds), then there will be an exec for
2775 each failed attempt, each of which will cause a PR_SYSEXIT
2776 stop, and we won't know how to distinguish the PR_SYSEXIT's
2777 for these failed execs with the ones for successful execs
2778 (whether the exec has succeeded is stored at that time in the
2779 carry bit or some such architecture-specific and
2780 non-ABI-specified place).
2781
2782 So I can't think of anything better than to search the PATH
2783 now. This has several disadvantages: (1) There is a race
2784 condition; if we find a file now and it is deleted before we
2785 exec it, we lose, even if the deletion leaves a valid file
2786 further down in the PATH, (2) there is no way to know exactly
2787 what an executable (in the sense of "capable of being
2788 exec'd") file is. Using access() loses because it may lose
2789 if the caller is the superuser; failing to use it loses if
2790 there are ACLs or some such. */
2791
2792 const char *p;
2793 const char *p1;
2794 /* FIXME-maybe: might want "set path" command so user can change what
2795 path is used from within GDB. */
2796 const char *path = getenv ("PATH");
2797 int len;
2798 struct stat statbuf;
2799
2800 if (path == NULL)
2801 path = "/bin:/usr/bin";
2802
2803 tryname = (char *) alloca (strlen (path) + strlen (shell_file) + 2);
2804 for (p = path; p != NULL; p = p1 ? p1 + 1: NULL)
2805 {
2806 p1 = strchr (p, ':');
2807 if (p1 != NULL)
2808 len = p1 - p;
2809 else
2810 len = strlen (p);
2811 memcpy (tryname, p, len);
2812 tryname[len] = '\0';
2813 strcat (tryname, "/");
2814 strcat (tryname, shell_file);
2815 if (access (tryname, X_OK) < 0)
2816 continue;
2817 if (stat (tryname, &statbuf) < 0)
2818 continue;
2819 if (!S_ISREG (statbuf.st_mode))
2820 /* We certainly need to reject directories. I'm not quite
2821 as sure about FIFOs, sockets, etc., but I kind of doubt
2822 that people want to exec() these things. */
2823 continue;
2824 break;
2825 }
2826 if (p == NULL)
2827 /* Not found. This must be an error rather than merely passing
2828 the file to execlp(), because execlp() would try all the
2829 exec()s, causing GDB to get confused. */
2830 error (_("procfs:%d -- Can't find shell %s in PATH"),
2831 __LINE__, shell_file);
2832
2833 shell_file = tryname;
2834 }
2835
2837 if (!inf->target_is_pushed (this))
2838 inf->push_target (this);
2839
2840 pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap,
2841 NULL, procfs_pre_trace, shell_file, NULL);
2842
2843 /* We have something that executes now. We'll be running through
2844 the shell at this point (if startup-with-shell is true), but the
2845 pid shouldn't change. */
2846 thread_info *thr = add_thread_silent (this, ptid_t (pid));
2847 switch_to_thread (thr);
2848
2850}
2851
2852/* Callback for update_thread_list. Calls "add_thread". */
2853
2854static int
2855procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr)
2856{
2857 ptid_t gdb_threadid = ptid_t (pi->pid, thread->tid, 0);
2858
2859 thread_info *thr = the_procfs_target.find_thread (gdb_threadid);
2860 if (thr == NULL || thr->state == THREAD_EXITED)
2861 add_thread (&the_procfs_target, gdb_threadid);
2862
2863 return 0;
2864}
2865
2866/* Query all the threads that the target knows about, and give them
2867 back to GDB to add to its list. */
2868
2869void
2871{
2872 procinfo *pi;
2873
2874 prune_threads ();
2875
2876 /* Find procinfo for main process. */
2877 pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
2880}
2881
2882/* Return true if the thread is still 'alive'. This guy doesn't
2883 really seem to be doing his job. Got to investigate how to tell
2884 when a thread is really gone. */
2885
2886bool
2888{
2889 int proc, thread;
2890 procinfo *pi;
2891
2892 proc = ptid.pid ();
2893 thread = ptid.lwp ();
2894 /* If I don't know it, it ain't alive! */
2895 pi = find_procinfo (proc, thread);
2896 if (pi == NULL)
2897 return false;
2898
2899 /* If I can't get its status, it ain't alive!
2900 What's more, I need to forget about it! */
2901 if (!proc_get_status (pi))
2902 {
2903 destroy_procinfo (pi);
2904 return false;
2905 }
2906 /* I couldn't have got its status if it weren't alive, so it's
2907 alive. */
2908 return true;
2909}
2910
2911/* Convert PTID to a string. */
2912
2913std::string
2915{
2916 if (ptid.lwp () == 0)
2917 return string_printf ("process %d", ptid.pid ());
2918 else
2919 return string_printf ("LWP %ld", ptid.lwp ());
2920}
2921
2922/* Accepts an integer PID; Returns a string representing a file that
2923 can be opened to get the symbols for the child process. */
2924
2925const char *
2927{
2928 static char buf[PATH_MAX];
2929 char name[PATH_MAX];
2930
2931 /* Solaris 11 introduced /proc/<proc-id>/execname. */
2932 xsnprintf (name, sizeof (name), "/proc/%d/execname", pid);
2933 scoped_fd fd (gdb_open_cloexec (name, O_RDONLY, 0));
2934 if (fd.get () < 0 || read (fd.get (), buf, PATH_MAX - 1) < 0)
2935 {
2936 /* If that fails, fall back to /proc/<proc-id>/path/a.out introduced in
2937 Solaris 10. */
2938 ssize_t len;
2939
2940 xsnprintf (name, sizeof (name), "/proc/%d/path/a.out", pid);
2941 len = readlink (name, buf, PATH_MAX - 1);
2942 if (len <= 0)
2943 strcpy (buf, name);
2944 else
2945 buf[len] = '\0';
2946 }
2947
2948 return buf;
2949}
2950
2951/* Insert a watchpoint. */
2952
2953static int
2954procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag,
2955 int after)
2956{
2957 int pflags = 0;
2958 procinfo *pi;
2959
2960 pi = find_procinfo_or_die (ptid.pid () == -1 ?
2961 inferior_ptid.pid () : ptid.pid (),
2962 0);
2963
2964 /* Translate from GDB's flags to /proc's. */
2965 if (len > 0) /* len == 0 means delete watchpoint. */
2966 {
2967 switch (rwflag) { /* FIXME: need an enum! */
2968 case hw_write: /* default watchpoint (write) */
2969 pflags = WA_WRITE;
2970 break;
2971 case hw_read: /* read watchpoint */
2972 pflags = WA_READ;
2973 break;
2974 case hw_access: /* access watchpoint */
2975 pflags = WA_READ | WA_WRITE;
2976 break;
2977 case hw_execute: /* execution HW breakpoint */
2978 pflags = WA_EXEC;
2979 break;
2980 default: /* Something weird. Return error. */
2981 return -1;
2982 }
2983 if (after) /* Stop after r/w access is completed. */
2984 pflags |= WA_TRAPAFTER;
2985 }
2986
2987 if (!proc_set_watchpoint (pi, addr, len, pflags))
2988 {
2989 if (errno == E2BIG) /* Typical error for no resources. */
2990 return -1; /* fail */
2991 /* GDB may try to remove the same watchpoint twice.
2992 If a remove request returns no match, don't error. */
2993 if (errno == ESRCH && len == 0)
2994 return 0; /* ignore */
2995 proc_error (pi, "set_watchpoint", __LINE__);
2996 }
2997 return 0;
2998}
2999
3000/* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE
3001 is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint,
3002 or bp_hardware_watchpoint. CNT is the number of watchpoints used so
3003 far. */
3004
3005int
3007{
3008 /* Due to the way that proc_set_watchpoint() is implemented, host
3009 and target pointers must be of the same size. If they are not,
3010 we can't use hardware watchpoints. This limitation is due to the
3011 fact that proc_set_watchpoint() calls
3012 procfs_address_to_host_pointer(); a close inspection of
3013 procfs_address_to_host_pointer will reveal that an internal error
3014 will be generated when the host and target pointer sizes are
3015 different. */
3016 struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
3017
3018 if (sizeof (void *) != ptr_type->length ())
3019 return 0;
3020
3021 /* Other tests here??? */
3022
3023 return 1;
3024}
3025
3026/* Returns non-zero if process is stopped on a hardware watchpoint
3027 fault, else returns zero. */
3028
3029bool
3031{
3032 procinfo *pi;
3033
3034 pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
3035
3036 if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))
3037 if (proc_why (pi) == PR_FAULTED)
3038 if (proc_what (pi) == FLTWATCH)
3039 return true;
3040 return false;
3041}
3042
3043/* Returns 1 if the OS knows the position of the triggered watchpoint,
3044 and sets *ADDR to that address. Returns 0 if OS cannot report that
3045 address. This function is only called if
3046 procfs_stopped_by_watchpoint returned 1, thus no further checks are
3047 done. The function also assumes that ADDR is not NULL. */
3048
3049bool
3051{
3052 procinfo *pi;
3053
3054 pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
3055 return proc_watchpoint_address (pi, addr);
3056}
3057
3058int
3059procfs_target::insert_watchpoint (CORE_ADDR addr, int len,
3060 enum target_hw_bp_type type,
3061 struct expression *cond)
3062{
3065 /* When a hardware watchpoint fires off the PC will be left at
3066 the instruction following the one which caused the
3067 watchpoint. It will *NOT* be necessary for GDB to step over
3068 the watchpoint. */
3069 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1);
3070 else
3071 /* When a hardware watchpoint fires off the PC will be left at
3072 the instruction which caused the watchpoint. It will be
3073 necessary for GDB to step over the watchpoint. */
3074 return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0);
3075}
3076
3077int
3078procfs_target::remove_watchpoint (CORE_ADDR addr, int len,
3079 enum target_hw_bp_type type,
3080 struct expression *cond)
3081{
3082 return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0);
3083}
3084
3085int
3087{
3088 /* The man page for proc(4) on Solaris 2.6 and up says that the
3089 system can support "thousands" of hardware watchpoints, but gives
3090 no method for finding out how many; It doesn't say anything about
3091 the allowed size for the watched area either. So we just tell
3092 GDB 'yes'. */
3093 return 1;
3094}
3095
3096/* Memory Mappings Functions: */
3097
3098/* Call a callback function once for each mapping, passing it the
3099 mapping, an optional secondary callback function, and some optional
3100 opaque data. Quit and return the first non-zero value returned
3101 from the callback.
3102
3103 PI is the procinfo struct for the process to be mapped. FUNC is
3104 the callback function to be called by this iterator. DATA is the
3105 optional opaque data to be passed to the callback function.
3106 CHILD_FUNC is the optional secondary function pointer to be passed
3107 to the child function. Returns the first non-zero return value
3108 from the callback function, or zero. */
3109
3110static int
3112 void *data,
3113 int (*func) (struct prmap *map,
3114 find_memory_region_ftype child_func,
3115 void *data))
3116{
3117 char pathname[MAX_PROC_NAME_SIZE];
3118 struct prmap *prmaps;
3119 struct prmap *prmap;
3120 int funcstat;
3121 int nmap;
3122 struct stat sbuf;
3123
3124 /* Get the number of mappings, allocate space,
3125 and read the mappings into prmaps. */
3126 /* Open map fd. */
3127 xsnprintf (pathname, sizeof (pathname), "/proc/%d/map", pi->pid);
3128
3129 scoped_fd map_fd (open (pathname, O_RDONLY));
3130 if (map_fd.get () < 0)
3131 proc_error (pi, "iterate_over_mappings (open)", __LINE__);
3132
3133 /* Use stat to determine the file size, and compute
3134 the number of prmap_t objects it contains. */
3135 if (fstat (map_fd.get (), &sbuf) != 0)
3136 proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
3137
3138 nmap = sbuf.st_size / sizeof (prmap_t);
3139 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
3140 if (read (map_fd.get (), (char *) prmaps, nmap * sizeof (*prmaps))
3141 != (nmap * sizeof (*prmaps)))
3142 proc_error (pi, "iterate_over_mappings (read)", __LINE__);
3143
3144 for (prmap = prmaps; nmap > 0; prmap++, nmap--)
3145 {
3146 funcstat = (*func) (prmap, child_func, data);
3147 if (funcstat != 0)
3148 return funcstat;
3149 }
3150
3151 return 0;
3152}
3153
3154/* Implements the to_find_memory_regions method. Calls an external
3155 function for each memory region.
3156 Returns the integer value returned by the callback. */
3157
3158static int
3160 find_memory_region_ftype func, void *data)
3161{
3162 return (*func) ((CORE_ADDR) map->pr_vaddr,
3163 map->pr_size,
3164 (map->pr_mflags & MA_READ) != 0,
3165 (map->pr_mflags & MA_WRITE) != 0,
3166 (map->pr_mflags & MA_EXEC) != 0,
3167 1, /* MODIFIED is unknown, pass it as true. */
3168 false,
3169 data);
3170}
3171
3172/* External interface. Calls a callback function once for each
3173 mapped memory region in the child process, passing as arguments:
3174
3175 CORE_ADDR virtual_address,
3176 unsigned long size,
3177 int read, TRUE if region is readable by the child
3178 int write, TRUE if region is writable by the child
3179 int execute TRUE if region is executable by the child.
3180
3181 Stops iterating and returns the first non-zero value returned by
3182 the callback. */
3183
3184int
3192
3193/* Returns an ascii representation of a memory mapping's flags. */
3194
3195static char *
3197{
3198 static char asciiflags[8];
3199
3200 strcpy (asciiflags, "-------");
3201 if (flags & MA_STACK)
3202 asciiflags[1] = 's';
3203 if (flags & MA_BREAK)
3204 asciiflags[2] = 'b';
3205 if (flags & MA_SHARED)
3206 asciiflags[3] = 's';
3207 if (flags & MA_READ)
3208 asciiflags[4] = 'r';
3209 if (flags & MA_WRITE)
3210 asciiflags[5] = 'w';
3211 if (flags & MA_EXEC)
3212 asciiflags[6] = 'x';
3213 return (asciiflags);
3214}
3215
3216/* Callback function, does the actual work for 'info proc
3217 mappings'. */
3218
3219static int
3221 void *unused)
3222{
3223 unsigned int pr_off;
3224
3225 pr_off = (unsigned int) map->pr_offset;
3226
3227 if (gdbarch_addr_bit (target_gdbarch ()) == 32)
3228 gdb_printf ("\t%#10lx %#10lx %#10lx %#10x %7s\n",
3229 (unsigned long) map->pr_vaddr,
3230 (unsigned long) map->pr_vaddr + map->pr_size - 1,
3231 (unsigned long) map->pr_size,
3232 pr_off,
3233 mappingflags (map->pr_mflags));
3234 else
3235 gdb_printf (" %#18lx %#18lx %#10lx %#10x %7s\n",
3236 (unsigned long) map->pr_vaddr,
3237 (unsigned long) map->pr_vaddr + map->pr_size - 1,
3238 (unsigned long) map->pr_size,
3239 pr_off,
3240 mappingflags (map->pr_mflags));
3241
3242 return 0;
3243}
3244
3245/* Implement the "info proc mappings" subcommand. */
3246
3247static void
3249{
3250 if (summary)
3251 return; /* No output for summary mode. */
3252
3253 gdb_printf (_("Mapped address spaces:\n\n"));
3254 if (gdbarch_ptr_bit (target_gdbarch ()) == 32)
3255 gdb_printf ("\t%10s %10s %10s %10s %7s\n",
3256 "Start Addr",
3257 " End Addr",
3258 " Size",
3259 " Offset",
3260 "Flags");
3261 else
3262 gdb_printf (" %18s %18s %10s %10s %7s\n",
3263 "Start Addr",
3264 " End Addr",
3265 " Size",
3266 " Offset",
3267 "Flags");
3268
3270 gdb_printf ("\n");
3271}
3272
3273/* Implement the "info proc" command. */
3274
3275bool
3276procfs_target::info_proc (const char *args, enum info_proc_what what)
3277{
3278 procinfo *process = NULL;
3279 procinfo *thread = NULL;
3280 char *tmp = NULL;
3281 int pid = 0;
3282 int tid = 0;
3283 int mappings = 0;
3284
3285 switch (what)
3286 {
3287 case IP_MINIMAL:
3288 break;
3289
3290 case IP_MAPPINGS:
3291 case IP_ALL:
3292 mappings = 1;
3293 break;
3294
3295 default:
3296 error (_("Not supported on this target."));
3297 }
3298
3299 gdb_argv built_argv (args);
3300 for (char *arg : built_argv)
3301 {
3302 if (isdigit (arg[0]))
3303 {
3304 pid = strtoul (arg, &tmp, 10);
3305 if (*tmp == '/')
3306 tid = strtoul (++tmp, NULL, 10);
3307 }
3308 else if (arg[0] == '/')
3309 {
3310 tid = strtoul (arg + 1, NULL, 10);
3311 }
3312 }
3313
3314 procinfo_up temporary_procinfo;
3315 if (pid == 0)
3316 pid = inferior_ptid.pid ();
3317 if (pid == 0)
3318 error (_("No current process: you must name one."));
3319 else
3320 {
3321 /* Have pid, will travel.
3322 First see if it's a process we're already debugging. */
3323 process = find_procinfo (pid, 0);
3324 if (process == NULL)
3325 {
3326 /* No. So open a procinfo for it, but
3327 remember to close it again when finished. */
3328 process = create_procinfo (pid, 0);
3329 temporary_procinfo.reset (process);
3330 if (!open_procinfo_files (process, FD_CTL))
3331 proc_error (process, "info proc, open_procinfo_files", __LINE__);
3332 }
3333 }
3334 if (tid != 0)
3335 thread = create_procinfo (pid, tid);
3336
3337 if (process)
3338 {
3339 gdb_printf (_("process %d flags:\n"), process->pid);
3340 proc_prettyprint_flags (proc_flags (process), 1);
3341 if (proc_flags (process) & (PR_STOPPED | PR_ISTOP))
3342 proc_prettyprint_why (proc_why (process), proc_what (process), 1);
3343 if (proc_get_nthreads (process) > 1)
3344 gdb_printf ("Process has %d threads.\n",
3345 proc_get_nthreads (process));
3346 }
3347 if (thread)
3348 {
3349 gdb_printf (_("thread %d flags:\n"), thread->tid);
3350 proc_prettyprint_flags (proc_flags (thread), 1);
3351 if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP))
3352 proc_prettyprint_why (proc_why (thread), proc_what (thread), 1);
3353 }
3354
3355 if (mappings)
3356 info_proc_mappings (process, 0);
3357
3358 return true;
3359}
3360
3361/* Modify the status of the system call identified by SYSCALLNUM in
3362 the set of syscalls that are currently traced/debugged.
3363
3364 If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set
3365 will be updated. Otherwise, the exit syscalls set will be updated.
3366
3367 If MODE is FLAG_SET, then traces will be enabled. Otherwise, they
3368 will be disabled. */
3369
3370static void
3371proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit,
3372 int mode, int from_tty)
3373{
3374 sysset_t *sysset;
3375
3376 if (entry_or_exit == PR_SYSENTRY)
3377 sysset = proc_get_traced_sysentry (pi, NULL);
3378 else
3379 sysset = proc_get_traced_sysexit (pi, NULL);
3380
3381 if (sysset == NULL)
3382 proc_error (pi, "proc-trace, get_traced_sysset", __LINE__);
3383
3384 if (mode == FLAG_SET)
3385 praddset (sysset, syscallnum);
3386 else
3387 prdelset (sysset, syscallnum);
3388
3389 if (entry_or_exit == PR_SYSENTRY)
3390 {
3391 if (!proc_set_traced_sysentry (pi, sysset))
3392 proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__);
3393 }
3394 else
3395 {
3396 if (!proc_set_traced_sysexit (pi, sysset))
3397 proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__);
3398 }
3399}
3400
3401static void
3402proc_trace_syscalls (const char *args, int from_tty, int entry_or_exit, int mode)
3403{
3404 procinfo *pi;
3405
3406 if (inferior_ptid.pid () <= 0)
3407 error (_("you must be debugging a process to use this command."));
3408
3409 if (args == NULL || args[0] == 0)
3410 error_no_arg (_("system call to trace"));
3411
3412 pi = find_procinfo_or_die (inferior_ptid.pid (), 0);
3413 if (isdigit (args[0]))
3414 {
3415 const int syscallnum = atoi (args);
3416
3417 proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty);
3418 }
3419}
3420
3421static void
3422proc_trace_sysentry_cmd (const char *args, int from_tty)
3423{
3424 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET);
3425}
3426
3427static void
3428proc_trace_sysexit_cmd (const char *args, int from_tty)
3429{
3430 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET);
3431}
3432
3433static void
3434proc_untrace_sysentry_cmd (const char *args, int from_tty)
3435{
3436 proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET);
3437}
3438
3439static void
3440proc_untrace_sysexit_cmd (const char *args, int from_tty)
3441{
3442 proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET);
3443}
3444
3445void _initialize_procfs ();
3446void
3448{
3449 add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd,
3450 _("Give a trace of entries into the syscall."));
3451 add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd,
3452 _("Give a trace of exits from the syscall."));
3453 add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd,
3454 _("Cancel a trace of entries into the syscall."));
3455 add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd,
3456 _("Cancel a trace of exits from the syscall."));
3457
3459}
3460
3461/* =================== END, GDB "MODULE" =================== */
3462
3463
3464
3465/* miscellaneous stubs: */
3466
3467/* The following satisfy a few random symbols mostly created by the
3468 solaris threads implementation, which I will chase down later. */
3469
3470/* Return a pid for which we guarantee we will be able to find a
3471 'live' procinfo. */
3472
3473ptid_t
3475{
3476 return ptid_t (procinfo_list ? procinfo_list->pid : -1);
3477}
3478
3479/* =================== GCORE .NOTE "MODULE" =================== */
3480
3481static void
3483 gdb::unique_xmalloc_ptr<char> &note_data,
3484 int *note_size, enum gdb_signal stop_signal)
3485{
3487 gdb_gregset_t gregs;
3488 gdb_fpregset_t fpregs;
3489 unsigned long merged_pid;
3490
3491 merged_pid = ptid.lwp () << 16 | ptid.pid ();
3492
3493 /* This part is the old method for fetching registers.
3494 It should be replaced by the newer one using regsets
3495 once it is implemented in this platform:
3496 gdbarch_iterate_over_regset_sections(). */
3497
3499
3500 fill_gregset (regcache, &gregs, -1);
3501 note_data.reset (elfcore_write_lwpstatus (obfd,
3502 note_data.release (),
3503 note_size,
3504 merged_pid,
3505 stop_signal,
3506 &gregs));
3507 fill_fpregset (regcache, &fpregs, -1);
3508 note_data.reset (elfcore_write_prfpreg (obfd,
3509 note_data.release (),
3510 note_size,
3511 &fpregs,
3512 sizeof (fpregs)));
3513}
3514
3516{
3518 gdb::unique_xmalloc_ptr<char> &note_data,
3519 int *note_size, gdb_signal stop_signal)
3522 {}
3523
3524 bfd *obfd;
3525 gdb::unique_xmalloc_ptr<char> &note_data;
3527 enum gdb_signal stop_signal;
3528};
3529
3530static int
3532{
3533 struct procfs_corefile_thread_data *args
3534 = (struct procfs_corefile_thread_data *) data;
3535
3536 if (pi != NULL)
3537 {
3538 ptid_t ptid = ptid_t (pi->pid, thread->tid, 0);
3539
3540 procfs_do_thread_registers (args->obfd, ptid,
3541 args->note_data,
3542 args->note_size,
3543 args->stop_signal);
3544 }
3545 return 0;
3546}
3547
3548static int
3549find_signalled_thread (struct thread_info *info, void *data)
3550{
3551 if (info->stop_signal () != GDB_SIGNAL_0
3552 && info->ptid.pid () == inferior_ptid.pid ())
3553 return 1;
3554
3555 return 0;
3556}
3557
3558static enum gdb_signal
3560{
3561 struct thread_info *info =
3563
3564 if (info)
3565 return info->stop_signal ();
3566 else
3567 return GDB_SIGNAL_0;
3568}
3569
3570gdb::unique_xmalloc_ptr<char>
3572{
3573 gdb_gregset_t gregs;
3574 char fname[16] = {'\0'};
3575 char psargs[80] = {'\0'};
3577 gdb::unique_xmalloc_ptr<char> note_data;
3578 enum gdb_signal stop_signal;
3579
3580 if (get_exec_file (0))
3581 {
3582 strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname));
3583 fname[sizeof (fname) - 1] = 0;
3584 strncpy (psargs, get_exec_file (0), sizeof (psargs));
3585 psargs[sizeof (psargs) - 1] = 0;
3586
3587 const std::string &inf_args = current_inferior ()->args ();
3588 if (!inf_args.empty () &&
3589 inf_args.length () < ((int) sizeof (psargs) - (int) strlen (psargs)))
3590 {
3591 strncat (psargs, " ",
3592 sizeof (psargs) - strlen (psargs));
3593 strncat (psargs, inf_args.c_str (),
3594 sizeof (psargs) - strlen (psargs));
3595 }
3596 }
3597
3598 note_data.reset (elfcore_write_prpsinfo (obfd,
3599 note_data.release (),
3600 note_size,
3601 fname,
3602 psargs));
3603
3604 stop_signal = find_stop_signal ();
3605
3606 fill_gregset (get_current_regcache (), &gregs, -1);
3607 note_data.reset (elfcore_write_pstatus (obfd, note_data.release (), note_size,
3608 inferior_ptid.pid (),
3609 stop_signal, &gregs));
3610
3611 procfs_corefile_thread_data thread_args (obfd, note_data, note_size,
3612 stop_signal);
3614 &thread_args);
3615
3616 gdb::optional<gdb::byte_vector> auxv =
3617 target_read_alloc (current_inferior ()->top_target (),
3618 TARGET_OBJECT_AUXV, NULL);
3619 if (auxv && !auxv->empty ())
3620 note_data.reset (elfcore_write_note (obfd, note_data.release (), note_size,
3621 "CORE", NT_AUXV, auxv->data (),
3622 auxv->size ()));
3623
3624 return note_data;
3625}
3626/* =================== END GCORE .NOTE "MODULE" =================== */
void supply_gregset(struct regcache *regcache, const gdb_gregset_t *gregsetp)
void fill_gregset(const struct regcache *regcache, gdb_gregset_t *gregsetp, int regno)
void supply_fpregset(struct regcache *regcache, const gdb_fpregset_t *fpregsetp)
void fill_fpregset(const struct regcache *regcache, gdb_fpregset_t *fpregsetp, int regno)
int regnum
const char *const name
void xfree(void *)
struct gdbarch * target_gdbarch(void)
target_xfer_partial_ftype memory_xfer_auxv
bptype
Definition breakpoint.h:84
ui_file_style style() const
Definition cli-style.c:169
void maybe_unpush_target()
Definition inf-child.c:199
const std::string & args() const
Definition inferior.h:533
thread_info * find_thread(ptid_t ptid)
void store_registers(struct regcache *, int) override
Definition procfs.c:1972
void fetch_registers(struct regcache *, int) override
Definition procfs.c:1923
int find_memory_regions(find_memory_region_ftype func, void *data) override
Definition procfs.c:3185
gdb::unique_xmalloc_ptr< char > make_corefile_notes(bfd *, int *) override
Definition procfs.c:3571
int region_ok_for_hw_watchpoint(CORE_ADDR, int) override
Definition procfs.c:3086
bool stopped_data_address(CORE_ADDR *) override
Definition procfs.c:3050
int can_use_hw_breakpoint(enum bptype, int, int) override
Definition procfs.c:3006
void update_thread_list() override
Definition procfs.c:2870
ptid_t wait(ptid_t, struct target_waitstatus *, target_wait_flags) override
Definition procfs.c:2021
const char * pid_to_exec_file(int pid) override
Definition procfs.c:2926
int insert_watchpoint(CORE_ADDR, int, enum target_hw_bp_type, struct expression *) override
Definition procfs.c:3059
void pass_signals(gdb::array_view< const unsigned char >) override
Definition procfs.c:2510
int remove_watchpoint(CORE_ADDR, int, enum target_hw_bp_type, struct expression *) override
Definition procfs.c:3078
std::string pid_to_str(ptid_t) override
Definition procfs.c:2914
void mourn_inferior() override
Definition procfs.c:2591
void files_info() override
Definition procfs.c:2532
bool info_proc(const char *, enum info_proc_what) override
Definition procfs.c:3276
int auxv_parse(const gdb_byte **readptr, const gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) override
Definition procfs.c:173
bool thread_alive(ptid_t ptid) override
Definition procfs.c:2887
enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) override
Definition procfs.c:2335
void attach(const char *, int) override
Definition procfs.c:1759
void detach(inferior *inf, int) override
Definition procfs.c:1786
thread_control_capabilities get_thread_control_capabilities() override
Definition procfs.c:133
void procfs_init_inferior(int pid)
Definition procfs.c:2614
void create_inferior(const char *, const std::string &, char **, int) override
Definition procfs.c:2760
void kill() override
Definition procfs.c:2575
bool stopped_by_watchpoint() override
Definition procfs.c:3030
void resume(ptid_t, int, enum gdb_signal) override
Definition procfs.c:2499
gdbarch * arch() const
Definition regcache.c:231
ptid_t ptid() const
Definition regcache.h:408
enum thread_state state
Definition gdbthread.h:339
void error_no_arg(const char *why)
Definition cli-cmds.c:206
struct cmd_list_element * add_com(const char *name, enum command_class theclass, cmd_simple_func_ftype *fun, const char *doc)
cli_style_option file_name_style
@ no_class
Definition command.h:53
const char * get_exec_file(int err)
Definition corefile.c:150
#define SEEK_SET
Definition defs.h:101
bool info_verbose
Definition top.c:1941
info_proc_what
Definition defs.h:380
@ IP_ALL
Definition defs.h:406
@ IP_MAPPINGS
Definition defs.h:385
@ IP_MINIMAL
Definition defs.h:382
int(* find_memory_region_ftype)(CORE_ADDR addr, unsigned long size, int read, int write, int exec, int modified, bool memory_tagged, void *data)
Definition defs.h:350
static ULONGEST extract_unsigned_integer(gdb::array_view< const gdb_byte > buf, enum bfd_endian byte_order)
Definition defs.h:480
ssize_t read(int fd, void *buf, size_t count)
ptid_t gdb_startup_inferior(pid_t pid, int num_traps)
Definition fork-child.c:124
pid_t fork_inferior(const char *exec_file_arg, const std::string &allargs, char **env, void(*traceme_fun)(), gdb::function_view< void(int)> init_trace_fun, void(*pre_trace_fun)(), const char *shell_file_arg, void(*exec_fun)(const char *file, char *const *argv, char *const *env))
#define START_INFERIOR_TRAPS_EXPECTED
int gdbarch_pc_regnum(struct gdbarch *gdbarch)
Definition gdbarch.c:2054
int gdbarch_have_nonsteppable_watchpoint(struct gdbarch *gdbarch)
Definition gdbarch.c:3564
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition gdbarch.c:1396
CORE_ADDR gdbarch_pointer_to_address(struct gdbarch *gdbarch, struct type *type, const gdb_byte *buf)
Definition gdbarch.c:2545
int gdbarch_addr_bit(struct gdbarch *gdbarch)
Definition gdbarch.c:1739
bfd int * note_size
bfd * obfd
int gdbarch_sp_regnum(struct gdbarch *gdbarch)
Definition gdbarch.c:2037
void gdbarch_address_to_pointer(struct gdbarch *gdbarch, struct type *type, gdb_byte *buf, CORE_ADDR addr)
Definition gdbarch.c:2562
int gdbarch_fp0_regnum(struct gdbarch *gdbarch)
Definition gdbarch.c:2088
int gdbarch_ptr_bit(struct gdbarch *gdbarch)
Definition gdbarch.c:1722
struct thread_info * add_thread(process_stratum_target *targ, ptid_t ptid)
Definition thread.c:336
struct thread_info * add_thread_silent(process_stratum_target *targ, ptid_t ptid)
Definition thread.c:296
void delete_thread(thread_info *thread)
Definition thread.c:527
@ THREAD_EXITED
Definition gdbthread.h:79
void switch_to_thread(struct thread_info *thr)
Definition thread.c:1360
struct thread_info * iterate_over_threads(thread_callback_func, void *)
Definition thread.c:584
void thread_change_ptid(process_stratum_target *targ, ptid_t old_ptid, ptid_t new_ptid)
Definition thread.c:811
void prune_threads(void)
Definition thread.c:737
bool in_thread_list(process_stratum_target *targ, ptid_t ptid)
Definition thread.c:631
void switch_to_no_thread()
Definition thread.c:1345
const struct builtin_type * builtin_type(struct gdbarch *gdbarch)
Definition gdbtypes.c:6168
mach_port_t kern_return_t mach_port_t mach_msg_type_name_t msgportsPoly mach_port_t kern_return_t pid_t pid mach_port_t kern_return_t mach_port_t task mach_port_t kern_return_t int flags
Definition gnu-nat.c:1861
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int int rusage_t pid_t pid
Definition gnu-nat.c:1791
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition gnu-nat.c:1790
GDB_FPREGSET_T gdb_fpregset_t
Definition gregset.h:35
GDB_GREGSET_T gdb_gregset_t
Definition gregset.h:34
target_waitstatus host_status_to_waitstatus(int hoststatus)
Definition inf-child.c:57
void add_inf_child_target(inf_child_target *target)
Definition inf-child.c:418
ptid_t inferior_ptid
Definition infcmd.c:74
void inferior_appeared(struct inferior *inf, int pid)
Definition inferior.c:363
void detach_inferior(inferior *inf)
Definition inferior.c:340
struct inferior * current_inferior(void)
Definition inferior.c:55
void set_sigint_trap(void)
Definition inflow.c:866
void generic_mourn_inferior(void)
Definition target.c:3661
void clear_sigint_trap(void)
Definition inflow.c:881
void get_last_target_status(process_stratum_target **target, ptid_t *ptid, target_waitstatus *status)
Definition infrun.c:4621
void proc_prettyprint_syscall(int num, int verbose)
void proc_prettyprint_flags(unsigned long flags, int verbose)
Definition proc-flags.c:108
long procfs_ctl_t
Definition proc-utils.h:118
#define PROC_PRETTYFPRINT_STATUS(X, Y, Z, T)
Definition proc-utils.h:113
#define PROCFS_NOTE(X)
Definition proc-utils.h:112
void proc_prettyprint_why(unsigned long why, unsigned long what, int verbose)
Definition proc-why.c:109
static int proc_get_nthreads(procinfo *pi)
Definition procfs.c:1556
static int proc_parent_pid(procinfo *pi)
Definition procfs.c:1497
@ KILL
Definition procfs.c:553
@ NOKILL
Definition procfs.c:553
static void proc_error(procinfo *pi, const char *func, int line)
Definition procfs.c:605
static int invalidate_cache(procinfo *parent, procinfo *pi, void *ptr)
Definition procfs.c:2409
static int proc_unset_async(procinfo *pi)
Definition procfs.c:849
static void procfs_pre_trace(void)
Definition procfs.c:2744
static int proc_set_gregs(procinfo *pi)
Definition procfs.c:1405
void _initialize_procfs()
Definition procfs.c:3447
static int proc_watchpoint_address(procinfo *pi, CORE_ADDR *addr)
Definition procfs.c:708
static int proc_delete_dead_threads(procinfo *parent, procinfo *thread, void *ignore)
Definition procfs.c:1597
static int iterate_over_mappings(procinfo *pi, find_memory_region_ftype child_func, void *data, int(*func)(struct prmap *map, find_memory_region_ftype child_func, void *data))
Definition procfs.c:3111
static long * proc_sysargs(procinfo *pi)
Definition procfs.c:737
ptid_t procfs_first_available(void)
Definition procfs.c:3474
static int proc_set_traced_sysexit(procinfo *pi, sysset_t *sysset)
Definition procfs.c:1057
static void do_detach()
Definition procfs.c:1871
static procinfo * find_procinfo_or_die(int pid, int tid)
Definition procfs.c:310
static void procfs_do_thread_registers(bfd *obfd, ptid_t ptid, gdb::unique_xmalloc_ptr< char > &note_data, int *note_size, enum gdb_signal stop_signal)
Definition procfs.c:3482
static int proc_set_held_signals(procinfo *pi, sigset_t *sighold)
Definition procfs.c:1091
static int proc_set_fpregs(procinfo *pi)
Definition procfs.c:1439
static int info_mappings_callback(struct prmap *map, find_memory_region_ftype ignore, void *unused)
Definition procfs.c:3220
static fltset_t * proc_get_traced_faults(procinfo *pi, fltset_t *save)
Definition procfs.c:1178
static enum target_xfer_status procfs_xfer_memory(gdb_byte *, const gdb_byte *, ULONGEST, ULONGEST, ULONGEST *)
Definition procfs.c:2360
static void proc_untrace_sysentry_cmd(const char *args, int from_tty)
Definition procfs.c:3434
static procinfo * create_procinfo(int pid, int tid)
Definition procfs.c:442
#define MAX_PROC_NAME_SIZE
Definition procfs.c:226
static sigset_t * proc_get_traced_signals(procinfo *pi, sigset_t *save)
Definition procfs.c:1151
static void dead_procinfo(procinfo *p, const char *msg, int killp)
Definition procfs.c:560
static procinfo * find_procinfo(int pid, int tid)
Definition procfs.c:281
static procfs_target the_procfs_target
Definition procfs.c:166
static int proc_iterate_over_threads(procinfo *pi, int(*func)(procinfo *, procinfo *, void *), void *ptr)
Definition procfs.c:1665
@ FD_STATUS
Definition procfs.c:363
@ FD_CTL
Definition procfs.c:363
@ FD_AS
Definition procfs.c:363
static void proc_trace_sysentry_cmd(const char *args, int from_tty)
Definition procfs.c:3422
static int procfs_notice_thread(procinfo *pi, procinfo *thread, void *ptr)
Definition procfs.c:2855
static gdb_gregset_t * proc_get_gregs(procinfo *pi)
Definition procfs.c:1378
static void proc_trace_syscalls(const char *args, int from_tty, int entry_or_exit, int mode)
Definition procfs.c:3402
static void proc_untrace_sysexit_cmd(const char *args, int from_tty)
Definition procfs.c:3440
static int proc_get_status(procinfo *pi)
Definition procfs.c:621
static int proc_stop_process(procinfo *pi)
Definition procfs.c:858
static int proc_clear_current_fault(procinfo *pi)
Definition procfs.c:1260
static int open_procinfo_files(procinfo *p, int which)
Definition procfs.c:366
static int procfs_debug_inferior(procinfo *pi)
Definition procfs.c:1712
static int proc_kill(procinfo *pi, int signo)
Definition procfs.c:1472
static int proc_set_traced_sysentry(procinfo *pi, sysset_t *sysset)
Definition procfs.c:1023
static int open_with_retry(const char *pathname, int flags)
Definition procfs.c:337
static void unconditionally_kill_inferior(procinfo *pi)
Definition procfs.c:2547
static int proc_what(procinfo *pi)
Definition procfs.c:693
static procinfo * procinfo_list
Definition procfs.c:275
static int proc_nsysarg(procinfo *pi)
Definition procfs.c:724
static enum gdb_signal find_stop_signal(void)
Definition procfs.c:3559
static int proc_set_async(procinfo *pi)
Definition procfs.c:839
static sigset_t * proc_get_held_signals(procinfo *pi, sigset_t *save)
Definition procfs.c:1124
static int proc_update_threads(procinfo *pi)
Definition procfs.c:1609
static int proc_get_current_thread(procinfo *pi)
Definition procfs.c:1575
static int procfs_corefile_thread_callback(procinfo *pi, procinfo *thread, void *data)
Definition procfs.c:3531
static void procfs_set_exec_trap(void)
Definition procfs.c:2691
static void * procfs_address_to_host_pointer(CORE_ADDR addr)
Definition procfs.c:1518
static int proc_set_watchpoint(procinfo *pi, CORE_ADDR addr, int len, int wflags)
Definition procfs.c:1530
static void do_attach(ptid_t ptid)
Definition procfs.c:1798
static int proc_unset_run_on_last_close(procinfo *pi)
Definition procfs.c:818
static int find_memory_regions_callback(struct prmap *map, find_memory_region_ftype func, void *data)
Definition procfs.c:3159
static gdb_fpregset_t * proc_get_fpregs(procinfo *pi)
Definition procfs.c:1391
static int proc_modify_flag(procinfo *pi, long flag, long mode)
Definition procfs.c:764
static int proc_unset_inherit_on_fork(procinfo *pi)
Definition procfs.c:829
static int proc_set_traced_signals(procinfo *pi, sigset_t *sigset)
Definition procfs.c:955
static void proc_resume(procinfo *pi, ptid_t scope_ptid, int step, enum gdb_signal signo)
Definition procfs.c:2437
static int proc_wait_for_stop(procinfo *pi)
Definition procfs.c:881
static int procfs_set_watchpoint(ptid_t ptid, CORE_ADDR addr, int len, int rwflag, int after)
Definition procfs.c:2954
@ FLAG_RESET
Definition procfs.c:761
@ FLAG_SET
Definition procfs.c:761
static int proc_clear_current_signal(procinfo *pi)
Definition procfs.c:1341
static int proc_set_current_signal(procinfo *pi, int signo)
Definition procfs.c:1288
static int proc_run_process(procinfo *pi, int step, int signo)
Definition procfs.c:923
static void info_proc_mappings(procinfo *pi, int summary)
Definition procfs.c:3248
struct procinfo procinfo
static void close_procinfo_files(procinfo *p)
Definition procfs.c:484
static void proc_warn(procinfo *pi, const char *func, int line)
Definition procfs.c:595
static void proc_trace_sysexit_cmd(const char *args, int from_tty)
Definition procfs.c:3428
static int proc_set_run_on_last_close(procinfo *pi)
Definition procfs.c:808
static int find_signalled_thread(struct thread_info *info, void *data)
Definition procfs.c:3549
static char * mappingflags(long flags)
Definition procfs.c:3196
static void proc_trace_syscalls_1(procinfo *pi, int syscallnum, int entry_or_exit, int mode, int from_tty)
Definition procfs.c:3371
static long proc_flags(procinfo *pi)
Definition procfs.c:669
std::unique_ptr< procinfo, procinfo_deleter > procinfo_up
Definition procfs.c:551
static sysset_t * proc_get_traced_sysentry(procinfo *pi, sysset_t *save)
Definition procfs.c:1205
static void destroy_procinfo(procinfo *p)
Definition procfs.c:523
static int proc_set_traced_faults(procinfo *pi, fltset_t *fltset)
Definition procfs.c:990
static int proc_why(procinfo *pi)
Definition procfs.c:681
static void destroy_one_procinfo(procinfo **list, procinfo *pi)
Definition procfs.c:498
static sysset_t * proc_get_traced_sysexit(procinfo *pi, sysset_t *save)
Definition procfs.c:1232
struct regcache * get_current_regcache(void)
Definition regcache.c:429
struct regcache * get_thread_regcache(process_stratum_target *target, ptid_t ptid)
Definition regcache.c:400
void(* func)(remote_target *remote, char *)
struct type * builtin_data_ptr
Definition gdbtypes.h:2135
Definition gnu-nat.c:153
pid_t pid
Definition gnu-nat.c:165
Definition gnu-nat.h:58
enum gdb_signal stop_signal
Definition procfs.c:3527
procfs_corefile_thread_data(bfd *obfd, gdb::unique_xmalloc_ptr< char > &note_data, int *note_size, gdb_signal stop_signal)
Definition procfs.c:3517
gdb::unique_xmalloc_ptr< char > & note_data
Definition procfs.c:3525
void operator()(procinfo *pi) const
Definition procfs.c:545
int ignore_next_sigstop
Definition procfs.c:235
int status_fd
Definition procfs.c:238
int threads_valid
Definition procfs.c:256
pstatus_t prstatus
Definition procfs.c:249
struct procinfo * next
Definition procfs.c:229
int was_stopped
Definition procfs.c:234
sigset_t saved_sighold
Definition procfs.c:245
int ctl_fd
Definition procfs.c:237
int fpregs_valid
Definition procfs.c:255
int as_fd
Definition procfs.c:239
int tid
Definition procfs.c:231
sysset_t * saved_entryset
Definition procfs.c:247
char pathname[MAX_PROC_NAME_SIZE]
Definition procfs.c:241
sysset_t * saved_exitset
Definition procfs.c:246
fltset_t saved_fltset
Definition procfs.c:243
struct procinfo * thread_list
Definition procfs.c:251
int pid
Definition procfs.c:230
int status_valid
Definition procfs.c:253
int gregs_valid
Definition procfs.c:254
sigset_t saved_sigset
Definition procfs.c:244
target_ops * beneath() const
Definition target.c:3041
virtual enum target_xfer_status xfer_partial(enum target_object object, const char *annex, gdb_byte *readbuf, const gdb_byte *writebuf, ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) TARGET_DEFAULT_RETURN(TARGET_XFER_E_IO)
enum gdb_signal sig
Definition waitstatus.h:414
target_waitkind kind() const
Definition waitstatus.h:345
ULONGEST length() const
Definition gdbtypes.h:983
void target_announce_detach(int from_tty)
Definition target.c:3622
bool target_have_steppable_watchpoint()
Definition target.c:508
void target_fetch_registers(struct regcache *regcache, int regno)
Definition target.c:3928
gdb::optional< gdb::byte_vector > target_read_alloc(struct target_ops *ops, enum target_object object, const char *annex)
Definition target.c:2312
void target_announce_attach(int from_tty, int pid)
Definition target.c:3643
std::string target_pid_to_str(ptid_t ptid)
Definition target.c:2623
void target_mourn_inferior(ptid_t ptid)
Definition target.c:2758
std::unique_ptr< struct target_ops, target_unpusher > target_unpush_up
Definition target.h:2388
target_xfer_status
Definition target.h:219
@ TARGET_XFER_E_IO
Definition target.h:232
@ TARGET_XFER_OK
Definition target.h:221
target_object
Definition target.h:143
@ TARGET_OBJECT_AUXV
Definition target.h:162
@ TARGET_OBJECT_MEMORY
Definition target.h:147
thread_control_capabilities
Definition target.h:105
@ tc_schedlock
Definition target.h:107
static styled_string_s * styled_string(const ui_file_style &style, const char *str, styled_string_s &&tmp={})
Definition ui-out.h:151
int query(const char *ctlstr,...)
Definition utils.c:943
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void gdb_flush(struct ui_file *stream)
Definition utils.c:1498
void warning_filename_and_errno(const char *filename, int saved_errno)
Definition utils.c:625
int parse_pid_to_attach(const char *args)
Definition utils.c:3331
#define gdb_stderr
Definition utils.h:187
@ TARGET_WAITKIND_STOPPED
Definition waitstatus.h:36