blob: 053bb401b83525eddb6e9f118827e7a368e5eb95 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- CommandInterpreter.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
Daniel Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner30fdc8d2010-06-08 16:52:24 +000012#include <string>
Caroline Tice4ab31c92010-10-12 21:57:09 +000013#include <vector>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000014#include <stdlib.h>
15
Greg Clayton4a33d312011-06-23 17:59:56 +000016#include "CommandObjectScript.h"
Peter Collingbourne08405b62011-06-23 20:37:26 +000017#include "lldb/Interpreter/CommandObjectRegexCommand.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000018
Eli Friedman3afb70c2010-06-13 02:17:17 +000019#include "../Commands/CommandObjectApropos.h"
20#include "../Commands/CommandObjectArgs.h"
21#include "../Commands/CommandObjectBreakpoint.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000022#include "../Commands/CommandObjectDisassemble.h"
23#include "../Commands/CommandObjectExpression.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000024#include "../Commands/CommandObjectFrame.h"
25#include "../Commands/CommandObjectHelp.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000026#include "../Commands/CommandObjectLog.h"
27#include "../Commands/CommandObjectMemory.h"
Greg Claytonded470d2011-03-19 01:12:21 +000028#include "../Commands/CommandObjectPlatform.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000029#include "../Commands/CommandObjectPlugin.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000030#include "../Commands/CommandObjectProcess.h"
31#include "../Commands/CommandObjectQuit.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000032#include "../Commands/CommandObjectRegister.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000033#include "../Commands/CommandObjectSettings.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000034#include "../Commands/CommandObjectSource.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000035#include "../Commands/CommandObjectCommands.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000036#include "../Commands/CommandObjectSyntax.h"
37#include "../Commands/CommandObjectTarget.h"
38#include "../Commands/CommandObjectThread.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000039#include "../Commands/CommandObjectType.h"
Johnny Chen31c39da2010-12-23 20:21:44 +000040#include "../Commands/CommandObjectVersion.h"
Johnny Chenf04ee932011-09-22 18:04:58 +000041#include "../Commands/CommandObjectWatchpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000042
Greg Clayton7d2ef162013-03-29 17:03:23 +000043
Chris Lattner30fdc8d2010-06-08 16:52:24 +000044#include "lldb/Core/Debugger.h"
Jim Ingham97a6dc72010-10-04 19:49:29 +000045#include "lldb/Core/InputReader.h"
Enrico Granatab5887262012-10-29 21:18:03 +000046#include "lldb/Core/Log.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000047#include "lldb/Core/Stream.h"
48#include "lldb/Core/Timer.h"
Enrico Granatab5887262012-10-29 21:18:03 +000049
Greg Clayton7fb56d02011-02-01 01:31:41 +000050#include "lldb/Host/Host.h"
Enrico Granatab5887262012-10-29 21:18:03 +000051
52#include "lldb/Interpreter/Args.h"
Greg Clayton7d2ef162013-03-29 17:03:23 +000053#include "lldb/Interpreter/CommandCompletions.h"
Enrico Granatab5887262012-10-29 21:18:03 +000054#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton7d2ef162013-03-29 17:03:23 +000055#include "lldb/Interpreter/CommandReturnObject.h"
Enrico Granatab5887262012-10-29 21:18:03 +000056#include "lldb/Interpreter/Options.h"
57#include "lldb/Interpreter/ScriptInterpreterNone.h"
58#include "lldb/Interpreter/ScriptInterpreterPython.h"
59
60
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "lldb/Target/Process.h"
62#include "lldb/Target/Thread.h"
63#include "lldb/Target/TargetList.h"
64
Enrico Granatab5887262012-10-29 21:18:03 +000065#include "lldb/Utility/CleanUp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
67using namespace lldb;
68using namespace lldb_private;
69
Greg Clayton754a9362012-08-23 00:22:02 +000070
71static PropertyDefinition
72g_properties[] =
73{
74 { "expand-regex-aliases", OptionValue::eTypeBoolean, true, false, NULL, NULL, "If true, regular expression alias commands will show the expanded command that will be executed. This can be used to debug new regular expression alias commands." },
Enrico Granatabcba2b22013-01-17 21:36:19 +000075 { "prompt-on-quit", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will prompt you before quitting if there are any live processes being debugged. If false, LLDB will quit without asking in any case." },
Enrico Granata012d4fc2013-06-11 01:26:35 +000076 { "stop-command-source-on-error", OptionValue::eTypeBoolean, true, true, NULL, NULL, "If true, LLDB will stop running a 'command source' script upon encountering an error." },
Greg Clayton754a9362012-08-23 00:22:02 +000077 { NULL , OptionValue::eTypeInvalid, true, 0 , NULL, NULL, NULL }
78};
79
80enum
81{
Enrico Granatabcba2b22013-01-17 21:36:19 +000082 ePropertyExpandRegexAliases = 0,
Enrico Granata012d4fc2013-06-11 01:26:35 +000083 ePropertyPromptOnQuit = 1,
84 ePropertyStopCmdSourceOnError = 2
Greg Clayton754a9362012-08-23 00:22:02 +000085};
86
Jim Ingham4bddaeb2012-02-16 06:50:00 +000087ConstString &
88CommandInterpreter::GetStaticBroadcasterClass ()
89{
90 static ConstString class_name ("lldb.commandInterpreter");
91 return class_name;
92}
93
Chris Lattner30fdc8d2010-06-08 16:52:24 +000094CommandInterpreter::CommandInterpreter
95(
Greg Clayton66111032010-06-23 01:19:29 +000096 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +000097 ScriptLanguage script_language,
Greg Clayton66111032010-06-23 01:19:29 +000098 bool synchronous_execution
Chris Lattner30fdc8d2010-06-08 16:52:24 +000099) :
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000100 Broadcaster (&debugger, "lldb.command-interpreter"),
Greg Clayton754a9362012-08-23 00:22:02 +0000101 Properties(OptionValuePropertiesSP(new OptionValueProperties(ConstString("interpreter")))),
Greg Clayton66111032010-06-23 01:19:29 +0000102 m_debugger (debugger),
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000103 m_synchronous_execution (synchronous_execution),
Caroline Tice2f88aad2011-01-14 00:29:16 +0000104 m_skip_lldbinit_files (false),
Jim Ingham16e0c682011-08-12 23:34:31 +0000105 m_skip_app_init_files (false),
Jim Inghame16c50a2011-02-18 00:54:25 +0000106 m_script_interpreter_ap (),
Caroline Ticed61c10b2011-06-16 16:27:19 +0000107 m_comment_char ('#'),
Johnny Chen4ac1d9e2012-08-09 22:06:10 +0000108 m_batch_command_mode (false),
Enrico Granata5f5ab602012-05-31 01:09:06 +0000109 m_truncation_warning(eNoTruncation),
110 m_command_source_depth (0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111{
Greg Clayton67cc0632012-08-22 17:17:09 +0000112 debugger.SetScriptLanguage (script_language);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000113 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
114 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
Greg Clayton67cc0632012-08-22 17:17:09 +0000115 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000116 CheckInWithManager ();
Greg Clayton754a9362012-08-23 00:22:02 +0000117 m_collection_sp->Initialize (g_properties);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000118}
119
Greg Clayton754a9362012-08-23 00:22:02 +0000120bool
121CommandInterpreter::GetExpandRegexAliases () const
122{
123 const uint32_t idx = ePropertyExpandRegexAliases;
124 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
125}
126
Enrico Granatabcba2b22013-01-17 21:36:19 +0000127bool
128CommandInterpreter::GetPromptOnQuit () const
129{
130 const uint32_t idx = ePropertyPromptOnQuit;
131 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
132}
Greg Clayton754a9362012-08-23 00:22:02 +0000133
Enrico Granata012d4fc2013-06-11 01:26:35 +0000134bool
135CommandInterpreter::GetStopCmdSourceOnError () const
136{
137 const uint32_t idx = ePropertyStopCmdSourceOnError;
138 return m_collection_sp->GetPropertyAtIndexAsBoolean (NULL, idx, g_properties[idx].default_uint_value != 0);
139}
140
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000141void
142CommandInterpreter::Initialize ()
143{
144 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
145
146 CommandReturnObject result;
147
148 LoadCommandDictionary ();
149
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000150 // Set up some initial aliases.
Caroline Ticeca90c472011-05-06 21:37:15 +0000151 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
152 if (cmd_obj_sp)
153 {
154 AddAlias ("q", cmd_obj_sp);
155 AddAlias ("exit", cmd_obj_sp);
156 }
Sean Callanan247e62a2012-05-04 23:15:02 +0000157
Johnny Chen6d675242012-08-24 18:15:45 +0000158 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
Sean Callanan247e62a2012-05-04 23:15:02 +0000159 if (cmd_obj_sp)
160 {
161 AddAlias ("attach", cmd_obj_sp);
162 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000163
Johnny Chen6d675242012-08-24 18:15:45 +0000164 cmd_obj_sp = GetCommandSPExact ("process detach",false);
165 if (cmd_obj_sp)
166 {
167 AddAlias ("detach", cmd_obj_sp);
168 }
169
Caroline Ticeca90c472011-05-06 21:37:15 +0000170 cmd_obj_sp = GetCommandSPExact ("process continue", false);
171 if (cmd_obj_sp)
172 {
173 AddAlias ("c", cmd_obj_sp);
174 AddAlias ("continue", cmd_obj_sp);
175 }
176
177 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
178 if (cmd_obj_sp)
179 AddAlias ("b", cmd_obj_sp);
180
Jim Inghamca36cd12012-10-05 19:16:31 +0000181 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
182 if (cmd_obj_sp)
183 AddAlias ("tbreak", cmd_obj_sp);
184
Caroline Ticeca90c472011-05-06 21:37:15 +0000185 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
186 if (cmd_obj_sp)
Jason Molendaf385f122011-10-22 00:47:41 +0000187 {
188 AddAlias ("stepi", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000189 AddAlias ("si", cmd_obj_sp);
Jason Molendaf385f122011-10-22 00:47:41 +0000190 }
191
192 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
193 if (cmd_obj_sp)
194 {
195 AddAlias ("nexti", cmd_obj_sp);
196 AddAlias ("ni", cmd_obj_sp);
197 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000198
199 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
200 if (cmd_obj_sp)
201 {
202 AddAlias ("s", cmd_obj_sp);
203 AddAlias ("step", cmd_obj_sp);
204 }
205
206 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
207 if (cmd_obj_sp)
208 {
209 AddAlias ("n", cmd_obj_sp);
210 AddAlias ("next", cmd_obj_sp);
211 }
212
213 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
214 if (cmd_obj_sp)
215 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000216 AddAlias ("finish", cmd_obj_sp);
217 }
218
Jim Ingham6d6d1072011-12-02 01:12:59 +0000219 cmd_obj_sp = GetCommandSPExact ("frame select", false);
220 if (cmd_obj_sp)
221 {
222 AddAlias ("f", cmd_obj_sp);
223 }
224
Jim Inghamca36cd12012-10-05 19:16:31 +0000225 cmd_obj_sp = GetCommandSPExact ("thread select", false);
226 if (cmd_obj_sp)
227 {
228 AddAlias ("t", cmd_obj_sp);
229 }
230
Greg Clayton6bade322013-02-01 23:33:03 +0000231 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
Caroline Ticeca90c472011-05-06 21:37:15 +0000232 if (cmd_obj_sp)
233 {
234 AddAlias ("l", cmd_obj_sp);
235 AddAlias ("list", cmd_obj_sp);
236 }
237
Greg Claytonef5651d2013-02-12 18:52:24 +0000238 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
239 if (cmd_obj_sp)
240 {
241 AddAlias ("env", cmd_obj_sp);
242 }
243
Caroline Ticeca90c472011-05-06 21:37:15 +0000244 cmd_obj_sp = GetCommandSPExact ("memory read", false);
245 if (cmd_obj_sp)
246 AddAlias ("x", cmd_obj_sp);
247
248 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
249 if (cmd_obj_sp)
250 AddAlias ("up", cmd_obj_sp);
251
252 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
253 if (cmd_obj_sp)
254 AddAlias ("down", cmd_obj_sp);
255
Jason Molenda0c8e0062011-10-25 02:11:20 +0000256 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000257 if (cmd_obj_sp)
258 AddAlias ("display", cmd_obj_sp);
Jim Ingham7e18e422011-10-24 18:37:00 +0000259
260 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
261 if (cmd_obj_sp)
262 AddAlias ("dis", cmd_obj_sp);
263
264 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
265 if (cmd_obj_sp)
266 AddAlias ("di", cmd_obj_sp);
267
268
Jason Molendabc7748b2011-10-22 01:30:52 +0000269
Jason Molenda0c8e0062011-10-25 02:11:20 +0000270 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000271 if (cmd_obj_sp)
272 AddAlias ("undisplay", cmd_obj_sp);
273
Jim Ingham71bf2992012-10-10 16:51:31 +0000274 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
275 if (cmd_obj_sp)
276 AddAlias ("bt", cmd_obj_sp);
277
Caroline Ticeca90c472011-05-06 21:37:15 +0000278 cmd_obj_sp = GetCommandSPExact ("target create", false);
279 if (cmd_obj_sp)
280 AddAlias ("file", cmd_obj_sp);
281
282 cmd_obj_sp = GetCommandSPExact ("target modules", false);
283 if (cmd_obj_sp)
284 AddAlias ("image", cmd_obj_sp);
285
286
287 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
Jim Inghamffba2292011-03-22 02:29:32 +0000288
Caroline Ticeca90c472011-05-06 21:37:15 +0000289 cmd_obj_sp = GetCommandSPExact ("expression", false);
290 if (cmd_obj_sp)
Sean Callanan90e579f2013-04-17 17:23:58 +0000291 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000292 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
293 AddAlias ("p", cmd_obj_sp);
294 AddAlias ("print", cmd_obj_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000295 AddAlias ("call", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000296 AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp);
297 AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000298 AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000299
300 alias_arguments_vector_sp.reset (new OptionArgVector);
Greg Clayton9e57dcd2013-05-15 01:03:08 +0000301 ProcessAliasOptionsArgs (cmd_obj_sp, "-O -- ", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000302 AddAlias ("po", cmd_obj_sp);
303 AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp);
304 }
305
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000306 cmd_obj_sp = GetCommandSPExact ("process kill", false);
307 if (cmd_obj_sp)
Greg Claytone86fd742012-09-27 00:02:27 +0000308 {
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000309 AddAlias ("kill", cmd_obj_sp);
Greg Claytone86fd742012-09-27 00:02:27 +0000310 }
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000311
Caroline Ticeca90c472011-05-06 21:37:15 +0000312 cmd_obj_sp = GetCommandSPExact ("process launch", false);
313 if (cmd_obj_sp)
314 {
315 alias_arguments_vector_sp.reset (new OptionArgVector);
Jason Molenda85da3122012-07-06 02:46:23 +0000316#if defined (__arm__)
317 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
318#else
Ed Masteb8ca4a22013-09-03 23:04:53 +0000319 ProcessAliasOptionsArgs (cmd_obj_sp, "--shell=" LLDB_DEFAULT_SHELL, alias_arguments_vector_sp);
Jason Molenda85da3122012-07-06 02:46:23 +0000320#endif
Caroline Ticeca90c472011-05-06 21:37:15 +0000321 AddAlias ("r", cmd_obj_sp);
322 AddAlias ("run", cmd_obj_sp);
323 AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp);
324 AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp);
325 }
Greg Clayton843d62d2012-03-29 21:47:51 +0000326
327 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
328 if (cmd_obj_sp)
329 {
330 AddAlias ("add-dsym", cmd_obj_sp);
331 }
Sean Callananfc732752012-05-21 18:25:19 +0000332
333 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
334 if (cmd_obj_sp)
335 {
336 alias_arguments_vector_sp.reset (new OptionArgVector);
337 ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp);
Jim Ingham06d282d2012-10-18 23:24:12 +0000338 AddAlias ("rbreak", cmd_obj_sp);
339 AddOrReplaceAliasOptions("rbreak", alias_arguments_vector_sp);
Sean Callananfc732752012-05-21 18:25:19 +0000340 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341}
342
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000343const char *
344CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
345{
346 // This function has not yet been implemented.
347
348 // Look for any embedded script command
349 // If found,
350 // get interpreter object from the command dictionary,
351 // call execute_one_command on it,
352 // get the results as a string,
353 // substitute that string for current stuff.
354
355 return arg;
356}
357
358
359void
360CommandInterpreter::LoadCommandDictionary ()
361{
362 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
363
Caroline Ticedaccaa92010-09-20 20:44:43 +0000364 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000365
Greg Claytona7015092010-09-18 01:14:36 +0000366 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000367 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Johnny Chenb89982d2011-04-21 00:39:18 +0000368 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000369 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
370 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000371 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000372 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000373 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
374 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
Greg Claytonded470d2011-03-19 01:12:21 +0000375 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000376 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000377 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000378 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000379 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000380 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000381 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Inghamebc09c32010-07-07 03:36:20 +0000382 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000383 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
384 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Enrico Granata223383e2011-08-16 23:24:13 +0000385 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
Johnny Chen31c39da2010-12-23 20:21:44 +0000386 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
Johnny Chenf04ee932011-09-22 18:04:58 +0000387 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000388
Jim Inghamca36cd12012-10-05 19:16:31 +0000389 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
390 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
Greg Clayton722e8852013-02-08 02:54:24 +0000391 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
Greg Clayton1b3815c2013-01-30 00:18:29 +0000392 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000393 {"^(-.*)$", "breakpoint set %1"},
394 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
Greg Clayton722e8852013-02-08 02:54:24 +0000395 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000396 {"^(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1'"}};
397
398 size_t num_regexes = sizeof break_regexes/sizeof(char *[2]);
399
Greg Clayton7b0992d2013-04-18 22:45:39 +0000400 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytona7015092010-09-18 01:14:36 +0000401 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000402 "_regexp-break",
Johnny Chenb417dcd2012-08-23 00:32:22 +0000403 "Set a breakpoint using a regular expression to specify the location, where <linenum> is in decimal and <address> is in hex.",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000404 "_regexp-break [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
405 2,
406 CommandCompletions::eSymbolCompletion |
407 CommandCompletions::eSourceFileCompletion));
Jim Inghamca36cd12012-10-05 19:16:31 +0000408
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409 if (break_regex_cmd_ap.get())
410 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000411 bool success = true;
412 for (size_t i = 0; i < num_regexes; i++)
413 {
414 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
415 if (!success)
416 break;
417 }
418 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
419
420 if (success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421 {
422 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
423 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
424 }
425 }
Jim Inghamffba2292011-03-22 02:29:32 +0000426
Greg Clayton7b0992d2013-04-18 22:45:39 +0000427 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamca36cd12012-10-05 19:16:31 +0000428 tbreak_regex_cmd_ap(new CommandObjectRegexCommand (*this,
429 "_regexp-tbreak",
430 "Set a one shot breakpoint using a regular expression to specify the location, where <linenum> is in decimal and <address> is in hex.",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000431 "_regexp-tbreak [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
432 2,
433 CommandCompletions::eSymbolCompletion |
434 CommandCompletions::eSourceFileCompletion));
Jim Inghamca36cd12012-10-05 19:16:31 +0000435
436 if (tbreak_regex_cmd_ap.get())
437 {
438 bool success = true;
439 for (size_t i = 0; i < num_regexes; i++)
440 {
441 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
442 char buffer[1024];
443 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
444 assert (num_printed < 1024);
445 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
446 if (!success)
447 break;
448 }
449 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
450
451 if (success)
452 {
453 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
454 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
455 }
456 }
457
Greg Clayton7b0992d2013-04-18 22:45:39 +0000458 std::unique_ptr<CommandObjectRegexCommand>
Johnny Chen6d675242012-08-24 18:15:45 +0000459 attach_regex_cmd_ap(new CommandObjectRegexCommand (*this,
460 "_regexp-attach",
461 "Attach to a process id if in decimal, otherwise treat the argument as a process name to attach to.",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000462 "_regexp-attach [<pid>]\n_regexp-attach [<process-name>]",
463 2));
Johnny Chen6d675242012-08-24 18:15:45 +0000464 if (attach_regex_cmd_ap.get())
465 {
Greg Clayton3cb4c7d2012-12-15 01:19:07 +0000466 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
467 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
468 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
469 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
Johnny Chen6d675242012-08-24 18:15:45 +0000470 {
471 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
472 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
473 }
474 }
475
Greg Clayton7b0992d2013-04-18 22:45:39 +0000476 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000477 down_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000478 "_regexp-down",
479 "Go down \"n\" frames in the stack (1 frame by default).",
480 "_regexp-down [n]", 2));
Jim Inghamffba2292011-03-22 02:29:32 +0000481 if (down_regex_cmd_ap.get())
482 {
483 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
484 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
485 {
486 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
487 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
488 }
489 }
490
Greg Clayton7b0992d2013-04-18 22:45:39 +0000491 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000492 up_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000493 "_regexp-up",
494 "Go up \"n\" frames in the stack (1 frame by default).",
495 "_regexp-up [n]", 2));
Jim Inghamffba2292011-03-22 02:29:32 +0000496 if (up_regex_cmd_ap.get())
497 {
498 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") &&
499 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1"))
500 {
501 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release());
502 m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp;
503 }
504 }
Jason Molendabc7748b2011-10-22 01:30:52 +0000505
Greg Clayton7b0992d2013-04-18 22:45:39 +0000506 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000507 display_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000508 "_regexp-display",
509 "Add an expression evaluation stop-hook.",
510 "_regexp-display expression", 2));
Jason Molendabc7748b2011-10-22 01:30:52 +0000511 if (display_regex_cmd_ap.get())
512 {
513 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
514 {
515 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
516 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
517 }
518 }
519
Greg Clayton7b0992d2013-04-18 22:45:39 +0000520 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000521 undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000522 "_regexp-undisplay",
523 "Remove an expression evaluation stop-hook.",
524 "_regexp-undisplay stop-hook-number", 2));
Jason Molendabc7748b2011-10-22 01:30:52 +0000525 if (undisplay_regex_cmd_ap.get())
526 {
527 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
528 {
529 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
530 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
531 }
532 }
533
Greg Clayton7b0992d2013-04-18 22:45:39 +0000534 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000535 connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000536 "gdb-remote",
537 "Connect to a remote GDB server. If no hostname is provided, localhost is assumed.",
538 "gdb-remote [<hostname>:]<portnum>", 2));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000539 if (connect_gdb_remote_cmd_ap.get())
540 {
541 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
542 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
543 {
544 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
545 m_command_dict[command_sp->GetCommandName ()] = command_sp;
546 }
547 }
548
Greg Clayton7b0992d2013-04-18 22:45:39 +0000549 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000550 connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand (*this,
551 "kdp-remote",
Jason Molendaa7dcb332012-10-23 03:05:16 +0000552 "Connect to a remote KDP server. udp port 41139 is the default port number.",
553 "kdp-remote <hostname>[:<portnum>]", 2));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000554 if (connect_kdp_remote_cmd_ap.get())
555 {
556 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
Jason Molendac36b1842012-09-27 02:47:55 +0000557 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000558 {
559 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
560 m_command_dict[command_sp->GetCommandName ()] = command_sp;
561 }
562 }
563
Greg Clayton7b0992d2013-04-18 22:45:39 +0000564 std::unique_ptr<CommandObjectRegexCommand>
Jason Molenda4cddfed2012-10-05 05:29:32 +0000565 bt_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Jim Ingham71bf2992012-10-10 16:51:31 +0000566 "_regexp-bt",
Jason Molenda4cddfed2012-10-05 05:29:32 +0000567 "Show a backtrace. An optional argument is accepted; if that argument is a number, it specifies the number of frames to display. If that argument is 'all', full backtraces of all threads are displayed.",
568 "bt [<digit>|all]", 2));
569 if (bt_regex_cmd_ap.get())
570 {
571 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
572 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
573 // so now "bt 3" is the preferred form, in line with gdb.
574 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
575 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
576 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
577 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
578 {
579 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
580 m_command_dict[command_sp->GetCommandName ()] = command_sp;
581 }
582 }
583
Greg Clayton7b0992d2013-04-18 22:45:39 +0000584 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton6bade322013-02-01 23:33:03 +0000585 list_regex_cmd_ap(new CommandObjectRegexCommand (*this,
586 "_regexp-list",
587 "Implements the GDB 'list' command in all of its forms except FILE:FUNCTION and maps them to the appropriate 'source list' commands.",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000588 "_regexp-list [<line>]\n_regexp-attach [<file>:<line>]\n_regexp-attach [<file>:<line>]",
589 2,
590 CommandCompletions::eSourceFileCompletion));
Greg Clayton6bade322013-02-01 23:33:03 +0000591 if (list_regex_cmd_ap.get())
592 {
593 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
594 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
595 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
Greg Claytond9010962013-02-12 18:42:05 +0000596 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
Greg Claytone4ca5152013-03-13 18:25:49 +0000597 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
Greg Clayton6bade322013-02-01 23:33:03 +0000598 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
599 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
600 {
601 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
602 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
603 }
604 }
605
Greg Clayton7b0992d2013-04-18 22:45:39 +0000606 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytonef5651d2013-02-12 18:52:24 +0000607 env_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000608 "_regexp-env",
609 "Implements a shortcut to viewing and setting environment variables.",
610 "_regexp-env\n_regexp-env FOO=BAR", 2));
Greg Claytonef5651d2013-02-12 18:52:24 +0000611 if (env_regex_cmd_ap.get())
612 {
613 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
614 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
615 {
616 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
617 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
618 }
619 }
620
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000621}
622
623int
624CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
625 StringList &matches)
626{
627 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
628
629 if (include_aliases)
630 {
631 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
632 }
633
634 return matches.GetSize();
635}
636
637CommandObjectSP
638CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
639{
640 CommandObject::CommandMap::iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +0000641 CommandObjectSP command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642
643 std::string cmd(cmd_cstr);
644
645 if (HasCommands())
646 {
647 pos = m_command_dict.find(cmd);
648 if (pos != m_command_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000649 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000650 }
651
652 if (include_aliases && HasAliases())
653 {
654 pos = m_alias_dict.find(cmd);
655 if (pos != m_alias_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000656 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000657 }
658
659 if (HasUserCommands())
660 {
661 pos = m_user_dict.find(cmd);
662 if (pos != m_user_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000663 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664 }
665
Greg Claytonc7bece562013-01-25 18:06:21 +0000666 if (!exact && !command_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000667 {
Jim Ingham279a6c22010-07-06 22:46:59 +0000668 // We will only get into here if we didn't find any exact matches.
669
670 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
671
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000672 StringList local_matches;
673 if (matches == NULL)
674 matches = &local_matches;
675
Jim Ingham279a6c22010-07-06 22:46:59 +0000676 unsigned int num_cmd_matches = 0;
677 unsigned int num_alias_matches = 0;
678 unsigned int num_user_matches = 0;
679
680 // Look through the command dictionaries one by one, and if we get only one match from any of
681 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
682
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 if (HasCommands())
684 {
685 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
686 }
687
688 if (num_cmd_matches == 1)
689 {
690 cmd.assign(matches->GetStringAtIndex(0));
691 pos = m_command_dict.find(cmd);
692 if (pos != m_command_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000693 real_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000694 }
695
Jim Ingham490ac552010-06-24 20:28:42 +0000696 if (include_aliases && HasAliases())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000697 {
698 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
699
700 }
701
Jim Ingham279a6c22010-07-06 22:46:59 +0000702 if (num_alias_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000703 {
704 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
705 pos = m_alias_dict.find(cmd);
706 if (pos != m_alias_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000707 alias_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708 }
709
Jim Ingham490ac552010-06-24 20:28:42 +0000710 if (HasUserCommands())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 {
712 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
713 }
714
Jim Ingham279a6c22010-07-06 22:46:59 +0000715 if (num_user_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 {
717 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
718
719 pos = m_user_dict.find (cmd);
720 if (pos != m_user_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000721 user_match_sp = pos->second;
722 }
723
724 // If we got exactly one match, return that, otherwise return the match list.
725
726 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
727 {
728 if (num_cmd_matches)
729 return real_match_sp;
730 else if (num_alias_matches)
731 return alias_match_sp;
732 else
733 return user_match_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 }
735 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000736 else if (matches && command_sp)
Jim Ingham279a6c22010-07-06 22:46:59 +0000737 {
738 matches->AppendString (cmd_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000739 }
740
741
Greg Claytonc7bece562013-01-25 18:06:21 +0000742 return command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743}
744
Greg Claytonde164aa2011-04-20 16:37:46 +0000745bool
746CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
747{
748 if (name && name[0])
749 {
750 std::string name_sstr(name);
Enrico Granatae00af802012-10-01 17:19:37 +0000751 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
752 if (found && !can_replace)
753 return false;
754 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000755 return false;
Greg Claytonde164aa2011-04-20 16:37:46 +0000756 m_command_dict[name_sstr] = cmd_sp;
757 return true;
758 }
759 return false;
760}
761
Enrico Granata223383e2011-08-16 23:24:13 +0000762bool
Enrico Granata0a305db2011-11-07 22:57:04 +0000763CommandInterpreter::AddUserCommand (std::string name,
Enrico Granata223383e2011-08-16 23:24:13 +0000764 const lldb::CommandObjectSP &cmd_sp,
765 bool can_replace)
766{
Enrico Granata0a305db2011-11-07 22:57:04 +0000767 if (!name.empty())
Enrico Granata223383e2011-08-16 23:24:13 +0000768 {
Enrico Granata0a305db2011-11-07 22:57:04 +0000769
770 const char* name_cstr = name.c_str();
771
772 // do not allow replacement of internal commands
773 if (CommandExists(name_cstr))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000774 {
775 if (can_replace == false)
776 return false;
777 if (m_command_dict[name]->IsRemovable() == false)
778 return false;
779 }
Enrico Granata0a305db2011-11-07 22:57:04 +0000780
Enrico Granata21dfcd92012-09-28 23:57:51 +0000781 if (UserCommandExists(name_cstr))
782 {
783 if (can_replace == false)
784 return false;
785 if (m_user_dict[name]->IsRemovable() == false)
786 return false;
787 }
788
Enrico Granata0a305db2011-11-07 22:57:04 +0000789 m_user_dict[name] = cmd_sp;
Enrico Granata223383e2011-08-16 23:24:13 +0000790 return true;
791 }
792 return false;
793}
Greg Claytonde164aa2011-04-20 16:37:46 +0000794
Jim Ingham279a6c22010-07-06 22:46:59 +0000795CommandObjectSP
796CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797{
Caroline Tice472362e2010-12-14 18:51:39 +0000798 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
799 CommandObjectSP ret_val; // Possibly empty return value.
800
801 if (cmd_cstr == NULL)
802 return ret_val;
803
804 if (cmd_words.GetArgumentCount() == 1)
805 return GetCommandSP(cmd_cstr, include_aliases, true, NULL);
806 else
807 {
808 // We have a multi-word command (seemingly), so we need to do more work.
809 // First, get the cmd_obj_sp for the first word in the command.
810 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, NULL);
811 if (cmd_obj_sp.get() != NULL)
812 {
813 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
814 // command name), and find the appropriate sub-command SP for each command word....
815 size_t end = cmd_words.GetArgumentCount();
816 for (size_t j= 1; j < end; ++j)
817 {
818 if (cmd_obj_sp->IsMultiwordObject())
819 {
Greg Clayton998255b2012-10-13 02:07:45 +0000820 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
Caroline Tice472362e2010-12-14 18:51:39 +0000821 if (cmd_obj_sp.get() == NULL)
822 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
823 return ret_val;
824 }
825 else
826 // We have more words in the command name, but we don't have a multiword object. Fail and return
827 // empty 'ret_val'.
828 return ret_val;
829 }
830 // We successfully looped through all the command words and got valid command objects for them. Assign the
831 // last object retrieved to 'ret_val'.
832 ret_val = cmd_obj_sp;
833 }
834 }
835 return ret_val;
Jim Ingham279a6c22010-07-06 22:46:59 +0000836}
837
838CommandObject *
839CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
840{
841 return GetCommandSPExact (cmd_cstr, include_aliases).get();
842}
843
844CommandObject *
845CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
846{
847 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
848
849 // If we didn't find an exact match to the command string in the commands, look in
850 // the aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000851
852 if (command_obj)
853 return command_obj;
Jim Ingham279a6c22010-07-06 22:46:59 +0000854
Enrico Granata5342c442013-06-18 18:01:08 +0000855 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
Jim Ingham279a6c22010-07-06 22:46:59 +0000856
Enrico Granata5342c442013-06-18 18:01:08 +0000857 if (command_obj)
858 return command_obj;
859
860 // If there wasn't an exact match then look for an inexact one in just the commands
861 command_obj = GetCommandSP(cmd_cstr, false, false, NULL).get();
Matt Kopec038ff812013-04-23 16:17:32 +0000862
863 // Finally, if there wasn't an inexact match among the commands, look for an inexact
864 // match in both the commands and aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000865
866 if (command_obj)
867 {
868 if (matches)
869 matches->AppendString(command_obj->GetCommandName());
870 return command_obj;
871 }
872
873 return GetCommandSP(cmd_cstr, true, false, matches).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000874}
875
876bool
877CommandInterpreter::CommandExists (const char *cmd)
878{
879 return m_command_dict.find(cmd) != m_command_dict.end();
880}
881
882bool
Caroline Ticeca90c472011-05-06 21:37:15 +0000883CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
884 const char *options_args,
885 OptionArgVectorSP &option_arg_vector_sp)
886{
887 bool success = true;
888 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
889
890 if (!options_args || (strlen (options_args) < 1))
891 return true;
892
893 std::string options_string (options_args);
894 Args args (options_args);
895 CommandReturnObject result;
896 // Check to see if the command being aliased can take any command options.
897 Options *options = cmd_obj_sp->GetOptions ();
898 if (options)
899 {
900 // See if any options were specified as part of the alias; if so, handle them appropriately.
901 options->NotifyOptionParsingStarting ();
902 args.Unshift ("dummy_arg");
903 args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
904 args.Shift ();
905 if (result.Succeeded())
906 options->VerifyPartialOptions (result);
907 if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
908 {
909 result.AppendError ("Unable to create requested alias.\n");
910 return false;
911 }
912 }
913
Greg Clayton5521f992011-10-28 21:38:01 +0000914 if (!options_string.empty())
Caroline Ticeca90c472011-05-06 21:37:15 +0000915 {
916 if (cmd_obj_sp->WantsRawCommandString ())
917 option_arg_vector->push_back (OptionArgPair ("<argument>",
918 OptionArgValue (-1,
919 options_string)));
920 else
921 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000922 const size_t argc = args.GetArgumentCount();
Caroline Ticeca90c472011-05-06 21:37:15 +0000923 for (size_t i = 0; i < argc; ++i)
924 if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
925 option_arg_vector->push_back
926 (OptionArgPair ("<argument>",
927 OptionArgValue (-1,
928 std::string (args.GetArgumentAtIndex (i)))));
929 }
930 }
931
932 return success;
933}
934
935bool
Jim Ingham298f3782013-04-03 00:25:49 +0000936CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
937{
938 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
939 if (exact_match)
940 {
941 full_name.assign(cmd);
942 return exact_match;
943 }
944 else
945 {
946 StringList matches;
947 size_t num_alias_matches;
948 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
949 if (num_alias_matches == 1)
950 {
951 // Make sure this isn't shadowing a command in the regular command space:
952 StringList regular_matches;
953 const bool include_aliases = false;
954 const bool exact = false;
955 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, &regular_matches));
956 if (cmd_obj_sp || regular_matches.GetSize() > 0)
957 return false;
958 else
959 {
960 full_name.assign (matches.GetStringAtIndex(0));
961 return true;
962 }
963 }
964 else
965 return false;
966 }
967}
968
969bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000970CommandInterpreter::AliasExists (const char *cmd)
971{
972 return m_alias_dict.find(cmd) != m_alias_dict.end();
973}
974
975bool
976CommandInterpreter::UserCommandExists (const char *cmd)
977{
978 return m_user_dict.find(cmd) != m_user_dict.end();
979}
980
981void
982CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
983{
Jim Ingham279a6c22010-07-06 22:46:59 +0000984 command_obj_sp->SetIsAlias (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000985 m_alias_dict[alias_name] = command_obj_sp;
986}
987
988bool
989CommandInterpreter::RemoveAlias (const char *alias_name)
990{
991 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
992 if (pos != m_alias_dict.end())
993 {
994 m_alias_dict.erase(pos);
995 return true;
996 }
997 return false;
998}
999bool
1000CommandInterpreter::RemoveUser (const char *alias_name)
1001{
1002 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1003 if (pos != m_user_dict.end())
1004 {
1005 m_user_dict.erase(pos);
1006 return true;
1007 }
1008 return false;
1009}
1010
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001011void
1012CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
1013{
1014 help_string.Printf ("'%s", command_name);
1015 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1016
Sean Callanan9a028512012-08-09 00:50:26 +00001017 if (option_arg_vector_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001018 {
1019 OptionArgVector *options = option_arg_vector_sp.get();
Andy Gibbsa297a972013-06-19 19:04:53 +00001020 for (size_t i = 0; i < options->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001021 {
1022 OptionArgPair cur_option = (*options)[i];
1023 std::string opt = cur_option.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00001024 OptionArgValue value_pair = cur_option.second;
1025 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001026 if (opt.compare("<argument>") == 0)
1027 {
1028 help_string.Printf (" %s", value.c_str());
1029 }
1030 else
1031 {
1032 help_string.Printf (" %s", opt.c_str());
1033 if ((value.compare ("<no-argument>") != 0)
1034 && (value.compare ("<need-argument") != 0))
1035 {
1036 help_string.Printf (" %s", value.c_str());
1037 }
1038 }
1039 }
1040 }
1041
1042 help_string.Printf ("'");
1043}
1044
Greg Clayton12fc3e02010-08-26 22:05:43 +00001045size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001046CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
1047{
1048 CommandObject::CommandMap::const_iterator pos;
Greg Clayton12fc3e02010-08-26 22:05:43 +00001049 CommandObject::CommandMap::const_iterator end = dict.end();
1050 size_t max_len = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001051
Greg Clayton12fc3e02010-08-26 22:05:43 +00001052 for (pos = dict.begin(); pos != end; ++pos)
1053 {
1054 size_t len = pos->first.size();
1055 if (max_len < len)
1056 max_len = len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001057 }
Greg Clayton12fc3e02010-08-26 22:05:43 +00001058 return max_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001059}
1060
1061void
Enrico Granata223383e2011-08-16 23:24:13 +00001062CommandInterpreter::GetHelp (CommandReturnObject &result,
Enrico Granata08633ee2011-09-09 17:49:36 +00001063 uint32_t cmd_types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064{
1065 CommandObject::CommandMap::const_iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +00001066 size_t max_len = FindLongestCommandWord (m_command_dict);
Enrico Granata223383e2011-08-16 23:24:13 +00001067
1068 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001069 {
Enrico Granata223383e2011-08-16 23:24:13 +00001070
1071 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
1072 result.AppendMessage("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001073
Enrico Granata223383e2011-08-16 23:24:13 +00001074 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1075 {
1076 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1077 max_len);
1078 }
1079 result.AppendMessage("");
1080
1081 }
1082
Greg Clayton5521f992011-10-28 21:38:01 +00001083 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001084 {
Jim Ingham49e80a12010-10-22 18:47:16 +00001085 result.AppendMessage("The following is a list of your current command abbreviations "
Johnny Chenb89982d2011-04-21 00:39:18 +00001086 "(see 'help command alias' for more info):");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001087 result.AppendMessage("");
Greg Clayton12fc3e02010-08-26 22:05:43 +00001088 max_len = FindLongestCommandWord (m_alias_dict);
1089
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001090 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
1091 {
1092 StreamString sstr;
1093 StreamString translation_and_help;
1094 std::string entry_name = pos->first;
1095 std::string second_entry = pos->second.get()->GetCommandName();
1096 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
1097
1098 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
1099 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
1100 translation_and_help.GetData(), max_len);
1101 }
1102 result.AppendMessage("");
1103 }
1104
Greg Clayton5521f992011-10-28 21:38:01 +00001105 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106 {
1107 result.AppendMessage ("The following is a list of your current user-defined commands:");
1108 result.AppendMessage("");
Enrico Granata223383e2011-08-16 23:24:13 +00001109 max_len = FindLongestCommandWord (m_user_dict);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001110 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1111 {
Enrico Granata223383e2011-08-16 23:24:13 +00001112 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1113 max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001114 }
1115 result.AppendMessage("");
1116 }
1117
1118 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
1119}
1120
Caroline Tice844d2302010-12-09 22:52:49 +00001121CommandObject *
1122CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123{
Caroline Tice844d2302010-12-09 22:52:49 +00001124 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1125 // eventually be invoked by the given command line.
1126
1127 CommandObject *cmd_obj = NULL;
1128 std::string white_space (" \t\v");
1129 size_t start = command_string.find_first_not_of (white_space);
1130 size_t end = 0;
1131 bool done = false;
1132 while (!done)
1133 {
1134 if (start != std::string::npos)
1135 {
1136 // Get the next word from command_string.
1137 end = command_string.find_first_of (white_space, start);
1138 if (end == std::string::npos)
1139 end = command_string.size();
1140 std::string cmd_word = command_string.substr (start, end - start);
1141
1142 if (cmd_obj == NULL)
1143 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1144 // command or alias.
1145 cmd_obj = GetCommandObject (cmd_word.c_str());
1146 else if (cmd_obj->IsMultiwordObject ())
1147 {
1148 // Our current object is a multi-word object; see if the cmd_word is a valid sub-command for our object.
Greg Clayton998255b2012-10-13 02:07:45 +00001149 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001150 if (sub_cmd_obj)
1151 cmd_obj = sub_cmd_obj;
1152 else // cmd_word was not a valid sub-command word, so we are donee
1153 done = true;
1154 }
1155 else
1156 // We have a cmd_obj and it is not a multi-word object, so we are done.
1157 done = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158
Caroline Tice844d2302010-12-09 22:52:49 +00001159 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1160 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1161 // next word.
1162
1163 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1164 done = true;
1165 else
1166 start = command_string.find_first_not_of (white_space, end);
1167 }
1168 else
1169 // Unable to find any more words.
1170 done = true;
1171 }
1172
1173 if (end == command_string.size())
1174 command_string.clear();
1175 else
1176 command_string = command_string.substr(end);
1177
1178 return cmd_obj;
1179}
1180
Greg Clayton51964162011-10-25 00:36:27 +00001181static const char *k_white_space = " \t\v";
Greg Clayton5521f992011-10-28 21:38:01 +00001182static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
Greg Clayton51964162011-10-25 00:36:27 +00001183static void
1184StripLeadingSpaces (std::string &s)
Caroline Tice844d2302010-12-09 22:52:49 +00001185{
Greg Clayton51964162011-10-25 00:36:27 +00001186 if (!s.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001187 {
Greg Clayton51964162011-10-25 00:36:27 +00001188 size_t pos = s.find_first_not_of (k_white_space);
1189 if (pos == std::string::npos)
1190 s.clear();
1191 else if (pos == 0)
1192 return;
1193 s.erase (0, pos);
1194 }
1195}
1196
Greg Clayton93c62e62011-11-09 23:25:03 +00001197static size_t
1198FindArgumentTerminator (const std::string &s)
1199{
Greg Clayton93c62e62011-11-09 23:25:03 +00001200 const size_t s_len = s.size();
1201 size_t offset = 0;
1202 while (offset < s_len)
1203 {
1204 size_t pos = s.find ("--", offset);
1205 if (pos == std::string::npos)
1206 break;
1207 if (pos > 0)
1208 {
1209 if (isspace(s[pos-1]))
1210 {
1211 // Check if the string ends "\s--" (where \s is a space character)
1212 // or if we have "\s--\s".
1213 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1214 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001215 return pos;
1216 }
1217 }
1218 }
1219 offset = pos + 2;
1220 }
Greg Clayton93c62e62011-11-09 23:25:03 +00001221 return std::string::npos;
1222}
1223
Greg Clayton51964162011-10-25 00:36:27 +00001224static bool
Greg Clayton5521f992011-10-28 21:38:01 +00001225ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
Greg Clayton51964162011-10-25 00:36:27 +00001226{
Greg Clayton5521f992011-10-28 21:38:01 +00001227 command.clear();
1228 suffix.clear();
Greg Clayton51964162011-10-25 00:36:27 +00001229 StripLeadingSpaces (command_string);
1230
1231 bool result = false;
1232 quote_char = '\0';
1233
1234 if (!command_string.empty())
1235 {
1236 const char first_char = command_string[0];
1237 if (first_char == '\'' || first_char == '"')
Caroline Tice844d2302010-12-09 22:52:49 +00001238 {
Greg Clayton51964162011-10-25 00:36:27 +00001239 quote_char = first_char;
1240 const size_t end_quote_pos = command_string.find (quote_char, 1);
1241 if (end_quote_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001242 {
Greg Clayton5521f992011-10-28 21:38:01 +00001243 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001244 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001245 }
1246 else
1247 {
Greg Clayton5521f992011-10-28 21:38:01 +00001248 command.assign (command_string, 1, end_quote_pos - 1);
Greg Clayton51964162011-10-25 00:36:27 +00001249 if (end_quote_pos + 1 < command_string.size())
1250 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1251 else
1252 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001253 }
Caroline Tice844d2302010-12-09 22:52:49 +00001254 }
1255 else
1256 {
Greg Clayton51964162011-10-25 00:36:27 +00001257 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1258 if (first_space_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001259 {
Greg Clayton5521f992011-10-28 21:38:01 +00001260 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001261 command_string.erase();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001262 }
1263 else
1264 {
Greg Clayton5521f992011-10-28 21:38:01 +00001265 command.assign (command_string, 0, first_space_pos);
1266 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
Caroline Tice2b5e8502011-05-11 16:07:06 +00001267 }
Caroline Tice844d2302010-12-09 22:52:49 +00001268 }
Greg Clayton51964162011-10-25 00:36:27 +00001269 result = true;
Caroline Tice844d2302010-12-09 22:52:49 +00001270 }
Greg Clayton5521f992011-10-28 21:38:01 +00001271
1272
1273 if (!command.empty())
1274 {
1275 // actual commands can't start with '-' or '_'
1276 if (command[0] != '-' && command[0] != '_')
1277 {
1278 size_t pos = command.find_first_not_of(k_valid_command_chars);
1279 if (pos > 0 && pos != std::string::npos)
1280 {
1281 suffix.assign (command.begin() + pos, command.end());
1282 command.erase (pos);
1283 }
1284 }
1285 }
Greg Clayton51964162011-10-25 00:36:27 +00001286
1287 return result;
Caroline Tice844d2302010-12-09 22:52:49 +00001288}
1289
Greg Clayton5521f992011-10-28 21:38:01 +00001290CommandObject *
1291CommandInterpreter::BuildAliasResult (const char *alias_name,
1292 std::string &raw_input_string,
1293 std::string &alias_result,
1294 CommandReturnObject &result)
Caroline Tice844d2302010-12-09 22:52:49 +00001295{
Greg Clayton5521f992011-10-28 21:38:01 +00001296 CommandObject *alias_cmd_obj = NULL;
Caroline Tice844d2302010-12-09 22:52:49 +00001297 Args cmd_args (raw_input_string.c_str());
1298 alias_cmd_obj = GetCommandObject (alias_name);
1299 StreamString result_str;
1300
1301 if (alias_cmd_obj)
1302 {
1303 std::string alias_name_str = alias_name;
1304 if ((cmd_args.GetArgumentCount() == 0)
1305 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1306 cmd_args.Unshift (alias_name);
1307
1308 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1309 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1310
1311 if (option_arg_vector_sp.get())
1312 {
1313 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1314
Andy Gibbsa297a972013-06-19 19:04:53 +00001315 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Caroline Tice844d2302010-12-09 22:52:49 +00001316 {
1317 OptionArgPair option_pair = (*option_arg_vector)[i];
1318 OptionArgValue value_pair = option_pair.second;
1319 int value_type = value_pair.first;
1320 std::string option = option_pair.first;
1321 std::string value = value_pair.second;
1322 if (option.compare ("<argument>") == 0)
1323 result_str.Printf (" %s", value.c_str());
1324 else
1325 {
1326 result_str.Printf (" %s", option.c_str());
Virgile Belloe2607b52013-09-05 16:42:23 +00001327 if (value_type != OptionParser::eOptionalArgument)
Caroline Tice844d2302010-12-09 22:52:49 +00001328 result_str.Printf (" ");
Virgile Belloe2607b52013-09-05 16:42:23 +00001329 if (value.compare ("<OptionParser::eNoArgument>") != 0)
Caroline Tice844d2302010-12-09 22:52:49 +00001330 {
1331 int index = GetOptionArgumentPosition (value.c_str());
1332 if (index == 0)
1333 result_str.Printf ("%s", value.c_str());
1334 else if (index >= cmd_args.GetArgumentCount())
1335 {
1336
1337 result.AppendErrorWithFormat
1338 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1339 index);
1340 result.SetStatus (eReturnStatusFailed);
Greg Clayton5521f992011-10-28 21:38:01 +00001341 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001342 }
1343 else
1344 {
1345 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1346 if (strpos != std::string::npos)
1347 raw_input_string = raw_input_string.erase (strpos,
1348 strlen (cmd_args.GetArgumentAtIndex (index)));
1349 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1350 }
1351 }
1352 }
1353 }
1354 }
1355
1356 alias_result = result_str.GetData();
1357 }
Greg Clayton5521f992011-10-28 21:38:01 +00001358 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001359}
1360
Greg Clayton5a314712011-10-14 07:41:33 +00001361Error
1362CommandInterpreter::PreprocessCommand (std::string &command)
1363{
1364 // The command preprocessor needs to do things to the command
1365 // line before any parsing of arguments or anything else is done.
1366 // The only current stuff that gets proprocessed is anyting enclosed
1367 // in backtick ('`') characters is evaluated as an expression and
1368 // the result of the expression must be a scalar that can be substituted
1369 // into the command. An example would be:
1370 // (lldb) memory read `$rsp + 20`
1371 Error error; // Error for any expressions that might not evaluate
1372 size_t start_backtick;
1373 size_t pos = 0;
1374 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1375 {
1376 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1377 {
1378 // The backtick was preceeded by a '\' character, remove the slash
1379 // and don't treat the backtick as the start of an expression
1380 command.erase(start_backtick-1, 1);
1381 // No need to add one to start_backtick since we just deleted a char
1382 pos = start_backtick;
1383 }
1384 else
1385 {
1386 const size_t expr_content_start = start_backtick + 1;
1387 const size_t end_backtick = command.find ('`', expr_content_start);
1388 if (end_backtick == std::string::npos)
1389 return error;
1390 else if (end_backtick == expr_content_start)
1391 {
1392 // Empty expression (two backticks in a row)
1393 command.erase (start_backtick, 2);
1394 }
1395 else
1396 {
1397 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1398
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00001399 ExecutionContext exe_ctx(GetExecutionContext());
1400 Target *target = exe_ctx.GetTargetPtr();
Johnny Chen51ea0ad2011-10-29 00:21:50 +00001401 // Get a dummy target to allow for calculator mode while processing backticks.
1402 // This also helps break the infinite loop caused when target is null.
1403 if (!target)
1404 target = Host::GetDummyTarget(GetDebugger()).get();
Greg Clayton5a314712011-10-14 07:41:33 +00001405 if (target)
1406 {
Greg Clayton5a314712011-10-14 07:41:33 +00001407 ValueObjectSP expr_result_valobj_sp;
Enrico Granatad4439aa2012-09-05 20:41:26 +00001408
Jim Ingham35e1bda2012-10-16 21:41:58 +00001409 EvaluateExpressionOptions options;
Enrico Granatad4439aa2012-09-05 20:41:26 +00001410 options.SetCoerceToId(false)
1411 .SetUnwindOnError(true)
Jim Ingham184e9812013-01-15 02:47:48 +00001412 .SetIgnoreBreakpoints(true)
Enrico Granatad4439aa2012-09-05 20:41:26 +00001413 .SetKeepInMemory(false)
Jim Ingham35e1bda2012-10-16 21:41:58 +00001414 .SetRunOthers(true)
1415 .SetTimeoutUsec(0);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001416
Greg Clayton5a314712011-10-14 07:41:33 +00001417 ExecutionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
Enrico Granatad4439aa2012-09-05 20:41:26 +00001418 exe_ctx.GetFramePtr(),
Enrico Granata3372f582012-07-16 23:10:35 +00001419 expr_result_valobj_sp,
Enrico Granatad4439aa2012-09-05 20:41:26 +00001420 options);
1421
Greg Clayton5a314712011-10-14 07:41:33 +00001422 if (expr_result == eExecutionCompleted)
1423 {
1424 Scalar scalar;
1425 if (expr_result_valobj_sp->ResolveValue (scalar))
1426 {
1427 command.erase (start_backtick, end_backtick - start_backtick + 1);
1428 StreamString value_strm;
1429 const bool show_type = false;
1430 scalar.GetValue (&value_strm, show_type);
1431 size_t value_string_size = value_strm.GetSize();
1432 if (value_string_size)
1433 {
1434 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1435 pos = start_backtick + value_string_size;
1436 continue;
1437 }
1438 else
1439 {
1440 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1441 }
1442 }
1443 else
1444 {
1445 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1446 }
1447 }
1448 else
1449 {
1450 if (expr_result_valobj_sp)
1451 error = expr_result_valobj_sp->GetError();
1452 if (error.Success())
1453 {
1454
1455 switch (expr_result)
1456 {
1457 case eExecutionSetupError:
1458 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1459 break;
1460 case eExecutionCompleted:
1461 break;
1462 case eExecutionDiscarded:
1463 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1464 break;
1465 case eExecutionInterrupted:
1466 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1467 break;
Jim Ingham184e9812013-01-15 02:47:48 +00001468 case eExecutionHitBreakpoint:
1469 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1470 break;
Greg Clayton5a314712011-10-14 07:41:33 +00001471 case eExecutionTimedOut:
1472 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1473 break;
1474 }
1475 }
1476 }
1477 }
1478 }
1479 if (error.Fail())
1480 break;
1481 }
1482 }
1483 return error;
1484}
1485
1486
Caroline Tice844d2302010-12-09 22:52:49 +00001487bool
1488CommandInterpreter::HandleCommand (const char *command_line,
Enrico Granata5f5ab602012-05-31 01:09:06 +00001489 LazyBool lazy_add_to_history,
Caroline Tice844d2302010-12-09 22:52:49 +00001490 CommandReturnObject &result,
Jim Inghame16c50a2011-02-18 00:54:25 +00001491 ExecutionContext *override_context,
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001492 bool repeat_on_empty_command,
1493 bool no_context_switching)
Jim Inghame16c50a2011-02-18 00:54:25 +00001494
Caroline Tice844d2302010-12-09 22:52:49 +00001495{
Jim Inghame16c50a2011-02-18 00:54:25 +00001496
Caroline Tice844d2302010-12-09 22:52:49 +00001497 bool done = false;
1498 CommandObject *cmd_obj = NULL;
Caroline Tice844d2302010-12-09 22:52:49 +00001499 bool wants_raw_input = false;
1500 std::string command_string (command_line);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001501 std::string original_command_string (command_line);
Caroline Tice844d2302010-12-09 22:52:49 +00001502
Greg Clayton5160ce52013-03-27 23:08:40 +00001503 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001504 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1505
1506 // Make a scoped cleanup object that will clear the crash description string
1507 // on exit of this function.
Enrico Granataf9fa6ee2011-07-12 00:18:11 +00001508 lldb_utility::CleanUp <const char *> crash_description_cleanup(NULL, Host::SetCrashDescription);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001509
Caroline Tice844d2302010-12-09 22:52:49 +00001510 if (log)
1511 log->Printf ("Processing command: %s", command_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001512
Jim Ingham30244832010-11-04 23:08:45 +00001513 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1514
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001515 if (!no_context_switching)
1516 UpdateExecutionContext (override_context);
Enrico Granata5f5ab602012-05-31 01:09:06 +00001517
Enrico Granata5f5ab602012-05-31 01:09:06 +00001518 bool add_to_history;
1519 if (lazy_add_to_history == eLazyBoolCalculate)
1520 add_to_history = (m_command_source_depth == 0);
1521 else
1522 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1523
Jim Inghame16c50a2011-02-18 00:54:25 +00001524 bool empty_command = false;
1525 bool comment_command = false;
1526 if (command_string.empty())
1527 empty_command = true;
1528 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001530 const char *k_space_characters = "\t\n\v\f\r ";
1531
1532 size_t non_space = command_string.find_first_not_of (k_space_characters);
1533 // Check for empty line or comment line (lines whose first
1534 // non-space character is the comment character for this interpreter)
1535 if (non_space == std::string::npos)
1536 empty_command = true;
1537 else if (command_string[non_space] == m_comment_char)
1538 comment_command = true;
Enrico Granata7594f142013-06-17 22:51:50 +00001539 else if (command_string[non_space] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001540 {
Enrico Granata7594f142013-06-17 22:51:50 +00001541 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001542 if (history_string == NULL)
1543 {
1544 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1545 result.SetStatus(eReturnStatusFailed);
1546 return false;
1547 }
1548 add_to_history = false;
1549 command_string = history_string;
1550 original_command_string = history_string;
1551 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001552 }
1553
1554 if (empty_command)
1555 {
1556 if (repeat_on_empty_command)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001557 {
Enrico Granata7594f142013-06-17 22:51:50 +00001558 if (m_command_history.IsEmpty())
Jim Inghame16c50a2011-02-18 00:54:25 +00001559 {
1560 result.AppendError ("empty command");
1561 result.SetStatus(eReturnStatusFailed);
1562 return false;
1563 }
1564 else
1565 {
1566 command_line = m_repeat_command.c_str();
1567 command_string = command_line;
Jim Inghama5a97eb2011-07-12 03:12:18 +00001568 original_command_string = command_line;
Jim Inghame16c50a2011-02-18 00:54:25 +00001569 if (m_repeat_command.empty())
1570 {
1571 result.AppendErrorWithFormat("No auto repeat.\n");
1572 result.SetStatus (eReturnStatusFailed);
1573 return false;
1574 }
1575 }
1576 add_to_history = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001577 }
1578 else
1579 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001580 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1581 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001582 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001583 }
1584 else if (comment_command)
1585 {
1586 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1587 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001588 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001589
Greg Clayton5a314712011-10-14 07:41:33 +00001590
1591 Error error (PreprocessCommand (command_string));
1592
1593 if (error.Fail())
1594 {
1595 result.AppendError (error.AsCString());
1596 result.SetStatus(eReturnStatusFailed);
1597 return false;
1598 }
Caroline Tice844d2302010-12-09 22:52:49 +00001599 // Phase 1.
1600
1601 // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object
1602 // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that
1603 // the user could have specified an alias, and in translating the alias there may also be command options and/or
1604 // even data (including raw text strings) that need to be found and inserted into the command line as part of
1605 // the translation. So this first step is plain look-up & replacement, resulting in three things: 1). the command
Greg Clayton710dd5a2011-01-08 20:28:42 +00001606 // object whose Execute method will actually be called; 2). a revised command string, with all substitutions &
Caroline Tice844d2302010-12-09 22:52:49 +00001607 // replacements taken care of; 3). whether or not the Execute function wants raw input or not.
Caroline Ticed9d63362010-12-07 19:58:26 +00001608
Caroline Tice844d2302010-12-09 22:52:49 +00001609 StreamString revised_command_line;
Caroline Tice01274c02010-12-11 08:16:56 +00001610 size_t actual_cmd_name_len = 0;
Greg Clayton5521f992011-10-28 21:38:01 +00001611 std::string next_word;
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001612 StringList matches;
Caroline Tice844d2302010-12-09 22:52:49 +00001613 while (!done)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001614 {
Caroline Tice2b5e8502011-05-11 16:07:06 +00001615 char quote_char = '\0';
Greg Clayton5521f992011-10-28 21:38:01 +00001616 std::string suffix;
1617 ExtractCommand (command_string, next_word, suffix, quote_char);
1618 if (cmd_obj == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001619 {
Jim Ingham298f3782013-04-03 00:25:49 +00001620 std::string full_name;
1621 if (GetAliasFullName(next_word.c_str(), full_name))
Caroline Tice472362e2010-12-14 18:51:39 +00001622 {
Greg Clayton5521f992011-10-28 21:38:01 +00001623 std::string alias_result;
Jim Ingham298f3782013-04-03 00:25:49 +00001624 cmd_obj = BuildAliasResult (full_name.c_str(), command_string, alias_result, result);
Greg Clayton5521f992011-10-28 21:38:01 +00001625 revised_command_line.Printf ("%s", alias_result.c_str());
1626 if (cmd_obj)
1627 {
1628 wants_raw_input = cmd_obj->WantsRawCommandString ();
1629 actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
1630 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001631 }
1632 else
1633 {
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001634 cmd_obj = GetCommandObject (next_word.c_str(), &matches);
Greg Clayton5521f992011-10-28 21:38:01 +00001635 if (cmd_obj)
1636 {
1637 actual_cmd_name_len += next_word.length();
1638 revised_command_line.Printf ("%s", next_word.c_str());
1639 wants_raw_input = cmd_obj->WantsRawCommandString ();
1640 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001641 else
Greg Clayton5521f992011-10-28 21:38:01 +00001642 {
1643 revised_command_line.Printf ("%s", next_word.c_str());
1644 }
Caroline Tice844d2302010-12-09 22:52:49 +00001645 }
1646 }
1647 else
1648 {
Greg Clayton5521f992011-10-28 21:38:01 +00001649 if (cmd_obj->IsMultiwordObject ())
1650 {
Greg Clayton998255b2012-10-13 02:07:45 +00001651 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (next_word.c_str());
Greg Clayton5521f992011-10-28 21:38:01 +00001652 if (sub_cmd_obj)
1653 {
1654 actual_cmd_name_len += next_word.length() + 1;
1655 revised_command_line.Printf (" %s", next_word.c_str());
1656 cmd_obj = sub_cmd_obj;
1657 wants_raw_input = cmd_obj->WantsRawCommandString ();
1658 }
1659 else
1660 {
1661 if (quote_char)
1662 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1663 else
1664 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1665 done = true;
1666 }
1667 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001668 else
Greg Clayton5521f992011-10-28 21:38:01 +00001669 {
1670 if (quote_char)
1671 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1672 else
1673 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1674 done = true;
1675 }
Caroline Tice844d2302010-12-09 22:52:49 +00001676 }
1677
1678 if (cmd_obj == NULL)
1679 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001680 const size_t num_matches = matches.GetSize();
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001681 if (matches.GetSize() > 1) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001682 StreamString error_msg;
1683 error_msg.Printf ("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001684
1685 for (uint32_t i = 0; i < num_matches; ++i) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001686 error_msg.Printf ("\t%s\n", matches.GetStringAtIndex(i));
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001687 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001688 result.AppendRawError (error_msg.GetString().c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001689 } else {
1690 // We didn't have only one match, otherwise we wouldn't get here.
1691 assert(num_matches == 0);
1692 result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
1693 }
Caroline Tice844d2302010-12-09 22:52:49 +00001694 result.SetStatus (eReturnStatusFailed);
1695 return false;
1696 }
1697
Greg Clayton5521f992011-10-28 21:38:01 +00001698 if (cmd_obj->IsMultiwordObject ())
1699 {
1700 if (!suffix.empty())
1701 {
1702
Enrico Granataaded51d2013-06-12 00:44:43 +00001703 result.AppendErrorWithFormat ("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
1704 cmd_obj->GetCommandName(),
1705 next_word.empty() ? "" : next_word.c_str(),
1706 next_word.empty() ? " -- " : " ",
Greg Clayton5521f992011-10-28 21:38:01 +00001707 suffix.c_str());
1708 result.SetStatus (eReturnStatusFailed);
1709 return false;
1710 }
1711 }
1712 else
1713 {
1714 // If we found a normal command, we are done
1715 done = true;
1716 if (!suffix.empty())
1717 {
1718 switch (suffix[0])
1719 {
1720 case '/':
1721 // GDB format suffixes
Greg Clayton52ec56c2011-10-29 00:57:28 +00001722 {
1723 Options *command_options = cmd_obj->GetOptions();
1724 if (command_options && command_options->SupportsLongOption("gdb-format"))
1725 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001726 std::string gdb_format_option ("--gdb-format=");
1727 gdb_format_option += (suffix.c_str() + 1);
1728
1729 bool inserted = false;
1730 std::string &cmd = revised_command_line.GetString();
1731 size_t arg_terminator_idx = FindArgumentTerminator (cmd);
1732 if (arg_terminator_idx != std::string::npos)
1733 {
1734 // Insert the gdb format option before the "--" that terminates options
1735 gdb_format_option.append(1,' ');
1736 cmd.insert(arg_terminator_idx, gdb_format_option);
1737 inserted = true;
1738 }
1739
1740 if (!inserted)
1741 revised_command_line.Printf (" %s", gdb_format_option.c_str());
1742
1743 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
1744 revised_command_line.PutCString (" --");
Greg Clayton52ec56c2011-10-29 00:57:28 +00001745 }
1746 else
1747 {
1748 result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n",
1749 cmd_obj->GetCommandName());
1750 result.SetStatus (eReturnStatusFailed);
1751 return false;
1752 }
1753 }
Greg Clayton5521f992011-10-28 21:38:01 +00001754 break;
Johnny Chen8e9383d2011-10-31 22:22:06 +00001755
1756 default:
1757 result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n",
1758 suffix.c_str());
1759 result.SetStatus (eReturnStatusFailed);
1760 return false;
1761
Greg Clayton5521f992011-10-28 21:38:01 +00001762 }
1763 }
1764 }
Caroline Tice844d2302010-12-09 22:52:49 +00001765 if (command_string.length() == 0)
1766 done = true;
1767
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001768 }
Caroline Tice844d2302010-12-09 22:52:49 +00001769
Greg Clayton5521f992011-10-28 21:38:01 +00001770 if (!command_string.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001771 revised_command_line.Printf (" %s", command_string.c_str());
1772
1773 // End of Phase 1.
1774 // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command
1775 // specified was valid; revised_command_line contains the complete command line (including command name(s)),
1776 // fully translated with all substitutions & translations taken care of (still in raw text format); and
1777 // wants_raw_input specifies whether the Execute method expects raw input or not.
1778
1779
1780 if (log)
1781 {
1782 log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1783 log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData());
1784 log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
1785 }
1786
1787 // Phase 2.
1788 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1789 // CommandObject, with the appropriate arguments.
1790
1791 if (cmd_obj != NULL)
1792 {
1793 if (add_to_history)
1794 {
1795 Args command_args (revised_command_line.GetData());
1796 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
1797 if (repeat_command != NULL)
1798 m_repeat_command.assign(repeat_command);
1799 else
Jim Inghama5a97eb2011-07-12 03:12:18 +00001800 m_repeat_command.assign(original_command_string.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001801
Enrico Granata7594f142013-06-17 22:51:50 +00001802 m_command_history.AppendString (original_command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001803 }
1804
1805 command_string = revised_command_line.GetData();
1806 std::string command_name (cmd_obj->GetCommandName());
Caroline Tice01274c02010-12-11 08:16:56 +00001807 std::string remainder;
1808 if (actual_cmd_name_len < command_string.length())
1809 remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter
1810 // than cmd_obj->GetCommandName(), because name completion
1811 // allows users to enter short versions of the names,
1812 // e.g. 'br s' for 'breakpoint set'.
Caroline Tice844d2302010-12-09 22:52:49 +00001813
1814 // Remove any initial spaces
1815 std::string white_space (" \t\v");
1816 size_t pos = remainder.find_first_not_of (white_space);
1817 if (pos != 0 && pos != std::string::npos)
Greg Claytona3482592011-04-22 20:58:45 +00001818 remainder.erase(0, pos);
Caroline Tice844d2302010-12-09 22:52:49 +00001819
1820 if (log)
Jason Molendabfb36ff2011-08-25 00:20:04 +00001821 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001822
Jim Ingham5a988412012-06-08 21:56:10 +00001823 cmd_obj->Execute (remainder.c_str(), result);
Caroline Tice844d2302010-12-09 22:52:49 +00001824 }
1825 else
1826 {
1827 // We didn't find the first command object, so complete the first argument.
1828 Args command_args (revised_command_line.GetData());
1829 StringList matches;
1830 int num_matches;
1831 int cursor_index = 0;
1832 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1833 bool word_complete;
1834 num_matches = HandleCompletionMatches (command_args,
1835 cursor_index,
1836 cursor_char_position,
1837 0,
1838 -1,
1839 word_complete,
1840 matches);
1841
1842 if (num_matches > 0)
1843 {
1844 std::string error_msg;
1845 error_msg.assign ("ambiguous command '");
1846 error_msg.append(command_args.GetArgumentAtIndex(0));
1847 error_msg.append ("'.");
1848
1849 error_msg.append (" Possible completions:");
1850 for (int i = 0; i < num_matches; i++)
1851 {
1852 error_msg.append ("\n\t");
1853 error_msg.append (matches.GetStringAtIndex (i));
1854 }
1855 error_msg.append ("\n");
Greg Claytonc7bece562013-01-25 18:06:21 +00001856 result.AppendRawError (error_msg.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001857 }
1858 else
1859 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1860
1861 result.SetStatus (eReturnStatusFailed);
1862 }
1863
Jason Molendabfb36ff2011-08-25 00:20:04 +00001864 if (log)
1865 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1866
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001867 return result.Succeeded();
1868}
1869
1870int
1871CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
1872 int &cursor_index,
1873 int &cursor_char_position,
1874 int match_start_point,
1875 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +00001876 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001877 StringList &matches)
1878{
1879 int num_command_matches = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001880 bool look_for_subcommand = false;
Jim Ingham558ce122010-06-30 05:02:46 +00001881
1882 // For any of the command completions a unique match will be a complete word.
1883 word_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001884
1885 if (cursor_index == -1)
1886 {
1887 // We got nothing on the command line, so return the list of commands
Jim Ingham279a6c22010-07-06 22:46:59 +00001888 bool include_aliases = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001889 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
1890 }
1891 else if (cursor_index == 0)
1892 {
1893 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Ingham279a6c22010-07-06 22:46:59 +00001894 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001895 num_command_matches = matches.GetSize();
1896
1897 if (num_command_matches == 1
1898 && cmd_obj && cmd_obj->IsMultiwordObject()
1899 && matches.GetStringAtIndex(0) != NULL
1900 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
1901 {
1902 look_for_subcommand = true;
1903 num_command_matches = 0;
1904 matches.DeleteStringAtIndex(0);
1905 parsed_line.AppendArgument ("");
1906 cursor_index++;
1907 cursor_char_position = 0;
1908 }
1909 }
1910
1911 if (cursor_index > 0 || look_for_subcommand)
1912 {
1913 // We are completing further on into a commands arguments, so find the command and tell it
1914 // to complete the command.
1915 // First see if there is a matching initial command:
Jim Ingham279a6c22010-07-06 22:46:59 +00001916 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001917 if (command_object == NULL)
1918 {
1919 return 0;
1920 }
1921 else
1922 {
1923 parsed_line.Shift();
1924 cursor_index--;
Greg Claytona7015092010-09-18 01:14:36 +00001925 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton66111032010-06-23 01:19:29 +00001926 cursor_index,
1927 cursor_char_position,
1928 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00001929 max_return_elements,
1930 word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001931 matches);
1932 }
1933 }
1934
1935 return num_command_matches;
1936
1937}
1938
1939int
1940CommandInterpreter::HandleCompletion (const char *current_line,
1941 const char *cursor,
1942 const char *last_char,
1943 int match_start_point,
1944 int max_return_elements,
1945 StringList &matches)
1946{
1947 // We parse the argument up to the cursor, so the last argument in parsed_line is
1948 // the one containing the cursor, and the cursor is after the last character.
1949
1950 Args parsed_line(current_line, last_char - current_line);
1951 Args partial_parsed_line(current_line, cursor - current_line);
1952
Jim Inghama5a97eb2011-07-12 03:12:18 +00001953 // Don't complete comments, and if the line we are completing is just the history repeat character,
1954 // substitute the appropriate history line.
1955 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
1956 if (first_arg)
1957 {
1958 if (first_arg[0] == m_comment_char)
1959 return 0;
Enrico Granata7594f142013-06-17 22:51:50 +00001960 else if (first_arg[0] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001961 {
Enrico Granata7594f142013-06-17 22:51:50 +00001962 const char *history_string = m_command_history.FindString (first_arg);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001963 if (history_string != NULL)
1964 {
1965 matches.Clear();
1966 matches.InsertStringAtIndex(0, history_string);
1967 return -2;
1968 }
1969 else
1970 return 0;
1971
1972 }
1973 }
1974
1975
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001976 int num_args = partial_parsed_line.GetArgumentCount();
1977 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
1978 int cursor_char_position;
1979
1980 if (cursor_index == -1)
1981 cursor_char_position = 0;
1982 else
1983 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
Jim Inghamfe0c4252010-12-14 19:56:01 +00001984
1985 if (cursor > current_line && cursor[-1] == ' ')
1986 {
1987 // We are just after a space. If we are in an argument, then we will continue
1988 // parsing, but if we are between arguments, then we have to complete whatever the next
1989 // element would be.
1990 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
1991 // protected by a quote) then the space will also be in the parsed argument...
1992
1993 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
1994 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
1995 {
1996 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '"');
1997 cursor_index++;
1998 cursor_char_position = 0;
1999 }
2000 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001
2002 int num_command_matches;
2003
2004 matches.Clear();
2005
2006 // Only max_return_elements == -1 is supported at present:
2007 assert (max_return_elements == -1);
Jim Ingham558ce122010-06-30 05:02:46 +00002008 bool word_complete;
Greg Clayton66111032010-06-23 01:19:29 +00002009 num_command_matches = HandleCompletionMatches (parsed_line,
2010 cursor_index,
2011 cursor_char_position,
2012 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002013 max_return_elements,
2014 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +00002015 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002016
2017 if (num_command_matches <= 0)
2018 return num_command_matches;
2019
2020 if (num_args == 0)
2021 {
2022 // If we got an empty string, insert nothing.
2023 matches.InsertStringAtIndex(0, "");
2024 }
2025 else
2026 {
2027 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
2028 // put an empty string in element 0.
2029 std::string command_partial_str;
2030 if (cursor_index >= 0)
Jim Ingham49e80a12010-10-22 18:47:16 +00002031 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
2032 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002033
2034 std::string common_prefix;
2035 matches.LongestCommonPrefix (common_prefix);
Greg Claytonc7bece562013-01-25 18:06:21 +00002036 const size_t partial_name_len = command_partial_str.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002037
2038 // If we matched a unique single command, add a space...
Jim Ingham558ce122010-06-30 05:02:46 +00002039 // Only do this if the completer told us this was a complete word, however...
2040 if (num_command_matches == 1 && word_complete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002041 {
2042 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
2043 if (quote_char != '\0')
2044 common_prefix.push_back(quote_char);
2045
2046 common_prefix.push_back(' ');
2047 }
2048 common_prefix.erase (0, partial_name_len);
2049 matches.InsertStringAtIndex(0, common_prefix.c_str());
2050 }
2051 return num_command_matches;
2052}
2053
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002054
2055CommandInterpreter::~CommandInterpreter ()
2056{
2057}
2058
2059const char *
2060CommandInterpreter::GetPrompt ()
2061{
Caroline Ticedaccaa92010-09-20 20:44:43 +00002062 return m_debugger.GetPrompt();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002063}
2064
2065void
2066CommandInterpreter::SetPrompt (const char *new_prompt)
2067{
Caroline Ticedaccaa92010-09-20 20:44:43 +00002068 m_debugger.SetPrompt (new_prompt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002069}
2070
Jim Ingham97a6dc72010-10-04 19:49:29 +00002071size_t
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002072CommandInterpreter::GetConfirmationInputReaderCallback
2073(
2074 void *baton,
2075 InputReader &reader,
2076 lldb::InputReaderAction action,
2077 const char *bytes,
2078 size_t bytes_len
2079)
Jim Ingham97a6dc72010-10-04 19:49:29 +00002080{
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002081 File &out_file = reader.GetDebugger().GetOutputFile();
Jim Ingham97a6dc72010-10-04 19:49:29 +00002082 bool *response_ptr = (bool *) baton;
2083
2084 switch (action)
2085 {
2086 case eInputReaderActivate:
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002087 if (out_file.IsValid())
Jim Ingham97a6dc72010-10-04 19:49:29 +00002088 {
2089 if (reader.GetPrompt())
Caroline Tice31f7d462011-02-02 01:17:56 +00002090 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002091 out_file.Printf ("%s", reader.GetPrompt());
2092 out_file.Flush ();
Caroline Tice31f7d462011-02-02 01:17:56 +00002093 }
Jim Ingham97a6dc72010-10-04 19:49:29 +00002094 }
2095 break;
2096
2097 case eInputReaderDeactivate:
2098 break;
2099
2100 case eInputReaderReactivate:
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002101 if (out_file.IsValid() && reader.GetPrompt())
Caroline Tice31f7d462011-02-02 01:17:56 +00002102 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002103 out_file.Printf ("%s", reader.GetPrompt());
2104 out_file.Flush ();
Caroline Tice31f7d462011-02-02 01:17:56 +00002105 }
Jim Ingham97a6dc72010-10-04 19:49:29 +00002106 break;
Caroline Tice969ed3d2011-05-02 20:41:46 +00002107
2108 case eInputReaderAsynchronousOutputWritten:
2109 break;
2110
Jim Ingham97a6dc72010-10-04 19:49:29 +00002111 case eInputReaderGotToken:
2112 if (bytes_len == 0)
2113 {
2114 reader.SetIsDone(true);
2115 }
Jim Inghamc8b47582011-11-14 20:02:01 +00002116 else if (bytes[0] == 'y' || bytes[0] == 'Y')
Jim Ingham97a6dc72010-10-04 19:49:29 +00002117 {
2118 *response_ptr = true;
2119 reader.SetIsDone(true);
2120 }
Jim Inghamc8b47582011-11-14 20:02:01 +00002121 else if (bytes[0] == 'n' || bytes[0] == 'N')
Jim Ingham97a6dc72010-10-04 19:49:29 +00002122 {
2123 *response_ptr = false;
2124 reader.SetIsDone(true);
2125 }
2126 else
2127 {
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002128 if (out_file.IsValid() && !reader.IsDone() && reader.GetPrompt())
Jim Ingham97a6dc72010-10-04 19:49:29 +00002129 {
Jim Ingham78d61482011-11-17 01:22:00 +00002130 out_file.Printf ("Please answer \"y\" or \"n\".\n%s", reader.GetPrompt());
Greg Clayton51b1e2d2011-02-09 01:08:52 +00002131 out_file.Flush ();
Jim Ingham97a6dc72010-10-04 19:49:29 +00002132 }
2133 }
2134 break;
2135
Caroline Ticeefed6132010-11-19 20:47:54 +00002136 case eInputReaderInterrupt:
2137 case eInputReaderEndOfFile:
2138 *response_ptr = false; // Assume ^C or ^D means cancel the proposed action
2139 reader.SetIsDone (true);
2140 break;
2141
Jim Ingham97a6dc72010-10-04 19:49:29 +00002142 case eInputReaderDone:
2143 break;
2144 }
2145
2146 return bytes_len;
2147
2148}
2149
2150bool
2151CommandInterpreter::Confirm (const char *message, bool default_answer)
2152{
Jim Ingham3bcdb292010-10-04 22:44:14 +00002153 // Check AutoConfirm first:
2154 if (m_debugger.GetAutoConfirm())
2155 return default_answer;
2156
Jim Ingham97a6dc72010-10-04 19:49:29 +00002157 InputReaderSP reader_sp (new InputReader(GetDebugger()));
2158 bool response = default_answer;
2159 if (reader_sp)
2160 {
2161 std::string prompt(message);
2162 prompt.append(": [");
2163 if (default_answer)
2164 prompt.append ("Y/n] ");
2165 else
2166 prompt.append ("y/N] ");
2167
2168 Error err (reader_sp->Initialize (CommandInterpreter::GetConfirmationInputReaderCallback,
2169 &response, // baton
2170 eInputReaderGranularityLine, // token size, to pass to callback function
2171 NULL, // end token
2172 prompt.c_str(), // prompt
2173 true)); // echo input
2174 if (err.Success())
2175 {
2176 GetDebugger().PushInputReader (reader_sp);
2177 }
2178 reader_sp->WaitOnReaderIsDone();
2179 }
2180 return response;
2181}
2182
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002183OptionArgVectorSP
2184CommandInterpreter::GetAliasOptions (const char *alias_name)
2185{
2186 OptionArgMap::iterator pos;
2187 OptionArgVectorSP ret_val;
2188
2189 std::string alias (alias_name);
2190
2191 if (HasAliasOptions())
2192 {
2193 pos = m_alias_options.find (alias);
2194 if (pos != m_alias_options.end())
2195 ret_val = pos->second;
2196 }
2197
2198 return ret_val;
2199}
2200
2201void
2202CommandInterpreter::RemoveAliasOptions (const char *alias_name)
2203{
2204 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
2205 if (pos != m_alias_options.end())
2206 {
2207 m_alias_options.erase (pos);
2208 }
2209}
2210
2211void
2212CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
2213{
2214 m_alias_options[alias_name] = option_arg_vector_sp;
2215}
2216
2217bool
2218CommandInterpreter::HasCommands ()
2219{
2220 return (!m_command_dict.empty());
2221}
2222
2223bool
2224CommandInterpreter::HasAliases ()
2225{
2226 return (!m_alias_dict.empty());
2227}
2228
2229bool
2230CommandInterpreter::HasUserCommands ()
2231{
2232 return (!m_user_dict.empty());
2233}
2234
2235bool
2236CommandInterpreter::HasAliasOptions ()
2237{
2238 return (!m_alias_options.empty());
2239}
2240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241void
Caroline Tice4ab31c92010-10-12 21:57:09 +00002242CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2243 const char *alias_name,
2244 Args &cmd_args,
Caroline Ticed9d63362010-12-07 19:58:26 +00002245 std::string &raw_input_string,
Caroline Tice4ab31c92010-10-12 21:57:09 +00002246 CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002247{
2248 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
Caroline Ticed9d63362010-12-07 19:58:26 +00002249
2250 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251
Caroline Ticed9d63362010-12-07 19:58:26 +00002252 // Make sure that the alias name is the 0th element in cmd_args
2253 std::string alias_name_str = alias_name;
2254 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2255 cmd_args.Unshift (alias_name);
2256
2257 Args new_args (alias_cmd_obj->GetCommandName());
2258 if (new_args.GetArgumentCount() == 2)
2259 new_args.Shift();
2260
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002261 if (option_arg_vector_sp.get())
2262 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002263 if (wants_raw_input)
2264 {
2265 // We have a command that both has command options and takes raw input. Make *sure* it has a
2266 // " -- " in the right place in the raw_input_string.
2267 size_t pos = raw_input_string.find(" -- ");
2268 if (pos == std::string::npos)
2269 {
2270 // None found; assume it goes at the beginning of the raw input string
2271 raw_input_string.insert (0, " -- ");
2272 }
2273 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274
2275 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00002276 const size_t old_size = cmd_args.GetArgumentCount();
Caroline Tice4ab31c92010-10-12 21:57:09 +00002277 std::vector<bool> used (old_size + 1, false);
2278
2279 used[0] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002280
Andy Gibbsa297a972013-06-19 19:04:53 +00002281 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002282 {
2283 OptionArgPair option_pair = (*option_arg_vector)[i];
Caroline Ticed9d63362010-12-07 19:58:26 +00002284 OptionArgValue value_pair = option_pair.second;
2285 int value_type = value_pair.first;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002286 std::string option = option_pair.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00002287 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002288 if (option.compare ("<argument>") == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002289 {
2290 if (!wants_raw_input
2291 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2292 new_args.AppendArgument (value.c_str());
2293 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002294 else
2295 {
Virgile Belloe2607b52013-09-05 16:42:23 +00002296 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002297 new_args.AppendArgument (option.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002298 if (value.compare ("<no-argument>") != 0)
2299 {
2300 int index = GetOptionArgumentPosition (value.c_str());
2301 if (index == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002302 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002303 // value was NOT a positional argument; must be a real value
Virgile Belloe2607b52013-09-05 16:42:23 +00002304 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002305 new_args.AppendArgument (value.c_str());
2306 else
2307 {
2308 char buffer[255];
2309 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2310 new_args.AppendArgument (buffer);
2311 }
2312
2313 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002314 else if (index >= cmd_args.GetArgumentCount())
2315 {
2316 result.AppendErrorWithFormat
2317 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2318 index);
2319 result.SetStatus (eReturnStatusFailed);
2320 return;
2321 }
2322 else
2323 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002324 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2325 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2326 if (strpos != std::string::npos)
2327 {
2328 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2329 }
2330
Virgile Belloe2607b52013-09-05 16:42:23 +00002331 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002332 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2333 else
2334 {
2335 char buffer[255];
2336 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2337 cmd_args.GetArgumentAtIndex (index));
2338 new_args.AppendArgument (buffer);
2339 }
Caroline Tice4ab31c92010-10-12 21:57:09 +00002340 used[index] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002341 }
2342 }
2343 }
2344 }
2345
Andy Gibbsa297a972013-06-19 19:04:53 +00002346 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002347 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002348 if (!used[j] && !wants_raw_input)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002349 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2350 }
2351
2352 cmd_args.Clear();
2353 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2354 }
2355 else
2356 {
2357 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Caroline Ticed9d63362010-12-07 19:58:26 +00002358 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2359 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2360 // input string.
2361 if (wants_raw_input)
2362 {
2363 cmd_args.Clear();
2364 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2365 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002366 return;
2367 }
2368
2369 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2370 return;
2371}
2372
2373
2374int
2375CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2376{
2377 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2378 // of zero.
2379
2380 char *cptr = (char *) in_string;
2381
2382 // Does it start with '%'
2383 if (cptr[0] == '%')
2384 {
2385 ++cptr;
2386
2387 // Is the rest of it entirely digits?
2388 if (isdigit (cptr[0]))
2389 {
2390 const char *start = cptr;
2391 while (isdigit (cptr[0]))
2392 ++cptr;
2393
2394 // We've gotten to the end of the digits; are we at the end of the string?
2395 if (cptr[0] == '\0')
2396 position = atoi (start);
2397 }
2398 }
2399
2400 return position;
2401}
2402
2403void
2404CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2405{
Jim Ingham16e0c682011-08-12 23:34:31 +00002406 FileSpec init_file;
Greg Clayton14a35512011-09-11 00:01:44 +00002407 if (in_cwd)
Jim Ingham16e0c682011-08-12 23:34:31 +00002408 {
Greg Clayton14a35512011-09-11 00:01:44 +00002409 // In the current working directory we don't load any program specific
2410 // .lldbinit files, we only look for a "./.lldbinit" file.
2411 if (m_skip_lldbinit_files)
2412 return;
2413
2414 init_file.SetFile ("./.lldbinit", true);
Jim Ingham16e0c682011-08-12 23:34:31 +00002415 }
Greg Clayton14a35512011-09-11 00:01:44 +00002416 else
Jim Ingham16e0c682011-08-12 23:34:31 +00002417 {
Greg Clayton14a35512011-09-11 00:01:44 +00002418 // If we aren't looking in the current working directory we are looking
2419 // in the home directory. We will first see if there is an application
2420 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2421 // "-" and the name of the program. If this file doesn't exist, we fall
2422 // back to just the "~/.lldbinit" file. We also obey any requests to not
2423 // load the init files.
2424 const char *init_file_path = "~/.lldbinit";
2425
2426 if (m_skip_app_init_files == false)
2427 {
2428 FileSpec program_file_spec (Host::GetProgramFileSpec());
2429 const char *program_name = program_file_spec.GetFilename().AsCString();
Jim Ingham16e0c682011-08-12 23:34:31 +00002430
Greg Clayton14a35512011-09-11 00:01:44 +00002431 if (program_name)
2432 {
2433 char program_init_file_name[PATH_MAX];
2434 ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path, program_name);
2435 init_file.SetFile (program_init_file_name, true);
2436 if (!init_file.Exists())
2437 init_file.Clear();
2438 }
2439 }
2440
2441 if (!init_file && !m_skip_lldbinit_files)
2442 init_file.SetFile (init_file_path, true);
2443 }
2444
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002445 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2446 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2447
2448 if (init_file.Exists())
2449 {
Jim Inghame16c50a2011-02-18 00:54:25 +00002450 ExecutionContext *exe_ctx = NULL; // We don't have any context yet.
2451 bool stop_on_continue = true;
2452 bool stop_on_error = false;
2453 bool echo_commands = false;
2454 bool print_results = false;
2455
Enrico Granata5f5ab602012-05-31 01:09:06 +00002456 HandleCommandsFromFile (init_file, exe_ctx, stop_on_continue, stop_on_error, echo_commands, print_results, eLazyBoolNo, result);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002457 }
2458 else
2459 {
2460 // nothing to be done if the file doesn't exist
2461 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2462 }
2463}
2464
Greg Clayton8b82f082011-04-12 05:54:46 +00002465PlatformSP
2466CommandInterpreter::GetPlatform (bool prefer_target_platform)
2467{
2468 PlatformSP platform_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +00002469 if (prefer_target_platform)
2470 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002471 ExecutionContext exe_ctx(GetExecutionContext());
2472 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002473 if (target)
2474 platform_sp = target->GetPlatform();
2475 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002476
2477 if (!platform_sp)
2478 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2479 return platform_sp;
2480}
2481
Jim Inghame16c50a2011-02-18 00:54:25 +00002482void
Jim Inghambad87fe2011-03-11 01:51:49 +00002483CommandInterpreter::HandleCommands (const StringList &commands,
Jim Inghame16c50a2011-02-18 00:54:25 +00002484 ExecutionContext *override_context,
2485 bool stop_on_continue,
2486 bool stop_on_error,
2487 bool echo_commands,
2488 bool print_results,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002489 LazyBool add_to_history,
Jim Inghame16c50a2011-02-18 00:54:25 +00002490 CommandReturnObject &result)
2491{
2492 size_t num_lines = commands.GetSize();
Jim Inghame16c50a2011-02-18 00:54:25 +00002493
2494 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2495 // Make sure you reset this value anywhere you return from the function.
2496
2497 bool old_async_execution = m_debugger.GetAsyncExecution();
2498
2499 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2500 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2501
2502 if (override_context != NULL)
Greg Clayton8b82f082011-04-12 05:54:46 +00002503 UpdateExecutionContext (override_context);
Jim Inghame16c50a2011-02-18 00:54:25 +00002504
2505 if (!stop_on_continue)
2506 {
2507 m_debugger.SetAsyncExecution (false);
2508 }
2509
Andy Gibbsa297a972013-06-19 19:04:53 +00002510 for (size_t idx = 0; idx < num_lines; idx++)
Jim Inghame16c50a2011-02-18 00:54:25 +00002511 {
2512 const char *cmd = commands.GetStringAtIndex(idx);
2513 if (cmd[0] == '\0')
2514 continue;
2515
Jim Inghame16c50a2011-02-18 00:54:25 +00002516 if (echo_commands)
2517 {
2518 result.AppendMessageWithFormat ("%s %s\n",
2519 GetPrompt(),
2520 cmd);
2521 }
2522
Greg Clayton9d0402b2011-02-20 02:15:07 +00002523 CommandReturnObject tmp_result;
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002524 // If override_context is not NULL, pass no_context_switching = true for
2525 // HandleCommand() since we updated our context already.
Jim Ingham076b7fc2013-05-02 23:15:37 +00002526
2527 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2528 // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here.
2529 if (!add_to_history)
2530 m_command_source_depth++;
Enrico Granata5f5ab602012-05-31 01:09:06 +00002531 bool success = HandleCommand(cmd, add_to_history, tmp_result,
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002532 NULL, /* override_context */
2533 true, /* repeat_on_empty_command */
2534 override_context != NULL /* no_context_switching */);
Jim Ingham076b7fc2013-05-02 23:15:37 +00002535 if (!add_to_history)
2536 m_command_source_depth--;
Jim Inghame16c50a2011-02-18 00:54:25 +00002537
2538 if (print_results)
2539 {
2540 if (tmp_result.Succeeded())
Jim Ingham85e8b812011-02-19 02:53:09 +00002541 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
Jim Inghame16c50a2011-02-18 00:54:25 +00002542 }
2543
2544 if (!success || !tmp_result.Succeeded())
2545 {
Jim Inghama5038812012-04-24 02:25:07 +00002546 const char *error_msg = tmp_result.GetErrorData();
2547 if (error_msg == NULL || error_msg[0] == '\0')
2548 error_msg = "<unknown error>.\n";
Jim Inghame16c50a2011-02-18 00:54:25 +00002549 if (stop_on_error)
2550 {
Andy Gibbsa297a972013-06-19 19:04:53 +00002551 result.AppendErrorWithFormat("Aborting reading of commands after command #%zu: '%s' failed with %s",
Jim Inghama5038812012-04-24 02:25:07 +00002552 idx, cmd, error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002553 result.SetStatus (eReturnStatusFailed);
2554 m_debugger.SetAsyncExecution (old_async_execution);
2555 return;
2556 }
2557 else if (print_results)
2558 {
Andy Gibbsa297a972013-06-19 19:04:53 +00002559 result.AppendMessageWithFormat ("Command #%zu '%s' failed with %s",
Jim Inghame16c50a2011-02-18 00:54:25 +00002560 idx + 1,
2561 cmd,
Jim Inghama5038812012-04-24 02:25:07 +00002562 error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002563 }
2564 }
2565
Caroline Tice969ed3d2011-05-02 20:41:46 +00002566 if (result.GetImmediateOutputStream())
2567 result.GetImmediateOutputStream()->Flush();
2568
2569 if (result.GetImmediateErrorStream())
2570 result.GetImmediateErrorStream()->Flush();
2571
Jim Inghame16c50a2011-02-18 00:54:25 +00002572 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2573 // could be running (for instance in Breakpoint Commands.
2574 // So we check the return value to see if it is has running in it.
2575 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2576 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2577 {
2578 if (stop_on_continue)
2579 {
2580 // If we caused the target to proceed, and we're going to stop in that case, set the
2581 // status in our real result before returning. This is an error if the continue was not the
2582 // last command in the set of commands to be run.
2583 if (idx != num_lines - 1)
Andy Gibbsa297a972013-06-19 19:04:53 +00002584 result.AppendErrorWithFormat("Aborting reading of commands after command #%zu: '%s' continued the target.\n",
Jim Inghame16c50a2011-02-18 00:54:25 +00002585 idx + 1, cmd);
2586 else
Andy Gibbsa297a972013-06-19 19:04:53 +00002587 result.AppendMessageWithFormat ("Command #%zu '%s' continued the target.\n", idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002588
2589 result.SetStatus(tmp_result.GetStatus());
2590 m_debugger.SetAsyncExecution (old_async_execution);
2591
2592 return;
2593 }
2594 }
2595
2596 }
2597
2598 result.SetStatus (eReturnStatusSuccessFinishResult);
2599 m_debugger.SetAsyncExecution (old_async_execution);
2600
2601 return;
2602}
2603
2604void
2605CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2606 ExecutionContext *context,
2607 bool stop_on_continue,
2608 bool stop_on_error,
2609 bool echo_command,
2610 bool print_result,
Enrico Granata5f5ab602012-05-31 01:09:06 +00002611 LazyBool add_to_history,
Jim Inghame16c50a2011-02-18 00:54:25 +00002612 CommandReturnObject &result)
2613{
2614 if (cmd_file.Exists())
2615 {
2616 bool success;
2617 StringList commands;
2618 success = commands.ReadFileLines(cmd_file);
2619 if (!success)
2620 {
2621 result.AppendErrorWithFormat ("Error reading commands from file: %s.\n", cmd_file.GetFilename().AsCString());
2622 result.SetStatus (eReturnStatusFailed);
2623 return;
2624 }
Enrico Granata5f5ab602012-05-31 01:09:06 +00002625 m_command_source_depth++;
2626 HandleCommands (commands, context, stop_on_continue, stop_on_error, echo_command, print_result, add_to_history, result);
2627 m_command_source_depth--;
Jim Inghame16c50a2011-02-18 00:54:25 +00002628 }
2629 else
2630 {
2631 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
2632 cmd_file.GetFilename().AsCString());
2633 result.SetStatus (eReturnStatusFailed);
2634 return;
2635 }
2636}
2637
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002638ScriptInterpreter *
Enrico Granatab5887262012-10-29 21:18:03 +00002639CommandInterpreter::GetScriptInterpreter (bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002640{
Enrico Granatab5887262012-10-29 21:18:03 +00002641 if (m_script_interpreter_ap.get() != NULL)
2642 return m_script_interpreter_ap.get();
2643
2644 if (!can_create)
2645 return NULL;
2646
Enrico Granataa29bdad2012-07-10 18:23:48 +00002647 // <rdar://problem/11751427>
2648 // we need to protect the initialization of the script interpreter
2649 // otherwise we could end up with two threads both trying to create
2650 // their instance of it, and for some languages (e.g. Python)
2651 // this is a bulletproof recipe for disaster!
2652 // this needs to be a function-level static because multiple Debugger instances living in the same process
2653 // still need to be isolated and not try to initialize Python concurrently
Enrico Granata8b95df22012-07-10 19:04:14 +00002654 static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive);
2655 Mutex::Locker interpreter_lock(g_interpreter_mutex);
Enrico Granataa29bdad2012-07-10 18:23:48 +00002656
Greg Clayton5160ce52013-03-27 23:08:40 +00002657 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Enrico Granatab5887262012-10-29 21:18:03 +00002658 if (log)
2659 log->Printf("Initializing the ScriptInterpreter now\n");
Greg Clayton66111032010-06-23 01:19:29 +00002660
Caroline Tice2f88aad2011-01-14 00:29:16 +00002661 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2662 switch (script_lang)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002663 {
Greg Claytondce502e2011-11-04 03:34:56 +00002664 case eScriptLanguagePython:
2665#ifndef LLDB_DISABLE_PYTHON
2666 m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this));
2667 break;
2668#else
2669 // Fall through to the None case when python is disabled
2670#endif
Caroline Tice2f88aad2011-01-14 00:29:16 +00002671 case eScriptLanguageNone:
2672 m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this));
2673 break;
Caroline Tice2f88aad2011-01-14 00:29:16 +00002674 };
2675
2676 return m_script_interpreter_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002677}
2678
2679
2680
2681bool
2682CommandInterpreter::GetSynchronous ()
2683{
2684 return m_synchronous_execution;
2685}
2686
2687void
2688CommandInterpreter::SetSynchronous (bool value)
2689{
Johnny Chenc066ab42010-10-14 01:22:03 +00002690 m_synchronous_execution = value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002691}
2692
2693void
2694CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2695 const char *word_text,
2696 const char *separator,
2697 const char *help_text,
Greg Claytonc7bece562013-01-25 18:06:21 +00002698 size_t max_word_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002699{
Greg Claytona7015092010-09-18 01:14:36 +00002700 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2701
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002702 int indent_size = max_word_len + strlen (separator) + 2;
2703
2704 strm.IndentMore (indent_size);
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002705
2706 StreamString text_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +00002707 text_strm.Printf ("%-*s %s %s", (int)max_word_len, word_text, separator, help_text);
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002708
2709 size_t len = text_strm.GetSize();
2710 const char *text = text_strm.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002711 if (text[len - 1] == '\n')
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002712 {
2713 text_strm.EOL();
2714 len = text_strm.GetSize();
2715 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002716
2717 if (len < max_columns)
2718 {
2719 // Output it as a single line.
2720 strm.Printf ("%s", text);
2721 }
2722 else
2723 {
2724 // We need to break it up into multiple lines.
2725 bool first_line = true;
2726 int text_width;
Greg Claytonc7bece562013-01-25 18:06:21 +00002727 size_t start = 0;
2728 size_t end = start;
2729 const size_t final_end = strlen (text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002730
2731 while (end < final_end)
2732 {
2733 if (first_line)
2734 text_width = max_columns - 1;
2735 else
2736 text_width = max_columns - indent_size - 1;
2737
2738 // Don't start the 'text' on a space, since we're already outputting the indentation.
2739 if (!first_line)
2740 {
2741 while ((start < final_end) && (text[start] == ' '))
2742 start++;
2743 }
2744
2745 end = start + text_width;
2746 if (end > final_end)
2747 end = final_end;
2748 else
2749 {
2750 // If we're not at the end of the text, make sure we break the line on white space.
2751 while (end > start
2752 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
2753 end--;
Greg Clayton67cc0632012-08-22 17:17:09 +00002754 assert (end > 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002755 }
2756
Greg Claytonc7bece562013-01-25 18:06:21 +00002757 const size_t sub_len = end - start;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002758 if (start != 0)
2759 strm.EOL();
2760 if (!first_line)
2761 strm.Indent();
2762 else
2763 first_line = false;
2764 assert (start <= final_end);
2765 assert (start + sub_len <= final_end);
2766 if (sub_len > 0)
2767 strm.Write (text + start, sub_len);
2768 start = end + 1;
2769 }
2770 }
2771 strm.EOL();
2772 strm.IndentLess(indent_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002773}
2774
2775void
Enrico Granata82a7d982011-07-07 00:38:40 +00002776CommandInterpreter::OutputHelpText (Stream &strm,
2777 const char *word_text,
2778 const char *separator,
2779 const char *help_text,
2780 uint32_t max_word_len)
2781{
2782 int indent_size = max_word_len + strlen (separator) + 2;
2783
2784 strm.IndentMore (indent_size);
2785
2786 StreamString text_strm;
2787 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2788
2789 const uint32_t max_columns = m_debugger.GetTerminalWidth();
Enrico Granata82a7d982011-07-07 00:38:40 +00002790
2791 size_t len = text_strm.GetSize();
2792 const char *text = text_strm.GetData();
2793
2794 uint32_t chars_left = max_columns;
2795
2796 for (uint32_t i = 0; i < len; i++)
2797 {
2798 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
2799 {
Enrico Granata82a7d982011-07-07 00:38:40 +00002800 chars_left = max_columns - indent_size;
2801 strm.EOL();
2802 strm.Indent();
2803 }
2804 else
2805 {
2806 strm.PutChar(text[i]);
2807 chars_left--;
2808 }
2809
2810 }
2811
2812 strm.EOL();
2813 strm.IndentLess(indent_size);
2814}
2815
2816void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002817CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
Jim Inghamaf3753e2013-05-17 01:30:37 +00002818 StringList &commands_help, bool search_builtin_commands, bool search_user_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002819{
2820 CommandObject::CommandMap::const_iterator pos;
2821
Jim Inghamaf3753e2013-05-17 01:30:37 +00002822 if (search_builtin_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002823 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002824 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002825 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002826 const char *command_name = pos->first.c_str();
2827 CommandObject *cmd_obj = pos->second.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002828
Jim Inghamaf3753e2013-05-17 01:30:37 +00002829 if (cmd_obj->HelpTextContainsWord (search_word))
2830 {
2831 commands_found.AppendString (command_name);
2832 commands_help.AppendString (cmd_obj->GetHelp());
2833 }
2834
2835 if (cmd_obj->IsMultiwordObject())
2836 cmd_obj->AproposAllSubCommands (command_name,
2837 search_word,
2838 commands_found,
2839 commands_help);
2840
2841 }
2842 }
2843
2844 if (search_user_commands)
2845 {
2846 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
2847 {
2848 const char *command_name = pos->first.c_str();
2849 CommandObject *cmd_obj = pos->second.get();
2850
2851 if (cmd_obj->HelpTextContainsWord (search_word))
2852 {
2853 commands_found.AppendString (command_name);
2854 commands_help.AppendString (cmd_obj->GetHelp());
2855 }
2856
2857 if (cmd_obj->IsMultiwordObject())
2858 cmd_obj->AproposAllSubCommands (command_name,
2859 search_word,
2860 commands_found,
2861 commands_help);
2862
2863 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002864 }
2865}
Greg Clayton8b82f082011-04-12 05:54:46 +00002866
2867
2868void
2869CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
2870{
Greg Clayton8b82f082011-04-12 05:54:46 +00002871 if (override_context != NULL)
2872 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002873 m_exe_ctx_ref = *override_context;
Greg Clayton8b82f082011-04-12 05:54:46 +00002874 }
2875 else
2876 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002877 const bool adopt_selected = true;
2878 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
Greg Clayton8b82f082011-04-12 05:54:46 +00002879 }
2880}