mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 01:07:35 +00:00
Kernel: Store Thread name as a KString
This commit is contained in:
parent
07599b48de
commit
d5d8fba579
8 changed files with 43 additions and 33 deletions
|
@ -464,6 +464,7 @@ private:
|
||||||
process_object.add("kernel", process.is_kernel_process());
|
process_object.add("kernel", process.is_kernel_process());
|
||||||
auto thread_array = process_object.add_array("threads");
|
auto thread_array = process_object.add_array("threads");
|
||||||
process.for_each_thread([&](const Thread& thread) {
|
process.for_each_thread([&](const Thread& thread) {
|
||||||
|
ScopedSpinLock locker(thread.get_lock());
|
||||||
auto thread_object = thread_array.add_object();
|
auto thread_object = thread_array.add_object();
|
||||||
#if LOCK_DEBUG
|
#if LOCK_DEBUG
|
||||||
thread_object.add("lock_count", thread.lock_count());
|
thread_object.add("lock_count", thread.lock_count());
|
||||||
|
|
|
@ -735,7 +735,7 @@ KResult Process::send_signal(u8 signal, Process* sender)
|
||||||
return ESRCH;
|
return ESRCH;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, const String& name, u32 affinity, bool joinable)
|
RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, OwnPtr<KString> name, u32 affinity, bool joinable)
|
||||||
{
|
{
|
||||||
VERIFY((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
|
VERIFY((priority >= THREAD_PRIORITY_MIN) && (priority <= THREAD_PRIORITY_MAX));
|
||||||
|
|
||||||
|
@ -746,7 +746,7 @@ RefPtr<Thread> Process::create_kernel_thread(void (*entry)(void*), void* entry_d
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
auto thread = thread_or_error.release_value();
|
auto thread = thread_or_error.release_value();
|
||||||
thread->set_name(name);
|
thread->set_name(move(name));
|
||||||
thread->set_affinity(affinity);
|
thread->set_affinity(affinity);
|
||||||
thread->set_priority(priority);
|
thread->set_priority(priority);
|
||||||
if (!joinable)
|
if (!joinable)
|
||||||
|
|
|
@ -185,7 +185,7 @@ public:
|
||||||
static NonnullRefPtrVector<Process> all_processes();
|
static NonnullRefPtrVector<Process> all_processes();
|
||||||
|
|
||||||
template<typename EntryFunction>
|
template<typename EntryFunction>
|
||||||
RefPtr<Thread> create_kernel_thread(EntryFunction entry, u32 priority, const String& name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true)
|
RefPtr<Thread> create_kernel_thread(EntryFunction entry, u32 priority, OwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true)
|
||||||
{
|
{
|
||||||
auto* entry_func = new EntryFunction(move(entry));
|
auto* entry_func = new EntryFunction(move(entry));
|
||||||
return create_kernel_thread([](void* data) {
|
return create_kernel_thread([](void* data) {
|
||||||
|
@ -193,9 +193,9 @@ public:
|
||||||
(*func)();
|
(*func)();
|
||||||
delete func;
|
delete func;
|
||||||
},
|
},
|
||||||
priority, name, affinity, joinable);
|
priority, move(name), affinity, joinable);
|
||||||
}
|
}
|
||||||
RefPtr<Thread> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, const String& name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true);
|
RefPtr<Thread> create_kernel_thread(void (*entry)(void*), void* entry_data, u32 priority, OwnPtr<KString> name, u32 affinity = THREAD_AFFINITY_DEFAULT, bool joinable = true);
|
||||||
|
|
||||||
bool is_profiling() const { return m_profiling; }
|
bool is_profiling() const { return m_profiling; }
|
||||||
void set_profiling(bool profiling) { m_profiling = profiling; }
|
void set_profiling(bool profiling) { m_profiling = profiling; }
|
||||||
|
|
|
@ -424,7 +424,7 @@ UNMAP_AFTER_INIT void Scheduler::initialize()
|
||||||
VERIFY(s_colonel_process);
|
VERIFY(s_colonel_process);
|
||||||
VERIFY(idle_thread);
|
VERIFY(idle_thread);
|
||||||
idle_thread->set_priority(THREAD_PRIORITY_MIN);
|
idle_thread->set_priority(THREAD_PRIORITY_MIN);
|
||||||
idle_thread->set_name(StringView("idle thread #0"));
|
idle_thread->set_name(KString::try_create("idle thread #0"));
|
||||||
|
|
||||||
set_idle_thread(idle_thread);
|
set_idle_thread(idle_thread);
|
||||||
}
|
}
|
||||||
|
@ -443,7 +443,7 @@ UNMAP_AFTER_INIT Thread* Scheduler::create_ap_idle_thread(u32 cpu)
|
||||||
VERIFY(Processor::is_bootstrap_processor());
|
VERIFY(Processor::is_bootstrap_processor());
|
||||||
|
|
||||||
VERIFY(s_colonel_process);
|
VERIFY(s_colonel_process);
|
||||||
Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, String::formatted("idle thread #{}", cpu), 1 << cpu, false);
|
Thread* idle_thread = s_colonel_process->create_kernel_thread(idle_loop, nullptr, THREAD_PRIORITY_MIN, KString::try_create(String::formatted("idle thread #{}", cpu)), 1 << cpu, false);
|
||||||
VERIFY(idle_thread);
|
VERIFY(idle_thread);
|
||||||
return idle_thread;
|
return idle_thread;
|
||||||
}
|
}
|
||||||
|
|
|
@ -634,7 +634,7 @@ KResult Process::do_exec(NonnullRefPtr<FileDescription> main_program_description
|
||||||
// NOTE: Be careful to not trigger any page faults below!
|
// NOTE: Be careful to not trigger any page faults below!
|
||||||
|
|
||||||
m_name = parts.take_last();
|
m_name = parts.take_last();
|
||||||
new_main_thread->set_name(m_name);
|
new_main_thread->set_name(KString::try_create(m_name));
|
||||||
|
|
||||||
{
|
{
|
||||||
ProtectedDataMutationScope scope { *this };
|
ProtectedDataMutationScope scope { *this };
|
||||||
|
|
|
@ -54,7 +54,7 @@ KResultOr<FlatPtr> Process::sys$create_thread(void* (*entry)(void*), Userspace<c
|
||||||
// So give it a unique name until the user calls $set_thread_name on it
|
// So give it a unique name until the user calls $set_thread_name on it
|
||||||
// length + 4 to give space for our extra junk at the end
|
// length + 4 to give space for our extra junk at the end
|
||||||
StringBuilder builder(m_name.length() + 4);
|
StringBuilder builder(m_name.length() + 4);
|
||||||
thread->set_name(String::formatted("{} [{}]", m_name, thread->tid().value()));
|
thread->set_name(KString::try_create(String::formatted("{} [{}]", m_name, thread->tid().value())));
|
||||||
|
|
||||||
if (!is_thread_joinable)
|
if (!is_thread_joinable)
|
||||||
thread->detach();
|
thread->detach();
|
||||||
|
@ -186,12 +186,14 @@ KResultOr<FlatPtr> Process::sys$set_thread_name(pid_t tid, Userspace<const char*
|
||||||
{
|
{
|
||||||
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
||||||
REQUIRE_PROMISE(stdio);
|
REQUIRE_PROMISE(stdio);
|
||||||
auto name = copy_string_from_user(user_name, user_name_length);
|
|
||||||
if (name.is_null())
|
auto name_or_error = try_copy_kstring_from_user(user_name, user_name_length);
|
||||||
return EFAULT;
|
if (name_or_error.is_error())
|
||||||
|
return name_or_error.error();
|
||||||
|
auto name = name_or_error.release_value();
|
||||||
|
|
||||||
const size_t max_thread_name_size = 64;
|
const size_t max_thread_name_size = 64;
|
||||||
if (name.length() > max_thread_name_size)
|
if (name->length() > max_thread_name_size)
|
||||||
return EINVAL;
|
return EINVAL;
|
||||||
|
|
||||||
auto thread = Thread::from_tid(tid);
|
auto thread = Thread::from_tid(tid);
|
||||||
|
@ -213,12 +215,20 @@ KResultOr<FlatPtr> Process::sys$get_thread_name(pid_t tid, Userspace<char*> buff
|
||||||
if (!thread || thread->pid() != pid())
|
if (!thread || thread->pid() != pid())
|
||||||
return ESRCH;
|
return ESRCH;
|
||||||
|
|
||||||
// We must make a temporary copy here to avoid a race with sys$set_thread_name
|
ScopedSpinLock locker(thread->get_lock());
|
||||||
auto thread_name = thread->name();
|
auto thread_name = thread->name();
|
||||||
|
|
||||||
|
if (thread_name.is_null()) {
|
||||||
|
char null_terminator = '\0';
|
||||||
|
if (!copy_to_user(buffer, &null_terminator, sizeof(null_terminator)))
|
||||||
|
return EFAULT;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (thread_name.length() + 1 > buffer_size)
|
if (thread_name.length() + 1 > buffer_size)
|
||||||
return ENAMETOOLONG;
|
return ENAMETOOLONG;
|
||||||
|
|
||||||
if (!copy_to_user(buffer, thread_name.characters(), thread_name.length() + 1))
|
if (!copy_to_user(buffer, thread_name.characters_without_null_termination(), thread_name.length() + 1))
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,18 +52,20 @@ KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> proce
|
||||||
if (!block_timer)
|
if (!block_timer)
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
||||||
auto thread = adopt_ref_if_nonnull(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull(), fpu_state.release_nonnull()));
|
auto name = KString::try_create(process->name());
|
||||||
|
|
||||||
|
auto thread = adopt_ref_if_nonnull(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull(), fpu_state.release_nonnull(), move(name)));
|
||||||
if (!thread)
|
if (!thread)
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
||||||
return thread.release_nonnull();
|
return thread.release_nonnull();
|
||||||
}
|
}
|
||||||
|
|
||||||
Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer, NonnullOwnPtr<FPUState> fpu_state)
|
Thread::Thread(NonnullRefPtr<Process> process, NonnullOwnPtr<Region> kernel_stack_region, NonnullRefPtr<Timer> block_timer, NonnullOwnPtr<FPUState> fpu_state, OwnPtr<KString> name)
|
||||||
: m_process(move(process))
|
: m_process(move(process))
|
||||||
, m_kernel_stack_region(move(kernel_stack_region))
|
, m_kernel_stack_region(move(kernel_stack_region))
|
||||||
, m_fpu_state(move(fpu_state))
|
, m_fpu_state(move(fpu_state))
|
||||||
, m_name(m_process->name())
|
, m_name(move(name))
|
||||||
, m_block_timer(block_timer)
|
, m_block_timer(block_timer)
|
||||||
, m_global_procfs_inode_index(ProcFSComponentRegistry::the().allocate_inode_index())
|
, m_global_procfs_inode_index(ProcFSComponentRegistry::the().allocate_inode_index())
|
||||||
{
|
{
|
||||||
|
@ -1020,7 +1022,7 @@ DispatchSignalResult Thread::dispatch_signal(u8 signal)
|
||||||
|
|
||||||
push_value_on_user_stack(stack, signal);
|
push_value_on_user_stack(stack, signal);
|
||||||
push_value_on_user_stack(stack, handler_vaddr.get());
|
push_value_on_user_stack(stack, handler_vaddr.get());
|
||||||
push_value_on_user_stack(stack, 0); //push fake return address
|
push_value_on_user_stack(stack, 0); // push fake return address
|
||||||
|
|
||||||
VERIFY((*stack % 16) == 0);
|
VERIFY((*stack % 16) == 0);
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -26,6 +26,7 @@
|
||||||
#include <Kernel/FileSystem/InodeIdentifier.h>
|
#include <Kernel/FileSystem/InodeIdentifier.h>
|
||||||
#include <Kernel/Forward.h>
|
#include <Kernel/Forward.h>
|
||||||
#include <Kernel/KResult.h>
|
#include <Kernel/KResult.h>
|
||||||
|
#include <Kernel/KString.h>
|
||||||
#include <Kernel/LockMode.h>
|
#include <Kernel/LockMode.h>
|
||||||
#include <Kernel/Scheduler.h>
|
#include <Kernel/Scheduler.h>
|
||||||
#include <Kernel/TimerQueue.h>
|
#include <Kernel/TimerQueue.h>
|
||||||
|
@ -178,19 +179,15 @@ public:
|
||||||
Process& process() { return m_process; }
|
Process& process() { return m_process; }
|
||||||
const Process& process() const { return m_process; }
|
const Process& process() const { return m_process; }
|
||||||
|
|
||||||
String name() const
|
// NOTE: This returns a null-terminated string.
|
||||||
|
StringView name() const
|
||||||
{
|
{
|
||||||
// Because the name can be changed, we can't return a const
|
// NOTE: Whoever is calling this needs to be holding our lock while reading the name.
|
||||||
// reference here. We must make a copy
|
VERIFY(m_lock.own_lock());
|
||||||
ScopedSpinLock lock(m_lock);
|
return m_name ? m_name->view() : StringView {};
|
||||||
return m_name;
|
|
||||||
}
|
}
|
||||||
void set_name(const StringView& s)
|
|
||||||
{
|
void set_name(OwnPtr<KString> name)
|
||||||
ScopedSpinLock lock(m_lock);
|
|
||||||
m_name = s;
|
|
||||||
}
|
|
||||||
void set_name(String&& name)
|
|
||||||
{
|
{
|
||||||
ScopedSpinLock lock(m_lock);
|
ScopedSpinLock lock(m_lock);
|
||||||
m_name = move(name);
|
m_name = move(name);
|
||||||
|
@ -1218,7 +1215,7 @@ public:
|
||||||
String backtrace();
|
String backtrace();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Thread(NonnullRefPtr<Process>, NonnullOwnPtr<Region>, NonnullRefPtr<Timer>, NonnullOwnPtr<FPUState>);
|
Thread(NonnullRefPtr<Process>, NonnullOwnPtr<Region>, NonnullRefPtr<Timer>, NonnullOwnPtr<FPUState>, OwnPtr<KString>);
|
||||||
|
|
||||||
IntrusiveListNode<Thread> m_process_thread_list_node;
|
IntrusiveListNode<Thread> m_process_thread_list_node;
|
||||||
int m_runnable_priority { -1 };
|
int m_runnable_priority { -1 };
|
||||||
|
@ -1349,7 +1346,7 @@ private:
|
||||||
|
|
||||||
OwnPtr<FPUState> m_fpu_state;
|
OwnPtr<FPUState> m_fpu_state;
|
||||||
State m_state { Invalid };
|
State m_state { Invalid };
|
||||||
String m_name;
|
OwnPtr<KString> m_name;
|
||||||
u32 m_priority { THREAD_PRIORITY_NORMAL };
|
u32 m_priority { THREAD_PRIORITY_NORMAL };
|
||||||
|
|
||||||
State m_stop_state { Invalid };
|
State m_stop_state { Invalid };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue