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 "gregset.h" /* for gregset */
28 #include "gdbcore.h" /* for get_exec_file */
29 #include "gdbthread.h" /* for struct thread_info etc. */
30 #include "elf-bfd.h" /* for elfcore_write_* */
31 #include "cli/cli-decode.h" /* for add_info */
33 /* Function: child_pid_to_exec_file
35 * Accepts an integer pid
36 * Returns a string representing a file that can be opened
37 * to get the symbols for the child process.
41 child_pid_to_exec_file (int pid
)
45 name1
= xmalloc (MAXPATHLEN
);
46 name2
= xmalloc (MAXPATHLEN
);
47 make_cleanup (xfree
, name1
);
48 make_cleanup (xfree
, name2
);
49 memset (name2
, 0, MAXPATHLEN
);
51 sprintf (name1
, "/proc/%d/exe", pid
);
52 if (readlink (name1
, name2
, MAXPATHLEN
) > 0)
58 /* Function: read_mappings
60 * Service function for corefiles and info proc.
64 read_mapping (FILE *mapfile
,
73 int ret
= fscanf (mapfile
, "%llx-%llx %s %llx %s %llx",
74 addr
, endaddr
, permissions
, offset
, device
, inode
);
76 if (ret
> 0 && ret
!= EOF
&& *inode
!= 0)
78 ret
+= fscanf (mapfile
, "%s\n", filename
);
82 filename
[0] = '\0'; /* no filename */
83 fscanf (mapfile
, "\n");
85 return (ret
!= 0 && ret
!= EOF
);
88 /* Function: linux_find_memory_regions
90 * Fills the "to_find_memory_regions" target vector.
91 * Lists the memory regions in the inferior for a corefile.
95 linux_find_memory_regions (int (*func
) (CORE_ADDR
,
101 long long pid
= PIDGET (inferior_ptid
);
102 char mapsfilename
[MAXPATHLEN
];
104 long long addr
, endaddr
, size
, offset
, inode
;
105 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
106 int read
, write
, exec
;
109 /* Compose the filename for the /proc memory map, and open it. */
110 sprintf (mapsfilename
, "/proc/%lld/maps", pid
);
111 if ((mapsfile
= fopen (mapsfilename
, "r")) == NULL
)
112 error ("Could not open %s\n", mapsfilename
);
115 fprintf_filtered (gdb_stdout
,
116 "Reading memory regions from %s\n", mapsfilename
);
118 /* Now iterate until end-of-file. */
119 while (read_mapping (mapsfile
, &addr
, &endaddr
, &permissions
[0],
120 &offset
, &device
[0], &inode
, &filename
[0]))
122 size
= endaddr
- addr
;
124 /* Get the segment's permissions. */
125 read
= (strchr (permissions
, 'r') != 0);
126 write
= (strchr (permissions
, 'w') != 0);
127 exec
= (strchr (permissions
, 'x') != 0);
131 fprintf_filtered (gdb_stdout
,
132 "Save segment, %lld bytes at 0x%s (%c%c%c)",
133 size
, paddr_nz (addr
),
137 if (filename
&& filename
[0])
138 fprintf_filtered (gdb_stdout
,
139 " for %s", filename
);
140 fprintf_filtered (gdb_stdout
, "\n");
143 /* Invoke the callback function to create the corefile segment. */
144 func (addr
, size
, read
, write
, exec
, obfd
);
150 /* Function: linux_do_thread_registers
152 * Records the thread's register state for the corefile note section.
156 linux_do_thread_registers (bfd
*obfd
, ptid_t ptid
,
157 char *note_data
, int *note_size
)
160 gdb_fpregset_t fpregs
;
161 unsigned long merged_pid
= ptid_get_tid (ptid
) << 16 | ptid_get_pid (ptid
);
163 fill_gregset (&gregs
, -1);
164 note_data
= (char *) elfcore_write_prstatus (obfd
,
171 fill_fpregset (&fpregs
, -1);
172 note_data
= (char *) elfcore_write_prfpreg (obfd
,
180 struct linux_corefile_thread_data
{
186 /* Function: linux_corefile_thread_callback
188 * Called by gdbthread.c once per thread.
189 * Records the thread's register state for the corefile note section.
193 linux_corefile_thread_callback (struct thread_info
*ti
, void *data
)
195 struct linux_corefile_thread_data
*args
= data
;
196 ptid_t saved_ptid
= inferior_ptid
;
198 inferior_ptid
= ti
->ptid
;
199 registers_changed ();
200 target_fetch_registers (-1); /* FIXME should not be necessary;
201 fill_gregset should do it automatically. */
202 args
->note_data
= linux_do_thread_registers (args
->obfd
,
206 inferior_ptid
= saved_ptid
;
207 registers_changed ();
208 target_fetch_registers (-1); /* FIXME should not be necessary;
209 fill_gregset should do it automatically. */
213 /* Function: linux_make_note_section
215 * Fills the "to_make_corefile_note" target vector.
216 * Builds the note section for a corefile, and returns it
217 * in a malloc buffer.
221 linux_make_note_section (bfd
*obfd
, int *note_size
)
223 struct linux_corefile_thread_data thread_args
;
224 struct cleanup
*old_chain
;
225 char fname
[16] = {'\0'};
226 char psargs
[80] = {'\0'};
227 char *note_data
= NULL
;
228 ptid_t current_ptid
= inferior_ptid
;
230 if (get_exec_file (0))
232 strncpy (fname
, strrchr (get_exec_file (0), '/') + 1, sizeof (fname
));
233 strncpy (psargs
, get_exec_file (0),
235 if (get_inferior_args ())
237 strncat (psargs
, " ",
238 sizeof (psargs
) - strlen (psargs
));
239 strncat (psargs
, get_inferior_args (),
240 sizeof (psargs
) - strlen (psargs
));
242 note_data
= (char *) elfcore_write_prpsinfo (obfd
,
249 /* Dump information for threads. */
250 thread_args
.obfd
= obfd
;
251 thread_args
.note_data
= note_data
;
252 thread_args
.note_size
= note_size
;
253 iterate_over_threads (linux_corefile_thread_callback
, &thread_args
);
254 if (thread_args
.note_data
== note_data
)
256 /* iterate_over_threads didn't come up with any threads;
257 just use inferior_ptid. */
258 note_data
= linux_do_thread_registers (obfd
, inferior_ptid
,
259 note_data
, note_size
);
263 note_data
= thread_args
.note_data
;
266 make_cleanup (xfree
, note_data
);
271 * Function: linux_info_proc_cmd
273 * Implement the "info proc" command.
277 linux_info_proc_cmd (char *args
, int from_tty
)
279 long long pid
= PIDGET (inferior_ptid
);
282 char buffer
[MAXPATHLEN
];
283 char fname1
[MAXPATHLEN
], fname2
[MAXPATHLEN
];
296 /* Break up 'args' into an argv array. */
297 if ((argv
= buildargv (args
)) == NULL
)
300 make_cleanup_freeargv (argv
);
302 while (argv
!= NULL
&& *argv
!= NULL
)
304 if (isdigit (argv
[0][0]))
306 pid
= strtoul (argv
[0], NULL
, 10);
308 else if (strncmp (argv
[0], "mappings", strlen (argv
[0])) == 0)
312 else if (strcmp (argv
[0], "status") == 0)
316 else if (strcmp (argv
[0], "stat") == 0)
320 else if (strcmp (argv
[0], "cmd") == 0)
324 else if (strncmp (argv
[0], "exe", strlen (argv
[0])) == 0)
328 else if (strcmp (argv
[0], "cwd") == 0)
332 else if (strncmp (argv
[0], "all", strlen (argv
[0])) == 0)
338 /* [...] (future options here) */
343 error ("No current process: you must name one.");
345 sprintf (fname1
, "/proc/%lld", pid
);
346 if (stat (fname1
, &dummy
) != 0)
347 error ("No /proc directory: '%s'", fname1
);
349 printf_filtered ("process %lld\n", pid
);
350 if (cmdline_f
|| all
)
352 sprintf (fname1
, "/proc/%lld/cmdline", pid
);
353 if ((procfile
= fopen (fname1
, "r")) > 0)
355 fgets (buffer
, sizeof (buffer
), procfile
);
356 printf_filtered ("cmdline = '%s'\n", buffer
);
360 warning ("unable to open /proc file '%s'", fname1
);
364 sprintf (fname1
, "/proc/%lld/cwd", pid
);
365 memset (fname2
, 0, sizeof (fname2
));
366 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
367 printf_filtered ("cwd = '%s'\n", fname2
);
369 warning ("unable to read link '%s'", fname1
);
373 sprintf (fname1
, "/proc/%lld/exe", pid
);
374 memset (fname2
, 0, sizeof (fname2
));
375 if (readlink (fname1
, fname2
, sizeof (fname2
)) > 0)
376 printf_filtered ("exe = '%s'\n", fname2
);
378 warning ("unable to read link '%s'", fname1
);
380 if (mappings_f
|| all
)
382 sprintf (fname1
, "/proc/%lld/maps", pid
);
383 if ((procfile
= fopen (fname1
, "r")) > 0)
385 long long addr
, endaddr
, size
, offset
, inode
;
386 char permissions
[8], device
[8], filename
[MAXPATHLEN
];
387 char *header_fmt_string
, *data_fmt_string
;
389 if (TARGET_ADDR_BIT
== 32)
391 header_fmt_string
= "\t%10s %10s %10s %10s %7s\n";
392 data_fmt_string
= "\t%#10lx %#10lx %#10x %#10x %7s\n";
396 header_fmt_string
= " %18s %18s %10s %10s %7s\n";
397 data_fmt_string
= " %#18lx %#18lx %#10x %#10x %7s\n";
400 printf_filtered ("Mapped address spaces:\n\n");
401 printf_filtered (header_fmt_string
,
408 while (read_mapping (procfile
, &addr
, &endaddr
, &permissions
[0],
409 &offset
, &device
[0], &inode
, &filename
[0]))
411 size
= endaddr
- addr
;
412 printf_filtered (data_fmt_string
,
413 (unsigned long) addr
, /* FIXME: pr_addr */
414 (unsigned long) endaddr
,
416 (unsigned int) offset
,
417 filename
[0] ? filename
: "");
424 warning ("unable to open /proc file '%s'", fname1
);
428 sprintf (fname1
, "/proc/%lld/status", pid
);
429 if ((procfile
= fopen (fname1
, "r")) > 0)
431 while (fgets (buffer
, sizeof (buffer
), procfile
) != NULL
)
432 printf_filtered (buffer
);
436 warning ("unable to open /proc file '%s'", fname1
);
440 sprintf (fname1
, "/proc/%lld/stat", pid
);
441 if ((procfile
= fopen (fname1
, "r")) > 0)
446 if (fscanf (procfile
, "%d ", &itmp
) > 0)
447 printf_filtered ("Process: %d\n", itmp
);
448 if (fscanf (procfile
, "%s ", &buffer
[0]) > 0)
449 printf_filtered ("Exec file: %s\n", buffer
);
450 if (fscanf (procfile
, "%c ", &ctmp
) > 0)
451 printf_filtered ("State: %c\n", ctmp
);
452 if (fscanf (procfile
, "%d ", &itmp
) > 0)
453 printf_filtered ("Parent process: %d\n", itmp
);
454 if (fscanf (procfile
, "%d ", &itmp
) > 0)
455 printf_filtered ("Process group: %d\n", itmp
);
456 if (fscanf (procfile
, "%d ", &itmp
) > 0)
457 printf_filtered ("Session id: %d\n", itmp
);
458 if (fscanf (procfile
, "%d ", &itmp
) > 0)
459 printf_filtered ("TTY: %d\n", itmp
);
460 if (fscanf (procfile
, "%d ", &itmp
) > 0)
461 printf_filtered ("TTY owner process group: %d\n", itmp
);
462 if (fscanf (procfile
, "%u ", &itmp
) > 0)
463 printf_filtered ("Flags: 0x%x\n", itmp
);
464 if (fscanf (procfile
, "%u ", &itmp
) > 0)
465 printf_filtered ("Minor faults (no memory page): %u\n",
466 (unsigned int) itmp
);
467 if (fscanf (procfile
, "%u ", &itmp
) > 0)
468 printf_filtered ("Minor faults, children: %u\n",
469 (unsigned int) itmp
);
470 if (fscanf (procfile
, "%u ", &itmp
) > 0)
471 printf_filtered ("Major faults (memory page faults): %u\n",
472 (unsigned int) itmp
);
473 if (fscanf (procfile
, "%u ", &itmp
) > 0)
474 printf_filtered ("Major faults, children: %u\n",
475 (unsigned int) itmp
);
476 if (fscanf (procfile
, "%d ", &itmp
) > 0)
477 printf_filtered ("utime: %d\n", itmp
);
478 if (fscanf (procfile
, "%d ", &itmp
) > 0)
479 printf_filtered ("stime: %d\n", itmp
);
480 if (fscanf (procfile
, "%d ", &itmp
) > 0)
481 printf_filtered ("utime, children: %d\n", itmp
);
482 if (fscanf (procfile
, "%d ", &itmp
) > 0)
483 printf_filtered ("stime, children: %d\n", itmp
);
484 if (fscanf (procfile
, "%d ", &itmp
) > 0)
485 printf_filtered ("jiffies remaining in current time slice: %d\n",
487 if (fscanf (procfile
, "%d ", &itmp
) > 0)
488 printf_filtered ("'nice' value: %d\n", itmp
);
489 if (fscanf (procfile
, "%u ", &itmp
) > 0)
490 printf_filtered ("jiffies until next timeout: %u\n",
491 (unsigned int) itmp
);
492 if (fscanf (procfile
, "%u ", &itmp
) > 0)
493 printf_filtered ("jiffies until next SIGALRM: %u\n",
494 (unsigned int) itmp
);
495 if (fscanf (procfile
, "%d ", &itmp
) > 0)
496 printf_filtered ("start time (jiffies since system boot): %d\n",
498 if (fscanf (procfile
, "%u ", &itmp
) > 0)
499 printf_filtered ("Virtual memory size: %u\n",
500 (unsigned int) itmp
);
501 if (fscanf (procfile
, "%u ", &itmp
) > 0)
502 printf_filtered ("Resident set size: %u\n",
503 (unsigned int) itmp
);
504 if (fscanf (procfile
, "%u ", &itmp
) > 0)
505 printf_filtered ("rlim: %u\n",
506 (unsigned int) itmp
);
507 if (fscanf (procfile
, "%u ", &itmp
) > 0)
508 printf_filtered ("Start of text: 0x%x\n", itmp
);
509 if (fscanf (procfile
, "%u ", &itmp
) > 0)
510 printf_filtered ("End of text: 0x%x\n", itmp
);
511 if (fscanf (procfile
, "%u ", &itmp
) > 0)
512 printf_filtered ("Start of stack: 0x%x\n", itmp
);
513 #if 0 /* Don't know how architecture-dependent the rest is...
514 Anyway the signal bitmap info is available from "status". */
515 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
516 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp
);
517 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
518 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp
);
519 if (fscanf (procfile
, "%d ", &itmp
) > 0)
520 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp
);
521 if (fscanf (procfile
, "%d ", &itmp
) > 0)
522 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp
);
523 if (fscanf (procfile
, "%d ", &itmp
) > 0)
524 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp
);
525 if (fscanf (procfile
, "%d ", &itmp
) > 0)
526 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp
);
527 if (fscanf (procfile
, "%u ", &itmp
) > 0) /* FIXME arch? */
528 printf_filtered ("wchan (system call): 0x%x\n", itmp
);
533 warning ("unable to open /proc file '%s'", fname1
);
538 _initialize_linux_proc (void)
540 extern void inftarg_set_find_memory_regions ();
541 extern void inftarg_set_make_corefile_notes ();
543 inftarg_set_find_memory_regions (linux_find_memory_regions
);
544 inftarg_set_make_corefile_notes (linux_make_note_section
);
546 add_info ("proc", linux_info_proc_cmd
,
547 "Show /proc process information about any running process.\n\
548 Specify any process id, or use the program being debugged by default.\n\
549 Specify any of the following keywords for detailed info:\n\
550 mappings -- list of mapped memory regions.\n\
551 stat -- list a bunch of random process info.\n\
552 status -- list a different bunch of random process info.\n\
553 all -- list all available /proc info.");