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