]>
Commit | Line | Data |
---|---|---|
578c1c03 MK |
1 | /* Native-dependent code for FreeBSD. |
2 | ||
3 | Copyright 2002, 2003, 2004 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 "gdbcore.h" | |
24 | #include "inferior.h" | |
25 | #include "regcache.h" | |
26 | #include "regset.h" | |
27 | ||
28 | #include "gdb_assert.h" | |
29 | #include "gdb_string.h" | |
30 | #include <sys/procfs.h> | |
31 | #include <sys/types.h> | |
32 | ||
33 | #include "elf-bfd.h" | |
34 | #include "fbsd-nat.h" | |
35 | ||
36 | /* Return a the name of file that can be opened to get the symbols for | |
37 | the child process identified by PID. */ | |
38 | ||
39 | char * | |
40 | fbsd_pid_to_exec_file (int pid) | |
41 | { | |
42 | char *path; | |
43 | char *buf; | |
44 | ||
45 | path = xstrprintf ("/proc/%d/file", pid); | |
46 | buf = xcalloc (MAXPATHLEN, sizeof (char)); | |
47 | make_cleanup (xfree, path); | |
48 | make_cleanup (xfree, buf); | |
49 | ||
50 | if (readlink (path, buf, MAXPATHLEN) > 0) | |
51 | return buf; | |
52 | ||
53 | return NULL; | |
54 | } | |
55 | ||
56 | static int | |
57 | fbsd_read_mapping (FILE *mapfile, unsigned long *start, unsigned long *end, | |
58 | char *protection) | |
59 | { | |
60 | /* FreeBSD 5.1-RELEASE uses a 256-byte buffer. */ | |
61 | char buf[256]; | |
62 | int resident, privateresident; | |
63 | unsigned long obj; | |
64 | int ret = EOF; | |
65 | ||
66 | /* As of FreeBSD 5.0-RELEASE, the layout is described in | |
67 | /usr/src/sys/fs/procfs/procfs_map.c. Somewhere in 5.1-CURRENT a | |
68 | new column was added to the procfs map. Therefore we can't use | |
69 | fscanf since we need to support older releases too. */ | |
70 | if (fgets (buf, sizeof buf, mapfile) != NULL) | |
71 | ret = sscanf (buf, "%lx %lx %d %d %lx %s", start, end, | |
72 | &resident, &privateresident, &obj, protection); | |
73 | ||
74 | return (ret != 0 && ret != EOF); | |
75 | } | |
76 | ||
77 | /* Iterate over all the memory regions in the current inferior, | |
78 | calling FUNC for each memory region. OBFD is passed as the last | |
79 | argument to FUNC. */ | |
80 | ||
81 | int | |
82 | fbsd_find_memory_regions (int (*func) (CORE_ADDR, unsigned long, | |
83 | int, int, int, void *), | |
84 | void *obfd) | |
85 | { | |
86 | pid_t pid = ptid_get_pid (inferior_ptid); | |
87 | char *mapfilename; | |
88 | FILE *mapfile; | |
89 | unsigned long start, end, size; | |
90 | char protection[4]; | |
91 | int read, write, exec; | |
92 | ||
93 | mapfilename = xstrprintf ("/proc/%ld/map", (long) pid); | |
94 | mapfile = fopen (mapfilename, "r"); | |
95 | if (mapfile == NULL) | |
8a3fe4f8 | 96 | error (_("Couldn't open %s."), mapfilename); |
578c1c03 MK |
97 | |
98 | if (info_verbose) | |
99 | fprintf_filtered (gdb_stdout, | |
100 | "Reading memory regions from %s\n", mapfilename); | |
101 | ||
102 | /* Now iterate until end-of-file. */ | |
103 | while (fbsd_read_mapping (mapfile, &start, &end, &protection[0])) | |
104 | { | |
105 | size = end - start; | |
106 | ||
107 | read = (strchr (protection, 'r') != 0); | |
108 | write = (strchr (protection, 'w') != 0); | |
109 | exec = (strchr (protection, 'x') != 0); | |
110 | ||
111 | if (info_verbose) | |
112 | { | |
113 | fprintf_filtered (gdb_stdout, | |
114 | "Save segment, %ld bytes at 0x%s (%c%c%c)\n", | |
115 | size, paddr_nz (start), | |
116 | read ? 'r' : '-', | |
117 | write ? 'w' : '-', | |
118 | exec ? 'x' : '-'); | |
119 | } | |
120 | ||
121 | /* Invoke the callback function to create the corefile segment. */ | |
122 | func (start, size, read, write, exec, obfd); | |
123 | } | |
124 | ||
125 | fclose (mapfile); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | /* Create appropriate note sections for a corefile, returning them in | |
130 | allocated memory. */ | |
131 | ||
132 | char * | |
133 | fbsd_make_corefile_notes (bfd *obfd, int *note_size) | |
134 | { | |
135 | struct gdbarch *gdbarch = current_gdbarch; | |
136 | const struct regcache *regcache = current_regcache; | |
137 | gregset_t gregs; | |
138 | fpregset_t fpregs; | |
139 | char *note_data = NULL; | |
140 | Elf_Internal_Ehdr *i_ehdrp; | |
141 | const struct regset *regset; | |
142 | size_t size; | |
143 | ||
144 | /* Put a "FreeBSD" label in the ELF header. */ | |
145 | i_ehdrp = elf_elfheader (obfd); | |
146 | i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; | |
147 | ||
148 | gdb_assert (gdbarch_regset_from_core_section_p (gdbarch)); | |
149 | ||
150 | size = sizeof gregs; | |
151 | regset = gdbarch_regset_from_core_section (gdbarch, ".reg", size); | |
152 | gdb_assert (regset && regset->collect_regset); | |
153 | regset->collect_regset (regset, regcache, -1, &gregs, size); | |
154 | ||
155 | note_data = elfcore_write_prstatus (obfd, note_data, note_size, | |
156 | ptid_get_pid (inferior_ptid), | |
157 | stop_signal, &gregs); | |
158 | ||
159 | size = sizeof fpregs; | |
160 | regset = gdbarch_regset_from_core_section (gdbarch, ".reg2", size); | |
161 | gdb_assert (regset && regset->collect_regset); | |
162 | regset->collect_regset (regset, regcache, -1, &fpregs, size); | |
163 | ||
164 | note_data = elfcore_write_prfpreg (obfd, note_data, note_size, | |
165 | &fpregs, sizeof (fpregs)); | |
166 | ||
167 | if (get_exec_file (0)) | |
168 | { | |
169 | char *fname = strrchr (get_exec_file (0), '/') + 1; | |
170 | char *psargs = xstrdup (fname); | |
171 | ||
172 | if (get_inferior_args ()) | |
173 | psargs = reconcat (psargs, psargs, " ", get_inferior_args (), NULL); | |
174 | ||
175 | note_data = elfcore_write_prpsinfo (obfd, note_data, note_size, | |
176 | fname, psargs); | |
177 | } | |
178 | ||
179 | make_cleanup (xfree, note_data); | |
180 | return note_data; | |
181 | } |