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