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