blob: d4a2444dc93fee280cc4ecdf94412bd403958a06 [file] [log] [blame]
Christopher Ferris6f3981c2017-07-27 09:29:18 -07001/*
2 * Copyright (C) 2017 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
21#include <backtrace/BacktraceMap.h>
22#include <unwindstack/Elf.h>
23#include <unwindstack/MapInfo.h>
24#include <unwindstack/Maps.h>
25
26#include "UnwindStackMap.h"
27
28//-------------------------------------------------------------------------
29UnwindStackMap::UnwindStackMap(pid_t pid) : BacktraceMap(pid) {}
30
31bool UnwindStackMap::Build() {
32 if (pid_ == 0) {
33 pid_ = getpid();
34 stack_maps_.reset(new unwindstack::LocalMaps);
35 } else {
36 stack_maps_.reset(new unwindstack::RemoteMaps(pid_));
37 }
38
Christopher Ferris5f118512017-09-01 11:17:16 -070039 // Create the process memory object.
40 process_memory_ = unwindstack::Memory::CreateProcessMemory(pid_);
41
Christopher Ferris6f3981c2017-07-27 09:29:18 -070042 if (!stack_maps_->Parse()) {
43 return false;
44 }
45
46 // Iterate through the maps and fill in the backtrace_map_t structure.
47 for (auto& map_info : *stack_maps_) {
48 backtrace_map_t map;
49 map.start = map_info.start;
50 map.end = map_info.end;
51 map.offset = map_info.offset;
52 // Set to -1 so that it is demand loaded.
53 map.load_bias = static_cast<uintptr_t>(-1);
54 map.flags = map_info.flags;
55 map.name = map_info.name;
56
57 maps_.push_back(map);
58 }
59
60 return true;
61}
62
63void UnwindStackMap::FillIn(uintptr_t addr, backtrace_map_t* map) {
64 BacktraceMap::FillIn(addr, map);
65 if (map->load_bias != static_cast<uintptr_t>(-1)) {
66 return;
67 }
68
69 // Fill in the load_bias.
70 unwindstack::MapInfo* map_info = stack_maps_->Find(addr);
71 if (map_info == nullptr) {
72 return;
73 }
Christopher Ferris5f118512017-09-01 11:17:16 -070074 unwindstack::Elf* elf = map_info->GetElf(process_memory_, true);
Christopher Ferris6f3981c2017-07-27 09:29:18 -070075 map->load_bias = elf->GetLoadBias();
76}
77
78//-------------------------------------------------------------------------
79// BacktraceMap create function.
80//-------------------------------------------------------------------------
81BacktraceMap* BacktraceMap::CreateNew(pid_t pid, bool uncached) {
82 BacktraceMap* map;
83
84 if (uncached) {
85 // Force use of the base class to parse the maps when this call is made.
86 map = new BacktraceMap(pid);
87 } else if (pid == getpid()) {
88 map = new UnwindStackMap(0);
89 } else {
90 map = new UnwindStackMap(pid);
91 }
92 if (!map->Build()) {
93 delete map;
94 return nullptr;
95 }
96 return map;
97}