blob: b81b7528ad707f9ee82ba0b1a7cc06e819e09f43 [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 Belousov5f440b42012-04-23 13:51:55 +030031#include <errno.h>
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020032
33#include "libunwind_i.h"
34
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030035static 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
Konstantin Belousov5f440b42012-04-23 13:51:55 +030052static 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
Christopher Ferrisf4a8df52014-03-07 19:40:06 -080091/* ANDROID support update. */
92struct map_info *
93map_create_list(pid_t pid)
Konstantin Belousov2646e0f2010-03-06 17:47:52 +020094{
Konstantin Belousove33fa9f2010-04-11 14:36:24 +030095 int mib[4], error, ret;
96 size_t len, len1;
97 char *buf, *bp, *eb;
98 struct kinfo_vmentry *kv;
Christopher Ferrisf4a8df52014-03-07 19:40:06 -080099 struct map_info *map_list = NULL;
Konstantin Belousov2646e0f2010-03-06 17:47:52 +0200100
Konstantin Belousove33fa9f2010-04-11 14:36:24 +0300101 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);
Konstantin Belousov5f440b42012-04-23 13:51:55 +0300108 if (error == -1) {
Christopher Ferrisf4a8df52014-03-07 19:40:06 -0800109 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
Konstantin Belousov5f440b42012-04-23 13:51:55 +0300118 return (-UNW_EUNSPEC);
119 }
Konstantin Belousove33fa9f2010-04-11 14:36:24 +0300120 len1 = len * 4 / 3;
121 buf = get_mem(len1);
122 if (buf == NULL)
Konstantin Belousov0fac4c82012-04-23 13:51:53 +0300123 return (-UNW_EUNSPEC);
Konstantin Belousov61f43452010-04-13 15:33:11 +0300124 len = len1;
Konstantin Belousove33fa9f2010-04-11 14:36:24 +0300125 error = sysctl(mib, 4, buf, &len, NULL, 0);
Christopher Ferrisf4a8df52014-03-07 19:40:06 -0800126 if (error == -1)
127 {
128 free_mem(buf, len1);
129 return (-UNW_EUNSPEC);
130 }
Konstantin Belousov0fac4c82012-04-23 13:51:53 +0300131 ret = -UNW_EUNSPEC;
Christopher Ferrisf4a8df52014-03-07 19:40:06 -0800132 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 }
Konstantin Belousove33fa9f2010-04-11 14:36:24 +0300151 free_mem(buf, len1);
Christopher Ferris7d46a212013-11-14 14:03:33 -0800152
Christopher Ferrisf4a8df52014-03-07 19:40:06 -0800153 return map_list;
Christopher Ferris7d46a212013-11-14 14:03:33 -0800154}
Christopher Ferrisf4a8df52014-03-07 19:40:06 -0800155/* End of ANDROID update. */