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