blob: 64fc9338f58ea53f578740bf2270f2a16eaebe67 [file] [log] [blame]
Konstantin Belousov2646e0f2010-03-06 17:47:52 +02001/* libunwind - a platform-independent unwind library
Konstantin Belousov6f7b3352010-04-10 01:42:26 +03002 Copyright (C) 2010 Konstantin Belousov <kib@freebsd.org>
Konstantin Belousov2646e0f2010-03-06 17:47:52 +02003
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
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030025#include <sys/param.h>
Konstantin Belousov4de09a92010-03-06 18:53:27 +020026#include <sys/types.h>
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030027#include <sys/mman.h>
28#include <sys/sysctl.h>
Konstantin Belousov4de09a92010-03-06 18:53:27 +020029#include <sys/user.h>
Konstantin Belousov63ae8ca2010-03-06 22:32:11 +020030#include <stdio.h>
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020031
32#include "libunwind_i.h"
33
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030034static void *
35get_mem(size_t sz)
36{
37 void *res;
38
39 res = mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
40 if (res == MAP_FAILED)
41 return (NULL);
42 return (res);
43}
44
45static void
46free_mem(void *ptr, size_t sz)
47{
48 munmap(ptr, sz);
49}
50
Konstantin Belousovbd279882010-04-03 23:29:28 +030051PROTECTED int
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020052tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
Arun Sharma1787a2f2010-05-15 12:14:09 -070053 unsigned long *segbase, unsigned long *mapoff, char *path, size_t pathlen)
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020054{
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030055 int mib[4], error, ret;
56 size_t len, len1;
57 char *buf, *bp, *eb;
58 struct kinfo_vmentry *kv;
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020059
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030060 len = 0;
61 mib[0] = CTL_KERN;
62 mib[1] = KERN_PROC;
63 mib[2] = KERN_PROC_VMMAP;
64 mib[3] = pid;
65
66 error = sysctl(mib, 4, NULL, &len, NULL, 0);
67 if (error)
Konstantin Belousovbd279882010-04-03 23:29:28 +030068 return (-1);
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030069 len1 = len * 4 / 3;
70 buf = get_mem(len1);
71 if (buf == NULL)
72 return (-1);
Konstantin Belousov61f43452010-04-13 15:33:11 +030073 len = len1;
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030074 error = sysctl(mib, 4, buf, &len, NULL, 0);
75 if (error) {
76 free_mem(buf, len1);
77 return (-1);
78 }
79 ret = -1;
80 for (bp = buf, eb = buf + len; bp < eb; bp += kv->kve_structsize) {
81 kv = (struct kinfo_vmentry *)(uintptr_t)bp;
82 if (ip < kv->kve_start || ip >= kv->kve_end)
83 continue;
84 if (kv->kve_type != KVME_TYPE_VNODE)
85 break;
86 *segbase = kv->kve_start;
87 *mapoff = kv->kve_offset;
Arun Sharma1787a2f2010-05-15 12:14:09 -070088 if (path)
89 {
Konstantin Belousov298e5752010-05-17 21:57:59 +030090 strncpy(path, kv->kve_path, pathlen);
Arun Sharma1787a2f2010-05-15 12:14:09 -070091 }
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030092 ret = elf_map_image(ei, kv->kve_path);
93 break;
94 }
95 free_mem(buf, len1);
96 return (ret);
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020097}