s/Linux/.../
[deliverable/binutils-gdb.git] / gdb / linux-proc.c
1 /* GNU/Linux specific methods for using the /proc file system.
2
3 Copyright 2001, 2002 Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "inferior.h"
24 #include <sys/param.h> /* for MAXPATHLEN */
25 #include <sys/procfs.h> /* for elf_gregset etc. */
26 #include <sys/stat.h> /* for struct stat */
27 #include <ctype.h> /* for isdigit */
28 #include "regcache.h" /* for registers_changed */
29 #include "gregset.h" /* for gregset */
30 #include "gdbcore.h" /* for get_exec_file */
31 #include "gdbthread.h" /* for struct thread_info etc. */
32 #include "elf-bfd.h" /* for elfcore_write_* */
33 #include "cli/cli-decode.h" /* for add_info */
34
35 /* Function: child_pid_to_exec_file
36 *
37 * Accepts an integer pid
38 * Returns a string representing a file that can be opened
39 * to get the symbols for the child process.
40 */
41
42 char *
43 child_pid_to_exec_file (int pid)
44 {
45 char *name1, *name2;
46
47 name1 = xmalloc (MAXPATHLEN);
48 name2 = xmalloc (MAXPATHLEN);
49 make_cleanup (xfree, name1);
50 make_cleanup (xfree, name2);
51 memset (name2, 0, MAXPATHLEN);
52
53 sprintf (name1, "/proc/%d/exe", pid);
54 if (readlink (name1, name2, MAXPATHLEN) > 0)
55 return name2;
56 else
57 return name1;
58 }
59
60 /* Function: read_mappings
61 *
62 * Service function for corefiles and info proc.
63 */
64
65 static int
66 read_mapping (FILE *mapfile,
67 long long *addr,
68 long long *endaddr,
69 char *permissions,
70 long long *offset,
71 char *device,
72 long long *inode,
73 char *filename)
74 {
75 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
76 addr, endaddr, permissions, offset, device, inode);
77
78 if (ret > 0 && ret != EOF && *inode != 0)
79 {
80 ret += fscanf (mapfile, "%s\n", filename);
81 }
82 else
83 {
84 filename[0] = '\0'; /* no filename */
85 fscanf (mapfile, "\n");
86 }
87 return (ret != 0 && ret != EOF);
88 }
89
90 /* Function: linux_find_memory_regions
91 *
92 * Fills the "to_find_memory_regions" target vector.
93 * Lists the memory regions in the inferior for a corefile.
94 */
95
96 static int
97 linux_find_memory_regions (int (*func) (CORE_ADDR,
98 unsigned long,
99 int, int, int,
100 void *),
101 void *obfd)
102 {
103 long long pid = PIDGET (inferior_ptid);
104 char mapsfilename[MAXPATHLEN];
105 FILE *mapsfile;
106 long long addr, endaddr, size, offset, inode;
107 char permissions[8], device[8], filename[MAXPATHLEN];
108 int read, write, exec;
109 int ret;
110
111 /* Compose the filename for the /proc memory map, and open it. */
112 sprintf (mapsfilename, "/proc/%lld/maps", pid);
113 if ((mapsfile = fopen (mapsfilename, "r")) == NULL)
114 error ("Could not open %s\n", mapsfilename);
115
116 if (info_verbose)
117 fprintf_filtered (gdb_stdout,
118 "Reading memory regions from %s\n", mapsfilename);
119
120 /* Now iterate until end-of-file. */
121 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
122 &offset, &device[0], &inode, &filename[0]))
123 {
124 size = endaddr - addr;
125
126 /* Get the segment's permissions. */
127 read = (strchr (permissions, 'r') != 0);
128 write = (strchr (permissions, 'w') != 0);
129 exec = (strchr (permissions, 'x') != 0);
130
131 if (info_verbose)
132 {
133 fprintf_filtered (gdb_stdout,
134 "Save segment, %lld bytes at 0x%s (%c%c%c)",
135 size, paddr_nz (addr),
136 read ? 'r' : ' ',
137 write ? 'w' : ' ',
138 exec ? 'x' : ' ');
139 if (filename && filename[0])
140 fprintf_filtered (gdb_stdout,
141 " for %s", filename);
142 fprintf_filtered (gdb_stdout, "\n");
143 }
144
145 /* Invoke the callback function to create the corefile segment. */
146 func (addr, size, read, write, exec, obfd);
147 }
148 fclose (mapsfile);
149 return 0;
150 }
151
152 /* Function: linux_do_thread_registers
153 *
154 * Records the thread's register state for the corefile note section.
155 */
156
157 static char *
158 linux_do_thread_registers (bfd *obfd, ptid_t ptid,
159 char *note_data, int *note_size)
160 {
161 gdb_gregset_t gregs;
162 gdb_fpregset_t fpregs;
163 unsigned long merged_pid = ptid_get_tid (ptid) << 16 | ptid_get_pid (ptid);
164
165 fill_gregset (&gregs, -1);
166 note_data = (char *) elfcore_write_prstatus (obfd,
167 note_data,
168 note_size,
169 merged_pid,
170 stop_signal,
171 &gregs);
172
173 fill_fpregset (&fpregs, -1);
174 note_data = (char *) elfcore_write_prfpreg (obfd,
175 note_data,
176 note_size,
177 &fpregs,
178 sizeof (fpregs));
179 return note_data;
180 }
181
182 struct linux_corefile_thread_data {
183 bfd *obfd;
184 char *note_data;
185 int *note_size;
186 };
187
188 /* Function: linux_corefile_thread_callback
189 *
190 * Called by gdbthread.c once per thread.
191 * Records the thread's register state for the corefile note section.
192 */
193
194 static int
195 linux_corefile_thread_callback (struct thread_info *ti, void *data)
196 {
197 struct linux_corefile_thread_data *args = data;
198 ptid_t saved_ptid = inferior_ptid;
199
200 inferior_ptid = ti->ptid;
201 registers_changed ();
202 target_fetch_registers (-1); /* FIXME should not be necessary;
203 fill_gregset should do it automatically. */
204 args->note_data = linux_do_thread_registers (args->obfd,
205 ti->ptid,
206 args->note_data,
207 args->note_size);
208 inferior_ptid = saved_ptid;
209 registers_changed ();
210 target_fetch_registers (-1); /* FIXME should not be necessary;
211 fill_gregset should do it automatically. */
212 return 0;
213 }
214
215 /* Function: linux_make_note_section
216 *
217 * Fills the "to_make_corefile_note" target vector.
218 * Builds the note section for a corefile, and returns it
219 * in a malloc buffer.
220 */
221
222 static char *
223 linux_make_note_section (bfd *obfd, int *note_size)
224 {
225 struct linux_corefile_thread_data thread_args;
226 struct cleanup *old_chain;
227 char fname[16] = {'\0'};
228 char psargs[80] = {'\0'};
229 char *note_data = NULL;
230 ptid_t current_ptid = inferior_ptid;
231
232 if (get_exec_file (0))
233 {
234 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
235 strncpy (psargs, get_exec_file (0),
236 sizeof (psargs));
237 if (get_inferior_args ())
238 {
239 strncat (psargs, " ",
240 sizeof (psargs) - strlen (psargs));
241 strncat (psargs, get_inferior_args (),
242 sizeof (psargs) - strlen (psargs));
243 }
244 note_data = (char *) elfcore_write_prpsinfo (obfd,
245 note_data,
246 note_size,
247 fname,
248 psargs);
249 }
250
251 /* Dump information for threads. */
252 thread_args.obfd = obfd;
253 thread_args.note_data = note_data;
254 thread_args.note_size = note_size;
255 iterate_over_threads (linux_corefile_thread_callback, &thread_args);
256 if (thread_args.note_data == note_data)
257 {
258 /* iterate_over_threads didn't come up with any threads;
259 just use inferior_ptid. */
260 note_data = linux_do_thread_registers (obfd, inferior_ptid,
261 note_data, note_size);
262 }
263 else
264 {
265 note_data = thread_args.note_data;
266 }
267
268 make_cleanup (xfree, note_data);
269 return note_data;
270 }
271
272 /*
273 * Function: linux_info_proc_cmd
274 *
275 * Implement the "info proc" command.
276 */
277
278 static void
279 linux_info_proc_cmd (char *args, int from_tty)
280 {
281 long long pid = PIDGET (inferior_ptid);
282 FILE *procfile;
283 char **argv = NULL;
284 char buffer[MAXPATHLEN];
285 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
286 int cmdline_f = 1;
287 int cwd_f = 1;
288 int exe_f = 1;
289 int mappings_f = 0;
290 int environ_f = 0;
291 int status_f = 0;
292 int stat_f = 0;
293 int all = 0;
294 struct stat dummy;
295
296 if (args)
297 {
298 /* Break up 'args' into an argv array. */
299 if ((argv = buildargv (args)) == NULL)
300 nomem (0);
301 else
302 make_cleanup_freeargv (argv);
303 }
304 while (argv != NULL && *argv != NULL)
305 {
306 if (isdigit (argv[0][0]))
307 {
308 pid = strtoul (argv[0], NULL, 10);
309 }
310 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
311 {
312 mappings_f = 1;
313 }
314 else if (strcmp (argv[0], "status") == 0)
315 {
316 status_f = 1;
317 }
318 else if (strcmp (argv[0], "stat") == 0)
319 {
320 stat_f = 1;
321 }
322 else if (strcmp (argv[0], "cmd") == 0)
323 {
324 cmdline_f = 1;
325 }
326 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
327 {
328 exe_f = 1;
329 }
330 else if (strcmp (argv[0], "cwd") == 0)
331 {
332 cwd_f = 1;
333 }
334 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
335 {
336 all = 1;
337 }
338 else
339 {
340 /* [...] (future options here) */
341 }
342 argv++;
343 }
344 if (pid == 0)
345 error ("No current process: you must name one.");
346
347 sprintf (fname1, "/proc/%lld", pid);
348 if (stat (fname1, &dummy) != 0)
349 error ("No /proc directory: '%s'", fname1);
350
351 printf_filtered ("process %lld\n", pid);
352 if (cmdline_f || all)
353 {
354 sprintf (fname1, "/proc/%lld/cmdline", pid);
355 if ((procfile = fopen (fname1, "r")) > 0)
356 {
357 fgets (buffer, sizeof (buffer), procfile);
358 printf_filtered ("cmdline = '%s'\n", buffer);
359 fclose (procfile);
360 }
361 else
362 warning ("unable to open /proc file '%s'", fname1);
363 }
364 if (cwd_f || all)
365 {
366 sprintf (fname1, "/proc/%lld/cwd", pid);
367 memset (fname2, 0, sizeof (fname2));
368 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
369 printf_filtered ("cwd = '%s'\n", fname2);
370 else
371 warning ("unable to read link '%s'", fname1);
372 }
373 if (exe_f || all)
374 {
375 sprintf (fname1, "/proc/%lld/exe", pid);
376 memset (fname2, 0, sizeof (fname2));
377 if (readlink (fname1, fname2, sizeof (fname2)) > 0)
378 printf_filtered ("exe = '%s'\n", fname2);
379 else
380 warning ("unable to read link '%s'", fname1);
381 }
382 if (mappings_f || all)
383 {
384 sprintf (fname1, "/proc/%lld/maps", pid);
385 if ((procfile = fopen (fname1, "r")) > 0)
386 {
387 long long addr, endaddr, size, offset, inode;
388 char permissions[8], device[8], filename[MAXPATHLEN];
389 char *header_fmt_string, *data_fmt_string;
390
391 if (TARGET_ADDR_BIT == 32)
392 {
393 header_fmt_string = "\t%10s %10s %10s %10s %7s\n";
394 data_fmt_string = "\t%#10lx %#10lx %#10x %#10x %7s\n";
395 }
396 else
397 {
398 header_fmt_string = " %18s %18s %10s %10s %7s\n";
399 data_fmt_string = " %#18lx %#18lx %#10x %#10x %7s\n";
400 }
401
402 printf_filtered ("Mapped address spaces:\n\n");
403 printf_filtered (header_fmt_string,
404 "Start Addr",
405 " End Addr",
406 " Size",
407 " Offset",
408 "objfile");
409
410 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
411 &offset, &device[0], &inode, &filename[0]))
412 {
413 size = endaddr - addr;
414 printf_filtered (data_fmt_string,
415 (unsigned long) addr, /* FIXME: pr_addr */
416 (unsigned long) endaddr,
417 (int) size,
418 (unsigned int) offset,
419 filename[0] ? filename : "");
420
421 }
422
423 fclose (procfile);
424 }
425 else
426 warning ("unable to open /proc file '%s'", fname1);
427 }
428 if (status_f || all)
429 {
430 sprintf (fname1, "/proc/%lld/status", pid);
431 if ((procfile = fopen (fname1, "r")) > 0)
432 {
433 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
434 printf_filtered (buffer);
435 fclose (procfile);
436 }
437 else
438 warning ("unable to open /proc file '%s'", fname1);
439 }
440 if (stat_f || all)
441 {
442 sprintf (fname1, "/proc/%lld/stat", pid);
443 if ((procfile = fopen (fname1, "r")) > 0)
444 {
445 int itmp;
446 char ctmp;
447
448 if (fscanf (procfile, "%d ", &itmp) > 0)
449 printf_filtered ("Process: %d\n", itmp);
450 if (fscanf (procfile, "%s ", &buffer[0]) > 0)
451 printf_filtered ("Exec file: %s\n", buffer);
452 if (fscanf (procfile, "%c ", &ctmp) > 0)
453 printf_filtered ("State: %c\n", ctmp);
454 if (fscanf (procfile, "%d ", &itmp) > 0)
455 printf_filtered ("Parent process: %d\n", itmp);
456 if (fscanf (procfile, "%d ", &itmp) > 0)
457 printf_filtered ("Process group: %d\n", itmp);
458 if (fscanf (procfile, "%d ", &itmp) > 0)
459 printf_filtered ("Session id: %d\n", itmp);
460 if (fscanf (procfile, "%d ", &itmp) > 0)
461 printf_filtered ("TTY: %d\n", itmp);
462 if (fscanf (procfile, "%d ", &itmp) > 0)
463 printf_filtered ("TTY owner process group: %d\n", itmp);
464 if (fscanf (procfile, "%u ", &itmp) > 0)
465 printf_filtered ("Flags: 0x%x\n", itmp);
466 if (fscanf (procfile, "%u ", &itmp) > 0)
467 printf_filtered ("Minor faults (no memory page): %u\n",
468 (unsigned int) itmp);
469 if (fscanf (procfile, "%u ", &itmp) > 0)
470 printf_filtered ("Minor faults, children: %u\n",
471 (unsigned int) itmp);
472 if (fscanf (procfile, "%u ", &itmp) > 0)
473 printf_filtered ("Major faults (memory page faults): %u\n",
474 (unsigned int) itmp);
475 if (fscanf (procfile, "%u ", &itmp) > 0)
476 printf_filtered ("Major faults, children: %u\n",
477 (unsigned int) itmp);
478 if (fscanf (procfile, "%d ", &itmp) > 0)
479 printf_filtered ("utime: %d\n", itmp);
480 if (fscanf (procfile, "%d ", &itmp) > 0)
481 printf_filtered ("stime: %d\n", itmp);
482 if (fscanf (procfile, "%d ", &itmp) > 0)
483 printf_filtered ("utime, children: %d\n", itmp);
484 if (fscanf (procfile, "%d ", &itmp) > 0)
485 printf_filtered ("stime, children: %d\n", itmp);
486 if (fscanf (procfile, "%d ", &itmp) > 0)
487 printf_filtered ("jiffies remaining in current time slice: %d\n",
488 itmp);
489 if (fscanf (procfile, "%d ", &itmp) > 0)
490 printf_filtered ("'nice' value: %d\n", itmp);
491 if (fscanf (procfile, "%u ", &itmp) > 0)
492 printf_filtered ("jiffies until next timeout: %u\n",
493 (unsigned int) itmp);
494 if (fscanf (procfile, "%u ", &itmp) > 0)
495 printf_filtered ("jiffies until next SIGALRM: %u\n",
496 (unsigned int) itmp);
497 if (fscanf (procfile, "%d ", &itmp) > 0)
498 printf_filtered ("start time (jiffies since system boot): %d\n",
499 itmp);
500 if (fscanf (procfile, "%u ", &itmp) > 0)
501 printf_filtered ("Virtual memory size: %u\n",
502 (unsigned int) itmp);
503 if (fscanf (procfile, "%u ", &itmp) > 0)
504 printf_filtered ("Resident set size: %u\n",
505 (unsigned int) itmp);
506 if (fscanf (procfile, "%u ", &itmp) > 0)
507 printf_filtered ("rlim: %u\n",
508 (unsigned int) itmp);
509 if (fscanf (procfile, "%u ", &itmp) > 0)
510 printf_filtered ("Start of text: 0x%x\n", itmp);
511 if (fscanf (procfile, "%u ", &itmp) > 0)
512 printf_filtered ("End of text: 0x%x\n", itmp);
513 if (fscanf (procfile, "%u ", &itmp) > 0)
514 printf_filtered ("Start of stack: 0x%x\n", itmp);
515 #if 0 /* Don't know how architecture-dependent the rest is...
516 Anyway the signal bitmap info is available from "status". */
517 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
518 printf_filtered ("Kernel stack pointer: 0x%x\n", itmp);
519 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
520 printf_filtered ("Kernel instr pointer: 0x%x\n", itmp);
521 if (fscanf (procfile, "%d ", &itmp) > 0)
522 printf_filtered ("Pending signals bitmap: 0x%x\n", itmp);
523 if (fscanf (procfile, "%d ", &itmp) > 0)
524 printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp);
525 if (fscanf (procfile, "%d ", &itmp) > 0)
526 printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp);
527 if (fscanf (procfile, "%d ", &itmp) > 0)
528 printf_filtered ("Catched signals bitmap: 0x%x\n", itmp);
529 if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */
530 printf_filtered ("wchan (system call): 0x%x\n", itmp);
531 #endif
532 fclose (procfile);
533 }
534 else
535 warning ("unable to open /proc file '%s'", fname1);
536 }
537 }
538
539 void
540 _initialize_linux_proc (void)
541 {
542 extern void inftarg_set_find_memory_regions ();
543 extern void inftarg_set_make_corefile_notes ();
544
545 inftarg_set_find_memory_regions (linux_find_memory_regions);
546 inftarg_set_make_corefile_notes (linux_make_note_section);
547
548 add_info ("proc", linux_info_proc_cmd,
549 "Show /proc process information about any running process.\n\
550 Specify any process id, or use the program being debugged by default.\n\
551 Specify any of the following keywords for detailed info:\n\
552 mappings -- list of mapped memory regions.\n\
553 stat -- list a bunch of random process info.\n\
554 status -- list a different bunch of random process info.\n\
555 all -- list all available /proc info.");
556 }
This page took 0.049958 seconds and 5 git commands to generate.