177fc4c14SDimitry Andric //===-- ThreadFreeBSDKernel.cpp -------------------------------------------===//
277fc4c14SDimitry Andric //
377fc4c14SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
477fc4c14SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
577fc4c14SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
677fc4c14SDimitry Andric //
777fc4c14SDimitry Andric //===----------------------------------------------------------------------===//
877fc4c14SDimitry Andric
977fc4c14SDimitry Andric #include "ThreadFreeBSDKernel.h"
1077fc4c14SDimitry Andric
1177fc4c14SDimitry Andric #include "lldb/Target/Unwind.h"
1277fc4c14SDimitry Andric #include "lldb/Utility/Log.h"
1377fc4c14SDimitry Andric
1477fc4c14SDimitry Andric #include "Plugins/Process/Utility/RegisterContextFreeBSD_i386.h"
1577fc4c14SDimitry Andric #include "Plugins/Process/Utility/RegisterContextFreeBSD_x86_64.h"
1677fc4c14SDimitry Andric #include "Plugins/Process/Utility/RegisterInfoPOSIX_arm64.h"
1777fc4c14SDimitry Andric #include "ProcessFreeBSDKernel.h"
1877fc4c14SDimitry Andric #include "RegisterContextFreeBSDKernel_arm64.h"
1977fc4c14SDimitry Andric #include "RegisterContextFreeBSDKernel_i386.h"
2077fc4c14SDimitry Andric #include "RegisterContextFreeBSDKernel_x86_64.h"
2177fc4c14SDimitry Andric #include "ThreadFreeBSDKernel.h"
2277fc4c14SDimitry Andric
2377fc4c14SDimitry Andric using namespace lldb;
2477fc4c14SDimitry Andric using namespace lldb_private;
2577fc4c14SDimitry Andric
ThreadFreeBSDKernel(Process & process,lldb::tid_t tid,lldb::addr_t pcb_addr,std::string thread_name)2677fc4c14SDimitry Andric ThreadFreeBSDKernel::ThreadFreeBSDKernel(Process &process, lldb::tid_t tid,
276f8fc217SDimitry Andric lldb::addr_t pcb_addr,
286f8fc217SDimitry Andric std::string thread_name)
296f8fc217SDimitry Andric : Thread(process, tid), m_thread_name(std::move(thread_name)),
306f8fc217SDimitry Andric m_pcb_addr(pcb_addr) {}
3177fc4c14SDimitry Andric
~ThreadFreeBSDKernel()3277fc4c14SDimitry Andric ThreadFreeBSDKernel::~ThreadFreeBSDKernel() {}
3377fc4c14SDimitry Andric
RefreshStateAfterStop()3477fc4c14SDimitry Andric void ThreadFreeBSDKernel::RefreshStateAfterStop() {}
3577fc4c14SDimitry Andric
GetRegisterContext()3677fc4c14SDimitry Andric lldb::RegisterContextSP ThreadFreeBSDKernel::GetRegisterContext() {
3777fc4c14SDimitry Andric if (!m_reg_context_sp)
3877fc4c14SDimitry Andric m_reg_context_sp = CreateRegisterContextForFrame(nullptr);
3977fc4c14SDimitry Andric return m_reg_context_sp;
4077fc4c14SDimitry Andric }
4177fc4c14SDimitry Andric
4277fc4c14SDimitry Andric lldb::RegisterContextSP
CreateRegisterContextForFrame(StackFrame * frame)4377fc4c14SDimitry Andric ThreadFreeBSDKernel::CreateRegisterContextForFrame(StackFrame *frame) {
4477fc4c14SDimitry Andric RegisterContextSP reg_ctx_sp;
4577fc4c14SDimitry Andric uint32_t concrete_frame_idx = 0;
4677fc4c14SDimitry Andric
4777fc4c14SDimitry Andric if (frame)
4877fc4c14SDimitry Andric concrete_frame_idx = frame->GetConcreteFrameIndex();
4977fc4c14SDimitry Andric
5077fc4c14SDimitry Andric if (concrete_frame_idx == 0) {
5177fc4c14SDimitry Andric if (m_thread_reg_ctx_sp)
5277fc4c14SDimitry Andric return m_thread_reg_ctx_sp;
5377fc4c14SDimitry Andric
5477fc4c14SDimitry Andric ProcessFreeBSDKernel *process =
5577fc4c14SDimitry Andric static_cast<ProcessFreeBSDKernel *>(GetProcess().get());
5677fc4c14SDimitry Andric ArchSpec arch = process->GetTarget().GetArchitecture();
5777fc4c14SDimitry Andric
5877fc4c14SDimitry Andric switch (arch.GetMachine()) {
5977fc4c14SDimitry Andric case llvm::Triple::aarch64:
6077fc4c14SDimitry Andric m_thread_reg_ctx_sp =
6177fc4c14SDimitry Andric std::make_shared<RegisterContextFreeBSDKernel_arm64>(
6277fc4c14SDimitry Andric *this, std::make_unique<RegisterInfoPOSIX_arm64>(arch, 0),
6377fc4c14SDimitry Andric m_pcb_addr);
6477fc4c14SDimitry Andric break;
6577fc4c14SDimitry Andric case llvm::Triple::x86:
666f8fc217SDimitry Andric m_thread_reg_ctx_sp = std::make_shared<RegisterContextFreeBSDKernel_i386>(
6777fc4c14SDimitry Andric *this, new RegisterContextFreeBSD_i386(arch), m_pcb_addr);
6877fc4c14SDimitry Andric break;
6977fc4c14SDimitry Andric case llvm::Triple::x86_64:
7077fc4c14SDimitry Andric m_thread_reg_ctx_sp =
7177fc4c14SDimitry Andric std::make_shared<RegisterContextFreeBSDKernel_x86_64>(
7277fc4c14SDimitry Andric *this, new RegisterContextFreeBSD_x86_64(arch), m_pcb_addr);
7377fc4c14SDimitry Andric break;
7477fc4c14SDimitry Andric default:
7577fc4c14SDimitry Andric assert(false && "Unsupported architecture passed to ThreadFreeBSDKernel");
7677fc4c14SDimitry Andric break;
7777fc4c14SDimitry Andric }
7877fc4c14SDimitry Andric
7977fc4c14SDimitry Andric reg_ctx_sp = m_thread_reg_ctx_sp;
8077fc4c14SDimitry Andric } else {
8177fc4c14SDimitry Andric reg_ctx_sp = GetUnwinder().CreateRegisterContextForFrame(frame);
8277fc4c14SDimitry Andric }
8377fc4c14SDimitry Andric return reg_ctx_sp;
8477fc4c14SDimitry Andric }
8577fc4c14SDimitry Andric
CalculateStopInfo()8677fc4c14SDimitry Andric bool ThreadFreeBSDKernel::CalculateStopInfo() { return false; }
87