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