]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/lldb-mi/MICmdCmdGdbShow.cpp
Vendor import of lldb trunk r290819:
[FreeBSD/FreeBSD.git] / tools / lldb-mi / MICmdCmdGdbShow.cpp
1 //===-- MICmdCmdGdbShow.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:    CMICmdCmdGdbShow implementation.
11
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"
17
18 // In-house headers:
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"
26
27 // Instantiations:
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}};
34
35 //++
36 //------------------------------------------------------------------------------------
37 // Details: CMICmdCmdGdbShow constructor.
38 // Type:    Method.
39 // Args:    None.
40 // Return:  None.
41 // Throws:  None.
42 //--
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";
49
50   // Required by the CMICmdFactory when registering *this command
51   m_pSelfCreatorFn = &CMICmdCmdGdbShow::CreateSelf;
52 }
53
54 //++
55 //------------------------------------------------------------------------------------
56 // Details: CMICmdCmdGdbShow destructor.
57 // Type:    Overrideable.
58 // Args:    None.
59 // Return:  None.
60 // Throws:  None.
61 //--
62 CMICmdCmdGdbShow::~CMICmdCmdGdbShow() {}
63
64 //++
65 //------------------------------------------------------------------------------------
66 // Details: The invoker requires this function. The parses the command line
67 // options
68 //          arguments to extract values for each of those arguments.
69 // Type:    Overridden.
70 // Args:    None.
71 // Return:  MIstatus::success - Function succeeded.
72 //          MIstatus::failure - Function failed.
73 // Throws:  None.
74 //--
75 bool CMICmdCmdGdbShow::ParseArgs() {
76   m_setCmdArgs.Add(new CMICmdArgValListOfN(
77       m_constStrArgNamedGdbOption, true, true,
78       CMICmdArgValListBase::eArgValType_StringAnything));
79   return ParseValidateCmdOptions();
80 }
81
82 //++
83 //------------------------------------------------------------------------------------
84 // Details: The invoker requires this function. The command is executed in this
85 // function.
86 // Type:    Overridden.
87 // Args:    None.
88 // Return:  MIstatus::success - Function succeeded.
89 //          MIstatus::failure - Function failed.
90 // Throws:  None.
91 //--
92 bool CMICmdCmdGdbShow::Execute() {
93   CMICMDBASE_GETOPTION(pArgGdbOption, ListOfN, m_constStrArgNamedGdbOption);
94   const CMICmdArgValListBase::VecArgObjPtr_t &rVecWords(
95       pArgGdbOption->GetExpectedOptions());
96
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());
103   ++it;
104
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());
111
112     // Next
113     ++it;
114   }
115
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;
124     }
125   }
126
127   m_bGdbOptionFnSuccessful = (this->*(pPrintRequestFn))(vecWords);
128   if (!m_bGdbOptionFnSuccessful && !m_bGbbOptionFnHasError)
129     return MIstatus::failure;
130
131   return MIstatus::success;
132 }
133
134 //++
135 //------------------------------------------------------------------------------------
136 // Details: The invoker requires this function. The command prepares a MI Record
137 // Result
138 //          for the work carried out in the Execute() method.
139 // Type:    Overridden.
140 // Args:    None.
141 // Return:  MIstatus::success - Function succeeded.
142 //          MIstatus::failure - Function failed.
143 // Throws:  None.
144 //--
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,
155         miValueResult);
156     m_miResultRecord = miRecordResult;
157     return MIstatus::success;
158   }
159
160   // ^done,value="%s"
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,
166         miValueResult);
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;
175   }
176
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,
184       miValueResult);
185   m_miResultRecord = miRecordResult;
186
187   return MIstatus::success;
188 }
189
190 //++
191 //------------------------------------------------------------------------------------
192 // Details: Required by the CMICmdFactory when registering *this command. The
193 // factory
194 //          calls this function to create an instance of *this command.
195 // Type:    Static method.
196 // Args:    None.
197 // Return:  CMICmdBase * - Pointer to a new command.
198 // Throws:  None.
199 //--
200 CMICmdBase *CMICmdCmdGdbShow::CreateSelf() { return new CMICmdCmdGdbShow(); }
201
202 //++
203 //------------------------------------------------------------------------------------
204 // Details: Retrieve the print function's pointer for the matching print
205 // request.
206 // Type:    Method.
207 // Args:    vrPrintFnName   - (R) The info requested.
208 //          vrwpFn          - (W) The print function's pointer of the function
209 //          to carry out
210 // Return:  bool    - True = Print request is implemented, false = not found.
211 // Throws:  None.
212 //--
213 bool CMICmdCmdGdbShow::GetOptionFn(const CMIUtilString &vrPrintFnName,
214                                    FnGdbOptionPtr &vrwpFn) const {
215   vrwpFn = nullptr;
216
217   const MapGdbOptionNameToFnGdbOptionPtr_t::const_iterator it =
218       ms_mapGdbOptionNameToFnGdbOptionPtr.find(vrPrintFnName);
219   if (it != ms_mapGdbOptionNameToFnGdbOptionPtr.end()) {
220     vrwpFn = (*it).second;
221     return true;
222   }
223
224   return false;
225 }
226
227 //++
228 //------------------------------------------------------------------------------------
229 // Details: Carry out work to complete the GDB show option 'target-async' to
230 // prepare
231 //          and send back the requested information.
232 // Type:    Method.
233 // Args:    vrWords - (R) List of additional parameters used by this option.
234 // Return:  MIstatus::success - Function succeeded.
235 //          MIstatus::failure - Function failed.
236 // Throws:  None.
237 //--
238 bool CMICmdCmdGdbShow::OptionFnTargetAsync(
239     const CMIUtilString::VecString_t &vrWords) {
240   MIunused(vrWords);
241
242   // Get async mode
243   CMICmnLLDBDebugSessionInfo &rSessionInfo(
244       CMICmnLLDBDebugSessionInfo::Instance());
245   const bool bAsyncMode = rSessionInfo.GetDebugger().GetAsync();
246
247   m_strValue = bAsyncMode ? "on" : "off";
248   return MIstatus::success;
249 }
250
251 //++
252 //------------------------------------------------------------------------------------
253 // Details: Carry out work to complete the GDB show option 'print' to prepare
254 // and send
255 //          back the requested information.
256 // Type:    Method.
257 // Args:    vrWords - (R) List of additional parameters used by this option.
258 // Return:  MIstatus::success - Function succeeded.
259 //          MIstatus::failure - Function failed.
260 // Throws:  None.
261 //--
262 bool CMICmdCmdGdbShow::OptionFnPrint(
263     const CMIUtilString::VecString_t &vrWords) {
264   const bool bAllArgs(vrWords.size() == 1);
265   if (!bAllArgs) {
266     m_bGbbOptionFnHasError = true;
267     m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS);
268     return MIstatus::failure;
269   }
270
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;
281   } else {
282     m_bGbbOptionFnHasError = true;
283     m_strGdbOptionFnError = CMIUtilString::Format(
284         MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_UNKNOWN_OPTION),
285         strOption.c_str());
286     return MIstatus::failure;
287   }
288
289   bool bOptionValue = false;
290   bOptionValue = bOptionValueDefault
291                      ? !m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(
292                            strOptionKey, bOptionValue) ||
293                            bOptionValue
294                      : m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(
295                            strOptionKey, bOptionValue) &&
296                            bOptionValue;
297
298   m_strValue = bOptionValue ? "on" : "off";
299   return MIstatus::success;
300 }
301
302 //++
303 //------------------------------------------------------------------------------------
304 // Details: Carry out work to complete the GDB show option 'language' to prepare
305 //          and send back the requested information.
306 // Type:    Method.
307 // Args:    vrWords - (R) List of additional parameters used by this option.
308 // Return:  MIstatus::success - Function succeeded.
309 //          MIstatus::failure - Function failed.
310 // Throws:  None.
311 //--
312 bool CMICmdCmdGdbShow::OptionFnLanguage(
313     const CMIUtilString::VecString_t &vrWords) {
314   MIunused(vrWords);
315
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();
323
324   m_strValue = lldb::SBLanguageRuntime::GetNameForLanguageType(eLanguageType);
325   return MIstatus::success;
326 }
327
328 //++
329 //------------------------------------------------------------------------------------
330 // Details: Carry out work to complete the GDB show option to prepare and send
331 // back the
332 //          requested information.
333 // Type:    Method.
334 // Args:    None.
335 // Return:  MIstatus::success - Function succeeded.
336 //          MIstatus::failure - Function failed.
337 // Throws:  None.
338 //--
339 bool CMICmdCmdGdbShow::OptionFnFallback(
340     const CMIUtilString::VecString_t &vrWords) {
341   MIunused(vrWords);
342
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
345   // option is not
346   // found (implemented).
347
348   return MIstatus::success;
349 }