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