blob: e60547890900d3bfc6a1924c26420de371e4ac98 [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)
96 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
Greg Clayton63094e02010-06-23 01:19:29 +000097 m_opaque_ptr->HandleCommand (command_line, add_to_history, result.ref());
98 }
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)
241 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
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)
266 api_locker.Reset(target_sp->GetAPIMutex().GetMutex());
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
Greg Claytonaa378b12011-02-20 02:15:07 +0000295const char *
296SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type)
297{
298 return CommandObject::GetArgumentTypeAsCString (arg_type);
299}
300
301const char *
302SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type)
303{
304 return CommandObject::GetArgumentDescriptionAsCString (arg_type);
305}
306
307
Greg Clayton3e4238d2011-11-04 03:34:56 +0000308#ifndef LLDB_DISABLE_PYTHON
Greg Claytone86cbb92011-03-22 01:14:58 +0000309extern "C" bool
310LLDBSwigPythonBreakpointCallbackFunction
311(
312 const char *python_function_name,
313 const char *session_dictionary_name,
314 const lldb::StackFrameSP& sb_frame,
315 const lldb::BreakpointLocationSP& sb_bp_loc
316);
Greg Claytonaa378b12011-02-20 02:15:07 +0000317
Enrico Granataf7a9b142011-07-15 02:26:42 +0000318extern "C" std::string
319LLDBSwigPythonCallTypeScript
320(
321 const char *python_function_name,
322 const char *session_dictionary_name,
323 const lldb::ValueObjectSP& valobj_sp
324);
325
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000326extern "C" void*
327LLDBSwigPythonCreateSyntheticProvider
328(
329 const std::string python_class_name,
330 const char *session_dictionary_name,
331 const lldb::ValueObjectSP& valobj_sp
332);
333
334
Enrico Granata979e20d2011-07-29 19:53:35 +0000335extern "C" uint32_t LLDBSwigPython_CalculateNumChildren (void *implementor);
336extern "C" void* LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx);
337extern "C" int LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name);
Enrico Granata91544802011-09-06 19:20:51 +0000338extern "C" void* LLDBSWIGPython_CastPyObjectToSBValue (void* data);
Enrico Granata979e20d2011-07-29 19:53:35 +0000339extern "C" void LLDBSwigPython_UpdateSynthProviderInstance (void* implementor);
Enrico Granataf7a9b142011-07-15 02:26:42 +0000340
Enrico Granatac2a28252011-08-16 16:49:25 +0000341extern "C" bool LLDBSwigPythonCallCommand
342(
343 const char *python_function_name,
344 const char *session_dictionary_name,
345 lldb::DebuggerSP& debugger,
346 const char* args,
347 std::string& err_msg,
Enrico Granata3370f0c2011-08-19 23:56:34 +0000348 lldb_private::CommandReturnObject& cmd_retobj
Enrico Granatac2a28252011-08-16 16:49:25 +0000349);
350
Greg Clayton3e4238d2011-11-04 03:34:56 +0000351// Defined in the SWIG source file
352extern "C" void
353init_lldb(void);
354
Enrico Granata59df36f2011-10-17 21:45:27 +0000355extern "C" bool LLDBSwigPythonCallModuleInit
356(
357 const std::string python_module_name,
358 const char *session_dictionary_name,
359 lldb::DebuggerSP& debugger
360);
361
Greg Clayton3e4238d2011-11-04 03:34:56 +0000362#else
Enrico Granatac2a28252011-08-16 16:49:25 +0000363
Greg Claytone86cbb92011-03-22 01:14:58 +0000364extern "C" void init_lldb(void);
365
Greg Clayton3e4238d2011-11-04 03:34:56 +0000366// Usually defined in the SWIG source file, but we have sripting disabled
367extern "C" void
368init_lldb(void)
369{
370}
371
372#endif
373
Greg Claytone86cbb92011-03-22 01:14:58 +0000374void
375SBCommandInterpreter::InitializeSWIG ()
376{
377 static bool g_initialized = false;
378 if (!g_initialized)
379 {
380 g_initialized = true;
Greg Clayton3e4238d2011-11-04 03:34:56 +0000381#ifndef LLDB_DISABLE_PYTHON
Greg Claytone86cbb92011-03-22 01:14:58 +0000382 ScriptInterpreter::InitializeInterpreter (init_lldb,
Enrico Granataf7a9b142011-07-15 02:26:42 +0000383 LLDBSwigPythonBreakpointCallbackFunction,
Enrico Granata9ae7cef2011-07-24 00:14:56 +0000384 LLDBSwigPythonCallTypeScript,
385 LLDBSwigPythonCreateSyntheticProvider,
386 LLDBSwigPython_CalculateNumChildren,
387 LLDBSwigPython_GetChildAtIndex,
388 LLDBSwigPython_GetIndexOfChildWithName,
Enrico Granata979e20d2011-07-29 19:53:35 +0000389 LLDBSWIGPython_CastPyObjectToSBValue,
Enrico Granatac2a28252011-08-16 16:49:25 +0000390 LLDBSwigPython_UpdateSynthProviderInstance,
Enrico Granata59df36f2011-10-17 21:45:27 +0000391 LLDBSwigPythonCallCommand,
392 LLDBSwigPythonCallModuleInit);
Greg Clayton3e4238d2011-11-04 03:34:56 +0000393#endif
Greg Claytone86cbb92011-03-22 01:14:58 +0000394 }
395}