1 //===-- MICmdCmdFile.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Overview: CMICmdCmdFileExecAndSymbols implementation.
12 // Third Party Headers:
13 #include "lldb/API/SBStream.h"
16 #include "MICmdArgValFile.h"
17 #include "MICmdArgValOptionLong.h"
18 #include "MICmdArgValOptionShort.h"
19 #include "MICmdArgValString.h"
20 #include "MICmdCmdFile.h"
21 #include "MICmnLLDBDebugSessionInfo.h"
22 #include "MICmnLLDBDebugger.h"
23 #include "MICmnMIResultRecord.h"
24 #include "MIUtilFileStd.h"
27 //------------------------------------------------------------------------------------
28 // Details: CMICmdCmdFileExecAndSymbols constructor.
34 CMICmdCmdFileExecAndSymbols::CMICmdCmdFileExecAndSymbols()
35 : m_constStrArgNameFile("file"), m_constStrArgNamedPlatformName("p"),
36 m_constStrArgNamedRemotePath("r") {
37 // Command factory matches this name with that received from the stdin stream
38 m_strMiCmd = "file-exec-and-symbols";
40 // Required by the CMICmdFactory when registering *this command
41 m_pSelfCreatorFn = &CMICmdCmdFileExecAndSymbols::CreateSelf;
45 //------------------------------------------------------------------------------------
46 // Details: CMICmdCmdFileExecAndSymbols destructor.
47 // Type: Overrideable.
52 CMICmdCmdFileExecAndSymbols::~CMICmdCmdFileExecAndSymbols() {}
55 //------------------------------------------------------------------------------------
56 // Details: The invoker requires this function. The parses the command line
58 // arguments to extract values for each of those arguments.
61 // Return: MIstatus::success - Functional succeeded.
62 // MIstatus::failure - Functional failed.
65 bool CMICmdCmdFileExecAndSymbols::ParseArgs() {
66 m_setCmdArgs.Add(new CMICmdArgValFile(m_constStrArgNameFile, true, true));
68 new CMICmdArgValOptionShort(m_constStrArgNamedPlatformName, false, true,
69 CMICmdArgValListBase::eArgValType_String, 1));
70 m_setCmdArgs.Add(new CMICmdArgValOptionShort(
71 m_constStrArgNamedRemotePath, false, true,
72 CMICmdArgValListBase::eArgValType_StringQuotedNumberPath, 1));
73 return ParseValidateCmdOptions();
77 //------------------------------------------------------------------------------------
78 // Details: The invoker requires this function. The command does work in this
80 // The command is likely to communicate with the LLDB SBDebugger in
82 // Synopsis: -file-exec-and-symbols file
84 // http://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-File-Commands.html#GDB_002fMI-File-Commands
87 // Return: MIstatus::success - Functional succeeded.
88 // MIstatus::failure - Functional failed.
91 bool CMICmdCmdFileExecAndSymbols::Execute() {
92 CMICMDBASE_GETOPTION(pArgNamedFile, File, m_constStrArgNameFile);
93 CMICMDBASE_GETOPTION(pArgPlatformName, OptionShort,
94 m_constStrArgNamedPlatformName);
95 CMICMDBASE_GETOPTION(pArgRemotePath, OptionShort,
96 m_constStrArgNamedRemotePath);
97 CMICmdArgValFile *pArgFile = static_cast<CMICmdArgValFile *>(pArgNamedFile);
98 const CMIUtilString &strExeFilePath(pArgFile->GetValue());
99 bool bPlatformName = pArgPlatformName->GetFound();
100 CMIUtilString platformName;
102 pArgPlatformName->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
105 CMICmnLLDBDebugSessionInfo &rSessionInfo(
106 CMICmnLLDBDebugSessionInfo::Instance());
107 lldb::SBDebugger &rDbgr = rSessionInfo.GetDebugger();
109 const char *pTargetTriple = nullptr; // Let LLDB discover the triple required
110 const char *pTargetPlatformName = platformName.c_str();
111 const bool bAddDepModules = false;
112 lldb::SBTarget target =
113 rDbgr.CreateTarget(strExeFilePath.c_str(), pTargetTriple,
114 pTargetPlatformName, bAddDepModules, error);
115 CMIUtilString strWkDir;
116 const CMIUtilString &rStrKeyWkDir(rSessionInfo.m_constStrSharedDataKeyWkDir);
117 if (!rSessionInfo.SharedDataRetrieve<CMIUtilString>(rStrKeyWkDir, strWkDir)) {
118 strWkDir = CMIUtilFileStd::StripOffFileName(strExeFilePath);
119 if (!rSessionInfo.SharedDataAdd<CMIUtilString>(rStrKeyWkDir, strWkDir)) {
120 SetError(CMIUtilString::Format(MIRSRC(IDS_DBGSESSION_ERR_SHARED_DATA_ADD),
121 m_cmdData.strMiCmd.c_str(),
122 rStrKeyWkDir.c_str()));
123 return MIstatus::failure;
126 if (!rDbgr.SetCurrentPlatformSDKRoot(strWkDir.c_str())) {
128 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_FNFAILED),
129 m_cmdData.strMiCmd.c_str(),
130 "SetCurrentPlatformSDKRoot()"));
131 return MIstatus::failure;
133 if (pArgRemotePath->GetFound()) {
134 CMIUtilString remotePath;
135 pArgRemotePath->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
137 lldb::SBModule module = target.FindModule(target.GetExecutable());
138 if (module.IsValid()) {
139 module.SetPlatformFileSpec(lldb::SBFileSpec(remotePath.c_str()));
144 const bool bOk = error.GetDescription(err);
147 if (!target.IsValid()) {
148 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INVALID_TARGET),
149 m_cmdData.strMiCmd.c_str(),
150 strExeFilePath.c_str(), err.GetData()));
151 return MIstatus::failure;
154 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_CREATE_TARGET),
155 m_cmdData.strMiCmd.c_str(), err.GetData()));
156 return MIstatus::failure;
159 return MIstatus::success;
163 //------------------------------------------------------------------------------------
164 // Details: The invoker requires this function. The command prepares a MI Record
166 // for the work carried out in the Execute().
169 // Return: MIstatus::success - Functional succeeded.
170 // MIstatus::failure - Functional failed.
173 bool CMICmdCmdFileExecAndSymbols::Acknowledge() {
174 const CMICmnMIResultRecord miRecordResult(
175 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
176 m_miResultRecord = miRecordResult;
178 return MIstatus::success;
182 //------------------------------------------------------------------------------------
183 // Details: Required by the CMICmdFactory when registering *this command. The
185 // calls this function to create an instance of *this command.
186 // Type: Static method.
188 // Return: CMICmdBase * - Pointer to a new command.
191 CMICmdBase *CMICmdCmdFileExecAndSymbols::CreateSelf() {
192 return new CMICmdCmdFileExecAndSymbols();
196 //------------------------------------------------------------------------------------
197 // Details: If the MI Driver is not operating via a client i.e. Eclipse but say
199 // on a executable passed in as a argument to the drive then what
201 // do on a command failing? Either continue operating or exit the
203 // Override this function where a command failure cannot allow the
205 // continue operating.
208 // Return: bool - True = Fatal if command fails, false = can continue if
212 bool CMICmdCmdFileExecAndSymbols::GetExitAppOnCommandFailure() const {