native_unwinder_android.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_NATIVE_UNWINDER_ANDROID_H_
  5. #define BASE_PROFILER_NATIVE_UNWINDER_ANDROID_H_
  6. #include "base/profiler/unwinder.h"
  7. #include "third_party/libunwindstack/src/libunwindstack/include/unwindstack/Maps.h"
  8. #include "third_party/libunwindstack/src/libunwindstack/include/unwindstack/Memory.h"
  9. namespace base {
  10. // Implementation of unwindstack::Memory that restricts memory access to a stack
  11. // buffer, used by NativeUnwinderAndroid. While unwinding, only memory accesses
  12. // within the stack should be performed to restore registers.
  13. class UnwindStackMemoryAndroid : public unwindstack::Memory {
  14. public:
  15. UnwindStackMemoryAndroid(uintptr_t stack_ptr, uintptr_t stack_top);
  16. ~UnwindStackMemoryAndroid() override;
  17. size_t Read(uint64_t addr, void* dst, size_t size) override;
  18. private:
  19. const uintptr_t stack_ptr_;
  20. const uintptr_t stack_top_;
  21. };
  22. // Native unwinder implementation for Android, using libunwindstack.
  23. class NativeUnwinderAndroid : public Unwinder {
  24. public:
  25. // Creates maps object from /proc/self/maps for use by NativeUnwinderAndroid.
  26. // Since this is an expensive call, the maps object should be re-used across
  27. // all profiles in a process.
  28. static std::unique_ptr<unwindstack::Maps> CreateMaps();
  29. static std::unique_ptr<unwindstack::Memory> CreateProcessMemory();
  30. // |exclude_module_with_base_address| is used to exclude a specific module
  31. // and let another unwinder take control. TryUnwind() will exit with
  32. // UNRECOGNIZED_FRAME and CanUnwindFrom() will return false when a frame is
  33. // encountered in that module.
  34. NativeUnwinderAndroid(unwindstack::Maps* memory_regions_map,
  35. unwindstack::Memory* process_memory,
  36. uintptr_t exclude_module_with_base_address);
  37. ~NativeUnwinderAndroid() override;
  38. NativeUnwinderAndroid(const NativeUnwinderAndroid&) = delete;
  39. NativeUnwinderAndroid& operator=(const NativeUnwinderAndroid&) = delete;
  40. // Unwinder
  41. void AddInitialModules(ModuleCache* module_cache) override;
  42. bool CanUnwindFrom(const Frame& current_frame) const override;
  43. UnwindResult TryUnwind(RegisterContext* thread_context,
  44. uintptr_t stack_top,
  45. ModuleCache* module_cache,
  46. std::vector<Frame>* stack) const override;
  47. // Adds modules found from executable loaded memory regions to |module_cache|.
  48. // Public for test access.
  49. static void AddInitialModulesFromMaps(
  50. const unwindstack::Maps& memory_regions_map,
  51. ModuleCache* module_cache);
  52. private:
  53. void EmitDexFrame(uintptr_t dex_pc,
  54. ModuleCache* module_cache,
  55. std::vector<Frame>* stack) const;
  56. unwindstack::Maps* const memory_regions_map_;
  57. unwindstack::Memory* const process_memory_;
  58. const uintptr_t exclude_module_with_base_address_;
  59. };
  60. } // namespace base
  61. #endif // BASE_PROFILER_NATIVE_UNWINDER_ANDROID_H_