180814287SRaphael Isemann //===-- HistoryThread.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/HistoryThread.h"
12796ac80bSJonas Devlieghere 
13b9c1b51eSKate Stone #include "Plugins/Process/Utility/HistoryUnwind.h"
1402706c32SJason Molenda #include "Plugins/Process/Utility/RegisterContextHistory.h"
1502706c32SJason Molenda 
1602706c32SJason Molenda #include "lldb/Target/Process.h"
17b9c1b51eSKate Stone #include "lldb/Target/StackFrameList.h"
18*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
196f9e6901SZachary Turner #include "lldb/Utility/Log.h"
2002706c32SJason Molenda 
21796ac80bSJonas Devlieghere #include <memory>
22796ac80bSJonas Devlieghere 
2302706c32SJason Molenda using namespace lldb;
2402706c32SJason Molenda using namespace lldb_private;
2502706c32SJason Molenda 
26a8ff543cSJason Molenda //  Constructor
27a8ff543cSJason Molenda 
HistoryThread(lldb_private::Process & process,lldb::tid_t tid,std::vector<lldb::addr_t> pcs,bool pcs_are_call_addresses)28b9c1b51eSKate Stone HistoryThread::HistoryThread(lldb_private::Process &process, lldb::tid_t tid,
29b40ee7ffSFred Riss                              std::vector<lldb::addr_t> pcs,
30b40ee7ffSFred Riss                              bool pcs_are_call_addresses)
31b9c1b51eSKate Stone     : Thread(process, tid, true), m_framelist_mutex(), m_framelist(),
3286df61ccSAlex Langford       m_pcs(pcs), m_extended_unwind_token(LLDB_INVALID_ADDRESS), m_queue_name(),
33b9c1b51eSKate Stone       m_thread_name(), m_originating_unique_thread_id(tid),
34b9c1b51eSKate Stone       m_queue_id(LLDB_INVALID_QUEUE_ID) {
3506412daeSJonas Devlieghere   m_unwinder_up =
3606412daeSJonas Devlieghere       std::make_unique<HistoryUnwind>(*this, pcs, pcs_are_call_addresses);
37a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Object);
3863e5fb76SJonas Devlieghere   LLDB_LOGF(log, "%p HistoryThread::HistoryThread", static_cast<void *>(this));
3902706c32SJason Molenda }
4002706c32SJason Molenda 
41a8ff543cSJason Molenda //  Destructor
42a8ff543cSJason Molenda 
~HistoryThread()43b9c1b51eSKate Stone HistoryThread::~HistoryThread() {
44a007a6d8SPavel Labath   Log *log = GetLog(LLDBLog::Object);
4563e5fb76SJonas Devlieghere   LLDB_LOGF(log, "%p HistoryThread::~HistoryThread (tid=0x%" PRIx64 ")",
46324a1036SSaleem Abdulrasool             static_cast<void *>(this), GetID());
47ee87e750SJason Molenda   DestroyThread();
4802706c32SJason Molenda }
4902706c32SJason Molenda 
GetRegisterContext()50b9c1b51eSKate Stone lldb::RegisterContextSP HistoryThread::GetRegisterContext() {
5102706c32SJason Molenda   RegisterContextSP rctx;
52b9c1b51eSKate Stone   if (m_pcs.size() > 0) {
53796ac80bSJonas Devlieghere     rctx = std::make_shared<RegisterContextHistory>(
54796ac80bSJonas Devlieghere         *this, 0, GetProcess()->GetAddressByteSize(), m_pcs[0]);
5502706c32SJason Molenda   }
5602706c32SJason Molenda   return rctx;
5702706c32SJason Molenda }
5802706c32SJason Molenda 
5902706c32SJason Molenda lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)60b9c1b51eSKate Stone HistoryThread::CreateRegisterContextForFrame(StackFrame *frame) {
61d5b44036SJonas Devlieghere   return m_unwinder_up->CreateRegisterContextForFrame(frame);
6202706c32SJason Molenda }
6302706c32SJason Molenda 
GetStackFrameList()64b9c1b51eSKate Stone lldb::StackFrameListSP HistoryThread::GetStackFrameList() {
6516ff8604SSaleem Abdulrasool   // FIXME do not throw away the lock after we acquire it..
66d9b22812SKuba Brecka   std::unique_lock<std::mutex> lock(m_framelist_mutex);
6700d7c563SKuba Brecka   lock.unlock();
68248a1305SKonrad Kleine   if (m_framelist.get() == nullptr) {
69796ac80bSJonas Devlieghere     m_framelist =
70796ac80bSJonas Devlieghere         std::make_shared<StackFrameList>(*this, StackFrameListSP(), true);
7102706c32SJason Molenda   }
7202706c32SJason Molenda 
7302706c32SJason Molenda   return m_framelist;
7402706c32SJason Molenda }
758ee9cb58SJason Molenda 
GetExtendedBacktraceOriginatingIndexID()76b9c1b51eSKate Stone uint32_t HistoryThread::GetExtendedBacktraceOriginatingIndexID() {
77b9c1b51eSKate Stone   if (m_originating_unique_thread_id != LLDB_INVALID_THREAD_ID) {
78b9c1b51eSKate Stone     if (GetProcess()->HasAssignedIndexIDToThread(
79b9c1b51eSKate Stone             m_originating_unique_thread_id)) {
80b9c1b51eSKate Stone       return GetProcess()->AssignIndexIDToThread(
81b9c1b51eSKate Stone           m_originating_unique_thread_id);
828ee9cb58SJason Molenda     }
838ee9cb58SJason Molenda   }
848ee9cb58SJason Molenda   return LLDB_INVALID_THREAD_ID;
858ee9cb58SJason Molenda }
86