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 | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 17 | #include <pthread.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 | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | UnwindMap::~UnwindMap() { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 39 | unw_map_cursor_destroy(&map_cursor_); |
| 40 | unw_map_cursor_clear(&map_cursor_); |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 43 | bool UnwindMap::GenerateMap() { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 44 | // Use the map_cursor information to construct the BacktraceMap data |
| 45 | // rather than reparsing /proc/self/maps. |
| 46 | unw_map_cursor_reset(&map_cursor_); |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 47 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 48 | unw_map_t unw_map; |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 49 | while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) { |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 50 | backtrace_map_t map; |
| 51 | |
| 52 | map.start = unw_map.start; |
| 53 | map.end = unw_map.end; |
| 54 | map.flags = unw_map.flags; |
| 55 | map.name = unw_map.path; |
| 56 | |
| 57 | // The maps are in descending order, but we want them in ascending order. |
| 58 | maps_.push_front(map); |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 64 | bool UnwindMap::Build() { |
| 65 | return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap(); |
| 66 | } |
| 67 | |
| 68 | UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) { |
| 69 | } |
| 70 | |
| 71 | UnwindMapLocal::~UnwindMapLocal() { |
| 72 | if (map_created_) { |
| 73 | unw_map_local_destroy(); |
| 74 | unw_map_cursor_clear(&map_cursor_); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | bool UnwindMapLocal::GenerateMap() { |
| 79 | // It's possible for the map to be regenerated while this loop is occurring. |
| 80 | // If that happens, get the map again, but only try at most three times |
| 81 | // before giving up. |
| 82 | for (int i = 0; i < 3; i++) { |
| 83 | maps_.clear(); |
| 84 | |
| 85 | unw_map_local_cursor_get(&map_cursor_); |
| 86 | |
| 87 | unw_map_t unw_map; |
| 88 | int ret; |
| 89 | while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) { |
| 90 | backtrace_map_t map; |
| 91 | |
| 92 | map.start = unw_map.start; |
| 93 | map.end = unw_map.end; |
| 94 | map.flags = unw_map.flags; |
| 95 | map.name = unw_map.path; |
| 96 | |
| 97 | free(unw_map.path); |
| 98 | |
| 99 | // The maps are in descending order, but we want them in ascending order. |
| 100 | maps_.push_front(map); |
| 101 | } |
| 102 | // Check to see if the map changed while getting the data. |
| 103 | if (ret != -UNW_EINVAL) { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | BACK_LOGW("Unable to generate the map."); |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | bool UnwindMapLocal::Build() { |
| 113 | return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();; |
| 114 | } |
| 115 | |
| 116 | const backtrace_map_t* UnwindMapLocal::Find(uintptr_t addr) { |
| 117 | const backtrace_map_t* map = BacktraceMap::Find(addr); |
| 118 | if (!map) { |
| 119 | // Check to see if the underlying map changed and regenerate the map |
| 120 | // if it did. |
| 121 | if (unw_map_local_cursor_valid(&map_cursor_) < 0) { |
| 122 | if (GenerateMap()) { |
| 123 | map = BacktraceMap::Find(addr); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return map; |
| 128 | } |
| 129 | |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 130 | //------------------------------------------------------------------------- |
| 131 | // BacktraceMap create function. |
| 132 | //------------------------------------------------------------------------- |
Christopher Ferris | dda47b7 | 2014-08-04 17:08:46 -0700 | [diff] [blame] | 133 | BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 134 | BacktraceMap* map; |
Christopher Ferris | dda47b7 | 2014-08-04 17:08:46 -0700 | [diff] [blame] | 135 | |
| 136 | if (uncached) { |
| 137 | // Force use of the base class to parse the maps when this call is made. |
| 138 | map = new BacktraceMap(pid); |
| 139 | } else if (pid == getpid()) { |
Christopher Ferris | e296091 | 2014-03-07 19:42:19 -0800 | [diff] [blame] | 140 | map = new UnwindMapLocal(); |
| 141 | } else { |
| 142 | map = new UnwindMap(pid); |
| 143 | } |
Christopher Ferris | df29061 | 2014-01-22 19:21:07 -0800 | [diff] [blame] | 144 | if (!map->Build()) { |
| 145 | delete map; |
| 146 | return NULL; |
| 147 | } |
| 148 | return map; |
| 149 | } |