| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [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 | #include <stdint.h> | 
|  | 18 |  | 
|  | 19 | #include <deque> | 
|  | 20 | #include <vector> | 
|  | 21 | #include <utility> | 
|  | 22 |  | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 23 | #include <unwindstack/Unwinder.h> | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 24 |  | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 25 | #include "backtrace.h" | 
|  | 26 | #include "backtrace_fake.h" | 
|  | 27 | #include "debug_log.h" | 
|  | 28 |  | 
|  | 29 | static std::deque<std::vector<uintptr_t>> g_fake_backtrace; | 
|  | 30 |  | 
|  | 31 | void backtrace_fake_clear_all() { | 
|  | 32 | g_fake_backtrace.clear(); | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | void backtrace_fake_add(const std::vector<uintptr_t>& ips) { | 
|  | 36 | g_fake_backtrace.push_back(ips); | 
|  | 37 | } | 
|  | 38 |  | 
|  | 39 | void backtrace_startup() { | 
|  | 40 | } | 
|  | 41 |  | 
|  | 42 | void backtrace_shutdown() { | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | size_t backtrace_get(uintptr_t* frames, size_t frame_num) { | 
|  | 46 | if (frame_num == 0 || g_fake_backtrace.size() == 0) { | 
|  | 47 | return 0; | 
|  | 48 | } | 
|  | 49 |  | 
|  | 50 | size_t ips_size = g_fake_backtrace[0].size(); | 
|  | 51 | size_t total_frames = (frame_num < ips_size) ? frame_num : ips_size; | 
|  | 52 | memcpy(frames, g_fake_backtrace[0].data(), sizeof(uintptr_t) * total_frames); | 
|  | 53 | g_fake_backtrace.pop_front(); | 
|  | 54 | return total_frames; | 
|  | 55 | } | 
|  | 56 |  | 
| Christopher Ferris | 7993b80 | 2016-01-28 18:35:05 -0800 | [diff] [blame] | 57 | void backtrace_log(const uintptr_t* frames, size_t frame_count) { | 
| Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 58 | for (size_t i = 0; i < frame_count; i++) { | 
|  | 59 | error_log("  #%02zd pc %p", i, reinterpret_cast<void*>(frames[i])); | 
|  | 60 | } | 
|  | 61 | } | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 62 |  | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 63 | static std::deque<std::vector<unwindstack::FrameData>> g_fake_local_frame_data; | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | void BacktraceUnwindFakeClearAll() { | 
|  | 66 | g_fake_local_frame_data.clear(); | 
|  | 67 | } | 
|  | 68 |  | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 69 | void BacktraceUnwindFake(const std::vector<unwindstack::FrameData>& frames) { | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 70 | g_fake_local_frame_data.push_back(frames); | 
|  | 71 | } | 
|  | 72 |  | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 73 | bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* info, size_t) { | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 74 | if (g_fake_local_frame_data.empty()) { | 
|  | 75 | return false; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | *info = g_fake_local_frame_data.front(); | 
|  | 79 | g_fake_local_frame_data.pop_front(); | 
|  | 80 | frames->clear(); | 
|  | 81 | for (const auto& frame : *info) { | 
|  | 82 | frames->push_back(frame.pc); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | return true; | 
|  | 86 | } | 
|  | 87 |  | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 88 | void UnwindLog(const std::vector<unwindstack::FrameData>& /*frame_info*/) {} |