elfcore.c (197437) | elfcore.c (199805) |
---|---|
1/*- | 1/*- |
2 * Copyright (c) 2007 Sandvine Incorporated |
|
2 * Copyright (c) 1998 John D. Polstra 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. --- 10 unchanged lines hidden (view full) --- 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27#include <sys/cdefs.h> | 3 * Copyright (c) 1998 John D. Polstra 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. --- 10 unchanged lines hidden (view full) --- 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/usr.bin/gcore/elfcore.c 197437 2009-09-23 15:32:59Z emaste $"); | 29__FBSDID("$FreeBSD: head/usr.bin/gcore/elfcore.c 199805 2009-11-25 15:23:14Z attilio $"); |
29 30#include <sys/param.h> 31#include <sys/procfs.h> | 30 31#include <sys/param.h> 32#include <sys/procfs.h> |
33#include <sys/ptrace.h> |
|
32#include <sys/queue.h> 33#include <sys/linker_set.h> | 34#include <sys/queue.h> 35#include <sys/linker_set.h> |
36#include <sys/sysctl.h> 37#include <sys/user.h> 38#include <sys/wait.h> |
|
34#include <machine/elf.h> 35#include <vm/vm_param.h> 36#include <vm/vm.h> 37#include <vm/pmap.h> 38#include <vm/vm_map.h> 39#include <err.h> 40#include <errno.h> 41#include <fcntl.h> 42#include <stdint.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <string.h> 46#include <unistd.h> | 39#include <machine/elf.h> 40#include <vm/vm_param.h> 41#include <vm/vm.h> 42#include <vm/pmap.h> 43#include <vm/vm_map.h> 44#include <err.h> 45#include <errno.h> 46#include <fcntl.h> 47#include <stdint.h> 48#include <stdio.h> 49#include <stdlib.h> 50#include <string.h> 51#include <unistd.h> |
52#include <libutil.h> |
|
47 48#include "extern.h" 49 50/* 51 * Code for generating ELF core dumps. 52 */ 53 54typedef void (*segment_callback)(vm_map_entry_t, void *); --- 9 unchanged lines hidden (view full) --- 64 int count; /* Count of writable segments. */ 65 size_t size; /* Total size of all writable segments. */ 66}; 67 68static void cb_put_phdr(vm_map_entry_t, void *); 69static void cb_size_segment(vm_map_entry_t, void *); 70static void each_writable_segment(vm_map_entry_t, segment_callback, 71 void *closure); | 53 54#include "extern.h" 55 56/* 57 * Code for generating ELF core dumps. 58 */ 59 60typedef void (*segment_callback)(vm_map_entry_t, void *); --- 9 unchanged lines hidden (view full) --- 70 int count; /* Count of writable segments. */ 71 size_t size; /* Total size of all writable segments. */ 72}; 73 74static void cb_put_phdr(vm_map_entry_t, void *); 75static void cb_size_segment(vm_map_entry_t, void *); 76static void each_writable_segment(vm_map_entry_t, segment_callback, 77 void *closure); |
72static void elf_corehdr(int fd, pid_t, vm_map_entry_t, int numsegs, 73 void *hdr, size_t hdrsize); 74static void elf_puthdr(vm_map_entry_t, void *, size_t *, 75 const prstatus_t *, const prfpregset_t *, const prpsinfo_t *, int numsegs); | 78static void elf_detach(void); /* atexit() handler. */ 79static void elf_puthdr(pid_t, vm_map_entry_t, void *, size_t *, int numsegs); |
76static void elf_putnote(void *dst, size_t *off, const char *name, int type, 77 const void *desc, size_t descsz); 78static void freemap(vm_map_entry_t); | 80static void elf_putnote(void *dst, size_t *off, const char *name, int type, 81 const void *desc, size_t descsz); 82static void freemap(vm_map_entry_t); |
79static void readhdrinfo(pid_t, prstatus_t *, prfpregset_t *, prpsinfo_t *); | |
80static vm_map_entry_t readmap(pid_t); 81 | 83static vm_map_entry_t readmap(pid_t); 84 |
85static pid_t g_pid; /* Pid being dumped, global for elf_detach */ 86 |
|
82static int 83elf_ident(int efd, pid_t pid __unused, char *binfile __unused) 84{ 85 Elf_Ehdr hdr; 86 int cnt; 87 88 cnt = read(efd, &hdr, sizeof(hdr)); 89 if (cnt != sizeof(hdr)) 90 return (0); 91 if (IS_ELF(hdr)) 92 return (1); 93 return (0); 94} 95 | 87static int 88elf_ident(int efd, pid_t pid __unused, char *binfile __unused) 89{ 90 Elf_Ehdr hdr; 91 int cnt; 92 93 cnt = read(efd, &hdr, sizeof(hdr)); 94 if (cnt != sizeof(hdr)) 95 return (0); 96 if (IS_ELF(hdr)) 97 return (1); 98 return (0); 99} 100 |
101static void 102elf_detach(void) 103{ 104 105 if (g_pid != 0) 106 ptrace(PT_DETACH, g_pid, (caddr_t)1, 0); 107} 108 |
|
96/* 97 * Write an ELF coredump for the given pid to the given fd. 98 */ 99static void 100elf_coredump(int efd __unused, int fd, pid_t pid) 101{ 102 vm_map_entry_t map; 103 struct sseg_closure seginfo; 104 void *hdr; 105 size_t hdrsize; | 109/* 110 * Write an ELF coredump for the given pid to the given fd. 111 */ 112static void 113elf_coredump(int efd __unused, int fd, pid_t pid) 114{ 115 vm_map_entry_t map; 116 struct sseg_closure seginfo; 117 void *hdr; 118 size_t hdrsize; |
106 char memname[64]; 107 int memfd; | |
108 Elf_Phdr *php; 109 int i; 110 | 119 Elf_Phdr *php; 120 int i; 121 |
122 /* Attach to process to dump. */ 123 g_pid = pid; 124 if (atexit(elf_detach) != 0) 125 err(1, "atexit"); 126 errno = 0; 127 ptrace(PT_ATTACH, pid, NULL, 0); 128 if (errno) 129 err(1, "PT_ATTACH"); 130 if (waitpid(pid, NULL, 0) == -1) 131 err(1, "waitpid"); 132 |
|
111 /* Get the program's memory map. */ 112 map = readmap(pid); 113 114 /* Size the program segments. */ 115 seginfo.count = 0; 116 seginfo.size = 0; 117 each_writable_segment(map, cb_size_segment, &seginfo); 118 119 /* 120 * Calculate the size of the core file header area by making 121 * a dry run of generating it. Nothing is written, but the 122 * size is calculated. 123 */ 124 hdrsize = 0; | 133 /* Get the program's memory map. */ 134 map = readmap(pid); 135 136 /* Size the program segments. */ 137 seginfo.count = 0; 138 seginfo.size = 0; 139 each_writable_segment(map, cb_size_segment, &seginfo); 140 141 /* 142 * Calculate the size of the core file header area by making 143 * a dry run of generating it. Nothing is written, but the 144 * size is calculated. 145 */ 146 hdrsize = 0; |
125 elf_puthdr(map, (void *)NULL, &hdrsize, 126 (const prstatus_t *)NULL, (const prfpregset_t *)NULL, 127 (const prpsinfo_t *)NULL, seginfo.count); | 147 elf_puthdr(pid, map, NULL, &hdrsize, seginfo.count); |
128 129 /* 130 * Allocate memory for building the header, fill it up, 131 * and write it out. 132 */ | 148 149 /* 150 * Allocate memory for building the header, fill it up, 151 * and write it out. 152 */ |
133 if ((hdr = malloc(hdrsize)) == NULL) | 153 if ((hdr = calloc(1, hdrsize)) == NULL) |
134 errx(1, "out of memory"); | 154 errx(1, "out of memory"); |
135 elf_corehdr(fd, pid, map, seginfo.count, hdr, hdrsize); | |
136 | 155 |
137 /* Write the contents of all of the writable segments. */ 138 snprintf(memname, sizeof memname, "/proc/%d/mem", pid); 139 if ((memfd = open(memname, O_RDONLY)) == -1) 140 err(1, "cannot open %s", memname); | 156 /* Fill in the header. */ 157 hdrsize = 0; 158 elf_puthdr(pid, map, hdr, &hdrsize, seginfo.count); |
141 | 159 |
160 /* Write it to the core file. */ 161 if (write(fd, hdr, hdrsize) == -1) 162 err(1, "write"); 163 164 /* Write the contents of all of the writable segments. */ |
|
142 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 143 for (i = 0; i < seginfo.count; i++) { | 165 php = (Elf_Phdr *)((char *)hdr + sizeof(Elf_Ehdr)) + 1; 166 for (i = 0; i < seginfo.count; i++) { |
167 struct ptrace_io_desc iorequest; |
|
144 uintmax_t nleft = php->p_filesz; 145 | 168 uintmax_t nleft = php->p_filesz; 169 |
146 lseek(memfd, (off_t)php->p_vaddr, SEEK_SET); | 170 iorequest.piod_op = PIOD_READ_D; 171 iorequest.piod_offs = (caddr_t)php->p_vaddr; |
147 while (nleft > 0) { 148 char buf[8*1024]; 149 size_t nwant; 150 ssize_t ngot; 151 152 if (nleft > sizeof(buf)) 153 nwant = sizeof buf; 154 else 155 nwant = nleft; | 172 while (nleft > 0) { 173 char buf[8*1024]; 174 size_t nwant; 175 ssize_t ngot; 176 177 if (nleft > sizeof(buf)) 178 nwant = sizeof buf; 179 else 180 nwant = nleft; |
156 ngot = read(memfd, buf, nwant); 157 if (ngot == -1) 158 err(1, "read from %s", memname); | 181 iorequest.piod_addr = buf; 182 iorequest.piod_len = nwant; 183 ptrace(PT_IO, pid, (caddr_t)&iorequest, 0); 184 ngot = iorequest.piod_len; |
159 if ((size_t)ngot < nwant) | 185 if ((size_t)ngot < nwant) |
160 errx(1, "short read from %s:" 161 " wanted %zu, got %zd", memname, | 186 errx(1, "short read wanted %d, got %d", |
162 nwant, ngot); 163 ngot = write(fd, buf, nwant); 164 if (ngot == -1) 165 err(1, "write of segment %d failed", i); 166 if ((size_t)ngot != nwant) 167 errx(1, "short write"); 168 nleft -= nwant; | 187 nwant, ngot); 188 ngot = write(fd, buf, nwant); 189 if (ngot == -1) 190 err(1, "write of segment %d failed", i); 191 if ((size_t)ngot != nwant) 192 errx(1, "short write"); 193 nleft -= nwant; |
194 iorequest.piod_offs += ngot; |
|
169 } 170 php++; 171 } | 195 } 196 php++; 197 } |
172 close(memfd); | |
173 free(hdr); 174 freemap(map); 175} 176 177/* 178 * A callback for each_writable_segment() to write out the segment's 179 * program header entry. 180 */ --- 45 unchanged lines hidden (view full) --- 226each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure) 227{ 228 vm_map_entry_t entry; 229 230 for (entry = map; entry != NULL; entry = entry->next) 231 (*func)(entry, closure); 232} 233 | 198 free(hdr); 199 freemap(map); 200} 201 202/* 203 * A callback for each_writable_segment() to write out the segment's 204 * program header entry. 205 */ --- 45 unchanged lines hidden (view full) --- 251each_writable_segment(vm_map_entry_t map, segment_callback func, void *closure) 252{ 253 vm_map_entry_t entry; 254 255 for (entry = map; entry != NULL; entry = entry->next) 256 (*func)(entry, closure); 257} 258 |
234/* 235 * Write the core file header to the file, including padding up to 236 * the page boundary. 237 */ | |
238static void | 259static void |
239elf_corehdr(int fd, pid_t pid, vm_map_entry_t map, int numsegs, void *hdr, 240 size_t hdrsize) | 260elf_getstatus(pid_t pid, prpsinfo_t *psinfo) |
241{ | 261{ |
242 size_t off; 243 prstatus_t status; 244 prfpregset_t fpregset; 245 prpsinfo_t psinfo; | 262 struct kinfo_proc kobj; 263 int name[4]; 264 size_t len; |
246 | 265 |
247 /* Gather the information for the header. */ 248 readhdrinfo(pid, &status, &fpregset, &psinfo); | 266 name[0] = CTL_KERN; 267 name[1] = KERN_PROC; 268 name[2] = KERN_PROC_PID; 269 name[3] = pid; |
249 | 270 |
250 /* Fill in the header. */ 251 memset(hdr, 0, hdrsize); 252 off = 0; 253 elf_puthdr(map, hdr, &off, &status, &fpregset, &psinfo, numsegs); 254 255 /* Write it to the core file. */ 256 if (write(fd, hdr, hdrsize) == -1) 257 err(1, "write"); | 271 len = sizeof(kobj); 272 if (sysctl(name, 4, &kobj, &len, NULL, 0) == -1) 273 err(1, "error accessing kern.proc.pid.%u sysctl", pid); 274 if (kobj.ki_pid != pid) 275 err(1, "error accessing kern.proc.pid.%u sysctl datas", pid); 276 strncpy(psinfo->pr_fname, kobj.ki_comm, MAXCOMLEN); 277 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ); |
258} 259 260/* 261 * Generate the ELF coredump header into the buffer at "dst". "dst" may 262 * be NULL, in which case the header is sized but not actually generated. 263 */ 264static void | 278} 279 280/* 281 * Generate the ELF coredump header into the buffer at "dst". "dst" may 282 * be NULL, in which case the header is sized but not actually generated. 283 */ 284static void |
265elf_puthdr(vm_map_entry_t map, void *dst, size_t *off, const prstatus_t *status, 266 const prfpregset_t *fpregset, const prpsinfo_t *psinfo, int numsegs) | 285elf_puthdr(pid_t pid, vm_map_entry_t map, void *dst, size_t *off, int numsegs) |
267{ | 286{ |
287 struct { 288 prstatus_t status; 289 prfpregset_t fpregset; 290 prpsinfo_t psinfo; 291 } *tempdata; |
|
268 size_t ehoff; 269 size_t phoff; 270 size_t noteoff; 271 size_t notesz; | 292 size_t ehoff; 293 size_t phoff; 294 size_t noteoff; 295 size_t notesz; |
296 size_t threads; 297 lwpid_t *tids; 298 int i; |
|
272 | 299 |
300 prstatus_t *status; 301 prfpregset_t *fpregset; 302 prpsinfo_t *psinfo; 303 |
|
273 ehoff = *off; 274 *off += sizeof(Elf_Ehdr); 275 276 phoff = *off; 277 *off += (numsegs + 1) * sizeof(Elf_Phdr); 278 279 noteoff = *off; | 304 ehoff = *off; 305 *off += sizeof(Elf_Ehdr); 306 307 phoff = *off; 308 *off += (numsegs + 1) * sizeof(Elf_Phdr); 309 310 noteoff = *off; |
280 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status, 281 sizeof *status); 282 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset, 283 sizeof *fpregset); | 311 312 if (dst != NULL) { 313 if ((tempdata = calloc(1, sizeof(*tempdata))) == NULL) 314 errx(1, "out of memory"); 315 status = &tempdata->status; 316 fpregset = &tempdata->fpregset; 317 psinfo = &tempdata->psinfo; 318 } else { 319 tempdata = NULL; 320 status = NULL; 321 fpregset = NULL; 322 psinfo = NULL; 323 } 324 325 errno = 0; 326 threads = ptrace(PT_GETNUMLWPS, pid, NULL, 0); 327 if (errno) 328 err(1, "PT_GETNUMLWPS"); 329 330 if (dst != NULL) { 331 psinfo->pr_version = PRPSINFO_VERSION; 332 psinfo->pr_psinfosz = sizeof(prpsinfo_t); 333 elf_getstatus(pid, psinfo); 334 335 } |
284 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo, 285 sizeof *psinfo); | 336 elf_putnote(dst, off, "FreeBSD", NT_PRPSINFO, psinfo, 337 sizeof *psinfo); |
338 339 if (dst != NULL) { 340 tids = malloc(threads * sizeof(*tids)); 341 if (tids == NULL) 342 errx(1, "out of memory"); 343 errno = 0; 344 ptrace(PT_GETLWPLIST, pid, (void *)tids, threads); 345 if (errno) 346 err(1, "PT_GETLWPLIST"); 347 } 348 for (i = 0; i < threads; ++i) { 349 if (dst != NULL) { 350 status->pr_version = PRSTATUS_VERSION; 351 status->pr_statussz = sizeof(prstatus_t); 352 status->pr_gregsetsz = sizeof(gregset_t); 353 status->pr_fpregsetsz = sizeof(fpregset_t); 354 status->pr_osreldate = __FreeBSD_version; 355 status->pr_pid = tids[i]; 356 357 ptrace(PT_GETREGS, tids[i], (void *)&status->pr_reg, 0); 358 ptrace(PT_GETFPREGS, tids[i], (void *)fpregset, 0); 359 } 360 elf_putnote(dst, off, "FreeBSD", NT_PRSTATUS, status, 361 sizeof *status); 362 elf_putnote(dst, off, "FreeBSD", NT_FPREGSET, fpregset, 363 sizeof *fpregset); 364 } 365 |
|
286 notesz = *off - noteoff; 287 | 366 notesz = *off - noteoff; 367 |
368 if (dst != NULL) { 369 free(tids); 370 free(tempdata); 371 } 372 |
|
288 /* Align up to a page boundary for the program segments. */ 289 *off = round_page(*off); 290 291 if (dst != NULL) { 292 Elf_Ehdr *ehdr; 293 Elf_Phdr *phdr; 294 struct phdr_closure phc; 295 --- 80 unchanged lines hidden (view full) --- 376 while (map != NULL) { 377 vm_map_entry_t next = map->next; 378 free(map); 379 map = next; 380 } 381} 382 383/* | 373 /* Align up to a page boundary for the program segments. */ 374 *off = round_page(*off); 375 376 if (dst != NULL) { 377 Elf_Ehdr *ehdr; 378 Elf_Phdr *phdr; 379 struct phdr_closure phc; 380 --- 80 unchanged lines hidden (view full) --- 461 while (map != NULL) { 462 vm_map_entry_t next = map->next; 463 free(map); 464 map = next; 465 } 466} 467 468/* |
384 * Read the process information necessary to fill in the core file's header. 385 */ 386static void 387readhdrinfo(pid_t pid, prstatus_t *status, prfpregset_t *fpregset, 388 prpsinfo_t *psinfo) 389{ 390 char name[64]; 391 char line[256]; 392 int fd; 393 int i; 394 int n; 395 396 memset(status, 0, sizeof *status); 397 status->pr_version = PRSTATUS_VERSION; 398 status->pr_statussz = sizeof(prstatus_t); 399 status->pr_gregsetsz = sizeof(gregset_t); 400 status->pr_fpregsetsz = sizeof(fpregset_t); 401 status->pr_osreldate = __FreeBSD_version; 402 status->pr_pid = pid; 403 404 memset(fpregset, 0, sizeof *fpregset); 405 406 memset(psinfo, 0, sizeof *psinfo); 407 psinfo->pr_version = PRPSINFO_VERSION; 408 psinfo->pr_psinfosz = sizeof(prpsinfo_t); 409 410 /* Read the general registers. */ 411 snprintf(name, sizeof name, "/proc/%d/regs", pid); 412 if ((fd = open(name, O_RDONLY)) == -1) 413 err(1, "cannot open %s", name); 414 if ((n = read(fd, &status->pr_reg, sizeof status->pr_reg)) == -1) 415 err(1, "read error from %s", name); 416 if ((size_t)n < sizeof(status->pr_reg)) 417 errx(1, "short read from %s: wanted %zu, got %d", name, 418 sizeof status->pr_reg, n); 419 close(fd); 420 421 /* Read the floating point registers. */ 422 snprintf(name, sizeof name, "/proc/%d/fpregs", pid); 423 if ((fd = open(name, O_RDONLY)) == -1) 424 err(1, "cannot open %s", name); 425 if ((n = read(fd, fpregset, sizeof *fpregset)) == -1) 426 err(1, "read error from %s", name); 427 if ((size_t)n < sizeof(*fpregset)) 428 errx(1, "short read from %s: wanted %zu, got %d", name, 429 sizeof *fpregset, n); 430 close(fd); 431 432 /* Read and parse the process status. */ 433 snprintf(name, sizeof name, "/proc/%d/status", pid); 434 if ((fd = open(name, O_RDONLY)) == -1) 435 err(1, "cannot open %s", name); 436 if ((n = read(fd, line, sizeof line - 1)) == -1) 437 err(1, "read error from %s", name); 438 if (n > MAXCOMLEN) 439 n = MAXCOMLEN; 440 for (i = 0; i < n && line[i] != ' '; i++) 441 psinfo->pr_fname[i] = line[i]; 442 strncpy(psinfo->pr_psargs, psinfo->pr_fname, PRARGSZ); 443 close(fd); 444} 445 446/* 447 * Read the process's memory map using procfs, and return a list of | 469 * Read the process's memory map using kinfo_getvmmap(), and return a list of |
448 * VM map entries. Only the non-device read/writable segments are 449 * returned. The map entries in the list aren't fully filled in; only 450 * the items we need are present. 451 */ 452static vm_map_entry_t 453readmap(pid_t pid) 454{ | 470 * VM map entries. Only the non-device read/writable segments are 471 * returned. The map entries in the list aren't fully filled in; only 472 * the items we need are present. 473 */ 474static vm_map_entry_t 475readmap(pid_t pid) 476{ |
455 char mapname[64]; 456 int mapfd; 457 ssize_t mapsize; 458 size_t bufsize; 459 char *mapbuf; 460 int pos; 461 vm_map_entry_t map; 462 vm_map_entry_t *linkp; | 477 vm_map_entry_t ent, *linkp, map; 478 struct kinfo_vmentry *vmentl, *kve; 479 int i, nitems; |
463 | 480 |
464 snprintf(mapname, sizeof mapname, "/proc/%d/map", pid); 465 if ((mapfd = open(mapname, O_RDONLY)) == -1) 466 err(1, "cannot open %s", mapname); | 481 vmentl = kinfo_getvmmap(pid, &nitems); 482 if (vmentl == NULL) 483 err(1, "cannot retrieve mappings for %u process", pid); |
467 | 484 |
468 /* 469 * Procfs requires (for consistency) that the entire memory map 470 * be read with a single read() call. Start with a reasonably sized 471 * buffer, and double it until it is big enough. 472 */ 473 bufsize = 8 * 1024; 474 mapbuf = NULL; 475 for ( ; ; ) { 476 if ((mapbuf = realloc(mapbuf, bufsize + 1)) == NULL) 477 errx(1, "out of memory"); 478 mapsize = read(mapfd, mapbuf, bufsize); 479 if (mapsize != -1 || errno != EFBIG) 480 break; 481 bufsize *= 2; 482 /* This lseek shouldn't be necessary, but it is. */ 483 lseek(mapfd, (off_t)0, SEEK_SET); 484 } 485 if (mapsize == -1) 486 err(1, "read error from %s", mapname); 487 if (mapsize == 0) 488 errx(1, "empty map file %s", mapname); 489 mapbuf[mapsize] = 0; 490 close(mapfd); 491 492 pos = 0; | |
493 map = NULL; 494 linkp = ↦ | 485 map = NULL; 486 linkp = ↦ |
495 while (pos < mapsize) { 496 vm_map_entry_t ent; 497 u_long start; 498 u_long end; 499 char prot[4]; 500 char type[16]; 501 int n; 502 int len; | 487 for (i = 0; i < nitems; i++) { 488 kve = &vmentl[i]; |
503 | 489 |
504 len = 0; 505 n = sscanf(mapbuf + pos, "%lx %lx %*d %*d %*x %3[-rwx]" 506 " %*d %*d %*x %*s %*s %16s %*s%*[\n]%n", 507 &start, &end, prot, type, &len); 508 if (n != 4 || len == 0) 509 errx(1, "ill-formed line in %s starting at character %d", mapname, pos + 1); 510 pos += len; 511 512 /* Ignore segments of the wrong kind, and unwritable ones */ 513 if (strncmp(prot, "rw", 2) != 0 || 514 (strcmp(type, "default") != 0 && 515 strcmp(type, "vnode") != 0 && 516 strcmp(type, "swap") != 0)) | 490 /* 491 * Ignore segments of the wrong kind and ones which are not 492 * readable and writable. 493 */ 494 if ((kve->kve_protection & KVME_PROT_WRITE) == 0 || 495 (kve->kve_protection & KVME_PROT_READ) == 0 || 496 (kve->kve_type != KVME_TYPE_DEFAULT && 497 kve->kve_type != KVME_TYPE_VNODE && 498 kve->kve_type != KVME_TYPE_SWAP)) |
517 continue; 518 | 499 continue; 500 |
519 if ((ent = (vm_map_entry_t)calloc(1, sizeof *ent)) == NULL) | 501 ent = calloc(1, sizeof(*ent)); 502 if (ent == NULL) |
520 errx(1, "out of memory"); | 503 errx(1, "out of memory"); |
521 ent->start = start; 522 ent->end = end; | 504 ent->start = (vm_offset_t)kve->kve_start; 505 ent->end = (vm_offset_t)kve->kve_end; |
523 ent->protection = VM_PROT_READ | VM_PROT_WRITE; | 506 ent->protection = VM_PROT_READ | VM_PROT_WRITE; |
524 if (prot[2] == 'x') 525 ent->protection |= VM_PROT_EXECUTE; | 507 if ((kve->kve_protection & KVME_PROT_EXEC) != 0) 508 ent->protection |= VM_PROT_EXECUTE; |
526 527 *linkp = ent; 528 linkp = &ent->next; 529 } | 509 510 *linkp = ent; 511 linkp = &ent->next; 512 } |
530 free(mapbuf); 531 return map; | 513 free(vmentl); 514 return (map); |
532} 533 534struct dumpers elfdump = { elf_ident, elf_coredump }; 535TEXT_SET(dumpset, elfdump); | 515} 516 517struct dumpers elfdump = { elf_ident, elf_coredump }; 518TEXT_SET(dumpset, elfdump); |