blob: 2b68f5c172e5c65ffb79ff6795cfb835bdfb0a6c [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"
63#include "lldb/Interpreter/ScriptInterpreterNone.h"
64#include "lldb/Interpreter/ScriptInterpreterPython.h"
65
66
Chris Lattner30fdc8d2010-06-08 16:52:24 +000067#include "lldb/Target/Process.h"
68#include "lldb/Target/Thread.h"
69#include "lldb/Target/TargetList.h"
70
Enrico Granatab5887262012-10-29 21:18:03 +000071#include "lldb/Utility/CleanUp.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000072
Zachary Turnera21fee02014-08-21 21:49:24 +000073#include "llvm/ADT/SmallString.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000074#include "llvm/ADT/STLExtras.h"
Zachary Turnera21fee02014-08-21 21:49:24 +000075#include "llvm/Support/Path.h"
Saleem Abdulrasool28606952014-06-27 05:17:41 +000076
Chris Lattner30fdc8d2010-06-08 16:52:24 +000077using namespace lldb;
78using namespace lldb_private;
79
Greg Clayton754a9362012-08-23 00:22:02 +000080
81static PropertyDefinition
82g_properties[] =
83{
Ed Masted78c9572014-04-20 00:31:37 +000084 { "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." },
85 { "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." },
86 { "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." },
87 { nullptr , OptionValue::eTypeInvalid, true, 0 , nullptr, nullptr, nullptr }
Greg Clayton754a9362012-08-23 00:22:02 +000088};
89
90enum
91{
Enrico Granatabcba2b22013-01-17 21:36:19 +000092 ePropertyExpandRegexAliases = 0,
Enrico Granata012d4fc2013-06-11 01:26:35 +000093 ePropertyPromptOnQuit = 1,
94 ePropertyStopCmdSourceOnError = 2
Greg Clayton754a9362012-08-23 00:22:02 +000095};
96
Jim Ingham4bddaeb2012-02-16 06:50:00 +000097ConstString &
98CommandInterpreter::GetStaticBroadcasterClass ()
99{
100 static ConstString class_name ("lldb.commandInterpreter");
101 return class_name;
102}
103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104CommandInterpreter::CommandInterpreter
105(
Greg Clayton66111032010-06-23 01:19:29 +0000106 Debugger &debugger,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000107 ScriptLanguage script_language,
Greg Clayton66111032010-06-23 01:19:29 +0000108 bool synchronous_execution
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109) :
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000110 Broadcaster (&debugger, "lldb.command-interpreter"),
Greg Clayton754a9362012-08-23 00:22:02 +0000111 Properties(OptionValuePropertiesSP(new OptionValueProperties(ConstString("interpreter")))),
Greg Clayton44d93782014-01-27 23:43:24 +0000112 IOHandlerDelegate (IOHandlerDelegate::Completion::LLDBCommand),
Greg Clayton66111032010-06-23 01:19:29 +0000113 m_debugger (debugger),
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000114 m_synchronous_execution (synchronous_execution),
Caroline Tice2f88aad2011-01-14 00:29:16 +0000115 m_skip_lldbinit_files (false),
Jim Ingham16e0c682011-08-12 23:34:31 +0000116 m_skip_app_init_files (false),
Jim Inghame16c50a2011-02-18 00:54:25 +0000117 m_script_interpreter_ap (),
Greg Clayton44d93782014-01-27 23:43:24 +0000118 m_command_io_handler_sp (),
Caroline Ticed61c10b2011-06-16 16:27:19 +0000119 m_comment_char ('#'),
Johnny Chen4ac1d9e2012-08-09 22:06:10 +0000120 m_batch_command_mode (false),
Enrico Granata5f5ab602012-05-31 01:09:06 +0000121 m_truncation_warning(eNoTruncation),
Jim Ingham26c7bf92014-10-11 00:38:27 +0000122 m_command_source_depth (0),
123 m_num_errors(0),
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000124 m_quit_requested(false),
125 m_stopped_for_crash(false)
Jim Ingham26c7bf92014-10-11 00:38:27 +0000126
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000127{
Greg Clayton67cc0632012-08-22 17:17:09 +0000128 debugger.SetScriptLanguage (script_language);
Greg Claytoncfd1ace2010-10-31 03:01:06 +0000129 SetEventName (eBroadcastBitThreadShouldExit, "thread-should-exit");
130 SetEventName (eBroadcastBitResetPrompt, "reset-prompt");
Greg Clayton67cc0632012-08-22 17:17:09 +0000131 SetEventName (eBroadcastBitQuitCommandReceived, "quit");
Jim Ingham4bddaeb2012-02-16 06:50:00 +0000132 CheckInWithManager ();
Greg Clayton754a9362012-08-23 00:22:02 +0000133 m_collection_sp->Initialize (g_properties);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000134}
135
Greg Clayton754a9362012-08-23 00:22:02 +0000136bool
137CommandInterpreter::GetExpandRegexAliases () const
138{
139 const uint32_t idx = ePropertyExpandRegexAliases;
Ed Masted78c9572014-04-20 00:31:37 +0000140 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Greg Clayton754a9362012-08-23 00:22:02 +0000141}
142
Enrico Granatabcba2b22013-01-17 21:36:19 +0000143bool
144CommandInterpreter::GetPromptOnQuit () const
145{
146 const uint32_t idx = ePropertyPromptOnQuit;
Ed Masted78c9572014-04-20 00:31:37 +0000147 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granatabcba2b22013-01-17 21:36:19 +0000148}
Greg Clayton754a9362012-08-23 00:22:02 +0000149
Enrico Granata012d4fc2013-06-11 01:26:35 +0000150bool
151CommandInterpreter::GetStopCmdSourceOnError () const
152{
153 const uint32_t idx = ePropertyStopCmdSourceOnError;
Ed Masted78c9572014-04-20 00:31:37 +0000154 return m_collection_sp->GetPropertyAtIndexAsBoolean (nullptr, idx, g_properties[idx].default_uint_value != 0);
Enrico Granata012d4fc2013-06-11 01:26:35 +0000155}
156
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000157void
158CommandInterpreter::Initialize ()
159{
160 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
161
162 CommandReturnObject result;
163
164 LoadCommandDictionary ();
165
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 // Set up some initial aliases.
Caroline Ticeca90c472011-05-06 21:37:15 +0000167 CommandObjectSP cmd_obj_sp = GetCommandSPExact ("quit", false);
168 if (cmd_obj_sp)
169 {
170 AddAlias ("q", cmd_obj_sp);
171 AddAlias ("exit", cmd_obj_sp);
172 }
Sean Callanan247e62a2012-05-04 23:15:02 +0000173
Johnny Chen6d675242012-08-24 18:15:45 +0000174 cmd_obj_sp = GetCommandSPExact ("_regexp-attach",false);
Sean Callanan247e62a2012-05-04 23:15:02 +0000175 if (cmd_obj_sp)
176 {
177 AddAlias ("attach", cmd_obj_sp);
178 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000179
Johnny Chen6d675242012-08-24 18:15:45 +0000180 cmd_obj_sp = GetCommandSPExact ("process detach",false);
181 if (cmd_obj_sp)
182 {
183 AddAlias ("detach", cmd_obj_sp);
184 }
185
Caroline Ticeca90c472011-05-06 21:37:15 +0000186 cmd_obj_sp = GetCommandSPExact ("process continue", false);
187 if (cmd_obj_sp)
188 {
189 AddAlias ("c", cmd_obj_sp);
190 AddAlias ("continue", cmd_obj_sp);
191 }
192
193 cmd_obj_sp = GetCommandSPExact ("_regexp-break",false);
194 if (cmd_obj_sp)
195 AddAlias ("b", cmd_obj_sp);
196
Jim Inghamca36cd12012-10-05 19:16:31 +0000197 cmd_obj_sp = GetCommandSPExact ("_regexp-tbreak",false);
198 if (cmd_obj_sp)
199 AddAlias ("tbreak", cmd_obj_sp);
200
Caroline Ticeca90c472011-05-06 21:37:15 +0000201 cmd_obj_sp = GetCommandSPExact ("thread step-inst", false);
202 if (cmd_obj_sp)
Jason Molendaf385f122011-10-22 00:47:41 +0000203 {
204 AddAlias ("stepi", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000205 AddAlias ("si", cmd_obj_sp);
Jason Molendaf385f122011-10-22 00:47:41 +0000206 }
207
208 cmd_obj_sp = GetCommandSPExact ("thread step-inst-over", false);
209 if (cmd_obj_sp)
210 {
211 AddAlias ("nexti", cmd_obj_sp);
212 AddAlias ("ni", cmd_obj_sp);
213 }
Caroline Ticeca90c472011-05-06 21:37:15 +0000214
215 cmd_obj_sp = GetCommandSPExact ("thread step-in", false);
216 if (cmd_obj_sp)
217 {
218 AddAlias ("s", cmd_obj_sp);
219 AddAlias ("step", cmd_obj_sp);
220 }
221
222 cmd_obj_sp = GetCommandSPExact ("thread step-over", false);
223 if (cmd_obj_sp)
224 {
225 AddAlias ("n", cmd_obj_sp);
226 AddAlias ("next", cmd_obj_sp);
227 }
228
229 cmd_obj_sp = GetCommandSPExact ("thread step-out", false);
230 if (cmd_obj_sp)
231 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000232 AddAlias ("finish", cmd_obj_sp);
233 }
234
Jim Ingham6d6d1072011-12-02 01:12:59 +0000235 cmd_obj_sp = GetCommandSPExact ("frame select", false);
236 if (cmd_obj_sp)
237 {
238 AddAlias ("f", cmd_obj_sp);
239 }
240
Jim Inghamca36cd12012-10-05 19:16:31 +0000241 cmd_obj_sp = GetCommandSPExact ("thread select", false);
242 if (cmd_obj_sp)
243 {
244 AddAlias ("t", cmd_obj_sp);
245 }
246
Richard Mittonf86248d2013-09-12 02:20:34 +0000247 cmd_obj_sp = GetCommandSPExact ("_regexp-jump",false);
248 if (cmd_obj_sp)
249 {
250 AddAlias ("j", cmd_obj_sp);
251 AddAlias ("jump", cmd_obj_sp);
252 }
253
Greg Clayton6bade322013-02-01 23:33:03 +0000254 cmd_obj_sp = GetCommandSPExact ("_regexp-list", false);
Caroline Ticeca90c472011-05-06 21:37:15 +0000255 if (cmd_obj_sp)
256 {
257 AddAlias ("l", cmd_obj_sp);
258 AddAlias ("list", cmd_obj_sp);
259 }
260
Greg Claytonef5651d2013-02-12 18:52:24 +0000261 cmd_obj_sp = GetCommandSPExact ("_regexp-env", false);
262 if (cmd_obj_sp)
263 {
264 AddAlias ("env", cmd_obj_sp);
265 }
266
Caroline Ticeca90c472011-05-06 21:37:15 +0000267 cmd_obj_sp = GetCommandSPExact ("memory read", false);
268 if (cmd_obj_sp)
269 AddAlias ("x", cmd_obj_sp);
270
271 cmd_obj_sp = GetCommandSPExact ("_regexp-up", false);
272 if (cmd_obj_sp)
273 AddAlias ("up", cmd_obj_sp);
274
275 cmd_obj_sp = GetCommandSPExact ("_regexp-down", false);
276 if (cmd_obj_sp)
277 AddAlias ("down", cmd_obj_sp);
278
Jason Molenda0c8e0062011-10-25 02:11:20 +0000279 cmd_obj_sp = GetCommandSPExact ("_regexp-display", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000280 if (cmd_obj_sp)
281 AddAlias ("display", cmd_obj_sp);
Jim Ingham7e18e422011-10-24 18:37:00 +0000282
283 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
284 if (cmd_obj_sp)
285 AddAlias ("dis", cmd_obj_sp);
286
287 cmd_obj_sp = GetCommandSPExact ("disassemble", false);
288 if (cmd_obj_sp)
289 AddAlias ("di", cmd_obj_sp);
290
291
Jason Molendabc7748b2011-10-22 01:30:52 +0000292
Jason Molenda0c8e0062011-10-25 02:11:20 +0000293 cmd_obj_sp = GetCommandSPExact ("_regexp-undisplay", false);
Jason Molendabc7748b2011-10-22 01:30:52 +0000294 if (cmd_obj_sp)
295 AddAlias ("undisplay", cmd_obj_sp);
296
Jim Ingham71bf2992012-10-10 16:51:31 +0000297 cmd_obj_sp = GetCommandSPExact ("_regexp-bt", false);
298 if (cmd_obj_sp)
299 AddAlias ("bt", cmd_obj_sp);
300
Caroline Ticeca90c472011-05-06 21:37:15 +0000301 cmd_obj_sp = GetCommandSPExact ("target create", false);
302 if (cmd_obj_sp)
303 AddAlias ("file", cmd_obj_sp);
304
305 cmd_obj_sp = GetCommandSPExact ("target modules", false);
306 if (cmd_obj_sp)
307 AddAlias ("image", cmd_obj_sp);
308
309
310 OptionArgVectorSP alias_arguments_vector_sp (new OptionArgVector);
Jim Inghamffba2292011-03-22 02:29:32 +0000311
Caroline Ticeca90c472011-05-06 21:37:15 +0000312 cmd_obj_sp = GetCommandSPExact ("expression", false);
313 if (cmd_obj_sp)
Sean Callanan90e579f2013-04-17 17:23:58 +0000314 {
Caroline Ticeca90c472011-05-06 21:37:15 +0000315 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
316 AddAlias ("p", cmd_obj_sp);
317 AddAlias ("print", cmd_obj_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000318 AddAlias ("call", cmd_obj_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000319 AddOrReplaceAliasOptions ("p", alias_arguments_vector_sp);
320 AddOrReplaceAliasOptions ("print", alias_arguments_vector_sp);
Sean Callanan316d5e42012-08-08 01:30:34 +0000321 AddOrReplaceAliasOptions ("call", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000322
323 alias_arguments_vector_sp.reset (new OptionArgVector);
Greg Clayton9e57dcd2013-05-15 01:03:08 +0000324 ProcessAliasOptionsArgs (cmd_obj_sp, "-O -- ", alias_arguments_vector_sp);
Caroline Ticeca90c472011-05-06 21:37:15 +0000325 AddAlias ("po", cmd_obj_sp);
326 AddOrReplaceAliasOptions ("po", alias_arguments_vector_sp);
327 }
328
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000329 cmd_obj_sp = GetCommandSPExact ("process kill", false);
330 if (cmd_obj_sp)
Greg Claytone86fd742012-09-27 00:02:27 +0000331 {
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000332 AddAlias ("kill", cmd_obj_sp);
Greg Claytone86fd742012-09-27 00:02:27 +0000333 }
Sean Callanan2e1d9ba2012-06-01 23:29:32 +0000334
Caroline Ticeca90c472011-05-06 21:37:15 +0000335 cmd_obj_sp = GetCommandSPExact ("process launch", false);
336 if (cmd_obj_sp)
337 {
338 alias_arguments_vector_sp.reset (new OptionArgVector);
Todd Fiala013434e2014-07-09 01:29:05 +0000339#if defined (__arm__) || defined (__arm64__) || defined (__aarch64__)
Jason Molenda85da3122012-07-06 02:46:23 +0000340 ProcessAliasOptionsArgs (cmd_obj_sp, "--", alias_arguments_vector_sp);
341#else
Zachary Turner10687b02014-10-20 17:46:43 +0000342 std::string shell_option;
343 shell_option.append("--shell=");
344 shell_option.append(HostInfo::GetDefaultShell().GetPath());
345 shell_option.append(" --");
346 ProcessAliasOptionsArgs (cmd_obj_sp, shell_option.c_str(), alias_arguments_vector_sp);
Jason Molenda85da3122012-07-06 02:46:23 +0000347#endif
Caroline Ticeca90c472011-05-06 21:37:15 +0000348 AddAlias ("r", cmd_obj_sp);
349 AddAlias ("run", cmd_obj_sp);
350 AddOrReplaceAliasOptions ("r", alias_arguments_vector_sp);
351 AddOrReplaceAliasOptions ("run", alias_arguments_vector_sp);
352 }
Greg Clayton843d62d2012-03-29 21:47:51 +0000353
354 cmd_obj_sp = GetCommandSPExact ("target symbols add", false);
355 if (cmd_obj_sp)
356 {
357 AddAlias ("add-dsym", cmd_obj_sp);
358 }
Sean Callananfc732752012-05-21 18:25:19 +0000359
360 cmd_obj_sp = GetCommandSPExact ("breakpoint set", false);
361 if (cmd_obj_sp)
362 {
363 alias_arguments_vector_sp.reset (new OptionArgVector);
364 ProcessAliasOptionsArgs (cmd_obj_sp, "--func-regex %1", alias_arguments_vector_sp);
Jim Ingham06d282d2012-10-18 23:24:12 +0000365 AddAlias ("rbreak", cmd_obj_sp);
366 AddOrReplaceAliasOptions("rbreak", alias_arguments_vector_sp);
Sean Callananfc732752012-05-21 18:25:19 +0000367 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000368}
369
Greg Clayton0c4129f2014-04-25 00:35:14 +0000370void
371CommandInterpreter::Clear()
372{
373 m_command_io_handler_sp.reset();
Greg Claytoned6499f2014-04-25 23:55:12 +0000374
375 if (m_script_interpreter_ap)
376 m_script_interpreter_ap->Clear();
Greg Clayton0c4129f2014-04-25 00:35:14 +0000377}
378
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000379const char *
380CommandInterpreter::ProcessEmbeddedScriptCommands (const char *arg)
381{
382 // This function has not yet been implemented.
383
384 // Look for any embedded script command
385 // If found,
386 // get interpreter object from the command dictionary,
387 // call execute_one_command on it,
388 // get the results as a string,
389 // substitute that string for current stuff.
390
391 return arg;
392}
393
394
395void
396CommandInterpreter::LoadCommandDictionary ()
397{
398 Timer scoped_timer (__PRETTY_FUNCTION__, __PRETTY_FUNCTION__);
399
Caroline Ticedaccaa92010-09-20 20:44:43 +0000400 lldb::ScriptLanguage script_language = m_debugger.GetScriptLanguage();
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000401
Greg Claytona7015092010-09-18 01:14:36 +0000402 m_command_dict["apropos"] = CommandObjectSP (new CommandObjectApropos (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000403 m_command_dict["breakpoint"]= CommandObjectSP (new CommandObjectMultiwordBreakpoint (*this));
Johnny Chenb89982d2011-04-21 00:39:18 +0000404 m_command_dict["command"] = CommandObjectSP (new CommandObjectMultiwordCommands (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000405 m_command_dict["disassemble"] = CommandObjectSP (new CommandObjectDisassemble (*this));
406 m_command_dict["expression"]= CommandObjectSP (new CommandObjectExpression (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000407 m_command_dict["frame"] = CommandObjectSP (new CommandObjectMultiwordFrame (*this));
Greg Clayton44d93782014-01-27 23:43:24 +0000408 m_command_dict["gui"] = CommandObjectSP (new CommandObjectGUI (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000409 m_command_dict["help"] = CommandObjectSP (new CommandObjectHelp (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000410 m_command_dict["log"] = CommandObjectSP (new CommandObjectLog (*this));
411 m_command_dict["memory"] = CommandObjectSP (new CommandObjectMemory (*this));
Greg Claytonded470d2011-03-19 01:12:21 +0000412 m_command_dict["platform"] = CommandObjectSP (new CommandObjectPlatform (*this));
Enrico Granata21dfcd92012-09-28 23:57:51 +0000413 m_command_dict["plugin"] = CommandObjectSP (new CommandObjectPlugin (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000414 m_command_dict["process"] = CommandObjectSP (new CommandObjectMultiwordProcess (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000415 m_command_dict["quit"] = CommandObjectSP (new CommandObjectQuit (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000416 m_command_dict["register"] = CommandObjectSP (new CommandObjectRegister (*this));
Greg Claytona7015092010-09-18 01:14:36 +0000417 m_command_dict["script"] = CommandObjectSP (new CommandObjectScript (*this, script_language));
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000418 m_command_dict["settings"] = CommandObjectSP (new CommandObjectMultiwordSettings (*this));
Jim Inghamebc09c32010-07-07 03:36:20 +0000419 m_command_dict["source"] = CommandObjectSP (new CommandObjectMultiwordSource (*this));
Greg Clayton66111032010-06-23 01:19:29 +0000420 m_command_dict["target"] = CommandObjectSP (new CommandObjectMultiwordTarget (*this));
421 m_command_dict["thread"] = CommandObjectSP (new CommandObjectMultiwordThread (*this));
Enrico Granata223383e2011-08-16 23:24:13 +0000422 m_command_dict["type"] = CommandObjectSP (new CommandObjectType (*this));
Johnny Chen31c39da2010-12-23 20:21:44 +0000423 m_command_dict["version"] = CommandObjectSP (new CommandObjectVersion (*this));
Johnny Chenf04ee932011-09-22 18:04:58 +0000424 m_command_dict["watchpoint"]= CommandObjectSP (new CommandObjectMultiwordWatchpoint (*this));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425
Jim Inghamca36cd12012-10-05 19:16:31 +0000426 const char *break_regexes[][2] = {{"^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "breakpoint set --file '%1' --line %2"},
Greg Claytond90ac932014-12-01 22:34:03 +0000427 {"^/([^/]+)/$", "breakpoint set --source-pattern-regexp '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000428 {"^([[:digit:]]+)[[:space:]]*$", "breakpoint set --line %1"},
Greg Clayton722e8852013-02-08 02:54:24 +0000429 {"^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "breakpoint set --address %1"},
Greg Clayton1b3815c2013-01-30 00:18:29 +0000430 {"^[\"']?([-+]?\\[.*\\])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"},
Jim Inghamca36cd12012-10-05 19:16:31 +0000431 {"^(-.*)$", "breakpoint set %1"},
432 {"^(.*[^[:space:]])`(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%2' --shlib '%1'"},
Greg Clayton722e8852013-02-08 02:54:24 +0000433 {"^\\&(.*[^[:space:]])[[:space:]]*$", "breakpoint set --name '%1' --skip-prologue=0"},
Greg Clayton2d4b5122014-08-14 17:58:33 +0000434 {"^[\"']?(.*[^[:space:]\"'])[\"']?[[:space:]]*$", "breakpoint set --name '%1'"}};
Jim Inghamca36cd12012-10-05 19:16:31 +0000435
Saleem Abdulrasool28606952014-06-27 05:17:41 +0000436 size_t num_regexes = llvm::array_lengthof(break_regexes);
Jim Inghamca36cd12012-10-05 19:16:31 +0000437
Greg Clayton7b0992d2013-04-18 22:45:39 +0000438 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytona7015092010-09-18 01:14:36 +0000439 break_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000440 "_regexp-break",
Johnny Chenb417dcd2012-08-23 00:32:22 +0000441 "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 +0000442 "_regexp-break [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
443 2,
444 CommandCompletions::eSymbolCompletion |
445 CommandCompletions::eSourceFileCompletion));
Jim Inghamca36cd12012-10-05 19:16:31 +0000446
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 if (break_regex_cmd_ap.get())
448 {
Jim Inghamca36cd12012-10-05 19:16:31 +0000449 bool success = true;
450 for (size_t i = 0; i < num_regexes; i++)
451 {
452 success = break_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], break_regexes[i][1]);
453 if (!success)
454 break;
455 }
456 success = break_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
457
458 if (success)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 {
460 CommandObjectSP break_regex_cmd_sp(break_regex_cmd_ap.release());
461 m_command_dict[break_regex_cmd_sp->GetCommandName ()] = break_regex_cmd_sp;
462 }
463 }
Jim Inghamffba2292011-03-22 02:29:32 +0000464
Greg Clayton7b0992d2013-04-18 22:45:39 +0000465 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamca36cd12012-10-05 19:16:31 +0000466 tbreak_regex_cmd_ap(new CommandObjectRegexCommand (*this,
467 "_regexp-tbreak",
468 "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 +0000469 "_regexp-tbreak [<filename>:<linenum>]\n_regexp-break [<linenum>]\n_regexp-break [<address>]\n_regexp-break <...>",
470 2,
471 CommandCompletions::eSymbolCompletion |
472 CommandCompletions::eSourceFileCompletion));
Jim Inghamca36cd12012-10-05 19:16:31 +0000473
474 if (tbreak_regex_cmd_ap.get())
475 {
476 bool success = true;
477 for (size_t i = 0; i < num_regexes; i++)
478 {
479 // If you add a resultant command string longer than 1024 characters be sure to increase the size of this buffer.
480 char buffer[1024];
481 int num_printed = snprintf(buffer, 1024, "%s %s", break_regexes[i][1], "-o");
482 assert (num_printed < 1024);
Eric Christopher5c91d5c12014-09-09 19:26:45 +0000483 // Quiet unused variable warning for release builds.
Eric Christopher9ad83942014-09-09 08:57:33 +0000484 (void) num_printed;
Jim Inghamca36cd12012-10-05 19:16:31 +0000485 success = tbreak_regex_cmd_ap->AddRegexCommand (break_regexes[i][0], buffer);
486 if (!success)
487 break;
488 }
489 success = tbreak_regex_cmd_ap->AddRegexCommand("^$", "breakpoint list --full");
490
491 if (success)
492 {
493 CommandObjectSP tbreak_regex_cmd_sp(tbreak_regex_cmd_ap.release());
494 m_command_dict[tbreak_regex_cmd_sp->GetCommandName ()] = tbreak_regex_cmd_sp;
495 }
496 }
497
Greg Clayton7b0992d2013-04-18 22:45:39 +0000498 std::unique_ptr<CommandObjectRegexCommand>
Johnny Chen6d675242012-08-24 18:15:45 +0000499 attach_regex_cmd_ap(new CommandObjectRegexCommand (*this,
500 "_regexp-attach",
501 "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 +0000502 "_regexp-attach [<pid>]\n_regexp-attach [<process-name>]",
503 2));
Johnny Chen6d675242012-08-24 18:15:45 +0000504 if (attach_regex_cmd_ap.get())
505 {
Greg Clayton3cb4c7d2012-12-15 01:19:07 +0000506 if (attach_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "process attach --pid %1") &&
507 attach_regex_cmd_ap->AddRegexCommand("^(-.*|.* -.*)$", "process attach %1") && // Any options that are specified get passed to 'process attach'
508 attach_regex_cmd_ap->AddRegexCommand("^(.+)$", "process attach --name '%1'") &&
509 attach_regex_cmd_ap->AddRegexCommand("^$", "process attach"))
Johnny Chen6d675242012-08-24 18:15:45 +0000510 {
511 CommandObjectSP attach_regex_cmd_sp(attach_regex_cmd_ap.release());
512 m_command_dict[attach_regex_cmd_sp->GetCommandName ()] = attach_regex_cmd_sp;
513 }
514 }
515
Greg Clayton7b0992d2013-04-18 22:45:39 +0000516 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000517 down_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000518 "_regexp-down",
519 "Go down \"n\" frames in the stack (1 frame by default).",
520 "_regexp-down [n]", 2));
Jim Inghamffba2292011-03-22 02:29:32 +0000521 if (down_regex_cmd_ap.get())
522 {
523 if (down_regex_cmd_ap->AddRegexCommand("^$", "frame select -r -1") &&
524 down_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r -%1"))
525 {
526 CommandObjectSP down_regex_cmd_sp(down_regex_cmd_ap.release());
527 m_command_dict[down_regex_cmd_sp->GetCommandName ()] = down_regex_cmd_sp;
528 }
529 }
530
Greg Clayton7b0992d2013-04-18 22:45:39 +0000531 std::unique_ptr<CommandObjectRegexCommand>
Jim Inghamffba2292011-03-22 02:29:32 +0000532 up_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton8b82f082011-04-12 05:54:46 +0000533 "_regexp-up",
534 "Go up \"n\" frames in the stack (1 frame by default).",
535 "_regexp-up [n]", 2));
Jim Inghamffba2292011-03-22 02:29:32 +0000536 if (up_regex_cmd_ap.get())
537 {
538 if (up_regex_cmd_ap->AddRegexCommand("^$", "frame select -r 1") &&
539 up_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "frame select -r %1"))
540 {
541 CommandObjectSP up_regex_cmd_sp(up_regex_cmd_ap.release());
542 m_command_dict[up_regex_cmd_sp->GetCommandName ()] = up_regex_cmd_sp;
543 }
544 }
Jason Molendabc7748b2011-10-22 01:30:52 +0000545
Greg Clayton7b0992d2013-04-18 22:45:39 +0000546 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000547 display_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000548 "_regexp-display",
549 "Add an expression evaluation stop-hook.",
550 "_regexp-display expression", 2));
Jason Molendabc7748b2011-10-22 01:30:52 +0000551 if (display_regex_cmd_ap.get())
552 {
553 if (display_regex_cmd_ap->AddRegexCommand("^(.+)$", "target stop-hook add -o \"expr -- %1\""))
554 {
555 CommandObjectSP display_regex_cmd_sp(display_regex_cmd_ap.release());
556 m_command_dict[display_regex_cmd_sp->GetCommandName ()] = display_regex_cmd_sp;
557 }
558 }
559
Greg Clayton7b0992d2013-04-18 22:45:39 +0000560 std::unique_ptr<CommandObjectRegexCommand>
Jason Molendabc7748b2011-10-22 01:30:52 +0000561 undisplay_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000562 "_regexp-undisplay",
563 "Remove an expression evaluation stop-hook.",
564 "_regexp-undisplay stop-hook-number", 2));
Jason Molendabc7748b2011-10-22 01:30:52 +0000565 if (undisplay_regex_cmd_ap.get())
566 {
567 if (undisplay_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "target stop-hook delete %1"))
568 {
569 CommandObjectSP undisplay_regex_cmd_sp(undisplay_regex_cmd_ap.release());
570 m_command_dict[undisplay_regex_cmd_sp->GetCommandName ()] = undisplay_regex_cmd_sp;
571 }
572 }
573
Greg Clayton7b0992d2013-04-18 22:45:39 +0000574 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000575 connect_gdb_remote_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000576 "gdb-remote",
577 "Connect to a remote GDB server. If no hostname is provided, localhost is assumed.",
578 "gdb-remote [<hostname>:]<portnum>", 2));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000579 if (connect_gdb_remote_cmd_ap.get())
580 {
581 if (connect_gdb_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin gdb-remote connect://%1") &&
582 connect_gdb_remote_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "process connect --plugin gdb-remote connect://localhost:%1"))
583 {
584 CommandObjectSP command_sp(connect_gdb_remote_cmd_ap.release());
585 m_command_dict[command_sp->GetCommandName ()] = command_sp;
586 }
587 }
588
Greg Clayton7b0992d2013-04-18 22:45:39 +0000589 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000590 connect_kdp_remote_cmd_ap(new CommandObjectRegexCommand (*this,
591 "kdp-remote",
Jason Molendaa7dcb332012-10-23 03:05:16 +0000592 "Connect to a remote KDP server. udp port 41139 is the default port number.",
593 "kdp-remote <hostname>[:<portnum>]", 2));
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000594 if (connect_kdp_remote_cmd_ap.get())
595 {
596 if (connect_kdp_remote_cmd_ap->AddRegexCommand("^([^:]+:[[:digit:]]+)$", "process connect --plugin kdp-remote udp://%1") &&
Jason Molendac36b1842012-09-27 02:47:55 +0000597 connect_kdp_remote_cmd_ap->AddRegexCommand("^(.+)$", "process connect --plugin kdp-remote udp://%1:41139"))
Greg Clayton30c0a1c2012-09-26 22:26:47 +0000598 {
599 CommandObjectSP command_sp(connect_kdp_remote_cmd_ap.release());
600 m_command_dict[command_sp->GetCommandName ()] = command_sp;
601 }
602 }
603
Greg Clayton7b0992d2013-04-18 22:45:39 +0000604 std::unique_ptr<CommandObjectRegexCommand>
Jason Molenda4cddfed2012-10-05 05:29:32 +0000605 bt_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Jim Ingham71bf2992012-10-10 16:51:31 +0000606 "_regexp-bt",
Jason Molenda4cddfed2012-10-05 05:29:32 +0000607 "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.",
608 "bt [<digit>|all]", 2));
609 if (bt_regex_cmd_ap.get())
610 {
611 // accept but don't document "bt -c <number>" -- before bt was a regex command if you wanted to backtrace
612 // three frames you would do "bt -c 3" but the intention is to have this emulate the gdb "bt" command and
613 // so now "bt 3" is the preferred form, in line with gdb.
614 if (bt_regex_cmd_ap->AddRegexCommand("^([[:digit:]]+)$", "thread backtrace -c %1") &&
615 bt_regex_cmd_ap->AddRegexCommand("^-c ([[:digit:]]+)$", "thread backtrace -c %1") &&
616 bt_regex_cmd_ap->AddRegexCommand("^all$", "thread backtrace all") &&
617 bt_regex_cmd_ap->AddRegexCommand("^$", "thread backtrace"))
618 {
619 CommandObjectSP command_sp(bt_regex_cmd_ap.release());
620 m_command_dict[command_sp->GetCommandName ()] = command_sp;
621 }
622 }
623
Greg Clayton7b0992d2013-04-18 22:45:39 +0000624 std::unique_ptr<CommandObjectRegexCommand>
Greg Clayton6bade322013-02-01 23:33:03 +0000625 list_regex_cmd_ap(new CommandObjectRegexCommand (*this,
626 "_regexp-list",
627 "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 +0000628 "_regexp-list [<line>]\n_regexp-list [<file>:<line>]\n_regexp-list [<file>:<line>]",
Greg Clayton7d2ef162013-03-29 17:03:23 +0000629 2,
630 CommandCompletions::eSourceFileCompletion));
Greg Clayton6bade322013-02-01 23:33:03 +0000631 if (list_regex_cmd_ap.get())
632 {
633 if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
634 list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
635 list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
Greg Claytond9010962013-02-12 18:42:05 +0000636 list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
Greg Claytone4ca5152013-03-13 18:25:49 +0000637 list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
Greg Clayton6bade322013-02-01 23:33:03 +0000638 list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
639 list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
640 {
641 CommandObjectSP list_regex_cmd_sp(list_regex_cmd_ap.release());
642 m_command_dict[list_regex_cmd_sp->GetCommandName ()] = list_regex_cmd_sp;
643 }
644 }
645
Greg Clayton7b0992d2013-04-18 22:45:39 +0000646 std::unique_ptr<CommandObjectRegexCommand>
Greg Claytonef5651d2013-02-12 18:52:24 +0000647 env_regex_cmd_ap(new CommandObjectRegexCommand (*this,
Greg Clayton7d2ef162013-03-29 17:03:23 +0000648 "_regexp-env",
649 "Implements a shortcut to viewing and setting environment variables.",
650 "_regexp-env\n_regexp-env FOO=BAR", 2));
Greg Claytonef5651d2013-02-12 18:52:24 +0000651 if (env_regex_cmd_ap.get())
652 {
653 if (env_regex_cmd_ap->AddRegexCommand("^$", "settings show target.env-vars") &&
654 env_regex_cmd_ap->AddRegexCommand("^([A-Za-z_][A-Za-z_0-9]*=.*)$", "settings set target.env-vars %1"))
655 {
656 CommandObjectSP env_regex_cmd_sp(env_regex_cmd_ap.release());
657 m_command_dict[env_regex_cmd_sp->GetCommandName ()] = env_regex_cmd_sp;
658 }
659 }
660
Richard Mittonf86248d2013-09-12 02:20:34 +0000661 std::unique_ptr<CommandObjectRegexCommand>
662 jump_regex_cmd_ap(new CommandObjectRegexCommand (*this,
663 "_regexp-jump",
664 "Sets the program counter to a new address.",
665 "_regexp-jump [<line>]\n"
666 "_regexp-jump [<+-lineoffset>]\n"
667 "_regexp-jump [<file>:<line>]\n"
668 "_regexp-jump [*<addr>]\n", 2));
669 if (jump_regex_cmd_ap.get())
670 {
671 if (jump_regex_cmd_ap->AddRegexCommand("^\\*(.*)$", "thread jump --addr %1") &&
672 jump_regex_cmd_ap->AddRegexCommand("^([0-9]+)$", "thread jump --line %1") &&
673 jump_regex_cmd_ap->AddRegexCommand("^([^:]+):([0-9]+)$", "thread jump --file %1 --line %2") &&
674 jump_regex_cmd_ap->AddRegexCommand("^([+\\-][0-9]+)$", "thread jump --by %1"))
675 {
676 CommandObjectSP jump_regex_cmd_sp(jump_regex_cmd_ap.release());
677 m_command_dict[jump_regex_cmd_sp->GetCommandName ()] = jump_regex_cmd_sp;
678 }
679 }
680
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000681}
682
683int
684CommandInterpreter::GetCommandNamesMatchingPartialString (const char *cmd_str, bool include_aliases,
685 StringList &matches)
686{
687 CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_str, matches);
688
689 if (include_aliases)
690 {
691 CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_str, matches);
692 }
693
694 return matches.GetSize();
695}
696
697CommandObjectSP
698CommandInterpreter::GetCommandSP (const char *cmd_cstr, bool include_aliases, bool exact, StringList *matches)
699{
700 CommandObject::CommandMap::iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +0000701 CommandObjectSP command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000702
703 std::string cmd(cmd_cstr);
704
705 if (HasCommands())
706 {
707 pos = m_command_dict.find(cmd);
708 if (pos != m_command_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000709 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000710 }
711
712 if (include_aliases && HasAliases())
713 {
714 pos = m_alias_dict.find(cmd);
715 if (pos != m_alias_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000716 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000717 }
718
719 if (HasUserCommands())
720 {
721 pos = m_user_dict.find(cmd);
722 if (pos != m_user_dict.end())
Greg Claytonc7bece562013-01-25 18:06:21 +0000723 command_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000724 }
725
Greg Claytonc7bece562013-01-25 18:06:21 +0000726 if (!exact && !command_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000727 {
Jim Ingham279a6c22010-07-06 22:46:59 +0000728 // We will only get into here if we didn't find any exact matches.
729
730 CommandObjectSP user_match_sp, alias_match_sp, real_match_sp;
731
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000732 StringList local_matches;
Ed Masted78c9572014-04-20 00:31:37 +0000733 if (matches == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000734 matches = &local_matches;
735
Jim Ingham279a6c22010-07-06 22:46:59 +0000736 unsigned int num_cmd_matches = 0;
737 unsigned int num_alias_matches = 0;
738 unsigned int num_user_matches = 0;
739
740 // Look through the command dictionaries one by one, and if we get only one match from any of
741 // them in toto, then return that, otherwise return an empty CommandObjectSP and the list of matches.
742
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 if (HasCommands())
744 {
745 num_cmd_matches = CommandObject::AddNamesMatchingPartialString (m_command_dict, cmd_cstr, *matches);
746 }
747
748 if (num_cmd_matches == 1)
749 {
750 cmd.assign(matches->GetStringAtIndex(0));
751 pos = m_command_dict.find(cmd);
752 if (pos != m_command_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000753 real_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000754 }
755
Jim Ingham490ac552010-06-24 20:28:42 +0000756 if (include_aliases && HasAliases())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 {
758 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd_cstr, *matches);
759
760 }
761
Jim Ingham279a6c22010-07-06 22:46:59 +0000762 if (num_alias_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000763 {
764 cmd.assign(matches->GetStringAtIndex (num_cmd_matches));
765 pos = m_alias_dict.find(cmd);
766 if (pos != m_alias_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000767 alias_match_sp = pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000768 }
769
Jim Ingham490ac552010-06-24 20:28:42 +0000770 if (HasUserCommands())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000771 {
772 num_user_matches = CommandObject::AddNamesMatchingPartialString (m_user_dict, cmd_cstr, *matches);
773 }
774
Jim Ingham279a6c22010-07-06 22:46:59 +0000775 if (num_user_matches == 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776 {
777 cmd.assign (matches->GetStringAtIndex (num_cmd_matches + num_alias_matches));
778
779 pos = m_user_dict.find (cmd);
780 if (pos != m_user_dict.end())
Jim Ingham279a6c22010-07-06 22:46:59 +0000781 user_match_sp = pos->second;
782 }
783
784 // If we got exactly one match, return that, otherwise return the match list.
785
786 if (num_user_matches + num_cmd_matches + num_alias_matches == 1)
787 {
788 if (num_cmd_matches)
789 return real_match_sp;
790 else if (num_alias_matches)
791 return alias_match_sp;
792 else
793 return user_match_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000794 }
795 }
Greg Claytonc7bece562013-01-25 18:06:21 +0000796 else if (matches && command_sp)
Jim Ingham279a6c22010-07-06 22:46:59 +0000797 {
798 matches->AppendString (cmd_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799 }
800
801
Greg Claytonc7bece562013-01-25 18:06:21 +0000802 return command_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000803}
804
Greg Claytonde164aa2011-04-20 16:37:46 +0000805bool
806CommandInterpreter::AddCommand (const char *name, const lldb::CommandObjectSP &cmd_sp, bool can_replace)
807{
808 if (name && name[0])
809 {
810 std::string name_sstr(name);
Enrico Granatae00af802012-10-01 17:19:37 +0000811 bool found = (m_command_dict.find (name_sstr) != m_command_dict.end());
812 if (found && !can_replace)
813 return false;
814 if (found && m_command_dict[name_sstr]->IsRemovable() == false)
Enrico Granata21dfcd92012-09-28 23:57:51 +0000815 return false;
Greg Claytonde164aa2011-04-20 16:37:46 +0000816 m_command_dict[name_sstr] = cmd_sp;
817 return true;
818 }
819 return false;
820}
821
Enrico Granata223383e2011-08-16 23:24:13 +0000822bool
Enrico Granata0a305db2011-11-07 22:57:04 +0000823CommandInterpreter::AddUserCommand (std::string name,
Enrico Granata223383e2011-08-16 23:24:13 +0000824 const lldb::CommandObjectSP &cmd_sp,
825 bool can_replace)
826{
Enrico Granata0a305db2011-11-07 22:57:04 +0000827 if (!name.empty())
Enrico Granata223383e2011-08-16 23:24:13 +0000828 {
Enrico Granata0a305db2011-11-07 22:57:04 +0000829
830 const char* name_cstr = name.c_str();
831
832 // do not allow replacement of internal commands
833 if (CommandExists(name_cstr))
Enrico Granata21dfcd92012-09-28 23:57:51 +0000834 {
835 if (can_replace == false)
836 return false;
837 if (m_command_dict[name]->IsRemovable() == false)
838 return false;
839 }
Enrico Granata0a305db2011-11-07 22:57:04 +0000840
Enrico Granata21dfcd92012-09-28 23:57:51 +0000841 if (UserCommandExists(name_cstr))
842 {
843 if (can_replace == false)
844 return false;
845 if (m_user_dict[name]->IsRemovable() == false)
846 return false;
847 }
848
Enrico Granata0a305db2011-11-07 22:57:04 +0000849 m_user_dict[name] = cmd_sp;
Enrico Granata223383e2011-08-16 23:24:13 +0000850 return true;
851 }
852 return false;
853}
Greg Claytonde164aa2011-04-20 16:37:46 +0000854
Jim Ingham279a6c22010-07-06 22:46:59 +0000855CommandObjectSP
856CommandInterpreter::GetCommandSPExact (const char *cmd_cstr, bool include_aliases)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000857{
Caroline Tice472362e2010-12-14 18:51:39 +0000858 Args cmd_words (cmd_cstr); // Break up the command string into words, in case it's a multi-word command.
859 CommandObjectSP ret_val; // Possibly empty return value.
860
Ed Masted78c9572014-04-20 00:31:37 +0000861 if (cmd_cstr == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000862 return ret_val;
863
864 if (cmd_words.GetArgumentCount() == 1)
Ed Masted78c9572014-04-20 00:31:37 +0000865 return GetCommandSP(cmd_cstr, include_aliases, true, nullptr);
Caroline Tice472362e2010-12-14 18:51:39 +0000866 else
867 {
868 // We have a multi-word command (seemingly), so we need to do more work.
869 // First, get the cmd_obj_sp for the first word in the command.
Ed Masted78c9572014-04-20 00:31:37 +0000870 CommandObjectSP cmd_obj_sp = GetCommandSP (cmd_words.GetArgumentAtIndex (0), include_aliases, true, nullptr);
871 if (cmd_obj_sp.get() != nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000872 {
873 // Loop through the rest of the words in the command (everything passed in was supposed to be part of a
874 // command name), and find the appropriate sub-command SP for each command word....
875 size_t end = cmd_words.GetArgumentCount();
876 for (size_t j= 1; j < end; ++j)
877 {
878 if (cmd_obj_sp->IsMultiwordObject())
879 {
Greg Clayton998255b2012-10-13 02:07:45 +0000880 cmd_obj_sp = cmd_obj_sp->GetSubcommandSP (cmd_words.GetArgumentAtIndex (j));
Ed Masted78c9572014-04-20 00:31:37 +0000881 if (cmd_obj_sp.get() == nullptr)
Caroline Tice472362e2010-12-14 18:51:39 +0000882 // The sub-command name was invalid. Fail and return the empty 'ret_val'.
883 return ret_val;
884 }
885 else
886 // We have more words in the command name, but we don't have a multiword object. Fail and return
887 // empty 'ret_val'.
888 return ret_val;
889 }
890 // We successfully looped through all the command words and got valid command objects for them. Assign the
891 // last object retrieved to 'ret_val'.
892 ret_val = cmd_obj_sp;
893 }
894 }
895 return ret_val;
Jim Ingham279a6c22010-07-06 22:46:59 +0000896}
897
898CommandObject *
899CommandInterpreter::GetCommandObjectExact (const char *cmd_cstr, bool include_aliases)
900{
901 return GetCommandSPExact (cmd_cstr, include_aliases).get();
902}
903
904CommandObject *
905CommandInterpreter::GetCommandObject (const char *cmd_cstr, StringList *matches)
906{
907 CommandObject *command_obj = GetCommandSP (cmd_cstr, false, true, matches).get();
908
909 // If we didn't find an exact match to the command string in the commands, look in
910 // the aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000911
912 if (command_obj)
913 return command_obj;
Jim Ingham279a6c22010-07-06 22:46:59 +0000914
Enrico Granata5342c442013-06-18 18:01:08 +0000915 command_obj = GetCommandSP (cmd_cstr, true, true, matches).get();
Jim Ingham279a6c22010-07-06 22:46:59 +0000916
Enrico Granata5342c442013-06-18 18:01:08 +0000917 if (command_obj)
918 return command_obj;
919
920 // If there wasn't an exact match then look for an inexact one in just the commands
Ed Masted78c9572014-04-20 00:31:37 +0000921 command_obj = GetCommandSP(cmd_cstr, false, false, nullptr).get();
Matt Kopec038ff812013-04-23 16:17:32 +0000922
923 // Finally, if there wasn't an inexact match among the commands, look for an inexact
924 // match in both the commands and aliases.
Enrico Granata5342c442013-06-18 18:01:08 +0000925
926 if (command_obj)
927 {
928 if (matches)
929 matches->AppendString(command_obj->GetCommandName());
930 return command_obj;
931 }
932
933 return GetCommandSP(cmd_cstr, true, false, matches).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934}
935
936bool
937CommandInterpreter::CommandExists (const char *cmd)
938{
939 return m_command_dict.find(cmd) != m_command_dict.end();
940}
941
942bool
Caroline Ticeca90c472011-05-06 21:37:15 +0000943CommandInterpreter::ProcessAliasOptionsArgs (lldb::CommandObjectSP &cmd_obj_sp,
944 const char *options_args,
945 OptionArgVectorSP &option_arg_vector_sp)
946{
947 bool success = true;
948 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
949
950 if (!options_args || (strlen (options_args) < 1))
951 return true;
952
953 std::string options_string (options_args);
954 Args args (options_args);
955 CommandReturnObject result;
956 // Check to see if the command being aliased can take any command options.
957 Options *options = cmd_obj_sp->GetOptions ();
958 if (options)
959 {
960 // See if any options were specified as part of the alias; if so, handle them appropriately.
961 options->NotifyOptionParsingStarting ();
962 args.Unshift ("dummy_arg");
963 args.ParseAliasOptions (*options, result, option_arg_vector, options_string);
964 args.Shift ();
965 if (result.Succeeded())
966 options->VerifyPartialOptions (result);
967 if (!result.Succeeded() && result.GetStatus() != lldb::eReturnStatusStarted)
968 {
969 result.AppendError ("Unable to create requested alias.\n");
970 return false;
971 }
972 }
973
Greg Clayton5521f992011-10-28 21:38:01 +0000974 if (!options_string.empty())
Caroline Ticeca90c472011-05-06 21:37:15 +0000975 {
976 if (cmd_obj_sp->WantsRawCommandString ())
977 option_arg_vector->push_back (OptionArgPair ("<argument>",
978 OptionArgValue (-1,
979 options_string)));
980 else
981 {
Greg Claytonc7bece562013-01-25 18:06:21 +0000982 const size_t argc = args.GetArgumentCount();
Caroline Ticeca90c472011-05-06 21:37:15 +0000983 for (size_t i = 0; i < argc; ++i)
984 if (strcmp (args.GetArgumentAtIndex (i), "") != 0)
985 option_arg_vector->push_back
986 (OptionArgPair ("<argument>",
987 OptionArgValue (-1,
988 std::string (args.GetArgumentAtIndex (i)))));
989 }
990 }
991
992 return success;
993}
994
995bool
Jim Ingham298f3782013-04-03 00:25:49 +0000996CommandInterpreter::GetAliasFullName (const char *cmd, std::string &full_name)
997{
998 bool exact_match = (m_alias_dict.find(cmd) != m_alias_dict.end());
999 if (exact_match)
1000 {
1001 full_name.assign(cmd);
1002 return exact_match;
1003 }
1004 else
1005 {
1006 StringList matches;
1007 size_t num_alias_matches;
1008 num_alias_matches = CommandObject::AddNamesMatchingPartialString (m_alias_dict, cmd, matches);
1009 if (num_alias_matches == 1)
1010 {
1011 // Make sure this isn't shadowing a command in the regular command space:
1012 StringList regular_matches;
1013 const bool include_aliases = false;
1014 const bool exact = false;
1015 CommandObjectSP cmd_obj_sp(GetCommandSP (cmd, include_aliases, exact, &regular_matches));
1016 if (cmd_obj_sp || regular_matches.GetSize() > 0)
1017 return false;
1018 else
1019 {
1020 full_name.assign (matches.GetStringAtIndex(0));
1021 return true;
1022 }
1023 }
1024 else
1025 return false;
1026 }
1027}
1028
1029bool
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030CommandInterpreter::AliasExists (const char *cmd)
1031{
1032 return m_alias_dict.find(cmd) != m_alias_dict.end();
1033}
1034
1035bool
1036CommandInterpreter::UserCommandExists (const char *cmd)
1037{
1038 return m_user_dict.find(cmd) != m_user_dict.end();
1039}
1040
1041void
1042CommandInterpreter::AddAlias (const char *alias_name, CommandObjectSP& command_obj_sp)
1043{
Jim Ingham279a6c22010-07-06 22:46:59 +00001044 command_obj_sp->SetIsAlias (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001045 m_alias_dict[alias_name] = command_obj_sp;
1046}
1047
1048bool
1049CommandInterpreter::RemoveAlias (const char *alias_name)
1050{
1051 CommandObject::CommandMap::iterator pos = m_alias_dict.find(alias_name);
1052 if (pos != m_alias_dict.end())
1053 {
1054 m_alias_dict.erase(pos);
1055 return true;
1056 }
1057 return false;
1058}
1059bool
1060CommandInterpreter::RemoveUser (const char *alias_name)
1061{
1062 CommandObject::CommandMap::iterator pos = m_user_dict.find(alias_name);
1063 if (pos != m_user_dict.end())
1064 {
1065 m_user_dict.erase(pos);
1066 return true;
1067 }
1068 return false;
1069}
1070
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001071void
1072CommandInterpreter::GetAliasHelp (const char *alias_name, const char *command_name, StreamString &help_string)
1073{
1074 help_string.Printf ("'%s", command_name);
1075 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1076
Sean Callanan9a028512012-08-09 00:50:26 +00001077 if (option_arg_vector_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001078 {
1079 OptionArgVector *options = option_arg_vector_sp.get();
Andy Gibbsa297a972013-06-19 19:04:53 +00001080 for (size_t i = 0; i < options->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001081 {
1082 OptionArgPair cur_option = (*options)[i];
1083 std::string opt = cur_option.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00001084 OptionArgValue value_pair = cur_option.second;
1085 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001086 if (opt.compare("<argument>") == 0)
1087 {
1088 help_string.Printf (" %s", value.c_str());
1089 }
1090 else
1091 {
1092 help_string.Printf (" %s", opt.c_str());
1093 if ((value.compare ("<no-argument>") != 0)
1094 && (value.compare ("<need-argument") != 0))
1095 {
1096 help_string.Printf (" %s", value.c_str());
1097 }
1098 }
1099 }
1100 }
1101
1102 help_string.Printf ("'");
1103}
1104
Greg Clayton12fc3e02010-08-26 22:05:43 +00001105size_t
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001106CommandInterpreter::FindLongestCommandWord (CommandObject::CommandMap &dict)
1107{
1108 CommandObject::CommandMap::const_iterator pos;
Greg Clayton12fc3e02010-08-26 22:05:43 +00001109 CommandObject::CommandMap::const_iterator end = dict.end();
1110 size_t max_len = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001111
Greg Clayton12fc3e02010-08-26 22:05:43 +00001112 for (pos = dict.begin(); pos != end; ++pos)
1113 {
1114 size_t len = pos->first.size();
1115 if (max_len < len)
1116 max_len = len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001117 }
Greg Clayton12fc3e02010-08-26 22:05:43 +00001118 return max_len;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001119}
1120
1121void
Enrico Granata223383e2011-08-16 23:24:13 +00001122CommandInterpreter::GetHelp (CommandReturnObject &result,
Enrico Granata08633ee2011-09-09 17:49:36 +00001123 uint32_t cmd_types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124{
1125 CommandObject::CommandMap::const_iterator pos;
Greg Claytonc7bece562013-01-25 18:06:21 +00001126 size_t max_len = FindLongestCommandWord (m_command_dict);
Enrico Granata223383e2011-08-16 23:24:13 +00001127
1128 if ( (cmd_types & eCommandTypesBuiltin) == eCommandTypesBuiltin )
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001129 {
Enrico Granata223383e2011-08-16 23:24:13 +00001130
1131 result.AppendMessage("The following is a list of built-in, permanent debugger commands:");
1132 result.AppendMessage("");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001133
Enrico Granata223383e2011-08-16 23:24:13 +00001134 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
1135 {
1136 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1137 max_len);
1138 }
1139 result.AppendMessage("");
1140
1141 }
1142
Greg Clayton5521f992011-10-28 21:38:01 +00001143 if (!m_alias_dict.empty() && ( (cmd_types & eCommandTypesAliases) == eCommandTypesAliases ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 {
Jim Ingham49e80a12010-10-22 18:47:16 +00001145 result.AppendMessage("The following is a list of your current command abbreviations "
Johnny Chenb89982d2011-04-21 00:39:18 +00001146 "(see 'help command alias' for more info):");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 result.AppendMessage("");
Greg Clayton12fc3e02010-08-26 22:05:43 +00001148 max_len = FindLongestCommandWord (m_alias_dict);
1149
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150 for (pos = m_alias_dict.begin(); pos != m_alias_dict.end(); ++pos)
1151 {
1152 StreamString sstr;
1153 StreamString translation_and_help;
1154 std::string entry_name = pos->first;
1155 std::string second_entry = pos->second.get()->GetCommandName();
1156 GetAliasHelp (pos->first.c_str(), pos->second->GetCommandName(), sstr);
1157
1158 translation_and_help.Printf ("(%s) %s", sstr.GetData(), pos->second->GetHelp());
1159 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--",
1160 translation_and_help.GetData(), max_len);
1161 }
1162 result.AppendMessage("");
1163 }
1164
Greg Clayton5521f992011-10-28 21:38:01 +00001165 if (!m_user_dict.empty() && ( (cmd_types & eCommandTypesUserDef) == eCommandTypesUserDef ))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001166 {
1167 result.AppendMessage ("The following is a list of your current user-defined commands:");
1168 result.AppendMessage("");
Enrico Granata223383e2011-08-16 23:24:13 +00001169 max_len = FindLongestCommandWord (m_user_dict);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001170 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
1171 {
Enrico Granata223383e2011-08-16 23:24:13 +00001172 OutputFormattedHelpText (result.GetOutputStream(), pos->first.c_str(), "--", pos->second->GetHelp(),
1173 max_len);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 }
1175 result.AppendMessage("");
1176 }
1177
1178 result.AppendMessage("For more information on any particular command, try 'help <command-name>'.");
1179}
1180
Caroline Tice844d2302010-12-09 22:52:49 +00001181CommandObject *
1182CommandInterpreter::GetCommandObjectForCommand (std::string &command_string)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001183{
Caroline Tice844d2302010-12-09 22:52:49 +00001184 // This function finds the final, lowest-level, alias-resolved command object whose 'Execute' function will
1185 // eventually be invoked by the given command line.
1186
Ed Masted78c9572014-04-20 00:31:37 +00001187 CommandObject *cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001188 std::string white_space (" \t\v");
1189 size_t start = command_string.find_first_not_of (white_space);
1190 size_t end = 0;
1191 bool done = false;
1192 while (!done)
1193 {
1194 if (start != std::string::npos)
1195 {
1196 // Get the next word from command_string.
1197 end = command_string.find_first_of (white_space, start);
1198 if (end == std::string::npos)
1199 end = command_string.size();
1200 std::string cmd_word = command_string.substr (start, end - start);
1201
Ed Masted78c9572014-04-20 00:31:37 +00001202 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001203 // Since cmd_obj is NULL we are on our first time through this loop. Check to see if cmd_word is a valid
1204 // command or alias.
1205 cmd_obj = GetCommandObject (cmd_word.c_str());
1206 else if (cmd_obj->IsMultiwordObject ())
1207 {
1208 // 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 +00001209 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (cmd_word.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001210 if (sub_cmd_obj)
1211 cmd_obj = sub_cmd_obj;
1212 else // cmd_word was not a valid sub-command word, so we are donee
1213 done = true;
1214 }
1215 else
1216 // We have a cmd_obj and it is not a multi-word object, so we are done.
1217 done = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218
Caroline Tice844d2302010-12-09 22:52:49 +00001219 // If we didn't find a valid command object, or our command object is not a multi-word object, or
1220 // we are at the end of the command_string, then we are done. Otherwise, find the start of the
1221 // next word.
1222
1223 if (!cmd_obj || !cmd_obj->IsMultiwordObject() || end >= command_string.size())
1224 done = true;
1225 else
1226 start = command_string.find_first_not_of (white_space, end);
1227 }
1228 else
1229 // Unable to find any more words.
1230 done = true;
1231 }
1232
1233 if (end == command_string.size())
1234 command_string.clear();
1235 else
1236 command_string = command_string.substr(end);
1237
1238 return cmd_obj;
1239}
1240
Greg Clayton51964162011-10-25 00:36:27 +00001241static const char *k_white_space = " \t\v";
Greg Clayton5521f992011-10-28 21:38:01 +00001242static const char *k_valid_command_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
Greg Clayton51964162011-10-25 00:36:27 +00001243static void
1244StripLeadingSpaces (std::string &s)
Caroline Tice844d2302010-12-09 22:52:49 +00001245{
Greg Clayton51964162011-10-25 00:36:27 +00001246 if (!s.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001247 {
Greg Clayton51964162011-10-25 00:36:27 +00001248 size_t pos = s.find_first_not_of (k_white_space);
1249 if (pos == std::string::npos)
1250 s.clear();
1251 else if (pos == 0)
1252 return;
1253 s.erase (0, pos);
1254 }
1255}
1256
Greg Clayton93c62e62011-11-09 23:25:03 +00001257static size_t
1258FindArgumentTerminator (const std::string &s)
1259{
Greg Clayton93c62e62011-11-09 23:25:03 +00001260 const size_t s_len = s.size();
1261 size_t offset = 0;
1262 while (offset < s_len)
1263 {
1264 size_t pos = s.find ("--", offset);
1265 if (pos == std::string::npos)
1266 break;
1267 if (pos > 0)
1268 {
1269 if (isspace(s[pos-1]))
1270 {
1271 // Check if the string ends "\s--" (where \s is a space character)
1272 // or if we have "\s--\s".
1273 if ((pos + 2 >= s_len) || isspace(s[pos+2]))
1274 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001275 return pos;
1276 }
1277 }
1278 }
1279 offset = pos + 2;
1280 }
Greg Clayton93c62e62011-11-09 23:25:03 +00001281 return std::string::npos;
1282}
1283
Greg Clayton51964162011-10-25 00:36:27 +00001284static bool
Greg Clayton5521f992011-10-28 21:38:01 +00001285ExtractCommand (std::string &command_string, std::string &command, std::string &suffix, char &quote_char)
Greg Clayton51964162011-10-25 00:36:27 +00001286{
Greg Clayton5521f992011-10-28 21:38:01 +00001287 command.clear();
1288 suffix.clear();
Greg Clayton51964162011-10-25 00:36:27 +00001289 StripLeadingSpaces (command_string);
1290
1291 bool result = false;
1292 quote_char = '\0';
1293
1294 if (!command_string.empty())
1295 {
1296 const char first_char = command_string[0];
1297 if (first_char == '\'' || first_char == '"')
Caroline Tice844d2302010-12-09 22:52:49 +00001298 {
Greg Clayton51964162011-10-25 00:36:27 +00001299 quote_char = first_char;
1300 const size_t end_quote_pos = command_string.find (quote_char, 1);
1301 if (end_quote_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001302 {
Greg Clayton5521f992011-10-28 21:38:01 +00001303 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001304 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001305 }
1306 else
1307 {
Greg Clayton5521f992011-10-28 21:38:01 +00001308 command.assign (command_string, 1, end_quote_pos - 1);
Greg Clayton51964162011-10-25 00:36:27 +00001309 if (end_quote_pos + 1 < command_string.size())
1310 command_string.erase (0, command_string.find_first_not_of (k_white_space, end_quote_pos + 1));
1311 else
1312 command_string.erase ();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001313 }
Caroline Tice844d2302010-12-09 22:52:49 +00001314 }
1315 else
1316 {
Greg Clayton51964162011-10-25 00:36:27 +00001317 const size_t first_space_pos = command_string.find_first_of (k_white_space);
1318 if (first_space_pos == std::string::npos)
Caroline Tice2b5e8502011-05-11 16:07:06 +00001319 {
Greg Clayton5521f992011-10-28 21:38:01 +00001320 command.swap (command_string);
Greg Clayton51964162011-10-25 00:36:27 +00001321 command_string.erase();
Caroline Tice2b5e8502011-05-11 16:07:06 +00001322 }
1323 else
1324 {
Greg Clayton5521f992011-10-28 21:38:01 +00001325 command.assign (command_string, 0, first_space_pos);
1326 command_string.erase(0, command_string.find_first_not_of (k_white_space, first_space_pos));
Caroline Tice2b5e8502011-05-11 16:07:06 +00001327 }
Caroline Tice844d2302010-12-09 22:52:49 +00001328 }
Greg Clayton51964162011-10-25 00:36:27 +00001329 result = true;
Caroline Tice844d2302010-12-09 22:52:49 +00001330 }
Greg Clayton5521f992011-10-28 21:38:01 +00001331
1332
1333 if (!command.empty())
1334 {
1335 // actual commands can't start with '-' or '_'
1336 if (command[0] != '-' && command[0] != '_')
1337 {
1338 size_t pos = command.find_first_not_of(k_valid_command_chars);
1339 if (pos > 0 && pos != std::string::npos)
1340 {
1341 suffix.assign (command.begin() + pos, command.end());
1342 command.erase (pos);
1343 }
1344 }
1345 }
Greg Clayton51964162011-10-25 00:36:27 +00001346
1347 return result;
Caroline Tice844d2302010-12-09 22:52:49 +00001348}
1349
Greg Clayton5521f992011-10-28 21:38:01 +00001350CommandObject *
1351CommandInterpreter::BuildAliasResult (const char *alias_name,
1352 std::string &raw_input_string,
1353 std::string &alias_result,
1354 CommandReturnObject &result)
Caroline Tice844d2302010-12-09 22:52:49 +00001355{
Ed Masted78c9572014-04-20 00:31:37 +00001356 CommandObject *alias_cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001357 Args cmd_args (raw_input_string.c_str());
1358 alias_cmd_obj = GetCommandObject (alias_name);
1359 StreamString result_str;
1360
1361 if (alias_cmd_obj)
1362 {
1363 std::string alias_name_str = alias_name;
1364 if ((cmd_args.GetArgumentCount() == 0)
1365 || (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0))
1366 cmd_args.Unshift (alias_name);
1367
1368 result_str.Printf ("%s", alias_cmd_obj->GetCommandName ());
1369 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
1370
1371 if (option_arg_vector_sp.get())
1372 {
1373 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
1374
Andy Gibbsa297a972013-06-19 19:04:53 +00001375 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Caroline Tice844d2302010-12-09 22:52:49 +00001376 {
1377 OptionArgPair option_pair = (*option_arg_vector)[i];
1378 OptionArgValue value_pair = option_pair.second;
1379 int value_type = value_pair.first;
1380 std::string option = option_pair.first;
1381 std::string value = value_pair.second;
1382 if (option.compare ("<argument>") == 0)
1383 result_str.Printf (" %s", value.c_str());
1384 else
1385 {
1386 result_str.Printf (" %s", option.c_str());
Virgile Belloe2607b52013-09-05 16:42:23 +00001387 if (value_type != OptionParser::eOptionalArgument)
Caroline Tice844d2302010-12-09 22:52:49 +00001388 result_str.Printf (" ");
Virgile Belloe2607b52013-09-05 16:42:23 +00001389 if (value.compare ("<OptionParser::eNoArgument>") != 0)
Caroline Tice844d2302010-12-09 22:52:49 +00001390 {
1391 int index = GetOptionArgumentPosition (value.c_str());
1392 if (index == 0)
1393 result_str.Printf ("%s", value.c_str());
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00001394 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Caroline Tice844d2302010-12-09 22:52:49 +00001395 {
1396
1397 result.AppendErrorWithFormat
1398 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
1399 index);
1400 result.SetStatus (eReturnStatusFailed);
Greg Clayton5521f992011-10-28 21:38:01 +00001401 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001402 }
1403 else
1404 {
1405 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
1406 if (strpos != std::string::npos)
1407 raw_input_string = raw_input_string.erase (strpos,
1408 strlen (cmd_args.GetArgumentAtIndex (index)));
1409 result_str.Printf ("%s", cmd_args.GetArgumentAtIndex (index));
1410 }
1411 }
1412 }
1413 }
1414 }
1415
1416 alias_result = result_str.GetData();
1417 }
Greg Clayton5521f992011-10-28 21:38:01 +00001418 return alias_cmd_obj;
Caroline Tice844d2302010-12-09 22:52:49 +00001419}
1420
Greg Clayton5a314712011-10-14 07:41:33 +00001421Error
1422CommandInterpreter::PreprocessCommand (std::string &command)
1423{
1424 // The command preprocessor needs to do things to the command
1425 // line before any parsing of arguments or anything else is done.
1426 // The only current stuff that gets proprocessed is anyting enclosed
1427 // in backtick ('`') characters is evaluated as an expression and
1428 // the result of the expression must be a scalar that can be substituted
1429 // into the command. An example would be:
1430 // (lldb) memory read `$rsp + 20`
1431 Error error; // Error for any expressions that might not evaluate
1432 size_t start_backtick;
1433 size_t pos = 0;
1434 while ((start_backtick = command.find ('`', pos)) != std::string::npos)
1435 {
1436 if (start_backtick > 0 && command[start_backtick-1] == '\\')
1437 {
1438 // The backtick was preceeded by a '\' character, remove the slash
1439 // and don't treat the backtick as the start of an expression
1440 command.erase(start_backtick-1, 1);
1441 // No need to add one to start_backtick since we just deleted a char
1442 pos = start_backtick;
1443 }
1444 else
1445 {
1446 const size_t expr_content_start = start_backtick + 1;
1447 const size_t end_backtick = command.find ('`', expr_content_start);
1448 if (end_backtick == std::string::npos)
1449 return error;
1450 else if (end_backtick == expr_content_start)
1451 {
1452 // Empty expression (two backticks in a row)
1453 command.erase (start_backtick, 2);
1454 }
1455 else
1456 {
1457 std::string expr_str (command, expr_content_start, end_backtick - expr_content_start);
1458
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00001459 ExecutionContext exe_ctx(GetExecutionContext());
1460 Target *target = exe_ctx.GetTargetPtr();
Johnny Chen51ea0ad2011-10-29 00:21:50 +00001461 // Get a dummy target to allow for calculator mode while processing backticks.
1462 // This also helps break the infinite loop caused when target is null.
1463 if (!target)
Jim Ingham893c9322014-11-22 01:42:44 +00001464 target = m_debugger.GetDummyTarget();
Greg Clayton5a314712011-10-14 07:41:33 +00001465 if (target)
1466 {
Greg Clayton5a314712011-10-14 07:41:33 +00001467 ValueObjectSP expr_result_valobj_sp;
Enrico Granatad4439aa2012-09-05 20:41:26 +00001468
Jim Ingham35e1bda2012-10-16 21:41:58 +00001469 EvaluateExpressionOptions options;
Jim Ingham6fbc48b2013-11-07 00:11:47 +00001470 options.SetCoerceToId(false);
1471 options.SetUnwindOnError(true);
1472 options.SetIgnoreBreakpoints(true);
1473 options.SetKeepInMemory(false);
1474 options.SetTryAllThreads(true);
1475 options.SetTimeoutUsec(0);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001476
Jim Ingham1624a2d2014-05-05 02:26:40 +00001477 ExpressionResults expr_result = target->EvaluateExpression (expr_str.c_str(),
Enrico Granataca0e5ad2014-10-09 23:09:40 +00001478 exe_ctx.GetFramePtr(),
1479 expr_result_valobj_sp,
1480 options);
Enrico Granatad4439aa2012-09-05 20:41:26 +00001481
Jim Ingham8646d3c2014-05-05 02:47:44 +00001482 if (expr_result == eExpressionCompleted)
Greg Clayton5a314712011-10-14 07:41:33 +00001483 {
1484 Scalar scalar;
Enrico Granataca0e5ad2014-10-09 23:09:40 +00001485 if (expr_result_valobj_sp)
1486 expr_result_valobj_sp = expr_result_valobj_sp->GetQualifiedRepresentationIfAvailable(expr_result_valobj_sp->GetDynamicValueType(), true);
Greg Clayton5a314712011-10-14 07:41:33 +00001487 if (expr_result_valobj_sp->ResolveValue (scalar))
1488 {
1489 command.erase (start_backtick, end_backtick - start_backtick + 1);
1490 StreamString value_strm;
1491 const bool show_type = false;
1492 scalar.GetValue (&value_strm, show_type);
1493 size_t value_string_size = value_strm.GetSize();
1494 if (value_string_size)
1495 {
1496 command.insert (start_backtick, value_strm.GetData(), value_string_size);
1497 pos = start_backtick + value_string_size;
1498 continue;
1499 }
1500 else
1501 {
1502 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1503 }
1504 }
1505 else
1506 {
1507 error.SetErrorStringWithFormat("expression value didn't result in a scalar value for the expression '%s'", expr_str.c_str());
1508 }
1509 }
1510 else
1511 {
1512 if (expr_result_valobj_sp)
1513 error = expr_result_valobj_sp->GetError();
1514 if (error.Success())
1515 {
1516
1517 switch (expr_result)
1518 {
Jim Ingham8646d3c2014-05-05 02:47:44 +00001519 case eExpressionSetupError:
Greg Clayton5a314712011-10-14 07:41:33 +00001520 error.SetErrorStringWithFormat("expression setup error for the expression '%s'", expr_str.c_str());
1521 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001522 case eExpressionParseError:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001523 error.SetErrorStringWithFormat ("expression parse error for the expression '%s'", expr_str.c_str());
1524 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001525 case eExpressionResultUnavailable:
Jim Ingham1624a2d2014-05-05 02:26:40 +00001526 error.SetErrorStringWithFormat ("expression error fetching result for the expression '%s'", expr_str.c_str());
Jim Ingham8646d3c2014-05-05 02:47:44 +00001527 case eExpressionCompleted:
Greg Clayton5a314712011-10-14 07:41:33 +00001528 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001529 case eExpressionDiscarded:
Greg Clayton5a314712011-10-14 07:41:33 +00001530 error.SetErrorStringWithFormat("expression discarded for the expression '%s'", expr_str.c_str());
1531 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001532 case eExpressionInterrupted:
Greg Clayton5a314712011-10-14 07:41:33 +00001533 error.SetErrorStringWithFormat("expression interrupted for the expression '%s'", expr_str.c_str());
1534 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001535 case eExpressionHitBreakpoint:
Jim Ingham184e9812013-01-15 02:47:48 +00001536 error.SetErrorStringWithFormat("expression hit breakpoint for the expression '%s'", expr_str.c_str());
1537 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001538 case eExpressionTimedOut:
Greg Clayton5a314712011-10-14 07:41:33 +00001539 error.SetErrorStringWithFormat("expression timed out for the expression '%s'", expr_str.c_str());
1540 break;
Jim Ingham8646d3c2014-05-05 02:47:44 +00001541 case eExpressionStoppedForDebug:
Greg Claytonc28ce782013-11-08 23:38:26 +00001542 error.SetErrorStringWithFormat("expression stop at entry point for debugging for the expression '%s'", expr_str.c_str());
1543 break;
Greg Clayton5a314712011-10-14 07:41:33 +00001544 }
1545 }
1546 }
1547 }
1548 }
1549 if (error.Fail())
1550 break;
1551 }
1552 }
1553 return error;
1554}
1555
1556
Caroline Tice844d2302010-12-09 22:52:49 +00001557bool
1558CommandInterpreter::HandleCommand (const char *command_line,
Enrico Granata5f5ab602012-05-31 01:09:06 +00001559 LazyBool lazy_add_to_history,
Caroline Tice844d2302010-12-09 22:52:49 +00001560 CommandReturnObject &result,
Jim Inghame16c50a2011-02-18 00:54:25 +00001561 ExecutionContext *override_context,
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001562 bool repeat_on_empty_command,
1563 bool no_context_switching)
Jim Inghame16c50a2011-02-18 00:54:25 +00001564
Caroline Tice844d2302010-12-09 22:52:49 +00001565{
Jim Inghame16c50a2011-02-18 00:54:25 +00001566
Caroline Tice844d2302010-12-09 22:52:49 +00001567 bool done = false;
Ed Masted78c9572014-04-20 00:31:37 +00001568 CommandObject *cmd_obj = nullptr;
Caroline Tice844d2302010-12-09 22:52:49 +00001569 bool wants_raw_input = false;
1570 std::string command_string (command_line);
Jim Inghama5a97eb2011-07-12 03:12:18 +00001571 std::string original_command_string (command_line);
Caroline Tice844d2302010-12-09 22:52:49 +00001572
Greg Clayton5160ce52013-03-27 23:08:40 +00001573 Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_COMMANDS));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001574 Host::SetCrashDescriptionWithFormat ("HandleCommand(command = \"%s\")", command_line);
1575
1576 // Make a scoped cleanup object that will clear the crash description string
1577 // on exit of this function.
Ed Masted78c9572014-04-20 00:31:37 +00001578 lldb_utility::CleanUp <const char *> crash_description_cleanup(nullptr, Host::SetCrashDescription);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001579
Caroline Tice844d2302010-12-09 22:52:49 +00001580 if (log)
1581 log->Printf ("Processing command: %s", command_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001582
Jim Ingham30244832010-11-04 23:08:45 +00001583 Timer scoped_timer (__PRETTY_FUNCTION__, "Handling command: %s.", command_line);
1584
Johnny Chen80fdd7c2011-10-05 00:42:59 +00001585 if (!no_context_switching)
1586 UpdateExecutionContext (override_context);
Enrico Granata5f5ab602012-05-31 01:09:06 +00001587
Enrico Granata5f5ab602012-05-31 01:09:06 +00001588 bool add_to_history;
1589 if (lazy_add_to_history == eLazyBoolCalculate)
1590 add_to_history = (m_command_source_depth == 0);
1591 else
1592 add_to_history = (lazy_add_to_history == eLazyBoolYes);
1593
Jim Inghame16c50a2011-02-18 00:54:25 +00001594 bool empty_command = false;
1595 bool comment_command = false;
1596 if (command_string.empty())
1597 empty_command = true;
1598 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001599 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001600 const char *k_space_characters = "\t\n\v\f\r ";
1601
1602 size_t non_space = command_string.find_first_not_of (k_space_characters);
1603 // Check for empty line or comment line (lines whose first
1604 // non-space character is the comment character for this interpreter)
1605 if (non_space == std::string::npos)
1606 empty_command = true;
1607 else if (command_string[non_space] == m_comment_char)
1608 comment_command = true;
Enrico Granata7594f142013-06-17 22:51:50 +00001609 else if (command_string[non_space] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001610 {
Enrico Granata7594f142013-06-17 22:51:50 +00001611 const char *history_string = m_command_history.FindString(command_string.c_str() + non_space);
Ed Masted78c9572014-04-20 00:31:37 +00001612 if (history_string == nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00001613 {
1614 result.AppendErrorWithFormat ("Could not find entry: %s in history", command_string.c_str());
1615 result.SetStatus(eReturnStatusFailed);
1616 return false;
1617 }
1618 add_to_history = false;
1619 command_string = history_string;
1620 original_command_string = history_string;
1621 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001622 }
1623
1624 if (empty_command)
1625 {
1626 if (repeat_on_empty_command)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001627 {
Enrico Granata7594f142013-06-17 22:51:50 +00001628 if (m_command_history.IsEmpty())
Jim Inghame16c50a2011-02-18 00:54:25 +00001629 {
1630 result.AppendError ("empty command");
1631 result.SetStatus(eReturnStatusFailed);
1632 return false;
1633 }
1634 else
1635 {
1636 command_line = m_repeat_command.c_str();
1637 command_string = command_line;
Jim Inghama5a97eb2011-07-12 03:12:18 +00001638 original_command_string = command_line;
Jim Inghame16c50a2011-02-18 00:54:25 +00001639 if (m_repeat_command.empty())
1640 {
1641 result.AppendErrorWithFormat("No auto repeat.\n");
1642 result.SetStatus (eReturnStatusFailed);
1643 return false;
1644 }
1645 }
1646 add_to_history = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001647 }
1648 else
1649 {
Jim Inghame16c50a2011-02-18 00:54:25 +00001650 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1651 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001652 }
Jim Inghame16c50a2011-02-18 00:54:25 +00001653 }
1654 else if (comment_command)
1655 {
1656 result.SetStatus (eReturnStatusSuccessFinishNoResult);
1657 return true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001659
Greg Clayton5a314712011-10-14 07:41:33 +00001660
1661 Error error (PreprocessCommand (command_string));
1662
1663 if (error.Fail())
1664 {
1665 result.AppendError (error.AsCString());
1666 result.SetStatus(eReturnStatusFailed);
1667 return false;
1668 }
Caroline Tice844d2302010-12-09 22:52:49 +00001669 // Phase 1.
1670
1671 // Before we do ANY kind of argument processing, etc. we need to figure out what the real/final command object
1672 // is for the specified command, and whether or not it wants raw input. This gets complicated by the fact that
1673 // the user could have specified an alias, and in translating the alias there may also be command options and/or
1674 // even data (including raw text strings) that need to be found and inserted into the command line as part of
1675 // 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 +00001676 // object whose Execute method will actually be called; 2). a revised command string, with all substitutions &
Caroline Tice844d2302010-12-09 22:52:49 +00001677 // replacements taken care of; 3). whether or not the Execute function wants raw input or not.
Caroline Ticed9d63362010-12-07 19:58:26 +00001678
Caroline Tice844d2302010-12-09 22:52:49 +00001679 StreamString revised_command_line;
Caroline Tice01274c02010-12-11 08:16:56 +00001680 size_t actual_cmd_name_len = 0;
Greg Clayton5521f992011-10-28 21:38:01 +00001681 std::string next_word;
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001682 StringList matches;
Caroline Tice844d2302010-12-09 22:52:49 +00001683 while (!done)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001684 {
Caroline Tice2b5e8502011-05-11 16:07:06 +00001685 char quote_char = '\0';
Greg Clayton5521f992011-10-28 21:38:01 +00001686 std::string suffix;
1687 ExtractCommand (command_string, next_word, suffix, quote_char);
Ed Masted78c9572014-04-20 00:31:37 +00001688 if (cmd_obj == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001689 {
Jim Ingham298f3782013-04-03 00:25:49 +00001690 std::string full_name;
1691 if (GetAliasFullName(next_word.c_str(), full_name))
Caroline Tice472362e2010-12-14 18:51:39 +00001692 {
Greg Clayton5521f992011-10-28 21:38:01 +00001693 std::string alias_result;
Jim Ingham298f3782013-04-03 00:25:49 +00001694 cmd_obj = BuildAliasResult (full_name.c_str(), command_string, alias_result, result);
Greg Clayton5521f992011-10-28 21:38:01 +00001695 revised_command_line.Printf ("%s", alias_result.c_str());
1696 if (cmd_obj)
1697 {
1698 wants_raw_input = cmd_obj->WantsRawCommandString ();
1699 actual_cmd_name_len = strlen (cmd_obj->GetCommandName());
1700 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001701 }
1702 else
1703 {
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001704 cmd_obj = GetCommandObject (next_word.c_str(), &matches);
Greg Clayton5521f992011-10-28 21:38:01 +00001705 if (cmd_obj)
1706 {
1707 actual_cmd_name_len += next_word.length();
1708 revised_command_line.Printf ("%s", next_word.c_str());
1709 wants_raw_input = cmd_obj->WantsRawCommandString ();
1710 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001711 else
Greg Clayton5521f992011-10-28 21:38:01 +00001712 {
1713 revised_command_line.Printf ("%s", next_word.c_str());
1714 }
Caroline Tice844d2302010-12-09 22:52:49 +00001715 }
1716 }
1717 else
1718 {
Greg Clayton5521f992011-10-28 21:38:01 +00001719 if (cmd_obj->IsMultiwordObject ())
1720 {
Greg Clayton998255b2012-10-13 02:07:45 +00001721 CommandObject *sub_cmd_obj = cmd_obj->GetSubcommandObject (next_word.c_str());
Greg Clayton5521f992011-10-28 21:38:01 +00001722 if (sub_cmd_obj)
1723 {
1724 actual_cmd_name_len += next_word.length() + 1;
1725 revised_command_line.Printf (" %s", next_word.c_str());
1726 cmd_obj = sub_cmd_obj;
1727 wants_raw_input = cmd_obj->WantsRawCommandString ();
1728 }
1729 else
1730 {
1731 if (quote_char)
1732 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1733 else
1734 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1735 done = true;
1736 }
1737 }
Caroline Tice2b5e8502011-05-11 16:07:06 +00001738 else
Greg Clayton5521f992011-10-28 21:38:01 +00001739 {
1740 if (quote_char)
1741 revised_command_line.Printf (" %c%s%s%c", quote_char, next_word.c_str(), suffix.c_str(), quote_char);
1742 else
1743 revised_command_line.Printf (" %s%s", next_word.c_str(), suffix.c_str());
1744 done = true;
1745 }
Caroline Tice844d2302010-12-09 22:52:49 +00001746 }
1747
Ed Masted78c9572014-04-20 00:31:37 +00001748 if (cmd_obj == nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001749 {
Greg Claytonc7bece562013-01-25 18:06:21 +00001750 const size_t num_matches = matches.GetSize();
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001751 if (matches.GetSize() > 1) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001752 StreamString error_msg;
1753 error_msg.Printf ("Ambiguous command '%s'. Possible matches:\n", next_word.c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001754
1755 for (uint32_t i = 0; i < num_matches; ++i) {
Greg Claytonc7bece562013-01-25 18:06:21 +00001756 error_msg.Printf ("\t%s\n", matches.GetStringAtIndex(i));
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001757 }
Greg Claytonc7bece562013-01-25 18:06:21 +00001758 result.AppendRawError (error_msg.GetString().c_str());
Filipe Cabecinhasaf1537f2012-05-16 23:25:54 +00001759 } else {
1760 // We didn't have only one match, otherwise we wouldn't get here.
1761 assert(num_matches == 0);
1762 result.AppendErrorWithFormat ("'%s' is not a valid command.\n", next_word.c_str());
1763 }
Caroline Tice844d2302010-12-09 22:52:49 +00001764 result.SetStatus (eReturnStatusFailed);
1765 return false;
1766 }
1767
Greg Clayton5521f992011-10-28 21:38:01 +00001768 if (cmd_obj->IsMultiwordObject ())
1769 {
1770 if (!suffix.empty())
1771 {
1772
Enrico Granataaded51d2013-06-12 00:44:43 +00001773 result.AppendErrorWithFormat ("command '%s' did not recognize '%s%s%s' as valid (subcommand might be invalid).\n",
1774 cmd_obj->GetCommandName(),
1775 next_word.empty() ? "" : next_word.c_str(),
1776 next_word.empty() ? " -- " : " ",
Greg Clayton5521f992011-10-28 21:38:01 +00001777 suffix.c_str());
1778 result.SetStatus (eReturnStatusFailed);
1779 return false;
1780 }
1781 }
1782 else
1783 {
1784 // If we found a normal command, we are done
1785 done = true;
1786 if (!suffix.empty())
1787 {
1788 switch (suffix[0])
1789 {
1790 case '/':
1791 // GDB format suffixes
Greg Clayton52ec56c2011-10-29 00:57:28 +00001792 {
1793 Options *command_options = cmd_obj->GetOptions();
1794 if (command_options && command_options->SupportsLongOption("gdb-format"))
1795 {
Greg Clayton93c62e62011-11-09 23:25:03 +00001796 std::string gdb_format_option ("--gdb-format=");
1797 gdb_format_option += (suffix.c_str() + 1);
1798
1799 bool inserted = false;
1800 std::string &cmd = revised_command_line.GetString();
1801 size_t arg_terminator_idx = FindArgumentTerminator (cmd);
1802 if (arg_terminator_idx != std::string::npos)
1803 {
1804 // Insert the gdb format option before the "--" that terminates options
1805 gdb_format_option.append(1,' ');
1806 cmd.insert(arg_terminator_idx, gdb_format_option);
1807 inserted = true;
1808 }
1809
1810 if (!inserted)
1811 revised_command_line.Printf (" %s", gdb_format_option.c_str());
1812
1813 if (wants_raw_input && FindArgumentTerminator(cmd) == std::string::npos)
1814 revised_command_line.PutCString (" --");
Greg Clayton52ec56c2011-10-29 00:57:28 +00001815 }
1816 else
1817 {
1818 result.AppendErrorWithFormat ("the '%s' command doesn't support the --gdb-format option\n",
1819 cmd_obj->GetCommandName());
1820 result.SetStatus (eReturnStatusFailed);
1821 return false;
1822 }
1823 }
Greg Clayton5521f992011-10-28 21:38:01 +00001824 break;
Johnny Chen8e9383d2011-10-31 22:22:06 +00001825
1826 default:
1827 result.AppendErrorWithFormat ("unknown command shorthand suffix: '%s'\n",
1828 suffix.c_str());
1829 result.SetStatus (eReturnStatusFailed);
1830 return false;
1831
Greg Clayton5521f992011-10-28 21:38:01 +00001832 }
1833 }
1834 }
Caroline Tice844d2302010-12-09 22:52:49 +00001835 if (command_string.length() == 0)
1836 done = true;
1837
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001838 }
Caroline Tice844d2302010-12-09 22:52:49 +00001839
Greg Clayton5521f992011-10-28 21:38:01 +00001840 if (!command_string.empty())
Caroline Tice844d2302010-12-09 22:52:49 +00001841 revised_command_line.Printf (" %s", command_string.c_str());
1842
1843 // End of Phase 1.
1844 // At this point cmd_obj should contain the CommandObject whose Execute method will be called, if the command
1845 // specified was valid; revised_command_line contains the complete command line (including command name(s)),
1846 // fully translated with all substitutions & translations taken care of (still in raw text format); and
1847 // wants_raw_input specifies whether the Execute method expects raw input or not.
1848
1849
1850 if (log)
1851 {
1852 log->Printf ("HandleCommand, cmd_obj : '%s'", cmd_obj ? cmd_obj->GetCommandName() : "<not found>");
1853 log->Printf ("HandleCommand, revised_command_line: '%s'", revised_command_line.GetData());
1854 log->Printf ("HandleCommand, wants_raw_input:'%s'", wants_raw_input ? "True" : "False");
1855 }
1856
1857 // Phase 2.
1858 // Take care of things like setting up the history command & calling the appropriate Execute method on the
1859 // CommandObject, with the appropriate arguments.
1860
Ed Masted78c9572014-04-20 00:31:37 +00001861 if (cmd_obj != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001862 {
1863 if (add_to_history)
1864 {
1865 Args command_args (revised_command_line.GetData());
1866 const char *repeat_command = cmd_obj->GetRepeatCommand(command_args, 0);
Ed Masted78c9572014-04-20 00:31:37 +00001867 if (repeat_command != nullptr)
Caroline Tice844d2302010-12-09 22:52:49 +00001868 m_repeat_command.assign(repeat_command);
1869 else
Jim Inghama5a97eb2011-07-12 03:12:18 +00001870 m_repeat_command.assign(original_command_string.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001871
Enrico Granata7594f142013-06-17 22:51:50 +00001872 m_command_history.AppendString (original_command_string);
Caroline Tice844d2302010-12-09 22:52:49 +00001873 }
1874
1875 command_string = revised_command_line.GetData();
1876 std::string command_name (cmd_obj->GetCommandName());
Caroline Tice01274c02010-12-11 08:16:56 +00001877 std::string remainder;
1878 if (actual_cmd_name_len < command_string.length())
1879 remainder = command_string.substr (actual_cmd_name_len); // Note: 'actual_cmd_name_len' may be considerably shorter
1880 // than cmd_obj->GetCommandName(), because name completion
1881 // allows users to enter short versions of the names,
1882 // e.g. 'br s' for 'breakpoint set'.
Caroline Tice844d2302010-12-09 22:52:49 +00001883
1884 // Remove any initial spaces
1885 std::string white_space (" \t\v");
1886 size_t pos = remainder.find_first_not_of (white_space);
1887 if (pos != 0 && pos != std::string::npos)
Greg Claytona3482592011-04-22 20:58:45 +00001888 remainder.erase(0, pos);
Caroline Tice844d2302010-12-09 22:52:49 +00001889
1890 if (log)
Jason Molendabfb36ff2011-08-25 00:20:04 +00001891 log->Printf ("HandleCommand, command line after removing command name(s): '%s'", remainder.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001892
Jim Ingham5a988412012-06-08 21:56:10 +00001893 cmd_obj->Execute (remainder.c_str(), result);
Caroline Tice844d2302010-12-09 22:52:49 +00001894 }
1895 else
1896 {
1897 // We didn't find the first command object, so complete the first argument.
1898 Args command_args (revised_command_line.GetData());
1899 StringList matches;
1900 int num_matches;
1901 int cursor_index = 0;
1902 int cursor_char_position = strlen (command_args.GetArgumentAtIndex(0));
1903 bool word_complete;
1904 num_matches = HandleCompletionMatches (command_args,
1905 cursor_index,
1906 cursor_char_position,
1907 0,
1908 -1,
1909 word_complete,
1910 matches);
1911
1912 if (num_matches > 0)
1913 {
1914 std::string error_msg;
1915 error_msg.assign ("ambiguous command '");
1916 error_msg.append(command_args.GetArgumentAtIndex(0));
1917 error_msg.append ("'.");
1918
1919 error_msg.append (" Possible completions:");
1920 for (int i = 0; i < num_matches; i++)
1921 {
1922 error_msg.append ("\n\t");
1923 error_msg.append (matches.GetStringAtIndex (i));
1924 }
1925 error_msg.append ("\n");
Greg Claytonc7bece562013-01-25 18:06:21 +00001926 result.AppendRawError (error_msg.c_str());
Caroline Tice844d2302010-12-09 22:52:49 +00001927 }
1928 else
1929 result.AppendErrorWithFormat ("Unrecognized command '%s'.\n", command_args.GetArgumentAtIndex (0));
1930
1931 result.SetStatus (eReturnStatusFailed);
1932 }
1933
Jason Molendabfb36ff2011-08-25 00:20:04 +00001934 if (log)
1935 log->Printf ("HandleCommand, command %s", (result.Succeeded() ? "succeeded" : "did not succeed"));
1936
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001937 return result.Succeeded();
1938}
1939
1940int
1941CommandInterpreter::HandleCompletionMatches (Args &parsed_line,
1942 int &cursor_index,
1943 int &cursor_char_position,
1944 int match_start_point,
1945 int max_return_elements,
Jim Ingham558ce122010-06-30 05:02:46 +00001946 bool &word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001947 StringList &matches)
1948{
1949 int num_command_matches = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001950 bool look_for_subcommand = false;
Jim Ingham558ce122010-06-30 05:02:46 +00001951
1952 // For any of the command completions a unique match will be a complete word.
1953 word_complete = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001954
1955 if (cursor_index == -1)
1956 {
1957 // We got nothing on the command line, so return the list of commands
Jim Ingham279a6c22010-07-06 22:46:59 +00001958 bool include_aliases = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001959 num_command_matches = GetCommandNamesMatchingPartialString ("", include_aliases, matches);
1960 }
1961 else if (cursor_index == 0)
1962 {
1963 // The cursor is in the first argument, so just do a lookup in the dictionary.
Jim Ingham279a6c22010-07-06 22:46:59 +00001964 CommandObject *cmd_obj = GetCommandObject (parsed_line.GetArgumentAtIndex(0), &matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001965 num_command_matches = matches.GetSize();
1966
1967 if (num_command_matches == 1
1968 && cmd_obj && cmd_obj->IsMultiwordObject()
Ed Masted78c9572014-04-20 00:31:37 +00001969 && matches.GetStringAtIndex(0) != nullptr
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001970 && strcmp (parsed_line.GetArgumentAtIndex(0), matches.GetStringAtIndex(0)) == 0)
1971 {
Greg Clayton765d2e22013-12-10 19:14:04 +00001972 if (parsed_line.GetArgumentCount() == 1)
1973 {
1974 word_complete = true;
1975 }
1976 else
1977 {
1978 look_for_subcommand = true;
1979 num_command_matches = 0;
1980 matches.DeleteStringAtIndex(0);
1981 parsed_line.AppendArgument ("");
1982 cursor_index++;
1983 cursor_char_position = 0;
1984 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001985 }
1986 }
1987
1988 if (cursor_index > 0 || look_for_subcommand)
1989 {
1990 // We are completing further on into a commands arguments, so find the command and tell it
1991 // to complete the command.
1992 // First see if there is a matching initial command:
Jim Ingham279a6c22010-07-06 22:46:59 +00001993 CommandObject *command_object = GetCommandObject (parsed_line.GetArgumentAtIndex(0));
Ed Masted78c9572014-04-20 00:31:37 +00001994 if (command_object == nullptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001995 {
1996 return 0;
1997 }
1998 else
1999 {
2000 parsed_line.Shift();
2001 cursor_index--;
Greg Claytona7015092010-09-18 01:14:36 +00002002 num_command_matches = command_object->HandleCompletion (parsed_line,
Greg Clayton66111032010-06-23 01:19:29 +00002003 cursor_index,
2004 cursor_char_position,
2005 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002006 max_return_elements,
2007 word_complete,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002008 matches);
2009 }
2010 }
2011
2012 return num_command_matches;
2013
2014}
2015
2016int
2017CommandInterpreter::HandleCompletion (const char *current_line,
2018 const char *cursor,
2019 const char *last_char,
2020 int match_start_point,
2021 int max_return_elements,
2022 StringList &matches)
2023{
2024 // We parse the argument up to the cursor, so the last argument in parsed_line is
2025 // the one containing the cursor, and the cursor is after the last character.
2026
2027 Args parsed_line(current_line, last_char - current_line);
2028 Args partial_parsed_line(current_line, cursor - current_line);
2029
Jim Inghama5a97eb2011-07-12 03:12:18 +00002030 // Don't complete comments, and if the line we are completing is just the history repeat character,
2031 // substitute the appropriate history line.
2032 const char *first_arg = parsed_line.GetArgumentAtIndex(0);
2033 if (first_arg)
2034 {
2035 if (first_arg[0] == m_comment_char)
2036 return 0;
Enrico Granata7594f142013-06-17 22:51:50 +00002037 else if (first_arg[0] == CommandHistory::g_repeat_char)
Jim Inghama5a97eb2011-07-12 03:12:18 +00002038 {
Enrico Granata7594f142013-06-17 22:51:50 +00002039 const char *history_string = m_command_history.FindString (first_arg);
Ed Masted78c9572014-04-20 00:31:37 +00002040 if (history_string != nullptr)
Jim Inghama5a97eb2011-07-12 03:12:18 +00002041 {
2042 matches.Clear();
2043 matches.InsertStringAtIndex(0, history_string);
2044 return -2;
2045 }
2046 else
2047 return 0;
2048
2049 }
2050 }
2051
2052
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002053 int num_args = partial_parsed_line.GetArgumentCount();
2054 int cursor_index = partial_parsed_line.GetArgumentCount() - 1;
2055 int cursor_char_position;
2056
2057 if (cursor_index == -1)
2058 cursor_char_position = 0;
2059 else
2060 cursor_char_position = strlen (partial_parsed_line.GetArgumentAtIndex(cursor_index));
Jim Inghamfe0c4252010-12-14 19:56:01 +00002061
2062 if (cursor > current_line && cursor[-1] == ' ')
2063 {
2064 // We are just after a space. If we are in an argument, then we will continue
2065 // parsing, but if we are between arguments, then we have to complete whatever the next
2066 // element would be.
2067 // We can distinguish the two cases because if we are in an argument (e.g. because the space is
2068 // protected by a quote) then the space will also be in the parsed argument...
2069
2070 const char *current_elem = partial_parsed_line.GetArgumentAtIndex(cursor_index);
2071 if (cursor_char_position == 0 || current_elem[cursor_char_position - 1] != ' ')
2072 {
Greg Clayton765d2e22013-12-10 19:14:04 +00002073 parsed_line.InsertArgumentAtIndex(cursor_index + 1, "", '\0');
Jim Inghamfe0c4252010-12-14 19:56:01 +00002074 cursor_index++;
2075 cursor_char_position = 0;
2076 }
2077 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002078
2079 int num_command_matches;
2080
2081 matches.Clear();
2082
2083 // Only max_return_elements == -1 is supported at present:
2084 assert (max_return_elements == -1);
Jim Ingham558ce122010-06-30 05:02:46 +00002085 bool word_complete;
Greg Clayton66111032010-06-23 01:19:29 +00002086 num_command_matches = HandleCompletionMatches (parsed_line,
2087 cursor_index,
2088 cursor_char_position,
2089 match_start_point,
Jim Ingham558ce122010-06-30 05:02:46 +00002090 max_return_elements,
2091 word_complete,
Greg Clayton66111032010-06-23 01:19:29 +00002092 matches);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002093
2094 if (num_command_matches <= 0)
2095 return num_command_matches;
2096
2097 if (num_args == 0)
2098 {
2099 // If we got an empty string, insert nothing.
2100 matches.InsertStringAtIndex(0, "");
2101 }
2102 else
2103 {
2104 // Now figure out if there is a common substring, and if so put that in element 0, otherwise
2105 // put an empty string in element 0.
2106 std::string command_partial_str;
2107 if (cursor_index >= 0)
Jim Ingham49e80a12010-10-22 18:47:16 +00002108 command_partial_str.assign(parsed_line.GetArgumentAtIndex(cursor_index),
2109 parsed_line.GetArgumentAtIndex(cursor_index) + cursor_char_position);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002110
2111 std::string common_prefix;
2112 matches.LongestCommonPrefix (common_prefix);
Greg Claytonc7bece562013-01-25 18:06:21 +00002113 const size_t partial_name_len = command_partial_str.size();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002114
2115 // If we matched a unique single command, add a space...
Jim Ingham558ce122010-06-30 05:02:46 +00002116 // Only do this if the completer told us this was a complete word, however...
2117 if (num_command_matches == 1 && word_complete)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002118 {
2119 char quote_char = parsed_line.GetArgumentQuoteCharAtIndex(cursor_index);
2120 if (quote_char != '\0')
2121 common_prefix.push_back(quote_char);
2122
2123 common_prefix.push_back(' ');
2124 }
2125 common_prefix.erase (0, partial_name_len);
2126 matches.InsertStringAtIndex(0, common_prefix.c_str());
2127 }
2128 return num_command_matches;
2129}
2130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002131
2132CommandInterpreter::~CommandInterpreter ()
2133{
2134}
2135
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002136void
Greg Clayton44d93782014-01-27 23:43:24 +00002137CommandInterpreter::UpdatePrompt (const char *new_prompt)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002138{
Greg Clayton44d93782014-01-27 23:43:24 +00002139 EventSP prompt_change_event_sp (new Event(eBroadcastBitResetPrompt, new EventDataBytes (new_prompt)));;
2140 BroadcastEvent (prompt_change_event_sp);
2141 if (m_command_io_handler_sp)
2142 m_command_io_handler_sp->SetPrompt(new_prompt);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002143}
2144
Jim Ingham97a6dc72010-10-04 19:49:29 +00002145
2146bool
2147CommandInterpreter::Confirm (const char *message, bool default_answer)
2148{
Jim Ingham3bcdb292010-10-04 22:44:14 +00002149 // Check AutoConfirm first:
2150 if (m_debugger.GetAutoConfirm())
2151 return default_answer;
Greg Clayton44d93782014-01-27 23:43:24 +00002152
2153 IOHandlerConfirm *confirm = new IOHandlerConfirm(m_debugger,
2154 message,
2155 default_answer);
2156 IOHandlerSP io_handler_sp (confirm);
2157 m_debugger.RunIOHandler (io_handler_sp);
2158 return confirm->GetResponse();
Jim Ingham97a6dc72010-10-04 19:49:29 +00002159}
2160
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002161OptionArgVectorSP
2162CommandInterpreter::GetAliasOptions (const char *alias_name)
2163{
2164 OptionArgMap::iterator pos;
2165 OptionArgVectorSP ret_val;
2166
2167 std::string alias (alias_name);
2168
2169 if (HasAliasOptions())
2170 {
2171 pos = m_alias_options.find (alias);
2172 if (pos != m_alias_options.end())
2173 ret_val = pos->second;
2174 }
2175
2176 return ret_val;
2177}
2178
2179void
2180CommandInterpreter::RemoveAliasOptions (const char *alias_name)
2181{
2182 OptionArgMap::iterator pos = m_alias_options.find(alias_name);
2183 if (pos != m_alias_options.end())
2184 {
2185 m_alias_options.erase (pos);
2186 }
2187}
2188
2189void
2190CommandInterpreter::AddOrReplaceAliasOptions (const char *alias_name, OptionArgVectorSP &option_arg_vector_sp)
2191{
2192 m_alias_options[alias_name] = option_arg_vector_sp;
2193}
2194
2195bool
2196CommandInterpreter::HasCommands ()
2197{
2198 return (!m_command_dict.empty());
2199}
2200
2201bool
2202CommandInterpreter::HasAliases ()
2203{
2204 return (!m_alias_dict.empty());
2205}
2206
2207bool
2208CommandInterpreter::HasUserCommands ()
2209{
2210 return (!m_user_dict.empty());
2211}
2212
2213bool
2214CommandInterpreter::HasAliasOptions ()
2215{
2216 return (!m_alias_options.empty());
2217}
2218
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002219void
Caroline Tice4ab31c92010-10-12 21:57:09 +00002220CommandInterpreter::BuildAliasCommandArgs (CommandObject *alias_cmd_obj,
2221 const char *alias_name,
2222 Args &cmd_args,
Caroline Ticed9d63362010-12-07 19:58:26 +00002223 std::string &raw_input_string,
Caroline Tice4ab31c92010-10-12 21:57:09 +00002224 CommandReturnObject &result)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002225{
2226 OptionArgVectorSP option_arg_vector_sp = GetAliasOptions (alias_name);
Caroline Ticed9d63362010-12-07 19:58:26 +00002227
2228 bool wants_raw_input = alias_cmd_obj->WantsRawCommandString();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002229
Caroline Ticed9d63362010-12-07 19:58:26 +00002230 // Make sure that the alias name is the 0th element in cmd_args
2231 std::string alias_name_str = alias_name;
2232 if (alias_name_str.compare (cmd_args.GetArgumentAtIndex(0)) != 0)
2233 cmd_args.Unshift (alias_name);
2234
2235 Args new_args (alias_cmd_obj->GetCommandName());
2236 if (new_args.GetArgumentCount() == 2)
2237 new_args.Shift();
2238
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002239 if (option_arg_vector_sp.get())
2240 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002241 if (wants_raw_input)
2242 {
2243 // We have a command that both has command options and takes raw input. Make *sure* it has a
2244 // " -- " in the right place in the raw_input_string.
2245 size_t pos = raw_input_string.find(" -- ");
2246 if (pos == std::string::npos)
2247 {
2248 // None found; assume it goes at the beginning of the raw input string
2249 raw_input_string.insert (0, " -- ");
2250 }
2251 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002252
2253 OptionArgVector *option_arg_vector = option_arg_vector_sp.get();
Greg Claytonc7bece562013-01-25 18:06:21 +00002254 const size_t old_size = cmd_args.GetArgumentCount();
Caroline Tice4ab31c92010-10-12 21:57:09 +00002255 std::vector<bool> used (old_size + 1, false);
2256
2257 used[0] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002258
Andy Gibbsa297a972013-06-19 19:04:53 +00002259 for (size_t i = 0; i < option_arg_vector->size(); ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 {
2261 OptionArgPair option_pair = (*option_arg_vector)[i];
Caroline Ticed9d63362010-12-07 19:58:26 +00002262 OptionArgValue value_pair = option_pair.second;
2263 int value_type = value_pair.first;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002264 std::string option = option_pair.first;
Caroline Ticed9d63362010-12-07 19:58:26 +00002265 std::string value = value_pair.second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002266 if (option.compare ("<argument>") == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002267 {
2268 if (!wants_raw_input
2269 || (value.compare("--") != 0)) // Since we inserted this above, make sure we don't insert it twice
2270 new_args.AppendArgument (value.c_str());
2271 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002272 else
2273 {
Virgile Belloe2607b52013-09-05 16:42:23 +00002274 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002275 new_args.AppendArgument (option.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002276 if (value.compare ("<no-argument>") != 0)
2277 {
2278 int index = GetOptionArgumentPosition (value.c_str());
2279 if (index == 0)
Caroline Ticed9d63362010-12-07 19:58:26 +00002280 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002281 // value was NOT a positional argument; must be a real value
Virgile Belloe2607b52013-09-05 16:42:23 +00002282 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002283 new_args.AppendArgument (value.c_str());
2284 else
2285 {
2286 char buffer[255];
2287 ::snprintf (buffer, sizeof (buffer), "%s%s", option.c_str(), value.c_str());
2288 new_args.AppendArgument (buffer);
2289 }
2290
2291 }
Saleem Abdulrasool3985c8c2014-04-02 03:51:35 +00002292 else if (static_cast<size_t>(index) >= cmd_args.GetArgumentCount())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002293 {
2294 result.AppendErrorWithFormat
2295 ("Not enough arguments provided; you need at least %d arguments to use this alias.\n",
2296 index);
2297 result.SetStatus (eReturnStatusFailed);
2298 return;
2299 }
2300 else
2301 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002302 // Find and remove cmd_args.GetArgumentAtIndex(i) from raw_input_string
2303 size_t strpos = raw_input_string.find (cmd_args.GetArgumentAtIndex (index));
2304 if (strpos != std::string::npos)
2305 {
2306 raw_input_string = raw_input_string.erase (strpos, strlen (cmd_args.GetArgumentAtIndex (index)));
2307 }
2308
Virgile Belloe2607b52013-09-05 16:42:23 +00002309 if (value_type != OptionParser::eOptionalArgument)
Caroline Ticed9d63362010-12-07 19:58:26 +00002310 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (index));
2311 else
2312 {
2313 char buffer[255];
2314 ::snprintf (buffer, sizeof(buffer), "%s%s", option.c_str(),
2315 cmd_args.GetArgumentAtIndex (index));
2316 new_args.AppendArgument (buffer);
2317 }
Caroline Tice4ab31c92010-10-12 21:57:09 +00002318 used[index] = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002319 }
2320 }
2321 }
2322 }
2323
Andy Gibbsa297a972013-06-19 19:04:53 +00002324 for (size_t j = 0; j < cmd_args.GetArgumentCount(); ++j)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002325 {
Caroline Ticed9d63362010-12-07 19:58:26 +00002326 if (!used[j] && !wants_raw_input)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002327 new_args.AppendArgument (cmd_args.GetArgumentAtIndex (j));
2328 }
2329
2330 cmd_args.Clear();
2331 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2332 }
2333 else
2334 {
2335 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Caroline Ticed9d63362010-12-07 19:58:26 +00002336 // This alias was not created with any options; nothing further needs to be done, unless it is a command that
2337 // wants raw input, in which case we need to clear the rest of the data from cmd_args, since its in the raw
2338 // input string.
2339 if (wants_raw_input)
2340 {
2341 cmd_args.Clear();
2342 cmd_args.SetArguments (new_args.GetArgumentCount(), (const char **) new_args.GetArgumentVector());
2343 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002344 return;
2345 }
2346
2347 result.SetStatus (eReturnStatusSuccessFinishNoResult);
2348 return;
2349}
2350
2351
2352int
2353CommandInterpreter::GetOptionArgumentPosition (const char *in_string)
2354{
2355 int position = 0; // Any string that isn't an argument position, i.e. '%' followed by an integer, gets a position
2356 // of zero.
2357
2358 char *cptr = (char *) in_string;
2359
2360 // Does it start with '%'
2361 if (cptr[0] == '%')
2362 {
2363 ++cptr;
2364
2365 // Is the rest of it entirely digits?
2366 if (isdigit (cptr[0]))
2367 {
2368 const char *start = cptr;
2369 while (isdigit (cptr[0]))
2370 ++cptr;
2371
2372 // We've gotten to the end of the digits; are we at the end of the string?
2373 if (cptr[0] == '\0')
2374 position = atoi (start);
2375 }
2376 }
2377
2378 return position;
2379}
2380
2381void
2382CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result)
2383{
Jim Ingham16e0c682011-08-12 23:34:31 +00002384 FileSpec init_file;
Greg Clayton14a35512011-09-11 00:01:44 +00002385 if (in_cwd)
Jim Ingham16e0c682011-08-12 23:34:31 +00002386 {
Greg Clayton14a35512011-09-11 00:01:44 +00002387 // In the current working directory we don't load any program specific
2388 // .lldbinit files, we only look for a "./.lldbinit" file.
2389 if (m_skip_lldbinit_files)
2390 return;
2391
2392 init_file.SetFile ("./.lldbinit", true);
Jim Ingham16e0c682011-08-12 23:34:31 +00002393 }
Greg Clayton14a35512011-09-11 00:01:44 +00002394 else
Jim Ingham16e0c682011-08-12 23:34:31 +00002395 {
Greg Clayton14a35512011-09-11 00:01:44 +00002396 // If we aren't looking in the current working directory we are looking
2397 // in the home directory. We will first see if there is an application
2398 // specific ".lldbinit" file whose name is "~/.lldbinit" followed by a
2399 // "-" and the name of the program. If this file doesn't exist, we fall
2400 // back to just the "~/.lldbinit" file. We also obey any requests to not
2401 // load the init files.
Zachary Turnera21fee02014-08-21 21:49:24 +00002402 llvm::SmallString<64> home_dir_path;
2403 llvm::sys::path::home_directory(home_dir_path);
2404 FileSpec profilePath(home_dir_path.c_str(), false);
Zachary Turner9e757b72014-07-28 16:45:05 +00002405 profilePath.AppendPathComponent(".lldbinit");
2406 std::string init_file_path = profilePath.GetPath();
Greg Clayton14a35512011-09-11 00:01:44 +00002407
2408 if (m_skip_app_init_files == false)
2409 {
Zachary Turnera21fee02014-08-21 21:49:24 +00002410 FileSpec program_file_spec(HostInfo::GetProgramFileSpec());
Greg Clayton14a35512011-09-11 00:01:44 +00002411 const char *program_name = program_file_spec.GetFilename().AsCString();
Jim Ingham16e0c682011-08-12 23:34:31 +00002412
Greg Clayton14a35512011-09-11 00:01:44 +00002413 if (program_name)
2414 {
2415 char program_init_file_name[PATH_MAX];
Zachary Turner9e757b72014-07-28 16:45:05 +00002416 ::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 +00002417 init_file.SetFile (program_init_file_name, true);
2418 if (!init_file.Exists())
2419 init_file.Clear();
2420 }
2421 }
2422
2423 if (!init_file && !m_skip_lldbinit_files)
Zachary Turner9e757b72014-07-28 16:45:05 +00002424 init_file.SetFile (init_file_path.c_str(), false);
Greg Clayton14a35512011-09-11 00:01:44 +00002425 }
2426
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002427 // If the file exists, tell HandleCommand to 'source' it; this will do the actual broadcasting
2428 // of the commands back to any appropriate listener (see CommandObjectSource::Execute for more details).
2429
2430 if (init_file.Exists())
2431 {
Greg Clayton44d93782014-01-27 23:43:24 +00002432 const bool saved_batch = SetBatchCommandMode (true);
Jim Ingham26c7bf92014-10-11 00:38:27 +00002433 CommandInterpreterRunOptions options;
2434 options.SetSilent (true);
2435 options.SetStopOnError (false);
2436 options.SetStopOnContinue (true);
2437
Greg Clayton340b0302014-02-05 17:57:57 +00002438 HandleCommandsFromFile (init_file,
Ed Masted78c9572014-04-20 00:31:37 +00002439 nullptr, // Execution context
Jim Ingham26c7bf92014-10-11 00:38:27 +00002440 options,
Greg Clayton340b0302014-02-05 17:57:57 +00002441 result);
Greg Clayton44d93782014-01-27 23:43:24 +00002442 SetBatchCommandMode (saved_batch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002443 }
2444 else
2445 {
2446 // nothing to be done if the file doesn't exist
2447 result.SetStatus(eReturnStatusSuccessFinishNoResult);
2448 }
2449}
2450
Greg Clayton8b82f082011-04-12 05:54:46 +00002451PlatformSP
2452CommandInterpreter::GetPlatform (bool prefer_target_platform)
2453{
2454 PlatformSP platform_sp;
Greg Claytonc14ee322011-09-22 04:58:26 +00002455 if (prefer_target_platform)
2456 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00002457 ExecutionContext exe_ctx(GetExecutionContext());
2458 Target *target = exe_ctx.GetTargetPtr();
Greg Claytonc14ee322011-09-22 04:58:26 +00002459 if (target)
2460 platform_sp = target->GetPlatform();
2461 }
Greg Clayton8b82f082011-04-12 05:54:46 +00002462
2463 if (!platform_sp)
2464 platform_sp = m_debugger.GetPlatformList().GetSelectedPlatform();
2465 return platform_sp;
2466}
2467
Jim Inghame16c50a2011-02-18 00:54:25 +00002468void
Jim Inghambad87fe2011-03-11 01:51:49 +00002469CommandInterpreter::HandleCommands (const StringList &commands,
Jim Ingham26c7bf92014-10-11 00:38:27 +00002470 ExecutionContext *override_context,
2471 CommandInterpreterRunOptions &options,
Jim Inghame16c50a2011-02-18 00:54:25 +00002472 CommandReturnObject &result)
2473{
2474 size_t num_lines = commands.GetSize();
Jim Inghame16c50a2011-02-18 00:54:25 +00002475
2476 // If we are going to continue past a "continue" then we need to run the commands synchronously.
2477 // Make sure you reset this value anywhere you return from the function.
2478
2479 bool old_async_execution = m_debugger.GetAsyncExecution();
2480
2481 // If we've been given an execution context, set it at the start, but don't keep resetting it or we will
2482 // cause series of commands that change the context, then do an operation that relies on that context to fail.
2483
Ed Masted78c9572014-04-20 00:31:37 +00002484 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00002485 UpdateExecutionContext (override_context);
Jim Inghame16c50a2011-02-18 00:54:25 +00002486
Jim Ingham26c7bf92014-10-11 00:38:27 +00002487 if (!options.GetStopOnContinue())
Jim Inghame16c50a2011-02-18 00:54:25 +00002488 {
2489 m_debugger.SetAsyncExecution (false);
2490 }
2491
Andy Gibbsa297a972013-06-19 19:04:53 +00002492 for (size_t idx = 0; idx < num_lines; idx++)
Jim Inghame16c50a2011-02-18 00:54:25 +00002493 {
2494 const char *cmd = commands.GetStringAtIndex(idx);
2495 if (cmd[0] == '\0')
2496 continue;
2497
Jim Ingham26c7bf92014-10-11 00:38:27 +00002498 if (options.GetEchoCommands())
Jim Inghame16c50a2011-02-18 00:54:25 +00002499 {
2500 result.AppendMessageWithFormat ("%s %s\n",
Greg Clayton44d93782014-01-27 23:43:24 +00002501 m_debugger.GetPrompt(),
2502 cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002503 }
2504
Greg Clayton9d0402b2011-02-20 02:15:07 +00002505 CommandReturnObject tmp_result;
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002506 // If override_context is not NULL, pass no_context_switching = true for
2507 // HandleCommand() since we updated our context already.
Jim Ingham076b7fc2013-05-02 23:15:37 +00002508
2509 // We might call into a regex or alias command, in which case the add_to_history will get lost. This
2510 // 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 +00002511 if (!options.GetAddToHistory())
Jim Ingham076b7fc2013-05-02 23:15:37 +00002512 m_command_source_depth++;
Jim Ingham26c7bf92014-10-11 00:38:27 +00002513 bool success = HandleCommand(cmd, options.m_add_to_history, tmp_result,
Ed Masted78c9572014-04-20 00:31:37 +00002514 nullptr, /* override_context */
Johnny Chen80fdd7c2011-10-05 00:42:59 +00002515 true, /* repeat_on_empty_command */
Ed Masted78c9572014-04-20 00:31:37 +00002516 override_context != nullptr /* no_context_switching */);
Jim Ingham26c7bf92014-10-11 00:38:27 +00002517 if (!options.GetAddToHistory())
Jim Ingham076b7fc2013-05-02 23:15:37 +00002518 m_command_source_depth--;
Jim Inghame16c50a2011-02-18 00:54:25 +00002519
Jim Ingham26c7bf92014-10-11 00:38:27 +00002520 if (options.GetPrintResults())
Jim Inghame16c50a2011-02-18 00:54:25 +00002521 {
2522 if (tmp_result.Succeeded())
Jim Ingham85e8b812011-02-19 02:53:09 +00002523 result.AppendMessageWithFormat("%s", tmp_result.GetOutputData());
Jim Inghame16c50a2011-02-18 00:54:25 +00002524 }
2525
2526 if (!success || !tmp_result.Succeeded())
2527 {
Jim Inghama5038812012-04-24 02:25:07 +00002528 const char *error_msg = tmp_result.GetErrorData();
Ed Masted78c9572014-04-20 00:31:37 +00002529 if (error_msg == nullptr || error_msg[0] == '\0')
Jim Inghama5038812012-04-24 02:25:07 +00002530 error_msg = "<unknown error>.\n";
Jim Ingham26c7bf92014-10-11 00:38:27 +00002531 if (options.GetStopOnError())
Jim Inghame16c50a2011-02-18 00:54:25 +00002532 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002533 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' failed with %s",
2534 (uint64_t)idx, cmd, error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002535 result.SetStatus (eReturnStatusFailed);
2536 m_debugger.SetAsyncExecution (old_async_execution);
2537 return;
2538 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002539 else if (options.GetPrintResults())
Jim Inghame16c50a2011-02-18 00:54:25 +00002540 {
Deepak Panickal99fbc072014-03-03 15:39:47 +00002541 result.AppendMessageWithFormat ("Command #%" PRIu64 " '%s' failed with %s",
2542 (uint64_t)idx + 1,
Jim Inghame16c50a2011-02-18 00:54:25 +00002543 cmd,
Jim Inghama5038812012-04-24 02:25:07 +00002544 error_msg);
Jim Inghame16c50a2011-02-18 00:54:25 +00002545 }
2546 }
2547
Caroline Tice969ed3d2011-05-02 20:41:46 +00002548 if (result.GetImmediateOutputStream())
2549 result.GetImmediateOutputStream()->Flush();
2550
2551 if (result.GetImmediateErrorStream())
2552 result.GetImmediateErrorStream()->Flush();
2553
Jim Inghame16c50a2011-02-18 00:54:25 +00002554 // N.B. Can't depend on DidChangeProcessState, because the state coming into the command execution
2555 // could be running (for instance in Breakpoint Commands.
2556 // So we check the return value to see if it is has running in it.
2557 if ((tmp_result.GetStatus() == eReturnStatusSuccessContinuingNoResult)
2558 || (tmp_result.GetStatus() == eReturnStatusSuccessContinuingResult))
2559 {
Jim Ingham26c7bf92014-10-11 00:38:27 +00002560 if (options.GetStopOnContinue())
Jim Inghame16c50a2011-02-18 00:54:25 +00002561 {
2562 // If we caused the target to proceed, and we're going to stop in that case, set the
2563 // status in our real result before returning. This is an error if the continue was not the
2564 // last command in the set of commands to be run.
2565 if (idx != num_lines - 1)
Deepak Panickal99fbc072014-03-03 15:39:47 +00002566 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' continued the target.\n",
2567 (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002568 else
Deepak Panickal99fbc072014-03-03 15:39:47 +00002569 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' continued the target.\n", (uint64_t)idx + 1, cmd);
Jim Inghame16c50a2011-02-18 00:54:25 +00002570
2571 result.SetStatus(tmp_result.GetStatus());
2572 m_debugger.SetAsyncExecution (old_async_execution);
2573
2574 return;
2575 }
2576 }
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002577
2578 // Also check for "stop on crash here:
2579 bool should_stop = false;
2580 if (tmp_result.GetDidChangeProcessState() && options.GetStopOnCrash())
2581 {
2582 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
2583 if (target_sp)
2584 {
2585 ProcessSP process_sp (target_sp->GetProcessSP());
2586 if (process_sp)
2587 {
2588 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
2589 {
2590 StopReason reason = thread_sp->GetStopReason();
2591 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
2592 {
2593 should_stop = true;
2594 break;
2595 }
2596 }
2597 }
2598 }
2599 if (should_stop)
2600 {
2601 if (idx != num_lines - 1)
2602 result.AppendErrorWithFormat("Aborting reading of commands after command #%" PRIu64 ": '%s' stopped with a signal or exception.\n",
2603 (uint64_t)idx + 1, cmd);
2604 else
2605 result.AppendMessageWithFormat("Command #%" PRIu64 " '%s' stopped with a signal or exception.\n", (uint64_t)idx + 1, cmd);
2606
2607 result.SetStatus(tmp_result.GetStatus());
2608 m_debugger.SetAsyncExecution (old_async_execution);
2609
2610 return;
2611 }
2612 }
Jim Inghame16c50a2011-02-18 00:54:25 +00002613
2614 }
2615
2616 result.SetStatus (eReturnStatusSuccessFinishResult);
2617 m_debugger.SetAsyncExecution (old_async_execution);
2618
2619 return;
2620}
2621
Greg Clayton340b0302014-02-05 17:57:57 +00002622// Make flags that we can pass into the IOHandler so our delegates can do the right thing
2623enum {
2624 eHandleCommandFlagStopOnContinue = (1u << 0),
2625 eHandleCommandFlagStopOnError = (1u << 1),
2626 eHandleCommandFlagEchoCommand = (1u << 2),
Jim Ingham26c7bf92014-10-11 00:38:27 +00002627 eHandleCommandFlagPrintResult = (1u << 3),
2628 eHandleCommandFlagStopOnCrash = (1u << 4)
Greg Clayton340b0302014-02-05 17:57:57 +00002629};
2630
Jim Inghame16c50a2011-02-18 00:54:25 +00002631void
2632CommandInterpreter::HandleCommandsFromFile (FileSpec &cmd_file,
2633 ExecutionContext *context,
Jim Ingham26c7bf92014-10-11 00:38:27 +00002634 CommandInterpreterRunOptions &options,
Jim Inghame16c50a2011-02-18 00:54:25 +00002635 CommandReturnObject &result)
2636{
2637 if (cmd_file.Exists())
2638 {
Greg Clayton44d93782014-01-27 23:43:24 +00002639 StreamFileSP input_file_sp (new StreamFile());
2640
2641 std::string cmd_file_path = cmd_file.GetPath();
2642 Error error = input_file_sp->GetFile().Open(cmd_file_path.c_str(), File::eOpenOptionRead);
2643
2644 if (error.Success())
2645 {
2646 Debugger &debugger = GetDebugger();
2647
Greg Clayton340b0302014-02-05 17:57:57 +00002648 uint32_t flags = 0;
2649
Jim Ingham26c7bf92014-10-11 00:38:27 +00002650 if (options.m_stop_on_continue == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002651 {
2652 if (m_command_source_flags.empty())
2653 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002654 // Stop on continue by default
Greg Clayton340b0302014-02-05 17:57:57 +00002655 flags |= eHandleCommandFlagStopOnContinue;
2656 }
2657 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnContinue)
2658 {
2659 flags |= eHandleCommandFlagStopOnContinue;
2660 }
2661 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002662 else if (options.m_stop_on_continue == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002663 {
2664 flags |= eHandleCommandFlagStopOnContinue;
2665 }
2666
Jim Ingham26c7bf92014-10-11 00:38:27 +00002667 if (options.m_stop_on_error == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002668 {
2669 if (m_command_source_flags.empty())
2670 {
2671 if (GetStopCmdSourceOnError())
2672 flags |= eHandleCommandFlagStopOnError;
2673 }
2674 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnError)
2675 {
2676 flags |= eHandleCommandFlagStopOnError;
2677 }
2678 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002679 else if (options.m_stop_on_error == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002680 {
2681 flags |= eHandleCommandFlagStopOnError;
2682 }
2683
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002684 if (options.GetStopOnCrash())
2685 {
2686 if (m_command_source_flags.empty())
2687 {
2688 // Echo command by default
2689 flags |= eHandleCommandFlagStopOnCrash;
2690 }
2691 else if (m_command_source_flags.back() & eHandleCommandFlagStopOnCrash)
2692 {
2693 flags |= eHandleCommandFlagStopOnCrash;
2694 }
2695 }
2696
Jim Ingham26c7bf92014-10-11 00:38:27 +00002697 if (options.m_echo_commands == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002698 {
2699 if (m_command_source_flags.empty())
2700 {
2701 // Echo command by default
2702 flags |= eHandleCommandFlagEchoCommand;
2703 }
2704 else if (m_command_source_flags.back() & eHandleCommandFlagEchoCommand)
2705 {
2706 flags |= eHandleCommandFlagEchoCommand;
2707 }
2708 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002709 else if (options.m_echo_commands == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002710 {
2711 flags |= eHandleCommandFlagEchoCommand;
2712 }
2713
Jim Ingham26c7bf92014-10-11 00:38:27 +00002714 if (options.m_print_results == eLazyBoolCalculate)
Greg Clayton340b0302014-02-05 17:57:57 +00002715 {
2716 if (m_command_source_flags.empty())
2717 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002718 // Print output by default
2719 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002720 }
Greg Claytone4e462c2014-02-05 21:03:22 +00002721 else if (m_command_source_flags.back() & eHandleCommandFlagPrintResult)
Greg Clayton340b0302014-02-05 17:57:57 +00002722 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002723 flags |= eHandleCommandFlagPrintResult;
Greg Clayton340b0302014-02-05 17:57:57 +00002724 }
2725 }
Jim Ingham26c7bf92014-10-11 00:38:27 +00002726 else if (options.m_print_results == eLazyBoolYes)
Greg Clayton340b0302014-02-05 17:57:57 +00002727 {
Greg Claytone4e462c2014-02-05 21:03:22 +00002728 flags |= eHandleCommandFlagPrintResult;
2729 }
2730
2731 if (flags & eHandleCommandFlagPrintResult)
2732 {
Greg Clayton8ee67312014-02-05 21:46:20 +00002733 debugger.GetOutputFile()->Printf("Executing commands in '%s'.\n", cmd_file_path.c_str());
Greg Clayton340b0302014-02-05 17:57:57 +00002734 }
2735
2736 // Used for inheriting the right settings when "command source" might have
2737 // nested "command source" commands
Greg Claytone4e462c2014-02-05 21:03:22 +00002738 lldb::StreamFileSP empty_stream_sp;
Greg Clayton340b0302014-02-05 17:57:57 +00002739 m_command_source_flags.push_back(flags);
Greg Clayton44d93782014-01-27 23:43:24 +00002740 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00002741 IOHandler::Type::CommandInterpreter,
Greg Clayton44d93782014-01-27 23:43:24 +00002742 input_file_sp,
Greg Claytone4e462c2014-02-05 21:03:22 +00002743 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader output stream
2744 empty_stream_sp, // Pass in an empty stream so we inherit the top input reader error stream
Greg Clayton340b0302014-02-05 17:57:57 +00002745 flags,
Ed Masted78c9572014-04-20 00:31:37 +00002746 nullptr, // Pass in NULL for "editline_name" so no history is saved, or written
Greg Clayton8ee67312014-02-05 21:46:20 +00002747 debugger.GetPrompt(),
Kate Stonee30f11d2014-11-17 19:06:59 +00002748 NULL,
Greg Clayton44d93782014-01-27 23:43:24 +00002749 false, // Not multi-line
Kate Stonee30f11d2014-11-17 19:06:59 +00002750 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00002751 0,
Greg Clayton44d93782014-01-27 23:43:24 +00002752 *this));
Greg Clayton8ee67312014-02-05 21:46:20 +00002753 const bool old_async_execution = debugger.GetAsyncExecution();
2754
Jim Inghamffc9f1d2014-10-14 01:20:07 +00002755 // Set synchronous execution if we are not stopping on continue
Greg Clayton8ee67312014-02-05 21:46:20 +00002756 if ((flags & eHandleCommandFlagStopOnContinue) == 0)
2757 debugger.SetAsyncExecution (false);
2758
Greg Clayton340b0302014-02-05 17:57:57 +00002759 m_command_source_depth++;
Greg Clayton8ee67312014-02-05 21:46:20 +00002760
2761 debugger.RunIOHandler(io_handler_sp);
Greg Clayton340b0302014-02-05 17:57:57 +00002762 if (!m_command_source_flags.empty())
2763 m_command_source_flags.pop_back();
2764 m_command_source_depth--;
Greg Clayton44d93782014-01-27 23:43:24 +00002765 result.SetStatus (eReturnStatusSuccessFinishNoResult);
Greg Clayton8ee67312014-02-05 21:46:20 +00002766 debugger.SetAsyncExecution (old_async_execution);
Greg Clayton44d93782014-01-27 23:43:24 +00002767 }
2768 else
2769 {
2770 result.AppendErrorWithFormat ("error: an error occurred read file '%s': %s\n", cmd_file_path.c_str(), error.AsCString());
2771 result.SetStatus (eReturnStatusFailed);
2772 }
Greg Clayton8ee67312014-02-05 21:46:20 +00002773
2774
Jim Inghame16c50a2011-02-18 00:54:25 +00002775 }
2776 else
2777 {
2778 result.AppendErrorWithFormat ("Error reading commands from file %s - file not found.\n",
Jim Ingham4af59612014-12-19 19:20:44 +00002779 cmd_file.GetFilename().AsCString("<Unknown>"));
Jim Inghame16c50a2011-02-18 00:54:25 +00002780 result.SetStatus (eReturnStatusFailed);
2781 return;
2782 }
2783}
2784
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002785ScriptInterpreter *
Enrico Granatab5887262012-10-29 21:18:03 +00002786CommandInterpreter::GetScriptInterpreter (bool can_create)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002787{
Ed Masted78c9572014-04-20 00:31:37 +00002788 if (m_script_interpreter_ap.get() != nullptr)
Enrico Granatab5887262012-10-29 21:18:03 +00002789 return m_script_interpreter_ap.get();
2790
2791 if (!can_create)
Ed Masted78c9572014-04-20 00:31:37 +00002792 return nullptr;
Enrico Granatab5887262012-10-29 21:18:03 +00002793
Enrico Granataa29bdad2012-07-10 18:23:48 +00002794 // <rdar://problem/11751427>
2795 // we need to protect the initialization of the script interpreter
2796 // otherwise we could end up with two threads both trying to create
2797 // their instance of it, and for some languages (e.g. Python)
2798 // this is a bulletproof recipe for disaster!
2799 // this needs to be a function-level static because multiple Debugger instances living in the same process
2800 // still need to be isolated and not try to initialize Python concurrently
Enrico Granata8b95df22012-07-10 19:04:14 +00002801 static Mutex g_interpreter_mutex(Mutex::eMutexTypeRecursive);
2802 Mutex::Locker interpreter_lock(g_interpreter_mutex);
Enrico Granataa29bdad2012-07-10 18:23:48 +00002803
Greg Clayton5160ce52013-03-27 23:08:40 +00002804 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_OBJECT));
Enrico Granatab5887262012-10-29 21:18:03 +00002805 if (log)
2806 log->Printf("Initializing the ScriptInterpreter now\n");
Greg Clayton66111032010-06-23 01:19:29 +00002807
Caroline Tice2f88aad2011-01-14 00:29:16 +00002808 lldb::ScriptLanguage script_lang = GetDebugger().GetScriptLanguage();
2809 switch (script_lang)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810 {
Greg Claytondce502e2011-11-04 03:34:56 +00002811 case eScriptLanguagePython:
2812#ifndef LLDB_DISABLE_PYTHON
2813 m_script_interpreter_ap.reset (new ScriptInterpreterPython (*this));
2814 break;
2815#else
2816 // Fall through to the None case when python is disabled
2817#endif
Caroline Tice2f88aad2011-01-14 00:29:16 +00002818 case eScriptLanguageNone:
2819 m_script_interpreter_ap.reset (new ScriptInterpreterNone (*this));
2820 break;
Caroline Tice2f88aad2011-01-14 00:29:16 +00002821 };
2822
2823 return m_script_interpreter_ap.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002824}
2825
2826
2827
2828bool
2829CommandInterpreter::GetSynchronous ()
2830{
2831 return m_synchronous_execution;
2832}
2833
2834void
2835CommandInterpreter::SetSynchronous (bool value)
2836{
Johnny Chenc066ab42010-10-14 01:22:03 +00002837 m_synchronous_execution = value;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002838}
2839
2840void
2841CommandInterpreter::OutputFormattedHelpText (Stream &strm,
2842 const char *word_text,
2843 const char *separator,
2844 const char *help_text,
Greg Claytonc7bece562013-01-25 18:06:21 +00002845 size_t max_word_len)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002846{
Greg Claytona7015092010-09-18 01:14:36 +00002847 const uint32_t max_columns = m_debugger.GetTerminalWidth();
2848
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002849 int indent_size = max_word_len + strlen (separator) + 2;
2850
2851 strm.IndentMore (indent_size);
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002852
2853 StreamString text_strm;
Greg Claytonc7bece562013-01-25 18:06:21 +00002854 text_strm.Printf ("%-*s %s %s", (int)max_word_len, word_text, separator, help_text);
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002855
2856 size_t len = text_strm.GetSize();
2857 const char *text = text_strm.GetData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002858 if (text[len - 1] == '\n')
Greg Claytonbfe5f3b2011-02-18 01:44:25 +00002859 {
2860 text_strm.EOL();
2861 len = text_strm.GetSize();
2862 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002863
2864 if (len < max_columns)
2865 {
2866 // Output it as a single line.
2867 strm.Printf ("%s", text);
2868 }
2869 else
2870 {
2871 // We need to break it up into multiple lines.
2872 bool first_line = true;
2873 int text_width;
Greg Claytonc7bece562013-01-25 18:06:21 +00002874 size_t start = 0;
2875 size_t end = start;
2876 const size_t final_end = strlen (text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002877
2878 while (end < final_end)
2879 {
2880 if (first_line)
2881 text_width = max_columns - 1;
2882 else
2883 text_width = max_columns - indent_size - 1;
2884
2885 // Don't start the 'text' on a space, since we're already outputting the indentation.
2886 if (!first_line)
2887 {
2888 while ((start < final_end) && (text[start] == ' '))
2889 start++;
2890 }
2891
2892 end = start + text_width;
2893 if (end > final_end)
2894 end = final_end;
2895 else
2896 {
2897 // If we're not at the end of the text, make sure we break the line on white space.
2898 while (end > start
2899 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
2900 end--;
Greg Clayton67cc0632012-08-22 17:17:09 +00002901 assert (end > 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002902 }
2903
Greg Claytonc7bece562013-01-25 18:06:21 +00002904 const size_t sub_len = end - start;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002905 if (start != 0)
2906 strm.EOL();
2907 if (!first_line)
2908 strm.Indent();
2909 else
2910 first_line = false;
2911 assert (start <= final_end);
2912 assert (start + sub_len <= final_end);
2913 if (sub_len > 0)
2914 strm.Write (text + start, sub_len);
2915 start = end + 1;
2916 }
2917 }
2918 strm.EOL();
2919 strm.IndentLess(indent_size);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002920}
2921
2922void
Enrico Granata82a7d982011-07-07 00:38:40 +00002923CommandInterpreter::OutputHelpText (Stream &strm,
2924 const char *word_text,
2925 const char *separator,
2926 const char *help_text,
2927 uint32_t max_word_len)
2928{
2929 int indent_size = max_word_len + strlen (separator) + 2;
2930
2931 strm.IndentMore (indent_size);
2932
2933 StreamString text_strm;
2934 text_strm.Printf ("%-*s %s %s", max_word_len, word_text, separator, help_text);
2935
2936 const uint32_t max_columns = m_debugger.GetTerminalWidth();
Enrico Granata82a7d982011-07-07 00:38:40 +00002937
2938 size_t len = text_strm.GetSize();
2939 const char *text = text_strm.GetData();
2940
2941 uint32_t chars_left = max_columns;
2942
2943 for (uint32_t i = 0; i < len; i++)
2944 {
2945 if ((text[i] == ' ' && ::strchr((text+i+1), ' ') && chars_left < ::strchr((text+i+1), ' ')-(text+i)) || text[i] == '\n')
2946 {
Enrico Granata82a7d982011-07-07 00:38:40 +00002947 chars_left = max_columns - indent_size;
2948 strm.EOL();
2949 strm.Indent();
2950 }
2951 else
2952 {
2953 strm.PutChar(text[i]);
2954 chars_left--;
2955 }
2956
2957 }
2958
2959 strm.EOL();
2960 strm.IndentLess(indent_size);
2961}
2962
2963void
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002964CommandInterpreter::FindCommandsForApropos (const char *search_word, StringList &commands_found,
Jim Inghamaf3753e2013-05-17 01:30:37 +00002965 StringList &commands_help, bool search_builtin_commands, bool search_user_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002966{
2967 CommandObject::CommandMap::const_iterator pos;
2968
Jim Inghamaf3753e2013-05-17 01:30:37 +00002969 if (search_builtin_commands)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002970 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002971 for (pos = m_command_dict.begin(); pos != m_command_dict.end(); ++pos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002972 {
Jim Inghamaf3753e2013-05-17 01:30:37 +00002973 const char *command_name = pos->first.c_str();
2974 CommandObject *cmd_obj = pos->second.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002975
Jim Inghamaf3753e2013-05-17 01:30:37 +00002976 if (cmd_obj->HelpTextContainsWord (search_word))
2977 {
2978 commands_found.AppendString (command_name);
2979 commands_help.AppendString (cmd_obj->GetHelp());
2980 }
2981
2982 if (cmd_obj->IsMultiwordObject())
2983 cmd_obj->AproposAllSubCommands (command_name,
2984 search_word,
2985 commands_found,
2986 commands_help);
2987
2988 }
2989 }
2990
2991 if (search_user_commands)
2992 {
2993 for (pos = m_user_dict.begin(); pos != m_user_dict.end(); ++pos)
2994 {
2995 const char *command_name = pos->first.c_str();
2996 CommandObject *cmd_obj = pos->second.get();
2997
2998 if (cmd_obj->HelpTextContainsWord (search_word))
2999 {
3000 commands_found.AppendString (command_name);
3001 commands_help.AppendString (cmd_obj->GetHelp());
3002 }
3003
3004 if (cmd_obj->IsMultiwordObject())
3005 cmd_obj->AproposAllSubCommands (command_name,
3006 search_word,
3007 commands_found,
3008 commands_help);
3009
3010 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003011 }
3012}
Greg Clayton8b82f082011-04-12 05:54:46 +00003013
Greg Clayton8b82f082011-04-12 05:54:46 +00003014void
3015CommandInterpreter::UpdateExecutionContext (ExecutionContext *override_context)
3016{
Ed Masted78c9572014-04-20 00:31:37 +00003017 if (override_context != nullptr)
Greg Clayton8b82f082011-04-12 05:54:46 +00003018 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00003019 m_exe_ctx_ref = *override_context;
Greg Clayton8b82f082011-04-12 05:54:46 +00003020 }
3021 else
3022 {
Greg Clayton4e0fe8a2012-07-12 20:32:19 +00003023 const bool adopt_selected = true;
3024 m_exe_ctx_ref.SetTargetPtr (m_debugger.GetSelectedTarget().get(), adopt_selected);
Greg Clayton8b82f082011-04-12 05:54:46 +00003025 }
3026}
Greg Clayton44d93782014-01-27 23:43:24 +00003027
3028
3029size_t
3030CommandInterpreter::GetProcessOutput ()
3031{
3032 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
3033 char stdio_buffer[1024];
3034 size_t len;
3035 size_t total_bytes = 0;
3036 Error error;
3037 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
3038 if (target_sp)
3039 {
3040 ProcessSP process_sp (target_sp->GetProcessSP());
3041 if (process_sp)
3042 {
3043 while ((len = process_sp->GetSTDOUT (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3044 {
3045 size_t bytes_written = len;
3046 m_debugger.GetOutputFile()->Write (stdio_buffer, bytes_written);
3047 total_bytes += len;
3048 }
3049 while ((len = process_sp->GetSTDERR (stdio_buffer, sizeof (stdio_buffer), error)) > 0)
3050 {
3051 size_t bytes_written = len;
3052 m_debugger.GetErrorFile()->Write (stdio_buffer, bytes_written);
3053 total_bytes += len;
3054 }
3055 }
3056 }
3057 return total_bytes;
3058}
3059
3060void
3061CommandInterpreter::IOHandlerInputComplete (IOHandler &io_handler, std::string &line)
3062{
Greg Clayton340b0302014-02-05 17:57:57 +00003063 const bool is_interactive = io_handler.GetIsInteractive();
3064 if (is_interactive == false)
3065 {
3066 // When we are not interactive, don't execute blank lines. This will happen
3067 // sourcing a commands file. We don't want blank lines to repeat the previous
3068 // command and cause any errors to occur (like redefining an alias, get an error
3069 // and stop parsing the commands file).
3070 if (line.empty())
3071 return;
3072
3073 // When using a non-interactive file handle (like when sourcing commands from a file)
3074 // we need to echo the command out so we don't just see the command output and no
3075 // command...
3076 if (io_handler.GetFlags().Test(eHandleCommandFlagEchoCommand))
3077 io_handler.GetOutputStreamFile()->Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3078 }
3079
Greg Clayton44d93782014-01-27 23:43:24 +00003080 lldb_private::CommandReturnObject result;
3081 HandleCommand(line.c_str(), eLazyBoolCalculate, result);
3082
Greg Clayton44d93782014-01-27 23:43:24 +00003083 // Now emit the command output text from the command we just executed
Greg Clayton340b0302014-02-05 17:57:57 +00003084 if (io_handler.GetFlags().Test(eHandleCommandFlagPrintResult))
3085 {
3086 // Display any STDOUT/STDERR _prior_ to emitting the command result text
3087 GetProcessOutput ();
3088
Greg Clayton8ee67312014-02-05 21:46:20 +00003089 if (!result.GetImmediateOutputStream())
3090 {
3091 const char *output = result.GetOutputData();
3092 if (output && output[0])
3093 io_handler.GetOutputStreamFile()->PutCString(output);
3094 }
Greg Clayton44d93782014-01-27 23:43:24 +00003095
Greg Clayton340b0302014-02-05 17:57:57 +00003096 // Now emit the command error text from the command we just executed
Greg Clayton8ee67312014-02-05 21:46:20 +00003097 if (!result.GetImmediateErrorStream())
3098 {
3099 const char *error = result.GetErrorData();
3100 if (error && error[0])
3101 io_handler.GetErrorStreamFile()->PutCString(error);
3102 }
Greg Clayton340b0302014-02-05 17:57:57 +00003103 }
Greg Clayton44d93782014-01-27 23:43:24 +00003104
3105 switch (result.GetStatus())
3106 {
3107 case eReturnStatusInvalid:
3108 case eReturnStatusSuccessFinishNoResult:
3109 case eReturnStatusSuccessFinishResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003110 case eReturnStatusStarted:
3111 break;
3112
Greg Clayton44d93782014-01-27 23:43:24 +00003113 case eReturnStatusSuccessContinuingNoResult:
3114 case eReturnStatusSuccessContinuingResult:
Greg Clayton340b0302014-02-05 17:57:57 +00003115 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnContinue))
3116 io_handler.SetIsDone(true);
3117 break;
3118
Greg Clayton44d93782014-01-27 23:43:24 +00003119 case eReturnStatusFailed:
Jim Ingham26c7bf92014-10-11 00:38:27 +00003120 m_num_errors++;
Greg Clayton340b0302014-02-05 17:57:57 +00003121 if (io_handler.GetFlags().Test(eHandleCommandFlagStopOnError))
3122 io_handler.SetIsDone(true);
Greg Clayton44d93782014-01-27 23:43:24 +00003123 break;
3124
3125 case eReturnStatusQuit:
Jim Ingham26c7bf92014-10-11 00:38:27 +00003126 m_quit_requested = true;
Greg Clayton44d93782014-01-27 23:43:24 +00003127 io_handler.SetIsDone(true);
3128 break;
3129 }
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003130
3131 // Finally, if we're going to stop on crash, check that here:
3132 if (!m_quit_requested
3133 && result.GetDidChangeProcessState()
3134 && io_handler.GetFlags().Test(eHandleCommandFlagStopOnCrash))
3135 {
3136 bool should_stop = false;
3137 TargetSP target_sp (m_debugger.GetTargetList().GetSelectedTarget());
3138 if (target_sp)
3139 {
3140 ProcessSP process_sp (target_sp->GetProcessSP());
3141 if (process_sp)
3142 {
3143 for (ThreadSP thread_sp : process_sp->GetThreadList().Threads())
3144 {
3145 StopReason reason = thread_sp->GetStopReason();
3146 if (reason == eStopReasonSignal || reason == eStopReasonException || reason == eStopReasonInstrumentation)
3147 {
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003148 should_stop = true;
3149 break;
3150 }
3151 }
3152 }
3153 }
3154 if (should_stop)
3155 {
3156 io_handler.SetIsDone(true);
3157 m_stopped_for_crash = true;
3158 }
3159 }
Greg Clayton44d93782014-01-27 23:43:24 +00003160}
3161
Greg Claytonf0066ad2014-05-02 00:45:31 +00003162bool
3163CommandInterpreter::IOHandlerInterrupt (IOHandler &io_handler)
3164{
3165 ExecutionContext exe_ctx (GetExecutionContext());
3166 Process *process = exe_ctx.GetProcessPtr();
3167
3168 if (process)
3169 {
3170 StateType state = process->GetState();
3171 if (StateIsRunningState(state))
3172 {
3173 process->Halt();
3174 return true; // Don't do any updating when we are running
3175 }
3176 }
3177 return false;
3178}
3179
Greg Clayton44d93782014-01-27 23:43:24 +00003180void
3181CommandInterpreter::GetLLDBCommandsFromIOHandler (const char *prompt,
3182 IOHandlerDelegate &delegate,
3183 bool asynchronously,
3184 void *baton)
3185{
3186 Debugger &debugger = GetDebugger();
3187 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00003188 IOHandler::Type::CommandList,
Greg Clayton44d93782014-01-27 23:43:24 +00003189 "lldb", // Name of input reader for history
3190 prompt, // Prompt
Kate Stonee30f11d2014-11-17 19:06:59 +00003191 NULL, // Continuation prompt
Greg Clayton44d93782014-01-27 23:43:24 +00003192 true, // Get multiple lines
Kate Stonee30f11d2014-11-17 19:06:59 +00003193 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00003194 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003195 delegate)); // IOHandlerDelegate
3196
3197 if (io_handler_sp)
3198 {
3199 io_handler_sp->SetUserData (baton);
3200 if (asynchronously)
3201 debugger.PushIOHandler(io_handler_sp);
3202 else
3203 debugger.RunIOHandler(io_handler_sp);
3204 }
3205
3206}
3207
3208
3209void
3210CommandInterpreter::GetPythonCommandsFromIOHandler (const char *prompt,
3211 IOHandlerDelegate &delegate,
3212 bool asynchronously,
3213 void *baton)
3214{
3215 Debugger &debugger = GetDebugger();
3216 IOHandlerSP io_handler_sp (new IOHandlerEditline (debugger,
Kate Stonee30f11d2014-11-17 19:06:59 +00003217 IOHandler::Type::PythonCode,
Greg Clayton44d93782014-01-27 23:43:24 +00003218 "lldb-python", // Name of input reader for history
3219 prompt, // Prompt
Kate Stonee30f11d2014-11-17 19:06:59 +00003220 NULL, // Continuation prompt
Greg Clayton44d93782014-01-27 23:43:24 +00003221 true, // Get multiple lines
Kate Stonee30f11d2014-11-17 19:06:59 +00003222 debugger.GetUseColor(),
Greg Claytonf6913cd2014-03-07 00:53:24 +00003223 0, // Don't show line numbers
Greg Clayton44d93782014-01-27 23:43:24 +00003224 delegate)); // IOHandlerDelegate
3225
3226 if (io_handler_sp)
3227 {
3228 io_handler_sp->SetUserData (baton);
3229 if (asynchronously)
3230 debugger.PushIOHandler(io_handler_sp);
3231 else
3232 debugger.RunIOHandler(io_handler_sp);
3233 }
3234
3235}
3236
3237bool
3238CommandInterpreter::IsActive ()
3239{
3240 return m_debugger.IsTopIOHandler (m_command_io_handler_sp);
3241}
3242
Kate Stonee30f11d2014-11-17 19:06:59 +00003243lldb::IOHandlerSP
3244CommandInterpreter::GetIOHandler(bool force_create, CommandInterpreterRunOptions *options)
3245{
3246 // Always re-create the IOHandlerEditline in case the input
3247 // changed. The old instance might have had a non-interactive
3248 // input and now it does or vice versa.
3249 if (force_create || !m_command_io_handler_sp)
3250 {
3251 // Always re-create the IOHandlerEditline in case the input
3252 // changed. The old instance might have had a non-interactive
3253 // input and now it does or vice versa.
3254 uint32_t flags = 0;
3255
3256 if (options)
3257 {
3258 if (options->m_stop_on_continue == eLazyBoolYes)
3259 flags |= eHandleCommandFlagStopOnContinue;
3260 if (options->m_stop_on_error == eLazyBoolYes)
3261 flags |= eHandleCommandFlagStopOnError;
3262 if (options->m_stop_on_crash == eLazyBoolYes)
3263 flags |= eHandleCommandFlagStopOnCrash;
3264 if (options->m_echo_commands != eLazyBoolNo)
3265 flags |= eHandleCommandFlagEchoCommand;
3266 if (options->m_print_results != eLazyBoolNo)
3267 flags |= eHandleCommandFlagPrintResult;
3268 }
3269 else
3270 {
3271 flags = eHandleCommandFlagEchoCommand | eHandleCommandFlagPrintResult;
3272 }
3273
3274 m_command_io_handler_sp.reset(new IOHandlerEditline (m_debugger,
3275 IOHandler::Type::CommandInterpreter,
3276 m_debugger.GetInputFile(),
3277 m_debugger.GetOutputFile(),
3278 m_debugger.GetErrorFile(),
3279 flags,
3280 "lldb",
3281 m_debugger.GetPrompt(),
3282 NULL, // Continuation prompt
3283 false, // Don't enable multiple line input, just single line commands
3284 m_debugger.GetUseColor(),
3285 0, // Don't show line numbers
3286 *this));
3287 }
3288 return m_command_io_handler_sp;
3289}
3290
Greg Clayton44d93782014-01-27 23:43:24 +00003291void
3292CommandInterpreter::RunCommandInterpreter(bool auto_handle_events,
Jim Ingham26c7bf92014-10-11 00:38:27 +00003293 bool spawn_thread,
3294 CommandInterpreterRunOptions &options)
Greg Clayton44d93782014-01-27 23:43:24 +00003295{
Kate Stonee30f11d2014-11-17 19:06:59 +00003296 // Always re-create the command intepreter when we run it in case
3297 // any file handles have changed.
3298 bool force_create = true;
3299 m_debugger.PushIOHandler(GetIOHandler(force_create, &options));
Jim Inghamffc9f1d2014-10-14 01:20:07 +00003300 m_stopped_for_crash = false;
Greg Clayton380f3d82014-07-31 19:46:19 +00003301
Greg Clayton44d93782014-01-27 23:43:24 +00003302 if (auto_handle_events)
3303 m_debugger.StartEventHandlerThread();
3304
3305 if (spawn_thread)
3306 {
3307 m_debugger.StartIOHandlerThread();
3308 }
3309 else
3310 {
3311 m_debugger.ExecuteIOHanders();
Kate Stonee30f11d2014-11-17 19:06:59 +00003312
Greg Clayton44d93782014-01-27 23:43:24 +00003313 if (auto_handle_events)
3314 m_debugger.StopEventHandlerThread();
3315 }
Kate Stonee30f11d2014-11-17 19:06:59 +00003316
Greg Clayton44d93782014-01-27 23:43:24 +00003317}
3318