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