blob: a315188657f66872c30280ead196cfd6d89bd880 [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
25#ifndef UNW_REMOTE_ONLY
26
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030027#include <sys/param.h>
Konstantin Belousov4de09a92010-03-06 18:53:27 +020028#include <sys/types.h>
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030029#include <sys/mman.h>
30#include <sys/sysctl.h>
Konstantin Belousov4de09a92010-03-06 18:53:27 +020031#include <sys/user.h>
Konstantin Belousov63ae8ca2010-03-06 22:32:11 +020032#include <stdio.h>
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020033
34#include "libunwind_i.h"
35
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030036static void *
37get_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
47static void
48free_mem(void *ptr, size_t sz)
49{
50 munmap(ptr, sz);
51}
52
Konstantin Belousovbd279882010-04-03 23:29:28 +030053PROTECTED int
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020054tdep_get_elf_image (struct elf_image *ei, pid_t pid, unw_word_t ip,
Arun Sharma1787a2f2010-05-15 12:14:09 -070055 unsigned long *segbase, unsigned long *mapoff, char *path, size_t pathlen)
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020056{
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030057 int mib[4], error, ret;
58 size_t len, len1;
59 char *buf, *bp, *eb;
60 struct kinfo_vmentry *kv;
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020061
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030062 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 Belousovbd279882010-04-03 23:29:28 +030070 return (-1);
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030071 len1 = len * 4 / 3;
72 buf = get_mem(len1);
73 if (buf == NULL)
74 return (-1);
Konstantin Belousov61f43452010-04-13 15:33:11 +030075 len = len1;
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030076 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 Sharma1787a2f2010-05-15 12:14:09 -070090 if (path)
91 {
Konstantin Belousov298e5752010-05-17 21:57:59 +030092 strncpy(path, kv->kve_path, pathlen);
Arun Sharma1787a2f2010-05-15 12:14:09 -070093 }
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030094 ret = elf_map_image(ei, kv->kve_path);
95 break;
96 }
97 free_mem(buf, len1);
98 return (ret);
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020099}
100
101#endif /* UNW_REMOTE_ONLY */