1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
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 #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
35 static void *
get_mem(size_t sz)36 get_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
46 static void
free_mem(void * ptr,size_t sz)47 free_mem(void *ptr, size_t sz)
48 {
49 munmap(ptr, sz);
50 }
51
52 static int
get_pid_by_tid(int tid)53 get_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 unsigned 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 int
tdep_get_elf_image(struct elf_image * ei,pid_t pid,unw_word_t ip,unsigned long * segbase,unsigned long * mapoff,char * path,size_t pathlen)92 tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
93 unsigned long *segbase, unsigned long *mapoff, char *path, size_t pathlen)
94 {
95 int mib[4], error, ret;
96 size_t len, len1;
97 char *buf, *bp, *eb;
98 struct kinfo_vmentry *kv;
99
100 len = 0;
101 mib[0] = CTL_KERN;
102 mib[1] = KERN_PROC;
103 mib[2] = KERN_PROC_VMMAP;
104 mib[3] = pid;
105
106 error = sysctl(mib, 4, NULL, &len, NULL, 0);
107 if (error == -1) {
108 if (errno == ESRCH) {
109 mib[3] = get_pid_by_tid(pid);
110 if (mib[3] != -1)
111 error = sysctl(mib, 4, NULL, &len, NULL, 0);
112 if (error == -1)
113 return (-UNW_EUNSPEC);
114 } else
115 return (-UNW_EUNSPEC);
116 }
117 len1 = len * 4 / 3;
118 buf = get_mem(len1);
119 if (buf == NULL)
120 return (-UNW_EUNSPEC);
121 len = len1;
122 error = sysctl(mib, 4, buf, &len, NULL, 0);
123 if (error == -1) {
124 free_mem(buf, len1);
125 return (-UNW_EUNSPEC);
126 }
127 ret = -UNW_EUNSPEC;
128 for (bp = buf, eb = buf + len; bp < eb; bp += kv->kve_structsize) {
129 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
130 if (ip < kv->kve_start || ip >= kv->kve_end)
131 continue;
132 if (kv->kve_type != KVME_TYPE_VNODE)
133 continue;
134 *segbase = kv->kve_start;
135 *mapoff = kv->kve_offset;
136 if (path)
137 {
138 strncpy(path, kv->kve_path, pathlen);
139 }
140 ret = elf_map_image (ei, kv->kve_path);
141 break;
142 }
143 free_mem(buf, len1);
144 return (ret);
145 }
146
147 #ifndef UNW_REMOTE_ONLY
148
149 void
tdep_get_exe_image_path(char * path)150 tdep_get_exe_image_path (char *path)
151 {
152 int mib[4], error;
153 size_t len;
154
155 len = 0;
156 mib[0] = CTL_KERN;
157 mib[1] = KERN_PROC;
158 mib[2] = KERN_PROC_PATHNAME;
159 mib[3] = getpid();
160
161 error = sysctl(mib, 4, path, &len, NULL, 0);
162 if (error == -1)
163 path[0] = 0;
164 }
165
166 #endif
167