blob: 05ad12ba6b7891b6fc507c8fa0bad64a5db1fad8 [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() {}
35};
36
37
38class BacktraceMock : public Backtrace {
39 public:
40 BacktraceMock(BacktraceMapMock* map) : Backtrace(0, 0, map) {
41 if (map_ == nullptr) {
42 abort();
43 }
44 }
45 virtual ~BacktraceMock() {}
46
47 virtual bool Unwind(size_t, ucontext_t*) { return false; }
48 virtual bool ReadWord(uintptr_t, word_t*) { return false;}
49
50 virtual std::string GetFunctionNameRaw(uintptr_t, uintptr_t*) { return ""; }
51
52 virtual size_t Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
53 size_t offset = 0;
54 if (last_read_addr_ > 0) {
55 offset = addr - last_read_addr_;
56 }
57 size_t bytes_available = buffer_.size() - offset;
58
59 if (bytes_partial_read_ > 0) {
60 // Do a partial read.
61 if (bytes > bytes_partial_read_) {
62 bytes = bytes_partial_read_;
63 }
64 bytes_partial_read_ -= bytes;
65 } else if (bytes > bytes_available) {
66 bytes = bytes_available;
67 }
68
69 if (bytes > 0) {
70 memcpy(buffer, buffer_.data() + offset, bytes);
71 }
72
73 last_read_addr_ = addr;
74 return bytes;
75 }
76
77 void SetReadData(uint8_t* buffer, size_t bytes) {
78 buffer_.resize(bytes);
79 memcpy(buffer_.data(), buffer, bytes);
80 bytes_partial_read_ = 0;
81 last_read_addr_ = 0;
82 }
83
84 void SetPartialReadAmount(size_t bytes) {
85 if (bytes > buffer_.size()) {
86 abort();
87 }
88 bytes_partial_read_ = bytes;
89 }
90
91 private:
92 std::vector<uint8_t> buffer_;
93 size_t bytes_partial_read_ = 0;
94 uintptr_t last_read_addr_ = 0;
95};
96
97#endif // _DEBUGGERD_TEST_BACKTRACE_MOCK_H