(
context: CpuContext,
maxEntries = DEFAULT_MAX_ENTRIES,
previous?: StackSnapshot | null,
)
| 10 | export type StackSnapshot = Record<string, string>; |
| 11 | |
| 12 | export function logStack( |
| 13 | context: CpuContext, |
| 14 | maxEntries = DEFAULT_MAX_ENTRIES, |
| 15 | previous?: StackSnapshot | null, |
| 16 | ): StackSnapshot { |
| 17 | const sp = getStackPointer(context); |
| 18 | const fp = getFramePointer(context); |
| 19 | if (sp === null) { |
| 20 | log("[stk] no stack pointer in current context", "yellow"); |
| 21 | return {}; |
| 22 | } |
| 23 | |
| 24 | const snapshot: StackSnapshot = {}; |
| 25 | const markers = getContextMarkers(context, sp, fp); |
| 26 | const totalEntries = getEntryCount(sp, fp, maxEntries); |
| 27 | const headEntries = Math.min(DEFAULT_HEAD_ENTRIES, maxEntries); |
| 28 | const tailEntries = Math.min(DEFAULT_TAIL_ENTRIES, Math.max(maxEntries - headEntries, 0)); |
| 29 | const omitted = totalEntries > maxEntries; |
| 30 | |
| 31 | log("[stk] ------------------------------------------------------------", "cyan"); |
| 32 | log(`[stk] fp=${fp ?? "n/a"} -> sp=${sp} entries=${totalEntries}${omitted ? " (truncated)" : ""}`, "green"); |
| 33 | |
| 34 | let didOmit = false; |
| 35 | for (const index of getDisplayIndices(totalEntries, omitted, headEntries, tailEntries)) { |
| 36 | if (index === -1) { |
| 37 | if (!didOmit) { |
| 38 | const omittedCount = totalEntries - headEntries - tailEntries; |
| 39 | log(`[stk] ... ${omittedCount} entries omitted ...`, "yellow"); |
| 40 | didOmit = true; |
| 41 | } |
| 42 | continue; |
| 43 | } |
| 44 | |
| 45 | const slot = sp.add(index * Process.pointerSize); |
| 46 | logStackSlot(index, slot, snapshot, previous, markers); |
| 47 | } |
| 48 | |
| 49 | return snapshot; |
| 50 | } |
| 51 | |
| 52 | function getDisplayIndices(totalEntries: number, omitted: boolean, headEntries: number, tailEntries: number): number[] { |
| 53 | if (!omitted) { |
no test coverage detected