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