1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
4 This file is part of libunwind.
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:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
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. */
25 #include <sys/param.h>
26 #include <sys/types.h>
28 #include <sys/sysctl.h>
33 #include "libunwind_i.h"
40 res
= mmap(NULL
, sz
, PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
41 if (res
== MAP_FAILED
)
47 free_mem(void *ptr
, size_t sz
)
53 get_pid_by_tid(int tid
)
58 struct kinfo_proc
*kv
;
64 mib
[2] = KERN_PROC_ALL
;
66 error
= sysctl(mib
, 3, NULL
, &len
, NULL
, 0);
74 error
= sysctl(mib
, 3, buf
, &len
, NULL
, 0);
80 for (i
= 0, kv
= (struct kinfo_proc
*)buf
; i
< len
/ sizeof(*kv
);
82 if (kv
->ki_tid
== tid
) {
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
)
95 int mib
[4], error
, ret
;
98 struct kinfo_vmentry
*kv
;
103 mib
[2] = KERN_PROC_VMMAP
;
106 error
= sysctl(mib
, 4, NULL
, &len
, NULL
, 0);
108 if (errno
== ESRCH
) {
109 mib
[3] = get_pid_by_tid(pid
);
111 error
= sysctl(mib
, 4, NULL
, &len
, NULL
, 0);
113 return (-UNW_EUNSPEC
);
115 return (-UNW_EUNSPEC
);
120 return (-UNW_EUNSPEC
);
122 error
= sysctl(mib
, 4, buf
, &len
, NULL
, 0);
125 return (-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
)
132 if (kv
->kve_type
!= KVME_TYPE_VNODE
)
134 *segbase
= kv
->kve_start
;
135 *mapoff
= kv
->kve_offset
;
138 strncpy(path
, kv
->kve_path
, pathlen
);
140 ret
= elf_map_image (ei
, kv
->kve_path
);