llvm-project/lldb/source/Commands/CommandObjectFile.cpp
Greg Clayton 6611103cfe Very large changes that were needed in order to allow multiple connections
to the debugger from GUI windows. Previously there was one global debugger
instance that could be accessed that had its own command interpreter and
current state (current target/process/thread/frame). When a GUI debugger
was attached, if it opened more than one window that each had a console
window, there were issues where the last one to setup the global debugger
object won and got control of the debugger.

To avoid this we now create instances of the lldb_private::Debugger that each 
has its own state:
- target list for targets the debugger instance owns
- current process/thread/frame
- its own command interpreter
- its own input, output and error file handles to avoid conflicts
- its own input reader stack

So now clients should call:

    SBDebugger::Initialize(); // (static function)

    SBDebugger debugger (SBDebugger::Create());
    // Use which ever file handles you wish
    debugger.SetErrorFileHandle (stderr, false);
    debugger.SetOutputFileHandle (stdout, false);
    debugger.SetInputFileHandle (stdin, true);

    // main loop
    
    SBDebugger::Terminate(); // (static function)
    
SBDebugger::Initialize() and SBDebugger::Terminate() are ref counted to
ensure nothing gets destroyed too early when multiple clients might be
attached.

Cleaned up the command interpreter and the CommandObject and all subclasses
to take more appropriate arguments.

llvm-svn: 106615
2010-06-23 01:19:29 +00:00

169 lines
4.6 KiB
C++

//===-- CommandObjectFile.cpp -----------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "CommandObjectFile.h"
// C Includes
// C++ Includes
// Other libraries and framework includes
// Project includes
#include "lldb/Interpreter/Args.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Core/Timer.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Interpreter/CommandInterpreter.h"
#include "lldb/Interpreter/CommandReturnObject.h"
#include "lldb/Target/Process.h"
using namespace lldb;
using namespace lldb_private;
CommandObjectFile::CommandOptions::CommandOptions() :
Options (),
m_arch () // Breakpoint info defaults to brief descriptions
{
}
CommandObjectFile::CommandOptions::~CommandOptions ()
{
}
lldb::OptionDefinition
CommandObjectFile::CommandOptions::g_option_table[] =
{
{ LLDB_OPT_SET_1, false, "arch", 'a', required_argument, NULL, 0, "<arch>", "Specify the architecture to launch."},
{ 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
};
const lldb::OptionDefinition *
CommandObjectFile::CommandOptions::GetDefinitions ()
{
return g_option_table;
}
Error
CommandObjectFile::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
{
Error error;
char short_option = (char) m_getopt_table[option_idx].val;
switch (short_option)
{
case 'a':
{
ArchSpec option_arch (option_arg);
if (option_arch.IsValid())
m_arch = option_arch;
else
error.SetErrorStringWithFormat ("Invalid arch string '%s'.\n", optarg);
}
break;
default:
error.SetErrorStringWithFormat ("Unrecognized option '%c'.\n", short_option);
break;
}
return error;
}
void
CommandObjectFile::CommandOptions::ResetOptionValues ()
{
Options::ResetOptionValues();
m_arch.Clear();
}
//-------------------------------------------------------------------------
// CommandObjectFile
//-------------------------------------------------------------------------
CommandObjectFile::CommandObjectFile() :
CommandObject ("file",
"Sets the file to be used as the main executable by the debugger.",
"file [<cmd-options>] <filename>")
{
}
CommandObjectFile::~CommandObjectFile ()
{
}
Options *
CommandObjectFile::GetOptions ()
{
return &m_options;
}
bool
CommandObjectFile::Execute
(
CommandInterpreter &interpreter,
Args& command,
CommandReturnObject &result
)
{
const char *file_path = command.GetArgumentAtIndex(0);
Timer scoped_timer(__PRETTY_FUNCTION__, "(dbg) file '%s'", file_path);
const int argc = command.GetArgumentCount();
if (argc == 1)
{
FileSpec file_spec (file_path);
if (! file_spec.Exists())
{
result.AppendErrorWithFormat ("File '%s' does not exist.\n", file_path);
result.SetStatus (eReturnStatusFailed);
return result.Succeeded();
}
TargetSP target_sp;
ArchSpec arch;
if (m_options.m_arch.IsValid())
arch = m_options.m_arch;
else
{
arch = lldb_private::GetDefaultArchitecture ();
if (!arch.IsValid())
arch = LLDB_ARCH_DEFAULT;
}
Debugger &debugger = interpreter.GetDebugger();
Error error = debugger.GetTargetList().CreateTarget (debugger, file_spec, arch, NULL, true, target_sp);
if (error.Fail() && !m_options.m_arch.IsValid())
{
if (arch == LLDB_ARCH_DEFAULT_32BIT)
arch = LLDB_ARCH_DEFAULT_64BIT;
else
arch = LLDB_ARCH_DEFAULT_32BIT;
error = debugger.GetTargetList().CreateTarget (debugger, file_spec, arch, NULL, true, target_sp);
}
if (target_sp)
{
debugger.GetTargetList().SetCurrentTarget(target_sp.get());
result.AppendMessageWithFormat ("Current executable set to '%s' (%s).\n", file_path, arch.AsCString());
result.SetStatus (eReturnStatusSuccessFinishNoResult);
}
else
{
result.AppendError(error.AsCString());
result.SetStatus (eReturnStatusFailed);
}
}
else
{
result.AppendErrorWithFormat("'%s' takes exactly one executable path argument.\n", m_cmd_name.c_str());
result.SetStatus (eReturnStatusFailed);
}
return result.Succeeded();
}