1 /* Linux-specific methods for using the /proc file system.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
23 #include <sys/param.h> /* for MAXPATHLEN */
24 #include <sys/procfs.h> /* for elf_gregset etc. */
25 #include <sys/stat.h> /* for struct stat */
26 #include <ctype.h> /* for isdigit */
27 #include "regcache.h" /* for registers_changed */
28 #include "gregset.h" /* for gregset */
29 #include "gdbcore.h" /* for get_exec_file */
30 #include "gdbthread.h" /* for struct thread_info etc. */
31 #include "elf-bfd.h" /* for elfcore_write_* */
32 #include "cli/cli-decode.h" /* for add_info */
34 /* Function: child_pid_to_exec_file
36 * Accepts an integer pid
37 * Returns a string representing a file that can be opened
38 * to get the symbols for the child process.
42 child_pid_to_exec_file (int pid
)
46 name1
= xmalloc (MAXPATHLEN
);
47 name2
= xmalloc (MAXPATHLEN
);
48 make_cleanup (xfree
, name1
);
49 make_cleanup (xfree
, name2
);
50 memset (name2
, 0, MAXPATHLEN
);
52 sprintf (name1
, "/proc/%d/exe", pid
);
53 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
59 /* Function: read_mappings
61 * Service function for corefiles and info proc.
65 read_mapping (FILE *mapfile
,
74 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
75 addr
, endaddr
, permissions
, offset
, device
, inode
);
77 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
79 ret
+= fscanf (mapfile
, "%s\n", filename
);
83 filename
[0] = '\0'; /* no filename */
84 fscanf (mapfile
, "\n");
86 return (ret
!= 0 && ret
!= EOF
);
89 /* Function: linux_find_memory_regions
91 * Fills the "to_find_memory_regions" target vector.
92 * Lists the memory regions in the inferior for a corefile.
96 linux_find_memory_regions (int (*func
) (CORE_ADDR
,
102 long long pid
= PIDGET (inferior_ptid
);
103 char mapsfilename
[MAXPATHLEN
];
105 long long addr
, endaddr
, size
, offset
, inode
;
106 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
107 int read
, write
, exec
;
110 /* Compose the filename for the /proc memory map, and open it. */
111 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
112 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
113 error ("Could not open %s\n", mapsfilename
);
116 fprintf_filtered (gdb_stdout
,
117 "Reading memory regions from %s\n", mapsfilename
);
119 /* Now iterate until end-of-file. */
120 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
121 &offset
, &device
[0], &inode
, &filename
[0]))
123 size
= endaddr
- addr
;
125 /* Get the segment's permissions. */
126 read
= (strchr (permissions
, 'r') != 0);
127 write
= (strchr (permissions
, 'w') != 0);
128 exec
= (strchr (permissions
, 'x') != 0);
132 fprintf_filtered (gdb_stdout
,
133 "Save segment, %lld bytes at 0x%s (%c%c%c)",
134 size
, paddr_nz (addr
),
138 if (filename
&& filename
[0])
139 fprintf_filtered (gdb_stdout
,
140 " for %s", filename
);
141 fprintf_filtered (gdb_stdout
, "\n");
144 /* Invoke the callback function to create the corefile segment. */
145 func (addr
, size
, read
, write
, exec
, obfd
);
151 /* Function: linux_do_thread_registers
153 * Records the thread's register state for the corefile note section.
157 linux_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
158 char *note_data
, int *note_size
)
161 gdb_fpregset_t fpregs
;
162 unsigned long merged_pid
= ptid_get_tid (ptid
) << 16 | ptid_get_pid (ptid
);
164 fill_gregset (&gregs
, -1);
165 note_data
= (char *) elfcore_write_prstatus (obfd
,
172 fill_fpregset (&fpregs
, -1);
173 note_data
= (char *) elfcore_write_prfpreg (obfd
,
181 struct linux_corefile_thread_data
{
187 /* Function: linux_corefile_thread_callback
189 * Called by gdbthread.c once per thread.
190 * Records the thread's register state for the corefile note section.
194 linux_corefile_thread_callback (struct thread_info
*ti
, void *data
)
196 struct linux_corefile_thread_data
*args
= data
;
197 ptid_t saved_ptid
= inferior_ptid
;
199 inferior_ptid
= ti
->ptid
;
200 registers_changed ();
201 target_fetch_registers (-1); /* FIXME should not be necessary;
202 fill_gregset should do it automatically. */
203 args
->note_data
= linux_do_thread_registers (args
->obfd
,
207 inferior_ptid
= saved_ptid
;
208 registers_changed ();
209 target_fetch_registers (-1); /* FIXME should not be necessary;
210 fill_gregset should do it automatically. */
214 /* Function: linux_make_note_section
216 * Fills the "to_make_corefile_note" target vector.
217 * Builds the note section for a corefile, and returns it
218 * in a malloc buffer.
222 linux_make_note_section (bfd
*obfd
, int *note_size
)
224 struct linux_corefile_thread_data thread_args
;
225 struct cleanup
*old_chain
;
226 char fname
[16] = {'\0'};
227 char psargs
[80] = {'\0'};
228 char *note_data
= NULL
;
229 ptid_t current_ptid
= inferior_ptid
;
231 if (get_exec_file (0))
233 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
234 strncpy (psargs
, get_exec_file (0),
236 if (get_inferior_args ())
238 strncat (psargs
, " ",
239 sizeof (psargs
) - strlen (psargs
));
240 strncat (psargs
, get_inferior_args (),
241 sizeof (psargs
) - strlen (psargs
));
243 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
250 /* Dump information for threads. */
251 thread_args
.obfd
= obfd
;
252 thread_args
.note_data
= note_data
;
253 thread_args
.note_size
= note_size
;
254 iterate_over_threads (linux_corefile_thread_callback
, &thread_args
);
255 if (thread_args
.note_data
== note_data
)
257 /* iterate_over_threads didn't come up with any threads;
258 just use inferior_ptid. */
259 note_data
= linux_do_thread_registers (obfd
, inferior_ptid
,
260 note_data
, note_size
);
264 note_data
= thread_args
.note_data
;
267 make_cleanup (xfree
, note_data
);
272 * Function: linux_info_proc_cmd
274 * Implement the "info proc" command.
278 linux_info_proc_cmd (char *args
, int from_tty
)
280 long long pid
= PIDGET (inferior_ptid
);
283 char buffer
[MAXPATHLEN
];
284 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
297 /* Break up 'args' into an argv array. */
298 if ((argv
= buildargv (args
)) == NULL
)
301 make_cleanup_freeargv (argv
);
303 while (argv
!= NULL
&& *argv
!= NULL
)
305 if (isdigit (argv
[0][0]))
307 pid
= strtoul (argv
[0], NULL
, 10);
309 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
313 else if (strcmp (argv
[0], "status") == 0)
317 else if (strcmp (argv
[0], "stat") == 0)
321 else if (strcmp (argv
[0], "cmd") == 0)
325 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
329 else if (strcmp (argv
[0], "cwd") == 0)
333 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
339 /* [...] (future options here) */
344 error ("No current process: you must name one.");
346 sprintf (fname1
, "/proc/%lld", pid
);
347 if (stat (fname1
, &dummy
) != 0)
348 error ("No /proc directory: '%s'", fname1
);
350 printf_filtered ("process %lld\n", pid
);
351 if (cmdline_f
|| all
)
353 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
354 if ((procfile
= fopen (fname1
, "r")) > 0)
356 fgets (buffer
, sizeof (buffer
), procfile
);
357 printf_filtered ("cmdline = '%s'\n", buffer
);
361 warning ("unable to open /proc file '%s'", fname1
);
365 sprintf (fname1
, "/proc/%lld/cwd", pid
);
366 memset (fname2
, 0, sizeof (fname2
));
367 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
368 printf_filtered ("cwd = '%s'\n", fname2
);
370 warning ("unable to read link '%s'", fname1
);
374 sprintf (fname1
, "/proc/%lld/exe", pid
);
375 memset (fname2
, 0, sizeof (fname2
));
376 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
377 printf_filtered ("exe = '%s'\n", fname2
);
379 warning ("unable to read link '%s'", fname1
);
381 if (mappings_f
|| all
)
383 sprintf (fname1
, "/proc/%lld/maps", pid
);
384 if ((procfile
= fopen (fname1
, "r")) > 0)
386 long long addr
, endaddr
, size
, offset
, inode
;
387 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
388 char *header_fmt_string
, *data_fmt_string
;
390 if (TARGET_ADDR_BIT
== 32)
392 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
393 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
397 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
398 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
401 printf_filtered ("Mapped address spaces:\n\n");
402 printf_filtered (header_fmt_string
,
409 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
410 &offset
, &device
[0], &inode
, &filename
[0]))
412 size
= endaddr
- addr
;
413 printf_filtered (data_fmt_string
,
414 (unsigned long) addr
, /* FIXME: pr_addr */
415 (unsigned long) endaddr
,
417 (unsigned int) offset
,
418 filename
[0] ? filename
: "");
425 warning ("unable to open /proc file '%s'", fname1
);
429 sprintf (fname1
, "/proc/%lld/status", pid
);
430 if ((procfile
= fopen (fname1
, "r")) > 0)
432 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
433 printf_filtered (buffer
);
437 warning ("unable to open /proc file '%s'", fname1
);
441 sprintf (fname1
, "/proc/%lld/stat", pid
);
442 if ((procfile
= fopen (fname1
, "r")) > 0)
447 if (fscanf (procfile
, "%d ", &itmp
) > 0)
448 printf_filtered ("Process: %d\n", itmp
);
449 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
450 printf_filtered ("Exec file: %s\n", buffer
);
451 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
452 printf_filtered ("State: %c\n", ctmp
);
453 if (fscanf (procfile
, "%d ", &itmp
) > 0)
454 printf_filtered ("Parent process: %d\n", itmp
);
455 if (fscanf (procfile
, "%d ", &itmp
) > 0)
456 printf_filtered ("Process group: %d\n", itmp
);
457 if (fscanf (procfile
, "%d ", &itmp
) > 0)
458 printf_filtered ("Session id: %d\n", itmp
);
459 if (fscanf (procfile
, "%d ", &itmp
) > 0)
460 printf_filtered ("TTY: %d\n", itmp
);
461 if (fscanf (procfile
, "%d ", &itmp
) > 0)
462 printf_filtered ("TTY owner process group: %d\n", itmp
);
463 if (fscanf (procfile
, "%u ", &itmp
) > 0)
464 printf_filtered ("Flags: 0x%x\n", itmp
);
465 if (fscanf (procfile
, "%u ", &itmp
) > 0)
466 printf_filtered ("Minor faults (no memory page): %u\n",
467 (unsigned int) itmp
);
468 if (fscanf (procfile
, "%u ", &itmp
) > 0)
469 printf_filtered ("Minor faults, children: %u\n",
470 (unsigned int) itmp
);
471 if (fscanf (procfile
, "%u ", &itmp
) > 0)
472 printf_filtered ("Major faults (memory page faults): %u\n",
473 (unsigned int) itmp
);
474 if (fscanf (procfile
, "%u ", &itmp
) > 0)
475 printf_filtered ("Major faults, children: %u\n",
476 (unsigned int) itmp
);
477 if (fscanf (procfile
, "%d ", &itmp
) > 0)
478 printf_filtered ("utime: %d\n", itmp
);
479 if (fscanf (procfile
, "%d ", &itmp
) > 0)
480 printf_filtered ("stime: %d\n", itmp
);
481 if (fscanf (procfile
, "%d ", &itmp
) > 0)
482 printf_filtered ("utime, children: %d\n", itmp
);
483 if (fscanf (procfile
, "%d ", &itmp
) > 0)
484 printf_filtered ("stime, children: %d\n", itmp
);
485 if (fscanf (procfile
, "%d ", &itmp
) > 0)
486 printf_filtered ("jiffies remaining in current time slice: %d\n",
488 if (fscanf (procfile
, "%d ", &itmp
) > 0)
489 printf_filtered ("'nice' value: %d\n", itmp
);
490 if (fscanf (procfile
, "%u ", &itmp
) > 0)
491 printf_filtered ("jiffies until next timeout: %u\n",
492 (unsigned int) itmp
);
493 if (fscanf (procfile
, "%u ", &itmp
) > 0)
494 printf_filtered ("jiffies until next SIGALRM: %u\n",
495 (unsigned int) itmp
);
496 if (fscanf (procfile
, "%d ", &itmp
) > 0)
497 printf_filtered ("start time (jiffies since system boot): %d\n",
499 if (fscanf (procfile
, "%u ", &itmp
) > 0)
500 printf_filtered ("Virtual memory size: %u\n",
501 (unsigned int) itmp
);
502 if (fscanf (procfile
, "%u ", &itmp
) > 0)
503 printf_filtered ("Resident set size: %u\n",
504 (unsigned int) itmp
);
505 if (fscanf (procfile
, "%u ", &itmp
) > 0)
506 printf_filtered ("rlim: %u\n",
507 (unsigned int) itmp
);
508 if (fscanf (procfile
, "%u ", &itmp
) > 0)
509 printf_filtered ("Start of text: 0x%x\n", itmp
);
510 if (fscanf (procfile
, "%u ", &itmp
) > 0)
511 printf_filtered ("End of text: 0x%x\n", itmp
);
512 if (fscanf (procfile
, "%u ", &itmp
) > 0)
513 printf_filtered ("Start of stack: 0x%x\n", itmp
);
514 #if 0 /* Don't know how architecture-dependent the rest is...
515 Anyway the signal bitmap info is available from "status". */
516 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
517 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp
);
518 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
519 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp
);
520 if (fscanf (procfile
, "%d ", &itmp
) > 0)
521 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp
);
522 if (fscanf (procfile
, "%d ", &itmp
) > 0)
523 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp
);
524 if (fscanf (procfile
, "%d ", &itmp
) > 0)
525 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp
);
526 if (fscanf (procfile
, "%d ", &itmp
) > 0)
527 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp
);
528 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
529 printf_filtered ("wchan (system call): 0x%x\n", itmp
);
534 warning ("unable to open /proc file '%s'", fname1
);
539 _initialize_linux_proc (void)
541 extern void inftarg_set_find_memory_regions ();
542 extern void inftarg_set_make_corefile_notes ();
544 inftarg_set_find_memory_regions (linux_find_memory_regions
);
545 inftarg_set_make_corefile_notes (linux_make_note_section
);
547 add_info ("proc", linux_info_proc_cmd
,
548 "Show /proc process information about any running process.\n\
549 Specify any process id, or use the program being debugged by default.\n\
550 Specify any of the following keywords for detailed info:\n\
551 mappings -- list of mapped memory regions.\n\
552 stat -- list a bunch of random process info.\n\
553 status -- list a different bunch of random process info.\n\
554 all -- list all available /proc info.");