2020-02-07 20:51:58 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-02-07 20:51:58 +01: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/>.
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <mesosphere/kern_common.hpp>
|
|
|
|
#include <mesosphere/kern_k_synchronization_object.hpp>
|
2020-07-19 05:03:27 +02:00
|
|
|
#include <mesosphere/kern_k_process.hpp>
|
|
|
|
#include <mesosphere/kern_k_event_info.hpp>
|
|
|
|
#include <mesosphere/kern_k_light_lock.hpp>
|
2020-02-07 20:51:58 +01:00
|
|
|
|
|
|
|
namespace ams::kern {
|
|
|
|
|
|
|
|
class KDebugBase : public KSynchronizationObject {
|
2020-07-19 05:03:27 +02:00
|
|
|
protected:
|
|
|
|
using DebugEventList = util::IntrusiveListBaseTraits<KEventInfo>::ListType;
|
|
|
|
private:
|
2020-12-18 02:18:47 +01:00
|
|
|
DebugEventList m_event_info_list;
|
|
|
|
u32 m_continue_flags;
|
2021-10-20 00:24:15 +02:00
|
|
|
KSharedAutoObject<KProcess> m_process_holder;
|
2020-12-18 02:18:47 +01:00
|
|
|
KLightLock m_lock;
|
|
|
|
KProcess::State m_old_process_state;
|
2021-09-18 03:10:05 +02:00
|
|
|
bool m_is_attached;
|
2020-02-07 20:51:58 +01:00
|
|
|
public:
|
2021-10-24 00:25:20 +02:00
|
|
|
explicit KDebugBase() { /* ... */ }
|
2020-07-31 11:49:43 +02:00
|
|
|
protected:
|
|
|
|
bool Is64Bit() const;
|
2020-07-19 05:03:27 +02:00
|
|
|
public:
|
|
|
|
void Initialize();
|
2021-10-24 22:04:31 +02:00
|
|
|
void Finalize();
|
2020-07-31 05:49:58 +02:00
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
Result Attach(KProcess *process);
|
2020-07-31 07:18:19 +02:00
|
|
|
Result BreakProcess();
|
|
|
|
Result TerminateProcess();
|
2020-07-19 05:03:27 +02:00
|
|
|
|
2020-07-31 05:49:58 +02:00
|
|
|
Result ContinueDebug(const u32 flags, const u64 *thread_ids, size_t num_thread_ids);
|
2020-07-31 01:52:11 +02:00
|
|
|
|
2020-07-30 01:21:54 +02:00
|
|
|
Result QueryMemoryInfo(ams::svc::MemoryInfo *out_memory_info, ams::svc::PageInfo *out_page_info, KProcessAddress address);
|
2020-07-30 12:37:40 +02:00
|
|
|
Result ReadMemory(KProcessAddress buffer, KProcessAddress address, size_t size);
|
|
|
|
Result WriteMemory(KProcessAddress buffer, KProcessAddress address, size_t size);
|
2020-07-30 01:21:54 +02:00
|
|
|
|
2020-07-31 11:49:43 +02:00
|
|
|
Result GetThreadContext(ams::svc::ThreadContext *out, u64 thread_id, u32 context_flags);
|
|
|
|
Result SetThreadContext(const ams::svc::ThreadContext &ctx, u64 thread_id, u32 context_flags);
|
|
|
|
|
2020-07-31 01:31:58 +02:00
|
|
|
Result GetRunningThreadInfo(ams::svc::LastThreadContext *out_context, u64 *out_thread_id);
|
|
|
|
|
2020-07-19 05:03:27 +02:00
|
|
|
Result GetDebugEventInfo(ams::svc::lp64::DebugEventInfo *out);
|
|
|
|
Result GetDebugEventInfo(ams::svc::ilp32::DebugEventInfo *out);
|
|
|
|
|
2021-09-18 03:10:05 +02:00
|
|
|
ALWAYS_INLINE bool IsAttached() const {
|
|
|
|
return m_is_attached;
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE bool OpenProcess() {
|
|
|
|
return m_process_holder.Open();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE void CloseProcess() {
|
|
|
|
return m_process_holder.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
ALWAYS_INLINE KProcess *GetProcessUnsafe() const {
|
2021-10-20 00:24:15 +02:00
|
|
|
return m_process_holder.Get();
|
2021-09-18 03:10:05 +02:00
|
|
|
}
|
2020-07-19 05:03:27 +02:00
|
|
|
private:
|
|
|
|
void PushDebugEvent(ams::svc::DebugEvent event, uintptr_t param0 = 0, uintptr_t param1 = 0, uintptr_t param2 = 0, uintptr_t param3 = 0, uintptr_t param4 = 0);
|
|
|
|
void EnqueueDebugEventInfo(KEventInfo *info);
|
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 GetDebugEventInfoImpl(T *out);
|
2020-07-19 05:03:27 +02:00
|
|
|
public:
|
|
|
|
virtual bool IsSignaled() const override;
|
2021-10-24 22:04:31 +02:00
|
|
|
private:
|
|
|
|
/* NOTE: This is public/virtual override in Nintendo's kernel. */
|
|
|
|
void OnFinalizeSynchronizationObject();
|
2020-07-23 08:52:29 +02:00
|
|
|
private:
|
|
|
|
static Result ProcessDebugEvent(ams::svc::DebugEvent event, uintptr_t param0, uintptr_t param1, uintptr_t param2, uintptr_t param3, uintptr_t param4);
|
2020-07-19 05:03:27 +02:00
|
|
|
public:
|
2020-07-23 08:52:29 +02:00
|
|
|
static Result OnDebugEvent(ams::svc::DebugEvent event, uintptr_t param0 = 0, uintptr_t param1 = 0, uintptr_t param2 = 0, uintptr_t param3 = 0, uintptr_t param4 = 0);
|
|
|
|
static Result OnExitProcess(KProcess *process);
|
|
|
|
static Result OnTerminateProcess(KProcess *process);
|
|
|
|
static Result OnExitThread(KThread *thread);
|
2020-07-19 05:03:27 +02:00
|
|
|
static KEventInfo *CreateDebugEvent(ams::svc::DebugEvent event, uintptr_t param0, uintptr_t param1, uintptr_t param2, uintptr_t param3, uintptr_t param4, u64 thread_id);
|
2020-02-07 20:51:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|