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