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