blob: 0784b4a9f78724c4d7e9e7467e5701365f95df42 [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
Greg Clayton238c0a12010-09-18 01:14:36 +000041CommandObject::CommandObject
42(
43 CommandInterpreter &interpreter,
44 const char *name,
45 const char *help,
46 const char *syntax,
47 uint32_t flags
48) :
49 m_interpreter (interpreter),
Chris Lattner24943d22010-06-08 16:52:24 +000050 m_cmd_name (name),
51 m_cmd_help_short (),
52 m_cmd_help_long (),
53 m_cmd_syntax (),
Jim Inghamd40f8a62010-07-06 22:46:59 +000054 m_is_alias (false),
Chris Lattner24943d22010-06-08 16:52:24 +000055 m_flags (flags)
56{
57 if (help && help[0])
58 m_cmd_help_short = help;
59 if (syntax && syntax[0])
60 m_cmd_syntax = syntax;
61}
62
63CommandObject::~CommandObject ()
64{
65}
66
67const char *
68CommandObject::GetHelp ()
69{
70 return m_cmd_help_short.c_str();
71}
72
73const char *
74CommandObject::GetHelpLong ()
75{
76 return m_cmd_help_long.c_str();
77}
78
79const char *
80CommandObject::GetSyntax ()
81{
82 return m_cmd_syntax.c_str();
83}
84
85const char *
86CommandObject::Translate ()
87{
88 //return m_cmd_func_name.c_str();
89 return "This function is currently not implemented.";
90}
91
92const char *
93CommandObject::GetCommandName ()
94{
95 return m_cmd_name.c_str();
96}
97
98void
99CommandObject::SetCommandName (const char *name)
100{
101 m_cmd_name = name;
102}
103
104void
105CommandObject::SetHelp (const char *cstr)
106{
107 m_cmd_help_short = cstr;
108}
109
110void
111CommandObject::SetHelpLong (const char *cstr)
112{
113 m_cmd_help_long = cstr;
114}
115
116void
117CommandObject::SetSyntax (const char *cstr)
118{
119 m_cmd_syntax = cstr;
120}
121
122Options *
123CommandObject::GetOptions ()
124{
125 // By default commands don't have options unless this virtual function
126 // is overridden by base classes.
127 return NULL;
128}
129
130Flags&
131CommandObject::GetFlags()
132{
133 return m_flags;
134}
135
136const Flags&
137CommandObject::GetFlags() const
138{
139 return m_flags;
140}
141
142bool
143CommandObject::ExecuteCommandString
144(
145 const char *command_line,
Chris Lattner24943d22010-06-08 16:52:24 +0000146 CommandReturnObject &result
147)
148{
149 Args command_args(command_line);
Greg Clayton238c0a12010-09-18 01:14:36 +0000150 return ExecuteWithOptions (command_args, result);
Chris Lattner24943d22010-06-08 16:52:24 +0000151}
152
153bool
154CommandObject::ParseOptions
155(
156 Args& args,
Chris Lattner24943d22010-06-08 16:52:24 +0000157 CommandReturnObject &result
158)
159{
160 // See if the subclass has options?
161 Options *options = GetOptions();
162 if (options != NULL)
163 {
164 Error error;
165 options->ResetOptionValues();
166
167 // ParseOptions calls getopt_long, which always skips the zero'th item in the array and starts at position 1,
168 // so we need to push a dummy value into position zero.
169 args.Unshift("dummy_string");
170 error = args.ParseOptions (*options);
171
172 // The "dummy_string" will have already been removed by ParseOptions,
173 // so no need to remove it.
174
175 if (error.Fail() || !options->VerifyOptions (result))
176 {
177 const char *error_cstr = error.AsCString();
178 if (error_cstr)
179 {
180 // We got an error string, lets use that
181 result.GetErrorStream().PutCString(error_cstr);
182 }
183 else
184 {
185 // No error string, output the usage information into result
Greg Clayton238c0a12010-09-18 01:14:36 +0000186 options->GenerateOptionUsage (m_interpreter, result.GetErrorStream(), this);
Chris Lattner24943d22010-06-08 16:52:24 +0000187 }
188 // Set the return status to failed (this was an error).
189 result.SetStatus (eReturnStatusFailed);
190 return false;
191 }
192 }
193 return true;
194}
195bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000196CommandObject::ExecuteWithOptions (Args& args, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000197{
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 Clayton238c0a12010-09-18 01:14:36 +0000202 args.ReplaceArgumentAtIndex (i, m_interpreter.ProcessEmbeddedScriptCommands (tmp_str));
Chris Lattner24943d22010-06-08 16:52:24 +0000203 }
204
Greg Clayton238c0a12010-09-18 01:14:36 +0000205 Process *process = m_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 Clayton238c0a12010-09-18 01:14:36 +0000251 if (!ParseOptions (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 Clayton238c0a12010-09-18 01:14:36 +0000255 return Execute (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(
303 Args &input,
304 int &cursor_index,
305 int &cursor_char_position,
306 int match_start_point,
307 int max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000308 bool &word_complete,
Chris Lattner24943d22010-06-08 16:52:24 +0000309 StringList &matches
310)
311{
312 if (WantsRawCommandString())
313 {
314 // FIXME: Abstract telling the completion to insert the completion character.
315 matches.Clear();
316 return -1;
317 }
318 else
319 {
320 // Can we do anything generic with the options?
321 Options *cur_options = GetOptions();
322 CommandReturnObject result;
323 OptionElementVector opt_element_vector;
324
325 if (cur_options != NULL)
326 {
327 // Re-insert the dummy command name string which will have been
328 // stripped off:
329 input.Unshift ("dummy-string");
330 cursor_index++;
331
332
333 // I stick an element on the end of the input, because if the last element is
334 // option that requires an argument, getopt_long will freak out.
335
336 input.AppendArgument ("<FAKE-VALUE>");
337
Jim Inghamadb84292010-06-24 20:31:04 +0000338 input.ParseArgsForCompletion (*cur_options, opt_element_vector, cursor_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000339
340 input.DeleteArgumentAtIndex(input.GetArgumentCount() - 1);
341
342 bool handled_by_options;
Greg Clayton238c0a12010-09-18 01:14:36 +0000343 handled_by_options = cur_options->HandleOptionCompletion (m_interpreter,
Greg Clayton63094e02010-06-23 01:19:29 +0000344 input,
345 opt_element_vector,
346 cursor_index,
347 cursor_char_position,
348 match_start_point,
349 max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000350 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000351 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000352 if (handled_by_options)
353 return matches.GetSize();
354 }
355
356 // If we got here, the last word is not an option or an option argument.
Greg Clayton238c0a12010-09-18 01:14:36 +0000357 return HandleArgumentCompletion (input,
Greg Clayton63094e02010-06-23 01:19:29 +0000358 cursor_index,
359 cursor_char_position,
360 opt_element_vector,
361 match_start_point,
362 max_return_elements,
Jim Ingham802f8b02010-06-30 05:02:46 +0000363 word_complete,
Greg Clayton63094e02010-06-23 01:19:29 +0000364 matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000365 }
366}
367
Chris Lattner24943d22010-06-08 16:52:24 +0000368// Case insensitive version of ::strstr()
369// Returns true if s2 is contained within s1.
370
371static bool
372contains_string (const char *s1, const char *s2)
373{
374 char *locase_s1 = (char *) malloc (strlen (s1) + 1);
375 char *locase_s2 = (char *) malloc (strlen (s2) + 1);
376 int i;
377 for (i = 0; s1 && s1[i] != '\0'; i++)
378 locase_s1[i] = ::tolower (s1[i]);
379 locase_s1[i] = '\0';
380 for (i = 0; s2 && s2[i] != '\0'; i++)
381 locase_s2[i] = ::tolower (s2[i]);
382 locase_s2[i] = '\0';
383
384 const char *result = ::strstr (locase_s1, locase_s2);
385 free (locase_s1);
386 free (locase_s2);
387 // 'result' points into freed memory - but we're not
388 // deref'ing it so hopefully current/future compilers
389 // won't complain..
390
391 if (result == NULL)
392 return false;
393 else
394 return true;
395}
396
397bool
Greg Clayton238c0a12010-09-18 01:14:36 +0000398CommandObject::HelpTextContainsWord (const char *search_word)
Chris Lattner24943d22010-06-08 16:52:24 +0000399{
400 const char *short_help;
401 const char *long_help;
402 const char *syntax_help;
403 std::string options_usage_help;
404
405
406 bool found_word = false;
407
408 short_help = GetHelp();
409 long_help = GetHelpLong();
410 syntax_help = GetSyntax();
411
412 if (contains_string (short_help, search_word))
413 found_word = true;
414 else if (contains_string (long_help, search_word))
415 found_word = true;
416 else if (contains_string (syntax_help, search_word))
417 found_word = true;
418
419 if (!found_word
420 && GetOptions() != NULL)
421 {
422 StreamString usage_help;
Greg Clayton238c0a12010-09-18 01:14:36 +0000423 GetOptions()->GenerateOptionUsage (m_interpreter, usage_help, this);
Chris Lattner24943d22010-06-08 16:52:24 +0000424 if (usage_help.GetSize() > 0)
425 {
426 const char *usage_text = usage_help.GetData();
427 if (contains_string (usage_text, search_word))
428 found_word = true;
429 }
430 }
431
432 return found_word;
433}