MemoryLogger.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #ifdef ENABLE_TRACE
  7. namespace Js
  8. {
  9. // Logs in memory/inproc into StringBuilder.
  10. // Thread safe.
  11. // Uses Arena but can be templatized for other allocators.
  12. // TODO: Consider unchaining and using circular buffer when reaching limit.
  13. class MemoryLogger : public ILogger
  14. {
  15. typedef ArenaAllocator TAllocator;
  16. private:
  17. ULONG m_current;
  18. ULONG m_capacity; // The number of elements in circular buffer.
  19. char16** m_log; // Points to a circular buffer of char16*.
  20. TAllocator* m_alloc;
  21. CriticalSection m_criticalSection;
  22. public:
  23. static MemoryLogger* Create(TAllocator* alloc, ULONG elementCount);
  24. MemoryLogger(TAllocator* alloc, ULONG elementCount);
  25. ~MemoryLogger();
  26. void Write(const char16* msg) override;
  27. };
  28. #ifdef STACK_BACK_TRACE
  29. // Used by output.cpp to print stack trace.
  30. // Separate class to aboid build errors with jscript9diag.dll which doesn't link with memory.lib.
  31. class StackTraceHelper : public IStackTraceHelper
  32. {
  33. typedef ArenaAllocator TAllocator;
  34. private:
  35. TAllocator* m_alloc;
  36. THREAD_LOCAL static StackBackTrace* s_stackBackTrace;
  37. StackBackTrace* GetStackBackTrace(ULONG frameCount);
  38. public:
  39. static StackTraceHelper* Create(TAllocator* alloc);
  40. virtual size_t PrintStackTrace(ULONG framesToSkip, ULONG framesToCapture) override;
  41. virtual ULONG GetStackTrace(ULONG framesToSkip, ULONG framesToCapture, void** stackFrames) override;
  42. private:
  43. StackTraceHelper(TAllocator* alloc) : m_alloc(alloc) {}
  44. void Print();
  45. };
  46. #endif // STACK_BACK_TRACE
  47. } // namespace Js.
  48. #endif // ENABLE_TRACE