1/* libunwind - a platform-independent unwind library 2 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org> 3 4This file is part of libunwind. 5 6Permission is hereby granted, free of charge, to any person obtaining 7a copy of this software and associated documentation files (the 8"Software"), to deal in the Software without restriction, including 9without limitation the rights to use, copy, modify, merge, publish, 10distribute, sublicense, and/or sell copies of the Software, and to 11permit persons to whom the Software is furnished to do so, subject to 12the following conditions: 13 14The above copyright notice and this permission notice shall be 15included in all copies or substantial portions of the Software. 16 17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 21LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 24 25#include <sys/param.h> 26#include <sys/types.h> 27#include <sys/mman.h> 28#include <sys/sysctl.h> 29#include <sys/user.h> 30#include <stdio.h> 31#include <errno.h> 32 33#include "libunwind_i.h" 34 35static void * 36get_mem(size_t sz) 37{ 38 void *res; 39 40 res = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); 41 if (res == MAP_FAILED) 42 return (NULL); 43 return (res); 44} 45 46static void 47free_mem(void *ptr, size_t sz) 48{ 49 munmap(ptr, sz); 50} 51 52static int 53get_pid_by_tid(int tid) 54{ 55 int mib[3], error; 56 size_t len, len1; 57 char *buf; 58 struct kinfo_proc *kv; 59 int i, pid; 60 61 len = 0; 62 mib[0] = CTL_KERN; 63 mib[1] = KERN_PROC; 64 mib[2] = KERN_PROC_ALL; 65 66 error = sysctl(mib, 3, NULL, &len, NULL, 0); 67 if (error == -1) 68 return (-1); 69 len1 = len * 4 / 3; 70 buf = get_mem(len1); 71 if (buf == NULL) 72 return (-1); 73 len = len1; 74 error = sysctl(mib, 3, buf, &len, NULL, 0); 75 if (error == -1) { 76 free_mem(buf, len1); 77 return (-1); 78 } 79 pid = -1; 80 for (i = 0, kv = (struct kinfo_proc *)buf; i < len / sizeof(*kv); 81 i++, kv++) { 82 if (kv->ki_tid == tid) { 83 pid = kv->ki_pid; 84 break; 85 } 86 } 87 free_mem(buf, len1); 88 return (pid); 89} 90 91/* ANDROID support update. */ 92struct map_info * 93map_create_list(pid_t pid) 94{ 95 int mib[4], error, ret; 96 size_t len, len1; 97 char *buf, *bp, *eb; 98 struct kinfo_vmentry *kv; 99 struct map_info *map_list = NULL; 100 101 len = 0; 102 mib[0] = CTL_KERN; 103 mib[1] = KERN_PROC; 104 mib[2] = KERN_PROC_VMMAP; 105 mib[3] = pid; 106 107 error = sysctl(mib, 4, NULL, &len, NULL, 0); 108 if (error == -1) { 109 if (errno == ESRCH) 110 { 111 mib[3] = get_pid_by_tid(pid); 112 if (mib[3] != -1) 113 error = sysctl(mib, 4, NULL, &len, NULL, 0); 114 if (error == -1) 115 return (-UNW_EUNSPEC); 116 } 117 else 118 return (-UNW_EUNSPEC); 119 } 120 len1 = len * 4 / 3; 121 buf = get_mem(len1); 122 if (buf == NULL) 123 return (-UNW_EUNSPEC); 124 len = len1; 125 error = sysctl(mib, 4, buf, &len, NULL, 0); 126 if (error == -1) 127 { 128 free_mem(buf, len1); 129 return (-UNW_EUNSPEC); 130 } 131 ret = -UNW_EUNSPEC; 132 for (bp = buf, eb = buf + len; bp < eb; bp += kv->kve_structsize) 133 { 134 kv = (struct kinfo_vmentry *)(uintptr_t)bp; 135 if (kv->kve_type != KVME_TYPE_VNODE) 136 continue; 137 138 cur_map = map_alloc_info (); 139 if (cur_map == NULL) 140 break; 141 cur_map->next = map_list; 142 cur_map->start = kv->kve_start; 143 cur_map->end = kv->kv_end; 144 cur_map->offset = kv->kve_offset; 145 cur_map->path = strdup(kv->kve_path); 146 mutex_init (&cur_map->ei_lock); 147 cur_map->ei.size = 0; 148 cur_map->ei.image = NULL; 149 cur_map->ei_shared = 0; 150 } 151 free_mem(buf, len1); 152 153 return map_list; 154} 155/* End of ANDROID update. */ 156