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