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