• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 #include <unwindstack/Unwinder.h>
24 
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 
backtrace_fake_clear_all()31 void backtrace_fake_clear_all() {
32   g_fake_backtrace.clear();
33 }
34 
backtrace_fake_add(const std::vector<uintptr_t> & ips)35 void backtrace_fake_add(const std::vector<uintptr_t>& ips) {
36   g_fake_backtrace.push_back(ips);
37 }
38 
backtrace_startup()39 void backtrace_startup() {
40 }
41 
backtrace_shutdown()42 void backtrace_shutdown() {
43 }
44 
backtrace_get(uintptr_t * frames,size_t frame_num)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 
backtrace_log(const uintptr_t * frames,size_t frame_count)57 void backtrace_log(const uintptr_t* frames, size_t frame_count) {
58   for (size_t i = 0; i < frame_count; i++) {
59     error_log("  #%02zd pc %p", i, reinterpret_cast<void*>(frames[i]));
60   }
61 }
62 
63 static std::deque<std::vector<unwindstack::FrameData>> g_fake_local_frame_data;
64 
BacktraceUnwindFakeClearAll()65 void BacktraceUnwindFakeClearAll() {
66   g_fake_local_frame_data.clear();
67 }
68 
BacktraceUnwindFake(const std::vector<unwindstack::FrameData> & frames)69 void BacktraceUnwindFake(const std::vector<unwindstack::FrameData>& frames) {
70   g_fake_local_frame_data.push_back(frames);
71 }
72 
Unwind(std::vector<uintptr_t> * frames,std::vector<unwindstack::FrameData> * info,size_t)73 bool Unwind(std::vector<uintptr_t>* frames, std::vector<unwindstack::FrameData>* info, size_t) {
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 
UnwindLog(const std::vector<unwindstack::FrameData> &)88 void UnwindLog(const std::vector<unwindstack::FrameData>& /*frame_info*/) {}
89