Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- Driver.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 "Driver.h" |
| 11 | |
| 12 | #include <getopt.h> |
| 13 | #include <libgen.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <termios.h> |
| 16 | #include <unistd.h> |
Eli Friedman | f2f321d | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <limits.h> |
Eli Friedman | d6e167d | 2010-06-09 19:11:30 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | |
| 22 | #include <string> |
| 23 | |
| 24 | #include "IOChannel.h" |
Eli Friedman | f2f321d | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 25 | #include "lldb/API/SBCommandInterpreter.h" |
| 26 | #include "lldb/API/SBCommandReturnObject.h" |
| 27 | #include "lldb/API/SBCommunication.h" |
| 28 | #include "lldb/API/SBDebugger.h" |
| 29 | #include "lldb/API/SBEvent.h" |
| 30 | #include "lldb/API/SBHostOS.h" |
| 31 | #include "lldb/API/SBListener.h" |
| 32 | #include "lldb/API/SBSourceManager.h" |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 33 | #include "lldb/API/SBStream.h" |
Eli Friedman | f2f321d | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 34 | #include "lldb/API/SBTarget.h" |
| 35 | #include "lldb/API/SBThread.h" |
| 36 | #include "lldb/API/SBProcess.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace lldb; |
| 39 | |
| 40 | static void reset_stdin_termios (); |
| 41 | static struct termios g_old_stdin_termios; |
| 42 | |
Caroline Tice | b8314fe | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 43 | static char *g_debugger_name = (char *) ""; |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 44 | static Driver *g_driver = NULL; |
Caroline Tice | b8314fe | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 46 | // In the Driver::MainLoop, we change the terminal settings. This function is |
| 47 | // added as an atexit handler to make sure we clean them up. |
| 48 | static void |
| 49 | reset_stdin_termios () |
| 50 | { |
| 51 | ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios); |
| 52 | } |
| 53 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 54 | typedef struct |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 55 | { |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 56 | uint32_t usage_mask; // Used to mark options that can be used together. If (1 << n & usage_mask) != 0 |
| 57 | // then this option belongs to option set n. |
| 58 | bool required; // This option is required (in the current usage level) |
| 59 | const char * long_option; // Full name for this option. |
| 60 | char short_option; // Single character for this option. |
| 61 | int option_has_arg; // no_argument, required_argument or optional_argument |
Greg Clayton | 5e342f5 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 62 | uint32_t completion_type; // Cookie the option class can use to do define the argument completion. |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 63 | lldb::CommandArgumentType argument_type; // Type of argument this option takes |
| 64 | const char * usage_text; // Full text explaining what this options does and what (if any) argument to |
| 65 | // pass it. |
| 66 | } OptionDefinition; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 69 | static OptionDefinition g_options[] = |
| 70 | { |
| 71 | { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , NULL, eArgTypeNone, "Prints out the usage information for the LLDB debugger." }, |
| 72 | { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , NULL, eArgTypeNone, "Prints out the current version number of the LLDB debugger." }, |
| 73 | { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, NULL, eArgTypeArchitecture,"Tells the debugger to use the specified architecture when starting and running the program. <architecture> must be one of the architectures for which the program was compiled." }, |
| 74 | { LLDB_OPT_SET_3 | LLDB_OPT_SET_4, false, "script-language", 'l', required_argument, NULL, eArgTypeScriptLang,"Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python extensions have been implemented." }, |
| 75 | { LLDB_OPT_SET_3 | LLDB_OPT_SET_4, false, "debug" , 'd', no_argument , NULL, eArgTypeNone,"Tells the debugger to print out extra information for debugging itself." }, |
| 76 | { LLDB_OPT_SET_3 | LLDB_OPT_SET_4, false, "source" , 's', required_argument, NULL, eArgTypeFilename, "Tells the debugger to read in and execute the file <file>, which should contain lldb commands." }, |
| 77 | { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, NULL, eArgTypeFilename, "Tells the debugger to use the file <filename> as the program to be debugged." }, |
| 78 | { LLDB_OPT_SET_ALL, false, "editor" , 'e', no_argument , NULL, eArgTypeNone, "Tells the debugger to open source files using the host's \"external editor\" mechanism." }, |
| 79 | { LLDB_OPT_SET_ALL, false, "no-lldbinit" , 'n', no_argument , NULL, eArgTypeNone, "Do not automatically parse any '.lldbinit' files." }, |
| 80 | { 0, false, NULL , 0 , 0 , NULL, eArgTypeNone, NULL } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | |
| 84 | Driver::Driver () : |
| 85 | SBBroadcaster ("Driver"), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 86 | m_debugger (SBDebugger::Create()), |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 87 | m_editline_pty (), |
| 88 | m_editline_slave_fh (NULL), |
| 89 | m_editline_reader (), |
| 90 | m_io_channel_ap (), |
| 91 | m_option_data (), |
| 92 | m_waiting_for_command (false) |
| 93 | { |
Greg Clayton | 421ca50 | 2011-05-29 04:06:55 +0000 | [diff] [blame] | 94 | // We want to be able to handle CTRL+D in the terminal to have it terminate |
| 95 | // certain input |
| 96 | m_debugger.SetCloseInputOnEOF (false); |
Caroline Tice | b8314fe | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 97 | g_debugger_name = (char *) m_debugger.GetInstanceName(); |
| 98 | if (g_debugger_name == NULL) |
| 99 | g_debugger_name = (char *) ""; |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 100 | g_driver = this; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | Driver::~Driver () |
| 104 | { |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 105 | g_driver = NULL; |
| 106 | g_debugger_name = NULL; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void |
| 110 | Driver::CloseIOChannelFile () |
| 111 | { |
| 112 | // Write and End of File sequence to the file descriptor to ensure any |
| 113 | // read functions can exit. |
| 114 | char eof_str[] = "\x04"; |
| 115 | ::write (m_editline_pty.GetMasterFileDescriptor(), eof_str, strlen(eof_str)); |
| 116 | |
| 117 | m_editline_pty.CloseMasterFileDescriptor(); |
| 118 | |
| 119 | if (m_editline_slave_fh) |
| 120 | { |
| 121 | ::fclose (m_editline_slave_fh); |
| 122 | m_editline_slave_fh = NULL; |
| 123 | } |
| 124 | } |
| 125 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 126 | // This function takes INDENT, which tells how many spaces to output at the front |
| 127 | // of each line; TEXT, which is the text that is to be output. It outputs the |
| 128 | // text, on multiple lines if necessary, to RESULT, with INDENT spaces at the |
| 129 | // front of each line. It breaks lines on spaces, tabs or newlines, shortening |
| 130 | // the line if necessary to not break in the middle of a word. It assumes that |
| 131 | // each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | |
| 133 | void |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 134 | OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 135 | { |
| 136 | int len = strlen (text); |
| 137 | std::string text_string (text); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 138 | |
| 139 | // Force indentation to be reasonable. |
| 140 | if (indent >= output_max_columns) |
| 141 | indent = 0; |
| 142 | |
| 143 | // Will it all fit on one line? |
| 144 | |
| 145 | if (len + indent < output_max_columns) |
| 146 | // Output as a single line |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 147 | fprintf (out, "%*s%s\n", indent, "", text); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 148 | else |
| 149 | { |
| 150 | // We need to break it up into multiple lines. |
| 151 | int text_width = output_max_columns - indent - 1; |
| 152 | int start = 0; |
| 153 | int end = start; |
| 154 | int final_end = len; |
| 155 | int sub_len; |
| 156 | |
| 157 | while (end < final_end) |
| 158 | { |
| 159 | // Dont start the 'text' on a space, since we're already outputting the indentation. |
| 160 | while ((start < final_end) && (text[start] == ' ')) |
| 161 | start++; |
| 162 | |
| 163 | end = start + text_width; |
| 164 | if (end > final_end) |
| 165 | end = final_end; |
| 166 | else |
| 167 | { |
| 168 | // If we're not at the end of the text, make sure we break the line on white space. |
| 169 | while (end > start |
| 170 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 171 | end--; |
| 172 | } |
| 173 | sub_len = end - start; |
| 174 | std::string substring = text_string.substr (start, sub_len); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 175 | fprintf (out, "%*s%s\n", indent, "", substring.c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 176 | start = end + 1; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 182 | ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 183 | { |
| 184 | uint32_t screen_width = 80; |
| 185 | uint32_t indent_level = 0; |
| 186 | const char *name = "lldb"; |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 187 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 188 | fprintf (out, "\nUsage:\n\n"); |
| 189 | |
| 190 | indent_level += 2; |
| 191 | |
| 192 | |
| 193 | // First, show each usage level set of options, e.g. <cmd> [options-for-level-0] |
| 194 | // <cmd> [options-for-level-1] |
| 195 | // etc. |
| 196 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 197 | uint32_t num_options; |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 198 | uint32_t num_option_sets = 0; |
| 199 | |
| 200 | for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 201 | { |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 202 | uint32_t this_usage_mask = option_table[num_options].usage_mask; |
| 203 | if (this_usage_mask == LLDB_OPT_SET_ALL) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 204 | { |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 205 | if (num_option_sets == 0) |
| 206 | num_option_sets = 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | } |
| 208 | else |
| 209 | { |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 210 | for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 211 | { |
| 212 | if (this_usage_mask & 1 << j) |
| 213 | { |
| 214 | if (num_option_sets <= j) |
| 215 | num_option_sets = j + 1; |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++) |
| 222 | { |
| 223 | uint32_t opt_set_mask; |
| 224 | |
| 225 | opt_set_mask = 1 << opt_set; |
| 226 | |
| 227 | if (opt_set > 0) |
| 228 | fprintf (out, "\n"); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 229 | fprintf (out, "%*s%s", indent_level, "", name); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 230 | |
| 231 | for (uint32_t i = 0; i < num_options; ++i) |
| 232 | { |
| 233 | if (option_table[i].usage_mask & opt_set_mask) |
| 234 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 235 | CommandArgumentType arg_type = option_table[i].argument_type; |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 236 | const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 237 | if (option_table[i].required) |
| 238 | { |
| 239 | if (option_table[i].option_has_arg == required_argument) |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 240 | fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 241 | else if (option_table[i].option_has_arg == optional_argument) |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 242 | fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 243 | else |
| 244 | fprintf (out, " -%c", option_table[i].short_option); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | if (option_table[i].option_has_arg == required_argument) |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 249 | fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 250 | else if (option_table[i].option_has_arg == optional_argument) |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 251 | fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 252 | else |
| 253 | fprintf (out, " [-%c]", option_table[i].short_option); |
| 254 | } |
| 255 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | |
| 259 | fprintf (out, "\n\n"); |
| 260 | |
| 261 | // Now print out all the detailed information about the various options: long form, short form and help text: |
| 262 | // -- long_name <argument> |
| 263 | // - short <argument> |
| 264 | // help text |
| 265 | |
| 266 | // This variable is used to keep track of which options' info we've printed out, because some options can be in |
| 267 | // more than one usage level, but we only want to print the long form of its information once. |
| 268 | |
| 269 | Driver::OptionData::OptionSet options_seen; |
| 270 | Driver::OptionData::OptionSet::iterator pos; |
| 271 | |
| 272 | indent_level += 5; |
| 273 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 274 | for (uint32_t i = 0; i < num_options; ++i) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 275 | { |
| 276 | // Only print this option if we haven't already seen it. |
| 277 | pos = options_seen.find (option_table[i].short_option); |
| 278 | if (pos == options_seen.end()) |
| 279 | { |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 280 | CommandArgumentType arg_type = option_table[i].argument_type; |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 281 | const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type); |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 282 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 283 | options_seen.insert (option_table[i].short_option); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 284 | fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option); |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 285 | if (arg_type != eArgTypeNone) |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 286 | fprintf (out, "<%s>", arg_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 287 | fprintf (out, "\n"); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 288 | fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option); |
Caroline Tice | 4d6675c | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 289 | if (arg_type != eArgTypeNone) |
Greg Clayton | aa378b1 | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 290 | fprintf (out, "<%s>", arg_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 291 | fprintf (out, "\n"); |
| 292 | indent_level += 5; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 293 | OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | indent_level -= 5; |
| 295 | fprintf (out, "\n"); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | indent_level -= 5; |
| 300 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 301 | fprintf (out, "\n%*s('%s <filename>' also works, to specify the file to be debugged.)\n\n", |
| 302 | indent_level, "", name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 306 | BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table, |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 307 | uint32_t num_options) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 308 | { |
| 309 | if (num_options == 0) |
| 310 | return; |
| 311 | |
| 312 | uint32_t i; |
| 313 | uint32_t j; |
| 314 | std::bitset<256> option_seen; |
| 315 | |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 316 | getopt_table.resize (num_options + 1); |
| 317 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 318 | for (i = 0, j = 0; i < num_options; ++i) |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 319 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 320 | char short_opt = expanded_option_table[i].short_option; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 322 | if (option_seen.test(short_opt) == false) |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 323 | { |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 324 | getopt_table[j].name = expanded_option_table[i].long_option; |
| 325 | getopt_table[j].has_arg = expanded_option_table[i].option_has_arg; |
| 326 | getopt_table[j].flag = NULL; |
| 327 | getopt_table[j].val = expanded_option_table[i].short_option; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 328 | option_seen.set(short_opt); |
| 329 | ++j; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 332 | |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 333 | getopt_table[j].name = NULL; |
| 334 | getopt_table[j].has_arg = 0; |
| 335 | getopt_table[j].flag = NULL; |
| 336 | getopt_table[j].val = 0; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 337 | |
| 338 | } |
| 339 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 340 | Driver::OptionData::OptionData () : |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 341 | m_args(), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 342 | m_script_lang (lldb::eScriptLanguageDefault), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 343 | m_crash_log (), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 344 | m_source_command_files (), |
| 345 | m_debug_mode (false), |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 346 | m_print_version (false), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 347 | m_print_help (false), |
Stephen Wilson | dbeb3e1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 348 | m_use_external_editor(false), |
| 349 | m_seen_options() |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 350 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | Driver::OptionData::~OptionData () |
| 354 | { |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | Driver::OptionData::Clear () |
| 359 | { |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 360 | m_args.clear (); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 361 | m_script_lang = lldb::eScriptLanguageDefault; |
| 362 | m_source_command_files.clear (); |
| 363 | m_debug_mode = false; |
| 364 | m_print_help = false; |
| 365 | m_print_version = false; |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 366 | m_use_external_editor = false; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void |
| 370 | Driver::ResetOptionValues () |
| 371 | { |
| 372 | m_option_data.Clear (); |
| 373 | } |
| 374 | |
| 375 | const char * |
| 376 | Driver::GetFilename() const |
| 377 | { |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 378 | if (m_option_data.m_args.empty()) |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 379 | return NULL; |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 380 | return m_option_data.m_args.front().c_str(); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | const char * |
| 384 | Driver::GetCrashLogFilename() const |
| 385 | { |
| 386 | if (m_option_data.m_crash_log.empty()) |
| 387 | return NULL; |
| 388 | return m_option_data.m_crash_log.c_str(); |
| 389 | } |
| 390 | |
| 391 | lldb::ScriptLanguage |
| 392 | Driver::GetScriptLanguage() const |
| 393 | { |
| 394 | return m_option_data.m_script_lang; |
| 395 | } |
| 396 | |
| 397 | size_t |
| 398 | Driver::GetNumSourceCommandFiles () const |
| 399 | { |
| 400 | return m_option_data.m_source_command_files.size(); |
| 401 | } |
| 402 | |
| 403 | const char * |
| 404 | Driver::GetSourceCommandFileAtIndex (uint32_t idx) const |
| 405 | { |
| 406 | if (idx < m_option_data.m_source_command_files.size()) |
| 407 | return m_option_data.m_source_command_files[idx].c_str(); |
| 408 | return NULL; |
| 409 | } |
| 410 | |
| 411 | bool |
| 412 | Driver::GetDebugMode() const |
| 413 | { |
| 414 | return m_option_data.m_debug_mode; |
| 415 | } |
| 416 | |
| 417 | |
| 418 | // Check the arguments that were passed to this program to make sure they are valid and to get their |
| 419 | // argument values (if any). Return a boolean value indicating whether or not to start up the full |
| 420 | // debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR |
| 421 | // if the user only wanted help or version information. |
| 422 | |
| 423 | SBError |
| 424 | Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit) |
| 425 | { |
| 426 | ResetOptionValues (); |
| 427 | |
| 428 | SBCommandReturnObject result; |
| 429 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 430 | SBError error; |
| 431 | std::string option_string; |
| 432 | struct option *long_options = NULL; |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 433 | std::vector<struct option> long_options_vector; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 434 | uint32_t num_options; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 435 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 436 | for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options) |
| 437 | /* Do Nothing. */; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 438 | |
| 439 | if (num_options == 0) |
| 440 | { |
| 441 | if (argc > 1) |
| 442 | error.SetErrorStringWithFormat ("invalid number of options"); |
| 443 | return error; |
| 444 | } |
| 445 | |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 446 | BuildGetOptTable (g_options, long_options_vector, num_options); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 447 | |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 448 | if (long_options_vector.empty()) |
| 449 | long_options = NULL; |
| 450 | else |
| 451 | long_options = &long_options_vector.front(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 452 | |
| 453 | if (long_options == NULL) |
| 454 | { |
| 455 | error.SetErrorStringWithFormat ("invalid long options"); |
| 456 | return error; |
| 457 | } |
| 458 | |
| 459 | // Build the option_string argument for call to getopt_long. |
| 460 | |
| 461 | for (int i = 0; long_options[i].name != NULL; ++i) |
| 462 | { |
| 463 | if (long_options[i].flag == NULL) |
| 464 | { |
| 465 | option_string.push_back ((char) long_options[i].val); |
| 466 | switch (long_options[i].has_arg) |
| 467 | { |
| 468 | default: |
| 469 | case no_argument: |
| 470 | break; |
| 471 | case required_argument: |
| 472 | option_string.push_back (':'); |
| 473 | break; |
| 474 | case optional_argument: |
| 475 | option_string.append ("::"); |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | // Prepare for & make calls to getopt_long. |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 482 | #if __GLIBC__ |
| 483 | optind = 0; |
| 484 | #else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | optreset = 1; |
| 486 | optind = 1; |
Eli Friedman | ef2bc87 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 487 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | int val; |
| 489 | while (1) |
| 490 | { |
| 491 | int long_options_index = -1; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 492 | val = ::getopt_long (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 493 | |
| 494 | if (val == -1) |
| 495 | break; |
| 496 | else if (val == '?') |
| 497 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 498 | m_option_data.m_print_help = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 499 | error.SetErrorStringWithFormat ("unknown or ambiguous option"); |
| 500 | break; |
| 501 | } |
| 502 | else if (val == 0) |
| 503 | continue; |
| 504 | else |
| 505 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 506 | m_option_data.m_seen_options.insert ((char) val); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 507 | if (long_options_index == -1) |
| 508 | { |
| 509 | for (int i = 0; |
| 510 | long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val; |
| 511 | ++i) |
| 512 | { |
| 513 | if (long_options[i].val == val) |
| 514 | { |
| 515 | long_options_index = i; |
| 516 | break; |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | if (long_options_index >= 0) |
| 522 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 523 | const char short_option = (char) g_options[long_options_index].short_option; |
| 524 | |
| 525 | switch (short_option) |
| 526 | { |
| 527 | case 'h': |
| 528 | m_option_data.m_print_help = true; |
| 529 | break; |
| 530 | |
| 531 | case 'v': |
| 532 | m_option_data.m_print_version = true; |
| 533 | break; |
| 534 | |
| 535 | case 'c': |
| 536 | m_option_data.m_crash_log = optarg; |
| 537 | break; |
Greg Clayton | 887aa28 | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 538 | |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 539 | case 'e': |
| 540 | m_option_data.m_use_external_editor = true; |
| 541 | break; |
Greg Clayton | 887aa28 | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 542 | |
| 543 | case 'n': |
| 544 | m_debugger.SkipLLDBInitFiles (true); |
| 545 | break; |
| 546 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 547 | case 'f': |
| 548 | { |
| 549 | SBFileSpec file(optarg); |
| 550 | if (file.Exists()) |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 551 | { |
| 552 | m_option_data.m_args.push_back (optarg); |
| 553 | } |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 554 | else if (file.ResolveExecutableLocation()) |
| 555 | { |
| 556 | char path[PATH_MAX]; |
Johnny Chen | ee6e790 | 2011-08-10 22:06:24 +0000 | [diff] [blame] | 557 | file.GetPath (path, sizeof(path)); |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 558 | m_option_data.m_args.push_back (path); |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 559 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 560 | else |
| 561 | error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg); |
| 562 | } |
| 563 | break; |
| 564 | |
| 565 | case 'a': |
| 566 | if (!m_debugger.SetDefaultArchitecture (optarg)) |
| 567 | error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg); |
| 568 | break; |
| 569 | |
| 570 | case 'l': |
| 571 | m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg); |
| 572 | break; |
| 573 | |
| 574 | case 'd': |
| 575 | m_option_data.m_debug_mode = true; |
| 576 | break; |
| 577 | |
| 578 | case 's': |
| 579 | { |
| 580 | SBFileSpec file(optarg); |
| 581 | if (file.Exists()) |
| 582 | m_option_data.m_source_command_files.push_back (optarg); |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 583 | else if (file.ResolveExecutableLocation()) |
| 584 | { |
| 585 | char final_path[PATH_MAX]; |
Johnny Chen | ee6e790 | 2011-08-10 22:06:24 +0000 | [diff] [blame] | 586 | file.GetPath (final_path, sizeof(final_path)); |
Caroline Tice | eddffe9 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 587 | std::string path_str (final_path); |
| 588 | m_option_data.m_source_command_files.push_back (path_str); |
| 589 | } |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 590 | else |
| 591 | error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg); |
| 592 | } |
| 593 | break; |
| 594 | |
| 595 | default: |
| 596 | m_option_data.m_print_help = true; |
| 597 | error.SetErrorStringWithFormat ("unrecognized option %c", short_option); |
| 598 | break; |
| 599 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | } |
| 601 | else |
| 602 | { |
| 603 | error.SetErrorStringWithFormat ("invalid option with value %i", val); |
| 604 | } |
| 605 | if (error.Fail()) |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 606 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 607 | return error; |
Caroline Tice | bd5c63e | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 608 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 611 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 612 | if (error.Fail() || m_option_data.m_print_help) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 613 | { |
| 614 | ShowUsage (out_fh, g_options, m_option_data); |
Greg Clayton | 9caa946 | 2010-10-11 01:13:37 +0000 | [diff] [blame] | 615 | exit = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 616 | } |
| 617 | else if (m_option_data.m_print_version) |
| 618 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 619 | ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString()); |
| 620 | exit = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 621 | } |
| 622 | else if (! m_option_data.m_crash_log.empty()) |
| 623 | { |
| 624 | // Handle crash log stuff here. |
| 625 | } |
| 626 | else |
| 627 | { |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 628 | // Any arguments that are left over after option parsing are for |
| 629 | // the program. If a file was specified with -f then the filename |
| 630 | // is already in the m_option_data.m_args array, and any remaining args |
| 631 | // are arguments for the inferior program. If no file was specified with |
| 632 | // -f, then what is left is the program name followed by any arguments. |
| 633 | |
| 634 | // Skip any options we consumed with getopt_long |
| 635 | argc -= optind; |
| 636 | argv += optind; |
| 637 | |
| 638 | if (argc > 0) |
| 639 | { |
| 640 | for (int arg_idx=0; arg_idx<argc; ++arg_idx) |
| 641 | { |
| 642 | const char *arg = argv[arg_idx]; |
| 643 | if (arg) |
| 644 | m_option_data.m_args.push_back (arg); |
| 645 | } |
| 646 | } |
| 647 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 648 | } |
| 649 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 650 | return error; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 651 | } |
| 652 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 653 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 654 | Driver::GetProcessSTDOUT () |
| 655 | { |
| 656 | // The process has stuff waiting for stdout; get it and write it out to the appropriate place. |
| 657 | char stdio_buffer[1024]; |
| 658 | size_t len; |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 659 | size_t total_bytes = 0; |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 660 | while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0) |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 661 | { |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 662 | m_io_channel_ap->OutWrite (stdio_buffer, len, ASYNC); |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 663 | total_bytes += len; |
| 664 | } |
| 665 | return total_bytes; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 668 | size_t |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 669 | Driver::GetProcessSTDERR () |
| 670 | { |
| 671 | // The process has stuff waiting for stderr; get it and write it out to the appropriate place. |
| 672 | char stdio_buffer[1024]; |
| 673 | size_t len; |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 674 | size_t total_bytes = 0; |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 675 | while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0) |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 676 | { |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 677 | m_io_channel_ap->ErrWrite (stdio_buffer, len, ASYNC); |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 678 | total_bytes += len; |
| 679 | } |
| 680 | return total_bytes; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | void |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 684 | Driver::UpdateSelectedThread () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | { |
| 686 | using namespace lldb; |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 687 | SBProcess process(m_debugger.GetSelectedTarget().GetProcess()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 688 | if (process.IsValid()) |
| 689 | { |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 690 | SBThread curr_thread (process.GetSelectedThread()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 691 | SBThread thread; |
| 692 | StopReason curr_thread_stop_reason = eStopReasonInvalid; |
| 693 | curr_thread_stop_reason = curr_thread.GetStopReason(); |
| 694 | |
| 695 | if (!curr_thread.IsValid() || |
| 696 | curr_thread_stop_reason == eStopReasonInvalid || |
| 697 | curr_thread_stop_reason == eStopReasonNone) |
| 698 | { |
| 699 | // Prefer a thread that has just completed its plan over another thread as current thread. |
| 700 | SBThread plan_thread; |
| 701 | SBThread other_thread; |
| 702 | const size_t num_threads = process.GetNumThreads(); |
| 703 | size_t i; |
| 704 | for (i = 0; i < num_threads; ++i) |
| 705 | { |
| 706 | thread = process.GetThreadAtIndex(i); |
| 707 | StopReason thread_stop_reason = thread.GetStopReason(); |
| 708 | switch (thread_stop_reason) |
| 709 | { |
| 710 | default: |
| 711 | case eStopReasonInvalid: |
| 712 | case eStopReasonNone: |
| 713 | break; |
| 714 | |
| 715 | case eStopReasonTrace: |
| 716 | case eStopReasonBreakpoint: |
| 717 | case eStopReasonWatchpoint: |
| 718 | case eStopReasonSignal: |
| 719 | case eStopReasonException: |
| 720 | if (!other_thread.IsValid()) |
| 721 | other_thread = thread; |
| 722 | break; |
| 723 | case eStopReasonPlanComplete: |
| 724 | if (!plan_thread.IsValid()) |
| 725 | plan_thread = thread; |
| 726 | break; |
| 727 | } |
| 728 | } |
| 729 | if (plan_thread.IsValid()) |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 730 | process.SetSelectedThread (plan_thread); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 731 | else if (other_thread.IsValid()) |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 732 | process.SetSelectedThread (other_thread); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 733 | else |
| 734 | { |
| 735 | if (curr_thread.IsValid()) |
| 736 | thread = curr_thread; |
| 737 | else |
| 738 | thread = process.GetThreadAtIndex(0); |
| 739 | |
| 740 | if (thread.IsValid()) |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 741 | process.SetSelectedThread (thread); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | |
| 748 | // This function handles events that were broadcast by the process. |
| 749 | void |
| 750 | Driver::HandleProcessEvent (const SBEvent &event) |
| 751 | { |
| 752 | using namespace lldb; |
| 753 | const uint32_t event_type = event.GetType(); |
| 754 | |
| 755 | if (event_type & SBProcess::eBroadcastBitSTDOUT) |
| 756 | { |
| 757 | // The process has stdout available, get it and write it out to the |
| 758 | // appropriate place. |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 759 | GetProcessSTDOUT (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 760 | } |
| 761 | else if (event_type & SBProcess::eBroadcastBitSTDERR) |
| 762 | { |
| 763 | // The process has stderr available, get it and write it out to the |
| 764 | // appropriate place. |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 765 | GetProcessSTDERR (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 766 | } |
| 767 | else if (event_type & SBProcess::eBroadcastBitStateChanged) |
| 768 | { |
| 769 | // Drain all stout and stderr so we don't see any output come after |
| 770 | // we print our prompts |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 771 | GetProcessSTDOUT (); |
| 772 | GetProcessSTDERR (); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 773 | // Something changed in the process; get the event and report the process's current status and location to |
| 774 | // the user. |
| 775 | StateType event_state = SBProcess::GetStateFromEvent (event); |
| 776 | if (event_state == eStateInvalid) |
| 777 | return; |
| 778 | |
| 779 | SBProcess process (SBProcess::GetProcessFromEvent (event)); |
| 780 | assert (process.IsValid()); |
| 781 | |
| 782 | switch (event_state) |
| 783 | { |
| 784 | case eStateInvalid: |
| 785 | case eStateUnloaded: |
Greg Clayton | e71e258 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 786 | case eStateConnected: |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 787 | case eStateAttaching: |
| 788 | case eStateLaunching: |
| 789 | case eStateStepping: |
| 790 | case eStateDetached: |
| 791 | { |
| 792 | char message[1024]; |
| 793 | int message_len = ::snprintf (message, sizeof(message), "Process %d %s\n", process.GetProcessID(), |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 794 | m_debugger.StateAsCString (event_state)); |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 795 | m_io_channel_ap->OutWrite(message, message_len, ASYNC); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 796 | } |
| 797 | break; |
| 798 | |
| 799 | case eStateRunning: |
| 800 | // Don't be chatty when we run... |
| 801 | break; |
| 802 | |
| 803 | case eStateExited: |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 804 | { |
| 805 | SBCommandReturnObject result; |
| 806 | m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false); |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 807 | m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize(), ASYNC); |
| 808 | m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), ASYNC); |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 809 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 810 | break; |
| 811 | |
| 812 | case eStateStopped: |
| 813 | case eStateCrashed: |
| 814 | case eStateSuspended: |
| 815 | // Make sure the program hasn't been auto-restarted: |
| 816 | if (SBProcess::GetRestartedFromEvent (event)) |
| 817 | { |
| 818 | // FIXME: Do we want to report this, or would that just be annoyingly chatty? |
| 819 | char message[1024]; |
| 820 | int message_len = ::snprintf (message, sizeof(message), "Process %d stopped and was programmatically restarted.\n", |
| 821 | process.GetProcessID()); |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 822 | m_io_channel_ap->OutWrite(message, message_len, ASYNC); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 823 | } |
| 824 | else |
| 825 | { |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 826 | SBCommandReturnObject result; |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 827 | UpdateSelectedThread (); |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 828 | m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false); |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 829 | m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize(), ASYNC); |
| 830 | m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), ASYNC); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 831 | } |
| 832 | break; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | // This function handles events broadcast by the IOChannel (HasInput, UserInterrupt, or ThreadShouldExit). |
| 838 | |
| 839 | bool |
| 840 | Driver::HandleIOEvent (const SBEvent &event) |
| 841 | { |
| 842 | bool quit = false; |
| 843 | |
| 844 | const uint32_t event_type = event.GetType(); |
| 845 | |
| 846 | if (event_type & IOChannel::eBroadcastBitHasUserInput) |
| 847 | { |
| 848 | // We got some input (i.e. a command string) from the user; pass it off to the command interpreter for |
| 849 | // handling. |
| 850 | |
| 851 | const char *command_string = SBEvent::GetCStringFromEvent(event); |
| 852 | if (command_string == NULL) |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 853 | command_string = ""; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 854 | SBCommandReturnObject result; |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 855 | |
Caroline Tice | 845d6da | 2011-05-16 19:20:50 +0000 | [diff] [blame] | 856 | // We don't want the result to bypass the OutWrite function in IOChannel, as this can result in odd |
| 857 | // output orderings and problems with the prompt. |
Jim Ingham | 2e8cb8a | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 858 | m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, true); |
| 859 | |
Caroline Tice | 845d6da | 2011-05-16 19:20:50 +0000 | [diff] [blame] | 860 | if (result.GetOutputSize() > 0) |
| 861 | m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), NO_ASYNC); |
| 862 | |
| 863 | if (result.GetErrorSize() > 0) |
| 864 | m_io_channel_ap->OutWrite (result.GetError(), result.GetErrorSize(), NO_ASYNC); |
| 865 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 866 | // We are done getting and running our command, we can now clear the |
| 867 | // m_waiting_for_command so we can get another one. |
| 868 | m_waiting_for_command = false; |
| 869 | |
| 870 | // If our editline input reader is active, it means another input reader |
| 871 | // got pushed onto the input reader and caused us to become deactivated. |
| 872 | // When the input reader above us gets popped, we will get re-activated |
| 873 | // and our prompt will refresh in our callback |
| 874 | if (m_editline_reader.IsActive()) |
| 875 | { |
| 876 | ReadyForCommand (); |
| 877 | } |
| 878 | } |
| 879 | else if (event_type & IOChannel::eBroadcastBitUserInterrupt) |
| 880 | { |
| 881 | // This is here to handle control-c interrupts from the user. It has not yet really been implemented. |
| 882 | // TO BE DONE: PROPERLY HANDLE CONTROL-C FROM USER |
| 883 | //m_io_channel_ap->CancelInput(); |
| 884 | // Anything else? Send Interrupt to process? |
| 885 | } |
| 886 | else if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) || |
| 887 | (event_type & IOChannel::eBroadcastBitThreadDidExit)) |
| 888 | { |
| 889 | // If the IOChannel thread is trying to go away, then it is definitely |
| 890 | // time to end the debugging session. |
| 891 | quit = true; |
| 892 | } |
| 893 | |
| 894 | return quit; |
| 895 | } |
| 896 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 897 | void |
| 898 | Driver::MasterThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 899 | { |
| 900 | Driver *driver = (Driver*)baton; |
| 901 | driver->GetFromMaster ((const char *)src, src_len); |
| 902 | } |
| 903 | |
| 904 | void |
| 905 | Driver::GetFromMaster (const char *src, size_t src_len) |
| 906 | { |
| 907 | // Echo the characters back to the Debugger's stdout, that way if you |
| 908 | // type characters while a command is running, you'll see what you've typed. |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 909 | FILE *out_fh = m_debugger.GetOutputFileHandle(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 910 | if (out_fh) |
| 911 | ::fwrite (src, 1, src_len, out_fh); |
| 912 | } |
| 913 | |
| 914 | size_t |
| 915 | Driver::EditLineInputReaderCallback |
| 916 | ( |
| 917 | void *baton, |
| 918 | SBInputReader *reader, |
| 919 | InputReaderAction notification, |
| 920 | const char *bytes, |
| 921 | size_t bytes_len |
| 922 | ) |
| 923 | { |
| 924 | Driver *driver = (Driver *)baton; |
| 925 | |
| 926 | switch (notification) |
| 927 | { |
| 928 | case eInputReaderActivate: |
| 929 | break; |
| 930 | |
| 931 | case eInputReaderReactivate: |
| 932 | driver->ReadyForCommand(); |
| 933 | break; |
| 934 | |
| 935 | case eInputReaderDeactivate: |
| 936 | break; |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 937 | |
| 938 | case eInputReaderAsynchronousOutputWritten: |
| 939 | if (driver->m_io_channel_ap.get() != NULL) |
| 940 | driver->m_io_channel_ap->RefreshPrompt(); |
| 941 | break; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 942 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 943 | case eInputReaderInterrupt: |
| 944 | if (driver->m_io_channel_ap.get() != NULL) |
| 945 | { |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 946 | driver->m_io_channel_ap->OutWrite ("^C\n", 3, NO_ASYNC); |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 947 | driver->m_io_channel_ap->RefreshPrompt(); |
| 948 | } |
| 949 | break; |
| 950 | |
| 951 | case eInputReaderEndOfFile: |
| 952 | if (driver->m_io_channel_ap.get() != NULL) |
| 953 | { |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 954 | driver->m_io_channel_ap->OutWrite ("^D\n", 3, NO_ASYNC); |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 955 | driver->m_io_channel_ap->RefreshPrompt (); |
| 956 | } |
| 957 | write (driver->m_editline_pty.GetMasterFileDescriptor(), "quit\n", 5); |
| 958 | break; |
| 959 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 960 | case eInputReaderGotToken: |
| 961 | write (driver->m_editline_pty.GetMasterFileDescriptor(), bytes, bytes_len); |
| 962 | break; |
| 963 | |
| 964 | case eInputReaderDone: |
| 965 | break; |
| 966 | } |
| 967 | return bytes_len; |
| 968 | } |
| 969 | |
| 970 | void |
| 971 | Driver::MainLoop () |
| 972 | { |
| 973 | char error_str[1024]; |
| 974 | if (m_editline_pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, error_str, sizeof(error_str)) == false) |
| 975 | { |
| 976 | ::fprintf (stderr, "error: failed to open driver pseudo terminal : %s", error_str); |
| 977 | exit(1); |
| 978 | } |
| 979 | else |
| 980 | { |
| 981 | const char *driver_slave_name = m_editline_pty.GetSlaveName (error_str, sizeof(error_str)); |
| 982 | if (driver_slave_name == NULL) |
| 983 | { |
| 984 | ::fprintf (stderr, "error: failed to get slave name for driver pseudo terminal : %s", error_str); |
| 985 | exit(2); |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | m_editline_slave_fh = ::fopen (driver_slave_name, "r+"); |
| 990 | if (m_editline_slave_fh == NULL) |
| 991 | { |
| 992 | SBError error; |
| 993 | error.SetErrorToErrno(); |
| 994 | ::fprintf (stderr, "error: failed to get open slave for driver pseudo terminal : %s", |
| 995 | error.GetCString()); |
| 996 | exit(3); |
| 997 | } |
| 998 | |
| 999 | ::setbuf (m_editline_slave_fh, NULL); |
| 1000 | } |
| 1001 | } |
| 1002 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1003 | lldb_utility::PseudoTerminal editline_output_pty; |
| 1004 | FILE *editline_output_slave_fh = NULL; |
| 1005 | |
| 1006 | if (editline_output_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, sizeof (error_str)) == false) |
| 1007 | { |
| 1008 | ::fprintf (stderr, "error: failed to open output pseudo terminal : %s", error_str); |
| 1009 | exit(1); |
| 1010 | } |
| 1011 | else |
| 1012 | { |
| 1013 | const char *output_slave_name = editline_output_pty.GetSlaveName (error_str, sizeof(error_str)); |
| 1014 | if (output_slave_name == NULL) |
| 1015 | { |
| 1016 | ::fprintf (stderr, "error: failed to get slave name for output pseudo terminal : %s", error_str); |
| 1017 | exit(2); |
| 1018 | } |
| 1019 | else |
| 1020 | { |
| 1021 | editline_output_slave_fh = ::fopen (output_slave_name, "r+"); |
| 1022 | if (editline_output_slave_fh == NULL) |
| 1023 | { |
| 1024 | SBError error; |
| 1025 | error.SetErrorToErrno(); |
| 1026 | ::fprintf (stderr, "error: failed to get open slave for output pseudo terminal : %s", |
| 1027 | error.GetCString()); |
| 1028 | exit(3); |
| 1029 | } |
| 1030 | ::setbuf (editline_output_slave_fh, NULL); |
| 1031 | } |
| 1032 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1033 | |
| 1034 | // struct termios stdin_termios; |
| 1035 | |
| 1036 | if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0) |
| 1037 | atexit (reset_stdin_termios); |
| 1038 | |
| 1039 | ::setbuf (stdin, NULL); |
| 1040 | ::setbuf (stdout, NULL); |
| 1041 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1042 | m_debugger.SetErrorFileHandle (stderr, false); |
| 1043 | m_debugger.SetOutputFileHandle (stdout, false); |
| 1044 | m_debugger.SetInputFileHandle (stdin, true); |
Jim Ingham | 74989e8 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 1045 | |
| 1046 | m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1047 | |
| 1048 | // You have to drain anything that comes to the master side of the PTY. master_out_comm is |
| 1049 | // for that purpose. The reason you need to do this is a curious reason... editline will echo |
| 1050 | // characters to the PTY when it gets characters while el_gets is not running, and then when |
| 1051 | // you call el_gets (or el_getc) it will try to reset the terminal back to raw mode which blocks |
| 1052 | // if there are unconsumed characters in the out buffer. |
| 1053 | // However, you don't need to do anything with the characters, since editline will dump these |
| 1054 | // unconsumed characters after printing the prompt again in el_gets. |
| 1055 | |
Greg Clayton | eecb0f3 | 2010-12-04 02:39:47 +0000 | [diff] [blame] | 1056 | SBCommunication master_out_comm("driver.editline"); |
| 1057 | master_out_comm.SetCloseOnEOF (false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1058 | master_out_comm.AdoptFileDesriptor(m_editline_pty.GetMasterFileDescriptor(), false); |
| 1059 | master_out_comm.SetReadThreadBytesReceivedCallback(Driver::MasterThreadBytesReceived, this); |
| 1060 | |
| 1061 | if (master_out_comm.ReadThreadStart () == false) |
| 1062 | { |
| 1063 | ::fprintf (stderr, "error: failed to start master out read thread"); |
| 1064 | exit(5); |
| 1065 | } |
| 1066 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1067 | SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1068 | |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1069 | m_io_channel_ap.reset (new IOChannel(m_editline_slave_fh, editline_output_slave_fh, stdout, stderr, this)); |
| 1070 | |
| 1071 | SBCommunication out_comm_2("driver.editline_output"); |
| 1072 | out_comm_2.SetCloseOnEOF (false); |
| 1073 | out_comm_2.AdoptFileDesriptor (editline_output_pty.GetMasterFileDescriptor(), false); |
| 1074 | out_comm_2.SetReadThreadBytesReceivedCallback (IOChannel::LibeditOutputBytesReceived, m_io_channel_ap.get()); |
| 1075 | |
| 1076 | if (out_comm_2.ReadThreadStart () == false) |
| 1077 | { |
| 1078 | ::fprintf (stderr, "error: failed to start libedit output read thread"); |
| 1079 | exit (5); |
| 1080 | } |
| 1081 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1082 | |
| 1083 | struct winsize window_size; |
| 1084 | if (isatty (STDIN_FILENO) |
| 1085 | && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0) |
| 1086 | { |
Caroline Tice | 6e4c5ce | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1087 | if (window_size.ws_col > 0) |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1088 | m_debugger.SetTerminalWidth (window_size.ws_col); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | // Since input can be redirected by the debugger, we must insert our editline |
| 1092 | // input reader in the queue so we know when our reader should be active |
| 1093 | // and so we can receive bytes only when we are supposed to. |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1094 | SBError err (m_editline_reader.Initialize (m_debugger, |
| 1095 | Driver::EditLineInputReaderCallback, // callback |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1096 | this, // baton |
| 1097 | eInputReaderGranularityByte, // token_size |
| 1098 | NULL, // end token - NULL means never done |
| 1099 | NULL, // prompt - taken care of elsewhere |
| 1100 | false)); // echo input - don't need Debugger |
| 1101 | // to do this, we handle it elsewhere |
| 1102 | |
| 1103 | if (err.Fail()) |
| 1104 | { |
| 1105 | ::fprintf (stderr, "error: %s", err.GetCString()); |
| 1106 | exit (6); |
| 1107 | } |
| 1108 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1109 | m_debugger.PushInputReader (m_editline_reader); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1110 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1111 | SBListener listener(m_debugger.GetListener()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1112 | if (listener.IsValid()) |
| 1113 | { |
| 1114 | |
| 1115 | listener.StartListeningForEvents (*m_io_channel_ap, |
| 1116 | IOChannel::eBroadcastBitHasUserInput | |
| 1117 | IOChannel::eBroadcastBitUserInterrupt | |
| 1118 | IOChannel::eBroadcastBitThreadShouldExit | |
| 1119 | IOChannel::eBroadcastBitThreadDidStart | |
| 1120 | IOChannel::eBroadcastBitThreadDidExit); |
| 1121 | |
| 1122 | if (m_io_channel_ap->Start ()) |
| 1123 | { |
| 1124 | bool iochannel_thread_exited = false; |
| 1125 | |
| 1126 | listener.StartListeningForEvents (sb_interpreter.GetBroadcaster(), |
Caroline Tice | 388ca8f | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 1127 | SBCommandInterpreter::eBroadcastBitQuitCommandReceived | |
| 1128 | SBCommandInterpreter::eBroadcastBitAsynchronousOutputData | |
| 1129 | SBCommandInterpreter::eBroadcastBitAsynchronousErrorData); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1130 | |
| 1131 | // Before we handle any options from the command line, we parse the |
| 1132 | // .lldbinit file in the user's home directory. |
| 1133 | SBCommandReturnObject result; |
| 1134 | sb_interpreter.SourceInitFileInHomeDirectory(result); |
| 1135 | if (GetDebugMode()) |
| 1136 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1137 | result.PutError (m_debugger.GetErrorFileHandle()); |
| 1138 | result.PutOutput (m_debugger.GetOutputFileHandle()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
| 1141 | // Now we handle options we got from the command line |
| 1142 | char command_string[PATH_MAX * 2]; |
| 1143 | const size_t num_source_command_files = GetNumSourceCommandFiles(); |
| 1144 | if (num_source_command_files > 0) |
| 1145 | { |
| 1146 | for (size_t i=0; i < num_source_command_files; ++i) |
| 1147 | { |
| 1148 | const char *command_file = GetSourceCommandFileAtIndex(i); |
Johnny Chen | 7c98424 | 2010-07-28 21:16:11 +0000 | [diff] [blame] | 1149 | ::snprintf (command_string, sizeof(command_string), "command source '%s'", command_file); |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1150 | m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, false); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1151 | if (GetDebugMode()) |
| 1152 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1153 | result.PutError (m_debugger.GetErrorFileHandle()); |
| 1154 | result.PutOutput (m_debugger.GetOutputFileHandle()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1159 | const size_t num_args = m_option_data.m_args.size(); |
| 1160 | if (num_args > 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1161 | { |
| 1162 | char arch_name[64]; |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1163 | if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name))) |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1164 | ::snprintf (command_string, |
| 1165 | sizeof (command_string), |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 1166 | "target create --arch=%s '%s'", |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1167 | arch_name, |
| 1168 | m_option_data.m_args[0].c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1169 | else |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1170 | ::snprintf (command_string, |
| 1171 | sizeof(command_string), |
Greg Clayton | abe0fed | 2011-04-18 08:33:37 +0000 | [diff] [blame] | 1172 | "target create '%s'", |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1173 | m_option_data.m_args[0].c_str()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1174 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1175 | m_debugger.HandleCommand (command_string); |
Greg Clayton | 4dc1892 | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1176 | |
| 1177 | if (num_args > 1) |
| 1178 | { |
| 1179 | m_debugger.HandleCommand ("settings clear target.process.run-args"); |
| 1180 | char arg_cstr[1024]; |
| 1181 | for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx) |
| 1182 | { |
| 1183 | ::snprintf (arg_cstr, sizeof(arg_cstr), "settings append target.process.run-args \"%s\"", m_option_data.m_args[arg_idx].c_str()); |
| 1184 | m_debugger.HandleCommand (arg_cstr); |
| 1185 | } |
| 1186 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | // Now that all option parsing is done, we try and parse the .lldbinit |
| 1190 | // file in the current working directory |
| 1191 | sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result); |
| 1192 | if (GetDebugMode()) |
| 1193 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1194 | result.PutError(m_debugger.GetErrorFileHandle()); |
| 1195 | result.PutOutput(m_debugger.GetOutputFileHandle()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | SBEvent event; |
| 1199 | |
| 1200 | // Make sure the IO channel is started up before we try to tell it we |
| 1201 | // are ready for input |
| 1202 | listener.WaitForEventForBroadcasterWithType (UINT32_MAX, |
| 1203 | *m_io_channel_ap, |
| 1204 | IOChannel::eBroadcastBitThreadDidStart, |
| 1205 | event); |
| 1206 | |
| 1207 | ReadyForCommand (); |
| 1208 | |
| 1209 | bool done = false; |
| 1210 | while (!done) |
| 1211 | { |
| 1212 | listener.WaitForEvent (UINT32_MAX, event); |
| 1213 | if (event.IsValid()) |
| 1214 | { |
| 1215 | if (event.GetBroadcaster().IsValid()) |
| 1216 | { |
| 1217 | uint32_t event_type = event.GetType(); |
| 1218 | if (event.BroadcasterMatchesRef (*m_io_channel_ap)) |
| 1219 | { |
| 1220 | if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) || |
| 1221 | (event_type & IOChannel::eBroadcastBitThreadDidExit)) |
| 1222 | { |
| 1223 | done = true; |
| 1224 | if (event_type & IOChannel::eBroadcastBitThreadDidExit) |
| 1225 | iochannel_thread_exited = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1226 | } |
| 1227 | else |
| 1228 | done = HandleIOEvent (event); |
| 1229 | } |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 1230 | else if (event.BroadcasterMatchesRef (m_debugger.GetSelectedTarget().GetProcess().GetBroadcaster())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1231 | { |
| 1232 | HandleProcessEvent (event); |
| 1233 | } |
| 1234 | else if (event.BroadcasterMatchesRef (sb_interpreter.GetBroadcaster())) |
| 1235 | { |
| 1236 | if (event_type & SBCommandInterpreter::eBroadcastBitQuitCommandReceived) |
| 1237 | done = true; |
Caroline Tice | 388ca8f | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 1238 | else if (event_type & SBCommandInterpreter::eBroadcastBitAsynchronousErrorData) |
| 1239 | { |
| 1240 | const char *data = SBEvent::GetCStringFromEvent (event); |
| 1241 | m_io_channel_ap->ErrWrite (data, strlen(data), ASYNC); |
| 1242 | } |
| 1243 | else if (event_type & SBCommandInterpreter::eBroadcastBitAsynchronousOutputData) |
| 1244 | { |
| 1245 | const char *data = SBEvent::GetCStringFromEvent (event); |
| 1246 | m_io_channel_ap->OutWrite (data, strlen(data), ASYNC); |
| 1247 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1248 | } |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | reset_stdin_termios (); |
| 1254 | |
| 1255 | CloseIOChannelFile (); |
| 1256 | |
| 1257 | if (!iochannel_thread_exited) |
| 1258 | { |
Greg Clayton | bef1583 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 1259 | event.Clear(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1260 | listener.GetNextEventForBroadcasterWithType (*m_io_channel_ap, |
| 1261 | IOChannel::eBroadcastBitThreadDidExit, |
| 1262 | event); |
| 1263 | if (!event.IsValid()) |
| 1264 | { |
| 1265 | // Send end EOF to the driver file descriptor |
| 1266 | m_io_channel_ap->Stop(); |
| 1267 | } |
| 1268 | } |
| 1269 | |
Jim Ingham | c833295 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 1270 | SBProcess process = m_debugger.GetSelectedTarget().GetProcess(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1271 | if (process.IsValid()) |
| 1272 | process.Destroy(); |
| 1273 | } |
| 1274 | } |
| 1275 | } |
| 1276 | |
| 1277 | |
| 1278 | void |
| 1279 | Driver::ReadyForCommand () |
| 1280 | { |
| 1281 | if (m_waiting_for_command == false) |
| 1282 | { |
| 1283 | m_waiting_for_command = true; |
| 1284 | BroadcastEventByType (Driver::eBroadcastBitReadyForInput, true); |
| 1285 | } |
| 1286 | } |
| 1287 | |
| 1288 | |
Caroline Tice | b8314fe | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1289 | void |
| 1290 | sigwinch_handler (int signo) |
| 1291 | { |
| 1292 | struct winsize window_size; |
| 1293 | if (isatty (STDIN_FILENO) |
| 1294 | && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0) |
| 1295 | { |
| 1296 | if ((window_size.ws_col > 0) && (strlen (g_debugger_name) > 0)) |
| 1297 | { |
| 1298 | char width_str_buffer[25]; |
| 1299 | ::sprintf (width_str_buffer, "%d", window_size.ws_col); |
| 1300 | SBDebugger::SetInternalVariable ("term-width", width_str_buffer, g_debugger_name); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1305 | void |
| 1306 | sigint_handler (int signo) |
| 1307 | { |
| 1308 | static bool g_interrupt_sent = false; |
| 1309 | if (g_driver) |
| 1310 | { |
| 1311 | if (!g_interrupt_sent) |
| 1312 | { |
| 1313 | g_interrupt_sent = true; |
| 1314 | g_driver->GetDebugger().DispatchInputInterrupt(); |
| 1315 | g_interrupt_sent = false; |
| 1316 | return; |
| 1317 | } |
| 1318 | } |
| 1319 | |
| 1320 | exit (signo); |
| 1321 | } |
| 1322 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1323 | int |
Jim Ingham | b2b604f | 2011-01-27 20:15:39 +0000 | [diff] [blame] | 1324 | main (int argc, char const *argv[], const char *envp[]) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1325 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1326 | SBDebugger::Initialize(); |
| 1327 | |
Greg Clayton | 95e33b7 | 2010-11-07 21:02:03 +0000 | [diff] [blame] | 1328 | SBHostOS::ThreadCreated ("<lldb.driver.main-thread>"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1329 | |
Greg Clayton | 36f63a9 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1330 | signal (SIGPIPE, SIG_IGN); |
Caroline Tice | b8314fe | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1331 | signal (SIGWINCH, sigwinch_handler); |
Caroline Tice | c4f55fe | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1332 | signal (SIGINT, sigint_handler); |
Caroline Tice | b8314fe | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1333 | |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1334 | // Create a scope for driver so that the driver object will destroy itself |
| 1335 | // before SBDebugger::Terminate() is called. |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1336 | { |
Greg Clayton | 63094e0 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1337 | Driver driver; |
| 1338 | |
| 1339 | bool exit = false; |
| 1340 | SBError error (driver.ParseArgs (argc, argv, stdout, exit)); |
| 1341 | if (error.Fail()) |
| 1342 | { |
| 1343 | const char *error_cstr = error.GetCString (); |
| 1344 | if (error_cstr) |
| 1345 | ::fprintf (stderr, "error: %s\n", error_cstr); |
| 1346 | } |
| 1347 | else if (!exit) |
| 1348 | { |
| 1349 | driver.MainLoop (); |
| 1350 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | SBDebugger::Terminate(); |
| 1354 | return 0; |
| 1355 | } |