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