1 //===-- MICmdCmdFile.cpp ----------------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // Overview: CMICmdCmdFileExecAndSymbols implementation.
11 // Third Party Headers:
12 #include "lldb/API/SBStream.h"
15 #include "MICmdArgValFile.h"
16 #include "MICmdArgValOptionLong.h"
17 #include "MICmdArgValOptionShort.h"
18 #include "MICmdArgValString.h"
19 #include "MICmdCmdFile.h"
20 #include "MICmnLLDBDebugSessionInfo.h"
21 #include "MICmnLLDBDebugger.h"
22 #include "MICmnMIResultRecord.h"
23 #include "MIUtilFileStd.h"
26 // Details: CMICmdCmdFileExecAndSymbols constructor.
32 CMICmdCmdFileExecAndSymbols::CMICmdCmdFileExecAndSymbols()
33 : m_constStrArgNameFile("file"), m_constStrArgNamedPlatformName("p"),
34 m_constStrArgNamedRemotePath("r") {
35 // Command factory matches this name with that received from the stdin stream
36 m_strMiCmd = "file-exec-and-symbols";
38 // Required by the CMICmdFactory when registering *this command
39 m_pSelfCreatorFn = &CMICmdCmdFileExecAndSymbols::CreateSelf;
43 // Details: CMICmdCmdFileExecAndSymbols destructor.
44 // Type: Overrideable.
49 CMICmdCmdFileExecAndSymbols::~CMICmdCmdFileExecAndSymbols() {}
52 // Details: The invoker requires this function. The parses the command line
54 // arguments to extract values for each of those arguments.
57 // Return: MIstatus::success - Functional succeeded.
58 // MIstatus::failure - Functional failed.
61 bool CMICmdCmdFileExecAndSymbols::ParseArgs() {
62 m_setCmdArgs.Add(new CMICmdArgValFile(m_constStrArgNameFile, true, true));
64 new CMICmdArgValOptionShort(m_constStrArgNamedPlatformName, false, true,
65 CMICmdArgValListBase::eArgValType_String, 1));
66 m_setCmdArgs.Add(new CMICmdArgValOptionShort(
67 m_constStrArgNamedRemotePath, false, true,
68 CMICmdArgValListBase::eArgValType_StringQuotedNumberPath, 1));
69 return ParseValidateCmdOptions();
73 // Details: The invoker requires this function. The command does work in this
75 // The command is likely to communicate with the LLDB SBDebugger in
77 // Synopsis: -file-exec-and-symbols file
79 // http://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-File-Commands.html#GDB_002fMI-File-Commands
82 // Return: MIstatus::success - Functional succeeded.
83 // MIstatus::failure - Functional failed.
86 bool CMICmdCmdFileExecAndSymbols::Execute() {
87 CMICMDBASE_GETOPTION(pArgNamedFile, File, m_constStrArgNameFile);
88 CMICMDBASE_GETOPTION(pArgPlatformName, OptionShort,
89 m_constStrArgNamedPlatformName);
90 CMICMDBASE_GETOPTION(pArgRemotePath, OptionShort,
91 m_constStrArgNamedRemotePath);
92 CMICmdArgValFile *pArgFile = static_cast<CMICmdArgValFile *>(pArgNamedFile);
93 const CMIUtilString &strExeFilePath(pArgFile->GetValue());
94 bool bPlatformName = pArgPlatformName->GetFound();
95 CMIUtilString platformName;
97 pArgPlatformName->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
100 CMICmnLLDBDebugSessionInfo &rSessionInfo(
101 CMICmnLLDBDebugSessionInfo::Instance());
102 lldb::SBDebugger &rDbgr = rSessionInfo.GetDebugger();
104 const char *pTargetTriple = nullptr; // Let LLDB discover the triple required
105 const char *pTargetPlatformName = platformName.c_str();
106 const bool bAddDepModules = false;
107 lldb::SBTarget target =
108 rDbgr.CreateTarget(strExeFilePath.c_str(), pTargetTriple,
109 pTargetPlatformName, bAddDepModules, error);
110 CMIUtilString strWkDir;
111 const CMIUtilString &rStrKeyWkDir(rSessionInfo.m_constStrSharedDataKeyWkDir);
112 if (!rSessionInfo.SharedDataRetrieve<CMIUtilString>(rStrKeyWkDir, strWkDir)) {
113 strWkDir = CMIUtilFileStd::StripOffFileName(strExeFilePath);
114 if (!rSessionInfo.SharedDataAdd<CMIUtilString>(rStrKeyWkDir, strWkDir)) {
115 SetError(CMIUtilString::Format(MIRSRC(IDS_DBGSESSION_ERR_SHARED_DATA_ADD),
116 m_cmdData.strMiCmd.c_str(),
117 rStrKeyWkDir.c_str()));
118 return MIstatus::failure;
121 if (!rDbgr.SetCurrentPlatformSDKRoot(strWkDir.c_str())) {
123 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_FNFAILED),
124 m_cmdData.strMiCmd.c_str(),
125 "SetCurrentPlatformSDKRoot()"));
126 return MIstatus::failure;
128 if (pArgRemotePath->GetFound()) {
129 CMIUtilString remotePath;
130 pArgRemotePath->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
132 lldb::SBModule module = target.FindModule(target.GetExecutable());
133 if (module.IsValid()) {
134 module.SetPlatformFileSpec(lldb::SBFileSpec(remotePath.c_str()));
139 const bool bOk = error.GetDescription(err);
142 if (!target.IsValid()) {
143 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INVALID_TARGET),
144 m_cmdData.strMiCmd.c_str(),
145 strExeFilePath.c_str(), err.GetData()));
146 return MIstatus::failure;
149 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_CREATE_TARGET),
150 m_cmdData.strMiCmd.c_str(), err.GetData()));
151 return MIstatus::failure;
154 return MIstatus::success;
158 // Details: The invoker requires this function. The command prepares a MI Record
160 // for the work carried out in the Execute().
163 // Return: MIstatus::success - Functional succeeded.
164 // MIstatus::failure - Functional failed.
167 bool CMICmdCmdFileExecAndSymbols::Acknowledge() {
168 const CMICmnMIResultRecord miRecordResult(
169 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
170 m_miResultRecord = miRecordResult;
172 return MIstatus::success;
176 // Details: Required by the CMICmdFactory when registering *this command. The
178 // calls this function to create an instance of *this command.
179 // Type: Static method.
181 // Return: CMICmdBase * - Pointer to a new command.
184 CMICmdBase *CMICmdCmdFileExecAndSymbols::CreateSelf() {
185 return new CMICmdCmdFileExecAndSymbols();
189 // Details: If the MI Driver is not operating via a client i.e. Eclipse but say
191 // on a executable passed in as a argument to the drive then what
193 // do on a command failing? Either continue operating or exit the
195 // Override this function where a command failure cannot allow the
197 // continue operating.
200 // Return: bool - True = Fatal if command fails, false = can continue if
204 bool CMICmdCmdFileExecAndSymbols::GetExitAppOnCommandFailure() const {