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