kern: improve kdebug attach semantics
This commit is contained in:
@@ -313,11 +313,17 @@ namespace ams::kern::svc {
|
||||
ON_SCOPE_EXIT { thread->Close(); };
|
||||
|
||||
/* Get the process from the debug object. */
|
||||
KScopedAutoObject process = debug->GetProcess();
|
||||
R_UNLESS(process.IsNotNull(), svc::ResultProcessTerminated());
|
||||
R_UNLESS(debug->IsAttached(), svc::ResultProcessTerminated());
|
||||
R_UNLESS(debug->OpenProcess(), svc::ResultProcessTerminated());
|
||||
|
||||
/* Close the process when we're done. */
|
||||
ON_SCOPE_EXIT { debug->CloseProcess(); };
|
||||
|
||||
/* Get the proces. */
|
||||
KProcess * const process = debug->GetProcessUnsafe();
|
||||
|
||||
/* Verify that the process is the thread's parent. */
|
||||
R_UNLESS(process.GetPointerUnsafe() == thread->GetOwnerProcess(), svc::ResultInvalidThreadId());
|
||||
R_UNLESS(process == thread->GetOwnerProcess(), svc::ResultInvalidThreadId());
|
||||
|
||||
/* Get the parameter. */
|
||||
switch (param) {
|
||||
|
||||
@@ -36,27 +36,35 @@ namespace ams::kern::svc {
|
||||
R_UNLESS(obj.IsNotNull(), svc::ResultInvalidHandle());
|
||||
|
||||
/* Get the process from the object. */
|
||||
KProcess *process = nullptr;
|
||||
if (KProcess *p = obj->DynamicCast<KProcess *>(); p != nullptr) {
|
||||
if (KProcess *process = obj->DynamicCast<KProcess *>(); process != nullptr) {
|
||||
/* The object is a process, so we can use it directly. */
|
||||
process = p;
|
||||
|
||||
/* Make sure the target process exists. */
|
||||
R_UNLESS(process != nullptr, svc::ResultInvalidHandle());
|
||||
|
||||
/* Get the process id. */
|
||||
*out_process_id = process->GetId();
|
||||
} else if (KThread *t = obj->DynamicCast<KThread *>(); t != nullptr) {
|
||||
/* The object is a thread, so we want to use its parent. */
|
||||
process = reinterpret_cast<KThread *>(obj.GetPointerUnsafe())->GetOwnerProcess();
|
||||
KProcess *process = t->GetOwnerProcess();
|
||||
|
||||
/* Make sure the target process exists. */
|
||||
R_UNLESS(process != nullptr, svc::ResultInvalidHandle());
|
||||
|
||||
/* Get the process id. */
|
||||
*out_process_id = process->GetId();
|
||||
} else if (KDebug *d = obj->DynamicCast<KDebug *>(); d != nullptr) {
|
||||
/* The object is a debug, so we want to use the process it's attached to. */
|
||||
obj = d->GetProcess();
|
||||
|
||||
if (obj.IsNotNull()) {
|
||||
process = static_cast<KProcess *>(obj.GetPointerUnsafe());
|
||||
}
|
||||
/* Make sure the target process exists. */
|
||||
R_UNLESS(d->IsAttached(), svc::ResultInvalidHandle());
|
||||
R_UNLESS(d->OpenProcess(), svc::ResultInvalidHandle());
|
||||
ON_SCOPE_EXIT { d->CloseProcess(); };
|
||||
|
||||
/* Get the process id. */
|
||||
*out_process_id = d->GetProcessUnsafe()->GetProcessId();
|
||||
}
|
||||
|
||||
/* Make sure the target process exists. */
|
||||
R_UNLESS(process != nullptr, svc::ResultInvalidHandle());
|
||||
|
||||
/* Get the process id. */
|
||||
*out_process_id = process->GetId();
|
||||
return ResultSuccess();
|
||||
}
|
||||
|
||||
|
||||
@@ -235,12 +235,13 @@ namespace ams::kern::svc {
|
||||
/* Try to get as a debug object. */
|
||||
KScopedAutoObject debug = handle_table.GetObject<KDebug>(debug_handle);
|
||||
if (debug.IsNotNull()) {
|
||||
/* Get the debug object's process. */
|
||||
KScopedAutoObject process = debug->GetProcess();
|
||||
R_UNLESS(process.IsNotNull(), svc::ResultProcessTerminated());
|
||||
/* Check that the debug object has a process. */
|
||||
R_UNLESS(debug->IsAttached(), svc::ResultProcessTerminated());
|
||||
R_UNLESS(debug->OpenProcess(), svc::ResultProcessTerminated());
|
||||
ON_SCOPE_EXIT { debug->CloseProcess(); };
|
||||
|
||||
/* Get the thread list. */
|
||||
R_TRY(process->GetThreadList(out_num_threads, out_thread_ids, max_out_count));
|
||||
R_TRY(debug->GetProcessUnsafe()->GetThreadList(out_num_threads, out_thread_ids, max_out_count));
|
||||
} else {
|
||||
/* Try to get as a process. */
|
||||
KScopedAutoObject process = handle_table.GetObjectWithoutPseudoHandle<KProcess>(debug_handle);
|
||||
|
||||
Reference in New Issue
Block a user