llvm-project/lldb/source/Commands/CommandObjectSource.cpp
Greg Clayton 32e0a7509c Many improvements to the Platform base class and subclasses. The base Platform
class now implements the Host functionality for a lot of things that make 
sense by default so that subclasses can check:

int
PlatformSubclass::Foo ()
{
    if (IsHost())
        return Platform::Foo (); // Let the platform base class do the host specific stuff
    
    // Platform subclass specific code...
    int result = ...
    return result;
}

Added new functions to the platform:

    virtual const char *Platform::GetUserName (uint32_t uid);
    virtual const char *Platform::GetGroupName (uint32_t gid);

The user and group names are cached locally so that remote platforms can avoid
sending packets multiple times to resolve this information.

Added the parent process ID to the ProcessInfo class. 

Added a new ProcessInfoMatch class which helps us to match processes up
and changed the Host layer over to using this new class. The new class allows
us to search for processs:
1 - by name (equal to, starts with, ends with, contains, and regex)
2 - by pid
3 - And further check for parent pid == value, uid == value, gid == value, 
    euid == value, egid == value, arch == value, parent == value.
    
This is all hookup up to the "platform process list" command which required
adding dumping routines to dump process information. If the Host class 
implements the process lookup routines, you can now lists processes on 
your local machine:

machine1.foo.com % lldb
(lldb) platform process list 
PID    PARENT USER       GROUP      EFF USER   EFF GROUP  TRIPLE                   NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99538  1      username   usergroup  username   usergroup  x86_64-apple-darwin      FileMerge
94943  1      username   usergroup  username   usergroup  x86_64-apple-darwin      mdworker
94852  244    username   usergroup  username   usergroup  x86_64-apple-darwin      Safari
94727  244    username   usergroup  username   usergroup  x86_64-apple-darwin      Xcode
92742  92710  username   usergroup  username   usergroup  i386-apple-darwin        debugserver


This of course also works remotely with the lldb-platform:

machine1.foo.com % lldb-platform --listen 1234

machine2.foo.com % lldb
(lldb) platform create remote-macosx
  Platform: remote-macosx
 Connected: no
(lldb) platform connect connect://localhost:1444
  Platform: remote-macosx
    Triple: x86_64-apple-darwin
OS Version: 10.6.7 (10J869)
    Kernel: Darwin Kernel Version 10.7.0: Sat Jan 29 15:17:16 PST 2011; root:xnu-1504.9.37~1/RELEASE_I386
  Hostname: machine1.foo.com
 Connected: yes
(lldb) platform process list 
PID    PARENT USER       GROUP      EFF USER   EFF GROUP  TRIPLE                   NAME
====== ====== ========== ========== ========== ========== ======================== ============================
99556  244    username   usergroup  username   usergroup  x86_64-apple-darwin      trustevaluation
99548  65539  username   usergroup  username   usergroup  x86_64-apple-darwin      lldb
99538  1      username   usergroup  username   usergroup  x86_64-apple-darwin      FileMerge
94943  1      username   usergroup  username   usergroup  x86_64-apple-darwin      mdworker
94852  244    username   usergroup  username   usergroup  x86_64-apple-darwin      Safari

The lldb-platform implements everything with the Host:: layer, so this should
"just work" for linux. I will probably be adding more stuff to the Host layer
for launching processes and attaching to processes so that this support should
eventually just work as well.

Modified the target to be able to be created with an architecture that differs
from the main executable. This is needed for iOS debugging since we can have
an "armv6" binary which can run on an "armv7" machine, so we want to be able
to do:

% lldb
(lldb) platform create remote-ios
(lldb) file --arch armv7 a.out

Where "a.out" is an armv6 executable. The platform then can correctly decide
to open all "armv7" images for all dependent shared libraries.

Modified the disassembly to show the current PC value. Example output:

(lldb) disassemble --frame
a.out`main:
   0x1eb7:  pushl  %ebp
   0x1eb8:  movl   %esp, %ebp
   0x1eba:  pushl  %ebx
   0x1ebb:  subl   $20, %esp
   0x1ebe:  calll  0x1ec3                   ; main + 12 at test.c:18
   0x1ec3:  popl   %ebx
-> 0x1ec4:  calll  0x1f12                   ; getpid
   0x1ec9:  movl   %eax, 4(%esp)
   0x1ecd:  leal   199(%ebx), %eax
   0x1ed3:  movl   %eax, (%esp)
   0x1ed6:  calll  0x1f18                   ; printf
   0x1edb:  leal   213(%ebx), %eax
   0x1ee1:  movl   %eax, (%esp)
   0x1ee4:  calll  0x1f1e                   ; puts
   0x1ee9:  calll  0x1f0c                   ; getchar
   0x1eee:  movl   $20, (%esp)
   0x1ef5:  calll  0x1e6a                   ; sleep_loop at test.c:6
   0x1efa:  movl   $12, %eax
   0x1eff:  addl   $20, %esp
   0x1f02:  popl   %ebx
   0x1f03:  leave
   0x1f04:  ret
   
This can be handy when dealing with the new --line options that was recently
added:

(lldb) disassemble --line
a.out`main + 13 at test.c:19
   18  	{
-> 19  		printf("Process: %i\n\n", getpid());
   20  	    puts("Press any key to continue..."); getchar();
-> 0x1ec4:  calll  0x1f12                   ; getpid
   0x1ec9:  movl   %eax, 4(%esp)
   0x1ecd:  leal   199(%ebx), %eax
   0x1ed3:  movl   %eax, (%esp)
   0x1ed6:  calll  0x1f18                   ; printf

Modified the ModuleList to have a lookup based solely on a UUID. Since the
UUID is typically the MD5 checksum of a binary image, there is no need
to give the path and architecture when searching for a pre-existing
image in an image list.

Now that we support remote debugging a bit better, our lldb_private::Module
needs to be able to track what the original path for file was as the platform
knows it, as well as where the file is locally. The module has the two 
following functions to retrieve both paths:

const FileSpec &Module::GetFileSpec () const;
const FileSpec &Module::GetPlatformFileSpec () const;

llvm-svn: 128563
2011-03-30 18:16:51 +00:00

598 lines
22 KiB
C++

//===-- CommandObjectSource.cpp ---------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CommandObjectSource.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Host/FileSpec.h"
#include "lldb/Target/Process.h"
#include "lldb/Core/SourceManager.h"
#include "lldb/Target/TargetList.h"
#include "lldb/Interpreter/CommandCompletions.h"
#include "lldb/Interpreter/Options.h"
using namespace lldb;
using namespace lldb_private;
//-------------------------------------------------------------------------
// CommandObjectSourceList
//-------------------------------------------------------------------------
class CommandObjectSourceInfo : public CommandObject
{
class CommandOptions : public Options
{
public:
CommandOptions () :
Options()
{
}
~CommandOptions ()
{
}
Error
SetOptionValue (int option_idx, const char *option_arg)
{
Error error;
const char short_option = g_option_table[option_idx].short_option;
switch (short_option)
{
case 'l':
start_line = Args::StringToUInt32 (option_arg, 0);
if (start_line == 0)
error.SetErrorStringWithFormat("Invalid line number: '%s'.\n", option_arg);
break;
case 'f':
file_name = option_arg;
break;
default:
error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option);
break;
}
return error;
}
void
ResetOptionValues ()
{
file_spec.Clear();
file_name.clear();
start_line = 0;
}
const OptionDefinition*
GetDefinitions ()
{
return g_option_table;
}
static OptionDefinition g_option_table[];
// Instance variables to hold the values for command options.
FileSpec file_spec;
std::string file_name;
uint32_t start_line;
};
public:
CommandObjectSourceInfo(CommandInterpreter &interpreter) :
CommandObject (interpreter,
"source info",
"Display information about the source lines from the current executable's debug info.",
"source info [<cmd-options>]")
{
}
~CommandObjectSourceInfo ()
{
}
Options *
GetOptions ()
{
return &m_options;
}
bool
Execute
(
Args& args,
CommandReturnObject &result
)
{
result.AppendError ("Not yet implemented");
result.SetStatus (eReturnStatusFailed);
return false;
}
protected:
CommandOptions m_options;
};
OptionDefinition
CommandObjectSourceInfo::CommandOptions::g_option_table[] =
{
{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
};
#pragma mark CommandObjectSourceList
//-------------------------------------------------------------------------
// CommandObjectSourceList
//-------------------------------------------------------------------------
class CommandObjectSourceList : public CommandObject
{
class CommandOptions : public Options
{
public:
CommandOptions () :
Options()
{
}
~CommandOptions ()
{
}
Error
SetOptionValue (int option_idx, const char *option_arg)
{
Error error;
const char short_option = g_option_table[option_idx].short_option;
switch (short_option)
{
case 'l':
start_line = Args::StringToUInt32 (option_arg, 0);
if (start_line == 0)
error.SetErrorStringWithFormat("Invalid line number: '%s'.\n", option_arg);
break;
case 'c':
num_lines = Args::StringToUInt32 (option_arg, 0);
if (num_lines == 0)
error.SetErrorStringWithFormat("Invalid line count: '%s'.\n", option_arg);
break;
case 'f':
file_name = option_arg;
break;
case 'n':
symbol_name = option_arg;
break;
case 's':
m_modules.push_back (std::string (option_arg));
break;
default:
error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option);
break;
}
return error;
}
void
ResetOptionValues ()
{
file_spec.Clear();
file_name.clear();
symbol_name.clear();
start_line = 0;
num_lines = 10;
m_modules.clear();
}
const OptionDefinition*
GetDefinitions ()
{
return g_option_table;
}
static OptionDefinition g_option_table[];
// Instance variables to hold the values for command options.
FileSpec file_spec;
std::string file_name;
std::string symbol_name;
uint32_t start_line;
uint32_t num_lines;
STLStringArray m_modules;
};
public:
CommandObjectSourceList(CommandInterpreter &interpreter) :
CommandObject (interpreter,
"source list",
"Display source code (as specified) based on the current executable's debug info.",
NULL)
{
CommandArgumentEntry arg;
CommandArgumentData file_arg;
// Define the first (and only) variant of this arg.
file_arg.arg_type = eArgTypeFilename;
file_arg.arg_repetition = eArgRepeatOptional;
// There is only one variant this argument could be; put it into the argument entry.
arg.push_back (file_arg);
// Push the data for the first argument into the m_arguments vector.
m_arguments.push_back (arg);
}
~CommandObjectSourceList ()
{
}
Options *
GetOptions ()
{
return &m_options;
}
bool
Execute
(
Args& args,
CommandReturnObject &result
)
{
const int argc = args.GetArgumentCount();
if (argc != 0)
{
result.AppendErrorWithFormat("'%s' takes no arguments, only flags.\n", GetCommandName());
result.SetStatus (eReturnStatusFailed);
}
ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
if (!m_options.symbol_name.empty())
{
// Displaying the source for a symbol:
Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
if (target == NULL)
{
result.AppendError ("invalid target, set executable file using 'file' command");
result.SetStatus (eReturnStatusFailed);
return false;
}
SymbolContextList sc_list;
ConstString name(m_options.symbol_name.c_str());
bool include_symbols = false;
bool append = true;
size_t num_matches = 0;
if (m_options.m_modules.size() > 0)
{
ModuleList matching_modules;
for (unsigned i = 0, e = m_options.m_modules.size(); i != e; i++)
{
FileSpec module_spec(m_options.m_modules[i].c_str(), false);
if (module_spec)
{
matching_modules.Clear();
target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules);
num_matches += matching_modules.FindFunctions (name, eFunctionNameTypeBase, include_symbols, append, sc_list);
}
}
}
else
{
num_matches = target->GetImages().FindFunctions (name, eFunctionNameTypeBase, include_symbols, append, sc_list);
}
SymbolContext sc;
if (num_matches == 0)
{
result.AppendErrorWithFormat("Could not find function named: \"%s\".\n", m_options.symbol_name.c_str());
result.SetStatus (eReturnStatusFailed);
return false;
}
sc_list.GetContextAtIndex (0, sc);
FileSpec start_file;
uint32_t start_line;
uint32_t end_line;
FileSpec end_file;
if (sc.function != NULL)
{
sc.function->GetStartLineSourceInfo (start_file, start_line);
if (start_line == 0)
{
result.AppendErrorWithFormat("Could not find line information for start of function: \"%s\".\n", m_options.symbol_name.c_str());
result.SetStatus (eReturnStatusFailed);
return false;
}
sc.function->GetEndLineSourceInfo (end_file, end_line);
}
else
{
result.AppendErrorWithFormat("Could not find function info for: \"%s\".\n", m_options.symbol_name.c_str());
result.SetStatus (eReturnStatusFailed);
return false;
}
if (num_matches > 1)
{
// This could either be because there are multiple functions of this name, in which case
// we'll have to specify this further... Or it could be because there are multiple inlined instances
// of one function. So run through the matches and if they all have the same file & line then we can just
// list one.
bool found_multiple = false;
for (size_t i = 1; i < num_matches; i++)
{
SymbolContext scratch_sc;
sc_list.GetContextAtIndex (i, scratch_sc);
if (scratch_sc.function != NULL)
{
FileSpec scratch_file;
uint32_t scratch_line;
scratch_sc.function->GetStartLineSourceInfo (scratch_file, scratch_line);
if (scratch_file != start_file
|| scratch_line != start_line)
{
found_multiple = true;
break;
}
}
}
if (found_multiple)
{
StreamString s;
for (size_t i = 0; i < num_matches; i++)
{
SymbolContext scratch_sc;
sc_list.GetContextAtIndex (i, scratch_sc);
if (scratch_sc.function != NULL)
{
s.Printf("\n%d: ", i);
scratch_sc.function->Dump (&s, true);
}
}
result.AppendErrorWithFormat("Multiple functions found matching: %s: \n%s\n",
m_options.symbol_name.c_str(),
s.GetData());
result.SetStatus (eReturnStatusFailed);
return false;
}
}
// This is a little hacky, but the first line table entry for a function points to the "{" that
// starts the function block. It would be nice to actually get the function
// declaration in there too. So back up a bit, but not further than what you're going to display.
size_t lines_to_back_up = m_options.num_lines >= 10 ? 5 : m_options.num_lines/2;
uint32_t line_no;
if (start_line <= lines_to_back_up)
line_no = 1;
else
line_no = start_line - lines_to_back_up;
// For fun, if the function is shorter than the number of lines we're supposed to display,
// only display the function...
if (end_line != 0)
{
if (m_options.num_lines > end_line - line_no)
m_options.num_lines = end_line - line_no;
}
char path_buf[PATH_MAX+1];
start_file.GetPath(path_buf, PATH_MAX);
result.AppendMessageWithFormat("File: %s.\n", path_buf);
m_interpreter.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbers (start_file,
line_no,
0,
m_options.num_lines,
"",
&result.GetOutputStream());
result.SetStatus (eReturnStatusSuccessFinishResult);
return true;
}
else if (m_options.file_name.empty())
{
// Last valid source manager context, or the current frame if no
// valid last context in source manager.
// One little trick here, if you type the exact same list command twice in a row, it is
// more likely because you typed it once, then typed it again
if (m_options.start_line == 0)
{
if (m_interpreter.GetDebugger().GetSourceManager().DisplayMoreWithLineNumbers (&result.GetOutputStream()))
{
result.SetStatus (eReturnStatusSuccessFinishResult);
}
}
else
{
if (m_interpreter.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbersUsingLastFile(
m_options.start_line, // Line to display
0, // Lines before line to display
m_options.num_lines, // Lines after line to display
"", // Don't mark "line"
&result.GetOutputStream()))
{
result.SetStatus (eReturnStatusSuccessFinishResult);
}
}
}
else
{
const char *filename = m_options.file_name.c_str();
Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
if (target == NULL)
{
result.AppendError ("invalid target, set executable file using 'file' command");
result.SetStatus (eReturnStatusFailed);
return false;
}
bool check_inlines = false;
SymbolContextList sc_list;
size_t num_matches = 0;
if (m_options.m_modules.size() > 0)
{
ModuleList matching_modules;
for (unsigned i = 0, e = m_options.m_modules.size(); i != e; i++)
{
FileSpec module_spec(m_options.m_modules[i].c_str(), false);
if (module_spec)
{
matching_modules.Clear();
target->GetImages().FindModules (&module_spec, NULL, NULL, NULL, matching_modules);
num_matches += matching_modules.ResolveSymbolContextForFilePath (filename,
0,
check_inlines,
eSymbolContextModule | eSymbolContextCompUnit,
sc_list);
}
}
}
else
{
num_matches = target->GetImages().ResolveSymbolContextForFilePath (filename,
0,
check_inlines,
eSymbolContextModule | eSymbolContextCompUnit,
sc_list);
}
if (num_matches == 0)
{
result.AppendErrorWithFormat("Could not find source file \"%s\".\n",
m_options.file_name.c_str());
result.SetStatus (eReturnStatusFailed);
return false;
}
if (num_matches > 1)
{
SymbolContext sc;
bool got_multiple = false;
FileSpec *test_cu_spec = NULL;
for (unsigned i = 0; i < num_matches; i++)
{
sc_list.GetContextAtIndex(i, sc);
if (sc.comp_unit)
{
if (test_cu_spec)
{
if (test_cu_spec != static_cast<FileSpec *> (sc.comp_unit))
got_multiple = true;
break;
}
else
test_cu_spec = sc.comp_unit;
}
}
if (got_multiple)
{
result.AppendErrorWithFormat("Multiple source files found matching: \"%s.\"\n",
m_options.file_name.c_str());
result.SetStatus (eReturnStatusFailed);
return false;
}
}
SymbolContext sc;
if (sc_list.GetContextAtIndex(0, sc))
{
if (sc.comp_unit)
{
m_interpreter.GetDebugger().GetSourceManager().DisplaySourceLinesWithLineNumbers (sc.comp_unit,
m_options.start_line,
0,
m_options.num_lines,
"",
&result.GetOutputStream());
result.SetStatus (eReturnStatusSuccessFinishResult);
}
else
{
result.AppendErrorWithFormat("No comp unit found for: \"%s.\"\n",
m_options.file_name.c_str());
result.SetStatus (eReturnStatusFailed);
return false;
}
}
}
return result.Succeeded();
}
virtual const char *GetRepeatCommand (Args &current_command_args, uint32_t index)
{
return m_cmd_name.c_str();
}
protected:
CommandOptions m_options;
};
OptionDefinition
CommandObjectSourceList::CommandOptions::g_option_table[] =
{
{ LLDB_OPT_SET_ALL, false, "count", 'c', required_argument, NULL, 0, eArgTypeCount, "The number of source lines to display."},
{ LLDB_OPT_SET_ALL, false, "shlib", 's', required_argument, NULL, CommandCompletions::eModuleCompletion, eArgTypeShlibName, "Look up the source file in the given shared library."},
{ LLDB_OPT_SET_1, false, "file", 'f', required_argument, NULL, CommandCompletions::eSourceFileCompletion, eArgTypeFilename, "The file from which to display source."},
{ LLDB_OPT_SET_1, false, "line", 'l', required_argument, NULL, 0, eArgTypeLineNum, "The line number at which to start the display source."},
{ LLDB_OPT_SET_2, false, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeSymbol, "The name of a function whose source to display."},
{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
};
#pragma mark CommandObjectMultiwordSource
//-------------------------------------------------------------------------
// CommandObjectMultiwordSource
//-------------------------------------------------------------------------
CommandObjectMultiwordSource::CommandObjectMultiwordSource (CommandInterpreter &interpreter) :
CommandObjectMultiword (interpreter,
"source",
"A set of commands for accessing source file information",
"source <subcommand> [<subcommand-options>]")
{
LoadSubCommand ("info", CommandObjectSP (new CommandObjectSourceInfo (interpreter)));
LoadSubCommand ("list", CommandObjectSP (new CommandObjectSourceList (interpreter)));
}
CommandObjectMultiwordSource::~CommandObjectMultiwordSource ()
{
}