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