| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2014 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 17 | #include <stdint.h> | 
| Dan Albert | ac2fe7e | 2014-05-21 18:21:02 -0700 | [diff] [blame] | 18 | #include <stdlib.h> | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 19 | #include <sys/types.h> | 
 | 20 | #include <unistd.h> | 
 | 21 |  | 
 | 22 | #include <backtrace/BacktraceMap.h> | 
 | 23 |  | 
 | 24 | #include <libunwind.h> | 
 | 25 |  | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 26 | #include "BacktraceLog.h" | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 27 | #include "UnwindMap.h" | 
 | 28 |  | 
 | 29 | //------------------------------------------------------------------------- | 
 | 30 | // libunwind has a single shared address space for the current process | 
 | 31 | // aka local. If multiple maps are created for the current pid, then | 
 | 32 | // only update the local address space once, and keep a reference count | 
 | 33 | // of maps using the same map cursor. | 
 | 34 | //------------------------------------------------------------------------- | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 35 | UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) { | 
| Christopher Ferris | d4c8843 | 2016-02-05 11:07:12 -0800 | [diff] [blame] | 36 |   unw_map_cursor_clear(&map_cursor_); | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 37 | } | 
 | 38 |  | 
| Christopher Ferris | d4c8843 | 2016-02-05 11:07:12 -0800 | [diff] [blame] | 39 | UnwindMapRemote::UnwindMapRemote(pid_t pid) : UnwindMap(pid) { | 
 | 40 | } | 
 | 41 |  | 
 | 42 | UnwindMapRemote::~UnwindMapRemote() { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 43 |   unw_map_cursor_destroy(&map_cursor_); | 
 | 44 |   unw_map_cursor_clear(&map_cursor_); | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 45 | } | 
 | 46 |  | 
| Christopher Ferris | d4c8843 | 2016-02-05 11:07:12 -0800 | [diff] [blame] | 47 | bool UnwindMapRemote::GenerateMap() { | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 48 |   // Use the map_cursor information to construct the BacktraceMap data | 
 | 49 |   // rather than reparsing /proc/self/maps. | 
 | 50 |   unw_map_cursor_reset(&map_cursor_); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 51 |  | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 52 |   unw_map_t unw_map; | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 53 |   while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) { | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 54 |     backtrace_map_t map; | 
 | 55 |  | 
 | 56 |     map.start = unw_map.start; | 
 | 57 |     map.end = unw_map.end; | 
| Christopher Ferris | af67fb2 | 2015-05-06 12:50:09 -0700 | [diff] [blame] | 58 |     map.offset = unw_map.offset; | 
| Christopher Ferris | 329ed7d | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 59 |     map.load_base = unw_map.load_base; | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 60 |     map.flags = unw_map.flags; | 
 | 61 |     map.name = unw_map.path; | 
 | 62 |  | 
 | 63 |     // The maps are in descending order, but we want them in ascending order. | 
 | 64 |     maps_.push_front(map); | 
 | 65 |   } | 
 | 66 |  | 
 | 67 |   return true; | 
 | 68 | } | 
 | 69 |  | 
| Christopher Ferris | d4c8843 | 2016-02-05 11:07:12 -0800 | [diff] [blame] | 70 | bool UnwindMapRemote::Build() { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 71 |   return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap(); | 
 | 72 | } | 
 | 73 |  | 
 | 74 | UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) { | 
 | 75 | } | 
 | 76 |  | 
 | 77 | UnwindMapLocal::~UnwindMapLocal() { | 
 | 78 |   if (map_created_) { | 
 | 79 |     unw_map_local_destroy(); | 
 | 80 |     unw_map_cursor_clear(&map_cursor_); | 
 | 81 |   } | 
 | 82 | } | 
 | 83 |  | 
 | 84 | bool UnwindMapLocal::GenerateMap() { | 
 | 85 |   // It's possible for the map to be regenerated while this loop is occurring. | 
 | 86 |   // If that happens, get the map again, but only try at most three times | 
 | 87 |   // before giving up. | 
 | 88 |   for (int i = 0; i < 3; i++) { | 
 | 89 |     maps_.clear(); | 
 | 90 |  | 
| Christopher Ferris | d4c8843 | 2016-02-05 11:07:12 -0800 | [diff] [blame] | 91 |     // Save the map data retrieved so we can tell if it changes. | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 92 |     unw_map_local_cursor_get(&map_cursor_); | 
 | 93 |  | 
 | 94 |     unw_map_t unw_map; | 
 | 95 |     int ret; | 
 | 96 |     while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) { | 
 | 97 |       backtrace_map_t map; | 
 | 98 |  | 
 | 99 |       map.start = unw_map.start; | 
 | 100 |       map.end = unw_map.end; | 
| Christopher Ferris | af67fb2 | 2015-05-06 12:50:09 -0700 | [diff] [blame] | 101 |       map.offset = unw_map.offset; | 
| Christopher Ferris | 329ed7d | 2015-05-01 15:02:03 -0700 | [diff] [blame] | 102 |       map.load_base = unw_map.load_base; | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 103 |       map.flags = unw_map.flags; | 
 | 104 |       map.name = unw_map.path; | 
 | 105 |  | 
 | 106 |       free(unw_map.path); | 
 | 107 |  | 
 | 108 |       // The maps are in descending order, but we want them in ascending order. | 
 | 109 |       maps_.push_front(map); | 
 | 110 |     } | 
 | 111 |     // Check to see if the map changed while getting the data. | 
 | 112 |     if (ret != -UNW_EINVAL) { | 
 | 113 |       return true; | 
 | 114 |     } | 
 | 115 |   } | 
 | 116 |  | 
 | 117 |   BACK_LOGW("Unable to generate the map."); | 
 | 118 |   return false; | 
 | 119 | } | 
 | 120 |  | 
 | 121 | bool UnwindMapLocal::Build() { | 
 | 122 |   return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();; | 
 | 123 | } | 
 | 124 |  | 
| Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 125 | void UnwindMapLocal::FillIn(uintptr_t addr, backtrace_map_t* map) { | 
 | 126 |   BacktraceMap::FillIn(addr, map); | 
 | 127 |   if (!IsValid(*map)) { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 128 |     // Check to see if the underlying map changed and regenerate the map | 
 | 129 |     // if it did. | 
 | 130 |     if (unw_map_local_cursor_valid(&map_cursor_) < 0) { | 
 | 131 |       if (GenerateMap()) { | 
| Christopher Ferris | 12385e3 | 2015-02-06 13:22:01 -0800 | [diff] [blame] | 132 |         BacktraceMap::FillIn(addr, map); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 133 |       } | 
 | 134 |     } | 
 | 135 |   } | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 136 | } | 
 | 137 |  | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 138 | //------------------------------------------------------------------------- | 
 | 139 | // BacktraceMap create function. | 
 | 140 | //------------------------------------------------------------------------- | 
| Christopher Ferris | dda47b7 | 2014-08-04 17:08:46 -0700 | [diff] [blame] | 141 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 142 |   BacktraceMap* map; | 
| Christopher Ferris | dda47b7 | 2014-08-04 17:08:46 -0700 | [diff] [blame] | 143 |  | 
 | 144 |   if (uncached) { | 
 | 145 |     // Force use of the base class to parse the maps when this call is made. | 
 | 146 |     map = new BacktraceMap(pid); | 
 | 147 |   } else if (pid == getpid()) { | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 148 |     map = new UnwindMapLocal(); | 
 | 149 |   } else { | 
| Christopher Ferris | d4c8843 | 2016-02-05 11:07:12 -0800 | [diff] [blame] | 150 |     map = new UnwindMapRemote(pid); | 
| Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 151 |   } | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 152 |   if (!map->Build()) { | 
 | 153 |     delete map; | 
| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 154 |     return nullptr; | 
| Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 155 |   } | 
 | 156 |   return map; | 
 | 157 | } |