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