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 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 42 unw_map_cursor_create (unw_map_cursor_t *map_cursor, pid_t pid) 43 { 44 map_cursor->map_list = map_create_list (pid); 45 46 return map_cursor->map_list == NULL; 47 } 48 49 PROTECTED void 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 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 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 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->flags = map_info->flags; 79 unw_map->path = map_info->path; 80 81 map_cursor->cur_map = map_info->next; 82 83 return 1; 84 } 85 86 HIDDEN struct map_info * 87 map_alloc_info (void) 88 { 89 if (!map_init_done) 90 { 91 intrmask_t saved_mask; 92 93 lock_acquire (&map_init_lock, saved_mask); 94 /* Check again under the lock. */ 95 if (!map_init_done) 96 { 97 mempool_init (&map_pool, sizeof(struct map_info), 0); 98 map_init_done = 1; 99 } 100 lock_release (&map_init_lock, saved_mask); 101 } 102 return mempool_alloc (&map_pool); 103 } 104 105 HIDDEN void 106 map_free_info (struct map_info *map) 107 { 108 mempool_free (&map_pool, map); 109 } 110 111 HIDDEN void 112 map_destroy_list (struct map_info *map_info) 113 { 114 struct map_info *map; 115 while (map_info) 116 { 117 map = map_info; 118 map_info = map->next; 119 if (map->ei.image != MAP_FAILED && map->ei.image != NULL) 120 munmap (map->ei.image, map->ei.size); 121 if (map->path) 122 free (map->path); 123 map_free_info (map); 124 } 125 } 126 127 HIDDEN struct map_info * 128 map_find_from_addr (struct map_info *map_list, unw_word_t addr) 129 { 130 while (map_list) 131 { 132 if (addr >= map_list->start && addr < map_list->end) 133 return map_list; 134 map_list = map_list->next; 135 } 136 return NULL; 137 } 138