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