Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 1 | //===-- main.cpp ------------------------------------------------*- C++ -*-===// |
| 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 | #include <stdint.h> |
| 11 | #include <stdlib.h> |
| 12 | |
Johnny Chen | 216d93a | 2012-02-20 21:39:21 +0000 | [diff] [blame] | 13 | #include "LLDB/SBBlock.h" |
| 14 | #include "LLDB/SBCompileUnit.h" |
| 15 | #include "LLDB/SBDebugger.h" |
| 16 | #include "LLDB/SBFunction.h" |
| 17 | #include "LLDB/SBModule.h" |
| 18 | #include "LLDB/SBSymbol.h" |
| 19 | #include "LLDB/SBTarget.h" |
| 20 | #include "LLDB/SBThread.h" |
| 21 | #include "LLDB/SBProcess.h" |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace lldb; |
| 24 | |
| 25 | //---------------------------------------------------------------------- |
| 26 | // This quick sample code shows how to create a debugger instance and |
| 27 | // create an "i386" executable target. Then we can lookup the executable |
| 28 | // module and resolve a file address into a section offset address, |
| 29 | // and find all symbol context objects (if any) for that address: |
| 30 | // compile unit, function, deepest block, line table entry and the |
| 31 | // symbol. |
Johnny Chen | 216d93a | 2012-02-20 21:39:21 +0000 | [diff] [blame] | 32 | // |
| 33 | // To build the program, type (while in this directory): |
| 34 | // |
| 35 | // $ make |
| 36 | // |
Johnny Chen | af9b2f6 | 2012-02-21 19:13:55 +0000 | [diff] [blame] | 37 | // then (for example): |
Johnny Chen | 216d93a | 2012-02-20 21:39:21 +0000 | [diff] [blame] | 38 | // |
| 39 | // $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/svn/ToT/build/Debug ./a.out executable_path file_address |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 40 | //---------------------------------------------------------------------- |
Johnny Chen | 3715c69 | 2012-02-22 02:03:13 +0000 | [diff] [blame] | 41 | class LLDBSentry |
| 42 | { |
| 43 | public: |
| 44 | LLDBSentry() { |
| 45 | // Initialize LLDB |
| 46 | SBDebugger::Initialize(); |
| 47 | } |
| 48 | ~LLDBSentry() { |
| 49 | // Terminate LLDB |
| 50 | SBDebugger::Terminate(); |
| 51 | } |
| 52 | }; |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 53 | int |
| 54 | main (int argc, char const *argv[]) |
| 55 | { |
Johnny Chen | 3715c69 | 2012-02-22 02:03:13 +0000 | [diff] [blame] | 56 | // Use a sentry object to properly initialize/terminate LLDB. |
| 57 | LLDBSentry sentry; |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 58 | |
| 59 | if (argc < 3) |
| 60 | exit (1); |
| 61 | |
| 62 | // The first argument is the file path we want to look something up in |
| 63 | const char *exe_file_path = argv[1]; |
Greg Clayton | 710dd5a | 2011-01-08 20:28:42 +0000 | [diff] [blame] | 64 | // The second argument in the address that we want to lookup |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 65 | lldb::addr_t file_addr = strtoull (argv[2], NULL, 0); |
| 66 | |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 67 | // Create a debugger instance so we can create a target |
| 68 | SBDebugger debugger (SBDebugger::Create()); |
| 69 | |
| 70 | if (debugger.IsValid()) |
| 71 | { |
| 72 | // Create a target using the executable. |
| 73 | SBTarget target (debugger.CreateTargetWithFileAndArch (exe_file_path, "i386")); |
| 74 | if (target.IsValid()) |
| 75 | { |
| 76 | // Find the executable module so we can do a lookup inside it |
Johnny Chen | 216d93a | 2012-02-20 21:39:21 +0000 | [diff] [blame] | 77 | SBFileSpec exe_file_spec (exe_file_path, true); |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 78 | SBModule module (target.FindModule (exe_file_spec)); |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 79 | |
| 80 | // Take a file virtual address and resolve it to a section offset |
| 81 | // address that can be used to do a symbol lookup by address |
Johnny Chen | 216d93a | 2012-02-20 21:39:21 +0000 | [diff] [blame] | 82 | SBAddress addr = module.ResolveFileAddress (file_addr); |
| 83 | if (addr.IsValid()) |
| 84 | |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 85 | { |
| 86 | // We can resolve a section offset address in the module |
| 87 | // and only ask for what we need. You can logical or together |
| 88 | // bits from the SymbolContextItem enumeration found in |
| 89 | // lldb-enumeration.h to request only what you want. Here we |
| 90 | // are asking for everything. |
| 91 | // |
| 92 | // NOTE: the less you ask for, the less LLDB will parse as |
| 93 | // LLDB does partial parsing on just about everything. |
| 94 | SBSymbolContext symbol_context (module.ResolveSymbolContextForAddress (addr, eSymbolContextEverything)); |
| 95 | |
| 96 | SBCompileUnit comp_unit (symbol_context.GetCompileUnit()); |
| 97 | if (comp_unit.IsValid()) |
| 98 | { |
| 99 | } |
| 100 | SBFunction function (symbol_context.GetFunction()); |
| 101 | if (function.IsValid()) |
| 102 | { |
| 103 | } |
| 104 | SBBlock block (symbol_context.GetBlock()); |
| 105 | if (block.IsValid()) |
| 106 | { |
| 107 | } |
| 108 | SBLineEntry line_entry (symbol_context.GetLineEntry()); |
| 109 | if (line_entry.IsValid()) |
| 110 | { |
| 111 | } |
| 112 | SBSymbol symbol (symbol_context.GetSymbol()); |
| 113 | if (symbol.IsValid()) |
| 114 | { |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
Greg Clayton | 0996003 | 2010-09-10 18:31:35 +0000 | [diff] [blame] | 120 | return 0; |
| 121 | } |
| 122 | |