aboutsummaryrefslogtreecommitdiff
path: root/source/Plugins/SymbolFile/DWARF/DWARFDIE.cpp
blob: 0564de9e5dd1f34ad1facb9e4181fa4545aab5f2 (plain) (blame)
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
//===-- DWARFDIE.cpp --------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "DWARFDIE.h"

#include "DWARFCompileUnit.h"
#include "DWARFDebugAbbrev.h"
#include "DWARFDebugAranges.h"
#include "DWARFDebugInfo.h"
#include "DWARFDebugInfoEntry.h"
#include "DWARFDebugRanges.h"
#include "DWARFDeclContext.h"
#include "DWARFDIECollection.h"
#include "DWARFFormValue.h"
#include "SymbolFileDWARF.h"

#include "lldb/Core/Module.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/Type.h"
#include "lldb/Symbol/TypeSystem.h"

using namespace lldb_private;

DIERef
DWARFDIE::GetDIERef() const
{
    if (!IsValid())
        return DIERef();

    dw_offset_t cu_offset = m_cu->GetOffset();
    if (m_cu->GetBaseObjOffset() != DW_INVALID_OFFSET)
        cu_offset = m_cu->GetBaseObjOffset();
    return DIERef(cu_offset, m_die->GetOffset());
}

dw_tag_t
DWARFDIE::Tag() const
{
    if (m_die)
        return m_die->Tag();
    else
        return 0;
}

const char *
DWARFDIE::GetTagAsCString () const
{
    return lldb_private::DW_TAG_value_to_name (Tag());
}

DWARFDIE
DWARFDIE::GetParent () const
{
    if (IsValid())
        return DWARFDIE(m_cu, m_die->GetParent());
    else
        return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetFirstChild () const
{
    if (IsValid())
        return DWARFDIE(m_cu, m_die->GetFirstChild());
    else
        return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetSibling () const
{
    if (IsValid())
        return DWARFDIE(m_cu, m_die->GetSibling());
    else
        return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetReferencedDIE (const dw_attr_t attr) const
{
    const dw_offset_t die_offset = GetAttributeValueAsReference (attr, DW_INVALID_OFFSET);
    if (die_offset != DW_INVALID_OFFSET)
        return GetDIE(die_offset);
    else
        return DWARFDIE();
}

DWARFDIE
DWARFDIE::GetDIE (dw_offset_t die_offset) const
{
    if (IsValid())
        return m_cu->GetDIE(die_offset);
    else
        return DWARFDIE();
}

const char *
DWARFDIE::GetAttributeValueAsString (const dw_attr_t attr, const char *fail_value) const
{
    if (IsValid())
        return m_die->GetAttributeValueAsString(GetDWARF(), GetCU(), attr, fail_value);
    else
        return fail_value;
}

uint64_t
DWARFDIE::GetAttributeValueAsUnsigned (const dw_attr_t attr, uint64_t fail_value) const
{
    if (IsValid())
        return m_die->GetAttributeValueAsUnsigned(GetDWARF(), GetCU(), attr, fail_value);
    else
        return fail_value;
}

int64_t
DWARFDIE::GetAttributeValueAsSigned (const dw_attr_t attr, int64_t fail_value) const
{
    if (IsValid())
        return m_die->GetAttributeValueAsSigned(GetDWARF(), GetCU(), attr, fail_value);
    else
        return fail_value;
}

uint64_t
DWARFDIE::GetAttributeValueAsReference (const dw_attr_t attr, uint64_t fail_value) const
{
    if (IsValid())
        return m_die->GetAttributeValueAsReference(GetDWARF(), GetCU(), attr, fail_value);
    else
        return fail_value;
}

uint64_t
DWARFDIE::GetAttributeValueAsAddress (const dw_attr_t attr, uint64_t fail_value) const
{
    if (IsValid())
        return m_die->GetAttributeValueAsAddress(GetDWARF(), GetCU(), attr, fail_value);
    else
        return fail_value;
}


DWARFDIE
DWARFDIE::LookupDeepestBlock (lldb::addr_t file_addr) const
{
    if (IsValid())
    {
        SymbolFileDWARF *dwarf= GetDWARF();
        DWARFCompileUnit *cu = GetCU();
        DWARFDebugInfoEntry* function_die = nullptr;
        DWARFDebugInfoEntry* block_die = nullptr;
        if (m_die->LookupAddress (file_addr,
                                  dwarf,
                                  cu,
                                  &function_die,
                                  &block_die))
        {
            if (block_die && block_die != function_die)
            {
                if (cu->ContainsDIEOffset(block_die->GetOffset()))
                    return DWARFDIE(cu, block_die);
                else
                    return DWARFDIE(dwarf->DebugInfo()->GetCompileUnitContainingDIE(DIERef(cu->GetOffset(), block_die->GetOffset())), block_die);
            }
        }
    }
    return DWARFDIE();
}

lldb::user_id_t
DWARFDIE::GetID () const
{
    const dw_offset_t die_offset = GetOffset();
    if (die_offset != DW_INVALID_OFFSET)
    {
        lldb::user_id_t id = 0;
        SymbolFileDWARF *dwarf = GetDWARF();
        if (dwarf)
            id = dwarf->MakeUserID(die_offset);
        else
            id = die_offset;

        if (m_cu)
        {
            lldb::user_id_t cu_id = m_cu->GetID()&0xffffffff00000000ull;
            assert ((id&0xffffffff00000000ull) == 0 ||
                    (cu_id&0xffffffff00000000ll) == 0 ||
                    (id&0xffffffff00000000ull) == (cu_id&0xffffffff00000000ll));
            id |= cu_id;
        }
        return id;
    }
    return LLDB_INVALID_UID;
}

const char *
DWARFDIE::GetName () const
{
    if (IsValid())
        return m_die->GetName (GetDWARF(), m_cu);
    else
        return nullptr;
}

const char *
DWARFDIE::GetMangledName () const
{
    if (IsValid())
        return m_die->GetMangledName (GetDWARF(), m_cu);
    else
        return nullptr;
}

const char *
DWARFDIE::GetPubname () const
{
    if (IsValid())
        return m_die->GetPubname (GetDWARF(), m_cu);
    else
        return nullptr;
}

const char *
DWARFDIE::GetQualifiedName (std::string &storage) const
{
    if (IsValid())
        return m_die->GetQualifiedName (GetDWARF(), m_cu, storage);
    else
        return nullptr;
}

lldb::LanguageType
DWARFDIE::GetLanguage () const
{
    if (IsValid())
        return m_cu->GetLanguageType();
    else
        return lldb::eLanguageTypeUnknown;
}


lldb::ModuleSP
DWARFDIE::GetModule () const
{
    SymbolFileDWARF *dwarf = GetDWARF();
    if (dwarf)
        return dwarf->GetObjectFile()->GetModule();
    else
        return lldb::ModuleSP();
}

lldb_private::CompileUnit *
DWARFDIE::GetLLDBCompileUnit () const
{
    if (IsValid())
        return GetDWARF()->GetCompUnitForDWARFCompUnit(GetCU());
    else
        return nullptr;
}

lldb_private::Type *
DWARFDIE::ResolveType () const
{
    if (IsValid())
        return GetDWARF()->ResolveType(*this, true);
    else
        return nullptr;
}

lldb_private::Type *
DWARFDIE::ResolveTypeUID (lldb::user_id_t uid) const
{
    SymbolFileDWARF *dwarf = GetDWARF();
    if (dwarf)
        return dwarf->ResolveTypeUID(uid);
    else
        return nullptr;
}

void
DWARFDIE::GetDeclContextDIEs (DWARFDIECollection &decl_context_dies) const
{
    if (IsValid())
    {
        DWARFDIE parent_decl_ctx_die = m_die->GetParentDeclContextDIE (GetDWARF(), GetCU());
        if (parent_decl_ctx_die && parent_decl_ctx_die.GetDIE() != GetDIE())
        {
            decl_context_dies.Append(parent_decl_ctx_die);
            parent_decl_ctx_die.GetDeclContextDIEs (decl_context_dies);
        }
    }
}

void
DWARFDIE::GetDWARFDeclContext (DWARFDeclContext &dwarf_decl_ctx) const
{
    if (IsValid())
    {
        m_die->GetDWARFDeclContext (GetDWARF(), GetCU(), dwarf_decl_ctx);
    }
    else
    {
        dwarf_decl_ctx.Clear();
    }
}

void
DWARFDIE::GetDWOContext (std::vector<CompilerContext> &context) const
{
    const dw_tag_t tag = Tag();
    if (tag == DW_TAG_compile_unit)
        return;
    DWARFDIE parent = GetParent();
    if (parent)
        parent.GetDWOContext(context);
    switch (tag)
    {
        case DW_TAG_module:
            context.push_back(CompilerContext(CompilerContextKind::Module, ConstString(GetName())));
            break;
        case DW_TAG_namespace:
            context.push_back(CompilerContext(CompilerContextKind::Namespace, ConstString(GetName())));
            break;
        case DW_TAG_structure_type:
            context.push_back(CompilerContext(CompilerContextKind::Structure, ConstString(GetName())));
            break;
        case DW_TAG_union_type:
            context.push_back(CompilerContext(CompilerContextKind::Union, ConstString(GetName())));
            break;
        case DW_TAG_class_type:
            context.push_back(CompilerContext(CompilerContextKind::Class, ConstString(GetName())));
            break;
        case DW_TAG_enumeration_type:
            context.push_back(CompilerContext(CompilerContextKind::Enumeration, ConstString(GetName())));
            break;
        case DW_TAG_subprogram:
            context.push_back(CompilerContext(CompilerContextKind::Function, ConstString(GetPubname())));
            break;
        case DW_TAG_variable:
            context.push_back(CompilerContext(CompilerContextKind::Variable, ConstString(GetPubname())));
            break;
        case DW_TAG_typedef:
            context.push_back(CompilerContext(CompilerContextKind::Typedef, ConstString(GetName())));
            break;
        default:
            break;
    }
}



DWARFDIE
DWARFDIE::GetParentDeclContextDIE () const
{
    if (IsValid())
        return m_die->GetParentDeclContextDIE(GetDWARF(), m_cu);
    else
        return DWARFDIE();
}


dw_offset_t
DWARFDIE::GetOffset () const
{
    if (IsValid())
        return m_die->GetOffset();
    else
        return DW_INVALID_OFFSET;
}

dw_offset_t
DWARFDIE::GetCompileUnitRelativeOffset () const
{
    if (IsValid())
        return m_die->GetOffset() - m_cu->GetOffset();
    else
        return DW_INVALID_OFFSET;
}

SymbolFileDWARF *
DWARFDIE::GetDWARF () const
{
    if (m_cu)
        return m_cu->GetSymbolFileDWARF();
    else
        return nullptr;
}

lldb_private::TypeSystem *
DWARFDIE::GetTypeSystem () const
{
    if (m_cu)
        return m_cu->GetTypeSystem();
    else
        return nullptr;
}

DWARFASTParser *
DWARFDIE::GetDWARFParser () const
{
    lldb_private::TypeSystem *type_system = GetTypeSystem ();
    if (type_system)
        return type_system->GetDWARFParser();
    else
        return nullptr;
}

bool
DWARFDIE::IsStructOrClass () const
{
    const dw_tag_t tag = Tag();
    return tag == DW_TAG_class_type || tag == DW_TAG_structure_type;
}


DWARFDIE
DWARFDIE::GetContainingDWOModuleDIE () const
{
    if (IsValid())
    {
        DWARFDIE top_module_die;
        // Now make sure this DIE is scoped in a DW_TAG_module tag and return true if so
        for (DWARFDIE parent = GetParent(); parent.IsValid(); parent = parent.GetParent())
        {
            const dw_tag_t tag = parent.Tag();
            if (tag == DW_TAG_module)
                top_module_die = parent;
            else if (tag == DW_TAG_compile_unit)
                break;
        }

        return top_module_die;
    }
    return DWARFDIE();
}

lldb::ModuleSP
DWARFDIE::GetContainingDWOModule () const
{
    if (IsValid())
    {
        DWARFDIE dwo_module_die = GetContainingDWOModuleDIE();

        if (dwo_module_die)
        {
            const char *module_name = dwo_module_die.GetName();
            if (module_name)
                return GetDWARF()->GetDWOModule (lldb_private::ConstString(module_name));
        }
    }
    return lldb::ModuleSP();
}

bool
DWARFDIE::HasChildren () const
{
    if (m_die)
        return m_die->HasChildren();
    else
        return false;
}

bool
DWARFDIE::Supports_DW_AT_APPLE_objc_complete_type () const
{
    if (IsValid())
        return GetDWARF()->Supports_DW_AT_APPLE_objc_complete_type(m_cu);
    else
        return false;
}

size_t
DWARFDIE::GetAttributes (DWARFAttributes &attributes, uint32_t depth) const
{
    if (IsValid())
    {
        return m_die->GetAttributes (m_cu,
                                     m_cu->GetFixedFormSizes(),
                                     attributes,
                                     depth);
    }
    if (depth == 0)
        attributes.Clear();
    return 0;
}


bool
DWARFDIE::GetDIENamesAndRanges (const char * &name,
                                const char * &mangled,
                                DWARFRangeList& ranges,
                                int& decl_file,
                                int& decl_line,
                                int& decl_column,
                                int& call_file,
                                int& call_line,
                                int& call_column,
                                lldb_private::DWARFExpression *frame_base) const
{
    if (IsValid())
    {
        return m_die->GetDIENamesAndRanges (GetDWARF(),
                                            GetCU(),
                                            name,
                                            mangled,
                                            ranges,
                                            decl_file,
                                            decl_line,
                                            decl_column,
                                            call_file,
                                            call_line,
                                            call_column,
                                            frame_base);
    }
    else
        return false;
}

void
DWARFDIE::Dump (lldb_private::Stream *s, const uint32_t recurse_depth) const
{
    if (s && IsValid())
        m_die->Dump (GetDWARF(), GetCU(), *s, recurse_depth);
}


bool operator == (const DWARFDIE &lhs, const DWARFDIE &rhs)
{
    return lhs.GetDIE() == rhs.GetDIE() && lhs.GetCU() == rhs.GetCU();
}

bool operator != (const DWARFDIE &lhs, const DWARFDIE &rhs)
{
    return lhs.GetDIE() != rhs.GetDIE() || lhs.GetCU() != rhs.GetCU();
}