kern: implement svc trace

This commit is contained in:
Michael Scire 2020-07-31 19:48:22 -07:00 committed by SciresM
parent f9d68db3f6
commit 920b017677
8 changed files with 203 additions and 19 deletions

View file

@ -25,6 +25,7 @@ namespace ams::kern {
constinit KSpinLock g_ktrace_lock;
constinit KVirtualAddress g_ktrace_buffer_address = Null<KVirtualAddress>;
constinit size_t g_ktrace_buffer_size = 0;
constinit u64 g_type_filter = 0;
struct KTraceHeader {
u32 magic;
@ -47,6 +48,10 @@ namespace ams::kern {
static_assert(util::is_pod<KTraceRecord>::value);
static_assert(sizeof(KTraceRecord) == 0x40);
ALWAYS_INLINE bool IsTypeFiltered(u8 type) {
return (g_type_filter & (UINT64_C(1) << (type & (BITSIZEOF(u64) - 1)))) != 0;
}
}
void KTrace::Initialize(KVirtualAddress address, size_t size) {
@ -67,6 +72,9 @@ namespace ams::kern {
/* Set the global data. */
g_ktrace_buffer_address = address;
g_ktrace_buffer_size = size;
/* Set the filters to defaults. */
g_type_filter = ~(UINT64_C(0));
}
}
}
@ -101,4 +109,42 @@ namespace ams::kern {
}
}
void KTrace::PushRecord(u8 type, u64 param0, u64 param1, u64 param2, u64 param3, u64 param4, u64 param5) {
/* Get exclusive access to the trace buffer. */
KScopedInterruptDisable di;
KScopedSpinLock lk(g_ktrace_lock);
/* Check whether we should push the record to the trace buffer. */
if (s_is_active && IsTypeFiltered(type)) {
/* Get the current thread and process. */
KThread &cur_thread = GetCurrentThread();
KProcess *cur_process = GetCurrentProcessPointer();
/* Get the current record index from the header. */
KTraceHeader *header = GetPointer<KTraceHeader>(g_ktrace_buffer_address);
u32 index = header->index;
/* Get the current record. */
KTraceRecord *record = GetPointer<KTraceRecord>(g_ktrace_buffer_address + header->offset + index * sizeof(KTraceRecord));
/* Set the record's data. */
*record = {
.core_id = static_cast<u8>(GetCurrentCoreId()),
.type = type,
.process_id = static_cast<u16>(cur_process != nullptr ? cur_process->GetId() : ~0),
.thread_id = static_cast<u32>(cur_thread.GetId()),
.tick = static_cast<u64>(KHardwareTimer::GetTick()),
.data = { param0, param1, param2, param3, param4, param5 },
};
/* Advance the current index. */
if ((++index) >= header->count) {
index = 0;
}
/* Set the next index. */
header->index = index;
}
}
}