130fdc8d8SChris Lattner //===-- CommandObject.cpp ---------------------------------------*- C++ -*-===//
230fdc8d8SChris Lattner //
330fdc8d8SChris Lattner //                     The LLVM Compiler Infrastructure
430fdc8d8SChris Lattner //
530fdc8d8SChris Lattner // This file is distributed under the University of Illinois Open Source
630fdc8d8SChris Lattner // License. See LICENSE.TXT for details.
730fdc8d8SChris Lattner //
830fdc8d8SChris Lattner //===----------------------------------------------------------------------===//
930fdc8d8SChris Lattner 
1093a64300SDaniel Malea #include "lldb/lldb-python.h"
1193a64300SDaniel Malea 
1230fdc8d8SChris Lattner #include "lldb/Interpreter/CommandObject.h"
1330fdc8d8SChris Lattner 
1430fdc8d8SChris Lattner #include <string>
1530fdc8d8SChris Lattner #include <map>
1630fdc8d8SChris Lattner 
1730fdc8d8SChris Lattner #include <stdlib.h>
1830fdc8d8SChris Lattner #include <ctype.h>
1930fdc8d8SChris Lattner 
2030fdc8d8SChris Lattner #include "lldb/Core/Address.h"
21ca7835c6SJohnny Chen #include "lldb/Core/ArchSpec.h"
2240af72e1SJim Ingham #include "lldb/Interpreter/Options.h"
2330fdc8d8SChris Lattner 
2430fdc8d8SChris Lattner // These are for the Sourcename completers.
2530fdc8d8SChris Lattner // FIXME: Make a separate file for the completers.
2653239f00SGreg Clayton #include "lldb/Host/FileSpec.h"
2730fdc8d8SChris Lattner #include "lldb/Core/FileSpecList.h"
28a78bd7ffSZachary Turner #include "lldb/DataFormatters/FormatManager.h"
2930fdc8d8SChris Lattner #include "lldb/Target/Process.h"
3030fdc8d8SChris Lattner #include "lldb/Target/Target.h"
3130fdc8d8SChris Lattner 
3230fdc8d8SChris Lattner #include "lldb/Interpreter/CommandInterpreter.h"
3330fdc8d8SChris Lattner #include "lldb/Interpreter/CommandReturnObject.h"
3430fdc8d8SChris Lattner #include "lldb/Interpreter/ScriptInterpreter.h"
3530fdc8d8SChris Lattner #include "lldb/Interpreter/ScriptInterpreterPython.h"
3630fdc8d8SChris Lattner 
3730fdc8d8SChris Lattner using namespace lldb;
3830fdc8d8SChris Lattner using namespace lldb_private;
3930fdc8d8SChris Lattner 
4030fdc8d8SChris Lattner //-------------------------------------------------------------------------
4130fdc8d8SChris Lattner // CommandObject
4230fdc8d8SChris Lattner //-------------------------------------------------------------------------
4330fdc8d8SChris Lattner 
44a7015092SGreg Clayton CommandObject::CommandObject
45a7015092SGreg Clayton (
46a7015092SGreg Clayton     CommandInterpreter &interpreter,
47a7015092SGreg Clayton     const char *name,
48a7015092SGreg Clayton     const char *help,
49a7015092SGreg Clayton     const char *syntax,
50a7015092SGreg Clayton     uint32_t flags
51a7015092SGreg Clayton ) :
52a7015092SGreg Clayton     m_interpreter (interpreter),
532f02fe0bSEnrico Granata     m_cmd_name (name ? name : ""),
5430fdc8d8SChris Lattner     m_cmd_help_short (),
5530fdc8d8SChris Lattner     m_cmd_help_long (),
5630fdc8d8SChris Lattner     m_cmd_syntax (),
57279a6c26SJim Ingham     m_is_alias (false),
58e139cf23SCaroline Tice     m_flags (flags),
59a9f7b79dSGreg Clayton     m_arguments(),
606d8873f9SJim Ingham     m_deprecated_command_override_callback (nullptr),
61d78c9576SEd Maste     m_command_override_callback (nullptr),
62d78c9576SEd Maste     m_command_override_baton (nullptr)
6330fdc8d8SChris Lattner {
6430fdc8d8SChris Lattner     if (help && help[0])
6530fdc8d8SChris Lattner         m_cmd_help_short = help;
6630fdc8d8SChris Lattner     if (syntax && syntax[0])
6730fdc8d8SChris Lattner         m_cmd_syntax = syntax;
6830fdc8d8SChris Lattner }
6930fdc8d8SChris Lattner 
7030fdc8d8SChris Lattner CommandObject::~CommandObject ()
7130fdc8d8SChris Lattner {
7230fdc8d8SChris Lattner }
7330fdc8d8SChris Lattner 
7430fdc8d8SChris Lattner const char *
7530fdc8d8SChris Lattner CommandObject::GetHelp ()
7630fdc8d8SChris Lattner {
7730fdc8d8SChris Lattner     return m_cmd_help_short.c_str();
7830fdc8d8SChris Lattner }
7930fdc8d8SChris Lattner 
8030fdc8d8SChris Lattner const char *
8130fdc8d8SChris Lattner CommandObject::GetHelpLong ()
8230fdc8d8SChris Lattner {
8330fdc8d8SChris Lattner     return m_cmd_help_long.c_str();
8430fdc8d8SChris Lattner }
8530fdc8d8SChris Lattner 
8630fdc8d8SChris Lattner const char *
8730fdc8d8SChris Lattner CommandObject::GetSyntax ()
8830fdc8d8SChris Lattner {
89e139cf23SCaroline Tice     if (m_cmd_syntax.length() == 0)
90e139cf23SCaroline Tice     {
91e139cf23SCaroline Tice         StreamString syntax_str;
92e139cf23SCaroline Tice         syntax_str.Printf ("%s", GetCommandName());
93d78c9576SEd Maste         if (GetOptions() != nullptr)
94e139cf23SCaroline Tice             syntax_str.Printf (" <cmd-options>");
95e139cf23SCaroline Tice         if (m_arguments.size() > 0)
96e139cf23SCaroline Tice         {
97e139cf23SCaroline Tice             syntax_str.Printf (" ");
98ca5acdbeSEnrico Granata             if (WantsRawCommandString() && GetOptions() && GetOptions()->NumCommandOptions())
99a4c6ad19SSean Callanan                 syntax_str.Printf("-- ");
100e139cf23SCaroline Tice             GetFormattedCommandArguments (syntax_str);
101e139cf23SCaroline Tice         }
102e139cf23SCaroline Tice         m_cmd_syntax = syntax_str.GetData ();
103e139cf23SCaroline Tice     }
104e139cf23SCaroline Tice 
10530fdc8d8SChris Lattner     return m_cmd_syntax.c_str();
10630fdc8d8SChris Lattner }
10730fdc8d8SChris Lattner 
10830fdc8d8SChris Lattner const char *
10930fdc8d8SChris Lattner CommandObject::GetCommandName ()
11030fdc8d8SChris Lattner {
11130fdc8d8SChris Lattner     return m_cmd_name.c_str();
11230fdc8d8SChris Lattner }
11330fdc8d8SChris Lattner 
11430fdc8d8SChris Lattner void
11530fdc8d8SChris Lattner CommandObject::SetCommandName (const char *name)
11630fdc8d8SChris Lattner {
11730fdc8d8SChris Lattner     m_cmd_name = name;
11830fdc8d8SChris Lattner }
11930fdc8d8SChris Lattner 
12030fdc8d8SChris Lattner void
12130fdc8d8SChris Lattner CommandObject::SetHelp (const char *cstr)
12230fdc8d8SChris Lattner {
12330fdc8d8SChris Lattner     m_cmd_help_short = cstr;
12430fdc8d8SChris Lattner }
12530fdc8d8SChris Lattner 
12630fdc8d8SChris Lattner void
1276f79bb2dSEnrico Granata CommandObject::SetHelp (std::string str)
1286f79bb2dSEnrico Granata {
1296f79bb2dSEnrico Granata     m_cmd_help_short = str;
1306f79bb2dSEnrico Granata }
1316f79bb2dSEnrico Granata 
1326f79bb2dSEnrico Granata void
13330fdc8d8SChris Lattner CommandObject::SetHelpLong (const char *cstr)
13430fdc8d8SChris Lattner {
13530fdc8d8SChris Lattner     m_cmd_help_long = cstr;
13630fdc8d8SChris Lattner }
13730fdc8d8SChris Lattner 
13830fdc8d8SChris Lattner void
13999f0b8f9SEnrico Granata CommandObject::SetHelpLong (std::string str)
14099f0b8f9SEnrico Granata {
14199f0b8f9SEnrico Granata     m_cmd_help_long = str;
14299f0b8f9SEnrico Granata }
14399f0b8f9SEnrico Granata 
14499f0b8f9SEnrico Granata void
14530fdc8d8SChris Lattner CommandObject::SetSyntax (const char *cstr)
14630fdc8d8SChris Lattner {
14730fdc8d8SChris Lattner     m_cmd_syntax = cstr;
14830fdc8d8SChris Lattner }
14930fdc8d8SChris Lattner 
15030fdc8d8SChris Lattner Options *
15130fdc8d8SChris Lattner CommandObject::GetOptions ()
15230fdc8d8SChris Lattner {
15330fdc8d8SChris Lattner     // By default commands don't have options unless this virtual function
15430fdc8d8SChris Lattner     // is overridden by base classes.
155d78c9576SEd Maste     return nullptr;
15630fdc8d8SChris Lattner }
15730fdc8d8SChris Lattner 
15830fdc8d8SChris Lattner bool
15930fdc8d8SChris Lattner CommandObject::ParseOptions
16030fdc8d8SChris Lattner (
16130fdc8d8SChris Lattner     Args& args,
16230fdc8d8SChris Lattner     CommandReturnObject &result
16330fdc8d8SChris Lattner )
16430fdc8d8SChris Lattner {
16530fdc8d8SChris Lattner     // See if the subclass has options?
16630fdc8d8SChris Lattner     Options *options = GetOptions();
167d78c9576SEd Maste     if (options != nullptr)
16830fdc8d8SChris Lattner     {
16930fdc8d8SChris Lattner         Error error;
170f6b8b581SGreg Clayton         options->NotifyOptionParsingStarting();
17130fdc8d8SChris Lattner 
172b7ad58a0SGreg Clayton         // ParseOptions calls getopt_long_only, which always skips the zero'th item in the array and starts at position 1,
17330fdc8d8SChris Lattner         // so we need to push a dummy value into position zero.
17430fdc8d8SChris Lattner         args.Unshift("dummy_string");
17530fdc8d8SChris Lattner         error = args.ParseOptions (*options);
17630fdc8d8SChris Lattner 
17730fdc8d8SChris Lattner         // The "dummy_string" will have already been removed by ParseOptions,
17830fdc8d8SChris Lattner         // so no need to remove it.
17930fdc8d8SChris Lattner 
180f6b8b581SGreg Clayton         if (error.Success())
181f6b8b581SGreg Clayton             error = options->NotifyOptionParsingFinished();
182f6b8b581SGreg Clayton 
183f6b8b581SGreg Clayton         if (error.Success())
184f6b8b581SGreg Clayton         {
185f6b8b581SGreg Clayton             if (options->VerifyOptions (result))
186f6b8b581SGreg Clayton                 return true;
187f6b8b581SGreg Clayton         }
188f6b8b581SGreg Clayton         else
18930fdc8d8SChris Lattner         {
19030fdc8d8SChris Lattner             const char *error_cstr = error.AsCString();
19130fdc8d8SChris Lattner             if (error_cstr)
19230fdc8d8SChris Lattner             {
19330fdc8d8SChris Lattner                 // We got an error string, lets use that
19486edbf41SGreg Clayton                 result.AppendError(error_cstr);
19530fdc8d8SChris Lattner             }
19630fdc8d8SChris Lattner             else
19730fdc8d8SChris Lattner             {
19830fdc8d8SChris Lattner                 // No error string, output the usage information into result
199eb0103f2SGreg Clayton                 options->GenerateOptionUsage (result.GetErrorStream(), this);
20030fdc8d8SChris Lattner             }
201f6b8b581SGreg Clayton         }
20230fdc8d8SChris Lattner         result.SetStatus (eReturnStatusFailed);
20330fdc8d8SChris Lattner         return false;
20430fdc8d8SChris Lattner     }
20530fdc8d8SChris Lattner     return true;
20630fdc8d8SChris Lattner }
20730fdc8d8SChris Lattner 
2085a988416SJim Ingham 
2095a988416SJim Ingham 
2105a988416SJim Ingham bool
211f9fc609fSGreg Clayton CommandObject::CheckRequirements (CommandReturnObject &result)
2125a988416SJim Ingham {
213f9fc609fSGreg Clayton #ifdef LLDB_CONFIGURATION_DEBUG
214f9fc609fSGreg Clayton     // Nothing should be stored in m_exe_ctx between running commands as m_exe_ctx
215f9fc609fSGreg Clayton     // has shared pointers to the target, process, thread and frame and we don't
216f9fc609fSGreg Clayton     // want any CommandObject instances to keep any of these objects around
217f9fc609fSGreg Clayton     // longer than for a single command. Every command should call
218f9fc609fSGreg Clayton     // CommandObject::Cleanup() after it has completed
219f9fc609fSGreg Clayton     assert (m_exe_ctx.GetTargetPtr() == NULL);
220f9fc609fSGreg Clayton     assert (m_exe_ctx.GetProcessPtr() == NULL);
221f9fc609fSGreg Clayton     assert (m_exe_ctx.GetThreadPtr() == NULL);
222f9fc609fSGreg Clayton     assert (m_exe_ctx.GetFramePtr() == NULL);
223f9fc609fSGreg Clayton #endif
224f9fc609fSGreg Clayton 
225f9fc609fSGreg Clayton     // Lock down the interpreter's execution context prior to running the
226f9fc609fSGreg Clayton     // command so we guarantee the selected target, process, thread and frame
227f9fc609fSGreg Clayton     // can't go away during the execution
228f9fc609fSGreg Clayton     m_exe_ctx = m_interpreter.GetExecutionContext();
229f9fc609fSGreg Clayton 
230f9fc609fSGreg Clayton     const uint32_t flags = GetFlags().Get();
231*e87764f2SEnrico Granata     if (flags & (eCommandRequiresTarget   |
232*e87764f2SEnrico Granata                  eCommandRequiresProcess  |
233*e87764f2SEnrico Granata                  eCommandRequiresThread   |
234*e87764f2SEnrico Granata                  eCommandRequiresFrame    |
235*e87764f2SEnrico Granata                  eCommandTryTargetAPILock ))
236f9fc609fSGreg Clayton     {
237f9fc609fSGreg Clayton 
238*e87764f2SEnrico Granata         if ((flags & eCommandRequiresTarget) && !m_exe_ctx.HasTargetScope())
239f9fc609fSGreg Clayton         {
240f9fc609fSGreg Clayton             result.AppendError (GetInvalidTargetDescription());
241f9fc609fSGreg Clayton             return false;
242f9fc609fSGreg Clayton         }
243f9fc609fSGreg Clayton 
244*e87764f2SEnrico Granata         if ((flags & eCommandRequiresProcess) && !m_exe_ctx.HasProcessScope())
245f9fc609fSGreg Clayton         {
246e59b0d2cSJason Molenda             if (!m_exe_ctx.HasTargetScope())
247e59b0d2cSJason Molenda                 result.AppendError (GetInvalidTargetDescription());
248e59b0d2cSJason Molenda             else
249f9fc609fSGreg Clayton                 result.AppendError (GetInvalidProcessDescription());
250f9fc609fSGreg Clayton             return false;
251f9fc609fSGreg Clayton         }
252f9fc609fSGreg Clayton 
253*e87764f2SEnrico Granata         if ((flags & eCommandRequiresThread) && !m_exe_ctx.HasThreadScope())
254f9fc609fSGreg Clayton         {
255e59b0d2cSJason Molenda             if (!m_exe_ctx.HasTargetScope())
256e59b0d2cSJason Molenda                 result.AppendError (GetInvalidTargetDescription());
257e59b0d2cSJason Molenda             else if (!m_exe_ctx.HasProcessScope())
258e59b0d2cSJason Molenda                 result.AppendError (GetInvalidProcessDescription());
259e59b0d2cSJason Molenda             else
260f9fc609fSGreg Clayton                 result.AppendError (GetInvalidThreadDescription());
261f9fc609fSGreg Clayton             return false;
262f9fc609fSGreg Clayton         }
263f9fc609fSGreg Clayton 
264*e87764f2SEnrico Granata         if ((flags & eCommandRequiresFrame) && !m_exe_ctx.HasFrameScope())
265f9fc609fSGreg Clayton         {
266e59b0d2cSJason Molenda             if (!m_exe_ctx.HasTargetScope())
267e59b0d2cSJason Molenda                 result.AppendError (GetInvalidTargetDescription());
268e59b0d2cSJason Molenda             else if (!m_exe_ctx.HasProcessScope())
269e59b0d2cSJason Molenda                 result.AppendError (GetInvalidProcessDescription());
270e59b0d2cSJason Molenda             else if (!m_exe_ctx.HasThreadScope())
271e59b0d2cSJason Molenda                 result.AppendError (GetInvalidThreadDescription());
272e59b0d2cSJason Molenda             else
273f9fc609fSGreg Clayton                 result.AppendError (GetInvalidFrameDescription());
274f9fc609fSGreg Clayton             return false;
275f9fc609fSGreg Clayton         }
276f9fc609fSGreg Clayton 
277*e87764f2SEnrico Granata         if ((flags & eCommandRequiresRegContext) && (m_exe_ctx.GetRegisterContext() == nullptr))
278f9fc609fSGreg Clayton         {
279f9fc609fSGreg Clayton             result.AppendError (GetInvalidRegContextDescription());
280f9fc609fSGreg Clayton             return false;
281f9fc609fSGreg Clayton         }
282f9fc609fSGreg Clayton 
283*e87764f2SEnrico Granata         if (flags & eCommandTryTargetAPILock)
284f9fc609fSGreg Clayton         {
285f9fc609fSGreg Clayton             Target *target = m_exe_ctx.GetTargetPtr();
286f9fc609fSGreg Clayton             if (target)
2879b5442aeSGreg Clayton                 m_api_locker.Lock (target->GetAPIMutex());
288f9fc609fSGreg Clayton         }
289f9fc609fSGreg Clayton     }
290f9fc609fSGreg Clayton 
291*e87764f2SEnrico Granata     if (GetFlags().AnySet (eCommandProcessMustBeLaunched | eCommandProcessMustBePaused))
292b766a73dSGreg Clayton     {
293c14ee32dSGreg Clayton         Process *process = m_interpreter.GetExecutionContext().GetProcessPtr();
294d78c9576SEd Maste         if (process == nullptr)
29530fdc8d8SChris Lattner         {
296b8e8a5f3SJim Ingham             // A process that is not running is considered paused.
297*e87764f2SEnrico Granata             if (GetFlags().Test(eCommandProcessMustBeLaunched))
298b8e8a5f3SJim Ingham             {
29930fdc8d8SChris Lattner                 result.AppendError ("Process must exist.");
30030fdc8d8SChris Lattner                 result.SetStatus (eReturnStatusFailed);
30130fdc8d8SChris Lattner                 return false;
30230fdc8d8SChris Lattner             }
303b8e8a5f3SJim Ingham         }
30430fdc8d8SChris Lattner         else
30530fdc8d8SChris Lattner         {
30630fdc8d8SChris Lattner             StateType state = process->GetState();
30730fdc8d8SChris Lattner             switch (state)
30830fdc8d8SChris Lattner             {
3097a5388bfSGreg Clayton             case eStateInvalid:
31030fdc8d8SChris Lattner             case eStateSuspended:
31130fdc8d8SChris Lattner             case eStateCrashed:
31230fdc8d8SChris Lattner             case eStateStopped:
31330fdc8d8SChris Lattner                 break;
31430fdc8d8SChris Lattner 
315b766a73dSGreg Clayton             case eStateConnected:
316b766a73dSGreg Clayton             case eStateAttaching:
317b766a73dSGreg Clayton             case eStateLaunching:
31830fdc8d8SChris Lattner             case eStateDetached:
31930fdc8d8SChris Lattner             case eStateExited:
32030fdc8d8SChris Lattner             case eStateUnloaded:
321*e87764f2SEnrico Granata                 if (GetFlags().Test(eCommandProcessMustBeLaunched))
32230fdc8d8SChris Lattner                 {
32330fdc8d8SChris Lattner                     result.AppendError ("Process must be launched.");
32430fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusFailed);
32530fdc8d8SChris Lattner                     return false;
32630fdc8d8SChris Lattner                 }
32730fdc8d8SChris Lattner                 break;
32830fdc8d8SChris Lattner 
32930fdc8d8SChris Lattner             case eStateRunning:
33030fdc8d8SChris Lattner             case eStateStepping:
331*e87764f2SEnrico Granata                 if (GetFlags().Test(eCommandProcessMustBePaused))
33230fdc8d8SChris Lattner                 {
33330fdc8d8SChris Lattner                     result.AppendError ("Process is running.  Use 'process interrupt' to pause execution.");
33430fdc8d8SChris Lattner                     result.SetStatus (eReturnStatusFailed);
33530fdc8d8SChris Lattner                     return false;
33630fdc8d8SChris Lattner                 }
33730fdc8d8SChris Lattner             }
33830fdc8d8SChris Lattner         }
339b766a73dSGreg Clayton     }
3405a988416SJim Ingham     return true;
34130fdc8d8SChris Lattner }
34230fdc8d8SChris Lattner 
343f9fc609fSGreg Clayton void
344f9fc609fSGreg Clayton CommandObject::Cleanup ()
345f9fc609fSGreg Clayton {
346f9fc609fSGreg Clayton     m_exe_ctx.Clear();
347f9fc609fSGreg Clayton     m_api_locker.Unlock();
348f9fc609fSGreg Clayton }
349f9fc609fSGreg Clayton 
350f9fc609fSGreg Clayton 
35130fdc8d8SChris Lattner class CommandDictCommandPartialMatch
35230fdc8d8SChris Lattner {
35330fdc8d8SChris Lattner     public:
35430fdc8d8SChris Lattner         CommandDictCommandPartialMatch (const char *match_str)
35530fdc8d8SChris Lattner         {
35630fdc8d8SChris Lattner             m_match_str = match_str;
35730fdc8d8SChris Lattner         }
35830fdc8d8SChris Lattner         bool operator() (const std::pair<std::string, lldb::CommandObjectSP> map_element) const
35930fdc8d8SChris Lattner         {
36030fdc8d8SChris Lattner             // A NULL or empty string matches everything.
361d78c9576SEd Maste             if (m_match_str == nullptr || *m_match_str == '\0')
362c7bece56SGreg Clayton                 return true;
36330fdc8d8SChris Lattner 
364c7bece56SGreg Clayton             return map_element.first.find (m_match_str, 0) == 0;
36530fdc8d8SChris Lattner         }
36630fdc8d8SChris Lattner 
36730fdc8d8SChris Lattner     private:
36830fdc8d8SChris Lattner         const char *m_match_str;
36930fdc8d8SChris Lattner };
37030fdc8d8SChris Lattner 
37130fdc8d8SChris Lattner int
37230fdc8d8SChris Lattner CommandObject::AddNamesMatchingPartialString (CommandObject::CommandMap &in_map, const char *cmd_str,
37330fdc8d8SChris Lattner                                               StringList &matches)
37430fdc8d8SChris Lattner {
37530fdc8d8SChris Lattner     int number_added = 0;
37630fdc8d8SChris Lattner     CommandDictCommandPartialMatch matcher(cmd_str);
37730fdc8d8SChris Lattner 
37830fdc8d8SChris Lattner     CommandObject::CommandMap::iterator matching_cmds = std::find_if (in_map.begin(), in_map.end(), matcher);
37930fdc8d8SChris Lattner 
38030fdc8d8SChris Lattner     while (matching_cmds != in_map.end())
38130fdc8d8SChris Lattner     {
38230fdc8d8SChris Lattner         ++number_added;
38330fdc8d8SChris Lattner         matches.AppendString((*matching_cmds).first.c_str());
38430fdc8d8SChris Lattner         matching_cmds = std::find_if (++matching_cmds, in_map.end(), matcher);;
38530fdc8d8SChris Lattner     }
38630fdc8d8SChris Lattner     return number_added;
38730fdc8d8SChris Lattner }
38830fdc8d8SChris Lattner 
38930fdc8d8SChris Lattner int
39030fdc8d8SChris Lattner CommandObject::HandleCompletion
39130fdc8d8SChris Lattner (
39230fdc8d8SChris Lattner     Args &input,
39330fdc8d8SChris Lattner     int &cursor_index,
39430fdc8d8SChris Lattner     int &cursor_char_position,
39530fdc8d8SChris Lattner     int match_start_point,
39630fdc8d8SChris Lattner     int max_return_elements,
397558ce124SJim Ingham     bool &word_complete,
39830fdc8d8SChris Lattner     StringList &matches
39930fdc8d8SChris Lattner )
40030fdc8d8SChris Lattner {
4016561d15dSJohnny Chen     // Default implmentation of WantsCompletion() is !WantsRawCommandString().
4026561d15dSJohnny Chen     // Subclasses who want raw command string but desire, for example,
4036561d15dSJohnny Chen     // argument completion should override WantsCompletion() to return true,
4046561d15dSJohnny Chen     // instead.
4056f99b637SJohnny Chen     if (WantsRawCommandString() && !WantsCompletion())
40630fdc8d8SChris Lattner     {
40730fdc8d8SChris Lattner         // FIXME: Abstract telling the completion to insert the completion character.
40830fdc8d8SChris Lattner         matches.Clear();
40930fdc8d8SChris Lattner         return -1;
41030fdc8d8SChris Lattner     }
41130fdc8d8SChris Lattner     else
41230fdc8d8SChris Lattner     {
41330fdc8d8SChris Lattner         // Can we do anything generic with the options?
41430fdc8d8SChris Lattner         Options *cur_options = GetOptions();
41530fdc8d8SChris Lattner         CommandReturnObject result;
41630fdc8d8SChris Lattner         OptionElementVector opt_element_vector;
41730fdc8d8SChris Lattner 
418d78c9576SEd Maste         if (cur_options != nullptr)
41930fdc8d8SChris Lattner         {
42030fdc8d8SChris Lattner             // Re-insert the dummy command name string which will have been
42130fdc8d8SChris Lattner             // stripped off:
42230fdc8d8SChris Lattner             input.Unshift ("dummy-string");
42330fdc8d8SChris Lattner             cursor_index++;
42430fdc8d8SChris Lattner 
42530fdc8d8SChris Lattner 
42630fdc8d8SChris Lattner             // I stick an element on the end of the input, because if the last element is
427b7ad58a0SGreg Clayton             // option that requires an argument, getopt_long_only will freak out.
42830fdc8d8SChris Lattner 
42930fdc8d8SChris Lattner             input.AppendArgument ("<FAKE-VALUE>");
43030fdc8d8SChris Lattner 
431d43e0094SJim Ingham             input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index);
43230fdc8d8SChris Lattner 
43330fdc8d8SChris Lattner             input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1);
43430fdc8d8SChris Lattner 
43530fdc8d8SChris Lattner             bool handled_by_options;
436eb0103f2SGreg Clayton             handled_by_options = cur_options->HandleOptionCompletion (input,
43730fdc8d8SChris Lattner                                                                       opt_element_vector,
43830fdc8d8SChris Lattner                                                                       cursor_index,
43930fdc8d8SChris Lattner                                                                       cursor_char_position,
44030fdc8d8SChris Lattner                                                                       match_start_point,
44130fdc8d8SChris Lattner                                                                       max_return_elements,
442558ce124SJim Ingham                                                                       word_complete,
44330fdc8d8SChris Lattner                                                                       matches);
44430fdc8d8SChris Lattner             if (handled_by_options)
44530fdc8d8SChris Lattner                 return matches.GetSize();
44630fdc8d8SChris Lattner         }
44730fdc8d8SChris Lattner 
44830fdc8d8SChris Lattner         // If we got here, the last word is not an option or an option argument.
449a7015092SGreg Clayton         return HandleArgumentCompletion (input,
45030fdc8d8SChris Lattner                                          cursor_index,
45130fdc8d8SChris Lattner                                          cursor_char_position,
45230fdc8d8SChris Lattner                                          opt_element_vector,
45330fdc8d8SChris Lattner                                          match_start_point,
45430fdc8d8SChris Lattner                                          max_return_elements,
455558ce124SJim Ingham                                          word_complete,
45630fdc8d8SChris Lattner                                          matches);
45730fdc8d8SChris Lattner     }
45830fdc8d8SChris Lattner }
45930fdc8d8SChris Lattner 
46030fdc8d8SChris Lattner bool
461a7015092SGreg Clayton CommandObject::HelpTextContainsWord (const char *search_word)
46230fdc8d8SChris Lattner {
46330fdc8d8SChris Lattner     std::string options_usage_help;
46430fdc8d8SChris Lattner 
46530fdc8d8SChris Lattner     bool found_word = false;
46630fdc8d8SChris Lattner 
467998255bfSGreg Clayton     const char *short_help = GetHelp();
468998255bfSGreg Clayton     const char *long_help = GetHelpLong();
469998255bfSGreg Clayton     const char *syntax_help = GetSyntax();
47030fdc8d8SChris Lattner 
471998255bfSGreg Clayton     if (short_help && strcasestr (short_help, search_word))
47230fdc8d8SChris Lattner         found_word = true;
473998255bfSGreg Clayton     else if (long_help && strcasestr (long_help, search_word))
47430fdc8d8SChris Lattner         found_word = true;
475998255bfSGreg Clayton     else if (syntax_help && strcasestr (syntax_help, search_word))
47630fdc8d8SChris Lattner         found_word = true;
47730fdc8d8SChris Lattner 
47830fdc8d8SChris Lattner     if (!found_word
479d78c9576SEd Maste         && GetOptions() != nullptr)
48030fdc8d8SChris Lattner     {
48130fdc8d8SChris Lattner         StreamString usage_help;
482eb0103f2SGreg Clayton         GetOptions()->GenerateOptionUsage (usage_help, this);
48330fdc8d8SChris Lattner         if (usage_help.GetSize() > 0)
48430fdc8d8SChris Lattner         {
48530fdc8d8SChris Lattner             const char *usage_text = usage_help.GetData();
4864b6fbf37SCaroline Tice             if (strcasestr (usage_text, search_word))
48730fdc8d8SChris Lattner               found_word = true;
48830fdc8d8SChris Lattner         }
48930fdc8d8SChris Lattner     }
49030fdc8d8SChris Lattner 
49130fdc8d8SChris Lattner     return found_word;
49230fdc8d8SChris Lattner }
493e139cf23SCaroline Tice 
494e139cf23SCaroline Tice int
495e139cf23SCaroline Tice CommandObject::GetNumArgumentEntries  ()
496e139cf23SCaroline Tice {
497e139cf23SCaroline Tice     return m_arguments.size();
498e139cf23SCaroline Tice }
499e139cf23SCaroline Tice 
500e139cf23SCaroline Tice CommandObject::CommandArgumentEntry *
501e139cf23SCaroline Tice CommandObject::GetArgumentEntryAtIndex (int idx)
502e139cf23SCaroline Tice {
5033985c8c6SSaleem Abdulrasool     if (static_cast<size_t>(idx) < m_arguments.size())
504e139cf23SCaroline Tice         return &(m_arguments[idx]);
505e139cf23SCaroline Tice 
506d78c9576SEd Maste     return nullptr;
507e139cf23SCaroline Tice }
508e139cf23SCaroline Tice 
509d7e6a4f2SVince Harron const CommandObject::ArgumentTableEntry *
510e139cf23SCaroline Tice CommandObject::FindArgumentDataByType (CommandArgumentType arg_type)
511e139cf23SCaroline Tice {
512e139cf23SCaroline Tice     const ArgumentTableEntry *table = CommandObject::GetArgumentTable();
513e139cf23SCaroline Tice 
514e139cf23SCaroline Tice     for (int i = 0; i < eArgTypeLastArg; ++i)
515e139cf23SCaroline Tice         if (table[i].arg_type == arg_type)
516d7e6a4f2SVince Harron             return &(table[i]);
517e139cf23SCaroline Tice 
518d78c9576SEd Maste     return nullptr;
519e139cf23SCaroline Tice }
520e139cf23SCaroline Tice 
521e139cf23SCaroline Tice void
522e139cf23SCaroline Tice CommandObject::GetArgumentHelp (Stream &str, CommandArgumentType arg_type, CommandInterpreter &interpreter)
523e139cf23SCaroline Tice {
524e139cf23SCaroline Tice     const ArgumentTableEntry* table = CommandObject::GetArgumentTable();
525d7e6a4f2SVince Harron     const ArgumentTableEntry *entry = &(table[arg_type]);
526e139cf23SCaroline Tice 
527e139cf23SCaroline Tice     // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
528e139cf23SCaroline Tice 
529e139cf23SCaroline Tice     if (entry->arg_type != arg_type)
530e139cf23SCaroline Tice         entry = CommandObject::FindArgumentDataByType (arg_type);
531e139cf23SCaroline Tice 
532e139cf23SCaroline Tice     if (!entry)
533e139cf23SCaroline Tice         return;
534e139cf23SCaroline Tice 
535e139cf23SCaroline Tice     StreamString name_str;
536e139cf23SCaroline Tice     name_str.Printf ("<%s>", entry->arg_name);
537e139cf23SCaroline Tice 
538fc7a7f3bSEnrico Granata     if (entry->help_function)
53982a7d983SEnrico Granata     {
540fc7a7f3bSEnrico Granata         const char* help_text = entry->help_function();
54182a7d983SEnrico Granata         if (!entry->help_function.self_formatting)
54282a7d983SEnrico Granata         {
54382a7d983SEnrico Granata             interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", help_text,
544e139cf23SCaroline Tice                                                  name_str.GetSize());
54582a7d983SEnrico Granata         }
54682a7d983SEnrico Granata         else
54782a7d983SEnrico Granata         {
54882a7d983SEnrico Granata             interpreter.OutputHelpText(str, name_str.GetData(), "--", help_text,
54982a7d983SEnrico Granata                                        name_str.GetSize());
55082a7d983SEnrico Granata         }
55182a7d983SEnrico Granata     }
552e139cf23SCaroline Tice     else
553e139cf23SCaroline Tice         interpreter.OutputFormattedHelpText (str, name_str.GetData(), "--", entry->help_text, name_str.GetSize());
554e139cf23SCaroline Tice }
555e139cf23SCaroline Tice 
556e139cf23SCaroline Tice const char *
557e139cf23SCaroline Tice CommandObject::GetArgumentName (CommandArgumentType arg_type)
558e139cf23SCaroline Tice {
559d7e6a4f2SVince Harron     const ArgumentTableEntry *entry = &(CommandObject::GetArgumentTable()[arg_type]);
560deaab222SCaroline Tice 
561deaab222SCaroline Tice     // The table is *supposed* to be kept in arg_type order, but someone *could* have messed it up...
562deaab222SCaroline Tice 
563deaab222SCaroline Tice     if (entry->arg_type != arg_type)
564deaab222SCaroline Tice         entry = CommandObject::FindArgumentDataByType (arg_type);
565deaab222SCaroline Tice 
566e6acf355SJohnny Chen     if (entry)
567deaab222SCaroline Tice         return entry->arg_name;
568e6acf355SJohnny Chen 
569e6acf355SJohnny Chen     StreamString str;
570e6acf355SJohnny Chen     str << "Arg name for type (" << arg_type << ") not in arg table!";
571e6acf355SJohnny Chen     return str.GetData();
572e139cf23SCaroline Tice }
573e139cf23SCaroline Tice 
574405fe67fSCaroline Tice bool
575e0d378b3SGreg Clayton CommandObject::IsPairType (ArgumentRepetitionType arg_repeat_type)
576405fe67fSCaroline Tice {
577405fe67fSCaroline Tice     if ((arg_repeat_type == eArgRepeatPairPlain)
578405fe67fSCaroline Tice         ||  (arg_repeat_type == eArgRepeatPairOptional)
579405fe67fSCaroline Tice         ||  (arg_repeat_type == eArgRepeatPairPlus)
580405fe67fSCaroline Tice         ||  (arg_repeat_type == eArgRepeatPairStar)
581405fe67fSCaroline Tice         ||  (arg_repeat_type == eArgRepeatPairRange)
582405fe67fSCaroline Tice         ||  (arg_repeat_type == eArgRepeatPairRangeOptional))
583405fe67fSCaroline Tice         return true;
584405fe67fSCaroline Tice 
585405fe67fSCaroline Tice     return false;
586405fe67fSCaroline Tice }
587405fe67fSCaroline Tice 
58834ddc8dbSJohnny Chen static CommandObject::CommandArgumentEntry
58934ddc8dbSJohnny Chen OptSetFiltered(uint32_t opt_set_mask, CommandObject::CommandArgumentEntry &cmd_arg_entry)
59034ddc8dbSJohnny Chen {
59134ddc8dbSJohnny Chen     CommandObject::CommandArgumentEntry ret_val;
59234ddc8dbSJohnny Chen     for (unsigned i = 0; i < cmd_arg_entry.size(); ++i)
59334ddc8dbSJohnny Chen         if (opt_set_mask & cmd_arg_entry[i].arg_opt_set_association)
59434ddc8dbSJohnny Chen             ret_val.push_back(cmd_arg_entry[i]);
59534ddc8dbSJohnny Chen     return ret_val;
59634ddc8dbSJohnny Chen }
59734ddc8dbSJohnny Chen 
59834ddc8dbSJohnny Chen // Default parameter value of opt_set_mask is LLDB_OPT_SET_ALL, which means take
59934ddc8dbSJohnny Chen // all the argument data into account.  On rare cases where some argument sticks
60034ddc8dbSJohnny Chen // with certain option sets, this function returns the option set filtered args.
601e139cf23SCaroline Tice void
60234ddc8dbSJohnny Chen CommandObject::GetFormattedCommandArguments (Stream &str, uint32_t opt_set_mask)
603e139cf23SCaroline Tice {
604e139cf23SCaroline Tice     int num_args = m_arguments.size();
605e139cf23SCaroline Tice     for (int i = 0; i < num_args; ++i)
606e139cf23SCaroline Tice     {
607e139cf23SCaroline Tice         if (i > 0)
608e139cf23SCaroline Tice             str.Printf (" ");
60934ddc8dbSJohnny Chen         CommandArgumentEntry arg_entry =
61034ddc8dbSJohnny Chen             opt_set_mask == LLDB_OPT_SET_ALL ? m_arguments[i]
61134ddc8dbSJohnny Chen                                              : OptSetFiltered(opt_set_mask, m_arguments[i]);
612e139cf23SCaroline Tice         int num_alternatives = arg_entry.size();
613405fe67fSCaroline Tice 
614405fe67fSCaroline Tice         if ((num_alternatives == 2)
615405fe67fSCaroline Tice             && IsPairType (arg_entry[0].arg_repetition))
616405fe67fSCaroline Tice         {
617405fe67fSCaroline Tice             const char *first_name = GetArgumentName (arg_entry[0].arg_type);
618405fe67fSCaroline Tice             const char *second_name = GetArgumentName (arg_entry[1].arg_type);
619405fe67fSCaroline Tice             switch (arg_entry[0].arg_repetition)
620405fe67fSCaroline Tice             {
621405fe67fSCaroline Tice                 case eArgRepeatPairPlain:
622405fe67fSCaroline Tice                     str.Printf ("<%s> <%s>", first_name, second_name);
623405fe67fSCaroline Tice                     break;
624405fe67fSCaroline Tice                 case eArgRepeatPairOptional:
625405fe67fSCaroline Tice                     str.Printf ("[<%s> <%s>]", first_name, second_name);
626405fe67fSCaroline Tice                     break;
627405fe67fSCaroline Tice                 case eArgRepeatPairPlus:
628405fe67fSCaroline Tice                     str.Printf ("<%s> <%s> [<%s> <%s> [...]]", first_name, second_name, first_name, second_name);
629405fe67fSCaroline Tice                     break;
630405fe67fSCaroline Tice                 case eArgRepeatPairStar:
631405fe67fSCaroline Tice                     str.Printf ("[<%s> <%s> [<%s> <%s> [...]]]", first_name, second_name, first_name, second_name);
632405fe67fSCaroline Tice                     break;
633405fe67fSCaroline Tice                 case eArgRepeatPairRange:
634405fe67fSCaroline Tice                     str.Printf ("<%s_1> <%s_1> ... <%s_n> <%s_n>", first_name, second_name, first_name, second_name);
635405fe67fSCaroline Tice                     break;
636405fe67fSCaroline Tice                 case eArgRepeatPairRangeOptional:
637405fe67fSCaroline Tice                     str.Printf ("[<%s_1> <%s_1> ... <%s_n> <%s_n>]", first_name, second_name, first_name, second_name);
638405fe67fSCaroline Tice                     break;
639ca1176aaSCaroline Tice                 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
640ca1176aaSCaroline Tice                 // missing case statement(s).
641ca1176aaSCaroline Tice                 case eArgRepeatPlain:
642ca1176aaSCaroline Tice                 case eArgRepeatOptional:
643ca1176aaSCaroline Tice                 case eArgRepeatPlus:
644ca1176aaSCaroline Tice                 case eArgRepeatStar:
645ca1176aaSCaroline Tice                 case eArgRepeatRange:
646ca1176aaSCaroline Tice                     // These should not be reached, as they should fail the IsPairType test above.
647ca1176aaSCaroline Tice                     break;
648405fe67fSCaroline Tice             }
649405fe67fSCaroline Tice         }
650405fe67fSCaroline Tice         else
651405fe67fSCaroline Tice         {
652e139cf23SCaroline Tice             StreamString names;
653e139cf23SCaroline Tice             for (int j = 0; j < num_alternatives; ++j)
654e139cf23SCaroline Tice             {
655e139cf23SCaroline Tice                 if (j > 0)
656e139cf23SCaroline Tice                     names.Printf (" | ");
657e139cf23SCaroline Tice                 names.Printf ("%s", GetArgumentName (arg_entry[j].arg_type));
658e139cf23SCaroline Tice             }
659e139cf23SCaroline Tice             switch (arg_entry[0].arg_repetition)
660e139cf23SCaroline Tice             {
661e139cf23SCaroline Tice                 case eArgRepeatPlain:
662e139cf23SCaroline Tice                     str.Printf ("<%s>", names.GetData());
663e139cf23SCaroline Tice                     break;
664e139cf23SCaroline Tice                 case eArgRepeatPlus:
665e139cf23SCaroline Tice                     str.Printf ("<%s> [<%s> [...]]", names.GetData(), names.GetData());
666e139cf23SCaroline Tice                     break;
667e139cf23SCaroline Tice                 case eArgRepeatStar:
668e139cf23SCaroline Tice                     str.Printf ("[<%s> [<%s> [...]]]", names.GetData(), names.GetData());
669e139cf23SCaroline Tice                     break;
670e139cf23SCaroline Tice                 case eArgRepeatOptional:
671e139cf23SCaroline Tice                     str.Printf ("[<%s>]", names.GetData());
672e139cf23SCaroline Tice                     break;
673405fe67fSCaroline Tice                 case eArgRepeatRange:
674fd54b368SJason Molenda                     str.Printf ("<%s_1> .. <%s_n>", names.GetData(), names.GetData());
675ca1176aaSCaroline Tice                     break;
676ca1176aaSCaroline Tice                 // Explicitly test for all the rest of the cases, so if new types get added we will notice the
677ca1176aaSCaroline Tice                 // missing case statement(s).
678ca1176aaSCaroline Tice                 case eArgRepeatPairPlain:
679ca1176aaSCaroline Tice                 case eArgRepeatPairOptional:
680ca1176aaSCaroline Tice                 case eArgRepeatPairPlus:
681ca1176aaSCaroline Tice                 case eArgRepeatPairStar:
682ca1176aaSCaroline Tice                 case eArgRepeatPairRange:
683ca1176aaSCaroline Tice                 case eArgRepeatPairRangeOptional:
684ca1176aaSCaroline Tice                     // These should not be hit, as they should pass the IsPairType test above, and control should
685ca1176aaSCaroline Tice                     // have gone into the other branch of the if statement.
686ca1176aaSCaroline Tice                     break;
687405fe67fSCaroline Tice             }
688e139cf23SCaroline Tice         }
689e139cf23SCaroline Tice     }
690e139cf23SCaroline Tice }
691e139cf23SCaroline Tice 
6920c16aa6dSStephen Wilson CommandArgumentType
693e139cf23SCaroline Tice CommandObject::LookupArgumentName (const char *arg_name)
694e139cf23SCaroline Tice {
695e139cf23SCaroline Tice     CommandArgumentType return_type = eArgTypeLastArg;
696e139cf23SCaroline Tice 
697e139cf23SCaroline Tice     std::string arg_name_str (arg_name);
698e139cf23SCaroline Tice     size_t len = arg_name_str.length();
699e139cf23SCaroline Tice     if (arg_name[0] == '<'
700e139cf23SCaroline Tice         && arg_name[len-1] == '>')
701e139cf23SCaroline Tice         arg_name_str = arg_name_str.substr (1, len-2);
702e139cf23SCaroline Tice 
703331eff39SJohnny Chen     const ArgumentTableEntry *table = GetArgumentTable();
704e139cf23SCaroline Tice     for (int i = 0; i < eArgTypeLastArg; ++i)
705331eff39SJohnny Chen         if (arg_name_str.compare (table[i].arg_name) == 0)
706e139cf23SCaroline Tice             return_type = g_arguments_data[i].arg_type;
707e139cf23SCaroline Tice 
708e139cf23SCaroline Tice     return return_type;
709e139cf23SCaroline Tice }
710e139cf23SCaroline Tice 
711e139cf23SCaroline Tice static const char *
712931e674aSJim Ingham RegisterNameHelpTextCallback ()
713931e674aSJim Ingham {
714931e674aSJim Ingham     return "Register names can be specified using the architecture specific names.  "
71584c7bd74SJim Ingham     "They can also be specified using generic names.  Not all generic entities have "
71684c7bd74SJim Ingham     "registers backing them on all architectures.  When they don't the generic name "
71784c7bd74SJim Ingham     "will return an error.\n"
718931e674aSJim Ingham     "The generic names defined in lldb are:\n"
719931e674aSJim Ingham     "\n"
720931e674aSJim Ingham     "pc       - program counter register\n"
721931e674aSJim Ingham     "ra       - return address register\n"
722931e674aSJim Ingham     "fp       - frame pointer register\n"
723931e674aSJim Ingham     "sp       - stack pointer register\n"
72484c7bd74SJim Ingham     "flags    - the flags register\n"
725931e674aSJim Ingham     "arg{1-6} - integer argument passing registers.\n";
726931e674aSJim Ingham }
727931e674aSJim Ingham 
728931e674aSJim Ingham static const char *
729e139cf23SCaroline Tice BreakpointIDHelpTextCallback ()
730e139cf23SCaroline Tice {
73186edbf41SGreg Clayton     return "Breakpoint ID's consist major and minor numbers;  the major number "
73286edbf41SGreg Clayton     "corresponds to the single entity that was created with a 'breakpoint set' "
73386edbf41SGreg Clayton     "command; the minor numbers correspond to all the locations that were actually "
73486edbf41SGreg Clayton     "found/set based on the major breakpoint.  A full breakpoint ID might look like "
73586edbf41SGreg Clayton     "3.14, meaning the 14th location set for the 3rd breakpoint.  You can specify "
73686edbf41SGreg Clayton     "all the locations of a breakpoint by just indicating the major breakpoint "
73786edbf41SGreg Clayton     "number. A valid breakpoint id consists either of just the major id number, "
73886edbf41SGreg Clayton     "or the major number, a dot, and the location number (e.g. 3 or 3.2 could "
73986edbf41SGreg Clayton     "both be valid breakpoint ids).";
740e139cf23SCaroline Tice }
741e139cf23SCaroline Tice 
742e139cf23SCaroline Tice static const char *
743e139cf23SCaroline Tice BreakpointIDRangeHelpTextCallback ()
744e139cf23SCaroline Tice {
74586edbf41SGreg Clayton     return "A 'breakpoint id list' is a manner of specifying multiple breakpoints. "
74686edbf41SGreg Clayton     "This can be done  through several mechanisms.  The easiest way is to just "
74786edbf41SGreg Clayton     "enter a space-separated list of breakpoint ids.  To specify all the "
74886edbf41SGreg Clayton     "breakpoint locations under a major breakpoint, you can use the major "
74986edbf41SGreg Clayton     "breakpoint number followed by '.*', eg. '5.*' means all the locations under "
75086edbf41SGreg Clayton     "breakpoint 5.  You can also indicate a range of breakpoints by using "
75186edbf41SGreg Clayton     "<start-bp-id> - <end-bp-id>.  The start-bp-id and end-bp-id for a range can "
75286edbf41SGreg Clayton     "be any valid breakpoint ids.  It is not legal, however, to specify a range "
75386edbf41SGreg Clayton     "using specific locations that cross major breakpoint numbers.  I.e. 3.2 - 3.7"
75486edbf41SGreg Clayton     " is legal; 2 - 5 is legal; but 3.2 - 4.4 is not legal.";
75586edbf41SGreg Clayton }
75686edbf41SGreg Clayton 
75786edbf41SGreg Clayton static const char *
7585e09c8c3SJim Ingham BreakpointNameHelpTextCallback ()
7595e09c8c3SJim Ingham {
7605e09c8c3SJim Ingham     return "A name that can be added to a breakpoint when it is created, or later "
7615e09c8c3SJim Ingham     "on with the \"breakpoint name add\" command.  "
7625e09c8c3SJim Ingham     "Breakpoint names can be used to specify breakpoints in all the places breakpoint ID's "
7635e09c8c3SJim Ingham     "and breakpoint ID ranges can be used.  As such they provide a convenient way to group breakpoints, "
7645e09c8c3SJim Ingham     "and to operate on breakpoints you create without having to track the breakpoint number.  "
7655e09c8c3SJim Ingham     "Note, the attributes you set when using a breakpoint name in a breakpoint command don't "
7665e09c8c3SJim Ingham     "adhere to the name, but instead are set individually on all the breakpoints currently tagged with that name.  Future breakpoints "
7675e09c8c3SJim Ingham     "tagged with that name will not pick up the attributes previously given using that name.  "
7685e09c8c3SJim Ingham     "In order to distinguish breakpoint names from breakpoint ID's and ranges, "
7695e09c8c3SJim Ingham     "names must start with a letter from a-z or A-Z and cannot contain spaces, \".\" or \"-\".  "
7705e09c8c3SJim Ingham     "Also, breakpoint names can only be applied to breakpoints, not to breakpoint locations.";
7715e09c8c3SJim Ingham }
7725e09c8c3SJim Ingham 
7735e09c8c3SJim Ingham static const char *
77486edbf41SGreg Clayton GDBFormatHelpTextCallback ()
77586edbf41SGreg Clayton {
776f91381e8SGreg Clayton     return "A GDB format consists of a repeat count, a format letter and a size letter. "
777f91381e8SGreg Clayton     "The repeat count is optional and defaults to 1. The format letter is optional "
778f91381e8SGreg Clayton     "and defaults to the previous format that was used. The size letter is optional "
779f91381e8SGreg Clayton     "and defaults to the previous size that was used.\n"
780f91381e8SGreg Clayton     "\n"
781f91381e8SGreg Clayton     "Format letters include:\n"
782f91381e8SGreg Clayton     "o - octal\n"
783f91381e8SGreg Clayton     "x - hexadecimal\n"
784f91381e8SGreg Clayton     "d - decimal\n"
785f91381e8SGreg Clayton     "u - unsigned decimal\n"
786f91381e8SGreg Clayton     "t - binary\n"
787f91381e8SGreg Clayton     "f - float\n"
788f91381e8SGreg Clayton     "a - address\n"
789f91381e8SGreg Clayton     "i - instruction\n"
790f91381e8SGreg Clayton     "c - char\n"
791f91381e8SGreg Clayton     "s - string\n"
792f91381e8SGreg Clayton     "T - OSType\n"
793f91381e8SGreg Clayton     "A - float as hex\n"
794f91381e8SGreg Clayton     "\n"
795f91381e8SGreg Clayton     "Size letters include:\n"
796f91381e8SGreg Clayton     "b - 1 byte  (byte)\n"
797f91381e8SGreg Clayton     "h - 2 bytes (halfword)\n"
798f91381e8SGreg Clayton     "w - 4 bytes (word)\n"
799f91381e8SGreg Clayton     "g - 8 bytes (giant)\n"
800f91381e8SGreg Clayton     "\n"
801f91381e8SGreg Clayton     "Example formats:\n"
802f91381e8SGreg Clayton     "32xb - show 32 1 byte hexadecimal integer values\n"
803f91381e8SGreg Clayton     "16xh - show 16 2 byte hexadecimal integer values\n"
804f91381e8SGreg Clayton     "64   - show 64 2 byte hexadecimal integer values (format and size from the last format)\n"
805f91381e8SGreg Clayton     "dw   - show 1 4 byte decimal integer value\n"
806f91381e8SGreg Clayton     ;
807e139cf23SCaroline Tice }
808e139cf23SCaroline Tice 
8090a3958e0SEnrico Granata static const char *
8100a3958e0SEnrico Granata FormatHelpTextCallback ()
8110a3958e0SEnrico Granata {
81282a7d983SEnrico Granata 
813d78c9576SEd Maste     static char* help_text_ptr = nullptr;
81482a7d983SEnrico Granata 
81582a7d983SEnrico Granata     if (help_text_ptr)
81682a7d983SEnrico Granata         return help_text_ptr;
81782a7d983SEnrico Granata 
8180a3958e0SEnrico Granata     StreamString sstr;
8190a3958e0SEnrico Granata     sstr << "One of the format names (or one-character names) that can be used to show a variable's value:\n";
8200a3958e0SEnrico Granata     for (Format f = eFormatDefault; f < kNumFormats; f = Format(f+1))
8210a3958e0SEnrico Granata     {
82282a7d983SEnrico Granata         if (f != eFormatDefault)
82382a7d983SEnrico Granata             sstr.PutChar('\n');
82482a7d983SEnrico Granata 
8250a3958e0SEnrico Granata         char format_char = FormatManager::GetFormatAsFormatChar(f);
8260a3958e0SEnrico Granata         if (format_char)
8270a3958e0SEnrico Granata             sstr.Printf("'%c' or ", format_char);
8280a3958e0SEnrico Granata 
82982a7d983SEnrico Granata         sstr.Printf ("\"%s\"", FormatManager::GetFormatAsCString(f));
8300a3958e0SEnrico Granata     }
8310a3958e0SEnrico Granata 
8320a3958e0SEnrico Granata     sstr.Flush();
8330a3958e0SEnrico Granata 
8340a3958e0SEnrico Granata     std::string data = sstr.GetString();
8350a3958e0SEnrico Granata 
83682a7d983SEnrico Granata     help_text_ptr = new char[data.length()+1];
8370a3958e0SEnrico Granata 
83882a7d983SEnrico Granata     data.copy(help_text_ptr, data.length());
8390a3958e0SEnrico Granata 
84082a7d983SEnrico Granata     return help_text_ptr;
8410a3958e0SEnrico Granata }
8420a3958e0SEnrico Granata 
8430a3958e0SEnrico Granata static const char *
844d9477397SSean Callanan LanguageTypeHelpTextCallback ()
845d9477397SSean Callanan {
846d78c9576SEd Maste     static char* help_text_ptr = nullptr;
847d9477397SSean Callanan 
848d9477397SSean Callanan     if (help_text_ptr)
849d9477397SSean Callanan         return help_text_ptr;
850d9477397SSean Callanan 
851d9477397SSean Callanan     StreamString sstr;
852d9477397SSean Callanan     sstr << "One of the following languages:\n";
853d9477397SSean Callanan 
854de50d36aSJim Ingham     LanguageRuntime::PrintAllLanguages(sstr, "  ", "\n");
855d9477397SSean Callanan 
856d9477397SSean Callanan     sstr.Flush();
857d9477397SSean Callanan 
858d9477397SSean Callanan     std::string data = sstr.GetString();
859d9477397SSean Callanan 
860d9477397SSean Callanan     help_text_ptr = new char[data.length()+1];
861d9477397SSean Callanan 
862d9477397SSean Callanan     data.copy(help_text_ptr, data.length());
863d9477397SSean Callanan 
864d9477397SSean Callanan     return help_text_ptr;
865d9477397SSean Callanan }
866d9477397SSean Callanan 
867d9477397SSean Callanan static const char *
86882a7d983SEnrico Granata SummaryStringHelpTextCallback()
8690a3958e0SEnrico Granata {
87082a7d983SEnrico Granata     return
87182a7d983SEnrico Granata         "A summary string is a way to extract information from variables in order to present them using a summary.\n"
87282a7d983SEnrico Granata         "Summary strings contain static text, variables, scopes and control sequences:\n"
87382a7d983SEnrico Granata         "  - Static text can be any sequence of non-special characters, i.e. anything but '{', '}', '$', or '\\'.\n"
87482a7d983SEnrico Granata         "  - Variables are sequences of characters beginning with ${, ending with } and that contain symbols in the format described below.\n"
87582a7d983SEnrico Granata         "  - Scopes are any sequence of text between { and }. Anything included in a scope will only appear in the output summary if there were no errors.\n"
87682a7d983SEnrico Granata         "  - Control sequences are the usual C/C++ '\\a', '\\n', ..., plus '\\$', '\\{' and '\\}'.\n"
87782a7d983SEnrico Granata         "A summary string works by copying static text verbatim, turning control sequences into their character counterpart, expanding variables and trying to expand scopes.\n"
87882a7d983SEnrico Granata         "A variable is expanded by giving it a value other than its textual representation, and the way this is done depends on what comes after the ${ marker.\n"
87982a7d983SEnrico Granata         "The most common sequence if ${var followed by an expression path, which is the text one would type to access a member of an aggregate types, given a variable of that type"
88082a7d983SEnrico Granata         " (e.g. if type T has a member named x, which has a member named y, and if t is of type T, the expression path would be .x.y and the way to fit that into a summary string would be"
8819128ee2fSEnrico Granata         " ${var.x.y}). You can also use ${*var followed by an expression path and in that case the object referred by the path will be dereferenced before being displayed."
8829128ee2fSEnrico Granata         " If the object is not a pointer, doing so will cause an error. For additional details on expression paths, you can type 'help expr-path'. \n"
88382a7d983SEnrico Granata         "By default, summary strings attempt to display the summary for any variable they reference, and if that fails the value. If neither can be shown, nothing is displayed."
88482a7d983SEnrico Granata         "In a summary string, you can also use an array index [n], or a slice-like range [n-m]. This can have two different meanings depending on what kind of object the expression"
88582a7d983SEnrico Granata         " path refers to:\n"
88682a7d983SEnrico Granata         "  - if it is a scalar type (any basic type like int, float, ...) the expression is a bitfield, i.e. the bits indicated by the indexing operator are extracted out of the number"
88782a7d983SEnrico Granata         " and displayed as an individual variable\n"
88882a7d983SEnrico Granata         "  - if it is an array or pointer the array items indicated by the indexing operator are shown as the result of the variable. if the expression is an array, real array items are"
88982a7d983SEnrico Granata         " printed; if it is a pointer, the pointer-as-array syntax is used to obtain the values (this means, the latter case can have no range checking)\n"
8909128ee2fSEnrico Granata         "If you are trying to display an array for which the size is known, you can also use [] instead of giving an exact range. This has the effect of showing items 0 thru size - 1.\n"
8919128ee2fSEnrico Granata         "Additionally, a variable can contain an (optional) format code, as in ${var.x.y%code}, where code can be any of the valid formats described in 'help format', or one of the"
8929128ee2fSEnrico Granata         " special symbols only allowed as part of a variable:\n"
8939128ee2fSEnrico Granata         "    %V: show the value of the object by default\n"
8949128ee2fSEnrico Granata         "    %S: show the summary of the object by default\n"
8959128ee2fSEnrico Granata         "    %@: show the runtime-provided object description (for Objective-C, it calls NSPrintForDebugger; for C/C++ it does nothing)\n"
8969128ee2fSEnrico Granata         "    %L: show the location of the object (memory address or a register name)\n"
8979128ee2fSEnrico Granata         "    %#: show the number of children of the object\n"
8989128ee2fSEnrico Granata         "    %T: show the type of the object\n"
8999128ee2fSEnrico Granata         "Another variable that you can use in summary strings is ${svar . This sequence works exactly like ${var, including the fact that ${*svar is an allowed sequence, but uses"
9009128ee2fSEnrico Granata         " the object's synthetic children provider instead of the actual objects. For instance, if you are using STL synthetic children providers, the following summary string would"
9019128ee2fSEnrico Granata         " count the number of actual elements stored in an std::list:\n"
9029128ee2fSEnrico Granata         "type summary add -s \"${svar%#}\" -x \"std::list<\"";
9039128ee2fSEnrico Granata }
9049128ee2fSEnrico Granata 
9059128ee2fSEnrico Granata static const char *
9069128ee2fSEnrico Granata ExprPathHelpTextCallback()
9079128ee2fSEnrico Granata {
9089128ee2fSEnrico Granata     return
9099128ee2fSEnrico Granata     "An expression path is the sequence of symbols that is used in C/C++ to access a member variable of an aggregate object (class).\n"
9109128ee2fSEnrico Granata     "For instance, given a class:\n"
9119128ee2fSEnrico Granata     "  class foo {\n"
9129128ee2fSEnrico Granata     "      int a;\n"
9139128ee2fSEnrico Granata     "      int b; .\n"
9149128ee2fSEnrico Granata     "      foo* next;\n"
9159128ee2fSEnrico Granata     "  };\n"
9169128ee2fSEnrico Granata     "the expression to read item b in the item pointed to by next for foo aFoo would be aFoo.next->b.\n"
9179128ee2fSEnrico Granata     "Given that aFoo could just be any object of type foo, the string '.next->b' is the expression path, because it can be attached to any foo instance to achieve the effect.\n"
9189128ee2fSEnrico Granata     "Expression paths in LLDB include dot (.) and arrow (->) operators, and most commands using expression paths have ways to also accept the star (*) operator.\n"
9199128ee2fSEnrico Granata     "The meaning of these operators is the same as the usual one given to them by the C/C++ standards.\n"
9209128ee2fSEnrico Granata     "LLDB also has support for indexing ([ ]) in expression paths, and extends the traditional meaning of the square brackets operator to allow bitfield extraction:\n"
9219128ee2fSEnrico Granata     "for objects of native types (int, float, char, ...) saying '[n-m]' as an expression path (where n and m are any positive integers, e.g. [3-5]) causes LLDB to extract"
9229128ee2fSEnrico Granata     " bits n thru m from the value of the variable. If n == m, [n] is also allowed as a shortcut syntax. For arrays and pointers, expression paths can only contain one index"
9239128ee2fSEnrico Granata     " and the meaning of the operation is the same as the one defined by C/C++ (item extraction). Some commands extend bitfield-like syntax for arrays and pointers with the"
9249128ee2fSEnrico Granata     " meaning of array slicing (taking elements n thru m inside the array or pointed-to memory).";
9250a3958e0SEnrico Granata }
9260a3958e0SEnrico Granata 
927184d7a72SJohnny Chen void
9289b62d1d5SEnrico Granata CommandObject::GenerateHelpText (CommandReturnObject &result)
9299b62d1d5SEnrico Granata {
9309b62d1d5SEnrico Granata     GenerateHelpText(result.GetOutputStream());
9319b62d1d5SEnrico Granata 
9329b62d1d5SEnrico Granata     result.SetStatus (eReturnStatusSuccessFinishNoResult);
9339b62d1d5SEnrico Granata }
9349b62d1d5SEnrico Granata 
9359b62d1d5SEnrico Granata void
9369b62d1d5SEnrico Granata CommandObject::GenerateHelpText (Stream &output_strm)
9379b62d1d5SEnrico Granata {
9389b62d1d5SEnrico Granata     CommandInterpreter& interpreter = GetCommandInterpreter();
939d78c9576SEd Maste     if (GetOptions() != nullptr)
9409b62d1d5SEnrico Granata     {
9419b62d1d5SEnrico Granata         if (WantsRawCommandString())
9429b62d1d5SEnrico Granata         {
9439b62d1d5SEnrico Granata             std::string help_text (GetHelp());
9449b62d1d5SEnrico Granata             help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
9459b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
9469b62d1d5SEnrico Granata         }
9479b62d1d5SEnrico Granata         else
9489b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
9499b62d1d5SEnrico Granata         output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
9509b62d1d5SEnrico Granata         GetOptions()->GenerateOptionUsage (output_strm, this);
9519b62d1d5SEnrico Granata         const char *long_help = GetHelpLong();
952d78c9576SEd Maste         if ((long_help != nullptr)
9539b62d1d5SEnrico Granata             && (strlen (long_help) > 0))
9549b62d1d5SEnrico Granata             output_strm.Printf ("\n%s", long_help);
9559b62d1d5SEnrico Granata         if (WantsRawCommandString() && !WantsCompletion())
9569b62d1d5SEnrico Granata         {
9579b62d1d5SEnrico Granata             // Emit the message about using ' -- ' between the end of the command options and the raw input
9589b62d1d5SEnrico Granata             // conditionally, i.e., only if the command object does not want completion.
9599b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "",
9609b62d1d5SEnrico Granata                                                  "\nIMPORTANT NOTE:  Because this command takes 'raw' input, if you use any command options"
9619b62d1d5SEnrico Granata                                                  " you must use ' -- ' between the end of the command options and the beginning of the raw input.", 1);
9629b62d1d5SEnrico Granata         }
9639b62d1d5SEnrico Granata         else if (GetNumArgumentEntries() > 0
9649b62d1d5SEnrico Granata                  && GetOptions()
9659b62d1d5SEnrico Granata                  && GetOptions()->NumCommandOptions() > 0)
9669b62d1d5SEnrico Granata         {
9679b62d1d5SEnrico Granata             // Also emit a warning about using "--" in case you are using a command that takes options and arguments.
9689b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "",
9699b62d1d5SEnrico Granata                                                  "\nThis command takes options and free-form arguments.  If your arguments resemble"
9709b62d1d5SEnrico Granata                                                  " option specifiers (i.e., they start with a - or --), you must use ' -- ' between"
9719b62d1d5SEnrico Granata                                                  " the end of the command options and the beginning of the arguments.", 1);
9729b62d1d5SEnrico Granata         }
9739b62d1d5SEnrico Granata     }
9749b62d1d5SEnrico Granata     else if (IsMultiwordObject())
9759b62d1d5SEnrico Granata     {
9769b62d1d5SEnrico Granata         if (WantsRawCommandString())
9779b62d1d5SEnrico Granata         {
9789b62d1d5SEnrico Granata             std::string help_text (GetHelp());
9799b62d1d5SEnrico Granata             help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
9809b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
9819b62d1d5SEnrico Granata         }
9829b62d1d5SEnrico Granata         else
9839b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
9849b62d1d5SEnrico Granata         GenerateHelpText (output_strm);
9859b62d1d5SEnrico Granata     }
9869b62d1d5SEnrico Granata     else
9879b62d1d5SEnrico Granata     {
9889b62d1d5SEnrico Granata         const char *long_help = GetHelpLong();
989d78c9576SEd Maste         if ((long_help != nullptr)
9909b62d1d5SEnrico Granata             && (strlen (long_help) > 0))
9919b62d1d5SEnrico Granata             output_strm.Printf ("%s", long_help);
9929b62d1d5SEnrico Granata         else if (WantsRawCommandString())
9939b62d1d5SEnrico Granata         {
9949b62d1d5SEnrico Granata             std::string help_text (GetHelp());
9959b62d1d5SEnrico Granata             help_text.append ("  This command takes 'raw' input (no need to quote stuff).");
9969b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "", help_text.c_str(), 1);
9979b62d1d5SEnrico Granata         }
9989b62d1d5SEnrico Granata         else
9999b62d1d5SEnrico Granata             interpreter.OutputFormattedHelpText (output_strm, "", "", GetHelp(), 1);
10009b62d1d5SEnrico Granata         output_strm.Printf ("\nSyntax: %s\n", GetSyntax());
10019b62d1d5SEnrico Granata     }
10029b62d1d5SEnrico Granata }
10039b62d1d5SEnrico Granata 
10049b62d1d5SEnrico Granata void
1005de753464SJohnny Chen CommandObject::AddIDsArgumentData(CommandArgumentEntry &arg, CommandArgumentType ID, CommandArgumentType IDRange)
1006184d7a72SJohnny Chen {
1007184d7a72SJohnny Chen     CommandArgumentData id_arg;
1008184d7a72SJohnny Chen     CommandArgumentData id_range_arg;
1009184d7a72SJohnny Chen 
1010184d7a72SJohnny Chen     // Create the first variant for the first (and only) argument for this command.
1011de753464SJohnny Chen     id_arg.arg_type = ID;
1012184d7a72SJohnny Chen     id_arg.arg_repetition = eArgRepeatOptional;
1013184d7a72SJohnny Chen 
1014184d7a72SJohnny Chen     // Create the second variant for the first (and only) argument for this command.
1015de753464SJohnny Chen     id_range_arg.arg_type = IDRange;
1016184d7a72SJohnny Chen     id_range_arg.arg_repetition = eArgRepeatOptional;
1017184d7a72SJohnny Chen 
1018a3234732SJohnny Chen     // The first (and only) argument for this command could be either an id or an id_range.
1019184d7a72SJohnny Chen     // Push both variants into the entry for the first argument for this command.
1020184d7a72SJohnny Chen     arg.push_back(id_arg);
1021184d7a72SJohnny Chen     arg.push_back(id_range_arg);
1022184d7a72SJohnny Chen }
1023184d7a72SJohnny Chen 
10249d0402b1SGreg Clayton const char *
10259d0402b1SGreg Clayton CommandObject::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
10269d0402b1SGreg Clayton {
102748b475cbSZachary Turner     assert(arg_type < eArgTypeLastArg && "Invalid argument type passed to GetArgumentTypeAsCString");
10289d0402b1SGreg Clayton     return g_arguments_data[arg_type].arg_name;
10299d0402b1SGreg Clayton }
10309d0402b1SGreg Clayton 
10319d0402b1SGreg Clayton const char *
10329d0402b1SGreg Clayton CommandObject::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
10339d0402b1SGreg Clayton {
103448b475cbSZachary Turner     assert(arg_type < eArgTypeLastArg && "Invalid argument type passed to GetArgumentDescriptionAsCString");
10359d0402b1SGreg Clayton     return g_arguments_data[arg_type].help_text;
10369d0402b1SGreg Clayton }
10379d0402b1SGreg Clayton 
1038893c932aSJim Ingham Target *
1039893c932aSJim Ingham CommandObject::GetDummyTarget()
1040893c932aSJim Ingham {
1041893c932aSJim Ingham     return m_interpreter.GetDebugger().GetDummyTarget();
1042893c932aSJim Ingham }
1043893c932aSJim Ingham 
1044893c932aSJim Ingham Target *
104533df7cd3SJim Ingham CommandObject::GetSelectedOrDummyTarget(bool prefer_dummy)
1046893c932aSJim Ingham {
104733df7cd3SJim Ingham     return m_interpreter.GetDebugger().GetSelectedOrDummyTarget(prefer_dummy);
1048893c932aSJim Ingham }
1049893c932aSJim Ingham 
10505a988416SJim Ingham bool
10515a988416SJim Ingham CommandObjectParsed::Execute (const char *args_string, CommandReturnObject &result)
10525a988416SJim Ingham {
10535a988416SJim Ingham     bool handled = false;
10545a988416SJim Ingham     Args cmd_args (args_string);
10553b652621SJim Ingham     if (HasOverrideCallback())
10565a988416SJim Ingham     {
10575a988416SJim Ingham         Args full_args (GetCommandName ());
10585a988416SJim Ingham         full_args.AppendArguments(cmd_args);
10593b652621SJim Ingham         handled = InvokeOverrideCallback (full_args.GetConstArgumentVector(), result);
10605a988416SJim Ingham     }
10615a988416SJim Ingham     if (!handled)
10625a988416SJim Ingham     {
10635a988416SJim Ingham         for (size_t i = 0; i < cmd_args.GetArgumentCount();  ++i)
10645a988416SJim Ingham         {
10655a988416SJim Ingham             const char *tmp_str = cmd_args.GetArgumentAtIndex (i);
10665a988416SJim Ingham             if (tmp_str[0] == '`')  // back-quote
10675a988416SJim Ingham                 cmd_args.ReplaceArgumentAtIndex (i, m_interpreter.ProcessEmbeddedScriptCommands (tmp_str));
10685a988416SJim Ingham         }
10695a988416SJim Ingham 
1070f9fc609fSGreg Clayton         if (CheckRequirements(result))
1071f9fc609fSGreg Clayton         {
1072f9fc609fSGreg Clayton             if (ParseOptions (cmd_args, result))
1073f9fc609fSGreg Clayton             {
10745a988416SJim Ingham                 // Call the command-specific version of 'Execute', passing it the already processed arguments.
10755a988416SJim Ingham                 handled = DoExecute (cmd_args, result);
10765a988416SJim Ingham             }
1077f9fc609fSGreg Clayton         }
1078f9fc609fSGreg Clayton 
1079f9fc609fSGreg Clayton         Cleanup();
1080f9fc609fSGreg Clayton     }
10815a988416SJim Ingham     return handled;
10825a988416SJim Ingham }
10835a988416SJim Ingham 
10845a988416SJim Ingham bool
10855a988416SJim Ingham CommandObjectRaw::Execute (const char *args_string, CommandReturnObject &result)
10865a988416SJim Ingham {
10875a988416SJim Ingham     bool handled = false;
10883b652621SJim Ingham     if (HasOverrideCallback())
10895a988416SJim Ingham     {
10905a988416SJim Ingham         std::string full_command (GetCommandName ());
10915a988416SJim Ingham         full_command += ' ';
10925a988416SJim Ingham         full_command += args_string;
1093d78c9576SEd Maste         const char *argv[2] = { nullptr, nullptr };
10945a988416SJim Ingham         argv[0] = full_command.c_str();
10953b652621SJim Ingham         handled = InvokeOverrideCallback (argv, result);
10965a988416SJim Ingham     }
10975a988416SJim Ingham     if (!handled)
10985a988416SJim Ingham     {
1099f9fc609fSGreg Clayton         if (CheckRequirements(result))
11005a988416SJim Ingham             handled = DoExecute (args_string, result);
1101f9fc609fSGreg Clayton 
1102f9fc609fSGreg Clayton         Cleanup();
11035a988416SJim Ingham     }
11045a988416SJim Ingham     return handled;
11055a988416SJim Ingham }
11065a988416SJim Ingham 
1107ca7835c6SJohnny Chen static
1108ca7835c6SJohnny Chen const char *arch_helper()
1109ca7835c6SJohnny Chen {
1110d70b14eaSGreg Clayton     static StreamString g_archs_help;
1111797a1b37SJohnny Chen     if (g_archs_help.Empty())
1112d70b14eaSGreg Clayton     {
1113ca7835c6SJohnny Chen         StringList archs;
1114d78c9576SEd Maste         ArchSpec::AutoComplete(nullptr, archs);
1115d70b14eaSGreg Clayton         g_archs_help.Printf("These are the supported architecture names:\n");
1116797a1b37SJohnny Chen         archs.Join("\n", g_archs_help);
1117d70b14eaSGreg Clayton     }
1118d70b14eaSGreg Clayton     return g_archs_help.GetData();
1119ca7835c6SJohnny Chen }
1120ca7835c6SJohnny Chen 
1121e139cf23SCaroline Tice CommandObject::ArgumentTableEntry
1122e139cf23SCaroline Tice CommandObject::g_arguments_data[] =
1123e139cf23SCaroline Tice {
1124d78c9576SEd Maste     { eArgTypeAddress, "address", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid address in the target program's execution space." },
1125d78c9576SEd Maste     { eArgTypeAddressOrExpression, "address-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "An expression that resolves to an address." },
1126d78c9576SEd Maste     { eArgTypeAliasName, "alias-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of an abbreviation (alias) for a debugger command." },
1127d78c9576SEd Maste     { eArgTypeAliasOptions, "options-for-aliased-command", CommandCompletions::eNoCompletion, { nullptr, false }, "Command options to be used as part of an alias (abbreviation) definition.  (See 'help commands alias' for more information.)" },
1128ca7835c6SJohnny Chen     { eArgTypeArchitecture, "arch", CommandCompletions::eArchitectureCompletion, { arch_helper, true }, "The architecture name, e.g. i386 or x86_64." },
1129d78c9576SEd Maste     { eArgTypeBoolean, "boolean", CommandCompletions::eNoCompletion, { nullptr, false }, "A Boolean value: 'true' or 'false'" },
1130d78c9576SEd Maste     { eArgTypeBreakpointID, "breakpt-id", CommandCompletions::eNoCompletion, { BreakpointIDHelpTextCallback, false }, nullptr },
1131d78c9576SEd Maste     { eArgTypeBreakpointIDRange, "breakpt-id-list", CommandCompletions::eNoCompletion, { BreakpointIDRangeHelpTextCallback, false }, nullptr },
11325e09c8c3SJim Ingham     { eArgTypeBreakpointName, "breakpoint-name", CommandCompletions::eNoCompletion, { BreakpointNameHelpTextCallback, false }, nullptr },
1133d78c9576SEd Maste     { eArgTypeByteSize, "byte-size", CommandCompletions::eNoCompletion, { nullptr, false }, "Number of bytes to use." },
1134d78c9576SEd Maste     { eArgTypeClassName, "class-name", CommandCompletions::eNoCompletion, { nullptr, false }, "Then name of a class from the debug information in the program." },
1135d78c9576SEd Maste     { eArgTypeCommandName, "cmd-name", CommandCompletions::eNoCompletion, { nullptr, false }, "A debugger command (may be multiple words), without any options or arguments." },
1136d78c9576SEd Maste     { eArgTypeCount, "count", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1137d78c9576SEd Maste     { eArgTypeDirectoryName, "directory", CommandCompletions::eDiskDirectoryCompletion, { nullptr, false }, "A directory name." },
1138d78c9576SEd Maste     { eArgTypeDisassemblyFlavor, "disassembly-flavor", CommandCompletions::eNoCompletion, { nullptr, false }, "A disassembly flavor recognized by your disassembly plugin.  Currently the only valid options are \"att\" and \"intel\" for Intel targets" },
1139d78c9576SEd Maste     { eArgTypeDescriptionVerbosity, "description-verbosity", CommandCompletions::eNoCompletion, { nullptr, false }, "How verbose the output of 'po' should be." },
1140d78c9576SEd Maste     { eArgTypeEndAddress, "end-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1141d78c9576SEd Maste     { eArgTypeExpression, "expr", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1142d78c9576SEd Maste     { eArgTypeExpressionPath, "expr-path", CommandCompletions::eNoCompletion, { ExprPathHelpTextCallback, true }, nullptr },
1143d78c9576SEd Maste     { eArgTypeExprFormat, "expression-format", CommandCompletions::eNoCompletion, { nullptr, false }, "[ [bool|b] | [bin] | [char|c] | [oct|o] | [dec|i|d|u] | [hex|x] | [float|f] | [cstr|s] ]" },
1144d78c9576SEd Maste     { eArgTypeFilename, "filename", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "The name of a file (can include path)." },
1145d78c9576SEd Maste     { eArgTypeFormat, "format", CommandCompletions::eNoCompletion, { FormatHelpTextCallback, true }, nullptr },
1146d78c9576SEd Maste     { eArgTypeFrameIndex, "frame-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into a thread's list of frames." },
1147d78c9576SEd Maste     { eArgTypeFullName, "fullname", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1148d78c9576SEd Maste     { eArgTypeFunctionName, "function-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function." },
1149d78c9576SEd Maste     { eArgTypeFunctionOrSymbol, "function-or-symbol", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a function or symbol." },
1150d78c9576SEd Maste     { eArgTypeGDBFormat, "gdb-format", CommandCompletions::eNoCompletion, { GDBFormatHelpTextCallback, true }, nullptr },
1151735152e3SEnrico Granata     { eArgTypeHelpText, "help-text", CommandCompletions::eNoCompletion, { nullptr, false }, "Text to be used as help for some other entity in LLDB" },
1152d78c9576SEd Maste     { eArgTypeIndex, "index", CommandCompletions::eNoCompletion, { nullptr, false }, "An index into a list." },
1153d78c9576SEd Maste     { eArgTypeLanguage, "language", CommandCompletions::eNoCompletion, { LanguageTypeHelpTextCallback, true }, nullptr },
1154d78c9576SEd Maste     { eArgTypeLineNum, "linenum", CommandCompletions::eNoCompletion, { nullptr, false }, "Line number in a source file." },
1155d78c9576SEd Maste     { eArgTypeLogCategory, "log-category", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a category within a log channel, e.g. all (try \"log list\" to see a list of all channels and their categories." },
1156d78c9576SEd Maste     { eArgTypeLogChannel, "log-channel", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a log channel, e.g. process.gdb-remote (try \"log list\" to see a list of all channels and their categories)." },
1157d78c9576SEd Maste     { eArgTypeMethod, "method", CommandCompletions::eNoCompletion, { nullptr, false }, "A C++ method name." },
1158d78c9576SEd Maste     { eArgTypeName, "name", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1159d78c9576SEd Maste     { eArgTypeNewPathPrefix, "new-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1160d78c9576SEd Maste     { eArgTypeNumLines, "num-lines", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of lines to use." },
1161d78c9576SEd Maste     { eArgTypeNumberPerLine, "number-per-line", CommandCompletions::eNoCompletion, { nullptr, false }, "The number of items per line to display." },
1162d78c9576SEd Maste     { eArgTypeOffset, "offset", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1163d78c9576SEd Maste     { eArgTypeOldPathPrefix, "old-path-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1164d78c9576SEd Maste     { eArgTypeOneLiner, "one-line-command", CommandCompletions::eNoCompletion, { nullptr, false }, "A command that is entered as a single line of text." },
1165d78c9576SEd Maste     { eArgTypePath, "path", CommandCompletions::eDiskFileCompletion, { nullptr, false }, "Path." },
1166d78c9576SEd Maste     { eArgTypePermissionsNumber, "perms-numeric", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as an octal number (e.g. 755)." },
1167d78c9576SEd Maste     { eArgTypePermissionsString, "perms=string", CommandCompletions::eNoCompletion, { nullptr, false }, "Permissions given as a string value (e.g. rw-r-xr--)." },
1168d78c9576SEd Maste     { eArgTypePid, "pid", CommandCompletions::eNoCompletion, { nullptr, false }, "The process ID number." },
1169d78c9576SEd Maste     { eArgTypePlugin, "plugin", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1170d78c9576SEd Maste     { eArgTypeProcessName, "process-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the process." },
1171d78c9576SEd Maste     { eArgTypePythonClass, "python-class", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python class." },
1172d78c9576SEd Maste     { eArgTypePythonFunction, "python-function", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a Python function." },
1173d78c9576SEd Maste     { eArgTypePythonScript, "python-script", CommandCompletions::eNoCompletion, { nullptr, false }, "Source code written in Python." },
1174d78c9576SEd Maste     { eArgTypeQueueName, "queue-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of the thread queue." },
1175d78c9576SEd Maste     { eArgTypeRegisterName, "register-name", CommandCompletions::eNoCompletion, { RegisterNameHelpTextCallback, true }, nullptr },
1176d78c9576SEd Maste     { eArgTypeRegularExpression, "regular-expression", CommandCompletions::eNoCompletion, { nullptr, false }, "A regular expression." },
1177d78c9576SEd Maste     { eArgTypeRunArgs, "run-args", CommandCompletions::eNoCompletion, { nullptr, false }, "Arguments to be passed to the target program when it starts executing." },
1178d78c9576SEd Maste     { eArgTypeRunMode, "run-mode", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1179d78c9576SEd Maste     { eArgTypeScriptedCommandSynchronicity, "script-cmd-synchronicity", CommandCompletions::eNoCompletion, { nullptr, false }, "The synchronicity to use to run scripted commands with regard to LLDB event system." },
1180d78c9576SEd Maste     { eArgTypeScriptLang, "script-language", CommandCompletions::eNoCompletion, { nullptr, false }, "The scripting language to be used for script-based commands.  Currently only Python is valid." },
1181d78c9576SEd Maste     { eArgTypeSearchWord, "search-word", CommandCompletions::eNoCompletion, { nullptr, false }, "The word for which you wish to search for information about." },
1182d78c9576SEd Maste     { eArgTypeSelector, "selector", CommandCompletions::eNoCompletion, { nullptr, false }, "An Objective-C selector name." },
1183d78c9576SEd Maste     { eArgTypeSettingIndex, "setting-index", CommandCompletions::eNoCompletion, { nullptr, false }, "An index into a settings variable that is an array (try 'settings list' to see all the possible settings variables and their types)." },
1184d78c9576SEd Maste     { eArgTypeSettingKey, "setting-key", CommandCompletions::eNoCompletion, { nullptr, false }, "A key into a settings variables that is a dictionary (try 'settings list' to see all the possible settings variables and their types)." },
1185d78c9576SEd Maste     { eArgTypeSettingPrefix, "setting-prefix", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a settable internal debugger variable up to a dot ('.'), e.g. 'target.process.'" },
1186d78c9576SEd Maste     { eArgTypeSettingVariableName, "setting-variable-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a settable internal debugger variable.  Type 'settings list' to see a complete list of such variables." },
1187d78c9576SEd Maste     { eArgTypeShlibName, "shlib-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a shared library." },
1188d78c9576SEd Maste     { eArgTypeSourceFile, "source-file", CommandCompletions::eSourceFileCompletion, { nullptr, false }, "The name of a source file.." },
1189d78c9576SEd Maste     { eArgTypeSortOrder, "sort-order", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify a sort order when dumping lists." },
1190d78c9576SEd Maste     { eArgTypeStartAddress, "start-address", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1191d78c9576SEd Maste     { eArgTypeSummaryString, "summary-string", CommandCompletions::eNoCompletion, { SummaryStringHelpTextCallback, true }, nullptr },
1192d78c9576SEd Maste     { eArgTypeSymbol, "symbol", CommandCompletions::eSymbolCompletion, { nullptr, false }, "Any symbol name (function name, variable, argument, etc.)" },
1193d78c9576SEd Maste     { eArgTypeThreadID, "thread-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Thread ID number." },
1194d78c9576SEd Maste     { eArgTypeThreadIndex, "thread-index", CommandCompletions::eNoCompletion, { nullptr, false }, "Index into the process' list of threads." },
1195d78c9576SEd Maste     { eArgTypeThreadName, "thread-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The thread's name." },
1196a72b31c7SJim Ingham     { eArgTypeTypeName, "type-name", CommandCompletions::eNoCompletion, { nullptr, false }, "A type name." },
1197d78c9576SEd Maste     { eArgTypeUnsignedInteger, "unsigned-integer", CommandCompletions::eNoCompletion, { nullptr, false }, "An unsigned integer." },
1198d78c9576SEd Maste     { eArgTypeUnixSignal, "unix-signal", CommandCompletions::eNoCompletion, { nullptr, false }, "A valid Unix signal name or number (e.g. SIGKILL, KILL or 9)." },
1199d78c9576SEd Maste     { eArgTypeVarName, "variable-name", CommandCompletions::eNoCompletion, { nullptr, false }, "The name of a variable in your program." },
1200d78c9576SEd Maste     { eArgTypeValue, "value", CommandCompletions::eNoCompletion, { nullptr, false }, "A value could be anything, depending on where and how it is used." },
1201d78c9576SEd Maste     { eArgTypeWidth, "width", CommandCompletions::eNoCompletion, { nullptr, false }, "Help text goes here." },
1202d78c9576SEd Maste     { eArgTypeNone, "none", CommandCompletions::eNoCompletion, { nullptr, false }, "No help available for this." },
1203d78c9576SEd Maste     { eArgTypePlatform, "platform-name", CommandCompletions::ePlatformPluginCompletion, { nullptr, false }, "The name of an installed platform plug-in . Type 'platform list' to see a complete list of installed platforms." },
1204d78c9576SEd Maste     { eArgTypeWatchpointID, "watchpt-id", CommandCompletions::eNoCompletion, { nullptr, false }, "Watchpoint IDs are positive integers." },
1205d78c9576SEd Maste     { eArgTypeWatchpointIDRange, "watchpt-id-list", CommandCompletions::eNoCompletion, { nullptr, false }, "For example, '1-3' or '1 to 3'." },
1206d78c9576SEd Maste     { eArgTypeWatchType, "watch-type", CommandCompletions::eNoCompletion, { nullptr, false }, "Specify the type for a watchpoint." }
1207e139cf23SCaroline Tice };
1208e139cf23SCaroline Tice 
1209e139cf23SCaroline Tice const CommandObject::ArgumentTableEntry*
1210e139cf23SCaroline Tice CommandObject::GetArgumentTable ()
1211e139cf23SCaroline Tice {
12129d0402b1SGreg Clayton     // If this assertion fires, then the table above is out of date with the CommandArgumentType enumeration
12139d0402b1SGreg Clayton     assert ((sizeof (CommandObject::g_arguments_data) / sizeof (CommandObject::ArgumentTableEntry)) == eArgTypeLastArg);
1214e139cf23SCaroline Tice     return CommandObject::g_arguments_data;
1215e139cf23SCaroline Tice }
1216e139cf23SCaroline Tice 
1217e139cf23SCaroline Tice 
1218