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