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