1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2014 The Android Open Source Project
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 <libunwind.h>
26 #include "libunwind_i.h"
27
28 HIDDEN int map_init_done = 0;
29 HIDDEN define_lock (map_init_lock);
30 HIDDEN struct mempool map_pool;
31
32 PROTECTED void
unw_map_set(unw_addr_space_t as,unw_map_cursor_t * map_cursor)33 unw_map_set (unw_addr_space_t as, unw_map_cursor_t *map_cursor)
34 {
35 if (map_cursor != NULL)
36 as->map_list = map_cursor->map_list;
37 else
38 as->map_list = NULL;
39 }
40
41 PROTECTED int
unw_map_cursor_create(unw_map_cursor_t * map_cursor,pid_t pid)42 unw_map_cursor_create (unw_map_cursor_t *map_cursor, pid_t pid)
43 {
44 map_cursor->map_list = map_create_list (UNW_MAP_CREATE_REMOTE, pid);
45
46 return map_cursor->map_list == NULL;
47 }
48
49 PROTECTED void
unw_map_cursor_destroy(unw_map_cursor_t * map_cursor)50 unw_map_cursor_destroy (unw_map_cursor_t *map_cursor)
51 {
52 map_destroy_list (map_cursor->map_list);
53 }
54
55 PROTECTED void
unw_map_cursor_reset(unw_map_cursor_t * map_cursor)56 unw_map_cursor_reset (unw_map_cursor_t *map_cursor)
57 {
58 map_cursor->cur_map = map_cursor->map_list;
59 }
60
61 PROTECTED void
unw_map_cursor_clear(unw_map_cursor_t * cursor_map)62 unw_map_cursor_clear (unw_map_cursor_t *cursor_map)
63 {
64 cursor_map->map_list = NULL;
65 cursor_map->cur_map = NULL;
66 }
67
68 PROTECTED int
unw_map_cursor_get_next(unw_map_cursor_t * map_cursor,unw_map_t * unw_map)69 unw_map_cursor_get_next (unw_map_cursor_t *map_cursor, unw_map_t *unw_map)
70 {
71 struct map_info *map_info = map_cursor->cur_map;
72
73 if (map_info == NULL)
74 return 0;
75
76 unw_map->start = map_info->start;
77 unw_map->end = map_info->end;
78 unw_map->offset = map_info->offset;
79 unw_map->load_base = map_info->load_base;
80 unw_map->flags = map_info->flags;
81 unw_map->path = map_info->path;
82
83 map_cursor->cur_map = map_info->next;
84
85 return 1;
86 }
87
88 HIDDEN struct map_info *
map_alloc_info(void)89 map_alloc_info (void)
90 {
91 if (!map_init_done)
92 {
93 intrmask_t saved_mask;
94
95 lock_acquire (&map_init_lock, saved_mask);
96 /* Check again under the lock. */
97 if (!map_init_done)
98 {
99 mempool_init (&map_pool, sizeof(struct map_info), 0);
100 map_init_done = 1;
101 }
102 lock_release (&map_init_lock, saved_mask);
103 }
104 return mempool_alloc (&map_pool);
105 }
106
107 HIDDEN void
map_free_info(struct map_info * map)108 map_free_info (struct map_info *map)
109 {
110 mempool_free (&map_pool, map);
111 }
112
113 HIDDEN void
map_destroy_list(struct map_info * map_info)114 map_destroy_list (struct map_info *map_info)
115 {
116 struct map_info *map;
117 while (map_info)
118 {
119 map = map_info;
120 map_info = map->next;
121 if (map->ei.mapped)
122 munmap (map->ei.u.mapped.image, map->ei.u.mapped.size);
123 if (map->path)
124 free (map->path);
125 if (map->ei.mini_debug_info_data) {
126 Debug(1, "Freed cached .gnu_debugdata");
127 free (map->ei.mini_debug_info_data);
128 }
129 map_free_info (map);
130 }
131 }
132
133 HIDDEN struct map_info *
map_find_from_addr(struct map_info * map_list,unw_word_t addr)134 map_find_from_addr (struct map_info *map_list, unw_word_t addr)
135 {
136 while (map_list)
137 {
138 if (addr >= map_list->start && addr < map_list->end)
139 return map_list;
140 map_list = map_list->next;
141 }
142 return NULL;
143 }
144