* alpha-opc.c (alpha_opcodes): Fix thinko in ret pseudo
[deliverable/binutils-gdb.git] / gdb / linux-proc.c
1 /* Linux-specific methods for using the /proc file system.
2 Copyright 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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. */
20
21 #include "defs.h"
22 #include "inferior.h"
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 */
33
34 /* Function: child_pid_to_exec_file
35 *
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.
39 */
40
41 char *
42 child_pid_to_exec_file (int pid)
43 {
44 char *name1, *name2;
45
46 name1 = xmalloc (MAXPATHLEN);
47 name2 = xmalloc (MAXPATHLEN);
48 make_cleanup (xfree, name1);
49 make_cleanup (xfree, name2);
50 memset (name2, 0, MAXPATHLEN);
51
52 sprintf (name1, "/proc/%d/exe", pid);
53 if (readlink (name1, name2, MAXPATHLEN) > 0)
54 return name2;
55 else
56 return name1;
57 }
58
59 /* Function: read_mappings
60 *
61 * Service function for corefiles and info proc.
62 */
63
64 static int
65 read_mapping (FILE *mapfile,
66 long long *addr,
67 long long *endaddr,
68 char *permissions,
69 long long *offset,
70 char *device,
71 long long *inode,
72 char *filename)
73 {
74 int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx",
75 addr, endaddr, permissions, offset, device, inode);
76
77 if (ret > 0 && ret != EOF && *inode != 0)
78 {
79 ret += fscanf (mapfile, "%s\n", filename);
80 }
81 else
82 {
83 filename[0] = '\0'; /* no filename */
84 fscanf (mapfile, "\n");
85 }
86 return (ret != 0 && ret != EOF);
87 }
88
89 /* Function: linux_find_memory_regions
90 *
91 * Fills the "to_find_memory_regions" target vector.
92 * Lists the memory regions in the inferior for a corefile.
93 */
94
95 static int
96 linux_find_memory_regions (int (*func) (CORE_ADDR,
97 unsigned long,
98 int, int, int,
99 void *),
100 void *obfd)
101 {
102 long long pid = PIDGET (inferior_ptid);
103 char mapsfilename[MAXPATHLEN];
104 FILE *mapsfile;
105 long long addr, endaddr, size, offset, inode;
106 char permissions[8], device[8], filename[MAXPATHLEN];
107 int read, write, exec;
108 int ret;
109
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);
114
115 if (info_verbose)
116 fprintf_filtered (gdb_stdout,
117 "Reading memory regions from %s\n", mapsfilename);
118
119 /* Now iterate until end-of-file. */
120 while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0],
121 &offset, &device[0], &inode, &filename[0]))
122 {
123 size = endaddr - addr;
124
125 /* Get the segment's permissions. */
126 read = (strchr (permissions, 'r') != 0);
127 write = (strchr (permissions, 'w') != 0);
128 exec = (strchr (permissions, 'x') != 0);
129
130 if (info_verbose)
131 {
132 fprintf_filtered (gdb_stdout,
133 "Save segment, %lld bytes at 0x%s (%c%c%c)",
134 size, paddr_nz (addr),
135 read ? 'r' : ' ',
136 write ? 'w' : ' ',
137 exec ? 'x' : ' ');
138 if (filename && filename[0])
139 fprintf_filtered (gdb_stdout,
140 " for %s", filename);
141 fprintf_filtered (gdb_stdout, "\n");
142 }
143
144 /* Invoke the callback function to create the corefile segment. */
145 func (addr, size, read, write, exec, obfd);
146 }
147 fclose (mapsfile);
148 return 0;
149 }
150
151 /* Function: linux_do_thread_registers
152 *
153 * Records the thread's register state for the corefile note section.
154 */
155
156 static char *
157 linux_do_thread_registers (bfd *obfd, ptid_t ptid,
158 char *note_data, int *note_size)
159 {
160 gdb_gregset_t gregs;
161 gdb_fpregset_t fpregs;
162 unsigned long merged_pid = ptid_get_tid (ptid) << 16 | ptid_get_pid (ptid);
163
164 fill_gregset (&gregs, -1);
165 note_data = (char *) elfcore_write_prstatus (obfd,
166 note_data,
167 note_size,
168 merged_pid,
169 stop_signal,
170 &gregs);
171
172 fill_fpregset (&fpregs, -1);
173 note_data = (char *) elfcore_write_prfpreg (obfd,
174 note_data,
175 note_size,
176 &fpregs,
177 sizeof (fpregs));
178 return note_data;
179 }
180
181 struct linux_corefile_thread_data {
182 bfd *obfd;
183 char *note_data;
184 int *note_size;
185 };
186
187 /* Function: linux_corefile_thread_callback
188 *
189 * Called by gdbthread.c once per thread.
190 * Records the thread's register state for the corefile note section.
191 */
192
193 static int
194 linux_corefile_thread_callback (struct thread_info *ti, void *data)
195 {
196 struct linux_corefile_thread_data *args = data;
197 ptid_t saved_ptid = inferior_ptid;
198
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,
204 ti->ptid,
205 args->note_data,
206 args->note_size);
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. */
211 return 0;
212 }
213
214 /* Function: linux_make_note_section
215 *
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.
219 */
220
221 static char *
222 linux_make_note_section (bfd *obfd, int *note_size)
223 {
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;
230
231 if (get_exec_file (0))
232 {
233 strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname));
234 strncpy (psargs, get_exec_file (0),
235 sizeof (psargs));
236 if (get_inferior_args ())
237 {
238 strncat (psargs, " ",
239 sizeof (psargs) - strlen (psargs));
240 strncat (psargs, get_inferior_args (),
241 sizeof (psargs) - strlen (psargs));
242 }
243 note_data = (char *) elfcore_write_prpsinfo (obfd,
244 note_data,
245 note_size,
246 fname,
247 psargs);
248 }
249
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)
256 {
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);
261 }
262 else
263 {
264 note_data = thread_args.note_data;
265 }
266
267 make_cleanup (xfree, note_data);
268 return note_data;
269 }
270
271 /*
272 * Function: linux_info_proc_cmd
273 *
274 * Implement the "info proc" command.
275 */
276
277 static void
278 linux_info_proc_cmd (char *args, int from_tty)
279 {
280 long long pid = PIDGET (inferior_ptid);
281 FILE *procfile;
282 char **argv = NULL;
283 char buffer[MAXPATHLEN];
284 char fname1[MAXPATHLEN], fname2[MAXPATHLEN];
285 int cmdline_f = 1;
286 int cwd_f = 1;
287 int exe_f = 1;
288 int mappings_f = 0;
289 int environ_f = 0;
290 int status_f = 0;
291 int stat_f = 0;
292 int all = 0;
293 struct stat dummy;
294
295 if (args)
296 {
297 /* Break up 'args' into an argv array. */
298 if ((argv = buildargv (args)) == NULL)
299 nomem (0);
300 else
301 make_cleanup_freeargv (argv);
302 }
303 while (argv != NULL && *argv != NULL)
304 {
305 if (isdigit (argv[0][0]))
306 {
307 pid = strtoul (argv[0], NULL, 10);
308 }
309 else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0)
310 {
311 mappings_f = 1;
312 }
313 else if (strcmp (argv[0], "status") == 0)
314 {
315 status_f = 1;
316 }
317 else if (strcmp (argv[0], "stat") == 0)
318 {
319 stat_f = 1;
320 }
321 else if (strcmp (argv[0], "cmd") == 0)
322 {
323 cmdline_f = 1;
324 }
325 else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0)
326 {
327 exe_f = 1;
328 }
329 else if (strcmp (argv[0], "cwd") == 0)
330 {
331 cwd_f = 1;
332 }
333 else if (strncmp (argv[0], "all", strlen (argv[0])) == 0)
334 {
335 all = 1;
336 }
337 else
338 {
339 /* [...] (future options here) */
340 }
341 argv++;
342 }
343 if (pid == 0)
344 error ("No current process: you must name one.");
345
346 sprintf (fname1, "/proc/%lld", pid);
347 if (stat (fname1, &dummy) != 0)
348 error ("No /proc directory: '%s'", fname1);
349
350 printf_filtered ("process %lld\n", pid);
351 if (cmdline_f || all)
352 {
353 sprintf (fname1, "/proc/%lld/cmdline", pid);
354 if ((procfile = fopen (fname1, "r")) > 0)
355 {
356 fgets (buffer, sizeof (buffer), procfile);
357 printf_filtered ("cmdline = '%s'\n", buffer);
358 fclose (procfile);
359 }
360 else
361 warning ("unable to open /proc file '%s'", fname1);
362 }
363 if (cwd_f || all)
364 {
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);
369 else
370 warning ("unable to read link '%s'", fname1);
371 }
372 if (exe_f || all)
373 {
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);
378 else
379 warning ("unable to read link '%s'", fname1);
380 }
381 if (mappings_f || all)
382 {
383 sprintf (fname1, "/proc/%lld/maps", pid);
384 if ((procfile = fopen (fname1, "r")) > 0)
385 {
386 long long addr, endaddr, size, offset, inode;
387 char permissions[8], device[8], filename[MAXPATHLEN];
388 char *header_fmt_string, *data_fmt_string;
389
390 if (TARGET_ADDR_BIT == 32)
391 {
392 header_fmt_string = "\t%10s %10s %10s %10s %7s\n";
393 data_fmt_string = "\t%#10lx %#10lx %#10x %#10x %7s\n";
394 }
395 else
396 {
397 header_fmt_string = " %18s %18s %10s %10s %7s\n";
398 data_fmt_string = " %#18lx %#18lx %#10x %#10x %7s\n";
399 }
400
401 printf_filtered ("Mapped address spaces:\n\n");
402 printf_filtered (header_fmt_string,
403 "Start Addr",
404 " End Addr",
405 " Size",
406 " Offset",
407 "objfile");
408
409 while (read_mapping (procfile, &addr, &endaddr, &permissions[0],
410 &offset, &device[0], &inode, &filename[0]))
411 {
412 size = endaddr - addr;
413 printf_filtered (data_fmt_string,
414 (unsigned long) addr, /* FIXME: pr_addr */
415 (unsigned long) endaddr,
416 (int) size,
417 (unsigned int) offset,
418 filename[0] ? filename : "");
419
420 }
421
422 fclose (procfile);
423 }
424 else
425 warning ("unable to open /proc file '%s'", fname1);
426 }
427 if (status_f || all)
428 {
429 sprintf (fname1, "/proc/%lld/status", pid);
430 if ((procfile = fopen (fname1, "r")) > 0)
431 {
432 while (fgets (buffer, sizeof (buffer), procfile) != NULL)
433 printf_filtered (buffer);
434 fclose (procfile);
435 }
436 else
437 warning ("unable to open /proc file '%s'", fname1);
438 }
439 if (stat_f || all)
440 {
441 sprintf (fname1, "/proc/%lld/stat", pid);
442 if ((procfile = fopen (fname1, "r")) > 0)
443 {
444 int itmp;
445 char ctmp;
446
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",
487 itmp);
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",
498 itmp);
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);
530 #endif
531 fclose (procfile);
532 }
533 else
534 warning ("unable to open /proc file '%s'", fname1);
535 }
536 }
537
538 void
539 _initialize_linux_proc (void)
540 {
541 extern void inftarg_set_find_memory_regions ();
542 extern void inftarg_set_make_corefile_notes ();
543
544 inftarg_set_find_memory_regions (linux_find_memory_regions);
545 inftarg_set_make_corefile_notes (linux_make_note_section);
546
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.");
555 }
This page took 0.039669 seconds and 4 git commands to generate.