UnwindLLDB.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===-- UnwindLLDB.h --------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef LLDB_TARGET_UNWINDLLDB_H
  9. #define LLDB_TARGET_UNWINDLLDB_H
  10. #include <vector>
  11. #include "lldb/Symbol/FuncUnwinders.h"
  12. #include "lldb/Symbol/SymbolContext.h"
  13. #include "lldb/Symbol/UnwindPlan.h"
  14. #include "lldb/Target/RegisterContext.h"
  15. #include "lldb/Target/Unwind.h"
  16. #include "lldb/Utility/ConstString.h"
  17. #include "lldb/lldb-public.h"
  18. namespace lldb_private {
  19. class RegisterContextUnwind;
  20. class UnwindLLDB : public lldb_private::Unwind {
  21. public:
  22. UnwindLLDB(lldb_private::Thread &thread);
  23. ~UnwindLLDB() override = default;
  24. enum RegisterSearchResult {
  25. eRegisterFound = 0,
  26. eRegisterNotFound,
  27. eRegisterIsVolatile
  28. };
  29. protected:
  30. friend class lldb_private::RegisterContextUnwind;
  31. struct RegisterLocation {
  32. enum RegisterLocationTypes {
  33. eRegisterNotSaved = 0, // register was not preserved by callee. If
  34. // volatile reg, is unavailable
  35. eRegisterSavedAtMemoryLocation, // register is saved at a specific word of
  36. // target mem (target_memory_location)
  37. eRegisterInRegister, // register is available in a (possible other)
  38. // register (register_number)
  39. eRegisterSavedAtHostMemoryLocation, // register is saved at a word in
  40. // lldb's address space
  41. eRegisterValueInferred, // register val was computed (and is in
  42. // inferred_value)
  43. eRegisterInLiveRegisterContext // register value is in a live (stack frame
  44. // #0) register
  45. };
  46. int type;
  47. union {
  48. lldb::addr_t target_memory_location;
  49. uint32_t
  50. register_number; // in eRegisterKindLLDB register numbering system
  51. void *host_memory_location;
  52. uint64_t inferred_value; // eRegisterValueInferred - e.g. stack pointer ==
  53. // cfa + offset
  54. } location;
  55. };
  56. void DoClear() override {
  57. m_frames.clear();
  58. m_candidate_frame.reset();
  59. m_unwind_complete = false;
  60. }
  61. uint32_t DoGetFrameCount() override;
  62. bool DoGetFrameInfoAtIndex(uint32_t frame_idx, lldb::addr_t &cfa,
  63. lldb::addr_t &start_pc,
  64. bool &behaves_like_zeroth_frame) override;
  65. lldb::RegisterContextSP
  66. DoCreateRegisterContextForFrame(lldb_private::StackFrame *frame) override;
  67. typedef std::shared_ptr<RegisterContextUnwind> RegisterContextLLDBSP;
  68. // Needed to retrieve the "next" frame (e.g. frame 2 needs to retrieve frame
  69. // 1's RegisterContextUnwind)
  70. // The RegisterContext for frame_num must already exist or this returns an
  71. // empty shared pointer.
  72. RegisterContextLLDBSP GetRegisterContextForFrameNum(uint32_t frame_num);
  73. // Iterate over the RegisterContextUnwind's in our m_frames vector, look for
  74. // the first one that has a saved location for this reg.
  75. bool SearchForSavedLocationForRegister(
  76. uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc,
  77. uint32_t starting_frame_num, bool pc_register);
  78. /// Provide the list of user-specified trap handler functions
  79. ///
  80. /// The Platform is one source of trap handler function names; that
  81. /// may be augmented via a setting. The setting needs to be converted
  82. /// into an array of ConstStrings before it can be used - we only want
  83. /// to do that once per thread so it's here in the UnwindLLDB object.
  84. ///
  85. /// \return
  86. /// Vector of ConstStrings of trap handler function names. May be
  87. /// empty.
  88. const std::vector<ConstString> &GetUserSpecifiedTrapHandlerFunctionNames() {
  89. return m_user_supplied_trap_handler_functions;
  90. }
  91. private:
  92. struct Cursor {
  93. lldb::addr_t start_pc; // The start address of the function/symbol for this
  94. // frame - current pc if unknown
  95. lldb::addr_t cfa; // The canonical frame address for this stack frame
  96. lldb_private::SymbolContext sctx; // A symbol context we'll contribute to &
  97. // provide to the StackFrame creation
  98. RegisterContextLLDBSP
  99. reg_ctx_lldb_sp; // These are all RegisterContextUnwind's
  100. Cursor()
  101. : start_pc(LLDB_INVALID_ADDRESS), cfa(LLDB_INVALID_ADDRESS), sctx(),
  102. reg_ctx_lldb_sp() {}
  103. private:
  104. Cursor(const Cursor &) = delete;
  105. const Cursor &operator=(const Cursor &) = delete;
  106. };
  107. typedef std::shared_ptr<Cursor> CursorSP;
  108. std::vector<CursorSP> m_frames;
  109. CursorSP m_candidate_frame;
  110. bool m_unwind_complete; // If this is true, we've enumerated all the frames in
  111. // the stack, and m_frames.size() is the
  112. // number of frames, etc. Otherwise we've only gone as far as directly asked,
  113. // and m_frames.size()
  114. // is how far we've currently gone.
  115. std::vector<ConstString> m_user_supplied_trap_handler_functions;
  116. // Check if Full UnwindPlan of First frame is valid or not.
  117. // If not then try Fallback UnwindPlan of the frame. If Fallback
  118. // UnwindPlan succeeds then update the Full UnwindPlan with the
  119. // Fallback UnwindPlan.
  120. void UpdateUnwindPlanForFirstFrameIfInvalid(ABI *abi);
  121. CursorSP GetOneMoreFrame(ABI *abi);
  122. bool AddOneMoreFrame(ABI *abi);
  123. bool AddFirstFrame();
  124. // For UnwindLLDB only
  125. UnwindLLDB(const UnwindLLDB &) = delete;
  126. const UnwindLLDB &operator=(const UnwindLLDB &) = delete;
  127. };
  128. } // namespace lldb_private
  129. #endif // LLDB_TARGET_UNWINDLLDB_H