| 372 | // --------------------------------------------------------------------------- |
| 373 | |
| 374 | static __declspec(noinline) unsigned walk_from_context(void **stack, unsigned maxFrames, |
| 375 | CONTEXT &ctx, int skipFrames, |
| 376 | bool useLandmark) noexcept { |
| 377 | const CachedModule *lastMod = nullptr; |
| 378 | ULONG64 imageBase = 0; |
| 379 | int pdataHint = -1; |
| 380 | int frame = -skipFrames; |
| 381 | |
| 382 | DWORD64 landmarkRsp = 0; |
| 383 | bool canMatch = false; |
| 384 | if (useLandmark && g_landmark.active) { |
| 385 | landmarkRsp = g_landmark.rsp; |
| 386 | canMatch = (landmarkRsp != 0); |
| 387 | } |
| 388 | |
| 389 | __try { |
| 390 | while (ctx.Rip && frame < (int)maxFrames) { |
| 391 | // Try to tail-memoize from landmark before recording this frame. |
| 392 | if (canMatch && frame >= 0 && ctx.Rsp == landmarkRsp) { |
| 393 | int copy = g_landmark.frameCount; |
| 394 | int room = (int)maxFrames - frame; |
| 395 | if (copy > room) copy = room; |
| 396 | memcpy(&stack[frame], g_landmark.frames, copy * sizeof(void*)); |
| 397 | return (unsigned)(frame + copy); |
| 398 | } |
| 399 | if (frame >= 0) stack[frame] = (void*)(uintptr_t)ctx.Rip; |
| 400 | |
| 401 | PRUNTIME_FUNCTION pFunc = cachedLookupOnly(ctx.Rip, lastMod, imageBase, pdataHint); |
| 402 | if (!pFunc) |
| 403 | pFunc = tryAddAndLookup(ctx.Rip, lastMod, imageBase, pdataHint); |
| 404 | if (!pFunc) break; |
| 405 | |
| 406 | if (!lightweightVirtualUnwind(pFunc, imageBase, ctx)) { |
| 407 | // PUSH_MACHFRAME or unknown opcode → fall back to OS. |
| 408 | PVOID handlerData = nullptr; |
| 409 | ULONG64 establisherFrame = 0; |
| 410 | RtlVirtualUnwind(UNW_FLAG_NHANDLER, imageBase, ctx.Rip, pFunc, |
| 411 | &ctx, &handlerData, &establisherFrame, nullptr); |
| 412 | } |
| 413 | ++frame; |
| 414 | } |
| 415 | } __except (EXCEPTION_EXECUTE_HANDLER) { |
| 416 | // Partial result is fine for diagnostics. |
| 417 | } |
| 418 | return frame > 0 ? (unsigned)frame : 0; |
| 419 | } |
| 420 | |
| 421 | // --------------------------------------------------------------------------- |
| 422 | // Public API (internal to this translation unit / alloc_tracker.cpp) |
no test coverage detected