• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003-2005 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 
5 This file is part of libunwind.
6 
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14 
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
25 
26 #include <limits.h>
27 #include <stdio.h>
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 
31 #include "libunwind_i.h"
32 #include "map_info.h"
33 #include "os-linux.h"
34 
35 struct map_info *
maps_create_list(pid_t pid)36 maps_create_list(pid_t pid)
37 {
38   struct map_iterator mi;
39   unsigned long start, end, offset, flags;
40   struct map_info *map_list = NULL;
41   struct map_info *cur_map;
42   struct map_info *buf;
43   int sz;
44   int index = 0;
45   if ((sz = maps_init (&mi, pid)) < 0)
46     return NULL;
47 
48   if (sz < 0 || sz > 65536) {
49     return NULL;
50   }
51 
52   buf = (struct map_info*)mmap(NULL, sz * sizeof(struct map_info), PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
53   if (buf == NULL) {
54     return NULL;
55   }
56 
57   while (maps_next (&mi, &start, &end, &offset, &flags))
58     {
59       if (index >= sz) {
60         break;
61       }
62 
63       cur_map = &buf[index];
64       cur_map->next = map_list;
65       cur_map->start = start;
66       cur_map->end = end;
67       cur_map->offset = offset;
68       cur_map->flags = flags;
69       cur_map->path = strdup(mi.path);
70       cur_map->ei.size = 0;
71       cur_map->ei.image = NULL;
72       cur_map->ei.has_dyn_info = 0;
73       cur_map->ei.load_bias = -1;
74       cur_map->ei.strtab = NULL;
75       cur_map->sz = sz;
76       cur_map->buf = buf;
77       map_list = cur_map;
78       index = index + 1;
79     }
80 
81   maps_close (&mi);
82 
83   return map_list;
84 }
85 
86 void
maps_destroy_list(struct map_info * map_info)87 maps_destroy_list(struct map_info *map_info)
88 {
89   struct map_info *map;
90   int sz  = map_info->sz;
91   void* buf = map_info->buf;
92   while (map_info)
93     {
94       map = map_info;
95       map_info = map->next;
96       if (map->ei.image != MAP_FAILED && map->ei.image != NULL) {
97         munmap(map->ei.image, map->ei.size);
98         map->ei.image = NULL;
99       }
100       if (map->path) {
101         free(map->path);
102         map->path = NULL;
103       }
104       map = NULL;
105     }
106   if (buf != NULL) {
107     munmap(buf, sz * sizeof(struct map_info));
108     buf = NULL;
109   }
110 }
111 
112 static struct map_info *
get_map(struct map_info * map_list,unw_word_t addr)113 get_map(struct map_info *map_list, unw_word_t addr)
114 {
115   if (map_list == NULL) {
116     return NULL;
117   }
118 
119   struct map_info* buf = map_list->buf;
120   if (buf == NULL) {
121     return NULL;
122   }
123 
124   int begin = 0;
125   int end = map_list->sz - 1;
126   while (begin <= end) {
127     int mid = begin + ((end - begin) / 2);
128     if (addr < buf[mid].start) {
129       end = mid - 1;
130     } else if (addr <= buf[mid].end) {
131       return &buf[mid];
132     } else {
133       begin = mid + 1;
134     }
135   }
136 
137   if ((addr >= buf[begin].start) && (addr <= buf[begin].end)) {
138     return &buf[begin];
139   }
140   return NULL;
141 }
142 
maps_is_readable(struct map_info * map_list,unw_word_t addr)143 int maps_is_readable(struct map_info *map_list, unw_word_t addr)
144 {
145   /* If there is no map, assume everything is okay. */
146   if (map_list == NULL)
147     return 1;
148   struct map_info *map = get_map(map_list, addr);
149   if (map != NULL)
150     return map->flags & PROT_READ;
151   return 0;
152 }
153 
maps_is_writable(struct map_info * map_list,unw_word_t addr)154 int maps_is_writable(struct map_info *map_list, unw_word_t addr)
155 {
156   /* If there is no map, assume everything is okay. */
157   if (map_list == NULL)
158     return 1;
159   struct map_info *map = get_map(map_list, addr);
160   if (map != NULL)
161     return map->flags & PROT_WRITE;
162   return 0;
163 }
164 
165 struct map_info*
tdep_get_elf_image(unw_addr_space_t as,pid_t pid,unw_word_t ip)166 tdep_get_elf_image(unw_addr_space_t as, pid_t pid, unw_word_t ip)
167 {
168   struct map_info *map;
169   struct cursor* cursor = get_cursor_from_as(as);
170   int find_cached_map = ((cursor != NULL) && (cursor->dwarf.ip == ip));
171   if (find_cached_map  &&
172     (cursor->dwarf.ip == cursor->dwarf.cached_ip) && cursor->dwarf.cached_map != NULL) {
173     return cursor->dwarf.cached_map;
174   }
175 
176   if (as->map_list == NULL && pid > 0)
177     as->map_list = maps_create_list(pid);
178 
179   map = get_map(as->map_list, ip);
180   if (!map)
181     return NULL;
182 
183   if (map->ei.image == NULL)
184     {
185       int ret = elf_map_image(&map->ei, map->path);
186       if (ret < 0)
187         {
188           map->ei.image = NULL;
189           return NULL;
190         }
191     }
192 
193   if (find_cached_map) {
194     cursor->dwarf.cached_map = map;
195     cursor->dwarf.cached_ip = ip;
196   }
197   return map;
198 }
199 
get_previous_instr_sz(unw_cursor_t * cursor)200 unw_word_t get_previous_instr_sz(unw_cursor_t *cursor)
201 {
202   struct cursor *c = (struct cursor *) cursor;
203   unw_addr_space_t as = c->dwarf.as;
204   unw_accessors_t *a = unw_get_accessors (as);
205   unw_word_t ip = c->dwarf.ip;
206   int sz = 4;
207 #if defined(UNW_TARGET_ARM)
208   if (ip)
209     {
210       if (ip & 1)
211         {
212           void *arg;
213           unw_word_t value;
214           arg = c->dwarf.as_arg;
215           // 0xe000f000 ---> machine code of blx Instr (blx label)
216           if (ip < 5 || (*a->access_mem) (as, ip - 5, &value, 0, arg) < 0 ||
217               (value & 0xe000f000) != 0xe000f000)
218             sz = 2;
219         }
220     }
221 #elif defined(UNW_TARGET_ARM64)
222   sz = 4;
223 #elif defined(UNW_TARGET_X86)
224   sz = 1;
225 #elif defined(UNW_TARGET_x86_64)
226   sz = 1;
227 #else
228 // other arch need to be add here.
229 #endif
230   return sz;
231 }
232 
233 
234 #ifndef UNW_REMOTE_ONLY
235 
236 void
tdep_get_exe_image_path(char * path)237 tdep_get_exe_image_path (char *path)
238 {
239   strcpy(path, "/proc/self/exe");
240 }
241 
242 #endif /* !UNW_REMOTE_ONLY */
243