diff options
| author | Zack <zmasi21@gmail.com> | 2026-11-28 20:46:18 -0800 |
|---|---|---|
| committer | Zack <zmasi21@gmail.com> | 2026-11-28 20:46:18 -0800 |
| commit | 770f5e0e18a9bcc36fd795f953333ab75b441cec (patch) | |
| tree | 169f11c62983cd79a4d67b56808619dca2c74f5b /runtime/src/thread_stacks.cpp | |
| parent | b55056766ee6dda71f8a86565d8eeeee3a159846 (diff) | |
| parent | ef329958483832297acb53e68be94b439e88a90e (diff) | |
Merge remote-tracking branch 'origin/main' into work/dstorage-multilevel-bcpack-fit
Diffstat (limited to 'runtime/src/thread_stacks.cpp')
| -rw-r--r-- | runtime/src/thread_stacks.cpp | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/runtime/src/thread_stacks.cpp b/runtime/src/thread_stacks.cpp index 38d7cc2a4..d87d91482 100644 --- a/runtime/src/thread_stacks.cpp +++ b/runtime/src/thread_stacks.cpp | |||
| @@ -9,6 +9,7 @@ | |||
| 9 | #include <chrono> | 9 | #include <chrono> |
| 10 | #include <condition_variable> | 10 | #include <condition_variable> |
| 11 | #include <cstdio> | 11 | #include <cstdio> |
| 12 | #include <limits> | ||
| 12 | #include <mutex> | 13 | #include <mutex> |
| 13 | #include <string> | 14 | #include <string> |
| 14 | 15 | ||
| @@ -39,12 +40,18 @@ bool copy_context_and_stack(HANDLE thread, CONTEXT& context, std::byte* buffer, | |||
| 39 | region.State == MEM_COMMIT && !(region.Protect & (PAGE_GUARD | PAGE_NOACCESS))) { | 40 | region.State == MEM_COMMIT && !(region.Protect & (PAGE_GUARD | PAGE_NOACCESS))) { |
| 40 | // From RSP (rounded down to keep the 16-byte alignment the copy buffer also has) to the top of the committed | 41 | // From RSP (rounded down to keep the 16-byte alignment the copy buffer also has) to the top of the committed |
| 41 | // run of stack pages, at most kMaxStackBytes. | 42 | // run of stack pages, at most kMaxStackBytes. |
| 43 | const DWORD64 base = reinterpret_cast<DWORD64>(region.BaseAddress); | ||
| 42 | stack.original = context.Rsp & ~DWORD64(15); | 44 | stack.original = context.Rsp & ~DWORD64(15); |
| 43 | const DWORD64 top = reinterpret_cast<DWORD64>(region.BaseAddress) + region.RegionSize; | 45 | if (region.RegionSize > 0 && region.RegionSize <= std::numeric_limits<DWORD64>::max() - base) { |
| 44 | stack.size = static_cast<size_t>(std::min<DWORD64>(StackSampler::kMaxStackBytes, top - stack.original)); | 46 | const DWORD64 top = base + region.RegionSize; |
| 45 | SIZE_T read = 0; | 47 | if (stack.original >= base && top > stack.original) { |
| 46 | copied = ReadProcessMemory(GetCurrentProcess(), reinterpret_cast<void*>(stack.original), buffer, stack.size, | 48 | stack.size = static_cast<size_t>(std::min<DWORD64>(StackSampler::kMaxStackBytes, top - stack.original)); |
| 47 | &read) && read == stack.size; | 49 | SIZE_T read = 0; |
| 50 | copied = ReadProcessMemory(GetCurrentProcess(), reinterpret_cast<void*>(stack.original), buffer, | ||
| 51 | stack.size, &read) && | ||
| 52 | read == stack.size; | ||
| 53 | } | ||
| 54 | } | ||
| 48 | } | 55 | } |
| 49 | ResumeThread(thread); | 56 | ResumeThread(thread); |
| 50 | stack.copy = reinterpret_cast<DWORD64>(buffer); | 57 | stack.copy = reinterpret_cast<DWORD64>(buffer); |
| @@ -79,7 +86,8 @@ size_t walk_copy(CONTEXT& context, const CopiedStack& stack, std::uint64_t* fram | |||
| 79 | if (address >= stack.original && address - stack.original < stack.size) address += stack.copy - stack.original; | 86 | if (address >= stack.original && address - stack.original < stack.size) address += stack.copy - stack.original; |
| 80 | }; | 87 | }; |
| 81 | const auto in_copy = [&](DWORD64 address) { | 88 | const auto in_copy = [&](DWORD64 address) { |
| 82 | return address >= stack.copy && address - stack.copy + sizeof(DWORD64) <= stack.size; | 89 | return stack.size >= sizeof(DWORD64) && address >= stack.copy && |
| 90 | address - stack.copy <= stack.size - sizeof(DWORD64); | ||
| 83 | }; | 91 | }; |
| 84 | size_t count = 0; | 92 | size_t count = 0; |
| 85 | rebase(context.Rsp); | 93 | rebase(context.Rsp); |
| @@ -102,7 +110,10 @@ std::string thread_label(HANDLE thread, GetDescription describe, QueryThread que | |||
| 102 | if (describe) { | 110 | if (describe) { |
| 103 | PWSTR description = nullptr; | 111 | PWSTR description = nullptr; |
| 104 | if (SUCCEEDED(describe(thread, &description)) && description) { | 112 | if (SUCCEEDED(describe(thread, &description)) && description) { |
| 105 | std::string name = startup_trace::detail::utf8_from_wide(description, static_cast<int>(wcslen(description))); | 113 | const size_t wide_len = wcslen(description); |
| 114 | const int clamped = static_cast<int>( | ||
| 115 | std::min<size_t>(wide_len, static_cast<size_t>(std::numeric_limits<int>::max()))); | ||
| 116 | std::string name = startup_trace::detail::utf8_from_wide(description, clamped); | ||
| 106 | LocalFree(description); | 117 | LocalFree(description); |
| 107 | if (!name.empty()) return name; | 118 | if (!name.empty()) return name; |
| 108 | } | 119 | } |
| @@ -179,6 +190,7 @@ void snapshot_threads(const std::string& tag, StackSampler& sampler, GetDescript | |||
| 179 | StackSampler::StackSampler() : stack_(std::make_unique_for_overwrite<std::byte[]>(kMaxStackBytes)) {} | 190 | StackSampler::StackSampler() : stack_(std::make_unique_for_overwrite<std::byte[]>(kMaxStackBytes)) {} |
| 180 | 191 | ||
| 181 | std::size_t StackSampler::sample(void* thread, std::uint64_t* frames) { | 192 | std::size_t StackSampler::sample(void* thread, std::uint64_t* frames) { |
| 193 | if (!thread || !frames) return 0; | ||
| 182 | CONTEXT context{}; | 194 | CONTEXT context{}; |
| 183 | CopiedStack stack; | 195 | CopiedStack stack; |
| 184 | if (!copy_context_and_stack(static_cast<HANDLE>(thread), context, stack_.get(), stack)) { | 196 | if (!copy_context_and_stack(static_cast<HANDLE>(thread), context, stack_.get(), stack)) { |
| @@ -234,10 +246,16 @@ std::jthread start_thread_stacks() { | |||
| 234 | return {}; | 246 | return {}; |
| 235 | } | 247 | } |
| 236 | return std::jthread([start, times = std::move(times)](std::stop_token stop) { | 248 | return std::jthread([start, times = std::move(times)](std::stop_token stop) { |
| 237 | const auto describe = reinterpret_cast<GetDescription>(reinterpret_cast<void*>( | 249 | const HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll"); |
| 238 | GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "GetThreadDescription"))); | 250 | const HMODULE ntdll = GetModuleHandleW(L"ntdll.dll"); |
| 239 | const auto query = reinterpret_cast<QueryThread>(reinterpret_cast<void*>( | 251 | const auto describe = |
| 240 | GetProcAddress(GetModuleHandleW(L"ntdll.dll"), "NtQueryInformationThread"))); | 252 | kernel32 ? reinterpret_cast<GetDescription>( |
| 253 | reinterpret_cast<void*>(GetProcAddress(kernel32, "GetThreadDescription"))) | ||
| 254 | : nullptr; | ||
| 255 | const auto query = | ||
| 256 | ntdll ? reinterpret_cast<QueryThread>( | ||
| 257 | reinterpret_cast<void*>(GetProcAddress(ntdll, "NtQueryInformationThread"))) | ||
| 258 | : nullptr; | ||
| 241 | StackSampler sampler; | 259 | StackSampler sampler; |
| 242 | std::condition_variable_any wake; | 260 | std::condition_variable_any wake; |
| 243 | std::mutex mutex; | 261 | std::mutex mutex; |