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