From: Andrew Cagney Date: Tue, 28 Sep 2004 17:29:12 +0000 (+0000) Subject: 2004-09-28 Andrew Cagney X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=dba24537b65478ffbefcb7d0de29ab6c976b55f9;p=deliverable%2Fbinutils-gdb.git 2004-09-28 Andrew Cagney * linux-proc.c: Delete file. * Makefile.in: Update. * config/sparc/linux64.mh (NATDEPFILES): * config/sparc/linux.mh (NATDEPFILES): Update. * config/s390/s390.mh (NATDEPFILES): Update. * config/powerpc/ppc64-linux.mh (NATDEPFILES): Update. * config/powerpc/linux.mh (NATDEPFILES): Update. * config/pa/linux.mh (NATDEPFILES): Update. * config/mips/linux.mh (NATDEPFILES): Update. * config/m68k/linux.mh (NATDEPFILES): Update. * config/ia64/linux.mh (NATDEPFILES): Update. * config/i386/linux64.mh (NATDEPFILES): Update. * config/i386/linux.mh (NATDEPFILES): Update. * config/arm/linux.mh (NATDEPFILES): Update. * config/alpha/alpha-linux.mh (NATDEPFILES): Update. * linux-nat.c: Update copyright. Include , , "elf-bfd.h", "gregset.h", , "gdbthread.h", "gdb_stat.h", . (O_LARGEFILE): Possibly define. (_initialize_linux_nat, linux_proc_pending_signals) (add_line_to_sigset, linux_proc_xfer_memory) (linux_nat_info_proc_cmd, linux_nat_make_corefile_notes) (linux_nat_do_registers, linux_nat_corefile_thread_callback) (struct linux_corefile_thread_data) (linux_nat_do_thread_registers, linux_nat_find_memory_regions) (child_pid_to_exec_file): Insert code previously in linux-proc.c. --- diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 323dec4e62..0e6ae0c2b2 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,32 @@ +2004-09-28 Andrew Cagney + + * linux-proc.c: Delete file. + * Makefile.in: Update. + * config/sparc/linux64.mh (NATDEPFILES): + * config/sparc/linux.mh (NATDEPFILES): Update. + * config/s390/s390.mh (NATDEPFILES): Update. + * config/powerpc/ppc64-linux.mh (NATDEPFILES): Update. + * config/powerpc/linux.mh (NATDEPFILES): Update. + * config/pa/linux.mh (NATDEPFILES): Update. + * config/mips/linux.mh (NATDEPFILES): Update. + * config/m68k/linux.mh (NATDEPFILES): Update. + * config/ia64/linux.mh (NATDEPFILES): Update. + * config/i386/linux64.mh (NATDEPFILES): Update. + * config/i386/linux.mh (NATDEPFILES): Update. + * config/arm/linux.mh (NATDEPFILES): Update. + * config/alpha/alpha-linux.mh (NATDEPFILES): Update. + * linux-nat.c: Update copyright. Include , + , "elf-bfd.h", "gregset.h", , + "gdbthread.h", "gdb_stat.h", . + (O_LARGEFILE): Possibly define. + (_initialize_linux_nat, linux_proc_pending_signals) + (add_line_to_sigset, linux_proc_xfer_memory) + (linux_nat_info_proc_cmd, linux_nat_make_corefile_notes) + (linux_nat_do_registers, linux_nat_corefile_thread_callback) + (struct linux_corefile_thread_data) + (linux_nat_do_thread_registers, linux_nat_find_memory_regions) + (child_pid_to_exec_file): Insert code previously in linux-proc.c. + 2004-09-16 Andrew Cagney * lin-lwp.c: Delete file. diff --git a/gdb/Makefile.in b/gdb/Makefile.in index 7f84623a1c..8a2163019f 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -1669,7 +1669,8 @@ amd64nbsd-tdep.o: amd64nbsd-tdep.c $(defs_h) $(arch_utils_h) $(frame_h) \ $(gdbcore_h) $(osabi_h) $(symtab_h) $(gdb_assert_h) $(amd64_tdep_h) \ $(nbsd_tdep_h) $(solib_svr4_h) amd64obsd-nat.o: amd64obsd-nat.c $(defs_h) $(gdbcore_h) $(regcache_h) \ - $(target_h) $(gdb_assert_h) $(amd64_tdep_h) $(amd64_nat_h) $(bsd_kvm_h) + $(target_h) $(gdb_assert_h) $(amd64_tdep_h) $(amd64_nat_h) \ + $(bsd_kvm_h) amd64obsd-tdep.o: amd64obsd-tdep.c $(defs_h) $(frame_h) $(gdbcore_h) \ $(symtab_h) $(objfiles_h) $(osabi_h) $(regset_h) $(target_h) \ $(gdb_assert_h) $(gdb_string_h) $(amd64_tdep_h) $(i387_tdep_h) \ @@ -1850,8 +1851,8 @@ dwarf2read.o: dwarf2read.c $(defs_h) $(bfd_h) $(symtab_h) $(gdbtypes_h) \ $(objfiles_h) $(elf_dwarf2_h) $(buildsym_h) $(demangle_h) \ $(expression_h) $(filenames_h) $(macrotab_h) $(language_h) \ $(complaints_h) $(bcache_h) $(dwarf2expr_h) $(dwarf2loc_h) \ - $(cp_support_h) $(hashtab_h) $(gdb_string_h) $(gdb_assert_h) \ - $(command_h) $(gdbcmd_h) + $(cp_support_h) $(hashtab_h) $(command_h) $(gdbcmd_h) \ + $(gdb_string_h) $(gdb_assert_h) dwarfread.o: dwarfread.c $(defs_h) $(symtab_h) $(gdbtypes_h) $(objfiles_h) \ $(elf_dwarf_h) $(buildsym_h) $(demangle_h) $(expression_h) \ $(language_h) $(complaints_h) $(gdb_string_h) @@ -1878,8 +1879,6 @@ expprint.o: expprint.c $(defs_h) $(symtab_h) $(gdbtypes_h) $(expression_h) \ fbsd-nat.o: fbsd-nat.c $(defs_h) $(gdbcore_h) $(inferior_h) $(regcache_h) \ $(regset_h) $(gdb_assert_h) $(gdb_string_h) $(elf_bfd_h) \ $(fbsd_nat_h) -fbsd-proc.o: fbsd-proc.c $(defs_h) $(gdbcore_h) $(inferior_h) $(regcache_h) \ - $(regset_h) $(gdb_assert_h) $(gdb_string_h) $(elf_bfd_h) f-exp.o: f-exp.c $(defs_h) $(gdb_string_h) $(expression_h) $(value_h) \ $(parser_defs_h) $(language_h) $(f_lang_h) $(bfd_h) $(symfile_h) \ $(objfiles_h) $(block_h) @@ -2116,15 +2115,10 @@ linespec.o: linespec.c $(defs_h) $(symtab_h) $(frame_h) $(command_h) \ $(symfile_h) $(objfiles_h) $(source_h) $(demangle_h) $(value_h) \ $(completer_h) $(cp_abi_h) $(parser_defs_h) $(block_h) \ $(objc_lang_h) $(linespec_h) -lin-lwp.o: lin-lwp.c $(defs_h) $(gdb_assert_h) $(gdb_string_h) $(gdb_wait_h) \ - $(gdbthread_h) $(inferior_h) $(target_h) $(regcache_h) $(gdbcmd_h) \ - $(linux_nat_h) linux-nat.o: linux-nat.c $(defs_h) $(inferior_h) $(target_h) $(gdb_string_h) \ $(gdb_wait_h) $(gdb_assert_h) $(linux_nat_h) $(gdbthread_h) \ - $(gdbcmd_h) $(regcache_h) -linux-proc.o: linux-proc.c $(defs_h) $(inferior_h) $(gdb_stat_h) \ - $(regcache_h) $(gregset_h) $(gdbcore_h) $(gdbthread_h) $(elf_bfd_h) \ - $(cli_decode_h) $(gdb_string_h) $(linux_nat_h) + $(gdbcmd_h) $(regcache_h) $(elf_bfd_h) $(gregset_h) $(gdbcore_h) \ + $(gdbthread_h) $(gdb_stat_h) lynx-nat.o: lynx-nat.c $(defs_h) $(frame_h) $(inferior_h) $(target_h) \ $(gdbcore_h) $(regcache_h) m2-exp.o: m2-exp.c $(defs_h) $(gdb_string_h) $(expression_h) $(language_h) \ @@ -2686,9 +2680,11 @@ xcoffread.o: xcoffread.c $(defs_h) $(bfd_h) $(gdb_string_h) $(gdb_stat_h) \ $(complaints_h) $(gdb_stabs_h) $(aout_stab_gnu_h) xcoffsolib.o: xcoffsolib.c $(defs_h) $(bfd_h) $(xcoffsolib_h) $(inferior_h) \ $(gdbcmd_h) $(symfile_h) $(frame_h) $(gdb_regex_h) -xstormy16-tdep.o: xstormy16-tdep.c $(defs_h) $(value_h) $(inferior_h) \ - $(arch_utils_h) $(regcache_h) $(gdbcore_h) $(objfiles_h) \ - $(dis_asm_h) +xstormy16-tdep.o: xstormy16-tdep.c $(defs_h) $(frame_h) $(frame_base_h) \ + $(frame_unwind_h) $(dwarf2_frame_h) $(symtab_h) $(gdbtypes_h) \ + $(gdbcmd_h) $(gdbcore_h) $(value_h) $(dis_asm_h) $(inferior_h) \ + $(gdb_string_h) $(gdb_assert_h) $(arch_utils_h) $(floatformat_h) \ + $(regcache_h) $(doublest_h) $(osabi_h) $(objfiles_h) # # gdb/cli/ dependencies diff --git a/gdb/config/alpha/alpha-linux.mh b/gdb/config/alpha/alpha-linux.mh index a927762ce2..4983441144 100644 --- a/gdb/config/alpha/alpha-linux.mh +++ b/gdb/config/alpha/alpha-linux.mh @@ -1,6 +1,6 @@ # Host: Little-endian Alpha running Linux NAT_FILE= nm-linux.h -NATDEPFILES= infptrace.o inftarg.o corelow.o alpha-nat.o linux-proc.o \ +NATDEPFILES= infptrace.o inftarg.o corelow.o alpha-nat.o \ fork-child.o proc-service.o thread-db.o gcore.o \ linux-nat.o diff --git a/gdb/config/arm/linux.mh b/gdb/config/arm/linux.mh index aa252cf3a6..386f681cdd 100644 --- a/gdb/config/arm/linux.mh +++ b/gdb/config/arm/linux.mh @@ -2,7 +2,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= infptrace.o inftarg.o fork-child.o corelow.o \ - core-regset.o arm-linux-nat.o linux-proc.o gcore.o \ + core-regset.o arm-linux-nat.o gcore.o \ proc-service.o thread-db.o linux-nat.o LOADLIBES= -ldl -rdynamic diff --git a/gdb/config/i386/linux.mh b/gdb/config/i386/linux.mh index 146b855c9b..bfe29c123e 100644 --- a/gdb/config/i386/linux.mh +++ b/gdb/config/i386/linux.mh @@ -1,9 +1,9 @@ # Host: Intel 386 running GNU/Linux. NAT_FILE= nm-linux.h -NATDEPFILES= infptrace.o inftarg.o fork-child.o corelow.o linux-proc.o \ +NATDEPFILES= infptrace.o inftarg.o fork-child.o corelow.o \ core-aout.o i386-nat.o i386-linux-nat.o \ - proc-service.o thread-db.o linux-proc.o gcore.o \ + proc-service.o thread-db.o gcore.o \ linux-nat.o # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/i386/linux64.mh b/gdb/config/i386/linux64.mh index 9fbba56629..e9117cf041 100644 --- a/gdb/config/i386/linux64.mh +++ b/gdb/config/i386/linux64.mh @@ -1,7 +1,7 @@ # Host: GNU/Linux x86-64 NATDEPFILES= infptrace.o inftarg.o fork-child.o \ i386-nat.o amd64-nat.o amd64-linux-nat.o linux-nat.o \ - proc-service.o thread-db.o linux-proc.o gcore.o + proc-service.o thread-db.o gcore.o NAT_FILE= nm-linux64.h # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/ia64/linux.mh b/gdb/config/ia64/linux.mh index 2468c67695..5ad3145e3c 100644 --- a/gdb/config/ia64/linux.mh +++ b/gdb/config/ia64/linux.mh @@ -2,7 +2,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= infptrace.o inftarg.o fork-child.o corelow.o gcore.o \ - core-aout.o core-regset.o ia64-linux-nat.o linux-proc.o \ + core-aout.o core-regset.o ia64-linux-nat.o \ proc-service.o thread-db.o linux-nat.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/m68k/linux.mh b/gdb/config/m68k/linux.mh index 42231d97b9..9e3e898728 100644 --- a/gdb/config/m68k/linux.mh +++ b/gdb/config/m68k/linux.mh @@ -2,7 +2,7 @@ NAT_FILE= nm-linux.h NATDEPFILES= infptrace.o inftarg.o fork-child.o \ - corelow.o core-aout.o m68klinux-nat.o linux-proc.o gcore.o \ + corelow.o core-aout.o m68klinux-nat.o gcore.o \ proc-service.o thread-db.o linux-nat.o # The dynamically loaded libthread_db needs access to symbols in the diff --git a/gdb/config/mips/linux.mh b/gdb/config/mips/linux.mh index d282c96824..44bac251c2 100644 --- a/gdb/config/mips/linux.mh +++ b/gdb/config/mips/linux.mh @@ -1,7 +1,7 @@ # Host: Linux/MIPS NAT_FILE= nm-linux.h NATDEPFILES= infptrace.o inftarg.o fork-child.o mips-linux-nat.o \ - thread-db.o proc-service.o linux-proc.o gcore.o \ + thread-db.o proc-service.o gcore.o \ linux-nat.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/pa/linux.mh b/gdb/config/pa/linux.mh index 7553d2443c..bc5a00120d 100644 --- a/gdb/config/pa/linux.mh +++ b/gdb/config/pa/linux.mh @@ -2,7 +2,7 @@ XDEPFILES= NAT_FILE= nm-linux.h NATDEPFILES= infptrace.o inftarg.o fork-child.o corelow.o gcore.o \ - core-regset.o hppa-linux-nat.o linux-proc.o \ + core-regset.o hppa-linux-nat.o \ proc-service.o thread-db.o linux-nat.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/powerpc/linux.mh b/gdb/config/powerpc/linux.mh index a207d3223e..a7be1992de 100644 --- a/gdb/config/powerpc/linux.mh +++ b/gdb/config/powerpc/linux.mh @@ -3,7 +3,7 @@ XM_CLIBS= NAT_FILE= nm-linux.h -NATDEPFILES= infptrace.o inftarg.o fork-child.o linux-proc.o \ +NATDEPFILES= infptrace.o inftarg.o fork-child.o \ ppc-linux-nat.o proc-service.o thread-db.o \ gcore.o linux-nat.o diff --git a/gdb/config/powerpc/ppc64-linux.mh b/gdb/config/powerpc/ppc64-linux.mh index 4c35f3bff2..2c42fc760d 100644 --- a/gdb/config/powerpc/ppc64-linux.mh +++ b/gdb/config/powerpc/ppc64-linux.mh @@ -3,7 +3,7 @@ XM_CLIBS= NAT_FILE= nm-ppc64-linux.h -NATDEPFILES= infptrace.o inftarg.o fork-child.o linux-proc.o \ +NATDEPFILES= infptrace.o inftarg.o fork-child.o \ ppc-linux-nat.o proc-service.o thread-db.o \ gcore.o linux-nat.o diff --git a/gdb/config/s390/s390.mh b/gdb/config/s390/s390.mh index 7bf24f8dc6..2b5f1908cc 100644 --- a/gdb/config/s390/s390.mh +++ b/gdb/config/s390/s390.mh @@ -1,5 +1,5 @@ # Host: S390, running Linux NAT_FILE= nm-linux.h NATDEPFILES= infptrace.o inftarg.o fork-child.o corelow.o s390-nat.o \ - linux-proc.o gcore.o thread-db.o proc-service.o linux-nat.o + gcore.o thread-db.o proc-service.o linux-nat.o LOADLIBES = -ldl -rdynamic diff --git a/gdb/config/sparc/linux.mh b/gdb/config/sparc/linux.mh index bc0644fb48..67ed965627 100644 --- a/gdb/config/sparc/linux.mh +++ b/gdb/config/sparc/linux.mh @@ -4,7 +4,7 @@ NATDEPFILES= sparc-nat.o sparc-sol2-nat.o \ corelow.o core-regset.o fork-child.o \ infptrace.o inftarg.o \ proc-service.o thread-db.o \ - linux-proc.o gcore.o linux-nat.o + gcore.o linux-nat.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/config/sparc/linux64.mh b/gdb/config/sparc/linux64.mh index 0cc8f01b9d..5f6aed5235 100644 --- a/gdb/config/sparc/linux64.mh +++ b/gdb/config/sparc/linux64.mh @@ -4,7 +4,7 @@ NATDEPFILES= sparc-nat.o sparc64-nat.o sparc-sol2-nat.o sparc64-linux-nat.o \ corelow.o core-regset.o \ fork-child.o infptrace.o inftarg.o \ proc-service.o thread-db.o \ - linux-proc.o gcore.o linux-nat.o + gcore.o linux-nat.o # The dynamically loaded libthread_db needs access to symbols in the # gdb executable. diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c index 6207e4d370..8ecabdace6 100644 --- a/gdb/linux-nat.c +++ b/gdb/linux-nat.c @@ -1,5 +1,6 @@ /* GNU/Linux native-dependent code common to multiple platforms. - Copyright (C) 2003, 2004 Free Software Foundation, Inc. + + Copyright 2001, 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GDB. @@ -33,6 +34,19 @@ #include "gdbthread.h" #include "gdbcmd.h" #include "regcache.h" +#include /* for MAXPATHLEN */ +#include /* for elf_gregset etc. */ +#include "elf-bfd.h" /* for elfcore_write_* */ +#include "gregset.h" /* for gregset */ +#include "gdbcore.h" /* for get_exec_file */ +#include /* for isdigit */ +#include "gdbthread.h" /* for struct thread_info etc. */ +#include "gdb_stat.h" /* for struct stat */ +#include /* for O_RDONLY */ + +#ifndef O_LARGEFILE +#define O_LARGEFILE 0 +#endif /* If the system headers did not provide the constants, hard-code the normal values. */ @@ -2348,12 +2362,674 @@ sigchld_handler (int signo) arrival of a SIGCHLD. */ } +/* Accepts an integer PID; Returns a string representing a file that + can be opened to get the symbols for the child process. */ + +char * +child_pid_to_exec_file (int pid) +{ + char *name1, *name2; + + name1 = xmalloc (MAXPATHLEN); + name2 = xmalloc (MAXPATHLEN); + make_cleanup (xfree, name1); + make_cleanup (xfree, name2); + memset (name2, 0, MAXPATHLEN); + + sprintf (name1, "/proc/%d/exe", pid); + if (readlink (name1, name2, MAXPATHLEN) > 0) + return name2; + else + return name1; +} + +/* Service function for corefiles and info proc. */ + +static int +read_mapping (FILE *mapfile, + long long *addr, + long long *endaddr, + char *permissions, + long long *offset, + char *device, long long *inode, char *filename) +{ + int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx", + addr, endaddr, permissions, offset, device, inode); + + if (ret > 0 && ret != EOF && *inode != 0) + { + /* Eat everything up to EOL for the filename. This will prevent + weird filenames (such as one with embedded whitespace) from + confusing this code. It also makes this code more robust in + respect to annotations the kernel may add after the filename. + + Note the filename is used for informational purposes + only. */ + ret += fscanf (mapfile, "%[^\n]\n", filename); + } + else + { + filename[0] = '\0'; /* no filename */ + fscanf (mapfile, "\n"); + } + return (ret != 0 && ret != EOF); +} + +/* Fills the "to_find_memory_regions" target vector. Lists the memory + regions in the inferior for a corefile. */ + +static int +linux_nat_find_memory_regions (int (*func) (CORE_ADDR, + unsigned long, + int, int, int, void *), void *obfd) +{ + long long pid = PIDGET (inferior_ptid); + char mapsfilename[MAXPATHLEN]; + FILE *mapsfile; + long long addr, endaddr, size, offset, inode; + char permissions[8], device[8], filename[MAXPATHLEN]; + int read, write, exec; + int ret; + + /* Compose the filename for the /proc memory map, and open it. */ + sprintf (mapsfilename, "/proc/%lld/maps", pid); + if ((mapsfile = fopen (mapsfilename, "r")) == NULL) + error ("Could not open %s\n", mapsfilename); + + if (info_verbose) + fprintf_filtered (gdb_stdout, + "Reading memory regions from %s\n", mapsfilename); + + /* Now iterate until end-of-file. */ + while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0], + &offset, &device[0], &inode, &filename[0])) + { + size = endaddr - addr; + + /* Get the segment's permissions. */ + read = (strchr (permissions, 'r') != 0); + write = (strchr (permissions, 'w') != 0); + exec = (strchr (permissions, 'x') != 0); + + if (info_verbose) + { + fprintf_filtered (gdb_stdout, + "Save segment, %lld bytes at 0x%s (%c%c%c)", + size, paddr_nz (addr), + read ? 'r' : ' ', + write ? 'w' : ' ', exec ? 'x' : ' '); + if (filename && filename[0]) + fprintf_filtered (gdb_stdout, " for %s", filename); + fprintf_filtered (gdb_stdout, "\n"); + } + + /* Invoke the callback function to create the corefile + segment. */ + func (addr, size, read, write, exec, obfd); + } + fclose (mapsfile); + return 0; +} + +/* Records the thread's register state for the corefile note + section. */ + +static char * +linux_nat_do_thread_registers (bfd *obfd, ptid_t ptid, + char *note_data, int *note_size) +{ + gdb_gregset_t gregs; + gdb_fpregset_t fpregs; +#ifdef FILL_FPXREGSET + gdb_fpxregset_t fpxregs; +#endif + unsigned long lwp = ptid_get_lwp (ptid); + + fill_gregset (&gregs, -1); + note_data = (char *) elfcore_write_prstatus (obfd, + note_data, + note_size, + lwp, + stop_signal, &gregs); + + fill_fpregset (&fpregs, -1); + note_data = (char *) elfcore_write_prfpreg (obfd, + note_data, + note_size, + &fpregs, sizeof (fpregs)); +#ifdef FILL_FPXREGSET + fill_fpxregset (&fpxregs, -1); + note_data = (char *) elfcore_write_prxfpreg (obfd, + note_data, + note_size, + &fpxregs, sizeof (fpxregs)); +#endif + return note_data; +} + +struct linux_nat_corefile_thread_data +{ + bfd *obfd; + char *note_data; + int *note_size; + int num_notes; +}; + +/* Called by gdbthread.c once per thread. Records the thread's + register state for the corefile note section. */ + +static int +linux_nat_corefile_thread_callback (struct lwp_info *ti, void *data) +{ + struct linux_nat_corefile_thread_data *args = data; + ptid_t saved_ptid = inferior_ptid; + + inferior_ptid = ti->ptid; + registers_changed (); + target_fetch_registers (-1); /* FIXME should not be necessary; + fill_gregset should do it automatically. */ + args->note_data = linux_nat_do_thread_registers (args->obfd, + ti->ptid, + args->note_data, + args->note_size); + args->num_notes++; + inferior_ptid = saved_ptid; + registers_changed (); + target_fetch_registers (-1); /* FIXME should not be necessary; + fill_gregset should do it automatically. */ + return 0; +} + +/* Records the register state for the corefile note section. */ + +static char * +linux_nat_do_registers (bfd *obfd, ptid_t ptid, + char *note_data, int *note_size) +{ + registers_changed (); + target_fetch_registers (-1); /* FIXME should not be necessary; + fill_gregset should do it automatically. */ + return linux_nat_do_thread_registers (obfd, + ptid_build (ptid_get_pid (inferior_ptid), + ptid_get_pid (inferior_ptid), + 0), + note_data, note_size); + return note_data; +} + +/* Fills the "to_make_corefile_note" target vector. Builds the note + section for a corefile, and returns it in a malloc buffer. */ + +static char * +linux_nat_make_corefile_notes (bfd *obfd, int *note_size) +{ + struct linux_nat_corefile_thread_data thread_args; + struct cleanup *old_chain; + char fname[16] = { '\0' }; + char psargs[80] = { '\0' }; + char *note_data = NULL; + ptid_t current_ptid = inferior_ptid; + char *auxv; + int auxv_len; + + if (get_exec_file (0)) + { + strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname)); + strncpy (psargs, get_exec_file (0), sizeof (psargs)); + if (get_inferior_args ()) + { + strncat (psargs, " ", sizeof (psargs) - strlen (psargs)); + strncat (psargs, get_inferior_args (), + sizeof (psargs) - strlen (psargs)); + } + note_data = (char *) elfcore_write_prpsinfo (obfd, + note_data, + note_size, fname, psargs); + } + + /* Dump information for threads. */ + thread_args.obfd = obfd; + thread_args.note_data = note_data; + thread_args.note_size = note_size; + thread_args.num_notes = 0; + iterate_over_lwps (linux_nat_corefile_thread_callback, &thread_args); + if (thread_args.num_notes == 0) + { + /* iterate_over_threads didn't come up with any threads; just + use inferior_ptid. */ + note_data = linux_nat_do_registers (obfd, inferior_ptid, + note_data, note_size); + } + else + { + note_data = thread_args.note_data; + } + + auxv_len = target_auxv_read (¤t_target, &auxv); + if (auxv_len > 0) + { + note_data = elfcore_write_note (obfd, note_data, note_size, + "CORE", NT_AUXV, auxv, auxv_len); + xfree (auxv); + } + + make_cleanup (xfree, note_data); + return note_data; +} + +/* Implement the "info proc" command. */ + +static void +linux_nat_info_proc_cmd (char *args, int from_tty) +{ + long long pid = PIDGET (inferior_ptid); + FILE *procfile; + char **argv = NULL; + char buffer[MAXPATHLEN]; + char fname1[MAXPATHLEN], fname2[MAXPATHLEN]; + int cmdline_f = 1; + int cwd_f = 1; + int exe_f = 1; + int mappings_f = 0; + int environ_f = 0; + int status_f = 0; + int stat_f = 0; + int all = 0; + struct stat dummy; + + if (args) + { + /* Break up 'args' into an argv array. */ + if ((argv = buildargv (args)) == NULL) + nomem (0); + else + make_cleanup_freeargv (argv); + } + while (argv != NULL && *argv != NULL) + { + if (isdigit (argv[0][0])) + { + pid = strtoul (argv[0], NULL, 10); + } + else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0) + { + mappings_f = 1; + } + else if (strcmp (argv[0], "status") == 0) + { + status_f = 1; + } + else if (strcmp (argv[0], "stat") == 0) + { + stat_f = 1; + } + else if (strcmp (argv[0], "cmd") == 0) + { + cmdline_f = 1; + } + else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0) + { + exe_f = 1; + } + else if (strcmp (argv[0], "cwd") == 0) + { + cwd_f = 1; + } + else if (strncmp (argv[0], "all", strlen (argv[0])) == 0) + { + all = 1; + } + else + { + /* [...] (future options here) */ + } + argv++; + } + if (pid == 0) + error ("No current process: you must name one."); + + sprintf (fname1, "/proc/%lld", pid); + if (stat (fname1, &dummy) != 0) + error ("No /proc directory: '%s'", fname1); + + printf_filtered ("process %lld\n", pid); + if (cmdline_f || all) + { + sprintf (fname1, "/proc/%lld/cmdline", pid); + if ((procfile = fopen (fname1, "r")) > 0) + { + fgets (buffer, sizeof (buffer), procfile); + printf_filtered ("cmdline = '%s'\n", buffer); + fclose (procfile); + } + else + warning ("unable to open /proc file '%s'", fname1); + } + if (cwd_f || all) + { + sprintf (fname1, "/proc/%lld/cwd", pid); + memset (fname2, 0, sizeof (fname2)); + if (readlink (fname1, fname2, sizeof (fname2)) > 0) + printf_filtered ("cwd = '%s'\n", fname2); + else + warning ("unable to read link '%s'", fname1); + } + if (exe_f || all) + { + sprintf (fname1, "/proc/%lld/exe", pid); + memset (fname2, 0, sizeof (fname2)); + if (readlink (fname1, fname2, sizeof (fname2)) > 0) + printf_filtered ("exe = '%s'\n", fname2); + else + warning ("unable to read link '%s'", fname1); + } + if (mappings_f || all) + { + sprintf (fname1, "/proc/%lld/maps", pid); + if ((procfile = fopen (fname1, "r")) > 0) + { + long long addr, endaddr, size, offset, inode; + char permissions[8], device[8], filename[MAXPATHLEN]; + + printf_filtered ("Mapped address spaces:\n\n"); + if (TARGET_ADDR_BIT == 32) + { + printf_filtered ("\t%10s %10s %10s %10s %7s\n", + "Start Addr", + " End Addr", + " Size", " Offset", "objfile"); + } + else + { + printf_filtered (" %18s %18s %10s %10s %7s\n", + "Start Addr", + " End Addr", + " Size", " Offset", "objfile"); + } + + while (read_mapping (procfile, &addr, &endaddr, &permissions[0], + &offset, &device[0], &inode, &filename[0])) + { + size = endaddr - addr; + + /* FIXME: carlton/2003-08-27: Maybe the printf_filtered + calls here (and possibly above) should be abstracted + out into their own functions? Andrew suggests using + a generic local_address_string instead to print out + the addresses; that makes sense to me, too. */ + + if (TARGET_ADDR_BIT == 32) + { + printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n", + (unsigned long) addr, /* FIXME: pr_addr */ + (unsigned long) endaddr, + (int) size, + (unsigned int) offset, + filename[0] ? filename : ""); + } + else + { + printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n", + (unsigned long) addr, /* FIXME: pr_addr */ + (unsigned long) endaddr, + (int) size, + (unsigned int) offset, + filename[0] ? filename : ""); + } + } + + fclose (procfile); + } + else + warning ("unable to open /proc file '%s'", fname1); + } + if (status_f || all) + { + sprintf (fname1, "/proc/%lld/status", pid); + if ((procfile = fopen (fname1, "r")) > 0) + { + while (fgets (buffer, sizeof (buffer), procfile) != NULL) + puts_filtered (buffer); + fclose (procfile); + } + else + warning ("unable to open /proc file '%s'", fname1); + } + if (stat_f || all) + { + sprintf (fname1, "/proc/%lld/stat", pid); + if ((procfile = fopen (fname1, "r")) > 0) + { + int itmp; + char ctmp; + + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Process: %d\n", itmp); + if (fscanf (procfile, "%s ", &buffer[0]) > 0) + printf_filtered ("Exec file: %s\n", buffer); + if (fscanf (procfile, "%c ", &ctmp) > 0) + printf_filtered ("State: %c\n", ctmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Parent process: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Process group: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Session id: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("TTY: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("TTY owner process group: %d\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Flags: 0x%x\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Minor faults (no memory page): %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Minor faults, children: %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Major faults (memory page faults): %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Major faults, children: %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("utime: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("stime: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("utime, children: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("stime, children: %d\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("jiffies remaining in current time slice: %d\n", + itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("'nice' value: %d\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("jiffies until next timeout: %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("jiffies until next SIGALRM: %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("start time (jiffies since system boot): %d\n", + itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Virtual memory size: %u\n", + (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Resident set size: %u\n", (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("rlim: %u\n", (unsigned int) itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Start of text: 0x%x\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("End of text: 0x%x\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) + printf_filtered ("Start of stack: 0x%x\n", itmp); +#if 0 /* Don't know how architecture-dependent the rest is... + Anyway the signal bitmap info is available from "status". */ + if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ + printf_filtered ("Kernel stack pointer: 0x%x\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ + printf_filtered ("Kernel instr pointer: 0x%x\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Pending signals bitmap: 0x%x\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp); + if (fscanf (procfile, "%d ", &itmp) > 0) + printf_filtered ("Catched signals bitmap: 0x%x\n", itmp); + if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ + printf_filtered ("wchan (system call): 0x%x\n", itmp); +#endif + fclose (procfile); + } + else + warning ("unable to open /proc file '%s'", fname1); + } +} + +int +linux_proc_xfer_memory (CORE_ADDR addr, char *myaddr, int len, int write, + struct mem_attrib *attrib, struct target_ops *target) +{ + int fd, ret; + char filename[64]; + + if (write) + return 0; + + /* Don't bother for one word. */ + if (len < 3 * sizeof (long)) + return 0; + + /* We could keep this file open and cache it - possibly one per + thread. That requires some juggling, but is even faster. */ + sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid)); + fd = open (filename, O_RDONLY | O_LARGEFILE); + if (fd == -1) + return 0; + + /* If pread64 is available, use it. It's faster if the kernel + supports it (only one syscall), and it's 64-bit safe even on + 32-bit platforms (for instance, SPARC debugging a SPARC64 + application). */ +#ifdef HAVE_PREAD64 + if (pread64 (fd, myaddr, len, addr) != len) +#else + if (lseek (fd, addr, SEEK_SET) == -1 || read (fd, myaddr, len) != len) +#endif + ret = 0; + else + ret = len; + + close (fd); + return ret; +} + +/* Parse LINE as a signal set and add its set bits to SIGS. */ + +static void +add_line_to_sigset (const char *line, sigset_t *sigs) +{ + int len = strlen (line) - 1; + const char *p; + int signum; + + if (line[len] != '\n') + error ("Could not parse signal set: %s", line); + + p = line; + signum = len * 4; + while (len-- > 0) + { + int digit; + + if (*p >= '0' && *p <= '9') + digit = *p - '0'; + else if (*p >= 'a' && *p <= 'f') + digit = *p - 'a' + 10; + else + error ("Could not parse signal set: %s", line); + + signum -= 4; + + if (digit & 1) + sigaddset (sigs, signum + 1); + if (digit & 2) + sigaddset (sigs, signum + 2); + if (digit & 4) + sigaddset (sigs, signum + 3); + if (digit & 8) + sigaddset (sigs, signum + 4); + + p++; + } +} + +/* Find process PID's pending signals from /proc/pid/status and set + SIGS to match. */ + +void +linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored) +{ + FILE *procfile; + char buffer[MAXPATHLEN], fname[MAXPATHLEN]; + int signum; + + sigemptyset (pending); + sigemptyset (blocked); + sigemptyset (ignored); + sprintf (fname, "/proc/%d/status", pid); + procfile = fopen (fname, "r"); + if (procfile == NULL) + error ("Could not open %s", fname); + + while (fgets (buffer, MAXPATHLEN, procfile) != NULL) + { + /* Normal queued signals are on the SigPnd line in the status + file. However, 2.6 kernels also have a "shared" pending + queue for delivering signals to a thread group, so check for + a ShdPnd line also. + + Unfortunately some Red Hat kernels include the shared pending + queue but not the ShdPnd status field. */ + + if (strncmp (buffer, "SigPnd:\t", 8) == 0) + add_line_to_sigset (buffer + 8, pending); + else if (strncmp (buffer, "ShdPnd:\t", 8) == 0) + add_line_to_sigset (buffer + 8, pending); + else if (strncmp (buffer, "SigBlk:\t", 8) == 0) + add_line_to_sigset (buffer + 8, blocked); + else if (strncmp (buffer, "SigIgn:\t", 8) == 0) + add_line_to_sigset (buffer + 8, ignored); + } + + fclose (procfile); +} + void _initialize_linux_nat (void) { struct sigaction action; - extern void thread_db_init (struct target_ops *); + extern void inftarg_set_find_memory_regions (); + extern void inftarg_set_make_corefile_notes (); + + inftarg_set_find_memory_regions (linux_nat_find_memory_regions); + inftarg_set_make_corefile_notes (linux_nat_make_corefile_notes); + + add_info ("proc", linux_nat_info_proc_cmd, + "Show /proc process information about any running process.\n\ +Specify any process id, or use the program being debugged by default.\n\ +Specify any of the following keywords for detailed info:\n\ + mappings -- list of mapped memory regions.\n\ + stat -- list a bunch of random process info.\n\ + status -- list a different bunch of random process info.\n\ + all -- list all available /proc info."); init_linux_nat_ops (); add_target (&linux_nat_ops); diff --git a/gdb/linux-proc.c b/gdb/linux-proc.c deleted file mode 100644 index 0a774593ed..0000000000 --- a/gdb/linux-proc.c +++ /dev/null @@ -1,737 +0,0 @@ -/* GNU/Linux specific methods for using the /proc file system. - - Copyright 2001, 2002 Free Software Foundation, Inc. - - This file is part of GDB. - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. */ - -#include "defs.h" -#include "inferior.h" -#include /* for MAXPATHLEN */ -#include /* for elf_gregset etc. */ -#include "gdb_stat.h" /* for struct stat */ -#include /* for isdigit */ -#include /* for open, pread64 */ -#include /* for O_RDONLY */ -#include "regcache.h" /* for registers_changed */ -#include "gregset.h" /* for gregset */ -#include "gdbcore.h" /* for get_exec_file */ -#include "gdbthread.h" /* for struct thread_info etc. */ -#include "elf-bfd.h" /* for elfcore_write_* */ -#include "cli/cli-decode.h" /* for add_info */ -#include "gdb_string.h" - -#include - -#include "linux-nat.h" - -#ifndef O_LARGEFILE -#define O_LARGEFILE 0 -#endif - -/* Function: child_pid_to_exec_file - * - * Accepts an integer pid - * Returns a string representing a file that can be opened - * to get the symbols for the child process. - */ - -char * -child_pid_to_exec_file (int pid) -{ - char *name1, *name2; - - name1 = xmalloc (MAXPATHLEN); - name2 = xmalloc (MAXPATHLEN); - make_cleanup (xfree, name1); - make_cleanup (xfree, name2); - memset (name2, 0, MAXPATHLEN); - - sprintf (name1, "/proc/%d/exe", pid); - if (readlink (name1, name2, MAXPATHLEN) > 0) - return name2; - else - return name1; -} - -/* Function: read_mappings - * - * Service function for corefiles and info proc. - */ - -static int -read_mapping (FILE *mapfile, - long long *addr, - long long *endaddr, - char *permissions, - long long *offset, - char *device, long long *inode, char *filename) -{ - int ret = fscanf (mapfile, "%llx-%llx %s %llx %s %llx", - addr, endaddr, permissions, offset, device, inode); - - if (ret > 0 && ret != EOF && *inode != 0) - { - /* Eat everything up to EOL for the filename. This will prevent - weird filenames (such as one with embedded whitespace) from - confusing this code. It also makes this code more robust - in respect to annotations the kernel may add after the - filename. - - Note the filename is used for informational purposes only. */ - ret += fscanf (mapfile, "%[^\n]\n", filename); - } - else - { - filename[0] = '\0'; /* no filename */ - fscanf (mapfile, "\n"); - } - return (ret != 0 && ret != EOF); -} - -/* Function: linux_find_memory_regions - * - * Fills the "to_find_memory_regions" target vector. - * Lists the memory regions in the inferior for a corefile. - */ - -static int -linux_find_memory_regions (int (*func) (CORE_ADDR, - unsigned long, - int, int, int, void *), void *obfd) -{ - long long pid = PIDGET (inferior_ptid); - char mapsfilename[MAXPATHLEN]; - FILE *mapsfile; - long long addr, endaddr, size, offset, inode; - char permissions[8], device[8], filename[MAXPATHLEN]; - int read, write, exec; - int ret; - - /* Compose the filename for the /proc memory map, and open it. */ - sprintf (mapsfilename, "/proc/%lld/maps", pid); - if ((mapsfile = fopen (mapsfilename, "r")) == NULL) - error ("Could not open %s\n", mapsfilename); - - if (info_verbose) - fprintf_filtered (gdb_stdout, - "Reading memory regions from %s\n", mapsfilename); - - /* Now iterate until end-of-file. */ - while (read_mapping (mapsfile, &addr, &endaddr, &permissions[0], - &offset, &device[0], &inode, &filename[0])) - { - size = endaddr - addr; - - /* Get the segment's permissions. */ - read = (strchr (permissions, 'r') != 0); - write = (strchr (permissions, 'w') != 0); - exec = (strchr (permissions, 'x') != 0); - - if (info_verbose) - { - fprintf_filtered (gdb_stdout, - "Save segment, %lld bytes at 0x%s (%c%c%c)", - size, paddr_nz (addr), - read ? 'r' : ' ', - write ? 'w' : ' ', exec ? 'x' : ' '); - if (filename && filename[0]) - fprintf_filtered (gdb_stdout, " for %s", filename); - fprintf_filtered (gdb_stdout, "\n"); - } - - /* Invoke the callback function to create the corefile segment. */ - func (addr, size, read, write, exec, obfd); - } - fclose (mapsfile); - return 0; -} - -/* Function: linux_do_thread_registers - * - * Records the thread's register state for the corefile note section. - */ - -static char * -linux_do_thread_registers (bfd *obfd, ptid_t ptid, - char *note_data, int *note_size) -{ - gdb_gregset_t gregs; - gdb_fpregset_t fpregs; -#ifdef FILL_FPXREGSET - gdb_fpxregset_t fpxregs; -#endif - unsigned long lwp = ptid_get_lwp (ptid); - - fill_gregset (&gregs, -1); - note_data = (char *) elfcore_write_prstatus (obfd, - note_data, - note_size, - lwp, - stop_signal, &gregs); - - fill_fpregset (&fpregs, -1); - note_data = (char *) elfcore_write_prfpreg (obfd, - note_data, - note_size, - &fpregs, sizeof (fpregs)); -#ifdef FILL_FPXREGSET - fill_fpxregset (&fpxregs, -1); - note_data = (char *) elfcore_write_prxfpreg (obfd, - note_data, - note_size, - &fpxregs, sizeof (fpxregs)); -#endif - return note_data; -} - -struct linux_corefile_thread_data -{ - bfd *obfd; - char *note_data; - int *note_size; - int num_notes; -}; - -/* Function: linux_corefile_thread_callback - * - * Called by gdbthread.c once per thread. - * Records the thread's register state for the corefile note section. - */ - -static int -linux_corefile_thread_callback (struct lwp_info *ti, void *data) -{ - struct linux_corefile_thread_data *args = data; - ptid_t saved_ptid = inferior_ptid; - - inferior_ptid = ti->ptid; - registers_changed (); - target_fetch_registers (-1); /* FIXME should not be necessary; - fill_gregset should do it automatically. */ - args->note_data = linux_do_thread_registers (args->obfd, - ti->ptid, - args->note_data, - args->note_size); - args->num_notes++; - inferior_ptid = saved_ptid; - registers_changed (); - target_fetch_registers (-1); /* FIXME should not be necessary; - fill_gregset should do it automatically. */ - return 0; -} - -/* Function: linux_do_registers - * - * Records the register state for the corefile note section. - */ - -static char * -linux_do_registers (bfd *obfd, ptid_t ptid, - char *note_data, int *note_size) -{ - registers_changed (); - target_fetch_registers (-1); /* FIXME should not be necessary; - fill_gregset should do it automatically. */ - return linux_do_thread_registers (obfd, - ptid_build (ptid_get_pid (inferior_ptid), - ptid_get_pid (inferior_ptid), - 0), - note_data, note_size); - return note_data; -} - -/* Function: linux_make_note_section - * - * Fills the "to_make_corefile_note" target vector. - * Builds the note section for a corefile, and returns it - * in a malloc buffer. - */ - -static char * -linux_make_note_section (bfd *obfd, int *note_size) -{ - struct linux_corefile_thread_data thread_args; - struct cleanup *old_chain; - char fname[16] = { '\0' }; - char psargs[80] = { '\0' }; - char *note_data = NULL; - ptid_t current_ptid = inferior_ptid; - char *auxv; - int auxv_len; - - if (get_exec_file (0)) - { - strncpy (fname, strrchr (get_exec_file (0), '/') + 1, sizeof (fname)); - strncpy (psargs, get_exec_file (0), sizeof (psargs)); - if (get_inferior_args ()) - { - strncat (psargs, " ", sizeof (psargs) - strlen (psargs)); - strncat (psargs, get_inferior_args (), - sizeof (psargs) - strlen (psargs)); - } - note_data = (char *) elfcore_write_prpsinfo (obfd, - note_data, - note_size, fname, psargs); - } - - /* Dump information for threads. */ - thread_args.obfd = obfd; - thread_args.note_data = note_data; - thread_args.note_size = note_size; - thread_args.num_notes = 0; - iterate_over_lwps (linux_corefile_thread_callback, &thread_args); - if (thread_args.num_notes == 0) - { - /* iterate_over_threads didn't come up with any threads; - just use inferior_ptid. */ - note_data = linux_do_registers (obfd, inferior_ptid, - note_data, note_size); - } - else - { - note_data = thread_args.note_data; - } - - auxv_len = target_auxv_read (¤t_target, &auxv); - if (auxv_len > 0) - { - note_data = elfcore_write_note (obfd, note_data, note_size, - "CORE", NT_AUXV, auxv, auxv_len); - xfree (auxv); - } - - make_cleanup (xfree, note_data); - return note_data; -} - -/* - * Function: linux_info_proc_cmd - * - * Implement the "info proc" command. - */ - -static void -linux_info_proc_cmd (char *args, int from_tty) -{ - long long pid = PIDGET (inferior_ptid); - FILE *procfile; - char **argv = NULL; - char buffer[MAXPATHLEN]; - char fname1[MAXPATHLEN], fname2[MAXPATHLEN]; - int cmdline_f = 1; - int cwd_f = 1; - int exe_f = 1; - int mappings_f = 0; - int environ_f = 0; - int status_f = 0; - int stat_f = 0; - int all = 0; - struct stat dummy; - - if (args) - { - /* Break up 'args' into an argv array. */ - if ((argv = buildargv (args)) == NULL) - nomem (0); - else - make_cleanup_freeargv (argv); - } - while (argv != NULL && *argv != NULL) - { - if (isdigit (argv[0][0])) - { - pid = strtoul (argv[0], NULL, 10); - } - else if (strncmp (argv[0], "mappings", strlen (argv[0])) == 0) - { - mappings_f = 1; - } - else if (strcmp (argv[0], "status") == 0) - { - status_f = 1; - } - else if (strcmp (argv[0], "stat") == 0) - { - stat_f = 1; - } - else if (strcmp (argv[0], "cmd") == 0) - { - cmdline_f = 1; - } - else if (strncmp (argv[0], "exe", strlen (argv[0])) == 0) - { - exe_f = 1; - } - else if (strcmp (argv[0], "cwd") == 0) - { - cwd_f = 1; - } - else if (strncmp (argv[0], "all", strlen (argv[0])) == 0) - { - all = 1; - } - else - { - /* [...] (future options here) */ - } - argv++; - } - if (pid == 0) - error ("No current process: you must name one."); - - sprintf (fname1, "/proc/%lld", pid); - if (stat (fname1, &dummy) != 0) - error ("No /proc directory: '%s'", fname1); - - printf_filtered ("process %lld\n", pid); - if (cmdline_f || all) - { - sprintf (fname1, "/proc/%lld/cmdline", pid); - if ((procfile = fopen (fname1, "r")) > 0) - { - fgets (buffer, sizeof (buffer), procfile); - printf_filtered ("cmdline = '%s'\n", buffer); - fclose (procfile); - } - else - warning ("unable to open /proc file '%s'", fname1); - } - if (cwd_f || all) - { - sprintf (fname1, "/proc/%lld/cwd", pid); - memset (fname2, 0, sizeof (fname2)); - if (readlink (fname1, fname2, sizeof (fname2)) > 0) - printf_filtered ("cwd = '%s'\n", fname2); - else - warning ("unable to read link '%s'", fname1); - } - if (exe_f || all) - { - sprintf (fname1, "/proc/%lld/exe", pid); - memset (fname2, 0, sizeof (fname2)); - if (readlink (fname1, fname2, sizeof (fname2)) > 0) - printf_filtered ("exe = '%s'\n", fname2); - else - warning ("unable to read link '%s'", fname1); - } - if (mappings_f || all) - { - sprintf (fname1, "/proc/%lld/maps", pid); - if ((procfile = fopen (fname1, "r")) > 0) - { - long long addr, endaddr, size, offset, inode; - char permissions[8], device[8], filename[MAXPATHLEN]; - - printf_filtered ("Mapped address spaces:\n\n"); - if (TARGET_ADDR_BIT == 32) - { - printf_filtered ("\t%10s %10s %10s %10s %7s\n", - "Start Addr", - " End Addr", - " Size", " Offset", "objfile"); - } - else - { - printf_filtered (" %18s %18s %10s %10s %7s\n", - "Start Addr", - " End Addr", - " Size", " Offset", "objfile"); - } - - while (read_mapping (procfile, &addr, &endaddr, &permissions[0], - &offset, &device[0], &inode, &filename[0])) - { - size = endaddr - addr; - - /* FIXME: carlton/2003-08-27: Maybe the printf_filtered - calls here (and possibly above) should be abstracted - out into their own functions? Andrew suggests using - a generic local_address_string instead to print out - the addresses; that makes sense to me, too. */ - - if (TARGET_ADDR_BIT == 32) - { - printf_filtered ("\t%#10lx %#10lx %#10x %#10x %7s\n", - (unsigned long) addr, /* FIXME: pr_addr */ - (unsigned long) endaddr, - (int) size, - (unsigned int) offset, - filename[0] ? filename : ""); - } - else - { - printf_filtered (" %#18lx %#18lx %#10x %#10x %7s\n", - (unsigned long) addr, /* FIXME: pr_addr */ - (unsigned long) endaddr, - (int) size, - (unsigned int) offset, - filename[0] ? filename : ""); - } - } - - fclose (procfile); - } - else - warning ("unable to open /proc file '%s'", fname1); - } - if (status_f || all) - { - sprintf (fname1, "/proc/%lld/status", pid); - if ((procfile = fopen (fname1, "r")) > 0) - { - while (fgets (buffer, sizeof (buffer), procfile) != NULL) - puts_filtered (buffer); - fclose (procfile); - } - else - warning ("unable to open /proc file '%s'", fname1); - } - if (stat_f || all) - { - sprintf (fname1, "/proc/%lld/stat", pid); - if ((procfile = fopen (fname1, "r")) > 0) - { - int itmp; - char ctmp; - - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Process: %d\n", itmp); - if (fscanf (procfile, "%s ", &buffer[0]) > 0) - printf_filtered ("Exec file: %s\n", buffer); - if (fscanf (procfile, "%c ", &ctmp) > 0) - printf_filtered ("State: %c\n", ctmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Parent process: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Process group: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Session id: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("TTY: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("TTY owner process group: %d\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Flags: 0x%x\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Minor faults (no memory page): %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Minor faults, children: %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Major faults (memory page faults): %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Major faults, children: %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("utime: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("stime: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("utime, children: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("stime, children: %d\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("jiffies remaining in current time slice: %d\n", - itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("'nice' value: %d\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("jiffies until next timeout: %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("jiffies until next SIGALRM: %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("start time (jiffies since system boot): %d\n", - itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Virtual memory size: %u\n", - (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Resident set size: %u\n", (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("rlim: %u\n", (unsigned int) itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Start of text: 0x%x\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("End of text: 0x%x\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) - printf_filtered ("Start of stack: 0x%x\n", itmp); -#if 0 /* Don't know how architecture-dependent the rest is... - Anyway the signal bitmap info is available from "status". */ - if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ - printf_filtered ("Kernel stack pointer: 0x%x\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ - printf_filtered ("Kernel instr pointer: 0x%x\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Pending signals bitmap: 0x%x\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Blocked signals bitmap: 0x%x\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Ignored signals bitmap: 0x%x\n", itmp); - if (fscanf (procfile, "%d ", &itmp) > 0) - printf_filtered ("Catched signals bitmap: 0x%x\n", itmp); - if (fscanf (procfile, "%u ", &itmp) > 0) /* FIXME arch? */ - printf_filtered ("wchan (system call): 0x%x\n", itmp); -#endif - fclose (procfile); - } - else - warning ("unable to open /proc file '%s'", fname1); - } -} - -void -_initialize_linux_proc (void) -{ - extern void inftarg_set_find_memory_regions (); - extern void inftarg_set_make_corefile_notes (); - - inftarg_set_find_memory_regions (linux_find_memory_regions); - inftarg_set_make_corefile_notes (linux_make_note_section); - - add_info ("proc", linux_info_proc_cmd, - "Show /proc process information about any running process.\n\ -Specify any process id, or use the program being debugged by default.\n\ -Specify any of the following keywords for detailed info:\n\ - mappings -- list of mapped memory regions.\n\ - stat -- list a bunch of random process info.\n\ - status -- list a different bunch of random process info.\n\ - all -- list all available /proc info."); -} - -int -linux_proc_xfer_memory (CORE_ADDR addr, char *myaddr, int len, int write, - struct mem_attrib *attrib, struct target_ops *target) -{ - int fd, ret; - char filename[64]; - - if (write) - return 0; - - /* Don't bother for one word. */ - if (len < 3 * sizeof (long)) - return 0; - - /* We could keep this file open and cache it - possibly one - per thread. That requires some juggling, but is even faster. */ - sprintf (filename, "/proc/%d/mem", PIDGET (inferior_ptid)); - fd = open (filename, O_RDONLY | O_LARGEFILE); - if (fd == -1) - return 0; - - /* If pread64 is available, use it. It's faster if the kernel - supports it (only one syscall), and it's 64-bit safe even - on 32-bit platforms (for instance, SPARC debugging a SPARC64 - application). */ -#ifdef HAVE_PREAD64 - if (pread64 (fd, myaddr, len, addr) != len) -#else - if (lseek (fd, addr, SEEK_SET) == -1 || read (fd, myaddr, len) != len) -#endif - ret = 0; - else - ret = len; - - close (fd); - return ret; -} - -/* Parse LINE as a signal set and add its set bits to SIGS. */ - -static void -linux_proc_add_line_to_sigset (const char *line, sigset_t *sigs) -{ - int len = strlen (line) - 1; - const char *p; - int signum; - - if (line[len] != '\n') - error ("Could not parse signal set: %s", line); - - p = line; - signum = len * 4; - while (len-- > 0) - { - int digit; - - if (*p >= '0' && *p <= '9') - digit = *p - '0'; - else if (*p >= 'a' && *p <= 'f') - digit = *p - 'a' + 10; - else - error ("Could not parse signal set: %s", line); - - signum -= 4; - - if (digit & 1) - sigaddset (sigs, signum + 1); - if (digit & 2) - sigaddset (sigs, signum + 2); - if (digit & 4) - sigaddset (sigs, signum + 3); - if (digit & 8) - sigaddset (sigs, signum + 4); - - p++; - } -} - -/* Find process PID's pending signals from /proc/pid/status and set SIGS - to match. */ - -void -linux_proc_pending_signals (int pid, sigset_t *pending, sigset_t *blocked, sigset_t *ignored) -{ - FILE *procfile; - char buffer[MAXPATHLEN], fname[MAXPATHLEN]; - int signum; - - sigemptyset (pending); - sigemptyset (blocked); - sigemptyset (ignored); - sprintf (fname, "/proc/%d/status", pid); - procfile = fopen (fname, "r"); - if (procfile == NULL) - error ("Could not open %s", fname); - - while (fgets (buffer, MAXPATHLEN, procfile) != NULL) - { - /* Normal queued signals are on the SigPnd line in the status - file. However, 2.6 kernels also have a "shared" pending queue - for delivering signals to a thread group, so check for a ShdPnd - line also. - - Unfortunately some Red Hat kernels include the shared pending queue - but not the ShdPnd status field. */ - - if (strncmp (buffer, "SigPnd:\t", 8) == 0) - linux_proc_add_line_to_sigset (buffer + 8, pending); - else if (strncmp (buffer, "ShdPnd:\t", 8) == 0) - linux_proc_add_line_to_sigset (buffer + 8, pending); - else if (strncmp (buffer, "SigBlk:\t", 8) == 0) - linux_proc_add_line_to_sigset (buffer + 8, blocked); - else if (strncmp (buffer, "SigIgn:\t", 8) == 0) - linux_proc_add_line_to_sigset (buffer + 8, ignored); - } - - fclose (procfile); -}