180814287SRaphael Isemann //===-- HistoryUnwind.cpp -------------------------------------------------===//
202706c32SJason Molenda //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
602706c32SJason Molenda //
702706c32SJason Molenda //===----------------------------------------------------------------------===//
802706c32SJason Molenda 
902706c32SJason Molenda #include "lldb/lldb-private.h"
1002706c32SJason Molenda 
1102706c32SJason Molenda #include "Plugins/Process/Utility/HistoryUnwind.h"
12b9c1b51eSKate Stone #include "Plugins/Process/Utility/RegisterContextHistory.h"
1302706c32SJason Molenda 
1402706c32SJason Molenda #include "lldb/Target/Process.h"
15b9c1b51eSKate Stone #include "lldb/Target/StackFrame.h"
1602706c32SJason Molenda #include "lldb/Target/Target.h"
17b9c1b51eSKate Stone #include "lldb/Target/Thread.h"
1802706c32SJason Molenda 
19796ac80bSJonas Devlieghere #include <memory>
20796ac80bSJonas Devlieghere 
2102706c32SJason Molenda using namespace lldb;
2202706c32SJason Molenda using namespace lldb_private;
2302706c32SJason Molenda 
24a8ff543cSJason Molenda // Constructor
25a8ff543cSJason Molenda 
HistoryUnwind(Thread & thread,std::vector<lldb::addr_t> pcs,bool pcs_are_call_addresses)26b40ee7ffSFred Riss HistoryUnwind::HistoryUnwind(Thread &thread, std::vector<lldb::addr_t> pcs,
27b40ee7ffSFred Riss                              bool pcs_are_call_addresses)
28b40ee7ffSFred Riss     : Unwind(thread), m_pcs(pcs),
29b40ee7ffSFred Riss       m_pcs_are_call_addresses(pcs_are_call_addresses) {}
3002706c32SJason Molenda 
31a8ff543cSJason Molenda // Destructor
32a8ff543cSJason Molenda 
33*fd2433e1SJonas Devlieghere HistoryUnwind::~HistoryUnwind() = default;
3402706c32SJason Molenda 
DoClear()35b9c1b51eSKate Stone void HistoryUnwind::DoClear() {
3616ff8604SSaleem Abdulrasool   std::lock_guard<std::recursive_mutex> guard(m_unwind_mutex);
3702706c32SJason Molenda   m_pcs.clear();
3802706c32SJason Molenda }
3902706c32SJason Molenda 
4002706c32SJason Molenda lldb::RegisterContextSP
DoCreateRegisterContextForFrame(StackFrame * frame)41b9c1b51eSKate Stone HistoryUnwind::DoCreateRegisterContextForFrame(StackFrame *frame) {
4202706c32SJason Molenda   RegisterContextSP rctx;
43b9c1b51eSKate Stone   if (frame) {
44b9c1b51eSKate Stone     addr_t pc = frame->GetFrameCodeAddress().GetLoadAddress(
45b9c1b51eSKate Stone         &frame->GetThread()->GetProcess()->GetTarget());
46b9c1b51eSKate Stone     if (pc != LLDB_INVALID_ADDRESS) {
47796ac80bSJonas Devlieghere       rctx = std::make_shared<RegisterContextHistory>(
48b9c1b51eSKate Stone           *frame->GetThread().get(), frame->GetConcreteFrameIndex(),
49796ac80bSJonas Devlieghere           frame->GetThread()->GetProcess()->GetAddressByteSize(), pc);
5002706c32SJason Molenda     }
5102706c32SJason Molenda   }
5202706c32SJason Molenda   return rctx;
5302706c32SJason Molenda }
5402706c32SJason Molenda 
DoGetFrameInfoAtIndex(uint32_t frame_idx,lldb::addr_t & cfa,lldb::addr_t & pc,bool & behaves_like_zeroth_frame)55b9c1b51eSKate Stone bool HistoryUnwind::DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
5631e6dbe1SJoseph Tremoulet                                           lldb::addr_t &pc,
5731e6dbe1SJoseph Tremoulet                                           bool &behaves_like_zeroth_frame) {
5816ff8604SSaleem Abdulrasool   // FIXME do not throw away the lock after we acquire it..
59d9b22812SKuba Brecka   std::unique_lock<std::recursive_mutex> guard(m_unwind_mutex);
6000d7c563SKuba Brecka   guard.unlock();
61b9c1b51eSKate Stone   if (frame_idx < m_pcs.size()) {
6202706c32SJason Molenda     cfa = frame_idx;
6302706c32SJason Molenda     pc = m_pcs[frame_idx];
64b40ee7ffSFred Riss     if (m_pcs_are_call_addresses)
65b40ee7ffSFred Riss       behaves_like_zeroth_frame = true;
66b40ee7ffSFred Riss     else
6731e6dbe1SJoseph Tremoulet       behaves_like_zeroth_frame = (frame_idx == 0);
6802706c32SJason Molenda     return true;
6902706c32SJason Molenda   }
7002706c32SJason Molenda   return false;
7102706c32SJason Molenda }
7202706c32SJason Molenda 
DoGetFrameCount()73b9c1b51eSKate Stone uint32_t HistoryUnwind::DoGetFrameCount() { return m_pcs.size(); }
74