]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/tools/lldb-mi/MICmdCmdGdbShow.cpp
Update llvm, clang and lldb to 3.7.0 release.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / 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 "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"
26
27 // Instantiations:
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}};
33
34 //++ ------------------------------------------------------------------------------------
35 // Details: CMICmdCmdGdbShow constructor.
36 // Type:    Method.
37 // Args:    None.
38 // Return:  None.
39 // Throws:  None.
40 //--
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))
48 {
49     // Command factory matches this name with that received from the stdin stream
50     m_strMiCmd = "gdb-show";
51
52     // Required by the CMICmdFactory when registering *this command
53     m_pSelfCreatorFn = &CMICmdCmdGdbShow::CreateSelf;
54 }
55
56 //++ ------------------------------------------------------------------------------------
57 // Details: CMICmdCmdGdbShow destructor.
58 // Type:    Overrideable.
59 // Args:    None.
60 // Return:  None.
61 // Throws:  None.
62 //--
63 CMICmdCmdGdbShow::~CMICmdCmdGdbShow(void)
64 {
65 }
66
67 //++ ------------------------------------------------------------------------------------
68 // Details: The invoker requires this function. The parses the command line options
69 //          arguments to extract values for each of those arguments.
70 // Type:    Overridden.
71 // Args:    None.
72 // Return:  MIstatus::success - Function succeeded.
73 //          MIstatus::failure - Function failed.
74 // Throws:  None.
75 //--
76 bool
77 CMICmdCmdGdbShow::ParseArgs(void)
78 {
79     bool bOk = m_setCmdArgs.Add(
80         *(new CMICmdArgValOptionLong(m_constStrArgNamedThreadGrp, false, false, CMICmdArgValListBase::eArgValType_ThreadGrp, 1)));
81     bOk = bOk &&
82           m_setCmdArgs.Add(
83               *(new CMICmdArgValListOfN(m_constStrArgNamedGdbOption, true, true, CMICmdArgValListBase::eArgValType_StringAnything)));
84     return (bOk && ParseValidateCmdOptions());
85 }
86
87 //++ ------------------------------------------------------------------------------------
88 // Details: The invoker requires this function. The command is executed in this function.
89 // Type:    Overridden.
90 // Args:    None.
91 // Return:  MIstatus::success - Function succeeded.
92 //          MIstatus::failure - Function failed.
93 // Throws:  None.
94 //--
95 bool
96 CMICmdCmdGdbShow::Execute(void)
97 {
98     CMICMDBASE_GETOPTION(pArgGdbOption, ListOfN, m_constStrArgNamedGdbOption);
99     const CMICmdArgValListBase::VecArgObjPtr_t &rVecWords(pArgGdbOption->GetExpectedOptions());
100
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());
106     ++it;
107
108     // Retrieve the parameter(s) for the option
109     CMIUtilString::VecString_t vecWords;
110     while (it != rVecWords.end())
111     {
112         const CMICmdArgValString *pWord = static_cast<const CMICmdArgValString *>(*it);
113         vecWords.push_back(pWord->GetValue());
114
115         // Next
116         ++it;
117     }
118
119     FnGdbOptionPtr pPrintRequestFn = nullptr;
120     if (!GetOptionFn(strOption, pPrintRequestFn))
121     {
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))
125         {
126             m_bGdbOptionRecognised = false;
127             m_strGdbOptionName = "fallback"; // This would be the strOption name
128             return MIstatus::success;
129         }
130     }
131
132     m_bGdbOptionFnSuccessful = (this->*(pPrintRequestFn))(vecWords);
133     if (!m_bGdbOptionFnSuccessful && !m_bGbbOptionFnHasError)
134         return MIstatus::failure;
135
136     return MIstatus::success;
137 }
138
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.
142 // Type:    Overridden.
143 // Args:    None.
144 // Return:  MIstatus::success - Function succeeded.
145 //          MIstatus::failure - Function failed.
146 // Throws:  None.
147 //--
148 bool
149 CMICmdCmdGdbShow::Acknowledge(void)
150 {
151     // Print error if option isn't recognized:
152     // ^error,msg="The request '%s' was not recognized, not implemented"
153     if (!m_bGdbOptionRecognised)
154     {
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;
161     }
162
163     // ^done,value="%s"
164     if (m_bGdbOptionFnSuccessful && !m_strValue.empty())
165     {
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;
171     }
172     else if (m_bGdbOptionFnSuccessful)
173     {
174         // Ignore empty value (for fallback)
175         const CMICmnMIResultRecord miRecordResult(m_cmdData.strMiCmdToken, CMICmnMIResultRecord::eResultClass_Done);
176         m_miResultRecord = miRecordResult;
177         return MIstatus::success;
178     }
179
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;
186
187     return MIstatus::success;
188 }
189
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.
194 // Args:    None.
195 // Return:  CMICmdBase * - Pointer to a new command.
196 // Throws:  None.
197 //--
198 CMICmdBase *
199 CMICmdCmdGdbShow::CreateSelf(void)
200 {
201     return new CMICmdCmdGdbShow();
202 }
203
204 //++ ------------------------------------------------------------------------------------
205 // Details: Retrieve the print function's pointer for the matching print request.
206 // Type:    Method.
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.
210 // Throws:  None.
211 //--
212 bool
213 CMICmdCmdGdbShow::GetOptionFn(const CMIUtilString &vrPrintFnName, FnGdbOptionPtr &vrwpFn) const
214 {
215     vrwpFn = nullptr;
216
217     const MapGdbOptionNameToFnGdbOptionPtr_t::const_iterator it = ms_mapGdbOptionNameToFnGdbOptionPtr.find(vrPrintFnName);
218     if (it != ms_mapGdbOptionNameToFnGdbOptionPtr.end())
219     {
220         vrwpFn = (*it).second;
221         return true;
222     }
223
224     return false;
225 }
226
227 //++ ------------------------------------------------------------------------------------
228 // Details: Carry out work to complete the GDB show option 'target-async' to prepare
229 //          and send back the requested information.
230 // Type:    Method.
231 // Args:    vrWords - (R) List of additional parameters used by this option.
232 // Return:  MIstatus::success - Function succeeded.
233 //          MIstatus::failure - Function failed.
234 // Throws:  None.
235 //--
236 bool
237 CMICmdCmdGdbShow::OptionFnTargetAsync(const CMIUtilString::VecString_t &vrWords)
238 {
239     MIunused(vrWords);
240
241     // Get async mode
242     CMICmnLLDBDebugSessionInfo &rSessionInfo(CMICmnLLDBDebugSessionInfo::Instance());
243     const bool bAsyncMode = rSessionInfo.GetDebugger().GetAsync();
244
245     m_strValue = bAsyncMode ? "on" : "off";
246     return MIstatus::success;
247 }
248
249 //++ ------------------------------------------------------------------------------------
250 // Details: Carry out work to complete the GDB show option 'print' to prepare and send
251 //          back the requested information.
252 // Type:    Method.
253 // Args:    vrWords - (R) List of additional parameters used by this option.
254 // Return:  MIstatus::success - Function succeeded.
255 //          MIstatus::failure - Function failed.
256 // Throws:  None.
257 //--
258 bool
259 CMICmdCmdGdbShow::OptionFnPrint(const CMIUtilString::VecString_t &vrWords)
260 {
261     const bool bAllArgs(vrWords.size() == 1);
262     if (!bAllArgs)
263     {
264         m_bGbbOptionFnHasError = true;
265         m_strGdbOptionFnError = MIRSRC(IDS_CMD_ERR_GDBSHOW_OPT_PRINT_BAD_ARGS);
266         return MIstatus::failure;
267     }
268
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"))
277     {
278         strOptionKey = m_rLLDBDebugSessionInfo.m_constStrPrintAggregateFieldNames;
279         bOptionValueDefault = true;
280     }
281     else
282     {
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;
286     }
287
288     bool bOptionValue = false;
289     bOptionValue = bOptionValueDefault ? !m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(strOptionKey, bOptionValue) || bOptionValue
290         : m_rLLDBDebugSessionInfo.SharedDataRetrieve<bool>(strOptionKey, bOptionValue) && bOptionValue;
291
292     m_strValue = bOptionValue ? "on" : "off";
293     return MIstatus::success;
294 }
295
296 //++ ------------------------------------------------------------------------------------
297 // Details: Carry out work to complete the GDB show option 'language' to prepare
298 //          and send back the requested information.
299 // Type:    Method.
300 // Args:    vrWords - (R) List of additional parameters used by this option.
301 // Return:  MIstatus::success - Function succeeded.
302 //          MIstatus::failure - Function failed.
303 // Throws:  None.
304 //--
305 bool
306 CMICmdCmdGdbShow::OptionFnLanguage(const CMIUtilString::VecString_t &vrWords)
307 {
308     MIunused(vrWords);
309
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();
316
317     m_strValue = lldb::SBLanguageRuntime::GetNameForLanguageType(eLanguageType);
318     return MIstatus::success;
319 }
320
321 //++ ------------------------------------------------------------------------------------
322 // Details: Carry out work to complete the GDB show option to prepare and send back the
323 //          requested information.
324 // Type:    Method.
325 // Args:    None.
326 // Return:  MIstatus::success - Function succeeded.
327 //          MIstatus::failure - Function failed.
328 // Throws:  None.
329 //--
330 bool
331 CMICmdCmdGdbShow::OptionFnFallback(const CMIUtilString::VecString_t &vrWords)
332 {
333     MIunused(vrWords);
334
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).
338
339     return MIstatus::success;
340 }