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