1314564Sdim//===-- RegisterContextLLDB.h --------------------------------------------*- C++
2314564Sdim//-*-===//
3254721Semaste//
4353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5353358Sdim// See https://llvm.org/LICENSE.txt for license information.
6353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#ifndef lldb_RegisterContextLLDB_h_
11254721Semaste#define lldb_RegisterContextLLDB_h_
12254721Semaste
13254721Semaste#include <vector>
14254721Semaste
15314564Sdim#include "UnwindLLDB.h"
16314564Sdim#include "lldb/Symbol/SymbolContext.h"
17314564Sdim#include "lldb/Symbol/UnwindPlan.h"
18254721Semaste#include "lldb/Target/RegisterContext.h"
19321369Sdim#include "lldb/Target/RegisterNumber.h"
20314564Sdim#include "lldb/lldb-private.h"
21254721Semaste
22254721Semastenamespace lldb_private {
23254721Semaste
24254721Semasteclass UnwindLLDB;
25254721Semaste
26314564Sdimclass RegisterContextLLDB : public lldb_private::RegisterContext {
27254721Semastepublic:
28314564Sdim  typedef std::shared_ptr<RegisterContextLLDB> SharedPtr;
29254721Semaste
30314564Sdim  RegisterContextLLDB(lldb_private::Thread &thread, const SharedPtr &next_frame,
31314564Sdim                      lldb_private::SymbolContext &sym_ctx,
32314564Sdim                      uint32_t frame_number,
33314564Sdim                      lldb_private::UnwindLLDB &unwind_lldb);
34254721Semaste
35314564Sdim  ~RegisterContextLLDB() override = default;
36254721Semaste
37314564Sdim  void InvalidateAllRegisters() override;
38254721Semaste
39314564Sdim  size_t GetRegisterCount() override;
40254721Semaste
41314564Sdim  const lldb_private::RegisterInfo *GetRegisterInfoAtIndex(size_t reg) override;
42254721Semaste
43314564Sdim  size_t GetRegisterSetCount() override;
44254721Semaste
45314564Sdim  const lldb_private::RegisterSet *GetRegisterSet(size_t reg_set) override;
46254721Semaste
47314564Sdim  bool ReadRegister(const lldb_private::RegisterInfo *reg_info,
48314564Sdim                    lldb_private::RegisterValue &value) override;
49254721Semaste
50314564Sdim  bool WriteRegister(const lldb_private::RegisterInfo *reg_info,
51314564Sdim                     const lldb_private::RegisterValue &value) override;
52254721Semaste
53314564Sdim  bool ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
54254721Semaste
55314564Sdim  bool WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
56254721Semaste
57314564Sdim  uint32_t ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind,
58314564Sdim                                               uint32_t num) override;
59254721Semaste
60314564Sdim  bool IsValid() const;
61254721Semaste
62314564Sdim  bool IsTrapHandlerFrame() const;
63262528Semaste
64314564Sdim  bool GetCFA(lldb::addr_t &cfa);
65254721Semaste
66314564Sdim  bool GetStartPC(lldb::addr_t &start_pc);
67254721Semaste
68314564Sdim  bool ReadPC(lldb::addr_t &start_pc);
69254721Semaste
70254721Semasteprivate:
71314564Sdim  enum FrameType {
72314564Sdim    eNormalFrame,
73314564Sdim    eTrapHandlerFrame,
74314564Sdim    eDebuggerFrame, // a debugger inferior function call frame; we get caller's
75314564Sdim                    // registers from debugger
76314564Sdim    eSkipFrame,     // The unwind resulted in a bogus frame but may get back on
77314564Sdim                    // track so we don't want to give up yet
78314564Sdim    eNotAValidFrame // this frame is invalid for some reason - most likely it is
79314564Sdim                    // past the top (end) of the stack
80314564Sdim  };
81254721Semaste
82314564Sdim  // UnwindLLDB needs to pass around references to RegisterLocations
83314564Sdim  friend class UnwindLLDB;
84254721Semaste
85314564Sdim  // Returns true if we have an unwind loop -- the same stack frame unwinding
86314564Sdim  // multiple times.
87314564Sdim  bool CheckIfLoopingStack();
88254721Semaste
89314564Sdim  // Indicates whether this frame is frame zero -- the currently
90314564Sdim  // executing frame -- or not.
91314564Sdim  bool IsFrameZero() const;
92280031Sdim
93314564Sdim  void InitializeZerothFrame();
94254721Semaste
95314564Sdim  void InitializeNonZerothFrame();
96254721Semaste
97314564Sdim  SharedPtr GetNextFrame() const;
98254721Semaste
99314564Sdim  SharedPtr GetPrevFrame() const;
100254721Semaste
101314564Sdim  // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've
102314564Sdim  // got one bogus frame at frame #1.
103314564Sdim  // There is a good chance we'll get back on track if we follow the frame
104314564Sdim  // pointer chain (or whatever is appropriate
105314564Sdim  // on this ABI) so we allow one invalid frame to be in the stack.  Ideally
106314564Sdim  // we'll mark this frame specially at some
107314564Sdim  // point and indicate to the user that the unwinder had a hiccup.  Often when
108314564Sdim  // this happens we will miss a frame of
109314564Sdim  // the program's actual stack in the unwind and we want to flag that for the
110314564Sdim  // user somehow.
111314564Sdim  bool IsSkipFrame() const;
112254721Semaste
113314564Sdim  /// Determines if a SymbolContext is a trap handler or not
114314564Sdim  ///
115314564Sdim  /// Given a SymbolContext, determines if this is a trap handler function
116314564Sdim  /// aka asynchronous signal handler.
117314564Sdim  ///
118353358Sdim  /// \return
119314564Sdim  ///     Returns true if the SymbolContext is a trap handler.
120314564Sdim  bool IsTrapHandlerSymbol(lldb_private::Process *process,
121314564Sdim                           const lldb_private::SymbolContext &m_sym_ctx) const;
122254721Semaste
123360784Sdim  /// Check if the given unwind plan indicates a signal trap handler, and
124360784Sdim  /// update frame type and symbol context if so.
125360784Sdim  void PropagateTrapHandlerFlagFromUnwindPlan(lldb::UnwindPlanSP unwind_plan);
126360784Sdim
127314564Sdim  // Provide a location for where THIS function saved the CALLER's register
128314564Sdim  // value
129314564Sdim  // Or a frame "below" this one saved it, i.e. a function called by this one,
130314564Sdim  // preserved a register that this
131314564Sdim  // function didn't modify/use.
132314564Sdim  //
133314564Sdim  // The RegisterLocation type may be set to eRegisterNotAvailable -- this will
134314564Sdim  // happen for a volatile register
135314564Sdim  // being queried mid-stack.  Instead of floating frame 0's contents of that
136314564Sdim  // register up the stack (which may
137314564Sdim  // or may not be the value of that reg when the function was executing), we
138314564Sdim  // won't return any value.
139314564Sdim  //
140314564Sdim  // If a non-volatile register (a "preserved" register) is requested mid-stack
141314564Sdim  // and no frames "below" the requested
142314564Sdim  // stack have saved the register anywhere, it is safe to assume that frame 0's
143314564Sdim  // register values are still the same
144314564Sdim  // as the requesting frame's.
145314564Sdim  lldb_private::UnwindLLDB::RegisterSearchResult
146314564Sdim  SavedLocationForRegister(uint32_t lldb_regnum,
147314564Sdim                           lldb_private::UnwindLLDB::RegisterLocation &regloc);
148262528Semaste
149314564Sdim  bool ReadRegisterValueFromRegisterLocation(
150314564Sdim      lldb_private::UnwindLLDB::RegisterLocation regloc,
151314564Sdim      const lldb_private::RegisterInfo *reg_info,
152314564Sdim      lldb_private::RegisterValue &value);
153254721Semaste
154314564Sdim  bool WriteRegisterValueToRegisterLocation(
155314564Sdim      lldb_private::UnwindLLDB::RegisterLocation regloc,
156314564Sdim      const lldb_private::RegisterInfo *reg_info,
157314564Sdim      const lldb_private::RegisterValue &value);
158254721Semaste
159314564Sdim  /// If the unwind has to the caller frame has failed, try something else
160314564Sdim  ///
161314564Sdim  /// If lldb is using an assembly language based UnwindPlan for a frame and
162314564Sdim  /// the unwind to the caller frame fails, try falling back to a generic
163314564Sdim  /// UnwindPlan (architecture default unwindplan) to see if that might work
164314564Sdim  /// better.  This is mostly helping to work around problems where the
165314564Sdim  /// assembly language inspection fails on hand-written assembly code.
166314564Sdim  ///
167353358Sdim  /// \return
168314564Sdim  ///     Returns true if a fallback unwindplan was found & was installed.
169314564Sdim  bool TryFallbackUnwindPlan();
170254721Semaste
171314564Sdim  /// Switch to the fallback unwind plan unconditionally without any safety
172314564Sdim  /// checks that it is providing better results than the normal unwind plan.
173314564Sdim  ///
174314564Sdim  /// The only time it is valid to call this method is if the full unwindplan is
175314564Sdim  /// found to be fundamentally incorrect/impossible.
176314564Sdim  ///
177314564Sdim  /// Returns true if it was able to install the fallback unwind plan.
178314564Sdim  bool ForceSwitchToFallbackUnwindPlan();
179254721Semaste
180314564Sdim  // Get the contents of a general purpose (address-size) register for this
181314564Sdim  // frame
182314564Sdim  // (usually retrieved from the next frame)
183314564Sdim  bool ReadGPRValue(lldb::RegisterKind register_kind, uint32_t regnum,
184314564Sdim                    lldb::addr_t &value);
185280031Sdim
186314564Sdim  bool ReadGPRValue(const RegisterNumber &reg_num, lldb::addr_t &value);
187254721Semaste
188344779Sdim  // Get the Frame Address register for a given frame.
189344779Sdim  bool ReadFrameAddress(lldb::RegisterKind register_kind,
190344779Sdim                          UnwindPlan::Row::FAValue &fa, lldb::addr_t &address);
191280031Sdim
192314564Sdim  lldb::UnwindPlanSP GetFastUnwindPlanForFrame();
193280031Sdim
194314564Sdim  lldb::UnwindPlanSP GetFullUnwindPlanForFrame();
195254721Semaste
196314564Sdim  void UnwindLogMsg(const char *fmt, ...) __attribute__((format(printf, 2, 3)));
197254721Semaste
198314564Sdim  void UnwindLogMsgVerbose(const char *fmt, ...)
199314564Sdim      __attribute__((format(printf, 2, 3)));
200254721Semaste
201314564Sdim  bool IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp,
202314564Sdim                                     int &valid_pc_offset);
203254721Semaste
204360784Sdim  lldb::addr_t GetReturnAddressHint(int32_t plan_offset);
205360784Sdim
206314564Sdim  lldb_private::Thread &m_thread;
207262528Semaste
208314564Sdim  ///
209314564Sdim  // The following tell us how to retrieve the CALLER's register values (ie the
210314564Sdim  // "previous" frame, aka the frame above)
211314564Sdim  // i.e. where THIS frame saved them
212314564Sdim  ///
213254721Semaste
214314564Sdim  lldb::UnwindPlanSP m_fast_unwind_plan_sp; // may be NULL
215314564Sdim  lldb::UnwindPlanSP m_full_unwind_plan_sp;
216314564Sdim  lldb::UnwindPlanSP m_fallback_unwind_plan_sp; // may be NULL
217254721Semaste
218314564Sdim  bool m_all_registers_available; // Can we retrieve all regs or just
219314564Sdim                                  // nonvolatile regs?
220314564Sdim  int m_frame_type;               // enum FrameType
221262528Semaste
222314564Sdim  lldb::addr_t m_cfa;
223344779Sdim  lldb::addr_t m_afa;
224314564Sdim  lldb_private::Address m_start_pc;
225314564Sdim  lldb_private::Address m_current_pc;
226254721Semaste
227314564Sdim  int m_current_offset; // how far into the function we've executed; -1 if
228314564Sdim                        // unknown
229314564Sdim                        // 0 if no instructions have been executed yet.
230254721Semaste
231314564Sdim  int m_current_offset_backed_up_one; // how far into the function we've
232314564Sdim                                      // executed; -1 if unknown
233314564Sdim  // 0 if no instructions have been executed yet.
234314564Sdim  // On architectures where the return address on the stack points
235314564Sdim  // to the instruction after the CALL, this value will have 1
236314564Sdim  // subtracted from it.  Else a function that ends in a CALL will
237314564Sdim  // have an offset pointing into the next function's address range.
238314564Sdim  // m_current_pc has the actual address of the "current" pc.
239254721Semaste
240314564Sdim  lldb_private::SymbolContext &m_sym_ctx;
241314564Sdim  bool m_sym_ctx_valid; // if ResolveSymbolContextForAddress fails, don't try to
242314564Sdim                        // use m_sym_ctx
243254721Semaste
244314564Sdim  uint32_t m_frame_number; // What stack frame this RegisterContext is
245254721Semaste
246314564Sdim  std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation>
247314564Sdim      m_registers; // where to find reg values for this frame
248254721Semaste
249314564Sdim  lldb_private::UnwindLLDB &m_parent_unwind; // The UnwindLLDB that is creating
250314564Sdim                                             // this RegisterContextLLDB
251254721Semaste
252314564Sdim  // For RegisterContextLLDB only
253254721Semaste
254314564Sdim  DISALLOW_COPY_AND_ASSIGN(RegisterContextLLDB);
255254721Semaste};
256254721Semaste
257254721Semaste} // namespace lldb_private
258254721Semaste
259296417Sdim#endif // lldb_RegisterContextLLDB_h_
260