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