2010-09-11 02:31:35 +08:00
|
|
|
//===-- main.cpp ------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-02-21 05:39:21 +08:00
|
|
|
#include "LLDB/SBBlock.h"
|
|
|
|
#include "LLDB/SBCompileUnit.h"
|
|
|
|
#include "LLDB/SBDebugger.h"
|
|
|
|
#include "LLDB/SBFunction.h"
|
|
|
|
#include "LLDB/SBModule.h"
|
|
|
|
#include "LLDB/SBSymbol.h"
|
|
|
|
#include "LLDB/SBTarget.h"
|
|
|
|
#include "LLDB/SBThread.h"
|
|
|
|
#include "LLDB/SBProcess.h"
|
2010-09-11 02:31:35 +08:00
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// This quick sample code shows how to create a debugger instance and
|
|
|
|
// create an "i386" executable target. Then we can lookup the executable
|
|
|
|
// module and resolve a file address into a section offset address,
|
|
|
|
// and find all symbol context objects (if any) for that address:
|
|
|
|
// compile unit, function, deepest block, line table entry and the
|
|
|
|
// symbol.
|
2012-02-21 05:39:21 +08:00
|
|
|
//
|
|
|
|
// To build the program, type (while in this directory):
|
|
|
|
//
|
|
|
|
// $ make
|
|
|
|
//
|
2012-02-22 03:13:55 +08:00
|
|
|
// then (for example):
|
2012-02-21 05:39:21 +08:00
|
|
|
//
|
|
|
|
// $ DYLD_FRAMEWORK_PATH=/Volumes/data/lldb/svn/ToT/build/Debug ./a.out executable_path file_address
|
2010-09-11 02:31:35 +08:00
|
|
|
//----------------------------------------------------------------------
|
2012-02-22 10:03:13 +08:00
|
|
|
class LLDBSentry
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
LLDBSentry() {
|
|
|
|
// Initialize LLDB
|
|
|
|
SBDebugger::Initialize();
|
|
|
|
}
|
|
|
|
~LLDBSentry() {
|
|
|
|
// Terminate LLDB
|
|
|
|
SBDebugger::Terminate();
|
|
|
|
}
|
|
|
|
};
|
2010-09-11 02:31:35 +08:00
|
|
|
int
|
|
|
|
main (int argc, char const *argv[])
|
|
|
|
{
|
2012-02-22 10:03:13 +08:00
|
|
|
// Use a sentry object to properly initialize/terminate LLDB.
|
|
|
|
LLDBSentry sentry;
|
2010-09-11 02:31:35 +08:00
|
|
|
|
|
|
|
if (argc < 3)
|
|
|
|
exit (1);
|
|
|
|
|
|
|
|
// The first argument is the file path we want to look something up in
|
|
|
|
const char *exe_file_path = argv[1];
|
2011-01-09 04:28:42 +08:00
|
|
|
// The second argument in the address that we want to lookup
|
2010-09-11 02:31:35 +08:00
|
|
|
lldb::addr_t file_addr = strtoull (argv[2], NULL, 0);
|
|
|
|
|
|
|
|
// Create a debugger instance so we can create a target
|
|
|
|
SBDebugger debugger (SBDebugger::Create());
|
|
|
|
|
|
|
|
if (debugger.IsValid())
|
|
|
|
{
|
|
|
|
// Create a target using the executable.
|
|
|
|
SBTarget target (debugger.CreateTargetWithFileAndArch (exe_file_path, "i386"));
|
|
|
|
if (target.IsValid())
|
|
|
|
{
|
|
|
|
// Find the executable module so we can do a lookup inside it
|
2012-02-21 05:39:21 +08:00
|
|
|
SBFileSpec exe_file_spec (exe_file_path, true);
|
2010-09-11 02:31:35 +08:00
|
|
|
SBModule module (target.FindModule (exe_file_spec));
|
|
|
|
|
|
|
|
// Take a file virtual address and resolve it to a section offset
|
|
|
|
// address that can be used to do a symbol lookup by address
|
2012-02-21 05:39:21 +08:00
|
|
|
SBAddress addr = module.ResolveFileAddress (file_addr);
|
|
|
|
if (addr.IsValid())
|
|
|
|
|
2010-09-11 02:31:35 +08:00
|
|
|
{
|
|
|
|
// We can resolve a section offset address in the module
|
|
|
|
// and only ask for what we need. You can logical or together
|
|
|
|
// bits from the SymbolContextItem enumeration found in
|
|
|
|
// lldb-enumeration.h to request only what you want. Here we
|
|
|
|
// are asking for everything.
|
|
|
|
//
|
|
|
|
// NOTE: the less you ask for, the less LLDB will parse as
|
|
|
|
// LLDB does partial parsing on just about everything.
|
|
|
|
SBSymbolContext symbol_context (module.ResolveSymbolContextForAddress (addr, eSymbolContextEverything));
|
|
|
|
|
|
|
|
SBCompileUnit comp_unit (symbol_context.GetCompileUnit());
|
|
|
|
if (comp_unit.IsValid())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
SBFunction function (symbol_context.GetFunction());
|
|
|
|
if (function.IsValid())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
SBBlock block (symbol_context.GetBlock());
|
|
|
|
if (block.IsValid())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
SBLineEntry line_entry (symbol_context.GetLineEntry());
|
|
|
|
if (line_entry.IsValid())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
SBSymbol symbol (symbol_context.GetSymbol());
|
|
|
|
if (symbol.IsValid())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|