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
|
//===-- Function.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/Function.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Section.h"
#include "lldb/Symbol/ClangASTType.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/LineTable.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "clang/AST/Type.h"
#include "clang/AST/CanonicalType.h"
using namespace lldb_private;
//----------------------------------------------------------------------
// Basic function information is contained in the FunctionInfo class.
// It is designed to contain the name, linkage name, and declaration
// location.
//----------------------------------------------------------------------
FunctionInfo::FunctionInfo (const char *name, const Declaration *decl_ptr) :
m_name(name),
m_declaration(decl_ptr)
{
}
FunctionInfo::FunctionInfo (const ConstString& name, const Declaration *decl_ptr) :
m_name(name),
m_declaration(decl_ptr)
{
}
FunctionInfo::~FunctionInfo()
{
}
void
FunctionInfo::Dump(Stream *s) const
{
if (m_name)
*s << ", name = \"" << m_name << "\"";
m_declaration.Dump(s);
}
int
FunctionInfo::Compare(const FunctionInfo& a, const FunctionInfo& b)
{
int result = ConstString::Compare(a.GetName(), b.GetName());
if (result)
return result;
return Declaration::Compare(a.m_declaration, b.m_declaration);
}
Declaration&
FunctionInfo::GetDeclaration()
{
return m_declaration;
}
const Declaration&
FunctionInfo::GetDeclaration() const
{
return m_declaration;
}
const ConstString&
FunctionInfo::GetName() const
{
return m_name;
}
size_t
FunctionInfo::MemorySize() const
{
return m_name.MemorySize() + m_declaration.MemorySize();
}
InlineFunctionInfo::InlineFunctionInfo
(
const char *name,
const char *mangled,
const Declaration *decl_ptr,
const Declaration *call_decl_ptr
) :
FunctionInfo(name, decl_ptr),
m_mangled(mangled, true),
m_call_decl (call_decl_ptr)
{
}
InlineFunctionInfo::InlineFunctionInfo
(
const ConstString& name,
const Mangled &mangled,
const Declaration *decl_ptr,
const Declaration *call_decl_ptr
) :
FunctionInfo(name, decl_ptr),
m_mangled(mangled),
m_call_decl (call_decl_ptr)
{
}
InlineFunctionInfo::~InlineFunctionInfo()
{
}
int
InlineFunctionInfo::Compare(const InlineFunctionInfo& a, const InlineFunctionInfo& b)
{
int result = FunctionInfo::Compare(a, b);
if (result)
return result;
// only compare the mangled names if both have them
return Mangled::Compare(a.m_mangled, a.m_mangled);
}
void
InlineFunctionInfo::Dump(Stream *s) const
{
FunctionInfo::Dump(s);
if (m_mangled)
m_mangled.Dump(s);
}
void
InlineFunctionInfo::DumpStopContext (Stream *s) const
{
// s->Indent("[inlined] ");
s->Indent();
if (m_mangled)
s->PutCString (m_mangled.GetName().AsCString());
else
s->PutCString (m_name.AsCString());
}
Declaration &
InlineFunctionInfo::GetCallSite ()
{
return m_call_decl;
}
const Declaration &
InlineFunctionInfo::GetCallSite () const
{
return m_call_decl;
}
Mangled&
InlineFunctionInfo::GetMangled()
{
return m_mangled;
}
const Mangled&
InlineFunctionInfo::GetMangled() const
{
return m_mangled;
}
size_t
InlineFunctionInfo::MemorySize() const
{
return FunctionInfo::MemorySize() + m_mangled.MemorySize();
}
//----------------------------------------------------------------------
//
//----------------------------------------------------------------------
Function::Function
(
CompileUnit *comp_unit,
lldb::user_id_t func_uid,
lldb::user_id_t type_uid,
const Mangled &mangled,
Type * type,
const AddressRange& range
) :
UserID(func_uid),
m_comp_unit(comp_unit),
m_type_uid(type_uid),
m_type(type),
m_mangled(mangled),
m_blocks(this, range),
m_frame_base(),
m_flags(),
m_prologue_byte_size(0)
{
assert(comp_unit != NULL);
}
Function::Function
(
CompileUnit *comp_unit,
lldb::user_id_t func_uid,
lldb::user_id_t type_uid,
const char *mangled,
Type *type,
const AddressRange &range
) :
UserID(func_uid),
m_comp_unit(comp_unit),
m_type_uid(type_uid),
m_type(type),
m_mangled(mangled, true),
m_blocks(this, range),
m_frame_base(),
m_flags(),
m_prologue_byte_size(0)
{
assert(comp_unit != NULL);
}
Function::~Function()
{
}
const AddressRange &
Function::GetAddressRange()
{
return GetBlocks(true).GetAddressRange();
}
bool
Function::IsInlined()
{
Block *root_block = GetBlocks(true).GetBlockByID(Block::RootID);
if (root_block)
{
if (root_block->InlinedFunctionInfo() != NULL)
return true;
}
return false;
}
void
Function::GetStartLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
{
line_no = 0;
source_file.Clear();
if (m_comp_unit == NULL)
return;
if (m_type != NULL && m_type->GetDeclaration().GetLine() != 0)
{
source_file = m_type->GetDeclaration().GetFile();
line_no = m_type->GetDeclaration().GetLine();
}
else
{
LineTable *line_table = m_comp_unit->GetLineTable();
if (line_table == NULL)
return;
LineEntry line_entry;
if (line_table->FindLineEntryByAddress (GetAddressRange().GetBaseAddress(), line_entry, NULL))
{
line_no = line_entry.line;
source_file = line_entry.file;
}
}
}
void
Function::GetEndLineSourceInfo (FileSpec &source_file, uint32_t &line_no)
{
line_no = 0;
source_file.Clear();
// The -1 is kind of cheesy, but I want to get the last line entry for the given function, not the
// first entry of the next.
Address scratch_addr(GetAddressRange().GetBaseAddress());
scratch_addr.SetOffset (scratch_addr.GetOffset() + GetAddressRange().GetByteSize() - 1);
LineTable *line_table = m_comp_unit->GetLineTable();
if (line_table == NULL)
return;
LineEntry line_entry;
if (line_table->FindLineEntryByAddress (scratch_addr, line_entry, NULL))
{
line_no = line_entry.line;
source_file = line_entry.file;
}
}
BlockList &
Function::GetBlocks(bool can_create)
{
if (m_blocks.IsEmpty() && can_create)
{
SymbolContext sc;
CalculateSymbolContext(&sc);
assert(sc.module_sp);
sc.module_sp->GetSymbolVendor()->ParseFunctionBlocks(sc);
}
return m_blocks;
}
CompileUnit*
Function::GetCompileUnit()
{
return m_comp_unit;
}
const CompileUnit*
Function::GetCompileUnit() const
{
return m_comp_unit;
}
void
Function::GetDescription(Stream *s, lldb::DescriptionLevel level, Process *process)
{
Type* func_type = GetType();
*s << '"' << func_type->GetName() << "\", id = " << (const UserID&)*this;
*s << ", range = ";
GetAddressRange().Dump(s, process, Address::DumpStyleLoadAddress, Address::DumpStyleModuleWithFileAddress);
}
void
Function::Dump(Stream *s, bool show_context) const
{
s->Printf("%.*p: ", (int)sizeof(void*) * 2, this);
s->Indent();
*s << "Function" << (const UserID&)*this;
m_mangled.Dump(s);
// FunctionInfo::Dump(s);
if (m_type)
{
*s << ", type = " << (void*)m_type;
/// << " (";
///m_type->DumpTypeName(s);
///s->PutChar(')');
}
else if (m_type_uid != LLDB_INVALID_UID)
*s << ", type_uid = " << m_type_uid;
s->EOL();
// Dump the root object
if (!m_blocks.IsEmpty())
m_blocks.Dump(s, Block::RootID, INT_MAX, show_context);
}
void
Function::CalculateSymbolContext(SymbolContext* sc)
{
sc->function = this;
m_comp_unit->CalculateSymbolContext(sc);
}
void
Function::DumpSymbolContext(Stream *s)
{
m_comp_unit->DumpSymbolContext(s);
s->Printf(", Function{0x%8.8x}", GetID());
}
size_t
Function::MemorySize () const
{
size_t mem_size = sizeof(Function) + m_blocks.MemorySize();
return mem_size;
}
Type*
Function::GetType()
{
return m_type;
}
const Type*
Function::GetType() const
{
return m_type;
}
Type
Function::GetReturnType ()
{
clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType()));
assert (clang_type->isFunctionType());
clang::FunctionType *function_type = dyn_cast<clang::FunctionType> (clang_type);
clang::QualType fun_return_qualtype = function_type->getResultType();
const ConstString fun_return_name(ClangASTType::GetClangTypeName(fun_return_qualtype.getAsOpaquePtr()));
SymbolContext sc;
CalculateSymbolContext (&sc);
// Null out everything below the CompUnit 'cause we don't actually know these.
size_t bit_size = ClangASTType::GetClangTypeBitWidth ((GetType()->GetClangASTContext().getASTContext()), fun_return_qualtype.getAsOpaquePtr());
Type return_type (0, GetType()->GetSymbolFile(), fun_return_name, bit_size, sc.comp_unit, 0, Type::eTypeUIDSynthetic, Declaration(), fun_return_qualtype.getAsOpaquePtr());
return return_type;
}
int
Function::GetArgumentCount ()
{
clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType()));
assert (clang_type->isFunctionType());
if (!clang_type->isFunctionProtoType())
return -1;
const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type);
if (function_proto_type != NULL)
return function_proto_type->getNumArgs();
return 0;
}
const Type
Function::GetArgumentTypeAtIndex (size_t idx)
{
clang::QualType clang_type (clang::QualType::getFromOpaquePtr(GetType()->GetOpaqueClangQualType()));
assert (clang_type->isFunctionType());
if (!clang_type->isFunctionProtoType())
return Type();
const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type);
if (function_proto_type != NULL)
{
unsigned num_args = function_proto_type->getNumArgs();
if (idx >= num_args)
return Type();
clang::QualType arg_qualtype = (function_proto_type->arg_type_begin())[idx];
const ConstString arg_return_name(ClangASTType::GetClangTypeName(arg_qualtype.getAsOpaquePtr()));
SymbolContext sc;
CalculateSymbolContext (&sc);
// Null out everything below the CompUnit 'cause we don't actually know these.
size_t bit_size = ClangASTType::GetClangTypeBitWidth ((GetType()->GetClangASTContext().getASTContext()), arg_qualtype.getAsOpaquePtr());
Type arg_type (0, GetType()->GetSymbolFile(), arg_return_name, bit_size, sc.comp_unit, 0, Type::eTypeUIDSynthetic, Declaration(), arg_qualtype.getAsOpaquePtr());
return arg_type;
}
return Type();
}
const char *
Function::GetArgumentNameAtIndex (size_t idx)
{
clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetOpaqueClangQualType())->getTypePtr();
assert (clang_type->isFunctionType());
if (!clang_type->isFunctionProtoType())
return NULL;
return NULL;
}
bool
Function::IsVariadic ()
{
const clang::Type *clang_type = static_cast<clang::QualType *>(GetType()->GetOpaqueClangQualType())->getTypePtr();
assert (clang_type->isFunctionType());
if (!clang_type->isFunctionProtoType())
return false;
const clang::FunctionProtoType *function_proto_type = dyn_cast<clang::FunctionProtoType>(clang_type);
if (function_proto_type != NULL)
{
return function_proto_type->isVariadic();
}
return false;
}
uint32_t
Function::GetPrologueByteSize ()
{
if (m_prologue_byte_size == 0 && m_flags.IsClear(flagsCalculatedPrologueSize))
{
m_flags.Set(flagsCalculatedPrologueSize);
LineTable* line_table = m_comp_unit->GetLineTable ();
if (line_table)
{
LineEntry line_entry;
if (line_table->FindLineEntryByAddress(GetAddressRange().GetBaseAddress(), line_entry))
m_prologue_byte_size = line_entry.range.GetByteSize();
}
}
return m_prologue_byte_size;
}
|