Commit | Line | Data |
---|---|---|
35f5886e | 1 | /* Machine independent support for SVR4 /proc (process file system) for GDB. |
ec8ceca3 | 2 | Copyright 1991, 1992 Free Software Foundation, Inc. |
35f5886e FF |
3 | Written by Fred Fish at Cygnus Support. |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | ||
22 | /* N O T E S | |
23 | ||
24 | For information on the details of using /proc consult section proc(4) | |
25 | in the UNIX System V Release 4 System Administrator's Reference Manual. | |
26 | ||
27 | The general register and floating point register sets are manipulated by | |
28 | separate ioctl's. This file makes the assumption that if FP0_REGNUM is | |
29 | defined, then support for the floating point register set is desired, | |
30 | regardless of whether or not the actual target has floating point hardware. | |
31 | ||
32 | */ | |
33 | ||
34 | ||
5129100c | 35 | #include "defs.h" |
35f5886e | 36 | |
407a8389 | 37 | #include <time.h> |
35f5886e FF |
38 | #include <sys/procfs.h> |
39 | #include <fcntl.h> | |
40 | #include <errno.h> | |
51b57ded | 41 | #include <string.h> |
de43d7d0 SG |
42 | #include <stropts.h> |
43 | #include <poll.h> | |
35f5886e | 44 | |
35f5886e FF |
45 | #include "inferior.h" |
46 | #include "target.h" | |
51b57ded | 47 | #include "command.h" |
3fbdd536 | 48 | #include "gdbcore.h" |
cc221e76 FF |
49 | |
50 | #define MAX_SYSCALLS 256 /* Maximum number of syscalls for table */ | |
35f5886e FF |
51 | |
52 | #ifndef PROC_NAME_FMT | |
6c316cfd | 53 | #define PROC_NAME_FMT "/proc/%05d" |
35f5886e FF |
54 | #endif |
55 | ||
3fbdd536 JG |
56 | extern struct target_ops procfs_ops; /* Forward declaration */ |
57 | ||
35f5886e FF |
58 | #if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */ |
59 | CORE_ADDR kernel_u_addr; | |
60 | #endif | |
61 | ||
cc221e76 FF |
62 | #ifdef BROKEN_SIGINFO_H /* Workaround broken SGS <sys/siginfo.h> */ |
63 | #undef si_pid | |
64 | #define si_pid _data._proc.pid | |
65 | #undef si_uid | |
66 | #define si_uid _data._proc._pdata._kill.uid | |
67 | #endif /* BROKEN_SIGINFO_H */ | |
68 | ||
35f5886e FF |
69 | /* All access to the inferior, either one started by gdb or one that has |
70 | been attached to, is controlled by an instance of a procinfo structure, | |
71 | defined below. Since gdb currently only handles one inferior at a time, | |
a39ad5ce FF |
72 | the procinfo structure for the inferior is statically allocated and |
73 | only one exists at any given time. There is a separate procinfo | |
74 | structure for use by the "info proc" command, so that we can print | |
75 | useful information about any random process without interfering with | |
76 | the inferior's procinfo information. */ | |
35f5886e FF |
77 | |
78 | struct procinfo { | |
de43d7d0 | 79 | struct procinfo *next; |
35f5886e FF |
80 | int pid; /* Process ID of inferior */ |
81 | int fd; /* File descriptor for /proc entry */ | |
82 | char *pathname; /* Pathname to /proc entry */ | |
de43d7d0 | 83 | int had_event; /* poll/select says something happened */ |
35f5886e | 84 | int was_stopped; /* Nonzero if was stopped prior to attach */ |
d65eee73 | 85 | int nopass_next_sigstop; /* Don't pass a sigstop on next resume */ |
35f5886e FF |
86 | prrun_t prrun; /* Control state when it is run */ |
87 | prstatus_t prstatus; /* Current process status info */ | |
88 | gregset_t gregset; /* General register set */ | |
89 | fpregset_t fpregset; /* Floating point register set */ | |
90 | fltset_t fltset; /* Current traced hardware fault set */ | |
91 | sigset_t trace; /* Current traced signal set */ | |
92 | sysset_t exitset; /* Current traced system call exit set */ | |
93 | sysset_t entryset; /* Current traced system call entry set */ | |
cc221e76 FF |
94 | fltset_t saved_fltset; /* Saved traced hardware fault set */ |
95 | sigset_t saved_trace; /* Saved traced signal set */ | |
96 | sigset_t saved_sighold; /* Saved held signal set */ | |
97 | sysset_t saved_exitset; /* Saved traced system call exit set */ | |
98 | sysset_t saved_entryset; /* Saved traced system call entry set */ | |
a39ad5ce FF |
99 | }; |
100 | ||
de43d7d0 SG |
101 | /* List of inferior process information */ |
102 | static struct procinfo *procinfo_list = NULL; | |
103 | ||
104 | static struct pollfd *poll_list; /* pollfds used for waiting on /proc */ | |
105 | ||
106 | static int num_poll_list = 0; /* Number of entries in poll_list */ | |
107 | ||
108 | static int last_resume_pid = -1; /* Last pid used with procfs_resume */ | |
35f5886e | 109 | |
cc221e76 FF |
110 | /* Much of the information used in the /proc interface, particularly for |
111 | printing status information, is kept as tables of structures of the | |
112 | following form. These tables can be used to map numeric values to | |
113 | their symbolic names and to a string that describes their specific use. */ | |
114 | ||
115 | struct trans { | |
116 | int value; /* The numeric value */ | |
117 | char *name; /* The equivalent symbolic value */ | |
118 | char *desc; /* Short description of value */ | |
119 | }; | |
120 | ||
121 | /* Translate bits in the pr_flags member of the prstatus structure, into the | |
122 | names and desc information. */ | |
123 | ||
124 | static struct trans pr_flag_table[] = | |
125 | { | |
126 | #if defined (PR_STOPPED) | |
127 | PR_STOPPED, "PR_STOPPED", "Process is stopped", | |
128 | #endif | |
129 | #if defined (PR_ISTOP) | |
130 | PR_ISTOP, "PR_ISTOP", "Stopped on an event of interest", | |
131 | #endif | |
132 | #if defined (PR_DSTOP) | |
133 | PR_DSTOP, "PR_DSTOP", "A stop directive is in effect", | |
134 | #endif | |
135 | #if defined (PR_ASLEEP) | |
136 | PR_ASLEEP, "PR_ASLEEP", "Sleeping in an interruptible system call", | |
137 | #endif | |
138 | #if defined (PR_FORK) | |
139 | PR_FORK, "PR_FORK", "Inherit-on-fork is in effect", | |
140 | #endif | |
141 | #if defined (PR_RLC) | |
142 | PR_RLC, "PR_RLC", "Run-on-last-close is in effect", | |
143 | #endif | |
144 | #if defined (PR_PTRACE) | |
145 | PR_PTRACE, "PR_PTRACE", "Process is being controlled by ptrace", | |
146 | #endif | |
147 | #if defined (PR_PCINVAL) | |
148 | PR_PCINVAL, "PR_PCINVAL", "PC refers to an invalid virtual address", | |
149 | #endif | |
150 | #if defined (PR_ISSYS) | |
151 | PR_ISSYS, "PR_ISSYS", "Is a system process", | |
5c1c5e67 FF |
152 | #endif |
153 | #if defined (PR_STEP) | |
154 | PR_STEP, "PR_STEP", "Process has single step pending", | |
155 | #endif | |
156 | #if defined (PR_KLC) | |
157 | PR_KLC, "PR_KLC", "Kill-on-last-close is in effect", | |
158 | #endif | |
159 | #if defined (PR_ASYNC) | |
160 | PR_ASYNC, "PR_ASYNC", "Asynchronous stop is in effect", | |
161 | #endif | |
162 | #if defined (PR_PCOMPAT) | |
163 | PR_PCOMPAT, "PR_PCOMPAT", "Ptrace compatibility mode in effect", | |
cc221e76 FF |
164 | #endif |
165 | 0, NULL, NULL | |
166 | }; | |
167 | ||
168 | /* Translate values in the pr_why field of the prstatus struct. */ | |
169 | ||
170 | static struct trans pr_why_table[] = | |
171 | { | |
172 | #if defined (PR_REQUESTED) | |
173 | PR_REQUESTED, "PR_REQUESTED", "Directed to stop via PIOCSTOP/PIOCWSTOP", | |
174 | #endif | |
175 | #if defined (PR_SIGNALLED) | |
176 | PR_SIGNALLED, "PR_SIGNALLED", "Receipt of a traced signal", | |
177 | #endif | |
178 | #if defined (PR_FAULTED) | |
179 | PR_FAULTED, "PR_FAULTED", "Incurred a traced hardware fault", | |
180 | #endif | |
181 | #if defined (PR_SYSENTRY) | |
182 | PR_SYSENTRY, "PR_SYSENTRY", "Entry to a traced system call", | |
183 | #endif | |
184 | #if defined (PR_SYSEXIT) | |
185 | PR_SYSEXIT, "PR_SYSEXIT", "Exit from a traced system call", | |
186 | #endif | |
187 | #if defined (PR_JOBCONTROL) | |
188 | PR_JOBCONTROL, "PR_JOBCONTROL", "Default job control stop signal action", | |
5c1c5e67 FF |
189 | #endif |
190 | #if defined (PR_SUSPENDED) | |
191 | PR_SUSPENDED, "PR_SUSPENDED", "Process suspended", | |
cc221e76 FF |
192 | #endif |
193 | 0, NULL, NULL | |
194 | }; | |
195 | ||
196 | /* Hardware fault translation table. */ | |
197 | ||
198 | static struct trans faults_table[] = | |
199 | { | |
200 | #if defined (FLTILL) | |
201 | FLTILL, "FLTILL", "Illegal instruction", | |
202 | #endif | |
203 | #if defined (FLTPRIV) | |
204 | FLTPRIV, "FLTPRIV", "Privileged instruction", | |
205 | #endif | |
206 | #if defined (FLTBPT) | |
207 | FLTBPT, "FLTBPT", "Breakpoint trap", | |
208 | #endif | |
209 | #if defined (FLTTRACE) | |
210 | FLTTRACE, "FLTTRACE", "Trace trap", | |
211 | #endif | |
212 | #if defined (FLTACCESS) | |
213 | FLTACCESS, "FLTACCESS", "Memory access fault", | |
214 | #endif | |
215 | #if defined (FLTBOUNDS) | |
216 | FLTBOUNDS, "FLTBOUNDS", "Memory bounds violation", | |
217 | #endif | |
218 | #if defined (FLTIOVF) | |
219 | FLTIOVF, "FLTIOVF", "Integer overflow", | |
220 | #endif | |
221 | #if defined (FLTIZDIV) | |
222 | FLTIZDIV, "FLTIZDIV", "Integer zero divide", | |
223 | #endif | |
224 | #if defined (FLTFPE) | |
225 | FLTFPE, "FLTFPE", "Floating-point exception", | |
226 | #endif | |
227 | #if defined (FLTSTACK) | |
228 | FLTSTACK, "FLTSTACK", "Unrecoverable stack fault", | |
229 | #endif | |
230 | #if defined (FLTPAGE) | |
231 | FLTPAGE, "FLTPAGE", "Recoverable page fault", | |
232 | #endif | |
233 | 0, NULL, NULL | |
234 | }; | |
235 | ||
236 | /* Translation table for signal generation information. See UNIX System | |
237 | V Release 4 Programmer's Reference Manual, siginfo(5). */ | |
238 | ||
239 | static struct sigcode { | |
240 | int signo; | |
241 | int code; | |
242 | char *codename; | |
243 | char *desc; | |
244 | } siginfo_table[] = { | |
245 | #if defined (SIGILL) && defined (ILL_ILLOPC) | |
246 | SIGILL, ILL_ILLOPC, "ILL_ILLOPC", "Illegal opcode", | |
247 | #endif | |
248 | #if defined (SIGILL) && defined (ILL_ILLOPN) | |
249 | SIGILL, ILL_ILLOPN, "ILL_ILLOPN", "Illegal operand", | |
250 | #endif | |
251 | #if defined (SIGILL) && defined (ILL_ILLADR) | |
252 | SIGILL, ILL_ILLADR, "ILL_ILLADR", "Illegal addressing mode", | |
253 | #endif | |
254 | #if defined (SIGILL) && defined (ILL_ILLTRP) | |
255 | SIGILL, ILL_ILLTRP, "ILL_ILLTRP", "Illegal trap", | |
256 | #endif | |
257 | #if defined (SIGILL) && defined (ILL_PRVOPC) | |
258 | SIGILL, ILL_PRVOPC, "ILL_PRVOPC", "Privileged opcode", | |
259 | #endif | |
260 | #if defined (SIGILL) && defined (ILL_PRVREG) | |
261 | SIGILL, ILL_PRVREG, "ILL_PRVREG", "Privileged register", | |
262 | #endif | |
263 | #if defined (SIGILL) && defined (ILL_COPROC) | |
264 | SIGILL, ILL_COPROC, "ILL_COPROC", "Coprocessor error", | |
265 | #endif | |
266 | #if defined (SIGILL) && defined (ILL_BADSTK) | |
267 | SIGILL, ILL_BADSTK, "ILL_BADSTK", "Internal stack error", | |
268 | #endif | |
269 | #if defined (SIGFPE) && defined (FPE_INTDIV) | |
270 | SIGFPE, FPE_INTDIV, "FPE_INTDIV", "Integer divide by zero", | |
271 | #endif | |
272 | #if defined (SIGFPE) && defined (FPE_INTOVF) | |
273 | SIGFPE, FPE_INTOVF, "FPE_INTOVF", "Integer overflow", | |
274 | #endif | |
275 | #if defined (SIGFPE) && defined (FPE_FLTDIV) | |
276 | SIGFPE, FPE_FLTDIV, "FPE_FLTDIV", "Floating point divide by zero", | |
277 | #endif | |
278 | #if defined (SIGFPE) && defined (FPE_FLTOVF) | |
279 | SIGFPE, FPE_FLTOVF, "FPE_FLTOVF", "Floating point overflow", | |
280 | #endif | |
281 | #if defined (SIGFPE) && defined (FPE_FLTUND) | |
282 | SIGFPE, FPE_FLTUND, "FPE_FLTUND", "Floating point underflow", | |
283 | #endif | |
284 | #if defined (SIGFPE) && defined (FPE_FLTRES) | |
285 | SIGFPE, FPE_FLTRES, "FPE_FLTRES", "Floating point inexact result", | |
286 | #endif | |
287 | #if defined (SIGFPE) && defined (FPE_FLTINV) | |
288 | SIGFPE, FPE_FLTINV, "FPE_FLTINV", "Invalid floating point operation", | |
289 | #endif | |
290 | #if defined (SIGFPE) && defined (FPE_FLTSUB) | |
291 | SIGFPE, FPE_FLTSUB, "FPE_FLTSUB", "Subscript out of range", | |
292 | #endif | |
293 | #if defined (SIGSEGV) && defined (SEGV_MAPERR) | |
294 | SIGSEGV, SEGV_MAPERR, "SEGV_MAPERR", "Address not mapped to object", | |
295 | #endif | |
296 | #if defined (SIGSEGV) && defined (SEGV_ACCERR) | |
297 | SIGSEGV, SEGV_ACCERR, "SEGV_ACCERR", "Invalid permissions for object", | |
298 | #endif | |
299 | #if defined (SIGBUS) && defined (BUS_ADRALN) | |
300 | SIGBUS, BUS_ADRALN, "BUS_ADRALN", "Invalid address alignment", | |
301 | #endif | |
302 | #if defined (SIGBUS) && defined (BUS_ADRERR) | |
303 | SIGBUS, BUS_ADRERR, "BUS_ADRERR", "Non-existent physical address", | |
304 | #endif | |
305 | #if defined (SIGBUS) && defined (BUS_OBJERR) | |
306 | SIGBUS, BUS_OBJERR, "BUS_OBJERR", "Object specific hardware error", | |
307 | #endif | |
308 | #if defined (SIGTRAP) && defined (TRAP_BRKPT) | |
309 | SIGTRAP, TRAP_BRKPT, "TRAP_BRKPT", "Process breakpoint", | |
310 | #endif | |
311 | #if defined (SIGTRAP) && defined (TRAP_TRACE) | |
312 | SIGTRAP, TRAP_TRACE, "TRAP_TRACE", "Process trace trap", | |
313 | #endif | |
314 | #if defined (SIGCLD) && defined (CLD_EXITED) | |
315 | SIGCLD, CLD_EXITED, "CLD_EXITED", "Child has exited", | |
316 | #endif | |
317 | #if defined (SIGCLD) && defined (CLD_KILLED) | |
318 | SIGCLD, CLD_KILLED, "CLD_KILLED", "Child was killed", | |
319 | #endif | |
320 | #if defined (SIGCLD) && defined (CLD_DUMPED) | |
321 | SIGCLD, CLD_DUMPED, "CLD_DUMPED", "Child has terminated abnormally", | |
322 | #endif | |
323 | #if defined (SIGCLD) && defined (CLD_TRAPPED) | |
324 | SIGCLD, CLD_TRAPPED, "CLD_TRAPPED", "Traced child has trapped", | |
325 | #endif | |
326 | #if defined (SIGCLD) && defined (CLD_STOPPED) | |
327 | SIGCLD, CLD_STOPPED, "CLD_STOPPED", "Child has stopped", | |
328 | #endif | |
329 | #if defined (SIGCLD) && defined (CLD_CONTINUED) | |
330 | SIGCLD, CLD_CONTINUED, "CLD_CONTINUED", "Stopped child had continued", | |
331 | #endif | |
332 | #if defined (SIGPOLL) && defined (POLL_IN) | |
333 | SIGPOLL, POLL_IN, "POLL_IN", "Input input available", | |
334 | #endif | |
335 | #if defined (SIGPOLL) && defined (POLL_OUT) | |
336 | SIGPOLL, POLL_OUT, "POLL_OUT", "Output buffers available", | |
337 | #endif | |
338 | #if defined (SIGPOLL) && defined (POLL_MSG) | |
339 | SIGPOLL, POLL_MSG, "POLL_MSG", "Input message available", | |
340 | #endif | |
341 | #if defined (SIGPOLL) && defined (POLL_ERR) | |
342 | SIGPOLL, POLL_ERR, "POLL_ERR", "I/O error", | |
343 | #endif | |
344 | #if defined (SIGPOLL) && defined (POLL_PRI) | |
345 | SIGPOLL, POLL_PRI, "POLL_PRI", "High priority input available", | |
346 | #endif | |
347 | #if defined (SIGPOLL) && defined (POLL_HUP) | |
348 | SIGPOLL, POLL_HUP, "POLL_HUP", "Device disconnected", | |
349 | #endif | |
350 | 0, 0, NULL, NULL | |
351 | }; | |
352 | ||
cc221e76 FF |
353 | static char *syscall_table[MAX_SYSCALLS]; |
354 | ||
1ab3bf1b JG |
355 | /* Prototypes for local functions */ |
356 | ||
6b801388 FF |
357 | static void |
358 | set_proc_siginfo PARAMS ((struct procinfo *, int)); | |
359 | ||
cc221e76 FF |
360 | static void |
361 | init_syscall_table PARAMS ((void)); | |
362 | ||
363 | static char * | |
364 | syscallname PARAMS ((int)); | |
365 | ||
366 | static char * | |
367 | signalname PARAMS ((int)); | |
368 | ||
4ace50a5 FF |
369 | static char * |
370 | errnoname PARAMS ((int)); | |
371 | ||
1ab3bf1b | 372 | static int |
de43d7d0 | 373 | proc_address_to_fd PARAMS ((struct procinfo *, CORE_ADDR, int)); |
1ab3bf1b JG |
374 | |
375 | static int | |
ec8ceca3 | 376 | open_proc_file PARAMS ((int, struct procinfo *, int)); |
1ab3bf1b JG |
377 | |
378 | static void | |
379 | close_proc_file PARAMS ((struct procinfo *)); | |
380 | ||
381 | static void | |
de43d7d0 | 382 | unconditionally_kill_inferior PARAMS ((struct procinfo *)); |
1ab3bf1b | 383 | |
de43d7d0 SG |
384 | static NORETURN void |
385 | proc_init_failed PARAMS ((struct procinfo *, char *)); | |
1ab3bf1b JG |
386 | |
387 | static void | |
cc221e76 FF |
388 | info_proc PARAMS ((char *, int)); |
389 | ||
390 | static void | |
391 | info_proc_flags PARAMS ((struct procinfo *, int)); | |
392 | ||
393 | static void | |
394 | info_proc_stop PARAMS ((struct procinfo *, int)); | |
1ab3bf1b JG |
395 | |
396 | static void | |
cc221e76 FF |
397 | info_proc_siginfo PARAMS ((struct procinfo *, int)); |
398 | ||
399 | static void | |
400 | info_proc_syscalls PARAMS ((struct procinfo *, int)); | |
401 | ||
402 | static void | |
403 | info_proc_mappings PARAMS ((struct procinfo *, int)); | |
404 | ||
405 | static void | |
406 | info_proc_signals PARAMS ((struct procinfo *, int)); | |
407 | ||
408 | static void | |
409 | info_proc_faults PARAMS ((struct procinfo *, int)); | |
1ab3bf1b JG |
410 | |
411 | static char * | |
412 | mappingflags PARAMS ((long)); | |
413 | ||
cc221e76 FF |
414 | static char * |
415 | lookupname PARAMS ((struct trans *, unsigned int, char *)); | |
416 | ||
417 | static char * | |
418 | lookupdesc PARAMS ((struct trans *, unsigned int)); | |
419 | ||
3fbdd536 JG |
420 | static int |
421 | do_attach PARAMS ((int pid)); | |
422 | ||
423 | static void | |
424 | do_detach PARAMS ((int siggnal)); | |
425 | ||
426 | static void | |
427 | procfs_create_inferior PARAMS ((char *, char *, char **)); | |
428 | ||
3950a34e | 429 | static void |
952a820e | 430 | procfs_notice_signals PARAMS ((int pid)); |
de43d7d0 SG |
431 | |
432 | static struct procinfo * | |
433 | find_procinfo PARAMS ((pid_t pid, int okfail)); | |
3950a34e | 434 | |
1ab3bf1b JG |
435 | /* External function prototypes that can't be easily included in any |
436 | header file because the args are typedefs in system include files. */ | |
437 | ||
438 | extern void | |
439 | supply_gregset PARAMS ((gregset_t *)); | |
440 | ||
441 | extern void | |
442 | fill_gregset PARAMS ((gregset_t *, int)); | |
443 | ||
444 | extern void | |
445 | supply_fpregset PARAMS ((fpregset_t *)); | |
446 | ||
447 | extern void | |
448 | fill_fpregset PARAMS ((fpregset_t *, int)); | |
35f5886e | 449 | |
cc221e76 FF |
450 | /* |
451 | ||
de43d7d0 SG |
452 | LOCAL FUNCTION |
453 | ||
454 | find_procinfo -- convert a process id to a struct procinfo | |
455 | ||
456 | SYNOPSIS | |
457 | ||
458 | static struct procinfo * find_procinfo (pid_t pid, int okfail); | |
459 | ||
460 | DESCRIPTION | |
461 | ||
462 | Given a process id, look it up in the procinfo chain. Returns | |
463 | a struct procinfo *. If can't find pid, then call error(), | |
464 | unless okfail is set, in which case, return NULL; | |
465 | */ | |
466 | ||
467 | static struct procinfo * | |
468 | find_procinfo (pid, okfail) | |
469 | pid_t pid; | |
470 | int okfail; | |
471 | { | |
472 | struct procinfo *procinfo; | |
473 | ||
474 | for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next) | |
475 | if (procinfo->pid == pid) | |
476 | return procinfo; | |
477 | ||
478 | if (okfail) | |
479 | return NULL; | |
480 | ||
481 | error ("procfs (find_procinfo): Couldn't locate pid %d", pid); | |
482 | } | |
483 | ||
484 | /* | |
485 | ||
7c5d526e SG |
486 | LOCAL MACRO |
487 | ||
488 | current_procinfo -- convert inferior_pid to a struct procinfo | |
489 | ||
490 | SYNOPSIS | |
491 | ||
492 | static struct procinfo * current_procinfo; | |
493 | ||
494 | DESCRIPTION | |
495 | ||
496 | Looks up inferior_pid in the procinfo chain. Always returns a | |
497 | struct procinfo *. If process can't be found, we error() out. | |
498 | */ | |
499 | ||
500 | #define current_procinfo find_procinfo (inferior_pid, 0) | |
501 | ||
502 | /* | |
503 | ||
de43d7d0 SG |
504 | LOCAL FUNCTION |
505 | ||
506 | add_fd -- Add the fd to the poll/select list | |
507 | ||
508 | SYNOPSIS | |
509 | ||
510 | static void add_fd (struct procinfo *); | |
511 | ||
512 | DESCRIPTION | |
513 | ||
514 | Add the fd of the supplied procinfo to the list of fds used for | |
515 | poll/select operations. | |
516 | */ | |
517 | ||
518 | static void | |
519 | add_fd (pi) | |
520 | struct procinfo *pi; | |
521 | { | |
522 | if (num_poll_list <= 0) | |
523 | poll_list = (struct pollfd *) xmalloc (sizeof (struct pollfd)); | |
524 | else | |
525 | poll_list = (struct pollfd *) xrealloc (poll_list, | |
526 | (num_poll_list + 1) | |
527 | * sizeof (struct pollfd)); | |
528 | poll_list[num_poll_list].fd = pi->fd; | |
529 | poll_list[num_poll_list].events = POLLPRI; | |
530 | ||
531 | num_poll_list++; | |
532 | } | |
533 | ||
534 | static void | |
535 | remove_fd (pi) | |
536 | struct procinfo *pi; | |
537 | { | |
538 | int i; | |
539 | ||
540 | for (i = 0; i < num_poll_list; i++) | |
541 | { | |
542 | if (poll_list[i].fd == pi->fd) | |
543 | { | |
544 | if (i != num_poll_list - 1) | |
545 | memcpy (poll_list, poll_list + i + 1, | |
546 | (num_poll_list - i - 1) * sizeof (struct pollfd)); | |
547 | ||
548 | num_poll_list--; | |
549 | ||
550 | if (num_poll_list == 0) | |
551 | free (poll_list); | |
552 | else | |
553 | poll_list = (struct pollfd *) xrealloc (poll_list, | |
554 | num_poll_list | |
555 | * sizeof (struct pollfd)); | |
556 | return; | |
557 | } | |
558 | } | |
559 | } | |
560 | ||
7c5d526e | 561 | #define LOSING_POLL unixware_sux |
de43d7d0 | 562 | |
7c5d526e SG |
563 | static struct procinfo * |
564 | wait_fd () | |
565 | { | |
566 | struct procinfo *pi; | |
567 | int num_fds; | |
568 | int i; | |
de43d7d0 | 569 | |
7c5d526e SG |
570 | if (attach_flag) |
571 | set_sigint_trap (); /* Causes SIGINT to be passed on to the | |
572 | attached process. */ | |
de43d7d0 | 573 | |
7c5d526e SG |
574 | #ifndef LOSING_POLL |
575 | num_fds = poll (poll_list, num_poll_list, -1); | |
576 | #else | |
577 | pi = current_procinfo; | |
de43d7d0 | 578 | |
7c5d526e SG |
579 | if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0) |
580 | { | |
581 | print_sys_errmsg (pi->pathname, errno); | |
582 | error ("PIOCWSTOP failed"); | |
583 | } | |
584 | #endif | |
585 | ||
586 | if (attach_flag) | |
587 | clear_sigint_trap(); | |
de43d7d0 | 588 | |
7c5d526e | 589 | #ifndef LOSING_POLL |
de43d7d0 | 590 | |
7c5d526e SG |
591 | if (num_fds <= 0) |
592 | { | |
593 | print_sys_errmsg ("poll failed\n", errno); | |
594 | error ("Poll failed, returned %d", num_fds); | |
595 | } | |
596 | ||
597 | for (i = 0; i < num_poll_list && num_fds > 0; i++) | |
598 | { | |
599 | if ((poll_list[i].revents & (POLLPRI|POLLERR|POLLHUP|POLLNVAL)) == 0) | |
600 | continue; | |
601 | for (pi = procinfo_list; pi; pi = pi->next) | |
602 | { | |
603 | if (poll_list[i].fd == pi->fd) | |
604 | { | |
605 | if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0) | |
606 | { | |
607 | print_sys_errmsg (pi->pathname, errno); | |
608 | error ("PIOCSTATUS failed"); | |
609 | } | |
610 | num_fds--; | |
611 | pi->had_event = 1; | |
612 | break; | |
613 | } | |
614 | } | |
615 | if (!pi) | |
616 | error ("procfs_wait: Couldn't find procinfo for fd %d\n", | |
617 | poll_list[i].fd); | |
618 | } | |
619 | #endif /* LOSING_POLL */ | |
620 | ||
621 | return pi; | |
622 | } | |
de43d7d0 SG |
623 | |
624 | /* | |
625 | ||
cc221e76 FF |
626 | LOCAL FUNCTION |
627 | ||
628 | lookupdesc -- translate a value to a summary desc string | |
629 | ||
630 | SYNOPSIS | |
631 | ||
632 | static char *lookupdesc (struct trans *transp, unsigned int val); | |
633 | ||
634 | DESCRIPTION | |
635 | ||
636 | Given a pointer to a translation table and a value to be translated, | |
637 | lookup the desc string and return it. | |
638 | */ | |
639 | ||
640 | static char * | |
641 | lookupdesc (transp, val) | |
642 | struct trans *transp; | |
643 | unsigned int val; | |
644 | { | |
645 | char *desc; | |
646 | ||
647 | for (desc = NULL; transp -> name != NULL; transp++) | |
648 | { | |
649 | if (transp -> value == val) | |
650 | { | |
651 | desc = transp -> desc; | |
652 | break; | |
653 | } | |
654 | } | |
655 | ||
656 | /* Didn't find a translation for the specified value, set a default one. */ | |
657 | ||
658 | if (desc == NULL) | |
659 | { | |
660 | desc = "Unknown"; | |
661 | } | |
662 | return (desc); | |
663 | } | |
664 | ||
665 | /* | |
666 | ||
667 | LOCAL FUNCTION | |
668 | ||
669 | lookupname -- translate a value to symbolic name | |
670 | ||
671 | SYNOPSIS | |
672 | ||
673 | static char *lookupname (struct trans *transp, unsigned int val, | |
674 | char *prefix); | |
675 | ||
676 | DESCRIPTION | |
677 | ||
678 | Given a pointer to a translation table, a value to be translated, | |
679 | and a default prefix to return if the value can't be translated, | |
680 | match the value with one of the translation table entries and | |
681 | return a pointer to the symbolic name. | |
682 | ||
683 | If no match is found it just returns the value as a printable string, | |
684 | with the given prefix. The previous such value, if any, is freed | |
685 | at this time. | |
686 | */ | |
687 | ||
688 | static char * | |
689 | lookupname (transp, val, prefix) | |
690 | struct trans *transp; | |
691 | unsigned int val; | |
692 | char *prefix; | |
693 | { | |
694 | static char *locbuf; | |
695 | char *name; | |
696 | ||
697 | for (name = NULL; transp -> name != NULL; transp++) | |
698 | { | |
699 | if (transp -> value == val) | |
700 | { | |
701 | name = transp -> name; | |
702 | break; | |
703 | } | |
704 | } | |
705 | ||
706 | /* Didn't find a translation for the specified value, build a default | |
707 | one using the specified prefix and return it. The lifetime of | |
708 | the value is only until the next one is needed. */ | |
709 | ||
710 | if (name == NULL) | |
711 | { | |
712 | if (locbuf != NULL) | |
713 | { | |
714 | free (locbuf); | |
715 | } | |
716 | locbuf = xmalloc (strlen (prefix) + 16); | |
4ed3a9ea | 717 | sprintf (locbuf, "%s %u", prefix, val); |
cc221e76 FF |
718 | name = locbuf; |
719 | } | |
720 | return (name); | |
721 | } | |
722 | ||
723 | static char * | |
724 | sigcodename (sip) | |
725 | siginfo_t *sip; | |
726 | { | |
727 | struct sigcode *scp; | |
728 | char *name = NULL; | |
729 | static char locbuf[32]; | |
730 | ||
731 | for (scp = siginfo_table; scp -> codename != NULL; scp++) | |
732 | { | |
733 | if ((scp -> signo == sip -> si_signo) && | |
734 | (scp -> code == sip -> si_code)) | |
735 | { | |
736 | name = scp -> codename; | |
737 | break; | |
738 | } | |
739 | } | |
740 | if (name == NULL) | |
741 | { | |
4ed3a9ea | 742 | sprintf (locbuf, "sigcode %u", sip -> si_signo); |
cc221e76 FF |
743 | name = locbuf; |
744 | } | |
745 | return (name); | |
746 | } | |
747 | ||
3fbdd536 JG |
748 | static char * |
749 | sigcodedesc (sip) | |
cc221e76 FF |
750 | siginfo_t *sip; |
751 | { | |
752 | struct sigcode *scp; | |
753 | char *desc = NULL; | |
754 | ||
755 | for (scp = siginfo_table; scp -> codename != NULL; scp++) | |
756 | { | |
757 | if ((scp -> signo == sip -> si_signo) && | |
758 | (scp -> code == sip -> si_code)) | |
759 | { | |
760 | desc = scp -> desc; | |
761 | break; | |
762 | } | |
763 | } | |
764 | if (desc == NULL) | |
765 | { | |
766 | desc = "Unrecognized signal or trap use"; | |
767 | } | |
768 | return (desc); | |
769 | } | |
770 | ||
771 | /* | |
772 | ||
773 | LOCAL FUNCTION | |
774 | ||
775 | syscallname - translate a system call number into a system call name | |
776 | ||
777 | SYNOPSIS | |
778 | ||
779 | char *syscallname (int syscallnum) | |
780 | ||
781 | DESCRIPTION | |
782 | ||
783 | Given a system call number, translate it into the printable name | |
784 | of a system call, or into "syscall <num>" if it is an unknown | |
785 | number. | |
786 | */ | |
787 | ||
788 | static char * | |
789 | syscallname (syscallnum) | |
790 | int syscallnum; | |
791 | { | |
792 | static char locbuf[32]; | |
793 | char *rtnval; | |
794 | ||
795 | if (syscallnum >= 0 && syscallnum < MAX_SYSCALLS) | |
796 | { | |
797 | rtnval = syscall_table[syscallnum]; | |
798 | } | |
799 | else | |
800 | { | |
4ed3a9ea | 801 | sprintf (locbuf, "syscall %u", syscallnum); |
cc221e76 FF |
802 | rtnval = locbuf; |
803 | } | |
804 | return (rtnval); | |
805 | } | |
806 | ||
807 | /* | |
808 | ||
809 | LOCAL FUNCTION | |
810 | ||
811 | init_syscall_table - initialize syscall translation table | |
812 | ||
813 | SYNOPSIS | |
814 | ||
815 | void init_syscall_table (void) | |
816 | ||
817 | DESCRIPTION | |
818 | ||
819 | Dynamically initialize the translation table to convert system | |
820 | call numbers into printable system call names. Done once per | |
821 | gdb run, on initialization. | |
822 | ||
823 | NOTES | |
824 | ||
825 | This is awfully ugly, but preprocessor tricks to make it prettier | |
826 | tend to be nonportable. | |
827 | */ | |
828 | ||
829 | static void | |
830 | init_syscall_table () | |
831 | { | |
cc221e76 FF |
832 | #if defined (SYS_exit) |
833 | syscall_table[SYS_exit] = "exit"; | |
834 | #endif | |
835 | #if defined (SYS_fork) | |
836 | syscall_table[SYS_fork] = "fork"; | |
837 | #endif | |
838 | #if defined (SYS_read) | |
839 | syscall_table[SYS_read] = "read"; | |
840 | #endif | |
841 | #if defined (SYS_write) | |
842 | syscall_table[SYS_write] = "write"; | |
843 | #endif | |
844 | #if defined (SYS_open) | |
845 | syscall_table[SYS_open] = "open"; | |
846 | #endif | |
847 | #if defined (SYS_close) | |
848 | syscall_table[SYS_close] = "close"; | |
849 | #endif | |
850 | #if defined (SYS_wait) | |
851 | syscall_table[SYS_wait] = "wait"; | |
852 | #endif | |
853 | #if defined (SYS_creat) | |
854 | syscall_table[SYS_creat] = "creat"; | |
855 | #endif | |
856 | #if defined (SYS_link) | |
857 | syscall_table[SYS_link] = "link"; | |
858 | #endif | |
859 | #if defined (SYS_unlink) | |
860 | syscall_table[SYS_unlink] = "unlink"; | |
861 | #endif | |
862 | #if defined (SYS_exec) | |
863 | syscall_table[SYS_exec] = "exec"; | |
864 | #endif | |
865 | #if defined (SYS_execv) | |
866 | syscall_table[SYS_execv] = "execv"; | |
867 | #endif | |
868 | #if defined (SYS_execve) | |
869 | syscall_table[SYS_execve] = "execve"; | |
870 | #endif | |
871 | #if defined (SYS_chdir) | |
872 | syscall_table[SYS_chdir] = "chdir"; | |
873 | #endif | |
874 | #if defined (SYS_time) | |
875 | syscall_table[SYS_time] = "time"; | |
876 | #endif | |
877 | #if defined (SYS_mknod) | |
878 | syscall_table[SYS_mknod] = "mknod"; | |
879 | #endif | |
880 | #if defined (SYS_chmod) | |
881 | syscall_table[SYS_chmod] = "chmod"; | |
882 | #endif | |
883 | #if defined (SYS_chown) | |
884 | syscall_table[SYS_chown] = "chown"; | |
885 | #endif | |
886 | #if defined (SYS_brk) | |
887 | syscall_table[SYS_brk] = "brk"; | |
888 | #endif | |
889 | #if defined (SYS_stat) | |
890 | syscall_table[SYS_stat] = "stat"; | |
891 | #endif | |
892 | #if defined (SYS_lseek) | |
893 | syscall_table[SYS_lseek] = "lseek"; | |
894 | #endif | |
895 | #if defined (SYS_getpid) | |
896 | syscall_table[SYS_getpid] = "getpid"; | |
897 | #endif | |
898 | #if defined (SYS_mount) | |
899 | syscall_table[SYS_mount] = "mount"; | |
900 | #endif | |
901 | #if defined (SYS_umount) | |
902 | syscall_table[SYS_umount] = "umount"; | |
903 | #endif | |
904 | #if defined (SYS_setuid) | |
905 | syscall_table[SYS_setuid] = "setuid"; | |
906 | #endif | |
907 | #if defined (SYS_getuid) | |
908 | syscall_table[SYS_getuid] = "getuid"; | |
909 | #endif | |
910 | #if defined (SYS_stime) | |
911 | syscall_table[SYS_stime] = "stime"; | |
912 | #endif | |
913 | #if defined (SYS_ptrace) | |
914 | syscall_table[SYS_ptrace] = "ptrace"; | |
915 | #endif | |
916 | #if defined (SYS_alarm) | |
917 | syscall_table[SYS_alarm] = "alarm"; | |
918 | #endif | |
919 | #if defined (SYS_fstat) | |
920 | syscall_table[SYS_fstat] = "fstat"; | |
921 | #endif | |
922 | #if defined (SYS_pause) | |
923 | syscall_table[SYS_pause] = "pause"; | |
924 | #endif | |
925 | #if defined (SYS_utime) | |
926 | syscall_table[SYS_utime] = "utime"; | |
927 | #endif | |
928 | #if defined (SYS_stty) | |
929 | syscall_table[SYS_stty] = "stty"; | |
930 | #endif | |
931 | #if defined (SYS_gtty) | |
932 | syscall_table[SYS_gtty] = "gtty"; | |
933 | #endif | |
934 | #if defined (SYS_access) | |
935 | syscall_table[SYS_access] = "access"; | |
936 | #endif | |
937 | #if defined (SYS_nice) | |
938 | syscall_table[SYS_nice] = "nice"; | |
939 | #endif | |
940 | #if defined (SYS_statfs) | |
941 | syscall_table[SYS_statfs] = "statfs"; | |
942 | #endif | |
943 | #if defined (SYS_sync) | |
944 | syscall_table[SYS_sync] = "sync"; | |
945 | #endif | |
946 | #if defined (SYS_kill) | |
947 | syscall_table[SYS_kill] = "kill"; | |
948 | #endif | |
949 | #if defined (SYS_fstatfs) | |
950 | syscall_table[SYS_fstatfs] = "fstatfs"; | |
951 | #endif | |
952 | #if defined (SYS_pgrpsys) | |
953 | syscall_table[SYS_pgrpsys] = "pgrpsys"; | |
954 | #endif | |
955 | #if defined (SYS_xenix) | |
956 | syscall_table[SYS_xenix] = "xenix"; | |
957 | #endif | |
958 | #if defined (SYS_dup) | |
959 | syscall_table[SYS_dup] = "dup"; | |
960 | #endif | |
961 | #if defined (SYS_pipe) | |
962 | syscall_table[SYS_pipe] = "pipe"; | |
963 | #endif | |
964 | #if defined (SYS_times) | |
965 | syscall_table[SYS_times] = "times"; | |
966 | #endif | |
967 | #if defined (SYS_profil) | |
968 | syscall_table[SYS_profil] = "profil"; | |
969 | #endif | |
970 | #if defined (SYS_plock) | |
971 | syscall_table[SYS_plock] = "plock"; | |
972 | #endif | |
973 | #if defined (SYS_setgid) | |
974 | syscall_table[SYS_setgid] = "setgid"; | |
975 | #endif | |
976 | #if defined (SYS_getgid) | |
977 | syscall_table[SYS_getgid] = "getgid"; | |
978 | #endif | |
979 | #if defined (SYS_signal) | |
980 | syscall_table[SYS_signal] = "signal"; | |
981 | #endif | |
982 | #if defined (SYS_msgsys) | |
983 | syscall_table[SYS_msgsys] = "msgsys"; | |
984 | #endif | |
985 | #if defined (SYS_sys3b) | |
986 | syscall_table[SYS_sys3b] = "sys3b"; | |
987 | #endif | |
988 | #if defined (SYS_acct) | |
989 | syscall_table[SYS_acct] = "acct"; | |
990 | #endif | |
991 | #if defined (SYS_shmsys) | |
992 | syscall_table[SYS_shmsys] = "shmsys"; | |
993 | #endif | |
994 | #if defined (SYS_semsys) | |
995 | syscall_table[SYS_semsys] = "semsys"; | |
996 | #endif | |
997 | #if defined (SYS_ioctl) | |
998 | syscall_table[SYS_ioctl] = "ioctl"; | |
999 | #endif | |
1000 | #if defined (SYS_uadmin) | |
1001 | syscall_table[SYS_uadmin] = "uadmin"; | |
1002 | #endif | |
1003 | #if defined (SYS_utssys) | |
1004 | syscall_table[SYS_utssys] = "utssys"; | |
1005 | #endif | |
1006 | #if defined (SYS_fsync) | |
1007 | syscall_table[SYS_fsync] = "fsync"; | |
1008 | #endif | |
1009 | #if defined (SYS_umask) | |
1010 | syscall_table[SYS_umask] = "umask"; | |
1011 | #endif | |
1012 | #if defined (SYS_chroot) | |
1013 | syscall_table[SYS_chroot] = "chroot"; | |
1014 | #endif | |
1015 | #if defined (SYS_fcntl) | |
1016 | syscall_table[SYS_fcntl] = "fcntl"; | |
1017 | #endif | |
1018 | #if defined (SYS_ulimit) | |
1019 | syscall_table[SYS_ulimit] = "ulimit"; | |
1020 | #endif | |
1021 | #if defined (SYS_rfsys) | |
1022 | syscall_table[SYS_rfsys] = "rfsys"; | |
1023 | #endif | |
1024 | #if defined (SYS_rmdir) | |
1025 | syscall_table[SYS_rmdir] = "rmdir"; | |
1026 | #endif | |
1027 | #if defined (SYS_mkdir) | |
1028 | syscall_table[SYS_mkdir] = "mkdir"; | |
1029 | #endif | |
1030 | #if defined (SYS_getdents) | |
1031 | syscall_table[SYS_getdents] = "getdents"; | |
1032 | #endif | |
1033 | #if defined (SYS_sysfs) | |
1034 | syscall_table[SYS_sysfs] = "sysfs"; | |
1035 | #endif | |
1036 | #if defined (SYS_getmsg) | |
1037 | syscall_table[SYS_getmsg] = "getmsg"; | |
1038 | #endif | |
1039 | #if defined (SYS_putmsg) | |
1040 | syscall_table[SYS_putmsg] = "putmsg"; | |
1041 | #endif | |
1042 | #if defined (SYS_poll) | |
1043 | syscall_table[SYS_poll] = "poll"; | |
1044 | #endif | |
1045 | #if defined (SYS_lstat) | |
1046 | syscall_table[SYS_lstat] = "lstat"; | |
1047 | #endif | |
1048 | #if defined (SYS_symlink) | |
1049 | syscall_table[SYS_symlink] = "symlink"; | |
1050 | #endif | |
1051 | #if defined (SYS_readlink) | |
1052 | syscall_table[SYS_readlink] = "readlink"; | |
1053 | #endif | |
1054 | #if defined (SYS_setgroups) | |
1055 | syscall_table[SYS_setgroups] = "setgroups"; | |
1056 | #endif | |
1057 | #if defined (SYS_getgroups) | |
1058 | syscall_table[SYS_getgroups] = "getgroups"; | |
1059 | #endif | |
1060 | #if defined (SYS_fchmod) | |
1061 | syscall_table[SYS_fchmod] = "fchmod"; | |
1062 | #endif | |
1063 | #if defined (SYS_fchown) | |
1064 | syscall_table[SYS_fchown] = "fchown"; | |
1065 | #endif | |
1066 | #if defined (SYS_sigprocmask) | |
1067 | syscall_table[SYS_sigprocmask] = "sigprocmask"; | |
1068 | #endif | |
1069 | #if defined (SYS_sigsuspend) | |
1070 | syscall_table[SYS_sigsuspend] = "sigsuspend"; | |
1071 | #endif | |
1072 | #if defined (SYS_sigaltstack) | |
1073 | syscall_table[SYS_sigaltstack] = "sigaltstack"; | |
1074 | #endif | |
1075 | #if defined (SYS_sigaction) | |
1076 | syscall_table[SYS_sigaction] = "sigaction"; | |
1077 | #endif | |
1078 | #if defined (SYS_sigpending) | |
1079 | syscall_table[SYS_sigpending] = "sigpending"; | |
1080 | #endif | |
1081 | #if defined (SYS_context) | |
1082 | syscall_table[SYS_context] = "context"; | |
1083 | #endif | |
1084 | #if defined (SYS_evsys) | |
1085 | syscall_table[SYS_evsys] = "evsys"; | |
1086 | #endif | |
1087 | #if defined (SYS_evtrapret) | |
1088 | syscall_table[SYS_evtrapret] = "evtrapret"; | |
1089 | #endif | |
1090 | #if defined (SYS_statvfs) | |
1091 | syscall_table[SYS_statvfs] = "statvfs"; | |
1092 | #endif | |
1093 | #if defined (SYS_fstatvfs) | |
1094 | syscall_table[SYS_fstatvfs] = "fstatvfs"; | |
1095 | #endif | |
1096 | #if defined (SYS_nfssys) | |
1097 | syscall_table[SYS_nfssys] = "nfssys"; | |
1098 | #endif | |
1099 | #if defined (SYS_waitsys) | |
1100 | syscall_table[SYS_waitsys] = "waitsys"; | |
1101 | #endif | |
1102 | #if defined (SYS_sigsendsys) | |
1103 | syscall_table[SYS_sigsendsys] = "sigsendsys"; | |
1104 | #endif | |
1105 | #if defined (SYS_hrtsys) | |
1106 | syscall_table[SYS_hrtsys] = "hrtsys"; | |
1107 | #endif | |
1108 | #if defined (SYS_acancel) | |
1109 | syscall_table[SYS_acancel] = "acancel"; | |
1110 | #endif | |
1111 | #if defined (SYS_async) | |
1112 | syscall_table[SYS_async] = "async"; | |
1113 | #endif | |
1114 | #if defined (SYS_priocntlsys) | |
1115 | syscall_table[SYS_priocntlsys] = "priocntlsys"; | |
1116 | #endif | |
1117 | #if defined (SYS_pathconf) | |
1118 | syscall_table[SYS_pathconf] = "pathconf"; | |
1119 | #endif | |
1120 | #if defined (SYS_mincore) | |
1121 | syscall_table[SYS_mincore] = "mincore"; | |
1122 | #endif | |
1123 | #if defined (SYS_mmap) | |
1124 | syscall_table[SYS_mmap] = "mmap"; | |
1125 | #endif | |
1126 | #if defined (SYS_mprotect) | |
1127 | syscall_table[SYS_mprotect] = "mprotect"; | |
1128 | #endif | |
1129 | #if defined (SYS_munmap) | |
1130 | syscall_table[SYS_munmap] = "munmap"; | |
1131 | #endif | |
1132 | #if defined (SYS_fpathconf) | |
1133 | syscall_table[SYS_fpathconf] = "fpathconf"; | |
1134 | #endif | |
1135 | #if defined (SYS_vfork) | |
1136 | syscall_table[SYS_vfork] = "vfork"; | |
1137 | #endif | |
1138 | #if defined (SYS_fchdir) | |
1139 | syscall_table[SYS_fchdir] = "fchdir"; | |
1140 | #endif | |
1141 | #if defined (SYS_readv) | |
1142 | syscall_table[SYS_readv] = "readv"; | |
1143 | #endif | |
1144 | #if defined (SYS_writev) | |
1145 | syscall_table[SYS_writev] = "writev"; | |
1146 | #endif | |
1147 | #if defined (SYS_xstat) | |
1148 | syscall_table[SYS_xstat] = "xstat"; | |
1149 | #endif | |
1150 | #if defined (SYS_lxstat) | |
1151 | syscall_table[SYS_lxstat] = "lxstat"; | |
1152 | #endif | |
1153 | #if defined (SYS_fxstat) | |
1154 | syscall_table[SYS_fxstat] = "fxstat"; | |
1155 | #endif | |
1156 | #if defined (SYS_xmknod) | |
1157 | syscall_table[SYS_xmknod] = "xmknod"; | |
1158 | #endif | |
1159 | #if defined (SYS_clocal) | |
1160 | syscall_table[SYS_clocal] = "clocal"; | |
1161 | #endif | |
1162 | #if defined (SYS_setrlimit) | |
1163 | syscall_table[SYS_setrlimit] = "setrlimit"; | |
1164 | #endif | |
1165 | #if defined (SYS_getrlimit) | |
1166 | syscall_table[SYS_getrlimit] = "getrlimit"; | |
1167 | #endif | |
1168 | #if defined (SYS_lchown) | |
1169 | syscall_table[SYS_lchown] = "lchown"; | |
1170 | #endif | |
1171 | #if defined (SYS_memcntl) | |
1172 | syscall_table[SYS_memcntl] = "memcntl"; | |
1173 | #endif | |
1174 | #if defined (SYS_getpmsg) | |
1175 | syscall_table[SYS_getpmsg] = "getpmsg"; | |
1176 | #endif | |
1177 | #if defined (SYS_putpmsg) | |
1178 | syscall_table[SYS_putpmsg] = "putpmsg"; | |
1179 | #endif | |
1180 | #if defined (SYS_rename) | |
1181 | syscall_table[SYS_rename] = "rename"; | |
1182 | #endif | |
1183 | #if defined (SYS_uname) | |
1184 | syscall_table[SYS_uname] = "uname"; | |
1185 | #endif | |
1186 | #if defined (SYS_setegid) | |
1187 | syscall_table[SYS_setegid] = "setegid"; | |
1188 | #endif | |
1189 | #if defined (SYS_sysconfig) | |
1190 | syscall_table[SYS_sysconfig] = "sysconfig"; | |
1191 | #endif | |
1192 | #if defined (SYS_adjtime) | |
1193 | syscall_table[SYS_adjtime] = "adjtime"; | |
1194 | #endif | |
1195 | #if defined (SYS_systeminfo) | |
1196 | syscall_table[SYS_systeminfo] = "systeminfo"; | |
1197 | #endif | |
1198 | #if defined (SYS_seteuid) | |
1199 | syscall_table[SYS_seteuid] = "seteuid"; | |
1200 | #endif | |
de43d7d0 SG |
1201 | #if defined (SYS_sproc) |
1202 | syscall_table[SYS_sproc] = "sproc"; | |
1203 | #endif | |
cc221e76 | 1204 | } |
35f5886e FF |
1205 | |
1206 | /* | |
1207 | ||
1208 | GLOBAL FUNCTION | |
1209 | ||
1210 | ptrace -- override library version to force errors for /proc version | |
1211 | ||
1212 | SYNOPSIS | |
1213 | ||
e676a15f | 1214 | int ptrace (int request, int pid, PTRACE_ARG3_TYPE arg3, int arg4) |
35f5886e FF |
1215 | |
1216 | DESCRIPTION | |
1217 | ||
1218 | When gdb is configured to use /proc, it should not be calling | |
1219 | or otherwise attempting to use ptrace. In order to catch errors | |
1220 | where use of /proc is configured, but some routine is still calling | |
1221 | ptrace, we provide a local version of a function with that name | |
1222 | that does nothing but issue an error message. | |
1223 | */ | |
1224 | ||
1225 | int | |
1ab3bf1b JG |
1226 | ptrace (request, pid, arg3, arg4) |
1227 | int request; | |
1228 | int pid; | |
e676a15f | 1229 | PTRACE_ARG3_TYPE arg3; |
1ab3bf1b | 1230 | int arg4; |
35f5886e FF |
1231 | { |
1232 | error ("internal error - there is a call to ptrace() somewhere"); | |
1233 | /*NOTREACHED*/ | |
1234 | } | |
1235 | ||
1236 | /* | |
1237 | ||
3fbdd536 | 1238 | LOCAL FUNCTION |
35f5886e | 1239 | |
3fbdd536 | 1240 | procfs_kill_inferior - kill any currently inferior |
35f5886e FF |
1241 | |
1242 | SYNOPSIS | |
1243 | ||
3fbdd536 | 1244 | void procfs_kill_inferior (void) |
35f5886e FF |
1245 | |
1246 | DESCRIPTION | |
1247 | ||
1248 | Kill any current inferior. | |
1249 | ||
1250 | NOTES | |
1251 | ||
1252 | Kills even attached inferiors. Presumably the user has already | |
1253 | been prompted that the inferior is an attached one rather than | |
1254 | one started by gdb. (FIXME?) | |
1255 | ||
1256 | */ | |
1257 | ||
3fbdd536 JG |
1258 | static void |
1259 | procfs_kill_inferior () | |
35f5886e | 1260 | { |
de43d7d0 SG |
1261 | struct procinfo *pi; |
1262 | ||
1263 | for (pi = procinfo_list; pi; pi = pi->next) | |
1264 | unconditionally_kill_inferior (pi); | |
1265 | ||
1266 | target_mourn_inferior (); | |
35f5886e FF |
1267 | } |
1268 | ||
1269 | /* | |
1270 | ||
1271 | LOCAL FUNCTION | |
1272 | ||
1273 | unconditionally_kill_inferior - terminate the inferior | |
1274 | ||
1275 | SYNOPSIS | |
1276 | ||
de43d7d0 | 1277 | static void unconditionally_kill_inferior (struct procinfo *) |
35f5886e FF |
1278 | |
1279 | DESCRIPTION | |
1280 | ||
de43d7d0 | 1281 | Kill the specified inferior. |
35f5886e FF |
1282 | |
1283 | NOTE | |
1284 | ||
1285 | A possibly useful enhancement would be to first try sending | |
1286 | the inferior a terminate signal, politely asking it to commit | |
de43d7d0 SG |
1287 | suicide, before we murder it (we could call that |
1288 | politely_kill_inferior()). | |
35f5886e FF |
1289 | |
1290 | */ | |
1291 | ||
1292 | static void | |
de43d7d0 SG |
1293 | unconditionally_kill_inferior (pi) |
1294 | struct procinfo *pi; | |
35f5886e FF |
1295 | { |
1296 | int signo; | |
de43d7d0 | 1297 | int ppid; |
35f5886e | 1298 | |
de43d7d0 SG |
1299 | ppid = pi->prstatus.pr_ppid; |
1300 | ||
35f5886e | 1301 | signo = SIGKILL; |
de43d7d0 SG |
1302 | ioctl (pi->fd, PIOCKILL, &signo); |
1303 | close_proc_file (pi); | |
1304 | ||
1305 | /* Only wait() for our direct children. Our grandchildren zombies are killed | |
1306 | by the death of their parents. */ | |
1307 | ||
1308 | if (ppid == getpid()) | |
1309 | wait ((int *) 0); | |
35f5886e FF |
1310 | } |
1311 | ||
1312 | /* | |
1313 | ||
3fbdd536 | 1314 | LOCAL FUNCTION |
35f5886e | 1315 | |
3fbdd536 | 1316 | procfs_xfer_memory -- copy data to or from inferior memory space |
35f5886e FF |
1317 | |
1318 | SYNOPSIS | |
1319 | ||
3fbdd536 | 1320 | int procfs_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, |
35f5886e FF |
1321 | int dowrite, struct target_ops target) |
1322 | ||
1323 | DESCRIPTION | |
1324 | ||
1325 | Copy LEN bytes to/from inferior's memory starting at MEMADDR | |
1326 | from/to debugger memory starting at MYADDR. Copy from inferior | |
1327 | if DOWRITE is zero or to inferior if DOWRITE is nonzero. | |
1328 | ||
1329 | Returns the length copied, which is either the LEN argument or | |
3fbdd536 | 1330 | zero. This xfer function does not do partial moves, since procfs_ops |
35f5886e FF |
1331 | doesn't allow memory operations to cross below us in the target stack |
1332 | anyway. | |
1333 | ||
1334 | NOTES | |
1335 | ||
1336 | The /proc interface makes this an almost trivial task. | |
1337 | */ | |
1338 | ||
3fbdd536 JG |
1339 | static int |
1340 | procfs_xfer_memory (memaddr, myaddr, len, dowrite, target) | |
1ab3bf1b JG |
1341 | CORE_ADDR memaddr; |
1342 | char *myaddr; | |
1343 | int len; | |
1344 | int dowrite; | |
1345 | struct target_ops *target; /* ignored */ | |
35f5886e FF |
1346 | { |
1347 | int nbytes = 0; | |
de43d7d0 | 1348 | struct procinfo *pi; |
35f5886e | 1349 | |
de43d7d0 SG |
1350 | pi = current_procinfo; |
1351 | ||
1352 | if (lseek(pi->fd, (off_t) memaddr, 0) == (off_t) memaddr) | |
35f5886e FF |
1353 | { |
1354 | if (dowrite) | |
1355 | { | |
de43d7d0 | 1356 | nbytes = write (pi->fd, myaddr, len); |
35f5886e FF |
1357 | } |
1358 | else | |
1359 | { | |
de43d7d0 | 1360 | nbytes = read (pi->fd, myaddr, len); |
35f5886e FF |
1361 | } |
1362 | if (nbytes < 0) | |
1363 | { | |
1364 | nbytes = 0; | |
1365 | } | |
1366 | } | |
1367 | return (nbytes); | |
1368 | } | |
1369 | ||
1370 | /* | |
1371 | ||
3fbdd536 | 1372 | LOCAL FUNCTION |
35f5886e | 1373 | |
3fbdd536 | 1374 | procfs_store_registers -- copy register values back to inferior |
35f5886e FF |
1375 | |
1376 | SYNOPSIS | |
1377 | ||
3fbdd536 | 1378 | void procfs_store_registers (int regno) |
35f5886e FF |
1379 | |
1380 | DESCRIPTION | |
1381 | ||
1382 | Store our current register values back into the inferior. If | |
1383 | REGNO is -1 then store all the register, otherwise store just | |
1384 | the value specified by REGNO. | |
1385 | ||
1386 | NOTES | |
1387 | ||
1388 | If we are storing only a single register, we first have to get all | |
1389 | the current values from the process, overwrite the desired register | |
1390 | in the gregset with the one we want from gdb's registers, and then | |
1391 | send the whole set back to the process. For writing all the | |
1392 | registers, all we have to do is generate the gregset and send it to | |
1393 | the process. | |
1394 | ||
1395 | Also note that the process has to be stopped on an event of interest | |
1396 | for this to work, which basically means that it has to have been | |
1397 | run under the control of one of the other /proc ioctl calls and not | |
1398 | ptrace. Since we don't use ptrace anyway, we don't worry about this | |
1399 | fine point, but it is worth noting for future reference. | |
1400 | ||
1401 | Gdb is confused about what this function is supposed to return. | |
1402 | Some versions return a value, others return nothing. Some are | |
1403 | declared to return a value and actually return nothing. Gdb ignores | |
1404 | anything returned. (FIXME) | |
1405 | ||
1406 | */ | |
1407 | ||
3fbdd536 JG |
1408 | static void |
1409 | procfs_store_registers (regno) | |
1ab3bf1b | 1410 | int regno; |
35f5886e | 1411 | { |
de43d7d0 SG |
1412 | struct procinfo *pi; |
1413 | ||
1414 | pi = current_procinfo; | |
1415 | ||
35f5886e FF |
1416 | if (regno != -1) |
1417 | { | |
de43d7d0 | 1418 | ioctl (pi->fd, PIOCGREG, &pi->gregset); |
35f5886e | 1419 | } |
de43d7d0 SG |
1420 | fill_gregset (&pi->gregset, regno); |
1421 | ioctl (pi->fd, PIOCSREG, &pi->gregset); | |
35f5886e FF |
1422 | |
1423 | #if defined (FP0_REGNUM) | |
1424 | ||
1425 | /* Now repeat everything using the floating point register set, if the | |
1426 | target has floating point hardware. Since we ignore the returned value, | |
1427 | we'll never know whether it worked or not anyway. */ | |
1428 | ||
1429 | if (regno != -1) | |
1430 | { | |
de43d7d0 | 1431 | ioctl (pi->fd, PIOCGFPREG, &pi->fpregset); |
35f5886e | 1432 | } |
de43d7d0 SG |
1433 | fill_fpregset (&pi->fpregset, regno); |
1434 | ioctl (pi->fd, PIOCSFPREG, &pi->fpregset); | |
35f5886e FF |
1435 | |
1436 | #endif /* FP0_REGNUM */ | |
1437 | ||
1438 | } | |
1439 | ||
1440 | /* | |
1441 | ||
3fbdd536 | 1442 | LOCAL FUNCTION |
35f5886e | 1443 | |
de43d7d0 SG |
1444 | create_procinfo - initialize access to a /proc entry |
1445 | ||
1446 | SYNOPSIS | |
1447 | ||
1448 | void create_procinfo (int pid) | |
1449 | ||
1450 | DESCRIPTION | |
1451 | ||
1452 | Allocate a procinfo structure, open the /proc file and then sets up | |
1453 | the set of signals and faults that are to be traced. | |
1454 | ||
1455 | NOTES | |
1456 | ||
1457 | If proc_init_failed ever gets called, control returns to the command | |
1458 | processing loop via the standard error handling code. | |
1459 | ||
1460 | */ | |
1461 | ||
1462 | static void | |
1463 | create_procinfo (pid) | |
1464 | int pid; | |
1465 | { | |
1466 | struct procinfo *pi; | |
1467 | ||
1468 | if (find_procinfo (pid, 1)) | |
1469 | return; /* All done! It already exists */ | |
1470 | ||
1471 | pi = (struct procinfo *) xmalloc (sizeof (struct procinfo)); | |
1472 | ||
1473 | if (!open_proc_file (pid, pi, O_RDWR)) | |
1474 | proc_init_failed (pi, "can't open process file"); | |
1475 | ||
1476 | /* Add new process to process info list */ | |
1477 | ||
1478 | pi->next = procinfo_list; | |
1479 | procinfo_list = pi; | |
1480 | ||
1481 | add_fd (pi); /* Add to list for poll/select */ | |
1482 | ||
1483 | memset ((char *) &pi->prrun, 0, sizeof (pi->prrun)); | |
1484 | prfillset (&pi->prrun.pr_trace); | |
1485 | procfs_notice_signals (pid); | |
1486 | prfillset (&pi->prrun.pr_fault); | |
1487 | prdelset (&pi->prrun.pr_fault, FLTPAGE); | |
1488 | ||
1489 | if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0) | |
1490 | proc_init_failed (pi, "PIOCWSTOP failed"); | |
1491 | ||
1492 | if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault) < 0) | |
1493 | proc_init_failed (pi, "PIOCSFAULT failed"); | |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | ||
1498 | LOCAL FUNCTION | |
1499 | ||
1500 | procfs_init_inferior - initialize target vector and access to a | |
1501 | /proc entry | |
35f5886e FF |
1502 | |
1503 | SYNOPSIS | |
1504 | ||
3fbdd536 | 1505 | void procfs_init_inferior (int pid) |
35f5886e FF |
1506 | |
1507 | DESCRIPTION | |
1508 | ||
1509 | When gdb starts an inferior, this function is called in the parent | |
1510 | process immediately after the fork. It waits for the child to stop | |
1511 | on the return from the exec system call (the child itself takes care | |
1512 | of ensuring that this is set up), then sets up the set of signals | |
1513 | and faults that are to be traced. | |
1514 | ||
1515 | NOTES | |
1516 | ||
1517 | If proc_init_failed ever gets called, control returns to the command | |
1518 | processing loop via the standard error handling code. | |
cc221e76 | 1519 | |
35f5886e FF |
1520 | */ |
1521 | ||
3fbdd536 JG |
1522 | static void |
1523 | procfs_init_inferior (pid) | |
1ab3bf1b | 1524 | int pid; |
35f5886e | 1525 | { |
3fbdd536 JG |
1526 | push_target (&procfs_ops); |
1527 | ||
de43d7d0 SG |
1528 | create_procinfo (pid); |
1529 | add_thread (pid); /* Setup initial thread */ | |
35f5886e FF |
1530 | } |
1531 | ||
1532 | /* | |
1533 | ||
cc221e76 FF |
1534 | GLOBAL FUNCTION |
1535 | ||
3950a34e | 1536 | procfs_notice_signals |
cc221e76 FF |
1537 | |
1538 | SYNOPSIS | |
1539 | ||
952a820e | 1540 | static void procfs_notice_signals (int pid); |
cc221e76 FF |
1541 | |
1542 | DESCRIPTION | |
1543 | ||
1544 | When the user changes the state of gdb's signal handling via the | |
1545 | "handle" command, this function gets called to see if any change | |
1546 | in the /proc interface is required. It is also called internally | |
1547 | by other /proc interface functions to initialize the state of | |
1548 | the traced signal set. | |
1549 | ||
1550 | One thing it does is that signals for which the state is "nostop", | |
1551 | "noprint", and "pass", have their trace bits reset in the pr_trace | |
1552 | field, so that they are no longer traced. This allows them to be | |
1553 | delivered directly to the inferior without the debugger ever being | |
1554 | involved. | |
1555 | */ | |
1556 | ||
3950a34e | 1557 | static void |
de43d7d0 | 1558 | procfs_notice_signals (pid) |
952a820e | 1559 | int pid; |
cc221e76 FF |
1560 | { |
1561 | int signo; | |
de43d7d0 | 1562 | struct procinfo *pi; |
cc221e76 | 1563 | |
de43d7d0 SG |
1564 | pi = find_procinfo (pid, 0); |
1565 | ||
1566 | for (signo = 0; signo < NSIG; signo++) | |
cc221e76 | 1567 | { |
de43d7d0 SG |
1568 | if (signal_stop_state (signo) == 0 && |
1569 | signal_print_state (signo) == 0 && | |
1570 | signal_pass_state (signo) == 1) | |
cc221e76 | 1571 | { |
de43d7d0 | 1572 | prdelset (&pi->prrun.pr_trace, signo); |
cc221e76 | 1573 | } |
de43d7d0 | 1574 | else |
cc221e76 | 1575 | { |
de43d7d0 | 1576 | praddset (&pi->prrun.pr_trace, signo); |
cc221e76 FF |
1577 | } |
1578 | } | |
de43d7d0 SG |
1579 | if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace)) |
1580 | { | |
1581 | print_sys_errmsg ("PIOCSTRACE failed", errno); | |
1582 | } | |
cc221e76 FF |
1583 | } |
1584 | ||
1585 | /* | |
1586 | ||
3fbdd536 | 1587 | LOCAL FUNCTION |
35f5886e FF |
1588 | |
1589 | proc_set_exec_trap -- arrange for exec'd child to halt at startup | |
1590 | ||
1591 | SYNOPSIS | |
1592 | ||
1593 | void proc_set_exec_trap (void) | |
1594 | ||
1595 | DESCRIPTION | |
1596 | ||
1597 | This function is called in the child process when starting up | |
1598 | an inferior, prior to doing the exec of the actual inferior. | |
1599 | It sets the child process's exitset to make exit from the exec | |
1600 | system call an event of interest to stop on, and then simply | |
1601 | returns. The child does the exec, the system call returns, and | |
1602 | the child stops at the first instruction, ready for the gdb | |
1603 | parent process to take control of it. | |
1604 | ||
1605 | NOTE | |
1606 | ||
1607 | We need to use all local variables since the child may be sharing | |
1608 | it's data space with the parent, if vfork was used rather than | |
1609 | fork. | |
cc221e76 FF |
1610 | |
1611 | Also note that we want to turn off the inherit-on-fork flag in | |
1612 | the child process so that any grand-children start with all | |
1613 | tracing flags cleared. | |
35f5886e FF |
1614 | */ |
1615 | ||
3fbdd536 | 1616 | static void |
1ab3bf1b | 1617 | proc_set_exec_trap () |
35f5886e FF |
1618 | { |
1619 | sysset_t exitset; | |
1620 | auto char procname[32]; | |
1621 | int fd; | |
1622 | ||
4ed3a9ea | 1623 | sprintf (procname, PROC_NAME_FMT, getpid ()); |
35f5886e FF |
1624 | if ((fd = open (procname, O_RDWR)) < 0) |
1625 | { | |
1626 | perror (procname); | |
1627 | fflush (stderr); | |
1628 | _exit (127); | |
1629 | } | |
1630 | premptyset (&exitset); | |
407a8389 | 1631 | |
cc221e76 FF |
1632 | /* GW: Rationale... |
1633 | Not all systems with /proc have all the exec* syscalls with the same | |
1634 | names. On the SGI, for example, there is no SYS_exec, but there | |
1635 | *is* a SYS_execv. So, we try to account for that. */ | |
1636 | ||
407a8389 | 1637 | #ifdef SYS_exec |
35f5886e | 1638 | praddset (&exitset, SYS_exec); |
407a8389 SG |
1639 | #endif |
1640 | #ifdef SYS_execve | |
35f5886e | 1641 | praddset (&exitset, SYS_execve); |
407a8389 SG |
1642 | #endif |
1643 | #ifdef SYS_execv | |
1644 | praddset(&exitset, SYS_execv); | |
1645 | #endif | |
1646 | ||
35f5886e FF |
1647 | if (ioctl (fd, PIOCSEXIT, &exitset) < 0) |
1648 | { | |
1649 | perror (procname); | |
1650 | fflush (stderr); | |
1651 | _exit (127); | |
1652 | } | |
cc221e76 FF |
1653 | |
1654 | /* Turn off inherit-on-fork flag so that all grand-children of gdb | |
1655 | start with tracing flags cleared. */ | |
1656 | ||
5c1c5e67 | 1657 | #if defined (PIOCRESET) /* New method */ |
cc221e76 FF |
1658 | { |
1659 | long pr_flags; | |
1660 | pr_flags = PR_FORK; | |
4ed3a9ea | 1661 | ioctl (fd, PIOCRESET, &pr_flags); |
cc221e76 | 1662 | } |
5c1c5e67 FF |
1663 | #else |
1664 | #if defined (PIOCRFORK) /* Original method */ | |
4ed3a9ea | 1665 | ioctl (fd, PIOCRFORK, NULL); |
cc221e76 | 1666 | #endif |
ec8ceca3 JG |
1667 | #endif |
1668 | ||
1669 | /* Turn on run-on-last-close flag so that this process will not hang | |
1670 | if GDB goes away for some reason. */ | |
1671 | ||
1672 | #if defined (PIOCSET) /* New method */ | |
1673 | { | |
1674 | long pr_flags; | |
1675 | pr_flags = PR_RLC; | |
1676 | (void) ioctl (fd, PIOCSET, &pr_flags); | |
1677 | } | |
1678 | #else | |
1679 | #if defined (PIOCSRLC) /* Original method */ | |
1680 | (void) ioctl (fd, PIOCSRLC, 0); | |
1681 | #endif | |
cc221e76 | 1682 | #endif |
35f5886e FF |
1683 | } |
1684 | ||
f8b76e70 FF |
1685 | /* |
1686 | ||
a39ad5ce FF |
1687 | GLOBAL FUNCTION |
1688 | ||
1689 | proc_iterate_over_mappings -- call function for every mapped space | |
1690 | ||
1691 | SYNOPSIS | |
1692 | ||
1693 | int proc_iterate_over_mappings (int (*func)()) | |
1694 | ||
1695 | DESCRIPTION | |
1696 | ||
1697 | Given a pointer to a function, call that function for every | |
1698 | mapped address space, passing it an open file descriptor for | |
1699 | the file corresponding to that mapped address space (if any) | |
1700 | and the base address of the mapped space. Quit when we hit | |
1701 | the end of the mappings or the function returns nonzero. | |
1702 | */ | |
1703 | ||
1704 | int | |
1ab3bf1b JG |
1705 | proc_iterate_over_mappings (func) |
1706 | int (*func) PARAMS ((int, CORE_ADDR)); | |
a39ad5ce FF |
1707 | { |
1708 | int nmap; | |
1709 | int fd; | |
1710 | int funcstat = 0; | |
1711 | struct prmap *prmaps; | |
1712 | struct prmap *prmap; | |
de43d7d0 SG |
1713 | struct procinfo *pi; |
1714 | ||
1715 | pi = current_procinfo; | |
a39ad5ce | 1716 | |
de43d7d0 | 1717 | if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0) |
a39ad5ce | 1718 | { |
1ab3bf1b | 1719 | prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); |
de43d7d0 | 1720 | if (ioctl (pi->fd, PIOCMAP, prmaps) == 0) |
a39ad5ce FF |
1721 | { |
1722 | for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap) | |
1723 | { | |
de43d7d0 | 1724 | fd = proc_address_to_fd (pi, (CORE_ADDR) prmap -> pr_vaddr, 0); |
1ab3bf1b | 1725 | funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr); |
a39ad5ce FF |
1726 | close (fd); |
1727 | } | |
1728 | } | |
1729 | } | |
1730 | return (funcstat); | |
1731 | } | |
1732 | ||
3fbdd536 | 1733 | #if 0 /* Currently unused */ |
a39ad5ce FF |
1734 | /* |
1735 | ||
f8b76e70 FF |
1736 | GLOBAL FUNCTION |
1737 | ||
1738 | proc_base_address -- find base address for segment containing address | |
1739 | ||
1740 | SYNOPSIS | |
1741 | ||
1742 | CORE_ADDR proc_base_address (CORE_ADDR addr) | |
1743 | ||
1744 | DESCRIPTION | |
1745 | ||
1746 | Given an address of a location in the inferior, find and return | |
1747 | the base address of the mapped segment containing that address. | |
1748 | ||
1749 | This is used for example, by the shared library support code, | |
1750 | where we have the pc value for some location in the shared library | |
1751 | where we are stopped, and need to know the base address of the | |
1752 | segment containing that address. | |
1753 | */ | |
1754 | ||
f8b76e70 | 1755 | CORE_ADDR |
1ab3bf1b | 1756 | proc_base_address (addr) |
cc221e76 | 1757 | CORE_ADDR addr; |
f8b76e70 FF |
1758 | { |
1759 | int nmap; | |
1760 | struct prmap *prmaps; | |
1761 | struct prmap *prmap; | |
1762 | CORE_ADDR baseaddr = 0; | |
de43d7d0 | 1763 | struct procinfo *pi; |
f8b76e70 | 1764 | |
de43d7d0 SG |
1765 | pi = current_procinfo; |
1766 | ||
1767 | if (ioctl (pi->fd, PIOCNMAP, &nmap) == 0) | |
f8b76e70 | 1768 | { |
1ab3bf1b | 1769 | prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); |
de43d7d0 | 1770 | if (ioctl (pi->fd, PIOCMAP, prmaps) == 0) |
f8b76e70 FF |
1771 | { |
1772 | for (prmap = prmaps; prmap -> pr_size; ++prmap) | |
1773 | { | |
1774 | if ((prmap -> pr_vaddr <= (caddr_t) addr) && | |
1775 | (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr)) | |
1776 | { | |
1777 | baseaddr = (CORE_ADDR) prmap -> pr_vaddr; | |
1778 | break; | |
1779 | } | |
1780 | } | |
1781 | } | |
1782 | } | |
1783 | return (baseaddr); | |
1784 | } | |
1785 | ||
1ab3bf1b JG |
1786 | #endif /* 0 */ |
1787 | ||
f8b76e70 FF |
1788 | /* |
1789 | ||
cc221e76 | 1790 | LOCAL FUNCTION |
f8b76e70 FF |
1791 | |
1792 | proc_address_to_fd -- return open fd for file mapped to address | |
1793 | ||
1794 | SYNOPSIS | |
1795 | ||
de43d7d0 | 1796 | int proc_address_to_fd (struct procinfo *pi, CORE_ADDR addr, complain) |
f8b76e70 FF |
1797 | |
1798 | DESCRIPTION | |
1799 | ||
1800 | Given an address in the current inferior's address space, use the | |
1801 | /proc interface to find an open file descriptor for the file that | |
1802 | this address was mapped in from. Return -1 if there is no current | |
1803 | inferior. Print a warning message if there is an inferior but | |
1804 | the address corresponds to no file (IE a bogus address). | |
1805 | ||
1806 | */ | |
1807 | ||
1ab3bf1b | 1808 | static int |
de43d7d0 SG |
1809 | proc_address_to_fd (pi, addr, complain) |
1810 | struct procinfo *pi; | |
1ab3bf1b JG |
1811 | CORE_ADDR addr; |
1812 | int complain; | |
f8b76e70 FF |
1813 | { |
1814 | int fd = -1; | |
1815 | ||
de43d7d0 | 1816 | if ((fd = ioctl (pi->fd, PIOCOPENM, (caddr_t *) &addr)) < 0) |
f8b76e70 | 1817 | { |
de43d7d0 | 1818 | if (complain) |
f8b76e70 | 1819 | { |
de43d7d0 SG |
1820 | print_sys_errmsg (pi->pathname, errno); |
1821 | warning ("can't find mapped file for address 0x%x", addr); | |
f8b76e70 FF |
1822 | } |
1823 | } | |
1824 | return (fd); | |
1825 | } | |
1826 | ||
35f5886e | 1827 | |
3fbdd536 JG |
1828 | /* Attach to process PID, then initialize for debugging it |
1829 | and wait for the trace-trap that results from attaching. */ | |
1830 | ||
1831 | static void | |
1832 | procfs_attach (args, from_tty) | |
1833 | char *args; | |
1834 | int from_tty; | |
1835 | { | |
1836 | char *exec_file; | |
1837 | int pid; | |
1838 | ||
1839 | if (!args) | |
1840 | error_no_arg ("process-id to attach"); | |
1841 | ||
1842 | pid = atoi (args); | |
1843 | ||
1844 | if (pid == getpid()) /* Trying to masturbate? */ | |
1845 | error ("I refuse to debug myself!"); | |
1846 | ||
1847 | if (from_tty) | |
1848 | { | |
1849 | exec_file = (char *) get_exec_file (0); | |
1850 | ||
1851 | if (exec_file) | |
25286543 | 1852 | printf ("Attaching to program `%s', %s\n", exec_file, target_pid_to_str (pid)); |
3fbdd536 | 1853 | else |
25286543 | 1854 | printf ("Attaching to %s\n", target_pid_to_str (pid)); |
3fbdd536 JG |
1855 | |
1856 | fflush (stdout); | |
1857 | } | |
1858 | ||
1859 | do_attach (pid); | |
1860 | inferior_pid = pid; | |
1861 | push_target (&procfs_ops); | |
1862 | } | |
1863 | ||
1864 | ||
1865 | /* Take a program previously attached to and detaches it. | |
1866 | The program resumes execution and will no longer stop | |
1867 | on signals, etc. We'd better not have left any breakpoints | |
1868 | in the program or it'll die when it hits one. For this | |
1869 | to work, it may be necessary for the process to have been | |
1870 | previously attached. It *might* work if the program was | |
1871 | started via the normal ptrace (PTRACE_TRACEME). */ | |
1872 | ||
1873 | static void | |
1874 | procfs_detach (args, from_tty) | |
1875 | char *args; | |
1876 | int from_tty; | |
1877 | { | |
1878 | int siggnal = 0; | |
1879 | ||
1880 | if (from_tty) | |
1881 | { | |
1882 | char *exec_file = get_exec_file (0); | |
1883 | if (exec_file == 0) | |
1884 | exec_file = ""; | |
25286543 SG |
1885 | printf ("Detaching from program: %s %s\n", |
1886 | exec_file, target_pid_to_str (inferior_pid)); | |
3fbdd536 JG |
1887 | fflush (stdout); |
1888 | } | |
1889 | if (args) | |
1890 | siggnal = atoi (args); | |
1891 | ||
1892 | do_detach (siggnal); | |
1893 | inferior_pid = 0; | |
1894 | unpush_target (&procfs_ops); /* Pop out of handling an inferior */ | |
1895 | } | |
1896 | ||
1897 | /* Get ready to modify the registers array. On machines which store | |
1898 | individual registers, this doesn't need to do anything. On machines | |
1899 | which store all the registers in one fell swoop, this makes sure | |
1900 | that registers contains all the registers from the program being | |
1901 | debugged. */ | |
1902 | ||
1903 | static void | |
1904 | procfs_prepare_to_store () | |
1905 | { | |
1906 | #ifdef CHILD_PREPARE_TO_STORE | |
1907 | CHILD_PREPARE_TO_STORE (); | |
1908 | #endif | |
1909 | } | |
1910 | ||
1911 | /* Print status information about what we're accessing. */ | |
1912 | ||
1913 | static void | |
1914 | procfs_files_info (ignore) | |
1915 | struct target_ops *ignore; | |
1916 | { | |
25286543 SG |
1917 | printf ("\tUsing the running image of %s %s via /proc.\n", |
1918 | attach_flag? "attached": "child", target_pid_to_str (inferior_pid)); | |
3fbdd536 JG |
1919 | } |
1920 | ||
1921 | /* ARGSUSED */ | |
1922 | static void | |
1923 | procfs_open (arg, from_tty) | |
1924 | char *arg; | |
1925 | int from_tty; | |
1926 | { | |
1927 | error ("Use the \"run\" command to start a Unix child process."); | |
1928 | } | |
35f5886e FF |
1929 | |
1930 | /* | |
1931 | ||
3fbdd536 | 1932 | LOCAL FUNCTION |
35f5886e | 1933 | |
3fbdd536 | 1934 | do_attach -- attach to an already existing process |
35f5886e FF |
1935 | |
1936 | SYNOPSIS | |
1937 | ||
3fbdd536 | 1938 | int do_attach (int pid) |
35f5886e FF |
1939 | |
1940 | DESCRIPTION | |
1941 | ||
1942 | Attach to an already existing process with the specified process | |
1943 | id. If the process is not already stopped, query whether to | |
1944 | stop it or not. | |
1945 | ||
1946 | NOTES | |
1947 | ||
1948 | The option of stopping at attach time is specific to the /proc | |
1949 | versions of gdb. Versions using ptrace force the attachee | |
ec8ceca3 JG |
1950 | to stop. (I have changed this version to do so, too. All you |
1951 | have to do is "continue" to make it go on. -- gnu@cygnus.com) | |
35f5886e FF |
1952 | |
1953 | */ | |
1954 | ||
3fbdd536 JG |
1955 | static int |
1956 | do_attach (pid) | |
1ab3bf1b | 1957 | int pid; |
35f5886e | 1958 | { |
ec8ceca3 | 1959 | int result; |
de43d7d0 SG |
1960 | struct procinfo *pi; |
1961 | ||
1962 | pi = (struct procinfo *) xmalloc (sizeof (struct procinfo)); | |
ec8ceca3 | 1963 | |
de43d7d0 | 1964 | if (!open_proc_file (pid, pi, O_RDWR)) |
35f5886e | 1965 | { |
de43d7d0 SG |
1966 | free (pi); |
1967 | perror_with_name (pi->pathname); | |
35f5886e FF |
1968 | /* NOTREACHED */ |
1969 | } | |
1970 | ||
de43d7d0 SG |
1971 | /* Add new process to process info list */ |
1972 | ||
1973 | pi->next = procinfo_list; | |
1974 | procinfo_list = pi; | |
1975 | ||
1976 | add_fd (pi); /* Add to list for poll/select */ | |
1977 | ||
35f5886e FF |
1978 | /* Get current status of process and if it is not already stopped, |
1979 | then stop it. Remember whether or not it was stopped when we first | |
1980 | examined it. */ | |
1981 | ||
de43d7d0 | 1982 | if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0) |
35f5886e | 1983 | { |
de43d7d0 SG |
1984 | print_sys_errmsg (pi->pathname, errno); |
1985 | close_proc_file (pi); | |
35f5886e FF |
1986 | error ("PIOCSTATUS failed"); |
1987 | } | |
de43d7d0 | 1988 | if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)) |
35f5886e | 1989 | { |
de43d7d0 | 1990 | pi->was_stopped = 1; |
35f5886e FF |
1991 | } |
1992 | else | |
1993 | { | |
de43d7d0 | 1994 | pi->was_stopped = 0; |
ec8ceca3 | 1995 | if (1 || query ("Process is currently running, stop it? ")) |
35f5886e | 1996 | { |
ec8ceca3 JG |
1997 | /* Make it run again when we close it. */ |
1998 | #if defined (PIOCSET) /* New method */ | |
1999 | { | |
2000 | long pr_flags; | |
2001 | pr_flags = PR_RLC; | |
de43d7d0 | 2002 | result = ioctl (pi->fd, PIOCSET, &pr_flags); |
ec8ceca3 JG |
2003 | } |
2004 | #else | |
2005 | #if defined (PIOCSRLC) /* Original method */ | |
de43d7d0 | 2006 | result = ioctl (pi->fd, PIOCSRLC, 0); |
ec8ceca3 JG |
2007 | #endif |
2008 | #endif | |
2009 | if (result < 0) | |
2010 | { | |
de43d7d0 SG |
2011 | print_sys_errmsg (pi->pathname, errno); |
2012 | close_proc_file (pi); | |
ec8ceca3 JG |
2013 | error ("PIOCSRLC or PIOCSET failed"); |
2014 | } | |
de43d7d0 | 2015 | if (ioctl (pi->fd, PIOCSTOP, &pi->prstatus) < 0) |
35f5886e | 2016 | { |
de43d7d0 SG |
2017 | print_sys_errmsg (pi->pathname, errno); |
2018 | close_proc_file (pi); | |
35f5886e FF |
2019 | error ("PIOCSTOP failed"); |
2020 | } | |
de43d7d0 | 2021 | pi->nopass_next_sigstop = 1; |
d65eee73 FF |
2022 | } |
2023 | else | |
2024 | { | |
25286543 | 2025 | printf ("Ok, gdb will wait for %s to stop.\n", target_pid_to_str (pid)); |
35f5886e FF |
2026 | } |
2027 | } | |
ec8ceca3 | 2028 | |
35f5886e FF |
2029 | /* Remember some things about the inferior that we will, or might, change |
2030 | so that we can restore them when we detach. */ | |
2031 | ||
de43d7d0 SG |
2032 | ioctl (pi->fd, PIOCGTRACE, &pi->saved_trace); |
2033 | ioctl (pi->fd, PIOCGHOLD, &pi->saved_sighold); | |
2034 | ioctl (pi->fd, PIOCGFAULT, &pi->saved_fltset); | |
2035 | ioctl (pi->fd, PIOCGENTRY, &pi->saved_entryset); | |
2036 | ioctl (pi->fd, PIOCGEXIT, &pi->saved_exitset); | |
35f5886e FF |
2037 | |
2038 | /* Set up trace and fault sets, as gdb expects them. */ | |
2039 | ||
de43d7d0 SG |
2040 | memset (&pi->prrun, 0, sizeof (pi->prrun)); |
2041 | prfillset (&pi->prrun.pr_trace); | |
2042 | procfs_notice_signals (pid); | |
2043 | prfillset (&pi->prrun.pr_fault); | |
2044 | prdelset (&pi->prrun.pr_fault, FLTPAGE); | |
2045 | if (ioctl (pi->fd, PIOCSFAULT, &pi->prrun.pr_fault)) | |
35f5886e | 2046 | { |
f66f459f | 2047 | print_sys_errmsg ("PIOCSFAULT failed", errno); |
35f5886e | 2048 | } |
de43d7d0 | 2049 | if (ioctl (pi->fd, PIOCSTRACE, &pi->prrun.pr_trace)) |
35f5886e | 2050 | { |
f66f459f | 2051 | print_sys_errmsg ("PIOCSTRACE failed", errno); |
35f5886e FF |
2052 | } |
2053 | attach_flag = 1; | |
2054 | return (pid); | |
2055 | } | |
2056 | ||
2057 | /* | |
2058 | ||
3fbdd536 | 2059 | LOCAL FUNCTION |
35f5886e | 2060 | |
3fbdd536 | 2061 | do_detach -- detach from an attached-to process |
35f5886e FF |
2062 | |
2063 | SYNOPSIS | |
2064 | ||
3fbdd536 | 2065 | void do_detach (int signal) |
35f5886e FF |
2066 | |
2067 | DESCRIPTION | |
2068 | ||
2069 | Detach from the current attachee. | |
2070 | ||
2071 | If signal is non-zero, the attachee is started running again and sent | |
2072 | the specified signal. | |
2073 | ||
2074 | If signal is zero and the attachee was not already stopped when we | |
2075 | attached to it, then we make it runnable again when we detach. | |
2076 | ||
2077 | Otherwise, we query whether or not to make the attachee runnable | |
2078 | again, since we may simply want to leave it in the state it was in | |
2079 | when we attached. | |
2080 | ||
2081 | We report any problems, but do not consider them errors, since we | |
2082 | MUST detach even if some things don't seem to go right. This may not | |
2083 | be the ideal situation. (FIXME). | |
2084 | */ | |
2085 | ||
3fbdd536 JG |
2086 | static void |
2087 | do_detach (signal) | |
1ab3bf1b | 2088 | int signal; |
35f5886e | 2089 | { |
ec8ceca3 | 2090 | int result; |
de43d7d0 SG |
2091 | struct procinfo *pi; |
2092 | ||
2093 | pi = current_procinfo; | |
ec8ceca3 | 2094 | |
35f5886e FF |
2095 | if (signal) |
2096 | { | |
de43d7d0 | 2097 | set_proc_siginfo (pi, signal); |
35f5886e | 2098 | } |
de43d7d0 | 2099 | if (ioctl (pi->fd, PIOCSEXIT, &pi->saved_exitset) < 0) |
35f5886e | 2100 | { |
de43d7d0 | 2101 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2102 | printf ("PIOCSEXIT failed.\n"); |
2103 | } | |
de43d7d0 | 2104 | if (ioctl (pi->fd, PIOCSENTRY, &pi->saved_entryset) < 0) |
35f5886e | 2105 | { |
de43d7d0 | 2106 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2107 | printf ("PIOCSENTRY failed.\n"); |
2108 | } | |
de43d7d0 | 2109 | if (ioctl (pi->fd, PIOCSTRACE, &pi->saved_trace) < 0) |
35f5886e | 2110 | { |
de43d7d0 | 2111 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2112 | printf ("PIOCSTRACE failed.\n"); |
2113 | } | |
de43d7d0 | 2114 | if (ioctl (pi->fd, PIOCSHOLD, &pi->saved_sighold) < 0) |
cc221e76 | 2115 | { |
de43d7d0 | 2116 | print_sys_errmsg (pi->pathname, errno); |
cc221e76 FF |
2117 | printf ("PIOSCHOLD failed.\n"); |
2118 | } | |
de43d7d0 | 2119 | if (ioctl (pi->fd, PIOCSFAULT, &pi->saved_fltset) < 0) |
35f5886e | 2120 | { |
de43d7d0 | 2121 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2122 | printf ("PIOCSFAULT failed.\n"); |
2123 | } | |
de43d7d0 | 2124 | if (ioctl (pi->fd, PIOCSTATUS, &pi->prstatus) < 0) |
35f5886e | 2125 | { |
de43d7d0 | 2126 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2127 | printf ("PIOCSTATUS failed.\n"); |
2128 | } | |
2129 | else | |
2130 | { | |
de43d7d0 | 2131 | if (signal || (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))) |
35f5886e | 2132 | { |
de43d7d0 | 2133 | if (signal || !pi->was_stopped || |
35f5886e FF |
2134 | query ("Was stopped when attached, make it runnable again? ")) |
2135 | { | |
ec8ceca3 | 2136 | /* Clear any fault that might have stopped it. */ |
de43d7d0 | 2137 | if (ioctl (pi->fd, PIOCCFAULT, 0)) |
ec8ceca3 | 2138 | { |
de43d7d0 | 2139 | print_sys_errmsg (pi->pathname, errno); |
ec8ceca3 JG |
2140 | printf ("PIOCCFAULT failed.\n"); |
2141 | } | |
2142 | ||
2143 | /* Make it run again when we close it. */ | |
2144 | #if defined (PIOCSET) /* New method */ | |
2145 | { | |
2146 | long pr_flags; | |
2147 | pr_flags = PR_RLC; | |
de43d7d0 | 2148 | result = ioctl (pi->fd, PIOCSET, &pr_flags); |
ec8ceca3 JG |
2149 | } |
2150 | #else | |
2151 | #if defined (PIOCSRLC) /* Original method */ | |
de43d7d0 | 2152 | result = ioctl (pi->fd, PIOCSRLC, 0); |
ec8ceca3 JG |
2153 | #endif |
2154 | #endif | |
2155 | if (result) | |
35f5886e | 2156 | { |
de43d7d0 | 2157 | print_sys_errmsg (pi->pathname, errno); |
ec8ceca3 | 2158 | printf ("PIOCSRLC or PIOCSET failed.\n"); |
35f5886e FF |
2159 | } |
2160 | } | |
2161 | } | |
2162 | } | |
de43d7d0 | 2163 | close_proc_file (pi); |
35f5886e FF |
2164 | attach_flag = 0; |
2165 | } | |
2166 | ||
2167 | /* | |
2168 | ||
3fbdd536 JG |
2169 | LOCAL FUNCTION |
2170 | ||
2171 | procfs_wait -- emulate wait() as much as possible | |
2172 | Wait for child to do something. Return pid of child, or -1 in case | |
2173 | of error; store status through argument pointer STATUS. | |
35f5886e | 2174 | |
35f5886e FF |
2175 | |
2176 | SYNOPSIS | |
2177 | ||
de43d7d0 | 2178 | int procfs_wait (int pid, int *statloc) |
35f5886e FF |
2179 | |
2180 | DESCRIPTION | |
2181 | ||
2182 | Try to emulate wait() as much as possible. Not sure why we can't | |
2183 | just use wait(), but it seems to have problems when applied to a | |
2184 | process being controlled with the /proc interface. | |
2185 | ||
2186 | NOTES | |
2187 | ||
2188 | We have a race problem here with no obvious solution. We need to let | |
2189 | the inferior run until it stops on an event of interest, which means | |
2190 | that we need to use the PIOCWSTOP ioctl. However, we cannot use this | |
2191 | ioctl if the process is already stopped on something that is not an | |
2192 | event of interest, or the call will hang indefinitely. Thus we first | |
2193 | use PIOCSTATUS to see if the process is not stopped. If not, then we | |
2194 | use PIOCWSTOP. But during the window between the two, if the process | |
2195 | stops for any reason that is not an event of interest (such as a job | |
2196 | control signal) then gdb will hang. One possible workaround is to set | |
2197 | an alarm to wake up every minute of so and check to see if the process | |
2198 | is still running, and if so, then reissue the PIOCWSTOP. But this is | |
2199 | a real kludge, so has not been implemented. FIXME: investigate | |
2200 | alternatives. | |
2201 | ||
2202 | FIXME: Investigate why wait() seems to have problems with programs | |
2203 | being control by /proc routines. | |
2204 | ||
2205 | */ | |
2206 | ||
3fbdd536 | 2207 | static int |
de43d7d0 SG |
2208 | procfs_wait (pid, statloc) |
2209 | int pid; | |
1ab3bf1b | 2210 | int *statloc; |
35f5886e FF |
2211 | { |
2212 | short what; | |
2213 | short why; | |
2214 | int statval = 0; | |
2215 | int checkerr = 0; | |
2216 | int rtnval = -1; | |
de43d7d0 SG |
2217 | struct procinfo *pi; |
2218 | ||
2219 | if (pid != -1) /* Non-specific process? */ | |
2220 | pi = NULL; | |
2221 | else | |
2222 | for (pi = procinfo_list; pi; pi = pi->next) | |
2223 | if (pi->had_event) | |
2224 | break; | |
2225 | ||
2226 | wait_again: | |
2227 | ||
2228 | if (!pi) | |
7c5d526e | 2229 | pi = wait_fd (); |
de43d7d0 SG |
2230 | |
2231 | if (pid != -1) | |
2232 | for (pi = procinfo_list; pi; pi = pi->next) | |
2233 | if (pi->pid == pid && pi->had_event) | |
2234 | break; | |
2235 | ||
2236 | if (!pi && !checkerr) | |
2237 | goto wait_again; | |
2238 | ||
2239 | if (!checkerr && !(pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))) | |
2240 | { | |
2241 | if (ioctl (pi->fd, PIOCWSTOP, &pi->prstatus) < 0) | |
2242 | { | |
2243 | checkerr++; | |
2244 | } | |
35f5886e FF |
2245 | } |
2246 | if (checkerr) | |
2247 | { | |
2248 | if (errno == ENOENT) | |
2249 | { | |
2250 | rtnval = wait (&statval); | |
2251 | if (rtnval != inferior_pid) | |
2252 | { | |
de43d7d0 | 2253 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2254 | error ("PIOCWSTOP, wait failed, returned %d", rtnval); |
2255 | /* NOTREACHED */ | |
2256 | } | |
2257 | } | |
2258 | else | |
2259 | { | |
de43d7d0 | 2260 | print_sys_errmsg (pi->pathname, errno); |
35f5886e FF |
2261 | error ("PIOCSTATUS or PIOCWSTOP failed."); |
2262 | /* NOTREACHED */ | |
2263 | } | |
2264 | } | |
de43d7d0 | 2265 | else if (pi->prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)) |
35f5886e | 2266 | { |
de43d7d0 SG |
2267 | rtnval = pi->prstatus.pr_pid; |
2268 | why = pi->prstatus.pr_why; | |
2269 | what = pi->prstatus.pr_what; | |
2270 | ||
2271 | switch (why) | |
35f5886e | 2272 | { |
de43d7d0 | 2273 | case PR_SIGNALLED: |
35f5886e | 2274 | statval = (what << 8) | 0177; |
de43d7d0 SG |
2275 | break; |
2276 | case PR_SYSEXIT: | |
2277 | switch (what) | |
2278 | { | |
407a8389 | 2279 | #ifdef SYS_exec |
de43d7d0 | 2280 | case SYS_exec: |
407a8389 SG |
2281 | #endif |
2282 | #ifdef SYS_execve | |
de43d7d0 | 2283 | case SYS_execve: |
407a8389 SG |
2284 | #endif |
2285 | #ifdef SYS_execv | |
de43d7d0 | 2286 | case SYS_execv: |
407a8389 | 2287 | #endif |
de43d7d0 SG |
2288 | statval = (SIGTRAP << 8) | 0177; |
2289 | break; | |
2290 | #ifdef SYS_sproc | |
2291 | case SYS_sproc: | |
2292 | /* We've just detected the completion of an sproc system call. Now we need to | |
2293 | setup a procinfo struct for this thread, and notify the thread system of the | |
2294 | new arrival. */ | |
2295 | ||
2296 | /* If sproc failed, then nothing interesting happened. Continue the process and | |
2297 | go back to sleep. */ | |
2298 | ||
2299 | if (pi->prstatus.pr_errno != 0) | |
2300 | { | |
2301 | pi->prrun.pr_flags &= PRSTEP; | |
2302 | pi->prrun.pr_flags |= PRCFAULT; | |
2303 | ||
2304 | if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0) | |
2305 | perror_with_name (pi->pathname); | |
2306 | ||
2307 | goto wait_again; | |
2308 | } | |
2309 | ||
2310 | /* At this point, the new thread is stopped at it's first instruction, and | |
2311 | the parent is stopped at the exit from sproc. */ | |
2312 | ||
2313 | /* Notify the caller of the arrival of a new thread. */ | |
2314 | create_procinfo (pi->prstatus.pr_rval1); | |
2315 | ||
2316 | rtnval = pi->prstatus.pr_rval1; | |
2317 | statval = (SIGTRAP << 8) | 0177; | |
2318 | ||
2319 | break; | |
2320 | #endif /* SYS_sproc */ | |
2321 | ||
2322 | default: | |
2323 | error ("PIOCSTATUS (PR_SYSEXIT): Unknown system call %d", what); | |
2324 | } | |
2325 | break; | |
2326 | case PR_REQUESTED: | |
35f5886e | 2327 | statval = (SIGSTOP << 8) | 0177; |
de43d7d0 SG |
2328 | break; |
2329 | case PR_JOBCONTROL: | |
35f5886e | 2330 | statval = (what << 8) | 0177; |
de43d7d0 SG |
2331 | break; |
2332 | case PR_FAULTED: | |
35f5886e FF |
2333 | switch (what) |
2334 | { | |
2335 | case FLTPRIV: | |
2336 | case FLTILL: | |
2337 | statval = (SIGILL << 8) | 0177; | |
2338 | break; | |
2339 | case FLTBPT: | |
2340 | case FLTTRACE: | |
2341 | statval = (SIGTRAP << 8) | 0177; | |
2342 | break; | |
2343 | case FLTSTACK: | |
2344 | case FLTACCESS: | |
2345 | case FLTBOUNDS: | |
2346 | statval = (SIGSEGV << 8) | 0177; | |
2347 | break; | |
2348 | case FLTIOVF: | |
2349 | case FLTIZDIV: | |
2350 | case FLTFPE: | |
2351 | statval = (SIGFPE << 8) | 0177; | |
2352 | break; | |
2353 | case FLTPAGE: /* Recoverable page fault */ | |
2354 | default: | |
35f5886e | 2355 | error ("PIOCWSTOP, unknown why %d, what %d", why, what); |
35f5886e | 2356 | } |
de43d7d0 SG |
2357 | break; |
2358 | default: | |
35f5886e | 2359 | error ("PIOCWSTOP, unknown why %d, what %d", why, what); |
35f5886e | 2360 | } |
de43d7d0 SG |
2361 | /* Stop all the other threads when any of them stops. */ |
2362 | ||
2363 | { | |
2364 | struct procinfo *procinfo; | |
2365 | ||
2366 | for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next) | |
2367 | { | |
2368 | if (!procinfo->had_event) | |
2369 | if (ioctl (procinfo->fd, PIOCSTOP, &procinfo->prstatus) < 0) | |
2370 | { | |
2371 | print_sys_errmsg (procinfo->pathname, errno); | |
2372 | error ("PIOCSTOP failed"); | |
2373 | } | |
2374 | } | |
2375 | } | |
35f5886e FF |
2376 | } |
2377 | else | |
2378 | { | |
2379 | error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x", | |
de43d7d0 | 2380 | pi->prstatus.pr_flags); |
35f5886e | 2381 | } |
3fbdd536 | 2382 | |
35f5886e FF |
2383 | if (statloc) |
2384 | { | |
2385 | *statloc = statval; | |
2386 | } | |
3fbdd536 JG |
2387 | |
2388 | if (rtnval == -1) /* No more children to wait for */ | |
2389 | { | |
2390 | fprintf (stderr, "Child process unexpectedly missing.\n"); | |
2391 | *statloc = 42; /* Claim it exited with signal 42 */ | |
2392 | return rtnval; | |
2393 | } | |
2394 | ||
de43d7d0 | 2395 | pi->had_event = 0; /* Indicate that we've seen this one */ |
35f5886e FF |
2396 | return (rtnval); |
2397 | } | |
2398 | ||
2399 | /* | |
2400 | ||
6b801388 FF |
2401 | LOCAL FUNCTION |
2402 | ||
2403 | set_proc_siginfo - set a process's current signal info | |
2404 | ||
2405 | SYNOPSIS | |
2406 | ||
2407 | void set_proc_siginfo (struct procinfo *pip, int signo); | |
2408 | ||
2409 | DESCRIPTION | |
2410 | ||
2411 | Given a pointer to a process info struct in PIP and a signal number | |
2412 | in SIGNO, set the process's current signal and its associated signal | |
2413 | information. The signal will be delivered to the process immediately | |
2414 | after execution is resumed, even if it is being held. In addition, | |
2415 | this particular delivery will not cause another PR_SIGNALLED stop | |
2416 | even if the signal is being traced. | |
2417 | ||
2418 | If we are not delivering the same signal that the prstatus siginfo | |
2419 | struct contains information about, then synthesize a siginfo struct | |
2420 | to match the signal we are doing to deliver, make it of the type | |
2421 | "generated by a user process", and send this synthesized copy. When | |
2422 | used to set the inferior's signal state, this will be required if we | |
2423 | are not currently stopped because of a traced signal, or if we decide | |
2424 | to continue with a different signal. | |
2425 | ||
2426 | Note that when continuing the inferior from a stop due to receipt | |
2427 | of a traced signal, we either have set PRCSIG to clear the existing | |
2428 | signal, or we have to call this function to do a PIOCSSIG with either | |
2429 | the existing siginfo struct from pr_info, or one we have synthesized | |
2430 | appropriately for the signal we want to deliver. Otherwise if the | |
2431 | signal is still being traced, the inferior will immediately stop | |
2432 | again. | |
2433 | ||
2434 | See siginfo(5) for more details. | |
2435 | */ | |
2436 | ||
2437 | static void | |
2438 | set_proc_siginfo (pip, signo) | |
cc221e76 FF |
2439 | struct procinfo *pip; |
2440 | int signo; | |
6b801388 FF |
2441 | { |
2442 | struct siginfo newsiginfo; | |
2443 | struct siginfo *sip; | |
2444 | ||
de43d7d0 | 2445 | if (signo == pip -> prstatus.pr_info.si_signo) |
6b801388 | 2446 | { |
de43d7d0 SG |
2447 | sip = &pip -> prstatus.pr_info; |
2448 | } | |
2449 | else | |
2450 | { | |
2451 | memset ((char *) &newsiginfo, 0, sizeof (newsiginfo)); | |
2452 | sip = &newsiginfo; | |
2453 | sip -> si_signo = signo; | |
2454 | sip -> si_code = 0; | |
2455 | sip -> si_errno = 0; | |
2456 | sip -> si_pid = getpid (); | |
2457 | sip -> si_uid = getuid (); | |
2458 | } | |
2459 | if (ioctl (pip -> fd, PIOCSSIG, sip) < 0) | |
2460 | { | |
2461 | print_sys_errmsg (pip -> pathname, errno); | |
2462 | warning ("PIOCSSIG failed"); | |
6b801388 FF |
2463 | } |
2464 | } | |
2465 | ||
25286543 | 2466 | /* Resume execution of process PID. If STEP is nozero, then |
59ba57da JK |
2467 | just single step it. If SIGNAL is nonzero, restart it with that |
2468 | signal activated. */ | |
35f5886e | 2469 | |
3fbdd536 | 2470 | static void |
25286543 SG |
2471 | procfs_resume (pid, step, signo) |
2472 | int pid; | |
1ab3bf1b | 2473 | int step; |
cc221e76 | 2474 | int signo; |
35f5886e | 2475 | { |
59ba57da | 2476 | int signal_to_pass; |
de43d7d0 SG |
2477 | struct procinfo *pi, *procinfo; |
2478 | ||
2479 | pi = find_procinfo (pid == -1 ? inferior_pid : pid, 0); | |
59ba57da | 2480 | |
35f5886e | 2481 | errno = 0; |
de43d7d0 | 2482 | pi->prrun.pr_flags = PRSTRACE | PRSFAULT | PRCFAULT; |
99fd9e3e | 2483 | |
59ba57da JK |
2484 | #if 0 |
2485 | /* It should not be necessary. If the user explicitly changes the value, | |
2486 | value_assign calls write_register_bytes, which writes it. */ | |
2487 | /* It may not be absolutely necessary to specify the PC value for | |
2488 | restarting, but to be safe we use the value that gdb considers | |
2489 | to be current. One case where this might be necessary is if the | |
2490 | user explicitly changes the PC value that gdb considers to be | |
2491 | current. FIXME: Investigate if this is necessary or not. */ | |
2492 | ||
c3192172 | 2493 | #ifdef PRSVADDR_BROKEN |
99fd9e3e SG |
2494 | /* Can't do this under Solaris running on a Sparc, as there seems to be no |
2495 | place to put nPC. In fact, if you use this, nPC seems to be set to some | |
2496 | random garbage. We have to rely on the fact that PC and nPC have been | |
2497 | written previously via PIOCSREG during a register flush. */ | |
2498 | ||
de43d7d0 SG |
2499 | pi->prrun.pr_vaddr = (caddr_t) *(int *) ®isters[REGISTER_BYTE (PC_REGNUM)]; |
2500 | pi->prrun.pr_flags != PRSVADDR; | |
99fd9e3e | 2501 | #endif |
59ba57da JK |
2502 | #endif |
2503 | ||
de43d7d0 | 2504 | if (signo == SIGSTOP && pi->nopass_next_sigstop) |
59ba57da JK |
2505 | /* When attaching to a child process, if we forced it to stop with |
2506 | a PIOCSTOP, then we will have set the nopass_next_sigstop flag. | |
2507 | Upon resuming the first time after such a stop, we explicitly | |
2508 | inhibit sending it another SIGSTOP, which would be the normal | |
2509 | result of default signal handling. One potential drawback to | |
2510 | this is that we will also ignore any attempt to by the user | |
2511 | to explicitly continue after the attach with a SIGSTOP. Ultimately | |
2512 | this problem should be dealt with by making the routines that | |
2513 | deal with the inferior a little smarter, and possibly even allow | |
2514 | an inferior to continue running at the same time as gdb. (FIXME?) */ | |
2515 | signal_to_pass = 0; | |
2516 | else if (signo == SIGTSTP | |
de43d7d0 SG |
2517 | && pi->prstatus.pr_cursig == SIGTSTP |
2518 | && pi->prstatus.pr_action.sa_handler == SIG_DFL) | |
59ba57da JK |
2519 | |
2520 | /* We are about to pass the inferior a SIGTSTP whose action is | |
2521 | SIG_DFL. The SIG_DFL action for a SIGTSTP is to stop | |
2522 | (notifying the parent via wait()), and then keep going from the | |
2523 | same place when the parent is ready for you to keep going. So | |
2524 | under the debugger, it should do nothing (as if the program had | |
2525 | been stopped and then later resumed. Under ptrace, this | |
2526 | happens for us, but under /proc, the system obligingly stops | |
2527 | the process, and wait_for_inferior would have no way of | |
2528 | distinguishing that type of stop (which indicates that we | |
2529 | should just start it again), with a stop due to the pr_trace | |
2530 | field of the prrun_t struct. | |
2531 | ||
2532 | Note that if the SIGTSTP is being caught, we *do* need to pass it, | |
2533 | because the handler needs to get executed. */ | |
2534 | signal_to_pass = 0; | |
2535 | else | |
2536 | signal_to_pass = signo; | |
99fd9e3e | 2537 | |
59ba57da | 2538 | if (signal_to_pass) |
35f5886e | 2539 | { |
de43d7d0 | 2540 | set_proc_siginfo (pi, signal_to_pass); |
35f5886e FF |
2541 | } |
2542 | else | |
2543 | { | |
de43d7d0 | 2544 | pi->prrun.pr_flags |= PRCSIG; |
35f5886e | 2545 | } |
de43d7d0 | 2546 | pi->nopass_next_sigstop = 0; |
35f5886e FF |
2547 | if (step) |
2548 | { | |
de43d7d0 | 2549 | pi->prrun.pr_flags |= PRSTEP; |
35f5886e | 2550 | } |
de43d7d0 | 2551 | if (ioctl (pi->fd, PIOCRUN, &pi->prrun) != 0) |
35f5886e | 2552 | { |
de43d7d0 | 2553 | perror_with_name (pi->pathname); |
35f5886e FF |
2554 | /* NOTREACHED */ |
2555 | } | |
de43d7d0 SG |
2556 | |
2557 | pi->had_event = 0; | |
2558 | ||
2559 | /* Continue all the other threads that haven't had an event of | |
2560 | interest. */ | |
2561 | ||
2562 | if (pid == -1) | |
2563 | for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next) | |
2564 | { | |
2565 | if (pi != procinfo && !procinfo->had_event) | |
2566 | { | |
2567 | procinfo->prrun.pr_flags &= PRSTEP; | |
2568 | procinfo->prrun.pr_flags |= PRCFAULT | PRCSIG; | |
2569 | ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus); | |
2570 | if (ioctl (procinfo->fd, PIOCRUN, &procinfo->prrun) < 0) | |
2571 | { | |
2572 | if (ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus) < 0) | |
2573 | { | |
2574 | fprintf(stderr, "PIOCSTATUS failed, errno=%d\n", errno); | |
2575 | } | |
2576 | print_sys_errmsg (procinfo->pathname, errno); | |
2577 | error ("PIOCRUN failed"); | |
2578 | } | |
2579 | ioctl (procinfo->fd, PIOCSTATUS, &procinfo->prstatus); | |
2580 | } | |
2581 | } | |
35f5886e FF |
2582 | } |
2583 | ||
2584 | /* | |
2585 | ||
3fbdd536 | 2586 | LOCAL FUNCTION |
35f5886e | 2587 | |
3fbdd536 | 2588 | procfs_fetch_registers -- fetch current registers from inferior |
35f5886e FF |
2589 | |
2590 | SYNOPSIS | |
2591 | ||
3fbdd536 | 2592 | void procfs_fetch_registers (int regno) |
35f5886e FF |
2593 | |
2594 | DESCRIPTION | |
2595 | ||
2596 | Read the current values of the inferior's registers, both the | |
2597 | general register set and floating point registers (if supported) | |
2598 | and update gdb's idea of their current values. | |
2599 | ||
2600 | */ | |
2601 | ||
3fbdd536 JG |
2602 | static void |
2603 | procfs_fetch_registers (regno) | |
1ab3bf1b | 2604 | int regno; |
35f5886e | 2605 | { |
de43d7d0 SG |
2606 | struct procinfo *pi; |
2607 | ||
2608 | pi = current_procinfo; | |
2609 | ||
2610 | if (ioctl (pi->fd, PIOCGREG, &pi->gregset) != -1) | |
35f5886e | 2611 | { |
de43d7d0 | 2612 | supply_gregset (&pi->gregset); |
35f5886e FF |
2613 | } |
2614 | #if defined (FP0_REGNUM) | |
de43d7d0 | 2615 | if (ioctl (pi->fd, PIOCGFPREG, &pi->fpregset) != -1) |
35f5886e | 2616 | { |
de43d7d0 | 2617 | supply_fpregset (&pi->fpregset); |
35f5886e FF |
2618 | } |
2619 | #endif | |
2620 | } | |
2621 | ||
fb182850 FF |
2622 | /* |
2623 | ||
35f5886e FF |
2624 | LOCAL FUNCTION |
2625 | ||
de43d7d0 SG |
2626 | proc_init_failed - called whenever /proc access initialization |
2627 | fails | |
35f5886e FF |
2628 | |
2629 | SYNOPSIS | |
2630 | ||
de43d7d0 | 2631 | static void proc_init_failed (struct procinfo *pi, char *why) |
35f5886e FF |
2632 | |
2633 | DESCRIPTION | |
2634 | ||
2635 | This function is called whenever initialization of access to a /proc | |
2636 | entry fails. It prints a suitable error message, does some cleanup, | |
2637 | and then invokes the standard error processing routine which dumps | |
2638 | us back into the command loop. | |
2639 | */ | |
2640 | ||
2641 | static void | |
de43d7d0 SG |
2642 | proc_init_failed (pi, why) |
2643 | struct procinfo *pi; | |
1ab3bf1b | 2644 | char *why; |
35f5886e | 2645 | { |
de43d7d0 SG |
2646 | print_sys_errmsg (pi->pathname, errno); |
2647 | kill (pi->pid, SIGKILL); | |
2648 | close_proc_file (pi); | |
35f5886e FF |
2649 | error (why); |
2650 | /* NOTREACHED */ | |
2651 | } | |
2652 | ||
2653 | /* | |
2654 | ||
2655 | LOCAL FUNCTION | |
2656 | ||
2657 | close_proc_file - close any currently open /proc entry | |
2658 | ||
2659 | SYNOPSIS | |
2660 | ||
a39ad5ce | 2661 | static void close_proc_file (struct procinfo *pip) |
35f5886e FF |
2662 | |
2663 | DESCRIPTION | |
2664 | ||
2665 | Close any currently open /proc entry and mark the process information | |
2666 | entry as invalid. In order to ensure that we don't try to reuse any | |
2667 | stale information, the pid, fd, and pathnames are explicitly | |
2668 | invalidated, which may be overkill. | |
2669 | ||
2670 | */ | |
2671 | ||
2672 | static void | |
1ab3bf1b JG |
2673 | close_proc_file (pip) |
2674 | struct procinfo *pip; | |
35f5886e | 2675 | { |
de43d7d0 SG |
2676 | struct procinfo *procinfo; |
2677 | ||
2678 | remove_fd (pip); /* Remove fd from poll/select list */ | |
2679 | ||
2680 | close (pip -> fd); | |
2681 | ||
2682 | free (pip -> pathname); | |
2683 | ||
2684 | /* Unlink pip from the procinfo chain. Note pip might not be on the list. */ | |
2685 | ||
2686 | if (procinfo_list == pip) | |
2687 | procinfo_list = pip->next; | |
2688 | else | |
2689 | for (procinfo = procinfo_list; procinfo; procinfo = procinfo->next) | |
2690 | if (procinfo->next == pip) | |
2691 | procinfo->next = pip->next; | |
2692 | ||
2693 | free (pip); | |
35f5886e FF |
2694 | } |
2695 | ||
2696 | /* | |
2697 | ||
2698 | LOCAL FUNCTION | |
2699 | ||
2700 | open_proc_file - open a /proc entry for a given process id | |
2701 | ||
2702 | SYNOPSIS | |
2703 | ||
ec8ceca3 | 2704 | static int open_proc_file (int pid, struct procinfo *pip, int mode) |
35f5886e FF |
2705 | |
2706 | DESCRIPTION | |
2707 | ||
ec8ceca3 JG |
2708 | Given a process id and a mode, close the existing open /proc |
2709 | entry (if any) and open one for the new process id, in the | |
2710 | specified mode. Once it is open, then mark the local process | |
2711 | information structure as valid, which guarantees that the pid, | |
2712 | fd, and pathname fields match an open /proc entry. Returns | |
2713 | zero if the open fails, nonzero otherwise. | |
35f5886e FF |
2714 | |
2715 | Note that the pathname is left intact, even when the open fails, | |
2716 | so that callers can use it to construct meaningful error messages | |
2717 | rather than just "file open failed". | |
2718 | */ | |
2719 | ||
2720 | static int | |
ec8ceca3 | 2721 | open_proc_file (pid, pip, mode) |
1ab3bf1b JG |
2722 | int pid; |
2723 | struct procinfo *pip; | |
ec8ceca3 | 2724 | int mode; |
35f5886e | 2725 | { |
de43d7d0 SG |
2726 | pip -> next = NULL; |
2727 | pip -> had_event = 0; | |
2728 | pip -> pathname = xmalloc (32); | |
2729 | pip -> pid = pid; | |
2730 | ||
a39ad5ce | 2731 | sprintf (pip -> pathname, PROC_NAME_FMT, pid); |
de43d7d0 SG |
2732 | if ((pip -> fd = open (pip -> pathname, mode)) < 0) |
2733 | return 0; | |
2734 | ||
2735 | return 1; | |
a39ad5ce FF |
2736 | } |
2737 | ||
f66f459f | 2738 | static char * |
1ab3bf1b JG |
2739 | mappingflags (flags) |
2740 | long flags; | |
a39ad5ce | 2741 | { |
5c1c5e67 | 2742 | static char asciiflags[8]; |
a39ad5ce | 2743 | |
5c1c5e67 FF |
2744 | strcpy (asciiflags, "-------"); |
2745 | #if defined (MA_PHYS) | |
2746 | if (flags & MA_PHYS) asciiflags[0] = 'd'; | |
2747 | #endif | |
2748 | if (flags & MA_STACK) asciiflags[1] = 's'; | |
2749 | if (flags & MA_BREAK) asciiflags[2] = 'b'; | |
2750 | if (flags & MA_SHARED) asciiflags[3] = 's'; | |
2751 | if (flags & MA_READ) asciiflags[4] = 'r'; | |
2752 | if (flags & MA_WRITE) asciiflags[5] = 'w'; | |
2753 | if (flags & MA_EXEC) asciiflags[6] = 'x'; | |
a39ad5ce FF |
2754 | return (asciiflags); |
2755 | } | |
2756 | ||
2757 | static void | |
cc221e76 FF |
2758 | info_proc_flags (pip, summary) |
2759 | struct procinfo *pip; | |
2760 | int summary; | |
2761 | { | |
2762 | struct trans *transp; | |
2763 | ||
2764 | printf_filtered ("%-32s", "Process status flags:"); | |
2765 | if (!summary) | |
2766 | { | |
2767 | printf_filtered ("\n\n"); | |
2768 | } | |
2769 | for (transp = pr_flag_table; transp -> name != NULL; transp++) | |
2770 | { | |
2771 | if (pip -> prstatus.pr_flags & transp -> value) | |
2772 | { | |
2773 | if (summary) | |
2774 | { | |
2775 | printf_filtered ("%s ", transp -> name); | |
2776 | } | |
2777 | else | |
2778 | { | |
2779 | printf_filtered ("\t%-16s %s.\n", transp -> name, transp -> desc); | |
2780 | } | |
2781 | } | |
2782 | } | |
2783 | printf_filtered ("\n"); | |
2784 | } | |
2785 | ||
2786 | static void | |
2787 | info_proc_stop (pip, summary) | |
2788 | struct procinfo *pip; | |
2789 | int summary; | |
2790 | { | |
2791 | struct trans *transp; | |
2792 | int why; | |
2793 | int what; | |
2794 | ||
2795 | why = pip -> prstatus.pr_why; | |
2796 | what = pip -> prstatus.pr_what; | |
2797 | ||
2798 | if (pip -> prstatus.pr_flags & PR_STOPPED) | |
2799 | { | |
2800 | printf_filtered ("%-32s", "Reason for stopping:"); | |
2801 | if (!summary) | |
2802 | { | |
2803 | printf_filtered ("\n\n"); | |
2804 | } | |
2805 | for (transp = pr_why_table; transp -> name != NULL; transp++) | |
2806 | { | |
2807 | if (why == transp -> value) | |
2808 | { | |
2809 | if (summary) | |
2810 | { | |
2811 | printf_filtered ("%s ", transp -> name); | |
2812 | } | |
2813 | else | |
2814 | { | |
2815 | printf_filtered ("\t%-16s %s.\n", | |
2816 | transp -> name, transp -> desc); | |
2817 | } | |
2818 | break; | |
2819 | } | |
2820 | } | |
2821 | ||
2822 | /* Use the pr_why field to determine what the pr_what field means, and | |
2823 | print more information. */ | |
2824 | ||
2825 | switch (why) | |
2826 | { | |
2827 | case PR_REQUESTED: | |
2828 | /* pr_what is unused for this case */ | |
2829 | break; | |
2830 | case PR_JOBCONTROL: | |
2831 | case PR_SIGNALLED: | |
2832 | if (summary) | |
2833 | { | |
2834 | printf_filtered ("%s ", signalname (what)); | |
2835 | } | |
2836 | else | |
2837 | { | |
2838 | printf_filtered ("\t%-16s %s.\n", signalname (what), | |
4ace50a5 | 2839 | safe_strsignal (what)); |
cc221e76 FF |
2840 | } |
2841 | break; | |
2842 | case PR_SYSENTRY: | |
2843 | if (summary) | |
2844 | { | |
2845 | printf_filtered ("%s ", syscallname (what)); | |
2846 | } | |
2847 | else | |
2848 | { | |
2849 | printf_filtered ("\t%-16s %s.\n", syscallname (what), | |
2850 | "Entered this system call"); | |
2851 | } | |
2852 | break; | |
2853 | case PR_SYSEXIT: | |
2854 | if (summary) | |
2855 | { | |
2856 | printf_filtered ("%s ", syscallname (what)); | |
2857 | } | |
2858 | else | |
2859 | { | |
2860 | printf_filtered ("\t%-16s %s.\n", syscallname (what), | |
2861 | "Returned from this system call"); | |
2862 | } | |
2863 | break; | |
2864 | case PR_FAULTED: | |
2865 | if (summary) | |
2866 | { | |
2867 | printf_filtered ("%s ", | |
2868 | lookupname (faults_table, what, "fault")); | |
2869 | } | |
2870 | else | |
2871 | { | |
2872 | printf_filtered ("\t%-16s %s.\n", | |
2873 | lookupname (faults_table, what, "fault"), | |
2874 | lookupdesc (faults_table, what)); | |
2875 | } | |
2876 | break; | |
2877 | } | |
2878 | printf_filtered ("\n"); | |
2879 | } | |
2880 | } | |
2881 | ||
2882 | static void | |
2883 | info_proc_siginfo (pip, summary) | |
2884 | struct procinfo *pip; | |
2885 | int summary; | |
2886 | { | |
2887 | struct siginfo *sip; | |
2888 | ||
2889 | if ((pip -> prstatus.pr_flags & PR_STOPPED) && | |
2890 | (pip -> prstatus.pr_why == PR_SIGNALLED || | |
2891 | pip -> prstatus.pr_why == PR_FAULTED)) | |
2892 | { | |
2893 | printf_filtered ("%-32s", "Additional signal/fault info:"); | |
2894 | sip = &pip -> prstatus.pr_info; | |
2895 | if (summary) | |
2896 | { | |
2897 | printf_filtered ("%s ", signalname (sip -> si_signo)); | |
2898 | if (sip -> si_errno > 0) | |
2899 | { | |
4ace50a5 | 2900 | printf_filtered ("%s ", errnoname (sip -> si_errno)); |
cc221e76 FF |
2901 | } |
2902 | if (sip -> si_code <= 0) | |
2903 | { | |
25286543 SG |
2904 | printf_filtered ("sent by %s, uid %d ", |
2905 | target_pid_to_str (sip -> si_pid), | |
cc221e76 FF |
2906 | sip -> si_uid); |
2907 | } | |
2908 | else | |
2909 | { | |
2910 | printf_filtered ("%s ", sigcodename (sip)); | |
2911 | if ((sip -> si_signo == SIGILL) || | |
2912 | (sip -> si_signo == SIGFPE) || | |
2913 | (sip -> si_signo == SIGSEGV) || | |
2914 | (sip -> si_signo == SIGBUS)) | |
2915 | { | |
2916 | printf_filtered ("addr=%#x ", sip -> si_addr); | |
2917 | } | |
2918 | else if ((sip -> si_signo == SIGCHLD)) | |
2919 | { | |
25286543 SG |
2920 | printf_filtered ("child %s, status %u ", |
2921 | target_pid_to_str (sip -> si_pid), | |
cc221e76 FF |
2922 | sip -> si_status); |
2923 | } | |
2924 | else if ((sip -> si_signo == SIGPOLL)) | |
2925 | { | |
2926 | printf_filtered ("band %u ", sip -> si_band); | |
2927 | } | |
2928 | } | |
2929 | } | |
2930 | else | |
2931 | { | |
2932 | printf_filtered ("\n\n"); | |
2933 | printf_filtered ("\t%-16s %s.\n", signalname (sip -> si_signo), | |
4ace50a5 | 2934 | safe_strsignal (sip -> si_signo)); |
cc221e76 FF |
2935 | if (sip -> si_errno > 0) |
2936 | { | |
2937 | printf_filtered ("\t%-16s %s.\n", | |
4ace50a5 FF |
2938 | errnoname (sip -> si_errno), |
2939 | safe_strerror (sip -> si_errno)); | |
cc221e76 FF |
2940 | } |
2941 | if (sip -> si_code <= 0) | |
2942 | { | |
25286543 | 2943 | printf_filtered ("\t%-16u %s\n", sip -> si_pid, /* XXX need target_pid_to_str() */ |
cc221e76 FF |
2944 | "PID of process sending signal"); |
2945 | printf_filtered ("\t%-16u %s\n", sip -> si_uid, | |
2946 | "UID of process sending signal"); | |
2947 | } | |
2948 | else | |
2949 | { | |
2950 | printf_filtered ("\t%-16s %s.\n", sigcodename (sip), | |
2951 | sigcodedesc (sip)); | |
2952 | if ((sip -> si_signo == SIGILL) || | |
2953 | (sip -> si_signo == SIGFPE)) | |
2954 | { | |
2955 | printf_filtered ("\t%-16#x %s.\n", sip -> si_addr, | |
2956 | "Address of faulting instruction"); | |
2957 | } | |
2958 | else if ((sip -> si_signo == SIGSEGV) || | |
2959 | (sip -> si_signo == SIGBUS)) | |
2960 | { | |
2961 | printf_filtered ("\t%-16#x %s.\n", sip -> si_addr, | |
2962 | "Address of faulting memory reference"); | |
2963 | } | |
2964 | else if ((sip -> si_signo == SIGCHLD)) | |
2965 | { | |
25286543 | 2966 | printf_filtered ("\t%-16u %s.\n", sip -> si_pid, /* XXX need target_pid_to_str() */ |
cc221e76 FF |
2967 | "Child process ID"); |
2968 | printf_filtered ("\t%-16u %s.\n", sip -> si_status, | |
2969 | "Child process exit value or signal"); | |
2970 | } | |
2971 | else if ((sip -> si_signo == SIGPOLL)) | |
2972 | { | |
2973 | printf_filtered ("\t%-16u %s.\n", sip -> si_band, | |
2974 | "Band event for POLL_{IN,OUT,MSG}"); | |
2975 | } | |
2976 | } | |
2977 | } | |
2978 | printf_filtered ("\n"); | |
2979 | } | |
2980 | } | |
2981 | ||
2982 | static void | |
2983 | info_proc_syscalls (pip, summary) | |
2984 | struct procinfo *pip; | |
2985 | int summary; | |
2986 | { | |
2987 | int syscallnum; | |
2988 | ||
2989 | if (!summary) | |
2990 | { | |
2991 | ||
2992 | #if 0 /* FIXME: Needs to use gdb-wide configured info about system calls. */ | |
2993 | if (pip -> prstatus.pr_flags & PR_ASLEEP) | |
2994 | { | |
2995 | int syscallnum = pip -> prstatus.pr_reg[R_D0]; | |
2996 | if (summary) | |
2997 | { | |
2998 | printf_filtered ("%-32s", "Sleeping in system call:"); | |
2999 | printf_filtered ("%s", syscallname (syscallnum)); | |
3000 | } | |
3001 | else | |
3002 | { | |
3003 | printf_filtered ("Sleeping in system call '%s'.\n", | |
3004 | syscallname (syscallnum)); | |
3005 | } | |
3006 | } | |
3007 | #endif | |
3008 | ||
3009 | if (ioctl (pip -> fd, PIOCGENTRY, &pip -> entryset) < 0) | |
3010 | { | |
3011 | print_sys_errmsg (pip -> pathname, errno); | |
3012 | error ("PIOCGENTRY failed"); | |
3013 | } | |
3014 | ||
3015 | if (ioctl (pip -> fd, PIOCGEXIT, &pip -> exitset) < 0) | |
3016 | { | |
3017 | print_sys_errmsg (pip -> pathname, errno); | |
3018 | error ("PIOCGEXIT failed"); | |
3019 | } | |
3020 | ||
3021 | printf_filtered ("System call tracing information:\n\n"); | |
3022 | ||
3023 | printf_filtered ("\t%-12s %-8s %-8s\n", | |
3024 | "System call", | |
3025 | "Entry", | |
3026 | "Exit"); | |
3027 | for (syscallnum = 0; syscallnum < MAX_SYSCALLS; syscallnum++) | |
3028 | { | |
3029 | QUIT; | |
3030 | if (syscall_table[syscallnum] != NULL) | |
3031 | { | |
3032 | printf_filtered ("\t%-12s ", syscall_table[syscallnum]); | |
3033 | printf_filtered ("%-8s ", | |
3034 | prismember (&pip -> entryset, syscallnum) | |
3035 | ? "on" : "off"); | |
3036 | printf_filtered ("%-8s ", | |
3037 | prismember (&pip -> exitset, syscallnum) | |
3038 | ? "on" : "off"); | |
3039 | printf_filtered ("\n"); | |
3040 | } | |
3041 | } | |
3042 | printf_filtered ("\n"); | |
3043 | } | |
3044 | } | |
3045 | ||
3046 | static char * | |
3047 | signalname (signo) | |
3048 | int signo; | |
3049 | { | |
4ace50a5 FF |
3050 | char *name; |
3051 | static char locbuf[32]; | |
3052 | ||
3053 | name = strsigno (signo); | |
3054 | if (name == NULL) | |
3055 | { | |
3056 | sprintf (locbuf, "Signal %d", signo); | |
3057 | } | |
3058 | else | |
3059 | { | |
3060 | sprintf (locbuf, "%s (%d)", name, signo); | |
3061 | } | |
3062 | return (locbuf); | |
3063 | } | |
3064 | ||
3065 | static char * | |
3066 | errnoname (errnum) | |
3067 | int errnum; | |
3068 | { | |
3069 | char *name; | |
cc221e76 FF |
3070 | static char locbuf[32]; |
3071 | ||
4ace50a5 FF |
3072 | name = strerrno (errnum); |
3073 | if (name == NULL) | |
cc221e76 | 3074 | { |
4ace50a5 | 3075 | sprintf (locbuf, "Errno %d", errnum); |
cc221e76 FF |
3076 | } |
3077 | else | |
3078 | { | |
4ace50a5 | 3079 | sprintf (locbuf, "%s (%d)", name, errnum); |
cc221e76 FF |
3080 | } |
3081 | return (locbuf); | |
3082 | } | |
3083 | ||
3084 | static void | |
3085 | info_proc_signals (pip, summary) | |
3086 | struct procinfo *pip; | |
3087 | int summary; | |
3088 | { | |
3089 | int signo; | |
3090 | ||
3091 | if (!summary) | |
3092 | { | |
3093 | if (ioctl (pip -> fd, PIOCGTRACE, &pip -> trace) < 0) | |
3094 | { | |
3095 | print_sys_errmsg (pip -> pathname, errno); | |
3096 | error ("PIOCGTRACE failed"); | |
3097 | } | |
3098 | ||
3099 | printf_filtered ("Disposition of signals:\n\n"); | |
3100 | printf_filtered ("\t%-15s %-8s %-8s %-8s %s\n\n", | |
3101 | "Signal", "Trace", "Hold", "Pending", "Description"); | |
3102 | for (signo = 0; signo < NSIG; signo++) | |
3103 | { | |
3104 | QUIT; | |
3105 | printf_filtered ("\t%-15s ", signalname (signo)); | |
3106 | printf_filtered ("%-8s ", | |
3107 | prismember (&pip -> trace, signo) | |
3108 | ? "on" : "off"); | |
3109 | printf_filtered ("%-8s ", | |
3110 | prismember (&pip -> prstatus.pr_sighold, signo) | |
3111 | ? "on" : "off"); | |
3112 | printf_filtered ("%-8s ", | |
3113 | prismember (&pip -> prstatus.pr_sigpend, signo) | |
3114 | ? "yes" : "no"); | |
4ace50a5 | 3115 | printf_filtered (" %s\n", safe_strsignal (signo)); |
cc221e76 FF |
3116 | } |
3117 | printf_filtered ("\n"); | |
3118 | } | |
3119 | } | |
3120 | ||
3121 | static void | |
3122 | info_proc_faults (pip, summary) | |
3123 | struct procinfo *pip; | |
3124 | int summary; | |
3125 | { | |
3126 | struct trans *transp; | |
3127 | ||
3128 | if (!summary) | |
3129 | { | |
3130 | if (ioctl (pip -> fd, PIOCGFAULT, &pip -> fltset) < 0) | |
3131 | { | |
3132 | print_sys_errmsg (pip -> pathname, errno); | |
3133 | error ("PIOCGFAULT failed"); | |
3134 | } | |
3135 | ||
3136 | printf_filtered ("Current traced hardware fault set:\n\n"); | |
3137 | printf_filtered ("\t%-12s %-8s\n", "Fault", "Trace"); | |
3138 | ||
3139 | for (transp = faults_table; transp -> name != NULL; transp++) | |
3140 | { | |
3141 | QUIT; | |
3142 | printf_filtered ("\t%-12s ", transp -> name); | |
3143 | printf_filtered ("%-8s", prismember (&pip -> fltset, transp -> value) | |
3144 | ? "on" : "off"); | |
3145 | printf_filtered ("\n"); | |
3146 | } | |
3147 | printf_filtered ("\n"); | |
3148 | } | |
3149 | } | |
3150 | ||
3151 | static void | |
3152 | info_proc_mappings (pip, summary) | |
1ab3bf1b | 3153 | struct procinfo *pip; |
cc221e76 | 3154 | int summary; |
a39ad5ce FF |
3155 | { |
3156 | int nmap; | |
3157 | struct prmap *prmaps; | |
3158 | struct prmap *prmap; | |
3159 | ||
cc221e76 | 3160 | if (!summary) |
a39ad5ce | 3161 | { |
cc221e76 | 3162 | printf_filtered ("Mapped address spaces:\n\n"); |
5c1c5e67 | 3163 | printf_filtered ("\t%10s %10s %10s %10s %7s\n", |
cc221e76 FF |
3164 | "Start Addr", |
3165 | " End Addr", | |
3166 | " Size", | |
3167 | " Offset", | |
3168 | "Flags"); | |
3169 | if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0) | |
a39ad5ce | 3170 | { |
cc221e76 FF |
3171 | prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); |
3172 | if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0) | |
a39ad5ce | 3173 | { |
cc221e76 FF |
3174 | for (prmap = prmaps; prmap -> pr_size; ++prmap) |
3175 | { | |
5c1c5e67 | 3176 | printf_filtered ("\t%#10x %#10x %#10x %#10x %7s\n", |
cc221e76 FF |
3177 | prmap -> pr_vaddr, |
3178 | prmap -> pr_vaddr + prmap -> pr_size - 1, | |
3179 | prmap -> pr_size, | |
3180 | prmap -> pr_off, | |
3181 | mappingflags (prmap -> pr_mflags)); | |
3182 | } | |
a39ad5ce FF |
3183 | } |
3184 | } | |
cc221e76 | 3185 | printf_filtered ("\n"); |
a39ad5ce | 3186 | } |
a39ad5ce FF |
3187 | } |
3188 | ||
3189 | /* | |
3190 | ||
3191 | LOCAL FUNCTION | |
3192 | ||
cc221e76 | 3193 | info_proc -- implement the "info proc" command |
a39ad5ce FF |
3194 | |
3195 | SYNOPSIS | |
3196 | ||
cc221e76 | 3197 | void info_proc (char *args, int from_tty) |
a39ad5ce FF |
3198 | |
3199 | DESCRIPTION | |
3200 | ||
3201 | Implement gdb's "info proc" command by using the /proc interface | |
3202 | to print status information about any currently running process. | |
3203 | ||
3204 | Examples of the use of "info proc" are: | |
3205 | ||
cc221e76 FF |
3206 | info proc (prints summary info for current inferior) |
3207 | info proc 123 (prints summary info for process with pid 123) | |
3208 | info proc mappings (prints address mappings) | |
3209 | info proc times (prints process/children times) | |
3210 | info proc id (prints pid, ppid, gid, sid, etc) | |
3fbdd536 | 3211 | FIXME: i proc id not implemented. |
cc221e76 | 3212 | info proc status (prints general process state info) |
3fbdd536 | 3213 | FIXME: i proc status not implemented. |
cc221e76 FF |
3214 | info proc signals (prints info about signal handling) |
3215 | info proc all (prints all info) | |
a39ad5ce FF |
3216 | |
3217 | */ | |
3218 | ||
3219 | static void | |
cc221e76 | 3220 | info_proc (args, from_tty) |
1ab3bf1b JG |
3221 | char *args; |
3222 | int from_tty; | |
a39ad5ce | 3223 | { |
a39ad5ce | 3224 | int pid; |
a39ad5ce FF |
3225 | struct procinfo *pip; |
3226 | struct cleanup *old_chain; | |
cc221e76 FF |
3227 | char **argv; |
3228 | int argsize; | |
3229 | int summary = 1; | |
3230 | int flags = 0; | |
3231 | int syscalls = 0; | |
3232 | int signals = 0; | |
3233 | int faults = 0; | |
3234 | int mappings = 0; | |
3235 | int times = 0; | |
3236 | int id = 0; | |
3237 | int status = 0; | |
3238 | int all = 0; | |
a39ad5ce FF |
3239 | |
3240 | old_chain = make_cleanup (null_cleanup, 0); | |
3241 | ||
de43d7d0 SG |
3242 | /* Default to using the current inferior if no pid specified. Note |
3243 | that inferior_pid may be 0, hence we set okerr. */ | |
a39ad5ce | 3244 | |
de43d7d0 | 3245 | pip = find_procinfo (inferior_pid, 1); |
a39ad5ce | 3246 | |
a39ad5ce | 3247 | if (args != NULL) |
35f5886e | 3248 | { |
cc221e76 | 3249 | if ((argv = buildargv (args)) == NULL) |
a39ad5ce | 3250 | { |
cc221e76 FF |
3251 | nomem (0); |
3252 | } | |
3253 | make_cleanup (freeargv, (char *) argv); | |
3254 | ||
3255 | while (*argv != NULL) | |
3256 | { | |
3257 | argsize = strlen (*argv); | |
3258 | if (argsize >= 1 && strncmp (*argv, "all", argsize) == 0) | |
3259 | { | |
3260 | summary = 0; | |
3261 | all = 1; | |
3262 | } | |
3263 | else if (argsize >= 2 && strncmp (*argv, "faults", argsize) == 0) | |
3264 | { | |
3265 | summary = 0; | |
3266 | faults = 1; | |
3267 | } | |
3268 | else if (argsize >= 2 && strncmp (*argv, "flags", argsize) == 0) | |
3269 | { | |
3270 | summary = 0; | |
3271 | flags = 1; | |
3272 | } | |
3273 | else if (argsize >= 1 && strncmp (*argv, "id", argsize) == 0) | |
3274 | { | |
3275 | summary = 0; | |
3276 | id = 1; | |
3277 | } | |
3278 | else if (argsize >= 1 && strncmp (*argv, "mappings", argsize) == 0) | |
3279 | { | |
3280 | summary = 0; | |
3281 | mappings = 1; | |
3282 | } | |
3283 | else if (argsize >= 2 && strncmp (*argv, "signals", argsize) == 0) | |
3284 | { | |
3285 | summary = 0; | |
3286 | signals = 1; | |
3287 | } | |
3288 | else if (argsize >= 2 && strncmp (*argv, "status", argsize) == 0) | |
3289 | { | |
3290 | summary = 0; | |
3291 | status = 1; | |
3292 | } | |
3293 | else if (argsize >= 2 && strncmp (*argv, "syscalls", argsize) == 0) | |
3294 | { | |
3295 | summary = 0; | |
3296 | syscalls = 1; | |
3297 | } | |
3298 | else if (argsize >= 1 && strncmp (*argv, "times", argsize) == 0) | |
a39ad5ce | 3299 | { |
cc221e76 FF |
3300 | summary = 0; |
3301 | times = 1; | |
a39ad5ce | 3302 | } |
de43d7d0 | 3303 | else if ((pid = atoi (*argv)) > 0) |
a39ad5ce | 3304 | { |
de43d7d0 SG |
3305 | pip = (struct procinfo *) xmalloc (sizeof (struct procinfo)); |
3306 | memset (pip, 0, sizeof (*pip)); | |
3307 | ||
3308 | pip->pid = pid; | |
ec8ceca3 | 3309 | if (!open_proc_file (pid, pip, O_RDONLY)) |
a39ad5ce FF |
3310 | { |
3311 | perror_with_name (pip -> pathname); | |
3312 | /* NOTREACHED */ | |
3313 | } | |
3314 | make_cleanup (close_proc_file, pip); | |
3315 | } | |
cc221e76 FF |
3316 | else if (**argv != '\000') |
3317 | { | |
3318 | error ("Unrecognized or ambiguous keyword `%s'.", *argv); | |
3319 | } | |
3320 | argv++; | |
a39ad5ce | 3321 | } |
35f5886e | 3322 | } |
a39ad5ce FF |
3323 | |
3324 | /* If we don't have a valid open process at this point, then we have no | |
3325 | inferior or didn't specify a specific pid. */ | |
3326 | ||
de43d7d0 | 3327 | if (!pip) |
35f5886e | 3328 | { |
6fe90fc8 JK |
3329 | error ("\ |
3330 | No process. Start debugging a program or specify an explicit process ID."); | |
35f5886e | 3331 | } |
a39ad5ce | 3332 | if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0) |
35f5886e | 3333 | { |
a39ad5ce FF |
3334 | print_sys_errmsg (pip -> pathname, errno); |
3335 | error ("PIOCSTATUS failed"); | |
35f5886e | 3336 | } |
a39ad5ce | 3337 | |
cc221e76 FF |
3338 | /* Print verbose information of the requested type(s), or just a summary |
3339 | of the information for all types. */ | |
3340 | ||
3341 | printf_filtered ("\nInformation for %s:\n\n", pip -> pathname); | |
3342 | if (summary || all || flags) | |
3343 | { | |
3344 | info_proc_flags (pip, summary); | |
3345 | } | |
3346 | if (summary || all) | |
3347 | { | |
3348 | info_proc_stop (pip, summary); | |
3349 | } | |
3350 | if (summary || all || signals || faults) | |
3351 | { | |
3352 | info_proc_siginfo (pip, summary); | |
3353 | } | |
3354 | if (summary || all || syscalls) | |
3355 | { | |
3356 | info_proc_syscalls (pip, summary); | |
3357 | } | |
3358 | if (summary || all || mappings) | |
3359 | { | |
3360 | info_proc_mappings (pip, summary); | |
3361 | } | |
3362 | if (summary || all || signals) | |
3363 | { | |
3364 | info_proc_signals (pip, summary); | |
3365 | } | |
3366 | if (summary || all || faults) | |
3367 | { | |
3368 | info_proc_faults (pip, summary); | |
3369 | } | |
3370 | printf_filtered ("\n"); | |
a39ad5ce FF |
3371 | |
3372 | /* All done, deal with closing any temporary process info structure, | |
3373 | freeing temporary memory , etc. */ | |
3374 | ||
3375 | do_cleanups (old_chain); | |
3376 | } | |
3377 | ||
de43d7d0 SG |
3378 | /* |
3379 | ||
3380 | LOCAL FUNCTION | |
3381 | ||
3382 | procfs_set_sproc_trap -- arrange for exec'd child stop on sproc | |
3383 | ||
3384 | SYNOPSIS | |
3385 | ||
3386 | void procfs_set_sproc_trap (void) | |
3387 | ||
3388 | DESCRIPTION | |
3389 | ||
3390 | This function sets up a trap on sproc system call exits so that we can | |
3391 | detect the arrival of a new thread. We are called with the child | |
3392 | stopped prior to it's first instruction. | |
3393 | ||
3394 | Also note that we turn on the inherit-on-fork flag in the child process | |
3395 | so that any grand-children start with all tracing flags set. | |
3396 | */ | |
3397 | ||
952a820e SG |
3398 | #ifdef SYS_sproc |
3399 | ||
de43d7d0 SG |
3400 | static void |
3401 | procfs_set_sproc_trap (pi) | |
3402 | struct procinfo *pi; | |
3403 | { | |
3404 | sysset_t exitset; | |
3405 | ||
3406 | if (ioctl (pi->fd, PIOCGEXIT, &exitset) < 0) | |
3407 | { | |
3408 | print_sys_errmsg (pi->pathname, errno); | |
3409 | error ("PIOCGEXIT failed"); | |
3410 | } | |
3411 | ||
de43d7d0 | 3412 | praddset (&exitset, SYS_sproc); |
de43d7d0 SG |
3413 | |
3414 | if (ioctl (pi->fd, PIOCSEXIT, &exitset) < 0) | |
3415 | { | |
3416 | print_sys_errmsg (pi->pathname, errno); | |
3417 | error ("PIOCSEXIT failed"); | |
3418 | } | |
3419 | ||
3420 | /* Turn on inherit-on-fork flag so that all grand-children of gdb start with | |
3421 | tracing flags set. */ | |
3422 | ||
3423 | #ifdef PIOCSET /* New method */ | |
3424 | { | |
3425 | long pr_flags; | |
3426 | pr_flags = PR_FORK; | |
3427 | ioctl (pi->fd, PIOCSET, &pr_flags); | |
3428 | } | |
3429 | #else | |
3430 | #ifdef PIOCSFORK /* Original method */ | |
3431 | ioctl (pi->fd, PIOCSFORK, NULL); | |
3432 | #endif | |
3433 | #endif | |
3434 | } | |
952a820e | 3435 | #endif /* SYS_sproc */ |
de43d7d0 | 3436 | |
3fbdd536 JG |
3437 | /* Fork an inferior process, and start debugging it with /proc. */ |
3438 | ||
3439 | static void | |
3440 | procfs_create_inferior (exec_file, allargs, env) | |
3441 | char *exec_file; | |
3442 | char *allargs; | |
3443 | char **env; | |
3444 | { | |
3445 | fork_inferior (exec_file, allargs, env, | |
3446 | proc_set_exec_trap, procfs_init_inferior); | |
3447 | /* We are at the first instruction we care about. */ | |
3448 | /* Pedal to the metal... */ | |
de43d7d0 SG |
3449 | |
3450 | /* Setup traps on exit from sproc() */ | |
3451 | ||
952a820e SG |
3452 | #ifdef SYS_sproc |
3453 | procfs_set_sproc_trap (current_procinfo); | |
3454 | #endif | |
de43d7d0 | 3455 | |
3fbdd536 JG |
3456 | proceed ((CORE_ADDR) -1, 0, 0); |
3457 | } | |
3458 | ||
3459 | /* Clean up after the inferior dies. */ | |
3460 | ||
3461 | static void | |
3462 | procfs_mourn_inferior () | |
3463 | { | |
3464 | unpush_target (&procfs_ops); | |
3465 | generic_mourn_inferior (); | |
3466 | } | |
3467 | ||
3468 | /* Mark our target-struct as eligible for stray "run" and "attach" commands. */ | |
3469 | static int | |
3470 | procfs_can_run () | |
3471 | { | |
3472 | return(1); | |
3473 | } | |
3474 | \f | |
3475 | struct target_ops procfs_ops = { | |
3476 | "procfs", /* to_shortname */ | |
3477 | "Unix /proc child process", /* to_longname */ | |
3478 | "Unix /proc child process (started by the \"run\" command).", /* to_doc */ | |
3479 | procfs_open, /* to_open */ | |
3480 | 0, /* to_close */ | |
3481 | procfs_attach, /* to_attach */ | |
3482 | procfs_detach, /* to_detach */ | |
3483 | procfs_resume, /* to_resume */ | |
3484 | procfs_wait, /* to_wait */ | |
3485 | procfs_fetch_registers, /* to_fetch_registers */ | |
3486 | procfs_store_registers, /* to_store_registers */ | |
3487 | procfs_prepare_to_store, /* to_prepare_to_store */ | |
3488 | procfs_xfer_memory, /* to_xfer_memory */ | |
3489 | procfs_files_info, /* to_files_info */ | |
3490 | memory_insert_breakpoint, /* to_insert_breakpoint */ | |
3491 | memory_remove_breakpoint, /* to_remove_breakpoint */ | |
3492 | terminal_init_inferior, /* to_terminal_init */ | |
3493 | terminal_inferior, /* to_terminal_inferior */ | |
3494 | terminal_ours_for_output, /* to_terminal_ours_for_output */ | |
3495 | terminal_ours, /* to_terminal_ours */ | |
3496 | child_terminal_info, /* to_terminal_info */ | |
3497 | procfs_kill_inferior, /* to_kill */ | |
3498 | 0, /* to_load */ | |
3499 | 0, /* to_lookup_symbol */ | |
3500 | procfs_create_inferior, /* to_create_inferior */ | |
3501 | procfs_mourn_inferior, /* to_mourn_inferior */ | |
3502 | procfs_can_run, /* to_can_run */ | |
3950a34e | 3503 | procfs_notice_signals, /* to_notice_signals */ |
3fbdd536 JG |
3504 | process_stratum, /* to_stratum */ |
3505 | 0, /* to_next */ | |
3506 | 1, /* to_has_all_memory */ | |
3507 | 1, /* to_has_memory */ | |
3508 | 1, /* to_has_stack */ | |
3509 | 1, /* to_has_registers */ | |
3510 | 1, /* to_has_execution */ | |
3511 | 0, /* sections */ | |
3512 | 0, /* sections_end */ | |
3513 | OPS_MAGIC /* to_magic */ | |
3514 | }; | |
3515 | ||
a39ad5ce FF |
3516 | /* |
3517 | ||
3518 | GLOBAL FUNCTION | |
3519 | ||
3fbdd536 | 3520 | _initialize_procfs -- initialize the process file system stuff |
a39ad5ce FF |
3521 | |
3522 | SYNOPSIS | |
3523 | ||
3fbdd536 | 3524 | void _initialize_procfs (void) |
a39ad5ce FF |
3525 | |
3526 | DESCRIPTION | |
3527 | ||
3528 | Do required initializations during gdb startup for using the | |
3529 | /proc file system interface. | |
3530 | ||
3531 | */ | |
3532 | ||
3fbdd536 JG |
3533 | void |
3534 | _initialize_procfs () | |
3535 | { | |
3536 | add_target (&procfs_ops); | |
3537 | ||
3538 | add_info ("proc", info_proc, | |
cc221e76 FF |
3539 | "Show process status information using /proc entry.\n\ |
3540 | Specify process id or use current inferior by default.\n\ | |
3541 | Specify keywords for detailed information; default is summary.\n\ | |
3542 | Keywords are: `all', `faults', `flags', `id', `mappings', `signals',\n\ | |
3543 | `status', `syscalls', and `times'.\n\ | |
3fbdd536 | 3544 | Unambiguous abbreviations may be used."); |
a39ad5ce | 3545 | |
cc221e76 | 3546 | init_syscall_table (); |
35f5886e | 3547 | } |