163860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris/*
263860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * Copyright (C) 2015 The Android Open Source Project
363860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris *
463860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * Licensed under the Apache License, Version 2.0 (the "License");
563860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * you may not use this file except in compliance with the License.
663860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * You may obtain a copy of the License at
763860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris *
863860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris *      http://www.apache.org/licenses/LICENSE-2.0
963860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris *
1063860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * Unless required by applicable law or agreed to in writing, software
1163860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * distributed under the License is distributed on an "AS IS" BASIS,
1263860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1363860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * See the License for the specific language governing permissions and
1463860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris * limitations under the License.
1563860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris */
1663860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
1763860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include <stdint.h>
1863860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
1963860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include <deque>
2063860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include <vector>
2163860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include <utility>
2263860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
2363860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include "backtrace.h"
2463860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include "backtrace_fake.h"
2563860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris#include "debug_log.h"
2663860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
2763860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferrisstatic std::deque<std::vector<uintptr_t>> g_fake_backtrace;
2863860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
2963860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferrisvoid backtrace_fake_clear_all() {
3063860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  g_fake_backtrace.clear();
3163860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris}
3263860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
3363860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferrisvoid backtrace_fake_add(const std::vector<uintptr_t>& ips) {
3463860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  g_fake_backtrace.push_back(ips);
3563860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris}
3663860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
3763860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferrisvoid backtrace_startup() {
3863860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris}
3963860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
4063860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferrisvoid backtrace_shutdown() {
4163860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris}
4263860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
4363860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferrissize_t backtrace_get(uintptr_t* frames, size_t frame_num) {
4463860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  if (frame_num == 0 || g_fake_backtrace.size() == 0) {
4563860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris    return 0;
4663860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  }
4763860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
4863860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  size_t ips_size = g_fake_backtrace[0].size();
4963860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  size_t total_frames = (frame_num < ips_size) ? frame_num : ips_size;
5063860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  memcpy(frames, g_fake_backtrace[0].data(), sizeof(uintptr_t) * total_frames);
5163860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  g_fake_backtrace.pop_front();
5263860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  return total_frames;
5363860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris}
5463860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris
557993b80f894db20af4d1d154221c42fea6171a3dChristopher Ferrisvoid backtrace_log(const uintptr_t* frames, size_t frame_count) {
5663860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  for (size_t i = 0; i < frame_count; i++) {
5763860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris    error_log("  #%02zd pc %p", i, reinterpret_cast<void*>(frames[i]));
5863860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris  }
5963860cb8fd1adf3f679b9b4ad876323a8d65cd9dChristopher Ferris}
60