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