blob: c4849d03151a7c9b5b4fb4d39f24e2e4de48a35f [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"
Greg Clayton44d93782014-01-27 23:43:24 +000025#include "../Commands/CommandObjectGUI.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000026#include "../Commands/CommandObjectHelp.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000027#include "../Commands/CommandObjectLog.h"
28#include "../Commands/CommandObjectMemory.h"
Greg Claytonded470d2011-03-19 01:12:21 +000029#include "../Commands/CommandObjectPlatform.h"
Enrico Granata21dfcd92012-09-28 23:57:51 +000030#include "../Commands/CommandObjectPlugin.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000031#include "../Commands/CommandObjectProcess.h"
32#include "../Commands/CommandObjectQuit.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000033#include "../Commands/CommandObjectRegister.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000034#include "../Commands/CommandObjectSettings.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000035#include "../Commands/CommandObjectSource.h"
Jim Inghamebc09c32010-07-07 03:36:20 +000036#include "../Commands/CommandObjectCommands.h"
Eli Friedman3afb70c2010-06-13 02:17:17 +000037#include "../Commands/CommandObjectSyntax.h"
38#include "../Commands/CommandObjectTarget.h"
39#include "../Commands/CommandObjectThread.h"
Greg Clayton4a33d312011-06-23 17:59:56 +000040#include "../Commands/CommandObjectType.h"
Johnny Chen31c39da2010-12-23 20:21:44 +000041#include "../Commands/CommandObjectVersion.h"
Johnny Chenf04ee932011-09-22 18:04:58 +000042#include "../Commands/CommandObjectWatchpoint.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Greg Clayton7d2ef162013-03-29 17:03:23 +000044
Chris Lattner30fdc8d2010-06-08 16:52:24 +000045#include "lldb/Core/Debugger.h"
Enrico Granatab5887262012-10-29 21:18:03 +000046#include "lldb/Core/Log.h"
Greg Claytonf0066ad2014-05-02 00:45:31 +000047#include "lldb/Core/State.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048#include "lldb/Core/Stream.h"
Greg Clayton44d93782014-01-27 23:43:24 +000049#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000050#include "lldb/Core/Timer.h"
Enrico Granatab5887262012-10-29 21:18:03 +000051
Todd Fialacacde7d2014-09-27 16:54:22 +000052#ifndef LLDB_DISABLE_LIBEDIT
Greg Clayton44d93782014-01-27 23:43:24 +000053#include "lldb/Host/Editline.h"
Todd Fialacacde7d2014-09-27 16:54:22 +000054#endif
Greg Clayton7fb56d02011-02-01 01:31:41 +000055#include "lldb/Host/Host.h"
Zachary Turnera21fee02014-08-21 21:49:24 +000056#include "lldb/Host/HostInfo.h"
Enrico Granatab5887262012-10-29 21:18:03 +000057
58#include "lldb/Interpreter/Args.h"
Greg Clayton7d2ef162013-03-29 17:03:23 +000059#include "lldb/Interpreter/CommandCompletions.h"
Enrico Granatab5887262012-10-29 21:18:03 +000060#include "lldb/Interpreter/CommandInterpreter.h"
Greg Clayton7d2ef162013-03-29 17:03:23 +000061#include "lldb/Interpreter/CommandReturnObject.h"
Enrico Granatab5887262012-10-29 21:18:03 +000062#include "lldb/Interpreter/Options.h"
Zachary Turner633a29c2015-03-04 01:58:01 +000063#include "lldb/Interpreter/OptionValueProperties.h"
64#include "lldb/Interpreter/Property.h"
Enrico Granatab5887262012-10-29 21:18:03 +000065#include "lldb/Interpreter/ScriptInterpreterNone.h"
66#include "lldb/Interpreter/ScriptInterpreterPython.h"
67
68
Chris Lattner30fdc8d2010-06-08 16:52:24 +000069#include "lldb/Target/Process.h"
70#include "lldb/Target/Thread.h"
71#include "lldb/Target/TargetList.h"
72
Enrico Granatab5887262012-10-29 21:18:03 +000073#include "lldb/Utility/CleanUp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000074
Zachary Turnera21fee02014-08-21 21:49:24 +000075#include "llvm/ADT/SmallString.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000076#include "llvm/ADT/STLExtras.h"
Zachary Turnera21fee02014-08-21 21:49:24 +000077#include "llvm/Support/Path.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000078
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079using namespace lldb;
80using namespace lldb_private;
81
Greg Clayton754a9362012-08-23 00:22:02 +000082
83static PropertyDefinition
84g_properties[] =
85{
Ed Masted78c9572014-04-20 00:31:37 +000086 { "expand-regex-aliases", OptionValue::eTypeBoolean, true, false, nullptr, nullptr, "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." },
87 { "prompt-on-quit", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "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." },
88 { "stop-command-source-on-error", OptionValue::eTypeBoolean, true, true, nullptr, nullptr, "If true, LLDB will stop running a 'command source' script upon encountering an error." },
89 { nullptr , OptionValue::eTypeInvalid, true, 0 , nullptr, nullptr, nullptr }
Greg Clayton754a9362012-08-23 00:22:02 +000090};
91
92enum
93{
Enrico Granatabcba2b22013-01-17 21:36:19 +000094 ePropertyExpandRegexAliases = 0,
Enrico Granata012d4fc2013-06-11 01:26:35 +000095 ePropertyPromptOnQuit = 1,
96 ePropertyStopCmdSourceOnError = 2
Greg Clayton754a9362012-08-23 00:22:02 +000097};
98
Jim Ingham4bddaeb2012-02-16 06:50:00 +000099ConstString &
100CommandInterpreter::GetStaticBroadcasterClass ()
101{
102 static ConstString class_name ("lldb.commandInterpreter");
103 return class_name;
104}
105
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000106CommandInterpreter::CommandInterpreter
107(
Greg Clayton66111032010-06-23 01:19:29 +0000108 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 ScriptLanguage script_language,
Greg Clayton66111032010-06-23 01:19:29 +0000110 bool synchronous_execution
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111) :
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000112 Broadcaster (&debugger, "lldb.command-interpreter"),
Greg Clayton754a9362012-08-23 00:22:02 +0000113 Properties(OptionValuePropertiesSP(new OptionValueProperties(ConstString("interpreter")))),
Greg Clayton44d93782014-01-27 23:43:24 +0000114 IOHandlerDelegate (IOHandlerDelegate::Completion::LLDBCommand),
Greg Clayton66111032010-06-23 01:19:29 +0000115 m_debugger (debugger),
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000116 m_synchronous_execution (synchronous_execution),
Caroline Tice2f88aad2011-01-14 00:29:16 +0000117 m_skip_lldbinit_files (false),
Jim Ingham16e0c682011-08-12 23:34:31 +0000118 m_skip_app_init_files (false),
Jim Inghame16c50a2011-02-18 00:54:25 +0000119 m_script_interpreter_ap (),
Greg Clayton44d93782014-01-27 23:43:24 +0000120 m_command_io_handler_sp (),
Caroline Ticed61c10b2011-06-16 16:27:19 +0000121 m_comment_char ('#'),
Johnny Chen4ac1d9e2012-08-09 22:06:10 +0000122 m_batch_command_mode (false),
Enrico Granata5f5ab602012-05-31 01:09:06 +0000123 m_truncation_warning(eNoTruncation),
Jim Ingham26c7bf92014-10-11 00:38:27 +0000124 m_command_source_depth (0),
125 m_num_errors(0),
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000126 m_quit_requested(false),
127 m_stopped_for_crash(false)
Jim Ingham26c7bf92014-10-11 00:38:27 +0000128
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000129{
Greg Clayton67cc0632012-08-22 17:17:09 +0000130 debugger.SetScriptLanguage (script_language);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000131 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
132 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
Greg Clayton67cc0632012-08-22 17:17:09 +0000133 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000134 CheckInWithManager ();
Greg Clayton754a9362012-08-23 00:22:02 +0000135 m_collection_sp->Initialize (g_properties);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136}
137
Greg Clayton754a9362012-08-23 00:22:02 +0000138bool
139CommandInterpreter::GetExpandRegexAliases () const
140{
141 const uint32_t idx = ePropertyExpandRegexAliases;
Ed Masted78c9572014-04-20 00:31:37 +0000142 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton754a9362012-08-23 00:22:02 +0000143}
144
Enrico Granatabcba2b22013-01-17 21:36:19 +0000145bool
146CommandInterpreter::GetPromptOnQuit () const
147{
148 const uint32_t idx = ePropertyPromptOnQuit;
Ed Masted78c9572014-04-20 00:31:37 +0000149 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granatabcba2b22013-01-17 21:36:19 +0000150}
Greg Clayton754a9362012-08-23 00:22:02 +0000151
Enrico Granata012d4fc2013-06-11 01:26:35 +0000152bool
153CommandInterpreter::GetStopCmdSourceOnError () const
154{
155 const uint32_t idx = ePropertyStopCmdSourceOnError;
Ed Masted78c9572014-04-20 00:31:37 +0000156 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granata012d4fc2013-06-11 01:26:35 +0000157}
158
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159void
160CommandInterpreter::Initialize ()
161{
162 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
163
164 CommandReturnObject result;
165
166 LoadCommandDictionary ();
167
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168 // Set up some initial aliases.
Caroline Ticeca90c472011-05-06 21:37:15 +0000169 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
170 if (cmd_obj_sp)
171 {
172 AddAlias ("q", cmd_obj_sp);
173 AddAlias ("exit", cmd_obj_sp);
174 }
Sean Callanan247e62a2012-05-04 23:15:02 +0000175
Johnny Chen6d675242012-08-24 18:15:45 +0000176 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
Sean Callanan247e62a2012-05-04 23:15:02 +0000177 if (cmd_obj_sp)
178 {
179 AddAlias ("attach", cmd_obj_sp);
180 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000181
Johnny Chen6d675242012-08-24 18:15:45 +0000182 cmd_obj_sp = GetCommandSPExact ("process detach",false);
183 if (cmd_obj_sp)
184 {
185 AddAlias ("detach", cmd_obj_sp);
186 }
187
Caroline Ticeca90c472011-05-06 21:37:15 +0000188 cmd_obj_sp = GetCommandSPExact ("process continue", false);
189 if (cmd_obj_sp)
190 {
191 AddAlias ("c", cmd_obj_sp);
192 AddAlias ("continue", cmd_obj_sp);
193 }
194
195 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
196 if (cmd_obj_sp)
197 AddAlias ("b", cmd_obj_sp);
198
Jim Inghamca36cd12012-10-05 19:16:31 +0000199 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
200 if (cmd_obj_sp)
201 AddAlias ("tbreak", cmd_obj_sp);
202
Caroline Ticeca90c472011-05-06 21:37:15 +0000203 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
204 if (cmd_obj_sp)
Jason Molendaf385f122011-10-22 00:47:41 +0000205 {
206 AddAlias ("stepi", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000207 AddAlias ("si", cmd_obj_sp);
Jason Molendaf385f122011-10-22 00:47:41 +0000208 }
209
210 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
211 if (cmd_obj_sp)
212 {
213 AddAlias ("nexti", cmd_obj_sp);
214 AddAlias ("ni", cmd_obj_sp);
215 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000216
217 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
218 if (cmd_obj_sp)
219 {
220 AddAlias ("s", cmd_obj_sp);
221 AddAlias ("step", cmd_obj_sp);
222 }
223
224 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
225 if (cmd_obj_sp)
226 {
227 AddAlias ("n", cmd_obj_sp);
228 AddAlias ("next", cmd_obj_sp);
229 }
230
231 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
232 if (cmd_obj_sp)
233 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000234 AddAlias ("finish", cmd_obj_sp);
235 }
236
Jim Ingham6d6d1072011-12-02 01:12:59 +0000237 cmd_obj_sp = GetCommandSPExact ("frame select", false);
238 if (cmd_obj_sp)
239 {
240 AddAlias ("f", cmd_obj_sp);
241 }
242
Jim Inghamca36cd12012-10-05 19:16:31 +0000243 cmd_obj_sp = GetCommandSPExact ("thread select", false);
244 if (cmd_obj_sp)
245 {
246 AddAlias ("t", cmd_obj_sp);
247 }
248
Richard Mittonf86248d2013-09-12 02:20:34 +0000249 cmd_obj_sp = GetCommandSPExact ("_regexp-jump",false);
250 if (cmd_obj_sp)
251 {
252 AddAlias ("j", cmd_obj_sp);
253 AddAlias ("jump", cmd_obj_sp);
254 }
255
Greg Clayton6bade322013-02-01 23:33:03 +0000256 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
Caroline Ticeca90c472011-05-06 21:37:15 +0000257 if (cmd_obj_sp)
258 {
259 AddAlias ("l", cmd_obj_sp);
260 AddAlias ("list", cmd_obj_sp);
261 }
262
Greg Claytonef5651d2013-02-12 18:52:24 +0000263 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
264 if (cmd_obj_sp)
265 {
266 AddAlias ("env", cmd_obj_sp);
267 }
268
Caroline Ticeca90c472011-05-06 21:37:15 +0000269 cmd_obj_sp = GetCommandSPExact ("memory read", false);
270 if (cmd_obj_sp)
271 AddAlias ("x", cmd_obj_sp);
272
273 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
274 if (cmd_obj_sp)
275 AddAlias ("up", cmd_obj_sp);
276
277 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
278 if (cmd_obj_sp)
279 AddAlias ("down", cmd_obj_sp);
280
Jason Molenda0c8e0062011-10-25 02:11:20 +0000281 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000282 if (cmd_obj_sp)
283 AddAlias ("display", cmd_obj_sp);
Jim Ingham7e18e422011-10-24 18:37:00 +0000284
285 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
286 if (cmd_obj_sp)
287 AddAlias ("dis", cmd_obj_sp);
288
289 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
290 if (cmd_obj_sp)
291 AddAlias ("di", cmd_obj_sp);
292
293
Jason Molendabc7748b2011-10-22 01:30:52 +0000294
Jason Molenda0c8e0062011-10-25 02:11:20 +0000295 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000296 if (cmd_obj_sp)
297 AddAlias ("undisplay", cmd_obj_sp);
298
Jim Ingham71bf2992012-10-10 16:51:31 +0000299 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
300 if (cmd_obj_sp)
301 AddAlias ("bt", cmd_obj_sp);
302
Caroline Ticeca90c472011-05-06 21:37:15 +0000303 cmd_obj_sp = GetCommandSPExact ("target create", false);
304 if (cmd_obj_sp)
305 AddAlias ("file", cmd_obj_sp);
306
307 cmd_obj_sp = GetCommandSPExact ("target modules", false);
308 if (cmd_obj_sp)
309 AddAlias ("image", cmd_obj_sp);
310
311
312 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
Jim Inghamffba2292011-03-22 02:29:32 +0000313
Caroline Ticeca90c472011-05-06 21:37:15 +0000314 cmd_obj_sp = GetCommandSPExact ("expression", false);
315 if (cmd_obj_sp)
Sean Callanan90e579f2013-04-17 17:23:58 +0000316 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000317 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
318 AddAlias ("p", cmd_obj_sp);
319 AddAlias ("print", cmd_obj_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000320 AddAlias ("call", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000321 AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp);
322 AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000323 AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000324
325 alias_arguments_vector_sp.reset (new OptionArgVector);
Greg Clayton9e57dcd2013-05-15 01:03:08 +0000326 ProcessAliasOptionsArgs (cmd_obj_sp, "-O -- ", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000327 AddAlias ("po", cmd_obj_sp);
328 AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp);
329 }
330
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000331 cmd_obj_sp = GetCommandSPExact ("process kill", false);
332 if (cmd_obj_sp)
Greg Claytone86fd742012-09-27 00:02:27 +0000333 {
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000334 AddAlias ("kill", cmd_obj_sp);
Greg Claytone86fd742012-09-27 00:02:27 +0000335 }
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000336
Caroline Ticeca90c472011-05-06 21:37:15 +0000337 cmd_obj_sp = GetCommandSPExact ("process launch", false);
338 if (cmd_obj_sp)
339 {
340 alias_arguments_vector_sp.reset (new OptionArgVector);
Todd Fiala013434e2014-07-09 01:29:05 +0000341#if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
Jason Molenda85da3122012-07-06 02:46:23 +0000342 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
343#else
Zachary Turner10687b02014-10-20 17:46:43 +0000344 std::string shell_option;
345 shell_option.append("--shell=");
346 shell_option.append(HostInfo::GetDefaultShell().GetPath());
347 shell_option.append(" --");
348 ProcessAliasOptionsArgs (cmd_obj_sp, shell_option.c_str(), alias_arguments_vector_sp);
Jason Molenda85da3122012-07-06 02:46:23 +0000349#endif
Caroline Ticeca90c472011-05-06 21:37:15 +0000350 AddAlias ("r", cmd_obj_sp);
351 AddAlias ("run", cmd_obj_sp);
352 AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp);
353 AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp);
354 }
Greg Clayton843d62d2012-03-29 21:47:51 +0000355
356 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
357 if (cmd_obj_sp)
358 {
359 AddAlias ("add-dsym", cmd_obj_sp);
360 }
Sean Callananfc732752012-05-21 18:25:19 +0000361
362 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
363 if (cmd_obj_sp)
364 {
365 alias_arguments_vector_sp.reset (new OptionArgVector);
366 ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp);
Jim Ingham06d282d2012-10-18 23:24:12 +0000367 AddAlias ("rbreak", cmd_obj_sp);
368 AddOrReplaceAliasOptions("rbreak", alias_arguments_vector_sp);
Sean Callananfc732752012-05-21 18:25:19 +0000369 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370}
371
Greg Clayton0c4129f2014-04-25 00:35:14 +0000372void
373CommandInterpreter::Clear()
374{
375 m_command_io_handler_sp.reset();
Greg Claytoned6499f2014-04-25 23:55:12 +0000376
377 if (m_script_interpreter_ap)
378 m_script_interpreter_ap->Clear();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000379}
380
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000381const char *
382CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
383{
384 // This function has not yet been implemented.
385
386 // Look for any embedded script command
387 // If found,
388 // get interpreter object from the command dictionary,
389 // call execute_one_command on it,
390 // get the results as a string,
391 // substitute that string for current stuff.
392
393 return arg;
394}
395
396
397void
398CommandInterpreter::LoadCommandDictionary ()
399{
400 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
401
Caroline Ticedaccaa92010-09-20 20:44:43 +0000402 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000403
Greg Claytona7015092010-09-18 01:14:36 +0000404 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000405 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Johnny Chenb89982d2011-04-21 00:39:18 +0000406 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000407 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
408 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000409 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton44d93782014-01-27 23:43:24 +0000410 m_command_dict["gui"] = CommandObjectSP (new CommandObjectGUI (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000411 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000412 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
413 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
Greg Claytonded470d2011-03-19 01:12:21 +0000414 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000415 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000416 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000417 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000418 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000419 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000420 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Inghamebc09c32010-07-07 03:36:20 +0000421 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000422 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
423 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Enrico Granata223383e2011-08-16 23:24:13 +0000424 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
Johnny Chen31c39da2010-12-23 20:21:44 +0000425 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
Johnny Chenf04ee932011-09-22 18:04:58 +0000426 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427
Jim Inghamca36cd12012-10-05 19:16:31 +0000428 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
Greg Claytond90ac932014-12-01 22:34:03 +0000429 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000430 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
Greg Clayton722e8852013-02-08 02:54:24 +0000431 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
Greg Clayton1b3815c2013-01-30 00:18:29 +0000432 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000433 {"^(-.*)$", "breakpoint set %1"},
434 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
Greg Clayton722e8852013-02-08 02:54:24 +0000435 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
Greg Clayton2d4b5122014-08-14 17:58:33 +0000436 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"}};
Jim Inghamca36cd12012-10-05 19:16:31 +0000437
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000438 size_t num_regexes = llvm::array_lengthof(break_regexes);
Jim Inghamca36cd12012-10-05 19:16:31 +0000439
Greg Clayton7b0992d2013-04-18 22:45:39 +0000440 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytona7015092010-09-18 01:14:36 +0000441 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000442 "_regexp-break",
Johnny Chenb417dcd2012-08-23 00:32:22 +0000443 "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 +0000444 "_regexp-break [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
445 2,
446 CommandCompletions::eSymbolCompletion |
Greg Claytonb5472782015-01-09 19:08:20 +0000447 CommandCompletions::eSourceFileCompletion,
448 false));
Jim Inghamca36cd12012-10-05 19:16:31 +0000449
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 if (break_regex_cmd_ap.get())
451 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000452 bool success = true;
453 for (size_t i = 0; i < num_regexes; i++)
454 {
455 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
456 if (!success)
457 break;
458 }
459 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
460
461 if (success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000462 {
463 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
464 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
465 }
466 }
Jim Inghamffba2292011-03-22 02:29:32 +0000467
Greg Clayton7b0992d2013-04-18 22:45:39 +0000468 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamca36cd12012-10-05 19:16:31 +0000469 tbreak_regex_cmd_ap(new CommandObjectRegexCommand (*this,
470 "_regexp-tbreak",
471 "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 +0000472 "_regexp-tbreak [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
473 2,
474 CommandCompletions::eSymbolCompletion |
Greg Claytonb5472782015-01-09 19:08:20 +0000475 CommandCompletions::eSourceFileCompletion,
476 false));
Jim Inghamca36cd12012-10-05 19:16:31 +0000477
478 if (tbreak_regex_cmd_ap.get())
479 {
480 bool success = true;
481 for (size_t i = 0; i < num_regexes; i++)
482 {
483 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
484 char buffer[1024];
485 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
486 assert (num_printed < 1024);
Eric Christopher5c91d5c12014-09-09 19:26:45 +0000487 // Quiet unused variable warning for release builds.
Eric Christopher9ad83942014-09-09 08:57:33 +0000488 (void) num_printed;
Jim Inghamca36cd12012-10-05 19:16:31 +0000489 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
490 if (!success)
491 break;
492 }
493 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
494
495 if (success)
496 {
497 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
498 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
499 }
500 }
501
Greg Clayton7b0992d2013-04-18 22:45:39 +0000502 std::unique_ptr<CommandObjectRegexCommand>
Johnny Chen6d675242012-08-24 18:15:45 +0000503 attach_regex_cmd_ap(new CommandObjectRegexCommand (*this,
504 "_regexp-attach",
505 "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 +0000506 "_regexp-attach [<pid>]\n_regexp-attach [<process-name>]",
Greg Claytonb5472782015-01-09 19:08:20 +0000507 2,
508 0,
509 false));
Johnny Chen6d675242012-08-24 18:15:45 +0000510 if (attach_regex_cmd_ap.get())
511 {
Greg Clayton3cb4c7d2012-12-15 01:19:07 +0000512 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
513 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
514 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
515 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
Johnny Chen6d675242012-08-24 18:15:45 +0000516 {
517 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
518 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
519 }
520 }
521
Greg Clayton7b0992d2013-04-18 22:45:39 +0000522 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000523 down_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000524 "_regexp-down",
525 "Go down \"n\" frames in the stack (1 frame by default).",
Greg Claytonb5472782015-01-09 19:08:20 +0000526 "_regexp-down [n]",
527 2,
528 0,
529 false));
Jim Inghamffba2292011-03-22 02:29:32 +0000530 if (down_regex_cmd_ap.get())
531 {
532 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
533 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
534 {
535 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
536 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
537 }
538 }
539
Greg Clayton7b0992d2013-04-18 22:45:39 +0000540 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000541 up_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000542 "_regexp-up",
543 "Go up \"n\" frames in the stack (1 frame by default).",
Greg Claytonb5472782015-01-09 19:08:20 +0000544 "_regexp-up [n]",
545 2,
546 0,
547 false));
Jim Inghamffba2292011-03-22 02:29:32 +0000548 if (up_regex_cmd_ap.get())
549 {
550 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") &&
551 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1"))
552 {
553 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release());
554 m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp;
555 }
556 }
Jason Molendabc7748b2011-10-22 01:30:52 +0000557
Greg Clayton7b0992d2013-04-18 22:45:39 +0000558 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000559 display_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000560 "_regexp-display",
561 "Add an expression evaluation stop-hook.",
Greg Claytonb5472782015-01-09 19:08:20 +0000562 "_regexp-display expression",
563 2,
564 0,
565 false));
Jason Molendabc7748b2011-10-22 01:30:52 +0000566 if (display_regex_cmd_ap.get())
567 {
568 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
569 {
570 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
571 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
572 }
573 }
574
Greg Clayton7b0992d2013-04-18 22:45:39 +0000575 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000576 undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000577 "_regexp-undisplay",
578 "Remove an expression evaluation stop-hook.",
Greg Claytonb5472782015-01-09 19:08:20 +0000579 "_regexp-undisplay stop-hook-number",
580 2,
581 0,
582 false));
Jason Molendabc7748b2011-10-22 01:30:52 +0000583 if (undisplay_regex_cmd_ap.get())
584 {
585 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
586 {
587 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
588 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
589 }
590 }
591
Greg Clayton7b0992d2013-04-18 22:45:39 +0000592 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000593 connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000594 "gdb-remote",
595 "Connect to a remote GDB server. If no hostname is provided, localhost is assumed.",
Greg Claytonb5472782015-01-09 19:08:20 +0000596 "gdb-remote [<hostname>:]<portnum>",
597 2,
598 0,
599 false));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000600 if (connect_gdb_remote_cmd_ap.get())
601 {
602 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
603 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
604 {
605 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
606 m_command_dict[command_sp->GetCommandName ()] = command_sp;
607 }
608 }
609
Greg Clayton7b0992d2013-04-18 22:45:39 +0000610 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000611 connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand (*this,
612 "kdp-remote",
Jason Molendaa7dcb332012-10-23 03:05:16 +0000613 "Connect to a remote KDP server. udp port 41139 is the default port number.",
Greg Claytonb5472782015-01-09 19:08:20 +0000614 "kdp-remote <hostname>[:<portnum>]",
615 2,
616 0,
617 false));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000618 if (connect_kdp_remote_cmd_ap.get())
619 {
620 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
Jason Molendac36b1842012-09-27 02:47:55 +0000621 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000622 {
623 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
624 m_command_dict[command_sp->GetCommandName ()] = command_sp;
625 }
626 }
627
Greg Clayton7b0992d2013-04-18 22:45:39 +0000628 std::unique_ptr<CommandObjectRegexCommand>
Jason Molenda4cddfed2012-10-05 05:29:32 +0000629 bt_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Claytonb5472782015-01-09 19:08:20 +0000630 "_regexp-bt",
631 "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.",
632 "bt [<digit>|all]",
633 2,
634 0,
635 false));
Jason Molenda4cddfed2012-10-05 05:29:32 +0000636 if (bt_regex_cmd_ap.get())
637 {
638 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
639 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
640 // so now "bt 3" is the preferred form, in line with gdb.
641 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
642 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
643 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
644 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
645 {
646 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
647 m_command_dict[command_sp->GetCommandName ()] = command_sp;
648 }
649 }
650
Greg Clayton7b0992d2013-04-18 22:45:39 +0000651 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton6bade322013-02-01 23:33:03 +0000652 list_regex_cmd_ap(new CommandObjectRegexCommand (*this,
653 "_regexp-list",
654 "Implements the GDB 'list' command in all of its forms except FILE:FUNCTION and maps them to the appropriate 'source list' commands.",
Ben Langmuiredb3b212013-09-26 20:00:01 +0000655 "_regexp-list [<line>]\n_regexp-list [<file>:<line>]\n_regexp-list [<file>:<line>]",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000656 2,
Greg Claytonb5472782015-01-09 19:08:20 +0000657 CommandCompletions::eSourceFileCompletion,
658 false));
Greg Clayton6bade322013-02-01 23:33:03 +0000659 if (list_regex_cmd_ap.get())
660 {
661 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
662 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
663 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
Greg Claytond9010962013-02-12 18:42:05 +0000664 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
Greg Claytone4ca5152013-03-13 18:25:49 +0000665 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
Greg Clayton6bade322013-02-01 23:33:03 +0000666 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
667 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
668 {
669 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
670 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
671 }
672 }
673
Greg Clayton7b0992d2013-04-18 22:45:39 +0000674 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytonef5651d2013-02-12 18:52:24 +0000675 env_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000676 "_regexp-env",
677 "Implements a shortcut to viewing and setting environment variables.",
Greg Claytonb5472782015-01-09 19:08:20 +0000678 "_regexp-env\n_regexp-env FOO=BAR",
679 2,
680 0,
681 false));
Greg Claytonef5651d2013-02-12 18:52:24 +0000682 if (env_regex_cmd_ap.get())
683 {
684 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
685 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
686 {
687 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
688 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
689 }
690 }
691
Richard Mittonf86248d2013-09-12 02:20:34 +0000692 std::unique_ptr<CommandObjectRegexCommand>
693 jump_regex_cmd_ap(new CommandObjectRegexCommand (*this,
694 "_regexp-jump",
695 "Sets the program counter to a new address.",
696 "_regexp-jump [<line>]\n"
697 "_regexp-jump [<+-lineoffset>]\n"
698 "_regexp-jump [<file>:<line>]\n"
Greg Claytonb5472782015-01-09 19:08:20 +0000699 "_regexp-jump [*<addr>]\n",
700 2,
701 0,
702 false));
Richard Mittonf86248d2013-09-12 02:20:34 +0000703 if (jump_regex_cmd_ap.get())
704 {
705 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", "thread jump --addr %1") &&
706 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "thread jump --line %1") &&
707 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", "thread jump --file %1 --line %2") &&
708 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", "thread jump --by %1"))
709 {
710 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release());
711 m_command_dict[jump_regex_cmd_sp->GetCommandName ()] = jump_regex_cmd_sp;
712 }
713 }
714
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715}
716
717int
718CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
719 StringList &matches)
720{
721 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
722
723 if (include_aliases)
724 {
725 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
726 }
727
728 return matches.GetSize();
729}
730
731CommandObjectSP
732CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
733{
734 CommandObject::CommandMap::iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +0000735 CommandObjectSP command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000736
737 std::string cmd(cmd_cstr);
738
739 if (HasCommands())
740 {
741 pos = m_command_dict.find(cmd);
742 if (pos != m_command_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000743 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 }
745
746 if (include_aliases && HasAliases())
747 {
748 pos = m_alias_dict.find(cmd);
749 if (pos != m_alias_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000750 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751 }
752
753 if (HasUserCommands())
754 {
755 pos = m_user_dict.find(cmd);
756 if (pos != m_user_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000757 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000758 }
759
Greg Claytonc7bece562013-01-25 18:06:21 +0000760 if (!exact && !command_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 {
Jim Ingham279a6c22010-07-06 22:46:59 +0000762 // We will only get into here if we didn't find any exact matches.
763
764 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
765
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000766 StringList local_matches;
Ed Masted78c9572014-04-20 00:31:37 +0000767 if (matches == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 matches = &local_matches;
769
Jim Ingham279a6c22010-07-06 22:46:59 +0000770 unsigned int num_cmd_matches = 0;
771 unsigned int num_alias_matches = 0;
772 unsigned int num_user_matches = 0;
773
774 // Look through the command dictionaries one by one, and if we get only one match from any of
775 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
776
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000777 if (HasCommands())
778 {
779 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
780 }
781
782 if (num_cmd_matches == 1)
783 {
784 cmd.assign(matches->GetStringAtIndex(0));
785 pos = m_command_dict.find(cmd);
786 if (pos != m_command_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000787 real_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 }
789
Jim Ingham490ac552010-06-24 20:28:42 +0000790 if (include_aliases && HasAliases())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 {
792 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
793
794 }
795
Jim Ingham279a6c22010-07-06 22:46:59 +0000796 if (num_alias_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 {
798 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
799 pos = m_alias_dict.find(cmd);
800 if (pos != m_alias_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000801 alias_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000802 }
803
Jim Ingham490ac552010-06-24 20:28:42 +0000804 if (HasUserCommands())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000805 {
806 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
807 }
808
Jim Ingham279a6c22010-07-06 22:46:59 +0000809 if (num_user_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 {
811 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
812
813 pos = m_user_dict.find (cmd);
814 if (pos != m_user_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000815 user_match_sp = pos->second;
816 }
817
818 // If we got exactly one match, return that, otherwise return the match list.
819
820 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
821 {
822 if (num_cmd_matches)
823 return real_match_sp;
824 else if (num_alias_matches)
825 return alias_match_sp;
826 else
827 return user_match_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828 }
829 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000830 else if (matches && command_sp)
Jim Ingham279a6c22010-07-06 22:46:59 +0000831 {
832 matches->AppendString (cmd_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 }
834
835
Greg Claytonc7bece562013-01-25 18:06:21 +0000836 return command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000837}
838
Greg Claytonde164aa2011-04-20 16:37:46 +0000839bool
840CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
841{
842 if (name && name[0])
843 {
844 std::string name_sstr(name);
Enrico Granatae00af802012-10-01 17:19:37 +0000845 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
846 if (found && !can_replace)
847 return false;
848 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000849 return false;
Greg Claytonde164aa2011-04-20 16:37:46 +0000850 m_command_dict[name_sstr] = cmd_sp;
851 return true;
852 }
853 return false;
854}
855
Enrico Granata223383e2011-08-16 23:24:13 +0000856bool
Enrico Granata0a305db2011-11-07 22:57:04 +0000857CommandInterpreter::AddUserCommand (std::string name,
Enrico Granata223383e2011-08-16 23:24:13 +0000858 const lldb::CommandObjectSP &cmd_sp,
859 bool can_replace)
860{
Enrico Granata0a305db2011-11-07 22:57:04 +0000861 if (!name.empty())
Enrico Granata223383e2011-08-16 23:24:13 +0000862 {
Enrico Granata0a305db2011-11-07 22:57:04 +0000863
864 const char* name_cstr = name.c_str();
865
866 // do not allow replacement of internal commands
867 if (CommandExists(name_cstr))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000868 {
869 if (can_replace == false)
870 return false;
871 if (m_command_dict[name]->IsRemovable() == false)
872 return false;
873 }
Enrico Granata0a305db2011-11-07 22:57:04 +0000874
Enrico Granata21dfcd92012-09-28 23:57:51 +0000875 if (UserCommandExists(name_cstr))
876 {
877 if (can_replace == false)
878 return false;
879 if (m_user_dict[name]->IsRemovable() == false)
880 return false;
881 }
882
Enrico Granata0a305db2011-11-07 22:57:04 +0000883 m_user_dict[name] = cmd_sp;
Enrico Granata223383e2011-08-16 23:24:13 +0000884 return true;
885 }
886 return false;
887}
Greg Claytonde164aa2011-04-20 16:37:46 +0000888
Jim Ingham279a6c22010-07-06 22:46:59 +0000889CommandObjectSP
890CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891{
Caroline Tice472362e2010-12-14 18:51:39 +0000892 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
893 CommandObjectSP ret_val; // Possibly empty return value.
894
Ed Masted78c9572014-04-20 00:31:37 +0000895 if (cmd_cstr == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000896 return ret_val;
897
898 if (cmd_words.GetArgumentCount() == 1)
Ed Masted78c9572014-04-20 00:31:37 +0000899 return GetCommandSP(cmd_cstr, include_aliases, true, nullptr);
Caroline Tice472362e2010-12-14 18:51:39 +0000900 else
901 {
902 // We have a multi-word command (seemingly), so we need to do more work.
903 // First, get the cmd_obj_sp for the first word in the command.
Ed Masted78c9572014-04-20 00:31:37 +0000904 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, nullptr);
905 if (cmd_obj_sp.get() != nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000906 {
907 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
908 // command name), and find the appropriate sub-command SP for each command word....
909 size_t end = cmd_words.GetArgumentCount();
910 for (size_t j= 1; j < end; ++j)
911 {
912 if (cmd_obj_sp->IsMultiwordObject())
913 {
Greg Clayton998255b2012-10-13 02:07:45 +0000914 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
Ed Masted78c9572014-04-20 00:31:37 +0000915 if (cmd_obj_sp.get() == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000916 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
917 return ret_val;
918 }
919 else
920 // We have more words in the command name, but we don't have a multiword object. Fail and return
921 // empty 'ret_val'.
922 return ret_val;
923 }
924 // We successfully looped through all the command words and got valid command objects for them. Assign the
925 // last object retrieved to 'ret_val'.
926 ret_val = cmd_obj_sp;
927 }
928 }
929 return ret_val;
Jim Ingham279a6c22010-07-06 22:46:59 +0000930}
931
932CommandObject *
933CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
934{
935 return GetCommandSPExact (cmd_cstr, include_aliases).get();
936}
937
938CommandObject *
939CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
940{
941 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
942
943 // If we didn't find an exact match to the command string in the commands, look in
944 // the aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000945
946 if (command_obj)
947 return command_obj;
Jim Ingham279a6c22010-07-06 22:46:59 +0000948
Enrico Granata5342c442013-06-18 18:01:08 +0000949 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
Jim Ingham279a6c22010-07-06 22:46:59 +0000950
Enrico Granata5342c442013-06-18 18:01:08 +0000951 if (command_obj)
952 return command_obj;
953
954 // If there wasn't an exact match then look for an inexact one in just the commands
Ed Masted78c9572014-04-20 00:31:37 +0000955 command_obj = GetCommandSP(cmd_cstr, false, false, nullptr).get();
Matt Kopec038ff812013-04-23 16:17:32 +0000956
957 // Finally, if there wasn't an inexact match among the commands, look for an inexact
958 // match in both the commands and aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000959
960 if (command_obj)
961 {
962 if (matches)
963 matches->AppendString(command_obj->GetCommandName());
964 return command_obj;
965 }
966
967 return GetCommandSP(cmd_cstr, true, false, matches).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000968}
969
970bool
971CommandInterpreter::CommandExists (const char *cmd)
972{
973 return m_command_dict.find(cmd) != m_command_dict.end();
974}
975
976bool
Caroline Ticeca90c472011-05-06 21:37:15 +0000977CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
978 const char *options_args,
979 OptionArgVectorSP &option_arg_vector_sp)
980{
981 bool success = true;
982 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
983
984 if (!options_args || (strlen (options_args) < 1))
985 return true;
986
987 std::string options_string (options_args);
988 Args args (options_args);
989 CommandReturnObject result;
990 // Check to see if the command being aliased can take any command options.
991 Options *options = cmd_obj_sp->GetOptions ();
992 if (options)
993 {
994 // See if any options were specified as part of the alias; if so, handle them appropriately.
995 options->NotifyOptionParsingStarting ();
996 args.Unshift ("dummy_arg");
997 args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
998 args.Shift ();
999 if (result.Succeeded())
1000 options->VerifyPartialOptions (result);
1001 if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
1002 {
1003 result.AppendError ("Unable to create requested alias.\n");
1004 return false;
1005 }
1006 }
1007
Greg Clayton5521f992011-10-28 21:38:01 +00001008 if (!options_string.empty())
Caroline Ticeca90c472011-05-06 21:37:15 +00001009 {
1010 if (cmd_obj_sp->WantsRawCommandString ())
1011 option_arg_vector->push_back (OptionArgPair ("<argument>",
1012 OptionArgValue (-1,
1013 options_string)));
1014 else
1015 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001016 const size_t argc = args.GetArgumentCount();
Caroline Ticeca90c472011-05-06 21:37:15 +00001017 for (size_t i = 0; i < argc; ++i)
1018 if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
1019 option_arg_vector->push_back
1020 (OptionArgPair ("<argument>",
1021 OptionArgValue (-1,
1022 std::string (args.GetArgumentAtIndex (i)))));
1023 }
1024 }
1025
1026 return success;
1027}
1028
1029bool
Jim Ingham298f3782013-04-03 00:25:49 +00001030CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
1031{
1032 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
1033 if (exact_match)
1034 {
1035 full_name.assign(cmd);
1036 return exact_match;
1037 }
1038 else
1039 {
1040 StringList matches;
1041 size_t num_alias_matches;
1042 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
1043 if (num_alias_matches == 1)
1044 {
1045 // Make sure this isn't shadowing a command in the regular command space:
1046 StringList regular_matches;
1047 const bool include_aliases = false;
1048 const bool exact = false;
1049 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, &regular_matches));
1050 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1051 return false;
1052 else
1053 {
1054 full_name.assign (matches.GetStringAtIndex(0));
1055 return true;
1056 }
1057 }
1058 else
1059 return false;
1060 }
1061}
1062
1063bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001064CommandInterpreter::AliasExists (const char *cmd)
1065{
1066 return m_alias_dict.find(cmd) != m_alias_dict.end();
1067}
1068
1069bool
1070CommandInterpreter::UserCommandExists (const char *cmd)
1071{
1072 return m_user_dict.find(cmd) != m_user_dict.end();
1073}
1074
1075void
1076CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
1077{
Jim Ingham279a6c22010-07-06 22:46:59 +00001078 command_obj_sp->SetIsAlias (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 m_alias_dict[alias_name] = command_obj_sp;
1080}
1081
1082bool
1083CommandInterpreter::RemoveAlias (const char *alias_name)
1084{
1085 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
1086 if (pos != m_alias_dict.end())
1087 {
1088 m_alias_dict.erase(pos);
1089 return true;
1090 }
1091 return false;
1092}
Greg Claytonb5472782015-01-09 19:08:20 +00001093
1094bool
1095CommandInterpreter::RemoveCommand (const char *cmd)
1096{
1097 auto pos = m_command_dict.find(cmd);
1098 if (pos != m_command_dict.end())
1099 {
1100 if (pos->second->IsRemovable())
1101 {
1102 // Only regular expression objects or python commands are removable
1103 m_command_dict.erase(pos);
1104 return true;
1105 }
1106 }
1107 return false;
1108}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001109bool
1110CommandInterpreter::RemoveUser (const char *alias_name)
1111{
1112 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1113 if (pos != m_user_dict.end())
1114 {
1115 m_user_dict.erase(pos);
1116 return true;
1117 }
1118 return false;
1119}
1120
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001121void
1122CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
1123{
1124 help_string.Printf ("'%s", command_name);
1125 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1126
Sean Callanan9a028512012-08-09 00:50:26 +00001127 if (option_arg_vector_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001128 {
1129 OptionArgVector *options = option_arg_vector_sp.get();
Andy Gibbsa297a972013-06-19 19:04:53 +00001130 for (size_t i = 0; i < options->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 {
1132 OptionArgPair cur_option = (*options)[i];
1133 std::string opt = cur_option.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00001134 OptionArgValue value_pair = cur_option.second;
1135 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001136 if (opt.compare("<argument>") == 0)
1137 {
1138 help_string.Printf (" %s", value.c_str());
1139 }
1140 else
1141 {
1142 help_string.Printf (" %s", opt.c_str());
1143 if ((value.compare ("<no-argument>") != 0)
1144 && (value.compare ("<need-argument") != 0))
1145 {
1146 help_string.Printf (" %s", value.c_str());
1147 }
1148 }
1149 }
1150 }
1151
1152 help_string.Printf ("'");
1153}
1154
Greg Clayton12fc3e02010-08-26 22:05:43 +00001155size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001156CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
1157{
1158 CommandObject::CommandMap::const_iterator pos;
Greg Clayton12fc3e02010-08-26 22:05:43 +00001159 CommandObject::CommandMap::const_iterator end = dict.end();
1160 size_t max_len = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001161
Greg Clayton12fc3e02010-08-26 22:05:43 +00001162 for (pos = dict.begin(); pos != end; ++pos)
1163 {
1164 size_t len = pos->first.size();
1165 if (max_len < len)
1166 max_len = len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 }
Greg Clayton12fc3e02010-08-26 22:05:43 +00001168 return max_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169}
1170
1171void
Enrico Granata223383e2011-08-16 23:24:13 +00001172CommandInterpreter::GetHelp (CommandReturnObject &result,
Enrico Granata08633ee2011-09-09 17:49:36 +00001173 uint32_t cmd_types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174{
Kate Stonea487aa42015-01-15 00:52:41 +00001175 const char * help_prologue = GetDebugger().GetIOHandlerHelpPrologue();
1176 if (help_prologue != NULL)
1177 {
1178 OutputFormattedHelpText(result.GetOutputStream(), NULL, help_prologue);
1179 }
1180
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001181 CommandObject::CommandMap::const_iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +00001182 size_t max_len = FindLongestCommandWord (m_command_dict);
Enrico Granata223383e2011-08-16 23:24:13 +00001183
1184 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001185 {
Kate Stonea487aa42015-01-15 00:52:41 +00001186 result.AppendMessage("Debugger commands:");
Enrico Granata223383e2011-08-16 23:24:13 +00001187 result.AppendMessage("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001188
Enrico Granata223383e2011-08-16 23:24:13 +00001189 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1190 {
Kate Stonea487aa42015-01-15 00:52:41 +00001191 if (!(cmd_types & eCommandTypesHidden) && (pos->first.compare(0, 1, "_") == 0))
1192 continue;
1193
Enrico Granata223383e2011-08-16 23:24:13 +00001194 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1195 max_len);
1196 }
1197 result.AppendMessage("");
1198
1199 }
1200
Greg Clayton5521f992011-10-28 21:38:01 +00001201 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001202 {
Kate Stonea487aa42015-01-15 00:52:41 +00001203 result.AppendMessageWithFormat("Current command abbreviations "
1204 "(type '%shelp command alias' for more info):\n",
1205 GetCommandPrefix());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 result.AppendMessage("");
Greg Clayton12fc3e02010-08-26 22:05:43 +00001207 max_len = FindLongestCommandWord (m_alias_dict);
1208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
1210 {
1211 StreamString sstr;
1212 StreamString translation_and_help;
1213 std::string entry_name = pos->first;
1214 std::string second_entry = pos->second.get()->GetCommandName();
1215 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
1216
1217 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
1218 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
1219 translation_and_help.GetData(), max_len);
1220 }
1221 result.AppendMessage("");
1222 }
1223
Greg Clayton5521f992011-10-28 21:38:01 +00001224 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225 {
Kate Stonea487aa42015-01-15 00:52:41 +00001226 result.AppendMessage ("Current user-defined commands:");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001227 result.AppendMessage("");
Enrico Granata223383e2011-08-16 23:24:13 +00001228 max_len = FindLongestCommandWord (m_user_dict);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1230 {
Enrico Granata223383e2011-08-16 23:24:13 +00001231 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1232 max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233 }
1234 result.AppendMessage("");
1235 }
1236
Kate Stonea487aa42015-01-15 00:52:41 +00001237 result.AppendMessageWithFormat("For more information on any command, type '%shelp <command-name>'.\n",
1238 GetCommandPrefix());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001239}
1240
Caroline Tice844d2302010-12-09 22:52:49 +00001241CommandObject *
1242CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243{
Caroline Tice844d2302010-12-09 22:52:49 +00001244 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1245 // eventually be invoked by the given command line.
1246
Ed Masted78c9572014-04-20 00:31:37 +00001247 CommandObject *cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001248 std::string white_space (" \t\v");
1249 size_t start = command_string.find_first_not_of (white_space);
1250 size_t end = 0;
1251 bool done = false;
1252 while (!done)
1253 {
1254 if (start != std::string::npos)
1255 {
1256 // Get the next word from command_string.
1257 end = command_string.find_first_of (white_space, start);
1258 if (end == std::string::npos)
1259 end = command_string.size();
1260 std::string cmd_word = command_string.substr (start, end - start);
1261
Ed Masted78c9572014-04-20 00:31:37 +00001262 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001263 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1264 // command or alias.
1265 cmd_obj = GetCommandObject (cmd_word.c_str());
1266 else if (cmd_obj->IsMultiwordObject ())
1267 {
1268 // 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 +00001269 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001270 if (sub_cmd_obj)
1271 cmd_obj = sub_cmd_obj;
1272 else // cmd_word was not a valid sub-command word, so we are donee
1273 done = true;
1274 }
1275 else
1276 // We have a cmd_obj and it is not a multi-word object, so we are done.
1277 done = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001278
Caroline Tice844d2302010-12-09 22:52:49 +00001279 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1280 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1281 // next word.
1282
1283 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1284 done = true;
1285 else
1286 start = command_string.find_first_not_of (white_space, end);
1287 }
1288 else
1289 // Unable to find any more words.
1290 done = true;
1291 }
1292
1293 if (end == command_string.size())
1294 command_string.clear();
1295 else
1296 command_string = command_string.substr(end);
1297
1298 return cmd_obj;
1299}
1300
Greg Clayton51964162011-10-25 00:36:27 +00001301static const char *k_white_space = " \t\v";
Greg Clayton5521f992011-10-28 21:38:01 +00001302static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
Greg Clayton51964162011-10-25 00:36:27 +00001303static void
1304StripLeadingSpaces (std::string &s)
Caroline Tice844d2302010-12-09 22:52:49 +00001305{
Greg Clayton51964162011-10-25 00:36:27 +00001306 if (!s.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001307 {
Greg Clayton51964162011-10-25 00:36:27 +00001308 size_t pos = s.find_first_not_of (k_white_space);
1309 if (pos == std::string::npos)
1310 s.clear();
1311 else if (pos == 0)
1312 return;
1313 s.erase (0, pos);
1314 }
1315}
1316
Greg Clayton93c62e62011-11-09 23:25:03 +00001317static size_t
1318FindArgumentTerminator (const std::string &s)
1319{
Greg Clayton93c62e62011-11-09 23:25:03 +00001320 const size_t s_len = s.size();
1321 size_t offset = 0;
1322 while (offset < s_len)
1323 {
1324 size_t pos = s.find ("--", offset);
1325 if (pos == std::string::npos)
1326 break;
1327 if (pos > 0)
1328 {
1329 if (isspace(s[pos-1]))
1330 {
1331 // Check if the string ends "\s--" (where \s is a space character)
1332 // or if we have "\s--\s".
1333 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1334 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001335 return pos;
1336 }
1337 }
1338 }
1339 offset = pos + 2;
1340 }
Greg Clayton93c62e62011-11-09 23:25:03 +00001341 return std::string::npos;
1342}
1343
Greg Clayton51964162011-10-25 00:36:27 +00001344static bool
Greg Clayton5521f992011-10-28 21:38:01 +00001345ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
Greg Clayton51964162011-10-25 00:36:27 +00001346{
Greg Clayton5521f992011-10-28 21:38:01 +00001347 command.clear();
1348 suffix.clear();
Greg Clayton51964162011-10-25 00:36:27 +00001349 StripLeadingSpaces (command_string);
1350
1351 bool result = false;
1352 quote_char = '\0';
1353
1354 if (!command_string.empty())
1355 {
1356 const char first_char = command_string[0];
1357 if (first_char == '\'' || first_char == '"')
Caroline Tice844d2302010-12-09 22:52:49 +00001358 {
Greg Clayton51964162011-10-25 00:36:27 +00001359 quote_char = first_char;
1360 const size_t end_quote_pos = command_string.find (quote_char, 1);
1361 if (end_quote_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001362 {
Greg Clayton5521f992011-10-28 21:38:01 +00001363 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001364 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001365 }
1366 else
1367 {
Greg Clayton5521f992011-10-28 21:38:01 +00001368 command.assign (command_string, 1, end_quote_pos - 1);
Greg Clayton51964162011-10-25 00:36:27 +00001369 if (end_quote_pos + 1 < command_string.size())
1370 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1371 else
1372 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001373 }
Caroline Tice844d2302010-12-09 22:52:49 +00001374 }
1375 else
1376 {
Greg Clayton51964162011-10-25 00:36:27 +00001377 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1378 if (first_space_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001379 {
Greg Clayton5521f992011-10-28 21:38:01 +00001380 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001381 command_string.erase();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001382 }
1383 else
1384 {
Greg Clayton5521f992011-10-28 21:38:01 +00001385 command.assign (command_string, 0, first_space_pos);
1386 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
Caroline Tice2b5e8502011-05-11 16:07:06 +00001387 }
Caroline Tice844d2302010-12-09 22:52:49 +00001388 }
Greg Clayton51964162011-10-25 00:36:27 +00001389 result = true;
Caroline Tice844d2302010-12-09 22:52:49 +00001390 }
Greg Clayton5521f992011-10-28 21:38:01 +00001391
1392
1393 if (!command.empty())
1394 {
1395 // actual commands can't start with '-' or '_'
1396 if (command[0] != '-' && command[0] != '_')
1397 {
1398 size_t pos = command.find_first_not_of(k_valid_command_chars);
1399 if (pos > 0 && pos != std::string::npos)
1400 {
1401 suffix.assign (command.begin() + pos, command.end());
1402 command.erase (pos);
1403 }
1404 }
1405 }
Greg Clayton51964162011-10-25 00:36:27 +00001406
1407 return result;
Caroline Tice844d2302010-12-09 22:52:49 +00001408}
1409
Greg Clayton5521f992011-10-28 21:38:01 +00001410CommandObject *
1411CommandInterpreter::BuildAliasResult (const char *alias_name,
1412 std::string &raw_input_string,
1413 std::string &alias_result,
1414 CommandReturnObject &result)
Caroline Tice844d2302010-12-09 22:52:49 +00001415{
Ed Masted78c9572014-04-20 00:31:37 +00001416 CommandObject *alias_cmd_obj = nullptr;
Pavel Labath00b7f952015-03-02 12:46:22 +00001417 Args cmd_args (raw_input_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001418 alias_cmd_obj = GetCommandObject (alias_name);
1419 StreamString result_str;
1420
1421 if (alias_cmd_obj)
1422 {
1423 std::string alias_name_str = alias_name;
1424 if ((cmd_args.GetArgumentCount() == 0)
1425 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1426 cmd_args.Unshift (alias_name);
1427
1428 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1429 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1430
1431 if (option_arg_vector_sp.get())
1432 {
1433 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1434
Andy Gibbsa297a972013-06-19 19:04:53 +00001435 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Caroline Tice844d2302010-12-09 22:52:49 +00001436 {
1437 OptionArgPair option_pair = (*option_arg_vector)[i];
1438 OptionArgValue value_pair = option_pair.second;
1439 int value_type = value_pair.first;
1440 std::string option = option_pair.first;
1441 std::string value = value_pair.second;
1442 if (option.compare ("<argument>") == 0)
1443 result_str.Printf (" %s", value.c_str());
1444 else
1445 {
1446 result_str.Printf (" %s", option.c_str());
Virgile Belloe2607b52013-09-05 16:42:23 +00001447 if (value_type != OptionParser::eOptionalArgument)
Caroline Tice844d2302010-12-09 22:52:49 +00001448 result_str.Printf (" ");
Virgile Belloe2607b52013-09-05 16:42:23 +00001449 if (value.compare ("<OptionParser::eNoArgument>") != 0)
Caroline Tice844d2302010-12-09 22:52:49 +00001450 {
1451 int index = GetOptionArgumentPosition (value.c_str());
1452 if (index == 0)
1453 result_str.Printf ("%s", value.c_str());
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001454 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Caroline Tice844d2302010-12-09 22:52:49 +00001455 {
1456
1457 result.AppendErrorWithFormat
1458 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1459 index);
1460 result.SetStatus (eReturnStatusFailed);
Greg Clayton5521f992011-10-28 21:38:01 +00001461 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001462 }
1463 else
1464 {
1465 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1466 if (strpos != std::string::npos)
1467 raw_input_string = raw_input_string.erase (strpos,
1468 strlen (cmd_args.GetArgumentAtIndex (index)));
1469 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1470 }
1471 }
1472 }
1473 }
1474 }
1475
1476 alias_result = result_str.GetData();
1477 }
Greg Clayton5521f992011-10-28 21:38:01 +00001478 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001479}
1480
Greg Clayton5a314712011-10-14 07:41:33 +00001481Error
1482CommandInterpreter::PreprocessCommand (std::string &command)
1483{
1484 // The command preprocessor needs to do things to the command
1485 // line before any parsing of arguments or anything else is done.
1486 // The only current stuff that gets proprocessed is anyting enclosed
1487 // in backtick ('`') characters is evaluated as an expression and
1488 // the result of the expression must be a scalar that can be substituted
1489 // into the command. An example would be:
1490 // (lldb) memory read `$rsp + 20`
1491 Error error; // Error for any expressions that might not evaluate
1492 size_t start_backtick;
1493 size_t pos = 0;
1494 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1495 {
1496 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1497 {
1498 // The backtick was preceeded by a '\' character, remove the slash
1499 // and don't treat the backtick as the start of an expression
1500 command.erase(start_backtick-1, 1);
1501 // No need to add one to start_backtick since we just deleted a char
1502 pos = start_backtick;
1503 }
1504 else
1505 {
1506 const size_t expr_content_start = start_backtick + 1;
1507 const size_t end_backtick = command.find ('`', expr_content_start);
1508 if (end_backtick == std::string::npos)
1509 return error;
1510 else if (end_backtick == expr_content_start)
1511 {
1512 // Empty expression (two backticks in a row)
1513 command.erase (start_backtick, 2);
1514 }
1515 else
1516 {
1517 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1518
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00001519 ExecutionContext exe_ctx(GetExecutionContext());
1520 Target *target = exe_ctx.GetTargetPtr();
Johnny Chen51ea0ad2011-10-29 00:21:50 +00001521 // Get a dummy target to allow for calculator mode while processing backticks.
1522 // This also helps break the infinite loop caused when target is null.
1523 if (!target)
Jim Ingham893c9322014-11-22 01:42:44 +00001524 target = m_debugger.GetDummyTarget();
Greg Clayton5a314712011-10-14 07:41:33 +00001525 if (target)
1526 {
Greg Clayton5a314712011-10-14 07:41:33 +00001527 ValueObjectSP expr_result_valobj_sp;
Enrico Granatad4439aa2012-09-05 20:41:26 +00001528
Jim Ingham35e1bda2012-10-16 21:41:58 +00001529 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00001530 options.SetCoerceToId(false);
1531 options.SetUnwindOnError(true);
1532 options.SetIgnoreBreakpoints(true);
1533 options.SetKeepInMemory(false);
1534 options.SetTryAllThreads(true);
1535 options.SetTimeoutUsec(0);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001536
Jim Ingham1624a2d2014-05-05 02:26:40 +00001537 ExpressionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
Enrico Granataca0e5ad2014-10-09 23:09:40 +00001538 exe_ctx.GetFramePtr(),
1539 expr_result_valobj_sp,
1540 options);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001541
Jim Ingham8646d3c2014-05-05 02:47:44 +00001542 if (expr_result == eExpressionCompleted)
Greg Clayton5a314712011-10-14 07:41:33 +00001543 {
1544 Scalar scalar;
Enrico Granataca0e5ad2014-10-09 23:09:40 +00001545 if (expr_result_valobj_sp)
1546 expr_result_valobj_sp = expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(expr_result_valobj_sp->GetDynamicValueType(), true);
Greg Clayton5a314712011-10-14 07:41:33 +00001547 if (expr_result_valobj_sp->ResolveValue (scalar))
1548 {
1549 command.erase (start_backtick, end_backtick - start_backtick + 1);
1550 StreamString value_strm;
1551 const bool show_type = false;
1552 scalar.GetValue (&value_strm, show_type);
1553 size_t value_string_size = value_strm.GetSize();
1554 if (value_string_size)
1555 {
1556 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1557 pos = start_backtick + value_string_size;
1558 continue;
1559 }
1560 else
1561 {
1562 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1563 }
1564 }
1565 else
1566 {
1567 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1568 }
1569 }
1570 else
1571 {
1572 if (expr_result_valobj_sp)
1573 error = expr_result_valobj_sp->GetError();
1574 if (error.Success())
1575 {
1576
1577 switch (expr_result)
1578 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00001579 case eExpressionSetupError:
Greg Clayton5a314712011-10-14 07:41:33 +00001580 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1581 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001582 case eExpressionParseError:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001583 error.SetErrorStringWithFormat ("expression parse error for the expression '%s'", expr_str.c_str());
1584 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001585 case eExpressionResultUnavailable:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001586 error.SetErrorStringWithFormat ("expression error fetching result for the expression '%s'", expr_str.c_str());
Jim Ingham8646d3c2014-05-05 02:47:44 +00001587 case eExpressionCompleted:
Greg Clayton5a314712011-10-14 07:41:33 +00001588 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001589 case eExpressionDiscarded:
Greg Clayton5a314712011-10-14 07:41:33 +00001590 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1591 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001592 case eExpressionInterrupted:
Greg Clayton5a314712011-10-14 07:41:33 +00001593 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1594 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001595 case eExpressionHitBreakpoint:
Jim Ingham184e9812013-01-15 02:47:48 +00001596 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1597 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001598 case eExpressionTimedOut:
Greg Clayton5a314712011-10-14 07:41:33 +00001599 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1600 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001601 case eExpressionStoppedForDebug:
Greg Claytonc28ce782013-11-08 23:38:26 +00001602 error.SetErrorStringWithFormat("expression stop at entry point for debugging for the expression '%s'", expr_str.c_str());
1603 break;
Greg Clayton5a314712011-10-14 07:41:33 +00001604 }
1605 }
1606 }
1607 }
1608 }
1609 if (error.Fail())
1610 break;
1611 }
1612 }
1613 return error;
1614}
1615
1616
Caroline Tice844d2302010-12-09 22:52:49 +00001617bool
1618CommandInterpreter::HandleCommand (const char *command_line,
Enrico Granata5f5ab602012-05-31 01:09:06 +00001619 LazyBool lazy_add_to_history,
Caroline Tice844d2302010-12-09 22:52:49 +00001620 CommandReturnObject &result,
Jim Inghame16c50a2011-02-18 00:54:25 +00001621 ExecutionContext *override_context,
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001622 bool repeat_on_empty_command,
1623 bool no_context_switching)
Jim Inghame16c50a2011-02-18 00:54:25 +00001624
Caroline Tice844d2302010-12-09 22:52:49 +00001625{
Jim Inghame16c50a2011-02-18 00:54:25 +00001626
Caroline Tice844d2302010-12-09 22:52:49 +00001627 bool done = false;
Ed Masted78c9572014-04-20 00:31:37 +00001628 CommandObject *cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001629 bool wants_raw_input = false;
1630 std::string command_string (command_line);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001631 std::string original_command_string (command_line);
Caroline Tice844d2302010-12-09 22:52:49 +00001632
Greg Clayton5160ce52013-03-27 23:08:40 +00001633 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001634 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1635
1636 // Make a scoped cleanup object that will clear the crash description string
1637 // on exit of this function.
Ed Masted78c9572014-04-20 00:31:37 +00001638 lldb_utility::CleanUp <const char *> crash_description_cleanup(nullptr, Host::SetCrashDescription);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001639
Caroline Tice844d2302010-12-09 22:52:49 +00001640 if (log)
1641 log->Printf ("Processing command: %s", command_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001642
Jim Ingham30244832010-11-04 23:08:45 +00001643 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1644
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001645 if (!no_context_switching)
1646 UpdateExecutionContext (override_context);
Enrico Granata5f5ab602012-05-31 01:09:06 +00001647
Enrico Granata5f5ab602012-05-31 01:09:06 +00001648 bool add_to_history;
1649 if (lazy_add_to_history == eLazyBoolCalculate)
1650 add_to_history = (m_command_source_depth == 0);
1651 else
1652 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1653
Jim Inghame16c50a2011-02-18 00:54:25 +00001654 bool empty_command = false;
1655 bool comment_command = false;
1656 if (command_string.empty())
1657 empty_command = true;
1658 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001659 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001660 const char *k_space_characters = "\t\n\v\f\r ";
1661
1662 size_t non_space = command_string.find_first_not_of (k_space_characters);
1663 // Check for empty line or comment line (lines whose first
1664 // non-space character is the comment character for this interpreter)
1665 if (non_space == std::string::npos)
1666 empty_command = true;
1667 else if (command_string[non_space] == m_comment_char)
1668 comment_command = true;
Enrico Granata7594f142013-06-17 22:51:50 +00001669 else if (command_string[non_space] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001670 {
Enrico Granata7594f142013-06-17 22:51:50 +00001671 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
Ed Masted78c9572014-04-20 00:31:37 +00001672 if (history_string == nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001673 {
1674 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1675 result.SetStatus(eReturnStatusFailed);
1676 return false;
1677 }
1678 add_to_history = false;
1679 command_string = history_string;
1680 original_command_string = history_string;
1681 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001682 }
1683
1684 if (empty_command)
1685 {
1686 if (repeat_on_empty_command)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001687 {
Enrico Granata7594f142013-06-17 22:51:50 +00001688 if (m_command_history.IsEmpty())
Jim Inghame16c50a2011-02-18 00:54:25 +00001689 {
1690 result.AppendError ("empty command");
1691 result.SetStatus(eReturnStatusFailed);
1692 return false;
1693 }
1694 else
1695 {
1696 command_line = m_repeat_command.c_str();
1697 command_string = command_line;
Jim Inghama5a97eb2011-07-12 03:12:18 +00001698 original_command_string = command_line;
Jim Inghame16c50a2011-02-18 00:54:25 +00001699 if (m_repeat_command.empty())
1700 {
1701 result.AppendErrorWithFormat("No auto repeat.\n");
1702 result.SetStatus (eReturnStatusFailed);
1703 return false;
1704 }
1705 }
1706 add_to_history = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001707 }
1708 else
1709 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001710 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1711 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001712 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001713 }
1714 else if (comment_command)
1715 {
1716 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1717 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001718 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001719
Greg Clayton5a314712011-10-14 07:41:33 +00001720
1721 Error error (PreprocessCommand (command_string));
1722
1723 if (error.Fail())
1724 {
1725 result.AppendError (error.AsCString());
1726 result.SetStatus(eReturnStatusFailed);
1727 return false;
1728 }
Caroline Tice844d2302010-12-09 22:52:49 +00001729 // Phase 1.
1730
1731 // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object
1732 // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that
1733 // the user could have specified an alias, and in translating the alias there may also be command options and/or
1734 // even data (including raw text strings) that need to be found and inserted into the command line as part of
1735 // 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 +00001736 // object whose Execute method will actually be called; 2). a revised command string, with all substitutions &
Caroline Tice844d2302010-12-09 22:52:49 +00001737 // replacements taken care of; 3). whether or not the Execute function wants raw input or not.
Caroline Ticed9d63362010-12-07 19:58:26 +00001738
Caroline Tice844d2302010-12-09 22:52:49 +00001739 StreamString revised_command_line;
Caroline Tice01274c02010-12-11 08:16:56 +00001740 size_t actual_cmd_name_len = 0;
Greg Clayton5521f992011-10-28 21:38:01 +00001741 std::string next_word;
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001742 StringList matches;
Caroline Tice844d2302010-12-09 22:52:49 +00001743 while (!done)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001744 {
Caroline Tice2b5e8502011-05-11 16:07:06 +00001745 char quote_char = '\0';
Greg Clayton5521f992011-10-28 21:38:01 +00001746 std::string suffix;
1747 ExtractCommand (command_string, next_word, suffix, quote_char);
Ed Masted78c9572014-04-20 00:31:37 +00001748 if (cmd_obj == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001749 {
Jim Ingham298f3782013-04-03 00:25:49 +00001750 std::string full_name;
1751 if (GetAliasFullName(next_word.c_str(), full_name))
Caroline Tice472362e2010-12-14 18:51:39 +00001752 {
Greg Clayton5521f992011-10-28 21:38:01 +00001753 std::string alias_result;
Jim Ingham298f3782013-04-03 00:25:49 +00001754 cmd_obj = BuildAliasResult (full_name.c_str(), command_string, alias_result, result);
Greg Clayton5521f992011-10-28 21:38:01 +00001755 revised_command_line.Printf ("%s", alias_result.c_str());
1756 if (cmd_obj)
1757 {
1758 wants_raw_input = cmd_obj->WantsRawCommandString ();
1759 actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
1760 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761 }
1762 else
1763 {
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001764 cmd_obj = GetCommandObject (next_word.c_str(), &matches);
Greg Clayton5521f992011-10-28 21:38:01 +00001765 if (cmd_obj)
1766 {
1767 actual_cmd_name_len += next_word.length();
1768 revised_command_line.Printf ("%s", next_word.c_str());
1769 wants_raw_input = cmd_obj->WantsRawCommandString ();
1770 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001771 else
Greg Clayton5521f992011-10-28 21:38:01 +00001772 {
1773 revised_command_line.Printf ("%s", next_word.c_str());
1774 }
Caroline Tice844d2302010-12-09 22:52:49 +00001775 }
1776 }
1777 else
1778 {
Greg Clayton5521f992011-10-28 21:38:01 +00001779 if (cmd_obj->IsMultiwordObject ())
1780 {
Greg Clayton998255b2012-10-13 02:07:45 +00001781 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (next_word.c_str());
Greg Clayton5521f992011-10-28 21:38:01 +00001782 if (sub_cmd_obj)
1783 {
1784 actual_cmd_name_len += next_word.length() + 1;
1785 revised_command_line.Printf (" %s", next_word.c_str());
1786 cmd_obj = sub_cmd_obj;
1787 wants_raw_input = cmd_obj->WantsRawCommandString ();
1788 }
1789 else
1790 {
1791 if (quote_char)
1792 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1793 else
1794 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1795 done = true;
1796 }
1797 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001798 else
Greg Clayton5521f992011-10-28 21:38:01 +00001799 {
1800 if (quote_char)
1801 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1802 else
1803 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1804 done = true;
1805 }
Caroline Tice844d2302010-12-09 22:52:49 +00001806 }
1807
Ed Masted78c9572014-04-20 00:31:37 +00001808 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001809 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001810 const size_t num_matches = matches.GetSize();
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001811 if (matches.GetSize() > 1) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001812 StreamString error_msg;
1813 error_msg.Printf ("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001814
1815 for (uint32_t i = 0; i < num_matches; ++i) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001816 error_msg.Printf ("\t%s\n", matches.GetStringAtIndex(i));
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001817 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001818 result.AppendRawError (error_msg.GetString().c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001819 } else {
1820 // We didn't have only one match, otherwise we wouldn't get here.
1821 assert(num_matches == 0);
1822 result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
1823 }
Caroline Tice844d2302010-12-09 22:52:49 +00001824 result.SetStatus (eReturnStatusFailed);
1825 return false;
1826 }
1827
Greg Clayton5521f992011-10-28 21:38:01 +00001828 if (cmd_obj->IsMultiwordObject ())
1829 {
1830 if (!suffix.empty())
1831 {
1832
Enrico Granataaded51d2013-06-12 00:44:43 +00001833 result.AppendErrorWithFormat ("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
1834 cmd_obj->GetCommandName(),
1835 next_word.empty() ? "" : next_word.c_str(),
1836 next_word.empty() ? " -- " : " ",
Greg Clayton5521f992011-10-28 21:38:01 +00001837 suffix.c_str());
1838 result.SetStatus (eReturnStatusFailed);
1839 return false;
1840 }
1841 }
1842 else
1843 {
1844 // If we found a normal command, we are done
1845 done = true;
1846 if (!suffix.empty())
1847 {
1848 switch (suffix[0])
1849 {
1850 case '/':
1851 // GDB format suffixes
Greg Clayton52ec56c2011-10-29 00:57:28 +00001852 {
1853 Options *command_options = cmd_obj->GetOptions();
1854 if (command_options && command_options->SupportsLongOption("gdb-format"))
1855 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001856 std::string gdb_format_option ("--gdb-format=");
1857 gdb_format_option += (suffix.c_str() + 1);
1858
1859 bool inserted = false;
1860 std::string &cmd = revised_command_line.GetString();
1861 size_t arg_terminator_idx = FindArgumentTerminator (cmd);
1862 if (arg_terminator_idx != std::string::npos)
1863 {
1864 // Insert the gdb format option before the "--" that terminates options
1865 gdb_format_option.append(1,' ');
1866 cmd.insert(arg_terminator_idx, gdb_format_option);
1867 inserted = true;
1868 }
1869
1870 if (!inserted)
1871 revised_command_line.Printf (" %s", gdb_format_option.c_str());
1872
1873 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
1874 revised_command_line.PutCString (" --");
Greg Clayton52ec56c2011-10-29 00:57:28 +00001875 }
1876 else
1877 {
1878 result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n",
1879 cmd_obj->GetCommandName());
1880 result.SetStatus (eReturnStatusFailed);
1881 return false;
1882 }
1883 }
Greg Clayton5521f992011-10-28 21:38:01 +00001884 break;
Johnny Chen8e9383d2011-10-31 22:22:06 +00001885
1886 default:
1887 result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n",
1888 suffix.c_str());
1889 result.SetStatus (eReturnStatusFailed);
1890 return false;
1891
Greg Clayton5521f992011-10-28 21:38:01 +00001892 }
1893 }
1894 }
Caroline Tice844d2302010-12-09 22:52:49 +00001895 if (command_string.length() == 0)
1896 done = true;
1897
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001898 }
Caroline Tice844d2302010-12-09 22:52:49 +00001899
Greg Clayton5521f992011-10-28 21:38:01 +00001900 if (!command_string.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001901 revised_command_line.Printf (" %s", command_string.c_str());
1902
1903 // End of Phase 1.
1904 // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command
1905 // specified was valid; revised_command_line contains the complete command line (including command name(s)),
1906 // fully translated with all substitutions & translations taken care of (still in raw text format); and
1907 // wants_raw_input specifies whether the Execute method expects raw input or not.
1908
1909
1910 if (log)
1911 {
1912 log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1913 log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData());
1914 log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
1915 }
1916
1917 // Phase 2.
1918 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1919 // CommandObject, with the appropriate arguments.
1920
Ed Masted78c9572014-04-20 00:31:37 +00001921 if (cmd_obj != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001922 {
1923 if (add_to_history)
1924 {
1925 Args command_args (revised_command_line.GetData());
1926 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
Ed Masted78c9572014-04-20 00:31:37 +00001927 if (repeat_command != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001928 m_repeat_command.assign(repeat_command);
1929 else
Jim Inghama5a97eb2011-07-12 03:12:18 +00001930 m_repeat_command.assign(original_command_string.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001931
Enrico Granata7594f142013-06-17 22:51:50 +00001932 m_command_history.AppendString (original_command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001933 }
1934
1935 command_string = revised_command_line.GetData();
1936 std::string command_name (cmd_obj->GetCommandName());
Caroline Tice01274c02010-12-11 08:16:56 +00001937 std::string remainder;
1938 if (actual_cmd_name_len < command_string.length())
1939 remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter
1940 // than cmd_obj->GetCommandName(), because name completion
1941 // allows users to enter short versions of the names,
1942 // e.g. 'br s' for 'breakpoint set'.
Caroline Tice844d2302010-12-09 22:52:49 +00001943
1944 // Remove any initial spaces
1945 std::string white_space (" \t\v");
1946 size_t pos = remainder.find_first_not_of (white_space);
1947 if (pos != 0 && pos != std::string::npos)
Greg Claytona3482592011-04-22 20:58:45 +00001948 remainder.erase(0, pos);
Caroline Tice844d2302010-12-09 22:52:49 +00001949
1950 if (log)
Jason Molendabfb36ff2011-08-25 00:20:04 +00001951 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001952
Jim Ingham5a988412012-06-08 21:56:10 +00001953 cmd_obj->Execute (remainder.c_str(), result);
Caroline Tice844d2302010-12-09 22:52:49 +00001954 }
1955 else
1956 {
1957 // We didn't find the first command object, so complete the first argument.
1958 Args command_args (revised_command_line.GetData());
1959 StringList matches;
1960 int num_matches;
1961 int cursor_index = 0;
1962 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1963 bool word_complete;
1964 num_matches = HandleCompletionMatches (command_args,
1965 cursor_index,
1966 cursor_char_position,
1967 0,
1968 -1,
1969 word_complete,
1970 matches);
1971
1972 if (num_matches > 0)
1973 {
1974 std::string error_msg;
1975 error_msg.assign ("ambiguous command '");
1976 error_msg.append(command_args.GetArgumentAtIndex(0));
1977 error_msg.append ("'.");
1978
1979 error_msg.append (" Possible completions:");
1980 for (int i = 0; i < num_matches; i++)
1981 {
1982 error_msg.append ("\n\t");
1983 error_msg.append (matches.GetStringAtIndex (i));
1984 }
1985 error_msg.append ("\n");
Greg Claytonc7bece562013-01-25 18:06:21 +00001986 result.AppendRawError (error_msg.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001987 }
1988 else
1989 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1990
1991 result.SetStatus (eReturnStatusFailed);
1992 }
1993
Jason Molendabfb36ff2011-08-25 00:20:04 +00001994 if (log)
1995 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1996
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001997 return result.Succeeded();
1998}
1999
2000int
2001CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
2002 int &cursor_index,
2003 int &cursor_char_position,
2004 int match_start_point,
2005 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +00002006 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002007 StringList &matches)
2008{
2009 int num_command_matches = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002010 bool look_for_subcommand = false;
Jim Ingham558ce122010-06-30 05:02:46 +00002011
2012 // For any of the command completions a unique match will be a complete word.
2013 word_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002014
2015 if (cursor_index == -1)
2016 {
2017 // We got nothing on the command line, so return the list of commands
Jim Ingham279a6c22010-07-06 22:46:59 +00002018 bool include_aliases = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002019 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
2020 }
2021 else if (cursor_index == 0)
2022 {
2023 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Ingham279a6c22010-07-06 22:46:59 +00002024 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002025 num_command_matches = matches.GetSize();
2026
2027 if (num_command_matches == 1
2028 && cmd_obj && cmd_obj->IsMultiwordObject()
Ed Masted78c9572014-04-20 00:31:37 +00002029 && matches.GetStringAtIndex(0) != nullptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002030 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
2031 {
Greg Clayton765d2e22013-12-10 19:14:04 +00002032 if (parsed_line.GetArgumentCount() == 1)
2033 {
2034 word_complete = true;
2035 }
2036 else
2037 {
2038 look_for_subcommand = true;
2039 num_command_matches = 0;
2040 matches.DeleteStringAtIndex(0);
2041 parsed_line.AppendArgument ("");
2042 cursor_index++;
2043 cursor_char_position = 0;
2044 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002045 }
2046 }
2047
2048 if (cursor_index > 0 || look_for_subcommand)
2049 {
2050 // We are completing further on into a commands arguments, so find the command and tell it
2051 // to complete the command.
2052 // First see if there is a matching initial command:
Jim Ingham279a6c22010-07-06 22:46:59 +00002053 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Ed Masted78c9572014-04-20 00:31:37 +00002054 if (command_object == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002055 {
2056 return 0;
2057 }
2058 else
2059 {
2060 parsed_line.Shift();
2061 cursor_index--;
Greg Claytona7015092010-09-18 01:14:36 +00002062 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton66111032010-06-23 01:19:29 +00002063 cursor_index,
2064 cursor_char_position,
2065 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002066 max_return_elements,
2067 word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002068 matches);
2069 }
2070 }
2071
2072 return num_command_matches;
2073
2074}
2075
2076int
2077CommandInterpreter::HandleCompletion (const char *current_line,
2078 const char *cursor,
2079 const char *last_char,
2080 int match_start_point,
2081 int max_return_elements,
2082 StringList &matches)
2083{
2084 // We parse the argument up to the cursor, so the last argument in parsed_line is
2085 // the one containing the cursor, and the cursor is after the last character.
2086
Pavel Labath00b7f952015-03-02 12:46:22 +00002087 Args parsed_line(llvm::StringRef(current_line, last_char - current_line));
2088 Args partial_parsed_line(llvm::StringRef(current_line, cursor - current_line));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002089
Jim Inghama5a97eb2011-07-12 03:12:18 +00002090 // Don't complete comments, and if the line we are completing is just the history repeat character,
2091 // substitute the appropriate history line.
2092 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
2093 if (first_arg)
2094 {
2095 if (first_arg[0] == m_comment_char)
2096 return 0;
Enrico Granata7594f142013-06-17 22:51:50 +00002097 else if (first_arg[0] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00002098 {
Enrico Granata7594f142013-06-17 22:51:50 +00002099 const char *history_string = m_command_history.FindString (first_arg);
Ed Masted78c9572014-04-20 00:31:37 +00002100 if (history_string != nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00002101 {
2102 matches.Clear();
2103 matches.InsertStringAtIndex(0, history_string);
2104 return -2;
2105 }
2106 else
2107 return 0;
2108
2109 }
2110 }
2111
2112
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002113 int num_args = partial_parsed_line.GetArgumentCount();
2114 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
2115 int cursor_char_position;
2116
2117 if (cursor_index == -1)
2118 cursor_char_position = 0;
2119 else
2120 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
Jim Inghamfe0c4252010-12-14 19:56:01 +00002121
2122 if (cursor > current_line && cursor[-1] == ' ')
2123 {
2124 // We are just after a space. If we are in an argument, then we will continue
2125 // parsing, but if we are between arguments, then we have to complete whatever the next
2126 // element would be.
2127 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
2128 // protected by a quote) then the space will also be in the parsed argument...
2129
2130 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
2131 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
2132 {
Greg Clayton765d2e22013-12-10 19:14:04 +00002133 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
Jim Inghamfe0c4252010-12-14 19:56:01 +00002134 cursor_index++;
2135 cursor_char_position = 0;
2136 }
2137 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002138
2139 int num_command_matches;
2140
2141 matches.Clear();
2142
2143 // Only max_return_elements == -1 is supported at present:
2144 assert (max_return_elements == -1);
Jim Ingham558ce122010-06-30 05:02:46 +00002145 bool word_complete;
Greg Clayton66111032010-06-23 01:19:29 +00002146 num_command_matches = HandleCompletionMatches (parsed_line,
2147 cursor_index,
2148 cursor_char_position,
2149 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002150 max_return_elements,
2151 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +00002152 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153
2154 if (num_command_matches <= 0)
2155 return num_command_matches;
2156
2157 if (num_args == 0)
2158 {
2159 // If we got an empty string, insert nothing.
2160 matches.InsertStringAtIndex(0, "");
2161 }
2162 else
2163 {
2164 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
2165 // put an empty string in element 0.
2166 std::string command_partial_str;
2167 if (cursor_index >= 0)
Jim Ingham49e80a12010-10-22 18:47:16 +00002168 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
2169 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002170
2171 std::string common_prefix;
2172 matches.LongestCommonPrefix (common_prefix);
Greg Claytonc7bece562013-01-25 18:06:21 +00002173 const size_t partial_name_len = command_partial_str.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002174
2175 // If we matched a unique single command, add a space...
Jim Ingham558ce122010-06-30 05:02:46 +00002176 // Only do this if the completer told us this was a complete word, however...
2177 if (num_command_matches == 1 && word_complete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002178 {
2179 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
2180 if (quote_char != '\0')
2181 common_prefix.push_back(quote_char);
2182
2183 common_prefix.push_back(' ');
2184 }
2185 common_prefix.erase (0, partial_name_len);
2186 matches.InsertStringAtIndex(0, common_prefix.c_str());
2187 }
2188 return num_command_matches;
2189}
2190
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002191
2192CommandInterpreter::~CommandInterpreter ()
2193{
2194}
2195
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002196void
Greg Clayton44d93782014-01-27 23:43:24 +00002197CommandInterpreter::UpdatePrompt (const char *new_prompt)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002198{
Greg Clayton44d93782014-01-27 23:43:24 +00002199 EventSP prompt_change_event_sp (new Event(eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
2200 BroadcastEvent (prompt_change_event_sp);
2201 if (m_command_io_handler_sp)
2202 m_command_io_handler_sp->SetPrompt(new_prompt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002203}
2204
Jim Ingham97a6dc72010-10-04 19:49:29 +00002205
2206bool
2207CommandInterpreter::Confirm (const char *message, bool default_answer)
2208{
Jim Ingham3bcdb292010-10-04 22:44:14 +00002209 // Check AutoConfirm first:
2210 if (m_debugger.GetAutoConfirm())
2211 return default_answer;
Greg Clayton44d93782014-01-27 23:43:24 +00002212
2213 IOHandlerConfirm *confirm = new IOHandlerConfirm(m_debugger,
2214 message,
2215 default_answer);
2216 IOHandlerSP io_handler_sp (confirm);
2217 m_debugger.RunIOHandler (io_handler_sp);
2218 return confirm->GetResponse();
Jim Ingham97a6dc72010-10-04 19:49:29 +00002219}
2220
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002221OptionArgVectorSP
2222CommandInterpreter::GetAliasOptions (const char *alias_name)
2223{
2224 OptionArgMap::iterator pos;
2225 OptionArgVectorSP ret_val;
2226
2227 std::string alias (alias_name);
2228
2229 if (HasAliasOptions())
2230 {
2231 pos = m_alias_options.find (alias);
2232 if (pos != m_alias_options.end())
2233 ret_val = pos->second;
2234 }
2235
2236 return ret_val;
2237}
2238
2239void
2240CommandInterpreter::RemoveAliasOptions (const char *alias_name)
2241{
2242 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
2243 if (pos != m_alias_options.end())
2244 {
2245 m_alias_options.erase (pos);
2246 }
2247}
2248
2249void
2250CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
2251{
2252 m_alias_options[alias_name] = option_arg_vector_sp;
2253}
2254
2255bool
2256CommandInterpreter::HasCommands ()
2257{
2258 return (!m_command_dict.empty());
2259}
2260
2261bool
2262CommandInterpreter::HasAliases ()
2263{
2264 return (!m_alias_dict.empty());
2265}
2266
2267bool
2268CommandInterpreter::HasUserCommands ()
2269{
2270 return (!m_user_dict.empty());
2271}
2272
2273bool
2274CommandInterpreter::HasAliasOptions ()
2275{
2276 return (!m_alias_options.empty());
2277}
2278
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002279void
Caroline Tice4ab31c92010-10-12 21:57:09 +00002280CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2281 const char *alias_name,
2282 Args &cmd_args,
Caroline Ticed9d63362010-12-07 19:58:26 +00002283 std::string &raw_input_string,
Caroline Tice4ab31c92010-10-12 21:57:09 +00002284 CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002285{
2286 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
Caroline Ticed9d63362010-12-07 19:58:26 +00002287
2288 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002289
Caroline Ticed9d63362010-12-07 19:58:26 +00002290 // Make sure that the alias name is the 0th element in cmd_args
2291 std::string alias_name_str = alias_name;
2292 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2293 cmd_args.Unshift (alias_name);
2294
2295 Args new_args (alias_cmd_obj->GetCommandName());
2296 if (new_args.GetArgumentCount() == 2)
2297 new_args.Shift();
2298
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002299 if (option_arg_vector_sp.get())
2300 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002301 if (wants_raw_input)
2302 {
2303 // We have a command that both has command options and takes raw input. Make *sure* it has a
2304 // " -- " in the right place in the raw_input_string.
2305 size_t pos = raw_input_string.find(" -- ");
2306 if (pos == std::string::npos)
2307 {
2308 // None found; assume it goes at the beginning of the raw input string
2309 raw_input_string.insert (0, " -- ");
2310 }
2311 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002312
2313 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00002314 const size_t old_size = cmd_args.GetArgumentCount();
Caroline Tice4ab31c92010-10-12 21:57:09 +00002315 std::vector<bool> used (old_size + 1, false);
2316
2317 used[0] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002318
Andy Gibbsa297a972013-06-19 19:04:53 +00002319 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002320 {
2321 OptionArgPair option_pair = (*option_arg_vector)[i];
Caroline Ticed9d63362010-12-07 19:58:26 +00002322 OptionArgValue value_pair = option_pair.second;
2323 int value_type = value_pair.first;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002324 std::string option = option_pair.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00002325 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002326 if (option.compare ("<argument>") == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002327 {
2328 if (!wants_raw_input
2329 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2330 new_args.AppendArgument (value.c_str());
2331 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002332 else
2333 {
Virgile Belloe2607b52013-09-05 16:42:23 +00002334 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002335 new_args.AppendArgument (option.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002336 if (value.compare ("<no-argument>") != 0)
2337 {
2338 int index = GetOptionArgumentPosition (value.c_str());
2339 if (index == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002340 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002341 // value was NOT a positional argument; must be a real value
Virgile Belloe2607b52013-09-05 16:42:23 +00002342 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002343 new_args.AppendArgument (value.c_str());
2344 else
2345 {
2346 char buffer[255];
2347 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2348 new_args.AppendArgument (buffer);
2349 }
2350
2351 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002352 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002353 {
2354 result.AppendErrorWithFormat
2355 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2356 index);
2357 result.SetStatus (eReturnStatusFailed);
2358 return;
2359 }
2360 else
2361 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002362 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2363 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2364 if (strpos != std::string::npos)
2365 {
2366 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2367 }
2368
Virgile Belloe2607b52013-09-05 16:42:23 +00002369 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002370 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2371 else
2372 {
2373 char buffer[255];
2374 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2375 cmd_args.GetArgumentAtIndex (index));
2376 new_args.AppendArgument (buffer);
2377 }
Caroline Tice4ab31c92010-10-12 21:57:09 +00002378 used[index] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002379 }
2380 }
2381 }
2382 }
2383
Andy Gibbsa297a972013-06-19 19:04:53 +00002384 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002385 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002386 if (!used[j] && !wants_raw_input)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002387 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2388 }
2389
2390 cmd_args.Clear();
2391 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2392 }
2393 else
2394 {
2395 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Caroline Ticed9d63362010-12-07 19:58:26 +00002396 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2397 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2398 // input string.
2399 if (wants_raw_input)
2400 {
2401 cmd_args.Clear();
2402 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2403 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002404 return;
2405 }
2406
2407 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2408 return;
2409}
2410
2411
2412int
2413CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2414{
2415 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2416 // of zero.
2417
2418 char *cptr = (char *) in_string;
2419
2420 // Does it start with '%'
2421 if (cptr[0] == '%')
2422 {
2423 ++cptr;
2424
2425 // Is the rest of it entirely digits?
2426 if (isdigit (cptr[0]))
2427 {
2428 const char *start = cptr;
2429 while (isdigit (cptr[0]))
2430 ++cptr;
2431
2432 // We've gotten to the end of the digits; are we at the end of the string?
2433 if (cptr[0] == '\0')
2434 position = atoi (start);
2435 }
2436 }
2437
2438 return position;
2439}
2440
2441void
2442CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2443{
Jim Ingham16e0c682011-08-12 23:34:31 +00002444 FileSpec init_file;
Greg Clayton14a35512011-09-11 00:01:44 +00002445 if (in_cwd)
Jim Ingham16e0c682011-08-12 23:34:31 +00002446 {
Greg Clayton14a35512011-09-11 00:01:44 +00002447 // In the current working directory we don't load any program specific
2448 // .lldbinit files, we only look for a "./.lldbinit" file.
2449 if (m_skip_lldbinit_files)
2450 return;
2451
2452 init_file.SetFile ("./.lldbinit", true);
Jim Ingham16e0c682011-08-12 23:34:31 +00002453 }
Greg Clayton14a35512011-09-11 00:01:44 +00002454 else
Jim Ingham16e0c682011-08-12 23:34:31 +00002455 {
Greg Clayton14a35512011-09-11 00:01:44 +00002456 // If we aren't looking in the current working directory we are looking
2457 // in the home directory. We will first see if there is an application
2458 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2459 // "-" and the name of the program. If this file doesn't exist, we fall
2460 // back to just the "~/.lldbinit" file. We also obey any requests to not
2461 // load the init files.
Zachary Turnera21fee02014-08-21 21:49:24 +00002462 llvm::SmallString<64> home_dir_path;
2463 llvm::sys::path::home_directory(home_dir_path);
2464 FileSpec profilePath(home_dir_path.c_str(), false);
Zachary Turner9e757b72014-07-28 16:45:05 +00002465 profilePath.AppendPathComponent(".lldbinit");
2466 std::string init_file_path = profilePath.GetPath();
Greg Clayton14a35512011-09-11 00:01:44 +00002467
2468 if (m_skip_app_init_files == false)
2469 {
Zachary Turnera21fee02014-08-21 21:49:24 +00002470 FileSpec program_file_spec(HostInfo::GetProgramFileSpec());
Greg Clayton14a35512011-09-11 00:01:44 +00002471 const char *program_name = program_file_spec.GetFilename().AsCString();
Jim Ingham16e0c682011-08-12 23:34:31 +00002472
Greg Clayton14a35512011-09-11 00:01:44 +00002473 if (program_name)
2474 {
2475 char program_init_file_name[PATH_MAX];
Zachary Turner9e757b72014-07-28 16:45:05 +00002476 ::snprintf (program_init_file_name, sizeof(program_init_file_name), "%s-%s", init_file_path.c_str(), program_name);
Greg Clayton14a35512011-09-11 00:01:44 +00002477 init_file.SetFile (program_init_file_name, true);
2478 if (!init_file.Exists())
2479 init_file.Clear();
2480 }
2481 }
2482
2483 if (!init_file && !m_skip_lldbinit_files)
Zachary Turner9e757b72014-07-28 16:45:05 +00002484 init_file.SetFile (init_file_path.c_str(), false);
Greg Clayton14a35512011-09-11 00:01:44 +00002485 }
2486
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002487 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2488 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2489
2490 if (init_file.Exists())
2491 {
Greg Clayton44d93782014-01-27 23:43:24 +00002492 const bool saved_batch = SetBatchCommandMode (true);
Jim Ingham26c7bf92014-10-11 00:38:27 +00002493 CommandInterpreterRunOptions options;
2494 options.SetSilent (true);
2495 options.SetStopOnError (false);
2496 options.SetStopOnContinue (true);
2497
Greg Clayton340b0302014-02-05 17:57:57 +00002498 HandleCommandsFromFile (init_file,
Ed Masted78c9572014-04-20 00:31:37 +00002499 nullptr, // Execution context
Jim Ingham26c7bf92014-10-11 00:38:27 +00002500 options,
Greg Clayton340b0302014-02-05 17:57:57 +00002501 result);
Greg Clayton44d93782014-01-27 23:43:24 +00002502 SetBatchCommandMode (saved_batch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002503 }
2504 else
2505 {
2506 // nothing to be done if the file doesn't exist
2507 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2508 }
2509}
2510
Kate Stonea487aa42015-01-15 00:52:41 +00002511const char *
2512CommandInterpreter::GetCommandPrefix()
2513{
2514 const char * prefix = GetDebugger().GetIOHandlerCommandPrefix();
2515 return prefix == NULL ? "" : prefix;
2516}
2517
Greg Clayton8b82f082011-04-12 05:54:46 +00002518PlatformSP
2519CommandInterpreter::GetPlatform (bool prefer_target_platform)
2520{
2521 PlatformSP platform_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +00002522 if (prefer_target_platform)
2523 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002524 ExecutionContext exe_ctx(GetExecutionContext());
2525 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002526 if (target)
2527 platform_sp = target->GetPlatform();
2528 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002529
2530 if (!platform_sp)
2531 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2532 return platform_sp;
2533}
2534
Jim Inghame16c50a2011-02-18 00:54:25 +00002535void
Jim Inghambad87fe2011-03-11 01:51:49 +00002536CommandInterpreter::HandleCommands (const StringList &commands,
Jim Ingham26c7bf92014-10-11 00:38:27 +00002537 ExecutionContext *override_context,
2538 CommandInterpreterRunOptions &options,
Jim Inghame16c50a2011-02-18 00:54:25 +00002539 CommandReturnObject &result)
2540{
2541 size_t num_lines = commands.GetSize();
Jim Inghame16c50a2011-02-18 00:54:25 +00002542
2543 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2544 // Make sure you reset this value anywhere you return from the function.
2545
2546 bool old_async_execution = m_debugger.GetAsyncExecution();
2547
2548 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2549 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2550
Ed Masted78c9572014-04-20 00:31:37 +00002551 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00002552 UpdateExecutionContext (override_context);
Jim Inghame16c50a2011-02-18 00:54:25 +00002553
Jim Ingham26c7bf92014-10-11 00:38:27 +00002554 if (!options.GetStopOnContinue())
Jim Inghame16c50a2011-02-18 00:54:25 +00002555 {
2556 m_debugger.SetAsyncExecution (false);
2557 }
2558
Andy Gibbsa297a972013-06-19 19:04:53 +00002559 for (size_t idx = 0; idx < num_lines; idx++)
Jim Inghame16c50a2011-02-18 00:54:25 +00002560 {
2561 const char *cmd = commands.GetStringAtIndex(idx);
2562 if (cmd[0] == '\0')
2563 continue;
2564
Jim Ingham26c7bf92014-10-11 00:38:27 +00002565 if (options.GetEchoCommands())
Jim Inghame16c50a2011-02-18 00:54:25 +00002566 {
2567 result.AppendMessageWithFormat ("%s %s\n",
Greg Clayton44d93782014-01-27 23:43:24 +00002568 m_debugger.GetPrompt(),
2569 cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002570 }
2571
Greg Clayton9d0402b2011-02-20 02:15:07 +00002572 CommandReturnObject tmp_result;
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002573 // If override_context is not NULL, pass no_context_switching = true for
2574 // HandleCommand() since we updated our context already.
Jim Ingham076b7fc2013-05-02 23:15:37 +00002575
2576 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2577 // m_command_source_depth dingus is the way we turn off adding to the history in that case, so set it up here.
Jim Ingham26c7bf92014-10-11 00:38:27 +00002578 if (!options.GetAddToHistory())
Jim Ingham076b7fc2013-05-02 23:15:37 +00002579 m_command_source_depth++;
Jim Ingham26c7bf92014-10-11 00:38:27 +00002580 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result,
Ed Masted78c9572014-04-20 00:31:37 +00002581 nullptr, /* override_context */
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002582 true, /* repeat_on_empty_command */
Ed Masted78c9572014-04-20 00:31:37 +00002583 override_context != nullptr /* no_context_switching */);
Jim Ingham26c7bf92014-10-11 00:38:27 +00002584 if (!options.GetAddToHistory())
Jim Ingham076b7fc2013-05-02 23:15:37 +00002585 m_command_source_depth--;
Jim Inghame16c50a2011-02-18 00:54:25 +00002586
Jim Ingham26c7bf92014-10-11 00:38:27 +00002587 if (options.GetPrintResults())
Jim Inghame16c50a2011-02-18 00:54:25 +00002588 {
2589 if (tmp_result.Succeeded())
Jim Ingham85e8b812011-02-19 02:53:09 +00002590 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
Jim Inghame16c50a2011-02-18 00:54:25 +00002591 }
2592
2593 if (!success || !tmp_result.Succeeded())
2594 {
Jim Inghama5038812012-04-24 02:25:07 +00002595 const char *error_msg = tmp_result.GetErrorData();
Ed Masted78c9572014-04-20 00:31:37 +00002596 if (error_msg == nullptr || error_msg[0] == '\0')
Jim Inghama5038812012-04-24 02:25:07 +00002597 error_msg = "<unknown error>.\n";
Jim Ingham26c7bf92014-10-11 00:38:27 +00002598 if (options.GetStopOnError())
Jim Inghame16c50a2011-02-18 00:54:25 +00002599 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002600 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' failed with %s",
2601 (uint64_t)idx, cmd, error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002602 result.SetStatus (eReturnStatusFailed);
2603 m_debugger.SetAsyncExecution (old_async_execution);
2604 return;
2605 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002606 else if (options.GetPrintResults())
Jim Inghame16c50a2011-02-18 00:54:25 +00002607 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002608 result.AppendMessageWithFormat ("Command #%" PRIu64 " '%s' failed with %s",
2609 (uint64_t)idx + 1,
Jim Inghame16c50a2011-02-18 00:54:25 +00002610 cmd,
Jim Inghama5038812012-04-24 02:25:07 +00002611 error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002612 }
2613 }
2614
Caroline Tice969ed3d2011-05-02 20:41:46 +00002615 if (result.GetImmediateOutputStream())
2616 result.GetImmediateOutputStream()->Flush();
2617
2618 if (result.GetImmediateErrorStream())
2619 result.GetImmediateErrorStream()->Flush();
2620
Jim Inghame16c50a2011-02-18 00:54:25 +00002621 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2622 // could be running (for instance in Breakpoint Commands.
2623 // So we check the return value to see if it is has running in it.
2624 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2625 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2626 {
Jim Ingham26c7bf92014-10-11 00:38:27 +00002627 if (options.GetStopOnContinue())
Jim Inghame16c50a2011-02-18 00:54:25 +00002628 {
2629 // If we caused the target to proceed, and we're going to stop in that case, set the
2630 // status in our real result before returning. This is an error if the continue was not the
2631 // last command in the set of commands to be run.
2632 if (idx != num_lines - 1)
Deepak Panickal99fbc072014-03-03 15:39:47 +00002633 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' continued the target.\n",
2634 (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002635 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00002636 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' continued the target.\n", (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002637
2638 result.SetStatus(tmp_result.GetStatus());
2639 m_debugger.SetAsyncExecution (old_async_execution);
2640
2641 return;
2642 }
2643 }
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002644
2645 // Also check for "stop on crash here:
2646 bool should_stop = false;
2647 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash())
2648 {
2649 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2650 if (target_sp)
2651 {
2652 ProcessSP process_sp (target_sp->GetProcessSP());
2653 if (process_sp)
2654 {
2655 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
2656 {
2657 StopReason reason = thread_sp->GetStopReason();
2658 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
2659 {
2660 should_stop = true;
2661 break;
2662 }
2663 }
2664 }
2665 }
2666 if (should_stop)
2667 {
2668 if (idx != num_lines - 1)
2669 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' stopped with a signal or exception.\n",
2670 (uint64_t)idx + 1, cmd);
2671 else
2672 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", (uint64_t)idx + 1, cmd);
2673
2674 result.SetStatus(tmp_result.GetStatus());
2675 m_debugger.SetAsyncExecution (old_async_execution);
2676
2677 return;
2678 }
2679 }
Jim Inghame16c50a2011-02-18 00:54:25 +00002680
2681 }
2682
2683 result.SetStatus (eReturnStatusSuccessFinishResult);
2684 m_debugger.SetAsyncExecution (old_async_execution);
2685
2686 return;
2687}
2688
Greg Clayton340b0302014-02-05 17:57:57 +00002689// Make flags that we can pass into the IOHandler so our delegates can do the right thing
2690enum {
2691 eHandleCommandFlagStopOnContinue = (1u << 0),
2692 eHandleCommandFlagStopOnError = (1u << 1),
2693 eHandleCommandFlagEchoCommand = (1u << 2),
Jim Ingham26c7bf92014-10-11 00:38:27 +00002694 eHandleCommandFlagPrintResult = (1u << 3),
2695 eHandleCommandFlagStopOnCrash = (1u << 4)
Greg Clayton340b0302014-02-05 17:57:57 +00002696};
2697
Jim Inghame16c50a2011-02-18 00:54:25 +00002698void
2699CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2700 ExecutionContext *context,
Jim Ingham26c7bf92014-10-11 00:38:27 +00002701 CommandInterpreterRunOptions &options,
Jim Inghame16c50a2011-02-18 00:54:25 +00002702 CommandReturnObject &result)
2703{
2704 if (cmd_file.Exists())
2705 {
Greg Clayton44d93782014-01-27 23:43:24 +00002706 StreamFileSP input_file_sp (new StreamFile());
2707
2708 std::string cmd_file_path = cmd_file.GetPath();
2709 Error error = input_file_sp->GetFile().Open(cmd_file_path.c_str(), File::eOpenOptionRead);
2710
2711 if (error.Success())
2712 {
2713 Debugger &debugger = GetDebugger();
2714
Greg Clayton340b0302014-02-05 17:57:57 +00002715 uint32_t flags = 0;
2716
Jim Ingham26c7bf92014-10-11 00:38:27 +00002717 if (options.m_stop_on_continue == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002718 {
2719 if (m_command_source_flags.empty())
2720 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002721 // Stop on continue by default
Greg Clayton340b0302014-02-05 17:57:57 +00002722 flags |= eHandleCommandFlagStopOnContinue;
2723 }
2724 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnContinue)
2725 {
2726 flags |= eHandleCommandFlagStopOnContinue;
2727 }
2728 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002729 else if (options.m_stop_on_continue == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002730 {
2731 flags |= eHandleCommandFlagStopOnContinue;
2732 }
2733
Jim Ingham26c7bf92014-10-11 00:38:27 +00002734 if (options.m_stop_on_error == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002735 {
2736 if (m_command_source_flags.empty())
2737 {
2738 if (GetStopCmdSourceOnError())
2739 flags |= eHandleCommandFlagStopOnError;
2740 }
2741 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError)
2742 {
2743 flags |= eHandleCommandFlagStopOnError;
2744 }
2745 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002746 else if (options.m_stop_on_error == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002747 {
2748 flags |= eHandleCommandFlagStopOnError;
2749 }
2750
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002751 if (options.GetStopOnCrash())
2752 {
2753 if (m_command_source_flags.empty())
2754 {
2755 // Echo command by default
2756 flags |= eHandleCommandFlagStopOnCrash;
2757 }
2758 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash)
2759 {
2760 flags |= eHandleCommandFlagStopOnCrash;
2761 }
2762 }
2763
Jim Ingham26c7bf92014-10-11 00:38:27 +00002764 if (options.m_echo_commands == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002765 {
2766 if (m_command_source_flags.empty())
2767 {
2768 // Echo command by default
2769 flags |= eHandleCommandFlagEchoCommand;
2770 }
2771 else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand)
2772 {
2773 flags |= eHandleCommandFlagEchoCommand;
2774 }
2775 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002776 else if (options.m_echo_commands == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002777 {
2778 flags |= eHandleCommandFlagEchoCommand;
2779 }
2780
Jim Ingham26c7bf92014-10-11 00:38:27 +00002781 if (options.m_print_results == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002782 {
2783 if (m_command_source_flags.empty())
2784 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002785 // Print output by default
2786 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002787 }
Greg Claytone4e462c2014-02-05 21:03:22 +00002788 else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult)
Greg Clayton340b0302014-02-05 17:57:57 +00002789 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002790 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002791 }
2792 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002793 else if (options.m_print_results == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002794 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002795 flags |= eHandleCommandFlagPrintResult;
2796 }
2797
2798 if (flags & eHandleCommandFlagPrintResult)
2799 {
Greg Clayton8ee67312014-02-05 21:46:20 +00002800 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", cmd_file_path.c_str());
Greg Clayton340b0302014-02-05 17:57:57 +00002801 }
2802
2803 // Used for inheriting the right settings when "command source" might have
2804 // nested "command source" commands
Greg Claytone4e462c2014-02-05 21:03:22 +00002805 lldb::StreamFileSP empty_stream_sp;
Greg Clayton340b0302014-02-05 17:57:57 +00002806 m_command_source_flags.push_back(flags);
Greg Clayton44d93782014-01-27 23:43:24 +00002807 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00002808 IOHandler::Type::CommandInterpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00002809 input_file_sp,
Greg Claytone4e462c2014-02-05 21:03:22 +00002810 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader output stream
2811 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader error stream
Greg Clayton340b0302014-02-05 17:57:57 +00002812 flags,
Ed Masted78c9572014-04-20 00:31:37 +00002813 nullptr, // Pass in NULL for "editline_name" so no history is saved, or written
Greg Clayton8ee67312014-02-05 21:46:20 +00002814 debugger.GetPrompt(),
Kate Stonee30f11d2014-11-17 19:06:59 +00002815 NULL,
Greg Clayton44d93782014-01-27 23:43:24 +00002816 false, // Not multi-line
Kate Stonee30f11d2014-11-17 19:06:59 +00002817 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00002818 0,
Greg Clayton44d93782014-01-27 23:43:24 +00002819 *this));
Greg Clayton8ee67312014-02-05 21:46:20 +00002820 const bool old_async_execution = debugger.GetAsyncExecution();
2821
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002822 // Set synchronous execution if we are not stopping on continue
Greg Clayton8ee67312014-02-05 21:46:20 +00002823 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2824 debugger.SetAsyncExecution (false);
2825
Greg Clayton340b0302014-02-05 17:57:57 +00002826 m_command_source_depth++;
Greg Clayton8ee67312014-02-05 21:46:20 +00002827
2828 debugger.RunIOHandler(io_handler_sp);
Greg Clayton340b0302014-02-05 17:57:57 +00002829 if (!m_command_source_flags.empty())
2830 m_command_source_flags.pop_back();
2831 m_command_source_depth--;
Greg Clayton44d93782014-01-27 23:43:24 +00002832 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton8ee67312014-02-05 21:46:20 +00002833 debugger.SetAsyncExecution (old_async_execution);
Greg Clayton44d93782014-01-27 23:43:24 +00002834 }
2835 else
2836 {
2837 result.AppendErrorWithFormat ("error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), error.AsCString());
2838 result.SetStatus (eReturnStatusFailed);
2839 }
Greg Clayton8ee67312014-02-05 21:46:20 +00002840
2841
Jim Inghame16c50a2011-02-18 00:54:25 +00002842 }
2843 else
2844 {
2845 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
Jim Ingham4af59612014-12-19 19:20:44 +00002846 cmd_file.GetFilename().AsCString("<Unknown>"));
Jim Inghame16c50a2011-02-18 00:54:25 +00002847 result.SetStatus (eReturnStatusFailed);
2848 return;
2849 }
2850}
2851
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002852ScriptInterpreter *
Enrico Granatab5887262012-10-29 21:18:03 +00002853CommandInterpreter::GetScriptInterpreter (bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002854{
Ed Masted78c9572014-04-20 00:31:37 +00002855 if (m_script_interpreter_ap.get() != nullptr)
Enrico Granatab5887262012-10-29 21:18:03 +00002856 return m_script_interpreter_ap.get();
2857
2858 if (!can_create)
Ed Masted78c9572014-04-20 00:31:37 +00002859 return nullptr;
Enrico Granatab5887262012-10-29 21:18:03 +00002860
Enrico Granataa29bdad2012-07-10 18:23:48 +00002861 // <rdar://problem/11751427>
2862 // we need to protect the initialization of the script interpreter
2863 // otherwise we could end up with two threads both trying to create
2864 // their instance of it, and for some languages (e.g. Python)
2865 // this is a bulletproof recipe for disaster!
2866 // this needs to be a function-level static because multiple Debugger instances living in the same process
2867 // still need to be isolated and not try to initialize Python concurrently
Enrico Granata8b95df22012-07-10 19:04:14 +00002868 static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive);
2869 Mutex::Locker interpreter_lock(g_interpreter_mutex);
Enrico Granataa29bdad2012-07-10 18:23:48 +00002870
Greg Clayton5160ce52013-03-27 23:08:40 +00002871 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Enrico Granatab5887262012-10-29 21:18:03 +00002872 if (log)
2873 log->Printf("Initializing the ScriptInterpreter now\n");
Greg Clayton66111032010-06-23 01:19:29 +00002874
Caroline Tice2f88aad2011-01-14 00:29:16 +00002875 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2876 switch (script_lang)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002877 {
Greg Claytondce502e2011-11-04 03:34:56 +00002878 case eScriptLanguagePython:
2879#ifndef LLDB_DISABLE_PYTHON
2880 m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this));
2881 break;
2882#else
2883 // Fall through to the None case when python is disabled
2884#endif
Caroline Tice2f88aad2011-01-14 00:29:16 +00002885 case eScriptLanguageNone:
2886 m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this));
2887 break;
Caroline Tice2f88aad2011-01-14 00:29:16 +00002888 };
2889
2890 return m_script_interpreter_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002891}
2892
2893
2894
2895bool
2896CommandInterpreter::GetSynchronous ()
2897{
2898 return m_synchronous_execution;
2899}
2900
2901void
2902CommandInterpreter::SetSynchronous (bool value)
2903{
Johnny Chenc066ab42010-10-14 01:22:03 +00002904 m_synchronous_execution = value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002905}
2906
2907void
2908CommandInterpreter::OutputFormattedHelpText (Stream &strm,
Kate Stonea487aa42015-01-15 00:52:41 +00002909 const char *prefix,
2910 const char *help_text)
2911{
2912 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2913 if (prefix == NULL)
2914 prefix = "";
2915
2916 size_t prefix_width = strlen(prefix);
2917 size_t line_width_max = max_columns - prefix_width;
2918 const char *help_text_end = help_text + strlen(help_text);
2919 const char *line_start = help_text;
2920 if (line_width_max < 16)
2921 line_width_max = help_text_end - help_text + prefix_width;
2922
2923 strm.IndentMore (prefix_width);
2924 while (line_start < help_text_end)
2925 {
2926 // Break each line at the first newline or last space/tab before
2927 // the maximum number of characters that fit on a line. Lines with no
2928 // natural break are left unbroken to wrap.
2929 const char *line_end = help_text_end;
2930 const char *line_scan = line_start;
2931 const char *line_scan_end = help_text_end;
2932 while (line_scan < line_scan_end)
2933 {
2934 char next = *line_scan;
2935 if (next == '\t' || next == ' ')
2936 {
2937 line_end = line_scan;
2938 line_scan_end = line_start + line_width_max;
2939 }
2940 else if (next == '\n' || next == '\0')
2941 {
2942 line_end = line_scan;
2943 break;
2944 }
2945 ++line_scan;
2946 }
2947
2948 // Prefix the first line, indent subsequent lines to line up
2949 if (line_start == help_text)
2950 strm.Write (prefix, prefix_width);
2951 else
2952 strm.Indent();
2953 strm.Write (line_start, line_end - line_start);
2954 strm.EOL();
2955
2956 // When a line breaks at whitespace consume it before continuing
2957 line_start = line_end;
2958 char next = *line_start;
2959 if (next == '\n')
2960 ++line_start;
2961 else while (next == ' ' || next == '\t')
2962 next = *(++line_start);
2963 }
2964 strm.IndentLess (prefix_width);
2965}
2966
2967void
2968CommandInterpreter::OutputFormattedHelpText (Stream &strm,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002969 const char *word_text,
2970 const char *separator,
2971 const char *help_text,
Greg Claytonc7bece562013-01-25 18:06:21 +00002972 size_t max_word_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002973{
Kate Stonea487aa42015-01-15 00:52:41 +00002974 StreamString prefix_stream;
2975 prefix_stream.Printf (" %-*s %s ", (int)max_word_len, word_text, separator);
2976 OutputFormattedHelpText (strm, prefix_stream.GetData(), help_text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002977}
2978
2979void
Enrico Granata82a7d982011-07-07 00:38:40 +00002980CommandInterpreter::OutputHelpText (Stream &strm,
2981 const char *word_text,
2982 const char *separator,
2983 const char *help_text,
2984 uint32_t max_word_len)
2985{
2986 int indent_size = max_word_len + strlen (separator) + 2;
2987
2988 strm.IndentMore (indent_size);
2989
2990 StreamString text_strm;
2991 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2992
2993 const uint32_t max_columns = m_debugger.GetTerminalWidth();
Enrico Granata82a7d982011-07-07 00:38:40 +00002994
2995 size_t len = text_strm.GetSize();
2996 const char *text = text_strm.GetData();
2997
2998 uint32_t chars_left = max_columns;
2999
3000 for (uint32_t i = 0; i < len; i++)
3001 {
3002 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
3003 {
Enrico Granata82a7d982011-07-07 00:38:40 +00003004 chars_left = max_columns - indent_size;
3005 strm.EOL();
3006 strm.Indent();
3007 }
3008 else
3009 {
3010 strm.PutChar(text[i]);
3011 chars_left--;
3012 }
3013
3014 }
3015
3016 strm.EOL();
3017 strm.IndentLess(indent_size);
3018}
3019
3020void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003021CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
Jim Inghamaf3753e2013-05-17 01:30:37 +00003022 StringList &commands_help, bool search_builtin_commands, bool search_user_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003023{
3024 CommandObject::CommandMap::const_iterator pos;
3025
Jim Inghamaf3753e2013-05-17 01:30:37 +00003026 if (search_builtin_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003027 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00003028 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003029 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00003030 const char *command_name = pos->first.c_str();
3031 CommandObject *cmd_obj = pos->second.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003032
Jim Inghamaf3753e2013-05-17 01:30:37 +00003033 if (cmd_obj->HelpTextContainsWord (search_word))
3034 {
3035 commands_found.AppendString (command_name);
3036 commands_help.AppendString (cmd_obj->GetHelp());
3037 }
3038
3039 if (cmd_obj->IsMultiwordObject())
3040 cmd_obj->AproposAllSubCommands (command_name,
3041 search_word,
3042 commands_found,
3043 commands_help);
3044
3045 }
3046 }
3047
3048 if (search_user_commands)
3049 {
3050 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
3051 {
3052 const char *command_name = pos->first.c_str();
3053 CommandObject *cmd_obj = pos->second.get();
3054
3055 if (cmd_obj->HelpTextContainsWord (search_word))
3056 {
3057 commands_found.AppendString (command_name);
3058 commands_help.AppendString (cmd_obj->GetHelp());
3059 }
3060
3061 if (cmd_obj->IsMultiwordObject())
3062 cmd_obj->AproposAllSubCommands (command_name,
3063 search_word,
3064 commands_found,
3065 commands_help);
3066
3067 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003068 }
3069}
Greg Clayton8b82f082011-04-12 05:54:46 +00003070
Greg Clayton8b82f082011-04-12 05:54:46 +00003071void
3072CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
3073{
Ed Masted78c9572014-04-20 00:31:37 +00003074 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00003075 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00003076 m_exe_ctx_ref = *override_context;
Greg Clayton8b82f082011-04-12 05:54:46 +00003077 }
3078 else
3079 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00003080 const bool adopt_selected = true;
3081 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
Greg Clayton8b82f082011-04-12 05:54:46 +00003082 }
3083}
Greg Clayton44d93782014-01-27 23:43:24 +00003084
3085
3086size_t
3087CommandInterpreter::GetProcessOutput ()
3088{
3089 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
3090 char stdio_buffer[1024];
3091 size_t len;
3092 size_t total_bytes = 0;
3093 Error error;
3094 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
3095 if (target_sp)
3096 {
3097 ProcessSP process_sp (target_sp->GetProcessSP());
3098 if (process_sp)
3099 {
3100 while ((len = process_sp->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3101 {
3102 size_t bytes_written = len;
3103 m_debugger.GetOutputFile()->Write (stdio_buffer, bytes_written);
3104 total_bytes += len;
3105 }
3106 while ((len = process_sp->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3107 {
3108 size_t bytes_written = len;
3109 m_debugger.GetErrorFile()->Write (stdio_buffer, bytes_written);
3110 total_bytes += len;
3111 }
3112 }
3113 }
3114 return total_bytes;
3115}
3116
3117void
3118CommandInterpreter::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
3119{
Greg Clayton340b0302014-02-05 17:57:57 +00003120 const bool is_interactive = io_handler.GetIsInteractive();
3121 if (is_interactive == false)
3122 {
3123 // When we are not interactive, don't execute blank lines. This will happen
3124 // sourcing a commands file. We don't want blank lines to repeat the previous
3125 // command and cause any errors to occur (like redefining an alias, get an error
3126 // and stop parsing the commands file).
3127 if (line.empty())
3128 return;
3129
3130 // When using a non-interactive file handle (like when sourcing commands from a file)
3131 // we need to echo the command out so we don't just see the command output and no
3132 // command...
3133 if (io_handler.GetFlags().Test(eHandleCommandFlagEchoCommand))
3134 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3135 }
3136
Greg Clayton44d93782014-01-27 23:43:24 +00003137 lldb_private::CommandReturnObject result;
3138 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
3139
Greg Clayton44d93782014-01-27 23:43:24 +00003140 // Now emit the command output text from the command we just executed
Greg Clayton340b0302014-02-05 17:57:57 +00003141 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult))
3142 {
3143 // Display any STDOUT/STDERR _prior_ to emitting the command result text
3144 GetProcessOutput ();
3145
Greg Clayton8ee67312014-02-05 21:46:20 +00003146 if (!result.GetImmediateOutputStream())
3147 {
3148 const char *output = result.GetOutputData();
3149 if (output && output[0])
3150 io_handler.GetOutputStreamFile()->PutCString(output);
3151 }
Greg Clayton44d93782014-01-27 23:43:24 +00003152
Greg Clayton340b0302014-02-05 17:57:57 +00003153 // Now emit the command error text from the command we just executed
Greg Clayton8ee67312014-02-05 21:46:20 +00003154 if (!result.GetImmediateErrorStream())
3155 {
3156 const char *error = result.GetErrorData();
3157 if (error && error[0])
3158 io_handler.GetErrorStreamFile()->PutCString(error);
3159 }
Greg Clayton340b0302014-02-05 17:57:57 +00003160 }
Greg Clayton44d93782014-01-27 23:43:24 +00003161
3162 switch (result.GetStatus())
3163 {
3164 case eReturnStatusInvalid:
3165 case eReturnStatusSuccessFinishNoResult:
3166 case eReturnStatusSuccessFinishResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003167 case eReturnStatusStarted:
3168 break;
3169
Greg Clayton44d93782014-01-27 23:43:24 +00003170 case eReturnStatusSuccessContinuingNoResult:
3171 case eReturnStatusSuccessContinuingResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003172 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3173 io_handler.SetIsDone(true);
3174 break;
3175
Greg Clayton44d93782014-01-27 23:43:24 +00003176 case eReturnStatusFailed:
Jim Ingham26c7bf92014-10-11 00:38:27 +00003177 m_num_errors++;
Greg Clayton340b0302014-02-05 17:57:57 +00003178 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError))
3179 io_handler.SetIsDone(true);
Greg Clayton44d93782014-01-27 23:43:24 +00003180 break;
3181
3182 case eReturnStatusQuit:
Jim Ingham26c7bf92014-10-11 00:38:27 +00003183 m_quit_requested = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003184 io_handler.SetIsDone(true);
3185 break;
3186 }
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003187
3188 // Finally, if we're going to stop on crash, check that here:
3189 if (!m_quit_requested
3190 && result.GetDidChangeProcessState()
3191 && io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash))
3192 {
3193 bool should_stop = false;
3194 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
3195 if (target_sp)
3196 {
3197 ProcessSP process_sp (target_sp->GetProcessSP());
3198 if (process_sp)
3199 {
3200 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
3201 {
3202 StopReason reason = thread_sp->GetStopReason();
3203 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
3204 {
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003205 should_stop = true;
3206 break;
3207 }
3208 }
3209 }
3210 }
3211 if (should_stop)
3212 {
3213 io_handler.SetIsDone(true);
3214 m_stopped_for_crash = true;
3215 }
3216 }
Greg Clayton44d93782014-01-27 23:43:24 +00003217}
3218
Greg Claytonf0066ad2014-05-02 00:45:31 +00003219bool
3220CommandInterpreter::IOHandlerInterrupt (IOHandler &io_handler)
3221{
3222 ExecutionContext exe_ctx (GetExecutionContext());
3223 Process *process = exe_ctx.GetProcessPtr();
3224
3225 if (process)
3226 {
3227 StateType state = process->GetState();
3228 if (StateIsRunningState(state))
3229 {
3230 process->Halt();
3231 return true; // Don't do any updating when we are running
3232 }
3233 }
Greg Claytone507bce2015-01-27 01:58:22 +00003234
3235 ScriptInterpreter *script_interpreter = GetScriptInterpreter (false);
3236 if (script_interpreter)
3237 {
3238 if (script_interpreter->Interrupt())
3239 return true;
3240 }
Greg Claytonf0066ad2014-05-02 00:45:31 +00003241 return false;
3242}
3243
Greg Clayton44d93782014-01-27 23:43:24 +00003244void
3245CommandInterpreter::GetLLDBCommandsFromIOHandler (const char *prompt,
3246 IOHandlerDelegate &delegate,
3247 bool asynchronously,
3248 void *baton)
3249{
3250 Debugger &debugger = GetDebugger();
3251 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00003252 IOHandler::Type::CommandList,
Greg Clayton44d93782014-01-27 23:43:24 +00003253 "lldb", // Name of input reader for history
3254 prompt, // Prompt
Kate Stonee30f11d2014-11-17 19:06:59 +00003255 NULL, // Continuation prompt
Greg Clayton44d93782014-01-27 23:43:24 +00003256 true, // Get multiple lines
Kate Stonee30f11d2014-11-17 19:06:59 +00003257 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00003258 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003259 delegate)); // IOHandlerDelegate
3260
3261 if (io_handler_sp)
3262 {
3263 io_handler_sp->SetUserData (baton);
3264 if (asynchronously)
3265 debugger.PushIOHandler(io_handler_sp);
3266 else
3267 debugger.RunIOHandler(io_handler_sp);
3268 }
3269
3270}
3271
3272
3273void
3274CommandInterpreter::GetPythonCommandsFromIOHandler (const char *prompt,
3275 IOHandlerDelegate &delegate,
3276 bool asynchronously,
3277 void *baton)
3278{
3279 Debugger &debugger = GetDebugger();
3280 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00003281 IOHandler::Type::PythonCode,
Greg Clayton44d93782014-01-27 23:43:24 +00003282 "lldb-python", // Name of input reader for history
3283 prompt, // Prompt
Kate Stonee30f11d2014-11-17 19:06:59 +00003284 NULL, // Continuation prompt
Greg Clayton44d93782014-01-27 23:43:24 +00003285 true, // Get multiple lines
Kate Stonee30f11d2014-11-17 19:06:59 +00003286 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00003287 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003288 delegate)); // IOHandlerDelegate
3289
3290 if (io_handler_sp)
3291 {
3292 io_handler_sp->SetUserData (baton);
3293 if (asynchronously)
3294 debugger.PushIOHandler(io_handler_sp);
3295 else
3296 debugger.RunIOHandler(io_handler_sp);
3297 }
3298
3299}
3300
3301bool
3302CommandInterpreter::IsActive ()
3303{
3304 return m_debugger.IsTopIOHandler (m_command_io_handler_sp);
3305}
3306
Kate Stonee30f11d2014-11-17 19:06:59 +00003307lldb::IOHandlerSP
3308CommandInterpreter::GetIOHandler(bool force_create, CommandInterpreterRunOptions *options)
3309{
3310 // Always re-create the IOHandlerEditline in case the input
3311 // changed. The old instance might have had a non-interactive
3312 // input and now it does or vice versa.
3313 if (force_create || !m_command_io_handler_sp)
3314 {
3315 // Always re-create the IOHandlerEditline in case the input
3316 // changed. The old instance might have had a non-interactive
3317 // input and now it does or vice versa.
3318 uint32_t flags = 0;
3319
3320 if (options)
3321 {
3322 if (options->m_stop_on_continue == eLazyBoolYes)
3323 flags |= eHandleCommandFlagStopOnContinue;
3324 if (options->m_stop_on_error == eLazyBoolYes)
3325 flags |= eHandleCommandFlagStopOnError;
3326 if (options->m_stop_on_crash == eLazyBoolYes)
3327 flags |= eHandleCommandFlagStopOnCrash;
3328 if (options->m_echo_commands != eLazyBoolNo)
3329 flags |= eHandleCommandFlagEchoCommand;
3330 if (options->m_print_results != eLazyBoolNo)
3331 flags |= eHandleCommandFlagPrintResult;
3332 }
3333 else
3334 {
3335 flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult;
3336 }
3337
3338 m_command_io_handler_sp.reset(new IOHandlerEditline (m_debugger,
3339 IOHandler::Type::CommandInterpreter,
3340 m_debugger.GetInputFile(),
3341 m_debugger.GetOutputFile(),
3342 m_debugger.GetErrorFile(),
3343 flags,
3344 "lldb",
3345 m_debugger.GetPrompt(),
3346 NULL, // Continuation prompt
3347 false, // Don't enable multiple line input, just single line commands
3348 m_debugger.GetUseColor(),
3349 0, // Don't show line numbers
3350 *this));
3351 }
3352 return m_command_io_handler_sp;
3353}
3354
Greg Clayton44d93782014-01-27 23:43:24 +00003355void
3356CommandInterpreter::RunCommandInterpreter(bool auto_handle_events,
Jim Ingham26c7bf92014-10-11 00:38:27 +00003357 bool spawn_thread,
3358 CommandInterpreterRunOptions &options)
Greg Clayton44d93782014-01-27 23:43:24 +00003359{
Kate Stonee30f11d2014-11-17 19:06:59 +00003360 // Always re-create the command intepreter when we run it in case
3361 // any file handles have changed.
3362 bool force_create = true;
3363 m_debugger.PushIOHandler(GetIOHandler(force_create, &options));
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003364 m_stopped_for_crash = false;
Greg Clayton380f3d82014-07-31 19:46:19 +00003365
Greg Clayton44d93782014-01-27 23:43:24 +00003366 if (auto_handle_events)
3367 m_debugger.StartEventHandlerThread();
3368
3369 if (spawn_thread)
3370 {
3371 m_debugger.StartIOHandlerThread();
3372 }
3373 else
3374 {
Siva Chandra9aaab552015-02-26 19:26:36 +00003375 m_debugger.ExecuteIOHandlers();
Kate Stonee30f11d2014-11-17 19:06:59 +00003376
Greg Clayton44d93782014-01-27 23:43:24 +00003377 if (auto_handle_events)
3378 m_debugger.StopEventHandlerThread();
3379 }
Kate Stonee30f11d2014-11-17 19:06:59 +00003380
Greg Clayton44d93782014-01-27 23:43:24 +00003381}
3382