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 
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"
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.
32 //
33 // To build the program, type (while in this directory):
34 //
35 //    $ make
36 //
37 // then:
38 //
39 //    $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/svn/ToT/build/Debug ./a.out executable_path file_address
40 //----------------------------------------------------------------------
41 int
42 main (int argc, char const *argv[])
43 {
44     // Initialize LLDB
45     SBDebugger::Initialize();
46 
47     if (argc < 3)
48         exit (1);
49 
50     // The first argument is the file path we want to look something up in
51     const char *exe_file_path = argv[1];
52     // The second argument in the address that we want to lookup
53     lldb::addr_t file_addr = strtoull (argv[2], NULL, 0);
54 
55     // Create a debugger instance so we can create a target
56     SBDebugger debugger (SBDebugger::Create());
57 
58     if (debugger.IsValid())
59     {
60         // Create a target using the executable.
61         SBTarget target (debugger.CreateTargetWithFileAndArch (exe_file_path, "i386"));
62         if (target.IsValid())
63         {
64             // Find the executable module so we can do a lookup inside it
65             SBFileSpec exe_file_spec (exe_file_path, true);
66             SBModule module (target.FindModule (exe_file_spec));
67 
68             // Take a file virtual address and resolve it to a section offset
69             // address that can be used to do a symbol lookup by address
70             SBAddress addr = module.ResolveFileAddress (file_addr);
71             if (addr.IsValid())
72 
73             {
74                 // We can resolve a section offset address in the module
75                 // and only ask for what we need. You can logical or together
76                 // bits from the SymbolContextItem enumeration found in
77                 // lldb-enumeration.h to request only what you want. Here we
78                 // are asking for everything.
79                 //
80                 // NOTE: the less you ask for, the less LLDB will parse as
81                 // LLDB does partial parsing on just about everything.
82                 SBSymbolContext symbol_context (module.ResolveSymbolContextForAddress (addr, eSymbolContextEverything));
83 
84                 SBCompileUnit comp_unit (symbol_context.GetCompileUnit());
85                 if (comp_unit.IsValid())
86                 {
87                 }
88                 SBFunction function (symbol_context.GetFunction());
89                 if (function.IsValid())
90                 {
91                 }
92                 SBBlock block (symbol_context.GetBlock());
93                 if (block.IsValid())
94                 {
95                 }
96                 SBLineEntry line_entry (symbol_context.GetLineEntry());
97                 if (line_entry.IsValid())
98                 {
99                 }
100                 SBSymbol symbol (symbol_context.GetSymbol());
101                 if (symbol.IsValid())
102                 {
103                 }
104             }
105         }
106     }
107 
108     // Terminate LLDB
109     SBDebugger::Terminate();
110     return 0;
111 }
112 
113