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