native_unwinder_android.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Adds modules found from executable loaded memory regions to |module_cache|.
  31. static void AddInitialModulesFromMaps(
  32. const unwindstack::Maps& memory_regions_map,
  33. ModuleCache* module_cache);
  34. // |exclude_module_with_base_address| is used to exclude a specific module
  35. // and let another unwinder take control. TryUnwind() will exit with
  36. // UNRECOGNIZED_FRAME and CanUnwindFrom() will return false when a frame is
  37. // encountered in that module.
  38. NativeUnwinderAndroid(unwindstack::Maps* memory_regions_map,
  39. unwindstack::Memory* process_memory,
  40. uintptr_t exclude_module_with_base_address);
  41. ~NativeUnwinderAndroid() override;
  42. NativeUnwinderAndroid(const NativeUnwinderAndroid&) = delete;
  43. NativeUnwinderAndroid& operator=(const NativeUnwinderAndroid&) = delete;
  44. // Unwinder
  45. void AddInitialModules(ModuleCache* module_cache) override;
  46. bool CanUnwindFrom(const Frame& current_frame) const override;
  47. UnwindResult TryUnwind(RegisterContext* thread_context,
  48. uintptr_t stack_top,
  49. ModuleCache* module_cache,
  50. std::vector<Frame>* stack) const override;
  51. private:
  52. void EmitDexFrame(uintptr_t dex_pc,
  53. ModuleCache* module_cache,
  54. std::vector<Frame>* stack) const;
  55. unwindstack::Maps* const memory_regions_map_;
  56. unwindstack::Memory* const process_memory_;
  57. const uintptr_t exclude_module_with_base_address_;
  58. };
  59. } // namespace base
  60. #endif // BASE_PROFILER_NATIVE_UNWINDER_ANDROID_H_