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