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/SBStringList.h"
17 #include "lldb/API/SBThread.h"
20 #include "MICmdArgValListOfN.h"
21 #include "MICmdArgValOptionLong.h"
22 #include "MICmdArgValString.h"
23 #include "MICmdCmdGdbShow.h"
24 #include "MICmnLLDBDebugSessionInfo.h"
25 #include "MICmnMIResultRecord.h"
26 #include "MICmnMIValueConst.h"
29 const CMICmdCmdGdbShow::MapGdbOptionNameToFnGdbOptionPtr_t
30 CMICmdCmdGdbShow::ms_mapGdbOptionNameToFnGdbOptionPtr = {
31 {"target-async", &CMICmdCmdGdbShow::OptionFnTargetAsync},
32 {"print", &CMICmdCmdGdbShow::OptionFnPrint},
33 {"language", &CMICmdCmdGdbShow::OptionFnLanguage},
34 {"disassembly-flavor", &CMICmdCmdGdbShow::OptionFnDisassemblyFlavor},
35 {"fallback", &CMICmdCmdGdbShow::OptionFnFallback},
36 {"breakpoint", &CMICmdCmdGdbShow::OptionFnBreakpoint}};
39 //------------------------------------------------------------------------------------
40 // Details: CMICmdCmdGdbShow constructor.
46 CMICmdCmdGdbShow::CMICmdCmdGdbShow()
47 : m_constStrArgNamedGdbOption("option"), m_bGdbOptionRecognised(true),
48 m_bGdbOptionFnSuccessful(false), m_bGbbOptionFnHasError(false),
49 m_strGdbOptionFnError(MIRSRC(IDS_WORD_ERR_MSG_NOT_IMPLEMENTED_BRKTS)) {
50 // Command factory matches this name with that received from the stdin stream
51 m_strMiCmd = "gdb-show";
53 // Required by the CMICmdFactory when registering *this command
54 m_pSelfCreatorFn = &CMICmdCmdGdbShow::CreateSelf;
58 //------------------------------------------------------------------------------------
59 // Details: CMICmdCmdGdbShow destructor.
60 // Type: Overrideable.
65 CMICmdCmdGdbShow::~CMICmdCmdGdbShow() {}
68 //------------------------------------------------------------------------------------
69 // Details: The invoker requires this function. The parses the command line
71 // arguments to extract values for each of those arguments.
74 // Return: MIstatus::success - Function succeeded.
75 // MIstatus::failure - Function failed.
78 bool CMICmdCmdGdbShow::ParseArgs() {
79 m_setCmdArgs.Add(new CMICmdArgValListOfN(
80 m_constStrArgNamedGdbOption, true, true,
81 CMICmdArgValListBase::eArgValType_StringAnything));
82 return ParseValidateCmdOptions();
86 //------------------------------------------------------------------------------------
87 // Details: The invoker requires this function. The command is executed in this
91 // Return: MIstatus::success - Function succeeded.
92 // MIstatus::failure - Function failed.
95 bool CMICmdCmdGdbShow::Execute() {
96 CMICMDBASE_GETOPTION(pArgGdbOption, ListOfN, m_constStrArgNamedGdbOption);
97 const CMICmdArgValListBase::VecArgObjPtr_t &rVecWords(
98 pArgGdbOption->GetExpectedOptions());
100 // Get the gdb-show option to carry out. This option will be used as an action
101 // which should be done. Further arguments will be used as parameters for it.
102 CMICmdArgValListBase::VecArgObjPtr_t::const_iterator it = rVecWords.begin();
103 const CMICmdArgValString *pOption =
104 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()) {
111 const CMICmdArgValString *pWord =
112 static_cast<const CMICmdArgValString *>(*it);
113 vecWords.push_back(pWord->GetValue());
119 FnGdbOptionPtr pPrintRequestFn = nullptr;
120 if (!GetOptionFn(strOption, pPrintRequestFn)) {
121 // For unimplemented option handlers, fallback to a generic handler
122 // ToDo: Remove this when ALL options have been implemented
123 if (!GetOptionFn("fallback", pPrintRequestFn)) {
124 m_bGdbOptionRecognised = false;
125 m_strGdbOptionName = "fallback"; // This would be the strOption name
126 return MIstatus::success;
130 m_bGdbOptionFnSuccessful = (this->*(pPrintRequestFn))(vecWords);
131 if (!m_bGdbOptionFnSuccessful && !m_bGbbOptionFnHasError)
132 return MIstatus::failure;
134 return MIstatus::success;
138 //------------------------------------------------------------------------------------
139 // Details: The invoker requires this function. The command prepares a MI Record
141 // for the work carried out in the Execute() method.
144 // Return: MIstatus::success - Function succeeded.
145 // MIstatus::failure - Function failed.
148 bool CMICmdCmdGdbShow::Acknowledge() {
149 // Print error if option isn't recognized:
150 // ^error,msg="The request '%s' was not recognized, not implemented"
151 if (!m_bGdbOptionRecognised) {
152 const CMICmnMIValueConst miValueConst(
153 CMIUtilString::Format(MIRSRC(IDS_CMD_ERR_INFO_PRINTFN_NOT_FOUND),
154 m_strGdbOptionName.c_str()));
155 const CMICmnMIValueResult miValueResult("msg", miValueConst);
156 const CMICmnMIResultRecord miRecordResult(
157 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error,
159 m_miResultRecord = miRecordResult;
160 return MIstatus::success;
164 if (m_bGdbOptionFnSuccessful && !m_strValue.empty()) {
165 const CMICmnMIValueConst miValueConst(m_strValue);
166 const CMICmnMIValueResult miValueResult("value", miValueConst);
167 const CMICmnMIResultRecord miRecordResult(
168 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done,
170 m_miResultRecord = miRecordResult;
171 return MIstatus::success;
172 } else if (m_bGdbOptionFnSuccessful) {
173 // Ignore empty value (for fallback)
174 const CMICmnMIResultRecord miRecordResult(
175 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(
183 MIRSRC(IDS_CMD_ERR_INFO_PRINTFN_FAILED), m_strGdbOptionFnError.c_str()));
184 const CMICmnMIValueResult miValueResult("msg", miValueConst);
185 const CMICmnMIResultRecord miRecordResult(
186 m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Error,
188 m_miResultRecord = miRecordResult;
190 return MIstatus::success;
194 //------------------------------------------------------------------------------------
195 // Details: Required by the CMICmdFactory when registering *this command. The
197 // calls this function to create an instance of *this command.
198 // Type: Static method.
200 // Return: CMICmdBase * - Pointer to a new command.
203 CMICmdBase *CMICmdCmdGdbShow::CreateSelf() { return new CMICmdCmdGdbShow(); }
206 //------------------------------------------------------------------------------------
207 // Details: Retrieve the print function's pointer for the matching print
210 // Args: vrPrintFnName - (R) The info requested.
211 // vrwpFn - (W) The print function's pointer of the function
213 // Return: bool - True = Print request is implemented, false = not found.
216 bool CMICmdCmdGdbShow::GetOptionFn(const CMIUtilString &vrPrintFnName,
217 FnGdbOptionPtr &vrwpFn) const {
220 const MapGdbOptionNameToFnGdbOptionPtr_t::const_iterator it =
221 ms_mapGdbOptionNameToFnGdbOptionPtr.find(vrPrintFnName);
222 if (it != ms_mapGdbOptionNameToFnGdbOptionPtr.end()) {
223 vrwpFn = (*it).second;
231 //------------------------------------------------------------------------------------
232 // Details: Carry out work to complete the GDB show option 'target-async' to
234 // and send back the requested information.
236 // Args: vrWords - (R) List of additional parameters used by this option.
237 // Return: MIstatus::success - Function succeeded.
238 // MIstatus::failure - Function failed.
241 bool CMICmdCmdGdbShow::OptionFnTargetAsync(
242 const CMIUtilString::VecString_t &vrWords) {
246 CMICmnLLDBDebugSessionInfo &rSessionInfo(
247 CMICmnLLDBDebugSessionInfo::Instance());
248 const bool bAsyncMode = rSessionInfo.GetDebugger().GetAsync();
250 m_strValue = bAsyncMode ? "on" : "off";
251 return MIstatus::success;
255 //------------------------------------------------------------------------------------
256 // Details: Carry out work to complete the GDB show option 'print' to prepare
258 // back the requested information.
260 // Args: vrWords - (R) List of additional parameters used by this option.
261 // Return: MIstatus::success - Function succeeded.
262 // MIstatus::failure - Function failed.
265 bool CMICmdCmdGdbShow::OptionFnPrint(
266 const CMIUtilString::VecString_t &vrWords) {
267 const bool bAllArgs(vrWords.size() == 1);
269 m_bGbbOptionFnHasError = true;
270 m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS);
271 return MIstatus::failure;
274 const CMIUtilString strOption(vrWords[0]);
275 CMIUtilString strOptionKey;
276 bool bOptionValueDefault = false;
277 if (CMIUtilString::Compare(strOption, "char-array-as-string"))
278 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintCharArrayAsString;
279 else if (CMIUtilString::Compare(strOption, "expand-aggregates"))
280 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintExpandAggregates;
281 else if (CMIUtilString::Compare(strOption, "aggregate-field-names")) {
282 strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintAggregateFieldNames;
283 bOptionValueDefault = true;
285 m_bGbbOptionFnHasError = true;
286 m_strGdbOptionFnError = CMIUtilString::Format(
287 MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION),
289 return MIstatus::failure;
292 bool bOptionValue = false;
293 bOptionValue = bOptionValueDefault
294 ? !m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(
295 strOptionKey, bOptionValue) ||
297 : m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(
298 strOptionKey, bOptionValue) &&
301 m_strValue = bOptionValue ? "on" : "off";
302 return MIstatus::success;
306 //------------------------------------------------------------------------------------
307 // Details: Carry out work to complete the GDB show option 'language' to prepare
308 // and send back the requested information.
310 // Args: vrWords - (R) List of additional parameters used by this option.
311 // Return: MIstatus::success - Function succeeded.
312 // MIstatus::failure - Function failed.
315 bool CMICmdCmdGdbShow::OptionFnLanguage(
316 const CMIUtilString::VecString_t &vrWords) {
319 // Get current language
320 CMICmnLLDBDebugSessionInfo &rSessionInfo(
321 CMICmnLLDBDebugSessionInfo::Instance());
322 lldb::SBThread sbThread = rSessionInfo.GetProcess().GetSelectedThread();
323 const lldb::SBFrame sbFrame = sbThread.GetSelectedFrame();
324 lldb::SBCompileUnit sbCompileUnit = sbFrame.GetCompileUnit();
325 const lldb::LanguageType eLanguageType = sbCompileUnit.GetLanguage();
327 m_strValue = lldb::SBLanguageRuntime::GetNameForLanguageType(eLanguageType);
328 return MIstatus::success;
332 //------------------------------------------------------------------------------------
333 // Details: Carry out work to complete the GDB show option 'disassembly-flavor' to prepare
334 // and send back the requested information.
336 // Args: vrWords - (R) List of additional parameters used by this option.
337 // Return: MIstatus::success - Function succeeded.
338 // MIstatus::failure - Function failed.
341 bool CMICmdCmdGdbShow::OptionFnDisassemblyFlavor(const CMIUtilString::VecString_t &vrWords) {
344 // Get current disassembly flavor
345 lldb::SBDebugger &rDbgr = m_rLLDBDebugSessionInfo.GetDebugger();
346 m_strValue = lldb::SBDebugger::GetInternalVariableValue("target.x86-disassembly-flavor",
347 rDbgr.GetInstanceName()).GetStringAtIndex(0);
348 return MIstatus::success;
352 //------------------------------------------------------------------------------------
353 // Details: Carry out work to complete the GDB show option 'breakpoint' to
355 // and send back the requested information.
357 // Args: vrWords - (R) List of additional parameters used by this option.
358 // Return: MIstatus::success - Function succeeded.
359 // MIstatus::failure - Function failed.
362 bool CMICmdCmdGdbShow::OptionFnBreakpoint(
363 const CMIUtilString::VecString_t &vrWords) {
364 if (vrWords.size() != 1) {
365 m_bGbbOptionFnHasError = true;
366 m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_BREAKPOINT_BAD_ARGS);
367 return MIstatus::failure;
370 const CMIUtilString strOption(vrWords[0]);
371 if (!CMIUtilString::Compare(strOption, "pending")) {
372 m_bGbbOptionFnHasError = true;
373 m_strGdbOptionFnError = CMIUtilString::Format(
374 MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_BREAKPOINT_UNKNOWN_OPTION),
376 return MIstatus::failure;
379 if (!m_rLLDBDebugSessionInfo.SharedDataRetrieve("breakpoint.pending",
381 if (m_strValue.empty())
385 return MIstatus::success;
389 //------------------------------------------------------------------------------------
390 // Details: Carry out work to complete the GDB show option to prepare and send
392 // requested information.
395 // Return: MIstatus::success - Function succeeded.
396 // MIstatus::failure - Function failed.
399 bool CMICmdCmdGdbShow::OptionFnFallback(
400 const CMIUtilString::VecString_t &vrWords) {
403 // Do nothing - intentional. This is a fallback function to do nothing.
404 // This allows the search for gdb-show options to always succeed when the
406 // found (implemented).
408 return MIstatus::success;