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