102706c32SJason Molenda //===-- HistoryThread.cpp ---------------------------------------*- C++ -*-===//
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,
2886df61ccSAlex Langford                              std::vector<lldb::addr_t> pcs)
29b9c1b51eSKate Stone     : Thread(process, tid, true), m_framelist_mutex(), m_framelist(),
3086df61ccSAlex Langford       m_pcs(pcs), m_extended_unwind_token(LLDB_INVALID_ADDRESS), m_queue_name(),
31b9c1b51eSKate Stone       m_thread_name(), m_originating_unique_thread_id(tid),
32b9c1b51eSKate Stone       m_queue_id(LLDB_INVALID_QUEUE_ID) {
3386df61ccSAlex Langford   m_unwinder_up.reset(new HistoryUnwind(*this, pcs));
34ee87e750SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
35*63e5fb76SJonas Devlieghere   LLDB_LOGF(log, "%p HistoryThread::HistoryThread", static_cast<void *>(this));
3602706c32SJason Molenda }
3702706c32SJason Molenda 
38a8ff543cSJason Molenda //  Destructor
39a8ff543cSJason Molenda 
40b9c1b51eSKate Stone HistoryThread::~HistoryThread() {
41ee87e750SJason Molenda   Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
42*63e5fb76SJonas Devlieghere   LLDB_LOGF(log, "%p HistoryThread::~HistoryThread (tid=0x%" PRIx64 ")",
43324a1036SSaleem Abdulrasool             static_cast<void *>(this), GetID());
44ee87e750SJason Molenda   DestroyThread();
4502706c32SJason Molenda }
4602706c32SJason Molenda 
47b9c1b51eSKate Stone lldb::RegisterContextSP HistoryThread::GetRegisterContext() {
4802706c32SJason Molenda   RegisterContextSP rctx;
49b9c1b51eSKate Stone   if (m_pcs.size() > 0) {
50796ac80bSJonas Devlieghere     rctx = std::make_shared<RegisterContextHistory>(
51796ac80bSJonas Devlieghere         *this, 0, GetProcess()->GetAddressByteSize(), m_pcs[0]);
5202706c32SJason Molenda   }
5302706c32SJason Molenda   return rctx;
5402706c32SJason Molenda }
5502706c32SJason Molenda 
5602706c32SJason Molenda lldb::RegisterContextSP
57b9c1b51eSKate Stone HistoryThread::CreateRegisterContextForFrame(StackFrame *frame) {
58d5b44036SJonas Devlieghere   return m_unwinder_up->CreateRegisterContextForFrame(frame);
5902706c32SJason Molenda }
6002706c32SJason Molenda 
61b9c1b51eSKate Stone lldb::StackFrameListSP HistoryThread::GetStackFrameList() {
6216ff8604SSaleem Abdulrasool   // FIXME do not throw away the lock after we acquire it..
63d9b22812SKuba Brecka   std::unique_lock<std::mutex> lock(m_framelist_mutex);
6400d7c563SKuba Brecka   lock.unlock();
65248a1305SKonrad Kleine   if (m_framelist.get() == nullptr) {
66796ac80bSJonas Devlieghere     m_framelist =
67796ac80bSJonas Devlieghere         std::make_shared<StackFrameList>(*this, StackFrameListSP(), true);
6802706c32SJason Molenda   }
6902706c32SJason Molenda 
7002706c32SJason Molenda   return m_framelist;
7102706c32SJason Molenda }
728ee9cb58SJason Molenda 
73b9c1b51eSKate Stone uint32_t HistoryThread::GetExtendedBacktraceOriginatingIndexID() {
74b9c1b51eSKate Stone   if (m_originating_unique_thread_id != LLDB_INVALID_THREAD_ID) {
75b9c1b51eSKate Stone     if (GetProcess()->HasAssignedIndexIDToThread(
76b9c1b51eSKate Stone             m_originating_unique_thread_id)) {
77b9c1b51eSKate Stone       return GetProcess()->AssignIndexIDToThread(
78b9c1b51eSKate Stone           m_originating_unique_thread_id);
798ee9cb58SJason Molenda     }
808ee9cb58SJason Molenda   }
818ee9cb58SJason Molenda   return LLDB_INVALID_THREAD_ID;
828ee9cb58SJason Molenda }
83