| 6 | #define BASE_PROFILER_STACK_COPIER_H_ |
| 7 |
| 8 | #include <stdint.h> |
| 9 |
| 10 | #include "base/base_export.h" |
| 11 | #include "base/profiler/register_context.h" |
| 12 | #include "base/time/time.h" |
| 13 |
| 14 | namespace base { |
| 15 |
| 16 | class StackBuffer; |
| 17 |
| 18 | // StackCopier causes a thread to be suspended, copies its stack, and resumes |
| 19 | // the thread's execution. It's intended to provide an abstraction over stack |
| 20 | // copying techniques where the thread suspension is performed directly by the |
| 21 | // profiler thread (Windows and Mac platforms) vs. where the thread suspension |
| 22 | // is performed by the OS through signals (Android). |
| 23 | class BASE_EXPORT StackCopier { |
| 24 | public: |
| 25 | // Interface that may be implemented by the caller of CopyStack() to receive a |
| 26 | // callback when the stack is copied, while the target thread is suspended. |
| 27 | class BASE_EXPORT Delegate { |
| 28 | public: |
| 29 | virtual ~Delegate() {} |
| 30 |
| 31 | // Invoked at the time the stack is copied. |
| 32 | // IMPORTANT NOTE: to avoid deadlock implementations of this interface must |
| 33 | // not invoke any non-reentrant code that is also invoked by the target |
| 34 | // thread. In particular, it may not perform any heap allocation or |
| 35 | // deallocation, including indirectly via use of DCHECK/CHECK or other |
| 36 | // logging statements. |
| 37 | virtual void OnStackCopy() = 0; |
| 38 |
| 39 | // Invoked after the stack has been copied and the target thread resumed. |
| 40 | virtual void OnThreadResume() = 0; |
| 41 | }; |
| 42 |
| 43 | virtual ~StackCopier(); |
| 44 |
| 45 | // Copies the thread's register context into |thread_context|, the stack into |
| 46 | // |stack_buffer|, and the top of stack address into |stack_top|. Records |
| 47 | // |timestamp| at the time the stack was copied. delegate->OnStackCopy() will |
| 48 | // be invoked while the thread is suspended. Returns true if successful. |
| 49 | virtual bool CopyStack(StackBuffer* stack_buffer, |
| 50 | uintptr_t* stack_top, |
| 51 | TimeTicks* timestamp, |
| 52 | RegisterContext* thread_context, |
| 53 | Delegate* delegate) = 0; |
| 54 |
| 55 | protected: |
| 56 | // If the value at |pointer| points to the original stack, rewrite it to point |
| 57 | // to the corresponding location in the copied stack. |
| 58 | // |
| 59 | // NO HEAP ALLOCATIONS. |
| 60 | static uintptr_t RewritePointerIfInOriginalStack( |
| 61 | const uint8_t* original_stack_bottom, |
| 62 | const uintptr_t* original_stack_top, |
| 63 | const uint8_t* stack_copy_bottom, |
| 64 | uintptr_t pointer); |
| 65 |
| 66 | // Copies the stack to a buffer while rewriting possible pointers to locations |
| 67 | // within the stack to point to the corresponding locations in the copy. This |
| 68 | // is necessary to handle stack frames with dynamic stack allocation, where a |
| 69 | // pointer to the beginning of the dynamic allocation area is stored on the |
| 70 | // stack and/or in a non-volatile register. |
| 71 | // |
| 72 | // Eager rewriting of anything that looks like a pointer to the stack, as done |
| 73 | // in this function, does not adversely affect the stack unwinding. The only |
| 74 | // other values on the stack the unwinding depends on are return addresses, |
| 75 | // which should not point within the stack memory. The rewriting is guaranteed |
| 76 | // to catch all pointers because the stacks are guaranteed by the ABI to be |
| 77 | // sizeof(uintptr_t*) aligned. |
| 78 | // |
| 79 | // |original_stack_bottom| and |original_stack_top| are different pointer |
| 80 | // types due on their differing guaranteed alignments -- the bottom may only |
| 81 | // be 1-byte aligned while the top is aligned to double the pointer width. |
| 82 | // |
| 83 | // Returns a pointer to the bottom address in the copied stack. This value |
| 84 | // matches the alignment of |original_stack_bottom| to ensure that the stack |
| 85 | // contents have the same alignment as in the original stack. As a result the |
| 86 | // value will be different than |stack_buffer_bottom| if |
| 87 | // |original_stack_bottom| is not aligned to double the pointer width. |
| 88 | // |
| 89 | // NO HEAP ALLOCATIONS. |
| 90 | static const uint8_t* CopyStackContentsAndRewritePointers( |
| 91 | const uint8_t* original_stack_bottom, |
| 92 | const uintptr_t* original_stack_top, |
| 93 | int platform_stack_alignment, |
| 94 | uintptr_t* stack_buffer_bottom); |
| 95 | }; |
| 96 |
| 97 | } // namespace base |
| 98 |
| 99 | #endif // BASE_PROFILER_STACK_COPIER_H_ |
| 100 |