RegisterContextLLDB.h revision 303975
1//===-- RegisterContextLLDB.h --------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef lldb_RegisterContextLLDB_h_
11#define lldb_RegisterContextLLDB_h_
12
13// C Includes
14// C++ Includes
15#include <vector>
16
17// Other libraries and framework includes
18// Project includes
19#include "lldb/lldb-private.h"
20#include "lldb/Target/RegisterContext.h"
21#include "lldb/Symbol/UnwindPlan.h"
22#include "lldb/Symbol/SymbolContext.h"
23#include "lldb/Utility/RegisterNumber.h"
24#include "UnwindLLDB.h"
25
26namespace lldb_private {
27
28class UnwindLLDB;
29
30class RegisterContextLLDB : public lldb_private::RegisterContext
31{
32public:
33    typedef std::shared_ptr<RegisterContextLLDB> SharedPtr;
34
35    RegisterContextLLDB (lldb_private::Thread &thread,
36                         const SharedPtr& next_frame,
37                         lldb_private::SymbolContext& sym_ctx,
38                         uint32_t frame_number, lldb_private::UnwindLLDB& unwind_lldb);
39
40    ~RegisterContextLLDB() override = default;
41
42    void
43    InvalidateAllRegisters() override;
44
45    size_t
46    GetRegisterCount() override;
47
48    const lldb_private::RegisterInfo *
49    GetRegisterInfoAtIndex(size_t reg) override;
50
51    size_t
52    GetRegisterSetCount() override;
53
54    const lldb_private::RegisterSet *
55    GetRegisterSet(size_t reg_set) override;
56
57    bool
58    ReadRegister(const lldb_private::RegisterInfo *reg_info,
59                 lldb_private::RegisterValue &value) override;
60
61    bool
62    WriteRegister(const lldb_private::RegisterInfo *reg_info,
63                  const lldb_private::RegisterValue &value) override;
64
65    bool
66    ReadAllRegisterValues(lldb::DataBufferSP &data_sp) override;
67
68    bool
69    WriteAllRegisterValues(const lldb::DataBufferSP &data_sp) override;
70
71    uint32_t
72    ConvertRegisterKindToRegisterNumber(lldb::RegisterKind kind, uint32_t num) override;
73
74    bool
75    IsValid () const;
76
77    bool
78    IsTrapHandlerFrame () const;
79
80    bool
81    GetCFA (lldb::addr_t& cfa);
82
83    bool
84    GetStartPC (lldb::addr_t& start_pc);
85
86    bool
87    ReadPC (lldb::addr_t& start_pc);
88
89private:
90
91    enum FrameType
92    {
93        eNormalFrame,
94        eTrapHandlerFrame,
95        eDebuggerFrame,  // a debugger inferior function call frame; we get caller's registers from debugger
96        eSkipFrame,      // The unwind resulted in a bogus frame but may get back on track so we don't want to give up yet
97        eNotAValidFrame  // this frame is invalid for some reason - most likely it is past the top (end) of the stack
98    };
99
100    // UnwindLLDB needs to pass around references to RegisterLocations
101    friend class UnwindLLDB;
102
103    // Returns true if we have an unwind loop -- the same stack frame unwinding
104    // multiple times.
105    bool
106    CheckIfLoopingStack ();
107
108    // Indicates whether this frame is frame zero -- the currently
109    // executing frame -- or not.
110    bool
111    IsFrameZero () const;
112
113    void
114    InitializeZerothFrame ();
115
116    void
117    InitializeNonZerothFrame();
118
119    SharedPtr
120    GetNextFrame () const;
121
122    SharedPtr
123    GetPrevFrame () const;
124
125    // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've got one bogus frame at frame #1.
126    // There is a good chance we'll get back on track if we follow the frame pointer chain (or whatever is appropriate
127    // on this ABI) so we allow one invalid frame to be in the stack.  Ideally we'll mark this frame specially at some
128    // point and indicate to the user that the unwinder had a hiccup.  Often when this happens we will miss a frame of
129    // the program's actual stack in the unwind and we want to flag that for the user somehow.
130    bool
131    IsSkipFrame () const;
132
133    //------------------------------------------------------------------
134    /// Determines if a SymbolContext is a trap handler or not
135    ///
136    /// Given a SymbolContext, determines if this is a trap handler function
137    /// aka asynchronous signal handler.
138    ///
139    /// @return
140    ///     Returns true if the SymbolContext is a trap handler.
141    //------------------------------------------------------------------
142    bool
143    IsTrapHandlerSymbol (lldb_private::Process *process, const lldb_private::SymbolContext &m_sym_ctx) const;
144
145    // Provide a location for where THIS function saved the CALLER's register value
146    // Or a frame "below" this one saved it, i.e. a function called by this one, preserved a register that this
147    // function didn't modify/use.
148    //
149    // The RegisterLocation type may be set to eRegisterNotAvailable -- this will happen for a volatile register
150    // being queried mid-stack.  Instead of floating frame 0's contents of that register up the stack (which may
151    // or may not be the value of that reg when the function was executing), we won't return any value.
152    //
153    // If a non-volatile register (a "preserved" register) is requested mid-stack and no frames "below" the requested
154    // stack have saved the register anywhere, it is safe to assume that frame 0's register values are still the same
155    // as the requesting frame's.
156    lldb_private::UnwindLLDB::RegisterSearchResult
157    SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc);
158
159    bool
160    ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
161                                           const lldb_private::RegisterInfo *reg_info,
162                                           lldb_private::RegisterValue &value);
163
164    bool
165    WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
166                                          const lldb_private::RegisterInfo *reg_info,
167                                          const lldb_private::RegisterValue &value);
168
169    //------------------------------------------------------------------
170    /// If the unwind has to the caller frame has failed, try something else
171    ///
172    /// If lldb is using an assembly language based UnwindPlan for a frame and
173    /// the unwind to the caller frame fails, try falling back to a generic
174    /// UnwindPlan (architecture default unwindplan) to see if that might work
175    /// better.  This is mostly helping to work around problems where the
176    /// assembly language inspection fails on hand-written assembly code.
177    ///
178    /// @return
179    ///     Returns true if a fallback unwindplan was found & was installed.
180    //------------------------------------------------------------------
181    bool
182    TryFallbackUnwindPlan ();
183
184    //------------------------------------------------------------------
185    /// Switch to the fallback unwind plan unconditionally without any safety
186    /// checks that it is providing better results than the normal unwind plan.
187    ///
188    /// The only time it is valid to call this method is if the full unwindplan is
189    /// found to be fundamentally incorrect/impossible.
190    ///
191    /// Returns true if it was able to install the fallback unwind plan.
192    //------------------------------------------------------------------
193    bool
194    ForceSwitchToFallbackUnwindPlan ();
195
196    // Get the contents of a general purpose (address-size) register for this frame
197    // (usually retrieved from the next frame)
198    bool
199    ReadGPRValue (lldb::RegisterKind register_kind, uint32_t regnum, lldb::addr_t &value);
200
201    bool
202    ReadGPRValue (const RegisterNumber &reg_num, lldb::addr_t &value);
203
204    // Get the CFA register for a given frame.
205    bool
206    ReadCFAValueForRow (lldb::RegisterKind register_kind, const UnwindPlan::RowSP &row, lldb::addr_t &value);
207
208    lldb::UnwindPlanSP
209    GetFastUnwindPlanForFrame ();
210
211    lldb::UnwindPlanSP
212    GetFullUnwindPlanForFrame ();
213
214    void
215    UnwindLogMsg (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
216
217    void
218    UnwindLogMsgVerbose (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
219
220    bool
221    IsUnwindPlanValidForCurrentPC(lldb::UnwindPlanSP unwind_plan_sp, int &valid_pc_offset);
222
223    lldb_private::Thread& m_thread;
224
225    ///
226    // The following tell us how to retrieve the CALLER's register values (ie the "previous" frame, aka the frame above)
227    // i.e. where THIS frame saved them
228    ///
229
230    lldb::UnwindPlanSP m_fast_unwind_plan_sp;     // may be NULL
231    lldb::UnwindPlanSP m_full_unwind_plan_sp;
232    lldb::UnwindPlanSP m_fallback_unwind_plan_sp; // may be NULL
233
234    bool m_all_registers_available;               // Can we retrieve all regs or just nonvolatile regs?
235    int m_frame_type;                             // enum FrameType
236
237    lldb::addr_t m_cfa;
238    lldb_private::Address m_start_pc;
239    lldb_private::Address m_current_pc;
240
241    int m_current_offset;                         // how far into the function we've executed; -1 if unknown
242                                                  // 0 if no instructions have been executed yet.
243
244    int m_current_offset_backed_up_one;           // how far into the function we've executed; -1 if unknown
245                                                  // 0 if no instructions have been executed yet.
246                                                  // On architectures where the return address on the stack points
247                                                  // to the instruction after the CALL, this value will have 1
248                                                  // subtracted from it.  Else a function that ends in a CALL will
249                                                  // have an offset pointing into the next function's address range.
250                                                  // m_current_pc has the actual address of the "current" pc.
251
252    lldb_private::SymbolContext& m_sym_ctx;
253    bool m_sym_ctx_valid;                         // if ResolveSymbolContextForAddress fails, don't try to use m_sym_ctx
254
255    uint32_t m_frame_number;                      // What stack frame this RegisterContext is
256
257    std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation> m_registers; // where to find reg values for this frame
258
259    lldb_private::UnwindLLDB& m_parent_unwind;    // The UnwindLLDB that is creating this RegisterContextLLDB
260
261    //------------------------------------------------------------------
262    // For RegisterContextLLDB only
263    //------------------------------------------------------------------
264
265    DISALLOW_COPY_AND_ASSIGN (RegisterContextLLDB);
266};
267
268} // namespace lldb_private
269
270#endif // lldb_RegisterContextLLDB_h_
271