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