summaryrefslogtreecommitdiffstats
path: root/lldb/source/Symbol/Block.cpp
blob: 321411940f410f60c21ce049acf7cb4ea6a09ff7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
//===-- Block.cpp -----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/Symbol/Block.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Section.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/VariableList.h"

using namespace lldb;
using namespace lldb_private;

Block::Block(user_id_t uid, uint32_t depth, BlockList* blocks) :
    UserID(uid),
    m_block_list(blocks),
    m_depth(depth),
    m_ranges(),
    m_inlineInfoSP(),
    m_variables()
{
}

Block::Block(const Block& rhs) :
    UserID(rhs),
    m_block_list(rhs.m_block_list),
    m_depth(rhs.m_depth),
    m_ranges(rhs.m_ranges),
    m_inlineInfoSP(rhs.m_inlineInfoSP),
    m_variables(rhs.m_variables)
{
}

const Block&
Block::operator= (const Block& rhs)
{
    if (this != &rhs)
    {
        UserID::operator= (rhs);
        m_block_list = rhs.m_block_list;
        m_depth = rhs.m_depth;
        m_ranges = rhs.m_ranges;
        m_inlineInfoSP = rhs.m_inlineInfoSP;
        m_variables = rhs.m_variables;
    }
    return *this;
}

Block::~Block ()
{
}

void
Block::Dump(Stream *s, addr_t base_addr, int32_t depth, bool show_context) const
{
    if (depth < 0)
    {
        // We have a depth that is less than zero, print our parent blocks
        // first
        m_block_list->Dump(s, GetParentUID(), depth + 1, show_context);
    }

    s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
    s->Indent();
    *s << "Block" << ((const UserID&)*this);
    user_id_t parentID = GetParentUID();
    const Block* parent_block = NULL;
    if (parentID != Block::InvalidID)
    {
        parent_block = m_block_list->GetBlockByID(parentID);
        s->Printf(", parent = {0x%8.8x}", parentID);
    }
    if (m_inlineInfoSP.get() != NULL)
        m_inlineInfoSP->Dump(s);

    if (!m_ranges.empty())
    {
        *s << ", ranges =";
        std::vector<VMRange>::const_iterator pos;
        std::vector<VMRange>::const_iterator end = m_ranges.end();
        for (pos = m_ranges.begin(); pos != end; ++pos)
        {
            if (parent_block != NULL && parent_block->Contains(*pos) == false)
                *s << '!';
            else
                *s << ' ';
            pos->Dump(s, base_addr);
        }
    }
    s->EOL();

    if (depth > 0)
    {
        s->IndentMore();

        if (m_variables.get())
        {
            m_variables->Dump(s, show_context);
        }

        uint32_t blockID = m_block_list->GetFirstChild(GetID());
        while (blockID != Block::InvalidID)
        {
            m_block_list->Dump(s, blockID, depth - 1, show_context);

            blockID = m_block_list->GetSibling(blockID);
        }

        s->IndentLess();
    }

}


void
Block::CalculateSymbolContext(SymbolContext* sc)
{
    sc->block = this;
    m_block_list->GetFunction()->CalculateSymbolContext(sc);
}

void
Block::DumpStopContext (Stream *s, const SymbolContext *sc)
{
    user_id_t parentID = GetParentUID();
    Block* parent_block = NULL;
    if (parentID != Block::InvalidID)
        parent_block = m_block_list->GetBlockByID(parentID);

    InlineFunctionInfo* inline_info = InlinedFunctionInfo ();
    if (inline_info)
    {
        const Declaration &call_site = inline_info->GetCallSite();
        if (sc)
        {
            // First frame, dump the first inline call site
//            if (call_site.IsValid())
//            {
//                s->PutCString(" at ");
//                call_site.DumpStopContext (s);
//            }
            s->PutCString (" [inlined]");
        }
        s->EOL();
        inline_info->DumpStopContext (s);
        if (sc == NULL)
        {
            if (call_site.IsValid())
            {
                s->PutCString(" at ");
                call_site.DumpStopContext (s);
            }
        }
    }

    if (sc)
    {
        // If we have any inlined functions, this will be the deepest most
        // inlined location
        if (sc->line_entry.IsValid())
        {
            s->PutCString(" at ");
            sc->line_entry.DumpStopContext (s);
        }
    }
    if (parent_block)
        parent_block->Block::DumpStopContext (s, NULL);
}


void
Block::DumpSymbolContext(Stream *s)
{
    m_block_list->GetFunction()->DumpSymbolContext(s);
    s->Printf(", Block{0x%8.8x}", GetID());
}

bool
Block::Contains (addr_t range_offset) const
{
    return VMRange::ContainsValue(m_ranges, range_offset);
}

bool
Block::Contains (const VMRange& range) const
{
    return VMRange::ContainsRange(m_ranges, range);
}



bool
BlockList::BlockContainsBlockWithID (const user_id_t block_id, const user_id_t find_block_id) const
{
    if (block_id == Block::InvalidID)
        return false;

    if (block_id == find_block_id)
        return true;
    else
    {
        user_id_t child_block_id = GetFirstChild(block_id);
        while (child_block_id != Block::InvalidID)
        {
            if (BlockContainsBlockWithID (child_block_id, find_block_id))
                return true;
            child_block_id = GetSibling(child_block_id);
        }
    }

    return false;
}

bool
Block::ContainsBlockWithID (user_id_t block_id) const
{
    return m_block_list->BlockContainsBlockWithID (GetID(), block_id);
}


void
Block::AddRange(addr_t start_offset, addr_t end_offset)
{
    m_ranges.resize(m_ranges.size()+1);
    m_ranges.back().Reset(start_offset, end_offset);
}

InlineFunctionInfo*
Block::InlinedFunctionInfo ()
{
    return m_inlineInfoSP.get();
}

const InlineFunctionInfo*
Block::InlinedFunctionInfo () const
{
    return m_inlineInfoSP.get();
}

// Return the current number of bytes that this object occupies in memory
size_t
Block::MemorySize() const
{
    size_t mem_size = sizeof(Block) + m_ranges.size() * sizeof(VMRange);
    if (m_inlineInfoSP.get())
        mem_size += m_inlineInfoSP->MemorySize();
    if (m_variables.get())
        mem_size += m_variables->MemorySize();
    return mem_size;

}

user_id_t
Block::GetParentUID() const
{
    return m_block_list->GetParent(GetID());
}

user_id_t
Block::GetSiblingUID() const
{
    return m_block_list->GetSibling(GetID());
}

user_id_t
Block::GetFirstChildUID() const
{
    return m_block_list->GetFirstChild(GetID());
}

user_id_t
Block::AddChild(user_id_t userID)
{
    return m_block_list->AddChild(GetID(), userID);
}

void
Block::SetInlinedFunctionInfo(const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
{
    m_inlineInfoSP.reset(new InlineFunctionInfo(name, mangled, decl_ptr, call_decl_ptr));
}

BlockList::BlockList(Function *function, const AddressRange& range) :
    m_function(function),
    m_range(range),
    m_blocks()
{
}

BlockList::~BlockList()
{
}

AddressRange &
BlockList::GetAddressRange()
{
    return m_range;
}

const AddressRange &
BlockList::GetAddressRange() const
{
    return m_range;
}

void
BlockList::Dump(Stream *s, user_id_t blockID, uint32_t depth, bool show_context) const
{
    const Block* block = GetBlockByID(blockID);
    if (block)
        block->Dump(s, m_range.GetBaseAddress().GetFileAddress(), depth, show_context);
}

Function *
BlockList::GetFunction()
{
    return m_function;
}


const Function *
BlockList::GetFunction() const
{
    return m_function;
}

user_id_t
BlockList::GetParent(user_id_t blockID) const
{
    collection::const_iterator end = m_blocks.end();
    collection::const_iterator begin = m_blocks.begin();
    collection::const_iterator pos = std::find_if(begin, end, UserID::IDMatches(blockID));

    if (pos != end && pos != begin && pos->Depth() > 0)
    {
        const uint32_t parent_depth = pos->Depth() - 1;

        while (--pos >= begin)
        {
            if (pos->Depth() == parent_depth)
                return pos->GetID();
        }
    }
    return Block::InvalidID;
}

user_id_t
BlockList::GetSibling(user_id_t blockID) const
{
    collection::const_iterator end = m_blocks.end();
    collection::const_iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));

    if (pos != end)
    {
        const uint32_t sibling_depth = pos->Depth();
        while (++pos != end)
        {
            uint32_t depth = pos->Depth();
            if (depth == sibling_depth)
                return pos->GetID();
            if (depth < sibling_depth)
                break;
        }
    }
    return Block::InvalidID;
}

user_id_t
BlockList::GetFirstChild(user_id_t blockID) const
{
    if (!m_blocks.empty())
    {
        if (blockID == Block::RootID)
        {
            return m_blocks.front().GetID();
        }
        else
        {
            collection::const_iterator end = m_blocks.end();
            collection::const_iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));

            if (pos != end)
            {
                collection::const_iterator child_pos = pos + 1;
                if (child_pos != end)
                {
                    if (child_pos->Depth() == pos->Depth() + 1)
                        return child_pos->GetID();
                }
            }
        }
    }
    return Block::InvalidID;
}


// Return the current number of bytes that this object occupies in memory
size_t
BlockList::MemorySize() const
{
    size_t mem_size = sizeof(BlockList);

    collection::const_iterator pos, end = m_blocks.end();
    for (pos = m_blocks.begin(); pos != end; ++pos)
        mem_size += pos->MemorySize();  // Each block can vary in size

    return mem_size;

}

user_id_t
BlockList::AddChild (user_id_t parentID, user_id_t childID)
{
    bool added = false;
    if (parentID == Block::RootID)
    {
        assert(m_blocks.empty());
        Block block(childID, 0, this);
        m_blocks.push_back(block);
        added = true;
    }
    else
    {
        collection::iterator end = m_blocks.end();
        collection::iterator parent_pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(parentID));
        assert(parent_pos != end);
        if (parent_pos != end)
        {
            const uint32_t parent_sibling_depth = parent_pos->Depth();

            collection::iterator insert_pos = parent_pos;
            collection::iterator prev_sibling = end;
            while (++insert_pos != end)
            {
                if (insert_pos->Depth() <= parent_sibling_depth)
                    break;
            }

            Block child_block(childID, parent_pos->Depth() + 1, this);
            collection::iterator child_pos = m_blocks.insert(insert_pos, child_block);
            added = true;
        }
    }
    if (added)
        return childID;
    return Block::InvalidID;
}

const Block *
BlockList::GetBlockByID(user_id_t blockID) const
{
    if (m_blocks.empty())
        return NULL;

    if (blockID == Block::RootID)
        blockID = m_blocks.front().GetID();

    collection::const_iterator end = m_blocks.end();
    collection::const_iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));
    if (pos != end)
        return &(*pos);
    return NULL;
}

Block *
BlockList::GetBlockByID(user_id_t blockID)
{
    if (m_blocks.empty())
        return NULL;

    if (blockID == Block::RootID)
        blockID = m_blocks.front().GetID();

    collection::iterator end = m_blocks.end();
    collection::iterator pos = std::find_if(m_blocks.begin(), end, UserID::IDMatches(blockID));
    if (pos != end)
        return &(*pos);
    return NULL;
}

bool
BlockList::AddRange(user_id_t blockID, addr_t start_offset, addr_t end_offset)
{
    Block *block = GetBlockByID(blockID);

    if (block)
    {
        block->AddRange(start_offset, end_offset);
        return true;
    }
    return false;
}
//
//const Block *
//BlockList::FindDeepestBlockForAddress (const Address &addr)
//{
//    if (m_range.Contains(addr))
//    {
//        addr_t block_offset = addr.GetFileAddress() - m_range.GetBaseAddress().GetFileAddress();
//        collection::const_iterator pos, end = m_blocks.end();
//        collection::const_iterator deepest_match_pos = end;
//        for (pos = m_blocks.begin(); pos != end; ++pos)
//        {
//            if (pos->Contains (block_offset))
//            {
//                if (deepest_match_pos == end || deepest_match_pos->Depth() < pos->Depth())
//                    deepest_match_pos = pos;
//            }
//        }
//        if (deepest_match_pos != end)
//            return &(*deepest_match_pos);
//    }
//    return NULL;
//}
//
bool
BlockList::SetInlinedFunctionInfo(user_id_t blockID, const char *name, const char *mangled, const Declaration *decl_ptr, const Declaration *call_decl_ptr)
{
    Block *block = GetBlockByID(blockID);

    if (block)
    {
        block->SetInlinedFunctionInfo(name, mangled, decl_ptr, call_decl_ptr);
        return true;
    }
    return false;
}

VariableListSP
BlockList::GetVariableList(user_id_t blockID, bool get_child_variables, bool can_create)
{
    VariableListSP variable_list_sp;
    Block *block = GetBlockByID(blockID);
    if (block)
        variable_list_sp = block->GetVariableList(get_child_variables, can_create);
    return variable_list_sp;
}

bool
BlockList::IsEmpty() const
{
    return m_blocks.empty();
}



bool
BlockList::SetVariableList(user_id_t blockID, VariableListSP& variables)
{
    Block *block = GetBlockByID(blockID);
    if (block)
    {
        block->SetVariableList(variables);
        return true;
    }
    return false;

}


VariableListSP
Block::GetVariableList (bool get_child_variables, bool can_create)
{
    VariableListSP variable_list_sp;
    if (m_variables.get() == NULL && can_create)
    {
        SymbolContext sc;
        CalculateSymbolContext(&sc);
        assert(sc.module_sp);
        sc.module_sp->GetSymbolVendor()->ParseVariablesForContext(sc);
    }

    if (m_variables.get())
    {
        variable_list_sp.reset(new VariableList());
        if (variable_list_sp.get())
            variable_list_sp->AddVariables(m_variables.get());

        if (get_child_variables)
        {
            user_id_t block_id = GetFirstChildUID();
            while (block_id != Block::InvalidID)
            {
                Block *child_block = m_block_list->GetBlockByID(block_id);
                assert(child_block);
                VariableListSP child_block_variable_list(child_block->GetVariableList(get_child_variables, can_create));
                if (child_block_variable_list.get())
                    variable_list_sp->AddVariables(child_block_variable_list.get());

                block_id = child_block->GetSiblingUID();
            }
        }
    }

    return variable_list_sp;
}

uint32_t
Block::AppendVariables (bool can_create, bool get_parent_variables, VariableList *variable_list)
{
    uint32_t num_variables_added = 0;
    VariableListSP variable_list_sp(GetVariableList(false, can_create));

    if (variable_list_sp.get())
    {
        num_variables_added = variable_list_sp->GetSize();
        variable_list->AddVariables(variable_list_sp.get());
    }

    if (get_parent_variables)
    {
        user_id_t parentID = GetParentUID();
        if (parentID != Block::InvalidID)
        {
            Block* parent_block = m_block_list->GetBlockByID(parentID);
            if (parent_block)
                num_variables_added += parent_block->AppendVariables (can_create, get_parent_variables, variable_list);
        }
    }
    return num_variables_added;
}


void
Block::SetVariableList(VariableListSP& variables)
{
    m_variables = variables;
}

uint32_t
Block::Depth () const
{
    return m_depth;
}

OpenPOWER on IntegriCloud