llvm.org GIT mirror llvm / 772e538
Add argument variable support to the debug info tutorial and rearrange the prologue source location hack to immediately after it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223725 91177308-0d34-0410-b5e6-96231b3b80d8 Eric Christopher 4 years ago
1 changed file(s) with 43 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
372372 when we start generating the code for each function.
374 also, don't forget to pop the scope back off of your scope stack at the
375 end of the code generation for the function:
377 .. code-block:: c++
379 // Pop off the lexical block for the function since we added it
380 // unconditionally.
381 KSDbgInfo.LexicalBlocks.pop_back();
383 Variables
384 =========
386 Now that we have functions, we need to be able to print out the variables
387 we have in scope. Let's get our function arguments set up so we can get
388 decent backtraces and see how our functions are being called. It isn't
389 a lot of code, and we generally handle it when we're creating the
390 argument allocas in ``PrototypeAST::CreateArgumentAllocas``.
392 .. code-block:: c++
394 DIScope *Scope = KSDbgInfo.LexicalBlocks.back();
395 DIFile Unit = DBuilder->createFile(KSDbgInfo.TheCU.getFilename(),
396 KSDbgInfo.TheCU.getDirectory());
397 DIVariable D = DBuilder->createLocalVariable(dwarf::DW_TAG_arg_variable,
398 *Scope, Args[Idx], Unit, Line,
399 KSDbgInfo.getDoubleTy(), Idx);
401 Instruction *Call = DBuilder->insertDeclare(
402 Alloca, D, DBuilder->createExpression(), Builder.GetInsertBlock());
403 Call->setDebugLoc(DebugLoc::get(Line, 0, *Scope));
405 Here we're doing a few things. First, we're grabbing our current scope
406 for the variable so we can say what range of code our variable is valid
407 through. Second, we're creating the variable, giving it the scope,
408 the name, source location, type, and since it's an argument, the argument
409 index. Third, we create an ``lvm.dbg.declare`` call to indicate at the IR
410 level that we've got a variable in an alloca (and it gives a starting
411 location for the variable). Lastly, we set a source location for the
412 beginning of the scope on the declare.
374414 One interesting thing to note at this point is that various debuggers have
375415 assumptions based on how code and debug information was generated for them
376416 in the past. In this case we need to do a little bit of a hack to avoid
393433 KSDbgInfo.emitLocation(Body);
395 also, don't forget to pop the scope back off of your scope stack at the
396 end of the code generation for the function:
398 .. code-block:: c++
400 // Pop off the lexical block for the function since we added it
401 // unconditionally.
402 KSDbgInfo.LexicalBlocks.pop_back();
435 With this we have enough debug information to set breakpoints in functions,
436 print out argument variables, and call functions. Not too bad for just a
437 few simple lines of code!
405439 Full Code Listing
406440 =================