blob: 6104f7e0df6c75eb540aa9c32c4e0f5cac8df8d5 [file] [log] [blame]
Christopher Ferrise8bc77e2015-05-22 14:26:13 -07001/*
2 * Copyright (C) 2015 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#ifndef _DEBUGGERD_TEST_BACKTRACE_MOCK_H
18#define _DEBUGGERD_TEST_BACKTRACE_MOCK_H
19
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/ucontext.h>
24
25#include <string>
26#include <vector>
27
28#include <backtrace/Backtrace.h>
29#include <backtrace/BacktraceMap.h>
30
31class BacktraceMapMock : public BacktraceMap {
32 public:
33 BacktraceMapMock() : BacktraceMap(0) {}
34 virtual ~BacktraceMapMock() {}
Christopher Ferris862fe022015-06-02 14:52:44 -070035
36 void AddMap(backtrace_map_t& map) {
37 maps_.push_back(map);
38 }
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070039};
40
41
42class BacktraceMock : public Backtrace {
43 public:
Chih-Hung Hsieh034c4752016-07-12 13:50:44 -070044 explicit BacktraceMock(BacktraceMapMock* map) : Backtrace(0, 0, map) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070045 if (map_ == nullptr) {
46 abort();
47 }
48 }
49 virtual ~BacktraceMock() {}
50
51 virtual bool Unwind(size_t, ucontext_t*) { return false; }
52 virtual bool ReadWord(uintptr_t, word_t*) { return false;}
53
54 virtual std::string GetFunctionNameRaw(uintptr_t, uintptr_t*) { return ""; }
55
56 virtual size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
57 size_t offset = 0;
58 if (last_read_addr_ > 0) {
59 offset = addr - last_read_addr_;
60 }
61 size_t bytes_available = buffer_.size() - offset;
62
Christopher Ferris456abba2015-07-09 15:35:47 -070063 if (do_partial_read_) {
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070064 // Do a partial read.
65 if (bytes > bytes_partial_read_) {
66 bytes = bytes_partial_read_;
67 }
68 bytes_partial_read_ -= bytes;
Christopher Ferris456abba2015-07-09 15:35:47 -070069 // Only support a single partial read.
70 do_partial_read_ = false;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070071 } else if (bytes > bytes_available) {
72 bytes = bytes_available;
73 }
74
75 if (bytes > 0) {
76 memcpy(buffer, buffer_.data() + offset, bytes);
77 }
78
79 last_read_addr_ = addr;
80 return bytes;
81 }
82
83 void SetReadData(uint8_t* buffer, size_t bytes) {
84 buffer_.resize(bytes);
85 memcpy(buffer_.data(), buffer, bytes);
86 bytes_partial_read_ = 0;
Christopher Ferris456abba2015-07-09 15:35:47 -070087 do_partial_read_ = false;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070088 last_read_addr_ = 0;
89 }
90
91 void SetPartialReadAmount(size_t bytes) {
92 if (bytes > buffer_.size()) {
93 abort();
94 }
95 bytes_partial_read_ = bytes;
Christopher Ferris456abba2015-07-09 15:35:47 -070096 do_partial_read_ = true;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -070097 }
98
99 private:
100 std::vector<uint8_t> buffer_;
101 size_t bytes_partial_read_ = 0;
102 uintptr_t last_read_addr_ = 0;
Christopher Ferris456abba2015-07-09 15:35:47 -0700103 bool do_partial_read_ = false;
Christopher Ferrise8bc77e2015-05-22 14:26:13 -0700104};
105
106#endif // _DEBUGGERD_TEST_BACKTRACE_MOCK_H