Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 1 | /* libunwind - a platform-independent unwind library |
Konstantin Belousov | 6f7b335 | 2010-04-10 01:42:26 +0300 | [diff] [blame] | 2 | Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org> |
Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 3 | |
| 4 | This file is part of libunwind. |
| 5 | |
| 6 | Permission is hereby granted, free of charge, to any person obtaining |
| 7 | a copy of this software and associated documentation files (the |
| 8 | "Software"), to deal in the Software without restriction, including |
| 9 | without limitation the rights to use, copy, modify, merge, publish, |
| 10 | distribute, sublicense, and/or sell copies of the Software, and to |
| 11 | permit persons to whom the Software is furnished to do so, subject to |
| 12 | the following conditions: |
| 13 | |
| 14 | The above copyright notice and this permission notice shall be |
| 15 | included in all copies or substantial portions of the Software. |
| 16 | |
| 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 21 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 24 | |
| 25 | #ifndef UNW_REMOTE_ONLY |
| 26 | |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 27 | #include <sys/param.h> |
Konstantin Belousov | 4de09a9 | 2010-03-06 18:53:27 +0200 | [diff] [blame] | 28 | #include <sys/types.h> |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 29 | #include <sys/mman.h> |
| 30 | #include <sys/sysctl.h> |
Konstantin Belousov | 4de09a9 | 2010-03-06 18:53:27 +0200 | [diff] [blame] | 31 | #include <sys/user.h> |
Konstantin Belousov | 63ae8ca | 2010-03-06 22:32:11 +0200 | [diff] [blame] | 32 | #include <stdio.h> |
Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 33 | |
| 34 | #include "libunwind_i.h" |
| 35 | |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 36 | static void * |
| 37 | get_mem(size_t sz) |
| 38 | { |
| 39 | void *res; |
| 40 | |
| 41 | res = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 42 | if (res == MAP_FAILED) |
| 43 | return (NULL); |
| 44 | return (res); |
| 45 | } |
| 46 | |
| 47 | static void |
| 48 | free_mem(void *ptr, size_t sz) |
| 49 | { |
| 50 | munmap(ptr, sz); |
| 51 | } |
| 52 | |
Konstantin Belousov | bd27988 | 2010-04-03 23:29:28 +0300 | [diff] [blame] | 53 | PROTECTED int |
Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 54 | tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip, |
Arun Sharma | 1787a2f | 2010-05-15 12:14:09 -0700 | [diff] [blame] | 55 | unsigned long *segbase, unsigned long *mapoff, char *path, size_t pathlen) |
Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 56 | { |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 57 | int mib[4], error, ret; |
| 58 | size_t len, len1; |
| 59 | char *buf, *bp, *eb; |
| 60 | struct kinfo_vmentry *kv; |
Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 61 | |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 62 | len = 0; |
| 63 | mib[0] = CTL_KERN; |
| 64 | mib[1] = KERN_PROC; |
| 65 | mib[2] = KERN_PROC_VMMAP; |
| 66 | mib[3] = pid; |
| 67 | |
| 68 | error = sysctl(mib, 4, NULL, &len, NULL, 0); |
| 69 | if (error) |
Konstantin Belousov | bd27988 | 2010-04-03 23:29:28 +0300 | [diff] [blame] | 70 | return (-1); |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 71 | len1 = len * 4 / 3; |
| 72 | buf = get_mem(len1); |
| 73 | if (buf == NULL) |
| 74 | return (-1); |
Konstantin Belousov | 61f4345 | 2010-04-13 15:33:11 +0300 | [diff] [blame] | 75 | len = len1; |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 76 | error = sysctl(mib, 4, buf, &len, NULL, 0); |
| 77 | if (error) { |
| 78 | free_mem(buf, len1); |
| 79 | return (-1); |
| 80 | } |
| 81 | ret = -1; |
| 82 | for (bp = buf, eb = buf + len; bp < eb; bp += kv->kve_structsize) { |
| 83 | kv = (struct kinfo_vmentry *)(uintptr_t)bp; |
| 84 | if (ip < kv->kve_start || ip >= kv->kve_end) |
| 85 | continue; |
| 86 | if (kv->kve_type != KVME_TYPE_VNODE) |
| 87 | break; |
| 88 | *segbase = kv->kve_start; |
| 89 | *mapoff = kv->kve_offset; |
Arun Sharma | 1787a2f | 2010-05-15 12:14:09 -0700 | [diff] [blame] | 90 | if (path) |
| 91 | { |
Konstantin Belousov | 298e575 | 2010-05-17 21:57:59 +0300 | [diff] [blame] | 92 | strncpy(path, kv->kve_path, pathlen); |
Arun Sharma | 1787a2f | 2010-05-15 12:14:09 -0700 | [diff] [blame] | 93 | } |
Konstantin Belousov | e33fa9f | 2010-04-11 14:36:24 +0300 | [diff] [blame] | 94 | ret = elf_map_image(ei, kv->kve_path); |
| 95 | break; |
| 96 | } |
| 97 | free_mem(buf, len1); |
| 98 | return (ret); |
Konstantin Belousov | 2646e0f | 2010-03-06 17:47:52 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | #endif /* UNW_REMOTE_ONLY */ |