| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 1 | /* | 
 | 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 |  | 
 | 31 | class BacktraceMapMock : public BacktraceMap { | 
 | 32 |  public: | 
 | 33 |   BacktraceMapMock() : BacktraceMap(0) {} | 
 | 34 |   virtual ~BacktraceMapMock() {} | 
| Christopher Ferris | 862fe02 | 2015-06-02 14:52:44 -0700 | [diff] [blame] | 35 |  | 
 | 36 |   void AddMap(backtrace_map_t& map) { | 
 | 37 |     maps_.push_back(map); | 
 | 38 |   } | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 39 | }; | 
 | 40 |  | 
 | 41 |  | 
 | 42 | class BacktraceMock : public Backtrace { | 
 | 43 |  public: | 
| Chih-Hung Hsieh | 034c475 | 2016-07-12 13:50:44 -0700 | [diff] [blame] | 44 |   explicit BacktraceMock(BacktraceMapMock* map) : Backtrace(0, 0, map) { | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 45 |     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 Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 63 |     if (do_partial_read_) { | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 64 |       // Do a partial read. | 
 | 65 |       if (bytes > bytes_partial_read_) { | 
 | 66 |         bytes = bytes_partial_read_; | 
 | 67 |       } | 
 | 68 |       bytes_partial_read_ -= bytes; | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 69 |       // Only support a single partial read. | 
 | 70 |       do_partial_read_ = false; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 71 |     } 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 Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 87 |     do_partial_read_ = false; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 88 |     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 Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 96 |     do_partial_read_ = true; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 97 |   } | 
 | 98 |  | 
 | 99 |  private: | 
 | 100 |   std::vector<uint8_t> buffer_; | 
 | 101 |   size_t bytes_partial_read_ = 0; | 
 | 102 |   uintptr_t last_read_addr_ = 0; | 
| Christopher Ferris | 456abba | 2015-07-09 15:35:47 -0700 | [diff] [blame] | 103 |   bool do_partial_read_ = false; | 
| Christopher Ferris | e8bc77e | 2015-05-22 14:26:13 -0700 | [diff] [blame] | 104 | }; | 
 | 105 |  | 
 | 106 | #endif //  _DEBUGGERD_TEST_BACKTRACE_MOCK_H |