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