UnwindLLDB.cpp revision 258054
1254721Semaste//===-- UnwindLLDB.cpp -------------------------------------*- C++ -*-===//
2254721Semaste//
3254721Semaste//                     The LLVM Compiler Infrastructure
4254721Semaste//
5254721Semaste// This file is distributed under the University of Illinois Open Source
6254721Semaste// License. See LICENSE.TXT for details.
7254721Semaste//
8254721Semaste//===----------------------------------------------------------------------===//
9254721Semaste
10254721Semaste#include "lldb/Core/Module.h"
11254721Semaste#include "lldb/Core/Log.h"
12254721Semaste#include "lldb/Symbol/FuncUnwinders.h"
13254721Semaste#include "lldb/Symbol/Function.h"
14254721Semaste#include "lldb/Symbol/UnwindPlan.h"
15254721Semaste#include "lldb/Target/Thread.h"
16254721Semaste#include "lldb/Target/Target.h"
17254721Semaste#include "lldb/Target/Process.h"
18254721Semaste#include "lldb/Target/RegisterContext.h"
19254721Semaste
20254721Semaste#include "UnwindLLDB.h"
21254721Semaste#include "RegisterContextLLDB.h"
22254721Semaste
23254721Semasteusing namespace lldb;
24254721Semasteusing namespace lldb_private;
25254721Semaste
26254721SemasteUnwindLLDB::UnwindLLDB (Thread &thread) :
27254721Semaste    Unwind (thread),
28254721Semaste    m_frames(),
29254721Semaste    m_unwind_complete(false)
30254721Semaste{
31254721Semaste}
32254721Semaste
33254721Semasteuint32_t
34254721SemasteUnwindLLDB::DoGetFrameCount()
35254721Semaste{
36254721Semaste    if (!m_unwind_complete)
37254721Semaste    {
38254721Semaste//#define DEBUG_FRAME_SPEED 1
39254721Semaste#if DEBUG_FRAME_SPEED
40254721Semaste#define FRAME_COUNT 10000
41254721Semaste        TimeValue time_value (TimeValue::Now());
42254721Semaste#endif
43254721Semaste        if (!AddFirstFrame ())
44254721Semaste            return 0;
45254721Semaste
46254721Semaste        ProcessSP process_sp (m_thread.GetProcess());
47254721Semaste        ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
48254721Semaste
49254721Semaste        while (AddOneMoreFrame (abi))
50254721Semaste        {
51254721Semaste#if DEBUG_FRAME_SPEED
52254721Semaste            if ((m_frames.size() % FRAME_COUNT) == 0)
53254721Semaste            {
54254721Semaste                TimeValue now(TimeValue::Now());
55254721Semaste                uint64_t delta_t = now - time_value;
56254721Semaste                printf ("%u frames in %" PRIu64 ".%09llu ms (%g frames/sec)\n",
57254721Semaste                        FRAME_COUNT,
58254721Semaste                        delta_t / TimeValue::NanoSecPerSec,
59254721Semaste                        delta_t % TimeValue::NanoSecPerSec,
60254721Semaste                        (float)FRAME_COUNT / ((float)delta_t / (float)TimeValue::NanoSecPerSec));
61254721Semaste                time_value = now;
62254721Semaste            }
63254721Semaste#endif
64254721Semaste        }
65254721Semaste    }
66254721Semaste    return m_frames.size ();
67254721Semaste}
68254721Semaste
69254721Semastebool
70254721SemasteUnwindLLDB::AddFirstFrame ()
71254721Semaste{
72254721Semaste    if (m_frames.size() > 0)
73254721Semaste        return true;
74254721Semaste
75254721Semaste    // First, set up the 0th (initial) frame
76254721Semaste    CursorSP first_cursor_sp(new Cursor ());
77254721Semaste    RegisterContextLLDBSP reg_ctx_sp (new RegisterContextLLDB (m_thread,
78254721Semaste                                                               RegisterContextLLDBSP(),
79254721Semaste                                                               first_cursor_sp->sctx,
80254721Semaste                                                               0, *this));
81254721Semaste    if (reg_ctx_sp.get() == NULL)
82254721Semaste        goto unwind_done;
83254721Semaste
84254721Semaste    if (!reg_ctx_sp->IsValid())
85254721Semaste        goto unwind_done;
86254721Semaste
87254721Semaste    if (!reg_ctx_sp->GetCFA (first_cursor_sp->cfa))
88254721Semaste        goto unwind_done;
89254721Semaste
90254721Semaste    if (!reg_ctx_sp->ReadPC (first_cursor_sp->start_pc))
91254721Semaste        goto unwind_done;
92254721Semaste
93254721Semaste    // Everything checks out, so release the auto pointer value and let the
94254721Semaste    // cursor own it in its shared pointer
95254721Semaste    first_cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
96254721Semaste    m_frames.push_back (first_cursor_sp);
97254721Semaste    return true;
98254721Semasteunwind_done:
99254721Semaste    m_unwind_complete = true;
100254721Semaste    return false;
101254721Semaste}
102254721Semaste
103254721Semaste// For adding a non-zero stack frame to m_frames.
104254721Semastebool
105254721SemasteUnwindLLDB::AddOneMoreFrame (ABI *abi)
106254721Semaste{
107254721Semaste    // If we've already gotten to the end of the stack, don't bother to try again...
108254721Semaste    if (m_unwind_complete)
109254721Semaste        return false;
110254721Semaste
111254721Semaste    Log *log(GetLogIfAllCategoriesSet (LIBLLDB_LOG_UNWIND));
112254721Semaste    CursorSP cursor_sp(new Cursor ());
113254721Semaste
114254721Semaste    // Frame zero is a little different
115254721Semaste    if (m_frames.size() == 0)
116254721Semaste        return false;
117254721Semaste
118254721Semaste    uint32_t cur_idx = m_frames.size ();
119254721Semaste    RegisterContextLLDBSP reg_ctx_sp(new RegisterContextLLDB (m_thread,
120254721Semaste                                                              m_frames[cur_idx - 1]->reg_ctx_lldb_sp,
121254721Semaste                                                              cursor_sp->sctx,
122254721Semaste                                                              cur_idx,
123254721Semaste                                                              *this));
124254721Semaste
125254721Semaste    // We want to detect an unwind that cycles erronously and stop backtracing.
126254721Semaste    // Don't want this maximum unwind limit to be too low -- if you have a backtrace
127254721Semaste    // with an "infinitely recursing" bug, it will crash when the stack blows out
128254721Semaste    // and the first 35,000 frames are uninteresting - it's the top most 5 frames that
129254721Semaste    // you actually care about.  So you can't just cap the unwind at 10,000 or something.
130254721Semaste    // Realistically anything over around 200,000 is going to blow out the stack space.
131254721Semaste    // If we're still unwinding at that point, we're probably never going to finish.
132254721Semaste    if (cur_idx > 300000)
133254721Semaste    {
134254721Semaste        if (log)
135254721Semaste            log->Printf ("%*sFrame %d unwound too many frames, assuming unwind has gone astray, stopping.",
136254721Semaste                         cur_idx < 100 ? cur_idx : 100, "", cur_idx);
137254721Semaste        goto unwind_done;
138254721Semaste    }
139254721Semaste
140254721Semaste    if (reg_ctx_sp.get() == NULL)
141254721Semaste        goto unwind_done;
142254721Semaste
143254721Semaste    if (!reg_ctx_sp->IsValid())
144254721Semaste    {
145254721Semaste        if (log)
146254721Semaste        {
147254721Semaste            log->Printf("%*sFrame %d invalid RegisterContext for this frame, stopping stack walk",
148254721Semaste                        cur_idx < 100 ? cur_idx : 100, "", cur_idx);
149254721Semaste        }
150254721Semaste        goto unwind_done;
151254721Semaste    }
152254721Semaste    if (!reg_ctx_sp->GetCFA (cursor_sp->cfa))
153254721Semaste    {
154254721Semaste        if (log)
155254721Semaste        {
156254721Semaste            log->Printf("%*sFrame %d did not get CFA for this frame, stopping stack walk",
157254721Semaste                        cur_idx < 100 ? cur_idx : 100, "", cur_idx);
158254721Semaste        }
159254721Semaste        goto unwind_done;
160254721Semaste    }
161254721Semaste    if (abi && !abi->CallFrameAddressIsValid(cursor_sp->cfa))
162254721Semaste    {
163254721Semaste        if (log)
164254721Semaste        {
165254721Semaste            log->Printf("%*sFrame %d did not get a valid CFA for this frame, stopping stack walk",
166254721Semaste                        cur_idx < 100 ? cur_idx : 100, "", cur_idx);
167254721Semaste        }
168254721Semaste        goto unwind_done;
169254721Semaste    }
170254721Semaste    if (!reg_ctx_sp->ReadPC (cursor_sp->start_pc))
171254721Semaste    {
172254721Semaste        if (log)
173254721Semaste        {
174254721Semaste            log->Printf("%*sFrame %d did not get PC for this frame, stopping stack walk",
175254721Semaste                        cur_idx < 100 ? cur_idx : 100, "", cur_idx);
176254721Semaste        }
177254721Semaste        goto unwind_done;
178254721Semaste    }
179254721Semaste    if (abi && !abi->CodeAddressIsValid (cursor_sp->start_pc))
180254721Semaste    {
181254721Semaste        if (log)
182254721Semaste        {
183254721Semaste            log->Printf("%*sFrame %d did not get a valid PC, stopping stack walk",
184254721Semaste                        cur_idx < 100 ? cur_idx : 100, "", cur_idx);
185254721Semaste        }
186254721Semaste        goto unwind_done;
187254721Semaste    }
188258054Semaste
189254721Semaste    cursor_sp->reg_ctx_lldb_sp = reg_ctx_sp;
190254721Semaste    m_frames.push_back (cursor_sp);
191254721Semaste    return true;
192254721Semaste
193254721Semasteunwind_done:
194254721Semaste    m_unwind_complete = true;
195254721Semaste    return false;
196254721Semaste}
197254721Semaste
198254721Semastebool
199254721SemasteUnwindLLDB::DoGetFrameInfoAtIndex (uint32_t idx, addr_t& cfa, addr_t& pc)
200254721Semaste{
201254721Semaste    if (m_frames.size() == 0)
202254721Semaste    {
203254721Semaste        if (!AddFirstFrame())
204254721Semaste            return false;
205254721Semaste    }
206254721Semaste
207254721Semaste    ProcessSP process_sp (m_thread.GetProcess());
208254721Semaste    ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
209254721Semaste
210254721Semaste    while (idx >= m_frames.size() && AddOneMoreFrame (abi))
211254721Semaste        ;
212254721Semaste
213254721Semaste    if (idx < m_frames.size ())
214254721Semaste    {
215254721Semaste        cfa = m_frames[idx]->cfa;
216254721Semaste        pc = m_frames[idx]->start_pc;
217254721Semaste        return true;
218254721Semaste    }
219254721Semaste    return false;
220254721Semaste}
221254721Semaste
222254721Semastelldb::RegisterContextSP
223254721SemasteUnwindLLDB::DoCreateRegisterContextForFrame (StackFrame *frame)
224254721Semaste{
225254721Semaste    lldb::RegisterContextSP reg_ctx_sp;
226254721Semaste    uint32_t idx = frame->GetConcreteFrameIndex ();
227254721Semaste
228254721Semaste    if (idx == 0)
229254721Semaste    {
230254721Semaste        return m_thread.GetRegisterContext();
231254721Semaste    }
232254721Semaste
233254721Semaste    if (m_frames.size() == 0)
234254721Semaste    {
235254721Semaste        if (!AddFirstFrame())
236254721Semaste            return reg_ctx_sp;
237254721Semaste    }
238254721Semaste
239254721Semaste    ProcessSP process_sp (m_thread.GetProcess());
240254721Semaste    ABI *abi = process_sp ? process_sp->GetABI().get() : NULL;
241254721Semaste
242254721Semaste    while (idx >= m_frames.size())
243254721Semaste    {
244254721Semaste        if (!AddOneMoreFrame (abi))
245254721Semaste            break;
246254721Semaste    }
247254721Semaste
248254721Semaste    const uint32_t num_frames = m_frames.size();
249254721Semaste    if (idx < num_frames)
250254721Semaste    {
251254721Semaste        Cursor *frame_cursor = m_frames[idx].get();
252254721Semaste        reg_ctx_sp = frame_cursor->reg_ctx_lldb_sp;
253254721Semaste    }
254254721Semaste    return reg_ctx_sp;
255254721Semaste}
256254721Semaste
257254721SemasteUnwindLLDB::RegisterContextLLDBSP
258254721SemasteUnwindLLDB::GetRegisterContextForFrameNum (uint32_t frame_num)
259254721Semaste{
260254721Semaste    RegisterContextLLDBSP reg_ctx_sp;
261254721Semaste    if (frame_num < m_frames.size())
262254721Semaste        reg_ctx_sp = m_frames[frame_num]->reg_ctx_lldb_sp;
263254721Semaste    return reg_ctx_sp;
264254721Semaste}
265254721Semaste
266254721Semastebool
267254721SemasteUnwindLLDB::SearchForSavedLocationForRegister (uint32_t lldb_regnum, lldb_private::UnwindLLDB::RegisterLocation &regloc, uint32_t starting_frame_num, bool pc_reg)
268254721Semaste{
269254721Semaste    int64_t frame_num = starting_frame_num;
270254721Semaste    if (frame_num >= m_frames.size())
271254721Semaste        return false;
272254721Semaste
273254721Semaste    // Never interrogate more than one level while looking for the saved pc value.  If the value
274254721Semaste    // isn't saved by frame_num, none of the frames lower on the stack will have a useful value.
275254721Semaste    if (pc_reg)
276254721Semaste    {
277254721Semaste        UnwindLLDB::RegisterSearchResult result;
278254721Semaste        result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
279254721Semaste        if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
280254721Semaste          return true;
281254721Semaste        else
282254721Semaste          return false;
283254721Semaste    }
284254721Semaste    while (frame_num >= 0)
285254721Semaste    {
286254721Semaste        UnwindLLDB::RegisterSearchResult result;
287254721Semaste        result = m_frames[frame_num]->reg_ctx_lldb_sp->SavedLocationForRegister (lldb_regnum, regloc);
288254721Semaste
289254721Semaste        // If we have unwind instructions saying that register N is saved in register M in the middle of
290254721Semaste        // the stack (and N can equal M here, meaning the register was not used in this function), then
291254721Semaste        // change the register number we're looking for to M and keep looking for a concrete  location
292254721Semaste        // down the stack, or an actual value from a live RegisterContext at frame 0.
293254721Semaste        if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound
294254721Semaste            && regloc.type == UnwindLLDB::RegisterLocation::eRegisterInRegister
295254721Semaste            && frame_num > 0)
296254721Semaste        {
297254721Semaste            result = UnwindLLDB::RegisterSearchResult::eRegisterNotFound;
298254721Semaste            lldb_regnum = regloc.location.register_number;
299254721Semaste        }
300254721Semaste
301254721Semaste        if (result == UnwindLLDB::RegisterSearchResult::eRegisterFound)
302254721Semaste            return true;
303254721Semaste        if (result == UnwindLLDB::RegisterSearchResult::eRegisterIsVolatile)
304254721Semaste            return false;
305254721Semaste        frame_num--;
306254721Semaste    }
307254721Semaste    return false;
308254721Semaste}
309