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"
186f9e6901SZachary Turner #include "lldb/Utility/Log.h"
1902706c32SJason Molenda 
20796ac80bSJonas Devlieghere #include <memory>
21796ac80bSJonas Devlieghere 
2202706c32SJason Molenda using namespace lldb;
2302706c32SJason Molenda using namespace lldb_private;
2402706c32SJason Molenda 
25a8ff543cSJason Molenda //  Constructor
26a8ff543cSJason Molenda 
27b9c1b51eSKate Stone HistoryThread::HistoryThread(lldb_private::Process &process, lldb::tid_t tid,
28*b40ee7ffSFred Riss                              std::vector<lldb::addr_t> pcs,
29*b40ee7ffSFred Riss                              bool pcs_are_call_addresses)
30b9c1b51eSKate Stone     : Thread(process, tid, true), m_framelist_mutex(), m_framelist(),
3186df61ccSAlex Langford       m_pcs(pcs), m_extended_unwind_token(LLDB_INVALID_ADDRESS), m_queue_name(),
32b9c1b51eSKate Stone       m_thread_name(), m_originating_unique_thread_id(tid),
33b9c1b51eSKate Stone       m_queue_id(LLDB_INVALID_QUEUE_ID) {
34*b40ee7ffSFred Riss   m_unwinder_up.reset(new HistoryUnwind(*this, pcs, pcs_are_call_addresses));
35ee87e750SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
3663e5fb76SJonas Devlieghere   LLDB_LOGF(log, "%p HistoryThread::HistoryThread", static_cast<void *>(this));
3702706c32SJason Molenda }
3802706c32SJason Molenda 
39a8ff543cSJason Molenda //  Destructor
40a8ff543cSJason Molenda 
41b9c1b51eSKate Stone HistoryThread::~HistoryThread() {
42ee87e750SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
4363e5fb76SJonas Devlieghere   LLDB_LOGF(log, "%p HistoryThread::~HistoryThread (tid=0x%" PRIx64 ")",
44324a1036SSaleem Abdulrasool             static_cast<void *>(this), GetID());
45ee87e750SJason Molenda   DestroyThread();
4602706c32SJason Molenda }
4702706c32SJason Molenda 
48b9c1b51eSKate Stone lldb::RegisterContextSP HistoryThread::GetRegisterContext() {
4902706c32SJason Molenda   RegisterContextSP rctx;
50b9c1b51eSKate Stone   if (m_pcs.size() > 0) {
51796ac80bSJonas Devlieghere     rctx = std::make_shared<RegisterContextHistory>(
52796ac80bSJonas Devlieghere         *this, 0, GetProcess()->GetAddressByteSize(), m_pcs[0]);
5302706c32SJason Molenda   }
5402706c32SJason Molenda   return rctx;
5502706c32SJason Molenda }
5602706c32SJason Molenda 
5702706c32SJason Molenda lldb::RegisterContextSP
58b9c1b51eSKate Stone HistoryThread::CreateRegisterContextForFrame(StackFrame *frame) {
59d5b44036SJonas Devlieghere   return m_unwinder_up->CreateRegisterContextForFrame(frame);
6002706c32SJason Molenda }
6102706c32SJason Molenda 
62b9c1b51eSKate Stone lldb::StackFrameListSP HistoryThread::GetStackFrameList() {
6316ff8604SSaleem Abdulrasool   // FIXME do not throw away the lock after we acquire it..
64d9b22812SKuba Brecka   std::unique_lock<std::mutex> lock(m_framelist_mutex);
6500d7c563SKuba Brecka   lock.unlock();
66248a1305SKonrad Kleine   if (m_framelist.get() == nullptr) {
67796ac80bSJonas Devlieghere     m_framelist =
68796ac80bSJonas Devlieghere         std::make_shared<StackFrameList>(*this, StackFrameListSP(), true);
6902706c32SJason Molenda   }
7002706c32SJason Molenda 
7102706c32SJason Molenda   return m_framelist;
7202706c32SJason Molenda }
738ee9cb58SJason Molenda 
74b9c1b51eSKate Stone uint32_t HistoryThread::GetExtendedBacktraceOriginatingIndexID() {
75b9c1b51eSKate Stone   if (m_originating_unique_thread_id != LLDB_INVALID_THREAD_ID) {
76b9c1b51eSKate Stone     if (GetProcess()->HasAssignedIndexIDToThread(
77b9c1b51eSKate Stone             m_originating_unique_thread_id)) {
78b9c1b51eSKate Stone       return GetProcess()->AssignIndexIDToThread(
79b9c1b51eSKate Stone           m_originating_unique_thread_id);
808ee9cb58SJason Molenda     }
818ee9cb58SJason Molenda   }
828ee9cb58SJason Molenda   return LLDB_INVALID_THREAD_ID;
838ee9cb58SJason Molenda }
84