2020-07-19 05:03:27 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-07-19 05:03:27 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
2020-07-23 08:52:29 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
ALWAYS_INLINE KDebugBase *GetDebugObject(KProcess *process) {
|
|
|
|
return static_cast<KDebugBase *>(process->GetDebugObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
void KDebugBase::Initialize() {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Clear the continue flags. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_continue_flags = 0;
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
bool KDebugBase::Is64Bit() const {
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_ASSERT(m_lock.IsLockedByCurrentThread());
|
2021-09-18 03:10:05 +02:00
|
|
|
MESOSPHERE_ASSERT(m_is_attached);
|
|
|
|
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
MESOSPHERE_ASSERT(process != nullptr);
|
|
|
|
return process->Is64Bit();
|
2020-07-31 11:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-30 01:21:54 +02:00
|
|
|
Result KDebugBase::QueryMemoryInfo(ams::svc::MemoryInfo *out_memory_info, ams::svc::PageInfo *out_page_info, KProcessAddress address) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
2020-07-30 01:21:54 +02:00
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-30 01:21:54 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
|
|
|
|
/* Check that the process isn't terminated. */
|
|
|
|
R_UNLESS(!process->IsTerminated(), svc::ResultProcessTerminated());
|
2020-07-30 01:21:54 +02:00
|
|
|
|
|
|
|
/* Query the mapping's info. */
|
|
|
|
KMemoryInfo info;
|
2021-09-18 03:10:05 +02:00
|
|
|
R_TRY(process->GetPageTable().QueryInfo(std::addressof(info), out_page_info, address));
|
2020-07-30 01:21:54 +02:00
|
|
|
|
|
|
|
/* Write output. */
|
|
|
|
*out_memory_info = info.GetSvcMemoryInfo();
|
2021-09-18 03:10:05 +02:00
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-30 01:21:54 +02:00
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:40 +02:00
|
|
|
Result KDebugBase::ReadMemory(KProcessAddress buffer, KProcessAddress address, size_t size) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
2020-07-30 12:37:40 +02:00
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-30 12:37:40 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
|
|
|
|
/* Check that the process isn't terminated. */
|
|
|
|
R_UNLESS(!process->IsTerminated(), svc::ResultProcessTerminated());
|
2020-07-30 12:37:40 +02:00
|
|
|
|
|
|
|
/* Get the page tables. */
|
|
|
|
KProcessPageTable &debugger_pt = GetCurrentProcess().GetPageTable();
|
2021-09-18 03:10:05 +02:00
|
|
|
KProcessPageTable &target_pt = process->GetPageTable();
|
2020-07-30 12:37:40 +02:00
|
|
|
|
|
|
|
/* Verify that the regions are in range. */
|
|
|
|
R_UNLESS(target_pt.Contains(address, size), svc::ResultInvalidCurrentMemory());
|
|
|
|
R_UNLESS(debugger_pt.Contains(buffer, size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Iterate over the target process's memory blocks. */
|
|
|
|
KProcessAddress cur_address = address;
|
|
|
|
size_t remaining = size;
|
|
|
|
while (remaining > 0) {
|
|
|
|
/* Get the current memory info. */
|
|
|
|
KMemoryInfo info;
|
|
|
|
ams::svc::PageInfo pi;
|
|
|
|
R_TRY(target_pt.QueryInfo(std::addressof(info), std::addressof(pi), cur_address));
|
|
|
|
|
|
|
|
/* Check that the memory is accessible. */
|
|
|
|
R_UNLESS(info.GetState() != static_cast<KMemoryState>(ams::svc::MemoryState_Inaccessible), svc::ResultInvalidAddress());
|
|
|
|
|
|
|
|
/* Get the current size. */
|
|
|
|
const size_t cur_size = std::min(remaining, info.GetEndAddress() - GetInteger(cur_address));
|
|
|
|
|
|
|
|
/* Read the memory. */
|
2023-10-11 14:00:23 +02:00
|
|
|
if (info.GetSvcState() != ams::svc::MemoryState_Io) {
|
2020-07-30 12:37:40 +02:00
|
|
|
/* The memory is normal memory. */
|
|
|
|
R_TRY(target_pt.ReadDebugMemory(GetVoidPointer(buffer), cur_address, cur_size));
|
|
|
|
} else {
|
|
|
|
/* The memory is IO memory. */
|
2023-10-11 14:00:23 +02:00
|
|
|
R_TRY(target_pt.ReadDebugIoMemory(GetVoidPointer(buffer), cur_address, cur_size, info.GetState()));
|
2020-07-30 12:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
buffer += cur_size;
|
|
|
|
cur_address += cur_size;
|
|
|
|
remaining -= cur_size;
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-30 12:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDebugBase::WriteMemory(KProcessAddress buffer, KProcessAddress address, size_t size) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
2020-07-30 12:37:40 +02:00
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-30 12:37:40 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
|
|
|
|
/* Check that the process isn't terminated. */
|
|
|
|
R_UNLESS(!process->IsTerminated(), svc::ResultProcessTerminated());
|
2020-07-30 12:37:40 +02:00
|
|
|
|
|
|
|
/* Get the page tables. */
|
|
|
|
KProcessPageTable &debugger_pt = GetCurrentProcess().GetPageTable();
|
2021-09-18 03:10:05 +02:00
|
|
|
KProcessPageTable &target_pt = process->GetPageTable();
|
2020-07-30 12:37:40 +02:00
|
|
|
|
|
|
|
/* Verify that the regions are in range. */
|
|
|
|
R_UNLESS(target_pt.Contains(address, size), svc::ResultInvalidCurrentMemory());
|
|
|
|
R_UNLESS(debugger_pt.Contains(buffer, size), svc::ResultInvalidCurrentMemory());
|
|
|
|
|
|
|
|
/* Iterate over the target process's memory blocks. */
|
|
|
|
KProcessAddress cur_address = address;
|
|
|
|
size_t remaining = size;
|
|
|
|
while (remaining > 0) {
|
|
|
|
/* Get the current memory info. */
|
|
|
|
KMemoryInfo info;
|
|
|
|
ams::svc::PageInfo pi;
|
|
|
|
R_TRY(target_pt.QueryInfo(std::addressof(info), std::addressof(pi), cur_address));
|
|
|
|
|
|
|
|
/* Check that the memory is accessible. */
|
|
|
|
R_UNLESS(info.GetState() != static_cast<KMemoryState>(ams::svc::MemoryState_Inaccessible), svc::ResultInvalidAddress());
|
|
|
|
|
|
|
|
/* Get the current size. */
|
|
|
|
const size_t cur_size = std::min(remaining, info.GetEndAddress() - GetInteger(cur_address));
|
|
|
|
|
|
|
|
/* Read the memory. */
|
2023-10-11 14:00:23 +02:00
|
|
|
if (info.GetSvcState() != ams::svc::MemoryState_Io) {
|
2020-07-30 12:37:40 +02:00
|
|
|
/* The memory is normal memory. */
|
|
|
|
R_TRY(target_pt.WriteDebugMemory(cur_address, GetVoidPointer(buffer), cur_size));
|
|
|
|
} else {
|
|
|
|
/* The memory is IO memory. */
|
2023-10-11 14:00:23 +02:00
|
|
|
R_TRY(target_pt.WriteDebugIoMemory(cur_address, GetVoidPointer(buffer), cur_size, info.GetState()));
|
2020-07-30 12:37:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Advance. */
|
|
|
|
buffer += cur_size;
|
|
|
|
cur_address += cur_size;
|
|
|
|
remaining -= cur_size;
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-30 12:37:40 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 01:31:58 +02:00
|
|
|
Result KDebugBase::GetRunningThreadInfo(ams::svc::LastThreadContext *out_context, u64 *out_thread_id) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
2020-07-31 01:31:58 +02:00
|
|
|
|
|
|
|
/* Get the thread info. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Get the running thread. */
|
|
|
|
const s32 core_id = GetCurrentCoreId();
|
|
|
|
KThread *thread = process->GetRunningThread(core_id);
|
|
|
|
|
2023-02-21 11:12:17 +01:00
|
|
|
/* We want to check that the thread is actually running. */
|
|
|
|
/* If it is, then the scheduler will have just switched from the thread to the current thread. */
|
|
|
|
/* This implies exactly one switch will have taken place, and the current thread will be on the current core. */
|
|
|
|
const auto &scheduler = Kernel::GetScheduler(core_id);
|
|
|
|
if (!(thread != nullptr && thread->GetActiveCore() == core_id && process->GetRunningThreadSwitchCount(core_id) + 1 == scheduler.GetSwitchCount())) {
|
|
|
|
/* The most recent thread switch was from a thread other than the expected one to the current one. */
|
|
|
|
/* We want to use the appropriate result to inform userland about what thread we switched from. */
|
|
|
|
if (scheduler.GetIdleCount() + 1 == scheduler.GetSwitchCount()) {
|
|
|
|
/* We switched from the idle thread. */
|
|
|
|
R_THROW(svc::ResultNoThread());
|
|
|
|
} else {
|
|
|
|
/* We switched from some other unknown thread. */
|
|
|
|
R_THROW(svc::ResultUnknownThread());
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 01:31:58 +02:00
|
|
|
|
|
|
|
/* Get the thread's exception context. */
|
|
|
|
GetExceptionContext(thread)->GetSvcThreadContext(out_context);
|
|
|
|
|
|
|
|
/* Get the thread's id. */
|
|
|
|
*out_thread_id = thread->GetId();
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-31 01:31:58 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
Result KDebugBase::Attach(KProcess *target) {
|
|
|
|
/* Check that the process isn't null. */
|
|
|
|
MESOSPHERE_ASSERT(target != nullptr);
|
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Clear ourselves as unattached. */
|
|
|
|
m_is_attached = false;
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Attach to the process. */
|
|
|
|
{
|
|
|
|
/* Lock both ourselves, the target process, and the scheduler. */
|
|
|
|
KScopedLightLock state_lk(target->GetStateLock());
|
|
|
|
KScopedLightLock list_lk(target->GetListLock());
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock this_lk(m_lock);
|
2020-07-19 05:03:27 +02:00
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Check that the process isn't already being debugged. */
|
|
|
|
R_UNLESS(!target->IsAttachedToDebugger(), svc::ResultBusy());
|
|
|
|
|
|
|
|
{
|
|
|
|
/* Ensure the process is in a state that allows for debugging. */
|
|
|
|
const KProcess::State state = target->GetState();
|
|
|
|
switch (state) {
|
|
|
|
case KProcess::State_Created:
|
|
|
|
case KProcess::State_Running:
|
|
|
|
case KProcess::State_Crashed:
|
|
|
|
break;
|
|
|
|
case KProcess::State_CreatedAttached:
|
|
|
|
case KProcess::State_RunningAttached:
|
|
|
|
case KProcess::State_DebugBreak:
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultBusy());
|
2020-07-19 05:03:27 +02:00
|
|
|
case KProcess::State_Terminating:
|
|
|
|
case KProcess::State_Terminated:
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultProcessTerminated());
|
2020-07-19 05:03:27 +02:00
|
|
|
MESOSPHERE_UNREACHABLE_DEFAULT_CASE();
|
|
|
|
}
|
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Attach to the target. */
|
|
|
|
m_process_holder.Attach(target);
|
|
|
|
m_is_attached = true;
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Set ourselves as the process's attached object. */
|
2021-09-18 03:10:05 +02:00
|
|
|
m_old_process_state = target->SetDebugObject(this);
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Send an event for our attaching to the process. */
|
2023-02-21 17:16:15 +01:00
|
|
|
this->PushDebugEvent(ams::svc::DebugEvent_CreateProcess, nullptr, 0);
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Send events for attaching to each thread in the process. */
|
|
|
|
{
|
2021-09-18 03:10:05 +02:00
|
|
|
auto end = target->GetThreadList().end();
|
|
|
|
for (auto it = target->GetThreadList().begin(); it != end; ++it) {
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Request that we suspend the thread. */
|
|
|
|
it->RequestSuspend(KThread::SuspendType_Debug);
|
|
|
|
|
|
|
|
/* If the thread is in a state for us to do so, generate the event. */
|
|
|
|
if (const auto thread_state = it->GetState(); thread_state == KThread::ThreadState_Runnable || thread_state == KThread::ThreadState_Waiting) {
|
|
|
|
/* Mark the thread as attached to. */
|
|
|
|
it->SetDebugAttached();
|
|
|
|
|
|
|
|
/* Send the event. */
|
2023-02-21 17:16:15 +01:00
|
|
|
const uintptr_t params[2] = { it->GetId(), GetInteger(it->GetThreadLocalRegionAddress()) };
|
|
|
|
this->PushDebugEvent(ams::svc::DebugEvent_CreateThread, params, util::size(params));
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send the process's jit debug info, if relevant. */
|
2021-09-18 03:10:05 +02:00
|
|
|
if (KEventInfo *jit_info = target->GetJitDebugInfo(); jit_info != nullptr) {
|
2020-07-19 05:03:27 +02:00
|
|
|
this->EnqueueDebugEventInfo(jit_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send an exception event to represent our attaching. */
|
2023-02-21 17:16:15 +01:00
|
|
|
const uintptr_t params[1] = { static_cast<uintptr_t>(ams::svc::DebugException_DebuggerAttached) };
|
|
|
|
this->PushDebugEvent(ams::svc::DebugEvent_Exception, params, util::size(params));
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Signal. */
|
|
|
|
this->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 07:18:19 +02:00
|
|
|
Result KDebugBase::BreakProcess() {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const target = this->GetProcessUnsafe();
|
2020-07-31 07:18:19 +02:00
|
|
|
|
|
|
|
/* Lock both ourselves, the target process, and the scheduler. */
|
|
|
|
KScopedLightLock state_lk(target->GetStateLock());
|
|
|
|
KScopedLightLock list_lk(target->GetListLock());
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock this_lk(m_lock);
|
2020-07-31 07:18:19 +02:00
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Check that the process isn't terminated. */
|
|
|
|
R_UNLESS(!target->IsTerminated(), svc::ResultProcessTerminated());
|
2020-07-31 07:18:19 +02:00
|
|
|
|
|
|
|
/* Get the currently active threads. */
|
|
|
|
constexpr u64 ThreadIdNoThread = -1ll;
|
|
|
|
constexpr u64 ThreadIdUnknownThread = -2ll;
|
2023-02-21 17:16:15 +01:00
|
|
|
uintptr_t debug_info_params[1 + cpu::NumCores] = { static_cast<uintptr_t>(ams::svc::DebugException_DebuggerBreak), };
|
|
|
|
for (size_t i = 0; i < cpu::NumCores; ++i) {
|
2020-07-31 07:18:19 +02:00
|
|
|
/* Get the currently running thread. */
|
|
|
|
KThread *thread = target->GetRunningThread(i);
|
|
|
|
|
|
|
|
/* Check that the thread's idle count is correct. */
|
|
|
|
if (target->GetRunningThreadIdleCount(i) == Kernel::GetScheduler(i).GetIdleCount()) {
|
|
|
|
if (thread != nullptr && static_cast<size_t>(thread->GetActiveCore()) == i) {
|
2023-02-21 17:16:15 +01:00
|
|
|
debug_info_params[1 + i] = thread->GetId();
|
2020-07-31 07:18:19 +02:00
|
|
|
} else {
|
|
|
|
/* We found an unknown thread. */
|
2023-02-21 17:16:15 +01:00
|
|
|
debug_info_params[1 + i] = ThreadIdUnknownThread;
|
2020-07-31 07:18:19 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* We didn't find a thread. */
|
2023-02-21 17:16:15 +01:00
|
|
|
debug_info_params[1 + i] = ThreadIdNoThread;
|
2020-07-31 07:18:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Suspend all the threads in the process. */
|
|
|
|
{
|
|
|
|
auto end = target->GetThreadList().end();
|
|
|
|
for (auto it = target->GetThreadList().begin(); it != end; ++it) {
|
|
|
|
/* Request that we suspend the thread. */
|
|
|
|
it->RequestSuspend(KThread::SuspendType_Debug);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send an exception event to represent our breaking the process. */
|
2023-02-21 17:16:15 +01:00
|
|
|
this->PushDebugEvent(ams::svc::DebugEvent_Exception, debug_info_params, util::size(debug_info_params));
|
2020-07-31 07:18:19 +02:00
|
|
|
|
|
|
|
/* Signal. */
|
|
|
|
this->NotifyAvailable();
|
|
|
|
|
|
|
|
/* Set the process as breaked. */
|
|
|
|
target->SetDebugBreak();
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-31 07:18:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDebugBase::TerminateProcess() {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), ResultSuccess());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), ResultSuccess());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const target = this->GetProcessUnsafe();
|
2020-07-31 07:18:19 +02:00
|
|
|
|
|
|
|
/* Detach from the process. */
|
|
|
|
{
|
|
|
|
/* Lock both ourselves and the target process. */
|
|
|
|
KScopedLightLock state_lk(target->GetStateLock());
|
|
|
|
KScopedLightLock list_lk(target->GetListLock());
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock this_lk(m_lock);
|
2020-07-31 07:18:19 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached. */
|
|
|
|
if (this->IsAttached()) {
|
2020-07-31 07:18:19 +02:00
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Get the process's state. */
|
|
|
|
const KProcess::State state = target->GetState();
|
|
|
|
|
|
|
|
/* Check that the process is in a state where we can terminate it. */
|
|
|
|
R_UNLESS(state != KProcess::State_Created, svc::ResultInvalidState());
|
|
|
|
R_UNLESS(state != KProcess::State_CreatedAttached, svc::ResultInvalidState());
|
|
|
|
|
|
|
|
/* Decide on a new state for the process. */
|
|
|
|
KProcess::State new_state;
|
|
|
|
if (state == KProcess::State_RunningAttached) {
|
|
|
|
/* If the process is running, transition it accordingly. */
|
|
|
|
new_state = KProcess::State_Running;
|
|
|
|
} else if (state == KProcess::State_DebugBreak) {
|
|
|
|
/* If the process is debug breaked, transition it accordingly. */
|
|
|
|
new_state = KProcess::State_Crashed;
|
2021-04-07 23:53:12 +02:00
|
|
|
|
|
|
|
/* Suspend all the threads in the process. */
|
|
|
|
{
|
|
|
|
auto end = target->GetThreadList().end();
|
|
|
|
for (auto it = target->GetThreadList().begin(); it != end; ++it) {
|
|
|
|
/* Request that we suspend the thread. */
|
|
|
|
it->RequestSuspend(KThread::SuspendType_Debug);
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 07:18:19 +02:00
|
|
|
} else {
|
|
|
|
/* Otherwise, don't transition. */
|
|
|
|
new_state = state;
|
|
|
|
}
|
|
|
|
|
2021-07-27 23:05:32 +02:00
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
2021-07-27 14:34:40 +02:00
|
|
|
/* Clear single step on all threads. */
|
|
|
|
{
|
|
|
|
auto end = target->GetThreadList().end();
|
|
|
|
for (auto it = target->GetThreadList().begin(); it != end; ++it) {
|
2022-03-23 03:59:39 +01:00
|
|
|
it->ClearHardwareSingleStep();
|
2021-07-27 14:34:40 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 23:05:32 +02:00
|
|
|
#endif
|
2021-07-27 14:34:40 +02:00
|
|
|
|
2020-07-31 07:18:19 +02:00
|
|
|
/* Detach from the process. */
|
|
|
|
target->ClearDebugObject(new_state);
|
2021-09-18 03:10:05 +02:00
|
|
|
m_is_attached = false;
|
|
|
|
|
|
|
|
/* Close the initial reference opened to our process. */
|
|
|
|
this->CloseProcess();
|
2020-07-31 07:18:19 +02:00
|
|
|
|
|
|
|
/* Clear our continue flags. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_continue_flags = 0;
|
2020-07-31 07:18:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the process. */
|
|
|
|
target->Terminate();
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-31 07:18:19 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
Result KDebugBase::GetThreadContext(ams::svc::ThreadContext *out, u64 thread_id, u32 context_flags) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-31 11:49:43 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
/* Get the thread from its id. */
|
2020-08-15 12:01:43 +02:00
|
|
|
KThread *thread = KThread::GetThreadFromId(thread_id);
|
|
|
|
R_UNLESS(thread != nullptr, svc::ResultInvalidId());
|
|
|
|
ON_SCOPE_EXIT { thread->Close(); };
|
2020-07-31 11:49:43 +02:00
|
|
|
|
|
|
|
/* Verify that the thread is owned by our process. */
|
2021-09-18 03:10:05 +02:00
|
|
|
R_UNLESS(process == thread->GetOwnerProcess(), svc::ResultInvalidId());
|
2020-07-31 11:49:43 +02:00
|
|
|
|
|
|
|
/* Verify that the thread isn't terminated. */
|
|
|
|
R_UNLESS(thread->GetState() != KThread::ThreadState_Terminated, svc::ResultTerminationRequested());
|
|
|
|
|
|
|
|
/* Check that the thread is not the current one. */
|
|
|
|
/* NOTE: Nintendo does not check this, and thus the following loop will deadlock. */
|
2020-08-15 12:01:43 +02:00
|
|
|
R_UNLESS(thread != GetCurrentThreadPointer(), svc::ResultInvalidId());
|
2020-07-31 11:49:43 +02:00
|
|
|
|
|
|
|
/* Try to get the thread context until the thread isn't current on any core. */
|
|
|
|
while (true) {
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* The thread needs to be requested for debug suspension. */
|
|
|
|
R_UNLESS(thread->IsSuspendRequested(KThread::SuspendType_Debug), svc::ResultInvalidState());
|
|
|
|
|
|
|
|
/* If the thread's raw state isn't runnable, check if it's current on some core. */
|
|
|
|
if (thread->GetRawState() != KThread::ThreadState_Runnable) {
|
|
|
|
bool current = false;
|
|
|
|
for (auto i = 0; i < static_cast<s32>(cpu::NumCores); ++i) {
|
2020-12-01 22:41:37 +01:00
|
|
|
if (thread == Kernel::GetScheduler(i).GetSchedulerCurrentThread()) {
|
2020-07-31 11:49:43 +02:00
|
|
|
current = true;
|
2021-08-19 16:38:25 +02:00
|
|
|
break;
|
2020-07-31 11:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the thread is current, retry until it isn't. */
|
|
|
|
if (current) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the thread context. */
|
2021-10-24 22:04:31 +02:00
|
|
|
static_assert(std::derived_from<KDebug, KDebugBase>);
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(static_cast<KDebug *>(this)->GetThreadContextImpl(out, thread, context_flags));
|
2020-07-31 11:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result KDebugBase::SetThreadContext(const ams::svc::ThreadContext &ctx, u64 thread_id, u32 context_flags) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
/* Lock ourselves. */
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock lk(m_lock);
|
2020-07-31 11:49:43 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
/* Get the thread from its id. */
|
2020-08-15 12:01:43 +02:00
|
|
|
KThread *thread = KThread::GetThreadFromId(thread_id);
|
|
|
|
R_UNLESS(thread != nullptr, svc::ResultInvalidId());
|
|
|
|
ON_SCOPE_EXIT { thread->Close(); };
|
2020-07-31 11:49:43 +02:00
|
|
|
|
|
|
|
/* Verify that the thread is owned by our process. */
|
2021-09-18 03:10:05 +02:00
|
|
|
R_UNLESS(process == thread->GetOwnerProcess(), svc::ResultInvalidId());
|
2020-07-31 11:49:43 +02:00
|
|
|
|
|
|
|
/* Verify that the thread isn't terminated. */
|
|
|
|
R_UNLESS(thread->GetState() != KThread::ThreadState_Terminated, svc::ResultTerminationRequested());
|
|
|
|
|
|
|
|
/* Check that the thread is not the current one. */
|
|
|
|
/* NOTE: Nintendo does not check this, and thus the following loop will deadlock. */
|
2020-08-15 12:01:43 +02:00
|
|
|
R_UNLESS(thread != GetCurrentThreadPointer(), svc::ResultInvalidId());
|
2020-07-31 11:49:43 +02:00
|
|
|
|
|
|
|
/* Try to get the thread context until the thread isn't current on any core. */
|
|
|
|
while (true) {
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* The thread needs to be requested for debug suspension. */
|
|
|
|
R_UNLESS(thread->IsSuspendRequested(KThread::SuspendType_Debug), svc::ResultInvalidState());
|
|
|
|
|
|
|
|
/* If the thread's raw state isn't runnable, check if it's current on some core. */
|
|
|
|
if (thread->GetRawState() != KThread::ThreadState_Runnable) {
|
|
|
|
bool current = false;
|
|
|
|
for (auto i = 0; i < static_cast<s32>(cpu::NumCores); ++i) {
|
2020-12-01 22:41:37 +01:00
|
|
|
if (thread == Kernel::GetScheduler(i).GetSchedulerCurrentThread()) {
|
2020-07-31 11:49:43 +02:00
|
|
|
current = true;
|
2021-08-19 16:34:08 +02:00
|
|
|
break;
|
2020-07-31 11:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the thread is current, retry until it isn't. */
|
|
|
|
if (current) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-27 14:34:40 +02:00
|
|
|
/* Update thread single-step state. */
|
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
|
|
|
{
|
|
|
|
if ((context_flags & ams::svc::ThreadContextFlag_SetSingleStep) != 0) {
|
|
|
|
/* Set single step. */
|
2022-03-23 03:59:39 +01:00
|
|
|
thread->SetHardwareSingleStep();
|
2021-07-27 14:34:40 +02:00
|
|
|
|
|
|
|
/* If no other thread flags are present, we're done. */
|
|
|
|
R_SUCCEED_IF((context_flags & ~ams::svc::ThreadContextFlag_SetSingleStep) == 0);
|
|
|
|
} else if ((context_flags & ams::svc::ThreadContextFlag_ClearSingleStep) != 0) {
|
|
|
|
/* Clear single step. */
|
2022-03-23 03:59:39 +01:00
|
|
|
thread->ClearHardwareSingleStep();
|
2021-07-27 14:34:40 +02:00
|
|
|
|
|
|
|
/* If no other thread flags are present, we're done. */
|
|
|
|
R_SUCCEED_IF((context_flags & ~ams::svc::ThreadContextFlag_ClearSingleStep) == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
/* Verify that the thread's svc state is valid. */
|
|
|
|
if (thread->IsCallingSvc()) {
|
2021-03-11 21:53:17 +01:00
|
|
|
const u8 svc_id = thread->GetSvcId();
|
|
|
|
|
|
|
|
const bool is_valid_svc = svc_id == svc::SvcId_Break ||
|
|
|
|
svc_id == svc::SvcId_ReturnFromException;
|
|
|
|
|
|
|
|
R_UNLESS(is_valid_svc, svc::ResultInvalidState());
|
2020-07-31 11:49:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the thread context. */
|
2021-10-24 22:04:31 +02:00
|
|
|
static_assert(std::derived_from<KDebug, KDebugBase>);
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(static_cast<KDebug *>(this)->SetThreadContextImpl(ctx, thread, context_flags));
|
2020-07-31 11:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-31 05:49:58 +02:00
|
|
|
Result KDebugBase::ContinueDebug(const u32 flags, const u64 *thread_ids, size_t num_thread_ids) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const target = this->GetProcessUnsafe();
|
2020-07-31 05:49:58 +02:00
|
|
|
|
|
|
|
/* Lock both ourselves, the target process, and the scheduler. */
|
|
|
|
KScopedLightLock state_lk(target->GetStateLock());
|
|
|
|
KScopedLightLock list_lk(target->GetListLock());
|
2020-12-18 02:18:47 +01:00
|
|
|
KScopedLightLock this_lk(m_lock);
|
2020-07-31 05:49:58 +02:00
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached now that we're locked. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Check that the process isn't terminated. */
|
|
|
|
R_UNLESS(!target->IsTerminated(), svc::ResultProcessTerminated());
|
2020-07-31 05:49:58 +02:00
|
|
|
|
|
|
|
/* Check that we have no pending events. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(m_event_info_list.empty(), svc::ResultBusy());
|
2020-07-31 05:49:58 +02:00
|
|
|
|
|
|
|
/* Clear the target's JIT debug info. */
|
|
|
|
target->ClearJitDebugInfo();
|
|
|
|
|
|
|
|
/* Set our continue flags. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_continue_flags = flags;
|
2020-07-31 05:49:58 +02:00
|
|
|
|
|
|
|
/* Iterate over threads, continuing them as we should. */
|
|
|
|
bool has_debug_break_thread = false;
|
|
|
|
{
|
|
|
|
/* Parse our flags. */
|
2020-12-18 02:18:47 +01:00
|
|
|
const bool exception_handled = (m_continue_flags & ams::svc::ContinueFlag_ExceptionHandled) != 0;
|
|
|
|
const bool continue_all = (m_continue_flags & ams::svc::ContinueFlag_ContinueAll) != 0;
|
|
|
|
const bool continue_others = (m_continue_flags & ams::svc::ContinueFlag_ContinueOthers) != 0;
|
2020-07-31 05:49:58 +02:00
|
|
|
|
|
|
|
/* Update each thread. */
|
|
|
|
auto end = target->GetThreadList().end();
|
|
|
|
for (auto it = target->GetThreadList().begin(); it != end; ++it) {
|
|
|
|
/* Determine if we should continue the thread. */
|
|
|
|
bool should_continue;
|
|
|
|
{
|
|
|
|
if (continue_all) {
|
|
|
|
/* Continue all threads. */
|
|
|
|
should_continue = true;
|
|
|
|
} else if (continue_others) {
|
|
|
|
/* Continue the thread if it doesn't match one of our target ids. */
|
|
|
|
const u64 thread_id = it->GetId();
|
|
|
|
should_continue = true;
|
|
|
|
for (size_t i = 0; i < num_thread_ids; ++i) {
|
|
|
|
if (thread_ids[i] == thread_id) {
|
|
|
|
should_continue = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Continue the thread if it matches one of our target ids. */
|
|
|
|
const u64 thread_id = it->GetId();
|
|
|
|
should_continue = false;
|
|
|
|
for (size_t i = 0; i < num_thread_ids; ++i) {
|
|
|
|
if (thread_ids[i] == thread_id) {
|
|
|
|
should_continue = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Continue the thread if we should. */
|
|
|
|
if (should_continue) {
|
|
|
|
if (exception_handled) {
|
|
|
|
it->SetDebugExceptionResult(svc::ResultStopProcessingException());
|
|
|
|
}
|
|
|
|
it->Resume(KThread::SuspendType_Debug);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the thread has debug suspend requested, note so. */
|
|
|
|
if (it->IsSuspendRequested(KThread::SuspendType_Debug)) {
|
|
|
|
has_debug_break_thread = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the process's state. */
|
|
|
|
if (has_debug_break_thread) {
|
|
|
|
target->SetDebugBreak();
|
|
|
|
} else {
|
|
|
|
target->SetAttached();
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-31 05:49:58 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:16:15 +01:00
|
|
|
KEventInfo *KDebugBase::CreateDebugEvent(ams::svc::DebugEvent event, u64 cur_thread_id, const uintptr_t *params, size_t num_params) {
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Allocate a new event. */
|
|
|
|
KEventInfo *info = KEventInfo::Allocate();
|
|
|
|
|
|
|
|
/* Populate the event info. */
|
|
|
|
if (info != nullptr) {
|
|
|
|
/* Set common fields. */
|
|
|
|
info->event = event;
|
|
|
|
info->thread_id = 0;
|
2020-07-31 05:49:58 +02:00
|
|
|
info->flags = ams::svc::DebugEventFlag_Stopped;
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Set event specific fields. */
|
|
|
|
switch (event) {
|
2020-07-31 10:27:09 +02:00
|
|
|
case ams::svc::DebugEvent_CreateProcess:
|
2020-07-19 05:03:27 +02:00
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
/* Check parameters. */
|
|
|
|
MESOSPHERE_ASSERT(params == nullptr);
|
|
|
|
MESOSPHERE_ASSERT(num_params == 0);
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-07-31 10:27:09 +02:00
|
|
|
case ams::svc::DebugEvent_CreateThread:
|
2020-07-19 05:03:27 +02:00
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
/* Check parameters. */
|
|
|
|
MESOSPHERE_ASSERT(params != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(num_params == 2);
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Set the thread id. */
|
2023-02-21 17:16:15 +01:00
|
|
|
info->thread_id = params[0];
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Set the thread creation info. */
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.create_thread.thread_id = params[0];
|
|
|
|
info->info.create_thread.tls_address = params[1];
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugEvent_ExitProcess:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
/* Check parameters. */
|
|
|
|
MESOSPHERE_ASSERT(params != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(num_params == 1);
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Set the exit reason. */
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.exit_process.reason = static_cast<ams::svc::ProcessExitReason>(params[0]);
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Clear the thread id and flags. */
|
|
|
|
info->thread_id = 0;
|
2020-07-31 10:27:09 +02:00
|
|
|
info->flags = 0;
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugEvent_ExitThread:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
/* Check parameters. */
|
|
|
|
MESOSPHERE_ASSERT(params != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(num_params == 2);
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Set the thread id. */
|
2023-02-21 17:16:15 +01:00
|
|
|
info->thread_id = params[0];
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Set the exit reason. */
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.exit_thread.reason = static_cast<ams::svc::ThreadExitReason>(params[1]);
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugEvent_Exception:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
/* Check parameters. */
|
|
|
|
MESOSPHERE_ASSERT(params != nullptr);
|
|
|
|
MESOSPHERE_ASSERT(num_params >= 1);
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Set the thread id. */
|
|
|
|
info->thread_id = cur_thread_id;
|
|
|
|
|
|
|
|
/* Set the exception type, and clear the count. */
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.exception.exception_type = static_cast<ams::svc::DebugException>(params[0]);
|
2020-07-19 05:03:27 +02:00
|
|
|
info->info.exception.exception_data_count = 0;
|
2023-02-21 17:16:15 +01:00
|
|
|
switch (static_cast<ams::svc::DebugException>(params[0])) {
|
2020-07-19 05:03:27 +02:00
|
|
|
case ams::svc::DebugException_UndefinedInstruction:
|
|
|
|
case ams::svc::DebugException_BreakPoint:
|
|
|
|
case ams::svc::DebugException_UndefinedSystemCall:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
MESOSPHERE_ASSERT(num_params >= 3);
|
|
|
|
|
|
|
|
info->info.exception.exception_address = params[1];
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
info->info.exception.exception_data_count = 1;
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.exception.exception_data[0] = params[2];
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_DebuggerAttached:
|
|
|
|
{
|
|
|
|
info->thread_id = 0;
|
|
|
|
|
|
|
|
info->info.exception.exception_address = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_UserBreak:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
MESOSPHERE_ASSERT(num_params >= 2);
|
|
|
|
|
|
|
|
info->info.exception.exception_address = params[1];
|
2020-07-19 05:03:27 +02:00
|
|
|
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.exception.exception_data_count = 0;
|
|
|
|
for (size_t i = 2; i < num_params; ++i) {
|
|
|
|
info->info.exception.exception_data[info->info.exception.exception_data_count++] = params[i];
|
|
|
|
}
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_DebuggerBreak:
|
|
|
|
{
|
|
|
|
info->thread_id = 0;
|
|
|
|
|
|
|
|
info->info.exception.exception_address = 0;
|
|
|
|
|
2023-02-21 17:16:15 +01:00
|
|
|
info->info.exception.exception_data_count = 0;
|
|
|
|
for (size_t i = 1; i < num_params; ++i) {
|
|
|
|
info->info.exception.exception_data[info->info.exception.exception_data_count++] = params[i];
|
|
|
|
}
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_MemorySystemError:
|
|
|
|
{
|
|
|
|
info->info.exception.exception_address = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_InstructionAbort:
|
|
|
|
case ams::svc::DebugException_DataAbort:
|
|
|
|
case ams::svc::DebugException_AlignmentFault:
|
|
|
|
default:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
MESOSPHERE_ASSERT(num_params >= 2);
|
|
|
|
|
|
|
|
info->info.exception.exception_address = params[1];
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
2023-02-21 17:16:15 +01:00
|
|
|
void KDebugBase::PushDebugEvent(ams::svc::DebugEvent event, const uintptr_t *params, size_t num_params) {
|
2020-07-19 05:03:27 +02:00
|
|
|
/* Create and enqueue and event. */
|
2023-02-21 17:16:15 +01:00
|
|
|
if (KEventInfo *new_info = CreateDebugEvent(event, GetCurrentThread().GetId(), params, num_params); new_info != nullptr) {
|
2020-07-19 05:03:27 +02:00
|
|
|
this->EnqueueDebugEventInfo(new_info);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KDebugBase::EnqueueDebugEventInfo(KEventInfo *info) {
|
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Push the event to the back of the list. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_event_info_list.push_back(*info);
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-31 09:36:33 +02:00
|
|
|
template<typename T> requires (std::same_as<T, ams::svc::lp64::DebugEventInfo> || std::same_as<T, ams::svc::ilp32::DebugEventInfo>)
|
|
|
|
Result KDebugBase::GetDebugEventInfoImpl(T *out) {
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're attached. */
|
|
|
|
R_UNLESS(this->IsAttached(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Open a reference to our process. */
|
|
|
|
R_UNLESS(this->OpenProcess(), svc::ResultProcessTerminated());
|
|
|
|
|
|
|
|
/* Close our reference to our process when we're done. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Pop an event info from our queue. */
|
|
|
|
KEventInfo *info = nullptr;
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Check that we have an event to dequeue. */
|
2020-12-18 02:18:47 +01:00
|
|
|
R_UNLESS(!m_event_info_list.empty(), svc::ResultNoEvent());
|
2020-07-19 05:03:27 +02:00
|
|
|
|
|
|
|
/* Pop the event from the front of the queue. */
|
2020-12-18 02:18:47 +01:00
|
|
|
info = std::addressof(m_event_info_list.front());
|
|
|
|
m_event_info_list.pop_front();
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
MESOSPHERE_ASSERT(info != nullptr);
|
|
|
|
|
|
|
|
/* Free the event info once we're done with it. */
|
|
|
|
ON_SCOPE_EXIT { KEventInfo::Free(info); };
|
|
|
|
|
|
|
|
/* Set common fields. */
|
|
|
|
out->type = info->event;
|
|
|
|
out->thread_id = info->thread_id;
|
|
|
|
out->flags = info->flags;
|
|
|
|
|
|
|
|
/* Set event specific fields. */
|
|
|
|
switch (info->event) {
|
2020-07-31 10:27:09 +02:00
|
|
|
case ams::svc::DebugEvent_CreateProcess:
|
2020-07-19 05:03:27 +02:00
|
|
|
{
|
2020-07-31 10:27:09 +02:00
|
|
|
out->info.create_process.program_id = process->GetProgramId();
|
|
|
|
out->info.create_process.process_id = process->GetId();
|
|
|
|
out->info.create_process.flags = process->GetCreateProcessFlags();
|
|
|
|
out->info.create_process.user_exception_context_address = GetInteger(process->GetProcessLocalRegionAddress());
|
2020-07-19 05:03:27 +02:00
|
|
|
|
2020-07-31 10:27:09 +02:00
|
|
|
std::memcpy(out->info.create_process.name, process->GetName(), sizeof(out->info.create_process.name));
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-07-31 10:27:09 +02:00
|
|
|
case ams::svc::DebugEvent_CreateThread:
|
2020-07-19 05:03:27 +02:00
|
|
|
{
|
2020-07-31 10:27:09 +02:00
|
|
|
out->info.create_thread.thread_id = info->info.create_thread.thread_id;
|
|
|
|
out->info.create_thread.tls_address = info->info.create_thread.tls_address;
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugEvent_ExitProcess:
|
|
|
|
{
|
|
|
|
out->info.exit_process.reason = info->info.exit_process.reason;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugEvent_ExitThread:
|
|
|
|
{
|
|
|
|
out->info.exit_thread.reason = info->info.exit_thread.reason;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugEvent_Exception:
|
|
|
|
{
|
|
|
|
out->info.exception.type = info->info.exception.exception_type;
|
|
|
|
out->info.exception.address = info->info.exception.exception_address;
|
|
|
|
|
|
|
|
switch (info->info.exception.exception_type) {
|
|
|
|
case ams::svc::DebugException_UndefinedInstruction:
|
|
|
|
{
|
|
|
|
MESOSPHERE_ASSERT(info->info.exception.exception_data_count == 1);
|
|
|
|
out->info.exception.specific.undefined_instruction.insn = info->info.exception.exception_data[0];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_BreakPoint:
|
|
|
|
{
|
|
|
|
MESOSPHERE_ASSERT(info->info.exception.exception_data_count == 1);
|
|
|
|
out->info.exception.specific.break_point.type = static_cast<ams::svc::BreakPointType>(info->info.exception.exception_data[0]);
|
|
|
|
out->info.exception.specific.break_point.address = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_UserBreak:
|
|
|
|
{
|
|
|
|
MESOSPHERE_ASSERT(info->info.exception.exception_data_count == 3);
|
|
|
|
out->info.exception.specific.user_break.break_reason = static_cast<ams::svc::BreakReason>(info->info.exception.exception_data[0]);
|
|
|
|
out->info.exception.specific.user_break.address = info->info.exception.exception_data[1];
|
|
|
|
out->info.exception.specific.user_break.size = info->info.exception.exception_data[2];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_DebuggerBreak:
|
|
|
|
{
|
2023-02-21 17:16:15 +01:00
|
|
|
/* TODO: How does this work with non-4 cpu count? */
|
|
|
|
static_assert(cpu::NumCores <= 4);
|
|
|
|
|
|
|
|
MESOSPHERE_ASSERT(info->info.exception.exception_data_count == cpu::NumCores);
|
2020-07-19 05:03:27 +02:00
|
|
|
out->info.exception.specific.debugger_break.active_thread_ids[0] = info->info.exception.exception_data[0];
|
|
|
|
out->info.exception.specific.debugger_break.active_thread_ids[1] = info->info.exception.exception_data[1];
|
|
|
|
out->info.exception.specific.debugger_break.active_thread_ids[2] = info->info.exception.exception_data[2];
|
|
|
|
out->info.exception.specific.debugger_break.active_thread_ids[3] = info->info.exception.exception_data[3];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ams::svc::DebugException_UndefinedSystemCall:
|
|
|
|
{
|
|
|
|
MESOSPHERE_ASSERT(info->info.exception.exception_data_count == 1);
|
|
|
|
out->info.exception.specific.undefined_system_call.id = info->info.exception.exception_data[0];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-07-31 09:36:33 +02:00
|
|
|
Result KDebugBase::GetDebugEventInfo(ams::svc::lp64::DebugEventInfo *out) {
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(this->GetDebugEventInfoImpl(out));
|
2020-07-31 09:36:33 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
Result KDebugBase::GetDebugEventInfo(ams::svc::ilp32::DebugEventInfo *out) {
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(this->GetDebugEventInfoImpl(out));
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
2021-10-24 22:04:31 +02:00
|
|
|
void KDebugBase::Finalize() {
|
|
|
|
/* Perform base finalization. */
|
|
|
|
KSynchronizationObject::Finalize();
|
|
|
|
|
|
|
|
/* Perform post-synchronization finalization. */
|
|
|
|
this->OnFinalizeSynchronizationObject();
|
|
|
|
}
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
void KDebugBase::OnFinalizeSynchronizationObject() {
|
2020-07-20 05:06:21 +02:00
|
|
|
/* Detach from our process, if we have one. */
|
2021-09-18 03:10:05 +02:00
|
|
|
if (this->IsAttached() && this->OpenProcess()) {
|
|
|
|
/* Close the process when we're done with it. */
|
|
|
|
ON_SCOPE_EXIT { this->CloseProcess(); };
|
|
|
|
|
|
|
|
/* Get the process pointer. */
|
|
|
|
KProcess * const process = this->GetProcessUnsafe();
|
|
|
|
|
|
|
|
/* Lock both ourselves and the target process. */
|
|
|
|
KScopedLightLock state_lk(process->GetStateLock());
|
|
|
|
KScopedLightLock list_lk(process->GetListLock());
|
|
|
|
KScopedLightLock this_lk(m_lock);
|
2020-07-20 05:06:21 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
/* Check that we're still attached. */
|
|
|
|
if (m_is_attached) {
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* Detach ourselves from the process. */
|
|
|
|
process->ClearDebugObject(m_old_process_state);
|
|
|
|
|
|
|
|
/* Release all threads. */
|
|
|
|
const bool resume = (process->GetState() != KProcess::State_Crashed);
|
2020-07-20 05:06:21 +02:00
|
|
|
{
|
2021-09-18 03:10:05 +02:00
|
|
|
auto end = process->GetThreadList().end();
|
|
|
|
for (auto it = process->GetThreadList().begin(); it != end; ++it) {
|
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
|
|
|
/* Clear the thread's single-step state. */
|
2022-03-23 03:59:39 +01:00
|
|
|
it->ClearHardwareSingleStep();
|
2021-09-18 03:10:05 +02:00
|
|
|
#endif
|
2020-07-20 05:06:21 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
if (resume) {
|
|
|
|
/* If the process isn't crashed, resume threads. */
|
|
|
|
it->Resume(KThread::SuspendType_Debug);
|
|
|
|
} else {
|
|
|
|
/* Otherwise, suspend them. */
|
|
|
|
it->RequestSuspend(KThread::SuspendType_Debug);
|
2020-07-20 05:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-18 03:10:05 +02:00
|
|
|
|
|
|
|
/* Note we're now unattached. */
|
|
|
|
m_is_attached = false;
|
|
|
|
|
|
|
|
/* Close the initial reference opened to our process. */
|
|
|
|
this->CloseProcess();
|
2020-07-20 05:06:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free any pending events. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
while (!m_event_info_list.empty()) {
|
|
|
|
KEventInfo *info = std::addressof(m_event_info_list.front());
|
|
|
|
m_event_info_list.pop_front();
|
2020-07-20 05:06:21 +02:00
|
|
|
KEventInfo::Free(info);
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KDebugBase::IsSignaled() const {
|
2021-09-18 03:10:05 +02:00
|
|
|
bool empty;
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
empty = m_event_info_list.empty();
|
|
|
|
}
|
2020-07-19 05:03:27 +02:00
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
return !empty || !m_is_attached || this->GetProcessUnsafe()->IsTerminated();
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:16:15 +01:00
|
|
|
Result KDebugBase::ProcessDebugEvent(ams::svc::DebugEvent event, const uintptr_t *params, size_t num_params) {
|
2020-07-31 09:50:03 +02:00
|
|
|
/* Get the current process. */
|
|
|
|
KProcess *process = GetCurrentProcessPointer();
|
|
|
|
|
2020-07-31 10:27:09 +02:00
|
|
|
/* If the event is CreateThread and we've already attached, there's nothing to do. */
|
|
|
|
if (event == ams::svc::DebugEvent_CreateThread) {
|
2020-07-31 09:50:03 +02:00
|
|
|
R_SUCCEED_IF(GetCurrentThread().IsAttachedToDebugger());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
/* Lock the process and the scheduler. */
|
|
|
|
KScopedLightLock state_lk(process->GetStateLock());
|
|
|
|
KScopedLightLock list_lk(process->GetListLock());
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* If the current thread is terminating, we can't process an event. */
|
|
|
|
R_SUCCEED_IF(GetCurrentThread().IsTerminationRequested());
|
|
|
|
|
|
|
|
/* Get the debug object. If we have none, there's nothing to process. */
|
|
|
|
KDebugBase *debug = GetDebugObject(process);
|
|
|
|
R_SUCCEED_IF(debug == nullptr);
|
|
|
|
|
|
|
|
/* If the event is an exception and we don't have exception events enabled, we can't handle the event. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (event == ams::svc::DebugEvent_Exception && (debug->m_continue_flags & ams::svc::ContinueFlag_EnableExceptionEvent) == 0) {
|
2020-07-31 09:50:03 +02:00
|
|
|
GetCurrentThread().SetDebugExceptionResult(ResultSuccess());
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultNotHandled());
|
2020-07-31 09:50:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If the current thread is suspended, retry. */
|
|
|
|
if (GetCurrentThread().IsSuspended()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Suspend all the process's threads. */
|
|
|
|
{
|
|
|
|
auto end = process->GetThreadList().end();
|
|
|
|
for (auto it = process->GetThreadList().begin(); it != end; ++it) {
|
|
|
|
it->RequestSuspend(KThread::SuspendType_Debug);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Push the event. */
|
2023-02-21 17:16:15 +01:00
|
|
|
debug->PushDebugEvent(event, params, num_params);
|
2020-07-31 09:50:03 +02:00
|
|
|
debug->NotifyAvailable();
|
|
|
|
|
|
|
|
/* Set the process as breaked. */
|
|
|
|
process->SetDebugBreak();
|
|
|
|
|
2021-07-27 14:34:40 +02:00
|
|
|
/* If the event is an exception, set the result and clear single step. */
|
2020-07-31 09:50:03 +02:00
|
|
|
if (event == ams::svc::DebugEvent_Exception) {
|
|
|
|
GetCurrentThread().SetDebugExceptionResult(ResultSuccess());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Exit our retry loop. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the event is an exception, get the exception result. */
|
|
|
|
if (event == ams::svc::DebugEvent_Exception) {
|
|
|
|
/* Lock the scheduler. */
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
/* If the thread is terminating, we can't process the exception. */
|
|
|
|
R_UNLESS(!GetCurrentThread().IsTerminationRequested(), svc::ResultStopProcessingException());
|
|
|
|
|
|
|
|
/* Get the debug object. */
|
|
|
|
if (KDebugBase *debug = GetDebugObject(process); debug != nullptr) {
|
|
|
|
/* If we have one, check the debug exception. */
|
2022-02-14 23:45:32 +01:00
|
|
|
R_RETURN(GetCurrentThread().GetDebugExceptionResult());
|
2020-07-31 09:50:03 +02:00
|
|
|
} else {
|
|
|
|
/* We don't have a debug object, so stop processing the exception. */
|
2022-02-14 23:45:32 +01:00
|
|
|
R_THROW(svc::ResultStopProcessingException());
|
2020-07-31 09:50:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 17:16:15 +01:00
|
|
|
Result KDebugBase::OnDebugEvent(ams::svc::DebugEvent event, const uintptr_t *params, size_t num_params) {
|
2020-07-23 08:52:29 +02:00
|
|
|
if (KProcess *process = GetCurrentProcessPointer(); process != nullptr && process->IsAttachedToDebugger()) {
|
2023-02-21 17:16:15 +01:00
|
|
|
R_RETURN(ProcessDebugEvent(event, params, num_params));
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDebugBase::OnExitProcess(KProcess *process) {
|
|
|
|
MESOSPHERE_ASSERT(process != nullptr);
|
|
|
|
|
2020-07-31 09:50:03 +02:00
|
|
|
/* Check if we're attached to a debugger. */
|
2020-07-23 08:52:29 +02:00
|
|
|
if (process->IsAttachedToDebugger()) {
|
2020-07-31 09:50:03 +02:00
|
|
|
/* If we are, lock the scheduler. */
|
2020-07-23 08:52:29 +02:00
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2020-07-31 09:50:03 +02:00
|
|
|
/* Push the event. */
|
2020-07-23 08:52:29 +02:00
|
|
|
if (KDebugBase *debug = GetDebugObject(process); debug != nullptr) {
|
2023-02-21 17:16:15 +01:00
|
|
|
const uintptr_t params[1] = { static_cast<uintptr_t>(ams::svc::ProcessExitReason_ExitProcess) };
|
|
|
|
debug->PushDebugEvent(ams::svc::DebugEvent_ExitProcess, params, util::size(params));
|
2020-07-23 08:52:29 +02:00
|
|
|
debug->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDebugBase::OnTerminateProcess(KProcess *process) {
|
|
|
|
MESOSPHERE_ASSERT(process != nullptr);
|
|
|
|
|
2020-07-31 09:50:03 +02:00
|
|
|
/* Check if we're attached to a debugger. */
|
2020-07-23 08:52:29 +02:00
|
|
|
if (process->IsAttachedToDebugger()) {
|
2020-07-31 09:50:03 +02:00
|
|
|
/* If we are, lock the scheduler. */
|
2020-07-23 08:52:29 +02:00
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
2020-07-31 09:50:03 +02:00
|
|
|
/* Push the event. */
|
2020-07-23 08:52:29 +02:00
|
|
|
if (KDebugBase *debug = GetDebugObject(process); debug != nullptr) {
|
2023-02-21 17:16:15 +01:00
|
|
|
const uintptr_t params[1] = { static_cast<uintptr_t>(ams::svc::ProcessExitReason_TerminateProcess) };
|
|
|
|
debug->PushDebugEvent(ams::svc::DebugEvent_ExitProcess, params, util::size(params));
|
2020-07-23 08:52:29 +02:00
|
|
|
debug->NotifyAvailable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result KDebugBase::OnExitThread(KThread *thread) {
|
|
|
|
MESOSPHERE_ASSERT(thread != nullptr);
|
|
|
|
|
2020-07-31 09:50:03 +02:00
|
|
|
/* Check if we're attached to a debugger. */
|
2020-07-23 08:52:29 +02:00
|
|
|
if (KProcess *process = thread->GetOwnerProcess(); process != nullptr && process->IsAttachedToDebugger()) {
|
2020-07-31 09:50:03 +02:00
|
|
|
/* If we are, submit the event. */
|
2023-02-21 17:16:15 +01:00
|
|
|
const uintptr_t params[2] = { thread->GetId(), static_cast<uintptr_t>(thread->IsTerminationRequested() ? ams::svc::ThreadExitReason_TerminateThread : ams::svc::ThreadExitReason_ExitThread) };
|
|
|
|
R_TRY(OnDebugEvent(ams::svc::DebugEvent_ExitThread, params, util::size(params)));
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 23:45:32 +01:00
|
|
|
R_SUCCEED();
|
2020-07-23 08:52:29 +02:00
|
|
|
}
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
}
|