blob: 51d95ede9b28e638ee7194f39c42eb36f9885eaa [file] [log] [blame]
Greg Clayton09960032010-09-10 18:31:35 +00001//===-- 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 Chen216d93a2012-02-20 21:39:21 +000013#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 Clayton09960032010-09-10 18:31:35 +000022
23using 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 Chen216d93a2012-02-20 21:39:21 +000032//
33// To build the program, type (while in this directory):
34//
35// $ make
36//
Johnny Chenaf9b2f62012-02-21 19:13:55 +000037// then (for example):
Johnny Chen216d93a2012-02-20 21:39:21 +000038//
39// $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/svn/ToT/build/Debug ./a.out executable_path file_address
Greg Clayton09960032010-09-10 18:31:35 +000040//----------------------------------------------------------------------
41int
42main (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];
Greg Clayton710dd5a2011-01-08 20:28:42 +000052 // The second argument in the address that we want to lookup
Greg Clayton09960032010-09-10 18:31:35 +000053 lldb::addr_t file_addr = strtoull (argv[2], NULL, 0);
54
Greg Clayton09960032010-09-10 18:31:35 +000055 // 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
Johnny Chen216d93a2012-02-20 21:39:21 +000065 SBFileSpec exe_file_spec (exe_file_path, true);
Greg Clayton09960032010-09-10 18:31:35 +000066 SBModule module (target.FindModule (exe_file_spec));
Greg Clayton09960032010-09-10 18:31:35 +000067
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
Johnny Chen216d93a2012-02-20 21:39:21 +000070 SBAddress addr = module.ResolveFileAddress (file_addr);
71 if (addr.IsValid())
72
Greg Clayton09960032010-09-10 18:31:35 +000073 {
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