arm_cfi_table.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2019 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_PROFILER_ARM_CFI_TABLE_H_
  5. #define BASE_PROFILER_ARM_CFI_TABLE_H_
  6. #include <memory>
  7. #include "base/containers/buffer_iterator.h"
  8. #include "base/containers/span.h"
  9. #include "base/macros.h"
  10. #include "base/optional.h"
  11. namespace base {
  12. // This class implements methods to read and parse the arm Call Frame
  13. // Information (CFI) for Chrome, which contains tables for unwinding Chrome
  14. // functions. For detailed description of the format, see
  15. // extract_unwind_tables.py.
  16. class BASE_EXPORT ArmCFITable {
  17. public:
  18. // The CFI information that correspond to an instruction. {0, 0} is a valid
  19. // entry and should be interpreted as the default rule:
  20. // .cfa: sp; .cfa = lr (link register).
  21. struct FrameEntry {
  22. // The offset of the call frame address (CFA) of previous function, relative
  23. // to the current stack pointer. Rule for unwinding CFA:
  24. // .cfa: sp + cfa_offset.
  25. uint16_t cfa_offset = 0;
  26. // The offset of location of return address (RA), relative to the previous
  27. // call frame address. Rule for unwinding RA:
  28. // .ra = *(cfa - ra_offset).
  29. uint16_t ra_offset = 0;
  30. };
  31. // Parses |cfi_data| and creates a ArmCFITable that reads from it.
  32. // |cfi_data| is required to remain valid for the lifetime of the object.
  33. static std::unique_ptr<ArmCFITable> Parse(span<const uint8_t> cfi_data);
  34. ArmCFITable(span<const uint32_t> function_addresses,
  35. span<const uint16_t> entry_data_indices,
  36. span<const uint8_t> entry_data);
  37. ~ArmCFITable();
  38. // Finds the CFI row for the given |address| in terms of offset from the
  39. // start of the current binary. Concurrent calls are thread safe.
  40. Optional<FrameEntry> FindEntryForAddress(uintptr_t address) const;
  41. size_t GetTableSizeForTesting() const { return function_addresses_.size(); }
  42. private:
  43. // The UNW_INDEX table allows readers to map functions start addresses to
  44. // that function's respective entry in the UNW_DATA table.
  45. // - A function's start address is at 0x123, and
  46. // - function_addresses_[2] == 0x123, and
  47. // - entry_data_indices_[2] = 42, then
  48. // - entry_data_[42] is the corresponding entry in the UNW_DATA table for
  49. // the function with the start address of 0x123
  50. //
  51. // Note that function_addresses is sorted to facilitate easy lookup.
  52. const span<const uint32_t> function_addresses_;
  53. const span<const uint16_t> entry_data_indices_;
  54. // A reference to the UNW_DATA table. Each entry in the UNW_DATA table
  55. // corresponds to a function, which in turn corresponds to an array of
  56. // CFIDataRows. (see arm_cfi_reader.cc).
  57. const span<const uint8_t> entry_data_;
  58. DISALLOW_COPY_AND_ASSIGN(ArmCFITable);
  59. };
  60. } // namespace base
  61. #endif // BASE_PROFILER_ARM_CFI_TABLE_H_