CGDebugInfo.cpp revision 2e1cd4264d363ca869bf37ef160902f211d21b8c
1//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This coordinates the debug information generation while generating code. 11// 12//===----------------------------------------------------------------------===// 13 14#include "CGDebugInfo.h" 15#include "CodeGenModule.h" 16#include "clang/AST/ASTContext.h" 17#include "clang/AST/Decl.h" 18#include "clang/AST/Expr.h" 19#include "clang/AST/RecordLayout.h" 20#include "clang/Basic/SourceManager.h" 21#include "clang/Basic/FileManager.h" 22#include "llvm/Constants.h" 23#include "llvm/DerivedTypes.h" 24#include "llvm/Instructions.h" 25#include "llvm/Intrinsics.h" 26#include "llvm/Module.h" 27#include "llvm/ADT/StringExtras.h" 28#include "llvm/ADT/SmallVector.h" 29#include "llvm/Support/Dwarf.h" 30#include "llvm/Target/TargetMachine.h" 31using namespace clang; 32using namespace clang::CodeGen; 33 34CGDebugInfo::CGDebugInfo(CodeGenModule *m) 35 : M(m), DebugFactory(M->getModule()) { 36} 37 38CGDebugInfo::~CGDebugInfo() { 39 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!"); 40} 41 42void CGDebugInfo::setLocation(SourceLocation Loc) { 43 if (Loc.isValid()) 44 CurLoc = M->getContext().getSourceManager().getLogicalLoc(Loc); 45} 46 47/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new 48/// one if necessary. This returns null for invalid source locations. 49llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) { 50 if (Loc.isInvalid()) 51 return llvm::DICompileUnit(); 52 53 SourceManager &SM = M->getContext().getSourceManager(); 54 const FileEntry *FE = SM.getFileEntryForLoc(Loc); 55 if (FE == 0) return llvm::DICompileUnit(); 56 57 // See if this compile unit has been used before. 58 llvm::DICompileUnit &Unit = CompileUnitCache[FE]; 59 if (!Unit.isNull()) return Unit; 60 61 // Get source file information. 62 const char *FileName = FE->getName(); 63 const char *DirName = FE->getDir()->getName(); 64 65 // Create new compile unit. 66 // FIXME: Handle other language IDs as well. 67 // FIXME: Do not know how to get clang version yet. 68 return Unit = DebugFactory.CreateCompileUnit(llvm::dwarf::DW_LANG_C89, 69 FileName, DirName, "clang"); 70} 71 72/// getOrCreateBuiltinType - Get the Basic type from the cache or create a new 73/// one if necessary. 74llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT, 75 llvm::DICompileUnit Unit){ 76 unsigned Encoding = 0; 77 switch (BT->getKind()) { 78 default: 79 case BuiltinType::Void: 80 return llvm::DIType(); 81 case BuiltinType::UChar: 82 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break; 83 case BuiltinType::Char_S: 84 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break; 85 case BuiltinType::UShort: 86 case BuiltinType::UInt: 87 case BuiltinType::ULong: 88 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break; 89 case BuiltinType::Short: 90 case BuiltinType::Int: 91 case BuiltinType::Long: 92 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break; 93 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break; 94 case BuiltinType::Float: 95 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break; 96 } 97 // Bit size, align and offset of the type. 98 uint64_t Size = M->getContext().getTypeSize(BT); 99 uint64_t Align = M->getContext().getTypeAlign(BT); 100 uint64_t Offset = 0; 101 102 return DebugFactory.CreateBasicType(Unit, BT->getName(), Unit, 0, Size, Align, 103 Offset, /*flags*/ 0, Encoding); 104} 105 106/// getOrCreateCVRType - Get the CVR qualified type from the cache or create 107/// a new one if necessary. 108llvm::DIType CGDebugInfo::CreateCVRType(QualType Ty, llvm::DICompileUnit Unit) { 109 // We will create one Derived type for one qualifier and recurse to handle any 110 // additional ones. 111 llvm::DIType FromTy; 112 unsigned Tag; 113 if (Ty.isConstQualified()) { 114 Tag = llvm::dwarf::DW_TAG_const_type; 115 Ty.removeConst(); 116 FromTy = getOrCreateType(Ty, Unit); 117 } else if (Ty.isVolatileQualified()) { 118 Tag = llvm::dwarf::DW_TAG_volatile_type; 119 Ty.removeVolatile(); 120 FromTy = getOrCreateType(Ty, Unit); 121 } else { 122 assert(Ty.isRestrictQualified() && "Unknown type qualifier for debug info"); 123 Tag = llvm::dwarf::DW_TAG_restrict_type; 124 Ty.removeRestrict(); 125 FromTy = getOrCreateType(Ty, Unit); 126 } 127 128 // No need to fill in the Name, Line, Size, Alignment, Offset in case of 129 // CVR derived types. 130 return DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(), 131 0, 0, 0, 0, 0, FromTy); 132} 133 134llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty, 135 llvm::DICompileUnit Unit) { 136 llvm::DIType EltTy = getOrCreateType(Ty->getPointeeType(), Unit); 137 138 // Bit size, align and offset of the type. 139 uint64_t Size = M->getContext().getTypeSize(Ty); 140 uint64_t Align = M->getContext().getTypeAlign(Ty); 141 142 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit, 143 "", llvm::DICompileUnit(), 144 0, Size, Align, 0, 0, EltTy); 145} 146 147llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty, 148 llvm::DICompileUnit Unit) { 149 // Typedefs are derived from some other type. If we have a typedef of a 150 // typedef, make sure to emit the whole chain. 151 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit); 152 153 // We don't set size information, but do specify where the typedef was 154 // declared. 155 const char *TyName = Ty->getDecl()->getIdentifierName(); 156 SourceLocation DefLoc = Ty->getDecl()->getLocation(); 157 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 158 159 SourceManager &SM = M->getContext().getSourceManager(); 160 uint64_t Line = SM.getLogicalLineNumber(DefLoc); 161 162 return DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef, Unit, 163 TyName, DefUnit, Line, 0, 0, 0, 0, Src); 164} 165 166llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty, 167 llvm::DICompileUnit Unit) { 168 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 169 170 // Add the result type at least. 171 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit)); 172 173 // Set up remainder of arguments if there is a prototype. 174 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'! 175 if (const FunctionTypeProto *FTP = dyn_cast<FunctionTypeProto>(Ty)) { 176 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i) 177 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit)); 178 } else { 179 // FIXME: Handle () case in C. llvm-gcc doesn't do it either. 180 } 181 182 llvm::DIArray EltTypeArray = 183 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); 184 185 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type, 186 Unit, "", llvm::DICompileUnit(), 187 0, 0, 0, 0, 0, 188 llvm::DIType(), EltTypeArray); 189} 190 191/// getOrCreateRecordType - get structure or union type. 192llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty, 193 llvm::DICompileUnit Unit) { 194 const RecordDecl *Decl = Ty->getDecl(); 195 196 unsigned Tag; 197 if (Decl->isStruct()) 198 Tag = llvm::dwarf::DW_TAG_structure_type; 199 else if (Decl->isUnion()) 200 Tag = llvm::dwarf::DW_TAG_union_type; 201 else { 202 assert(Decl->isClass() && "Unknown RecordType!"); 203 Tag = llvm::dwarf::DW_TAG_class_type; 204 } 205 206 SourceManager &SM = M->getContext().getSourceManager(); 207 208 // Get overall information about the record type for the debug info. 209 const char *Name = Decl->getIdentifierName(); 210 if (Name == 0) Name = ""; 211 212 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation()); 213 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation()); 214 215 216 // Records and classes and unions can all be recursive. To handle them, we 217 // first generate a debug descriptor for the struct as a forward declaration. 218 // Then (if it is a definition) we go through and get debug info for all of 219 // its members. Finally, we create a descriptor for the complete type (which 220 // may refer to the forward decl if the struct is recursive) and replace all 221 // uses of the forward declaration with the final definition. 222 llvm::DIType FwdDecl = 223 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, 0, 0, 0, 0, 224 llvm::DIType(), llvm::DIArray()); 225 226 // If this is just a forward declaration, return it. 227 if (!Decl->getDefinition(M->getContext())) 228 return FwdDecl; 229 230 // Otherwise, insert it into the TypeCache so that recursive uses will find 231 // it. 232 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl; 233 234 // Convert all the elements. 235 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys; 236 237 const ASTRecordLayout &RL = M->getContext().getASTRecordLayout(Decl); 238 239 unsigned FieldNo = 0; 240 for (RecordDecl::field_const_iterator I = Decl->field_begin(), 241 E = Decl->field_end(); I != E; ++I, ++FieldNo) { 242 FieldDecl *Field = *I; 243 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit); 244 245 const char *FieldName = Field->getIdentifierName(); 246 if (FieldName == 0) FieldName = ""; 247 248 // Get the location for the field. 249 SourceLocation FieldDefLoc = Field->getLocation(); 250 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc); 251 uint64_t FieldLine = SM.getLogicalLineNumber(FieldDefLoc); 252 253 // Bit size, align and offset of the type. 254 uint64_t FieldSize = M->getContext().getTypeSize(Ty); 255 uint64_t FieldAlign = M->getContext().getTypeAlign(Ty); 256 uint64_t FieldOffset = RL.getFieldOffset(FieldNo); 257 258 // Create a DW_TAG_member node to remember the offset of this field in the 259 // struct. FIXME: This is an absolutely insane way to capture this 260 // information. When we gut debug info, this should be fixed. 261 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit, 262 FieldName, FieldDefUnit, 263 FieldLine, FieldSize, FieldAlign, 264 FieldOffset, 0, FieldTy); 265 EltTys.push_back(FieldTy); 266 } 267 268 llvm::DIArray Elements = 269 DebugFactory.GetOrCreateArray(&EltTys[0], EltTys.size()); 270 271 // Bit size, align and offset of the type. 272 uint64_t Size = M->getContext().getTypeSize(Ty); 273 uint64_t Align = M->getContext().getTypeAlign(Ty); 274 275 llvm::DIType RealDecl = 276 DebugFactory.CreateCompositeType(Tag, Unit, Name, DefUnit, Line, Size, 277 Align, 0, 0, llvm::DIType(), Elements); 278 279 // Now that we have a real decl for the struct, replace anything using the 280 // old decl with the new one. This will recursively update the debug info. 281 FwdDecl.getGV()->replaceAllUsesWith(RealDecl.getGV()); 282 FwdDecl.getGV()->eraseFromParent(); 283 284 return RealDecl; 285} 286 287llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty, 288 llvm::DICompileUnit Unit) { 289 EnumDecl *Decl = Ty->getDecl(); 290 291 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators; 292 293 // Create DIEnumerator elements for each enumerator. 294 for (EnumConstantDecl *Elt = Decl->getEnumConstantList(); Elt; 295 Elt = dyn_cast_or_null<EnumConstantDecl>(Elt->getNextDeclarator())) { 296 Enumerators.push_back(DebugFactory.CreateEnumerator(Elt->getName(), 297 Elt->getInitVal().getZExtValue())); 298 } 299 300 // Return a CompositeType for the enum itself. 301 llvm::DIArray EltArray = 302 DebugFactory.GetOrCreateArray(&Enumerators[0], Enumerators.size()); 303 304 const char *EnumName 305 = Decl->getIdentifierName() ? Decl->getIdentifierName() : ""; 306 SourceLocation DefLoc = Decl->getLocation(); 307 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc); 308 SourceManager &SM = M->getContext().getSourceManager(); 309 uint64_t Line = SM.getLogicalLineNumber(DefLoc); 310 311 // Size and align of the type. 312 uint64_t Size = M->getContext().getTypeSize(Ty); 313 uint64_t Align = M->getContext().getTypeAlign(Ty); 314 315 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type, 316 Unit, EnumName, DefUnit, Line, 317 Size, Align, 0, 0, 318 llvm::DIType(), EltArray); 319} 320 321llvm::DIType CGDebugInfo::CreateType(const TagType *Ty, 322 llvm::DICompileUnit Unit) { 323 if (const RecordType *RT = dyn_cast<RecordType>(Ty)) 324 return CreateType(RT, Unit); 325 else if (const EnumType *ET = dyn_cast<EnumType>(Ty)) 326 return CreateType(ET, Unit); 327 328 return llvm::DIType(); 329} 330 331llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty, 332 llvm::DICompileUnit Unit) { 333 // Size and align of the whole array, not the element type. 334 uint64_t Size = M->getContext().getTypeSize(Ty); 335 uint64_t Align = M->getContext().getTypeAlign(Ty); 336 337 // Add the dimensions of the array. FIXME: This loses CV qualifiers from 338 // interior arrays, do we care? Why aren't nested arrays represented the 339 // obvious/recursive way? 340 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts; 341 QualType EltTy(Ty, 0); 342 while ((Ty = dyn_cast<ArrayType>(EltTy))) { 343 uint64_t Upper = 0; 344 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty)) 345 Upper = CAT->getSize().getZExtValue() - 1; 346 // FIXME: Verify this is right for VLAs. 347 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper)); 348 EltTy = Ty->getElementType(); 349 } 350 351 llvm::DIArray SubscriptArray = 352 DebugFactory.GetOrCreateArray(&Subscripts[0], Subscripts.size()); 353 354 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type, 355 Unit, "", llvm::DICompileUnit(), 356 0, Size, Align, 0, 0, 357 getOrCreateType(EltTy, Unit), 358 SubscriptArray); 359} 360 361 362/// getOrCreateType - Get the type from the cache or create a new 363/// one if necessary. 364llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty, 365 llvm::DICompileUnit Unit) { 366 if (Ty.isNull()) 367 return llvm::DIType(); 368 369 // Check to see if the compile unit already has created this type. 370 llvm::DIType &Slot = TypeCache[Ty.getAsOpaquePtr()]; 371 if (!Slot.isNull()) return Slot; 372 373 // Handle CVR qualifiers, which recursively handles what they refer to. 374 if (Ty.getCVRQualifiers()) 375 return Slot = CreateCVRType(Ty, Unit); 376 377 // Work out details of type. 378 switch (Ty->getTypeClass()) { 379 case Type::Complex: 380 case Type::Reference: 381 case Type::Vector: 382 case Type::ExtVector: 383 case Type::ASQual: 384 case Type::ObjCInterface: 385 case Type::ObjCQualifiedInterface: 386 case Type::ObjCQualifiedId: 387 default: 388 return llvm::DIType(); 389 390 case Type::Builtin: Slot = CreateType(cast<BuiltinType>(Ty), Unit); break; 391 case Type::Pointer: Slot = CreateType(cast<PointerType>(Ty), Unit); break; 392 case Type::TypeName: Slot = CreateType(cast<TypedefType>(Ty), Unit); break; 393 case Type::Tagged: Slot = CreateType(cast<TagType>(Ty), Unit); break; 394 case Type::FunctionProto: 395 case Type::FunctionNoProto: 396 return Slot = CreateType(cast<FunctionType>(Ty), Unit); 397 398 case Type::ConstantArray: 399 case Type::VariableArray: 400 case Type::IncompleteArray: 401 return Slot = CreateType(cast<ArrayType>(Ty), Unit); 402 case Type::TypeOfExp: 403 return Slot = getOrCreateType(cast<TypeOfExpr>(Ty)->getUnderlyingExpr() 404 ->getType(), Unit); 405 case Type::TypeOfTyp: 406 return Slot = getOrCreateType(cast<TypeOfType>(Ty)->getUnderlyingType(), 407 Unit); 408 } 409 410 return Slot; 411} 412 413/// EmitFunctionStart - Constructs the debug code for entering a function - 414/// "llvm.dbg.func.start.". 415void CGDebugInfo::EmitFunctionStart(const char *Name, QualType ReturnType, 416 llvm::Function *Fn, 417 CGBuilderTy &Builder) { 418 // FIXME: Why is this using CurLoc??? 419 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 420 SourceManager &SM = M->getContext().getSourceManager(); 421 uint64_t LineNo = SM.getLogicalLineNumber(CurLoc); 422 423 llvm::DISubprogram SP = 424 DebugFactory.CreateSubprogram(Unit, Name, Name, "", Unit, LineNo, 425 getOrCreateType(ReturnType, Unit), 426 Fn->hasInternalLinkage(), true/*definition*/); 427 428 DebugFactory.InsertSubprogramStart(SP, Builder.GetInsertBlock()); 429 430 // Push function on region stack. 431 RegionStack.push_back(SP); 432} 433 434 435void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) { 436 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return; 437 438 // Don't bother if things are the same as last time. 439 SourceManager &SM = M->getContext().getSourceManager(); 440 if (CurLoc == PrevLoc 441 || (SM.getLineNumber(CurLoc) == SM.getLineNumber(PrevLoc) 442 && SM.isFromSameFile(CurLoc, PrevLoc))) 443 return; 444 445 // Update last state. 446 PrevLoc = CurLoc; 447 448 // Get the appropriate compile unit. 449 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc); 450 DebugFactory.InsertStopPoint(Unit, SM.getLogicalLineNumber(CurLoc), 451 SM.getLogicalColumnNumber(CurLoc), 452 Builder.GetInsertBlock()); 453} 454 455/// EmitRegionStart- Constructs the debug code for entering a declarative 456/// region - "llvm.dbg.region.start.". 457void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) { 458 llvm::DIDescriptor D; 459 if (!RegionStack.empty()) 460 D = RegionStack.back(); 461 D = DebugFactory.CreateBlock(D); 462 RegionStack.push_back(D); 463 DebugFactory.InsertRegionStart(D, Builder.GetInsertBlock()); 464} 465 466/// EmitRegionEnd - Constructs the debug code for exiting a declarative 467/// region - "llvm.dbg.region.end." 468void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) { 469 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 470 471 // Provide an region stop point. 472 EmitStopPoint(Fn, Builder); 473 474 DebugFactory.InsertRegionEnd(RegionStack.back(), Builder.GetInsertBlock()); 475 RegionStack.pop_back(); 476} 477 478/// EmitDeclare - Emit local variable declaration debug info. 479void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag, 480 llvm::Value *Storage, CGBuilderTy &Builder) { 481 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!"); 482 483 // Get location information. 484 SourceManager &SM = M->getContext().getSourceManager(); 485 uint64_t Line = SM.getLogicalLineNumber(Decl->getLocation()); 486 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 487 488 // Create the descriptor for the variable. 489 llvm::DIVariable D = 490 DebugFactory.CreateVariable(Tag, RegionStack.back(), Decl->getName(), 491 Unit, Line, 492 getOrCreateType(Decl->getType(), Unit)); 493 // Insert an llvm.dbg.declare into the current block. 494 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock()); 495} 496 497void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl, 498 llvm::Value *Storage, 499 CGBuilderTy &Builder) { 500 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder); 501} 502 503/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument 504/// variable declaration. 505void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI, 506 CGBuilderTy &Builder) { 507 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder); 508} 509 510 511 512/// EmitGlobalVariable - Emit information about a global variable. 513void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var, 514 const VarDecl *Decl) { 515 // Create global variable debug descriptor. 516 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation()); 517 SourceManager &SM = M->getContext().getSourceManager(); 518 uint64_t LineNo = SM.getLogicalLineNumber(Decl->getLocation()); 519 const char *Name = Decl->getIdentifierName(); 520 521 DebugFactory.CreateGlobalVariable(Unit, Name, Name, "", Unit, LineNo, 522 getOrCreateType(Decl->getType(), Unit), 523 Var->hasInternalLinkage(), 524 true/*definition*/, Var); 525} 526 527