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