KCOVInstance.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2021, Patrick Meyer <git@the-space.agency>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <Kernel/Locking/Spinlock.h>
  8. #include <Kernel/Memory/AnonymousVMObject.h>
  9. namespace Kernel {
  10. // Note: These need to be kept in sync with Userland/Libraries/LibC/sys/kcov.h
  11. typedef volatile u64 kcov_pc_t;
  12. #define KCOV_ENTRY_SIZE sizeof(kcov_pc_t)
  13. #define KCOV_MAX_ENTRIES (10 * 1024 * 1024)
  14. /*
  15. * One KCOVInstance is allocated per process, when the process opens /dev/kcov
  16. * for the first time. At this point it is in state OPENED. When a thread in
  17. * the same process then uses the KCOV_ENABLE ioctl on the block device, the
  18. * instance enters state TRACING.
  19. *
  20. * A KCOVInstance in state TRACING can return to state OPENED by either the
  21. * KCOV_DISABLE ioctl or by killing the thread. A KCOVInstance in state OPENED
  22. * can return to state UNUSED only when the process dies. At this point
  23. * KCOVDevice::free_process will delete the KCOVInstance.
  24. */
  25. class KCOVInstance final {
  26. public:
  27. explicit KCOVInstance(ProcessID pid);
  28. KResult buffer_allocate(size_t buffer_size_in_entries);
  29. bool has_buffer() const { return m_buffer != nullptr; }
  30. void buffer_add_pc(u64 pc);
  31. Spinlock<u8> lock;
  32. enum {
  33. UNUSED = 0,
  34. OPENED = 1,
  35. TRACING = 2,
  36. } state;
  37. RefPtr<Memory::AnonymousVMObject> vmobject;
  38. private:
  39. ProcessID m_pid = { 0 };
  40. u64 m_buffer_size_in_entries = { 0 };
  41. size_t m_buffer_size_in_bytes = { 0 };
  42. kcov_pc_t* m_buffer = { nullptr };
  43. // Here to ensure it's not garbage collected at the end of open()
  44. OwnPtr<Memory::Region> m_kernel_region;
  45. };
  46. }