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