cfi_backtrace_android.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_TRACE_EVENT_CFI_BACKTRACE_ANDROID_H_
  5. #define BASE_TRACE_EVENT_CFI_BACKTRACE_ANDROID_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include "base/base_export.h"
  10. #include "base/debug/debugging_buildflags.h"
  11. #include "base/files/memory_mapped_file.h"
  12. #include "base/gtest_prod_util.h"
  13. #include "base/threading/thread_local_storage.h"
  14. namespace base {
  15. namespace trace_event {
  16. // This class is used to unwind stack frames in the current thread. The unwind
  17. // information (dwarf debug info) is stripped from the chrome binary and we do
  18. // not build with exception tables (ARM EHABI) in release builds. So, we use a
  19. // custom unwind table which is generated and added to specific android builds,
  20. // when add_unwind_tables_in_apk build option is specified. This unwind table
  21. // contains information for unwinding stack frames when the functions calls are
  22. // from lib[mono]chrome.so. The file is added as an asset to the apk and the
  23. // table is used to unwind stack frames for profiling. This class implements
  24. // methods to read and parse the unwind table and unwind stack frames using this
  25. // data.
  26. class BASE_EXPORT CFIBacktraceAndroid {
  27. public:
  28. // Creates and initializes by memory mapping the unwind tables from apk assets
  29. // on first call.
  30. static CFIBacktraceAndroid* GetInitializedInstance();
  31. // Returns true if the given program counter |pc| is mapped in chrome library.
  32. static bool is_chrome_address(uintptr_t pc);
  33. // Returns the start and end address of the current library.
  34. static uintptr_t executable_start_addr();
  35. static uintptr_t executable_end_addr();
  36. // Returns true if stack unwinding is possible using CFI unwind tables in apk.
  37. // There is no need to check this before each unwind call. Will always return
  38. // the same value based on CFI tables being present in the binary.
  39. bool can_unwind_stack_frames() const { return can_unwind_stack_frames_; }
  40. // Returns the program counters by unwinding stack in the current thread in
  41. // order of latest call frame first. Unwinding works only if
  42. // can_unwind_stack_frames() returns true. This function allocates memory from
  43. // heap for cache on the first call of the calling thread, unless
  44. // AllocateCacheForCurrentThread() is called from the thread. For each stack
  45. // frame, this method searches through the unwind table mapped in memory to
  46. // find the unwind information for function and walks the stack to find all
  47. // the return address. This only works until the last function call from the
  48. // chrome.so. We do not have unwind information to unwind beyond any frame
  49. // outside of chrome.so. Calls to Unwind() are thread safe and lock free, once
  50. // Initialize() returns success.
  51. size_t Unwind(const void** out_trace, size_t max_depth);
  52. // Same as above function, but starts from a given program counter |pc|,
  53. // stack pointer |sp| and link register |lr|. This can be from current thread
  54. // or any other thread. But the caller must make sure that the thread's stack
  55. // segment is not racy to read.
  56. size_t Unwind(uintptr_t pc,
  57. uintptr_t sp,
  58. uintptr_t lr,
  59. const void** out_trace,
  60. size_t max_depth);
  61. // Allocates memory for CFI cache for the current thread so that Unwind()
  62. // calls are safe for signal handlers.
  63. void AllocateCacheForCurrentThread();
  64. // The CFI information that correspond to an instruction.
  65. struct CFIRow {
  66. bool operator==(const CFIBacktraceAndroid::CFIRow& o) const {
  67. return cfa_offset == o.cfa_offset && ra_offset == o.ra_offset;
  68. }
  69. // The offset of the call frame address of previous function from the
  70. // current stack pointer. Rule for unwinding SP: SP_prev = SP_cur +
  71. // cfa_offset.
  72. uint16_t cfa_offset = 0;
  73. // The offset of location of return address from the previous call frame
  74. // address. Rule for unwinding PC: PC_prev = * (SP_prev - ra_offset).
  75. uint16_t ra_offset = 0;
  76. };
  77. // Finds the CFI row for the given |func_addr| in terms of offset from
  78. // the start of the current binary. Concurrent calls are thread safe.
  79. bool FindCFIRowForPC(uintptr_t func_addr, CFIRow* out);
  80. private:
  81. FRIEND_TEST_ALL_PREFIXES(CFIBacktraceAndroidTest, TestCFICache);
  82. FRIEND_TEST_ALL_PREFIXES(CFIBacktraceAndroidTest, TestFindCFIRow);
  83. FRIEND_TEST_ALL_PREFIXES(CFIBacktraceAndroidTest, TestUnwinding);
  84. // A simple cache that stores entries in table using prime modulo hashing.
  85. // This cache with 500 entries already gives us 95% hit rate, and fits in a
  86. // single system page (usually 4KiB). Using a thread local cache for each
  87. // thread gives us 30% improvements on performance of heap profiling.
  88. class CFICache {
  89. public:
  90. // Add new item to the cache. It replaces an existing item with same hash.
  91. // Constant time operation.
  92. void Add(uintptr_t address, CFIRow cfi);
  93. // Finds the given address and fills |cfi| with the info for the address.
  94. // returns true if found, otherwise false. Assumes |address| is never 0.
  95. bool Find(uintptr_t address, CFIRow* cfi);
  96. private:
  97. FRIEND_TEST_ALL_PREFIXES(CFIBacktraceAndroidTest, TestCFICache);
  98. // Size is the highest prime which fits the cache in a single system page,
  99. // usually 4KiB. A prime is chosen to make sure addresses are hashed evenly.
  100. static const int kLimit = 509;
  101. struct AddrAndCFI {
  102. uintptr_t address;
  103. CFIRow cfi;
  104. };
  105. AddrAndCFI cache_[kLimit] = {};
  106. };
  107. static_assert(sizeof(CFIBacktraceAndroid::CFICache) < 4096,
  108. "The cache does not fit in a single page.");
  109. CFIBacktraceAndroid();
  110. ~CFIBacktraceAndroid();
  111. // Initializes unwind tables using the CFI asset file in the apk if present.
  112. // Also stores the limits of mapped region of the lib[mono]chrome.so binary,
  113. // since the unwind is only feasible for addresses within the .so file. Once
  114. // initialized, the memory map of the unwind table is never cleared since we
  115. // cannot guarantee that all the threads are done using the memory map when
  116. // heap profiling is turned off. But since we keep the memory map is clean,
  117. // the system can choose to evict the unused pages when needed. This would
  118. // still reduce the total amount of address space available in process.
  119. void Initialize();
  120. // Finds the UNW_INDEX and UNW_DATA tables in from the CFI file memory map.
  121. void ParseCFITables();
  122. CFICache* GetThreadLocalCFICache();
  123. // The start address of the memory mapped unwind table asset file. Unique ptr
  124. // because it is replaced in tests.
  125. std::unique_ptr<MemoryMappedFile> cfi_mmap_;
  126. // The UNW_INDEX table: Start address of the function address column. The
  127. // memory segment corresponding to this column is treated as an array of
  128. // uintptr_t.
  129. const uintptr_t* unw_index_function_col_ = nullptr;
  130. // The UNW_INDEX table: Start address of the index column. The memory segment
  131. // corresponding to this column is treated as an array of uint16_t.
  132. const uint16_t* unw_index_indices_col_ = nullptr;
  133. // The number of rows in UNW_INDEX table.
  134. size_t unw_index_row_count_ = 0;
  135. // The start address of UNW_DATA table.
  136. const uint16_t* unw_data_start_addr_ = nullptr;
  137. bool can_unwind_stack_frames_ = false;
  138. ThreadLocalStorage::Slot thread_local_cfi_cache_;
  139. };
  140. } // namespace trace_event
  141. } // namespace base
  142. #endif // BASE_TRACE_EVENT_CFI_BACKTRACE_ANDROID_H_