|
7 | 7 | #ifndef DD_PROFILER_SAFE_READ_TESTING_H_ |
8 | 8 | #define DD_PROFILER_SAFE_READ_TESTING_H_ |
9 | 9 |
|
10 | | -#include <stdbool.h> |
11 | 10 | #include <stddef.h> |
| 11 | +#include <stdint.h> |
12 | 12 |
|
13 | 13 | #ifdef __APPLE__ |
14 | 14 | #include <TargetConditionals.h> |
15 | 15 | #if !TARGET_OS_WATCH |
16 | 16 |
|
| 17 | +#include "dd_profiler.h" // stack_trace_t |
| 18 | + |
17 | 19 | #ifdef __cplusplus |
18 | 20 | extern "C" { |
19 | 21 | #endif |
20 | 22 |
|
21 | 23 | /** |
22 | | - * Helper to manually re-install handlers if needed by tests. |
| 24 | + * Reads a contiguous region of the calling task's virtual memory starting at sp |
| 25 | + * into buf (which must be at least buf_size bytes). |
| 26 | + * |
| 27 | + * Mirrors the production read_stack_region used inside stack_trace_sample_thread. |
| 28 | + * Returns the number of contiguous bytes actually read from sp; returns 0 if sp |
| 29 | + * itself is unmapped or invalid. |
23 | 30 | */ |
24 | | -void init_safe_read_handlers_for_testing(void); |
| 31 | +size_t read_stack_region_for_testing(void* sp, void* buf, size_t buf_size); |
25 | 32 |
|
26 | 33 | /** |
27 | | - * Validates the Safe Read mechanism. |
| 34 | + * Walks a frame pointer chain entirely from a pre-populated local buffer. |
| 35 | + * |
| 36 | + * This is the same walk used inside stack_trace_sample_thread, extracted so tests |
| 37 | + * can exercise the bounds-check guards and cycle-termination behavior directly |
| 38 | + * without needing to suspend a real thread or craft live stack memory. |
| 39 | + * |
| 40 | + * @param trace Pre-allocated trace; trace->frames must point to at least |
| 41 | + * max_depth entries. frame_count is reset to 0 on entry. |
| 42 | + * @param initial_fp First frame pointer to walk from (typically the suspended |
| 43 | + * thread's FP register). |
| 44 | + * @param initial_pc First program counter to record (typically the suspended |
| 45 | + * thread's PC register). |
| 46 | + * @param stack_base Base address that stack_buf was read from (typically SP). |
| 47 | + * @param stack_buf Buffer containing a snapshot of stack memory. |
| 48 | + * @param bytes_read Valid bytes in stack_buf. |
| 49 | + * @param max_depth Hard cap on frames to record. |
28 | 50 | */ |
29 | | -bool safe_read_memory_for_testing(void* addr, void* buffer, size_t size); |
| 51 | +void walk_frames_in_buffer( |
| 52 | + stack_trace_t* trace, |
| 53 | + void* initial_fp, |
| 54 | + void* initial_pc, |
| 55 | + uintptr_t stack_base, |
| 56 | + const uint8_t* stack_buf, |
| 57 | + size_t bytes_read, |
| 58 | + uint32_t max_depth |
| 59 | +); |
30 | 60 |
|
31 | 61 | #ifdef __cplusplus |
32 | 62 | } |
|
0 commit comments