1 /* GNU/Linux specific methods for using the /proc file system.
3 Copyright 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
24 #include <sys/param.h> /* for MAXPATHLEN */
25 #include <sys/procfs.h> /* for elf_gregset etc. */
26 #include "gdb_stat.h" /* for struct stat */
27 #include <ctype.h> /* for isdigit */
28 #include <unistd.h> /* for open, pread64 */
29 #include <fcntl.h> /* for O_RDONLY */
30 #include "regcache.h" /* for registers_changed */
31 #include "gregset.h" /* for gregset */
32 #include "gdbcore.h" /* for get_exec_file */
33 #include "gdbthread.h" /* for struct thread_info etc. */
34 #include "elf-bfd.h" /* for elfcore_write_* */
35 #include "cli/cli-decode.h" /* for add_info */
36 #include "gdb_string.h"
38 #include "linux-nat.h"
44 /* Function: child_pid_to_exec_file
46 * Accepts an integer pid
47 * Returns a string representing a file that can be opened
48 * to get the symbols for the child process.
52 child_pid_to_exec_file (int pid
)
56 name1
= xmalloc (MAXPATHLEN
);
57 name2
= xmalloc (MAXPATHLEN
);
58 make_cleanup (xfree
, name1
);
59 make_cleanup (xfree
, name2
);
60 memset (name2
, 0, MAXPATHLEN
);
62 sprintf (name1
, "/proc/%d/exe", pid
);
63 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
69 /* Function: read_mappings
71 * Service function for corefiles and info proc.
75 read_mapping (FILE *mapfile
,
80 char *device
, long long *inode
, char *filename
)
82 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
83 addr
, endaddr
, permissions
, offset
, device
, inode
);
85 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
87 /* Eat everything up to EOL for the filename. This will prevent
88 weird filenames (such as one with embedded whitespace) from
89 confusing this code. It also makes this code more robust
90 in respect to annotations the kernel may add after the
93 Note the filename is used for informational purposes only. */
94 ret
+= fscanf (mapfile
, "%[^\n]\n", filename
);
98 filename
[0] = '\0'; /* no filename */
99 fscanf (mapfile
, "\n");
101 return (ret
!= 0 && ret
!= EOF
);
104 /* Function: linux_find_memory_regions
106 * Fills the "to_find_memory_regions" target vector.
107 * Lists the memory regions in the inferior for a corefile.
111 linux_find_memory_regions (int (*func
) (CORE_ADDR
,
113 int, int, int, void *), void *obfd
)
115 long long pid
= PIDGET (inferior_ptid
);
116 char mapsfilename
[MAXPATHLEN
];
118 long long addr
, endaddr
, size
, offset
, inode
;
119 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
120 int read
, write
, exec
;
123 /* Compose the filename for the /proc memory map, and open it. */
124 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
125 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
126 error ("Could not open %s\n", mapsfilename
);
129 fprintf_filtered (gdb_stdout
,
130 "Reading memory regions from %s\n", mapsfilename
);
132 /* Now iterate until end-of-file. */
133 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
134 &offset
, &device
[0], &inode
, &filename
[0]))
136 size
= endaddr
- addr
;
138 /* Get the segment's permissions. */
139 read
= (strchr (permissions
, 'r') != 0);
140 write
= (strchr (permissions
, 'w') != 0);
141 exec
= (strchr (permissions
, 'x') != 0);
145 fprintf_filtered (gdb_stdout
,
146 "Save segment, %lld bytes at 0x%s (%c%c%c)",
147 size
, paddr_nz (addr
),
149 write
? 'w' : ' ', exec
? 'x' : ' ');
150 if (filename
&& filename
[0])
151 fprintf_filtered (gdb_stdout
, " for %s", filename
);
152 fprintf_filtered (gdb_stdout
, "\n");
155 /* Invoke the callback function to create the corefile segment. */
156 func (addr
, size
, read
, write
, exec
, obfd
);
162 /* Function: linux_do_thread_registers
164 * Records the thread's register state for the corefile note section.
168 linux_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
169 char *note_data
, int *note_size
)
172 gdb_fpregset_t fpregs
;
173 #ifdef FILL_FPXREGSET
174 gdb_fpxregset_t fpxregs
;
176 unsigned long lwp
= ptid_get_lwp (ptid
);
178 fill_gregset (&gregs
, -1);
179 note_data
= (char *) elfcore_write_prstatus (obfd
,
183 stop_signal
, &gregs
);
185 fill_fpregset (&fpregs
, -1);
186 note_data
= (char *) elfcore_write_prfpreg (obfd
,
189 &fpregs
, sizeof (fpregs
));
190 #ifdef FILL_FPXREGSET
191 fill_fpxregset (&fpxregs
, -1);
192 note_data
= (char *) elfcore_write_prxfpreg (obfd
,
195 &fpxregs
, sizeof (fpxregs
));
200 struct linux_corefile_thread_data
208 /* Function: linux_corefile_thread_callback
210 * Called by gdbthread.c once per thread.
211 * Records the thread's register state for the corefile note section.
215 linux_corefile_thread_callback (struct lwp_info
*ti
, void *data
)
217 struct linux_corefile_thread_data
*args
= data
;
218 ptid_t saved_ptid
= inferior_ptid
;
220 inferior_ptid
= ti
->ptid
;
221 registers_changed ();
222 target_fetch_registers (-1); /* FIXME should not be necessary;
223 fill_gregset should do it automatically. */
224 args
->note_data
= linux_do_thread_registers (args
->obfd
,
229 inferior_ptid
= saved_ptid
;
230 registers_changed ();
231 target_fetch_registers (-1); /* FIXME should not be necessary;
232 fill_gregset should do it automatically. */
236 /* Function: linux_make_note_section
238 * Fills the "to_make_corefile_note" target vector.
239 * Builds the note section for a corefile, and returns it
240 * in a malloc buffer.
244 linux_make_note_section (bfd
*obfd
, int *note_size
)
246 struct linux_corefile_thread_data thread_args
;
247 struct cleanup
*old_chain
;
248 char fname
[16] = { '\0' };
249 char psargs
[80] = { '\0' };
250 char *note_data
= NULL
;
251 ptid_t current_ptid
= inferior_ptid
;
253 if (get_exec_file (0))
255 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
256 strncpy (psargs
, get_exec_file (0), sizeof (psargs
));
257 if (get_inferior_args ())
259 strncat (psargs
, " ", sizeof (psargs
) - strlen (psargs
));
260 strncat (psargs
, get_inferior_args (),
261 sizeof (psargs
) - strlen (psargs
));
263 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
265 note_size
, fname
, psargs
);
268 /* Dump information for threads. */
269 thread_args
.obfd
= obfd
;
270 thread_args
.note_data
= note_data
;
271 thread_args
.note_size
= note_size
;
272 thread_args
.num_notes
= 0;
273 iterate_over_lwps (linux_corefile_thread_callback
, &thread_args
);
274 if (thread_args
.num_notes
== 0)
276 /* iterate_over_threads didn't come up with any threads;
277 just use inferior_ptid. */
278 note_data
= linux_do_thread_registers (obfd
, inferior_ptid
,
279 note_data
, note_size
);
283 note_data
= thread_args
.note_data
;
286 make_cleanup (xfree
, note_data
);
291 * Function: linux_info_proc_cmd
293 * Implement the "info proc" command.
297 linux_info_proc_cmd (char *args
, int from_tty
)
299 long long pid
= PIDGET (inferior_ptid
);
302 char buffer
[MAXPATHLEN
];
303 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
316 /* Break up 'args' into an argv array. */
317 if ((argv
= buildargv (args
)) == NULL
)
320 make_cleanup_freeargv (argv
);
322 while (argv
!= NULL
&& *argv
!= NULL
)
324 if (isdigit (argv
[0][0]))
326 pid
= strtoul (argv
[0], NULL
, 10);
328 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
332 else if (strcmp (argv
[0], "status") == 0)
336 else if (strcmp (argv
[0], "stat") == 0)
340 else if (strcmp (argv
[0], "cmd") == 0)
344 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
348 else if (strcmp (argv
[0], "cwd") == 0)
352 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
358 /* [...] (future options here) */
363 error ("No current process: you must name one.");
365 sprintf (fname1
, "/proc/%lld", pid
);
366 if (stat (fname1
, &dummy
) != 0)
367 error ("No /proc directory: '%s'", fname1
);
369 printf_filtered ("process %lld\n", pid
);
370 if (cmdline_f
|| all
)
372 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
373 if ((procfile
= fopen (fname1
, "r")) > 0)
375 fgets (buffer
, sizeof (buffer
), procfile
);
376 printf_filtered ("cmdline = '%s'\n", buffer
);
380 warning ("unable to open /proc file '%s'", fname1
);
384 sprintf (fname1
, "/proc/%lld/cwd", pid
);
385 memset (fname2
, 0, sizeof (fname2
));
386 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
387 printf_filtered ("cwd = '%s'\n", fname2
);
389 warning ("unable to read link '%s'", fname1
);
393 sprintf (fname1
, "/proc/%lld/exe", pid
);
394 memset (fname2
, 0, sizeof (fname2
));
395 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
396 printf_filtered ("exe = '%s'\n", fname2
);
398 warning ("unable to read link '%s'", fname1
);
400 if (mappings_f
|| all
)
402 sprintf (fname1
, "/proc/%lld/maps", pid
);
403 if ((procfile
= fopen (fname1
, "r")) > 0)
405 long long addr
, endaddr
, size
, offset
, inode
;
406 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
407 char *header_fmt_string
, *data_fmt_string
;
409 if (TARGET_ADDR_BIT
== 32)
411 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
412 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
416 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
417 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
420 printf_filtered ("Mapped address spaces:\n\n");
421 printf_filtered (header_fmt_string
,
424 " Size", " Offset", "objfile");
426 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
427 &offset
, &device
[0], &inode
, &filename
[0]))
429 size
= endaddr
- addr
;
430 printf_filtered (data_fmt_string
, (unsigned long) addr
, /* FIXME: pr_addr */
431 (unsigned long) endaddr
,
433 (unsigned int) offset
,
434 filename
[0] ? filename
: "");
441 warning ("unable to open /proc file '%s'", fname1
);
445 sprintf (fname1
, "/proc/%lld/status", pid
);
446 if ((procfile
= fopen (fname1
, "r")) > 0)
448 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
449 puts_filtered (buffer
);
453 warning ("unable to open /proc file '%s'", fname1
);
457 sprintf (fname1
, "/proc/%lld/stat", pid
);
458 if ((procfile
= fopen (fname1
, "r")) > 0)
463 if (fscanf (procfile
, "%d ", &itmp
) > 0)
464 printf_filtered ("Process: %d\n", itmp
);
465 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
466 printf_filtered ("Exec file: %s\n", buffer
);
467 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
468 printf_filtered ("State: %c\n", ctmp
);
469 if (fscanf (procfile
, "%d ", &itmp
) > 0)
470 printf_filtered ("Parent process: %d\n", itmp
);
471 if (fscanf (procfile
, "%d ", &itmp
) > 0)
472 printf_filtered ("Process group: %d\n", itmp
);
473 if (fscanf (procfile
, "%d ", &itmp
) > 0)
474 printf_filtered ("Session id: %d\n", itmp
);
475 if (fscanf (procfile
, "%d ", &itmp
) > 0)
476 printf_filtered ("TTY: %d\n", itmp
);
477 if (fscanf (procfile
, "%d ", &itmp
) > 0)
478 printf_filtered ("TTY owner process group: %d\n", itmp
);
479 if (fscanf (procfile
, "%u ", &itmp
) > 0)
480 printf_filtered ("Flags: 0x%x\n", itmp
);
481 if (fscanf (procfile
, "%u ", &itmp
) > 0)
482 printf_filtered ("Minor faults (no memory page): %u\n",
483 (unsigned int) itmp
);
484 if (fscanf (procfile
, "%u ", &itmp
) > 0)
485 printf_filtered ("Minor faults, children: %u\n",
486 (unsigned int) itmp
);
487 if (fscanf (procfile
, "%u ", &itmp
) > 0)
488 printf_filtered ("Major faults (memory page faults): %u\n",
489 (unsigned int) itmp
);
490 if (fscanf (procfile
, "%u ", &itmp
) > 0)
491 printf_filtered ("Major faults, children: %u\n",
492 (unsigned int) itmp
);
493 if (fscanf (procfile
, "%d ", &itmp
) > 0)
494 printf_filtered ("utime: %d\n", itmp
);
495 if (fscanf (procfile
, "%d ", &itmp
) > 0)
496 printf_filtered ("stime: %d\n", itmp
);
497 if (fscanf (procfile
, "%d ", &itmp
) > 0)
498 printf_filtered ("utime, children: %d\n", itmp
);
499 if (fscanf (procfile
, "%d ", &itmp
) > 0)
500 printf_filtered ("stime, children: %d\n", itmp
);
501 if (fscanf (procfile
, "%d ", &itmp
) > 0)
502 printf_filtered ("jiffies remaining in current time slice: %d\n",
504 if (fscanf (procfile
, "%d ", &itmp
) > 0)
505 printf_filtered ("'nice' value: %d\n", itmp
);
506 if (fscanf (procfile
, "%u ", &itmp
) > 0)
507 printf_filtered ("jiffies until next timeout: %u\n",
508 (unsigned int) itmp
);
509 if (fscanf (procfile
, "%u ", &itmp
) > 0)
510 printf_filtered ("jiffies until next SIGALRM: %u\n",
511 (unsigned int) itmp
);
512 if (fscanf (procfile
, "%d ", &itmp
) > 0)
513 printf_filtered ("start time (jiffies since system boot): %d\n",
515 if (fscanf (procfile
, "%u ", &itmp
) > 0)
516 printf_filtered ("Virtual memory size: %u\n",
517 (unsigned int) itmp
);
518 if (fscanf (procfile
, "%u ", &itmp
) > 0)
519 printf_filtered ("Resident set size: %u\n", (unsigned int) itmp
);
520 if (fscanf (procfile
, "%u ", &itmp
) > 0)
521 printf_filtered ("rlim: %u\n", (unsigned int) itmp
);
522 if (fscanf (procfile
, "%u ", &itmp
) > 0)
523 printf_filtered ("Start of text: 0x%x\n", itmp
);
524 if (fscanf (procfile
, "%u ", &itmp
) > 0)
525 printf_filtered ("End of text: 0x%x\n", itmp
);
526 if (fscanf (procfile
, "%u ", &itmp
) > 0)
527 printf_filtered ("Start of stack: 0x%x\n", itmp
);
528 #if 0 /* Don't know how architecture-dependent the rest is...
529 Anyway the signal bitmap info is available from "status". */
530 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
531 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp
);
532 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
533 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp
);
534 if (fscanf (procfile
, "%d ", &itmp
) > 0)
535 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp
);
536 if (fscanf (procfile
, "%d ", &itmp
) > 0)
537 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp
);
538 if (fscanf (procfile
, "%d ", &itmp
) > 0)
539 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp
);
540 if (fscanf (procfile
, "%d ", &itmp
) > 0)
541 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp
);
542 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
543 printf_filtered ("wchan (system call): 0x%x\n", itmp
);
548 warning ("unable to open /proc file '%s'", fname1
);
553 _initialize_linux_proc (void)
555 extern void inftarg_set_find_memory_regions ();
556 extern void inftarg_set_make_corefile_notes ();
558 inftarg_set_find_memory_regions (linux_find_memory_regions
);
559 inftarg_set_make_corefile_notes (linux_make_note_section
);
561 add_info ("proc", linux_info_proc_cmd
,
562 "Show /proc process information about any running process.\n\
563 Specify any process id, or use the program being debugged by default.\n\
564 Specify any of the following keywords for detailed info:\n\
565 mappings -- list of mapped memory regions.\n\
566 stat -- list a bunch of random process info.\n\
567 status -- list a different bunch of random process info.\n\
568 all -- list all available /proc info.");
572 linux_proc_xfer_memory (CORE_ADDR addr
, char *myaddr
, int len
, int write
,
573 struct mem_attrib
*attrib
, struct target_ops
*target
)
581 /* Don't bother for one word. */
582 if (len
< 3 * sizeof (long))
585 /* We could keep this file open and cache it - possibly one
586 per thread. That requires some juggling, but is even faster. */
587 sprintf (filename
, "/proc/%d/mem", PIDGET (inferior_ptid
));
588 fd
= open (filename
, O_RDONLY
| O_LARGEFILE
);
592 /* If pread64 is available, use it. It's faster if the kernel
593 supports it (only one syscall), and it's 64-bit safe even
594 on 32-bit platforms (for instance, SPARC debugging a SPARC64
597 We play some autoconf and CFLAGS games to get this declaration
598 exposed: -D_XOPEN_SOURCE=500 -D_LARGEFILE64_SOURCE. And then
599 a -D_BSD_SOURCE to counteract the defaults for _XOPEN_SOURCE. */
601 if (pread64 (fd
, myaddr
, len
, addr
) != len
)
603 if (lseek (fd
, addr
, SEEK_SET
) == -1 || read (fd
, myaddr
, len
) != len
)