1 //===-- MICmdCmdFile.cpp ----------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // Overview: CMICmdCmdFileExecAndSymbols implementation.
11
12 // Third Party Headers:
13 #include "lldb/API/SBStream.h"
14
15 // In-house headers:
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"
25
26 //++
27 //------------------------------------------------------------------------------------
28 // Details: CMICmdCmdFileExecAndSymbols constructor.
29 // Type: Method.
30 // Args: None.
31 // Return: None.
32 // Throws: None.
33 //--
CMICmdCmdFileExecAndSymbols()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";
39
40 // Required by the CMICmdFactory when registering *this command
41 m_pSelfCreatorFn = &CMICmdCmdFileExecAndSymbols::CreateSelf;
42 }
43
44 //++
45 //------------------------------------------------------------------------------------
46 // Details: CMICmdCmdFileExecAndSymbols destructor.
47 // Type: Overrideable.
48 // Args: None.
49 // Return: None.
50 // Throws: None.
51 //--
~CMICmdCmdFileExecAndSymbols()52 CMICmdCmdFileExecAndSymbols::~CMICmdCmdFileExecAndSymbols() {}
53
54 //++
55 //------------------------------------------------------------------------------------
56 // Details: The invoker requires this function. The parses the command line
57 // options
58 // arguments to extract values for each of those arguments.
59 // Type: Overridden.
60 // Args: None.
61 // Return: MIstatus::success - Functional succeeded.
62 // MIstatus::failure - Functional failed.
63 // Throws: None.
64 //--
ParseArgs()65 bool CMICmdCmdFileExecAndSymbols::ParseArgs() {
66 m_setCmdArgs.Add(new CMICmdArgValFile(m_constStrArgNameFile, true, true));
67 m_setCmdArgs.Add(
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();
74 }
75
76 //++
77 //------------------------------------------------------------------------------------
78 // Details: The invoker requires this function. The command does work in this
79 // function.
80 // The command is likely to communicate with the LLDB SBDebugger in
81 // here.
82 // Synopsis: -file-exec-and-symbols file
83 // Ref:
84 // http://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-File-Commands.html#GDB_002fMI-File-Commands
85 // Type: Overridden.
86 // Args: None.
87 // Return: MIstatus::success - Functional succeeded.
88 // MIstatus::failure - Functional failed.
89 // Throws: None.
90 //--
Execute()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;
101 if (bPlatformName) {
102 pArgPlatformName->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
103 platformName);
104 }
105 CMICmnLLDBDebugSessionInfo &rSessionInfo(
106 CMICmnLLDBDebugSessionInfo::Instance());
107 lldb::SBDebugger &rDbgr = rSessionInfo.GetDebugger();
108 lldb::SBError error;
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;
124 }
125 }
126 if (!rDbgr.SetCurrentPlatformSDKRoot(strWkDir.c_str())) {
127
128 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_FNFAILED),
129 m_cmdData.strMiCmd.c_str(),
130 "SetCurrentPlatformSDKRoot()"));
131 return MIstatus::failure;
132 }
133 if (pArgRemotePath->GetFound()) {
134 CMIUtilString remotePath;
135 pArgRemotePath->GetExpectedOption<CMICmdArgValString, CMIUtilString>(
136 remotePath);
137 lldb::SBModule module = target.FindModule(target.GetExecutable());
138 if (module.IsValid()) {
139 module.SetPlatformFileSpec(lldb::SBFileSpec(remotePath.c_str()));
140 }
141 }
142 lldb::SBStream err;
143 if (error.Fail()) {
144 const bool bOk = error.GetDescription(err);
145 MIunused(bOk);
146 }
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;
152 }
153 if (error.Fail()) {
154 SetError(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_CREATE_TARGET),
155 m_cmdData.strMiCmd.c_str(), err.GetData()));
156 return MIstatus::failure;
157 }
158
159 return MIstatus::success;
160 }
161
162 //++
163 //------------------------------------------------------------------------------------
164 // Details: The invoker requires this function. The command prepares a MI Record
165 // Result
166 // for the work carried out in the Execute().
167 // Type: Overridden.
168 // Args: None.
169 // Return: MIstatus::success - Functional succeeded.
170 // MIstatus::failure - Functional failed.
171 // Throws: None.
172 //--
Acknowledge()173 bool CMICmdCmdFileExecAndSymbols::Acknowledge() {
174 const CMICmnMIResultRecord miRecordResult(
175 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
176 m_miResultRecord = miRecordResult;
177
178 return MIstatus::success;
179 }
180
181 //++
182 //------------------------------------------------------------------------------------
183 // Details: Required by the CMICmdFactory when registering *this command. The
184 // factory
185 // calls this function to create an instance of *this command.
186 // Type: Static method.
187 // Args: None.
188 // Return: CMICmdBase * - Pointer to a new command.
189 // Throws: None.
190 //--
CreateSelf()191 CMICmdBase *CMICmdCmdFileExecAndSymbols::CreateSelf() {
192 return new CMICmdCmdFileExecAndSymbols();
193 }
194
195 //++
196 //------------------------------------------------------------------------------------
197 // Details: If the MI Driver is not operating via a client i.e. Eclipse but say
198 // operating
199 // on a executable passed in as a argument to the drive then what
200 // should the driver
201 // do on a command failing? Either continue operating or exit the
202 // application.
203 // Override this function where a command failure cannot allow the
204 // driver to
205 // continue operating.
206 // Type: Overridden.
207 // Args: None.
208 // Return: bool - True = Fatal if command fails, false = can continue if
209 // command fails.
210 // Throws: None.
211 //--
GetExitAppOnCommandFailure() const212 bool CMICmdCmdFileExecAndSymbols::GetExitAppOnCommandFailure() const {
213 return true;
214 }
215