xref: /aosp_15_r20/bionic/libc/malloc_debug/tests/backtrace_fake.cpp (revision 8d67ca893c1523eb926b9080dbe4e2ffd2a27ba1)
1*8d67ca89SAndroid Build Coastguard Worker /*
2*8d67ca89SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*8d67ca89SAndroid Build Coastguard Worker  *
4*8d67ca89SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8d67ca89SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8d67ca89SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8d67ca89SAndroid Build Coastguard Worker  *
8*8d67ca89SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8d67ca89SAndroid Build Coastguard Worker  *
10*8d67ca89SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8d67ca89SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8d67ca89SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8d67ca89SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8d67ca89SAndroid Build Coastguard Worker  * limitations under the License.
15*8d67ca89SAndroid Build Coastguard Worker  */
16*8d67ca89SAndroid Build Coastguard Worker 
17*8d67ca89SAndroid Build Coastguard Worker #include <stdint.h>
18*8d67ca89SAndroid Build Coastguard Worker 
19*8d67ca89SAndroid Build Coastguard Worker #include <deque>
20*8d67ca89SAndroid Build Coastguard Worker #include <vector>
21*8d67ca89SAndroid Build Coastguard Worker #include <utility>
22*8d67ca89SAndroid Build Coastguard Worker 
23*8d67ca89SAndroid Build Coastguard Worker #include <unwindstack/Unwinder.h>
24*8d67ca89SAndroid Build Coastguard Worker 
25*8d67ca89SAndroid Build Coastguard Worker #include "backtrace.h"
26*8d67ca89SAndroid Build Coastguard Worker #include "backtrace_fake.h"
27*8d67ca89SAndroid Build Coastguard Worker #include "debug_log.h"
28*8d67ca89SAndroid Build Coastguard Worker 
29*8d67ca89SAndroid Build Coastguard Worker static std::deque<std::vector<uintptr_t>> g_fake_backtrace;
30*8d67ca89SAndroid Build Coastguard Worker 
backtrace_fake_clear_all()31*8d67ca89SAndroid Build Coastguard Worker void backtrace_fake_clear_all() {
32*8d67ca89SAndroid Build Coastguard Worker   g_fake_backtrace.clear();
33*8d67ca89SAndroid Build Coastguard Worker }
34*8d67ca89SAndroid Build Coastguard Worker 
backtrace_fake_add(const std::vector<uintptr_t> & ips)35*8d67ca89SAndroid Build Coastguard Worker void backtrace_fake_add(const std::vector<uintptr_t>& ips) {
36*8d67ca89SAndroid Build Coastguard Worker   g_fake_backtrace.push_back(ips);
37*8d67ca89SAndroid Build Coastguard Worker }
38*8d67ca89SAndroid Build Coastguard Worker 
backtrace_startup()39*8d67ca89SAndroid Build Coastguard Worker void backtrace_startup() {
40*8d67ca89SAndroid Build Coastguard Worker }
41*8d67ca89SAndroid Build Coastguard Worker 
backtrace_shutdown()42*8d67ca89SAndroid Build Coastguard Worker void backtrace_shutdown() {
43*8d67ca89SAndroid Build Coastguard Worker }
44*8d67ca89SAndroid Build Coastguard Worker 
backtrace_get(uintptr_t * frames,size_t frame_num)45*8d67ca89SAndroid Build Coastguard Worker size_t backtrace_get(uintptr_t* frames, size_t frame_num) {
46*8d67ca89SAndroid Build Coastguard Worker   if (frame_num == 0 || g_fake_backtrace.size() == 0) {
47*8d67ca89SAndroid Build Coastguard Worker     return 0;
48*8d67ca89SAndroid Build Coastguard Worker   }
49*8d67ca89SAndroid Build Coastguard Worker 
50*8d67ca89SAndroid Build Coastguard Worker   size_t ips_size = g_fake_backtrace[0].size();
51*8d67ca89SAndroid Build Coastguard Worker   size_t total_frames = (frame_num < ips_size) ? frame_num : ips_size;
52*8d67ca89SAndroid Build Coastguard Worker   memcpy(frames, g_fake_backtrace[0].data(), sizeof(uintptr_t) * total_frames);
53*8d67ca89SAndroid Build Coastguard Worker   g_fake_backtrace.pop_front();
54*8d67ca89SAndroid Build Coastguard Worker   return total_frames;
55*8d67ca89SAndroid Build Coastguard Worker }
56*8d67ca89SAndroid Build Coastguard Worker 
backtrace_log(const uintptr_t * frames,size_t frame_count)57*8d67ca89SAndroid Build Coastguard Worker void backtrace_log(const uintptr_t* frames, size_t frame_count) {
58*8d67ca89SAndroid Build Coastguard Worker   for (size_t i = 0; i < frame_count; i++) {
59*8d67ca89SAndroid Build Coastguard Worker     error_log("  #%02zd pc %p", i, reinterpret_cast<void*>(frames[i]));
60*8d67ca89SAndroid Build Coastguard Worker   }
61*8d67ca89SAndroid Build Coastguard Worker }
62*8d67ca89SAndroid Build Coastguard Worker 
63*8d67ca89SAndroid Build Coastguard Worker static std::deque<std::vector<unwindstack::FrameData>> g_fake_local_frame_data;
64*8d67ca89SAndroid Build Coastguard Worker 
BacktraceUnwindFakeClearAll()65*8d67ca89SAndroid Build Coastguard Worker void BacktraceUnwindFakeClearAll() {
66*8d67ca89SAndroid Build Coastguard Worker   g_fake_local_frame_data.clear();
67*8d67ca89SAndroid Build Coastguard Worker }
68*8d67ca89SAndroid Build Coastguard Worker 
BacktraceUnwindFake(const std::vector<unwindstack::FrameData> & frames)69*8d67ca89SAndroid Build Coastguard Worker void BacktraceUnwindFake(const std::vector<unwindstack::FrameData>& frames) {
70*8d67ca89SAndroid Build Coastguard Worker   g_fake_local_frame_data.push_back(frames);
71*8d67ca89SAndroid Build Coastguard Worker }
72*8d67ca89SAndroid Build Coastguard Worker 
Unwind(std::vector<uintptr_t> * frames,std::vector<unwindstack::FrameData> * info,size_t)73*8d67ca89SAndroid Build Coastguard Worker bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* info, size_t) {
74*8d67ca89SAndroid Build Coastguard Worker   if (g_fake_local_frame_data.empty()) {
75*8d67ca89SAndroid Build Coastguard Worker     return false;
76*8d67ca89SAndroid Build Coastguard Worker   }
77*8d67ca89SAndroid Build Coastguard Worker 
78*8d67ca89SAndroid Build Coastguard Worker   *info = g_fake_local_frame_data.front();
79*8d67ca89SAndroid Build Coastguard Worker   g_fake_local_frame_data.pop_front();
80*8d67ca89SAndroid Build Coastguard Worker   frames->clear();
81*8d67ca89SAndroid Build Coastguard Worker   for (const auto& frame : *info) {
82*8d67ca89SAndroid Build Coastguard Worker     frames->push_back(frame.pc);
83*8d67ca89SAndroid Build Coastguard Worker   }
84*8d67ca89SAndroid Build Coastguard Worker 
85*8d67ca89SAndroid Build Coastguard Worker   return true;
86*8d67ca89SAndroid Build Coastguard Worker }
87*8d67ca89SAndroid Build Coastguard Worker 
UnwindLog(const std::vector<unwindstack::FrameData> &)88*8d67ca89SAndroid Build Coastguard Worker void UnwindLog(const std::vector<unwindstack::FrameData>& /*frame_info*/) {}
89