1//===-- HistoryUnwind.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "lldb/lldb-private.h"
10
11#include "Plugins/Process/Utility/HistoryUnwind.h"
12#include "Plugins/Process/Utility/RegisterContextHistory.h"
13
14#include "lldb/Target/Process.h"
15#include "lldb/Target/StackFrame.h"
16#include "lldb/Target/Target.h"
17#include "lldb/Target/Thread.h"
18
19#include <memory>
20
21using namespace lldb;
22using namespace lldb_private;
23
24// Constructor
25
26HistoryUnwind::HistoryUnwind(Thread &thread, std::vector<lldb::addr_t> pcs,
27                             bool pcs_are_call_addresses)
28    : Unwind(thread), m_pcs(pcs),
29      m_pcs_are_call_addresses(pcs_are_call_addresses) {}
30
31// Destructor
32
33HistoryUnwind::~HistoryUnwind() {}
34
35void HistoryUnwind::DoClear() {
36  std::lock_guard<std::recursive_mutex> guard(m_unwind_mutex);
37  m_pcs.clear();
38}
39
40lldb::RegisterContextSP
41HistoryUnwind::DoCreateRegisterContextForFrame(StackFrame *frame) {
42  RegisterContextSP rctx;
43  if (frame) {
44    addr_t pc = frame->GetFrameCodeAddress().GetLoadAddress(
45        &frame->GetThread()->GetProcess()->GetTarget());
46    if (pc != LLDB_INVALID_ADDRESS) {
47      rctx = std::make_shared<RegisterContextHistory>(
48          *frame->GetThread().get(), frame->GetConcreteFrameIndex(),
49          frame->GetThread()->GetProcess()->GetAddressByteSize(), pc);
50    }
51  }
52  return rctx;
53}
54
55bool HistoryUnwind::DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
56                                          lldb::addr_t &pc,
57                                          bool &behaves_like_zeroth_frame) {
58  // FIXME do not throw away the lock after we acquire it..
59  std::unique_lock<std::recursive_mutex> guard(m_unwind_mutex);
60  guard.unlock();
61  if (frame_idx < m_pcs.size()) {
62    cfa = frame_idx;
63    pc = m_pcs[frame_idx];
64    if (m_pcs_are_call_addresses)
65      behaves_like_zeroth_frame = true;
66    else
67      behaves_like_zeroth_frame = (frame_idx == 0);
68    return true;
69  }
70  return false;
71}
72
73uint32_t HistoryUnwind::DoGetFrameCount() { return m_pcs.size(); }
74