Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Machine independent support for SVR4 /proc (process file system) for GDB. |
2555fe1a | 2 | |
0b302171 | 3 | Copyright (C) 1999-2003, 2006-2012 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" | |
25 | #include "target.h" | |
26 | #include "gdbcore.h" | |
65554fef | 27 | #include "elf-bfd.h" /* for elfcore_write_* */ |
c3f6f71d | 28 | #include "gdbcmd.h" |
0fda6bd2 | 29 | #include "gdbthread.h" |
7f7fe91e | 30 | #include "regcache.h" |
28439f5e | 31 | #include "inf-child.h" |
c906108c | 32 | |
c3f6f71d | 33 | #if defined (NEW_PROC_API) |
0df8b418 | 34 | #define _STRUCTURED_PROC 1 /* Should be done by configure script. */ |
c3f6f71d | 35 | #endif |
c906108c | 36 | |
c3f6f71d | 37 | #include <sys/procfs.h> |
37de36c6 | 38 | #ifdef HAVE_SYS_FAULT_H |
c3f6f71d | 39 | #include <sys/fault.h> |
37de36c6 KB |
40 | #endif |
41 | #ifdef HAVE_SYS_SYSCALL_H | |
c3f6f71d | 42 | #include <sys/syscall.h> |
37de36c6 | 43 | #endif |
c3f6f71d | 44 | #include <sys/errno.h> |
2555fe1a | 45 | #include "gdb_wait.h" |
0fda6bd2 JM |
46 | #include <signal.h> |
47 | #include <ctype.h> | |
92107356 | 48 | #include "gdb_bfd.h" |
19958708 | 49 | #include "gdb_string.h" |
df8f7274 | 50 | #include "gdb_assert.h" |
44270758 | 51 | #include "inflow.h" |
4e73f23d | 52 | #include "auxv.h" |
d1a7880c | 53 | #include "procfs.h" |
27087a3d | 54 | #include "observer.h" |
0fda6bd2 | 55 | |
77382aee PA |
56 | /* This module provides the interface between GDB and the |
57 | /proc file system, which is used on many versions of Unix | |
58 | as a means for debuggers to control other processes. | |
59 | ||
60 | Examples of the systems that use this interface are: | |
61 | ||
62 | Irix | |
63 | Solaris | |
64 | OSF | |
65 | Unixware | |
66 | AIX5 | |
67 | ||
68 | /proc works by imitating a file system: you open a simulated file | |
69 | that represents the process you wish to interact with, and perform | |
70 | operations on that "file" in order to examine or change the state | |
71 | of the other process. | |
72 | ||
73 | The most important thing to know about /proc and this module is | |
74 | that there are two very different interfaces to /proc: | |
75 | ||
76 | One that uses the ioctl system call, and another that uses read | |
77 | and write system calls. | |
78 | ||
79 | This module has to support both /proc interfaces. This means that | |
80 | there are two different ways of doing every basic operation. | |
81 | ||
82 | In order to keep most of the code simple and clean, I have defined | |
83 | an interface "layer" which hides all these system calls. An ifdef | |
84 | (NEW_PROC_API) determines which interface we are using, and most or | |
85 | all occurrances of this ifdef should be confined to this interface | |
86 | layer. */ | |
87 | ||
88 | /* Determine which /proc API we are using: The ioctl API defines | |
89 | PIOCSTATUS, while the read/write (multiple fd) API never does. */ | |
c906108c | 90 | |
c3f6f71d | 91 | #ifdef NEW_PROC_API |
c906108c | 92 | #include <sys/types.h> |
4b14d3e4 | 93 | #include "gdb_dirent.h" /* opendir/readdir, for listing the LWP's */ |
c3f6f71d | 94 | #endif |
c906108c | 95 | |
c3f6f71d JM |
96 | #include <fcntl.h> /* for O_RDONLY */ |
97 | #include <unistd.h> /* for "X_OK" */ | |
98 | #include "gdb_stat.h" /* for struct stat */ | |
c906108c | 99 | |
103b3ef5 MS |
100 | /* Note: procfs-utils.h must be included after the above system header |
101 | files, because it redefines various system calls using macros. | |
102 | This may be incompatible with the prototype declarations. */ | |
103 | ||
103b3ef5 MS |
104 | #include "proc-utils.h" |
105 | ||
77382aee | 106 | /* Prototypes for supply_gregset etc. */ |
c60c0f5f MS |
107 | #include "gregset.h" |
108 | ||
c3f6f71d | 109 | /* =================== TARGET_OPS "MODULE" =================== */ |
c906108c | 110 | |
77382aee | 111 | /* This module defines the GDB target vector and its methods. */ |
c906108c | 112 | |
136d6dae VP |
113 | static void procfs_attach (struct target_ops *, char *, int); |
114 | static void procfs_detach (struct target_ops *, char *, int); | |
28439f5e | 115 | static void procfs_resume (struct target_ops *, |
2ea28649 | 116 | ptid_t, int, enum gdb_signal); |
f9c72d52 | 117 | static void procfs_stop (ptid_t); |
a14ed312 | 118 | static void procfs_files_info (struct target_ops *); |
28439f5e PA |
119 | static void procfs_fetch_registers (struct target_ops *, |
120 | struct regcache *, int); | |
121 | static void procfs_store_registers (struct target_ops *, | |
122 | struct regcache *, int); | |
13bdd2e7 | 123 | static void procfs_pass_signals (int, unsigned char *); |
7d85a9c0 | 124 | static void procfs_kill_inferior (struct target_ops *ops); |
136d6dae | 125 | static void procfs_mourn_inferior (struct target_ops *ops); |
77382aee | 126 | static void procfs_create_inferior (struct target_ops *, char *, |
136d6dae | 127 | char *, char **, int); |
117de6a9 | 128 | static ptid_t procfs_wait (struct target_ops *, |
4fcbb494 | 129 | ptid_t, struct target_waitstatus *, int); |
0b62613e | 130 | static int procfs_xfer_memory (CORE_ADDR, gdb_byte *, int, int, |
043780a1 AC |
131 | struct mem_attrib *attrib, |
132 | struct target_ops *); | |
4e73f23d RM |
133 | static LONGEST procfs_xfer_partial (struct target_ops *ops, |
134 | enum target_object object, | |
135 | const char *annex, | |
3e43a32a MS |
136 | gdb_byte *readbuf, |
137 | const gdb_byte *writebuf, | |
4e73f23d | 138 | ULONGEST offset, LONGEST len); |
a14ed312 | 139 | |
28439f5e | 140 | static int procfs_thread_alive (struct target_ops *ops, ptid_t); |
a14ed312 | 141 | |
d3581e61 JB |
142 | static void procfs_find_new_threads (struct target_ops *ops); |
143 | static char *procfs_pid_to_str (struct target_ops *, ptid_t); | |
c3f6f71d | 144 | |
19958708 RM |
145 | static int proc_find_memory_regions (int (*) (CORE_ADDR, |
146 | unsigned long, | |
147 | int, int, int, | |
148 | void *), | |
be4d1333 MS |
149 | void *); |
150 | ||
151 | static char * procfs_make_note_section (bfd *, int *); | |
152 | ||
1e03ad20 KB |
153 | static int procfs_can_use_hw_breakpoint (int, int, int); |
154 | ||
145b16a9 UW |
155 | static void procfs_info_proc (struct target_ops *, char *, |
156 | enum info_proc_what); | |
157 | ||
c47ffbe3 | 158 | #if defined (PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64) |
77382aee PA |
159 | /* When GDB is built as 64-bit application on Solaris, the auxv data |
160 | is presented in 64-bit format. We need to provide a custom parser | |
161 | to handle that. */ | |
c47ffbe3 VP |
162 | static int |
163 | procfs_auxv_parse (struct target_ops *ops, gdb_byte **readptr, | |
77382aee | 164 | gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) |
c47ffbe3 | 165 | { |
e17a4113 | 166 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch); |
c47ffbe3 VP |
167 | gdb_byte *ptr = *readptr; |
168 | ||
169 | if (endptr == ptr) | |
170 | return 0; | |
77382aee | 171 | |
c47ffbe3 VP |
172 | if (endptr - ptr < 8 * 2) |
173 | return -1; | |
174 | ||
e17a4113 | 175 | *typep = extract_unsigned_integer (ptr, 4, byte_order); |
c47ffbe3 VP |
176 | ptr += 8; |
177 | /* The size of data is always 64-bit. If the application is 32-bit, | |
178 | it will be zero extended, as expected. */ | |
e17a4113 | 179 | *valp = extract_unsigned_integer (ptr, 8, byte_order); |
c47ffbe3 VP |
180 | ptr += 8; |
181 | ||
182 | *readptr = ptr; | |
183 | return 1; | |
184 | } | |
185 | #endif | |
186 | ||
d1a7880c | 187 | struct target_ops * |
28439f5e | 188 | procfs_target (void) |
c3f6f71d | 189 | { |
28439f5e PA |
190 | struct target_ops *t = inf_child_target (); |
191 | ||
77382aee PA |
192 | t->to_shortname = "procfs"; |
193 | t->to_longname = "Unix /proc child process"; | |
194 | t->to_doc = | |
c3f6f71d | 195 | "Unix /proc child process (started by the \"run\" command)."; |
77382aee PA |
196 | t->to_create_inferior = procfs_create_inferior; |
197 | t->to_kill = procfs_kill_inferior; | |
198 | t->to_mourn_inferior = procfs_mourn_inferior; | |
199 | t->to_attach = procfs_attach; | |
200 | t->to_detach = procfs_detach; | |
201 | t->to_wait = procfs_wait; | |
202 | t->to_resume = procfs_resume; | |
203 | t->to_fetch_registers = procfs_fetch_registers; | |
204 | t->to_store_registers = procfs_store_registers; | |
205 | t->to_xfer_partial = procfs_xfer_partial; | |
28439f5e | 206 | t->deprecated_xfer_memory = procfs_xfer_memory; |
2455069d | 207 | t->to_pass_signals = procfs_pass_signals; |
77382aee PA |
208 | t->to_files_info = procfs_files_info; |
209 | t->to_stop = procfs_stop; | |
28439f5e | 210 | |
77382aee PA |
211 | t->to_find_new_threads = procfs_find_new_threads; |
212 | t->to_thread_alive = procfs_thread_alive; | |
213 | t->to_pid_to_str = procfs_pid_to_str; | |
28439f5e | 214 | |
77382aee | 215 | t->to_has_thread_control = tc_schedlock; |
28439f5e PA |
216 | t->to_find_memory_regions = proc_find_memory_regions; |
217 | t->to_make_corefile_notes = procfs_make_note_section; | |
145b16a9 | 218 | t->to_info_proc = procfs_info_proc; |
c47ffbe3 VP |
219 | |
220 | #if defined(PR_MODEL_NATIVE) && (PR_MODEL_NATIVE == PR_MODEL_LP64) | |
28439f5e | 221 | t->to_auxv_parse = procfs_auxv_parse; |
c47ffbe3 VP |
222 | #endif |
223 | ||
77382aee | 224 | t->to_magic = OPS_MAGIC; |
28439f5e PA |
225 | |
226 | return t; | |
c3f6f71d | 227 | } |
c906108c | 228 | |
c3f6f71d JM |
229 | /* =================== END, TARGET_OPS "MODULE" =================== */ |
230 | ||
77382aee PA |
231 | /* World Unification: |
232 | ||
233 | Put any typedefs, defines etc. here that are required for the | |
234 | unification of code that handles different versions of /proc. */ | |
c3f6f71d JM |
235 | |
236 | #ifdef NEW_PROC_API /* Solaris 7 && 8 method for watchpoints */ | |
37de36c6 | 237 | #ifdef WA_READ |
19958708 | 238 | enum { READ_WATCHFLAG = WA_READ, |
c3f6f71d JM |
239 | WRITE_WATCHFLAG = WA_WRITE, |
240 | EXEC_WATCHFLAG = WA_EXEC, | |
241 | AFTER_WATCHFLAG = WA_TRAPAFTER | |
242 | }; | |
243 | #endif | |
244 | #else /* Irix method for watchpoints */ | |
19958708 | 245 | enum { READ_WATCHFLAG = MA_READ, |
c3f6f71d JM |
246 | WRITE_WATCHFLAG = MA_WRITE, |
247 | EXEC_WATCHFLAG = MA_EXEC, | |
248 | AFTER_WATCHFLAG = 0 /* trapafter not implemented */ | |
249 | }; | |
250 | #endif | |
251 | ||
37de36c6 KB |
252 | /* gdb_sigset_t */ |
253 | #ifdef HAVE_PR_SIGSET_T | |
254 | typedef pr_sigset_t gdb_sigset_t; | |
255 | #else | |
256 | typedef sigset_t gdb_sigset_t; | |
257 | #endif | |
258 | ||
259 | /* sigaction */ | |
260 | #ifdef HAVE_PR_SIGACTION64_T | |
261 | typedef pr_sigaction64_t gdb_sigaction_t; | |
262 | #else | |
263 | typedef struct sigaction gdb_sigaction_t; | |
264 | #endif | |
265 | ||
266 | /* siginfo */ | |
267 | #ifdef HAVE_PR_SIGINFO64_T | |
268 | typedef pr_siginfo64_t gdb_siginfo_t; | |
269 | #else | |
a5362b9a | 270 | typedef siginfo_t gdb_siginfo_t; |
37de36c6 KB |
271 | #endif |
272 | ||
3c95f01c JB |
273 | /* On mips-irix, praddset and prdelset are defined in such a way that |
274 | they return a value, which causes GCC to emit a -Wunused error | |
275 | because the returned value is not used. Prevent this warning | |
276 | by casting the return value to void. On sparc-solaris, this issue | |
277 | does not exist because the definition of these macros already include | |
278 | that cast to void. */ | |
279 | #define gdb_praddset(sp, flag) ((void) praddset (sp, flag)) | |
280 | #define gdb_prdelset(sp, flag) ((void) prdelset (sp, flag)) | |
281 | ||
37de36c6 KB |
282 | /* gdb_premptysysset */ |
283 | #ifdef premptysysset | |
284 | #define gdb_premptysysset premptysysset | |
285 | #else | |
286 | #define gdb_premptysysset premptyset | |
287 | #endif | |
288 | ||
289 | /* praddsysset */ | |
290 | #ifdef praddsysset | |
291 | #define gdb_praddsysset praddsysset | |
292 | #else | |
3c95f01c | 293 | #define gdb_praddsysset gdb_praddset |
37de36c6 KB |
294 | #endif |
295 | ||
296 | /* prdelsysset */ | |
297 | #ifdef prdelsysset | |
298 | #define gdb_prdelsysset prdelsysset | |
299 | #else | |
3c95f01c | 300 | #define gdb_prdelsysset gdb_prdelset |
37de36c6 KB |
301 | #endif |
302 | ||
303 | /* prissyssetmember */ | |
304 | #ifdef prissyssetmember | |
305 | #define gdb_pr_issyssetmember prissyssetmember | |
306 | #else | |
307 | #define gdb_pr_issyssetmember prismember | |
308 | #endif | |
309 | ||
310 | /* As a feature test, saying ``#if HAVE_PRSYSENT_T'' everywhere isn't | |
311 | as intuitively descriptive as it could be, so we'll define | |
312 | DYNAMIC_SYSCALLS to mean the same thing. Anyway, at the time of | |
313 | this writing, this feature is only found on AIX5 systems and | |
314 | basically means that the set of syscalls is not fixed. I.e, | |
315 | there's no nice table that one can #include to get all of the | |
316 | syscall numbers. Instead, they're stored in /proc/PID/sysent | |
317 | for each process. We are at least guaranteed that they won't | |
318 | change over the lifetime of the process. But each process could | |
77382aee | 319 | (in theory) have different syscall numbers. */ |
37de36c6 KB |
320 | #ifdef HAVE_PRSYSENT_T |
321 | #define DYNAMIC_SYSCALLS | |
322 | #endif | |
c3f6f71d JM |
323 | |
324 | ||
325 | ||
326 | /* =================== STRUCT PROCINFO "MODULE" =================== */ | |
327 | ||
328 | /* FIXME: this comment will soon be out of date W.R.T. threads. */ | |
329 | ||
330 | /* The procinfo struct is a wrapper to hold all the state information | |
331 | concerning a /proc process. There should be exactly one procinfo | |
332 | for each process, and since GDB currently can debug only one | |
333 | process at a time, that means there should be only one procinfo. | |
334 | All of the LWP's of a process can be accessed indirectly thru the | |
335 | single process procinfo. | |
336 | ||
337 | However, against the day when GDB may debug more than one process, | |
338 | this data structure is kept in a list (which for now will hold no | |
339 | more than one member), and many functions will have a pointer to a | |
340 | procinfo as an argument. | |
341 | ||
342 | There will be a separate procinfo structure for use by the (not yet | |
343 | implemented) "info proc" command, so that we can print useful | |
344 | information about any random process without interfering with the | |
0df8b418 | 345 | inferior's procinfo information. */ |
c3f6f71d JM |
346 | |
347 | #ifdef NEW_PROC_API | |
348 | /* format strings for /proc paths */ | |
349 | # ifndef CTL_PROC_NAME_FMT | |
350 | # define MAIN_PROC_NAME_FMT "/proc/%d" | |
351 | # define CTL_PROC_NAME_FMT "/proc/%d/ctl" | |
352 | # define AS_PROC_NAME_FMT "/proc/%d/as" | |
353 | # define MAP_PROC_NAME_FMT "/proc/%d/map" | |
354 | # define STATUS_PROC_NAME_FMT "/proc/%d/status" | |
355 | # define MAX_PROC_NAME_SIZE sizeof("/proc/99999/lwp/8096/lstatus") | |
356 | # endif | |
357 | /* the name of the proc status struct depends on the implementation */ | |
358 | typedef pstatus_t gdb_prstatus_t; | |
359 | typedef lwpstatus_t gdb_lwpstatus_t; | |
360 | #else /* ! NEW_PROC_API */ | |
361 | /* format strings for /proc paths */ | |
362 | # ifndef CTL_PROC_NAME_FMT | |
363 | # define MAIN_PROC_NAME_FMT "/proc/%05d" | |
364 | # define CTL_PROC_NAME_FMT "/proc/%05d" | |
365 | # define AS_PROC_NAME_FMT "/proc/%05d" | |
366 | # define MAP_PROC_NAME_FMT "/proc/%05d" | |
367 | # define STATUS_PROC_NAME_FMT "/proc/%05d" | |
368 | # define MAX_PROC_NAME_SIZE sizeof("/proc/ttttppppp") | |
369 | # endif | |
0df8b418 | 370 | /* The name of the proc status struct depends on the implementation. */ |
c5aa993b | 371 | typedef prstatus_t gdb_prstatus_t; |
c3f6f71d JM |
372 | typedef prstatus_t gdb_lwpstatus_t; |
373 | #endif /* NEW_PROC_API */ | |
c906108c | 374 | |
c3f6f71d JM |
375 | typedef struct procinfo { |
376 | struct procinfo *next; | |
377 | int pid; /* Process ID */ | |
378 | int tid; /* Thread/LWP id */ | |
c906108c | 379 | |
c3f6f71d JM |
380 | /* process state */ |
381 | int was_stopped; | |
382 | int ignore_next_sigstop; | |
c906108c | 383 | |
19958708 | 384 | /* The following four fd fields may be identical, or may contain |
c3f6f71d JM |
385 | several different fd's, depending on the version of /proc |
386 | (old ioctl or new read/write). */ | |
c906108c | 387 | |
c3f6f71d | 388 | int ctl_fd; /* File descriptor for /proc control file */ |
77382aee PA |
389 | |
390 | /* The next three file descriptors are actually only needed in the | |
391 | read/write, multiple-file-descriptor implemenation | |
392 | (NEW_PROC_API). However, to avoid a bunch of #ifdefs in the | |
393 | code, we will use them uniformly by (in the case of the ioctl | |
394 | single-file-descriptor implementation) filling them with copies | |
395 | of the control fd. */ | |
c3f6f71d JM |
396 | int status_fd; /* File descriptor for /proc status file */ |
397 | int as_fd; /* File descriptor for /proc as file */ | |
c906108c | 398 | |
c3f6f71d | 399 | char pathname[MAX_PROC_NAME_SIZE]; /* Pathname to /proc entry */ |
c906108c | 400 | |
c3f6f71d | 401 | fltset_t saved_fltset; /* Saved traced hardware fault set */ |
37de36c6 KB |
402 | gdb_sigset_t saved_sigset; /* Saved traced signal set */ |
403 | gdb_sigset_t saved_sighold; /* Saved held signal set */ | |
404 | sysset_t *saved_exitset; /* Saved traced system call exit set */ | |
405 | sysset_t *saved_entryset; /* Saved traced system call entry set */ | |
c906108c | 406 | |
c3f6f71d | 407 | gdb_prstatus_t prstatus; /* Current process status info */ |
c906108c | 408 | |
c3f6f71d JM |
409 | #ifndef NEW_PROC_API |
410 | gdb_fpregset_t fpregset; /* Current floating point registers */ | |
c5aa993b | 411 | #endif |
37de36c6 KB |
412 | |
413 | #ifdef DYNAMIC_SYSCALLS | |
414 | int num_syscalls; /* Total number of syscalls */ | |
415 | char **syscall_names; /* Syscall number to name map */ | |
416 | #endif | |
19958708 | 417 | |
c3f6f71d | 418 | struct procinfo *thread_list; |
c906108c | 419 | |
c3f6f71d JM |
420 | int status_valid : 1; |
421 | int gregs_valid : 1; | |
422 | int fpregs_valid : 1; | |
423 | int threads_valid: 1; | |
424 | } procinfo; | |
c906108c | 425 | |
c3f6f71d | 426 | static char errmsg[128]; /* shared error msg buffer */ |
c906108c | 427 | |
c3f6f71d | 428 | /* Function prototypes for procinfo module: */ |
c906108c | 429 | |
a14ed312 KB |
430 | static procinfo *find_procinfo_or_die (int pid, int tid); |
431 | static procinfo *find_procinfo (int pid, int tid); | |
432 | static procinfo *create_procinfo (int pid, int tid); | |
433 | static void destroy_procinfo (procinfo * p); | |
004527cb | 434 | static void do_destroy_procinfo_cleanup (void *); |
a14ed312 KB |
435 | static void dead_procinfo (procinfo * p, char *msg, int killp); |
436 | static int open_procinfo_files (procinfo * p, int which); | |
437 | static void close_procinfo_files (procinfo * p); | |
37de36c6 KB |
438 | static int sysset_t_size (procinfo *p); |
439 | static sysset_t *sysset_t_alloc (procinfo * pi); | |
440 | #ifdef DYNAMIC_SYSCALLS | |
441 | static void load_syscalls (procinfo *pi); | |
442 | static void free_syscalls (procinfo *pi); | |
443 | static int find_syscall (procinfo *pi, char *name); | |
444 | #endif /* DYNAMIC_SYSCALLS */ | |
c906108c | 445 | |
e9ef4f39 | 446 | static int iterate_over_mappings |
b8edc417 JK |
447 | (procinfo *pi, find_memory_region_ftype child_func, void *data, |
448 | int (*func) (struct prmap *map, find_memory_region_ftype child_func, | |
77382aee | 449 | void *data)); |
e9ef4f39 | 450 | |
c3f6f71d JM |
451 | /* The head of the procinfo list: */ |
452 | static procinfo * procinfo_list; | |
c906108c | 453 | |
77382aee PA |
454 | /* Search the procinfo list. Return a pointer to procinfo, or NULL if |
455 | not found. */ | |
c906108c | 456 | |
19958708 | 457 | static procinfo * |
fba45db2 | 458 | find_procinfo (int pid, int tid) |
c5aa993b | 459 | { |
c3f6f71d | 460 | procinfo *pi; |
c906108c | 461 | |
c3f6f71d JM |
462 | for (pi = procinfo_list; pi; pi = pi->next) |
463 | if (pi->pid == pid) | |
464 | break; | |
c906108c | 465 | |
c3f6f71d JM |
466 | if (pi) |
467 | if (tid) | |
468 | { | |
469 | /* Don't check threads_valid. If we're updating the | |
470 | thread_list, we want to find whatever threads are already | |
471 | here. This means that in general it is the caller's | |
472 | responsibility to check threads_valid and update before | |
473 | calling find_procinfo, if the caller wants to find a new | |
77382aee | 474 | thread. */ |
c3f6f71d JM |
475 | |
476 | for (pi = pi->thread_list; pi; pi = pi->next) | |
477 | if (pi->tid == tid) | |
478 | break; | |
479 | } | |
c906108c | 480 | |
c3f6f71d JM |
481 | return pi; |
482 | } | |
c906108c | 483 | |
77382aee | 484 | /* Calls find_procinfo, but errors on failure. */ |
c906108c | 485 | |
c3f6f71d | 486 | static procinfo * |
fba45db2 | 487 | find_procinfo_or_die (int pid, int tid) |
c3f6f71d JM |
488 | { |
489 | procinfo *pi = find_procinfo (pid, tid); | |
c906108c | 490 | |
c3f6f71d | 491 | if (pi == NULL) |
0fda6bd2 JM |
492 | { |
493 | if (tid) | |
3e43a32a MS |
494 | error (_("procfs: couldn't find pid %d " |
495 | "(kernel thread %d) in procinfo list."), | |
0fda6bd2 JM |
496 | pid, tid); |
497 | else | |
8a3fe4f8 | 498 | error (_("procfs: couldn't find pid %d in procinfo list."), pid); |
0fda6bd2 | 499 | } |
c3f6f71d JM |
500 | return pi; |
501 | } | |
c906108c | 502 | |
77382aee PA |
503 | /* Wrapper for `open'. The appropriate open call is attempted; if |
504 | unsuccessful, it will be retried as many times as needed for the | |
505 | EAGAIN and EINTR conditions. | |
19958708 | 506 | |
77382aee PA |
507 | For other conditions, retry the open a limited number of times. In |
508 | addition, a short sleep is imposed prior to retrying the open. The | |
509 | reason for this sleep is to give the kernel a chance to catch up | |
510 | and create the file in question in the event that GDB "wins" the | |
511 | race to open a file before the kernel has created it. */ | |
19958708 | 512 | |
4d1bcd09 KB |
513 | static int |
514 | open_with_retry (const char *pathname, int flags) | |
515 | { | |
516 | int retries_remaining, status; | |
517 | ||
518 | retries_remaining = 2; | |
519 | ||
520 | while (1) | |
521 | { | |
522 | status = open (pathname, flags); | |
523 | ||
524 | if (status >= 0 || retries_remaining == 0) | |
525 | break; | |
526 | else if (errno != EINTR && errno != EAGAIN) | |
527 | { | |
528 | retries_remaining--; | |
529 | sleep (1); | |
530 | } | |
531 | } | |
532 | ||
533 | return status; | |
534 | } | |
535 | ||
77382aee PA |
536 | /* Open the file descriptor for the process or LWP. If NEW_PROC_API |
537 | is defined, we only open the control file descriptor; the others | |
538 | are opened lazily as needed. Otherwise (if not NEW_PROC_API), | |
539 | there is only one real file descriptor, but we keep multiple copies | |
540 | of it so that the code that uses them does not have to be #ifdef'd. | |
541 | Returns the file descriptor, or zero for failure. */ | |
c906108c | 542 | |
c3f6f71d | 543 | enum { FD_CTL, FD_STATUS, FD_AS }; |
c906108c | 544 | |
c3f6f71d | 545 | static int |
fba45db2 | 546 | open_procinfo_files (procinfo *pi, int which) |
c3f6f71d | 547 | { |
0fda6bd2 | 548 | #ifdef NEW_PROC_API |
c3f6f71d | 549 | char tmp[MAX_PROC_NAME_SIZE]; |
0fda6bd2 | 550 | #endif |
c3f6f71d JM |
551 | int fd; |
552 | ||
77382aee PA |
553 | /* This function is getting ALMOST long enough to break up into |
554 | several. Here is some rationale: | |
555 | ||
556 | NEW_PROC_API (Solaris 2.6, Solaris 2.7, Unixware): | |
557 | There are several file descriptors that may need to be open | |
558 | for any given process or LWP. The ones we're intereted in are: | |
559 | - control (ctl) write-only change the state | |
560 | - status (status) read-only query the state | |
561 | - address space (as) read/write access memory | |
562 | - map (map) read-only virtual addr map | |
563 | Most of these are opened lazily as they are needed. | |
564 | The pathnames for the 'files' for an LWP look slightly | |
565 | different from those of a first-class process: | |
566 | Pathnames for a process (<proc-id>): | |
567 | /proc/<proc-id>/ctl | |
568 | /proc/<proc-id>/status | |
569 | /proc/<proc-id>/as | |
570 | /proc/<proc-id>/map | |
571 | Pathnames for an LWP (lwp-id): | |
572 | /proc/<proc-id>/lwp/<lwp-id>/lwpctl | |
573 | /proc/<proc-id>/lwp/<lwp-id>/lwpstatus | |
574 | An LWP has no map or address space file descriptor, since | |
575 | the memory map and address space are shared by all LWPs. | |
576 | ||
577 | Everyone else (Solaris 2.5, Irix, OSF) | |
578 | There is only one file descriptor for each process or LWP. | |
579 | For convenience, we copy the same file descriptor into all | |
580 | three fields of the procinfo struct (ctl_fd, status_fd, and | |
581 | as_fd, see NEW_PROC_API above) so that code that uses them | |
582 | doesn't need any #ifdef's. | |
583 | Pathname for all: | |
584 | /proc/<proc-id> | |
585 | ||
586 | Solaris 2.5 LWP's: | |
587 | Each LWP has an independent file descriptor, but these | |
588 | are not obtained via the 'open' system call like the rest: | |
589 | instead, they're obtained thru an ioctl call (PIOCOPENLWP) | |
590 | to the file descriptor of the parent process. | |
591 | ||
592 | OSF threads: | |
593 | These do not even have their own independent file descriptor. | |
594 | All operations are carried out on the file descriptor of the | |
595 | parent process. Therefore we just call open again for each | |
0df8b418 | 596 | thread, getting a new handle for the same 'file'. */ |
c3f6f71d JM |
597 | |
598 | #ifdef NEW_PROC_API | |
77382aee PA |
599 | /* In this case, there are several different file descriptors that |
600 | we might be asked to open. The control file descriptor will be | |
601 | opened early, but the others will be opened lazily as they are | |
602 | needed. */ | |
c3f6f71d JM |
603 | |
604 | strcpy (tmp, pi->pathname); | |
0df8b418 | 605 | switch (which) { /* Which file descriptor to open? */ |
c3f6f71d JM |
606 | case FD_CTL: |
607 | if (pi->tid) | |
608 | strcat (tmp, "/lwpctl"); | |
609 | else | |
610 | strcat (tmp, "/ctl"); | |
4d1bcd09 | 611 | fd = open_with_retry (tmp, O_WRONLY); |
e28cade7 | 612 | if (fd < 0) |
c3f6f71d JM |
613 | return 0; /* fail */ |
614 | pi->ctl_fd = fd; | |
615 | break; | |
616 | case FD_AS: | |
617 | if (pi->tid) | |
0df8b418 | 618 | return 0; /* There is no 'as' file descriptor for an lwp. */ |
c3f6f71d | 619 | strcat (tmp, "/as"); |
4d1bcd09 | 620 | fd = open_with_retry (tmp, O_RDWR); |
e28cade7 | 621 | if (fd < 0) |
c3f6f71d JM |
622 | return 0; /* fail */ |
623 | pi->as_fd = fd; | |
624 | break; | |
625 | case FD_STATUS: | |
626 | if (pi->tid) | |
627 | strcat (tmp, "/lwpstatus"); | |
628 | else | |
629 | strcat (tmp, "/status"); | |
4d1bcd09 | 630 | fd = open_with_retry (tmp, O_RDONLY); |
e28cade7 | 631 | if (fd < 0) |
c3f6f71d JM |
632 | return 0; /* fail */ |
633 | pi->status_fd = fd; | |
634 | break; | |
635 | default: | |
636 | return 0; /* unknown file descriptor */ | |
637 | } | |
638 | #else /* not NEW_PROC_API */ | |
77382aee PA |
639 | /* In this case, there is only one file descriptor for each procinfo |
640 | (ie. each process or LWP). In fact, only the file descriptor for | |
641 | the process can actually be opened by an 'open' system call. The | |
642 | ones for the LWPs have to be obtained thru an IOCTL call on the | |
643 | process's file descriptor. | |
644 | ||
645 | For convenience, we copy each procinfo's single file descriptor | |
646 | into all of the fields occupied by the several file descriptors | |
647 | of the NEW_PROC_API implementation. That way, the code that uses | |
648 | them can be written without ifdefs. */ | |
c3f6f71d JM |
649 | |
650 | ||
651 | #ifdef PIOCTSTATUS /* OSF */ | |
0df8b418 | 652 | /* Only one FD; just open it. */ |
e28cade7 | 653 | if ((fd = open_with_retry (pi->pathname, O_RDWR)) < 0) |
c3f6f71d | 654 | return 0; |
0df8b418 | 655 | #else /* Sol 2.5, Irix, other? */ |
c3f6f71d JM |
656 | if (pi->tid == 0) /* Master procinfo for the process */ |
657 | { | |
4d1bcd09 | 658 | fd = open_with_retry (pi->pathname, O_RDWR); |
e28cade7 | 659 | if (fd < 0) |
c3f6f71d JM |
660 | return 0; /* fail */ |
661 | } | |
662 | else /* LWP thread procinfo */ | |
663 | { | |
664 | #ifdef PIOCOPENLWP /* Sol 2.5, thread/LWP */ | |
665 | procinfo *process; | |
666 | int lwpid = pi->tid; | |
667 | ||
0df8b418 | 668 | /* Find the procinfo for the entire process. */ |
c3f6f71d JM |
669 | if ((process = find_procinfo (pi->pid, 0)) == NULL) |
670 | return 0; /* fail */ | |
671 | ||
0df8b418 | 672 | /* Now obtain the file descriptor for the LWP. */ |
e28cade7 | 673 | if ((fd = ioctl (process->ctl_fd, PIOCOPENLWP, &lwpid)) < 0) |
c3f6f71d | 674 | return 0; /* fail */ |
0df8b418 MS |
675 | #else /* Irix, other? */ |
676 | return 0; /* Don't know how to open threads. */ | |
c3f6f71d JM |
677 | #endif /* Sol 2.5 PIOCOPENLWP */ |
678 | } | |
679 | #endif /* OSF PIOCTSTATUS */ | |
680 | pi->ctl_fd = pi->as_fd = pi->status_fd = fd; | |
681 | #endif /* NEW_PROC_API */ | |
c906108c | 682 | |
c3f6f71d JM |
683 | return 1; /* success */ |
684 | } | |
c906108c | 685 | |
77382aee PA |
686 | /* Allocate a data structure and link it into the procinfo list. |
687 | First tries to find a pre-existing one (FIXME: why?). Returns the | |
688 | pointer to new procinfo struct. */ | |
c906108c | 689 | |
c3f6f71d | 690 | static procinfo * |
fba45db2 | 691 | create_procinfo (int pid, int tid) |
c3f6f71d | 692 | { |
0b62613e | 693 | procinfo *pi, *parent = NULL; |
c906108c | 694 | |
0d06e24b | 695 | if ((pi = find_procinfo (pid, tid))) |
0df8b418 | 696 | return pi; /* Already exists, nothing to do. */ |
c906108c | 697 | |
0df8b418 | 698 | /* Find parent before doing malloc, to save having to cleanup. */ |
c3f6f71d JM |
699 | if (tid != 0) |
700 | parent = find_procinfo_or_die (pid, 0); /* FIXME: should I | |
701 | create it if it | |
0df8b418 | 702 | doesn't exist yet? */ |
c906108c | 703 | |
c3f6f71d JM |
704 | pi = (procinfo *) xmalloc (sizeof (procinfo)); |
705 | memset (pi, 0, sizeof (procinfo)); | |
706 | pi->pid = pid; | |
707 | pi->tid = tid; | |
c906108c | 708 | |
37de36c6 KB |
709 | #ifdef DYNAMIC_SYSCALLS |
710 | load_syscalls (pi); | |
711 | #endif | |
712 | ||
1d5e0602 KB |
713 | pi->saved_entryset = sysset_t_alloc (pi); |
714 | pi->saved_exitset = sysset_t_alloc (pi); | |
715 | ||
c3f6f71d JM |
716 | /* Chain into list. */ |
717 | if (tid == 0) | |
718 | { | |
719 | sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid); | |
720 | pi->next = procinfo_list; | |
721 | procinfo_list = pi; | |
722 | } | |
723 | else | |
724 | { | |
725 | #ifdef NEW_PROC_API | |
726 | sprintf (pi->pathname, "/proc/%05d/lwp/%d", pid, tid); | |
727 | #else | |
728 | sprintf (pi->pathname, MAIN_PROC_NAME_FMT, pid); | |
729 | #endif | |
730 | pi->next = parent->thread_list; | |
731 | parent->thread_list = pi; | |
732 | } | |
733 | return pi; | |
734 | } | |
c906108c | 735 | |
77382aee | 736 | /* Close all file descriptors associated with the procinfo. */ |
c906108c | 737 | |
c3f6f71d | 738 | static void |
fba45db2 | 739 | close_procinfo_files (procinfo *pi) |
c3f6f71d JM |
740 | { |
741 | if (pi->ctl_fd > 0) | |
742 | close (pi->ctl_fd); | |
743 | #ifdef NEW_PROC_API | |
744 | if (pi->as_fd > 0) | |
745 | close (pi->as_fd); | |
746 | if (pi->status_fd > 0) | |
747 | close (pi->status_fd); | |
748 | #endif | |
749 | pi->ctl_fd = pi->as_fd = pi->status_fd = 0; | |
750 | } | |
c906108c | 751 | |
77382aee | 752 | /* Destructor function. Close, unlink and deallocate the object. */ |
c906108c | 753 | |
c3f6f71d | 754 | static void |
fba45db2 | 755 | destroy_one_procinfo (procinfo **list, procinfo *pi) |
c3f6f71d JM |
756 | { |
757 | procinfo *ptr; | |
758 | ||
77382aee | 759 | /* Step one: unlink the procinfo from its list. */ |
c3f6f71d JM |
760 | if (pi == *list) |
761 | *list = pi->next; | |
19958708 | 762 | else |
c3f6f71d JM |
763 | for (ptr = *list; ptr; ptr = ptr->next) |
764 | if (ptr->next == pi) | |
765 | { | |
766 | ptr->next = pi->next; | |
767 | break; | |
768 | } | |
7a292a7a | 769 | |
77382aee | 770 | /* Step two: close any open file descriptors. */ |
c3f6f71d | 771 | close_procinfo_files (pi); |
7a292a7a | 772 | |
77382aee | 773 | /* Step three: free the memory. */ |
37de36c6 KB |
774 | #ifdef DYNAMIC_SYSCALLS |
775 | free_syscalls (pi); | |
776 | #endif | |
1d5e0602 KB |
777 | xfree (pi->saved_entryset); |
778 | xfree (pi->saved_exitset); | |
b8c9b27d | 779 | xfree (pi); |
c3f6f71d | 780 | } |
c906108c | 781 | |
c3f6f71d | 782 | static void |
fba45db2 | 783 | destroy_procinfo (procinfo *pi) |
c3f6f71d JM |
784 | { |
785 | procinfo *tmp; | |
c906108c | 786 | |
0df8b418 | 787 | if (pi->tid != 0) /* Destroy a thread procinfo. */ |
c3f6f71d | 788 | { |
0df8b418 | 789 | tmp = find_procinfo (pi->pid, 0); /* Find the parent process. */ |
c3f6f71d JM |
790 | destroy_one_procinfo (&tmp->thread_list, pi); |
791 | } | |
0df8b418 | 792 | else /* Destroy a process procinfo and all its threads. */ |
c3f6f71d JM |
793 | { |
794 | /* First destroy the children, if any; */ | |
795 | while (pi->thread_list != NULL) | |
796 | destroy_one_procinfo (&pi->thread_list, pi->thread_list); | |
797 | /* Then destroy the parent. Genocide!!! */ | |
798 | destroy_one_procinfo (&procinfo_list, pi); | |
799 | } | |
800 | } | |
c906108c | 801 | |
004527cb AC |
802 | static void |
803 | do_destroy_procinfo_cleanup (void *pi) | |
804 | { | |
805 | destroy_procinfo (pi); | |
806 | } | |
807 | ||
c3f6f71d | 808 | enum { NOKILL, KILL }; |
c906108c | 809 | |
77382aee PA |
810 | /* To be called on a non_recoverable error for a procinfo. Prints |
811 | error messages, optionally sends a SIGKILL to the process, then | |
812 | destroys the data structure. */ | |
c906108c | 813 | |
c3f6f71d | 814 | static void |
fba45db2 | 815 | dead_procinfo (procinfo *pi, char *msg, int kill_p) |
c3f6f71d JM |
816 | { |
817 | char procfile[80]; | |
c906108c | 818 | |
c3f6f71d JM |
819 | if (pi->pathname) |
820 | { | |
821 | print_sys_errmsg (pi->pathname, errno); | |
822 | } | |
823 | else | |
824 | { | |
825 | sprintf (procfile, "process %d", pi->pid); | |
826 | print_sys_errmsg (procfile, errno); | |
827 | } | |
828 | if (kill_p == KILL) | |
829 | kill (pi->pid, SIGKILL); | |
c906108c | 830 | |
c3f6f71d | 831 | destroy_procinfo (pi); |
0b62613e | 832 | error ("%s", msg); |
c3f6f71d | 833 | } |
c906108c | 834 | |
77382aee PA |
835 | /* Returns the (complete) size of a sysset_t struct. Normally, this |
836 | is just sizeof (sysset_t), but in the case of Monterey/64, the | |
837 | actual size of sysset_t isn't known until runtime. */ | |
37de36c6 KB |
838 | |
839 | static int | |
840 | sysset_t_size (procinfo * pi) | |
841 | { | |
842 | #ifndef DYNAMIC_SYSCALLS | |
843 | return sizeof (sysset_t); | |
844 | #else | |
845 | return sizeof (sysset_t) - sizeof (uint64_t) | |
846 | + sizeof (uint64_t) * ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1)) | |
847 | / (8 * sizeof (uint64_t))); | |
848 | #endif | |
849 | } | |
850 | ||
77382aee | 851 | /* Allocate and (partially) initialize a sysset_t struct. */ |
37de36c6 KB |
852 | |
853 | static sysset_t * | |
854 | sysset_t_alloc (procinfo * pi) | |
855 | { | |
856 | sysset_t *ret; | |
857 | int size = sysset_t_size (pi); | |
f4a14ae6 | 858 | |
37de36c6 KB |
859 | ret = xmalloc (size); |
860 | #ifdef DYNAMIC_SYSCALLS | |
77382aee PA |
861 | ret->pr_size = ((pi->num_syscalls + (8 * sizeof (uint64_t) - 1)) |
862 | / (8 * sizeof (uint64_t))); | |
37de36c6 KB |
863 | #endif |
864 | return ret; | |
865 | } | |
866 | ||
867 | #ifdef DYNAMIC_SYSCALLS | |
868 | ||
77382aee | 869 | /* Extract syscall numbers and names from /proc/<pid>/sysent. Initialize |
37de36c6 KB |
870 | pi->num_syscalls with the number of syscalls and pi->syscall_names |
871 | with the names. (Certain numbers may be skipped in which case the | |
0df8b418 | 872 | names for these numbers will be left as NULL.) */ |
37de36c6 KB |
873 | |
874 | #define MAX_SYSCALL_NAME_LENGTH 256 | |
875 | #define MAX_SYSCALLS 65536 | |
876 | ||
877 | static void | |
878 | load_syscalls (procinfo *pi) | |
879 | { | |
880 | char pathname[MAX_PROC_NAME_SIZE]; | |
881 | int sysent_fd; | |
882 | prsysent_t header; | |
883 | prsyscall_t *syscalls; | |
884 | int i, size, maxcall; | |
e28cade7 | 885 | struct cleanup *cleanups; |
37de36c6 KB |
886 | |
887 | pi->num_syscalls = 0; | |
888 | pi->syscall_names = 0; | |
889 | ||
77382aee | 890 | /* Open the file descriptor for the sysent file. */ |
37de36c6 | 891 | sprintf (pathname, "/proc/%d/sysent", pi->pid); |
4d1bcd09 | 892 | sysent_fd = open_with_retry (pathname, O_RDONLY); |
37de36c6 KB |
893 | if (sysent_fd < 0) |
894 | { | |
8a3fe4f8 | 895 | error (_("load_syscalls: Can't open /proc/%d/sysent"), pi->pid); |
37de36c6 | 896 | } |
e28cade7 | 897 | cleanups = make_cleanup_close (sysent_fd); |
37de36c6 KB |
898 | |
899 | size = sizeof header - sizeof (prsyscall_t); | |
900 | if (read (sysent_fd, &header, size) != size) | |
901 | { | |
8a3fe4f8 | 902 | error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid); |
37de36c6 KB |
903 | } |
904 | ||
905 | if (header.pr_nsyscalls == 0) | |
906 | { | |
3e43a32a MS |
907 | error (_("load_syscalls: /proc/%d/sysent contains no syscalls!"), |
908 | pi->pid); | |
37de36c6 KB |
909 | } |
910 | ||
911 | size = header.pr_nsyscalls * sizeof (prsyscall_t); | |
912 | syscalls = xmalloc (size); | |
e28cade7 | 913 | make_cleanup (free_current_contents, &syscalls); |
37de36c6 KB |
914 | |
915 | if (read (sysent_fd, syscalls, size) != size) | |
e28cade7 | 916 | error (_("load_syscalls: Error reading /proc/%d/sysent"), pi->pid); |
37de36c6 KB |
917 | |
918 | /* Find maximum syscall number. This may not be the same as | |
919 | pr_nsyscalls since that value refers to the number of entries | |
920 | in the table. (Also, the docs indicate that some system | |
0df8b418 | 921 | call numbers may be skipped.) */ |
37de36c6 KB |
922 | |
923 | maxcall = syscalls[0].pr_number; | |
924 | ||
925 | for (i = 1; i < header.pr_nsyscalls; i++) | |
926 | if (syscalls[i].pr_number > maxcall | |
77382aee | 927 | && syscalls[i].pr_nameoff > 0 |
37de36c6 KB |
928 | && syscalls[i].pr_number < MAX_SYSCALLS) |
929 | maxcall = syscalls[i].pr_number; | |
930 | ||
931 | pi->num_syscalls = maxcall+1; | |
932 | pi->syscall_names = xmalloc (pi->num_syscalls * sizeof (char *)); | |
933 | ||
934 | for (i = 0; i < pi->num_syscalls; i++) | |
935 | pi->syscall_names[i] = NULL; | |
936 | ||
77382aee | 937 | /* Read the syscall names in. */ |
37de36c6 KB |
938 | for (i = 0; i < header.pr_nsyscalls; i++) |
939 | { | |
940 | char namebuf[MAX_SYSCALL_NAME_LENGTH]; | |
941 | int nread; | |
942 | int callnum; | |
943 | ||
944 | if (syscalls[i].pr_number >= MAX_SYSCALLS | |
77382aee | 945 | || syscalls[i].pr_number < 0 |
37de36c6 KB |
946 | || syscalls[i].pr_nameoff <= 0 |
947 | || (lseek (sysent_fd, (off_t) syscalls[i].pr_nameoff, SEEK_SET) | |
77382aee | 948 | != (off_t) syscalls[i].pr_nameoff)) |
37de36c6 KB |
949 | continue; |
950 | ||
951 | nread = read (sysent_fd, namebuf, sizeof namebuf); | |
952 | if (nread <= 0) | |
953 | continue; | |
954 | ||
955 | callnum = syscalls[i].pr_number; | |
956 | ||
957 | if (pi->syscall_names[callnum] != NULL) | |
958 | { | |
0df8b418 | 959 | /* FIXME: Generate warning. */ |
37de36c6 KB |
960 | continue; |
961 | } | |
962 | ||
963 | namebuf[nread-1] = '\0'; | |
964 | size = strlen (namebuf) + 1; | |
965 | pi->syscall_names[callnum] = xmalloc (size); | |
966 | strncpy (pi->syscall_names[callnum], namebuf, size-1); | |
967 | pi->syscall_names[callnum][size-1] = '\0'; | |
968 | } | |
19958708 | 969 | |
e28cade7 | 970 | do_cleanups (cleanups); |
37de36c6 KB |
971 | } |
972 | ||
77382aee | 973 | /* Free the space allocated for the syscall names from the procinfo |
37de36c6 KB |
974 | structure. */ |
975 | ||
976 | static void | |
977 | free_syscalls (procinfo *pi) | |
978 | { | |
979 | if (pi->syscall_names) | |
980 | { | |
981 | int i; | |
982 | ||
983 | for (i = 0; i < pi->num_syscalls; i++) | |
984 | if (pi->syscall_names[i] != NULL) | |
985 | xfree (pi->syscall_names[i]); | |
986 | ||
987 | xfree (pi->syscall_names); | |
988 | pi->syscall_names = 0; | |
989 | } | |
990 | } | |
991 | ||
77382aee | 992 | /* Given a name, look up (and return) the corresponding syscall number. |
37de36c6 | 993 | If no match is found, return -1. */ |
19958708 | 994 | |
37de36c6 KB |
995 | static int |
996 | find_syscall (procinfo *pi, char *name) | |
997 | { | |
998 | int i; | |
f4a14ae6 | 999 | |
37de36c6 KB |
1000 | for (i = 0; i < pi->num_syscalls; i++) |
1001 | { | |
1002 | if (pi->syscall_names[i] && strcmp (name, pi->syscall_names[i]) == 0) | |
1003 | return i; | |
1004 | } | |
1005 | return -1; | |
1006 | } | |
1007 | #endif | |
1008 | ||
c3f6f71d | 1009 | /* =================== END, STRUCT PROCINFO "MODULE" =================== */ |
c906108c | 1010 | |
c3f6f71d | 1011 | /* =================== /proc "MODULE" =================== */ |
c906108c | 1012 | |
77382aee PA |
1013 | /* This "module" is the interface layer between the /proc system API |
1014 | and the gdb target vector functions. This layer consists of access | |
1015 | functions that encapsulate each of the basic operations that we | |
1016 | need to use from the /proc API. | |
1017 | ||
1018 | The main motivation for this layer is to hide the fact that there | |
1019 | are two very different implementations of the /proc API. Rather | |
1020 | than have a bunch of #ifdefs all thru the gdb target vector | |
1021 | functions, we do our best to hide them all in here. */ | |
c906108c | 1022 | |
d3581e61 JB |
1023 | static long proc_flags (procinfo * pi); |
1024 | static int proc_why (procinfo * pi); | |
1025 | static int proc_what (procinfo * pi); | |
1026 | static int proc_set_current_signal (procinfo * pi, int signo); | |
1027 | static int proc_get_current_thread (procinfo * pi); | |
1028 | static int proc_iterate_over_threads | |
1029 | (procinfo * pi, | |
1030 | int (*func) (procinfo *, procinfo *, void *), | |
1031 | void *ptr); | |
1032 | ||
1033 | static void | |
fba45db2 | 1034 | proc_warn (procinfo *pi, char *func, int line) |
c3f6f71d JM |
1035 | { |
1036 | sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname); | |
1037 | print_sys_errmsg (errmsg, errno); | |
1038 | } | |
c906108c | 1039 | |
d3581e61 | 1040 | static void |
fba45db2 | 1041 | proc_error (procinfo *pi, char *func, int line) |
c3f6f71d JM |
1042 | { |
1043 | sprintf (errmsg, "procfs: %s line %d, %s", func, line, pi->pathname); | |
1044 | perror_with_name (errmsg); | |
1045 | } | |
c906108c | 1046 | |
77382aee PA |
1047 | /* Updates the status struct in the procinfo. There is a 'valid' |
1048 | flag, to let other functions know when this function needs to be | |
1049 | called (so the status is only read when it is needed). The status | |
1050 | file descriptor is also only opened when it is needed. Returns | |
1051 | non-zero for success, zero for failure. */ | |
c906108c | 1052 | |
d3581e61 | 1053 | static int |
fba45db2 | 1054 | proc_get_status (procinfo *pi) |
c3f6f71d | 1055 | { |
0df8b418 | 1056 | /* Status file descriptor is opened "lazily". */ |
c3f6f71d JM |
1057 | if (pi->status_fd == 0 && |
1058 | open_procinfo_files (pi, FD_STATUS) == 0) | |
1059 | { | |
1060 | pi->status_valid = 0; | |
1061 | return 0; | |
1062 | } | |
c906108c | 1063 | |
c3f6f71d JM |
1064 | #ifdef NEW_PROC_API |
1065 | if (lseek (pi->status_fd, 0, SEEK_SET) < 0) | |
1066 | pi->status_valid = 0; /* fail */ | |
1067 | else | |
1068 | { | |
19958708 | 1069 | /* Sigh... I have to read a different data structure, |
0df8b418 | 1070 | depending on whether this is a main process or an LWP. */ |
c3f6f71d | 1071 | if (pi->tid) |
19958708 RM |
1072 | pi->status_valid = (read (pi->status_fd, |
1073 | (char *) &pi->prstatus.pr_lwp, | |
c3f6f71d JM |
1074 | sizeof (lwpstatus_t)) |
1075 | == sizeof (lwpstatus_t)); | |
1076 | else | |
1077 | { | |
19958708 | 1078 | pi->status_valid = (read (pi->status_fd, |
c3f6f71d JM |
1079 | (char *) &pi->prstatus, |
1080 | sizeof (gdb_prstatus_t)) | |
1081 | == sizeof (gdb_prstatus_t)); | |
1082 | #if 0 /*def UNIXWARE*/ | |
1083 | if (pi->status_valid && | |
1084 | (pi->prstatus.pr_lwp.pr_flags & PR_ISTOP) && | |
1085 | pi->prstatus.pr_lwp.pr_why == PR_REQUESTED) | |
0df8b418 | 1086 | /* Unixware peculiarity -- read the damn thing again! */ |
19958708 | 1087 | pi->status_valid = (read (pi->status_fd, |
c3f6f71d JM |
1088 | (char *) &pi->prstatus, |
1089 | sizeof (gdb_prstatus_t)) | |
1090 | == sizeof (gdb_prstatus_t)); | |
1091 | #endif /* UNIXWARE */ | |
1092 | } | |
1093 | } | |
1094 | #else /* ioctl method */ | |
1095 | #ifdef PIOCTSTATUS /* osf */ | |
1096 | if (pi->tid == 0) /* main process */ | |
1097 | { | |
0df8b418 | 1098 | /* Just read the danged status. Now isn't that simple? */ |
19958708 | 1099 | pi->status_valid = |
c3f6f71d JM |
1100 | (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0); |
1101 | } | |
1102 | else | |
1103 | { | |
1104 | int win; | |
1105 | struct { | |
1106 | long pr_count; | |
1107 | tid_t pr_error_thread; | |
1108 | struct prstatus status; | |
1109 | } thread_status; | |
1110 | ||
1111 | thread_status.pr_count = 1; | |
1112 | thread_status.status.pr_tid = pi->tid; | |
1113 | win = (ioctl (pi->status_fd, PIOCTSTATUS, &thread_status) >= 0); | |
1114 | if (win) | |
1115 | { | |
19958708 | 1116 | memcpy (&pi->prstatus, &thread_status.status, |
c3f6f71d JM |
1117 | sizeof (pi->prstatus)); |
1118 | pi->status_valid = 1; | |
1119 | } | |
1120 | } | |
1121 | #else | |
77382aee | 1122 | /* Just read the danged status. Now isn't that simple? */ |
c3f6f71d JM |
1123 | pi->status_valid = (ioctl (pi->status_fd, PIOCSTATUS, &pi->prstatus) >= 0); |
1124 | #endif | |
1125 | #endif | |
c906108c | 1126 | |
c3f6f71d JM |
1127 | if (pi->status_valid) |
1128 | { | |
19958708 | 1129 | PROC_PRETTYFPRINT_STATUS (proc_flags (pi), |
c3f6f71d | 1130 | proc_why (pi), |
19958708 | 1131 | proc_what (pi), |
c3f6f71d JM |
1132 | proc_get_current_thread (pi)); |
1133 | } | |
c906108c | 1134 | |
77382aee | 1135 | /* The status struct includes general regs, so mark them valid too. */ |
c3f6f71d JM |
1136 | pi->gregs_valid = pi->status_valid; |
1137 | #ifdef NEW_PROC_API | |
77382aee PA |
1138 | /* In the read/write multiple-fd model, the status struct includes |
1139 | the fp regs too, so mark them valid too. */ | |
c3f6f71d JM |
1140 | pi->fpregs_valid = pi->status_valid; |
1141 | #endif | |
77382aee | 1142 | return pi->status_valid; /* True if success, false if failure. */ |
c3f6f71d | 1143 | } |
c906108c | 1144 | |
77382aee | 1145 | /* Returns the process flags (pr_flags field). */ |
c3f6f71d | 1146 | |
d3581e61 | 1147 | static long |
fba45db2 | 1148 | proc_flags (procinfo *pi) |
c3f6f71d JM |
1149 | { |
1150 | if (!pi->status_valid) | |
1151 | if (!proc_get_status (pi)) | |
0df8b418 | 1152 | return 0; /* FIXME: not a good failure value (but what is?) */ |
c906108c | 1153 | |
c3f6f71d | 1154 | #ifdef NEW_PROC_API |
0d06e24b JM |
1155 | # ifdef UNIXWARE |
1156 | /* UnixWare 7.1 puts process status flags, e.g. PR_ASYNC, in | |
1157 | pstatus_t and LWP status flags, e.g. PR_STOPPED, in lwpstatus_t. | |
77382aee | 1158 | The two sets of flags don't overlap. */ |
0d06e24b JM |
1159 | return pi->prstatus.pr_flags | pi->prstatus.pr_lwp.pr_flags; |
1160 | # else | |
c3f6f71d | 1161 | return pi->prstatus.pr_lwp.pr_flags; |
0d06e24b | 1162 | # endif |
c3f6f71d JM |
1163 | #else |
1164 | return pi->prstatus.pr_flags; | |
1165 | #endif | |
1166 | } | |
c906108c | 1167 | |
77382aee | 1168 | /* Returns the pr_why field (why the process stopped). */ |
c906108c | 1169 | |
d3581e61 | 1170 | static int |
fba45db2 | 1171 | proc_why (procinfo *pi) |
c3f6f71d JM |
1172 | { |
1173 | if (!pi->status_valid) | |
1174 | if (!proc_get_status (pi)) | |
0df8b418 | 1175 | return 0; /* FIXME: not a good failure value (but what is?) */ |
c906108c | 1176 | |
c3f6f71d JM |
1177 | #ifdef NEW_PROC_API |
1178 | return pi->prstatus.pr_lwp.pr_why; | |
1179 | #else | |
1180 | return pi->prstatus.pr_why; | |
1181 | #endif | |
1182 | } | |
c906108c | 1183 | |
77382aee | 1184 | /* Returns the pr_what field (details of why the process stopped). */ |
c906108c | 1185 | |
d3581e61 | 1186 | static int |
fba45db2 | 1187 | proc_what (procinfo *pi) |
c3f6f71d JM |
1188 | { |
1189 | if (!pi->status_valid) | |
1190 | if (!proc_get_status (pi)) | |
0df8b418 | 1191 | return 0; /* FIXME: not a good failure value (but what is?) */ |
c906108c | 1192 | |
c3f6f71d JM |
1193 | #ifdef NEW_PROC_API |
1194 | return pi->prstatus.pr_lwp.pr_what; | |
1195 | #else | |
1196 | return pi->prstatus.pr_what; | |
c906108c | 1197 | #endif |
c3f6f71d | 1198 | } |
c906108c | 1199 | |
77382aee PA |
1200 | /* This function is only called when PI is stopped by a watchpoint. |
1201 | Assuming the OS supports it, write to *ADDR the data address which | |
1202 | triggered it and return 1. Return 0 if it is not possible to know | |
1203 | the address. */ | |
bf701c2c PM |
1204 | |
1205 | static int | |
1206 | proc_watchpoint_address (procinfo *pi, CORE_ADDR *addr) | |
1207 | { | |
1208 | if (!pi->status_valid) | |
1209 | if (!proc_get_status (pi)) | |
1210 | return 0; | |
1211 | ||
1212 | #ifdef NEW_PROC_API | |
1213 | *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch, | |
1214 | builtin_type (target_gdbarch)->builtin_data_ptr, | |
1215 | (gdb_byte *) &pi->prstatus.pr_lwp.pr_info.si_addr); | |
1216 | #else | |
1217 | *addr = (CORE_ADDR) gdbarch_pointer_to_address (target_gdbarch, | |
1218 | builtin_type (target_gdbarch)->builtin_data_ptr, | |
1219 | (gdb_byte *) &pi->prstatus.pr_info.si_addr); | |
1220 | #endif | |
1221 | return 1; | |
1222 | } | |
1223 | ||
c3f6f71d | 1224 | #ifndef PIOCSSPCACT /* The following is not supported on OSF. */ |
77382aee PA |
1225 | |
1226 | /* Returns the pr_nsysarg field (number of args to the current | |
1227 | syscall). */ | |
c3f6f71d | 1228 | |
d3581e61 | 1229 | static int |
fba45db2 | 1230 | proc_nsysarg (procinfo *pi) |
c3f6f71d JM |
1231 | { |
1232 | if (!pi->status_valid) | |
1233 | if (!proc_get_status (pi)) | |
1234 | return 0; | |
19958708 | 1235 | |
c3f6f71d JM |
1236 | #ifdef NEW_PROC_API |
1237 | return pi->prstatus.pr_lwp.pr_nsysarg; | |
1238 | #else | |
1239 | return pi->prstatus.pr_nsysarg; | |
c906108c | 1240 | #endif |
c3f6f71d | 1241 | } |
c906108c | 1242 | |
77382aee PA |
1243 | /* Returns the pr_sysarg field (pointer to the arguments of current |
1244 | syscall). */ | |
c906108c | 1245 | |
d3581e61 | 1246 | static long * |
fba45db2 | 1247 | proc_sysargs (procinfo *pi) |
c3f6f71d JM |
1248 | { |
1249 | if (!pi->status_valid) | |
1250 | if (!proc_get_status (pi)) | |
1251 | return NULL; | |
19958708 | 1252 | |
c3f6f71d JM |
1253 | #ifdef NEW_PROC_API |
1254 | return (long *) &pi->prstatus.pr_lwp.pr_sysarg; | |
1255 | #else | |
1256 | return (long *) &pi->prstatus.pr_sysarg; | |
1257 | #endif | |
1258 | } | |
c3f6f71d | 1259 | #endif /* PIOCSSPCACT */ |
c906108c | 1260 | |
d3581e61 | 1261 | #ifdef PROCFS_DONT_PIOCSSIG_CURSIG |
77382aee | 1262 | /* Returns the pr_cursig field (current signal). */ |
c906108c | 1263 | |
d3581e61 | 1264 | static long |
c3f6f71d JM |
1265 | proc_cursig (struct procinfo *pi) |
1266 | { | |
1267 | if (!pi->status_valid) | |
1268 | if (!proc_get_status (pi)) | |
0df8b418 | 1269 | return 0; /* FIXME: not a good failure value (but what is?) */ |
c906108c | 1270 | |
c3f6f71d JM |
1271 | #ifdef NEW_PROC_API |
1272 | return pi->prstatus.pr_lwp.pr_cursig; | |
1273 | #else | |
1274 | return pi->prstatus.pr_cursig; | |
1275 | #endif | |
1276 | } | |
d3581e61 | 1277 | #endif /* PROCFS_DONT_PIOCSSIG_CURSIG */ |
c906108c | 1278 | |
77382aee PA |
1279 | /* === I appologize for the messiness of this function. |
1280 | === This is an area where the different versions of | |
1281 | === /proc are more inconsistent than usual. | |
1282 | ||
1283 | Set or reset any of the following process flags: | |
1284 | PR_FORK -- forked child will inherit trace flags | |
1285 | PR_RLC -- traced process runs when last /proc file closed. | |
1286 | PR_KLC -- traced process is killed when last /proc file closed. | |
1287 | PR_ASYNC -- LWP's get to run/stop independently. | |
1288 | ||
1289 | There are three methods for doing this function: | |
1290 | 1) Newest: read/write [PCSET/PCRESET/PCUNSET] | |
1291 | [Sol6, Sol7, UW] | |
1292 | 2) Middle: PIOCSET/PIOCRESET | |
1293 | [Irix, Sol5] | |
1294 | 3) Oldest: PIOCSFORK/PIOCRFORK/PIOCSRLC/PIOCRRLC | |
1295 | [OSF, Sol5] | |
1296 | ||
1297 | Note: Irix does not define PR_ASYNC. | |
1298 | Note: OSF does not define PR_KLC. | |
1299 | Note: OSF is the only one that can ONLY use the oldest method. | |
1300 | ||
1301 | Arguments: | |
1302 | pi -- the procinfo | |
1303 | flag -- one of PR_FORK, PR_RLC, or PR_ASYNC | |
1304 | mode -- 1 for set, 0 for reset. | |
1305 | ||
1306 | Returns non-zero for success, zero for failure. */ | |
c906108c | 1307 | |
c3f6f71d | 1308 | enum { FLAG_RESET, FLAG_SET }; |
c906108c | 1309 | |
c3f6f71d | 1310 | static int |
fba45db2 | 1311 | proc_modify_flag (procinfo *pi, long flag, long mode) |
c3f6f71d JM |
1312 | { |
1313 | long win = 0; /* default to fail */ | |
1314 | ||
77382aee PA |
1315 | /* These operations affect the process as a whole, and applying them |
1316 | to an individual LWP has the same meaning as applying them to the | |
1317 | main process. Therefore, if we're ever called with a pointer to | |
1318 | an LWP's procinfo, let's substitute the process's procinfo and | |
1319 | avoid opening the LWP's file descriptor unnecessarily. */ | |
c3f6f71d JM |
1320 | |
1321 | if (pi->pid != 0) | |
1322 | pi = find_procinfo_or_die (pi->pid, 0); | |
1323 | ||
0df8b418 | 1324 | #ifdef NEW_PROC_API /* Newest method: UnixWare and newer Solarii. */ |
19958708 | 1325 | /* First normalize the PCUNSET/PCRESET command opcode |
c3f6f71d JM |
1326 | (which for no obvious reason has a different definition |
1327 | from one operating system to the next...) */ | |
1328 | #ifdef PCUNSET | |
1329 | #define GDBRESET PCUNSET | |
37de36c6 | 1330 | #else |
c3f6f71d JM |
1331 | #ifdef PCRESET |
1332 | #define GDBRESET PCRESET | |
37de36c6 | 1333 | #endif |
c906108c | 1334 | #endif |
c3f6f71d | 1335 | { |
37de36c6 | 1336 | procfs_ctl_t arg[2]; |
c906108c | 1337 | |
0df8b418 | 1338 | if (mode == FLAG_SET) /* Set the flag (RLC, FORK, or ASYNC). */ |
c3f6f71d | 1339 | arg[0] = PCSET; |
0df8b418 | 1340 | else /* Reset the flag. */ |
c3f6f71d | 1341 | arg[0] = GDBRESET; |
c5aa993b | 1342 | |
c3f6f71d JM |
1343 | arg[1] = flag; |
1344 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); | |
1345 | } | |
1346 | #else | |
1347 | #ifdef PIOCSET /* Irix/Sol5 method */ | |
0df8b418 | 1348 | if (mode == FLAG_SET) /* Set the flag (hopefully RLC, FORK, or ASYNC). */ |
c3f6f71d JM |
1349 | { |
1350 | win = (ioctl (pi->ctl_fd, PIOCSET, &flag) >= 0); | |
1351 | } | |
0df8b418 | 1352 | else /* Reset the flag. */ |
c3f6f71d JM |
1353 | { |
1354 | win = (ioctl (pi->ctl_fd, PIOCRESET, &flag) >= 0); | |
1355 | } | |
c906108c | 1356 | |
c3f6f71d JM |
1357 | #else |
1358 | #ifdef PIOCSRLC /* Oldest method: OSF */ | |
1359 | switch (flag) { | |
1360 | case PR_RLC: | |
1361 | if (mode == FLAG_SET) /* Set run-on-last-close */ | |
1362 | { | |
1363 | win = (ioctl (pi->ctl_fd, PIOCSRLC, NULL) >= 0); | |
1364 | } | |
1365 | else /* Clear run-on-last-close */ | |
1366 | { | |
1367 | win = (ioctl (pi->ctl_fd, PIOCRRLC, NULL) >= 0); | |
1368 | } | |
1369 | break; | |
1370 | case PR_FORK: | |
1371 | if (mode == FLAG_SET) /* Set inherit-on-fork */ | |
1372 | { | |
1373 | win = (ioctl (pi->ctl_fd, PIOCSFORK, NULL) >= 0); | |
1374 | } | |
1375 | else /* Clear inherit-on-fork */ | |
1376 | { | |
1377 | win = (ioctl (pi->ctl_fd, PIOCRFORK, NULL) >= 0); | |
1378 | } | |
1379 | break; | |
1380 | default: | |
0df8b418 | 1381 | win = 0; /* Fail -- unknown flag (can't do PR_ASYNC). */ |
c3f6f71d JM |
1382 | break; |
1383 | } | |
1384 | #endif | |
1385 | #endif | |
1386 | #endif | |
1387 | #undef GDBRESET | |
77382aee PA |
1388 | /* The above operation renders the procinfo's cached pstatus |
1389 | obsolete. */ | |
c3f6f71d | 1390 | pi->status_valid = 0; |
c906108c | 1391 | |
c3f6f71d | 1392 | if (!win) |
8a3fe4f8 | 1393 | warning (_("procfs: modify_flag failed to turn %s %s"), |
c3f6f71d JM |
1394 | flag == PR_FORK ? "PR_FORK" : |
1395 | flag == PR_RLC ? "PR_RLC" : | |
1396 | #ifdef PR_ASYNC | |
1397 | flag == PR_ASYNC ? "PR_ASYNC" : | |
0d06e24b JM |
1398 | #endif |
1399 | #ifdef PR_KLC | |
1400 | flag == PR_KLC ? "PR_KLC" : | |
c3f6f71d JM |
1401 | #endif |
1402 | "<unknown flag>", | |
1403 | mode == FLAG_RESET ? "off" : "on"); | |
c906108c | 1404 | |
c3f6f71d JM |
1405 | return win; |
1406 | } | |
c906108c | 1407 | |
77382aee PA |
1408 | /* Set the run_on_last_close flag. Process with all threads will |
1409 | become runnable when debugger closes all /proc fds. Returns | |
1410 | non-zero for success, zero for failure. */ | |
c906108c | 1411 | |
d3581e61 | 1412 | static int |
fba45db2 | 1413 | proc_set_run_on_last_close (procinfo *pi) |
c906108c | 1414 | { |
c3f6f71d JM |
1415 | return proc_modify_flag (pi, PR_RLC, FLAG_SET); |
1416 | } | |
c906108c | 1417 | |
77382aee PA |
1418 | /* Reset the run_on_last_close flag. The process will NOT become |
1419 | runnable when debugger closes its file handles. Returns non-zero | |
1420 | for success, zero for failure. */ | |
c906108c | 1421 | |
d3581e61 | 1422 | static int |
fba45db2 | 1423 | proc_unset_run_on_last_close (procinfo *pi) |
c3f6f71d JM |
1424 | { |
1425 | return proc_modify_flag (pi, PR_RLC, FLAG_RESET); | |
c906108c SS |
1426 | } |
1427 | ||
77382aee PA |
1428 | /* Reset inherit_on_fork flag. If the process forks a child while we |
1429 | are registered for events in the parent, then we will NOT recieve | |
1430 | events from the child. Returns non-zero for success, zero for | |
1431 | failure. */ | |
c906108c | 1432 | |
d3581e61 | 1433 | static int |
fba45db2 | 1434 | proc_unset_inherit_on_fork (procinfo *pi) |
c3f6f71d JM |
1435 | { |
1436 | return proc_modify_flag (pi, PR_FORK, FLAG_RESET); | |
1437 | } | |
c906108c | 1438 | |
c3f6f71d | 1439 | #ifdef PR_ASYNC |
77382aee PA |
1440 | /* Set PR_ASYNC flag. If one LWP stops because of a debug event |
1441 | (signal etc.), the remaining LWPs will continue to run. Returns | |
1442 | non-zero for success, zero for failure. */ | |
c906108c | 1443 | |
d3581e61 | 1444 | static int |
fba45db2 | 1445 | proc_set_async (procinfo *pi) |
c3f6f71d JM |
1446 | { |
1447 | return proc_modify_flag (pi, PR_ASYNC, FLAG_SET); | |
1448 | } | |
c906108c | 1449 | |
77382aee PA |
1450 | /* Reset PR_ASYNC flag. If one LWP stops because of a debug event |
1451 | (signal etc.), then all other LWPs will stop as well. Returns | |
1452 | non-zero for success, zero for failure. */ | |
c906108c | 1453 | |
d3581e61 | 1454 | static int |
fba45db2 | 1455 | proc_unset_async (procinfo *pi) |
c3f6f71d JM |
1456 | { |
1457 | return proc_modify_flag (pi, PR_ASYNC, FLAG_RESET); | |
1458 | } | |
1459 | #endif /* PR_ASYNC */ | |
c906108c | 1460 | |
77382aee PA |
1461 | /* Request the process/LWP to stop. Does not wait. Returns non-zero |
1462 | for success, zero for failure. */ | |
c906108c | 1463 | |
d3581e61 | 1464 | static int |
fba45db2 | 1465 | proc_stop_process (procinfo *pi) |
c3f6f71d JM |
1466 | { |
1467 | int win; | |
c906108c | 1468 | |
77382aee PA |
1469 | /* We might conceivably apply this operation to an LWP, and the |
1470 | LWP's ctl file descriptor might not be open. */ | |
c906108c | 1471 | |
c3f6f71d JM |
1472 | if (pi->ctl_fd == 0 && |
1473 | open_procinfo_files (pi, FD_CTL) == 0) | |
1474 | return 0; | |
1475 | else | |
1476 | { | |
1477 | #ifdef NEW_PROC_API | |
37de36c6 | 1478 | procfs_ctl_t cmd = PCSTOP; |
f4a14ae6 | 1479 | |
c3f6f71d JM |
1480 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
1481 | #else /* ioctl method */ | |
1482 | win = (ioctl (pi->ctl_fd, PIOCSTOP, &pi->prstatus) >= 0); | |
1483 | /* Note: the call also reads the prstatus. */ | |
1484 | if (win) | |
1485 | { | |
1486 | pi->status_valid = 1; | |
19958708 | 1487 | PROC_PRETTYFPRINT_STATUS (proc_flags (pi), |
c3f6f71d | 1488 | proc_why (pi), |
19958708 | 1489 | proc_what (pi), |
c3f6f71d JM |
1490 | proc_get_current_thread (pi)); |
1491 | } | |
1492 | #endif | |
1493 | } | |
c906108c | 1494 | |
c3f6f71d JM |
1495 | return win; |
1496 | } | |
c5aa993b | 1497 | |
77382aee PA |
1498 | /* Wait for the process or LWP to stop (block until it does). Returns |
1499 | non-zero for success, zero for failure. */ | |
c906108c | 1500 | |
d3581e61 | 1501 | static int |
fba45db2 | 1502 | proc_wait_for_stop (procinfo *pi) |
c906108c | 1503 | { |
c3f6f71d JM |
1504 | int win; |
1505 | ||
77382aee PA |
1506 | /* We should never have to apply this operation to any procinfo |
1507 | except the one for the main process. If that ever changes for | |
1508 | any reason, then take out the following clause and replace it | |
1509 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1510 | |
c3f6f71d JM |
1511 | if (pi->tid != 0) |
1512 | pi = find_procinfo_or_die (pi->pid, 0); | |
1513 | ||
1514 | #ifdef NEW_PROC_API | |
1515 | { | |
37de36c6 | 1516 | procfs_ctl_t cmd = PCWSTOP; |
f4a14ae6 | 1517 | |
c3f6f71d JM |
1518 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
1519 | /* We been runnin' and we stopped -- need to update status. */ | |
1520 | pi->status_valid = 0; | |
1521 | } | |
1522 | #else /* ioctl method */ | |
1523 | win = (ioctl (pi->ctl_fd, PIOCWSTOP, &pi->prstatus) >= 0); | |
1524 | /* Above call also refreshes the prstatus. */ | |
1525 | if (win) | |
1526 | { | |
1527 | pi->status_valid = 1; | |
19958708 | 1528 | PROC_PRETTYFPRINT_STATUS (proc_flags (pi), |
c3f6f71d | 1529 | proc_why (pi), |
19958708 | 1530 | proc_what (pi), |
c3f6f71d JM |
1531 | proc_get_current_thread (pi)); |
1532 | } | |
c906108c SS |
1533 | #endif |
1534 | ||
c3f6f71d | 1535 | return win; |
c906108c SS |
1536 | } |
1537 | ||
77382aee PA |
1538 | /* Make the process or LWP runnable. |
1539 | ||
1540 | Options (not all are implemented): | |
1541 | - single-step | |
1542 | - clear current fault | |
1543 | - clear current signal | |
1544 | - abort the current system call | |
1545 | - stop as soon as finished with system call | |
1546 | - (ioctl): set traced signal set | |
1547 | - (ioctl): set held signal set | |
1548 | - (ioctl): set traced fault set | |
1549 | - (ioctl): set start pc (vaddr) | |
1550 | ||
1551 | Always clears the current fault. PI is the process or LWP to | |
1552 | operate on. If STEP is true, set the process or LWP to trap after | |
1553 | one instruction. If SIGNO is zero, clear the current signal if | |
1554 | any; if non-zero, set the current signal to this one. Returns | |
1555 | non-zero for success, zero for failure. */ | |
c3f6f71d | 1556 | |
d3581e61 | 1557 | static int |
fba45db2 | 1558 | proc_run_process (procinfo *pi, int step, int signo) |
c3f6f71d JM |
1559 | { |
1560 | int win; | |
1561 | int runflags; | |
1562 | ||
77382aee PA |
1563 | /* We will probably have to apply this operation to individual |
1564 | threads, so make sure the control file descriptor is open. */ | |
19958708 | 1565 | |
c3f6f71d JM |
1566 | if (pi->ctl_fd == 0 && |
1567 | open_procinfo_files (pi, FD_CTL) == 0) | |
1568 | { | |
1569 | return 0; | |
1570 | } | |
c906108c | 1571 | |
0df8b418 | 1572 | runflags = PRCFAULT; /* Always clear current fault. */ |
c3f6f71d JM |
1573 | if (step) |
1574 | runflags |= PRSTEP; | |
1575 | if (signo == 0) | |
1576 | runflags |= PRCSIG; | |
0df8b418 | 1577 | else if (signo != -1) /* -1 means do nothing W.R.T. signals. */ |
c3f6f71d | 1578 | proc_set_current_signal (pi, signo); |
c5aa993b | 1579 | |
c3f6f71d JM |
1580 | #ifdef NEW_PROC_API |
1581 | { | |
37de36c6 | 1582 | procfs_ctl_t cmd[2]; |
c906108c | 1583 | |
c3f6f71d JM |
1584 | cmd[0] = PCRUN; |
1585 | cmd[1] = runflags; | |
1586 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); | |
1587 | } | |
1588 | #else /* ioctl method */ | |
1589 | { | |
1590 | prrun_t prrun; | |
c906108c | 1591 | |
c3f6f71d JM |
1592 | memset (&prrun, 0, sizeof (prrun)); |
1593 | prrun.pr_flags = runflags; | |
1594 | win = (ioctl (pi->ctl_fd, PIOCRUN, &prrun) >= 0); | |
1595 | } | |
1596 | #endif | |
c906108c | 1597 | |
c3f6f71d JM |
1598 | return win; |
1599 | } | |
c906108c | 1600 | |
77382aee PA |
1601 | /* Register to trace signals in the process or LWP. Returns non-zero |
1602 | for success, zero for failure. */ | |
c906108c | 1603 | |
d3581e61 | 1604 | static int |
37de36c6 | 1605 | proc_set_traced_signals (procinfo *pi, gdb_sigset_t *sigset) |
c906108c | 1606 | { |
c3f6f71d JM |
1607 | int win; |
1608 | ||
77382aee PA |
1609 | /* We should never have to apply this operation to any procinfo |
1610 | except the one for the main process. If that ever changes for | |
1611 | any reason, then take out the following clause and replace it | |
1612 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1613 | |
c3f6f71d JM |
1614 | if (pi->tid != 0) |
1615 | pi = find_procinfo_or_die (pi->pid, 0); | |
1616 | ||
1617 | #ifdef NEW_PROC_API | |
1618 | { | |
1619 | struct { | |
37de36c6 | 1620 | procfs_ctl_t cmd; |
c3f6f71d | 1621 | /* Use char array to avoid alignment issues. */ |
37de36c6 | 1622 | char sigset[sizeof (gdb_sigset_t)]; |
c3f6f71d | 1623 | } arg; |
c906108c | 1624 | |
c3f6f71d | 1625 | arg.cmd = PCSTRACE; |
37de36c6 | 1626 | memcpy (&arg.sigset, sigset, sizeof (gdb_sigset_t)); |
c906108c | 1627 | |
c3f6f71d JM |
1628 | win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg)); |
1629 | } | |
1630 | #else /* ioctl method */ | |
1631 | win = (ioctl (pi->ctl_fd, PIOCSTRACE, sigset) >= 0); | |
1632 | #endif | |
0df8b418 | 1633 | /* The above operation renders the procinfo's cached pstatus obsolete. */ |
c3f6f71d | 1634 | pi->status_valid = 0; |
c906108c | 1635 | |
c3f6f71d | 1636 | if (!win) |
8a3fe4f8 | 1637 | warning (_("procfs: set_traced_signals failed")); |
c3f6f71d | 1638 | return win; |
c906108c SS |
1639 | } |
1640 | ||
77382aee PA |
1641 | /* Register to trace hardware faults in the process or LWP. Returns |
1642 | non-zero for success, zero for failure. */ | |
c906108c | 1643 | |
d3581e61 | 1644 | static int |
fba45db2 | 1645 | proc_set_traced_faults (procinfo *pi, fltset_t *fltset) |
c3f6f71d JM |
1646 | { |
1647 | int win; | |
1648 | ||
77382aee PA |
1649 | /* We should never have to apply this operation to any procinfo |
1650 | except the one for the main process. If that ever changes for | |
1651 | any reason, then take out the following clause and replace it | |
1652 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1653 | |
c3f6f71d JM |
1654 | if (pi->tid != 0) |
1655 | pi = find_procinfo_or_die (pi->pid, 0); | |
1656 | ||
1657 | #ifdef NEW_PROC_API | |
1658 | { | |
1659 | struct { | |
37de36c6 | 1660 | procfs_ctl_t cmd; |
c3f6f71d JM |
1661 | /* Use char array to avoid alignment issues. */ |
1662 | char fltset[sizeof (fltset_t)]; | |
1663 | } arg; | |
c906108c | 1664 | |
c3f6f71d JM |
1665 | arg.cmd = PCSFAULT; |
1666 | memcpy (&arg.fltset, fltset, sizeof (fltset_t)); | |
c906108c | 1667 | |
c3f6f71d JM |
1668 | win = (write (pi->ctl_fd, (char *) &arg, sizeof (arg)) == sizeof (arg)); |
1669 | } | |
1670 | #else /* ioctl method */ | |
1671 | win = (ioctl (pi->ctl_fd, PIOCSFAULT, fltset) >= 0); | |
1672 | #endif | |
0df8b418 | 1673 | /* The above operation renders the procinfo's cached pstatus obsolete. */ |
c3f6f71d | 1674 | pi->status_valid = 0; |
c906108c | 1675 | |
c3f6f71d JM |
1676 | return win; |
1677 | } | |
c5aa993b | 1678 | |
77382aee PA |
1679 | /* Register to trace entry to system calls in the process or LWP. |
1680 | Returns non-zero for success, zero for failure. */ | |
c906108c | 1681 | |
d3581e61 | 1682 | static int |
fba45db2 | 1683 | proc_set_traced_sysentry (procinfo *pi, sysset_t *sysset) |
c906108c | 1684 | { |
c3f6f71d JM |
1685 | int win; |
1686 | ||
77382aee PA |
1687 | /* We should never have to apply this operation to any procinfo |
1688 | except the one for the main process. If that ever changes for | |
1689 | any reason, then take out the following clause and replace it | |
1690 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1691 | |
c3f6f71d JM |
1692 | if (pi->tid != 0) |
1693 | pi = find_procinfo_or_die (pi->pid, 0); | |
1694 | ||
1695 | #ifdef NEW_PROC_API | |
1696 | { | |
37de36c6 KB |
1697 | struct gdb_proc_ctl_pcsentry { |
1698 | procfs_ctl_t cmd; | |
c3f6f71d JM |
1699 | /* Use char array to avoid alignment issues. */ |
1700 | char sysset[sizeof (sysset_t)]; | |
37de36c6 KB |
1701 | } *argp; |
1702 | int argp_size = sizeof (struct gdb_proc_ctl_pcsentry) | |
77382aee | 1703 | - sizeof (sysset_t) |
37de36c6 | 1704 | + sysset_t_size (pi); |
c3f6f71d | 1705 | |
37de36c6 | 1706 | argp = xmalloc (argp_size); |
c3f6f71d | 1707 | |
37de36c6 KB |
1708 | argp->cmd = PCSENTRY; |
1709 | memcpy (&argp->sysset, sysset, sysset_t_size (pi)); | |
1710 | ||
1711 | win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size); | |
1712 | xfree (argp); | |
c3f6f71d JM |
1713 | } |
1714 | #else /* ioctl method */ | |
1715 | win = (ioctl (pi->ctl_fd, PIOCSENTRY, sysset) >= 0); | |
1716 | #endif | |
77382aee PA |
1717 | /* The above operation renders the procinfo's cached pstatus |
1718 | obsolete. */ | |
c3f6f71d | 1719 | pi->status_valid = 0; |
19958708 | 1720 | |
c3f6f71d | 1721 | return win; |
c906108c SS |
1722 | } |
1723 | ||
77382aee PA |
1724 | /* Register to trace exit from system calls in the process or LWP. |
1725 | Returns non-zero for success, zero for failure. */ | |
c906108c | 1726 | |
d3581e61 | 1727 | static int |
fba45db2 | 1728 | proc_set_traced_sysexit (procinfo *pi, sysset_t *sysset) |
c3f6f71d JM |
1729 | { |
1730 | int win; | |
1731 | ||
77382aee PA |
1732 | /* We should never have to apply this operation to any procinfo |
1733 | except the one for the main process. If that ever changes for | |
1734 | any reason, then take out the following clause and replace it | |
1735 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1736 | |
c3f6f71d JM |
1737 | if (pi->tid != 0) |
1738 | pi = find_procinfo_or_die (pi->pid, 0); | |
1739 | ||
1740 | #ifdef NEW_PROC_API | |
1741 | { | |
37de36c6 KB |
1742 | struct gdb_proc_ctl_pcsexit { |
1743 | procfs_ctl_t cmd; | |
c3f6f71d JM |
1744 | /* Use char array to avoid alignment issues. */ |
1745 | char sysset[sizeof (sysset_t)]; | |
37de36c6 KB |
1746 | } *argp; |
1747 | int argp_size = sizeof (struct gdb_proc_ctl_pcsexit) | |
77382aee | 1748 | - sizeof (sysset_t) |
37de36c6 | 1749 | + sysset_t_size (pi); |
c906108c | 1750 | |
37de36c6 | 1751 | argp = xmalloc (argp_size); |
c906108c | 1752 | |
37de36c6 KB |
1753 | argp->cmd = PCSEXIT; |
1754 | memcpy (&argp->sysset, sysset, sysset_t_size (pi)); | |
1755 | ||
1756 | win = (write (pi->ctl_fd, (char *) argp, argp_size) == argp_size); | |
1757 | xfree (argp); | |
c3f6f71d JM |
1758 | } |
1759 | #else /* ioctl method */ | |
1760 | win = (ioctl (pi->ctl_fd, PIOCSEXIT, sysset) >= 0); | |
1761 | #endif | |
77382aee PA |
1762 | /* The above operation renders the procinfo's cached pstatus |
1763 | obsolete. */ | |
c3f6f71d | 1764 | pi->status_valid = 0; |
c906108c | 1765 | |
c3f6f71d JM |
1766 | return win; |
1767 | } | |
c906108c | 1768 | |
77382aee PA |
1769 | /* Specify the set of blocked / held signals in the process or LWP. |
1770 | Returns non-zero for success, zero for failure. */ | |
c906108c | 1771 | |
d3581e61 | 1772 | static int |
37de36c6 | 1773 | proc_set_held_signals (procinfo *pi, gdb_sigset_t *sighold) |
c906108c | 1774 | { |
c3f6f71d JM |
1775 | int win; |
1776 | ||
77382aee PA |
1777 | /* We should never have to apply this operation to any procinfo |
1778 | except the one for the main process. If that ever changes for | |
1779 | any reason, then take out the following clause and replace it | |
1780 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1781 | |
c3f6f71d JM |
1782 | if (pi->tid != 0) |
1783 | pi = find_procinfo_or_die (pi->pid, 0); | |
1784 | ||
1785 | #ifdef NEW_PROC_API | |
1786 | { | |
1787 | struct { | |
37de36c6 | 1788 | procfs_ctl_t cmd; |
c3f6f71d | 1789 | /* Use char array to avoid alignment issues. */ |
37de36c6 | 1790 | char hold[sizeof (gdb_sigset_t)]; |
c3f6f71d JM |
1791 | } arg; |
1792 | ||
1793 | arg.cmd = PCSHOLD; | |
37de36c6 | 1794 | memcpy (&arg.hold, sighold, sizeof (gdb_sigset_t)); |
c3f6f71d JM |
1795 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); |
1796 | } | |
c906108c | 1797 | #else |
c3f6f71d | 1798 | win = (ioctl (pi->ctl_fd, PIOCSHOLD, sighold) >= 0); |
c906108c | 1799 | #endif |
77382aee PA |
1800 | /* The above operation renders the procinfo's cached pstatus |
1801 | obsolete. */ | |
c3f6f71d JM |
1802 | pi->status_valid = 0; |
1803 | ||
1804 | return win; | |
c906108c SS |
1805 | } |
1806 | ||
77382aee PA |
1807 | /* Returns the set of signals that are held / blocked. Will also copy |
1808 | the sigset if SAVE is non-zero. */ | |
c906108c | 1809 | |
d3581e61 | 1810 | static gdb_sigset_t * |
37de36c6 | 1811 | proc_get_held_signals (procinfo *pi, gdb_sigset_t *save) |
c906108c | 1812 | { |
37de36c6 | 1813 | gdb_sigset_t *ret = NULL; |
c3f6f71d | 1814 | |
77382aee PA |
1815 | /* We should never have to apply this operation to any procinfo |
1816 | except the one for the main process. If that ever changes for | |
1817 | any reason, then take out the following clause and replace it | |
1818 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1819 | |
c3f6f71d JM |
1820 | if (pi->tid != 0) |
1821 | pi = find_procinfo_or_die (pi->pid, 0); | |
1822 | ||
1823 | #ifdef NEW_PROC_API | |
1824 | if (!pi->status_valid) | |
1825 | if (!proc_get_status (pi)) | |
1826 | return NULL; | |
1827 | ||
1828 | #ifdef UNIXWARE | |
1829 | ret = &pi->prstatus.pr_lwp.pr_context.uc_sigmask; | |
c906108c | 1830 | #else |
c3f6f71d JM |
1831 | ret = &pi->prstatus.pr_lwp.pr_lwphold; |
1832 | #endif /* UNIXWARE */ | |
1833 | #else /* not NEW_PROC_API */ | |
1834 | { | |
37de36c6 | 1835 | static gdb_sigset_t sigheld; |
c3f6f71d JM |
1836 | |
1837 | if (ioctl (pi->ctl_fd, PIOCGHOLD, &sigheld) >= 0) | |
1838 | ret = &sigheld; | |
1839 | } | |
1840 | #endif /* NEW_PROC_API */ | |
1841 | if (save && ret) | |
37de36c6 | 1842 | memcpy (save, ret, sizeof (gdb_sigset_t)); |
c3f6f71d JM |
1843 | |
1844 | return ret; | |
c906108c SS |
1845 | } |
1846 | ||
77382aee PA |
1847 | /* Returns the set of signals that are traced / debugged. Will also |
1848 | copy the sigset if SAVE is non-zero. */ | |
c3f6f71d | 1849 | |
d3581e61 | 1850 | static gdb_sigset_t * |
37de36c6 | 1851 | proc_get_traced_signals (procinfo *pi, gdb_sigset_t *save) |
c906108c | 1852 | { |
37de36c6 | 1853 | gdb_sigset_t *ret = NULL; |
c3f6f71d | 1854 | |
77382aee PA |
1855 | /* We should never have to apply this operation to any procinfo |
1856 | except the one for the main process. If that ever changes for | |
1857 | any reason, then take out the following clause and replace it | |
1858 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1859 | |
c3f6f71d JM |
1860 | if (pi->tid != 0) |
1861 | pi = find_procinfo_or_die (pi->pid, 0); | |
1862 | ||
1863 | #ifdef NEW_PROC_API | |
1864 | if (!pi->status_valid) | |
1865 | if (!proc_get_status (pi)) | |
1866 | return NULL; | |
1867 | ||
1868 | ret = &pi->prstatus.pr_sigtrace; | |
1869 | #else | |
1870 | { | |
37de36c6 | 1871 | static gdb_sigset_t sigtrace; |
c3f6f71d JM |
1872 | |
1873 | if (ioctl (pi->ctl_fd, PIOCGTRACE, &sigtrace) >= 0) | |
1874 | ret = &sigtrace; | |
1875 | } | |
c906108c | 1876 | #endif |
c3f6f71d | 1877 | if (save && ret) |
37de36c6 | 1878 | memcpy (save, ret, sizeof (gdb_sigset_t)); |
c906108c | 1879 | |
c3f6f71d JM |
1880 | return ret; |
1881 | } | |
c906108c | 1882 | |
77382aee PA |
1883 | /* Returns the set of hardware faults that are traced /debugged. Will |
1884 | also copy the faultset if SAVE is non-zero. */ | |
c3f6f71d | 1885 | |
d3581e61 | 1886 | static fltset_t * |
fba45db2 | 1887 | proc_get_traced_faults (procinfo *pi, fltset_t *save) |
c3f6f71d JM |
1888 | { |
1889 | fltset_t *ret = NULL; | |
1890 | ||
77382aee PA |
1891 | /* We should never have to apply this operation to any procinfo |
1892 | except the one for the main process. If that ever changes for | |
1893 | any reason, then take out the following clause and replace it | |
1894 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1895 | |
c3f6f71d JM |
1896 | if (pi->tid != 0) |
1897 | pi = find_procinfo_or_die (pi->pid, 0); | |
1898 | ||
1899 | #ifdef NEW_PROC_API | |
1900 | if (!pi->status_valid) | |
1901 | if (!proc_get_status (pi)) | |
1902 | return NULL; | |
1903 | ||
1904 | ret = &pi->prstatus.pr_flttrace; | |
1905 | #else | |
1906 | { | |
1907 | static fltset_t flttrace; | |
1908 | ||
1909 | if (ioctl (pi->ctl_fd, PIOCGFAULT, &flttrace) >= 0) | |
1910 | ret = &flttrace; | |
1911 | } | |
1912 | #endif | |
1913 | if (save && ret) | |
1914 | memcpy (save, ret, sizeof (fltset_t)); | |
c906108c | 1915 | |
c3f6f71d JM |
1916 | return ret; |
1917 | } | |
c906108c | 1918 | |
77382aee PA |
1919 | /* Returns the set of syscalls that are traced /debugged on entry. |
1920 | Will also copy the syscall set if SAVE is non-zero. */ | |
c906108c | 1921 | |
d3581e61 | 1922 | static sysset_t * |
fba45db2 | 1923 | proc_get_traced_sysentry (procinfo *pi, sysset_t *save) |
c3f6f71d JM |
1924 | { |
1925 | sysset_t *ret = NULL; | |
1926 | ||
77382aee PA |
1927 | /* We should never have to apply this operation to any procinfo |
1928 | except the one for the main process. If that ever changes for | |
1929 | any reason, then take out the following clause and replace it | |
1930 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1931 | |
c3f6f71d JM |
1932 | if (pi->tid != 0) |
1933 | pi = find_procinfo_or_die (pi->pid, 0); | |
1934 | ||
1935 | #ifdef NEW_PROC_API | |
1936 | if (!pi->status_valid) | |
1937 | if (!proc_get_status (pi)) | |
1938 | return NULL; | |
1939 | ||
37de36c6 | 1940 | #ifndef DYNAMIC_SYSCALLS |
c3f6f71d | 1941 | ret = &pi->prstatus.pr_sysentry; |
37de36c6 KB |
1942 | #else /* DYNAMIC_SYSCALLS */ |
1943 | { | |
1944 | static sysset_t *sysentry; | |
1945 | size_t size; | |
1946 | ||
1947 | if (!sysentry) | |
1948 | sysentry = sysset_t_alloc (pi); | |
1949 | ret = sysentry; | |
1950 | if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0) | |
1951 | return NULL; | |
1952 | if (pi->prstatus.pr_sysentry_offset == 0) | |
1953 | { | |
1954 | gdb_premptysysset (sysentry); | |
1955 | } | |
1956 | else | |
1957 | { | |
1958 | int rsize; | |
1959 | ||
1960 | if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysentry_offset, | |
77382aee | 1961 | SEEK_SET) |
37de36c6 KB |
1962 | != (off_t) pi->prstatus.pr_sysentry_offset) |
1963 | return NULL; | |
1964 | size = sysset_t_size (pi); | |
1965 | gdb_premptysysset (sysentry); | |
1966 | rsize = read (pi->status_fd, sysentry, size); | |
1967 | if (rsize < 0) | |
1968 | return NULL; | |
1969 | } | |
1970 | } | |
1971 | #endif /* DYNAMIC_SYSCALLS */ | |
1972 | #else /* !NEW_PROC_API */ | |
c3f6f71d JM |
1973 | { |
1974 | static sysset_t sysentry; | |
c906108c | 1975 | |
c3f6f71d JM |
1976 | if (ioctl (pi->ctl_fd, PIOCGENTRY, &sysentry) >= 0) |
1977 | ret = &sysentry; | |
1978 | } | |
37de36c6 | 1979 | #endif /* NEW_PROC_API */ |
c3f6f71d | 1980 | if (save && ret) |
37de36c6 | 1981 | memcpy (save, ret, sysset_t_size (pi)); |
c906108c | 1982 | |
c3f6f71d JM |
1983 | return ret; |
1984 | } | |
c5aa993b | 1985 | |
77382aee PA |
1986 | /* Returns the set of syscalls that are traced /debugged on exit. |
1987 | Will also copy the syscall set if SAVE is non-zero. */ | |
c906108c | 1988 | |
d3581e61 | 1989 | static sysset_t * |
fba45db2 | 1990 | proc_get_traced_sysexit (procinfo *pi, sysset_t *save) |
c906108c | 1991 | { |
c3f6f71d JM |
1992 | sysset_t * ret = NULL; |
1993 | ||
77382aee PA |
1994 | /* We should never have to apply this operation to any procinfo |
1995 | except the one for the main process. If that ever changes for | |
1996 | any reason, then take out the following clause and replace it | |
1997 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 1998 | |
c3f6f71d JM |
1999 | if (pi->tid != 0) |
2000 | pi = find_procinfo_or_die (pi->pid, 0); | |
2001 | ||
2002 | #ifdef NEW_PROC_API | |
2003 | if (!pi->status_valid) | |
2004 | if (!proc_get_status (pi)) | |
2005 | return NULL; | |
2006 | ||
37de36c6 | 2007 | #ifndef DYNAMIC_SYSCALLS |
c3f6f71d | 2008 | ret = &pi->prstatus.pr_sysexit; |
37de36c6 KB |
2009 | #else /* DYNAMIC_SYSCALLS */ |
2010 | { | |
2011 | static sysset_t *sysexit; | |
2012 | size_t size; | |
2013 | ||
2014 | if (!sysexit) | |
2015 | sysexit = sysset_t_alloc (pi); | |
2016 | ret = sysexit; | |
2017 | if (pi->status_fd == 0 && open_procinfo_files (pi, FD_STATUS) == 0) | |
2018 | return NULL; | |
2019 | if (pi->prstatus.pr_sysexit_offset == 0) | |
2020 | { | |
2021 | gdb_premptysysset (sysexit); | |
2022 | } | |
2023 | else | |
2024 | { | |
2025 | int rsize; | |
2026 | ||
77382aee PA |
2027 | if (lseek (pi->status_fd, (off_t) pi->prstatus.pr_sysexit_offset, |
2028 | SEEK_SET) | |
37de36c6 KB |
2029 | != (off_t) pi->prstatus.pr_sysexit_offset) |
2030 | return NULL; | |
2031 | size = sysset_t_size (pi); | |
2032 | gdb_premptysysset (sysexit); | |
2033 | rsize = read (pi->status_fd, sysexit, size); | |
2034 | if (rsize < 0) | |
2035 | return NULL; | |
2036 | } | |
2037 | } | |
2038 | #endif /* DYNAMIC_SYSCALLS */ | |
c3f6f71d JM |
2039 | #else |
2040 | { | |
2041 | static sysset_t sysexit; | |
c5aa993b | 2042 | |
c3f6f71d JM |
2043 | if (ioctl (pi->ctl_fd, PIOCGEXIT, &sysexit) >= 0) |
2044 | ret = &sysexit; | |
2045 | } | |
2046 | #endif | |
2047 | if (save && ret) | |
37de36c6 | 2048 | memcpy (save, ret, sysset_t_size (pi)); |
c3f6f71d JM |
2049 | |
2050 | return ret; | |
2051 | } | |
c906108c | 2052 | |
77382aee PA |
2053 | /* The current fault (if any) is cleared; the associated signal will |
2054 | not be sent to the process or LWP when it resumes. Returns | |
2055 | non-zero for success, zero for failure. */ | |
c906108c | 2056 | |
d3581e61 | 2057 | static int |
fba45db2 | 2058 | proc_clear_current_fault (procinfo *pi) |
c3f6f71d JM |
2059 | { |
2060 | int win; | |
2061 | ||
77382aee PA |
2062 | /* We should never have to apply this operation to any procinfo |
2063 | except the one for the main process. If that ever changes for | |
2064 | any reason, then take out the following clause and replace it | |
2065 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2066 | |
c3f6f71d JM |
2067 | if (pi->tid != 0) |
2068 | pi = find_procinfo_or_die (pi->pid, 0); | |
2069 | ||
2070 | #ifdef NEW_PROC_API | |
2071 | { | |
37de36c6 | 2072 | procfs_ctl_t cmd = PCCFAULT; |
f4a14ae6 | 2073 | |
c3f6f71d JM |
2074 | win = (write (pi->ctl_fd, (void *) &cmd, sizeof (cmd)) == sizeof (cmd)); |
2075 | } | |
2076 | #else | |
2077 | win = (ioctl (pi->ctl_fd, PIOCCFAULT, 0) >= 0); | |
2078 | #endif | |
2079 | ||
2080 | return win; | |
c906108c SS |
2081 | } |
2082 | ||
77382aee PA |
2083 | /* Set the "current signal" that will be delivered next to the |
2084 | process. NOTE: semantics are different from those of KILL. This | |
2085 | signal will be delivered to the process or LWP immediately when it | |
2086 | is resumed (even if the signal is held/blocked); it will NOT | |
2087 | immediately cause another event of interest, and will NOT first | |
2088 | trap back to the debugger. Returns non-zero for success, zero for | |
2089 | failure. */ | |
c3f6f71d | 2090 | |
d3581e61 | 2091 | static int |
fba45db2 | 2092 | proc_set_current_signal (procinfo *pi, int signo) |
c3f6f71d JM |
2093 | { |
2094 | int win; | |
2095 | struct { | |
37de36c6 | 2096 | procfs_ctl_t cmd; |
c3f6f71d | 2097 | /* Use char array to avoid alignment issues. */ |
37de36c6 | 2098 | char sinfo[sizeof (gdb_siginfo_t)]; |
c3f6f71d | 2099 | } arg; |
73930d4d | 2100 | gdb_siginfo_t mysinfo; |
c162e8c9 JM |
2101 | ptid_t wait_ptid; |
2102 | struct target_waitstatus wait_status; | |
c3f6f71d | 2103 | |
77382aee PA |
2104 | /* We should never have to apply this operation to any procinfo |
2105 | except the one for the main process. If that ever changes for | |
2106 | any reason, then take out the following clause and replace it | |
2107 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2108 | |
c3f6f71d JM |
2109 | if (pi->tid != 0) |
2110 | pi = find_procinfo_or_die (pi->pid, 0); | |
2111 | ||
2112 | #ifdef PROCFS_DONT_PIOCSSIG_CURSIG | |
2113 | /* With Alpha OSF/1 procfs, the kernel gets really confused if it | |
77382aee PA |
2114 | receives a PIOCSSIG with a signal identical to the current |
2115 | signal, it messes up the current signal. Work around the kernel | |
2116 | bug. */ | |
c3f6f71d JM |
2117 | if (signo > 0 && |
2118 | signo == proc_cursig (pi)) | |
0df8b418 | 2119 | return 1; /* I assume this is a success? */ |
c3f6f71d JM |
2120 | #endif |
2121 | ||
2122 | /* The pointer is just a type alias. */ | |
c162e8c9 JM |
2123 | get_last_target_status (&wait_ptid, &wait_status); |
2124 | if (ptid_equal (wait_ptid, inferior_ptid) | |
2125 | && wait_status.kind == TARGET_WAITKIND_STOPPED | |
2ea28649 | 2126 | && wait_status.value.sig == gdb_signal_from_host (signo) |
c162e8c9 JM |
2127 | && proc_get_status (pi) |
2128 | #ifdef NEW_PROC_API | |
2129 | && pi->prstatus.pr_lwp.pr_info.si_signo == signo | |
2130 | #else | |
2131 | && pi->prstatus.pr_info.si_signo == signo | |
2132 | #endif | |
2133 | ) | |
2134 | /* Use the siginfo associated with the signal being | |
2135 | redelivered. */ | |
2136 | #ifdef NEW_PROC_API | |
73930d4d | 2137 | memcpy (arg.sinfo, &pi->prstatus.pr_lwp.pr_info, sizeof (gdb_siginfo_t)); |
c162e8c9 | 2138 | #else |
73930d4d | 2139 | memcpy (arg.sinfo, &pi->prstatus.pr_info, sizeof (gdb_siginfo_t)); |
c162e8c9 JM |
2140 | #endif |
2141 | else | |
2142 | { | |
73930d4d KH |
2143 | mysinfo.si_signo = signo; |
2144 | mysinfo.si_code = 0; | |
2145 | mysinfo.si_pid = getpid (); /* ?why? */ | |
2146 | mysinfo.si_uid = getuid (); /* ?why? */ | |
2147 | memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t)); | |
c162e8c9 | 2148 | } |
c3f6f71d JM |
2149 | |
2150 | #ifdef NEW_PROC_API | |
2151 | arg.cmd = PCSSIG; | |
2152 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); | |
2153 | #else | |
2154 | win = (ioctl (pi->ctl_fd, PIOCSSIG, (void *) &arg.sinfo) >= 0); | |
2155 | #endif | |
c906108c | 2156 | |
c3f6f71d JM |
2157 | return win; |
2158 | } | |
c906108c | 2159 | |
77382aee PA |
2160 | /* The current signal (if any) is cleared, and is not sent to the |
2161 | process or LWP when it resumes. Returns non-zero for success, zero | |
2162 | for failure. */ | |
c906108c | 2163 | |
d3581e61 | 2164 | static int |
fba45db2 | 2165 | proc_clear_current_signal (procinfo *pi) |
c3f6f71d JM |
2166 | { |
2167 | int win; | |
2168 | ||
77382aee PA |
2169 | /* We should never have to apply this operation to any procinfo |
2170 | except the one for the main process. If that ever changes for | |
2171 | any reason, then take out the following clause and replace it | |
2172 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2173 | |
c3f6f71d JM |
2174 | if (pi->tid != 0) |
2175 | pi = find_procinfo_or_die (pi->pid, 0); | |
2176 | ||
2177 | #ifdef NEW_PROC_API | |
2178 | { | |
2179 | struct { | |
37de36c6 | 2180 | procfs_ctl_t cmd; |
c3f6f71d | 2181 | /* Use char array to avoid alignment issues. */ |
37de36c6 | 2182 | char sinfo[sizeof (gdb_siginfo_t)]; |
c3f6f71d | 2183 | } arg; |
73930d4d | 2184 | gdb_siginfo_t mysinfo; |
c3f6f71d JM |
2185 | |
2186 | arg.cmd = PCSSIG; | |
2187 | /* The pointer is just a type alias. */ | |
73930d4d KH |
2188 | mysinfo.si_signo = 0; |
2189 | mysinfo.si_code = 0; | |
2190 | mysinfo.si_errno = 0; | |
2191 | mysinfo.si_pid = getpid (); /* ?why? */ | |
2192 | mysinfo.si_uid = getuid (); /* ?why? */ | |
2193 | memcpy (arg.sinfo, &mysinfo, sizeof (gdb_siginfo_t)); | |
c3f6f71d JM |
2194 | |
2195 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); | |
2196 | } | |
2197 | #else | |
2198 | win = (ioctl (pi->ctl_fd, PIOCSSIG, 0) >= 0); | |
2199 | #endif | |
c906108c | 2200 | |
c3f6f71d JM |
2201 | return win; |
2202 | } | |
c906108c | 2203 | |
772cf8be MK |
2204 | /* Return the general-purpose registers for the process or LWP |
2205 | corresponding to PI. Upon failure, return NULL. */ | |
c906108c | 2206 | |
d3581e61 | 2207 | static gdb_gregset_t * |
fba45db2 | 2208 | proc_get_gregs (procinfo *pi) |
c3f6f71d JM |
2209 | { |
2210 | if (!pi->status_valid || !pi->gregs_valid) | |
2211 | if (!proc_get_status (pi)) | |
2212 | return NULL; | |
2213 | ||
772cf8be MK |
2214 | /* OK, sorry about the ifdef's. There's three cases instead of two, |
2215 | because in this case Unixware and Solaris/RW differ. */ | |
c3f6f71d JM |
2216 | |
2217 | #ifdef NEW_PROC_API | |
772cf8be | 2218 | # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */ |
c3f6f71d | 2219 | return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.gregs; |
772cf8be | 2220 | # else |
c3f6f71d | 2221 | return &pi->prstatus.pr_lwp.pr_reg; |
772cf8be MK |
2222 | # endif |
2223 | #else | |
c3f6f71d | 2224 | return &pi->prstatus.pr_reg; |
772cf8be | 2225 | #endif |
c3f6f71d | 2226 | } |
c5aa993b | 2227 | |
772cf8be MK |
2228 | /* Return the general-purpose registers for the process or LWP |
2229 | corresponding to PI. Upon failure, return NULL. */ | |
c906108c | 2230 | |
d3581e61 | 2231 | static gdb_fpregset_t * |
fba45db2 | 2232 | proc_get_fpregs (procinfo *pi) |
c906108c | 2233 | { |
c3f6f71d JM |
2234 | #ifdef NEW_PROC_API |
2235 | if (!pi->status_valid || !pi->fpregs_valid) | |
2236 | if (!proc_get_status (pi)) | |
2237 | return NULL; | |
2238 | ||
772cf8be | 2239 | # ifdef UNIXWARE /* FIXME: Should be autoconfigured. */ |
c3f6f71d | 2240 | return &pi->prstatus.pr_lwp.pr_context.uc_mcontext.fpregs; |
772cf8be | 2241 | # else |
c3f6f71d | 2242 | return &pi->prstatus.pr_lwp.pr_fpreg; |
772cf8be | 2243 | # endif |
c5aa993b | 2244 | |
772cf8be | 2245 | #else /* not NEW_PROC_API */ |
c3f6f71d | 2246 | if (pi->fpregs_valid) |
772cf8be | 2247 | return &pi->fpregset; /* Already got 'em. */ |
c3f6f71d | 2248 | else |
c906108c | 2249 | { |
772cf8be | 2250 | if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0) |
c906108c | 2251 | { |
c3f6f71d | 2252 | return NULL; |
c906108c | 2253 | } |
c3f6f71d | 2254 | else |
c906108c | 2255 | { |
772cf8be | 2256 | # ifdef PIOCTGFPREG |
c3f6f71d JM |
2257 | struct { |
2258 | long pr_count; | |
2259 | tid_t pr_error_thread; | |
2260 | tfpregset_t thread_1; | |
2261 | } thread_fpregs; | |
2262 | ||
2263 | thread_fpregs.pr_count = 1; | |
2264 | thread_fpregs.thread_1.tid = pi->tid; | |
2265 | ||
772cf8be MK |
2266 | if (pi->tid == 0 |
2267 | && ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0) | |
c3f6f71d JM |
2268 | { |
2269 | pi->fpregs_valid = 1; | |
772cf8be | 2270 | return &pi->fpregset; /* Got 'em now! */ |
c3f6f71d | 2271 | } |
772cf8be MK |
2272 | else if (pi->tid != 0 |
2273 | && ioctl (pi->ctl_fd, PIOCTGFPREG, &thread_fpregs) >= 0) | |
c3f6f71d JM |
2274 | { |
2275 | memcpy (&pi->fpregset, &thread_fpregs.thread_1.pr_fpregs, | |
2276 | sizeof (pi->fpregset)); | |
2277 | pi->fpregs_valid = 1; | |
772cf8be | 2278 | return &pi->fpregset; /* Got 'em now! */ |
c3f6f71d JM |
2279 | } |
2280 | else | |
2281 | { | |
2282 | return NULL; | |
2283 | } | |
772cf8be | 2284 | # else |
c3f6f71d JM |
2285 | if (ioctl (pi->ctl_fd, PIOCGFPREG, &pi->fpregset) >= 0) |
2286 | { | |
2287 | pi->fpregs_valid = 1; | |
772cf8be | 2288 | return &pi->fpregset; /* Got 'em now! */ |
c3f6f71d JM |
2289 | } |
2290 | else | |
2291 | { | |
2292 | return NULL; | |
2293 | } | |
772cf8be | 2294 | # endif |
c906108c | 2295 | } |
c906108c | 2296 | } |
772cf8be | 2297 | #endif /* NEW_PROC_API */ |
c906108c SS |
2298 | } |
2299 | ||
772cf8be MK |
2300 | /* Write the general-purpose registers back to the process or LWP |
2301 | corresponding to PI. Return non-zero for success, zero for | |
2302 | failure. */ | |
c3f6f71d | 2303 | |
d3581e61 | 2304 | static int |
fba45db2 | 2305 | proc_set_gregs (procinfo *pi) |
c906108c | 2306 | { |
c3f6f71d JM |
2307 | gdb_gregset_t *gregs; |
2308 | int win; | |
c5aa993b | 2309 | |
772cf8be MK |
2310 | gregs = proc_get_gregs (pi); |
2311 | if (gregs == NULL) | |
2312 | return 0; /* proc_get_regs has already warned. */ | |
c3f6f71d | 2313 | |
772cf8be | 2314 | if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0) |
c906108c | 2315 | { |
c3f6f71d | 2316 | return 0; |
c906108c | 2317 | } |
c3f6f71d | 2318 | else |
c906108c | 2319 | { |
c3f6f71d JM |
2320 | #ifdef NEW_PROC_API |
2321 | struct { | |
37de36c6 | 2322 | procfs_ctl_t cmd; |
c3f6f71d JM |
2323 | /* Use char array to avoid alignment issues. */ |
2324 | char gregs[sizeof (gdb_gregset_t)]; | |
2325 | } arg; | |
2326 | ||
772cf8be | 2327 | arg.cmd = PCSREG; |
c3f6f71d JM |
2328 | memcpy (&arg.gregs, gregs, sizeof (arg.gregs)); |
2329 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); | |
2330 | #else | |
2331 | win = (ioctl (pi->ctl_fd, PIOCSREG, gregs) >= 0); | |
2332 | #endif | |
c906108c | 2333 | } |
c3f6f71d | 2334 | |
772cf8be | 2335 | /* Policy: writing the registers invalidates our cache. */ |
c3f6f71d JM |
2336 | pi->gregs_valid = 0; |
2337 | return win; | |
c906108c SS |
2338 | } |
2339 | ||
772cf8be MK |
2340 | /* Write the floating-pointer registers back to the process or LWP |
2341 | corresponding to PI. Return non-zero for success, zero for | |
2342 | failure. */ | |
c3f6f71d | 2343 | |
d3581e61 | 2344 | static int |
fba45db2 | 2345 | proc_set_fpregs (procinfo *pi) |
c906108c | 2346 | { |
c3f6f71d JM |
2347 | gdb_fpregset_t *fpregs; |
2348 | int win; | |
2349 | ||
772cf8be MK |
2350 | fpregs = proc_get_fpregs (pi); |
2351 | if (fpregs == NULL) | |
2352 | return 0; /* proc_get_fpregs has already warned. */ | |
c5aa993b | 2353 | |
772cf8be | 2354 | if (pi->ctl_fd == 0 && open_procinfo_files (pi, FD_CTL) == 0) |
c906108c | 2355 | { |
c3f6f71d | 2356 | return 0; |
c906108c | 2357 | } |
c3f6f71d | 2358 | else |
c906108c | 2359 | { |
c3f6f71d JM |
2360 | #ifdef NEW_PROC_API |
2361 | struct { | |
37de36c6 | 2362 | procfs_ctl_t cmd; |
c3f6f71d JM |
2363 | /* Use char array to avoid alignment issues. */ |
2364 | char fpregs[sizeof (gdb_fpregset_t)]; | |
2365 | } arg; | |
2366 | ||
772cf8be | 2367 | arg.cmd = PCSFPREG; |
c3f6f71d JM |
2368 | memcpy (&arg.fpregs, fpregs, sizeof (arg.fpregs)); |
2369 | win = (write (pi->ctl_fd, (void *) &arg, sizeof (arg)) == sizeof (arg)); | |
2370 | #else | |
772cf8be | 2371 | # ifdef PIOCTSFPREG |
c3f6f71d JM |
2372 | if (pi->tid == 0) |
2373 | win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0); | |
2374 | else | |
2375 | { | |
2376 | struct { | |
2377 | long pr_count; | |
2378 | tid_t pr_error_thread; | |
2379 | tfpregset_t thread_1; | |
2380 | } thread_fpregs; | |
2381 | ||
2382 | thread_fpregs.pr_count = 1; | |
2383 | thread_fpregs.thread_1.tid = pi->tid; | |
2384 | memcpy (&thread_fpregs.thread_1.pr_fpregs, fpregs, | |
2385 | sizeof (*fpregs)); | |
2386 | win = (ioctl (pi->ctl_fd, PIOCTSFPREG, &thread_fpregs) >= 0); | |
2387 | } | |
772cf8be | 2388 | # else |
c3f6f71d | 2389 | win = (ioctl (pi->ctl_fd, PIOCSFPREG, fpregs) >= 0); |
772cf8be MK |
2390 | # endif |
2391 | #endif /* NEW_PROC_API */ | |
c906108c | 2392 | } |
c3f6f71d | 2393 | |
772cf8be | 2394 | /* Policy: writing the registers invalidates our cache. */ |
c3f6f71d JM |
2395 | pi->fpregs_valid = 0; |
2396 | return win; | |
c906108c SS |
2397 | } |
2398 | ||
77382aee PA |
2399 | /* Send a signal to the proc or lwp with the semantics of "kill()". |
2400 | Returns non-zero for success, zero for failure. */ | |
c906108c | 2401 | |
d3581e61 | 2402 | static int |
fba45db2 | 2403 | proc_kill (procinfo *pi, int signo) |
c3f6f71d JM |
2404 | { |
2405 | int win; | |
c906108c | 2406 | |
77382aee PA |
2407 | /* We might conceivably apply this operation to an LWP, and the |
2408 | LWP's ctl file descriptor might not be open. */ | |
c906108c | 2409 | |
c3f6f71d JM |
2410 | if (pi->ctl_fd == 0 && |
2411 | open_procinfo_files (pi, FD_CTL) == 0) | |
2412 | { | |
2413 | return 0; | |
2414 | } | |
2415 | else | |
2416 | { | |
2417 | #ifdef NEW_PROC_API | |
37de36c6 | 2418 | procfs_ctl_t cmd[2]; |
c906108c | 2419 | |
c3f6f71d JM |
2420 | cmd[0] = PCKILL; |
2421 | cmd[1] = signo; | |
2422 | win = (write (pi->ctl_fd, (char *) &cmd, sizeof (cmd)) == sizeof (cmd)); | |
2423 | #else /* ioctl method */ | |
2424 | /* FIXME: do I need the Alpha OSF fixups present in | |
0df8b418 | 2425 | procfs.c/unconditionally_kill_inferior? Perhaps only for SIGKILL? */ |
c3f6f71d JM |
2426 | win = (ioctl (pi->ctl_fd, PIOCKILL, &signo) >= 0); |
2427 | #endif | |
2428 | } | |
c906108c | 2429 | |
c3f6f71d JM |
2430 | return win; |
2431 | } | |
c906108c | 2432 | |
77382aee PA |
2433 | /* Find the pid of the process that started this one. Returns the |
2434 | parent process pid, or zero. */ | |
c906108c | 2435 | |
d3581e61 | 2436 | static int |
fba45db2 | 2437 | proc_parent_pid (procinfo *pi) |
c906108c | 2438 | { |
77382aee PA |
2439 | /* We should never have to apply this operation to any procinfo |
2440 | except the one for the main process. If that ever changes for | |
2441 | any reason, then take out the following clause and replace it | |
2442 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2443 | |
c3f6f71d JM |
2444 | if (pi->tid != 0) |
2445 | pi = find_procinfo_or_die (pi->pid, 0); | |
2446 | ||
2447 | if (!pi->status_valid) | |
2448 | if (!proc_get_status (pi)) | |
2449 | return 0; | |
c5aa993b | 2450 | |
c3f6f71d JM |
2451 | return pi->prstatus.pr_ppid; |
2452 | } | |
2453 | ||
9a043c1d AC |
2454 | /* Convert a target address (a.k.a. CORE_ADDR) into a host address |
2455 | (a.k.a void pointer)! */ | |
2456 | ||
1ca8fce0 JB |
2457 | #if (defined (PCWATCH) || defined (PIOCSWATCH)) \ |
2458 | && !(defined (PIOCOPENLWP) || defined (UNIXWARE)) | |
9a043c1d AC |
2459 | static void * |
2460 | procfs_address_to_host_pointer (CORE_ADDR addr) | |
2461 | { | |
4e906f53 | 2462 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
9a043c1d AC |
2463 | void *ptr; |
2464 | ||
4e906f53 | 2465 | gdb_assert (sizeof (ptr) == TYPE_LENGTH (ptr_type)); |
0b62613e PA |
2466 | gdbarch_address_to_pointer (target_gdbarch, ptr_type, |
2467 | (gdb_byte *) &ptr, addr); | |
9a043c1d AC |
2468 | return ptr; |
2469 | } | |
1ca8fce0 | 2470 | #endif |
9a043c1d | 2471 | |
a0911fd0 | 2472 | static int |
fba45db2 | 2473 | proc_set_watchpoint (procinfo *pi, CORE_ADDR addr, int len, int wflags) |
c3f6f71d | 2474 | { |
d1a7880c PA |
2475 | #if !defined (PCWATCH) && !defined (PIOCSWATCH) |
2476 | /* If neither or these is defined, we can't support watchpoints. | |
2477 | This just avoids possibly failing to compile the below on such | |
2478 | systems. */ | |
c3f6f71d JM |
2479 | return 0; |
2480 | #else | |
0df8b418 MS |
2481 | /* Horrible hack! Detect Solaris 2.5, because this doesn't work on 2.5. */ |
2482 | #if defined (PIOCOPENLWP) || defined (UNIXWARE) /* Solaris 2.5: bail out. */ | |
c3f6f71d JM |
2483 | return 0; |
2484 | #else | |
2485 | struct { | |
37de36c6 | 2486 | procfs_ctl_t cmd; |
c3f6f71d JM |
2487 | char watch[sizeof (prwatch_t)]; |
2488 | } arg; | |
73930d4d | 2489 | prwatch_t pwatch; |
c3f6f71d | 2490 | |
9a043c1d AC |
2491 | /* NOTE: cagney/2003-02-01: Even more horrible hack. Need to |
2492 | convert a target address into something that can be stored in a | |
2493 | native data structure. */ | |
831e682e | 2494 | #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */ |
73930d4d | 2495 | pwatch.pr_vaddr = (uintptr_t) procfs_address_to_host_pointer (addr); |
831e682e | 2496 | #else |
73930d4d | 2497 | pwatch.pr_vaddr = (caddr_t) procfs_address_to_host_pointer (addr); |
831e682e | 2498 | #endif |
73930d4d KH |
2499 | pwatch.pr_size = len; |
2500 | pwatch.pr_wflags = wflags; | |
c3f6f71d JM |
2501 | #if defined(NEW_PROC_API) && defined (PCWATCH) |
2502 | arg.cmd = PCWATCH; | |
73930d4d | 2503 | memcpy (arg.watch, &pwatch, sizeof (prwatch_t)); |
c3f6f71d JM |
2504 | return (write (pi->ctl_fd, &arg, sizeof (arg)) == sizeof (arg)); |
2505 | #else | |
2506 | #if defined (PIOCSWATCH) | |
73930d4d | 2507 | return (ioctl (pi->ctl_fd, PIOCSWATCH, &pwatch) >= 0); |
c3f6f71d JM |
2508 | #else |
2509 | return 0; /* Fail */ | |
2510 | #endif | |
2511 | #endif | |
2512 | #endif | |
2513 | #endif | |
c906108c SS |
2514 | } |
2515 | ||
965b60ee | 2516 | #if (defined(__i386__) || defined(__x86_64__)) && defined (sun) |
c906108c | 2517 | |
c3f6f71d | 2518 | #include <sys/sysi86.h> |
c906108c | 2519 | |
77382aee PA |
2520 | /* The KEY is actually the value of the lower 16 bits of the GS |
2521 | register for the LWP that we're interested in. Returns the | |
2522 | matching ssh struct (LDT entry). */ | |
c906108c | 2523 | |
c3f6f71d | 2524 | struct ssd * |
fba45db2 | 2525 | proc_get_LDT_entry (procinfo *pi, int key) |
c906108c | 2526 | { |
c3f6f71d JM |
2527 | static struct ssd *ldt_entry = NULL; |
2528 | #ifdef NEW_PROC_API | |
2529 | char pathname[MAX_PROC_NAME_SIZE]; | |
2530 | struct cleanup *old_chain = NULL; | |
2531 | int fd; | |
2532 | ||
2533 | /* Allocate space for one LDT entry. | |
2534 | This alloc must persist, because we return a pointer to it. */ | |
2535 | if (ldt_entry == NULL) | |
2536 | ldt_entry = (struct ssd *) xmalloc (sizeof (struct ssd)); | |
2537 | ||
2538 | /* Open the file descriptor for the LDT table. */ | |
2539 | sprintf (pathname, "/proc/%d/ldt", pi->pid); | |
4d1bcd09 | 2540 | if ((fd = open_with_retry (pathname, O_RDONLY)) < 0) |
c906108c | 2541 | { |
c3f6f71d JM |
2542 | proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__); |
2543 | return NULL; | |
c906108c | 2544 | } |
0df8b418 | 2545 | /* Make sure it gets closed again! */ |
004527cb | 2546 | old_chain = make_cleanup_close (fd); |
c906108c | 2547 | |
c3f6f71d JM |
2548 | /* Now 'read' thru the table, find a match and return it. */ |
2549 | while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd)) | |
c906108c | 2550 | { |
c3f6f71d JM |
2551 | if (ldt_entry->sel == 0 && |
2552 | ldt_entry->bo == 0 && | |
2553 | ldt_entry->acc1 == 0 && | |
2554 | ldt_entry->acc2 == 0) | |
2555 | break; /* end of table */ | |
0df8b418 | 2556 | /* If key matches, return this entry. */ |
c3f6f71d JM |
2557 | if (ldt_entry->sel == key) |
2558 | return ldt_entry; | |
c906108c | 2559 | } |
0df8b418 | 2560 | /* Loop ended, match not found. */ |
c3f6f71d JM |
2561 | return NULL; |
2562 | #else | |
2563 | int nldt, i; | |
2564 | static int nalloc = 0; | |
c906108c | 2565 | |
c3f6f71d JM |
2566 | /* Get the number of LDT entries. */ |
2567 | if (ioctl (pi->ctl_fd, PIOCNLDT, &nldt) < 0) | |
c906108c | 2568 | { |
c3f6f71d JM |
2569 | proc_warn (pi, "proc_get_LDT_entry (PIOCNLDT)", __LINE__); |
2570 | return NULL; | |
c906108c SS |
2571 | } |
2572 | ||
0df8b418 MS |
2573 | /* Allocate space for the number of LDT entries. */ |
2574 | /* This alloc has to persist, 'cause we return a pointer to it. */ | |
c3f6f71d | 2575 | if (nldt > nalloc) |
c906108c | 2576 | { |
19958708 | 2577 | ldt_entry = (struct ssd *) |
c3f6f71d JM |
2578 | xrealloc (ldt_entry, (nldt + 1) * sizeof (struct ssd)); |
2579 | nalloc = nldt; | |
2580 | } | |
19958708 | 2581 | |
c3f6f71d JM |
2582 | /* Read the whole table in one gulp. */ |
2583 | if (ioctl (pi->ctl_fd, PIOCLDT, ldt_entry) < 0) | |
2584 | { | |
2585 | proc_warn (pi, "proc_get_LDT_entry (PIOCLDT)", __LINE__); | |
2586 | return NULL; | |
c906108c SS |
2587 | } |
2588 | ||
0df8b418 | 2589 | /* Search the table and return the (first) entry matching 'key'. */ |
c3f6f71d JM |
2590 | for (i = 0; i < nldt; i++) |
2591 | if (ldt_entry[i].sel == key) | |
2592 | return &ldt_entry[i]; | |
c906108c | 2593 | |
0df8b418 | 2594 | /* Loop ended, match not found. */ |
c3f6f71d JM |
2595 | return NULL; |
2596 | #endif | |
2597 | } | |
c906108c | 2598 | |
77382aee | 2599 | /* Returns the pointer to the LDT entry of PTID. */ |
965b60ee JB |
2600 | |
2601 | struct ssd * | |
2602 | procfs_find_LDT_entry (ptid_t ptid) | |
2603 | { | |
2604 | gdb_gregset_t *gregs; | |
2605 | int key; | |
2606 | procinfo *pi; | |
2607 | ||
0df8b418 | 2608 | /* Find procinfo for the lwp. */ |
965b60ee JB |
2609 | if ((pi = find_procinfo (PIDGET (ptid), TIDGET (ptid))) == NULL) |
2610 | { | |
0b62613e | 2611 | warning (_("procfs_find_LDT_entry: could not find procinfo for %d:%ld."), |
965b60ee JB |
2612 | PIDGET (ptid), TIDGET (ptid)); |
2613 | return NULL; | |
2614 | } | |
0df8b418 | 2615 | /* get its general registers. */ |
965b60ee JB |
2616 | if ((gregs = proc_get_gregs (pi)) == NULL) |
2617 | { | |
0b62613e | 2618 | warning (_("procfs_find_LDT_entry: could not read gregs for %d:%ld."), |
965b60ee JB |
2619 | PIDGET (ptid), TIDGET (ptid)); |
2620 | return NULL; | |
2621 | } | |
0df8b418 | 2622 | /* Now extract the GS register's lower 16 bits. */ |
965b60ee JB |
2623 | key = (*gregs)[GS] & 0xffff; |
2624 | ||
0df8b418 | 2625 | /* Find the matching entry and return it. */ |
965b60ee JB |
2626 | return proc_get_LDT_entry (pi, key); |
2627 | } | |
2628 | ||
2629 | #endif | |
c906108c | 2630 | |
c3f6f71d | 2631 | /* =============== END, non-thread part of /proc "MODULE" =============== */ |
c906108c | 2632 | |
c3f6f71d | 2633 | /* =================== Thread "MODULE" =================== */ |
c906108c | 2634 | |
c3f6f71d JM |
2635 | /* NOTE: you'll see more ifdefs and duplication of functions here, |
2636 | since there is a different way to do threads on every OS. */ | |
c906108c | 2637 | |
77382aee | 2638 | /* Returns the number of threads for the process. */ |
c906108c | 2639 | |
c3f6f71d | 2640 | #if defined (PIOCNTHR) && defined (PIOCTLIST) |
77382aee | 2641 | /* OSF version */ |
d3581e61 | 2642 | static int |
fba45db2 | 2643 | proc_get_nthreads (procinfo *pi) |
c3f6f71d JM |
2644 | { |
2645 | int nthreads = 0; | |
c906108c | 2646 | |
c3f6f71d JM |
2647 | if (ioctl (pi->ctl_fd, PIOCNTHR, &nthreads) < 0) |
2648 | proc_warn (pi, "procfs: PIOCNTHR failed", __LINE__); | |
c906108c | 2649 | |
c3f6f71d | 2650 | return nthreads; |
c906108c SS |
2651 | } |
2652 | ||
c3f6f71d JM |
2653 | #else |
2654 | #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */ | |
77382aee | 2655 | /* Solaris and Unixware version */ |
d3581e61 | 2656 | static int |
fba45db2 | 2657 | proc_get_nthreads (procinfo *pi) |
c906108c | 2658 | { |
c3f6f71d JM |
2659 | if (!pi->status_valid) |
2660 | if (!proc_get_status (pi)) | |
2661 | return 0; | |
c5aa993b | 2662 | |
77382aee PA |
2663 | /* NEW_PROC_API: only works for the process procinfo, because the |
2664 | LWP procinfos do not get prstatus filled in. */ | |
19958708 | 2665 | #ifdef NEW_PROC_API |
0df8b418 | 2666 | if (pi->tid != 0) /* Find the parent process procinfo. */ |
c3f6f71d | 2667 | pi = find_procinfo_or_die (pi->pid, 0); |
c5aa993b | 2668 | #endif |
c3f6f71d | 2669 | return pi->prstatus.pr_nlwp; |
c906108c SS |
2670 | } |
2671 | ||
c3f6f71d | 2672 | #else |
77382aee | 2673 | /* Default version */ |
d3581e61 | 2674 | static int |
fba45db2 | 2675 | proc_get_nthreads (procinfo *pi) |
c906108c | 2676 | { |
c3f6f71d JM |
2677 | return 0; |
2678 | } | |
2679 | #endif | |
2680 | #endif | |
2681 | ||
77382aee PA |
2682 | /* LWP version. |
2683 | ||
2684 | Return the ID of the thread that had an event of interest. | |
2685 | (ie. the one that hit a breakpoint or other traced event). All | |
2686 | other things being equal, this should be the ID of a thread that is | |
2687 | currently executing. */ | |
c3f6f71d JM |
2688 | |
2689 | #if defined (SYS_lwpcreate) || defined (SYS_lwp_create) /* FIXME: multiple */ | |
77382aee | 2690 | /* Solaris and Unixware version */ |
d3581e61 | 2691 | static int |
fba45db2 | 2692 | proc_get_current_thread (procinfo *pi) |
c3f6f71d | 2693 | { |
77382aee PA |
2694 | /* Note: this should be applied to the root procinfo for the |
2695 | process, not to the procinfo for an LWP. If applied to the | |
2696 | procinfo for an LWP, it will simply return that LWP's ID. In | |
2697 | that case, find the parent process procinfo. */ | |
19958708 | 2698 | |
c3f6f71d JM |
2699 | if (pi->tid != 0) |
2700 | pi = find_procinfo_or_die (pi->pid, 0); | |
2701 | ||
2702 | if (!pi->status_valid) | |
2703 | if (!proc_get_status (pi)) | |
2704 | return 0; | |
2705 | ||
2706 | #ifdef NEW_PROC_API | |
2707 | return pi->prstatus.pr_lwp.pr_lwpid; | |
c906108c | 2708 | #else |
c3f6f71d | 2709 | return pi->prstatus.pr_who; |
c906108c | 2710 | #endif |
c3f6f71d | 2711 | } |
c906108c | 2712 | |
c3f6f71d JM |
2713 | #else |
2714 | #if defined (PIOCNTHR) && defined (PIOCTLIST) | |
77382aee | 2715 | /* OSF version */ |
d3581e61 | 2716 | static int |
fba45db2 | 2717 | proc_get_current_thread (procinfo *pi) |
c3f6f71d | 2718 | { |
0df8b418 | 2719 | #if 0 /* FIXME: not ready for prime time? */ |
c3f6f71d JM |
2720 | return pi->prstatus.pr_tid; |
2721 | #else | |
2722 | return 0; | |
2723 | #endif | |
c906108c SS |
2724 | } |
2725 | ||
c3f6f71d | 2726 | #else |
77382aee | 2727 | /* Default version */ |
d3581e61 | 2728 | static int |
fba45db2 | 2729 | proc_get_current_thread (procinfo *pi) |
c906108c | 2730 | { |
c3f6f71d JM |
2731 | return 0; |
2732 | } | |
2733 | ||
2734 | #endif | |
2735 | #endif | |
c906108c | 2736 | |
77382aee PA |
2737 | /* Discover the IDs of all the threads within the process, and create |
2738 | a procinfo for each of them (chained to the parent). This | |
2739 | unfortunately requires a different method on every OS. Returns | |
2740 | non-zero for success, zero for failure. */ | |
c906108c | 2741 | |
a0911fd0 | 2742 | static int |
fba45db2 | 2743 | proc_delete_dead_threads (procinfo *parent, procinfo *thread, void *ignore) |
c3f6f71d JM |
2744 | { |
2745 | if (thread && parent) /* sanity */ | |
c906108c | 2746 | { |
c3f6f71d JM |
2747 | thread->status_valid = 0; |
2748 | if (!proc_get_status (thread)) | |
2749 | destroy_one_procinfo (&parent->thread_list, thread); | |
2750 | } | |
2751 | return 0; /* keep iterating */ | |
2752 | } | |
c5aa993b | 2753 | |
c3f6f71d | 2754 | #if defined (PIOCLSTATUS) |
77382aee | 2755 | /* Solaris 2.5 (ioctl) version */ |
d3581e61 | 2756 | static int |
fba45db2 | 2757 | proc_update_threads (procinfo *pi) |
c3f6f71d JM |
2758 | { |
2759 | gdb_prstatus_t *prstatus; | |
2760 | struct cleanup *old_chain = NULL; | |
2761 | procinfo *thread; | |
2762 | int nlwp, i; | |
2763 | ||
77382aee PA |
2764 | /* We should never have to apply this operation to any procinfo |
2765 | except the one for the main process. If that ever changes for | |
2766 | any reason, then take out the following clause and replace it | |
2767 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2768 | |
c3f6f71d JM |
2769 | if (pi->tid != 0) |
2770 | pi = find_procinfo_or_die (pi->pid, 0); | |
2771 | ||
2772 | proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); | |
2773 | ||
2774 | if ((nlwp = proc_get_nthreads (pi)) <= 1) | |
2775 | return 1; /* Process is not multi-threaded; nothing to do. */ | |
2776 | ||
3c37485b | 2777 | prstatus = xmalloc (sizeof (gdb_prstatus_t) * (nlwp + 1)); |
c3f6f71d | 2778 | |
b8c9b27d | 2779 | old_chain = make_cleanup (xfree, prstatus); |
c3f6f71d JM |
2780 | if (ioctl (pi->ctl_fd, PIOCLSTATUS, prstatus) < 0) |
2781 | proc_error (pi, "update_threads (PIOCLSTATUS)", __LINE__); | |
2782 | ||
77382aee | 2783 | /* Skip element zero, which represents the process as a whole. */ |
c3f6f71d JM |
2784 | for (i = 1; i < nlwp + 1; i++) |
2785 | { | |
2786 | if ((thread = create_procinfo (pi->pid, prstatus[i].pr_who)) == NULL) | |
2787 | proc_error (pi, "update_threads, create_procinfo", __LINE__); | |
c5aa993b | 2788 | |
c3f6f71d JM |
2789 | memcpy (&thread->prstatus, &prstatus[i], sizeof (*prstatus)); |
2790 | thread->status_valid = 1; | |
2791 | } | |
2792 | pi->threads_valid = 1; | |
2793 | do_cleanups (old_chain); | |
2794 | return 1; | |
2795 | } | |
2796 | #else | |
2797 | #ifdef NEW_PROC_API | |
0df8b418 | 2798 | /* Unixware and Solaris 6 (and later) version. */ |
004527cb AC |
2799 | static void |
2800 | do_closedir_cleanup (void *dir) | |
2801 | { | |
2802 | closedir (dir); | |
2803 | } | |
2804 | ||
d3581e61 | 2805 | static int |
fba45db2 | 2806 | proc_update_threads (procinfo *pi) |
c3f6f71d JM |
2807 | { |
2808 | char pathname[MAX_PROC_NAME_SIZE + 16]; | |
2809 | struct dirent *direntry; | |
2810 | struct cleanup *old_chain = NULL; | |
2811 | procinfo *thread; | |
2812 | DIR *dirp; | |
2813 | int lwpid; | |
2814 | ||
77382aee PA |
2815 | /* We should never have to apply this operation to any procinfo |
2816 | except the one for the main process. If that ever changes for | |
2817 | any reason, then take out the following clause and replace it | |
2818 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2819 | |
c3f6f71d JM |
2820 | if (pi->tid != 0) |
2821 | pi = find_procinfo_or_die (pi->pid, 0); | |
2822 | ||
2823 | proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); | |
2824 | ||
77382aee PA |
2825 | /* Unixware |
2826 | ||
2827 | Note: this brute-force method is the only way I know of to | |
2828 | accomplish this task on Unixware. This method will also work on | |
2829 | Solaris 2.6 and 2.7. There is a much simpler and more elegant | |
2830 | way to do this on Solaris, but the margins of this manuscript are | |
2831 | too small to write it here... ;-) */ | |
c3f6f71d JM |
2832 | |
2833 | strcpy (pathname, pi->pathname); | |
2834 | strcat (pathname, "/lwp"); | |
2835 | if ((dirp = opendir (pathname)) == NULL) | |
2836 | proc_error (pi, "update_threads, opendir", __LINE__); | |
2837 | ||
004527cb | 2838 | old_chain = make_cleanup (do_closedir_cleanup, dirp); |
c3f6f71d JM |
2839 | while ((direntry = readdir (dirp)) != NULL) |
2840 | if (direntry->d_name[0] != '.') /* skip '.' and '..' */ | |
2841 | { | |
2842 | lwpid = atoi (&direntry->d_name[0]); | |
2843 | if ((thread = create_procinfo (pi->pid, lwpid)) == NULL) | |
2844 | proc_error (pi, "update_threads, create_procinfo", __LINE__); | |
2845 | } | |
2846 | pi->threads_valid = 1; | |
2847 | do_cleanups (old_chain); | |
2848 | return 1; | |
2849 | } | |
2850 | #else | |
2851 | #ifdef PIOCTLIST | |
77382aee | 2852 | /* OSF version */ |
d3581e61 | 2853 | static int |
fba45db2 | 2854 | proc_update_threads (procinfo *pi) |
c3f6f71d JM |
2855 | { |
2856 | int nthreads, i; | |
2857 | tid_t *threads; | |
2858 | ||
77382aee PA |
2859 | /* We should never have to apply this operation to any procinfo |
2860 | except the one for the main process. If that ever changes for | |
2861 | any reason, then take out the following clause and replace it | |
2862 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2863 | |
c3f6f71d JM |
2864 | if (pi->tid != 0) |
2865 | pi = find_procinfo_or_die (pi->pid, 0); | |
2866 | ||
2867 | proc_iterate_over_threads (pi, proc_delete_dead_threads, NULL); | |
2868 | ||
2869 | nthreads = proc_get_nthreads (pi); | |
2870 | if (nthreads < 2) | |
0df8b418 | 2871 | return 0; /* Nothing to do for 1 or fewer threads. */ |
c3f6f71d | 2872 | |
3c37485b | 2873 | threads = xmalloc (nthreads * sizeof (tid_t)); |
19958708 | 2874 | |
c3f6f71d JM |
2875 | if (ioctl (pi->ctl_fd, PIOCTLIST, threads) < 0) |
2876 | proc_error (pi, "procfs: update_threads (PIOCTLIST)", __LINE__); | |
2877 | ||
2878 | for (i = 0; i < nthreads; i++) | |
2879 | { | |
2880 | if (!find_procinfo (pi->pid, threads[i])) | |
2881 | if (!create_procinfo (pi->pid, threads[i])) | |
2882 | proc_error (pi, "update_threads, create_procinfo", __LINE__); | |
c906108c | 2883 | } |
c3f6f71d JM |
2884 | pi->threads_valid = 1; |
2885 | return 1; | |
c906108c | 2886 | } |
c3f6f71d | 2887 | #else |
77382aee | 2888 | /* Default version */ |
d3581e61 | 2889 | static int |
fba45db2 | 2890 | proc_update_threads (procinfo *pi) |
c3f6f71d JM |
2891 | { |
2892 | return 0; | |
2893 | } | |
2894 | #endif /* OSF PIOCTLIST */ | |
2895 | #endif /* NEW_PROC_API */ | |
2896 | #endif /* SOL 2.5 PIOCLSTATUS */ | |
c906108c | 2897 | |
77382aee PA |
2898 | /* Given a pointer to a function, call that function once for each lwp |
2899 | in the procinfo list, until the function returns non-zero, in which | |
2900 | event return the value returned by the function. | |
2901 | ||
2902 | Note: this function does NOT call update_threads. If you want to | |
2903 | discover new threads first, you must call that function explicitly. | |
2904 | This function just makes a quick pass over the currently-known | |
2905 | procinfos. | |
2906 | ||
2907 | PI is the parent process procinfo. FUNC is the per-thread | |
2908 | function. PTR is an opaque parameter for function. Returns the | |
2909 | first non-zero return value from the callee, or zero. */ | |
c3f6f71d | 2910 | |
d3581e61 | 2911 | static int |
d0849a9a KB |
2912 | proc_iterate_over_threads (procinfo *pi, |
2913 | int (*func) (procinfo *, procinfo *, void *), | |
2914 | void *ptr) | |
c906108c | 2915 | { |
c3f6f71d JM |
2916 | procinfo *thread, *next; |
2917 | int retval = 0; | |
c906108c | 2918 | |
77382aee PA |
2919 | /* We should never have to apply this operation to any procinfo |
2920 | except the one for the main process. If that ever changes for | |
2921 | any reason, then take out the following clause and replace it | |
2922 | with one that makes sure the ctl_fd is open. */ | |
19958708 | 2923 | |
c3f6f71d JM |
2924 | if (pi->tid != 0) |
2925 | pi = find_procinfo_or_die (pi->pid, 0); | |
2926 | ||
2927 | for (thread = pi->thread_list; thread != NULL; thread = next) | |
c906108c | 2928 | { |
0df8b418 | 2929 | next = thread->next; /* In case thread is destroyed. */ |
c3f6f71d JM |
2930 | if ((retval = (*func) (pi, thread, ptr)) != 0) |
2931 | break; | |
c906108c | 2932 | } |
c3f6f71d JM |
2933 | |
2934 | return retval; | |
c906108c SS |
2935 | } |
2936 | ||
c3f6f71d JM |
2937 | /* =================== END, Thread "MODULE" =================== */ |
2938 | ||
2939 | /* =================== END, /proc "MODULE" =================== */ | |
2940 | ||
2941 | /* =================== GDB "MODULE" =================== */ | |
2942 | ||
77382aee PA |
2943 | /* Here are all of the gdb target vector functions and their |
2944 | friends. */ | |
c3f6f71d | 2945 | |
39f77062 | 2946 | static ptid_t do_attach (ptid_t ptid); |
a14ed312 | 2947 | static void do_detach (int signo); |
9185ddce | 2948 | static void proc_trace_syscalls_1 (procinfo *pi, int syscallnum, |
77382aee | 2949 | int entry_or_exit, int mode, int from_tty); |
9185ddce JB |
2950 | |
2951 | /* On mips-irix, we need to insert a breakpoint at __dbx_link during | |
2952 | the startup phase. The following two variables are used to record | |
2953 | the address of the breakpoint, and the code that was replaced by | |
2954 | a breakpoint. */ | |
2955 | static int dbx_link_bpt_addr = 0; | |
8181d85f | 2956 | static void *dbx_link_bpt; |
c3f6f71d | 2957 | |
77382aee PA |
2958 | /* Sets up the inferior to be debugged. Registers to trace signals, |
2959 | hardware faults, and syscalls. Note: does not set RLC flag: caller | |
2960 | may want to customize that. Returns zero for success (note! | |
2961 | unlike most functions in this module); on failure, returns the LINE | |
2962 | NUMBER where it failed! */ | |
c3f6f71d JM |
2963 | |
2964 | static int | |
fba45db2 | 2965 | procfs_debug_inferior (procinfo *pi) |
c906108c | 2966 | { |
c3f6f71d | 2967 | fltset_t traced_faults; |
37de36c6 KB |
2968 | gdb_sigset_t traced_signals; |
2969 | sysset_t *traced_syscall_entries; | |
2970 | sysset_t *traced_syscall_exits; | |
2971 | int status; | |
c906108c | 2972 | |
c3f6f71d JM |
2973 | #ifdef PROCFS_DONT_TRACE_FAULTS |
2974 | /* On some systems (OSF), we don't trace hardware faults. | |
2975 | Apparently it's enough that we catch them as signals. | |
0df8b418 MS |
2976 | Wonder why we don't just do that in general? */ |
2977 | premptyset (&traced_faults); /* don't trace faults. */ | |
c3f6f71d | 2978 | #else |
0df8b418 MS |
2979 | /* Register to trace hardware faults in the child. */ |
2980 | prfillset (&traced_faults); /* trace all faults... */ | |
3c95f01c | 2981 | gdb_prdelset (&traced_faults, FLTPAGE); /* except page fault. */ |
c3f6f71d JM |
2982 | #endif |
2983 | if (!proc_set_traced_faults (pi, &traced_faults)) | |
2984 | return __LINE__; | |
c906108c | 2985 | |
2455069d UW |
2986 | /* Initially, register to trace all signals in the child. */ |
2987 | prfillset (&traced_signals); | |
2988 | if (!proc_set_traced_signals (pi, &traced_signals)) | |
c3f6f71d JM |
2989 | return __LINE__; |
2990 | ||
37de36c6 | 2991 | |
c3f6f71d | 2992 | /* Register to trace the 'exit' system call (on entry). */ |
37de36c6 KB |
2993 | traced_syscall_entries = sysset_t_alloc (pi); |
2994 | gdb_premptysysset (traced_syscall_entries); | |
2995 | #ifdef SYS_exit | |
2996 | gdb_praddsysset (traced_syscall_entries, SYS_exit); | |
2997 | #endif | |
c3f6f71d | 2998 | #ifdef SYS_lwpexit |
0df8b418 | 2999 | gdb_praddsysset (traced_syscall_entries, SYS_lwpexit);/* And _lwp_exit... */ |
c3f6f71d JM |
3000 | #endif |
3001 | #ifdef SYS_lwp_exit | |
37de36c6 KB |
3002 | gdb_praddsysset (traced_syscall_entries, SYS_lwp_exit); |
3003 | #endif | |
3004 | #ifdef DYNAMIC_SYSCALLS | |
3005 | { | |
3006 | int callnum = find_syscall (pi, "_exit"); | |
f4a14ae6 | 3007 | |
37de36c6 KB |
3008 | if (callnum >= 0) |
3009 | gdb_praddsysset (traced_syscall_entries, callnum); | |
3010 | } | |
c906108c SS |
3011 | #endif |
3012 | ||
37de36c6 KB |
3013 | status = proc_set_traced_sysentry (pi, traced_syscall_entries); |
3014 | xfree (traced_syscall_entries); | |
3015 | if (!status) | |
c3f6f71d JM |
3016 | return __LINE__; |
3017 | ||
3018 | #ifdef PRFS_STOPEXEC /* defined on OSF */ | |
3019 | /* OSF method for tracing exec syscalls. Quoting: | |
3020 | Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace | |
3021 | exits from exec system calls because of the user level loader. */ | |
0df8b418 | 3022 | /* FIXME: make nice and maybe move into an access function. */ |
c3f6f71d JM |
3023 | { |
3024 | int prfs_flags; | |
3025 | ||
3026 | if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0) | |
3027 | return __LINE__; | |
3028 | ||
3029 | prfs_flags |= PRFS_STOPEXEC; | |
3030 | ||
3031 | if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0) | |
3032 | return __LINE__; | |
3033 | } | |
3034 | #else /* not PRFS_STOPEXEC */ | |
0df8b418 | 3035 | /* Everyone else's (except OSF) method for tracing exec syscalls. */ |
c3f6f71d JM |
3036 | /* GW: Rationale... |
3037 | Not all systems with /proc have all the exec* syscalls with the same | |
3038 | names. On the SGI, for example, there is no SYS_exec, but there | |
0df8b418 | 3039 | *is* a SYS_execv. So, we try to account for that. */ |
c3f6f71d | 3040 | |
37de36c6 KB |
3041 | traced_syscall_exits = sysset_t_alloc (pi); |
3042 | gdb_premptysysset (traced_syscall_exits); | |
c3f6f71d | 3043 | #ifdef SYS_exec |
37de36c6 | 3044 | gdb_praddsysset (traced_syscall_exits, SYS_exec); |
c3f6f71d JM |
3045 | #endif |
3046 | #ifdef SYS_execve | |
37de36c6 | 3047 | gdb_praddsysset (traced_syscall_exits, SYS_execve); |
c3f6f71d JM |
3048 | #endif |
3049 | #ifdef SYS_execv | |
37de36c6 | 3050 | gdb_praddsysset (traced_syscall_exits, SYS_execv); |
c3f6f71d | 3051 | #endif |
c5aa993b | 3052 | |
c3f6f71d | 3053 | #ifdef SYS_lwpcreate |
37de36c6 KB |
3054 | gdb_praddsysset (traced_syscall_exits, SYS_lwpcreate); |
3055 | gdb_praddsysset (traced_syscall_exits, SYS_lwpexit); | |
c906108c | 3056 | #endif |
c5aa993b | 3057 | |
0df8b418 | 3058 | #ifdef SYS_lwp_create /* FIXME: once only, please. */ |
37de36c6 KB |
3059 | gdb_praddsysset (traced_syscall_exits, SYS_lwp_create); |
3060 | gdb_praddsysset (traced_syscall_exits, SYS_lwp_exit); | |
c3f6f71d | 3061 | #endif |
c5aa993b | 3062 | |
37de36c6 KB |
3063 | #ifdef DYNAMIC_SYSCALLS |
3064 | { | |
3065 | int callnum = find_syscall (pi, "execve"); | |
f4a14ae6 | 3066 | |
37de36c6 KB |
3067 | if (callnum >= 0) |
3068 | gdb_praddsysset (traced_syscall_exits, callnum); | |
3069 | callnum = find_syscall (pi, "ra_execve"); | |
3070 | if (callnum >= 0) | |
3071 | gdb_praddsysset (traced_syscall_exits, callnum); | |
3072 | } | |
3073 | #endif | |
c906108c | 3074 | |
37de36c6 KB |
3075 | status = proc_set_traced_sysexit (pi, traced_syscall_exits); |
3076 | xfree (traced_syscall_exits); | |
3077 | if (!status) | |
c3f6f71d JM |
3078 | return __LINE__; |
3079 | ||
3080 | #endif /* PRFS_STOPEXEC */ | |
3081 | return 0; | |
c906108c SS |
3082 | } |
3083 | ||
19958708 | 3084 | static void |
136d6dae | 3085 | procfs_attach (struct target_ops *ops, char *args, int from_tty) |
c906108c | 3086 | { |
c3f6f71d JM |
3087 | char *exec_file; |
3088 | int pid; | |
3089 | ||
74164c56 | 3090 | pid = parse_pid_to_attach (args); |
c3f6f71d | 3091 | |
c3f6f71d | 3092 | if (pid == getpid ()) |
8a3fe4f8 | 3093 | error (_("Attaching GDB to itself is not a good idea...")); |
c906108c | 3094 | |
c3f6f71d | 3095 | if (from_tty) |
c906108c | 3096 | { |
c3f6f71d JM |
3097 | exec_file = get_exec_file (0); |
3098 | ||
3099 | if (exec_file) | |
a3f17187 | 3100 | printf_filtered (_("Attaching to program `%s', %s\n"), |
39f77062 | 3101 | exec_file, target_pid_to_str (pid_to_ptid (pid))); |
c3f6f71d | 3102 | else |
a3f17187 | 3103 | printf_filtered (_("Attaching to %s\n"), |
77382aee | 3104 | target_pid_to_str (pid_to_ptid (pid))); |
c3f6f71d JM |
3105 | |
3106 | fflush (stdout); | |
c906108c | 3107 | } |
39f77062 | 3108 | inferior_ptid = do_attach (pid_to_ptid (pid)); |
28439f5e | 3109 | push_target (ops); |
c3f6f71d JM |
3110 | } |
3111 | ||
19958708 | 3112 | static void |
136d6dae | 3113 | procfs_detach (struct target_ops *ops, char *args, int from_tty) |
c3f6f71d | 3114 | { |
cc377e6b | 3115 | int sig = 0; |
7f9f62ba | 3116 | int pid = PIDGET (inferior_ptid); |
cc377e6b MK |
3117 | |
3118 | if (args) | |
3119 | sig = atoi (args); | |
c3f6f71d JM |
3120 | |
3121 | if (from_tty) | |
c906108c | 3122 | { |
cc377e6b MK |
3123 | char *exec_file; |
3124 | ||
c3f6f71d | 3125 | exec_file = get_exec_file (0); |
cc377e6b | 3126 | if (exec_file == NULL) |
c3f6f71d | 3127 | exec_file = ""; |
cc377e6b | 3128 | |
a3f17187 | 3129 | printf_filtered (_("Detaching from program: %s, %s\n"), exec_file, |
cc377e6b MK |
3130 | target_pid_to_str (pid_to_ptid (pid))); |
3131 | gdb_flush (gdb_stdout); | |
c906108c | 3132 | } |
19958708 | 3133 | |
cc377e6b MK |
3134 | do_detach (sig); |
3135 | ||
39f77062 | 3136 | inferior_ptid = null_ptid; |
7f9f62ba | 3137 | detach_inferior (pid); |
28439f5e | 3138 | unpush_target (ops); |
c906108c SS |
3139 | } |
3140 | ||
39f77062 KB |
3141 | static ptid_t |
3142 | do_attach (ptid_t ptid) | |
c906108c | 3143 | { |
c3f6f71d | 3144 | procinfo *pi; |
181e7f93 | 3145 | struct inferior *inf; |
c3f6f71d | 3146 | int fail; |
2689673f | 3147 | int lwpid; |
c3f6f71d | 3148 | |
39f77062 | 3149 | if ((pi = create_procinfo (PIDGET (ptid), 0)) == NULL) |
8a3fe4f8 | 3150 | perror (_("procfs: out of memory in 'attach'")); |
c3f6f71d JM |
3151 | |
3152 | if (!open_procinfo_files (pi, FD_CTL)) | |
3153 | { | |
3154 | fprintf_filtered (gdb_stderr, "procfs:%d -- ", __LINE__); | |
19958708 | 3155 | sprintf (errmsg, "do_attach: couldn't open /proc file for process %d", |
39f77062 | 3156 | PIDGET (ptid)); |
c3f6f71d JM |
3157 | dead_procinfo (pi, errmsg, NOKILL); |
3158 | } | |
c906108c | 3159 | |
c3f6f71d JM |
3160 | /* Stop the process (if it isn't already stopped). */ |
3161 | if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) | |
c906108c | 3162 | { |
c3f6f71d JM |
3163 | pi->was_stopped = 1; |
3164 | proc_prettyprint_why (proc_why (pi), proc_what (pi), 1); | |
c906108c SS |
3165 | } |
3166 | else | |
3167 | { | |
c3f6f71d JM |
3168 | pi->was_stopped = 0; |
3169 | /* Set the process to run again when we close it. */ | |
3170 | if (!proc_set_run_on_last_close (pi)) | |
3171 | dead_procinfo (pi, "do_attach: couldn't set RLC.", NOKILL); | |
3172 | ||
0df8b418 | 3173 | /* Now stop the process. */ |
c3f6f71d JM |
3174 | if (!proc_stop_process (pi)) |
3175 | dead_procinfo (pi, "do_attach: couldn't stop the process.", NOKILL); | |
3176 | pi->ignore_next_sigstop = 1; | |
c906108c | 3177 | } |
c3f6f71d JM |
3178 | /* Save some of the /proc state to be restored if we detach. */ |
3179 | if (!proc_get_traced_faults (pi, &pi->saved_fltset)) | |
3180 | dead_procinfo (pi, "do_attach: couldn't save traced faults.", NOKILL); | |
3181 | if (!proc_get_traced_signals (pi, &pi->saved_sigset)) | |
3182 | dead_procinfo (pi, "do_attach: couldn't save traced signals.", NOKILL); | |
37de36c6 | 3183 | if (!proc_get_traced_sysentry (pi, pi->saved_entryset)) |
c3f6f71d JM |
3184 | dead_procinfo (pi, "do_attach: couldn't save traced syscall entries.", |
3185 | NOKILL); | |
37de36c6 | 3186 | if (!proc_get_traced_sysexit (pi, pi->saved_exitset)) |
19958708 | 3187 | dead_procinfo (pi, "do_attach: couldn't save traced syscall exits.", |
c3f6f71d JM |
3188 | NOKILL); |
3189 | if (!proc_get_held_signals (pi, &pi->saved_sighold)) | |
3190 | dead_procinfo (pi, "do_attach: couldn't save held signals.", NOKILL); | |
3191 | ||
3192 | if ((fail = procfs_debug_inferior (pi)) != 0) | |
3193 | dead_procinfo (pi, "do_attach: failed in procfs_debug_inferior", NOKILL); | |
3194 | ||
6c95b8df PA |
3195 | inf = current_inferior (); |
3196 | inferior_appeared (inf, pi->pid); | |
c3f6f71d | 3197 | /* Let GDB know that the inferior was attached. */ |
181e7f93 | 3198 | inf->attach_flag = 1; |
2689673f PA |
3199 | |
3200 | /* Create a procinfo for the current lwp. */ | |
3201 | lwpid = proc_get_current_thread (pi); | |
3202 | create_procinfo (pi->pid, lwpid); | |
3203 | ||
3204 | /* Add it to gdb's thread list. */ | |
3205 | ptid = MERGEPID (pi->pid, lwpid); | |
3206 | add_thread (ptid); | |
3207 | ||
3208 | return ptid; | |
c906108c SS |
3209 | } |
3210 | ||
3211 | static void | |
fba45db2 | 3212 | do_detach (int signo) |
c906108c | 3213 | { |
c3f6f71d | 3214 | procinfo *pi; |
c906108c | 3215 | |
0df8b418 | 3216 | /* Find procinfo for the main process. */ |
39f77062 | 3217 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); /* FIXME: threads */ |
c3f6f71d JM |
3218 | if (signo) |
3219 | if (!proc_set_current_signal (pi, signo)) | |
3220 | proc_warn (pi, "do_detach, set_current_signal", __LINE__); | |
c5aa993b | 3221 | |
c3f6f71d JM |
3222 | if (!proc_set_traced_signals (pi, &pi->saved_sigset)) |
3223 | proc_warn (pi, "do_detach, set_traced_signal", __LINE__); | |
c906108c | 3224 | |
c3f6f71d JM |
3225 | if (!proc_set_traced_faults (pi, &pi->saved_fltset)) |
3226 | proc_warn (pi, "do_detach, set_traced_faults", __LINE__); | |
3227 | ||
37de36c6 | 3228 | if (!proc_set_traced_sysentry (pi, pi->saved_entryset)) |
c3f6f71d JM |
3229 | proc_warn (pi, "do_detach, set_traced_sysentry", __LINE__); |
3230 | ||
37de36c6 | 3231 | if (!proc_set_traced_sysexit (pi, pi->saved_exitset)) |
c3f6f71d JM |
3232 | proc_warn (pi, "do_detach, set_traced_sysexit", __LINE__); |
3233 | ||
3234 | if (!proc_set_held_signals (pi, &pi->saved_sighold)) | |
3235 | proc_warn (pi, "do_detach, set_held_signals", __LINE__); | |
3236 | ||
3237 | if (signo || (proc_flags (pi) & (PR_STOPPED | PR_ISTOP))) | |
3238 | if (signo || !(pi->was_stopped) || | |
e2e0b3e5 | 3239 | query (_("Was stopped when attached, make it runnable again? "))) |
c3f6f71d JM |
3240 | { |
3241 | /* Clear any pending signal. */ | |
3242 | if (!proc_clear_current_fault (pi)) | |
3243 | proc_warn (pi, "do_detach, clear_current_fault", __LINE__); | |
3244 | ||
1a303dec MS |
3245 | if (signo == 0 && !proc_clear_current_signal (pi)) |
3246 | proc_warn (pi, "do_detach, clear_current_signal", __LINE__); | |
3247 | ||
c3f6f71d JM |
3248 | if (!proc_set_run_on_last_close (pi)) |
3249 | proc_warn (pi, "do_detach, set_rlc", __LINE__); | |
3250 | } | |
3251 | ||
c3f6f71d | 3252 | destroy_procinfo (pi); |
c906108c SS |
3253 | } |
3254 | ||
772cf8be MK |
3255 | /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this |
3256 | for all registers. | |
3257 | ||
3258 | ??? Is the following note still relevant? We can't get individual | |
3259 | registers with the PT_GETREGS ptrace(2) request either, yet we | |
3260 | don't bother with caching at all in that case. | |
3261 | ||
3262 | NOTE: Since the /proc interface cannot give us individual | |
3263 | registers, we pay no attention to REGNUM, and just fetch them all. | |
3264 | This results in the possibility that we will do unnecessarily many | |
3265 | fetches, since we may be called repeatedly for individual | |
3266 | registers. So we cache the results, and mark the cache invalid | |
3267 | when the process is resumed. */ | |
c3f6f71d | 3268 | |
c906108c | 3269 | static void |
28439f5e PA |
3270 | procfs_fetch_registers (struct target_ops *ops, |
3271 | struct regcache *regcache, int regnum) | |
c906108c | 3272 | { |
772cf8be MK |
3273 | gdb_gregset_t *gregs; |
3274 | procinfo *pi; | |
3275 | int pid = PIDGET (inferior_ptid); | |
3276 | int tid = TIDGET (inferior_ptid); | |
40a6adc1 | 3277 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
c3f6f71d | 3278 | |
2689673f | 3279 | pi = find_procinfo_or_die (pid, tid); |
c3f6f71d JM |
3280 | |
3281 | if (pi == NULL) | |
8a3fe4f8 | 3282 | error (_("procfs: fetch_registers failed to find procinfo for %s"), |
39f77062 | 3283 | target_pid_to_str (inferior_ptid)); |
c3f6f71d | 3284 | |
772cf8be MK |
3285 | gregs = proc_get_gregs (pi); |
3286 | if (gregs == NULL) | |
c3f6f71d JM |
3287 | proc_error (pi, "fetch_registers, get_gregs", __LINE__); |
3288 | ||
56be3814 | 3289 | supply_gregset (regcache, (const gdb_gregset_t *) gregs); |
c3f6f71d | 3290 | |
40a6adc1 | 3291 | if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */ |
60054393 | 3292 | { |
772cf8be MK |
3293 | gdb_fpregset_t *fpregs; |
3294 | ||
40a6adc1 MD |
3295 | if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch)) |
3296 | || regnum == gdbarch_pc_regnum (gdbarch) | |
3297 | || regnum == gdbarch_sp_regnum (gdbarch)) | |
772cf8be | 3298 | return; /* Not a floating point register. */ |
c5aa993b | 3299 | |
772cf8be MK |
3300 | fpregs = proc_get_fpregs (pi); |
3301 | if (fpregs == NULL) | |
60054393 | 3302 | proc_error (pi, "fetch_registers, get_fpregs", __LINE__); |
c906108c | 3303 | |
56be3814 | 3304 | supply_fpregset (regcache, (const gdb_fpregset_t *) fpregs); |
60054393 | 3305 | } |
c906108c SS |
3306 | } |
3307 | ||
772cf8be MK |
3308 | /* Store register REGNUM back into the inferior. If REGNUM is -1, do |
3309 | this for all registers. | |
3310 | ||
3311 | NOTE: Since the /proc interface will not read individual registers, | |
3312 | we will cache these requests until the process is resumed, and only | |
3313 | then write them back to the inferior process. | |
77382aee | 3314 | |
772cf8be MK |
3315 | FIXME: is that a really bad idea? Have to think about cases where |
3316 | writing one register might affect the value of others, etc. */ | |
c906108c | 3317 | |
c3f6f71d | 3318 | static void |
28439f5e PA |
3319 | procfs_store_registers (struct target_ops *ops, |
3320 | struct regcache *regcache, int regnum) | |
c3f6f71d | 3321 | { |
772cf8be MK |
3322 | gdb_gregset_t *gregs; |
3323 | procinfo *pi; | |
3324 | int pid = PIDGET (inferior_ptid); | |
3325 | int tid = TIDGET (inferior_ptid); | |
40a6adc1 | 3326 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
c3f6f71d | 3327 | |
2689673f | 3328 | pi = find_procinfo_or_die (pid, tid); |
c3f6f71d JM |
3329 | |
3330 | if (pi == NULL) | |
8a3fe4f8 | 3331 | error (_("procfs: store_registers: failed to find procinfo for %s"), |
39f77062 | 3332 | target_pid_to_str (inferior_ptid)); |
c906108c | 3333 | |
772cf8be MK |
3334 | gregs = proc_get_gregs (pi); |
3335 | if (gregs == NULL) | |
c3f6f71d | 3336 | proc_error (pi, "store_registers, get_gregs", __LINE__); |
c906108c | 3337 | |
56be3814 | 3338 | fill_gregset (regcache, gregs, regnum); |
c3f6f71d JM |
3339 | if (!proc_set_gregs (pi)) |
3340 | proc_error (pi, "store_registers, set_gregs", __LINE__); | |
c906108c | 3341 | |
40a6adc1 | 3342 | if (gdbarch_fp0_regnum (gdbarch) >= 0) /* Do we have an FPU? */ |
60054393 | 3343 | { |
772cf8be MK |
3344 | gdb_fpregset_t *fpregs; |
3345 | ||
40a6adc1 MD |
3346 | if ((regnum >= 0 && regnum < gdbarch_fp0_regnum (gdbarch)) |
3347 | || regnum == gdbarch_pc_regnum (gdbarch) | |
3348 | || regnum == gdbarch_sp_regnum (gdbarch)) | |
772cf8be | 3349 | return; /* Not a floating point register. */ |
60054393 | 3350 | |
772cf8be MK |
3351 | fpregs = proc_get_fpregs (pi); |
3352 | if (fpregs == NULL) | |
60054393 MS |
3353 | proc_error (pi, "store_registers, get_fpregs", __LINE__); |
3354 | ||
56be3814 | 3355 | fill_fpregset (regcache, fpregs, regnum); |
60054393 MS |
3356 | if (!proc_set_fpregs (pi)) |
3357 | proc_error (pi, "store_registers, set_fpregs", __LINE__); | |
3358 | } | |
c3f6f71d | 3359 | } |
c906108c | 3360 | |
37de36c6 KB |
3361 | static int |
3362 | syscall_is_lwp_exit (procinfo *pi, int scall) | |
3363 | { | |
37de36c6 KB |
3364 | #ifdef SYS_lwp_exit |
3365 | if (scall == SYS_lwp_exit) | |
3366 | return 1; | |
3367 | #endif | |
3368 | #ifdef SYS_lwpexit | |
3369 | if (scall == SYS_lwpexit) | |
3370 | return 1; | |
3371 | #endif | |
3372 | return 0; | |
3373 | } | |
3374 | ||
3375 | static int | |
3376 | syscall_is_exit (procinfo *pi, int scall) | |
3377 | { | |
3378 | #ifdef SYS_exit | |
3379 | if (scall == SYS_exit) | |
3380 | return 1; | |
3381 | #endif | |
3382 | #ifdef DYNAMIC_SYSCALLS | |
3383 | if (find_syscall (pi, "_exit") == scall) | |
3384 | return 1; | |
3385 | #endif | |
3386 | return 0; | |
3387 | } | |
3388 | ||
3389 | static int | |
3390 | syscall_is_exec (procinfo *pi, int scall) | |
3391 | { | |
3392 | #ifdef SYS_exec | |
3393 | if (scall == SYS_exec) | |
3394 | return 1; | |
3395 | #endif | |
3396 | #ifdef SYS_execv | |
3397 | if (scall == SYS_execv) | |
3398 | return 1; | |
3399 | #endif | |
3400 | #ifdef SYS_execve | |
3401 | if (scall == SYS_execve) | |
3402 | return 1; | |
3403 | #endif | |
3404 | #ifdef DYNAMIC_SYSCALLS | |
3405 | if (find_syscall (pi, "_execve")) | |
3406 | return 1; | |
3407 | if (find_syscall (pi, "ra_execve")) | |
3408 | return 1; | |
3409 | #endif | |
3410 | return 0; | |
3411 | } | |
3412 | ||
3413 | static int | |
3414 | syscall_is_lwp_create (procinfo *pi, int scall) | |
3415 | { | |
3416 | #ifdef SYS_lwp_create | |
3417 | if (scall == SYS_lwp_create) | |
3418 | return 1; | |
3419 | #endif | |
3420 | #ifdef SYS_lwpcreate | |
3421 | if (scall == SYS_lwpcreate) | |
3422 | return 1; | |
3423 | #endif | |
3424 | return 0; | |
3425 | } | |
3426 | ||
7434dadd JB |
3427 | /* Remove the breakpoint that we inserted in __dbx_link(). |
3428 | Does nothing if the breakpoint hasn't been inserted or has already | |
3429 | been removed. */ | |
3430 | ||
3431 | static void | |
3432 | remove_dbx_link_breakpoint (void) | |
3433 | { | |
3434 | if (dbx_link_bpt_addr == 0) | |
3435 | return; | |
3436 | ||
3437 | if (deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt) != 0) | |
3438 | warning (_("Unable to remove __dbx_link breakpoint.")); | |
3439 | ||
3440 | dbx_link_bpt_addr = 0; | |
3441 | dbx_link_bpt = NULL; | |
3442 | } | |
3443 | ||
3444 | #ifdef SYS_syssgi | |
3445 | /* Return the address of the __dbx_link() function in the file | |
3446 | refernced by ABFD by scanning its symbol table. Return 0 if | |
3447 | the symbol was not found. */ | |
3448 | ||
3449 | static CORE_ADDR | |
3450 | dbx_link_addr (bfd *abfd) | |
3451 | { | |
3452 | long storage_needed; | |
3453 | asymbol **symbol_table; | |
3454 | long number_of_symbols; | |
3455 | long i; | |
3456 | ||
3457 | storage_needed = bfd_get_symtab_upper_bound (abfd); | |
3458 | if (storage_needed <= 0) | |
3459 | return 0; | |
3460 | ||
3461 | symbol_table = (asymbol **) xmalloc (storage_needed); | |
3462 | make_cleanup (xfree, symbol_table); | |
3463 | ||
3464 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); | |
3465 | ||
3466 | for (i = 0; i < number_of_symbols; i++) | |
3467 | { | |
3468 | asymbol *sym = symbol_table[i]; | |
3469 | ||
3470 | if ((sym->flags & BSF_GLOBAL) | |
77382aee PA |
3471 | && sym->name != NULL && strcmp (sym->name, "__dbx_link") == 0) |
3472 | return (sym->value + sym->section->vma); | |
7434dadd JB |
3473 | } |
3474 | ||
3475 | /* Symbol not found, return NULL. */ | |
3476 | return 0; | |
3477 | } | |
3478 | ||
3479 | /* Search the symbol table of the file referenced by FD for a symbol | |
0df8b418 | 3480 | named __dbx_link(). If found, then insert a breakpoint at this location, |
7434dadd JB |
3481 | and return nonzero. Return zero otherwise. */ |
3482 | ||
3483 | static int | |
3484 | insert_dbx_link_bpt_in_file (int fd, CORE_ADDR ignored) | |
3485 | { | |
3486 | bfd *abfd; | |
3487 | long storage_needed; | |
3488 | CORE_ADDR sym_addr; | |
3489 | ||
64c31149 | 3490 | abfd = gdb_bfd_fdopenr ("unamed", 0, fd); |
7434dadd JB |
3491 | if (abfd == NULL) |
3492 | { | |
3493 | warning (_("Failed to create a bfd: %s."), bfd_errmsg (bfd_get_error ())); | |
3494 | return 0; | |
3495 | } | |
3496 | ||
3497 | if (!bfd_check_format (abfd, bfd_object)) | |
3498 | { | |
3499 | /* Not the correct format, so we can not possibly find the dbx_link | |
77382aee | 3500 | symbol in it. */ |
cbb099e8 | 3501 | gdb_bfd_unref (abfd); |
7434dadd JB |
3502 | return 0; |
3503 | } | |
3504 | ||
3505 | sym_addr = dbx_link_addr (abfd); | |
3506 | if (sym_addr != 0) | |
3507 | { | |
3508 | /* Insert the breakpoint. */ | |
3509 | dbx_link_bpt_addr = sym_addr; | |
3510 | dbx_link_bpt = deprecated_insert_raw_breakpoint (target_gdbarch, NULL, | |
3511 | sym_addr); | |
3512 | if (dbx_link_bpt == NULL) | |
77382aee PA |
3513 | { |
3514 | warning (_("Failed to insert dbx_link breakpoint.")); | |
cbb099e8 | 3515 | gdb_bfd_unref (abfd); |
77382aee PA |
3516 | return 0; |
3517 | } | |
cbb099e8 | 3518 | gdb_bfd_unref (abfd); |
7434dadd JB |
3519 | return 1; |
3520 | } | |
3521 | ||
cbb099e8 | 3522 | gdb_bfd_unref (abfd); |
7434dadd JB |
3523 | return 0; |
3524 | } | |
3525 | ||
e294797a | 3526 | /* Calls the supplied callback function once for each mapped address |
77382aee PA |
3527 | space in the process. The callback function receives an open file |
3528 | descriptor for the file corresponding to that mapped address space | |
3529 | (if there is one), and the base address of the mapped space. Quit | |
3530 | when the callback function returns a nonzero value, or at teh end | |
3531 | of the mappings. Returns the first non-zero return value of the | |
3532 | callback function, or zero. */ | |
e294797a JB |
3533 | |
3534 | static int | |
3535 | solib_mappings_callback (struct prmap *map, int (*func) (int, CORE_ADDR), | |
3536 | void *data) | |
3537 | { | |
3538 | procinfo *pi = data; | |
3539 | int fd; | |
3540 | ||
3541 | #ifdef NEW_PROC_API | |
3542 | char name[MAX_PROC_NAME_SIZE + sizeof (map->pr_mapname)]; | |
3543 | ||
3544 | if (map->pr_vaddr == 0 && map->pr_size == 0) | |
3545 | return -1; /* sanity */ | |
3546 | ||
3547 | if (map->pr_mapname[0] == 0) | |
3548 | { | |
3549 | fd = -1; /* no map file */ | |
3550 | } | |
3551 | else | |
3552 | { | |
3553 | sprintf (name, "/proc/%d/object/%s", pi->pid, map->pr_mapname); | |
3554 | /* Note: caller's responsibility to close this fd! */ | |
3555 | fd = open_with_retry (name, O_RDONLY); | |
3556 | /* Note: we don't test the above call for failure; | |
3557 | we just pass the FD on as given. Sometimes there is | |
3558 | no file, so the open may return failure, but that's | |
3559 | not a problem. */ | |
3560 | } | |
3561 | #else | |
3562 | fd = ioctl (pi->ctl_fd, PIOCOPENM, &map->pr_vaddr); | |
3563 | /* Note: we don't test the above call for failure; | |
3564 | we just pass the FD on as given. Sometimes there is | |
3565 | no file, so the ioctl may return failure, but that's | |
3566 | not a problem. */ | |
3567 | #endif | |
3568 | return (*func) (fd, (CORE_ADDR) map->pr_vaddr); | |
3569 | } | |
3570 | ||
7434dadd JB |
3571 | /* If the given memory region MAP contains a symbol named __dbx_link, |
3572 | insert a breakpoint at this location and return nonzero. Return | |
3573 | zero otherwise. */ | |
3574 | ||
3575 | static int | |
3576 | insert_dbx_link_bpt_in_region (struct prmap *map, | |
b8edc417 | 3577 | find_memory_region_ftype child_func, |
77382aee | 3578 | void *data) |
7434dadd JB |
3579 | { |
3580 | procinfo *pi = (procinfo *) data; | |
3581 | ||
3582 | /* We know the symbol we're looking for is in a text region, so | |
3583 | only look for it if the region is a text one. */ | |
3584 | if (map->pr_mflags & MA_EXEC) | |
3585 | return solib_mappings_callback (map, insert_dbx_link_bpt_in_file, pi); | |
3586 | ||
3587 | return 0; | |
3588 | } | |
3589 | ||
3590 | /* Search all memory regions for a symbol named __dbx_link. If found, | |
3591 | insert a breakpoint at its location, and return nonzero. Return zero | |
3592 | otherwise. */ | |
3593 | ||
3594 | static int | |
3595 | insert_dbx_link_breakpoint (procinfo *pi) | |
3596 | { | |
3597 | return iterate_over_mappings (pi, NULL, pi, insert_dbx_link_bpt_in_region); | |
3598 | } | |
3599 | #endif | |
3600 | ||
77382aee PA |
3601 | /* Retrieve the next stop event from the child process. If child has |
3602 | not stopped yet, wait for it to stop. Translate /proc eventcodes | |
3603 | (or possibly wait eventcodes) into gdb internal event codes. | |
3604 | Returns the id of process (and possibly thread) that incurred the | |
3605 | event. Event codes are returned through a pointer parameter. */ | |
c906108c | 3606 | |
39f77062 | 3607 | static ptid_t |
117de6a9 | 3608 | procfs_wait (struct target_ops *ops, |
4fcbb494 | 3609 | ptid_t ptid, struct target_waitstatus *status, int options) |
c906108c | 3610 | { |
0df8b418 | 3611 | /* First cut: loosely based on original version 2.1. */ |
c3f6f71d | 3612 | procinfo *pi; |
39f77062 KB |
3613 | int wstat; |
3614 | int temp_tid; | |
3615 | ptid_t retval, temp_ptid; | |
c3f6f71d JM |
3616 | int why, what, flags; |
3617 | int retry = 0; | |
c906108c | 3618 | |
c3f6f71d | 3619 | wait_again: |
c906108c | 3620 | |
c3f6f71d JM |
3621 | retry++; |
3622 | wstat = 0; | |
39f77062 | 3623 | retval = pid_to_ptid (-1); |
c906108c | 3624 | |
0df8b418 | 3625 | /* Find procinfo for main process. */ |
39f77062 | 3626 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
c3f6f71d | 3627 | if (pi) |
c906108c | 3628 | { |
0df8b418 | 3629 | /* We must assume that the status is stale now... */ |
c3f6f71d JM |
3630 | pi->status_valid = 0; |
3631 | pi->gregs_valid = 0; | |
3632 | pi->fpregs_valid = 0; | |
3633 | ||
0df8b418 | 3634 | #if 0 /* just try this out... */ |
c3f6f71d JM |
3635 | flags = proc_flags (pi); |
3636 | why = proc_why (pi); | |
3637 | if ((flags & PR_STOPPED) && (why == PR_REQUESTED)) | |
0df8b418 | 3638 | pi->status_valid = 0; /* re-read again, IMMEDIATELY... */ |
c3f6f71d JM |
3639 | #endif |
3640 | /* If child is not stopped, wait for it to stop. */ | |
3641 | if (!(proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) && | |
3642 | !proc_wait_for_stop (pi)) | |
c906108c | 3643 | { |
0df8b418 | 3644 | /* wait_for_stop failed: has the child terminated? */ |
c3f6f71d | 3645 | if (errno == ENOENT) |
c906108c | 3646 | { |
39f77062 KB |
3647 | int wait_retval; |
3648 | ||
0df8b418 MS |
3649 | /* /proc file not found; presumably child has terminated. */ |
3650 | wait_retval = wait (&wstat); /* "wait" for the child's exit. */ | |
c3f6f71d | 3651 | |
0df8b418 | 3652 | if (wait_retval != PIDGET (inferior_ptid)) /* wrong child? */ |
3e43a32a MS |
3653 | error (_("procfs: couldn't stop " |
3654 | "process %d: wait returned %d."), | |
39f77062 | 3655 | PIDGET (inferior_ptid), wait_retval); |
c3f6f71d | 3656 | /* FIXME: might I not just use waitpid? |
0df8b418 | 3657 | Or try find_procinfo to see if I know about this child? */ |
39f77062 | 3658 | retval = pid_to_ptid (wait_retval); |
c906108c | 3659 | } |
d1566ff5 FN |
3660 | else if (errno == EINTR) |
3661 | goto wait_again; | |
c3f6f71d | 3662 | else |
c906108c | 3663 | { |
0df8b418 | 3664 | /* Unknown error from wait_for_stop. */ |
c3f6f71d | 3665 | proc_error (pi, "target_wait (wait_for_stop)", __LINE__); |
c906108c | 3666 | } |
c3f6f71d JM |
3667 | } |
3668 | else | |
3669 | { | |
3670 | /* This long block is reached if either: | |
3671 | a) the child was already stopped, or | |
3672 | b) we successfully waited for the child with wait_for_stop. | |
3673 | This block will analyze the /proc status, and translate it | |
3674 | into a waitstatus for GDB. | |
3675 | ||
3676 | If we actually had to call wait because the /proc file | |
19958708 | 3677 | is gone (child terminated), then we skip this block, |
c3f6f71d JM |
3678 | because we already have a waitstatus. */ |
3679 | ||
3680 | flags = proc_flags (pi); | |
3681 | why = proc_why (pi); | |
3682 | what = proc_what (pi); | |
3683 | ||
c3f6f71d | 3684 | if (flags & (PR_STOPPED | PR_ISTOP)) |
c906108c | 3685 | { |
c3f6f71d JM |
3686 | #ifdef PR_ASYNC |
3687 | /* If it's running async (for single_thread control), | |
3688 | set it back to normal again. */ | |
3689 | if (flags & PR_ASYNC) | |
3690 | if (!proc_unset_async (pi)) | |
3691 | proc_error (pi, "target_wait, unset_async", __LINE__); | |
3692 | #endif | |
3693 | ||
3694 | if (info_verbose) | |
3695 | proc_prettyprint_why (why, what, 1); | |
3696 | ||
3697 | /* The 'pid' we will return to GDB is composed of | |
3698 | the process ID plus the lwp ID. */ | |
3699 | retval = MERGEPID (pi->pid, proc_get_current_thread (pi)); | |
3700 | ||
3701 | switch (why) { | |
3702 | case PR_SIGNALLED: | |
3703 | wstat = (what << 8) | 0177; | |
3704 | break; | |
3705 | case PR_SYSENTRY: | |
37de36c6 | 3706 | if (syscall_is_lwp_exit (pi, what)) |
c3f6f71d | 3707 | { |
17faa917 DJ |
3708 | if (print_thread_events) |
3709 | printf_unfiltered (_("[%s exited]\n"), | |
3710 | target_pid_to_str (retval)); | |
37de36c6 KB |
3711 | delete_thread (retval); |
3712 | status->kind = TARGET_WAITKIND_SPURIOUS; | |
3713 | return retval; | |
3714 | } | |
3715 | else if (syscall_is_exit (pi, what)) | |
3716 | { | |
181e7f93 PA |
3717 | struct inferior *inf; |
3718 | ||
0df8b418 | 3719 | /* Handle SYS_exit call only. */ |
37de36c6 | 3720 | /* Stopped at entry to SYS_exit. |
19958708 | 3721 | Make it runnable, resume it, then use |
37de36c6 | 3722 | the wait system call to get its exit code. |
19958708 | 3723 | Proc_run_process always clears the current |
37de36c6 KB |
3724 | fault and signal. |
3725 | Then return its exit status. */ | |
3726 | pi->status_valid = 0; | |
3727 | wstat = 0; | |
19958708 | 3728 | /* FIXME: what we should do is return |
37de36c6 KB |
3729 | TARGET_WAITKIND_SPURIOUS. */ |
3730 | if (!proc_run_process (pi, 0, 0)) | |
3731 | proc_error (pi, "target_wait, run_process", __LINE__); | |
181e7f93 PA |
3732 | |
3733 | inf = find_inferior_pid (pi->pid); | |
3734 | if (inf->attach_flag) | |
c3f6f71d | 3735 | { |
19958708 | 3736 | /* Don't call wait: simulate waiting for exit, |
37de36c6 KB |
3737 | return a "success" exit code. Bogus: what if |
3738 | it returns something else? */ | |
3739 | wstat = 0; | |
39f77062 | 3740 | retval = inferior_ptid; /* ? ? ? */ |
37de36c6 KB |
3741 | } |
3742 | else | |
3743 | { | |
3744 | int temp = wait (&wstat); | |
3745 | ||
3746 | /* FIXME: shouldn't I make sure I get the right | |
3747 | event from the right process? If (for | |
3748 | instance) I have killed an earlier inferior | |
3749 | process but failed to clean up after it | |
3750 | somehow, I could get its termination event | |
3751 | here. */ | |
3752 | ||
0df8b418 MS |
3753 | /* If wait returns -1, that's what we return |
3754 | to GDB. */ | |
37de36c6 | 3755 | if (temp < 0) |
39f77062 | 3756 | retval = pid_to_ptid (temp); |
c3f6f71d | 3757 | } |
c3f6f71d | 3758 | } |
37de36c6 KB |
3759 | else |
3760 | { | |
a3f17187 | 3761 | printf_filtered (_("procfs: trapped on entry to ")); |
37de36c6 KB |
3762 | proc_prettyprint_syscall (proc_what (pi), 0); |
3763 | printf_filtered ("\n"); | |
3764 | #ifndef PIOCSSPCACT | |
c3f6f71d | 3765 | { |
37de36c6 | 3766 | long i, nsysargs, *sysargs; |
c3f6f71d | 3767 | |
37de36c6 KB |
3768 | if ((nsysargs = proc_nsysarg (pi)) > 0 && |
3769 | (sysargs = proc_sysargs (pi)) != NULL) | |
3770 | { | |
3e43a32a MS |
3771 | printf_filtered (_("%ld syscall arguments:\n"), |
3772 | nsysargs); | |
37de36c6 | 3773 | for (i = 0; i < nsysargs; i++) |
19958708 | 3774 | printf_filtered ("#%ld: 0x%08lx\n", |
37de36c6 KB |
3775 | i, sysargs[i]); |
3776 | } | |
c3f6f71d | 3777 | |
c3f6f71d | 3778 | } |
c3f6f71d | 3779 | #endif |
37de36c6 KB |
3780 | if (status) |
3781 | { | |
0df8b418 MS |
3782 | /* How to exit gracefully, returning "unknown |
3783 | event". */ | |
37de36c6 | 3784 | status->kind = TARGET_WAITKIND_SPURIOUS; |
39f77062 | 3785 | return inferior_ptid; |
37de36c6 KB |
3786 | } |
3787 | else | |
3788 | { | |
3789 | /* How to keep going without returning to wfi: */ | |
a493e3e2 | 3790 | target_resume (ptid, 0, GDB_SIGNAL_0); |
37de36c6 KB |
3791 | goto wait_again; |
3792 | } | |
3793 | } | |
3794 | break; | |
3795 | case PR_SYSEXIT: | |
3796 | if (syscall_is_exec (pi, what)) | |
c3f6f71d | 3797 | { |
37de36c6 KB |
3798 | /* Hopefully this is our own "fork-child" execing |
3799 | the real child. Hoax this event into a trap, and | |
3800 | GDB will see the child about to execute its start | |
0df8b418 | 3801 | address. */ |
37de36c6 KB |
3802 | wstat = (SIGTRAP << 8) | 0177; |
3803 | } | |
9185ddce | 3804 | #ifdef SYS_syssgi |
77382aee PA |
3805 | else if (what == SYS_syssgi) |
3806 | { | |
3807 | /* see if we can break on dbx_link(). If yes, then | |
3808 | we no longer need the SYS_syssgi notifications. */ | |
3809 | if (insert_dbx_link_breakpoint (pi)) | |
3810 | proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, | |
3811 | FLAG_RESET, 0); | |
3812 | ||
3813 | /* This is an internal event and should be transparent | |
3814 | to wfi, so resume the execution and wait again. See | |
3815 | comment in procfs_init_inferior() for more details. */ | |
a493e3e2 | 3816 | target_resume (ptid, 0, GDB_SIGNAL_0); |
77382aee PA |
3817 | goto wait_again; |
3818 | } | |
9185ddce | 3819 | #endif |
37de36c6 KB |
3820 | else if (syscall_is_lwp_create (pi, what)) |
3821 | { | |
77382aee PA |
3822 | /* This syscall is somewhat like fork/exec. We |
3823 | will get the event twice: once for the parent | |
3824 | LWP, and once for the child. We should already | |
3825 | know about the parent LWP, but the child will | |
3826 | be new to us. So, whenever we get this event, | |
3827 | if it represents a new thread, simply add the | |
3828 | thread to the list. */ | |
c3f6f71d | 3829 | |
37de36c6 | 3830 | /* If not in procinfo list, add it. */ |
39f77062 KB |
3831 | temp_tid = proc_get_current_thread (pi); |
3832 | if (!find_procinfo (pi->pid, temp_tid)) | |
3833 | create_procinfo (pi->pid, temp_tid); | |
37de36c6 | 3834 | |
39f77062 | 3835 | temp_ptid = MERGEPID (pi->pid, temp_tid); |
37de36c6 | 3836 | /* If not in GDB's thread list, add it. */ |
39f77062 | 3837 | if (!in_thread_list (temp_ptid)) |
93815fbf VP |
3838 | add_thread (temp_ptid); |
3839 | ||
0df8b418 | 3840 | /* Return to WFI, but tell it to immediately resume. */ |
37de36c6 | 3841 | status->kind = TARGET_WAITKIND_SPURIOUS; |
39f77062 | 3842 | return inferior_ptid; |
37de36c6 KB |
3843 | } |
3844 | else if (syscall_is_lwp_exit (pi, what)) | |
3845 | { | |
17faa917 DJ |
3846 | if (print_thread_events) |
3847 | printf_unfiltered (_("[%s exited]\n"), | |
3848 | target_pid_to_str (retval)); | |
37de36c6 KB |
3849 | delete_thread (retval); |
3850 | status->kind = TARGET_WAITKIND_SPURIOUS; | |
3851 | return retval; | |
c3f6f71d | 3852 | } |
37de36c6 KB |
3853 | else if (0) |
3854 | { | |
3855 | /* FIXME: Do we need to handle SYS_sproc, | |
3856 | SYS_fork, or SYS_vfork here? The old procfs | |
3857 | seemed to use this event to handle threads on | |
3858 | older (non-LWP) systems, where I'm assuming | |
19958708 | 3859 | that threads were actually separate processes. |
37de36c6 KB |
3860 | Irix, maybe? Anyway, low priority for now. */ |
3861 | } | |
3862 | else | |
3863 | { | |
a3f17187 | 3864 | printf_filtered (_("procfs: trapped on exit from ")); |
37de36c6 KB |
3865 | proc_prettyprint_syscall (proc_what (pi), 0); |
3866 | printf_filtered ("\n"); | |
3867 | #ifndef PIOCSSPCACT | |
3868 | { | |
3869 | long i, nsysargs, *sysargs; | |
3870 | ||
3871 | if ((nsysargs = proc_nsysarg (pi)) > 0 && | |
3872 | (sysargs = proc_sysargs (pi)) != NULL) | |
3873 | { | |
77382aee PA |
3874 | printf_filtered (_("%ld syscall arguments:\n"), |
3875 | nsysargs); | |
37de36c6 | 3876 | for (i = 0; i < nsysargs; i++) |
19958708 | 3877 | printf_filtered ("#%ld: 0x%08lx\n", |
37de36c6 KB |
3878 | i, sysargs[i]); |
3879 | } | |
3880 | } | |
c3f6f71d | 3881 | #endif |
37de36c6 | 3882 | status->kind = TARGET_WAITKIND_SPURIOUS; |
39f77062 | 3883 | return inferior_ptid; |
37de36c6 | 3884 | } |
c3f6f71d JM |
3885 | break; |
3886 | case PR_REQUESTED: | |
3887 | #if 0 /* FIXME */ | |
3888 | wstat = (SIGSTOP << 8) | 0177; | |
3889 | break; | |
3890 | #else | |
3891 | if (retry < 5) | |
3892 | { | |
a3f17187 | 3893 | printf_filtered (_("Retry #%d:\n"), retry); |
c3f6f71d JM |
3894 | pi->status_valid = 0; |
3895 | goto wait_again; | |
3896 | } | |
3897 | else | |
3898 | { | |
3899 | /* If not in procinfo list, add it. */ | |
39f77062 KB |
3900 | temp_tid = proc_get_current_thread (pi); |
3901 | if (!find_procinfo (pi->pid, temp_tid)) | |
3902 | create_procinfo (pi->pid, temp_tid); | |
c3f6f71d JM |
3903 | |
3904 | /* If not in GDB's thread list, add it. */ | |
39f77062 KB |
3905 | temp_ptid = MERGEPID (pi->pid, temp_tid); |
3906 | if (!in_thread_list (temp_ptid)) | |
93815fbf | 3907 | add_thread (temp_ptid); |
c3f6f71d JM |
3908 | |
3909 | status->kind = TARGET_WAITKIND_STOPPED; | |
3910 | status->value.sig = 0; | |
3911 | return retval; | |
3912 | } | |
3913 | #endif | |
3914 | case PR_JOBCONTROL: | |
3915 | wstat = (what << 8) | 0177; | |
3916 | break; | |
3917 | case PR_FAULTED: | |
7af6341f | 3918 | switch (what) { |
c3f6f71d JM |
3919 | #ifdef FLTWATCH |
3920 | case FLTWATCH: | |
3921 | wstat = (SIGTRAP << 8) | 0177; | |
3922 | break; | |
3923 | #endif | |
3924 | #ifdef FLTKWATCH | |
3925 | case FLTKWATCH: | |
3926 | wstat = (SIGTRAP << 8) | 0177; | |
3927 | break; | |
3928 | #endif | |
0df8b418 | 3929 | /* FIXME: use si_signo where possible. */ |
c3f6f71d | 3930 | case FLTPRIV: |
0df8b418 | 3931 | #if (FLTILL != FLTPRIV) /* Avoid "duplicate case" error. */ |
c3f6f71d JM |
3932 | case FLTILL: |
3933 | #endif | |
3934 | wstat = (SIGILL << 8) | 0177; | |
3935 | break; | |
3936 | case FLTBPT: | |
0df8b418 | 3937 | #if (FLTTRACE != FLTBPT) /* Avoid "duplicate case" error. */ |
c3f6f71d JM |
3938 | case FLTTRACE: |
3939 | #endif | |
77382aee PA |
3940 | /* If we hit our __dbx_link() internal breakpoint, |
3941 | then remove it. See comments in procfs_init_inferior() | |
3942 | for more details. */ | |
3943 | if (dbx_link_bpt_addr != 0 | |
3944 | && dbx_link_bpt_addr | |
fb14de7b | 3945 | == regcache_read_pc (get_current_regcache ())) |
77382aee | 3946 | remove_dbx_link_breakpoint (); |
9185ddce | 3947 | |
c3f6f71d JM |
3948 | wstat = (SIGTRAP << 8) | 0177; |
3949 | break; | |
3950 | case FLTSTACK: | |
3951 | case FLTACCESS: | |
0df8b418 | 3952 | #if (FLTBOUNDS != FLTSTACK) /* Avoid "duplicate case" error. */ |
c3f6f71d JM |
3953 | case FLTBOUNDS: |
3954 | #endif | |
3955 | wstat = (SIGSEGV << 8) | 0177; | |
3956 | break; | |
3957 | case FLTIOVF: | |
3958 | case FLTIZDIV: | |
0df8b418 | 3959 | #if (FLTFPE != FLTIOVF) /* Avoid "duplicate case" error. */ |
c3f6f71d JM |
3960 | case FLTFPE: |
3961 | #endif | |
3962 | wstat = (SIGFPE << 8) | 0177; | |
3963 | break; | |
3e43a32a | 3964 | case FLTPAGE: /* Recoverable page fault */ |
0df8b418 MS |
3965 | default: /* FIXME: use si_signo if possible for |
3966 | fault. */ | |
39f77062 | 3967 | retval = pid_to_ptid (-1); |
c3f6f71d | 3968 | printf_filtered ("procfs:%d -- ", __LINE__); |
a3f17187 | 3969 | printf_filtered (_("child stopped for unknown reason:\n")); |
c3f6f71d | 3970 | proc_prettyprint_why (why, what, 1); |
8a3fe4f8 | 3971 | error (_("... giving up...")); |
c3f6f71d JM |
3972 | break; |
3973 | } | |
3974 | break; /* case PR_FAULTED: */ | |
3975 | default: /* switch (why) unmatched */ | |
3976 | printf_filtered ("procfs:%d -- ", __LINE__); | |
a3f17187 | 3977 | printf_filtered (_("child stopped for unknown reason:\n")); |
c3f6f71d | 3978 | proc_prettyprint_why (why, what, 1); |
8a3fe4f8 | 3979 | error (_("... giving up...")); |
c3f6f71d JM |
3980 | break; |
3981 | } | |
77382aee PA |
3982 | /* Got this far without error: If retval isn't in the |
3983 | threads database, add it. */ | |
39f77062 KB |
3984 | if (PIDGET (retval) > 0 && |
3985 | !ptid_equal (retval, inferior_ptid) && | |
c3f6f71d | 3986 | !in_thread_list (retval)) |
c906108c | 3987 | { |
77382aee PA |
3988 | /* We have a new thread. We need to add it both to |
3989 | GDB's list and to our own. If we don't create a | |
3990 | procinfo, resume may be unhappy later. */ | |
c3f6f71d JM |
3991 | add_thread (retval); |
3992 | if (find_procinfo (PIDGET (retval), TIDGET (retval)) == NULL) | |
3993 | create_procinfo (PIDGET (retval), TIDGET (retval)); | |
c906108c | 3994 | } |
c906108c | 3995 | } |
0df8b418 | 3996 | else /* Flags do not indicate STOPPED. */ |
c906108c | 3997 | { |
0df8b418 | 3998 | /* surely this can't happen... */ |
c3f6f71d JM |
3999 | printf_filtered ("procfs:%d -- process not stopped.\n", |
4000 | __LINE__); | |
4001 | proc_prettyprint_flags (flags, 1); | |
8a3fe4f8 | 4002 | error (_("procfs: ...giving up...")); |
c906108c | 4003 | } |
c906108c | 4004 | } |
c906108c | 4005 | |
c3f6f71d JM |
4006 | if (status) |
4007 | store_waitstatus (status, wstat); | |
c906108c SS |
4008 | } |
4009 | ||
c3f6f71d JM |
4010 | return retval; |
4011 | } | |
c906108c | 4012 | |
4e73f23d RM |
4013 | /* Perform a partial transfer to/from the specified object. For |
4014 | memory transfers, fall back to the old memory xfer functions. */ | |
4015 | ||
4016 | static LONGEST | |
4017 | procfs_xfer_partial (struct target_ops *ops, enum target_object object, | |
0b62613e PA |
4018 | const char *annex, gdb_byte *readbuf, |
4019 | const gdb_byte *writebuf, ULONGEST offset, LONGEST len) | |
4e73f23d RM |
4020 | { |
4021 | switch (object) | |
4022 | { | |
4023 | case TARGET_OBJECT_MEMORY: | |
4024 | if (readbuf) | |
0b62613e PA |
4025 | return (*ops->deprecated_xfer_memory) (offset, readbuf, |
4026 | len, 0/*read*/, NULL, ops); | |
4e73f23d | 4027 | if (writebuf) |
0b62613e PA |
4028 | return (*ops->deprecated_xfer_memory) (offset, (gdb_byte *) writebuf, |
4029 | len, 1/*write*/, NULL, ops); | |
4e73f23d RM |
4030 | return -1; |
4031 | ||
4032 | #ifdef NEW_PROC_API | |
4033 | case TARGET_OBJECT_AUXV: | |
9f2982ff | 4034 | return memory_xfer_auxv (ops, object, annex, readbuf, writebuf, |
4e73f23d RM |
4035 | offset, len); |
4036 | #endif | |
4037 | ||
4038 | default: | |
4039 | if (ops->beneath != NULL) | |
4040 | return ops->beneath->to_xfer_partial (ops->beneath, object, annex, | |
4041 | readbuf, writebuf, offset, len); | |
4042 | return -1; | |
4043 | } | |
4044 | } | |
4045 | ||
4046 | ||
d0849a9a KB |
4047 | /* Transfer LEN bytes between GDB address MYADDR and target address |
4048 | MEMADDR. If DOWRITE is non-zero, transfer them to the target, | |
4049 | otherwise transfer them from the target. TARGET is unused. | |
4050 | ||
4051 | The return value is 0 if an error occurred or no bytes were | |
4052 | transferred. Otherwise, it will be a positive value which | |
4053 | indicates the number of bytes transferred between gdb and the | |
4054 | target. (Note that the interface also makes provisions for | |
0df8b418 | 4055 | negative values, but this capability isn't implemented here.) */ |
d0849a9a | 4056 | |
c3f6f71d | 4057 | static int |
0b62613e | 4058 | procfs_xfer_memory (CORE_ADDR memaddr, gdb_byte *myaddr, int len, int dowrite, |
37de36c6 | 4059 | struct mem_attrib *attrib, struct target_ops *target) |
c3f6f71d JM |
4060 | { |
4061 | procinfo *pi; | |
4062 | int nbytes = 0; | |
c906108c | 4063 | |
0df8b418 | 4064 | /* Find procinfo for main process. */ |
39f77062 | 4065 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
c3f6f71d JM |
4066 | if (pi->as_fd == 0 && |
4067 | open_procinfo_files (pi, FD_AS) == 0) | |
c906108c | 4068 | { |
c3f6f71d JM |
4069 | proc_warn (pi, "xfer_memory, open_proc_files", __LINE__); |
4070 | return 0; | |
c906108c | 4071 | } |
c906108c | 4072 | |
c3f6f71d | 4073 | if (lseek (pi->as_fd, (off_t) memaddr, SEEK_SET) == (off_t) memaddr) |
c906108c | 4074 | { |
c3f6f71d | 4075 | if (dowrite) |
c906108c | 4076 | { |
c3f6f71d | 4077 | #ifdef NEW_PROC_API |
cce7e648 | 4078 | PROCFS_NOTE ("write memory:\n"); |
c906108c | 4079 | #else |
cce7e648 | 4080 | PROCFS_NOTE ("write memory:\n"); |
c906108c | 4081 | #endif |
c3f6f71d | 4082 | nbytes = write (pi->as_fd, myaddr, len); |
c906108c | 4083 | } |
c3f6f71d | 4084 | else |
c906108c | 4085 | { |
cce7e648 | 4086 | PROCFS_NOTE ("read memory:\n"); |
c3f6f71d | 4087 | nbytes = read (pi->as_fd, myaddr, len); |
c906108c | 4088 | } |
c3f6f71d | 4089 | if (nbytes < 0) |
c906108c | 4090 | { |
c3f6f71d | 4091 | nbytes = 0; |
c906108c | 4092 | } |
c906108c | 4093 | } |
c3f6f71d | 4094 | return nbytes; |
c906108c SS |
4095 | } |
4096 | ||
77382aee PA |
4097 | /* Called by target_resume before making child runnable. Mark cached |
4098 | registers and status's invalid. If there are "dirty" caches that | |
4099 | need to be written back to the child process, do that. | |
c906108c | 4100 | |
77382aee PA |
4101 | File descriptors are also cached. As they are a limited resource, |
4102 | we cannot hold onto them indefinitely. However, as they are | |
4103 | expensive to open, we don't want to throw them away | |
4104 | indescriminately either. As a compromise, we will keep the file | |
4105 | descriptors for the parent process, but discard any file | |
4106 | descriptors we may have accumulated for the threads. | |
4107 | ||
4108 | As this function is called by iterate_over_threads, it always | |
4109 | returns zero (so that iterate_over_threads will keep | |
4110 | iterating). */ | |
c3f6f71d JM |
4111 | |
4112 | static int | |
fba45db2 | 4113 | invalidate_cache (procinfo *parent, procinfo *pi, void *ptr) |
c906108c | 4114 | { |
77382aee PA |
4115 | /* About to run the child; invalidate caches and do any other |
4116 | cleanup. */ | |
c906108c | 4117 | |
c3f6f71d JM |
4118 | #if 0 |
4119 | if (pi->gregs_dirty) | |
4120 | if (parent == NULL || | |
4121 | proc_get_current_thread (parent) != pi->tid) | |
4122 | if (!proc_set_gregs (pi)) /* flush gregs cache */ | |
4123 | proc_warn (pi, "target_resume, set_gregs", | |
4124 | __LINE__); | |
a97b0ac8 | 4125 | if (gdbarch_fp0_regnum (target_gdbarch) >= 0) |
60054393 MS |
4126 | if (pi->fpregs_dirty) |
4127 | if (parent == NULL || | |
4128 | proc_get_current_thread (parent) != pi->tid) | |
4129 | if (!proc_set_fpregs (pi)) /* flush fpregs cache */ | |
19958708 | 4130 | proc_warn (pi, "target_resume, set_fpregs", |
60054393 | 4131 | __LINE__); |
c906108c | 4132 | #endif |
c906108c | 4133 | |
c3f6f71d | 4134 | if (parent != NULL) |
c906108c | 4135 | { |
c3f6f71d | 4136 | /* The presence of a parent indicates that this is an LWP. |
19958708 | 4137 | Close any file descriptors that it might have open. |
c3f6f71d JM |
4138 | We don't do this to the master (parent) procinfo. */ |
4139 | ||
4140 | close_procinfo_files (pi); | |
c906108c | 4141 | } |
c3f6f71d JM |
4142 | pi->gregs_valid = 0; |
4143 | pi->fpregs_valid = 0; | |
4144 | #if 0 | |
4145 | pi->gregs_dirty = 0; | |
4146 | pi->fpregs_dirty = 0; | |
c906108c | 4147 | #endif |
c3f6f71d JM |
4148 | pi->status_valid = 0; |
4149 | pi->threads_valid = 0; | |
c906108c | 4150 | |
c3f6f71d | 4151 | return 0; |
c906108c SS |
4152 | } |
4153 | ||
0fda6bd2 | 4154 | #if 0 |
77382aee PA |
4155 | /* A callback function for iterate_over_threads. Find the |
4156 | asynchronous signal thread, and make it runnable. See if that | |
4157 | helps matters any. */ | |
c906108c | 4158 | |
c3f6f71d | 4159 | static int |
fba45db2 | 4160 | make_signal_thread_runnable (procinfo *process, procinfo *pi, void *ptr) |
c906108c | 4161 | { |
c3f6f71d JM |
4162 | #ifdef PR_ASLWP |
4163 | if (proc_flags (pi) & PR_ASLWP) | |
c906108c | 4164 | { |
c3f6f71d JM |
4165 | if (!proc_run_process (pi, 0, -1)) |
4166 | proc_error (pi, "make_signal_thread_runnable", __LINE__); | |
4167 | return 1; | |
c906108c | 4168 | } |
c906108c | 4169 | #endif |
c3f6f71d | 4170 | return 0; |
c906108c | 4171 | } |
0fda6bd2 | 4172 | #endif |
c906108c | 4173 | |
77382aee PA |
4174 | /* Make the child process runnable. Normally we will then call |
4175 | procfs_wait and wait for it to stop again (unless gdb is async). | |
4176 | ||
4177 | If STEP is true, then arrange for the child to stop again after | |
4178 | executing a single instruction. If SIGNO is zero, then cancel any | |
4179 | pending signal; if non-zero, then arrange for the indicated signal | |
4180 | to be delivered to the child when it runs. If PID is -1, then | |
4181 | allow any child thread to run; if non-zero, then allow only the | |
4182 | indicated thread to run. (not implemented yet). */ | |
c906108c SS |
4183 | |
4184 | static void | |
28439f5e | 4185 | procfs_resume (struct target_ops *ops, |
2ea28649 | 4186 | ptid_t ptid, int step, enum gdb_signal signo) |
c906108c | 4187 | { |
c3f6f71d JM |
4188 | procinfo *pi, *thread; |
4189 | int native_signo; | |
4190 | ||
19958708 | 4191 | /* 2.1: |
c3f6f71d | 4192 | prrun.prflags |= PRSVADDR; |
19958708 | 4193 | prrun.pr_vaddr = $PC; set resume address |
c3f6f71d | 4194 | prrun.prflags |= PRSTRACE; trace signals in pr_trace (all) |
19958708 | 4195 | prrun.prflags |= PRSFAULT; trace faults in pr_fault (all but PAGE) |
c3f6f71d JM |
4196 | prrun.prflags |= PRCFAULT; clear current fault. |
4197 | ||
4198 | PRSTRACE and PRSFAULT can be done by other means | |
77382aee | 4199 | (proc_trace_signals, proc_trace_faults) |
c3f6f71d JM |
4200 | PRSVADDR is unnecessary. |
4201 | PRCFAULT may be replaced by a PIOCCFAULT call (proc_clear_current_fault) | |
4202 | This basically leaves PRSTEP and PRCSIG. | |
4203 | PRCSIG is like PIOCSSIG (proc_clear_current_signal). | |
4204 | So basically PR_STEP is the sole argument that must be passed | |
77382aee | 4205 | to proc_run_process (for use in the prrun struct by ioctl). */ |
c3f6f71d | 4206 | |
0df8b418 | 4207 | /* Find procinfo for main process. */ |
39f77062 | 4208 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
c3f6f71d | 4209 | |
77382aee | 4210 | /* First cut: ignore pid argument. */ |
c3f6f71d | 4211 | errno = 0; |
c906108c | 4212 | |
c3f6f71d JM |
4213 | /* Convert signal to host numbering. */ |
4214 | if (signo == 0 || | |
a493e3e2 | 4215 | (signo == GDB_SIGNAL_STOP && pi->ignore_next_sigstop)) |
c3f6f71d JM |
4216 | native_signo = 0; |
4217 | else | |
2ea28649 | 4218 | native_signo = gdb_signal_to_host (signo); |
c906108c | 4219 | |
c3f6f71d | 4220 | pi->ignore_next_sigstop = 0; |
c906108c | 4221 | |
77382aee PA |
4222 | /* Running the process voids all cached registers and status. */ |
4223 | /* Void the threads' caches first. */ | |
19958708 | 4224 | proc_iterate_over_threads (pi, invalidate_cache, NULL); |
c3f6f71d JM |
4225 | /* Void the process procinfo's caches. */ |
4226 | invalidate_cache (NULL, pi, NULL); | |
c906108c | 4227 | |
39f77062 | 4228 | if (PIDGET (ptid) != -1) |
c906108c | 4229 | { |
77382aee PA |
4230 | /* Resume a specific thread, presumably suppressing the |
4231 | others. */ | |
39f77062 | 4232 | thread = find_procinfo (PIDGET (ptid), TIDGET (ptid)); |
7de45904 | 4233 | if (thread != NULL) |
c906108c | 4234 | { |
c3f6f71d JM |
4235 | if (thread->tid != 0) |
4236 | { | |
77382aee PA |
4237 | /* We're to resume a specific thread, and not the |
4238 | others. Set the child process's PR_ASYNC flag. */ | |
c3f6f71d JM |
4239 | #ifdef PR_ASYNC |
4240 | if (!proc_set_async (pi)) | |
4241 | proc_error (pi, "target_resume, set_async", __LINE__); | |
4242 | #endif | |
4243 | #if 0 | |
19958708 | 4244 | proc_iterate_over_threads (pi, |
c3f6f71d JM |
4245 | make_signal_thread_runnable, |
4246 | NULL); | |
4247 | #endif | |
0df8b418 MS |
4248 | pi = thread; /* Substitute the thread's procinfo |
4249 | for run. */ | |
c3f6f71d | 4250 | } |
c906108c SS |
4251 | } |
4252 | } | |
c906108c | 4253 | |
c3f6f71d | 4254 | if (!proc_run_process (pi, step, native_signo)) |
c906108c | 4255 | { |
c3f6f71d | 4256 | if (errno == EBUSY) |
77382aee PA |
4257 | warning (_("resume: target already running. " |
4258 | "Pretend to resume, and hope for the best!")); | |
c3f6f71d JM |
4259 | else |
4260 | proc_error (pi, "target_resume", __LINE__); | |
c906108c | 4261 | } |
c3f6f71d | 4262 | } |
c906108c | 4263 | |
77382aee | 4264 | /* Set up to trace signals in the child process. */ |
c906108c | 4265 | |
c3f6f71d | 4266 | static void |
2455069d | 4267 | procfs_pass_signals (int numsigs, unsigned char *pass_signals) |
c3f6f71d | 4268 | { |
37de36c6 | 4269 | gdb_sigset_t signals; |
2455069d UW |
4270 | procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
4271 | int signo; | |
c906108c | 4272 | |
2455069d UW |
4273 | prfillset (&signals); |
4274 | ||
4275 | for (signo = 0; signo < NSIG; signo++) | |
4276 | { | |
2ea28649 | 4277 | int target_signo = gdb_signal_from_host (signo); |
2455069d UW |
4278 | if (target_signo < numsigs && pass_signals[target_signo]) |
4279 | gdb_prdelset (&signals, signo); | |
4280 | } | |
4281 | ||
4282 | if (!proc_set_traced_signals (pi, &signals)) | |
4283 | proc_error (pi, "pass_signals", __LINE__); | |
c3f6f71d | 4284 | } |
c906108c | 4285 | |
77382aee | 4286 | /* Print status information about the child process. */ |
c906108c | 4287 | |
c3f6f71d | 4288 | static void |
fba45db2 | 4289 | procfs_files_info (struct target_ops *ignore) |
c3f6f71d | 4290 | { |
181e7f93 | 4291 | struct inferior *inf = current_inferior (); |
f4a14ae6 | 4292 | |
a3f17187 | 4293 | printf_filtered (_("\tUsing the running image of %s %s via /proc.\n"), |
181e7f93 | 4294 | inf->attach_flag? "attached": "child", |
39f77062 | 4295 | target_pid_to_str (inferior_ptid)); |
c3f6f71d | 4296 | } |
c906108c | 4297 | |
77382aee PA |
4298 | /* Stop the child process asynchronously, as when the gdb user types |
4299 | control-c or presses a "stop" button. Works by sending | |
4300 | kill(SIGINT) to the child's process group. */ | |
c906108c | 4301 | |
c3f6f71d | 4302 | static void |
f9c72d52 | 4303 | procfs_stop (ptid_t ptid) |
c3f6f71d | 4304 | { |
7e1789f5 | 4305 | kill (-inferior_process_group (), SIGINT); |
c906108c SS |
4306 | } |
4307 | ||
77382aee PA |
4308 | /* Make it die. Wait for it to die. Clean up after it. Note: this |
4309 | should only be applied to the real process, not to an LWP, because | |
4310 | of the check for parent-process. If we need this to work for an | |
4311 | LWP, it needs some more logic. */ | |
c906108c | 4312 | |
c3f6f71d | 4313 | static void |
fba45db2 | 4314 | unconditionally_kill_inferior (procinfo *pi) |
c3f6f71d JM |
4315 | { |
4316 | int parent_pid; | |
c906108c | 4317 | |
c3f6f71d JM |
4318 | parent_pid = proc_parent_pid (pi); |
4319 | #ifdef PROCFS_NEED_CLEAR_CURSIG_FOR_KILL | |
0df8b418 | 4320 | /* FIXME: use access functions. */ |
c3f6f71d JM |
4321 | /* Alpha OSF/1-3.x procfs needs a clear of the current signal |
4322 | before the PIOCKILL, otherwise it might generate a corrupted core | |
4323 | file for the inferior. */ | |
4324 | if (ioctl (pi->ctl_fd, PIOCSSIG, NULL) < 0) | |
4325 | { | |
4326 | printf_filtered ("unconditionally_kill: SSIG failed!\n"); | |
4327 | } | |
4328 | #endif | |
4329 | #ifdef PROCFS_NEED_PIOCSSIG_FOR_KILL | |
4330 | /* Alpha OSF/1-2.x procfs needs a PIOCSSIG call with a SIGKILL signal | |
4331 | to kill the inferior, otherwise it might remain stopped with a | |
4332 | pending SIGKILL. | |
4333 | We do not check the result of the PIOCSSIG, the inferior might have | |
4334 | died already. */ | |
4335 | { | |
37de36c6 | 4336 | gdb_siginfo_t newsiginfo; |
c906108c | 4337 | |
c3f6f71d JM |
4338 | memset ((char *) &newsiginfo, 0, sizeof (newsiginfo)); |
4339 | newsiginfo.si_signo = SIGKILL; | |
4340 | newsiginfo.si_code = 0; | |
4341 | newsiginfo.si_errno = 0; | |
4342 | newsiginfo.si_pid = getpid (); | |
4343 | newsiginfo.si_uid = getuid (); | |
0df8b418 | 4344 | /* FIXME: use proc_set_current_signal. */ |
c3f6f71d JM |
4345 | ioctl (pi->ctl_fd, PIOCSSIG, &newsiginfo); |
4346 | } | |
4347 | #else /* PROCFS_NEED_PIOCSSIG_FOR_KILL */ | |
4348 | if (!proc_kill (pi, SIGKILL)) | |
103b3ef5 | 4349 | proc_error (pi, "unconditionally_kill, proc_kill", __LINE__); |
c3f6f71d JM |
4350 | #endif /* PROCFS_NEED_PIOCSSIG_FOR_KILL */ |
4351 | destroy_procinfo (pi); | |
c906108c | 4352 | |
c3f6f71d JM |
4353 | /* If pi is GDB's child, wait for it to die. */ |
4354 | if (parent_pid == getpid ()) | |
19958708 | 4355 | /* FIXME: should we use waitpid to make sure we get the right event? |
c3f6f71d JM |
4356 | Should we check the returned event? */ |
4357 | { | |
0d06e24b | 4358 | #if 0 |
c3f6f71d | 4359 | int status, ret; |
c906108c | 4360 | |
c3f6f71d JM |
4361 | ret = waitpid (pi->pid, &status, 0); |
4362 | #else | |
4363 | wait (NULL); | |
4364 | #endif | |
4365 | } | |
4366 | } | |
c906108c | 4367 | |
77382aee PA |
4368 | /* We're done debugging it, and we want it to go away. Then we want |
4369 | GDB to forget all about it. */ | |
c906108c | 4370 | |
19958708 | 4371 | static void |
7d85a9c0 | 4372 | procfs_kill_inferior (struct target_ops *ops) |
c906108c | 4373 | { |
39f77062 | 4374 | if (!ptid_equal (inferior_ptid, null_ptid)) /* ? */ |
c3f6f71d | 4375 | { |
0df8b418 | 4376 | /* Find procinfo for main process. */ |
39f77062 | 4377 | procinfo *pi = find_procinfo (PIDGET (inferior_ptid), 0); |
c906108c | 4378 | |
c3f6f71d JM |
4379 | if (pi) |
4380 | unconditionally_kill_inferior (pi); | |
4381 | target_mourn_inferior (); | |
c906108c | 4382 | } |
c3f6f71d JM |
4383 | } |
4384 | ||
77382aee | 4385 | /* Forget we ever debugged this thing! */ |
c906108c | 4386 | |
19958708 | 4387 | static void |
136d6dae | 4388 | procfs_mourn_inferior (struct target_ops *ops) |
c3f6f71d JM |
4389 | { |
4390 | procinfo *pi; | |
c906108c | 4391 | |
39f77062 | 4392 | if (!ptid_equal (inferior_ptid, null_ptid)) |
c3f6f71d | 4393 | { |
0df8b418 | 4394 | /* Find procinfo for main process. */ |
39f77062 | 4395 | pi = find_procinfo (PIDGET (inferior_ptid), 0); |
c3f6f71d JM |
4396 | if (pi) |
4397 | destroy_procinfo (pi); | |
c906108c | 4398 | } |
28439f5e | 4399 | unpush_target (ops); |
8181d85f DJ |
4400 | |
4401 | if (dbx_link_bpt != NULL) | |
4402 | { | |
a6d9a66e | 4403 | deprecated_remove_raw_breakpoint (target_gdbarch, dbx_link_bpt); |
8181d85f DJ |
4404 | dbx_link_bpt_addr = 0; |
4405 | dbx_link_bpt = NULL; | |
4406 | } | |
4407 | ||
c3f6f71d JM |
4408 | generic_mourn_inferior (); |
4409 | } | |
c906108c | 4410 | |
77382aee PA |
4411 | /* When GDB forks to create a runnable inferior process, this function |
4412 | is called on the parent side of the fork. It's job is to do | |
4413 | whatever is necessary to make the child ready to be debugged, and | |
4414 | then wait for the child to synchronize. */ | |
c906108c | 4415 | |
19958708 | 4416 | static void |
28439f5e | 4417 | procfs_init_inferior (struct target_ops *ops, int pid) |
c3f6f71d JM |
4418 | { |
4419 | procinfo *pi; | |
37de36c6 | 4420 | gdb_sigset_t signals; |
c3f6f71d | 4421 | int fail; |
2689673f | 4422 | int lwpid; |
c906108c | 4423 | |
c3f6f71d JM |
4424 | /* This routine called on the parent side (GDB side) |
4425 | after GDB forks the inferior. */ | |
28439f5e | 4426 | push_target (ops); |
c906108c | 4427 | |
c3f6f71d | 4428 | if ((pi = create_procinfo (pid, 0)) == NULL) |
9b20d036 | 4429 | perror (_("procfs: out of memory in 'init_inferior'")); |
c3f6f71d JM |
4430 | |
4431 | if (!open_procinfo_files (pi, FD_CTL)) | |
4432 | proc_error (pi, "init_inferior, open_proc_files", __LINE__); | |
4433 | ||
4434 | /* | |
4435 | xmalloc // done | |
4436 | open_procinfo_files // done | |
4437 | link list // done | |
4438 | prfillset (trace) | |
4439 | procfs_notice_signals | |
4440 | prfillset (fault) | |
4441 | prdelset (FLTPAGE) | |
4442 | PIOCWSTOP | |
4443 | PIOCSFAULT | |
4444 | */ | |
4445 | ||
77382aee | 4446 | /* If not stopped yet, wait for it to stop. */ |
c3f6f71d JM |
4447 | if (!(proc_flags (pi) & PR_STOPPED) && |
4448 | !(proc_wait_for_stop (pi))) | |
4449 | dead_procinfo (pi, "init_inferior: wait_for_stop failed", KILL); | |
4450 | ||
4451 | /* Save some of the /proc state to be restored if we detach. */ | |
4452 | /* FIXME: Why? In case another debugger was debugging it? | |
0df8b418 | 4453 | We're it's parent, for Ghu's sake! */ |
c3f6f71d JM |
4454 | if (!proc_get_traced_signals (pi, &pi->saved_sigset)) |
4455 | proc_error (pi, "init_inferior, get_traced_signals", __LINE__); | |
4456 | if (!proc_get_held_signals (pi, &pi->saved_sighold)) | |
4457 | proc_error (pi, "init_inferior, get_held_signals", __LINE__); | |
4458 | if (!proc_get_traced_faults (pi, &pi->saved_fltset)) | |
4459 | proc_error (pi, "init_inferior, get_traced_faults", __LINE__); | |
37de36c6 | 4460 | if (!proc_get_traced_sysentry (pi, pi->saved_entryset)) |
c3f6f71d | 4461 | proc_error (pi, "init_inferior, get_traced_sysentry", __LINE__); |
37de36c6 | 4462 | if (!proc_get_traced_sysexit (pi, pi->saved_exitset)) |
c3f6f71d JM |
4463 | proc_error (pi, "init_inferior, get_traced_sysexit", __LINE__); |
4464 | ||
c3f6f71d JM |
4465 | if ((fail = procfs_debug_inferior (pi)) != 0) |
4466 | proc_error (pi, "init_inferior (procfs_debug_inferior)", fail); | |
4467 | ||
0d06e24b JM |
4468 | /* FIXME: logically, we should really be turning OFF run-on-last-close, |
4469 | and possibly even turning ON kill-on-last-close at this point. But | |
4470 | I can't make that change without careful testing which I don't have | |
4471 | time to do right now... */ | |
c3f6f71d JM |
4472 | /* Turn on run-on-last-close flag so that the child |
4473 | will die if GDB goes away for some reason. */ | |
4474 | if (!proc_set_run_on_last_close (pi)) | |
4475 | proc_error (pi, "init_inferior, set_RLC", __LINE__); | |
4476 | ||
2689673f PA |
4477 | /* We now have have access to the lwpid of the main thread/lwp. */ |
4478 | lwpid = proc_get_current_thread (pi); | |
4479 | ||
4480 | /* Create a procinfo for the main lwp. */ | |
4481 | create_procinfo (pid, lwpid); | |
4482 | ||
4483 | /* We already have a main thread registered in the thread table at | |
4484 | this point, but it didn't have any lwp info yet. Notify the core | |
4485 | about it. This changes inferior_ptid as well. */ | |
4486 | thread_change_ptid (pid_to_ptid (pid), | |
4487 | MERGEPID (pid, lwpid)); | |
c906108c | 4488 | |
46ac7a5d AC |
4489 | /* Typically two, one trap to exec the shell, one to exec the |
4490 | program being debugged. Defined by "inferior.h". */ | |
c3f6f71d | 4491 | startup_inferior (START_INFERIOR_TRAPS_EXPECTED); |
9185ddce JB |
4492 | |
4493 | #ifdef SYS_syssgi | |
4494 | /* On mips-irix, we need to stop the inferior early enough during | |
4495 | the startup phase in order to be able to load the shared library | |
4496 | symbols and insert the breakpoints that are located in these shared | |
4497 | libraries. Stopping at the program entry point is not good enough | |
4498 | because the -init code is executed before the execution reaches | |
4499 | that point. | |
4500 | ||
4501 | So what we need to do is to insert a breakpoint in the runtime | |
4502 | loader (rld), more precisely in __dbx_link(). This procedure is | |
4503 | called by rld once all shared libraries have been mapped, but before | |
0df8b418 | 4504 | the -init code is executed. Unfortuantely, this is not straightforward, |
9185ddce JB |
4505 | as rld is not part of the executable we are running, and thus we need |
4506 | the inferior to run until rld itself has been mapped in memory. | |
77382aee | 4507 | |
9185ddce JB |
4508 | For this, we trace all syssgi() syscall exit events. Each time |
4509 | we detect such an event, we iterate over each text memory maps, | |
4510 | get its associated fd, and scan the symbol table for __dbx_link(). | |
4511 | When found, we know that rld has been mapped, and that we can insert | |
4512 | the breakpoint at the symbol address. Once the dbx_link() breakpoint | |
4513 | has been inserted, the syssgi() notifications are no longer necessary, | |
4514 | so they should be canceled. */ | |
4515 | proc_trace_syscalls_1 (pi, SYS_syssgi, PR_SYSEXIT, FLAG_SET, 0); | |
9185ddce | 4516 | #endif |
c3f6f71d | 4517 | } |
c906108c | 4518 | |
77382aee PA |
4519 | /* When GDB forks to create a new process, this function is called on |
4520 | the child side of the fork before GDB exec's the user program. Its | |
4521 | job is to make the child minimally debuggable, so that the parent | |
4522 | GDB process can connect to the child and take over. This function | |
4523 | should do only the minimum to make that possible, and to | |
4524 | synchronize with the parent process. The parent process should | |
4525 | take care of the details. */ | |
c3f6f71d JM |
4526 | |
4527 | static void | |
fba45db2 | 4528 | procfs_set_exec_trap (void) |
c3f6f71d JM |
4529 | { |
4530 | /* This routine called on the child side (inferior side) | |
4531 | after GDB forks the inferior. It must use only local variables, | |
4532 | because it may be sharing data space with its parent. */ | |
c906108c | 4533 | |
c3f6f71d | 4534 | procinfo *pi; |
37de36c6 | 4535 | sysset_t *exitset; |
c906108c | 4536 | |
c3f6f71d | 4537 | if ((pi = create_procinfo (getpid (), 0)) == NULL) |
e2e0b3e5 | 4538 | perror_with_name (_("procfs: create_procinfo failed in child.")); |
c906108c | 4539 | |
c3f6f71d JM |
4540 | if (open_procinfo_files (pi, FD_CTL) == 0) |
4541 | { | |
4542 | proc_warn (pi, "set_exec_trap, open_proc_files", __LINE__); | |
4543 | gdb_flush (gdb_stderr); | |
77382aee PA |
4544 | /* No need to call "dead_procinfo", because we're going to |
4545 | exit. */ | |
c3f6f71d JM |
4546 | _exit (127); |
4547 | } | |
c906108c | 4548 | |
c3f6f71d JM |
4549 | #ifdef PRFS_STOPEXEC /* defined on OSF */ |
4550 | /* OSF method for tracing exec syscalls. Quoting: | |
4551 | Under Alpha OSF/1 we have to use a PIOCSSPCACT ioctl to trace | |
4552 | exits from exec system calls because of the user level loader. */ | |
77382aee | 4553 | /* FIXME: make nice and maybe move into an access function. */ |
c3f6f71d JM |
4554 | { |
4555 | int prfs_flags; | |
c906108c | 4556 | |
c3f6f71d JM |
4557 | if (ioctl (pi->ctl_fd, PIOCGSPCACT, &prfs_flags) < 0) |
4558 | { | |
4559 | proc_warn (pi, "set_exec_trap (PIOCGSPCACT)", __LINE__); | |
4560 | gdb_flush (gdb_stderr); | |
4561 | _exit (127); | |
4562 | } | |
4563 | prfs_flags |= PRFS_STOPEXEC; | |
c906108c | 4564 | |
c3f6f71d JM |
4565 | if (ioctl (pi->ctl_fd, PIOCSSPCACT, &prfs_flags) < 0) |
4566 | { | |
4567 | proc_warn (pi, "set_exec_trap (PIOCSSPCACT)", __LINE__); | |
4568 | gdb_flush (gdb_stderr); | |
4569 | _exit (127); | |
4570 | } | |
4571 | } | |
4572 | #else /* not PRFS_STOPEXEC */ | |
77382aee | 4573 | /* Everyone else's (except OSF) method for tracing exec syscalls. */ |
c3f6f71d JM |
4574 | /* GW: Rationale... |
4575 | Not all systems with /proc have all the exec* syscalls with the same | |
4576 | names. On the SGI, for example, there is no SYS_exec, but there | |
77382aee | 4577 | *is* a SYS_execv. So, we try to account for that. */ |
c906108c | 4578 | |
37de36c6 KB |
4579 | exitset = sysset_t_alloc (pi); |
4580 | gdb_premptysysset (exitset); | |
c3f6f71d | 4581 | #ifdef SYS_exec |
37de36c6 | 4582 | gdb_praddsysset (exitset, SYS_exec); |
c3f6f71d JM |
4583 | #endif |
4584 | #ifdef SYS_execve | |
37de36c6 | 4585 | gdb_praddsysset (exitset, SYS_execve); |
c3f6f71d JM |
4586 | #endif |
4587 | #ifdef SYS_execv | |
37de36c6 | 4588 | gdb_praddsysset (exitset, SYS_execv); |
c906108c | 4589 | #endif |
37de36c6 KB |
4590 | #ifdef DYNAMIC_SYSCALLS |
4591 | { | |
4592 | int callnum = find_syscall (pi, "execve"); | |
4593 | ||
4594 | if (callnum >= 0) | |
4595 | gdb_praddsysset (exitset, callnum); | |
c906108c | 4596 | |
37de36c6 KB |
4597 | callnum = find_syscall (pi, "ra_execve"); |
4598 | if (callnum >= 0) | |
4599 | gdb_praddsysset (exitset, callnum); | |
4600 | } | |
4601 | #endif /* DYNAMIC_SYSCALLS */ | |
4602 | ||
4603 | if (!proc_set_traced_sysexit (pi, exitset)) | |
c906108c | 4604 | { |
c3f6f71d JM |
4605 | proc_warn (pi, "set_exec_trap, set_traced_sysexit", __LINE__); |
4606 | gdb_flush (gdb_stderr); | |
4607 | _exit (127); | |
c906108c | 4608 | } |
c3f6f71d JM |
4609 | #endif /* PRFS_STOPEXEC */ |
4610 | ||
0df8b418 | 4611 | /* FIXME: should this be done in the parent instead? */ |
c3f6f71d JM |
4612 | /* Turn off inherit on fork flag so that all grand-children |
4613 | of gdb start with tracing flags cleared. */ | |
4614 | if (!proc_unset_inherit_on_fork (pi)) | |
4615 | proc_warn (pi, "set_exec_trap, unset_inherit", __LINE__); | |
4616 | ||
4617 | /* Turn off run on last close flag, so that the child process | |
4618 | cannot run away just because we close our handle on it. | |
4619 | We want it to wait for the parent to attach. */ | |
4620 | if (!proc_unset_run_on_last_close (pi)) | |
4621 | proc_warn (pi, "set_exec_trap, unset_RLC", __LINE__); | |
4622 | ||
19958708 | 4623 | /* FIXME: No need to destroy the procinfo -- |
0df8b418 | 4624 | we have our own address space, and we're about to do an exec! */ |
c3f6f71d | 4625 | /*destroy_procinfo (pi);*/ |
c906108c | 4626 | } |
c906108c | 4627 | |
77382aee PA |
4628 | /* This function is called BEFORE gdb forks the inferior process. Its |
4629 | only real responsibility is to set things up for the fork, and tell | |
4630 | GDB which two functions to call after the fork (one for the parent, | |
4631 | and one for the child). | |
4632 | ||
4633 | This function does a complicated search for a unix shell program, | |
4634 | which it then uses to parse arguments and environment variables to | |
4635 | be sent to the child. I wonder whether this code could not be | |
4636 | abstracted out and shared with other unix targets such as | |
4637 | inf-ptrace? */ | |
c906108c SS |
4638 | |
4639 | static void | |
136d6dae VP |
4640 | procfs_create_inferior (struct target_ops *ops, char *exec_file, |
4641 | char *allargs, char **env, int from_tty) | |
c906108c SS |
4642 | { |
4643 | char *shell_file = getenv ("SHELL"); | |
4644 | char *tryname; | |
28439f5e PA |
4645 | int pid; |
4646 | ||
c906108c SS |
4647 | if (shell_file != NULL && strchr (shell_file, '/') == NULL) |
4648 | { | |
4649 | ||
4650 | /* We will be looking down the PATH to find shell_file. If we | |
c3f6f71d JM |
4651 | just do this the normal way (via execlp, which operates by |
4652 | attempting an exec for each element of the PATH until it | |
4653 | finds one which succeeds), then there will be an exec for | |
4654 | each failed attempt, each of which will cause a PR_SYSEXIT | |
4655 | stop, and we won't know how to distinguish the PR_SYSEXIT's | |
4656 | for these failed execs with the ones for successful execs | |
4657 | (whether the exec has succeeded is stored at that time in the | |
4658 | carry bit or some such architecture-specific and | |
4659 | non-ABI-specified place). | |
4660 | ||
4661 | So I can't think of anything better than to search the PATH | |
4662 | now. This has several disadvantages: (1) There is a race | |
4663 | condition; if we find a file now and it is deleted before we | |
4664 | exec it, we lose, even if the deletion leaves a valid file | |
4665 | further down in the PATH, (2) there is no way to know exactly | |
4666 | what an executable (in the sense of "capable of being | |
4667 | exec'd") file is. Using access() loses because it may lose | |
4668 | if the caller is the superuser; failing to use it loses if | |
4669 | there are ACLs or some such. */ | |
c906108c SS |
4670 | |
4671 | char *p; | |
4672 | char *p1; | |
4673 | /* FIXME-maybe: might want "set path" command so user can change what | |
c3f6f71d | 4674 | path is used from within GDB. */ |
c906108c SS |
4675 | char *path = getenv ("PATH"); |
4676 | int len; | |
4677 | struct stat statbuf; | |
4678 | ||
4679 | if (path == NULL) | |
4680 | path = "/bin:/usr/bin"; | |
4681 | ||
4682 | tryname = alloca (strlen (path) + strlen (shell_file) + 2); | |
c3f6f71d | 4683 | for (p = path; p != NULL; p = p1 ? p1 + 1: NULL) |
c906108c SS |
4684 | { |
4685 | p1 = strchr (p, ':'); | |
4686 | if (p1 != NULL) | |
4687 | len = p1 - p; | |
4688 | else | |
4689 | len = strlen (p); | |
4690 | strncpy (tryname, p, len); | |
4691 | tryname[len] = '\0'; | |
4692 | strcat (tryname, "/"); | |
4693 | strcat (tryname, shell_file); | |
4694 | if (access (tryname, X_OK) < 0) | |
4695 | continue; | |
4696 | if (stat (tryname, &statbuf) < 0) | |
4697 | continue; | |
4698 | if (!S_ISREG (statbuf.st_mode)) | |
4699 | /* We certainly need to reject directories. I'm not quite | |
4700 | as sure about FIFOs, sockets, etc., but I kind of doubt | |
4701 | that people want to exec() these things. */ | |
4702 | continue; | |
4703 | break; | |
4704 | } | |
4705 | if (p == NULL) | |
4706 | /* Not found. This must be an error rather than merely passing | |
4707 | the file to execlp(), because execlp() would try all the | |
4708 | exec()s, causing GDB to get confused. */ | |
8a3fe4f8 | 4709 | error (_("procfs:%d -- Can't find shell %s in PATH"), |
c3f6f71d | 4710 | __LINE__, shell_file); |
c906108c SS |
4711 | |
4712 | shell_file = tryname; | |
4713 | } | |
4714 | ||
28439f5e | 4715 | pid = fork_inferior (exec_file, allargs, env, procfs_set_exec_trap, |
e69860f1 | 4716 | NULL, NULL, shell_file, NULL); |
28439f5e PA |
4717 | |
4718 | procfs_init_inferior (ops, pid); | |
27087a3d JB |
4719 | } |
4720 | ||
4721 | /* An observer for the "inferior_created" event. */ | |
c906108c | 4722 | |
27087a3d JB |
4723 | static void |
4724 | procfs_inferior_created (struct target_ops *ops, int from_tty) | |
4725 | { | |
9185ddce | 4726 | #ifdef SYS_syssgi |
77382aee | 4727 | /* Make sure to cancel the syssgi() syscall-exit notifications. |
9185ddce JB |
4728 | They should normally have been removed by now, but they may still |
4729 | be activated if the inferior doesn't use shared libraries, or if | |
4730 | we didn't locate __dbx_link, or if we never stopped in __dbx_link. | |
27087a3d JB |
4731 | See procfs_init_inferior() for more details. |
4732 | ||
4733 | Since these notifications are only ever enabled when we spawned | |
4734 | the inferior ourselves, there is nothing to do when the inferior | |
4735 | was created by attaching to an already running process, or when | |
4736 | debugging a core file. */ | |
4737 | if (current_inferior ()->attach_flag || !target_can_run (¤t_target)) | |
4738 | return; | |
4739 | ||
9185ddce | 4740 | proc_trace_syscalls_1 (find_procinfo_or_die (PIDGET (inferior_ptid), 0), |
77382aee | 4741 | SYS_syssgi, PR_SYSEXIT, FLAG_RESET, 0); |
9185ddce | 4742 | #endif |
c906108c SS |
4743 | } |
4744 | ||
77382aee | 4745 | /* Callback for find_new_threads. Calls "add_thread". */ |
c906108c | 4746 | |
c3f6f71d | 4747 | static int |
fba45db2 | 4748 | procfs_notice_thread (procinfo *pi, procinfo *thread, void *ptr) |
c906108c | 4749 | { |
39f77062 | 4750 | ptid_t gdb_threadid = MERGEPID (pi->pid, thread->tid); |
c906108c | 4751 | |
2689673f | 4752 | if (!in_thread_list (gdb_threadid) || is_exited (gdb_threadid)) |
c3f6f71d | 4753 | add_thread (gdb_threadid); |
c906108c | 4754 | |
c3f6f71d JM |
4755 | return 0; |
4756 | } | |
4757 | ||
77382aee PA |
4758 | /* Query all the threads that the target knows about, and give them |
4759 | back to GDB to add to its list. */ | |
c3f6f71d | 4760 | |
d3581e61 | 4761 | static void |
28439f5e | 4762 | procfs_find_new_threads (struct target_ops *ops) |
c3f6f71d JM |
4763 | { |
4764 | procinfo *pi; | |
4765 | ||
0df8b418 | 4766 | /* Find procinfo for main process. */ |
39f77062 | 4767 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
c3f6f71d JM |
4768 | proc_update_threads (pi); |
4769 | proc_iterate_over_threads (pi, procfs_notice_thread, NULL); | |
c906108c SS |
4770 | } |
4771 | ||
77382aee PA |
4772 | /* Return true if the thread is still 'alive'. This guy doesn't |
4773 | really seem to be doing his job. Got to investigate how to tell | |
4774 | when a thread is really gone. */ | |
c906108c | 4775 | |
c906108c | 4776 | static int |
28439f5e | 4777 | procfs_thread_alive (struct target_ops *ops, ptid_t ptid) |
c906108c | 4778 | { |
c3f6f71d JM |
4779 | int proc, thread; |
4780 | procinfo *pi; | |
c906108c | 4781 | |
39f77062 KB |
4782 | proc = PIDGET (ptid); |
4783 | thread = TIDGET (ptid); | |
0df8b418 | 4784 | /* If I don't know it, it ain't alive! */ |
c3f6f71d JM |
4785 | if ((pi = find_procinfo (proc, thread)) == NULL) |
4786 | return 0; | |
4787 | ||
4788 | /* If I can't get its status, it ain't alive! | |
4789 | What's more, I need to forget about it! */ | |
4790 | if (!proc_get_status (pi)) | |
4791 | { | |
4792 | destroy_procinfo (pi); | |
4793 | return 0; | |
4794 | } | |
77382aee PA |
4795 | /* I couldn't have got its status if it weren't alive, so it's |
4796 | alive. */ | |
c3f6f71d | 4797 | return 1; |
c906108c | 4798 | } |
c3f6f71d | 4799 | |
77382aee PA |
4800 | /* Convert PTID to a string. Returns the string in a static |
4801 | buffer. */ | |
c3f6f71d | 4802 | |
d3581e61 | 4803 | static char * |
117de6a9 | 4804 | procfs_pid_to_str (struct target_ops *ops, ptid_t ptid) |
c3f6f71d JM |
4805 | { |
4806 | static char buf[80]; | |
c3f6f71d | 4807 | |
5240ceac MK |
4808 | if (TIDGET (ptid) == 0) |
4809 | sprintf (buf, "process %d", PIDGET (ptid)); | |
c3f6f71d | 4810 | else |
21749010 | 4811 | sprintf (buf, "LWP %ld", TIDGET (ptid)); |
5240ceac MK |
4812 | |
4813 | return buf; | |
c3f6f71d JM |
4814 | } |
4815 | ||
77382aee | 4816 | /* Insert a watchpoint. */ |
c3f6f71d | 4817 | |
a0911fd0 | 4818 | static int |
39f77062 | 4819 | procfs_set_watchpoint (ptid_t ptid, CORE_ADDR addr, int len, int rwflag, |
77382aee | 4820 | int after) |
c906108c | 4821 | { |
c3f6f71d | 4822 | #ifndef UNIXWARE |
37de36c6 | 4823 | #ifndef AIX5 |
c3f6f71d | 4824 | int pflags = 0; |
19958708 | 4825 | procinfo *pi; |
c3f6f71d | 4826 | |
19958708 | 4827 | pi = find_procinfo_or_die (PIDGET (ptid) == -1 ? |
39f77062 | 4828 | PIDGET (inferior_ptid) : PIDGET (ptid), 0); |
c3f6f71d | 4829 | |
0df8b418 MS |
4830 | /* Translate from GDB's flags to /proc's. */ |
4831 | if (len > 0) /* len == 0 means delete watchpoint. */ | |
c906108c | 4832 | { |
0df8b418 | 4833 | switch (rwflag) { /* FIXME: need an enum! */ |
c3f6f71d JM |
4834 | case hw_write: /* default watchpoint (write) */ |
4835 | pflags = WRITE_WATCHFLAG; | |
4836 | break; | |
4837 | case hw_read: /* read watchpoint */ | |
4838 | pflags = READ_WATCHFLAG; | |
4839 | break; | |
4840 | case hw_access: /* access watchpoint */ | |
4841 | pflags = READ_WATCHFLAG | WRITE_WATCHFLAG; | |
4842 | break; | |
4843 | case hw_execute: /* execution HW breakpoint */ | |
4844 | pflags = EXEC_WATCHFLAG; | |
4845 | break; | |
0df8b418 | 4846 | default: /* Something weird. Return error. */ |
c906108c | 4847 | return -1; |
c3f6f71d | 4848 | } |
0df8b418 | 4849 | if (after) /* Stop after r/w access is completed. */ |
c3f6f71d JM |
4850 | pflags |= AFTER_WATCHFLAG; |
4851 | } | |
4852 | ||
4853 | if (!proc_set_watchpoint (pi, addr, len, pflags)) | |
4854 | { | |
0df8b418 | 4855 | if (errno == E2BIG) /* Typical error for no resources. */ |
c3f6f71d JM |
4856 | return -1; /* fail */ |
4857 | /* GDB may try to remove the same watchpoint twice. | |
4858 | If a remove request returns no match, don't error. */ | |
c906108c | 4859 | if (errno == ESRCH && len == 0) |
c3f6f71d JM |
4860 | return 0; /* ignore */ |
4861 | proc_error (pi, "set_watchpoint", __LINE__); | |
c906108c | 4862 | } |
37de36c6 KB |
4863 | #endif /* AIX5 */ |
4864 | #endif /* UNIXWARE */ | |
c906108c SS |
4865 | return 0; |
4866 | } | |
4867 | ||
1e03ad20 KB |
4868 | /* Return non-zero if we can set a hardware watchpoint of type TYPE. TYPE |
4869 | is one of bp_hardware_watchpoint, bp_read_watchpoint, bp_write_watchpoint, | |
4870 | or bp_hardware_watchpoint. CNT is the number of watchpoints used so | |
4871 | far. | |
19958708 | 4872 | |
1e03ad20 KB |
4873 | Note: procfs_can_use_hw_breakpoint() is not yet used by all |
4874 | procfs.c targets due to the fact that some of them still define | |
d92524f1 | 4875 | target_can_use_hardware_watchpoint. */ |
1e03ad20 KB |
4876 | |
4877 | static int | |
4878 | procfs_can_use_hw_breakpoint (int type, int cnt, int othertype) | |
4879 | { | |
1e03ad20 KB |
4880 | /* Due to the way that proc_set_watchpoint() is implemented, host |
4881 | and target pointers must be of the same size. If they are not, | |
4882 | we can't use hardware watchpoints. This limitation is due to the | |
9a043c1d AC |
4883 | fact that proc_set_watchpoint() calls |
4884 | procfs_address_to_host_pointer(); a close inspection of | |
4885 | procfs_address_to_host_pointer will reveal that an internal error | |
4886 | will be generated when the host and target pointer sizes are | |
4887 | different. */ | |
4e906f53 | 4888 | struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr; |
f4a14ae6 | 4889 | |
4e906f53 | 4890 | if (sizeof (void *) != TYPE_LENGTH (ptr_type)) |
1e03ad20 KB |
4891 | return 0; |
4892 | ||
4893 | /* Other tests here??? */ | |
4894 | ||
4895 | return 1; | |
1e03ad20 KB |
4896 | } |
4897 | ||
77382aee PA |
4898 | /* Returns non-zero if process is stopped on a hardware watchpoint |
4899 | fault, else returns zero. */ | |
c3f6f71d | 4900 | |
25513619 PA |
4901 | static int |
4902 | procfs_stopped_by_watchpoint (void) | |
c906108c | 4903 | { |
c3f6f71d | 4904 | procinfo *pi; |
c906108c | 4905 | |
25513619 | 4906 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
aaeb7efa | 4907 | |
c3f6f71d | 4908 | if (proc_flags (pi) & (PR_STOPPED | PR_ISTOP)) |
c906108c | 4909 | { |
c3f6f71d | 4910 | if (proc_why (pi) == PR_FAULTED) |
19958708 | 4911 | { |
c906108c | 4912 | #ifdef FLTWATCH |
c3f6f71d JM |
4913 | if (proc_what (pi) == FLTWATCH) |
4914 | return 1; | |
c906108c SS |
4915 | #endif |
4916 | #ifdef FLTKWATCH | |
c3f6f71d JM |
4917 | if (proc_what (pi) == FLTKWATCH) |
4918 | return 1; | |
c906108c | 4919 | #endif |
c3f6f71d | 4920 | } |
c906108c SS |
4921 | } |
4922 | return 0; | |
4923 | } | |
c906108c | 4924 | |
77382aee PA |
4925 | /* Returns 1 if the OS knows the position of the triggered watchpoint, |
4926 | and sets *ADDR to that address. Returns 0 if OS cannot report that | |
4927 | address. This function is only called if | |
4928 | procfs_stopped_by_watchpoint returned 1, thus no further checks are | |
4929 | done. The function also assumes that ADDR is not NULL. */ | |
bf701c2c PM |
4930 | |
4931 | static int | |
4932 | procfs_stopped_data_address (struct target_ops *targ, CORE_ADDR *addr) | |
4933 | { | |
4934 | procinfo *pi; | |
4935 | ||
4936 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); | |
4937 | return proc_watchpoint_address (pi, addr); | |
4938 | } | |
4939 | ||
25513619 | 4940 | static int |
0cf6dd15 TJB |
4941 | procfs_insert_watchpoint (CORE_ADDR addr, int len, int type, |
4942 | struct expression *cond) | |
25513619 | 4943 | { |
d92524f1 | 4944 | if (!target_have_steppable_watchpoint |
a97b0ac8 | 4945 | && !gdbarch_have_nonsteppable_watchpoint (target_gdbarch)) |
25513619 PA |
4946 | { |
4947 | /* When a hardware watchpoint fires off the PC will be left at | |
4948 | the instruction following the one which caused the | |
4949 | watchpoint. It will *NOT* be necessary for GDB to step over | |
4950 | the watchpoint. */ | |
4951 | return procfs_set_watchpoint (inferior_ptid, addr, len, type, 1); | |
4952 | } | |
4953 | else | |
4954 | { | |
4955 | /* When a hardware watchpoint fires off the PC will be left at | |
4956 | the instruction which caused the watchpoint. It will be | |
4957 | necessary for GDB to step over the watchpoint. */ | |
4958 | return procfs_set_watchpoint (inferior_ptid, addr, len, type, 0); | |
4959 | } | |
4960 | } | |
4961 | ||
4962 | static int | |
0cf6dd15 TJB |
4963 | procfs_remove_watchpoint (CORE_ADDR addr, int len, int type, |
4964 | struct expression *cond) | |
25513619 PA |
4965 | { |
4966 | return procfs_set_watchpoint (inferior_ptid, addr, 0, 0, 0); | |
4967 | } | |
4968 | ||
4969 | static int | |
4970 | procfs_region_ok_for_hw_watchpoint (CORE_ADDR addr, int len) | |
4971 | { | |
4972 | /* The man page for proc(4) on Solaris 2.6 and up says that the | |
4973 | system can support "thousands" of hardware watchpoints, but gives | |
4974 | no method for finding out how many; It doesn't say anything about | |
4975 | the allowed size for the watched area either. So we just tell | |
4976 | GDB 'yes'. */ | |
4977 | return 1; | |
4978 | } | |
4979 | ||
4980 | void | |
4981 | procfs_use_watchpoints (struct target_ops *t) | |
4982 | { | |
4983 | t->to_stopped_by_watchpoint = procfs_stopped_by_watchpoint; | |
4984 | t->to_insert_watchpoint = procfs_insert_watchpoint; | |
4985 | t->to_remove_watchpoint = procfs_remove_watchpoint; | |
4986 | t->to_region_ok_for_hw_watchpoint = procfs_region_ok_for_hw_watchpoint; | |
d1a7880c | 4987 | t->to_can_use_hw_breakpoint = procfs_can_use_hw_breakpoint; |
bf701c2c | 4988 | t->to_stopped_data_address = procfs_stopped_data_address; |
25513619 PA |
4989 | } |
4990 | ||
77382aee PA |
4991 | /* Memory Mappings Functions: */ |
4992 | ||
4993 | /* Call a callback function once for each mapping, passing it the | |
4994 | mapping, an optional secondary callback function, and some optional | |
4995 | opaque data. Quit and return the first non-zero value returned | |
4996 | from the callback. | |
4997 | ||
4998 | PI is the procinfo struct for the process to be mapped. FUNC is | |
4999 | the callback function to be called by this iterator. DATA is the | |
5000 | optional opaque data to be passed to the callback function. | |
5001 | CHILD_FUNC is the optional secondary function pointer to be passed | |
5002 | to the child function. Returns the first non-zero return value | |
5003 | from the callback function, or zero. */ | |
831e682e MS |
5004 | |
5005 | static int | |
b8edc417 | 5006 | iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func, |
e9ef4f39 | 5007 | void *data, |
19958708 | 5008 | int (*func) (struct prmap *map, |
b8edc417 | 5009 | find_memory_region_ftype child_func, |
831e682e MS |
5010 | void *data)) |
5011 | { | |
5012 | char pathname[MAX_PROC_NAME_SIZE]; | |
5013 | struct prmap *prmaps; | |
5014 | struct prmap *prmap; | |
5015 | int funcstat; | |
5016 | int map_fd; | |
5017 | int nmap; | |
0a86f364 | 5018 | struct cleanup *cleanups = make_cleanup (null_cleanup, NULL); |
831e682e MS |
5019 | #ifdef NEW_PROC_API |
5020 | struct stat sbuf; | |
5021 | #endif | |
5022 | ||
19958708 | 5023 | /* Get the number of mappings, allocate space, |
831e682e MS |
5024 | and read the mappings into prmaps. */ |
5025 | #ifdef NEW_PROC_API | |
0df8b418 | 5026 | /* Open map fd. */ |
831e682e MS |
5027 | sprintf (pathname, "/proc/%d/map", pi->pid); |
5028 | if ((map_fd = open (pathname, O_RDONLY)) < 0) | |
5029 | proc_error (pi, "iterate_over_mappings (open)", __LINE__); | |
5030 | ||
0df8b418 | 5031 | /* Make sure it gets closed again. */ |
831e682e MS |
5032 | make_cleanup_close (map_fd); |
5033 | ||
19958708 | 5034 | /* Use stat to determine the file size, and compute |
831e682e MS |
5035 | the number of prmap_t objects it contains. */ |
5036 | if (fstat (map_fd, &sbuf) != 0) | |
5037 | proc_error (pi, "iterate_over_mappings (fstat)", __LINE__); | |
5038 | ||
5039 | nmap = sbuf.st_size / sizeof (prmap_t); | |
5040 | prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); | |
5041 | if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps)) | |
5042 | != (nmap * sizeof (*prmaps))) | |
5043 | proc_error (pi, "iterate_over_mappings (read)", __LINE__); | |
5044 | #else | |
5045 | /* Use ioctl command PIOCNMAP to get number of mappings. */ | |
5046 | if (ioctl (pi->ctl_fd, PIOCNMAP, &nmap) != 0) | |
5047 | proc_error (pi, "iterate_over_mappings (PIOCNMAP)", __LINE__); | |
5048 | ||
5049 | prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); | |
5050 | if (ioctl (pi->ctl_fd, PIOCMAP, prmaps) != 0) | |
5051 | proc_error (pi, "iterate_over_mappings (PIOCMAP)", __LINE__); | |
5052 | #endif | |
5053 | ||
5054 | for (prmap = prmaps; nmap > 0; prmap++, nmap--) | |
5055 | if ((funcstat = (*func) (prmap, child_func, data)) != 0) | |
0a86f364 JB |
5056 | { |
5057 | do_cleanups (cleanups); | |
5058 | return funcstat; | |
5059 | } | |
831e682e | 5060 | |
0a86f364 | 5061 | do_cleanups (cleanups); |
831e682e MS |
5062 | return 0; |
5063 | } | |
5064 | ||
77382aee | 5065 | /* Implements the to_find_memory_regions method. Calls an external |
b8edc417 | 5066 | function for each memory region. |
77382aee | 5067 | Returns the integer value returned by the callback. */ |
be4d1333 MS |
5068 | |
5069 | static int | |
19958708 | 5070 | find_memory_regions_callback (struct prmap *map, |
b8edc417 | 5071 | find_memory_region_ftype func, void *data) |
be4d1333 | 5072 | { |
bf75638e | 5073 | return (*func) ((CORE_ADDR) map->pr_vaddr, |
19958708 | 5074 | map->pr_size, |
be4d1333 MS |
5075 | (map->pr_mflags & MA_READ) != 0, |
5076 | (map->pr_mflags & MA_WRITE) != 0, | |
19958708 | 5077 | (map->pr_mflags & MA_EXEC) != 0, |
4f69f4c2 | 5078 | 1, /* MODIFIED is unknown, pass it as true. */ |
be4d1333 MS |
5079 | data); |
5080 | } | |
5081 | ||
77382aee PA |
5082 | /* External interface. Calls a callback function once for each |
5083 | mapped memory region in the child process, passing as arguments: | |
5084 | ||
5085 | CORE_ADDR virtual_address, | |
5086 | unsigned long size, | |
5087 | int read, TRUE if region is readable by the child | |
5088 | int write, TRUE if region is writable by the child | |
5089 | int execute TRUE if region is executable by the child. | |
5090 | ||
5091 | Stops iterating and returns the first non-zero value returned by | |
5092 | the callback. */ | |
be4d1333 MS |
5093 | |
5094 | static int | |
b8edc417 | 5095 | proc_find_memory_regions (find_memory_region_ftype func, void *data) |
be4d1333 MS |
5096 | { |
5097 | procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); | |
5098 | ||
19958708 | 5099 | return iterate_over_mappings (pi, func, data, |
be4d1333 MS |
5100 | find_memory_regions_callback); |
5101 | } | |
5102 | ||
77382aee | 5103 | /* Returns an ascii representation of a memory mapping's flags. */ |
c3f6f71d | 5104 | |
388faa48 | 5105 | static char * |
5ae5f592 | 5106 | mappingflags (long flags) |
388faa48 MS |
5107 | { |
5108 | static char asciiflags[8]; | |
5109 | ||
5110 | strcpy (asciiflags, "-------"); | |
5111 | #if defined (MA_PHYS) | |
5112 | if (flags & MA_PHYS) | |
5113 | asciiflags[0] = 'd'; | |
5114 | #endif | |
5115 | if (flags & MA_STACK) | |
5116 | asciiflags[1] = 's'; | |
5117 | if (flags & MA_BREAK) | |
5118 | asciiflags[2] = 'b'; | |
5119 | if (flags & MA_SHARED) | |
5120 | asciiflags[3] = 's'; | |
5121 | if (flags & MA_READ) | |
5122 | asciiflags[4] = 'r'; | |
5123 | if (flags & MA_WRITE) | |
5124 | asciiflags[5] = 'w'; | |
5125 | if (flags & MA_EXEC) | |
5126 | asciiflags[6] = 'x'; | |
5127 | return (asciiflags); | |
5128 | } | |
5129 | ||
77382aee PA |
5130 | /* Callback function, does the actual work for 'info proc |
5131 | mappings'. */ | |
831e682e | 5132 | |
831e682e | 5133 | static int |
b8edc417 | 5134 | info_mappings_callback (struct prmap *map, find_memory_region_ftype ignore, |
e9ef4f39 | 5135 | void *unused) |
831e682e | 5136 | { |
0b62613e | 5137 | unsigned int pr_off; |
831e682e | 5138 | |
831e682e | 5139 | #ifdef PCAGENT /* Horrible hack: only defined on Solaris 2.6+ */ |
0b62613e | 5140 | pr_off = (unsigned int) map->pr_offset; |
831e682e | 5141 | #else |
0b62613e | 5142 | pr_off = map->pr_off; |
831e682e | 5143 | #endif |
0b62613e | 5144 | |
a97b0ac8 | 5145 | if (gdbarch_addr_bit (target_gdbarch) == 32) |
f6a96b83 | 5146 | printf_filtered ("\t%#10lx %#10lx %#10lx %#10x %7s\n", |
0b62613e PA |
5147 | (unsigned long) map->pr_vaddr, |
5148 | (unsigned long) map->pr_vaddr + map->pr_size - 1, | |
f6a96b83 | 5149 | (unsigned long) map->pr_size, |
0b62613e PA |
5150 | pr_off, |
5151 | mappingflags (map->pr_mflags)); | |
5152 | else | |
f6a96b83 | 5153 | printf_filtered (" %#18lx %#18lx %#10lx %#10x %7s\n", |
0b62613e PA |
5154 | (unsigned long) map->pr_vaddr, |
5155 | (unsigned long) map->pr_vaddr + map->pr_size - 1, | |
f6a96b83 | 5156 | (unsigned long) map->pr_size, |
0b62613e PA |
5157 | pr_off, |
5158 | mappingflags (map->pr_mflags)); | |
831e682e MS |
5159 | |
5160 | return 0; | |
5161 | } | |
5162 | ||
77382aee | 5163 | /* Implement the "info proc mappings" subcommand. */ |
388faa48 MS |
5164 | |
5165 | static void | |
5166 | info_proc_mappings (procinfo *pi, int summary) | |
5167 | { | |
388faa48 | 5168 | if (summary) |
0df8b418 | 5169 | return; /* No output for summary mode. */ |
388faa48 | 5170 | |
a3f17187 | 5171 | printf_filtered (_("Mapped address spaces:\n\n")); |
a97b0ac8 | 5172 | if (gdbarch_ptr_bit (target_gdbarch) == 32) |
0b62613e PA |
5173 | printf_filtered ("\t%10s %10s %10s %10s %7s\n", |
5174 | "Start Addr", | |
5175 | " End Addr", | |
5176 | " Size", | |
5177 | " Offset", | |
5178 | "Flags"); | |
5179 | else | |
5180 | printf_filtered (" %18s %18s %10s %10s %7s\n", | |
5181 | "Start Addr", | |
5182 | " End Addr", | |
5183 | " Size", | |
5184 | " Offset", | |
5185 | "Flags"); | |
388faa48 | 5186 | |
831e682e | 5187 | iterate_over_mappings (pi, NULL, NULL, info_mappings_callback); |
388faa48 MS |
5188 | printf_filtered ("\n"); |
5189 | } | |
5190 | ||
77382aee | 5191 | /* Implement the "info proc" command. */ |
c3f6f71d JM |
5192 | |
5193 | static void | |
145b16a9 UW |
5194 | procfs_info_proc (struct target_ops *ops, char *args, |
5195 | enum info_proc_what what) | |
c906108c | 5196 | { |
c3f6f71d | 5197 | struct cleanup *old_chain; |
388faa48 MS |
5198 | procinfo *process = NULL; |
5199 | procinfo *thread = NULL; | |
5200 | char **argv = NULL; | |
5201 | char *tmp = NULL; | |
5202 | int pid = 0; | |
5203 | int tid = 0; | |
5204 | int mappings = 0; | |
c906108c | 5205 | |
145b16a9 UW |
5206 | switch (what) |
5207 | { | |
5208 | case IP_MINIMAL: | |
5209 | break; | |
5210 | ||
5211 | case IP_MAPPINGS: | |
5212 | case IP_ALL: | |
5213 | mappings = 1; | |
5214 | break; | |
5215 | ||
5216 | default: | |
5217 | error (_("Not supported on this target.")); | |
5218 | } | |
5219 | ||
c3f6f71d JM |
5220 | old_chain = make_cleanup (null_cleanup, 0); |
5221 | if (args) | |
0fda6bd2 | 5222 | { |
d1a41061 PP |
5223 | argv = gdb_buildargv (args); |
5224 | make_cleanup_freeargv (argv); | |
0fda6bd2 | 5225 | } |
c3f6f71d JM |
5226 | while (argv != NULL && *argv != NULL) |
5227 | { | |
5228 | if (isdigit (argv[0][0])) | |
5229 | { | |
5230 | pid = strtoul (argv[0], &tmp, 10); | |
5231 | if (*tmp == '/') | |
5232 | tid = strtoul (++tmp, NULL, 10); | |
5233 | } | |
5234 | else if (argv[0][0] == '/') | |
5235 | { | |
5236 | tid = strtoul (argv[0] + 1, NULL, 10); | |
5237 | } | |
c3f6f71d JM |
5238 | argv++; |
5239 | } | |
5240 | if (pid == 0) | |
39f77062 | 5241 | pid = PIDGET (inferior_ptid); |
c3f6f71d | 5242 | if (pid == 0) |
8a3fe4f8 | 5243 | error (_("No current process: you must name one.")); |
c3f6f71d | 5244 | else |
c906108c | 5245 | { |
c3f6f71d | 5246 | /* Have pid, will travel. |
0df8b418 | 5247 | First see if it's a process we're already debugging. */ |
c3f6f71d JM |
5248 | process = find_procinfo (pid, 0); |
5249 | if (process == NULL) | |
5250 | { | |
19958708 | 5251 | /* No. So open a procinfo for it, but |
c3f6f71d JM |
5252 | remember to close it again when finished. */ |
5253 | process = create_procinfo (pid, 0); | |
004527cb | 5254 | make_cleanup (do_destroy_procinfo_cleanup, process); |
c3f6f71d JM |
5255 | if (!open_procinfo_files (process, FD_CTL)) |
5256 | proc_error (process, "info proc, open_procinfo_files", __LINE__); | |
5257 | } | |
c906108c | 5258 | } |
c3f6f71d JM |
5259 | if (tid != 0) |
5260 | thread = create_procinfo (pid, tid); | |
5261 | ||
5262 | if (process) | |
5263 | { | |
a3f17187 | 5264 | printf_filtered (_("process %d flags:\n"), process->pid); |
c3f6f71d JM |
5265 | proc_prettyprint_flags (proc_flags (process), 1); |
5266 | if (proc_flags (process) & (PR_STOPPED | PR_ISTOP)) | |
5267 | proc_prettyprint_why (proc_why (process), proc_what (process), 1); | |
5268 | if (proc_get_nthreads (process) > 1) | |
19958708 | 5269 | printf_filtered ("Process has %d threads.\n", |
c3f6f71d JM |
5270 | proc_get_nthreads (process)); |
5271 | } | |
5272 | if (thread) | |
5273 | { | |
a3f17187 | 5274 | printf_filtered (_("thread %d flags:\n"), thread->tid); |
c3f6f71d JM |
5275 | proc_prettyprint_flags (proc_flags (thread), 1); |
5276 | if (proc_flags (thread) & (PR_STOPPED | PR_ISTOP)) | |
5277 | proc_prettyprint_why (proc_why (thread), proc_what (thread), 1); | |
5278 | } | |
5279 | ||
388faa48 MS |
5280 | if (mappings) |
5281 | { | |
5282 | info_proc_mappings (process, 0); | |
5283 | } | |
5284 | ||
c3f6f71d | 5285 | do_cleanups (old_chain); |
c906108c SS |
5286 | } |
5287 | ||
9185ddce JB |
5288 | /* Modify the status of the system call identified by SYSCALLNUM in |
5289 | the set of syscalls that are currently traced/debugged. | |
5290 | ||
5291 | If ENTRY_OR_EXIT is set to PR_SYSENTRY, then the entry syscalls set | |
0df8b418 | 5292 | will be updated. Otherwise, the exit syscalls set will be updated. |
9185ddce | 5293 | |
0df8b418 | 5294 | If MODE is FLAG_SET, then traces will be enabled. Otherwise, they |
9185ddce JB |
5295 | will be disabled. */ |
5296 | ||
5297 | static void | |
5298 | proc_trace_syscalls_1 (procinfo *pi, int syscallnum, int entry_or_exit, | |
77382aee | 5299 | int mode, int from_tty) |
9185ddce JB |
5300 | { |
5301 | sysset_t *sysset; | |
77382aee | 5302 | |
9185ddce JB |
5303 | if (entry_or_exit == PR_SYSENTRY) |
5304 | sysset = proc_get_traced_sysentry (pi, NULL); | |
5305 | else | |
5306 | sysset = proc_get_traced_sysexit (pi, NULL); | |
5307 | ||
5308 | if (sysset == NULL) | |
5309 | proc_error (pi, "proc-trace, get_traced_sysset", __LINE__); | |
5310 | ||
5311 | if (mode == FLAG_SET) | |
5312 | gdb_praddsysset (sysset, syscallnum); | |
5313 | else | |
5314 | gdb_prdelsysset (sysset, syscallnum); | |
5315 | ||
5316 | if (entry_or_exit == PR_SYSENTRY) | |
5317 | { | |
5318 | if (!proc_set_traced_sysentry (pi, sysset)) | |
77382aee | 5319 | proc_error (pi, "proc-trace, set_traced_sysentry", __LINE__); |
9185ddce JB |
5320 | } |
5321 | else | |
5322 | { | |
5323 | if (!proc_set_traced_sysexit (pi, sysset)) | |
77382aee | 5324 | proc_error (pi, "proc-trace, set_traced_sysexit", __LINE__); |
9185ddce JB |
5325 | } |
5326 | } | |
5327 | ||
c3f6f71d | 5328 | static void |
fba45db2 | 5329 | proc_trace_syscalls (char *args, int from_tty, int entry_or_exit, int mode) |
c906108c | 5330 | { |
c3f6f71d | 5331 | procinfo *pi; |
c906108c | 5332 | |
39f77062 | 5333 | if (PIDGET (inferior_ptid) <= 0) |
8a3fe4f8 | 5334 | error (_("you must be debugging a process to use this command.")); |
c906108c | 5335 | |
c3f6f71d | 5336 | if (args == NULL || args[0] == 0) |
e2e0b3e5 | 5337 | error_no_arg (_("system call to trace")); |
c3f6f71d | 5338 | |
39f77062 | 5339 | pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); |
c3f6f71d JM |
5340 | if (isdigit (args[0])) |
5341 | { | |
9185ddce | 5342 | const int syscallnum = atoi (args); |
c906108c | 5343 | |
9185ddce | 5344 | proc_trace_syscalls_1 (pi, syscallnum, entry_or_exit, mode, from_tty); |
c3f6f71d JM |
5345 | } |
5346 | } | |
5347 | ||
19958708 | 5348 | static void |
fba45db2 | 5349 | proc_trace_sysentry_cmd (char *args, int from_tty) |
c906108c | 5350 | { |
c3f6f71d JM |
5351 | proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_SET); |
5352 | } | |
c906108c | 5353 | |
19958708 | 5354 | static void |
fba45db2 | 5355 | proc_trace_sysexit_cmd (char *args, int from_tty) |
c3f6f71d JM |
5356 | { |
5357 | proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_SET); | |
c906108c | 5358 | } |
c906108c | 5359 | |
19958708 | 5360 | static void |
fba45db2 | 5361 | proc_untrace_sysentry_cmd (char *args, int from_tty) |
c3f6f71d JM |
5362 | { |
5363 | proc_trace_syscalls (args, from_tty, PR_SYSENTRY, FLAG_RESET); | |
5364 | } | |
5365 | ||
19958708 | 5366 | static void |
fba45db2 | 5367 | proc_untrace_sysexit_cmd (char *args, int from_tty) |
c906108c | 5368 | { |
c3f6f71d JM |
5369 | proc_trace_syscalls (args, from_tty, PR_SYSEXIT, FLAG_RESET); |
5370 | } | |
c906108c | 5371 | |
c906108c | 5372 | |
a0911fd0 MR |
5373 | /* Provide a prototype to silence -Wmissing-prototypes. */ |
5374 | extern void _initialize_procfs (void); | |
5375 | ||
c906108c | 5376 | void |
fba45db2 | 5377 | _initialize_procfs (void) |
c906108c | 5378 | { |
27087a3d JB |
5379 | observer_attach_inferior_created (procfs_inferior_created); |
5380 | ||
19958708 | 5381 | add_com ("proc-trace-entry", no_class, proc_trace_sysentry_cmd, |
1bedd215 | 5382 | _("Give a trace of entries into the syscall.")); |
19958708 | 5383 | add_com ("proc-trace-exit", no_class, proc_trace_sysexit_cmd, |
1bedd215 | 5384 | _("Give a trace of exits from the syscall.")); |
19958708 | 5385 | add_com ("proc-untrace-entry", no_class, proc_untrace_sysentry_cmd, |
1bedd215 | 5386 | _("Cancel a trace of entries into the syscall.")); |
19958708 | 5387 | add_com ("proc-untrace-exit", no_class, proc_untrace_sysexit_cmd, |
1bedd215 | 5388 | _("Cancel a trace of exits from the syscall.")); |
c3f6f71d JM |
5389 | } |
5390 | ||
5391 | /* =================== END, GDB "MODULE" =================== */ | |
5392 | ||
5393 | ||
5394 | ||
77382aee PA |
5395 | /* miscellaneous stubs: */ |
5396 | ||
5397 | /* The following satisfy a few random symbols mostly created by the | |
5398 | solaris threads implementation, which I will chase down later. */ | |
c3f6f71d | 5399 | |
77382aee PA |
5400 | /* Return a pid for which we guarantee we will be able to find a |
5401 | 'live' procinfo. */ | |
c3f6f71d | 5402 | |
39f77062 | 5403 | ptid_t |
fba45db2 | 5404 | procfs_first_available (void) |
c3f6f71d | 5405 | { |
39f77062 | 5406 | return pid_to_ptid (procinfo_list ? procinfo_list->pid : -1); |
c3f6f71d | 5407 | } |
be4d1333 MS |
5408 | |
5409 | /* =================== GCORE .NOTE "MODULE" =================== */ | |
65554fef MS |
5410 | #if defined (UNIXWARE) || defined (PIOCOPENLWP) || defined (PCAGENT) |
5411 | /* gcore only implemented on solaris and unixware (so far) */ | |
be4d1333 MS |
5412 | |
5413 | static char * | |
19958708 | 5414 | procfs_do_thread_registers (bfd *obfd, ptid_t ptid, |
2020b7ab | 5415 | char *note_data, int *note_size, |
2ea28649 | 5416 | enum gdb_signal stop_signal) |
be4d1333 | 5417 | { |
594f7785 | 5418 | struct regcache *regcache = get_thread_regcache (ptid); |
be4d1333 MS |
5419 | gdb_gregset_t gregs; |
5420 | gdb_fpregset_t fpregs; | |
5421 | unsigned long merged_pid; | |
75125405 | 5422 | struct cleanup *old_chain; |
be4d1333 MS |
5423 | |
5424 | merged_pid = TIDGET (ptid) << 16 | PIDGET (ptid); | |
5425 | ||
75125405 DJ |
5426 | /* This part is the old method for fetching registers. |
5427 | It should be replaced by the newer one using regsets | |
5428 | once it is implemented in this platform: | |
0df8b418 | 5429 | gdbarch_regset_from_core_section() and regset->collect_regset(). */ |
75125405 DJ |
5430 | |
5431 | old_chain = save_inferior_ptid (); | |
5432 | inferior_ptid = ptid; | |
5433 | target_fetch_registers (regcache, -1); | |
5434 | ||
594f7785 | 5435 | fill_gregset (regcache, &gregs, -1); |
4c490650 | 5436 | #if defined (NEW_PROC_API) |
65554fef MS |
5437 | note_data = (char *) elfcore_write_lwpstatus (obfd, |
5438 | note_data, | |
5439 | note_size, | |
19958708 | 5440 | merged_pid, |
65554fef MS |
5441 | stop_signal, |
5442 | &gregs); | |
5443 | #else | |
be4d1333 | 5444 | note_data = (char *) elfcore_write_prstatus (obfd, |
65554fef MS |
5445 | note_data, |
5446 | note_size, | |
19958708 | 5447 | merged_pid, |
be4d1333 | 5448 | stop_signal, |
65554fef MS |
5449 | &gregs); |
5450 | #endif | |
594f7785 | 5451 | fill_fpregset (regcache, &fpregs, -1); |
be4d1333 MS |
5452 | note_data = (char *) elfcore_write_prfpreg (obfd, |
5453 | note_data, | |
5454 | note_size, | |
5455 | &fpregs, | |
5456 | sizeof (fpregs)); | |
75125405 DJ |
5457 | |
5458 | do_cleanups (old_chain); | |
5459 | ||
be4d1333 MS |
5460 | return note_data; |
5461 | } | |
5462 | ||
5463 | struct procfs_corefile_thread_data { | |
5464 | bfd *obfd; | |
5465 | char *note_data; | |
5466 | int *note_size; | |
2ea28649 | 5467 | enum gdb_signal stop_signal; |
be4d1333 MS |
5468 | }; |
5469 | ||
5470 | static int | |
65554fef | 5471 | procfs_corefile_thread_callback (procinfo *pi, procinfo *thread, void *data) |
be4d1333 MS |
5472 | { |
5473 | struct procfs_corefile_thread_data *args = data; | |
be4d1333 | 5474 | |
2689673f | 5475 | if (pi != NULL) |
be4d1333 | 5476 | { |
75125405 | 5477 | ptid_t ptid = MERGEPID (pi->pid, thread->tid); |
f4a14ae6 | 5478 | |
75125405 | 5479 | args->note_data = procfs_do_thread_registers (args->obfd, ptid, |
19958708 | 5480 | args->note_data, |
2020b7ab PA |
5481 | args->note_size, |
5482 | args->stop_signal); | |
be4d1333 MS |
5483 | } |
5484 | return 0; | |
5485 | } | |
5486 | ||
a223f1e7 JB |
5487 | static int |
5488 | find_signalled_thread (struct thread_info *info, void *data) | |
5489 | { | |
a493e3e2 | 5490 | if (info->suspend.stop_signal != GDB_SIGNAL_0 |
a223f1e7 JB |
5491 | && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid)) |
5492 | return 1; | |
5493 | ||
5494 | return 0; | |
5495 | } | |
5496 | ||
2ea28649 | 5497 | static enum gdb_signal |
a223f1e7 JB |
5498 | find_stop_signal (void) |
5499 | { | |
5500 | struct thread_info *info = | |
5501 | iterate_over_threads (find_signalled_thread, NULL); | |
5502 | ||
5503 | if (info) | |
16c381f0 | 5504 | return info->suspend.stop_signal; |
a223f1e7 | 5505 | else |
a493e3e2 | 5506 | return GDB_SIGNAL_0; |
a223f1e7 JB |
5507 | } |
5508 | ||
be4d1333 MS |
5509 | static char * |
5510 | procfs_make_note_section (bfd *obfd, int *note_size) | |
5511 | { | |
5512 | struct cleanup *old_chain; | |
5513 | gdb_gregset_t gregs; | |
5514 | gdb_fpregset_t fpregs; | |
5515 | char fname[16] = {'\0'}; | |
5516 | char psargs[80] = {'\0'}; | |
5517 | procinfo *pi = find_procinfo_or_die (PIDGET (inferior_ptid), 0); | |
5518 | char *note_data = NULL; | |
6dbdc4a3 | 5519 | char *inf_args; |
be4d1333 | 5520 | struct procfs_corefile_thread_data thread_args; |
0b62613e | 5521 | gdb_byte *auxv; |
4e73f23d | 5522 | int auxv_len; |
2ea28649 | 5523 | enum gdb_signal stop_signal; |
be4d1333 MS |
5524 | |
5525 | if (get_exec_file (0)) | |
5526 | { | |
9f37bbcc | 5527 | strncpy (fname, lbasename (get_exec_file (0)), sizeof (fname)); |
4e2af517 JM |
5528 | fname[sizeof (fname) - 1] = 0; |
5529 | strncpy (psargs, get_exec_file (0), sizeof (psargs)); | |
5530 | psargs[sizeof (psargs) - 1] = 0; | |
6dbdc4a3 MS |
5531 | |
5532 | inf_args = get_inferior_args (); | |
5533 | if (inf_args && *inf_args && | |
5534 | strlen (inf_args) < ((int) sizeof (psargs) - (int) strlen (psargs))) | |
be4d1333 | 5535 | { |
19958708 | 5536 | strncat (psargs, " ", |
be4d1333 | 5537 | sizeof (psargs) - strlen (psargs)); |
19958708 | 5538 | strncat (psargs, inf_args, |
be4d1333 MS |
5539 | sizeof (psargs) - strlen (psargs)); |
5540 | } | |
5541 | } | |
5542 | ||
19958708 RM |
5543 | note_data = (char *) elfcore_write_prpsinfo (obfd, |
5544 | note_data, | |
5545 | note_size, | |
5546 | fname, | |
be4d1333 MS |
5547 | psargs); |
5548 | ||
651c8d2d PA |
5549 | stop_signal = find_stop_signal (); |
5550 | ||
8903eff6 | 5551 | #ifdef UNIXWARE |
594f7785 | 5552 | fill_gregset (get_current_regcache (), &gregs, -1); |
19958708 RM |
5553 | note_data = elfcore_write_pstatus (obfd, note_data, note_size, |
5554 | PIDGET (inferior_ptid), | |
65554fef MS |
5555 | stop_signal, &gregs); |
5556 | #endif | |
5557 | ||
be4d1333 MS |
5558 | thread_args.obfd = obfd; |
5559 | thread_args.note_data = note_data; | |
5560 | thread_args.note_size = note_size; | |
651c8d2d | 5561 | thread_args.stop_signal = stop_signal; |
3e43a32a MS |
5562 | proc_iterate_over_threads (pi, procfs_corefile_thread_callback, |
5563 | &thread_args); | |
65554fef | 5564 | |
2689673f PA |
5565 | /* There should be always at least one thread. */ |
5566 | gdb_assert (thread_args.note_data != note_data); | |
5567 | note_data = thread_args.note_data; | |
be4d1333 | 5568 | |
13547ab6 DJ |
5569 | auxv_len = target_read_alloc (¤t_target, TARGET_OBJECT_AUXV, |
5570 | NULL, &auxv); | |
4e73f23d RM |
5571 | if (auxv_len > 0) |
5572 | { | |
5573 | note_data = elfcore_write_note (obfd, note_data, note_size, | |
5574 | "CORE", NT_AUXV, auxv, auxv_len); | |
5575 | xfree (auxv); | |
5576 | } | |
5577 | ||
be4d1333 MS |
5578 | make_cleanup (xfree, note_data); |
5579 | return note_data; | |
5580 | } | |
65554fef MS |
5581 | #else /* !(Solaris or Unixware) */ |
5582 | static char * | |
5583 | procfs_make_note_section (bfd *obfd, int *note_size) | |
5584 | { | |
8a3fe4f8 | 5585 | error (_("gcore not implemented for this host.")); |
65554fef MS |
5586 | return NULL; /* lint */ |
5587 | } | |
5588 | #endif /* Solaris or Unixware */ | |
be4d1333 | 5589 | /* =================== END GCORE .NOTE "MODULE" =================== */ |