GDB (API)
|
00001 /* Native-dependent code for FreeBSD. 00002 00003 Copyright (C) 2002-2013 Free Software Foundation, Inc. 00004 00005 This file is part of GDB. 00006 00007 This program is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 00019 00020 #include "defs.h" 00021 #include "gdbcore.h" 00022 #include "inferior.h" 00023 #include "regcache.h" 00024 #include "regset.h" 00025 #include "gdbthread.h" 00026 00027 #include "gdb_assert.h" 00028 #include "gdb_string.h" 00029 #include <sys/types.h> 00030 #include <sys/procfs.h> 00031 #include <sys/sysctl.h> 00032 00033 #include "elf-bfd.h" 00034 #include "fbsd-nat.h" 00035 00036 /* Return the name of a file that can be opened to get the symbols for 00037 the child process identified by PID. */ 00038 00039 char * 00040 fbsd_pid_to_exec_file (int pid) 00041 { 00042 size_t len = PATH_MAX; 00043 char *buf = xcalloc (len, sizeof (char)); 00044 char *path; 00045 00046 #ifdef KERN_PROC_PATHNAME 00047 int mib[4]; 00048 00049 mib[0] = CTL_KERN; 00050 mib[1] = KERN_PROC; 00051 mib[2] = KERN_PROC_PATHNAME; 00052 mib[3] = pid; 00053 if (sysctl (mib, 4, buf, &len, NULL, 0) == 0) 00054 return buf; 00055 #endif 00056 00057 path = xstrprintf ("/proc/%d/file", pid); 00058 if (readlink (path, buf, PATH_MAX - 1) == -1) 00059 { 00060 xfree (buf); 00061 buf = NULL; 00062 } 00063 00064 xfree (path); 00065 return buf; 00066 } 00067 00068 static int 00069 fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end, 00070 char *protection) 00071 { 00072 /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */ 00073 char buf[256]; 00074 int resident, privateresident; 00075 unsigned long obj; 00076 int ret = EOF; 00077 00078 /* As of FreeBSD 5.0-RELEASE, the layout is described in 00079 /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a 00080 new column was added to the procfs map. Therefore we can't use 00081 fscanf since we need to support older releases too. */ 00082 if (fgets (buf, sizeof buf, mapfile) != NULL) 00083 ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end, 00084 &resident, &privateresident, &obj, protection); 00085 00086 return (ret != 0 && ret != EOF); 00087 } 00088 00089 /* Iterate over all the memory regions in the current inferior, 00090 calling FUNC for each memory region. OBFD is passed as the last 00091 argument to FUNC. */ 00092 00093 int 00094 fbsd_find_memory_regions (find_memory_region_ftype func, void *obfd) 00095 { 00096 pid_t pid = ptid_get_pid (inferior_ptid); 00097 char *mapfilename; 00098 FILE *mapfile; 00099 unsigned long start, end, size; 00100 char protection[4]; 00101 int read, write, exec; 00102 struct cleanup *cleanup; 00103 00104 mapfilename = xstrprintf ("/proc/%ld/map", (long) pid); 00105 cleanup = make_cleanup (xfree, mapfilename); 00106 mapfile = fopen (mapfilename, "r"); 00107 if (mapfile == NULL) 00108 error (_("Couldn't open %s."), mapfilename); 00109 make_cleanup_fclose (mapfile); 00110 00111 if (info_verbose) 00112 fprintf_filtered (gdb_stdout, 00113 "Reading memory regions from %s\n", mapfilename); 00114 00115 /* Now iterate until end-of-file. */ 00116 while (fbsd_read_mapping (mapfile, &start, &end, &protection[0])) 00117 { 00118 size = end - start; 00119 00120 read = (strchr (protection, 'r') != 0); 00121 write = (strchr (protection, 'w') != 0); 00122 exec = (strchr (protection, 'x') != 0); 00123 00124 if (info_verbose) 00125 { 00126 fprintf_filtered (gdb_stdout, 00127 "Save segment, %ld bytes at %s (%c%c%c)\n", 00128 size, paddress (target_gdbarch (), start), 00129 read ? 'r' : '-', 00130 write ? 'w' : '-', 00131 exec ? 'x' : '-'); 00132 } 00133 00134 /* Invoke the callback function to create the corefile segment. 00135 Pass MODIFIED as true, we do not know the real modification state. */ 00136 func (start, size, read, write, exec, 1, obfd); 00137 } 00138 00139 do_cleanups (cleanup); 00140 return 0; 00141 } 00142 00143 static int 00144 find_signalled_thread (struct thread_info *info, void *data) 00145 { 00146 if (info->suspend.stop_signal != GDB_SIGNAL_0 00147 && ptid_get_pid (info->ptid) == ptid_get_pid (inferior_ptid)) 00148 return 1; 00149 00150 return 0; 00151 } 00152 00153 static enum gdb_signal 00154 find_stop_signal (void) 00155 { 00156 struct thread_info *info = 00157 iterate_over_threads (find_signalled_thread, NULL); 00158 00159 if (info) 00160 return info->suspend.stop_signal; 00161 else 00162 return GDB_SIGNAL_0; 00163 } 00164 00165 /* Create appropriate note sections for a corefile, returning them in 00166 allocated memory. */ 00167 00168 char * 00169 fbsd_make_corefile_notes (bfd *obfd, int *note_size) 00170 { 00171 const struct regcache *regcache = get_current_regcache (); 00172 struct gdbarch *gdbarch = get_regcache_arch (regcache); 00173 gregset_t gregs; 00174 fpregset_t fpregs; 00175 char *note_data = NULL; 00176 Elf_Internal_Ehdr *i_ehdrp; 00177 const struct regset *regset; 00178 size_t size; 00179 00180 /* Put a "FreeBSD" label in the ELF header. */ 00181 i_ehdrp = elf_elfheader (obfd); 00182 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 00183 00184 gdb_assert (gdbarch_regset_from_core_section_p (gdbarch)); 00185 00186 size = sizeof gregs; 00187 regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size); 00188 gdb_assert (regset && regset->collect_regset); 00189 regset->collect_regset (regset, regcache, -1, &gregs, size); 00190 00191 note_data = elfcore_write_prstatus (obfd, note_data, note_size, 00192 ptid_get_pid (inferior_ptid), 00193 find_stop_signal (), &gregs); 00194 00195 size = sizeof fpregs; 00196 regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size); 00197 gdb_assert (regset && regset->collect_regset); 00198 regset->collect_regset (regset, regcache, -1, &fpregs, size); 00199 00200 note_data = elfcore_write_prfpreg (obfd, note_data, note_size, 00201 &fpregs, sizeof (fpregs)); 00202 00203 if (get_exec_file (0)) 00204 { 00205 const char *fname = lbasename (get_exec_file (0)); 00206 char *psargs = xstrdup (fname); 00207 00208 if (get_inferior_args ()) 00209 psargs = reconcat (psargs, psargs, " ", get_inferior_args (), 00210 (char *) NULL); 00211 00212 note_data = elfcore_write_prpsinfo (obfd, note_data, note_size, 00213 fname, psargs); 00214 } 00215 00216 make_cleanup (xfree, note_data); 00217 return note_data; 00218 }