Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #include "lldb/lldb-types.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 13 | #include "lldb/Core/SourceManager.h" |
| 14 | #include "lldb/Core/Listener.h" |
| 15 | #include "lldb/Interpreter/CommandInterpreter.h" |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 16 | #include "lldb/Interpreter/CommandObjectMultiword.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 17 | #include "lldb/Interpreter/CommandReturnObject.h" |
| 18 | #include "lldb/Target/Target.h" |
| 19 | |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 20 | #include "lldb/API/SBBroadcaster.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 21 | #include "lldb/API/SBCommandReturnObject.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 22 | #include "lldb/API/SBCommandInterpreter.h" |
| 23 | #include "lldb/API/SBProcess.h" |
| 24 | #include "lldb/API/SBTarget.h" |
| 25 | #include "lldb/API/SBListener.h" |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 26 | #include "lldb/API/SBStream.h" |
Eli Friedman | ca93cc1 | 2010-06-09 07:37:52 +0000 | [diff] [blame] | 27 | #include "lldb/API/SBStringList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace lldb; |
| 30 | using namespace lldb_private; |
| 31 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 32 | class CommandPluginInterfaceImplementation : public CommandObjectParsed |
| 33 | { |
| 34 | public: |
| 35 | CommandPluginInterfaceImplementation (CommandInterpreter &interpreter, |
| 36 | const char *name, |
| 37 | lldb::SBCommandPluginInterface* backend, |
| 38 | const char *help = NULL, |
| 39 | const char *syntax = NULL, |
| 40 | uint32_t flags = 0) : |
| 41 | CommandObjectParsed (interpreter, name, help, syntax, flags), |
| 42 | m_backend(backend) {} |
| 43 | |
| 44 | virtual bool |
Greg Clayton | 3a18e31 | 2012-10-08 22:41:53 +0000 | [diff] [blame] | 45 | IsRemovable() const { return true; } |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 46 | |
| 47 | protected: |
| 48 | virtual bool |
| 49 | DoExecute (Args& command, CommandReturnObject &result) |
| 50 | { |
| 51 | SBCommandReturnObject sb_return(&result); |
| 52 | SBCommandInterpreter sb_interpreter(&m_interpreter); |
| 53 | SBDebugger debugger_sb(m_interpreter.GetDebugger().shared_from_this()); |
| 54 | bool ret = m_backend->DoExecute (debugger_sb,(char**)command.GetArgumentVector(), sb_return); |
| 55 | sb_return.Release(); |
| 56 | return ret; |
| 57 | } |
| 58 | lldb::SBCommandPluginInterface* m_backend; |
| 59 | }; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 60 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 61 | SBCommandInterpreter::SBCommandInterpreter (CommandInterpreter *interpreter) : |
| 62 | m_opaque_ptr (interpreter) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 63 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 64 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 65 | |
| 66 | if (log) |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 67 | log->Printf ("SBCommandInterpreter::SBCommandInterpreter (interpreter=%p)" |
| 68 | " => SBCommandInterpreter(%p)", interpreter, m_opaque_ptr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Greg Clayton | efabb12 | 2010-11-05 23:17:00 +0000 | [diff] [blame] | 71 | SBCommandInterpreter::SBCommandInterpreter(const SBCommandInterpreter &rhs) : |
| 72 | m_opaque_ptr (rhs.m_opaque_ptr) |
| 73 | { |
| 74 | } |
| 75 | |
| 76 | const SBCommandInterpreter & |
| 77 | SBCommandInterpreter::operator = (const SBCommandInterpreter &rhs) |
| 78 | { |
| 79 | m_opaque_ptr = rhs.m_opaque_ptr; |
| 80 | return *this; |
| 81 | } |
| 82 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 83 | SBCommandInterpreter::~SBCommandInterpreter () |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | bool |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 88 | SBCommandInterpreter::IsValid() const |
| 89 | { |
| 90 | return m_opaque_ptr != NULL; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | SBCommandInterpreter::CommandExists (const char *cmd) |
| 96 | { |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 97 | if (cmd && m_opaque_ptr) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 98 | return m_opaque_ptr->CommandExists (cmd); |
| 99 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool |
| 103 | SBCommandInterpreter::AliasExists (const char *cmd) |
| 104 | { |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 105 | if (cmd && m_opaque_ptr) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 106 | return m_opaque_ptr->AliasExists (cmd); |
| 107 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 110 | bool |
| 111 | SBCommandInterpreter::IsActive () |
| 112 | { |
| 113 | if (m_opaque_ptr) |
| 114 | return m_opaque_ptr->IsActive (); |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | const char * |
| 119 | SBCommandInterpreter::GetIOHandlerControlSequence(char ch) |
| 120 | { |
| 121 | if (m_opaque_ptr) |
| 122 | return m_opaque_ptr->GetDebugger().GetTopIOHandlerControlSequence (ch).GetCString(); |
| 123 | return NULL; |
| 124 | } |
| 125 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | lldb::ReturnStatus |
| 127 | SBCommandInterpreter::HandleCommand (const char *command_line, SBCommandReturnObject &result, bool add_to_history) |
| 128 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 129 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 130 | |
| 131 | if (log) |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 132 | log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p), add_to_history=%i)", |
| 133 | m_opaque_ptr, command_line, result.get(), add_to_history); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | result.Clear(); |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 136 | if (command_line && m_opaque_ptr) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 137 | { |
Enrico Granata | 5f5ab60 | 2012-05-31 01:09:06 +0000 | [diff] [blame] | 138 | m_opaque_ptr->HandleCommand (command_line, add_to_history ? eLazyBoolYes : eLazyBoolNo, result.ref()); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 139 | } |
| 140 | else |
| 141 | { |
Johnny Chen | 872e062 | 2011-12-19 21:16:29 +0000 | [diff] [blame] | 142 | result->AppendError ("SBCommandInterpreter or the command line is not valid"); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 143 | result->SetStatus (eReturnStatusFailed); |
| 144 | } |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 145 | |
Caroline Tice | 7b9da4a | 2010-10-27 21:23:37 +0000 | [diff] [blame] | 146 | // We need to get the value again, in case the command disabled the log! |
| 147 | log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 148 | if (log) |
| 149 | { |
| 150 | SBStream sstr; |
| 151 | result.GetDescription (sstr); |
Greg Clayton | cfd1ace | 2010-10-31 03:01:06 +0000 | [diff] [blame] | 152 | log->Printf ("SBCommandInterpreter(%p)::HandleCommand (command=\"%s\", SBCommandReturnObject(%p): %s, add_to_history=%i) => %i", |
| 153 | m_opaque_ptr, command_line, result.get(), sstr.GetData(), add_to_history, result.GetStatus()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 156 | return result.GetStatus(); |
| 157 | } |
| 158 | |
| 159 | int |
| 160 | SBCommandInterpreter::HandleCompletion (const char *current_line, |
| 161 | const char *cursor, |
| 162 | const char *last_char, |
| 163 | int match_start_point, |
| 164 | int max_return_elements, |
| 165 | SBStringList &matches) |
| 166 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 167 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 168 | int num_completions = 0; |
Jim Ingham | ac01260 | 2011-12-05 19:24:15 +0000 | [diff] [blame] | 169 | |
| 170 | // Sanity check the arguments that are passed in: |
| 171 | // cursor & last_char have to be within the current_line. |
| 172 | if (current_line == NULL || cursor == NULL || last_char == NULL) |
| 173 | return 0; |
| 174 | |
| 175 | if (cursor < current_line || last_char < current_line) |
| 176 | return 0; |
| 177 | |
| 178 | size_t current_line_size = strlen (current_line); |
Saleem Abdulrasool | 3985c8c | 2014-04-02 03:51:35 +0000 | [diff] [blame^] | 179 | if (cursor - current_line > static_cast<ptrdiff_t>(current_line_size) || |
| 180 | last_char - current_line > static_cast<ptrdiff_t>(current_line_size)) |
Jim Ingham | ac01260 | 2011-12-05 19:24:15 +0000 | [diff] [blame] | 181 | return 0; |
| 182 | |
Jim Ingham | 389512d | 2012-06-26 01:21:59 +0000 | [diff] [blame] | 183 | if (log) |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 184 | log->Printf ("SBCommandInterpreter(%p)::HandleCompletion (current_line=\"%s\", cursor at: %" PRId64 ", last char at: %" PRId64 ", match_start_point: %d, max_return_elements: %d)", |
Jason Molenda | 24a8378 | 2012-07-17 01:57:24 +0000 | [diff] [blame] | 185 | m_opaque_ptr, current_line, (uint64_t) (cursor - current_line), (uint64_t) (last_char - current_line), match_start_point, max_return_elements); |
Jim Ingham | 389512d | 2012-06-26 01:21:59 +0000 | [diff] [blame] | 186 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 187 | if (m_opaque_ptr) |
| 188 | { |
| 189 | lldb_private::StringList lldb_matches; |
| 190 | num_completions = m_opaque_ptr->HandleCompletion (current_line, cursor, last_char, match_start_point, |
| 191 | max_return_elements, lldb_matches); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 192 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 193 | SBStringList temp_list (&lldb_matches); |
| 194 | matches.AppendList (temp_list); |
| 195 | } |
Jim Ingham | 389512d | 2012-06-26 01:21:59 +0000 | [diff] [blame] | 196 | if (log) |
| 197 | log->Printf ("SBCommandInterpreter(%p)::HandleCompletion - Found %d completions.", m_opaque_ptr, num_completions); |
| 198 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | return num_completions; |
| 200 | } |
| 201 | |
Jim Ingham | 969795f | 2011-09-21 01:17:13 +0000 | [diff] [blame] | 202 | int |
| 203 | SBCommandInterpreter::HandleCompletion (const char *current_line, |
| 204 | uint32_t cursor_pos, |
| 205 | int match_start_point, |
| 206 | int max_return_elements, |
| 207 | lldb::SBStringList &matches) |
| 208 | { |
| 209 | const char *cursor = current_line + cursor_pos; |
| 210 | const char *last_char = current_line + strlen (current_line); |
| 211 | return HandleCompletion (current_line, cursor, last_char, match_start_point, max_return_elements, matches); |
| 212 | } |
| 213 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 214 | bool |
| 215 | SBCommandInterpreter::HasCommands () |
| 216 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 217 | if (m_opaque_ptr) |
| 218 | return m_opaque_ptr->HasCommands(); |
| 219 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool |
| 223 | SBCommandInterpreter::HasAliases () |
| 224 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 225 | if (m_opaque_ptr) |
| 226 | return m_opaque_ptr->HasAliases(); |
| 227 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | bool |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | SBCommandInterpreter::HasAliasOptions () |
| 232 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 233 | if (m_opaque_ptr) |
| 234 | return m_opaque_ptr->HasAliasOptions (); |
| 235 | return false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 238 | SBProcess |
| 239 | SBCommandInterpreter::GetProcess () |
| 240 | { |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 241 | SBProcess sb_process; |
| 242 | ProcessSP process_sp; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 243 | if (m_opaque_ptr) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 245 | TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); |
| 246 | if (target_sp) |
| 247 | { |
| 248 | Mutex::Locker api_locker(target_sp->GetAPIMutex()); |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 249 | process_sp = target_sp->GetProcessSP(); |
| 250 | sb_process.SetSP(process_sp); |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 251 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | } |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 253 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 254 | |
| 255 | if (log) |
| 256 | log->Printf ("SBCommandInterpreter(%p)::GetProcess () => SBProcess(%p)", |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 257 | m_opaque_ptr, process_sp.get()); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 258 | |
| 259 | |
Greg Clayton | b9556ac | 2012-01-30 07:41:31 +0000 | [diff] [blame] | 260 | return sb_process; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 263 | SBDebugger |
| 264 | SBCommandInterpreter::GetDebugger () |
| 265 | { |
| 266 | SBDebugger sb_debugger; |
| 267 | if (m_opaque_ptr) |
| 268 | sb_debugger.reset(m_opaque_ptr->GetDebugger().shared_from_this()); |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 269 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 270 | |
| 271 | if (log) |
| 272 | log->Printf ("SBCommandInterpreter(%p)::GetDebugger () => SBDebugger(%p)", |
| 273 | m_opaque_ptr, sb_debugger.get()); |
| 274 | |
| 275 | |
| 276 | return sb_debugger; |
| 277 | } |
| 278 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 279 | CommandInterpreter * |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 280 | SBCommandInterpreter::get () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 281 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 282 | return m_opaque_ptr; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | CommandInterpreter & |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 286 | SBCommandInterpreter::ref () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 288 | assert (m_opaque_ptr); |
| 289 | return *m_opaque_ptr; |
| 290 | } |
| 291 | |
| 292 | void |
| 293 | SBCommandInterpreter::reset (lldb_private::CommandInterpreter *interpreter) |
| 294 | { |
| 295 | m_opaque_ptr = interpreter; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void |
| 299 | SBCommandInterpreter::SourceInitFileInHomeDirectory (SBCommandReturnObject &result) |
| 300 | { |
| 301 | result.Clear(); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 302 | if (m_opaque_ptr) |
| 303 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 304 | TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); |
| 305 | Mutex::Locker api_locker; |
| 306 | if (target_sp) |
Jim Ingham | 10ebffa | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 307 | api_locker.Lock(target_sp->GetAPIMutex()); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 308 | m_opaque_ptr->SourceInitFile (false, result.ref()); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | result->AppendError ("SBCommandInterpreter is not valid"); |
| 313 | result->SetStatus (eReturnStatusFailed); |
| 314 | } |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 315 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 316 | |
| 317 | if (log) |
| 318 | log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInHomeDirectory (&SBCommandReturnObject(%p))", |
| 319 | m_opaque_ptr, result.get()); |
| 320 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | void |
| 324 | SBCommandInterpreter::SourceInitFileInCurrentWorkingDirectory (SBCommandReturnObject &result) |
| 325 | { |
| 326 | result.Clear(); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 327 | if (m_opaque_ptr) |
| 328 | { |
Greg Clayton | af67cec | 2010-12-20 20:49:23 +0000 | [diff] [blame] | 329 | TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); |
| 330 | Mutex::Locker api_locker; |
| 331 | if (target_sp) |
Jim Ingham | 10ebffa | 2012-05-04 23:02:50 +0000 | [diff] [blame] | 332 | api_locker.Lock(target_sp->GetAPIMutex()); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 333 | m_opaque_ptr->SourceInitFile (true, result.ref()); |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | result->AppendError ("SBCommandInterpreter is not valid"); |
| 338 | result->SetStatus (eReturnStatusFailed); |
| 339 | } |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 340 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 341 | |
| 342 | if (log) |
| 343 | log->Printf ("SBCommandInterpreter(%p)::SourceInitFileInCurrentWorkingDirectory (&SBCommandReturnObject(%p))", |
| 344 | m_opaque_ptr, result.get()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | SBBroadcaster |
| 348 | SBCommandInterpreter::GetBroadcaster () |
| 349 | { |
Greg Clayton | 5160ce5 | 2013-03-27 23:08:40 +0000 | [diff] [blame] | 350 | Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_API)); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 351 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 352 | SBBroadcaster broadcaster (m_opaque_ptr, false); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 353 | |
| 354 | if (log) |
Greg Clayton | 4838131 | 2010-10-30 04:51:46 +0000 | [diff] [blame] | 355 | log->Printf ("SBCommandInterpreter(%p)::GetBroadcaster() => SBBroadcaster(%p)", |
Caroline Tice | 750cd17 | 2010-10-26 23:49:36 +0000 | [diff] [blame] | 356 | m_opaque_ptr, broadcaster.get()); |
Caroline Tice | ceb6b13 | 2010-10-26 03:11:13 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 358 | return broadcaster; |
| 359 | } |
| 360 | |
Jim Ingham | 4bddaeb | 2012-02-16 06:50:00 +0000 | [diff] [blame] | 361 | const char * |
| 362 | SBCommandInterpreter::GetBroadcasterClass () |
| 363 | { |
| 364 | return Communication::GetStaticBroadcasterClass().AsCString(); |
| 365 | } |
| 366 | |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 367 | const char * |
| 368 | SBCommandInterpreter::GetArgumentTypeAsCString (const lldb::CommandArgumentType arg_type) |
| 369 | { |
| 370 | return CommandObject::GetArgumentTypeAsCString (arg_type); |
| 371 | } |
| 372 | |
| 373 | const char * |
| 374 | SBCommandInterpreter::GetArgumentDescriptionAsCString (const lldb::CommandArgumentType arg_type) |
| 375 | { |
| 376 | return CommandObject::GetArgumentDescriptionAsCString (arg_type); |
| 377 | } |
| 378 | |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 379 | bool |
| 380 | SBCommandInterpreter::SetCommandOverrideCallback (const char *command_name, |
| 381 | lldb::CommandOverrideCallback callback, |
| 382 | void *baton) |
| 383 | { |
| 384 | if (command_name && command_name[0] && m_opaque_ptr) |
| 385 | { |
Greg Clayton | af6f275 | 2012-05-08 04:29:20 +0000 | [diff] [blame] | 386 | std::string command_name_str (command_name); |
| 387 | CommandObject *cmd_obj = m_opaque_ptr->GetCommandObjectForCommand(command_name_str); |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 388 | if (cmd_obj) |
| 389 | { |
Greg Clayton | af6f275 | 2012-05-08 04:29:20 +0000 | [diff] [blame] | 390 | assert(command_name_str.empty()); |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 391 | cmd_obj->SetOverrideCallback (callback, baton); |
| 392 | return true; |
| 393 | } |
| 394 | } |
| 395 | return false; |
| 396 | } |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 397 | |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 398 | #ifndef LLDB_DISABLE_PYTHON |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 399 | |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 400 | // Defined in the SWIG source file |
| 401 | extern "C" void |
| 402 | init_lldb(void); |
| 403 | |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 404 | // these are the Pythonic implementations of the required callbacks |
| 405 | // these are scripting-language specific, which is why they belong here |
| 406 | // we still need to use function pointers to them instead of relying |
| 407 | // on linkage-time resolution because the SWIG stuff and this file |
| 408 | // get built at different times |
| 409 | extern "C" bool |
| 410 | LLDBSwigPythonBreakpointCallbackFunction (const char *python_function_name, |
| 411 | const char *session_dictionary_name, |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 412 | const lldb::StackFrameSP& sb_frame, |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 413 | const lldb::BreakpointLocationSP& sb_bp_loc); |
Enrico Granata | be93a35 | 2011-08-16 16:49:25 +0000 | [diff] [blame] | 414 | |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 415 | extern "C" bool |
| 416 | LLDBSwigPythonWatchpointCallbackFunction (const char *python_function_name, |
| 417 | const char *session_dictionary_name, |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 418 | const lldb::StackFrameSP& sb_frame, |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 419 | const lldb::WatchpointSP& sb_wp); |
Greg Clayton | fc36f791 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 420 | |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 421 | extern "C" bool |
| 422 | LLDBSwigPythonCallTypeScript (const char *python_function_name, |
| 423 | void *session_dictionary, |
| 424 | const lldb::ValueObjectSP& valobj_sp, |
| 425 | void** pyfunct_wrapper, |
| 426 | std::string& retval); |
| 427 | |
| 428 | extern "C" void* |
| 429 | LLDBSwigPythonCreateSyntheticProvider (const char *python_class_name, |
| 430 | const char *session_dictionary_name, |
| 431 | const lldb::ValueObjectSP& valobj_sp); |
| 432 | |
| 433 | |
| 434 | extern "C" uint32_t |
| 435 | LLDBSwigPython_CalculateNumChildren (void *implementor); |
| 436 | |
| 437 | extern "C" void * |
| 438 | LLDBSwigPython_GetChildAtIndex (void *implementor, uint32_t idx); |
| 439 | |
| 440 | extern "C" int |
| 441 | LLDBSwigPython_GetIndexOfChildWithName (void *implementor, const char* child_name); |
| 442 | |
| 443 | extern "C" void * |
| 444 | LLDBSWIGPython_CastPyObjectToSBValue (void* data); |
| 445 | |
| 446 | extern lldb::ValueObjectSP |
| 447 | LLDBSWIGPython_GetValueObjectSPFromSBValue (void* data); |
| 448 | |
| 449 | extern "C" bool |
| 450 | LLDBSwigPython_UpdateSynthProviderInstance (void* implementor); |
| 451 | |
| 452 | extern "C" bool |
| 453 | LLDBSwigPython_MightHaveChildrenSynthProviderInstance (void* implementor); |
| 454 | |
| 455 | extern "C" bool |
| 456 | LLDBSwigPythonCallCommand (const char *python_function_name, |
| 457 | const char *session_dictionary_name, |
| 458 | lldb::DebuggerSP& debugger, |
| 459 | const char* args, |
| 460 | lldb_private::CommandReturnObject &cmd_retobj); |
| 461 | |
| 462 | extern "C" bool |
| 463 | LLDBSwigPythonCallModuleInit (const char *python_module_name, |
| 464 | const char *session_dictionary_name, |
| 465 | lldb::DebuggerSP& debugger); |
| 466 | |
| 467 | extern "C" void* |
| 468 | LLDBSWIGPythonCreateOSPlugin (const char *python_class_name, |
| 469 | const char *session_dictionary_name, |
| 470 | const lldb::ProcessSP& process_sp); |
| 471 | |
| 472 | extern "C" bool |
| 473 | LLDBSWIGPythonRunScriptKeywordProcess (const char* python_function_name, |
| 474 | const char* session_dictionary_name, |
| 475 | lldb::ProcessSP& process, |
| 476 | std::string& output); |
| 477 | |
| 478 | extern "C" bool |
| 479 | LLDBSWIGPythonRunScriptKeywordThread (const char* python_function_name, |
| 480 | const char* session_dictionary_name, |
| 481 | lldb::ThreadSP& thread, |
| 482 | std::string& output); |
| 483 | |
| 484 | extern "C" bool |
| 485 | LLDBSWIGPythonRunScriptKeywordTarget (const char* python_function_name, |
| 486 | const char* session_dictionary_name, |
| 487 | lldb::TargetSP& target, |
| 488 | std::string& output); |
| 489 | |
| 490 | extern "C" bool |
| 491 | LLDBSWIGPythonRunScriptKeywordFrame (const char* python_function_name, |
| 492 | const char* session_dictionary_name, |
Jason Molenda | b57e4a1 | 2013-11-04 09:33:30 +0000 | [diff] [blame] | 493 | lldb::StackFrameSP& frame, |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 494 | std::string& output); |
| 495 | |
| 496 | extern "C" void* |
| 497 | LLDBSWIGPython_GetDynamicSetting (void* module, |
| 498 | const char* setting, |
| 499 | const lldb::TargetSP& target_sp); |
| 500 | |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 501 | |
| 502 | #endif |
| 503 | |
Greg Clayton | fc36f791 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 504 | void |
| 505 | SBCommandInterpreter::InitializeSWIG () |
| 506 | { |
| 507 | static bool g_initialized = false; |
| 508 | if (!g_initialized) |
| 509 | { |
| 510 | g_initialized = true; |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 511 | #ifndef LLDB_DISABLE_PYTHON |
Greg Clayton | 8afa543 | 2013-10-17 00:27:14 +0000 | [diff] [blame] | 512 | ScriptInterpreter::InitializeInterpreter (init_lldb, |
| 513 | LLDBSwigPythonBreakpointCallbackFunction, |
| 514 | LLDBSwigPythonWatchpointCallbackFunction, |
| 515 | LLDBSwigPythonCallTypeScript, |
| 516 | LLDBSwigPythonCreateSyntheticProvider, |
| 517 | LLDBSwigPython_CalculateNumChildren, |
| 518 | LLDBSwigPython_GetChildAtIndex, |
| 519 | LLDBSwigPython_GetIndexOfChildWithName, |
| 520 | LLDBSWIGPython_CastPyObjectToSBValue, |
| 521 | LLDBSWIGPython_GetValueObjectSPFromSBValue, |
| 522 | LLDBSwigPython_UpdateSynthProviderInstance, |
| 523 | LLDBSwigPython_MightHaveChildrenSynthProviderInstance, |
| 524 | LLDBSwigPythonCallCommand, |
| 525 | LLDBSwigPythonCallModuleInit, |
| 526 | LLDBSWIGPythonCreateOSPlugin, |
| 527 | LLDBSWIGPythonRunScriptKeywordProcess, |
| 528 | LLDBSWIGPythonRunScriptKeywordThread, |
| 529 | LLDBSWIGPythonRunScriptKeywordTarget, |
| 530 | LLDBSWIGPythonRunScriptKeywordFrame, |
| 531 | LLDBSWIGPython_GetDynamicSetting); |
Greg Clayton | dce502e | 2011-11-04 03:34:56 +0000 | [diff] [blame] | 532 | #endif |
Greg Clayton | fc36f791 | 2011-03-22 01:14:58 +0000 | [diff] [blame] | 533 | } |
| 534 | } |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 535 | |
Enrico Granata | 21dfcd9 | 2012-09-28 23:57:51 +0000 | [diff] [blame] | 536 | lldb::SBCommand |
| 537 | SBCommandInterpreter::AddMultiwordCommand (const char* name, const char* help) |
| 538 | { |
| 539 | CommandObjectMultiword *new_command = new CommandObjectMultiword(*m_opaque_ptr,name,help); |
| 540 | new_command->SetRemovable (true); |
| 541 | lldb::CommandObjectSP new_command_sp(new_command); |
| 542 | if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true)) |
| 543 | return lldb::SBCommand(new_command_sp); |
| 544 | return lldb::SBCommand(); |
| 545 | } |
| 546 | |
| 547 | lldb::SBCommand |
| 548 | SBCommandInterpreter::AddCommand (const char* name, lldb::SBCommandPluginInterface* impl, const char* help) |
| 549 | { |
| 550 | lldb::CommandObjectSP new_command_sp; |
| 551 | new_command_sp.reset(new CommandPluginInterfaceImplementation(*m_opaque_ptr,name,impl,help)); |
| 552 | |
| 553 | if (new_command_sp && m_opaque_ptr->AddUserCommand(name, new_command_sp, true)) |
| 554 | return lldb::SBCommand(new_command_sp); |
| 555 | return lldb::SBCommand(); |
| 556 | } |
| 557 | |
| 558 | SBCommand::SBCommand () |
| 559 | {} |
| 560 | |
| 561 | SBCommand::SBCommand (lldb::CommandObjectSP cmd_sp) : m_opaque_sp (cmd_sp) |
| 562 | {} |
| 563 | |
| 564 | bool |
| 565 | SBCommand::IsValid () |
| 566 | { |
| 567 | return (bool)m_opaque_sp; |
| 568 | } |
| 569 | |
| 570 | const char* |
| 571 | SBCommand::GetName () |
| 572 | { |
| 573 | if (IsValid ()) |
| 574 | return m_opaque_sp->GetCommandName (); |
| 575 | return NULL; |
| 576 | } |
| 577 | |
| 578 | const char* |
| 579 | SBCommand::GetHelp () |
| 580 | { |
| 581 | if (IsValid ()) |
| 582 | return m_opaque_sp->GetHelp (); |
| 583 | return NULL; |
| 584 | } |
| 585 | |
| 586 | lldb::SBCommand |
| 587 | SBCommand::AddMultiwordCommand (const char* name, const char* help) |
| 588 | { |
| 589 | if (!IsValid ()) |
| 590 | return lldb::SBCommand(); |
| 591 | if (m_opaque_sp->IsMultiwordObject() == false) |
| 592 | return lldb::SBCommand(); |
| 593 | CommandObjectMultiword *new_command = new CommandObjectMultiword(m_opaque_sp->GetCommandInterpreter(),name,help); |
| 594 | new_command->SetRemovable (true); |
| 595 | lldb::CommandObjectSP new_command_sp(new_command); |
| 596 | if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp)) |
| 597 | return lldb::SBCommand(new_command_sp); |
| 598 | return lldb::SBCommand(); |
| 599 | } |
| 600 | |
| 601 | lldb::SBCommand |
| 602 | SBCommand::AddCommand (const char* name, lldb::SBCommandPluginInterface *impl, const char* help) |
| 603 | { |
| 604 | if (!IsValid ()) |
| 605 | return lldb::SBCommand(); |
| 606 | if (m_opaque_sp->IsMultiwordObject() == false) |
| 607 | return lldb::SBCommand(); |
| 608 | lldb::CommandObjectSP new_command_sp; |
| 609 | new_command_sp.reset(new CommandPluginInterfaceImplementation(m_opaque_sp->GetCommandInterpreter(),name,impl,help)); |
| 610 | if (new_command_sp && m_opaque_sp->LoadSubCommand(name,new_command_sp)) |
| 611 | return lldb::SBCommand(new_command_sp); |
| 612 | return lldb::SBCommand(); |
| 613 | } |
| 614 | |