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