• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define UNW_LOCAL_ONLY
26 #include <libunwind.h>
27 #include "libunwind_i.h"
28 
29 /* Globals to hold the map data for local unwinds. */
30 HIDDEN struct map_info *local_map_list = NULL;
31 HIDDEN int local_map_list_refs = 0;
32 HIDDEN lock_rdwr_var (local_rdwr_lock);
33 
34 PROTECTED void
unw_map_local_cursor_get(unw_map_cursor_t * map_cursor)35 unw_map_local_cursor_get (unw_map_cursor_t *map_cursor)
36 {
37   intrmask_t saved_mask;
38 
39   lock_rdwr_wr_acquire (&local_rdwr_lock, saved_mask);
40   map_cursor->map_list = local_map_list;
41   map_cursor->cur_map = local_map_list;
42   lock_rdwr_release (&local_rdwr_lock, saved_mask);
43 }
44 
45 PROTECTED int
unw_map_local_cursor_valid(unw_map_cursor_t * map_cursor)46 unw_map_local_cursor_valid (unw_map_cursor_t *map_cursor)
47 {
48   if (map_cursor->map_list == local_map_list)
49     return 0;
50   return -1;
51 }
52 
53 PROTECTED int
unw_map_local_create(void)54 unw_map_local_create (void)
55 {
56   intrmask_t saved_mask;
57   int ret_value = 0;
58 
59   lock_rdwr_wr_acquire (&local_rdwr_lock, saved_mask);
60   if (local_map_list_refs == 0)
61     {
62       local_map_list = map_create_list (getpid());
63       if (local_map_list != NULL)
64         local_map_list_refs = 1;
65       else
66         ret_value = -1;
67     }
68   else
69     local_map_list_refs++;
70   lock_rdwr_release (&local_rdwr_lock, saved_mask);
71   return ret_value;
72 }
73 
74 PROTECTED void
unw_map_local_destroy(void)75 unw_map_local_destroy (void)
76 {
77   intrmask_t saved_mask;
78 
79   lock_rdwr_wr_acquire (&local_rdwr_lock, saved_mask);
80   if (local_map_list != NULL && --local_map_list_refs == 0)
81     {
82       map_destroy_list (local_map_list);
83       local_map_list = NULL;
84     }
85   lock_rdwr_release (&local_rdwr_lock, saved_mask);
86 }
87 
88 PROTECTED int
unw_map_local_cursor_get_next(unw_map_cursor_t * map_cursor,unw_map_t * unw_map)89 unw_map_local_cursor_get_next (unw_map_cursor_t *map_cursor, unw_map_t *unw_map)
90 {
91   struct map_info *map_info = map_cursor->cur_map;
92   intrmask_t saved_mask;
93   int ret = 1;
94 
95   if (map_info == NULL)
96     return 0;
97 
98   lock_rdwr_rd_acquire (&local_rdwr_lock, saved_mask);
99   if (map_cursor->map_list != local_map_list)
100     {
101       map_cursor->map_list = local_map_list;
102       ret = -UNW_EINVAL;
103     }
104   else
105     {
106       unw_map->start = map_info->start;
107       unw_map->end = map_info->end;
108       unw_map->flags = map_info->flags;
109       if (map_info->path)
110         unw_map->path = strdup (map_info->path);
111       else
112         unw_map->path = NULL;
113 
114       map_cursor->cur_map = map_info->next;
115     }
116   lock_rdwr_release (&local_rdwr_lock, saved_mask);
117 
118   return ret;
119 }
120