LLVM  9.0.0svn
BTFDebug.cpp
Go to the documentation of this file.
1 //===- BTFDebug.cpp - BTF Generator ---------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains support for writing BTF debug info.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "BTFDebug.h"
14 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCSectionELF.h"
20 #include "llvm/MC/MCStreamer.h"
22 
23 using namespace llvm;
24 
25 static const char *BTFKindStr[] = {
26 #define HANDLE_BTF_KIND(ID, NAME) "BTF_KIND_" #NAME,
27 #include "BTF.def"
28 };
29 
30 /// Emit a BTF common type.
32  OS.AddComment(std::string(BTFKindStr[Kind]) + "(id = " + std::to_string(Id) +
33  ")");
36  OS.EmitIntValue(BTFType.Info, 4);
37  OS.EmitIntValue(BTFType.Size, 4);
38 }
39 
41  : DTy(DTy) {
42  switch (Tag) {
43  case dwarf::DW_TAG_pointer_type:
44  Kind = BTF::BTF_KIND_PTR;
45  break;
46  case dwarf::DW_TAG_const_type:
47  Kind = BTF::BTF_KIND_CONST;
48  break;
49  case dwarf::DW_TAG_volatile_type:
50  Kind = BTF::BTF_KIND_VOLATILE;
51  break;
52  case dwarf::DW_TAG_typedef:
53  Kind = BTF::BTF_KIND_TYPEDEF;
54  break;
55  case dwarf::DW_TAG_restrict_type:
56  Kind = BTF::BTF_KIND_RESTRICT;
57  break;
58  default:
59  llvm_unreachable("Unknown DIDerivedType Tag");
60  }
61  BTFType.Info = Kind << 24;
62 }
63 
65  BTFType.NameOff = BDebug.addString(DTy->getName());
66 
67  // The base type for PTR/CONST/VOLATILE could be void.
68  const DIType *ResolvedType = DTy->getBaseType();
69  if (!ResolvedType) {
70  assert((Kind == BTF::BTF_KIND_PTR || Kind == BTF::BTF_KIND_CONST ||
71  Kind == BTF::BTF_KIND_VOLATILE) &&
72  "Invalid null basetype");
73  BTFType.Type = 0;
74  } else {
75  BTFType.Type = BDebug.getTypeId(ResolvedType);
76  }
77 }
78 
80 
81 /// Represent a struct/union forward declaration.
82 BTFTypeFwd::BTFTypeFwd(StringRef Name, bool IsUnion) : Name(Name) {
83  Kind = BTF::BTF_KIND_FWD;
84  BTFType.Info = IsUnion << 31 | Kind << 24;
85  BTFType.Type = 0;
86 }
87 
89  BTFType.NameOff = BDebug.addString(Name);
90 }
91 
93 
95  uint32_t OffsetInBits, StringRef TypeName)
96  : Name(TypeName) {
97  // Translate IR int encoding to BTF int encoding.
98  uint8_t BTFEncoding;
99  switch (Encoding) {
100  case dwarf::DW_ATE_boolean:
101  BTFEncoding = BTF::INT_BOOL;
102  break;
103  case dwarf::DW_ATE_signed:
104  case dwarf::DW_ATE_signed_char:
105  BTFEncoding = BTF::INT_SIGNED;
106  break;
107  case dwarf::DW_ATE_unsigned:
108  case dwarf::DW_ATE_unsigned_char:
109  BTFEncoding = 0;
110  break;
111  default:
112  llvm_unreachable("Unknown BTFTypeInt Encoding");
113  }
114 
115  Kind = BTF::BTF_KIND_INT;
116  BTFType.Info = Kind << 24;
117  BTFType.Size = roundupToBytes(SizeInBits);
118  IntVal = (BTFEncoding << 24) | OffsetInBits << 16 | SizeInBits;
119 }
120 
122  BTFType.NameOff = BDebug.addString(Name);
123 }
124 
127  OS.AddComment("0x" + Twine::utohexstr(IntVal));
128  OS.EmitIntValue(IntVal, 4);
129 }
130 
131 BTFTypeEnum::BTFTypeEnum(const DICompositeType *ETy, uint32_t VLen) : ETy(ETy) {
132  Kind = BTF::BTF_KIND_ENUM;
133  BTFType.Info = Kind << 24 | VLen;
135 }
136 
138  BTFType.NameOff = BDebug.addString(ETy->getName());
139 
140  DINodeArray Elements = ETy->getElements();
141  for (const auto Element : Elements) {
142  const auto *Enum = cast<DIEnumerator>(Element);
143 
144  struct BTF::BTFEnum BTFEnum;
145  BTFEnum.NameOff = BDebug.addString(Enum->getName());
146  // BTF enum value is 32bit, enforce it.
147  BTFEnum.Val = static_cast<uint32_t>(Enum->getValue());
148  EnumValues.push_back(BTFEnum);
149  }
150 }
151 
154  for (const auto &Enum : EnumValues) {
155  OS.EmitIntValue(Enum.NameOff, 4);
156  OS.EmitIntValue(Enum.Val, 4);
157  }
158 }
159 
161  Kind = BTF::BTF_KIND_ARRAY;
162  BTFType.NameOff = 0;
163  BTFType.Info = Kind << 24;
164  BTFType.Size = 0;
165 
166  ArrayInfo.ElemType = ElemTypeId;
167  ArrayInfo.Nelems = NumElems;
168 }
169 
170 /// Represent a BTF array.
172 
173  // The IR does not really have a type for the index.
174  // A special type for array index should have been
175  // created during initial type traversal. Just
176  // retrieve that type id.
177  ArrayInfo.IndexType = BDebug.getArrayIndexTypeId();
178 }
179 
182  OS.EmitIntValue(ArrayInfo.ElemType, 4);
183  OS.EmitIntValue(ArrayInfo.IndexType, 4);
184  OS.EmitIntValue(ArrayInfo.Nelems, 4);
185 }
186 
187 /// Represent either a struct or a union.
189  bool HasBitField, uint32_t Vlen)
190  : STy(STy), HasBitField(HasBitField) {
191  Kind = IsStruct ? BTF::BTF_KIND_STRUCT : BTF::BTF_KIND_UNION;
193  BTFType.Info = (HasBitField << 31) | (Kind << 24) | Vlen;
194 }
195 
197  BTFType.NameOff = BDebug.addString(STy->getName());
198 
199  // Add struct/union members.
200  const DINodeArray Elements = STy->getElements();
201  for (const auto *Element : Elements) {
202  struct BTF::BTFMember BTFMember;
203  const auto *DDTy = cast<DIDerivedType>(Element);
204 
205  BTFMember.NameOff = BDebug.addString(DDTy->getName());
206  if (HasBitField) {
207  uint8_t BitFieldSize = DDTy->isBitField() ? DDTy->getSizeInBits() : 0;
208  BTFMember.Offset = BitFieldSize << 24 | DDTy->getOffsetInBits();
209  } else {
210  BTFMember.Offset = DDTy->getOffsetInBits();
211  }
212  BTFMember.Type = BDebug.getTypeId(DDTy->getBaseType());
213  Members.push_back(BTFMember);
214  }
215 }
216 
219  for (const auto &Member : Members) {
220  OS.EmitIntValue(Member.NameOff, 4);
221  OS.EmitIntValue(Member.Type, 4);
222  OS.AddComment("0x" + Twine::utohexstr(Member.Offset));
223  OS.EmitIntValue(Member.Offset, 4);
224  }
225 }
226 
227 /// The Func kind represents both subprogram and pointee of function
228 /// pointers. If the FuncName is empty, it represents a pointee of function
229 /// pointer. Otherwise, it represents a subprogram. The func arg names
230 /// are empty for pointee of function pointer case, and are valid names
231 /// for subprogram.
233  const DISubroutineType *STy, uint32_t VLen,
234  const std::unordered_map<uint32_t, StringRef> &FuncArgNames)
235  : STy(STy), FuncArgNames(FuncArgNames) {
236  Kind = BTF::BTF_KIND_FUNC_PROTO;
237  BTFType.Info = (Kind << 24) | VLen;
238 }
239 
241  DITypeRefArray Elements = STy->getTypeArray();
242  auto RetType = Elements[0];
243  BTFType.Type = RetType ? BDebug.getTypeId(RetType) : 0;
244  BTFType.NameOff = 0;
245 
246  // For null parameter which is typically the last one
247  // to represent the vararg, encode the NameOff/Type to be 0.
248  for (unsigned I = 1, N = Elements.size(); I < N; ++I) {
249  struct BTF::BTFParam Param;
250  auto Element = Elements[I];
251  if (Element) {
252  Param.NameOff = BDebug.addString(FuncArgNames[I]);
253  Param.Type = BDebug.getTypeId(Element);
254  } else {
255  Param.NameOff = 0;
256  Param.Type = 0;
257  }
258  Parameters.push_back(Param);
259  }
260 }
261 
264  for (const auto &Param : Parameters) {
265  OS.EmitIntValue(Param.NameOff, 4);
266  OS.EmitIntValue(Param.Type, 4);
267  }
268 }
269 
271  : Name(FuncName) {
272  Kind = BTF::BTF_KIND_FUNC;
273  BTFType.Info = Kind << 24;
274  BTFType.Type = ProtoTypeId;
275 }
276 
278  BTFType.NameOff = BDebug.addString(Name);
279 }
280 
282 
284  : Name(VarName) {
285  Kind = BTF::BTF_KIND_VAR;
286  BTFType.Info = Kind << 24;
287  BTFType.Type = TypeId;
288  Info = VarInfo;
289 }
290 
292  BTFType.NameOff = BDebug.addString(Name);
293 }
294 
297  OS.EmitIntValue(Info, 4);
298 }
299 
300 BTFKindDataSec::BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
301  : Asm(AsmPrt), Name(SecName) {
302  Kind = BTF::BTF_KIND_DATASEC;
303  BTFType.Info = Kind << 24;
304  BTFType.Size = 0;
305 }
306 
308  BTFType.NameOff = BDebug.addString(Name);
309  BTFType.Info |= Vars.size();
310 }
311 
314 
315  for (const auto &V : Vars) {
316  OS.EmitIntValue(std::get<0>(V), 4);
317  Asm->EmitLabelReference(std::get<1>(V), 4);
318  OS.EmitIntValue(std::get<2>(V), 4);
319  }
320 }
321 
323  // Check whether the string already exists.
324  for (auto &OffsetM : OffsetToIdMap) {
325  if (Table[OffsetM.second] == S)
326  return OffsetM.first;
327  }
328  // Not find, add to the string table.
329  uint32_t Offset = Size;
330  OffsetToIdMap[Offset] = Table.size();
331  Table.push_back(S);
332  Size += S.size() + 1;
333  return Offset;
334 }
335 
337  : DebugHandlerBase(AP), OS(*Asm->OutStreamer), SkipInstruction(false),
338  LineInfoGenerated(false), SecNameOff(0), ArrayIndexTypeId(0) {
339  addString("\0");
340 }
341 
342 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry,
343  const DIType *Ty) {
344  TypeEntry->setId(TypeEntries.size() + 1);
345  uint32_t Id = TypeEntry->getId();
346  DIToIdMap[Ty] = Id;
347  TypeEntries.push_back(std::move(TypeEntry));
348  return Id;
349 }
350 
351 uint32_t BTFDebug::addType(std::unique_ptr<BTFTypeBase> TypeEntry) {
352  TypeEntry->setId(TypeEntries.size() + 1);
353  uint32_t Id = TypeEntry->getId();
354  TypeEntries.push_back(std::move(TypeEntry));
355  return Id;
356 }
357 
358 void BTFDebug::visitBasicType(const DIBasicType *BTy, uint32_t &TypeId) {
359  // Only int types are supported in BTF.
360  uint32_t Encoding = BTy->getEncoding();
361  if (Encoding != dwarf::DW_ATE_boolean && Encoding != dwarf::DW_ATE_signed &&
362  Encoding != dwarf::DW_ATE_signed_char &&
363  Encoding != dwarf::DW_ATE_unsigned &&
364  Encoding != dwarf::DW_ATE_unsigned_char)
365  return;
366 
367  // Create a BTF type instance for this DIBasicType and put it into
368  // DIToIdMap for cross-type reference check.
369  auto TypeEntry = llvm::make_unique<BTFTypeInt>(
370  Encoding, BTy->getSizeInBits(), BTy->getOffsetInBits(), BTy->getName());
371  TypeId = addType(std::move(TypeEntry), BTy);
372 }
373 
374 /// Handle subprogram or subroutine types.
375 void BTFDebug::visitSubroutineType(
376  const DISubroutineType *STy, bool ForSubprog,
377  const std::unordered_map<uint32_t, StringRef> &FuncArgNames,
378  uint32_t &TypeId) {
379  DITypeRefArray Elements = STy->getTypeArray();
380  uint32_t VLen = Elements.size() - 1;
381  if (VLen > BTF::MAX_VLEN)
382  return;
383 
384  // Subprogram has a valid non-zero-length name, and the pointee of
385  // a function pointer has an empty name. The subprogram type will
386  // not be added to DIToIdMap as it should not be referenced by
387  // any other types.
388  auto TypeEntry = llvm::make_unique<BTFTypeFuncProto>(STy, VLen, FuncArgNames);
389  if (ForSubprog)
390  TypeId = addType(std::move(TypeEntry)); // For subprogram
391  else
392  TypeId = addType(std::move(TypeEntry), STy); // For func ptr
393 
394  // Visit return type and func arg types.
395  for (const auto Element : Elements) {
396  visitTypeEntry(Element);
397  }
398 }
399 
400 /// Handle structure/union types.
401 void BTFDebug::visitStructType(const DICompositeType *CTy, bool IsStruct,
402  uint32_t &TypeId) {
403  const DINodeArray Elements = CTy->getElements();
404  uint32_t VLen = Elements.size();
405  if (VLen > BTF::MAX_VLEN)
406  return;
407 
408  // Check whether we have any bitfield members or not
409  bool HasBitField = false;
410  for (const auto *Element : Elements) {
411  auto E = cast<DIDerivedType>(Element);
412  if (E->isBitField()) {
413  HasBitField = true;
414  break;
415  }
416  }
417 
418  auto TypeEntry =
419  llvm::make_unique<BTFTypeStruct>(CTy, IsStruct, HasBitField, VLen);
420  TypeId = addType(std::move(TypeEntry), CTy);
421 
422  // Visit all struct members.
423  for (const auto *Element : Elements)
424  visitTypeEntry(cast<DIDerivedType>(Element));
425 }
426 
427 void BTFDebug::visitArrayType(const DICompositeType *CTy, uint32_t &TypeId) {
428  // Visit array element type.
429  uint32_t ElemTypeId;
430  visitTypeEntry(CTy->getBaseType(), ElemTypeId);
431 
432  if (!CTy->getSizeInBits()) {
433  auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, 0);
434  ElemTypeId = addType(std::move(TypeEntry), CTy);
435  } else {
436  // Visit array dimensions.
437  DINodeArray Elements = CTy->getElements();
438  for (int I = Elements.size() - 1; I >= 0; --I) {
439  if (auto *Element = dyn_cast_or_null<DINode>(Elements[I]))
440  if (Element->getTag() == dwarf::DW_TAG_subrange_type) {
441  const DISubrange *SR = cast<DISubrange>(Element);
442  auto *CI = SR->getCount().dyn_cast<ConstantInt *>();
443  int64_t Count = CI->getSExtValue();
444 
445  auto TypeEntry = llvm::make_unique<BTFTypeArray>(ElemTypeId, Count);
446  if (I == 0)
447  ElemTypeId = addType(std::move(TypeEntry), CTy);
448  else
449  ElemTypeId = addType(std::move(TypeEntry));
450  }
451  }
452  }
453 
454  // The array TypeId is the type id of the outermost dimension.
455  TypeId = ElemTypeId;
456 
457  // The IR does not have a type for array index while BTF wants one.
458  // So create an array index type if there is none.
459  if (!ArrayIndexTypeId) {
460  auto TypeEntry = llvm::make_unique<BTFTypeInt>(dwarf::DW_ATE_unsigned, 32,
461  0, "__ARRAY_SIZE_TYPE__");
462  ArrayIndexTypeId = addType(std::move(TypeEntry));
463  }
464 }
465 
466 void BTFDebug::visitEnumType(const DICompositeType *CTy, uint32_t &TypeId) {
467  DINodeArray Elements = CTy->getElements();
468  uint32_t VLen = Elements.size();
469  if (VLen > BTF::MAX_VLEN)
470  return;
471 
472  auto TypeEntry = llvm::make_unique<BTFTypeEnum>(CTy, VLen);
473  TypeId = addType(std::move(TypeEntry), CTy);
474  // No need to visit base type as BTF does not encode it.
475 }
476 
477 /// Handle structure/union forward declarations.
478 void BTFDebug::visitFwdDeclType(const DICompositeType *CTy, bool IsUnion,
479  uint32_t &TypeId) {
480  auto TypeEntry = llvm::make_unique<BTFTypeFwd>(CTy->getName(), IsUnion);
481  TypeId = addType(std::move(TypeEntry), CTy);
482 }
483 
484 /// Handle structure, union, array and enumeration types.
485 void BTFDebug::visitCompositeType(const DICompositeType *CTy,
486  uint32_t &TypeId) {
487  auto Tag = CTy->getTag();
488  if (Tag == dwarf::DW_TAG_structure_type || Tag == dwarf::DW_TAG_union_type) {
489  // Handle forward declaration differently as it does not have members.
490  if (CTy->isForwardDecl())
491  visitFwdDeclType(CTy, Tag == dwarf::DW_TAG_union_type, TypeId);
492  else
493  visitStructType(CTy, Tag == dwarf::DW_TAG_structure_type, TypeId);
494  } else if (Tag == dwarf::DW_TAG_array_type)
495  visitArrayType(CTy, TypeId);
496  else if (Tag == dwarf::DW_TAG_enumeration_type)
497  visitEnumType(CTy, TypeId);
498 }
499 
500 /// Handle pointer, typedef, const, volatile, restrict and member types.
501 void BTFDebug::visitDerivedType(const DIDerivedType *DTy, uint32_t &TypeId) {
502  unsigned Tag = DTy->getTag();
503 
504  if (Tag == dwarf::DW_TAG_pointer_type || Tag == dwarf::DW_TAG_typedef ||
505  Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
506  Tag == dwarf::DW_TAG_restrict_type) {
507  auto TypeEntry = llvm::make_unique<BTFTypeDerived>(DTy, Tag);
508  TypeId = addType(std::move(TypeEntry), DTy);
509  } else if (Tag != dwarf::DW_TAG_member) {
510  return;
511  }
512 
513  // Visit base type of pointer, typedef, const, volatile, restrict or
514  // struct/union member.
515  uint32_t TempTypeId = 0;
516  visitTypeEntry(DTy->getBaseType(), TempTypeId);
517 }
518 
519 void BTFDebug::visitTypeEntry(const DIType *Ty, uint32_t &TypeId) {
520  if (!Ty || DIToIdMap.find(Ty) != DIToIdMap.end()) {
521  TypeId = DIToIdMap[Ty];
522  return;
523  }
524 
525  if (const auto *BTy = dyn_cast<DIBasicType>(Ty))
526  visitBasicType(BTy, TypeId);
527  else if (const auto *STy = dyn_cast<DISubroutineType>(Ty))
528  visitSubroutineType(STy, false, std::unordered_map<uint32_t, StringRef>(),
529  TypeId);
530  else if (const auto *CTy = dyn_cast<DICompositeType>(Ty))
531  visitCompositeType(CTy, TypeId);
532  else if (const auto *DTy = dyn_cast<DIDerivedType>(Ty))
533  visitDerivedType(DTy, TypeId);
534  else
535  llvm_unreachable("Unknown DIType");
536 }
537 
538 void BTFDebug::visitTypeEntry(const DIType *Ty) {
539  uint32_t TypeId;
540  visitTypeEntry(Ty, TypeId);
541 }
542 
543 /// Read file contents from the actual file or from the source
544 std::string BTFDebug::populateFileContent(const DISubprogram *SP) {
545  auto File = SP->getFile();
546  std::string FileName;
547 
548  if (!File->getFilename().startswith("/") && File->getDirectory().size())
549  FileName = File->getDirectory().str() + "/" + File->getFilename().str();
550  else
551  FileName = File->getFilename();
552 
553  // No need to populate the contends if it has been populated!
554  if (FileContent.find(FileName) != FileContent.end())
555  return FileName;
556 
557  std::vector<std::string> Content;
558  std::string Line;
559  Content.push_back(Line); // Line 0 for empty string
560 
561  std::unique_ptr<MemoryBuffer> Buf;
562  auto Source = File->getSource();
563  if (Source)
565  else if (ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr =
566  MemoryBuffer::getFile(FileName))
567  Buf = std::move(*BufOrErr);
568  if (Buf)
569  for (line_iterator I(*Buf, false), E; I != E; ++I)
570  Content.push_back(*I);
571 
572  FileContent[FileName] = Content;
573  return FileName;
574 }
575 
576 void BTFDebug::constructLineInfo(const DISubprogram *SP, MCSymbol *Label,
577  uint32_t Line, uint32_t Column) {
578  std::string FileName = populateFileContent(SP);
579  BTFLineInfo LineInfo;
580 
581  LineInfo.Label = Label;
582  LineInfo.FileNameOff = addString(FileName);
583  // If file content is not available, let LineOff = 0.
584  if (Line < FileContent[FileName].size())
585  LineInfo.LineOff = addString(FileContent[FileName][Line]);
586  else
587  LineInfo.LineOff = 0;
588  LineInfo.LineNum = Line;
589  LineInfo.ColumnNum = Column;
590  LineInfoTable[SecNameOff].push_back(LineInfo);
591 }
592 
593 void BTFDebug::emitCommonHeader() {
595  OS.EmitIntValue(BTF::MAGIC, 2);
596  OS.EmitIntValue(BTF::VERSION, 1);
597  OS.EmitIntValue(0, 1);
598 }
599 
600 void BTFDebug::emitBTFSection() {
601  // Do not emit section if no types and only "" string.
602  if (!TypeEntries.size() && StringTable.getSize() == 1)
603  return;
604 
605  MCContext &Ctx = OS.getContext();
606  OS.SwitchSection(Ctx.getELFSection(".BTF", ELF::SHT_PROGBITS, 0));
607 
608  // Emit header.
609  emitCommonHeader();
611 
612  uint32_t TypeLen = 0, StrLen;
613  for (const auto &TypeEntry : TypeEntries)
614  TypeLen += TypeEntry->getSize();
615  StrLen = StringTable.getSize();
616 
617  OS.EmitIntValue(0, 4);
618  OS.EmitIntValue(TypeLen, 4);
619  OS.EmitIntValue(TypeLen, 4);
620  OS.EmitIntValue(StrLen, 4);
621 
622  // Emit type table.
623  for (const auto &TypeEntry : TypeEntries)
624  TypeEntry->emitType(OS);
625 
626  // Emit string table.
627  uint32_t StringOffset = 0;
628  for (const auto &S : StringTable.getTable()) {
629  OS.AddComment("string offset=" + std::to_string(StringOffset));
630  OS.EmitBytes(S);
631  OS.EmitBytes(StringRef("\0", 1));
632  StringOffset += S.size() + 1;
633  }
634 }
635 
636 void BTFDebug::emitBTFExtSection() {
637  // Do not emit section if empty FuncInfoTable and LineInfoTable.
638  if (!FuncInfoTable.size() && !LineInfoTable.size())
639  return;
640 
641  MCContext &Ctx = OS.getContext();
642  OS.SwitchSection(Ctx.getELFSection(".BTF.ext", ELF::SHT_PROGBITS, 0));
643 
644  // Emit header.
645  emitCommonHeader();
647 
648  // Account for FuncInfo/LineInfo record size as well.
649  uint32_t FuncLen = 4, LineLen = 4;
650  for (const auto &FuncSec : FuncInfoTable) {
651  FuncLen += BTF::SecFuncInfoSize;
652  FuncLen += FuncSec.second.size() * BTF::BPFFuncInfoSize;
653  }
654  for (const auto &LineSec : LineInfoTable) {
655  LineLen += BTF::SecLineInfoSize;
656  LineLen += LineSec.second.size() * BTF::BPFLineInfoSize;
657  }
658 
659  OS.EmitIntValue(0, 4);
660  OS.EmitIntValue(FuncLen, 4);
661  OS.EmitIntValue(FuncLen, 4);
662  OS.EmitIntValue(LineLen, 4);
663 
664  // Emit func_info table.
665  OS.AddComment("FuncInfo");
667  for (const auto &FuncSec : FuncInfoTable) {
668  OS.AddComment("FuncInfo section string offset=" +
669  std::to_string(FuncSec.first));
670  OS.EmitIntValue(FuncSec.first, 4);
671  OS.EmitIntValue(FuncSec.second.size(), 4);
672  for (const auto &FuncInfo : FuncSec.second) {
673  Asm->EmitLabelReference(FuncInfo.Label, 4);
674  OS.EmitIntValue(FuncInfo.TypeId, 4);
675  }
676  }
677 
678  // Emit line_info table.
679  OS.AddComment("LineInfo");
681  for (const auto &LineSec : LineInfoTable) {
682  OS.AddComment("LineInfo section string offset=" +
683  std::to_string(LineSec.first));
684  OS.EmitIntValue(LineSec.first, 4);
685  OS.EmitIntValue(LineSec.second.size(), 4);
686  for (const auto &LineInfo : LineSec.second) {
687  Asm->EmitLabelReference(LineInfo.Label, 4);
688  OS.EmitIntValue(LineInfo.FileNameOff, 4);
689  OS.EmitIntValue(LineInfo.LineOff, 4);
690  OS.AddComment("Line " + std::to_string(LineInfo.LineNum) + " Col " +
691  std::to_string(LineInfo.ColumnNum));
692  OS.EmitIntValue(LineInfo.LineNum << 10 | LineInfo.ColumnNum, 4);
693  }
694  }
695 }
696 
698  auto *SP = MF->getFunction().getSubprogram();
699  auto *Unit = SP->getUnit();
700 
701  if (Unit->getEmissionKind() == DICompileUnit::NoDebug) {
702  SkipInstruction = true;
703  return;
704  }
705  SkipInstruction = false;
706 
707  // Collect all types locally referenced in this function.
708  // Use RetainedNodes so we can collect all argument names
709  // even if the argument is not used.
710  std::unordered_map<uint32_t, StringRef> FuncArgNames;
711  for (const DINode *DN : SP->getRetainedNodes()) {
712  if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
713  // Collect function arguments for subprogram func type.
714  uint32_t Arg = DV->getArg();
715  if (Arg) {
716  visitTypeEntry(DV->getType());
717  FuncArgNames[Arg] = DV->getName();
718  }
719  }
720  }
721 
722  // Construct subprogram func proto type.
723  uint32_t ProtoTypeId;
724  visitSubroutineType(SP->getType(), true, FuncArgNames, ProtoTypeId);
725 
726  // Construct subprogram func type
727  auto FuncTypeEntry =
728  llvm::make_unique<BTFTypeFunc>(SP->getName(), ProtoTypeId);
729  uint32_t FuncTypeId = addType(std::move(FuncTypeEntry));
730 
731  // Construct funcinfo and the first lineinfo for the function.
732  MCSymbol *FuncLabel = Asm->getFunctionBegin();
733  BTFFuncInfo FuncInfo;
734  FuncInfo.Label = FuncLabel;
735  FuncInfo.TypeId = FuncTypeId;
736  if (FuncLabel->isInSection()) {
737  MCSection &Section = FuncLabel->getSection();
738  const MCSectionELF *SectionELF = dyn_cast<MCSectionELF>(&Section);
739  assert(SectionELF && "Null section for Function Label");
740  SecNameOff = addString(SectionELF->getSectionName());
741  } else {
742  SecNameOff = addString(".text");
743  }
744  FuncInfoTable[SecNameOff].push_back(FuncInfo);
745 }
746 
748  SkipInstruction = false;
749  LineInfoGenerated = false;
750  SecNameOff = 0;
751 }
752 
755 
756  if (SkipInstruction || MI->isMetaInstruction() ||
758  return;
759 
760  if (MI->isInlineAsm()) {
761  // Count the number of register definitions to find the asm string.
762  unsigned NumDefs = 0;
763  for (; MI->getOperand(NumDefs).isReg() && MI->getOperand(NumDefs).isDef();
764  ++NumDefs)
765  ;
766 
767  // Skip this inline asm instruction if the asmstr is empty.
768  const char *AsmStr = MI->getOperand(NumDefs).getSymbolName();
769  if (AsmStr[0] == 0)
770  return;
771  }
772 
773  // Skip this instruction if no DebugLoc or the DebugLoc
774  // is the same as the previous instruction.
775  const DebugLoc &DL = MI->getDebugLoc();
776  if (!DL || PrevInstLoc == DL) {
777  // This instruction will be skipped, no LineInfo has
778  // been generated, construct one based on function signature.
779  if (LineInfoGenerated == false) {
780  auto *S = MI->getMF()->getFunction().getSubprogram();
781  MCSymbol *FuncLabel = Asm->getFunctionBegin();
782  constructLineInfo(S, FuncLabel, S->getLine(), 0);
783  LineInfoGenerated = true;
784  }
785 
786  return;
787  }
788 
789  // Create a temporary label to remember the insn for lineinfo.
790  MCSymbol *LineSym = OS.getContext().createTempSymbol();
791  OS.EmitLabel(LineSym);
792 
793  // Construct the lineinfo.
794  auto SP = DL.get()->getScope()->getSubprogram();
795  constructLineInfo(SP, LineSym, DL.getLine(), DL.getCol());
796 
797  LineInfoGenerated = true;
798  PrevInstLoc = DL;
799 }
800 
801 void BTFDebug::processGlobals() {
802  // Collect all types referenced by globals.
803  const Module *M = MMI->getModule();
804  for (const GlobalVariable &Global : M->globals()) {
805  // Ignore external globals for now.
806  if (!Global.hasInitializer() && Global.hasExternalLinkage())
807  continue;
808 
810  Global.getDebugInfo(GVs);
811  uint32_t GVTypeId = 0;
812  for (auto *GVE : GVs) {
813  visitTypeEntry(GVE->getVariable()->getType(), GVTypeId);
814  break;
815  }
816 
817  // Only support the following globals:
818  // . static variables
819  // . non-static global variables with section attributes
820  // Essentially means:
821  // . .bcc/.data/.rodata DataSec entities only contain static data
822  // . Other DataSec entities contain static or initialized global data.
823  // Initialized global data are mostly used for finding map key/value type
824  // id's. Whether DataSec is readonly or not can be found from
825  // corresponding ELF section flags.
826  auto Linkage = Global.getLinkage();
827  if (Linkage != GlobalValue::InternalLinkage &&
828  (Linkage != GlobalValue::ExternalLinkage || !Global.hasSection()))
829  continue;
830 
831  uint32_t GVarInfo = Linkage == GlobalValue::ExternalLinkage
833  : BTF::VAR_STATIC;
834  auto VarEntry =
835  llvm::make_unique<BTFKindVar>(Global.getName(), GVTypeId, GVarInfo);
836  uint32_t VarId = addType(std::move(VarEntry));
837 
838  // Decide the section name.
839  std::string SecName;
840  if (Global.hasSection()) {
841  SecName = Global.getSection().str();
842  } else {
843  // data, bss, or readonly sections
844  if (Global.isConstant())
845  SecName += ".rodata";
846  else
847  SecName += Global.getInitializer()->isZeroValue() ? ".bss" : ".data";
848  }
849 
850  // Find or create a DataSec
851  if (DataSecEntries.find(SecName) == DataSecEntries.end()) {
852  DataSecEntries[SecName] = llvm::make_unique<BTFKindDataSec>(Asm, SecName);
853  }
854 
855  // Calculate symbol size
856  const DataLayout &DL = Global.getParent()->getDataLayout();
857  uint32_t Size = DL.getTypeAllocSize(Global.getType()->getElementType());
858 
859  DataSecEntries[SecName]->addVar(VarId, Asm->getSymbol(&Global), Size);
860  }
861 
862  for (auto &DataSec : DataSecEntries)
863  addType(std::move(DataSec.second));
864 }
865 
867  // Collect all global types/variables.
868  processGlobals();
869 
870  // Complete BTF type cross refereences.
871  for (const auto &TypeEntry : TypeEntries)
872  TypeEntry->completeType(*this);
873 
874  // Emit BTF sections.
875  emitBTFSection();
876  emitBTFExtSection();
877 }
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:38
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:110
BTFTypeArray(uint32_t ElemTypeId, uint32_t NumElems)
Definition: BTFDebug.cpp:160
Represents either an error or a value T.
Definition: ErrorOr.h:56
uint32_t Type
Definition: BTF.h:167
uint64_t getOffsetInBits() const
void endFunctionImpl(const MachineFunction *MF) override
Post process after all instructions in this function are processed.
Definition: BTFDebug.cpp:747
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:21
Represent one func and its type id.
Definition: BTFDebug.h:207
BTFKindDataSec(AsmPrinter *AsmPrt, std::string SecName)
Definition: BTFDebug.cpp:300
BTFTypeFunc(StringRef FuncName, uint32_t ProtoTypeId)
Definition: BTFDebug.cpp:270
uint32_t ColumnNum
the column number
Definition: BTFDebug.h:218
const MachineFunction * getMF() const
Return the function that contains the basic block that this instruction belongs to.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
uint32_t NameOff
Definition: BTF.h:166
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
unsigned size() const
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
DIFile * getFile() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:382
uint32_t getArrayIndexTypeId()
Get the special array index type id.
Definition: BTFDebug.h:295
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
bool isInlineAsm() const
uint32_t getSize()
Definition: BTFDebug.h:199
BTFTypeEnum(const DICompositeType *ETy, uint32_t NumValues)
Definition: BTFDebug.cpp:131
iterator find(StringRef Key)
Definition: StringMap.h:332
Externally visible function.
Definition: GlobalValue.h:48
BTFTypeInt(uint32_t Encoding, uint32_t SizeInBits, uint32_t OffsetInBits, StringRef TypeName)
Definition: BTFDebug.cpp:94
A forward iterator which reads text lines from a buffer.
Definition: LineIterator.h:31
BTFTypeDerived(const DIDerivedType *Ty, unsigned Tag)
Definition: BTFDebug.cpp:40
void completeType(BTFDebug &BDebug)
Represent a BTF array.
Definition: BTFDebug.cpp:171
unsigned getLine() const
Definition: DebugLoc.cpp:25
Linkage: ExternalLinkage.
Definition: BTF.h:173
uint32_t Size
Definition: BTF.h:110
A debug info location.
Definition: DebugLoc.h:33
bool isMetaInstruction() const
Return true if this instruction doesn&#39;t produce any output in the form of executable instructions...
bool isForwardDecl() const
static const char * BTFKindStr[]
Definition: BTFDebug.cpp:25
uint32_t Offset
BitOffset or BitFieldSize+BitOffset.
Definition: BTF.h:159
StringRef getName() const
Tagged DWARF-like metadata node.
DINodeArray getElements() const
uint32_t Type
Member type.
Definition: BTF.h:158
MCContext & getContext() const
Definition: MCStreamer.h:250
DebugLoc PrevInstLoc
Previous instruction&#39;s location information.
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:281
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:262
unsigned getTag() const
Array subrange.
virtual void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:31
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:251
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:121
BTF_KIND_ENUM is followed by multiple "struct BTFEnum".
Definition: BTF.h:134
BTFKindVar(StringRef VarName, uint32_t TypeId, uint32_t VarInfo)
Definition: BTFDebug.cpp:283
uint32_t roundupToBytes(uint32_t NumBits)
Definition: BTFDebug.h:42
uint64_t getSizeInBits() const
const char * getSymbolName() const
void beginFunctionImpl(const MachineFunction *MF) override
Gather pre-function debug information.
Definition: BTFDebug.cpp:697
Context object for machine code objects.
Definition: MCContext.h:62
Subprogram description.
virtual void AddComment(const Twine &T, bool EOL=true)
Add a textual comment.
Definition: MCStreamer.h:309
DIType * getBaseType() const
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:137
MCSymbol * getFunctionBegin() const
Definition: AsmPrinter.h:208
uint32_t Type
Definition: BTF.h:111
LLVM_NODISCARD size_t size() const
size - Get the string size.
Definition: StringRef.h:130
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:312
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:128
BTF_KIND_FUNC_PROTO are followed by multiple "struct BTFParam".
Definition: BTF.h:165
Analysis containing CSE Info
Definition: CSEInfo.cpp:20
void endModule() override
Complete all the types and emit the BTF sections.
Definition: BTFDebug.cpp:866
uint8_t Kind
Definition: BTFDebug.h:34
BTFTypeStruct(const DICompositeType *STy, bool IsStruct, bool HasBitField, uint32_t NumMembers)
Represent either a struct or a union.
Definition: BTFDebug.cpp:188
Streaming machine code generation interface.
Definition: MCStreamer.h:188
MCSymbol * createTempSymbol(bool CanBeUnnamed=true)
Create and return a new assembler temporary symbol with a unique but unspecified name.
Definition: MCContext.cpp:220
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:88
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:307
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1504
Instrumentation for Order File
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
virtual void SwitchSection(MCSection *Section, const MCExpr *Subsection=nullptr)
Set the current section where code is being emitted to Section.
CountType getCount() const
struct BTF::CommonType BTFType
Definition: BTFDebug.h:36
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
Definition: BTFDebug.cpp:753
AsmPrinter * Asm
Target of debug info emission.
Collect and emit BTF information.
Definition: BTFDebug.h:222
This class is intended to be used as a driving class for all asm writers.
Definition: AsmPrinter.h:78
uint32_t NameOff
Member name offset in the string table.
Definition: BTF.h:157
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:125
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:291
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
void EmitLabelReference(const MCSymbol *Label, unsigned Size, bool IsSectionRelative=false) const
Emit something like ".long Label" where the size in bytes of the directive is specified by Size and L...
Definition: AsmPrinter.h:499
Represent one line info.
Definition: BTFDebug.h:213
void beginInstruction(const MachineInstr *MI) override
Process beginning of an instruction.
BTF_KIND_STRUCT and BTF_KIND_UNION are followed by multiple "struct BTFMember".
Definition: BTF.h:156
uint32_t NameOff
Type name offset in the string table.
Definition: BTF.h:92
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:152
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::vector< std::string > & getTable()
Definition: BTFDebug.h:200
Base class for types.
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
uint32_t LineNum
the line number
Definition: BTFDebug.h:217
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:1166
uint32_t Id
Definition: BTFDebug.h:35
StringRef getName() const
MCSymbol * getSymbol(const GlobalValue *GV) const
Definition: AsmPrinter.cpp:441
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:841
int32_t Val
Enum member value.
Definition: BTF.h:136
uint32_t NameOff
Enum name offset in the string table.
Definition: BTF.h:135
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:196
MCSymbol * Label
MCSymbol identifying insn for the lineinfo.
Definition: BTFDebug.h:214
static Twine utohexstr(const uint64_t &Val)
Definition: Twine.h:387
uint32_t getTypeId(const DIType *Ty)
Get the type id for a particular DIType.
Definition: BTFDebug.h:304
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:240
const Function & getFunction() const
Return the LLVM function that this machine code represents.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:266
This file contains support for writing BTF debug info.
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:180
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:64
BTFDebug(AsmPrinter *AP)
Definition: BTFDebug.cpp:336
Type array for a subprogram.
Linkage: InternalLinkage.
Definition: BTF.h:172
Representation of each machine instruction.
Definition: MachineInstr.h:63
uint32_t addString(StringRef S)
Add a string to the string table and returns its offset in the table.
Definition: BTFDebug.cpp:322
unsigned getEncoding() const
BTFTypeFwd(StringRef Name, bool IsUnion)
Represent a struct/union forward declaration.
Definition: BTFDebug.cpp:82
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:92
#define I(x, y, z)
Definition: MD5.cpp:58
#define N
This represents a section on linux, lots of unix variants and some bare metal systems.
Definition: MCSectionELF.h:27
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
Base class for debug information backends.
BTFTypeFuncProto(const DISubroutineType *STy, uint32_t NumParams, const std::unordered_map< uint32_t, StringRef > &FuncArgNames)
The Func kind represents both subprogram and pointee of function pointers.
Definition: BTFDebug.cpp:232
uint32_t Size
Definition: Profile.cpp:46
Rename collisions when linking (static functions).
Definition: GlobalValue.h:55
const MCSymbol * Label
Func MCSymbol.
Definition: BTFDebug.h:208
const Module * getModule() const
uint32_t FileNameOff
file name offset in the .BTF string table
Definition: BTFDebug.h:215
size_t addString(StringRef S)
Add string to the string table.
Definition: BTFDebug.h:301
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const std::string to_string(const T &Value)
Definition: ScopedPrinter.h:61
unsigned getCol() const
Definition: DebugLoc.cpp:30
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
void completeType(BTFDebug &BDebug)
Complete BTF type generation after all related DebugInfo types have been visited so their BTF type id...
Definition: BTFDebug.cpp:277
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCSectionELF * getELFSection(const Twine &Section, unsigned Type, unsigned Flags)
Definition: MCContext.h:388
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:79
virtual void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
Definition: MCStreamer.cpp:351
DITypeRefArray getTypeArray() const
MachineModuleInfo * MMI
Collected machine module information.
iterator_range< global_iterator > globals()
Definition: Module.h:587
IRTranslator LLVM IR MI
Max # of struct/union/enum members or func args.
Definition: BTF.h:80
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
uint32_t Info
"Info" bits arrangement: Bits 0-15: vlen (e.g.
Definition: BTF.h:101
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:156
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:295
const MachineOperand & getOperand(unsigned i) const
Definition: MachineInstr.h:413
void emitType(MCStreamer &OS)
Emit types for this BTF type entry.
Definition: BTFDebug.cpp:217
bool getFlag(MIFlag Flag) const
Return whether an MI flag is set.
Definition: MachineInstr.h:294
iterator end()
Definition: StringMap.h:317
uint32_t TypeId
Type id referring to .BTF type section.
Definition: BTFDebug.h:209
StringRef getSectionName() const
Definition: MCSectionELF.h:70
uint32_t LineOff
line offset in the .BTF string table
Definition: BTFDebug.h:216
Basic type, like &#39;int&#39; or &#39;float&#39;.