1 //===-- MICmdCmdGdbShow.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: CMICmdCmdGdbShow implementation.
12 // Third party headers:
13 #include "lldb/API/SBCompileUnit.h"
14 #include "lldb/API/SBFrame.h"
15 #include "lldb/API/SBLanguageRuntime.h"
16 #include "lldb/API/SBThread.h"
19 #include "MICmdArgValListOfN.h"
20 #include "MICmdArgValOptionLong.h"
21 #include "MICmdArgValString.h"
22 #include "MICmdCmdGdbShow.h"
23 #include "MICmnLLDBDebugSessionInfo.h"
24 #include "MICmnMIResultRecord.h"
25 #include "MICmnMIValueConst.h"
28 const CMICmdCmdGdbShow::MapGdbOptionNameToFnGdbOptionPtr_t
29 CMICmdCmdGdbShow::ms_mapGdbOptionNameToFnGdbOptionPtr = {
30 {"target-async", &CMICmdCmdGdbShow::OptionFnTargetAsync},
31 {"print", &CMICmdCmdGdbShow::OptionFnPrint},
32 {"language", &CMICmdCmdGdbShow::OptionFnLanguage},
33 {"fallback", &CMICmdCmdGdbShow::OptionFnFallback}};
36 //------------------------------------------------------------------------------------
37 // Details: CMICmdCmdGdbShow constructor.
43 CMICmdCmdGdbShow::CMICmdCmdGdbShow()
44 : m_constStrArgNamedGdbOption("option"), m_bGdbOptionRecognised(true),
45 m_bGdbOptionFnSuccessful(false), m_bGbbOptionFnHasError(false),
46 m_strGdbOptionFnError(MIRSRC(IDS_WORD_ERR_MSG_NOT_IMPLEMENTED_BRKTS)) {
47 // Command factory matches this name with that received from the stdin stream
48 m_strMiCmd = "gdb-show";
50 // Required by the CMICmdFactory when registering *this command
51 m_pSelfCreatorFn = &CMICmdCmdGdbShow::CreateSelf;
55 //------------------------------------------------------------------------------------
56 // Details: CMICmdCmdGdbShow destructor.
57 // Type: Overrideable.
62 CMICmdCmdGdbShow::~CMICmdCmdGdbShow() {}
65 //------------------------------------------------------------------------------------
66 // Details: The invoker requires this function. The parses the command line
68 // arguments to extract values for each of those arguments.
71 // Return: MIstatus::success - Function succeeded.
72 // MIstatus::failure - Function failed.
75 bool CMICmdCmdGdbShow::ParseArgs() {
76 m_setCmdArgs.Add(new CMICmdArgValListOfN(
77 m_constStrArgNamedGdbOption, true, true,
78 CMICmdArgValListBase::eArgValType_StringAnything));
79 return ParseValidateCmdOptions();
83 //------------------------------------------------------------------------------------
84 // Details: The invoker requires this function. The command is executed in this
88 // Return: MIstatus::success - Function succeeded.
89 // MIstatus::failure - Function failed.
92 bool CMICmdCmdGdbShow::Execute() {
93 CMICMDBASE_GETOPTION(pArgGdbOption, ListOfN, m_constStrArgNamedGdbOption);
94 const CMICmdArgValListBase::VecArgObjPtr_t &rVecWords(
95 pArgGdbOption->GetExpectedOptions());
97 // Get the gdb-show option to carry out. This option will be used as an action
98 // which should be done. Further arguments will be used as parameters for it.
99 CMICmdArgValListBase::VecArgObjPtr_t::const_iterator it = rVecWords.begin();
100 const CMICmdArgValString *pOption =
101 static_cast<const CMICmdArgValString *>(*it);
102 const CMIUtilString strOption(pOption->GetValue());
105 // Retrieve the parameter(s) for the option
106 CMIUtilString::VecString_t vecWords;
107 while (it != rVecWords.end()) {
108 const CMICmdArgValString *pWord =
109 static_cast<const CMICmdArgValString *>(*it);
110 vecWords.push_back(pWord->GetValue());
116 FnGdbOptionPtr pPrintRequestFn = nullptr;
117 if (!GetOptionFn(strOption, pPrintRequestFn)) {
118 // For unimplemented option handlers, fallback to a generic handler
119 // ToDo: Remove this when ALL options have been implemented
120 if (!GetOptionFn("fallback", pPrintRequestFn)) {
121 m_bGdbOptionRecognised = false;
122 m_strGdbOptionName = "fallback"; // This would be the strOption name
123 return MIstatus::success;
127 m_bGdbOptionFnSuccessful = (this->*(pPrintRequestFn))(vecWords);
128 if (!m_bGdbOptionFnSuccessful && !m_bGbbOptionFnHasError)
129 return MIstatus::failure;
131 return MIstatus::success;
135 //------------------------------------------------------------------------------------
136 // Details: The invoker requires this function. The command prepares a MI Record
138 // for the work carried out in the Execute() method.
141 // Return: MIstatus::success - Function succeeded.
142 // MIstatus::failure - Function failed.
145 bool CMICmdCmdGdbShow::Acknowledge() {
146 // Print error if option isn't recognized:
147 // ^error,msg="The request '%s' was not recognized, not implemented"
148 if (!m_bGdbOptionRecognised) {
149 const CMICmnMIValueConst miValueConst(
150 CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INFO_PRINTFN_NOT_FOUND),
151 m_strGdbOptionName.c_str()));
152 const CMICmnMIValueResult miValueResult("msg", miValueConst);
153 const CMICmnMIResultRecord miRecordResult(
154 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error,
156 m_miResultRecord = miRecordResult;
157 return MIstatus::success;
161 if (m_bGdbOptionFnSuccessful && !m_strValue.empty()) {
162 const CMICmnMIValueConst miValueConst(m_strValue);
163 const CMICmnMIValueResult miValueResult("value", miValueConst);
164 const CMICmnMIResultRecord miRecordResult(
165 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done,
167 m_miResultRecord = miRecordResult;
168 return MIstatus::success;
169 } else if (m_bGdbOptionFnSuccessful) {
170 // Ignore empty value (for fallback)
171 const CMICmnMIResultRecord miRecordResult(
172 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
173 m_miResultRecord = miRecordResult;
174 return MIstatus::success;
177 // Print error if request failed:
178 // ^error,msg="The request '%s' failed.
179 const CMICmnMIValueConst miValueConst(CMIUtilString::Format(
180 MIRSRC(IDS_CMD_ERR_INFO_PRINTFN_FAILED), m_strGdbOptionFnError.c_str()));
181 const CMICmnMIValueResult miValueResult("msg", miValueConst);
182 const CMICmnMIResultRecord miRecordResult(
183 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error,
185 m_miResultRecord = miRecordResult;
187 return MIstatus::success;
191 //------------------------------------------------------------------------------------
192 // Details: Required by the CMICmdFactory when registering *this command. The
194 // calls this function to create an instance of *this command.
195 // Type: Static method.
197 // Return: CMICmdBase * - Pointer to a new command.
200 CMICmdBase *CMICmdCmdGdbShow::CreateSelf() { return new CMICmdCmdGdbShow(); }
203 //------------------------------------------------------------------------------------
204 // Details: Retrieve the print function's pointer for the matching print
207 // Args: vrPrintFnName - (R) The info requested.
208 // vrwpFn - (W) The print function's pointer of the function
210 // Return: bool - True = Print request is implemented, false = not found.
213 bool CMICmdCmdGdbShow::GetOptionFn(const CMIUtilString &vrPrintFnName,
214 FnGdbOptionPtr &vrwpFn) const {
217 const MapGdbOptionNameToFnGdbOptionPtr_t::const_iterator it =
218 ms_mapGdbOptionNameToFnGdbOptionPtr.find(vrPrintFnName);
219 if (it != ms_mapGdbOptionNameToFnGdbOptionPtr.end()) {
220 vrwpFn = (*it).second;
228 //------------------------------------------------------------------------------------
229 // Details: Carry out work to complete the GDB show option 'target-async' to
231 // and send back the requested information.
233 // Args: vrWords - (R) List of additional parameters used by this option.
234 // Return: MIstatus::success - Function succeeded.
235 // MIstatus::failure - Function failed.
238 bool CMICmdCmdGdbShow::OptionFnTargetAsync(
239 const CMIUtilString::VecString_t &vrWords) {
243 CMICmnLLDBDebugSessionInfo &rSessionInfo(
244 CMICmnLLDBDebugSessionInfo::Instance());
245 const bool bAsyncMode = rSessionInfo.GetDebugger().GetAsync();
247 m_strValue = bAsyncMode ? "on" : "off";
248 return MIstatus::success;
252 //------------------------------------------------------------------------------------
253 // Details: Carry out work to complete the GDB show option 'print' to prepare
255 // back the requested information.
257 // Args: vrWords - (R) List of additional parameters used by this option.
258 // Return: MIstatus::success - Function succeeded.
259 // MIstatus::failure - Function failed.
262 bool CMICmdCmdGdbShow::OptionFnPrint(
263 const CMIUtilString::VecString_t &vrWords) {
264 const bool bAllArgs(vrWords.size() == 1);
266 m_bGbbOptionFnHasError = true;
267 m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS);
268 return MIstatus::failure;
271 const CMIUtilString strOption(vrWords[0]);
272 CMIUtilString strOptionKey;
273 bool bOptionValueDefault = false;
274 if (CMIUtilString::Compare(strOption, "char-array-as-string"))
275 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintCharArrayAsString;
276 else if (CMIUtilString::Compare(strOption, "expand-aggregates"))
277 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintExpandAggregates;
278 else if (CMIUtilString::Compare(strOption, "aggregate-field-names")) {
279 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintAggregateFieldNames;
280 bOptionValueDefault = true;
282 m_bGbbOptionFnHasError = true;
283 m_strGdbOptionFnError = CMIUtilString::Format(
284 MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION),
286 return MIstatus::failure;
289 bool bOptionValue = false;
290 bOptionValue = bOptionValueDefault
291 ? !m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(
292 strOptionKey, bOptionValue) ||
294 : m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(
295 strOptionKey, bOptionValue) &&
298 m_strValue = bOptionValue ? "on" : "off";
299 return MIstatus::success;
303 //------------------------------------------------------------------------------------
304 // Details: Carry out work to complete the GDB show option 'language' to prepare
305 // and send back the requested information.
307 // Args: vrWords - (R) List of additional parameters used by this option.
308 // Return: MIstatus::success - Function succeeded.
309 // MIstatus::failure - Function failed.
312 bool CMICmdCmdGdbShow::OptionFnLanguage(
313 const CMIUtilString::VecString_t &vrWords) {
316 // Get current language
317 CMICmnLLDBDebugSessionInfo &rSessionInfo(
318 CMICmnLLDBDebugSessionInfo::Instance());
319 lldb::SBThread sbThread = rSessionInfo.GetProcess().GetSelectedThread();
320 const lldb::SBFrame sbFrame = sbThread.GetSelectedFrame();
321 lldb::SBCompileUnit sbCompileUnit = sbFrame.GetCompileUnit();
322 const lldb::LanguageType eLanguageType = sbCompileUnit.GetLanguage();
324 m_strValue = lldb::SBLanguageRuntime::GetNameForLanguageType(eLanguageType);
325 return MIstatus::success;
329 //------------------------------------------------------------------------------------
330 // Details: Carry out work to complete the GDB show option to prepare and send
332 // requested information.
335 // Return: MIstatus::success - Function succeeded.
336 // MIstatus::failure - Function failed.
339 bool CMICmdCmdGdbShow::OptionFnFallback(
340 const CMIUtilString::VecString_t &vrWords) {
343 // Do nothing - intentional. This is a fallback function to do nothing.
344 // This allows the search for gdb-show options to always succeed when the
346 // found (implemented).
348 return MIstatus::success;