* Check in Fred Fish's changes in these modules. Fred
[deliverable/binutils-gdb.git] / gdb / procfs.c
CommitLineData
35f5886e
FF
1/* Machine independent support for SVR4 /proc (process file system) for GDB.
2 Copyright (C) 1991 Free Software Foundation, Inc.
3 Written by Fred Fish at Cygnus Support.
4
5This file is part of GDB.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
21
22/* N O T E S
23
24For information on the details of using /proc consult section proc(4)
25in the UNIX System V Release 4 System Administrator's Reference Manual.
26
27The general register and floating point register sets are manipulated by
28separate ioctl's. This file makes the assumption that if FP0_REGNUM is
29defined, then support for the floating point register set is desired,
30regardless of whether or not the actual target has floating point hardware.
31
32 */
33
34
1ab3bf1b 35#include <stdio.h>
35f5886e 36
5129100c 37#include "defs.h"
35f5886e
FF
38
39#ifdef USE_PROC_FS /* Entire file goes away if not using /proc */
40
41#include <stdio.h>
42#include <sys/procfs.h>
43#include <fcntl.h>
44#include <errno.h>
45
35f5886e
FF
46#include "inferior.h"
47#include "target.h"
48
49#ifndef PROC_NAME_FMT
50#define PROC_NAME_FMT "/proc/%d"
51#endif
52
35f5886e
FF
53#if 1 /* FIXME: Gross and ugly hack to resolve coredep.c global */
54CORE_ADDR kernel_u_addr;
55#endif
56
57/* All access to the inferior, either one started by gdb or one that has
58 been attached to, is controlled by an instance of a procinfo structure,
59 defined below. Since gdb currently only handles one inferior at a time,
a39ad5ce
FF
60 the procinfo structure for the inferior is statically allocated and
61 only one exists at any given time. There is a separate procinfo
62 structure for use by the "info proc" command, so that we can print
63 useful information about any random process without interfering with
64 the inferior's procinfo information. */
35f5886e
FF
65
66struct procinfo {
67 int valid; /* Nonzero if pid, fd, & pathname are valid */
68 int pid; /* Process ID of inferior */
69 int fd; /* File descriptor for /proc entry */
70 char *pathname; /* Pathname to /proc entry */
71 int was_stopped; /* Nonzero if was stopped prior to attach */
72 prrun_t prrun; /* Control state when it is run */
73 prstatus_t prstatus; /* Current process status info */
74 gregset_t gregset; /* General register set */
75 fpregset_t fpregset; /* Floating point register set */
76 fltset_t fltset; /* Current traced hardware fault set */
77 sigset_t trace; /* Current traced signal set */
78 sysset_t exitset; /* Current traced system call exit set */
79 sysset_t entryset; /* Current traced system call entry set */
a39ad5ce
FF
80};
81
82static struct procinfo pi; /* Inferior's process information */
35f5886e 83
1ab3bf1b
JG
84/* Prototypes for local functions */
85
86static int
87proc_address_to_fd PARAMS ((CORE_ADDR, int));
88
89static int
90open_proc_file PARAMS ((int, struct procinfo *));
91
92static void
93close_proc_file PARAMS ((struct procinfo *));
94
95static void
96unconditionally_kill_inferior PARAMS ((void));
97
98static void
99proc_init_failed PARAMS ((char *));
100
101static void
102proc_info PARAMS ((char *, int));
103
104static void
105proc_info_address_map PARAMS ((struct procinfo *, int));
106
107static char *
108mappingflags PARAMS ((long));
109
110/* External function prototypes that can't be easily included in any
111 header file because the args are typedefs in system include files. */
112
113extern void
114supply_gregset PARAMS ((gregset_t *));
115
116extern void
117fill_gregset PARAMS ((gregset_t *, int));
118
119extern void
120supply_fpregset PARAMS ((fpregset_t *));
121
122extern void
123fill_fpregset PARAMS ((fpregset_t *, int));
35f5886e 124
35f5886e
FF
125
126/*
127
128GLOBAL FUNCTION
129
130 ptrace -- override library version to force errors for /proc version
131
132SYNOPSIS
133
134 int ptrace (int request, int pid, int arg3, int arg4)
135
136DESCRIPTION
137
138 When gdb is configured to use /proc, it should not be calling
139 or otherwise attempting to use ptrace. In order to catch errors
140 where use of /proc is configured, but some routine is still calling
141 ptrace, we provide a local version of a function with that name
142 that does nothing but issue an error message.
143*/
144
145int
1ab3bf1b
JG
146ptrace (request, pid, arg3, arg4)
147 int request;
148 int pid;
149 int arg3;
150 int arg4;
35f5886e
FF
151{
152 error ("internal error - there is a call to ptrace() somewhere");
153 /*NOTREACHED*/
154}
155
156/*
157
158GLOBAL FUNCTION
159
160 kill_inferior_fast -- kill inferior while gdb is exiting
161
162SYNOPSIS
163
164 void kill_inferior_fast (void)
165
166DESCRIPTION
167
168 This is used when GDB is exiting. It gives less chance of error.
169
170NOTES
171
172 Don't attempt to kill attached inferiors since we may be called
173 when gdb is in the process of aborting, and killing the attached
174 inferior may be very anti-social. This is particularly true if we
175 were attached just so we could use the /proc facilities to get
176 detailed information about it's status.
177
178*/
179
180void
1ab3bf1b 181kill_inferior_fast ()
35f5886e
FF
182{
183 if (inferior_pid != 0 && !attach_flag)
184 {
185 unconditionally_kill_inferior ();
186 }
187}
188
189/*
190
191GLOBAL FUNCTION
192
193 kill_inferior - kill any currently inferior
194
195SYNOPSIS
196
197 void kill_inferior (void)
198
199DESCRIPTION
200
201 Kill any current inferior.
202
203NOTES
204
205 Kills even attached inferiors. Presumably the user has already
206 been prompted that the inferior is an attached one rather than
207 one started by gdb. (FIXME?)
208
209*/
210
211void
1ab3bf1b 212kill_inferior ()
35f5886e
FF
213{
214 if (inferior_pid != 0)
215 {
216 unconditionally_kill_inferior ();
217 target_mourn_inferior ();
218 }
219}
220
221/*
222
223LOCAL FUNCTION
224
225 unconditionally_kill_inferior - terminate the inferior
226
227SYNOPSIS
228
229 static void unconditionally_kill_inferior (void)
230
231DESCRIPTION
232
233 Kill the current inferior. Should not be called until it
234 is at least tested that there is an inferior.
235
236NOTE
237
238 A possibly useful enhancement would be to first try sending
239 the inferior a terminate signal, politely asking it to commit
240 suicide, before we murder it.
241
242*/
243
244static void
1ab3bf1b 245unconditionally_kill_inferior ()
35f5886e
FF
246{
247 int signo;
248
249 signo = SIGKILL;
250 (void) ioctl (pi.fd, PIOCKILL, &signo);
a39ad5ce 251 close_proc_file (&pi);
35f5886e
FF
252 wait ((int *) 0);
253}
254
255/*
256
257GLOBAL FUNCTION
258
259 child_xfer_memory -- copy data to or from inferior memory space
260
261SYNOPSIS
262
263 int child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len,
264 int dowrite, struct target_ops target)
265
266DESCRIPTION
267
268 Copy LEN bytes to/from inferior's memory starting at MEMADDR
269 from/to debugger memory starting at MYADDR. Copy from inferior
270 if DOWRITE is zero or to inferior if DOWRITE is nonzero.
271
272 Returns the length copied, which is either the LEN argument or
273 zero. This xfer function does not do partial moves, since child_ops
274 doesn't allow memory operations to cross below us in the target stack
275 anyway.
276
277NOTES
278
279 The /proc interface makes this an almost trivial task.
280 */
281
282
283int
1ab3bf1b
JG
284child_xfer_memory (memaddr, myaddr, len, dowrite, target)
285 CORE_ADDR memaddr;
286 char *myaddr;
287 int len;
288 int dowrite;
289 struct target_ops *target; /* ignored */
35f5886e
FF
290{
291 int nbytes = 0;
292
293 if (lseek (pi.fd, (off_t) memaddr, 0) == (off_t) memaddr)
294 {
295 if (dowrite)
296 {
297 nbytes = write (pi.fd, myaddr, len);
298 }
299 else
300 {
301 nbytes = read (pi.fd, myaddr, len);
302 }
303 if (nbytes < 0)
304 {
305 nbytes = 0;
306 }
307 }
308 return (nbytes);
309}
310
311/*
312
313GLOBAL FUNCTION
314
315 store_inferior_registers -- copy register values back to inferior
316
317SYNOPSIS
318
319 void store_inferior_registers (int regno)
320
321DESCRIPTION
322
323 Store our current register values back into the inferior. If
324 REGNO is -1 then store all the register, otherwise store just
325 the value specified by REGNO.
326
327NOTES
328
329 If we are storing only a single register, we first have to get all
330 the current values from the process, overwrite the desired register
331 in the gregset with the one we want from gdb's registers, and then
332 send the whole set back to the process. For writing all the
333 registers, all we have to do is generate the gregset and send it to
334 the process.
335
336 Also note that the process has to be stopped on an event of interest
337 for this to work, which basically means that it has to have been
338 run under the control of one of the other /proc ioctl calls and not
339 ptrace. Since we don't use ptrace anyway, we don't worry about this
340 fine point, but it is worth noting for future reference.
341
342 Gdb is confused about what this function is supposed to return.
343 Some versions return a value, others return nothing. Some are
344 declared to return a value and actually return nothing. Gdb ignores
345 anything returned. (FIXME)
346
347 */
348
349void
1ab3bf1b
JG
350store_inferior_registers (regno)
351 int regno;
35f5886e
FF
352{
353 if (regno != -1)
354 {
355 (void) ioctl (pi.fd, PIOCGREG, &pi.gregset);
356 }
357 fill_gregset (&pi.gregset, regno);
358 (void) ioctl (pi.fd, PIOCSREG, &pi.gregset);
359
360#if defined (FP0_REGNUM)
361
362 /* Now repeat everything using the floating point register set, if the
363 target has floating point hardware. Since we ignore the returned value,
364 we'll never know whether it worked or not anyway. */
365
366 if (regno != -1)
367 {
368 (void) ioctl (pi.fd, PIOCGFPREG, &pi.fpregset);
369 }
370 fill_fpregset (&pi.fpregset, regno);
371 (void) ioctl (pi.fd, PIOCSFPREG, &pi.fpregset);
372
373#endif /* FP0_REGNUM */
374
375}
376
377/*
378
379GLOBAL FUNCTION
380
381 inferior_proc_init - initialize access to a /proc entry
382
383SYNOPSIS
384
385 void inferior_proc_init (int pid)
386
387DESCRIPTION
388
389 When gdb starts an inferior, this function is called in the parent
390 process immediately after the fork. It waits for the child to stop
391 on the return from the exec system call (the child itself takes care
392 of ensuring that this is set up), then sets up the set of signals
393 and faults that are to be traced.
394
395NOTES
396
397 If proc_init_failed ever gets called, control returns to the command
398 processing loop via the standard error handling code.
399 */
400
401void
1ab3bf1b
JG
402inferior_proc_init (pid)
403 int pid;
35f5886e 404{
a39ad5ce 405 if (!open_proc_file (pid, &pi))
35f5886e
FF
406 {
407 proc_init_failed ("can't open process file");
408 }
409 else
410 {
411 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
412 prfillset (&pi.prrun.pr_trace);
413 prfillset (&pi.prrun.pr_fault);
414 prdelset (&pi.prrun.pr_fault, FLTPAGE);
415 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
416 {
417 proc_init_failed ("PIOCWSTOP failed");
418 }
419 else if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace) < 0)
420 {
421 proc_init_failed ("PIOCSTRACE failed");
422 }
423 else if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault) < 0)
424 {
425 proc_init_failed ("PIOCSFAULT failed");
426 }
427 }
428}
429
430/*
431
432GLOBAL FUNCTION
433
434 proc_set_exec_trap -- arrange for exec'd child to halt at startup
435
436SYNOPSIS
437
438 void proc_set_exec_trap (void)
439
440DESCRIPTION
441
442 This function is called in the child process when starting up
443 an inferior, prior to doing the exec of the actual inferior.
444 It sets the child process's exitset to make exit from the exec
445 system call an event of interest to stop on, and then simply
446 returns. The child does the exec, the system call returns, and
447 the child stops at the first instruction, ready for the gdb
448 parent process to take control of it.
449
450NOTE
451
452 We need to use all local variables since the child may be sharing
453 it's data space with the parent, if vfork was used rather than
454 fork.
455 */
456
457void
1ab3bf1b 458proc_set_exec_trap ()
35f5886e
FF
459{
460 sysset_t exitset;
461 auto char procname[32];
462 int fd;
463
464 (void) sprintf (procname, PROC_NAME_FMT, getpid ());
465 if ((fd = open (procname, O_RDWR)) < 0)
466 {
467 perror (procname);
468 fflush (stderr);
469 _exit (127);
470 }
471 premptyset (&exitset);
472 praddset (&exitset, SYS_exec);
473 praddset (&exitset, SYS_execve);
474 if (ioctl (fd, PIOCSEXIT, &exitset) < 0)
475 {
476 perror (procname);
477 fflush (stderr);
478 _exit (127);
479 }
480}
481
f8b76e70
FF
482/*
483
a39ad5ce
FF
484GLOBAL FUNCTION
485
486 proc_iterate_over_mappings -- call function for every mapped space
487
488SYNOPSIS
489
490 int proc_iterate_over_mappings (int (*func)())
491
492DESCRIPTION
493
494 Given a pointer to a function, call that function for every
495 mapped address space, passing it an open file descriptor for
496 the file corresponding to that mapped address space (if any)
497 and the base address of the mapped space. Quit when we hit
498 the end of the mappings or the function returns nonzero.
499 */
500
501int
1ab3bf1b
JG
502proc_iterate_over_mappings (func)
503 int (*func) PARAMS ((int, CORE_ADDR));
a39ad5ce
FF
504{
505 int nmap;
506 int fd;
507 int funcstat = 0;
508 struct prmap *prmaps;
509 struct prmap *prmap;
510 CORE_ADDR baseaddr = 0;
511
512 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
513 {
1ab3bf1b 514 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
a39ad5ce
FF
515 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
516 {
517 for (prmap = prmaps; prmap -> pr_size && funcstat == 0; ++prmap)
518 {
1ab3bf1b
JG
519 fd = proc_address_to_fd ((CORE_ADDR) prmap -> pr_vaddr, 0);
520 funcstat = (*func) (fd, (CORE_ADDR) prmap -> pr_vaddr);
a39ad5ce
FF
521 close (fd);
522 }
523 }
524 }
525 return (funcstat);
526}
527
528/*
529
f8b76e70
FF
530GLOBAL FUNCTION
531
532 proc_base_address -- find base address for segment containing address
533
534SYNOPSIS
535
536 CORE_ADDR proc_base_address (CORE_ADDR addr)
537
538DESCRIPTION
539
540 Given an address of a location in the inferior, find and return
541 the base address of the mapped segment containing that address.
542
543 This is used for example, by the shared library support code,
544 where we have the pc value for some location in the shared library
545 where we are stopped, and need to know the base address of the
546 segment containing that address.
547*/
548
549
1ab3bf1b
JG
550#if 0 /* Currently unused */
551
f8b76e70 552CORE_ADDR
1ab3bf1b
JG
553proc_base_address (addr)
554CORE_ADDR addr;
f8b76e70
FF
555{
556 int nmap;
557 struct prmap *prmaps;
558 struct prmap *prmap;
559 CORE_ADDR baseaddr = 0;
560
a39ad5ce 561 if (pi.valid && (ioctl (pi.fd, PIOCNMAP, &nmap) == 0))
f8b76e70 562 {
1ab3bf1b 563 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
f8b76e70
FF
564 if (ioctl (pi.fd, PIOCMAP, prmaps) == 0)
565 {
566 for (prmap = prmaps; prmap -> pr_size; ++prmap)
567 {
568 if ((prmap -> pr_vaddr <= (caddr_t) addr) &&
569 (prmap -> pr_vaddr + prmap -> pr_size > (caddr_t) addr))
570 {
571 baseaddr = (CORE_ADDR) prmap -> pr_vaddr;
572 break;
573 }
574 }
575 }
576 }
577 return (baseaddr);
578}
579
1ab3bf1b
JG
580#endif /* 0 */
581
f8b76e70
FF
582/*
583
584GLOBAL_FUNCTION
585
586 proc_address_to_fd -- return open fd for file mapped to address
587
588SYNOPSIS
589
a39ad5ce 590 int proc_address_to_fd (CORE_ADDR addr, complain)
f8b76e70
FF
591
592DESCRIPTION
593
594 Given an address in the current inferior's address space, use the
595 /proc interface to find an open file descriptor for the file that
596 this address was mapped in from. Return -1 if there is no current
597 inferior. Print a warning message if there is an inferior but
598 the address corresponds to no file (IE a bogus address).
599
600*/
601
1ab3bf1b
JG
602static int
603proc_address_to_fd (addr, complain)
604 CORE_ADDR addr;
605 int complain;
f8b76e70
FF
606{
607 int fd = -1;
608
609 if (pi.valid)
610 {
611 if ((fd = ioctl (pi.fd, PIOCOPENM, (caddr_t *) &addr)) < 0)
612 {
a39ad5ce
FF
613 if (complain)
614 {
615 print_sys_errmsg (pi.pathname, errno);
616 warning ("can't find mapped file for address 0x%x", addr);
617 }
f8b76e70
FF
618 }
619 }
620 return (fd);
621}
622
35f5886e
FF
623
624#ifdef ATTACH_DETACH
625
626/*
627
628GLOBAL FUNCTION
629
630 attach -- attach to an already existing process
631
632SYNOPSIS
633
634 int attach (int pid)
635
636DESCRIPTION
637
638 Attach to an already existing process with the specified process
639 id. If the process is not already stopped, query whether to
640 stop it or not.
641
642NOTES
643
644 The option of stopping at attach time is specific to the /proc
645 versions of gdb. Versions using ptrace force the attachee
646 to stop.
647
648*/
649
650int
1ab3bf1b
JG
651attach (pid)
652 int pid;
35f5886e 653{
a39ad5ce 654 if (!open_proc_file (pid, &pi))
35f5886e
FF
655 {
656 perror_with_name (pi.pathname);
657 /* NOTREACHED */
658 }
659
660 /* Get current status of process and if it is not already stopped,
661 then stop it. Remember whether or not it was stopped when we first
662 examined it. */
663
664 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
665 {
666 print_sys_errmsg (pi.pathname, errno);
a39ad5ce 667 close_proc_file (&pi);
35f5886e
FF
668 error ("PIOCSTATUS failed");
669 }
670 if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
671 {
672 pi.was_stopped = 1;
673 }
674 else
675 {
676 pi.was_stopped = 0;
677 if (query ("Process is currently running, stop it? "))
678 {
679 if (ioctl (pi.fd, PIOCSTOP, &pi.prstatus) < 0)
680 {
681 print_sys_errmsg (pi.pathname, errno);
a39ad5ce 682 close_proc_file (&pi);
35f5886e
FF
683 error ("PIOCSTOP failed");
684 }
685 }
686 }
687
688 /* Remember some things about the inferior that we will, or might, change
689 so that we can restore them when we detach. */
690
691 (void) ioctl (pi.fd, PIOCGTRACE, &pi.trace);
692 (void) ioctl (pi.fd, PIOCGFAULT, &pi.fltset);
693 (void) ioctl (pi.fd, PIOCGENTRY, &pi.entryset);
694 (void) ioctl (pi.fd, PIOCGEXIT, &pi.exitset);
695
696 /* Set up trace and fault sets, as gdb expects them. */
697
698 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
699 prfillset (&pi.prrun.pr_trace);
700 prfillset (&pi.prrun.pr_fault);
701 prdelset (&pi.prrun.pr_fault, FLTPAGE);
702 if (ioctl (pi.fd, PIOCSFAULT, &pi.prrun.pr_fault))
703 {
f66f459f 704 print_sys_errmsg ("PIOCSFAULT failed", errno);
35f5886e
FF
705 }
706 if (ioctl (pi.fd, PIOCSTRACE, &pi.prrun.pr_trace))
707 {
f66f459f 708 print_sys_errmsg ("PIOCSTRACE failed", errno);
35f5886e
FF
709 }
710 attach_flag = 1;
711 return (pid);
712}
713
714/*
715
716GLOBAL FUNCTION
717
718 detach -- detach from an attached-to process
719
720SYNOPSIS
721
722 void detach (int signal)
723
724DESCRIPTION
725
726 Detach from the current attachee.
727
728 If signal is non-zero, the attachee is started running again and sent
729 the specified signal.
730
731 If signal is zero and the attachee was not already stopped when we
732 attached to it, then we make it runnable again when we detach.
733
734 Otherwise, we query whether or not to make the attachee runnable
735 again, since we may simply want to leave it in the state it was in
736 when we attached.
737
738 We report any problems, but do not consider them errors, since we
739 MUST detach even if some things don't seem to go right. This may not
740 be the ideal situation. (FIXME).
741 */
742
743void
1ab3bf1b
JG
744detach (signal)
745 int signal;
35f5886e
FF
746{
747 if (signal)
748 {
749 struct siginfo siginfo;
750 siginfo.si_signo = signal;
751 siginfo.si_code = 0;
752 siginfo.si_errno = 0;
753 if (ioctl (pi.fd, PIOCSSIG, &siginfo) < 0)
754 {
755 print_sys_errmsg (pi.pathname, errno);
756 printf ("PIOCSSIG failed.\n");
757 }
758 }
759 if (ioctl (pi.fd, PIOCSEXIT, &pi.exitset) < 0)
760 {
761 print_sys_errmsg (pi.pathname, errno);
762 printf ("PIOCSEXIT failed.\n");
763 }
764 if (ioctl (pi.fd, PIOCSENTRY, &pi.entryset) < 0)
765 {
766 print_sys_errmsg (pi.pathname, errno);
767 printf ("PIOCSENTRY failed.\n");
768 }
769 if (ioctl (pi.fd, PIOCSTRACE, &pi.trace) < 0)
770 {
771 print_sys_errmsg (pi.pathname, errno);
772 printf ("PIOCSTRACE failed.\n");
773 }
774 if (ioctl (pi.fd, PIOCSFAULT, &pi.fltset) < 0)
775 {
776 print_sys_errmsg (pi.pathname, errno);
777 printf ("PIOCSFAULT failed.\n");
778 }
779 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
780 {
781 print_sys_errmsg (pi.pathname, errno);
782 printf ("PIOCSTATUS failed.\n");
783 }
784 else
785 {
786 if (signal || (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
787 {
788 if (signal || !pi.was_stopped ||
789 query ("Was stopped when attached, make it runnable again? "))
790 {
791 (void) memset (&pi.prrun, 0, sizeof (pi.prrun));
792 pi.prrun.pr_flags = PRCFAULT;
793 if (ioctl (pi.fd, PIOCRUN, &pi.prrun))
794 {
795 print_sys_errmsg (pi.pathname, errno);
796 printf ("PIOCRUN failed.\n");
797 }
798 }
799 }
800 }
a39ad5ce 801 close_proc_file (&pi);
35f5886e
FF
802 attach_flag = 0;
803}
804
fb182850
FF
805#endif /* ATTACH_DETACH */
806
35f5886e
FF
807/*
808
809GLOBAL FUNCTION
810
811 proc_wait -- emulate wait() as much as possible
812
813SYNOPSIS
814
815 int proc_wait (int *statloc)
816
817DESCRIPTION
818
819 Try to emulate wait() as much as possible. Not sure why we can't
820 just use wait(), but it seems to have problems when applied to a
821 process being controlled with the /proc interface.
822
823NOTES
824
825 We have a race problem here with no obvious solution. We need to let
826 the inferior run until it stops on an event of interest, which means
827 that we need to use the PIOCWSTOP ioctl. However, we cannot use this
828 ioctl if the process is already stopped on something that is not an
829 event of interest, or the call will hang indefinitely. Thus we first
830 use PIOCSTATUS to see if the process is not stopped. If not, then we
831 use PIOCWSTOP. But during the window between the two, if the process
832 stops for any reason that is not an event of interest (such as a job
833 control signal) then gdb will hang. One possible workaround is to set
834 an alarm to wake up every minute of so and check to see if the process
835 is still running, and if so, then reissue the PIOCWSTOP. But this is
836 a real kludge, so has not been implemented. FIXME: investigate
837 alternatives.
838
839 FIXME: Investigate why wait() seems to have problems with programs
840 being control by /proc routines.
841
842 */
843
844int
1ab3bf1b
JG
845proc_wait (statloc)
846 int *statloc;
35f5886e
FF
847{
848 short what;
849 short why;
850 int statval = 0;
851 int checkerr = 0;
852 int rtnval = -1;
853
854 if (ioctl (pi.fd, PIOCSTATUS, &pi.prstatus) < 0)
855 {
856 checkerr++;
857 }
858 else if (!(pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP)))
859 {
860 if (ioctl (pi.fd, PIOCWSTOP, &pi.prstatus) < 0)
861 {
862 checkerr++;
863 }
864 }
865 if (checkerr)
866 {
867 if (errno == ENOENT)
868 {
869 rtnval = wait (&statval);
870 if (rtnval != inferior_pid)
871 {
872 error ("PIOCWSTOP, wait failed, returned %d", rtnval);
873 /* NOTREACHED */
874 }
875 }
876 else
877 {
878 print_sys_errmsg (pi.pathname, errno);
879 error ("PIOCSTATUS or PIOCWSTOP failed.");
880 /* NOTREACHED */
881 }
882 }
883 else if (pi.prstatus.pr_flags & (PR_STOPPED | PR_ISTOP))
884 {
885 rtnval = pi.prstatus.pr_pid;
886 why = pi.prstatus.pr_why;
887 what = pi.prstatus.pr_what;
888 if (why == PR_SIGNALLED)
889 {
890 statval = (what << 8) | 0177;
891 }
892 else if ((why == PR_SYSEXIT) &&
893 (what == SYS_exec || what == SYS_execve))
894 {
895 statval = (SIGTRAP << 8) | 0177;
896 }
897 else if (why == PR_REQUESTED)
898 {
899 statval = (SIGSTOP << 8) | 0177;
900 }
901 else if (why == PR_JOBCONTROL)
902 {
903 statval = (what << 8) | 0177;
904 }
905 else if (why == PR_FAULTED)
906 {
907 switch (what)
908 {
909 case FLTPRIV:
910 case FLTILL:
911 statval = (SIGILL << 8) | 0177;
912 break;
913 case FLTBPT:
914 case FLTTRACE:
915 statval = (SIGTRAP << 8) | 0177;
916 break;
917 case FLTSTACK:
918 case FLTACCESS:
919 case FLTBOUNDS:
920 statval = (SIGSEGV << 8) | 0177;
921 break;
922 case FLTIOVF:
923 case FLTIZDIV:
924 case FLTFPE:
925 statval = (SIGFPE << 8) | 0177;
926 break;
927 case FLTPAGE: /* Recoverable page fault */
928 default:
929 rtnval = -1;
930 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
931 /* NOTREACHED */
932 }
933 }
934 else
935 {
936 rtnval = -1;
937 error ("PIOCWSTOP, unknown why %d, what %d", why, what);
938 /* NOTREACHED */
939 }
940 }
941 else
942 {
943 error ("PIOCWSTOP, stopped for unknown/unhandled reason, flags %#x",
944 pi.prstatus.pr_flags);
945 /* NOTREACHED */
946 }
947 if (statloc)
948 {
949 *statloc = statval;
950 }
951 return (rtnval);
952}
953
954/*
955
956GLOBAL FUNCTION
957
958 child_resume -- resume execution of the inferior process
959
960SYNOPSIS
961
962 void child_resume (int step, int signal)
963
964DESCRIPTION
965
966 Resume execution of the inferior process. If STEP is nozero, then
967 just single step it. If SIGNAL is nonzero, restart it with that
968 signal activated.
969
970NOTE
971
972 It may not be absolutely necessary to specify the PC value for
973 restarting, but to be safe we use the value that gdb considers
974 to be current. One case where this might be necessary is if the
975 user explicitly changes the PC value that gdb considers to be
976 current. FIXME: Investigate if this is necessary or not.
977 */
978
979void
1ab3bf1b
JG
980child_resume (step, signal)
981 int step;
982 int signal;
35f5886e
FF
983{
984 errno = 0;
985 pi.prrun.pr_flags = PRSVADDR | PRSTRACE | PRSFAULT | PRCFAULT;
986 pi.prrun.pr_vaddr = (caddr_t) *(int *) &registers[REGISTER_BYTE (PC_REGNUM)];
987 if (signal)
988 {
989 if (signal != pi.prstatus.pr_cursig)
990 {
991 struct siginfo siginfo;
992 siginfo.si_signo = signal;
993 siginfo.si_code = 0;
994 siginfo.si_errno = 0;
995 (void) ioctl (pi.fd, PIOCSSIG, &siginfo);
996 }
997 }
998 else
999 {
1000 pi.prrun.pr_flags |= PRCSIG;
1001 }
1002 if (step)
1003 {
1004 pi.prrun.pr_flags |= PRSTEP;
1005 }
1006 if (ioctl (pi.fd, PIOCRUN, &pi.prrun) != 0)
1007 {
1008 perror_with_name (pi.pathname);
1009 /* NOTREACHED */
1010 }
1011}
1012
1013/*
1014
1015GLOBAL FUNCTION
1016
1017 fetch_inferior_registers -- fetch current registers from inferior
1018
1019SYNOPSIS
1020
1ab3bf1b 1021 void fetch_inferior_registers (int regno)
35f5886e
FF
1022
1023DESCRIPTION
1024
1025 Read the current values of the inferior's registers, both the
1026 general register set and floating point registers (if supported)
1027 and update gdb's idea of their current values.
1028
1029*/
1030
1031void
1ab3bf1b
JG
1032fetch_inferior_registers (regno)
1033 int regno;
35f5886e
FF
1034{
1035 if (ioctl (pi.fd, PIOCGREG, &pi.gregset) != -1)
1036 {
1037 supply_gregset (&pi.gregset);
1038 }
1039#if defined (FP0_REGNUM)
1040 if (ioctl (pi.fd, PIOCGFPREG, &pi.fpregset) != -1)
1041 {
1042 supply_fpregset (&pi.fpregset);
1043 }
1044#endif
1045}
1046
fb182850
FF
1047/*
1048
1049GLOBAL FUNCTION
1050
1051 fetch_core_registers -- fetch current registers from core file data
1052
1053SYNOPSIS
1054
1055 void fetch_core_registers (char *core_reg_sect, unsigned core_reg_size,
1ab3bf1b 1056 int which, unsigned in reg_addr)
fb182850
FF
1057
1058DESCRIPTION
1059
1060 Read the values of either the general register set (WHICH equals 0)
1061 or the floating point register set (WHICH equals 2) from the core
1062 file data (pointed to by CORE_REG_SECT), and update gdb's idea of
1063 their current values. The CORE_REG_SIZE parameter is ignored.
1064
1065NOTES
1066
1067 Use the indicated sizes to validate the gregset and fpregset
1068 structures.
1069*/
1070
1071void
1ab3bf1b 1072fetch_core_registers (core_reg_sect, core_reg_size, which, reg_addr)
fb182850
FF
1073 char *core_reg_sect;
1074 unsigned core_reg_size;
1075 int which;
1ab3bf1b 1076 unsigned int reg_addr; /* Unused in this version */
fb182850
FF
1077{
1078
1079 if (which == 0)
1080 {
1081 if (core_reg_size != sizeof (pi.gregset))
1082 {
1083 warning ("wrong size gregset struct in core file");
1084 }
1085 else
1086 {
1087 (void) memcpy ((char *) &pi.gregset, core_reg_sect,
1088 sizeof (pi.gregset));
1089 supply_gregset (&pi.gregset);
1090 }
1091 }
1092 else if (which == 2)
1093 {
1094 if (core_reg_size != sizeof (pi.fpregset))
1095 {
1096 warning ("wrong size fpregset struct in core file");
1097 }
1098 else
1099 {
1100 (void) memcpy ((char *) &pi.fpregset, core_reg_sect,
1101 sizeof (pi.fpregset));
1102#if defined (FP0_REGNUM)
1103 supply_fpregset (&pi.fpregset);
1104#endif
1105 }
1106 }
1107}
35f5886e
FF
1108
1109/*
1110
1111LOCAL FUNCTION
1112
1113 proc_init_failed - called whenever /proc access initialization fails
1114
1115SYNOPSIS
1116
1117 static void proc_init_failed (char *why)
1118
1119DESCRIPTION
1120
1121 This function is called whenever initialization of access to a /proc
1122 entry fails. It prints a suitable error message, does some cleanup,
1123 and then invokes the standard error processing routine which dumps
1124 us back into the command loop.
1125 */
1126
1127static void
1ab3bf1b
JG
1128proc_init_failed (why)
1129 char *why;
35f5886e
FF
1130{
1131 print_sys_errmsg (pi.pathname, errno);
1132 (void) kill (pi.pid, SIGKILL);
a39ad5ce 1133 close_proc_file (&pi);
35f5886e
FF
1134 error (why);
1135 /* NOTREACHED */
1136}
1137
1138/*
1139
1140LOCAL FUNCTION
1141
1142 close_proc_file - close any currently open /proc entry
1143
1144SYNOPSIS
1145
a39ad5ce 1146 static void close_proc_file (struct procinfo *pip)
35f5886e
FF
1147
1148DESCRIPTION
1149
1150 Close any currently open /proc entry and mark the process information
1151 entry as invalid. In order to ensure that we don't try to reuse any
1152 stale information, the pid, fd, and pathnames are explicitly
1153 invalidated, which may be overkill.
1154
1155 */
1156
1157static void
1ab3bf1b
JG
1158close_proc_file (pip)
1159 struct procinfo *pip;
35f5886e 1160{
a39ad5ce
FF
1161 pip -> pid = 0;
1162 if (pip -> valid)
35f5886e 1163 {
a39ad5ce 1164 (void) close (pip -> fd);
35f5886e 1165 }
a39ad5ce
FF
1166 pip -> fd = -1;
1167 if (pip -> pathname)
35f5886e 1168 {
a39ad5ce
FF
1169 free (pip -> pathname);
1170 pip -> pathname = NULL;
35f5886e 1171 }
a39ad5ce 1172 pip -> valid = 0;
35f5886e
FF
1173}
1174
1175/*
1176
1177LOCAL FUNCTION
1178
1179 open_proc_file - open a /proc entry for a given process id
1180
1181SYNOPSIS
1182
a39ad5ce 1183 static int open_proc_file (pid, struct procinfo *pip)
35f5886e
FF
1184
1185DESCRIPTION
1186
1187 Given a process id, close the existing open /proc entry (if any)
1188 and open one for the new process id. Once it is open, then
1189 mark the local process information structure as valid, which
1190 guarantees that the pid, fd, and pathname fields match an open
1191 /proc entry. Returns zero if the open fails, nonzero otherwise.
1192
1193 Note that the pathname is left intact, even when the open fails,
1194 so that callers can use it to construct meaningful error messages
1195 rather than just "file open failed".
1196 */
1197
1198static int
1ab3bf1b
JG
1199open_proc_file (pid, pip)
1200 int pid;
1201 struct procinfo *pip;
35f5886e 1202{
a39ad5ce
FF
1203 pip -> valid = 0;
1204 if (pip -> valid)
1205 {
1206 (void) close (pip -> fd);
1207 }
1208 if (pip -> pathname == NULL)
1209 {
1210 pip -> pathname = xmalloc (32);
1211 }
1212 sprintf (pip -> pathname, PROC_NAME_FMT, pid);
1213 if ((pip -> fd = open (pip -> pathname, O_RDWR)) >= 0)
1214 {
1215 pip -> valid = 1;
1216 pip -> pid = pid;
1217 }
1218 return (pip -> valid);
1219}
1220
f66f459f 1221static char *
1ab3bf1b
JG
1222mappingflags (flags)
1223 long flags;
a39ad5ce
FF
1224{
1225 static char asciiflags[7];
1226
1227 strcpy (asciiflags, "------");
1228 if (flags & MA_STACK) asciiflags[0] = 's';
1229 if (flags & MA_BREAK) asciiflags[1] = 'b';
1230 if (flags & MA_SHARED) asciiflags[2] = 's';
1231 if (flags & MA_READ) asciiflags[3] = 'r';
1232 if (flags & MA_WRITE) asciiflags[4] = 'w';
1233 if (flags & MA_EXEC) asciiflags[5] = 'x';
1234 return (asciiflags);
1235}
1236
1237static void
1ab3bf1b
JG
1238proc_info_address_map (pip, verbose)
1239 struct procinfo *pip;
1240 int verbose;
a39ad5ce
FF
1241{
1242 int nmap;
1243 struct prmap *prmaps;
1244 struct prmap *prmap;
1245
1246 printf_filtered ("Mapped address spaces:\n\n");
1247 printf_filtered ("\t%10s %10s %10s %10s %6s\n",
1248 "Start Addr",
1249 " End Addr",
1250 " Size",
1251 " Offset",
1252 "Flags");
1253 if (ioctl (pip -> fd, PIOCNMAP, &nmap) == 0)
1254 {
1ab3bf1b 1255 prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
a39ad5ce
FF
1256 if (ioctl (pip -> fd, PIOCMAP, prmaps) == 0)
1257 {
1258 for (prmap = prmaps; prmap -> pr_size; ++prmap)
1259 {
1260 printf_filtered ("\t%#10x %#10x %#10x %#10x %6s\n",
1261 prmap -> pr_vaddr,
1262 prmap -> pr_vaddr + prmap -> pr_size - 1,
1263 prmap -> pr_size,
1264 prmap -> pr_off,
1265 mappingflags (prmap -> pr_mflags));
1266 }
1267 }
1268 }
1269 printf_filtered ("\n\n");
1270}
1271
1272/*
1273
1274LOCAL FUNCTION
1275
1276 proc_info -- implement the "info proc" command
1277
1278SYNOPSIS
1279
1280 void proc_info (char *args, int from_tty)
1281
1282DESCRIPTION
1283
1284 Implement gdb's "info proc" command by using the /proc interface
1285 to print status information about any currently running process.
1286
1287 Examples of the use of "info proc" are:
1288
1289 info proc Print short info about current inferior.
1290 info proc verbose Print verbose info about current inferior.
1291 info proc 123 Print short info about process pid 123.
1292 info proc 123 verbose Print verbose info about process pid 123.
1293
1294 */
1295
1296static void
1ab3bf1b
JG
1297proc_info (args, from_tty)
1298 char *args;
1299 int from_tty;
a39ad5ce
FF
1300{
1301 int verbose = 0;
1302 int pid;
1303 struct procinfo pii;
1304 struct procinfo *pip;
1305 struct cleanup *old_chain;
1306 char *nexttok;
a39ad5ce
FF
1307
1308 old_chain = make_cleanup (null_cleanup, 0);
1309
1310 /* Default to using the current inferior if no pid specified */
1311
1312 pip = &pi;
1313
1314 /* Parse the args string, looking for "verbose" (or any abbrev) and
1315 for a specific pid. If a specific pid is found, the process
1316 file is opened. */
1317
1318 if (args != NULL)
35f5886e 1319 {
a39ad5ce
FF
1320 while ((nexttok = strtok (args, " \t")) != NULL)
1321 {
1322 args = NULL;
1323 if (strncmp (nexttok, "verbose", strlen (nexttok)) == 0)
1324 {
1325 verbose++;
1326 }
1327 else if ((pii.pid = atoi (nexttok)) > 0)
1328 {
1329 pid = pii.pid;
1330 pip = &pii;
1331 (void) memset (&pii, 0, sizeof (pii));
1332 if (!open_proc_file (pid, pip))
1333 {
1334 perror_with_name (pip -> pathname);
1335 /* NOTREACHED */
1336 }
1337 make_cleanup (close_proc_file, pip);
1338 }
1339 }
35f5886e 1340 }
a39ad5ce
FF
1341
1342 /* If we don't have a valid open process at this point, then we have no
1343 inferior or didn't specify a specific pid. */
1344
1345 if (!pip -> valid)
35f5886e 1346 {
a39ad5ce 1347 error ("No process. Run an inferior or specify an explicit pid.");
35f5886e 1348 }
a39ad5ce 1349 if (ioctl (pip -> fd, PIOCSTATUS, &(pip -> prstatus)) < 0)
35f5886e 1350 {
a39ad5ce
FF
1351 print_sys_errmsg (pip -> pathname, errno);
1352 error ("PIOCSTATUS failed");
35f5886e 1353 }
a39ad5ce
FF
1354
1355 printf_filtered ("\nStatus information for %s:\n\n", pip -> pathname);
1356 proc_info_address_map (pip, verbose);
1357#if 0
1358 proc_info_flags (pip, verbose);
1359 proc_info_why (pip, verbose);
1360 proc_info_what (pip, verbose);
1361 proc_info_info (pip, verbose);
1362 proc_info_cursig (pip, verbose);
1363 proc_info_sigpend (pip, verbose);
1364 proc_info_sighold (pip, verbose);
1365 proc_info_altstack (pip, verbose);
1366 proc_info_action (pip, verbose);
1367 proc_info_id (pip, verbose);
1368 proc_info_times (pip, verbose);
1369 proc_info_clname (pip,verbose);
1370 proc_info_instr (pip, verbose);
1371 proc_info_reg (pip, verbose);
1372#endif
1373
1374 /* All done, deal with closing any temporary process info structure,
1375 freeing temporary memory , etc. */
1376
1377 do_cleanups (old_chain);
1378}
1379
1380/*
1381
1382GLOBAL FUNCTION
1383
1384 _initialize_proc_fs -- initialize the process file system stuff
1385
1386SYNOPSIS
1387
1388 void _initialize_proc_fs (void)
1389
1390DESCRIPTION
1391
1392 Do required initializations during gdb startup for using the
1393 /proc file system interface.
1394
1395*/
1396
1397static char *proc_desc =
1398"Show current process status information using /proc entry.\n\
1399With no arguments, prints short form. With 'verbose' prints long form.";
1400
1401void
1402_initialize_proc_fs ()
1403{
1404 add_info ("proc", proc_info, proc_desc);
35f5886e
FF
1405}
1406
1407#endif /* USE_PROC_FS */
This page took 0.087627 seconds and 4 git commands to generate.