blob: 10c16407bb40797c8275e6ad415332b4112ccb52 [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//----------------------------------------------------------------------
Johnny Chen3715c692012-02-22 02:03:13 +000041class LLDBSentry
42{
43public:
44 LLDBSentry() {
45 // Initialize LLDB
46 SBDebugger::Initialize();
47 }
48 ~LLDBSentry() {
49 // Terminate LLDB
50 SBDebugger::Terminate();
51 }
52};
Greg Clayton09960032010-09-10 18:31:35 +000053int
54main (int argc, char const *argv[])
55{
Johnny Chen3715c692012-02-22 02:03:13 +000056 // Use a sentry object to properly initialize/terminate LLDB.
57 LLDBSentry sentry;
Greg Clayton09960032010-09-10 18:31:35 +000058
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 Clayton710dd5a2011-01-08 20:28:42 +000064 // The second argument in the address that we want to lookup
Greg Clayton09960032010-09-10 18:31:35 +000065 lldb::addr_t file_addr = strtoull (argv[2], NULL, 0);
66
Greg Clayton09960032010-09-10 18:31:35 +000067 // 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 Chen216d93a2012-02-20 21:39:21 +000077 SBFileSpec exe_file_spec (exe_file_path, true);
Greg Clayton09960032010-09-10 18:31:35 +000078 SBModule module (target.FindModule (exe_file_spec));
Greg Clayton09960032010-09-10 18:31:35 +000079
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 Chen216d93a2012-02-20 21:39:21 +000082 SBAddress addr = module.ResolveFileAddress (file_addr);
83 if (addr.IsValid())
84
Greg Clayton09960032010-09-10 18:31:35 +000085 {
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 Clayton09960032010-09-10 18:31:35 +0000120 return 0;
121}
122