Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- IOChannel.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 "IOChannel.h" |
| 11 | |
| 12 | #include <map> |
| 13 | |
Eli Friedman | a382d47 | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 14 | #include "lldb/API/SBCommandInterpreter.h" |
| 15 | #include "lldb/API/SBDebugger.h" |
| 16 | #include "lldb/API/SBError.h" |
| 17 | #include "lldb/API/SBEvent.h" |
| 18 | #include "lldb/API/SBFileSpec.h" |
| 19 | #include "lldb/API/SBHostOS.h" |
| 20 | #include "lldb/API/SBListener.h" |
| 21 | #include "lldb/API/SBStringList.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
Eli Friedman | 07b1627 | 2010-06-09 19:11:30 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | #include <limits.h> |
| 25 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 26 | using namespace lldb; |
| 27 | |
| 28 | typedef std::map<EditLine *, std::string> PromptMap; |
| 29 | const char *g_default_prompt = "(lldb) "; |
| 30 | PromptMap g_prompt_map; |
| 31 | |
Caroline Tice | fe1bdf2 | 2011-05-04 16:44:57 +0000 | [diff] [blame] | 32 | // Printing the following string causes libedit to back up to the beginning of the line & blank it out. |
| 33 | const char undo_prompt_string[4] = { (char) 13, (char) 27, (char) 91, (char) 75}; |
| 34 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 35 | static const char* |
| 36 | el_prompt(EditLine *el) |
| 37 | { |
| 38 | PromptMap::const_iterator pos = g_prompt_map.find (el); |
| 39 | if (pos == g_prompt_map.end()) |
| 40 | return g_default_prompt; |
| 41 | return pos->second.c_str(); |
| 42 | } |
| 43 | |
| 44 | const char * |
| 45 | IOChannel::GetPrompt () |
| 46 | { |
| 47 | PromptMap::const_iterator pos = g_prompt_map.find (m_edit_line); |
| 48 | if (pos == g_prompt_map.end()) |
| 49 | return g_default_prompt; |
| 50 | return pos->second.c_str(); |
| 51 | } |
| 52 | |
| 53 | unsigned char |
| 54 | IOChannel::ElCompletionFn (EditLine *e, int ch) |
| 55 | { |
| 56 | IOChannel *io_channel; |
| 57 | if (el_get(e, EL_CLIENTDATA, &io_channel) == 0) |
| 58 | { |
| 59 | return io_channel->HandleCompletion (e, ch); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | return CC_ERROR; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | unsigned char |
| 68 | IOChannel::HandleCompletion (EditLine *e, int ch) |
| 69 | { |
| 70 | assert (e == m_edit_line); |
| 71 | |
| 72 | const LineInfo *line_info = el_line(m_edit_line); |
| 73 | SBStringList completions; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 74 | int page_size = 40; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 75 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 76 | int num_completions = m_driver->GetDebugger().GetCommandInterpreter().HandleCompletion (line_info->buffer, |
| 77 | line_info->cursor, |
| 78 | line_info->lastchar, |
| 79 | 0, |
| 80 | -1, |
| 81 | completions); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | |
| 83 | if (num_completions == -1) |
| 84 | { |
| 85 | el_insertstr (m_edit_line, m_completion_key); |
| 86 | return CC_REDISPLAY; |
| 87 | } |
Jim Ingham | a5a97eb | 2011-07-12 03:12:18 +0000 | [diff] [blame^] | 88 | else if (num_completions == -2) |
| 89 | { |
| 90 | el_deletestr (m_edit_line, line_info->cursor - line_info->buffer); |
| 91 | el_insertstr (m_edit_line, completions.GetStringAtIndex(0)); |
| 92 | return CC_REDISPLAY; |
| 93 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
| 95 | // If we get a longer match display that first. |
| 96 | const char *completion_str = completions.GetStringAtIndex(0); |
| 97 | if (completion_str != NULL && *completion_str != '\0') |
| 98 | { |
| 99 | el_insertstr (m_edit_line, completion_str); |
| 100 | return CC_REDISPLAY; |
| 101 | } |
| 102 | |
| 103 | if (num_completions > 1) |
| 104 | { |
| 105 | const char *comment = "\nAvailable completions:"; |
| 106 | |
| 107 | int num_elements = num_completions + 1; |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 108 | OutWrite(comment, strlen (comment), NO_ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | if (num_completions < page_size) |
| 110 | { |
| 111 | for (int i = 1; i < num_elements; i++) |
| 112 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 113 | completion_str = completions.GetStringAtIndex(i); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 114 | OutWrite("\n\t", 2, NO_ASYNC); |
| 115 | OutWrite(completion_str, strlen (completion_str), NO_ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | } |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 117 | OutWrite ("\n", 1, NO_ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
| 121 | int cur_pos = 1; |
| 122 | char reply; |
| 123 | int got_char; |
| 124 | while (cur_pos < num_elements) |
| 125 | { |
| 126 | int endpoint = cur_pos + page_size; |
| 127 | if (endpoint > num_elements) |
| 128 | endpoint = num_elements; |
| 129 | for (; cur_pos < endpoint; cur_pos++) |
| 130 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 131 | completion_str = completions.GetStringAtIndex(cur_pos); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 132 | OutWrite("\n\t", 2, NO_ASYNC); |
| 133 | OutWrite(completion_str, strlen (completion_str), NO_ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | if (cur_pos >= num_elements) |
| 137 | { |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 138 | OutWrite("\n", 1, NO_ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | break; |
| 140 | } |
| 141 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 142 | OutWrite("\nMore (Y/n/a): ", strlen ("\nMore (Y/n/a): "), NO_ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | reply = 'n'; |
| 144 | got_char = el_getc(m_edit_line, &reply); |
| 145 | if (got_char == -1 || reply == 'n') |
| 146 | break; |
| 147 | if (reply == 'a') |
| 148 | page_size = num_elements - cur_pos; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | } |
| 153 | |
| 154 | if (num_completions == 0) |
| 155 | return CC_REFRESH_BEEP; |
| 156 | else |
| 157 | return CC_REDISPLAY; |
| 158 | } |
| 159 | |
| 160 | IOChannel::IOChannel |
| 161 | ( |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 162 | FILE *editline_in, |
| 163 | FILE *editline_out, |
| 164 | FILE *out, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | FILE *err, |
| 166 | Driver *driver |
| 167 | ) : |
| 168 | SBBroadcaster ("IOChannel"), |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 169 | m_output_mutex (), |
| 170 | m_enter_elgets_time (), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 171 | m_driver (driver), |
| 172 | m_read_thread (LLDB_INVALID_HOST_THREAD), |
| 173 | m_read_thread_should_exit (false), |
| 174 | m_out_file (out), |
| 175 | m_err_file (err), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 176 | m_command_queue (), |
| 177 | m_completion_key ("\t"), |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 178 | m_edit_line (::el_init (SBHostOS::GetProgramFileSpec().GetFilename(), editline_in, editline_out, editline_out)), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 179 | m_history (history_init()), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 180 | m_history_event(), |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 181 | m_getting_command (false), |
| 182 | m_expecting_prompt (false), |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 183 | m_prompt_str (), |
| 184 | m_refresh_request_pending (false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 185 | { |
| 186 | assert (m_edit_line); |
| 187 | ::el_set (m_edit_line, EL_PROMPT, el_prompt); |
| 188 | ::el_set (m_edit_line, EL_EDITOR, "emacs"); |
| 189 | ::el_set (m_edit_line, EL_HIST, history, m_history); |
| 190 | |
| 191 | // Source $PWD/.editrc then $HOME/.editrc |
| 192 | ::el_source (m_edit_line, NULL); |
| 193 | |
Caroline Tice | 0de8d45 | 2011-05-04 21:39:02 +0000 | [diff] [blame] | 194 | el_set (m_edit_line, EL_ADDFN, "lldb_complete", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 195 | "LLDB completion function", |
| 196 | IOChannel::ElCompletionFn); |
Caroline Tice | 0de8d45 | 2011-05-04 21:39:02 +0000 | [diff] [blame] | 197 | el_set (m_edit_line, EL_BIND, m_completion_key, "lldb_complete", NULL); |
| 198 | el_set (m_edit_line, EL_BIND, "^r", "em-inc-search-prev", NULL); // Cycle through backwards search, entering string |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 199 | el_set (m_edit_line, EL_CLIENTDATA, this); |
| 200 | |
| 201 | assert (m_history); |
| 202 | ::history (m_history, &m_history_event, H_SETSIZE, 800); |
| 203 | ::history (m_history, &m_history_event, H_SETUNIQUE, 1); |
| 204 | // Load history |
| 205 | HistorySaveLoad (false); |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 206 | |
| 207 | // Set up mutex to make sure OutErr, OutWrite and RefreshPrompt do not interfere |
| 208 | // with each other when writing. |
| 209 | |
| 210 | int error; |
| 211 | ::pthread_mutexattr_t attr; |
| 212 | error = ::pthread_mutexattr_init (&attr); |
| 213 | assert (error == 0); |
| 214 | error = ::pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); |
| 215 | assert (error == 0); |
| 216 | error = ::pthread_mutex_init (&m_output_mutex, &attr); |
| 217 | assert (error == 0); |
| 218 | error = ::pthread_mutexattr_destroy (&attr); |
| 219 | assert (error == 0); |
| 220 | |
| 221 | // Initialize time that ::el_gets was last called. |
| 222 | |
| 223 | m_enter_elgets_time.tv_sec = 0; |
| 224 | m_enter_elgets_time.tv_usec = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | IOChannel::~IOChannel () |
| 228 | { |
| 229 | // Save history |
| 230 | HistorySaveLoad (true); |
| 231 | |
| 232 | if (m_history != NULL) |
| 233 | { |
| 234 | ::history_end (m_history); |
| 235 | m_history = NULL; |
| 236 | } |
| 237 | |
| 238 | if (m_edit_line != NULL) |
| 239 | { |
| 240 | ::el_end (m_edit_line); |
| 241 | m_edit_line = NULL; |
| 242 | } |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 243 | |
| 244 | ::pthread_mutex_destroy (&m_output_mutex); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void |
| 248 | IOChannel::HistorySaveLoad (bool save) |
| 249 | { |
| 250 | if (m_history != NULL) |
| 251 | { |
| 252 | char history_path[PATH_MAX]; |
Johnny Chen | 23fd10c | 2010-08-27 22:35:26 +0000 | [diff] [blame] | 253 | ::snprintf (history_path, sizeof(history_path), "~/.%s-history", SBHostOS::GetProgramFileSpec().GetFilename()); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 254 | if ((size_t)SBFileSpec::ResolvePath (history_path, history_path, sizeof(history_path)) < sizeof(history_path) - 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 255 | { |
| 256 | const char *path_ptr = history_path; |
| 257 | if (save) |
| 258 | ::history (m_history, &m_history_event, H_SAVE, path_ptr); |
| 259 | else |
| 260 | ::history (m_history, &m_history_event, H_LOAD, path_ptr); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 265 | void |
| 266 | IOChannel::LibeditOutputBytesReceived (void *baton, const void *src, size_t src_len) |
| 267 | { |
| 268 | // Make this a member variable. |
| 269 | // static std::string prompt_str; |
| 270 | IOChannel *io_channel = (IOChannel *) baton; |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 271 | IOLocker locker (io_channel->m_output_mutex); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 272 | const char *bytes = (const char *) src; |
| 273 | |
| 274 | if (io_channel->IsGettingCommand() && io_channel->m_expecting_prompt) |
| 275 | { |
| 276 | io_channel->m_prompt_str.append (bytes, src_len); |
| 277 | // Log this to make sure the prompt is really what you think it is. |
| 278 | if (io_channel->m_prompt_str.find (el_prompt(io_channel->m_edit_line)) == 0) |
| 279 | { |
| 280 | io_channel->m_expecting_prompt = false; |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 281 | io_channel->m_refresh_request_pending = false; |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 282 | io_channel->OutWrite (io_channel->m_prompt_str.c_str(), |
| 283 | io_channel->m_prompt_str.size(), NO_ASYNC); |
| 284 | io_channel->m_prompt_str.clear(); |
| 285 | } |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
| 289 | if (io_channel->m_prompt_str.size() > 0) |
| 290 | io_channel->m_prompt_str.clear(); |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 291 | std::string tmp_str (bytes, src_len); |
| 292 | if (tmp_str.find (el_prompt (io_channel->m_edit_line)) == 0) |
| 293 | io_channel->m_refresh_request_pending = false; |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 294 | io_channel->OutWrite (bytes, src_len, NO_ASYNC); |
| 295 | } |
| 296 | } |
| 297 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 298 | bool |
| 299 | IOChannel::LibeditGetInput (std::string &new_line) |
| 300 | { |
| 301 | if (m_edit_line != NULL) |
| 302 | { |
| 303 | int line_len = 0; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 304 | |
| 305 | // Set boolean indicating whether or not el_gets is trying to get input (i.e. whether or not to attempt |
| 306 | // to refresh the prompt after writing data). |
| 307 | SetGettingCommand (true); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 308 | m_expecting_prompt = true; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 309 | |
| 310 | // Call el_gets to prompt the user and read the user's input. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 311 | const char *line = ::el_gets (m_edit_line, &line_len); |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 312 | |
| 313 | // Re-set the boolean indicating whether or not el_gets is trying to get input. |
| 314 | SetGettingCommand (false); |
| 315 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 316 | if (line) |
| 317 | { |
| 318 | // strip any newlines off the end of the string... |
| 319 | while (line_len > 0 && (line[line_len - 1] == '\n' || line[line_len - 1] == '\r')) |
| 320 | --line_len; |
| 321 | if (line_len > 0) |
| 322 | { |
| 323 | ::history (m_history, &m_history_event, H_ENTER, line); |
| 324 | new_line.assign (line, line_len); // Omit the newline |
| 325 | } |
| 326 | else |
| 327 | { |
| 328 | // Someone just hit ENTER, return the empty string |
| 329 | new_line.clear(); |
| 330 | } |
| 331 | // Return true to indicate success even if a string is empty |
| 332 | return true; |
| 333 | } |
| 334 | } |
| 335 | // Return false to indicate failure. This can happen when the file handle |
| 336 | // is closed (EOF). |
| 337 | new_line.clear(); |
| 338 | return false; |
| 339 | } |
| 340 | |
| 341 | void * |
| 342 | IOChannel::IOReadThread (void *ptr) |
| 343 | { |
| 344 | IOChannel *myself = static_cast<IOChannel *> (ptr); |
| 345 | myself->Run(); |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | void |
| 350 | IOChannel::Run () |
| 351 | { |
| 352 | SBListener listener("IOChannel::Run"); |
| 353 | std::string new_line; |
| 354 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 355 | SBBroadcaster interpreter_broadcaster (m_driver->GetDebugger().GetCommandInterpreter().GetBroadcaster()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 356 | listener.StartListeningForEvents (interpreter_broadcaster, |
| 357 | SBCommandInterpreter::eBroadcastBitResetPrompt | |
| 358 | SBCommandInterpreter::eBroadcastBitThreadShouldExit | |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 359 | SBCommandInterpreter::eBroadcastBitQuitCommandReceived); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 360 | |
| 361 | listener.StartListeningForEvents (*this, |
| 362 | IOChannel::eBroadcastBitThreadShouldExit); |
| 363 | |
| 364 | listener.StartListeningForEvents (*m_driver, |
| 365 | Driver::eBroadcastBitReadyForInput | |
| 366 | Driver::eBroadcastBitThreadShouldExit); |
| 367 | |
| 368 | // Let anyone know that the IO channel is up and listening and ready for events |
| 369 | BroadcastEventByType (eBroadcastBitThreadDidStart); |
| 370 | bool done = false; |
| 371 | while (!done) |
| 372 | { |
| 373 | SBEvent event; |
| 374 | |
| 375 | listener.WaitForEvent (UINT32_MAX, event); |
| 376 | if (!event.IsValid()) |
| 377 | continue; |
| 378 | |
| 379 | const uint32_t event_type = event.GetType(); |
| 380 | |
| 381 | if (event.GetBroadcaster().IsValid()) |
| 382 | { |
| 383 | if (event.BroadcasterMatchesPtr (m_driver)) |
| 384 | { |
| 385 | if (event_type & Driver::eBroadcastBitReadyForInput) |
| 386 | { |
| 387 | std::string line; |
| 388 | |
| 389 | if (CommandQueueIsEmpty()) |
| 390 | { |
| 391 | if (LibeditGetInput(line) == false) |
| 392 | { |
| 393 | // EOF or some other file error occurred |
| 394 | done = true; |
| 395 | continue; |
| 396 | } |
| 397 | } |
| 398 | else |
| 399 | { |
| 400 | GetCommandFromQueue (line); |
| 401 | } |
| 402 | |
| 403 | // TO BE DONE: FIGURE OUT WHICH COMMANDS SHOULD NOT BE REPEATED IF USER PRESSES PLAIN 'RETURN' |
| 404 | // AND TAKE CARE OF THAT HERE. |
| 405 | |
| 406 | SBEvent line_event(IOChannel::eBroadcastBitHasUserInput, |
| 407 | line.c_str(), |
| 408 | line.size()); |
| 409 | BroadcastEvent (line_event); |
| 410 | } |
| 411 | else if (event_type & Driver::eBroadcastBitThreadShouldExit) |
| 412 | { |
| 413 | done = true; |
| 414 | break; |
| 415 | } |
| 416 | } |
| 417 | else if (event.BroadcasterMatchesRef (interpreter_broadcaster)) |
| 418 | { |
| 419 | switch (event_type) |
| 420 | { |
| 421 | case SBCommandInterpreter::eBroadcastBitResetPrompt: |
| 422 | { |
| 423 | const char *new_prompt = SBEvent::GetCStringFromEvent (event); |
| 424 | if (new_prompt) |
| 425 | g_prompt_map[m_edit_line] = new_prompt; |
| 426 | } |
| 427 | break; |
| 428 | |
| 429 | case SBCommandInterpreter::eBroadcastBitThreadShouldExit: |
| 430 | case SBCommandInterpreter::eBroadcastBitQuitCommandReceived: |
| 431 | done = true; |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | else if (event.BroadcasterMatchesPtr (this)) |
| 436 | { |
| 437 | if (event_type & IOChannel::eBroadcastBitThreadShouldExit) |
| 438 | { |
| 439 | done = true; |
| 440 | break; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | BroadcastEventByType (IOChannel::eBroadcastBitThreadDidExit); |
| 446 | m_driver = NULL; |
| 447 | m_read_thread = NULL; |
| 448 | } |
| 449 | |
| 450 | bool |
| 451 | IOChannel::Start () |
| 452 | { |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 453 | if (IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 454 | return true; |
| 455 | |
| 456 | m_read_thread = SBHostOS::ThreadCreate ("<lldb.driver.commandline_io>", IOChannel::IOReadThread, this, |
| 457 | NULL); |
| 458 | |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 459 | return (IS_VALID_LLDB_HOST_THREAD(m_read_thread)); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | bool |
| 463 | IOChannel::Stop () |
| 464 | { |
Greg Clayton | 2da6d49 | 2011-02-08 01:34:25 +0000 | [diff] [blame] | 465 | if (!IS_VALID_LLDB_HOST_THREAD(m_read_thread)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 466 | return true; |
| 467 | |
| 468 | BroadcastEventByType (eBroadcastBitThreadShouldExit); |
| 469 | |
| 470 | // Don't call Host::ThreadCancel since el_gets won't respond to this |
| 471 | // function call -- the thread will just die and all local variables in |
| 472 | // IOChannel::Run() won't get destructed down which is bad since there is |
| 473 | // a local listener holding onto broadcasters... To ensure proper shutdown, |
| 474 | // a ^D (control-D) sequence (0x04) should be written to other end of the |
| 475 | // the "in" file handle that was passed into the contructor as closing the |
| 476 | // file handle doesn't seem to make el_gets() exit.... |
| 477 | return SBHostOS::ThreadJoin (m_read_thread, NULL, NULL); |
| 478 | } |
| 479 | |
| 480 | void |
| 481 | IOChannel::RefreshPrompt () |
| 482 | { |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 483 | // If we are not in the middle of getting input from the user, there is no need to |
| 484 | // refresh the prompt. |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 485 | IOLocker locker (m_output_mutex); |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 486 | if (! IsGettingCommand()) |
| 487 | return; |
| 488 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 489 | // If we haven't finished writing the prompt, there's no need to refresh it. |
| 490 | if (m_expecting_prompt) |
| 491 | return; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 492 | |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 493 | if (m_refresh_request_pending) |
| 494 | return; |
| 495 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 496 | ::el_set (m_edit_line, EL_REFRESH); |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 497 | m_refresh_request_pending = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | void |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 501 | IOChannel::OutWrite (const char *buffer, size_t len, bool asynchronous) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 502 | { |
| 503 | if (len == 0) |
| 504 | return; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 505 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 506 | // Use the mutex to make sure OutWrite and ErrWrite do not interfere with each other's output. |
| 507 | IOLocker locker (m_output_mutex); |
Caroline Tice | 9088b06 | 2011-05-09 23:06:58 +0000 | [diff] [blame] | 508 | if (m_driver->EditlineReaderIsTop() && asynchronous) |
Caroline Tice | fe1bdf2 | 2011-05-04 16:44:57 +0000 | [diff] [blame] | 509 | ::fwrite (undo_prompt_string, 1, 4, m_out_file); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 510 | ::fwrite (buffer, 1, len, m_out_file); |
| 511 | if (asynchronous) |
| 512 | m_driver->GetDebugger().NotifyTopInputReader (eInputReaderAsynchronousOutputWritten); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 516 | IOChannel::ErrWrite (const char *buffer, size_t len, bool asynchronous) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 517 | { |
| 518 | if (len == 0) |
| 519 | return; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 520 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 521 | // Use the mutex to make sure OutWrite and ErrWrite do not interfere with each other's output. |
| 522 | IOLocker locker (m_output_mutex); |
| 523 | if (asynchronous) |
Caroline Tice | fe1bdf2 | 2011-05-04 16:44:57 +0000 | [diff] [blame] | 524 | ::fwrite (undo_prompt_string, 1, 4, m_err_file); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 525 | ::fwrite (buffer, 1, len, m_err_file); |
| 526 | if (asynchronous) |
| 527 | m_driver->GetDebugger().NotifyTopInputReader (eInputReaderAsynchronousOutputWritten); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | void |
| 531 | IOChannel::AddCommandToQueue (const char *command) |
| 532 | { |
| 533 | m_command_queue.push (std::string(command)); |
| 534 | } |
| 535 | |
| 536 | bool |
| 537 | IOChannel::GetCommandFromQueue (std::string &cmd) |
| 538 | { |
| 539 | if (m_command_queue.empty()) |
| 540 | return false; |
| 541 | cmd.swap(m_command_queue.front()); |
| 542 | m_command_queue.pop (); |
| 543 | return true; |
| 544 | } |
| 545 | |
| 546 | int |
| 547 | IOChannel::CommandQueueSize () const |
| 548 | { |
| 549 | return m_command_queue.size(); |
| 550 | } |
| 551 | |
| 552 | void |
| 553 | IOChannel::ClearCommandQueue () |
| 554 | { |
| 555 | while (!m_command_queue.empty()) |
| 556 | m_command_queue.pop(); |
| 557 | } |
| 558 | |
| 559 | bool |
| 560 | IOChannel::CommandQueueIsEmpty () const |
| 561 | { |
| 562 | return m_command_queue.empty(); |
| 563 | } |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 564 | |
| 565 | bool |
| 566 | IOChannel::IsGettingCommand () const |
| 567 | { |
| 568 | return m_getting_command; |
| 569 | } |
| 570 | |
| 571 | void |
| 572 | IOChannel::SetGettingCommand (bool new_value) |
| 573 | { |
| 574 | m_getting_command = new_value; |
| 575 | } |
| 576 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 577 | IOLocker::IOLocker (pthread_mutex_t &mutex) : |
| 578 | m_mutex_ptr (&mutex) |
| 579 | { |
| 580 | if (m_mutex_ptr) |
| 581 | ::pthread_mutex_lock (m_mutex_ptr); |
| 582 | |
| 583 | } |
| 584 | |
| 585 | IOLocker::~IOLocker () |
| 586 | { |
| 587 | if (m_mutex_ptr) |
| 588 | ::pthread_mutex_unlock (m_mutex_ptr); |
| 589 | } |