summaryrefslogtreecommitdiff
path: root/llvm/lib/DebugInfo/LogicalView/Core/LVSymbol.cpp
blob: 7e86a2f4c1b8a945e0348bb2b763dc2b88db47cf (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
//===-- LVSymbol.cpp ------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This implements the LVSymbol class.
//
//===----------------------------------------------------------------------===//

#include "llvm/DebugInfo/LogicalView/Core/LVSymbol.h"
#include "llvm/DebugInfo/LogicalView/Core/LVCompare.h"
#include "llvm/DebugInfo/LogicalView/Core/LVLocation.h"
#include "llvm/DebugInfo/LogicalView/Core/LVReader.h"
#include "llvm/DebugInfo/LogicalView/Core/LVScope.h"

using namespace llvm;
using namespace llvm::logicalview;

#define DEBUG_TYPE "Symbol"

namespace {
const char *const KindCallSiteParameter = "CallSiteParameter";
const char *const KindConstant = "Constant";
const char *const KindInherits = "Inherits";
const char *const KindMember = "Member";
const char *const KindParameter = "Parameter";
const char *const KindUndefined = "Undefined";
const char *const KindUnspecified = "Unspecified";
const char *const KindVariable = "Variable";
} // end anonymous namespace

// Return a string representation for the symbol kind.
const char *LVSymbol::kind() const {
  const char *Kind = KindUndefined;
  if (getIsCallSiteParameter())
    Kind = KindCallSiteParameter;
  else if (getIsConstant())
    Kind = KindConstant;
  else if (getIsInheritance())
    Kind = KindInherits;
  else if (getIsMember())
    Kind = KindMember;
  else if (getIsParameter())
    Kind = KindParameter;
  else if (getIsUnspecified())
    Kind = KindUnspecified;
  else if (getIsVariable())
    Kind = KindVariable;
  return Kind;
}

LVSymbolDispatch LVSymbol::Dispatch = {
    {LVSymbolKind::IsCallSiteParameter, &LVSymbol::getIsCallSiteParameter},
    {LVSymbolKind::IsConstant, &LVSymbol::getIsConstant},
    {LVSymbolKind::IsInheritance, &LVSymbol::getIsInheritance},
    {LVSymbolKind::IsMember, &LVSymbol::getIsMember},
    {LVSymbolKind::IsParameter, &LVSymbol::getIsParameter},
    {LVSymbolKind::IsUnspecified, &LVSymbol::getIsUnspecified},
    {LVSymbolKind::IsVariable, &LVSymbol::getIsVariable}};

// Add a Location Entry.
void LVSymbol::addLocation(dwarf::Attribute Attr, LVAddress LowPC,
                           LVAddress HighPC, LVUnsigned SectionOffset,
                           uint64_t LocDescOffset, bool CallSiteLocation) {
  if (!Locations)
    Locations = std::make_unique<LVLocations>();

  // Create the location entry.
  CurrentLocation = getReader().createLocationSymbol();
  CurrentLocation->setParent(this);
  CurrentLocation->setAttr(Attr);
  if (CallSiteLocation)
    CurrentLocation->setIsCallSite();
  CurrentLocation->addObject(LowPC, HighPC, SectionOffset, LocDescOffset);
  Locations->push_back(CurrentLocation);

  // Mark the symbol as having location information.
  setHasLocation();
}

// Add a Location Record.
void LVSymbol::addLocationOperands(LVSmall Opcode, uint64_t Operand1,
                                   uint64_t Operand2) {
  if (CurrentLocation)
    CurrentLocation->addObject(Opcode, Operand1, Operand2);
}

// Add a Location Entry.
void LVSymbol::addLocationConstant(dwarf::Attribute Attr, LVUnsigned Constant,
                                   uint64_t LocDescOffset) {
  // Create a Location Entry, with the global information.
  addLocation(Attr,
              /*LowPC=*/0, /*HighPC=*/-1,
              /*SectionOffset=*/0, LocDescOffset);

  // Add records to Location Entry.
  addLocationOperands(/*Opcode=*/LVLocationMemberOffset,
                      /*Operand1=*/Constant, /*Operand2=*/0);
}

LVLocations::iterator LVSymbol::addLocationGap(LVLocations::iterator Pos,
                                               LVAddress LowPC,
                                               LVAddress HighPC) {
  // Create a location entry for the gap.
  LVLocation *Gap = getReader().createLocationSymbol();
  Gap->setParent(this);
  Gap->setAttr(dwarf::DW_AT_location);
  Gap->addObject(LowPC, HighPC,
                 /*section_offset=*/0,
                 /*locdesc_offset=*/0);

  LVLocations::iterator Iter = Locations->insert(Pos, Gap);

  // Add gap to Location Entry.
  Gap->addObject(/*op=*/dwarf::DW_OP_hi_user,
                 /*opd1=*/0, /*opd2=*/0);

  // Mark the entry as a gap.
  Gap->setIsGapEntry();

  return Iter;
}

void LVSymbol::fillLocationGaps() {
  // The symbol has locations records. Fill gaps in the location list.
  if (!getHasLocation() || !getFillGaps())
    return;

  // Get the parent range information and add dummy location entries.
  const LVLocations *Ranges = getParentScope()->getRanges();
  if (!Ranges)
    return;

  for (const LVLocation *Entry : *Ranges) {
    LVAddress ParentLowPC = Entry->getLowerAddress();
    LVAddress ParentHighPC = Entry->getUpperAddress();

    // Traverse the symbol locations and for each location contained in
    // the current parent range, insert locations for any existing gap.
    LVLocation *Location;
    LVAddress LowPC = 0;
    LVAddress Marker = ParentLowPC;
    for (LVLocations::iterator Iter = Locations->begin();
         Iter != Locations->end(); ++Iter) {
      Location = *Iter;
      LowPC = Location->getLowerAddress();
      if (LowPC != Marker) {
        // We have a gap at [Marker,LowPC - 1].
        Iter = addLocationGap(Iter, Marker, LowPC - 1);
        ++Iter;
      }

      // Move to the next item in the location list.
      Marker = Location->getUpperAddress() + 1;
    }

    // Check any gap at the end.
    if (Marker < ParentHighPC)
      // We have a gap at [Marker,ParentHighPC].
      addLocationGap(Locations->end(), Marker, ParentHighPC);
  }
}

// Get all the locations based on the valid function.
void LVSymbol::getLocations(LVLocations &LocationList,
                            LVValidLocation ValidLocation, bool RecordInvalid) {
  if (!Locations)
    return;

  for (LVLocation *Location : *Locations) {
    // Add the invalid location object.
    if (!(Location->*ValidLocation)() && RecordInvalid)
      LocationList.push_back(Location);
  }

  // Calculate coverage factor.
  calculateCoverage();
}

void LVSymbol::getLocations(LVLocations &LocationList) const {
  if (!Locations)
    return;

  for (LVLocation *Location : *Locations)
    LocationList.push_back(Location);
}

// Calculate coverage factor.
void LVSymbol::calculateCoverage() {
  if (!LVLocation::calculateCoverage(Locations.get(), CoverageFactor,
                                     CoveragePercentage)) {
    LVScope *Parent = getParentScope();
    if (Parent->getIsInlinedFunction()) {
      // For symbols representing the inlined function parameters and its
      // variables, get the outer most parent that contains their location
      // lower address.
      // The symbol can have a set of non-contiguous locations. We are using
      // only the first location entry to get the outermost parent.
      // If no scope contains the location, assume its enclosing parent.
      LVScope *Scope =
          Parent->outermostParent(Locations->front()->getLowerAddress());
      if (Scope)
        Parent = Scope;
    }
    unsigned CoverageParent = Parent->getCoverageFactor();
    // Get a percentage rounded to two decimal digits. This avoids
    // implementation-defined rounding inside printing functions.
    CoveragePercentage =
        CoverageParent
            ? rint((double(CoverageFactor) / CoverageParent) * 100.0 * 100.0) /
                  100.0
            : 0;
    // Record invalid coverage entry.
    if (options().getWarningCoverages() && CoveragePercentage > 100)
      getReaderCompileUnit()->addInvalidCoverage(this);
  }
}

void LVSymbol::resolveName() {
  if (getIsResolvedName())
    return;
  setIsResolvedName();

  LVElement::resolveName();

  // Resolve any given pattern.
  patterns().resolvePatternMatch(this);
}

void LVSymbol::resolveReferences() {
  // The symbols can have the following references to other elements:
  //   A Type:
  //     DW_AT_type             ->  Type or Scope
  //     DW_AT_import           ->  Type
  //   A Reference:
  //     DW_AT_specification    ->  Symbol
  //     DW_AT_abstract_origin  ->  Symbol
  //     DW_AT_extension        ->  Symbol

  // Resolve any referenced symbol.
  LVSymbol *Reference = getReference();
  if (Reference) {
    Reference->resolve();
    // Recursively resolve the symbol names.
    resolveReferencesChain();
  }

  // Set the file/line information using the Debug Information entry.
  setFile(Reference);

  // Resolve symbol type.
  if (LVElement *Element = getType()) {
    Element->resolve();

    // In the case of demoted typedefs, use the underlying type.
    if (Element->getIsTypedefReduced()) {
      Element = Element->getType();
      Element->resolve();
    }

    // If the type is a template parameter, get its type, which can
    // point to a type or scope, depending on the argument instance.
    setGenericType(Element);
  }

  // Resolve the variable associated type.
  if (!getType() && Reference)
    setType(Reference->getType());
}

StringRef LVSymbol::resolveReferencesChain() {
  // If the symbol have a DW_AT_specification or DW_AT_abstract_origin,
  // follow the chain to resolve the name from those references.
  if (getHasReference() && !isNamed())
    setName(getReference()->resolveReferencesChain());

  return getName();
}

void LVSymbol::markMissingParents(const LVSymbols *References,
                                  const LVSymbols *Targets) {
  if (!(References && Targets))
    return;

  LLVM_DEBUG({
    dbgs() << "\n[LVSymbol::markMissingParents]\n";
    for (const LVSymbol *Reference : *References)
      dbgs() << "References: "
             << "Kind = " << formattedKind(Reference->kind()) << ", "
             << "Name = " << formattedName(Reference->getName()) << "\n";
    for (const LVSymbol *Target : *Targets)
      dbgs() << "Targets   : "
             << "Kind = " << formattedKind(Target->kind()) << ", "
             << "Name = " << formattedName(Target->getName()) << "\n";
  });

  for (LVSymbol *Reference : *References) {
    LLVM_DEBUG({
      dbgs() << "Search Reference: Name = "
             << formattedName(Reference->getName()) << "\n";
    });
    if (!Reference->findIn(Targets))
      Reference->markBranchAsMissing();
  }
}

LVSymbol *LVSymbol::findIn(const LVSymbols *Targets) const {
  if (!Targets)
    return nullptr;

  LLVM_DEBUG({
    dbgs() << "\n[LVSymbol::findIn]\n"
           << "Reference: "
           << "Level = " << getLevel() << ", "
           << "Kind = " << formattedKind(kind()) << ", "
           << "Name = " << formattedName(getName()) << "\n";
    for (const LVSymbol *Target : *Targets)
      dbgs() << "Target   : "
             << "Level = " << Target->getLevel() << ", "
             << "Kind = " << formattedKind(Target->kind()) << ", "
             << "Name = " << formattedName(Target->getName()) << "\n";
  });

  for (LVSymbol *Target : *Targets)
    if (equals(Target))
      return Target;

  return nullptr;
}

// Check for a match on the arguments of a function.
bool LVSymbol::parametersMatch(const LVSymbols *References,
                               const LVSymbols *Targets) {
  if (!References && !Targets)
    return true;
  if (References && Targets) {
    LVSymbols ReferenceParams;
    getParameters(References, &ReferenceParams);
    LVSymbols TargetParams;
    getParameters(Targets, &TargetParams);
    return LVSymbol::equals(&ReferenceParams, &TargetParams);
  }
  return false;
}

// Return the symbols which are parameters.
void LVSymbol::getParameters(const LVSymbols *Symbols, LVSymbols *Parameters) {
  if (Symbols)
    for (LVSymbol *Symbol : *Symbols)
      if (Symbol->getIsParameter())
        Parameters->push_back(Symbol);
}

bool LVSymbol::equals(const LVSymbol *Symbol) const {
  if (!LVElement::equals(Symbol))
    return false;

  // Check if any reference is the same.
  if (!referenceMatch(Symbol))
    return false;

  if (getReference() && !getReference()->equals(Symbol->getReference()))
    return false;

  return true;
}

bool LVSymbol::equals(const LVSymbols *References, const LVSymbols *Targets) {
  if (!References && !Targets)
    return true;
  if (References && Targets && References->size() == Targets->size()) {
    for (const LVSymbol *Reference : *References)
      if (!Reference->findIn(Targets))
        return false;
    return true;
  }
  return false;
}

void LVSymbol::report(LVComparePass Pass) {
  getComparator().printItem(this, Pass);
}

void LVSymbol::printLocations(raw_ostream &OS, bool Full) const {
  if (Locations)
    for (const LVLocation *Location : *Locations)
      Location->printRaw(OS, Full);
}

void LVSymbol::print(raw_ostream &OS, bool Full) const {
  if (getIncludeInPrint() && getReader().doPrintSymbol(this)) {
    getReaderCompileUnit()->incrementPrintedSymbols();
    LVElement::print(OS, Full);
    printExtra(OS, Full);
  }
}

void LVSymbol::printExtra(raw_ostream &OS, bool Full) const {
  // Accessibility depends on the parent (class, structure).
  uint32_t AccessCode = 0;
  if (getIsMember() || getIsInheritance())
    AccessCode = getParentScope()->getIsClass() ? dwarf::DW_ACCESS_private
                                                : dwarf::DW_ACCESS_public;

  const LVSymbol *Symbol = getIsInlined() ? Reference : this;
  std::string Attributes =
      Symbol->getIsCallSiteParameter()
          ? ""
          : formatAttributes(Symbol->externalString(),
                             Symbol->accessibilityString(AccessCode),
                             virtualityString());

  OS << formattedKind(Symbol->kind()) << " " << Attributes;
  if (Symbol->getIsUnspecified())
    OS << formattedName(Symbol->getName());
  else {
    if (Symbol->getIsInheritance())
      OS << Symbol->typeOffsetAsString()
         << formattedNames(Symbol->getTypeQualifiedName(),
                           Symbol->typeAsString());
    else {
      OS << formattedName(Symbol->getName());
      // Print any bitfield information.
      if (uint32_t Size = getBitSize())
        OS << ":" << Size;
      OS << " -> " << Symbol->typeOffsetAsString()
         << formattedNames(Symbol->getTypeQualifiedName(),
                           Symbol->typeAsString());
    }
  }

  // Print any initial value if any.
  if (ValueIndex)
    OS << " = " << formattedName(getValue());
  OS << "\n";

  if (Full && options().getPrintFormatting()) {
    if (getLinkageNameIndex())
      printLinkageName(OS, Full, const_cast<LVSymbol *>(this));
    if (LVSymbol *Reference = getReference())
      Reference->printReference(OS, Full, const_cast<LVSymbol *>(this));

    // Print location information.
    LVLocation::print(Locations.get(), OS, Full);
  }
}