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 "backtrace.h"
24 #include "backtrace_fake.h"
25 #include "debug_log.h"
26 
27 static std::deque<std::vector<uintptr_t>> g_fake_backtrace;
28 
backtrace_fake_clear_all()29 void backtrace_fake_clear_all() {
30   g_fake_backtrace.clear();
31 }
32 
backtrace_fake_add(const std::vector<uintptr_t> & ips)33 void backtrace_fake_add(const std::vector<uintptr_t>& ips) {
34   g_fake_backtrace.push_back(ips);
35 }
36 
backtrace_startup()37 void backtrace_startup() {
38 }
39 
backtrace_shutdown()40 void backtrace_shutdown() {
41 }
42 
backtrace_get(uintptr_t * frames,size_t frame_num)43 size_t backtrace_get(uintptr_t* frames, size_t frame_num) {
44   if (frame_num == 0 || g_fake_backtrace.size() == 0) {
45     return 0;
46   }
47 
48   size_t ips_size = g_fake_backtrace[0].size();
49   size_t total_frames = (frame_num < ips_size) ? frame_num : ips_size;
50   memcpy(frames, g_fake_backtrace[0].data(), sizeof(uintptr_t) * total_frames);
51   g_fake_backtrace.pop_front();
52   return total_frames;
53 }
54 
backtrace_log(const uintptr_t * frames,size_t frame_count)55 void backtrace_log(const uintptr_t* frames, size_t frame_count) {
56   for (size_t i = 0; i < frame_count; i++) {
57     error_log("  #%02zd pc %p", i, reinterpret_cast<void*>(frames[i]));
58   }
59 }
60