• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   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 
91 /* ANDROID support update. */
92 struct map_info *
map_create_list(pid_t pid)93 map_create_list(pid_t pid)
94 {
95   int mib[4], error, ret;
96   size_t len, len1;
97   char *buf, *bp, *eb;
98   struct kinfo_vmentry *kv;
99   struct map_info *map_list = NULL;
100 
101   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);
108   if (error == -1) {
109     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
118       return (-UNW_EUNSPEC);
119   }
120   len1 = len * 4 / 3;
121   buf = get_mem(len1);
122   if (buf == NULL)
123     return (-UNW_EUNSPEC);
124   len = len1;
125   error = sysctl(mib, 4, buf, &len, NULL, 0);
126   if (error == -1)
127     {
128       free_mem(buf, len1);
129       return (-UNW_EUNSPEC);
130     }
131   ret = -UNW_EUNSPEC;
132   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     }
151   free_mem(buf, len1);
152 
153   return map_list;
154 }
155 /* End of ANDROID update. */
156