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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-06-14 05:27:14 +08:00
|
|
|
#include <getopt.h>
|
2010-09-11 02:31:35 +08:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2013-06-14 05:27:14 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <LLDB/LLDB.h>
|
|
|
|
#else
|
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"
|
2013-06-14 05:27:14 +08:00
|
|
|
#include "LLDB/SBStream.h"
|
2012-02-21 05:39:21 +08:00
|
|
|
#include "LLDB/SBSymbol.h"
|
|
|
|
#include "LLDB/SBTarget.h"
|
|
|
|
#include "LLDB/SBThread.h"
|
|
|
|
#include "LLDB/SBProcess.h"
|
2013-06-14 05:27:14 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <string>
|
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();
|
|
|
|
}
|
|
|
|
};
|
2013-06-14 05:27:14 +08:00
|
|
|
|
|
|
|
static struct option g_long_options[] =
|
|
|
|
{
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ "arch", required_argument, NULL, 'a' },
|
|
|
|
{ "platform", required_argument, NULL, 'p' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PROGRAM_NAME "lldb-lookup"
|
|
|
|
void
|
|
|
|
usage ()
|
|
|
|
{
|
|
|
|
puts (
|
|
|
|
"NAME\n"
|
|
|
|
" " PROGRAM_NAME " -- symbolicate addresses using lldb.\n"
|
|
|
|
"\n"
|
|
|
|
"SYNOPSIS\n"
|
|
|
|
" " PROGRAM_NAME " [[--arch=<ARCH>] [--platform=<PLATFORM>] [--verbose] [--help] --] <PATH> <ADDRESS> [<ADDRESS>....]\n"
|
|
|
|
"\n"
|
|
|
|
"DESCRIPTION\n"
|
|
|
|
" Loads the executable pointed to by <PATH> and looks up and <ADDRESS>\n"
|
|
|
|
" arguments\n"
|
|
|
|
"\n"
|
|
|
|
"EXAMPLE\n"
|
|
|
|
" " PROGRAM_NAME " --arch=x86_64 -- /usr/lib/dyld 0x100000000\n"
|
|
|
|
);
|
|
|
|
exit(0);
|
|
|
|
}
|
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
|
|
|
|
2013-06-14 05:27:14 +08:00
|
|
|
SBDebugger debugger (SBDebugger::Create());
|
2010-09-11 02:31:35 +08:00
|
|
|
|
|
|
|
// Create a debugger instance so we can create a target
|
2013-06-14 05:27:14 +08:00
|
|
|
if (!debugger.IsValid())
|
|
|
|
fprintf (stderr, "error: failed to create a debugger object\n");
|
2010-09-11 02:31:35 +08:00
|
|
|
|
2013-06-14 05:27:14 +08:00
|
|
|
bool show_usage = false;
|
|
|
|
bool verbose = false;
|
|
|
|
const char *arch = NULL;
|
|
|
|
const char *platform = NULL;
|
|
|
|
std::string short_options("h?");
|
|
|
|
for (const struct option *opt = g_long_options; opt->name; ++opt)
|
2010-09-11 02:31:35 +08:00
|
|
|
{
|
2013-06-14 05:27:14 +08:00
|
|
|
if (isprint(opt->val))
|
|
|
|
{
|
|
|
|
short_options.append(1, (char)opt->val);
|
|
|
|
switch (opt->has_arg)
|
|
|
|
{
|
|
|
|
case no_argument:
|
|
|
|
break;
|
|
|
|
case required_argument:
|
|
|
|
short_options.append(1, ':');
|
|
|
|
break;
|
|
|
|
case optional_argument:
|
|
|
|
short_options.append(2, ':');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
optind = 0;
|
|
|
|
#else
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
#endif
|
|
|
|
char ch;
|
|
|
|
while ((ch = getopt_long_only(argc, (char * const *)argv, short_options.c_str(), g_long_options, 0)) != -1)
|
|
|
|
{
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
if (arch != NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "error: the --arch option can only be specified once\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
arch = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
platform = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
verbose = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
show_usage = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (show_usage || argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
int arg_idx = 0;
|
|
|
|
// The first argument is the file path we want to look something up in
|
|
|
|
const char *exe_file_path = argv[arg_idx];
|
|
|
|
const char *addr_cstr;
|
|
|
|
const bool add_dependent_libs = false;
|
|
|
|
SBError error;
|
|
|
|
SBStream strm;
|
|
|
|
strm.RedirectToFileHandle (stdout, false);
|
|
|
|
|
|
|
|
while ((addr_cstr = argv[++arg_idx]) != NULL)
|
|
|
|
{
|
|
|
|
// The second argument in the address that we want to lookup
|
|
|
|
lldb::addr_t file_addr = strtoull (addr_cstr, NULL, 0);
|
|
|
|
|
2010-09-11 02:31:35 +08:00
|
|
|
// Create a target using the executable.
|
2013-06-14 05:27:14 +08:00
|
|
|
SBTarget target = debugger.CreateTarget (exe_file_path,
|
|
|
|
arch,
|
|
|
|
platform,
|
|
|
|
add_dependent_libs,
|
|
|
|
error);
|
|
|
|
if (!error.Success())
|
|
|
|
{
|
|
|
|
fprintf (stderr, "error: %s\n", error.GetCString());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf ("%sLooking up 0x%llx in '%s':\n", (arg_idx > 1) ? "\n" : "", file_addr, exe_file_path);
|
|
|
|
|
2010-09-11 02:31:35 +08:00
|
|
|
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));
|
2013-06-14 05:27:14 +08:00
|
|
|
|
2010-09-11 02:31:35 +08:00
|
|
|
// 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);
|
2013-06-14 05:27:14 +08:00
|
|
|
bool success = addr.IsValid() && addr.GetSection().IsValid();
|
|
|
|
if (success)
|
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.
|
2013-06-14 05:27:14 +08:00
|
|
|
SBSymbolContext sc (module.ResolveSymbolContextForAddress (addr, eSymbolContextEverything));
|
|
|
|
|
|
|
|
strm.Printf (" Address: %s + 0x%llx\n Summary: ", addr.GetSection().GetName (), addr.GetOffset());
|
|
|
|
addr.GetDescription (strm);
|
|
|
|
strm.Printf ("\n");
|
|
|
|
if (verbose)
|
|
|
|
sc.GetDescription (strm);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf ("error: 0x%llx does not resolve to a valid file address in '%s'\n", file_addr, exe_file_path);
|
2010-09-11 02:31:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|