blob: 571c3d49f1401596b70df0e0d01f0c0ac80cbf70 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObject.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#include "lldb/Interpreter/CommandObject.h"
11
12#include <string>
13#include <map>
14
15#include <getopt.h>
16#include <stdlib.h>
17#include <ctype.h>
18
19#include "lldb/Core/Address.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000020#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000021
22// These are for the Sourcename completers.
23// FIXME: Make a separate file for the completers.
24#include "lldb/Core/FileSpec.h"
25#include "lldb/Core/FileSpecList.h"
26#include "lldb/Target/Process.h"
27#include "lldb/Target/Target.h"
28
29#include "lldb/Interpreter/CommandInterpreter.h"
30#include "lldb/Interpreter/CommandReturnObject.h"
31#include "lldb/Interpreter/ScriptInterpreter.h"
32#include "lldb/Interpreter/ScriptInterpreterPython.h"
33
34using namespace lldb;
35using namespace lldb_private;
36
37//-------------------------------------------------------------------------
38// CommandObject
39//-------------------------------------------------------------------------
40
41CommandObject::CommandObject (const char *name, const char *help, const char *syntax, uint32_t flags) :
42 m_cmd_name (name),
43 m_cmd_help_short (),
44 m_cmd_help_long (),
45 m_cmd_syntax (),
Jim Inghamd40f8a62010-07-06 22:46:59 +000046 m_is_alias (false),
Chris Lattner24943d22010-06-08 16:52:24 +000047 m_flags (flags)
48{
49 if (help && help[0])
50 m_cmd_help_short = help;
51 if (syntax && syntax[0])
52 m_cmd_syntax = syntax;
53}
54
55CommandObject::~CommandObject ()
56{
57}
58
59const char *
60CommandObject::GetHelp ()
61{
62 return m_cmd_help_short.c_str();
63}
64
65const char *
66CommandObject::GetHelpLong ()
67{
68 return m_cmd_help_long.c_str();
69}
70
71const char *
72CommandObject::GetSyntax ()
73{
74 return m_cmd_syntax.c_str();
75}
76
77const char *
78CommandObject::Translate ()
79{
80 //return m_cmd_func_name.c_str();
81 return "This function is currently not implemented.";
82}
83
84const char *
85CommandObject::GetCommandName ()
86{
87 return m_cmd_name.c_str();
88}
89
90void
91CommandObject::SetCommandName (const char *name)
92{
93 m_cmd_name = name;
94}
95
96void
97CommandObject::SetHelp (const char *cstr)
98{
99 m_cmd_help_short = cstr;
100}
101
102void
103CommandObject::SetHelpLong (const char *cstr)
104{
105 m_cmd_help_long = cstr;
106}
107
108void
109CommandObject::SetSyntax (const char *cstr)
110{
111 m_cmd_syntax = cstr;
112}
113
114Options *
115CommandObject::GetOptions ()
116{
117 // By default commands don't have options unless this virtual function
118 // is overridden by base classes.
119 return NULL;
120}
121
122Flags&
123CommandObject::GetFlags()
124{
125 return m_flags;
126}
127
128const Flags&
129CommandObject::GetFlags() const
130{
131 return m_flags;
132}
133
134bool
135CommandObject::ExecuteCommandString
136(
Greg Clayton63094e02010-06-23 01:19:29 +0000137 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000138 const char *command_line,
Chris Lattner24943d22010-06-08 16:52:24 +0000139 CommandReturnObject &result
140)
141{
142 Args command_args(command_line);
Greg Clayton63094e02010-06-23 01:19:29 +0000143 return ExecuteWithOptions (interpreter, command_args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000144}
145
146bool
147CommandObject::ParseOptions
148(
Greg Clayton63094e02010-06-23 01:19:29 +0000149 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000150 Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000151 CommandReturnObject &result
152)
153{
154 // See if the subclass has options?
155 Options *options = GetOptions();
156 if (options != NULL)
157 {
158 Error error;
159 options->ResetOptionValues();
160
161 // ParseOptions calls getopt_long, which always skips the zero'th item in the array and starts at position 1,
162 // so we need to push a dummy value into position zero.
163 args.Unshift("dummy_string");
164 error = args.ParseOptions (*options);
165
166 // The "dummy_string" will have already been removed by ParseOptions,
167 // so no need to remove it.
168
169 if (error.Fail() || !options->VerifyOptions (result))
170 {
171 const char *error_cstr = error.AsCString();
172 if (error_cstr)
173 {
174 // We got an error string, lets use that
175 result.GetErrorStream().PutCString(error_cstr);
176 }
177 else
178 {
179 // No error string, output the usage information into result
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000180 options->GenerateOptionUsage (result.GetErrorStream(), this,
181 interpreter.GetDebugger().GetInstanceName().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000182 }
183 // Set the return status to failed (this was an error).
184 result.SetStatus (eReturnStatusFailed);
185 return false;
186 }
187 }
188 return true;
189}
190bool
191CommandObject::ExecuteWithOptions
192(
Greg Clayton63094e02010-06-23 01:19:29 +0000193 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000194 Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000195 CommandReturnObject &result
196)
197{
198 for (size_t i = 0; i < args.GetArgumentCount(); ++i)
199 {
200 const char *tmp_str = args.GetArgumentAtIndex (i);
201 if (tmp_str[0] == '`') // back-quote
Greg Clayton63094e02010-06-23 01:19:29 +0000202 args.ReplaceArgumentAtIndex (i, interpreter.ProcessEmbeddedScriptCommands (tmp_str));
Chris Lattner24943d22010-06-08 16:52:24 +0000203 }
204
Greg Clayton63094e02010-06-23 01:19:29 +0000205 Process *process = interpreter.GetDebugger().GetExecutionContext().process;
Chris Lattner24943d22010-06-08 16:52:24 +0000206 if (process == NULL)
207 {
208 if (GetFlags().IsSet(CommandObject::eFlagProcessMustBeLaunched | CommandObject::eFlagProcessMustBePaused))
209 {
210 result.AppendError ("Process must exist.");
211 result.SetStatus (eReturnStatusFailed);
212 return false;
213 }
214 }
215 else
216 {
217 StateType state = process->GetState();
218
219 switch (state)
220 {
221
222 case eStateAttaching:
223 case eStateLaunching:
224 case eStateSuspended:
225 case eStateCrashed:
226 case eStateStopped:
227 break;
228
229 case eStateDetached:
230 case eStateExited:
231 case eStateUnloaded:
232 if (GetFlags().IsSet(CommandObject::eFlagProcessMustBeLaunched))
233 {
234 result.AppendError ("Process must be launched.");
235 result.SetStatus (eReturnStatusFailed);
236 return false;
237 }
238 break;
239
240 case eStateRunning:
241 case eStateStepping:
242 if (GetFlags().IsSet(CommandObject::eFlagProcessMustBePaused))
243 {
244 result.AppendError ("Process is running. Use 'process interrupt' to pause execution.");
245 result.SetStatus (eReturnStatusFailed);
246 return false;
247 }
248 }
249 }
250
Greg Clayton63094e02010-06-23 01:19:29 +0000251 if (!ParseOptions (interpreter, args, result))
Chris Lattner24943d22010-06-08 16:52:24 +0000252 return false;
253
254 // Call the command-specific version of 'Execute', passing it the already processed arguments.
Greg Clayton63094e02010-06-23 01:19:29 +0000255 return Execute (interpreter, args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000256}
257
258class CommandDictCommandPartialMatch
259{
260 public:
261 CommandDictCommandPartialMatch (const char *match_str)
262 {
263 m_match_str = match_str;
264 }
265 bool operator() (const std::pair<std::string, lldb::CommandObjectSP> map_element) const
266 {
267 // A NULL or empty string matches everything.
268 if (m_match_str == NULL || *m_match_str == '\0')
269 return 1;
270
271 size_t found = map_element.first.find (m_match_str, 0);
272 if (found == std::string::npos)
273 return 0;
274 else
275 return found == 0;
276 }
277
278 private:
279 const char *m_match_str;
280};
281
282int
283CommandObject::AddNamesMatchingPartialString (CommandObject::CommandMap &in_map, const char *cmd_str,
284 StringList &matches)
285{
286 int number_added = 0;
287 CommandDictCommandPartialMatch matcher(cmd_str);
288
289 CommandObject::CommandMap::iterator matching_cmds = std::find_if (in_map.begin(), in_map.end(), matcher);
290
291 while (matching_cmds != in_map.end())
292 {
293 ++number_added;
294 matches.AppendString((*matching_cmds).first.c_str());
295 matching_cmds = std::find_if (++matching_cmds, in_map.end(), matcher);;
296 }
297 return number_added;
298}
299
300int
301CommandObject::HandleCompletion
302(
Greg Clayton63094e02010-06-23 01:19:29 +0000303 CommandInterpreter &interpreter,
Chris Lattner24943d22010-06-08 16:52:24 +0000304 Args &input,
305 int &cursor_index,
306 int &cursor_char_position,
307 int match_start_point,
308 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000309 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000310 StringList &matches
311)
312{
313 if (WantsRawCommandString())
314 {
315 // FIXME: Abstract telling the completion to insert the completion character.
316 matches.Clear();
317 return -1;
318 }
319 else
320 {
321 // Can we do anything generic with the options?
322 Options *cur_options = GetOptions();
323 CommandReturnObject result;
324 OptionElementVector opt_element_vector;
325
326 if (cur_options != NULL)
327 {
328 // Re-insert the dummy command name string which will have been
329 // stripped off:
330 input.Unshift ("dummy-string");
331 cursor_index++;
332
333
334 // I stick an element on the end of the input, because if the last element is
335 // option that requires an argument, getopt_long will freak out.
336
337 input.AppendArgument ("<FAKE-VALUE>");
338
Jim Inghamadb84292010-06-24 20:31:04 +0000339 input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000340
341 input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1);
342
343 bool handled_by_options;
Greg Clayton63094e02010-06-23 01:19:29 +0000344 handled_by_options = cur_options->HandleOptionCompletion (interpreter,
345 input,
346 opt_element_vector,
347 cursor_index,
348 cursor_char_position,
349 match_start_point,
350 max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000351 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000352 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000353 if (handled_by_options)
354 return matches.GetSize();
355 }
356
357 // If we got here, the last word is not an option or an option argument.
Greg Clayton63094e02010-06-23 01:19:29 +0000358 return HandleArgumentCompletion (interpreter,
359 input,
360 cursor_index,
361 cursor_char_position,
362 opt_element_vector,
363 match_start_point,
364 max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000365 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000366 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000367 }
368}
369
Chris Lattner24943d22010-06-08 16:52:24 +0000370// Case insensitive version of ::strstr()
371// Returns true if s2 is contained within s1.
372
373static bool
374contains_string (const char *s1, const char *s2)
375{
376 char *locase_s1 = (char *) malloc (strlen (s1) + 1);
377 char *locase_s2 = (char *) malloc (strlen (s2) + 1);
378 int i;
379 for (i = 0; s1 && s1[i] != '\0'; i++)
380 locase_s1[i] = ::tolower (s1[i]);
381 locase_s1[i] = '\0';
382 for (i = 0; s2 && s2[i] != '\0'; i++)
383 locase_s2[i] = ::tolower (s2[i]);
384 locase_s2[i] = '\0';
385
386 const char *result = ::strstr (locase_s1, locase_s2);
387 free (locase_s1);
388 free (locase_s2);
389 // 'result' points into freed memory - but we're not
390 // deref'ing it so hopefully current/future compilers
391 // won't complain..
392
393 if (result == NULL)
394 return false;
395 else
396 return true;
397}
398
399bool
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000400CommandObject::HelpTextContainsWord (const char *search_word, CommandInterpreter &interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000401{
402 const char *short_help;
403 const char *long_help;
404 const char *syntax_help;
405 std::string options_usage_help;
406
407
408 bool found_word = false;
409
410 short_help = GetHelp();
411 long_help = GetHelpLong();
412 syntax_help = GetSyntax();
413
414 if (contains_string (short_help, search_word))
415 found_word = true;
416 else if (contains_string (long_help, search_word))
417 found_word = true;
418 else if (contains_string (syntax_help, search_word))
419 found_word = true;
420
421 if (!found_word
422 && GetOptions() != NULL)
423 {
424 StreamString usage_help;
Caroline Tice1d2aefd2010-09-09 06:25:08 +0000425 GetOptions()->GenerateOptionUsage (usage_help, this, interpreter.GetDebugger().GetInstanceName().AsCString());
Chris Lattner24943d22010-06-08 16:52:24 +0000426 if (usage_help.GetSize() > 0)
427 {
428 const char *usage_text = usage_help.GetData();
429 if (contains_string (usage_text, search_word))
430 found_word = true;
431 }
432 }
433
434 return found_word;
435}