blob: 31ecfc5319aaf98dcfe9f83be4f93e9d4043597f [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{
Greg Clayton63094e02010-06-23 01:19:29 +0000126 int num_completions = 0;
Jim Ingham3cbf8482011-12-05 19:24:15 +0000127
128 // Sanity check the arguments that are passed in:
129 // cursor & last_char have to be within the current_line.
130 if (current_line == NULL || cursor == NULL || last_char == NULL)
131 return 0;
132
133 if (cursor < current_line || last_char < current_line)
134 return 0;
135
136 size_t current_line_size = strlen (current_line);
137 if (cursor - current_line > current_line_size || last_char - current_line > current_line_size)
138 return 0;
139
Greg Clayton63094e02010-06-23 01:19:29 +0000140 if (m_opaque_ptr)
141 {
142 lldb_private::StringList lldb_matches;
143 num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point,
144 max_return_elements, lldb_matches);
Chris Lattner24943d22010-06-08 16:52:24 +0000145
Greg Clayton63094e02010-06-23 01:19:29 +0000146 SBStringList temp_list (&lldb_matches);
147 matches.AppendList (temp_list);
148 }
Chris Lattner24943d22010-06-08 16:52:24 +0000149 return num_completions;
150}
151
Jim Ingham03c8ee52011-09-21 01:17:13 +0000152int
153SBCommandInterpreter::HandleCompletion (const char *current_line,
154 uint32_t cursor_pos,
155 int match_start_point,
156 int max_return_elements,
157 lldb::SBStringList &matches)
158{
159 const char *cursor = current_line + cursor_pos;
160 const char *last_char = current_line + strlen (current_line);
161 return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches);
162}
163
Chris Lattner24943d22010-06-08 16:52:24 +0000164bool
165SBCommandInterpreter::HasCommands ()
166{
Greg Clayton63094e02010-06-23 01:19:29 +0000167 if (m_opaque_ptr)
168 return m_opaque_ptr->HasCommands();
169 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000170}
171
172bool
173SBCommandInterpreter::HasAliases ()
174{
Greg Clayton63094e02010-06-23 01:19:29 +0000175 if (m_opaque_ptr)
176 return m_opaque_ptr->HasAliases();
177 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000178}
179
180bool
Chris Lattner24943d22010-06-08 16:52:24 +0000181SBCommandInterpreter::HasAliasOptions ()
182{
Greg Clayton63094e02010-06-23 01:19:29 +0000183 if (m_opaque_ptr)
184 return m_opaque_ptr->HasAliasOptions ();
185 return false;
Chris Lattner24943d22010-06-08 16:52:24 +0000186}
187
Chris Lattner24943d22010-06-08 16:52:24 +0000188SBProcess
189SBCommandInterpreter::GetProcess ()
190{
Greg Clayton334d33a2012-01-30 07:41:31 +0000191 SBProcess sb_process;
192 ProcessSP process_sp;
Greg Clayton63094e02010-06-23 01:19:29 +0000193 if (m_opaque_ptr)
Chris Lattner24943d22010-06-08 16:52:24 +0000194 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000195 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
196 if (target_sp)
197 {
198 Mutex::Locker api_locker(target_sp->GetAPIMutex());
Greg Clayton334d33a2012-01-30 07:41:31 +0000199 process_sp = target_sp->GetProcessSP();
200 sb_process.SetSP(process_sp);
Greg Claytonbdcda462010-12-20 20:49:23 +0000201 }
Chris Lattner24943d22010-06-08 16:52:24 +0000202 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000203 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000204
205 if (log)
206 log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)",
Greg Clayton334d33a2012-01-30 07:41:31 +0000207 m_opaque_ptr, process_sp.get());
Greg Claytona66ba462010-10-30 04:51:46 +0000208
209
Greg Clayton334d33a2012-01-30 07:41:31 +0000210 return sb_process;
Chris Lattner24943d22010-06-08 16:52:24 +0000211}
212
Chris Lattner24943d22010-06-08 16:52:24 +0000213CommandInterpreter *
Greg Clayton63094e02010-06-23 01:19:29 +0000214SBCommandInterpreter::get ()
Chris Lattner24943d22010-06-08 16:52:24 +0000215{
Greg Clayton63094e02010-06-23 01:19:29 +0000216 return m_opaque_ptr;
Chris Lattner24943d22010-06-08 16:52:24 +0000217}
218
219CommandInterpreter &
Greg Clayton63094e02010-06-23 01:19:29 +0000220SBCommandInterpreter::ref ()
Chris Lattner24943d22010-06-08 16:52:24 +0000221{
Greg Clayton63094e02010-06-23 01:19:29 +0000222 assert (m_opaque_ptr);
223 return *m_opaque_ptr;
224}
225
226void
227SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter)
228{
229 m_opaque_ptr = interpreter;
Chris Lattner24943d22010-06-08 16:52:24 +0000230}
231
232void
233SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result)
234{
235 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000236 if (m_opaque_ptr)
237 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000238 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
239 Mutex::Locker api_locker;
240 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +0000241 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000242 m_opaque_ptr->SourceInitFile (false, result.ref());
243 }
244 else
245 {
246 result->AppendError ("SBCommandInterpreter is not valid");
247 result->SetStatus (eReturnStatusFailed);
248 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000249 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000250
251 if (log)
252 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))",
253 m_opaque_ptr, result.get());
254
Chris Lattner24943d22010-06-08 16:52:24 +0000255}
256
257void
258SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result)
259{
260 result.Clear();
Greg Clayton63094e02010-06-23 01:19:29 +0000261 if (m_opaque_ptr)
262 {
Greg Claytonbdcda462010-12-20 20:49:23 +0000263 TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
264 Mutex::Locker api_locker;
265 if (target_sp)
Jim Ingham1b584eb2012-05-04 23:02:50 +0000266 api_locker.Lock(target_sp->GetAPIMutex());
Greg Clayton63094e02010-06-23 01:19:29 +0000267 m_opaque_ptr->SourceInitFile (true, result.ref());
268 }
269 else
270 {
271 result->AppendError ("SBCommandInterpreter is not valid");
272 result->SetStatus (eReturnStatusFailed);
273 }
Greg Claytone005f2c2010-11-06 01:53:30 +0000274 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Greg Claytona66ba462010-10-30 04:51:46 +0000275
276 if (log)
277 log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))",
278 m_opaque_ptr, result.get());
Chris Lattner24943d22010-06-08 16:52:24 +0000279}
280
281SBBroadcaster
282SBCommandInterpreter::GetBroadcaster ()
283{
Greg Claytone005f2c2010-11-06 01:53:30 +0000284 LogSP log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API));
Caroline Tice7826c882010-10-26 03:11:13 +0000285
Greg Clayton63094e02010-06-23 01:19:29 +0000286 SBBroadcaster broadcaster (m_opaque_ptr, false);
Caroline Tice7826c882010-10-26 03:11:13 +0000287
288 if (log)
Greg Claytona66ba462010-10-30 04:51:46 +0000289 log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)",
Caroline Tice61ba7ec2010-10-26 23:49:36 +0000290 m_opaque_ptr, broadcaster.get());
Caroline Tice7826c882010-10-26 03:11:13 +0000291
Chris Lattner24943d22010-06-08 16:52:24 +0000292 return broadcaster;
293}
294
Jim Ingham5a15e692012-02-16 06:50:00 +0000295const char *
296SBCommandInterpreter::GetBroadcasterClass ()
297{
298 return Communication::GetStaticBroadcasterClass().AsCString();
299}
300
Greg Claytonaa378b12011-02-20 02:15:07 +0000301const char *
302SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
303{
304 return CommandObject::GetArgumentTypeAsCString (arg_type);
305}
306
307const char *
308SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
309{
310 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
311}
312
Greg Claytonf1252502012-02-29 04:21:24 +0000313bool
314SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name,
315 lldb::CommandOverrideCallback callback,
316 void *baton)
317{
318 if (command_name && command_name[0] && m_opaque_ptr)
319 {
Greg Clayton5f2b9262012-05-08 04:29:20 +0000320 std::string command_name_str (command_name);
321 CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str);
Greg Claytonf1252502012-02-29 04:21:24 +0000322 if (cmd_obj)
323 {
Greg Clayton5f2b9262012-05-08 04:29:20 +0000324 assert(command_name_str.empty());
Greg Claytonf1252502012-02-29 04:21:24 +0000325 cmd_obj->SetOverrideCallback (callback, baton);
326 return true;
327 }
328 }
329 return false;
330}
Greg Claytonaa378b12011-02-20 02:15:07 +0000331
Greg Clayton3e4238d2011-11-04 03:34:56 +0000332#ifndef LLDB_DISABLE_PYTHON
Enrico Granatac2a28252011-08-16 16:49:25 +0000333
Greg Clayton3e4238d2011-11-04 03:34:56 +0000334// Defined in the SWIG source file
335extern "C" void
336init_lldb(void);
337
Greg Clayton3e4238d2011-11-04 03:34:56 +0000338#else
Enrico Granatac2a28252011-08-16 16:49:25 +0000339
Greg Claytone86cbb92011-03-22 01:14:58 +0000340extern "C" void init_lldb(void);
341
Greg Clayton3e4238d2011-11-04 03:34:56 +0000342// Usually defined in the SWIG source file, but we have sripting disabled
343extern "C" void
344init_lldb(void)
345{
346}
347
348#endif
349
Greg Claytone86cbb92011-03-22 01:14:58 +0000350void
351SBCommandInterpreter::InitializeSWIG ()
352{
353 static bool g_initialized = false;
354 if (!g_initialized)
355 {
356 g_initialized = true;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000357#ifndef LLDB_DISABLE_PYTHON
Enrico Granata1328b142012-02-29 03:28:49 +0000358 ScriptInterpreter::InitializeInterpreter (init_lldb);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000359#endif
Greg Claytone86cbb92011-03-22 01:14:58 +0000360 }
361}
Greg Claytonf1252502012-02-29 04:21:24 +0000362