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