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 "MICmdCmdGdbShow.h"
20 #include "MICmnMIResultRecord.h"
21 #include "MICmnMIValueConst.h"
22 #include "MICmdArgValString.h"
23 #include "MICmdArgValListOfN.h"
24 #include "MICmdArgValOptionLong.h"
25 #include "MICmnLLDBDebugSessionInfo.h"
28 const CMICmdCmdGdbShow::MapGdbOptionNameToFnGdbOptionPtr_t CMICmdCmdGdbShow::ms_mapGdbOptionNameToFnGdbOptionPtr = {
29 {"target-async", &CMICmdCmdGdbShow::OptionFnTargetAsync},
30 {"print", &CMICmdCmdGdbShow::OptionFnPrint},
31 {"language", &CMICmdCmdGdbShow::OptionFnLanguage},
32 {"fallback", &CMICmdCmdGdbShow::OptionFnFallback}};
34 //++ ------------------------------------------------------------------------------------
35 // Details: CMICmdCmdGdbShow constructor.
41 CMICmdCmdGdbShow::CMICmdCmdGdbShow(void)
42 : m_constStrArgNamedThreadGrp("thread-group")
43 , m_constStrArgNamedGdbOption("option")
44 , m_bGdbOptionRecognised(true)
45 , m_bGdbOptionFnSuccessful(false)
46 , m_bGbbOptionFnHasError(false)
47 , m_strGdbOptionFnError(MIRSRC(IDS_WORD_ERR_MSG_NOT_IMPLEMENTED_BRKTS))
49 // Command factory matches this name with that received from the stdin stream
50 m_strMiCmd = "gdb-show";
52 // Required by the CMICmdFactory when registering *this command
53 m_pSelfCreatorFn = &CMICmdCmdGdbShow::CreateSelf;
56 //++ ------------------------------------------------------------------------------------
57 // Details: CMICmdCmdGdbShow destructor.
58 // Type: Overrideable.
63 CMICmdCmdGdbShow::~CMICmdCmdGdbShow(void)
67 //++ ------------------------------------------------------------------------------------
68 // Details: The invoker requires this function. The parses the command line options
69 // arguments to extract values for each of those arguments.
72 // Return: MIstatus::success - Function succeeded.
73 // MIstatus::failure - Function failed.
77 CMICmdCmdGdbShow::ParseArgs(void)
79 bool bOk = m_setCmdArgs.Add(
80 *(new CMICmdArgValOptionLong(m_constStrArgNamedThreadGrp, false, false, CMICmdArgValListBase::eArgValType_ThreadGrp, 1)));
83 *(new CMICmdArgValListOfN(m_constStrArgNamedGdbOption, true, true, CMICmdArgValListBase::eArgValType_StringAnything)));
84 return (bOk && ParseValidateCmdOptions());
87 //++ ------------------------------------------------------------------------------------
88 // Details: The invoker requires this function. The command is executed in this function.
91 // Return: MIstatus::success - Function succeeded.
92 // MIstatus::failure - Function failed.
96 CMICmdCmdGdbShow::Execute(void)
98 CMICMDBASE_GETOPTION(pArgGdbOption, ListOfN, m_constStrArgNamedGdbOption);
99 const CMICmdArgValListBase::VecArgObjPtr_t &rVecWords(pArgGdbOption->GetExpectedOptions());
101 // Get the gdb-show option to carry out. This option will be used as an action
102 // which should be done. Further arguments will be used as parameters for it.
103 CMICmdArgValListBase::VecArgObjPtr_t::const_iterator it = rVecWords.begin();
104 const CMICmdArgValString *pOption = static_cast<const CMICmdArgValString *>(*it);
105 const CMIUtilString strOption(pOption->GetValue());
108 // Retrieve the parameter(s) for the option
109 CMIUtilString::VecString_t vecWords;
110 while (it != rVecWords.end())
112 const CMICmdArgValString *pWord = static_cast<const CMICmdArgValString *>(*it);
113 vecWords.push_back(pWord->GetValue());
119 FnGdbOptionPtr pPrintRequestFn = nullptr;
120 if (!GetOptionFn(strOption, pPrintRequestFn))
122 // For unimplemented option handlers, fallback to a generic handler
123 // ToDo: Remove this when ALL options have been implemented
124 if (!GetOptionFn("fallback", pPrintRequestFn))
126 m_bGdbOptionRecognised = false;
127 m_strGdbOptionName = "fallback"; // This would be the strOption name
128 return MIstatus::success;
132 m_bGdbOptionFnSuccessful = (this->*(pPrintRequestFn))(vecWords);
133 if (!m_bGdbOptionFnSuccessful && !m_bGbbOptionFnHasError)
134 return MIstatus::failure;
136 return MIstatus::success;
139 //++ ------------------------------------------------------------------------------------
140 // Details: The invoker requires this function. The command prepares a MI Record Result
141 // for the work carried out in the Execute() method.
144 // Return: MIstatus::success - Function succeeded.
145 // MIstatus::failure - Function failed.
149 CMICmdCmdGdbShow::Acknowledge(void)
151 // Print error if option isn't recognized:
152 // ^error,msg="The request '%s' was not recognized, not implemented"
153 if (!m_bGdbOptionRecognised)
155 const CMICmnMIValueConst miValueConst(
156 CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INFO_PRINTFN_NOT_FOUND), m_strGdbOptionName.c_str()));
157 const CMICmnMIValueResult miValueResult("msg", miValueConst);
158 const CMICmnMIResultRecord miRecordResult(m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error, miValueResult);
159 m_miResultRecord = miRecordResult;
160 return MIstatus::success;
164 if (m_bGdbOptionFnSuccessful && !m_strValue.empty())
166 const CMICmnMIValueConst miValueConst(m_strValue);
167 const CMICmnMIValueResult miValueResult("value", miValueConst);
168 const CMICmnMIResultRecord miRecordResult(m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done, miValueResult);
169 m_miResultRecord = miRecordResult;
170 return MIstatus::success;
172 else if (m_bGdbOptionFnSuccessful)
174 // Ignore empty value (for fallback)
175 const CMICmnMIResultRecord miRecordResult(m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
176 m_miResultRecord = miRecordResult;
177 return MIstatus::success;
180 // Print error if request failed:
181 // ^error,msg="The request '%s' failed.
182 const CMICmnMIValueConst miValueConst(CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INFO_PRINTFN_FAILED), m_strGdbOptionFnError.c_str()));
183 const CMICmnMIValueResult miValueResult("msg", miValueConst);
184 const CMICmnMIResultRecord miRecordResult(m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error, miValueResult);
185 m_miResultRecord = miRecordResult;
187 return MIstatus::success;
190 //++ ------------------------------------------------------------------------------------
191 // Details: Required by the CMICmdFactory when registering *this command. The factory
192 // calls this function to create an instance of *this command.
193 // Type: Static method.
195 // Return: CMICmdBase * - Pointer to a new command.
199 CMICmdCmdGdbShow::CreateSelf(void)
201 return new CMICmdCmdGdbShow();
204 //++ ------------------------------------------------------------------------------------
205 // Details: Retrieve the print function's pointer for the matching print request.
207 // Args: vrPrintFnName - (R) The info requested.
208 // vrwpFn - (W) The print function's pointer of the function to carry out
209 // Return: bool - True = Print request is implemented, false = not found.
213 CMICmdCmdGdbShow::GetOptionFn(const CMIUtilString &vrPrintFnName, FnGdbOptionPtr &vrwpFn) const
217 const MapGdbOptionNameToFnGdbOptionPtr_t::const_iterator it = ms_mapGdbOptionNameToFnGdbOptionPtr.find(vrPrintFnName);
218 if (it != ms_mapGdbOptionNameToFnGdbOptionPtr.end())
220 vrwpFn = (*it).second;
227 //++ ------------------------------------------------------------------------------------
228 // Details: Carry out work to complete the GDB show option 'target-async' to prepare
229 // and send back the requested information.
231 // Args: vrWords - (R) List of additional parameters used by this option.
232 // Return: MIstatus::success - Function succeeded.
233 // MIstatus::failure - Function failed.
237 CMICmdCmdGdbShow::OptionFnTargetAsync(const CMIUtilString::VecString_t &vrWords)
242 CMICmnLLDBDebugSessionInfo &rSessionInfo(CMICmnLLDBDebugSessionInfo::Instance());
243 const bool bAsyncMode = rSessionInfo.GetDebugger().GetAsync();
245 m_strValue = bAsyncMode ? "on" : "off";
246 return MIstatus::success;
249 //++ ------------------------------------------------------------------------------------
250 // Details: Carry out work to complete the GDB show option 'print' to prepare and send
251 // back the requested information.
253 // Args: vrWords - (R) List of additional parameters used by this option.
254 // Return: MIstatus::success - Function succeeded.
255 // MIstatus::failure - Function failed.
259 CMICmdCmdGdbShow::OptionFnPrint(const CMIUtilString::VecString_t &vrWords)
261 const bool bAllArgs(vrWords.size() == 1);
264 m_bGbbOptionFnHasError = true;
265 m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS);
266 return MIstatus::failure;
269 const CMIUtilString strOption(vrWords[0]);
270 CMIUtilString strOptionKey;
271 bool bOptionValueDefault = false;
272 if (CMIUtilString::Compare(strOption, "char-array-as-string"))
273 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintCharArrayAsString;
274 else if (CMIUtilString::Compare(strOption, "expand-aggregates"))
275 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintExpandAggregates;
276 else if (CMIUtilString::Compare(strOption, "aggregate-field-names"))
278 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintAggregateFieldNames;
279 bOptionValueDefault = true;
283 m_bGbbOptionFnHasError = true;
284 m_strGdbOptionFnError = CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION), strOption.c_str());
285 return MIstatus::failure;
288 bool bOptionValue = false;
289 bOptionValue = bOptionValueDefault ? !m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(strOptionKey, bOptionValue) || bOptionValue
290 : m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(strOptionKey, bOptionValue) && bOptionValue;
292 m_strValue = bOptionValue ? "on" : "off";
293 return MIstatus::success;
296 //++ ------------------------------------------------------------------------------------
297 // Details: Carry out work to complete the GDB show option 'language' to prepare
298 // and send back the requested information.
300 // Args: vrWords - (R) List of additional parameters used by this option.
301 // Return: MIstatus::success - Function succeeded.
302 // MIstatus::failure - Function failed.
306 CMICmdCmdGdbShow::OptionFnLanguage(const CMIUtilString::VecString_t &vrWords)
310 // Get current language
311 CMICmnLLDBDebugSessionInfo &rSessionInfo(CMICmnLLDBDebugSessionInfo::Instance());
312 lldb::SBThread sbThread = rSessionInfo.GetProcess().GetSelectedThread();
313 const lldb::SBFrame sbFrame = sbThread.GetSelectedFrame();
314 lldb::SBCompileUnit sbCompileUnit = sbFrame.GetCompileUnit();
315 const lldb::LanguageType eLanguageType = sbCompileUnit.GetLanguage();
317 m_strValue = lldb::SBLanguageRuntime::GetNameForLanguageType(eLanguageType);
318 return MIstatus::success;
321 //++ ------------------------------------------------------------------------------------
322 // Details: Carry out work to complete the GDB show option to prepare and send back the
323 // requested information.
326 // Return: MIstatus::success - Function succeeded.
327 // MIstatus::failure - Function failed.
331 CMICmdCmdGdbShow::OptionFnFallback(const CMIUtilString::VecString_t &vrWords)
335 // Do nothing - intentional. This is a fallback function to do nothing.
336 // This allows the search for gdb-show options to always succeed when the option is not
337 // found (implemented).
339 return MIstatus::success;