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