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#include <vector>
14
15#include "lldb/lldb-private.h"
16#include "lldb/Target/RegisterContext.h"
17#include "lldb/Symbol/UnwindPlan.h"
18#include "lldb/Symbol/SymbolContext.h"
19#include "UnwindLLDB.h"
20
21namespace lldb_private {
22
23class UnwindLLDB;
24
25class RegisterContextLLDB : public lldb_private::RegisterContext
26{
27public:
28    typedef std::shared_ptr<RegisterContextLLDB> SharedPtr;
29
30    RegisterContextLLDB (lldb_private::Thread &thread,
31                         const SharedPtr& next_frame,
32                         lldb_private::SymbolContext& sym_ctx,
33                         uint32_t frame_number, lldb_private::UnwindLLDB& unwind_lldb);
34
35    ///
36    // pure virtual functions from the base class that we must implement
37    ///
38
39    virtual
40    ~RegisterContextLLDB () { }
41
42    virtual void
43    InvalidateAllRegisters ();
44
45    virtual size_t
46    GetRegisterCount ();
47
48    virtual const lldb_private::RegisterInfo *
49    GetRegisterInfoAtIndex (size_t reg);
50
51    virtual size_t
52    GetRegisterSetCount ();
53
54    virtual const lldb_private::RegisterSet *
55    GetRegisterSet (size_t reg_set);
56
57    virtual bool
58    ReadRegister (const lldb_private::RegisterInfo *reg_info, lldb_private::RegisterValue &value);
59
60    virtual bool
61    WriteRegister (const lldb_private::RegisterInfo *reg_info, const lldb_private::RegisterValue &value);
62
63    virtual bool
64    ReadAllRegisterValues (lldb::DataBufferSP &data_sp);
65
66    virtual bool
67    WriteAllRegisterValues (const lldb::DataBufferSP &data_sp);
68
69    virtual uint32_t
70    ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num);
71
72    bool
73    IsValid () const;
74
75    bool
76    GetCFA (lldb::addr_t& cfa);
77
78    bool
79    GetStartPC (lldb::addr_t& start_pc);
80
81    bool
82    ReadPC (lldb::addr_t& start_pc);
83
84private:
85
86    enum FrameType
87    {
88        eNormalFrame,
89        eSigtrampFrame,
90        eDebuggerFrame,  // a debugger inferior function call frame; we get caller's registers from debugger
91        eSkipFrame,      // The unwind resulted in a bogus frame but may get back on track so we don't want to give up yet
92        eNotAValidFrame  // this frame is invalid for some reason - most likely it is past the top (end) of the stack
93    };
94
95    // UnwindLLDB needs to pass around references to RegisterLocations
96    friend class UnwindLLDB;
97
98    // Indicates whether this frame is frame zero -- the currently
99    // executing frame -- or not.
100    bool
101    IsFrameZero () const;
102
103    void
104    InitializeZerothFrame ();
105
106    void
107    InitializeNonZerothFrame();
108
109    SharedPtr
110    GetNextFrame () const;
111
112    SharedPtr
113    GetPrevFrame () const;
114
115    // A SkipFrame occurs when the unwind out of frame 0 didn't go right -- we've got one bogus frame at frame #1.
116    // There is a good chance we'll get back on track if we follow the frame pointer chain (or whatever is appropriate
117    // on this ABI) so we allow one invalid frame to be in the stack.  Ideally we'll mark this frame specially at some
118    // point and indicate to the user that the unwinder had a hiccup.  Often when this happens we will miss a frame of
119    // the program's actual stack in the unwind and we want to flag that for the user somehow.
120    bool
121    IsSkipFrame () const;
122
123    // Provide a location for where THIS function saved the CALLER's register value
124    // Or a frame "below" this one saved it, i.e. a function called by this one, preserved a register that this
125    // function didn't modify/use.
126    //
127    // The RegisterLocation type may be set to eRegisterNotAvailable -- this will happen for a volatile register
128    // being queried mid-stack.  Instead of floating frame 0's contents of that register up the stack (which may
129    // or may not be the value of that reg when the function was executing), we won't return any value.
130    //
131    // If a non-volatile register (a "preserved" register) is requested mid-stack and no frames "below" the requested
132    // stack have saved the register anywhere, it is safe to assume that frame 0's register values are still the same
133    // as the requesting frame's.
134    lldb_private::UnwindLLDB::RegisterSearchResult
135    SavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc);
136
137    bool
138    ReadRegisterValueFromRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
139                                           const lldb_private::RegisterInfo *reg_info,
140                                           lldb_private::RegisterValue &value);
141
142    bool
143    WriteRegisterValueToRegisterLocation (lldb_private::UnwindLLDB::RegisterLocation regloc,
144                                          const lldb_private::RegisterInfo *reg_info,
145                                          const lldb_private::RegisterValue &value);
146
147    void
148    InvalidateFullUnwindPlan ();
149
150    // Get the contents of a general purpose (address-size) register for this frame
151    // (usually retrieved from the next frame)
152    bool
153    ReadGPRValue (int register_kind, uint32_t regnum, lldb::addr_t &value);
154
155    lldb::UnwindPlanSP
156    GetFastUnwindPlanForFrame ();
157
158    lldb::UnwindPlanSP
159    GetFullUnwindPlanForFrame ();
160
161    void
162    UnwindLogMsg (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
163
164    void
165    UnwindLogMsgVerbose (const char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
166
167    lldb_private::Thread& m_thread;
168
169    ///
170    // The following tell us how to retrieve the CALLER's register values (ie the "previous" frame, aka the frame above)
171    // i.e. where THIS frame saved them
172    ///
173
174    lldb::UnwindPlanSP m_fast_unwind_plan_sp;  // may be NULL
175    lldb::UnwindPlanSP m_full_unwind_plan_sp;
176    bool m_all_registers_available;               // Can we retrieve all regs or just nonvolatile regs?
177    int m_frame_type;                             // enum FrameType
178
179    lldb::addr_t m_cfa;
180    lldb_private::Address m_start_pc;
181    lldb_private::Address m_current_pc;
182
183    int m_current_offset;                         // how far into the function we've executed; -1 if unknown
184                                                  // 0 if no instructions have been executed yet.
185
186    int m_current_offset_backed_up_one;           // how far into the function we've executed; -1 if unknown
187                                                  // 0 if no instructions have been executed yet.
188                                                  // On architectures where the return address on the stack points
189                                                  // to the instruction after the CALL, this value will have 1
190                                                  // subtracted from it.  Else a function that ends in a CALL will
191                                                  // have an offset pointing into the next function's address range.
192                                                  // m_current_pc has the actual address of the "current" pc.
193
194    lldb_private::SymbolContext& m_sym_ctx;
195    bool m_sym_ctx_valid;                         // if ResolveSymbolContextForAddress fails, don't try to use m_sym_ctx
196
197    uint32_t m_frame_number;                      // What stack frame this RegisterContext is
198
199    std::map<uint32_t, lldb_private::UnwindLLDB::RegisterLocation> m_registers; // where to find reg values for this frame
200
201    lldb_private::UnwindLLDB& m_parent_unwind;    // The UnwindLLDB that is creating this RegisterContextLLDB
202
203    //------------------------------------------------------------------
204    // For RegisterContextLLDB only
205    //------------------------------------------------------------------
206
207    DISALLOW_COPY_AND_ASSIGN (RegisterContextLLDB);
208};
209
210} // namespace lldb_private
211
212#endif  // lldb_RegisterContextLLDB_h_
213