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