blob: 08e845f7e156d6ab73a456fbcc283c5511adcb0b [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- SBCommandInterpreter.cpp --------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/lldb-types.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000011#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "lldb/Core/SourceManager.h"
13#include "lldb/Core/Listener.h"
14#include "lldb/Interpreter/CommandInterpreter.h"
15#include "lldb/Interpreter/CommandReturnObject.h"
16#include "lldb/Target/Target.h"
17
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000018#include "lldb/API/SBBroadcaster.h"
19#include "lldb/API/SBDebugger.h"
20#include "lldb/API/SBCommandReturnObject.h"
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000021#include "lldb/API/SBCommandInterpreter.h"
22#include "lldb/API/SBProcess.h"
23#include "lldb/API/SBTarget.h"
24#include "lldb/API/SBListener.h"
Caroline Tice7826c882010-10-26 03:11:13 +000025#include "lldb/API/SBStream.h"
Eli Friedmand6ec8aa2010-06-09 07:37:52 +000026#include "lldb/API/SBStringList.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027
28using namespace lldb;
29using namespace lldb_private;
30
31
Greg Clayton63094e02010-06-23 01:19:29 +000032SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) :
33 m_opaque_ptr (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +000034{
Greg Claytone005f2c2010-11-06 01:53:30 +000035 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000036
37 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +000038 log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)"
39 " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr);
Chris Lattner24943d22010-06-08 16:52:24 +000040}
41
Greg Clayton538eb822010-11-05 23:17:00 +000042SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) :
43 m_opaque_ptr (rhs.m_opaque_ptr)
44{
45}
46
47const SBCommandInterpreter &
48SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs)
49{
50 m_opaque_ptr = rhs.m_opaque_ptr;
51 return *this;
52}
53
Chris Lattner24943d22010-06-08 16:52:24 +000054SBCommandInterpreter::~SBCommandInterpreter ()
55{
56}
57
58bool
Greg Clayton63094e02010-06-23 01:19:29 +000059SBCommandInterpreter::IsValid() const
60{
61 return m_opaque_ptr != NULL;
62}
63
64
65bool
Chris Lattner24943d22010-06-08 16:52:24 +000066SBCommandInterpreter::CommandExists (const char *cmd)
67{
Johnny Chenbab8cc92011-12-19 21:16:29 +000068 if (cmd && m_opaque_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000069 return m_opaque_ptr->CommandExists (cmd);
70 return false;
Chris Lattner24943d22010-06-08 16:52:24 +000071}
72
73bool
74SBCommandInterpreter::AliasExists (const char *cmd)
75{
Johnny Chenbab8cc92011-12-19 21:16:29 +000076 if (cmd && m_opaque_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000077 return m_opaque_ptr->AliasExists (cmd);
78 return false;
Chris Lattner24943d22010-06-08 16:52:24 +000079}
80
Chris Lattner24943d22010-06-08 16:52:24 +000081lldb::ReturnStatus
82SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history)
83{
Greg Claytone005f2c2010-11-06 01:53:30 +000084 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +000085
86 if (log)
Greg Clayton49ce6822010-10-31 03:01:06 +000087 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)",
88 m_opaque_ptr, command_line, result.get(), add_to_history);
Caroline Tice7826c882010-10-26 03:11:13 +000089
Chris Lattner24943d22010-06-08 16:52:24 +000090 result.Clear();
Johnny Chenbab8cc92011-12-19 21:16:29 +000091 if (command_line && m_opaque_ptr)
Greg Clayton63094e02010-06-23 01:19:29 +000092 {
Greg Claytonbdcda462010-12-20 20:49:23 +000093 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
94 Mutex::Locker api_locker;
95 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +000096 api_locker.Lock(target_sp->GetAPIMutex());
Enrico Granata01bc2d42012-05-31 01:09:06 +000097 m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref());
Greg Clayton63094e02010-06-23 01:19:29 +000098 }
99 else
100 {
Johnny Chenbab8cc92011-12-19 21:16:29 +0000101 result->AppendError ("SBCommandInterpreter or the command line is not valid");
Greg Clayton63094e02010-06-23 01:19:29 +0000102 result->SetStatus (eReturnStatusFailed);
103 }
Caroline Tice7826c882010-10-26 03:11:13 +0000104
Caroline Tice7de24cc2010-10-27 21:23:37 +0000105 // We need to get the value again, in case the command disabled the log!
106 log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API);
Caroline Tice7826c882010-10-26 03:11:13 +0000107 if (log)
108 {
109 SBStream sstr;
110 result.GetDescription (sstr);
Greg Clayton49ce6822010-10-31 03:01:06 +0000111 log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i",
112 m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus());
Caroline Tice7826c882010-10-26 03:11:13 +0000113 }
114
Chris Lattner24943d22010-06-08 16:52:24 +0000115 return result.GetStatus();
116}
117
118int
119SBCommandInterpreter::HandleCompletion (const char *current_line,
120 const char *cursor,
121 const char *last_char,
122 int match_start_point,
123 int max_return_elements,
124 SBStringList &matches)
125{
Jim Inghambc8c4992012-06-26 01:21:59 +0000126 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Clayton63094e02010-06-23 01:19:29 +0000127 int num_completions = 0;
Jim Ingham3cbf8482011-12-05 19:24:15 +0000128
129 // Sanity check the arguments that are passed in:
130 // cursor & last_char have to be within the current_line.
131 if (current_line == NULL || cursor == NULL || last_char == NULL)
132 return 0;
133
134 if (cursor < current_line || last_char < current_line)
135 return 0;
136
137 size_t current_line_size = strlen (current_line);
138 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size)
139 return 0;
140
Jim Inghambc8c4992012-06-26 01:21:59 +0000141 if (log)
Jason Molendaa092d902012-07-17 01:57:24 +0000142 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %lld, last char at: %lld, match_start_point: %d, max_return_elements: %d)",
143 m_opaque_ptr, current_line, (uint64_t) (cursor - current_line), (uint64_t) (last_char - current_line), match_start_point, max_return_elements);
Jim Inghambc8c4992012-06-26 01:21:59 +0000144
Greg Clayton63094e02010-06-23 01:19:29 +0000145 if (m_opaque_ptr)
146 {
147 lldb_private::StringList lldb_matches;
148 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
149 max_return_elements, lldb_matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000150
Greg Clayton63094e02010-06-23 01:19:29 +0000151 SBStringList temp_list (&lldb_matches);
152 matches.AppendList (temp_list);
153 }
Jim Inghambc8c4992012-06-26 01:21:59 +0000154 if (log)
155 log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", m_opaque_ptr, num_completions);
156
Chris Lattner24943d22010-06-08 16:52:24 +0000157 return num_completions;
158}
159
Jim Ingham03c8ee52011-09-21 01:17:13 +0000160int
161SBCommandInterpreter::HandleCompletion (const char *current_line,
162 uint32_t cursor_pos,
163 int match_start_point,
164 int max_return_elements,
165 lldb::SBStringList &matches)
166{
167 const char *cursor = current_line + cursor_pos;
168 const char *last_char = current_line + strlen (current_line);
169 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
170}
171
Chris Lattner24943d22010-06-08 16:52:24 +0000172bool
173SBCommandInterpreter::HasCommands ()
174{
Greg Clayton63094e02010-06-23 01:19:29 +0000175 if (m_opaque_ptr)
176 return m_opaque_ptr->HasCommands();
177 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000178}
179
180bool
181SBCommandInterpreter::HasAliases ()
182{
Greg Clayton63094e02010-06-23 01:19:29 +0000183 if (m_opaque_ptr)
184 return m_opaque_ptr->HasAliases();
185 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
188bool
Chris Lattner24943d22010-06-08 16:52:24 +0000189SBCommandInterpreter::HasAliasOptions ()
190{
Greg Clayton63094e02010-06-23 01:19:29 +0000191 if (m_opaque_ptr)
192 return m_opaque_ptr->HasAliasOptions ();
193 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000194}
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196SBProcess
197SBCommandInterpreter::GetProcess ()
198{
Greg Clayton334d33a2012-01-30 07:41:31 +0000199 SBProcess sb_process;
200 ProcessSP process_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000201 if (m_opaque_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000202 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000203 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
204 if (target_sp)
205 {
206 Mutex::Locker api_locker(target_sp->GetAPIMutex());
Greg Clayton334d33a2012-01-30 07:41:31 +0000207 process_sp = target_sp->GetProcessSP();
208 sb_process.SetSP(process_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000209 }
Chris Lattner24943d22010-06-08 16:52:24 +0000210 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000211 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000212
213 if (log)
214 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
Greg Clayton334d33a2012-01-30 07:41:31 +0000215 m_opaque_ptr, process_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000216
217
Greg Clayton334d33a2012-01-30 07:41:31 +0000218 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000219}
220
Chris Lattner24943d22010-06-08 16:52:24 +0000221CommandInterpreter *
Greg Clayton63094e02010-06-23 01:19:29 +0000222SBCommandInterpreter::get ()
Chris Lattner24943d22010-06-08 16:52:24 +0000223{
Greg Clayton63094e02010-06-23 01:19:29 +0000224 return m_opaque_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000225}
226
227CommandInterpreter &
Greg Clayton63094e02010-06-23 01:19:29 +0000228SBCommandInterpreter::ref ()
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
Greg Clayton63094e02010-06-23 01:19:29 +0000230 assert (m_opaque_ptr);
231 return *m_opaque_ptr;
232}
233
234void
235SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
236{
237 m_opaque_ptr = interpreter;
Chris Lattner24943d22010-06-08 16:52:24 +0000238}
239
240void
241SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
242{
243 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000244 if (m_opaque_ptr)
245 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000246 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
247 Mutex::Locker api_locker;
248 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +0000249 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000250 m_opaque_ptr->SourceInitFile (false, result.ref());
251 }
252 else
253 {
254 result->AppendError ("SBCommandInterpreter is not valid");
255 result->SetStatus (eReturnStatusFailed);
256 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000257 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000258
259 if (log)
260 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
261 m_opaque_ptr, result.get());
262
Chris Lattner24943d22010-06-08 16:52:24 +0000263}
264
265void
266SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
267{
268 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000269 if (m_opaque_ptr)
270 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000271 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
272 Mutex::Locker api_locker;
273 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +0000274 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000275 m_opaque_ptr->SourceInitFile (true, result.ref());
276 }
277 else
278 {
279 result->AppendError ("SBCommandInterpreter is not valid");
280 result->SetStatus (eReturnStatusFailed);
281 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000282 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000283
284 if (log)
285 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
286 m_opaque_ptr, result.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000287}
288
289SBBroadcaster
290SBCommandInterpreter::GetBroadcaster ()
291{
Greg Claytone005f2c2010-11-06 01:53:30 +0000292 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000293
Greg Clayton63094e02010-06-23 01:19:29 +0000294 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000295
296 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000297 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000298 m_opaque_ptr, broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000299
Chris Lattner24943d22010-06-08 16:52:24 +0000300 return broadcaster;
301}
302
Jim Ingham5a15e692012-02-16 06:50:00 +0000303const char *
304SBCommandInterpreter::GetBroadcasterClass ()
305{
306 return Communication::GetStaticBroadcasterClass().AsCString();
307}
308
Greg Claytonaa378b12011-02-20 02:15:07 +0000309const char *
310SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
311{
312 return CommandObject::GetArgumentTypeAsCString (arg_type);
313}
314
315const char *
316SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
317{
318 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
319}
320
Greg Claytonf1252502012-02-29 04:21:24 +0000321bool
322SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
323 lldb::CommandOverrideCallback callback,
324 void *baton)
325{
326 if (command_name && command_name[0] && m_opaque_ptr)
327 {
Greg Clayton5f2b9262012-05-08 04:29:20 +0000328 std::string command_name_str (command_name);
329 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
Greg Claytonf1252502012-02-29 04:21:24 +0000330 if (cmd_obj)
331 {
Greg Clayton5f2b9262012-05-08 04:29:20 +0000332 assert(command_name_str.empty());
Greg Claytonf1252502012-02-29 04:21:24 +0000333 cmd_obj->SetOverrideCallback (callback, baton);
334 return true;
335 }
336 }
337 return false;
338}
Greg Claytonaa378b12011-02-20 02:15:07 +0000339
Greg Clayton3e4238d2011-11-04 03:34:56 +0000340#ifndef LLDB_DISABLE_PYTHON
Enrico Granatac2a28252011-08-16 16:49:25 +0000341
Greg Clayton3e4238d2011-11-04 03:34:56 +0000342// Defined in the SWIG source file
343extern "C" void
344init_lldb(void);
345
Greg Clayton3e4238d2011-11-04 03:34:56 +0000346#else
Enrico Granatac2a28252011-08-16 16:49:25 +0000347
Greg Claytone86cbb92011-03-22 01:14:58 +0000348extern "C" void init_lldb(void);
349
Greg Clayton3e4238d2011-11-04 03:34:56 +0000350// Usually defined in the SWIG source file, but we have sripting disabled
351extern "C" void
352init_lldb(void)
353{
354}
355
356#endif
357
Greg Claytone86cbb92011-03-22 01:14:58 +0000358void
359SBCommandInterpreter::InitializeSWIG ()
360{
361 static bool g_initialized = false;
362 if (!g_initialized)
363 {
364 g_initialized = true;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000365#ifndef LLDB_DISABLE_PYTHON
Enrico Granata1328b142012-02-29 03:28:49 +0000366 ScriptInterpreter::InitializeInterpreter (init_lldb);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000367#endif
Greg Claytone86cbb92011-03-22 01:14:58 +0000368 }
369}
Greg Claytonf1252502012-02-29 04:21:24 +0000370