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