2020-07-30 23:38:15 +02:00
|
|
|
/*
|
2021-01-11 09:52:18 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-07-30 23:38:15 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-11-13 22:38:58 +02:00
|
|
|
#include <Kernel/CoreDump.h>
|
2021-01-11 09:52:18 +01:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
|
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-03-02 17:19:35 +01:00
|
|
|
bool g_profiling_all_threads;
|
2021-04-25 23:42:36 +02:00
|
|
|
PerformanceEventBuffer* g_global_perf_events;
|
2021-03-02 17:19:35 +01:00
|
|
|
|
2021-03-01 13:49:16 +01:00
|
|
|
KResultOr<int> Process::sys$profiling_enable(pid_t pid)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
2021-03-02 17:19:35 +01:00
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
if (!is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
ScopedCritical critical;
|
|
|
|
|
if (g_global_perf_events)
|
|
|
|
|
g_global_perf_events->clear();
|
|
|
|
|
else
|
|
|
|
|
g_global_perf_events = PerformanceEventBuffer::try_create_with_size(32 * MiB).leak_ptr();
|
2021-04-25 23:42:36 +02:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
|
Process::for_each([](auto& process) {
|
|
|
|
|
g_global_perf_events->add_process(process, ProcessEventType::Create);
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-03-02 17:19:35 +01:00
|
|
|
g_profiling_all_threads = true;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2020-08-01 20:04:56 -06:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!process)
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (process->is_dead())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
2021-03-10 19:59:46 +01:00
|
|
|
if (!is_superuser() && process->uid() != euid())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-03-02 16:55:54 +01:00
|
|
|
if (!process->create_perf_events_buffer_if_needed())
|
|
|
|
|
return ENOMEM;
|
2020-07-30 23:38:15 +02:00
|
|
|
process->set_profiling(true);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-01 13:49:16 +01:00
|
|
|
KResultOr<int> Process::sys$profiling_disable(pid_t pid)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2021-04-18 21:10:05 -07:00
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
|
2021-03-02 17:19:35 +01:00
|
|
|
if (pid == -1) {
|
|
|
|
|
if (!is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
ScopedCritical critical;
|
|
|
|
|
g_profiling_all_threads = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-30 23:38:15 +02:00
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
2020-08-01 20:04:56 -06:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!process)
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
2021-03-10 19:59:46 +01:00
|
|
|
if (!is_superuser() && process->uid() != euid())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-01-11 09:52:18 +01:00
|
|
|
if (!process->is_profiling())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2020-07-30 23:38:15 +02:00
|
|
|
process->set_profiling(false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-18 21:10:05 -07:00
|
|
|
KResultOr<int> Process::sys$profiling_free_buffer(pid_t pid)
|
|
|
|
|
{
|
|
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
if (!is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
|
|
OwnPtr<PerformanceEventBuffer> perf_events;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ScopedCritical critical;
|
|
|
|
|
|
|
|
|
|
perf_events = g_global_perf_events;
|
|
|
|
|
g_global_perf_events = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ScopedSpinLock lock(g_processes_lock);
|
|
|
|
|
auto process = Process::from_pid(pid);
|
|
|
|
|
if (!process)
|
|
|
|
|
return ESRCH;
|
|
|
|
|
if (!is_superuser() && process->uid() != euid())
|
|
|
|
|
return EPERM;
|
|
|
|
|
if (process->is_profiling())
|
|
|
|
|
return EINVAL;
|
|
|
|
|
process->delete_perf_events_buffer();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|