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