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