1cfca06d7SDimitry Andric //===-- HistoryThread.cpp -------------------------------------------------===//
286758c71SEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
686758c71SEd Maste //
786758c71SEd Maste //===----------------------------------------------------------------------===//
886758c71SEd Maste
986758c71SEd Maste #include "lldb/lldb-private.h"
1086758c71SEd Maste
1186758c71SEd Maste #include "Plugins/Process/Utility/HistoryThread.h"
125f29bb8aSDimitry Andric
1314f1b3e8SDimitry Andric #include "Plugins/Process/Utility/HistoryUnwind.h"
1486758c71SEd Maste #include "Plugins/Process/Utility/RegisterContextHistory.h"
1586758c71SEd Maste
1686758c71SEd Maste #include "lldb/Target/Process.h"
1714f1b3e8SDimitry Andric #include "lldb/Target/StackFrameList.h"
18145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
1974a628f7SDimitry Andric #include "lldb/Utility/Log.h"
2086758c71SEd Maste
215f29bb8aSDimitry Andric #include <memory>
225f29bb8aSDimitry Andric
2386758c71SEd Maste using namespace lldb;
2486758c71SEd Maste using namespace lldb_private;
2586758c71SEd Maste
260cac4ca3SEd Maste // Constructor
270cac4ca3SEd Maste
HistoryThread(lldb_private::Process & process,lldb::tid_t tid,std::vector<lldb::addr_t> pcs,bool pcs_are_call_addresses)2814f1b3e8SDimitry Andric HistoryThread::HistoryThread(lldb_private::Process &process, lldb::tid_t tid,
29cfca06d7SDimitry Andric std::vector<lldb::addr_t> pcs,
30cfca06d7SDimitry Andric bool pcs_are_call_addresses)
3114f1b3e8SDimitry Andric : Thread(process, tid, true), m_framelist_mutex(), m_framelist(),
325f29bb8aSDimitry Andric m_pcs(pcs), m_extended_unwind_token(LLDB_INVALID_ADDRESS), m_queue_name(),
3314f1b3e8SDimitry Andric m_thread_name(), m_originating_unique_thread_id(tid),
3414f1b3e8SDimitry Andric m_queue_id(LLDB_INVALID_QUEUE_ID) {
35cfca06d7SDimitry Andric m_unwinder_up =
36cfca06d7SDimitry Andric std::make_unique<HistoryUnwind>(*this, pcs, pcs_are_call_addresses);
37145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Object);
38ead24645SDimitry Andric LLDB_LOGF(log, "%p HistoryThread::HistoryThread", static_cast<void *>(this));
3986758c71SEd Maste }
4086758c71SEd Maste
410cac4ca3SEd Maste // Destructor
420cac4ca3SEd Maste
~HistoryThread()4314f1b3e8SDimitry Andric HistoryThread::~HistoryThread() {
44145449b1SDimitry Andric Log *log = GetLog(LLDBLog::Object);
45ead24645SDimitry Andric LLDB_LOGF(log, "%p HistoryThread::~HistoryThread (tid=0x%" PRIx64 ")",
460cac4ca3SEd Maste static_cast<void *>(this), GetID());
4786758c71SEd Maste DestroyThread();
4886758c71SEd Maste }
4986758c71SEd Maste
GetRegisterContext()5014f1b3e8SDimitry Andric lldb::RegisterContextSP HistoryThread::GetRegisterContext() {
5186758c71SEd Maste RegisterContextSP rctx;
5214f1b3e8SDimitry Andric if (m_pcs.size() > 0) {
535f29bb8aSDimitry Andric rctx = std::make_shared<RegisterContextHistory>(
545f29bb8aSDimitry Andric *this, 0, GetProcess()->GetAddressByteSize(), m_pcs[0]);
5586758c71SEd Maste }
5686758c71SEd Maste return rctx;
5786758c71SEd Maste }
5886758c71SEd Maste
5986758c71SEd Maste lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)6014f1b3e8SDimitry Andric HistoryThread::CreateRegisterContextForFrame(StackFrame *frame) {
615f29bb8aSDimitry Andric return m_unwinder_up->CreateRegisterContextForFrame(frame);
6286758c71SEd Maste }
6386758c71SEd Maste
GetStackFrameList()6414f1b3e8SDimitry Andric lldb::StackFrameListSP HistoryThread::GetStackFrameList() {
65f3fbd1c0SDimitry Andric // FIXME do not throw away the lock after we acquire it..
66f3fbd1c0SDimitry Andric std::unique_lock<std::mutex> lock(m_framelist_mutex);
67f3fbd1c0SDimitry Andric lock.unlock();
685f29bb8aSDimitry Andric if (m_framelist.get() == nullptr) {
695f29bb8aSDimitry Andric m_framelist =
705f29bb8aSDimitry Andric std::make_shared<StackFrameList>(*this, StackFrameListSP(), true);
7186758c71SEd Maste }
7286758c71SEd Maste
7386758c71SEd Maste return m_framelist;
7486758c71SEd Maste }
7586758c71SEd Maste
GetExtendedBacktraceOriginatingIndexID()7614f1b3e8SDimitry Andric uint32_t HistoryThread::GetExtendedBacktraceOriginatingIndexID() {
7714f1b3e8SDimitry Andric if (m_originating_unique_thread_id != LLDB_INVALID_THREAD_ID) {
7814f1b3e8SDimitry Andric if (GetProcess()->HasAssignedIndexIDToThread(
7914f1b3e8SDimitry Andric m_originating_unique_thread_id)) {
8014f1b3e8SDimitry Andric return GetProcess()->AssignIndexIDToThread(
8114f1b3e8SDimitry Andric m_originating_unique_thread_id);
8286758c71SEd Maste }
8386758c71SEd Maste }
8486758c71SEd Maste return LLDB_INVALID_THREAD_ID;
8586758c71SEd Maste }
86