Chris Lattner | 30fdc8d | 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 | a382d47 | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <limits.h> |
Eli Friedman | 07b1627 | 2010-06-09 19:11:30 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 21 | #include <inttypes.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | #include "IOChannel.h" |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 26 | #include "lldb/API/SBBreakpoint.h" |
Eli Friedman | a382d47 | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 27 | #include "lldb/API/SBCommandInterpreter.h" |
| 28 | #include "lldb/API/SBCommandReturnObject.h" |
| 29 | #include "lldb/API/SBCommunication.h" |
| 30 | #include "lldb/API/SBDebugger.h" |
| 31 | #include "lldb/API/SBEvent.h" |
| 32 | #include "lldb/API/SBHostOS.h" |
| 33 | #include "lldb/API/SBListener.h" |
Jim Ingham | 85e8b81 | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 34 | #include "lldb/API/SBStream.h" |
Eli Friedman | a382d47 | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 35 | #include "lldb/API/SBTarget.h" |
| 36 | #include "lldb/API/SBThread.h" |
| 37 | #include "lldb/API/SBProcess.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace lldb; |
| 40 | |
| 41 | static void reset_stdin_termios (); |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 42 | static bool g_old_stdin_termios_is_valid = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | static struct termios g_old_stdin_termios; |
| 44 | |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 45 | static char *g_debugger_name = (char *) ""; |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 46 | static Driver *g_driver = NULL; |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | // In the Driver::MainLoop, we change the terminal settings. This function is |
| 49 | // added as an atexit handler to make sure we clean them up. |
| 50 | static void |
| 51 | reset_stdin_termios () |
| 52 | { |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 53 | if (g_old_stdin_termios_is_valid) |
| 54 | { |
| 55 | g_old_stdin_termios_is_valid = false; |
| 56 | ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios); |
| 57 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 60 | typedef struct |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 62 | uint32_t usage_mask; // Used to mark options that can be used together. If (1 << n & usage_mask) != 0 |
| 63 | // then this option belongs to option set n. |
| 64 | bool required; // This option is required (in the current usage level) |
| 65 | const char * long_option; // Full name for this option. |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 66 | int short_option; // Single character for this option. |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 67 | int option_has_arg; // no_argument, required_argument or optional_argument |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 68 | uint32_t completion_type; // Cookie the option class can use to do define the argument completion. |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 69 | lldb::CommandArgumentType argument_type; // Type of argument this option takes |
| 70 | const char * usage_text; // Full text explaining what this options does and what (if any) argument to |
| 71 | // pass it. |
| 72 | } OptionDefinition; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 73 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 74 | #define LLDB_3_TO_5 LLDB_OPT_SET_3|LLDB_OPT_SET_4|LLDB_OPT_SET_5 |
| 75 | #define LLDB_4_TO_5 LLDB_OPT_SET_4|LLDB_OPT_SET_5 |
| 76 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 77 | static OptionDefinition g_options[] = |
| 78 | { |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 79 | { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 80 | "Prints out the usage information for the LLDB debugger." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 81 | { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 82 | "Prints out the current version number of the LLDB debugger." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 83 | { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, 0, eArgTypeArchitecture, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 84 | "Tells the debugger to use the specified architecture when starting and running the program. <architecture> must " |
| 85 | "be one of the architectures for which the program was compiled." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 86 | { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, 0, eArgTypeFilename, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 87 | "Tells the debugger to use the file <filename> as the program to be debugged." }, |
Jason Molenda | 67c3cf5 | 2012-10-24 03:29:40 +0000 | [diff] [blame] | 88 | { LLDB_OPT_SET_3, false, "core" , 'c', required_argument, 0, eArgTypeFilename, |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 89 | "Tells the debugger to use the fullpath to <path> as the core file." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 90 | { LLDB_OPT_SET_4, true , "attach-name" , 'n', required_argument, 0, eArgTypeProcessName, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 91 | "Tells the debugger to attach to a process with the given name." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 92 | { LLDB_OPT_SET_4, true , "wait-for" , 'w', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 93 | "Tells the debugger to wait for a process with the given pid or name to launch before attaching." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 94 | { LLDB_OPT_SET_5, true , "attach-pid" , 'p', required_argument, 0, eArgTypePid, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 95 | "Tells the debugger to attach to a process with the given pid." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 96 | { LLDB_3_TO_5, false, "script-language", 'l', required_argument, 0, eArgTypeScriptLang, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 97 | "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. " |
| 98 | "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python " |
| 99 | "extensions have been implemented." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 100 | { LLDB_3_TO_5, false, "debug" , 'd', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 101 | "Tells the debugger to print out extra information for debugging itself." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 102 | { LLDB_3_TO_5, false, "source" , 's', required_argument, 0, eArgTypeFilename, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 103 | "Tells the debugger to read in and execute the file <file>, which should contain lldb commands." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 104 | { LLDB_3_TO_5, false, "editor" , 'e', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 105 | "Tells the debugger to open source files using the host's \"external editor\" mechanism." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 106 | { LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 107 | "Do not automatically parse any '.lldbinit' files." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 108 | { 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 111 | static const uint32_t last_option_set_with_args = 2; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 112 | |
| 113 | Driver::Driver () : |
| 114 | SBBroadcaster ("Driver"), |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 115 | m_debugger (SBDebugger::Create(false)), |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 116 | m_editline_pty (), |
| 117 | m_editline_slave_fh (NULL), |
| 118 | m_editline_reader (), |
| 119 | m_io_channel_ap (), |
| 120 | m_option_data (), |
Daniel Malea | 926758b | 2012-12-17 17:40:07 +0000 | [diff] [blame] | 121 | m_waiting_for_command (false), |
| 122 | m_done(false) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 123 | { |
Greg Clayton | fc3f027 | 2011-05-29 04:06:55 +0000 | [diff] [blame] | 124 | // We want to be able to handle CTRL+D in the terminal to have it terminate |
| 125 | // certain input |
| 126 | m_debugger.SetCloseInputOnEOF (false); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 127 | g_debugger_name = (char *) m_debugger.GetInstanceName(); |
| 128 | if (g_debugger_name == NULL) |
| 129 | g_debugger_name = (char *) ""; |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 130 | g_driver = this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | Driver::~Driver () |
| 134 | { |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 135 | g_driver = NULL; |
| 136 | g_debugger_name = NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void |
| 140 | Driver::CloseIOChannelFile () |
| 141 | { |
Johnny Chen | e26c721 | 2012-05-16 22:01:10 +0000 | [diff] [blame] | 142 | // Write an End of File sequence to the file descriptor to ensure any |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 143 | // read functions can exit. |
| 144 | char eof_str[] = "\x04"; |
| 145 | ::write (m_editline_pty.GetMasterFileDescriptor(), eof_str, strlen(eof_str)); |
| 146 | |
| 147 | m_editline_pty.CloseMasterFileDescriptor(); |
| 148 | |
| 149 | if (m_editline_slave_fh) |
| 150 | { |
| 151 | ::fclose (m_editline_slave_fh); |
| 152 | m_editline_slave_fh = NULL; |
| 153 | } |
| 154 | } |
| 155 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 156 | // This function takes INDENT, which tells how many spaces to output at the front |
| 157 | // of each line; TEXT, which is the text that is to be output. It outputs the |
| 158 | // text, on multiple lines if necessary, to RESULT, with INDENT spaces at the |
| 159 | // front of each line. It breaks lines on spaces, tabs or newlines, shortening |
| 160 | // the line if necessary to not break in the middle of a word. It assumes that |
| 161 | // each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 162 | |
| 163 | void |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 164 | OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 165 | { |
| 166 | int len = strlen (text); |
| 167 | std::string text_string (text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 168 | |
| 169 | // Force indentation to be reasonable. |
| 170 | if (indent >= output_max_columns) |
| 171 | indent = 0; |
| 172 | |
| 173 | // Will it all fit on one line? |
| 174 | |
| 175 | if (len + indent < output_max_columns) |
| 176 | // Output as a single line |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 177 | fprintf (out, "%*s%s\n", indent, "", text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | else |
| 179 | { |
| 180 | // We need to break it up into multiple lines. |
| 181 | int text_width = output_max_columns - indent - 1; |
| 182 | int start = 0; |
| 183 | int end = start; |
| 184 | int final_end = len; |
| 185 | int sub_len; |
| 186 | |
| 187 | while (end < final_end) |
| 188 | { |
| 189 | // Dont start the 'text' on a space, since we're already outputting the indentation. |
| 190 | while ((start < final_end) && (text[start] == ' ')) |
| 191 | start++; |
| 192 | |
| 193 | end = start + text_width; |
| 194 | if (end > final_end) |
| 195 | end = final_end; |
| 196 | else |
| 197 | { |
| 198 | // If we're not at the end of the text, make sure we break the line on white space. |
| 199 | while (end > start |
| 200 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 201 | end--; |
| 202 | } |
| 203 | sub_len = end - start; |
| 204 | std::string substring = text_string.substr (start, sub_len); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 205 | fprintf (out, "%*s%s\n", indent, "", substring.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 206 | start = end + 1; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 212 | ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 213 | { |
| 214 | uint32_t screen_width = 80; |
| 215 | uint32_t indent_level = 0; |
| 216 | const char *name = "lldb"; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 217 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 218 | fprintf (out, "\nUsage:\n\n"); |
| 219 | |
| 220 | indent_level += 2; |
| 221 | |
| 222 | |
| 223 | // First, show each usage level set of options, e.g. <cmd> [options-for-level-0] |
| 224 | // <cmd> [options-for-level-1] |
| 225 | // etc. |
| 226 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 227 | uint32_t num_options; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 228 | uint32_t num_option_sets = 0; |
| 229 | |
| 230 | for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 231 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 232 | uint32_t this_usage_mask = option_table[num_options].usage_mask; |
| 233 | if (this_usage_mask == LLDB_OPT_SET_ALL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 234 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 235 | if (num_option_sets == 0) |
| 236 | num_option_sets = 1; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 237 | } |
| 238 | else |
| 239 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 240 | for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 241 | { |
| 242 | if (this_usage_mask & 1 << j) |
| 243 | { |
| 244 | if (num_option_sets <= j) |
| 245 | num_option_sets = j + 1; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++) |
| 252 | { |
| 253 | uint32_t opt_set_mask; |
| 254 | |
| 255 | opt_set_mask = 1 << opt_set; |
| 256 | |
| 257 | if (opt_set > 0) |
| 258 | fprintf (out, "\n"); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 259 | fprintf (out, "%*s%s", indent_level, "", name); |
Jim Ingham | 556e653 | 2011-08-16 23:15:02 +0000 | [diff] [blame] | 260 | bool is_help_line = false; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 261 | |
| 262 | for (uint32_t i = 0; i < num_options; ++i) |
| 263 | { |
| 264 | if (option_table[i].usage_mask & opt_set_mask) |
| 265 | { |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 266 | CommandArgumentType arg_type = option_table[i].argument_type; |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 267 | const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type); |
Jim Ingham | 556e653 | 2011-08-16 23:15:02 +0000 | [diff] [blame] | 268 | // This is a bit of a hack, but there's no way to say certain options don't have arguments yet... |
| 269 | // so we do it by hand here. |
| 270 | if (option_table[i].short_option == 'h') |
| 271 | is_help_line = true; |
| 272 | |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 273 | if (option_table[i].required) |
| 274 | { |
| 275 | if (option_table[i].option_has_arg == required_argument) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 276 | fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 277 | else if (option_table[i].option_has_arg == optional_argument) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 278 | fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 279 | else |
| 280 | fprintf (out, " -%c", option_table[i].short_option); |
| 281 | } |
| 282 | else |
| 283 | { |
| 284 | if (option_table[i].option_has_arg == required_argument) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 285 | fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 286 | else if (option_table[i].option_has_arg == optional_argument) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 287 | fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 288 | else |
| 289 | fprintf (out, " [-%c]", option_table[i].short_option); |
| 290 | } |
| 291 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 292 | } |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 293 | if (!is_help_line && (opt_set <= last_option_set_with_args)) |
Jim Ingham | 556e653 | 2011-08-16 23:15:02 +0000 | [diff] [blame] | 294 | fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | fprintf (out, "\n\n"); |
| 298 | |
| 299 | // Now print out all the detailed information about the various options: long form, short form and help text: |
| 300 | // -- long_name <argument> |
| 301 | // - short <argument> |
| 302 | // help text |
| 303 | |
| 304 | // This variable is used to keep track of which options' info we've printed out, because some options can be in |
| 305 | // more than one usage level, but we only want to print the long form of its information once. |
| 306 | |
| 307 | Driver::OptionData::OptionSet options_seen; |
| 308 | Driver::OptionData::OptionSet::iterator pos; |
| 309 | |
| 310 | indent_level += 5; |
| 311 | |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 312 | for (uint32_t i = 0; i < num_options; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 313 | { |
| 314 | // Only print this option if we haven't already seen it. |
| 315 | pos = options_seen.find (option_table[i].short_option); |
| 316 | if (pos == options_seen.end()) |
| 317 | { |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 318 | CommandArgumentType arg_type = option_table[i].argument_type; |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 319 | const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type); |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 321 | options_seen.insert (option_table[i].short_option); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 322 | fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option); |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 323 | if (arg_type != eArgTypeNone) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 324 | fprintf (out, "<%s>", arg_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 325 | fprintf (out, "\n"); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 326 | fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option); |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 327 | if (arg_type != eArgTypeNone) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 328 | fprintf (out, "<%s>", arg_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 329 | fprintf (out, "\n"); |
| 330 | indent_level += 5; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 331 | OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 332 | indent_level -= 5; |
| 333 | fprintf (out, "\n"); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | indent_level -= 5; |
| 338 | |
Jim Ingham | a9deaf9 | 2011-08-16 23:57:58 +0000 | [diff] [blame] | 339 | fprintf (out, "\n%*s(If you don't provide -f then the first argument will be the file to be debugged" |
| 340 | "\n%*s so '%s -- <filename> [<ARG1> [<ARG2>]]' also works." |
| 341 | "\n%*s Remember to end the options with \"--\" if any of your arguments have a \"-\" in them.)\n\n", |
| 342 | indent_level, "", |
| 343 | indent_level, "", |
| 344 | name, |
| 345 | indent_level, ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | void |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 349 | BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table, |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 350 | uint32_t num_options) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 351 | { |
| 352 | if (num_options == 0) |
| 353 | return; |
| 354 | |
| 355 | uint32_t i; |
| 356 | uint32_t j; |
| 357 | std::bitset<256> option_seen; |
| 358 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 359 | getopt_table.resize (num_options + 1); |
| 360 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | for (i = 0, j = 0; i < num_options; ++i) |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 362 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 363 | char short_opt = expanded_option_table[i].short_option; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 365 | if (option_seen.test(short_opt) == false) |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 366 | { |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 367 | getopt_table[j].name = expanded_option_table[i].long_option; |
| 368 | getopt_table[j].has_arg = expanded_option_table[i].option_has_arg; |
| 369 | getopt_table[j].flag = NULL; |
| 370 | getopt_table[j].val = expanded_option_table[i].short_option; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 371 | option_seen.set(short_opt); |
| 372 | ++j; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 375 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 376 | getopt_table[j].name = NULL; |
| 377 | getopt_table[j].has_arg = 0; |
| 378 | getopt_table[j].flag = NULL; |
| 379 | getopt_table[j].val = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 380 | |
| 381 | } |
| 382 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 383 | Driver::OptionData::OptionData () : |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 384 | m_args(), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 385 | m_script_lang (lldb::eScriptLanguageDefault), |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 386 | m_core_file (), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 387 | m_crash_log (), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 388 | m_source_command_files (), |
| 389 | m_debug_mode (false), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 390 | m_print_version (false), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 391 | m_print_help (false), |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 392 | m_wait_for(false), |
| 393 | m_process_name(), |
| 394 | m_process_pid(LLDB_INVALID_PROCESS_ID), |
Daniel Dunbar | a08823f | 2011-10-31 22:50:49 +0000 | [diff] [blame] | 395 | m_use_external_editor(false), |
Stephen Wilson | 71c21d1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 396 | m_seen_options() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 397 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | Driver::OptionData::~OptionData () |
| 401 | { |
| 402 | } |
| 403 | |
| 404 | void |
| 405 | Driver::OptionData::Clear () |
| 406 | { |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 407 | m_args.clear (); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 408 | m_script_lang = lldb::eScriptLanguageDefault; |
| 409 | m_source_command_files.clear (); |
| 410 | m_debug_mode = false; |
| 411 | m_print_help = false; |
| 412 | m_print_version = false; |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 413 | m_use_external_editor = false; |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 414 | m_wait_for = false; |
| 415 | m_process_name.erase(); |
| 416 | m_process_pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | void |
| 420 | Driver::ResetOptionValues () |
| 421 | { |
| 422 | m_option_data.Clear (); |
| 423 | } |
| 424 | |
| 425 | const char * |
| 426 | Driver::GetFilename() const |
| 427 | { |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 428 | if (m_option_data.m_args.empty()) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 429 | return NULL; |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 430 | return m_option_data.m_args.front().c_str(); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | const char * |
| 434 | Driver::GetCrashLogFilename() const |
| 435 | { |
| 436 | if (m_option_data.m_crash_log.empty()) |
| 437 | return NULL; |
| 438 | return m_option_data.m_crash_log.c_str(); |
| 439 | } |
| 440 | |
| 441 | lldb::ScriptLanguage |
| 442 | Driver::GetScriptLanguage() const |
| 443 | { |
| 444 | return m_option_data.m_script_lang; |
| 445 | } |
| 446 | |
| 447 | size_t |
| 448 | Driver::GetNumSourceCommandFiles () const |
| 449 | { |
| 450 | return m_option_data.m_source_command_files.size(); |
| 451 | } |
| 452 | |
| 453 | const char * |
| 454 | Driver::GetSourceCommandFileAtIndex (uint32_t idx) const |
| 455 | { |
| 456 | if (idx < m_option_data.m_source_command_files.size()) |
| 457 | return m_option_data.m_source_command_files[idx].c_str(); |
| 458 | return NULL; |
| 459 | } |
| 460 | |
| 461 | bool |
| 462 | Driver::GetDebugMode() const |
| 463 | { |
| 464 | return m_option_data.m_debug_mode; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | // Check the arguments that were passed to this program to make sure they are valid and to get their |
| 469 | // argument values (if any). Return a boolean value indicating whether or not to start up the full |
| 470 | // debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR |
| 471 | // if the user only wanted help or version information. |
| 472 | |
| 473 | SBError |
| 474 | Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit) |
| 475 | { |
| 476 | ResetOptionValues (); |
| 477 | |
| 478 | SBCommandReturnObject result; |
| 479 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 480 | SBError error; |
| 481 | std::string option_string; |
| 482 | struct option *long_options = NULL; |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 483 | std::vector<struct option> long_options_vector; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 484 | uint32_t num_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 485 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 486 | for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options) |
| 487 | /* Do Nothing. */; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 488 | |
| 489 | if (num_options == 0) |
| 490 | { |
| 491 | if (argc > 1) |
| 492 | error.SetErrorStringWithFormat ("invalid number of options"); |
| 493 | return error; |
| 494 | } |
| 495 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 496 | BuildGetOptTable (g_options, long_options_vector, num_options); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 497 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 498 | if (long_options_vector.empty()) |
| 499 | long_options = NULL; |
| 500 | else |
| 501 | long_options = &long_options_vector.front(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 502 | |
| 503 | if (long_options == NULL) |
| 504 | { |
| 505 | error.SetErrorStringWithFormat ("invalid long options"); |
| 506 | return error; |
| 507 | } |
| 508 | |
| 509 | // Build the option_string argument for call to getopt_long. |
| 510 | |
| 511 | for (int i = 0; long_options[i].name != NULL; ++i) |
| 512 | { |
| 513 | if (long_options[i].flag == NULL) |
| 514 | { |
| 515 | option_string.push_back ((char) long_options[i].val); |
| 516 | switch (long_options[i].has_arg) |
| 517 | { |
| 518 | default: |
| 519 | case no_argument: |
| 520 | break; |
| 521 | case required_argument: |
| 522 | option_string.push_back (':'); |
| 523 | break; |
| 524 | case optional_argument: |
| 525 | option_string.append ("::"); |
| 526 | break; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 531 | // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't |
| 532 | // know at that point whether we should read in init files yet. So we don't read them in in the |
| 533 | // Driver constructor, then set the flags back to "read them in" here, and then if we see the |
| 534 | // "-n" flag, we'll turn it off again. Finally we have to read them in by hand later in the |
| 535 | // main loop. |
| 536 | |
| 537 | m_debugger.SkipLLDBInitFiles (false); |
| 538 | m_debugger.SkipAppInitFiles (false); |
| 539 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 540 | // Prepare for & make calls to getopt_long. |
Eli Friedman | adb3502 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 541 | #if __GLIBC__ |
| 542 | optind = 0; |
| 543 | #else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 544 | optreset = 1; |
| 545 | optind = 1; |
Eli Friedman | adb3502 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 546 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 547 | int val; |
| 548 | while (1) |
| 549 | { |
| 550 | int long_options_index = -1; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 551 | val = ::getopt_long (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 552 | |
| 553 | if (val == -1) |
| 554 | break; |
| 555 | else if (val == '?') |
| 556 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 557 | m_option_data.m_print_help = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 558 | error.SetErrorStringWithFormat ("unknown or ambiguous option"); |
| 559 | break; |
| 560 | } |
| 561 | else if (val == 0) |
| 562 | continue; |
| 563 | else |
| 564 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 565 | m_option_data.m_seen_options.insert ((char) val); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 566 | if (long_options_index == -1) |
| 567 | { |
| 568 | for (int i = 0; |
| 569 | long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val; |
| 570 | ++i) |
| 571 | { |
| 572 | if (long_options[i].val == val) |
| 573 | { |
| 574 | long_options_index = i; |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | if (long_options_index >= 0) |
| 581 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 582 | const int short_option = g_options[long_options_index].short_option; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 583 | |
| 584 | switch (short_option) |
| 585 | { |
| 586 | case 'h': |
| 587 | m_option_data.m_print_help = true; |
| 588 | break; |
| 589 | |
| 590 | case 'v': |
| 591 | m_option_data.m_print_version = true; |
| 592 | break; |
| 593 | |
| 594 | case 'c': |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 595 | { |
| 596 | SBFileSpec file(optarg); |
| 597 | if (file.Exists()) |
| 598 | { |
| 599 | m_option_data.m_core_file = optarg; |
| 600 | } |
| 601 | else |
| 602 | error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg); |
| 603 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 604 | break; |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 605 | |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 606 | case 'e': |
| 607 | m_option_data.m_use_external_editor = true; |
| 608 | break; |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 609 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 610 | case 'x': |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 611 | m_debugger.SkipLLDBInitFiles (true); |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 612 | m_debugger.SkipAppInitFiles (true); |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 613 | break; |
| 614 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 615 | case 'f': |
| 616 | { |
| 617 | SBFileSpec file(optarg); |
| 618 | if (file.Exists()) |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 619 | { |
| 620 | m_option_data.m_args.push_back (optarg); |
| 621 | } |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 622 | else if (file.ResolveExecutableLocation()) |
| 623 | { |
| 624 | char path[PATH_MAX]; |
Johnny Chen | 25f3a3c | 2011-08-10 22:06:24 +0000 | [diff] [blame] | 625 | file.GetPath (path, sizeof(path)); |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 626 | m_option_data.m_args.push_back (path); |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 627 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 628 | else |
| 629 | error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg); |
| 630 | } |
| 631 | break; |
| 632 | |
| 633 | case 'a': |
| 634 | if (!m_debugger.SetDefaultArchitecture (optarg)) |
| 635 | error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg); |
| 636 | break; |
| 637 | |
| 638 | case 'l': |
| 639 | m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg); |
| 640 | break; |
| 641 | |
| 642 | case 'd': |
| 643 | m_option_data.m_debug_mode = true; |
| 644 | break; |
| 645 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 646 | case 'n': |
| 647 | m_option_data.m_process_name = optarg; |
| 648 | break; |
| 649 | |
| 650 | case 'w': |
| 651 | m_option_data.m_wait_for = true; |
| 652 | break; |
| 653 | |
| 654 | case 'p': |
| 655 | { |
| 656 | char *remainder; |
| 657 | m_option_data.m_process_pid = strtol (optarg, &remainder, 0); |
| 658 | if (remainder == optarg || *remainder != '\0') |
| 659 | error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.", |
| 660 | optarg); |
| 661 | } |
| 662 | break; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 663 | case 's': |
| 664 | { |
| 665 | SBFileSpec file(optarg); |
| 666 | if (file.Exists()) |
| 667 | m_option_data.m_source_command_files.push_back (optarg); |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 668 | else if (file.ResolveExecutableLocation()) |
| 669 | { |
| 670 | char final_path[PATH_MAX]; |
Johnny Chen | 25f3a3c | 2011-08-10 22:06:24 +0000 | [diff] [blame] | 671 | file.GetPath (final_path, sizeof(final_path)); |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 672 | std::string path_str (final_path); |
| 673 | m_option_data.m_source_command_files.push_back (path_str); |
| 674 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 675 | else |
| 676 | error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg); |
| 677 | } |
| 678 | break; |
| 679 | |
| 680 | default: |
| 681 | m_option_data.m_print_help = true; |
| 682 | error.SetErrorStringWithFormat ("unrecognized option %c", short_option); |
| 683 | break; |
| 684 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 685 | } |
| 686 | else |
| 687 | { |
| 688 | error.SetErrorStringWithFormat ("invalid option with value %i", val); |
| 689 | } |
| 690 | if (error.Fail()) |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 691 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 692 | return error; |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 693 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 694 | } |
| 695 | } |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 696 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 697 | if (error.Fail() || m_option_data.m_print_help) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 698 | { |
| 699 | ShowUsage (out_fh, g_options, m_option_data); |
Greg Clayton | e46dd32 | 2010-10-11 01:13:37 +0000 | [diff] [blame] | 700 | exit = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 701 | } |
| 702 | else if (m_option_data.m_print_version) |
| 703 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 704 | ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString()); |
| 705 | exit = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 706 | } |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 707 | else if (m_option_data.m_process_name.empty() && m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 708 | { |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 709 | // Any arguments that are left over after option parsing are for |
| 710 | // the program. If a file was specified with -f then the filename |
| 711 | // is already in the m_option_data.m_args array, and any remaining args |
| 712 | // are arguments for the inferior program. If no file was specified with |
| 713 | // -f, then what is left is the program name followed by any arguments. |
| 714 | |
| 715 | // Skip any options we consumed with getopt_long |
| 716 | argc -= optind; |
| 717 | argv += optind; |
| 718 | |
| 719 | if (argc > 0) |
| 720 | { |
| 721 | for (int arg_idx=0; arg_idx<argc; ++arg_idx) |
| 722 | { |
| 723 | const char *arg = argv[arg_idx]; |
| 724 | if (arg) |
| 725 | m_option_data.m_args.push_back (arg); |
| 726 | } |
| 727 | } |
| 728 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 729 | } |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 730 | else |
| 731 | { |
| 732 | // Skip any options we consumed with getopt_long |
| 733 | argc -= optind; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 734 | //argv += optind; // Commented out to keep static analyzer happy |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 735 | |
| 736 | if (argc > 0) |
| 737 | ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n"); |
| 738 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 739 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 740 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 743 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 744 | Driver::GetProcessSTDOUT () |
| 745 | { |
| 746 | // The process has stuff waiting for stdout; get it and write it out to the appropriate place. |
| 747 | char stdio_buffer[1024]; |
| 748 | size_t len; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 749 | size_t total_bytes = 0; |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 750 | while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0) |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 751 | { |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 752 | m_io_channel_ap->OutWrite (stdio_buffer, len, ASYNC); |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 753 | total_bytes += len; |
| 754 | } |
| 755 | return total_bytes; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 758 | size_t |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 759 | Driver::GetProcessSTDERR () |
| 760 | { |
| 761 | // The process has stuff waiting for stderr; get it and write it out to the appropriate place. |
| 762 | char stdio_buffer[1024]; |
| 763 | size_t len; |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 764 | size_t total_bytes = 0; |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 765 | while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0) |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 766 | { |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 767 | m_io_channel_ap->ErrWrite (stdio_buffer, len, ASYNC); |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 768 | total_bytes += len; |
| 769 | } |
| 770 | return total_bytes; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | void |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 774 | Driver::UpdateSelectedThread () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 775 | { |
| 776 | using namespace lldb; |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 777 | SBProcess process(m_debugger.GetSelectedTarget().GetProcess()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 778 | if (process.IsValid()) |
| 779 | { |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 780 | SBThread curr_thread (process.GetSelectedThread()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 781 | SBThread thread; |
| 782 | StopReason curr_thread_stop_reason = eStopReasonInvalid; |
| 783 | curr_thread_stop_reason = curr_thread.GetStopReason(); |
| 784 | |
| 785 | if (!curr_thread.IsValid() || |
| 786 | curr_thread_stop_reason == eStopReasonInvalid || |
| 787 | curr_thread_stop_reason == eStopReasonNone) |
| 788 | { |
| 789 | // Prefer a thread that has just completed its plan over another thread as current thread. |
| 790 | SBThread plan_thread; |
| 791 | SBThread other_thread; |
| 792 | const size_t num_threads = process.GetNumThreads(); |
| 793 | size_t i; |
| 794 | for (i = 0; i < num_threads; ++i) |
| 795 | { |
| 796 | thread = process.GetThreadAtIndex(i); |
| 797 | StopReason thread_stop_reason = thread.GetStopReason(); |
| 798 | switch (thread_stop_reason) |
| 799 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 800 | case eStopReasonInvalid: |
| 801 | case eStopReasonNone: |
| 802 | break; |
| 803 | |
| 804 | case eStopReasonTrace: |
| 805 | case eStopReasonBreakpoint: |
| 806 | case eStopReasonWatchpoint: |
| 807 | case eStopReasonSignal: |
| 808 | case eStopReasonException: |
Greg Clayton | 90ba811 | 2012-12-05 00:16:59 +0000 | [diff] [blame] | 809 | case eStopReasonExec: |
Andrew Kaylor | f85defa | 2012-12-20 23:08:03 +0000 | [diff] [blame] | 810 | case eStopReasonThreadExiting: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 811 | if (!other_thread.IsValid()) |
| 812 | other_thread = thread; |
| 813 | break; |
| 814 | case eStopReasonPlanComplete: |
| 815 | if (!plan_thread.IsValid()) |
| 816 | plan_thread = thread; |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | if (plan_thread.IsValid()) |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 821 | process.SetSelectedThread (plan_thread); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 822 | else if (other_thread.IsValid()) |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 823 | process.SetSelectedThread (other_thread); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 824 | else |
| 825 | { |
| 826 | if (curr_thread.IsValid()) |
| 827 | thread = curr_thread; |
| 828 | else |
| 829 | thread = process.GetThreadAtIndex(0); |
| 830 | |
| 831 | if (thread.IsValid()) |
Jim Ingham | 2976d00 | 2010-08-26 21:32:51 +0000 | [diff] [blame] | 832 | process.SetSelectedThread (thread); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 838 | // This function handles events that were broadcast by the process. |
| 839 | void |
| 840 | Driver::HandleBreakpointEvent (const SBEvent &event) |
| 841 | { |
| 842 | using namespace lldb; |
| 843 | const uint32_t event_type = SBBreakpoint::GetBreakpointEventTypeFromEvent (event); |
| 844 | |
| 845 | if (event_type & eBreakpointEventTypeAdded |
| 846 | || event_type & eBreakpointEventTypeRemoved |
| 847 | || event_type & eBreakpointEventTypeEnabled |
| 848 | || event_type & eBreakpointEventTypeDisabled |
| 849 | || event_type & eBreakpointEventTypeCommandChanged |
| 850 | || event_type & eBreakpointEventTypeConditionChanged |
| 851 | || event_type & eBreakpointEventTypeIgnoreChanged |
| 852 | || event_type & eBreakpointEventTypeLocationsResolved) |
| 853 | { |
| 854 | // Don't do anything about these events, since the breakpoint commands already echo these actions. |
| 855 | } |
| 856 | else if (event_type & eBreakpointEventTypeLocationsAdded) |
| 857 | { |
| 858 | char message[256]; |
| 859 | uint32_t num_new_locations = SBBreakpoint::GetNumBreakpointLocationsFromEvent(event); |
| 860 | if (num_new_locations > 0) |
| 861 | { |
| 862 | SBBreakpoint breakpoint = SBBreakpoint::GetBreakpointFromEvent(event); |
Jim Ingham | fab10e8 | 2012-03-06 00:37:27 +0000 | [diff] [blame] | 863 | int message_len = ::snprintf (message, sizeof(message), "%d location%s added to breakpoint %d\n", |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 864 | num_new_locations, |
Jason Molenda | 65c28cb | 2012-09-28 01:50:47 +0000 | [diff] [blame] | 865 | num_new_locations == 1 ? "" : "s", |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 866 | breakpoint.GetID()); |
| 867 | m_io_channel_ap->OutWrite(message, message_len, ASYNC); |
| 868 | } |
| 869 | } |
| 870 | else if (event_type & eBreakpointEventTypeLocationsRemoved) |
| 871 | { |
| 872 | // These locations just get disabled, not sure it is worth spamming folks about this on the command line. |
| 873 | } |
| 874 | else if (event_type & eBreakpointEventTypeLocationsResolved) |
| 875 | { |
| 876 | // This might be an interesting thing to note, but I'm going to leave it quiet for now, it just looked noisy. |
| 877 | } |
| 878 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 879 | |
| 880 | // This function handles events that were broadcast by the process. |
| 881 | void |
| 882 | Driver::HandleProcessEvent (const SBEvent &event) |
| 883 | { |
| 884 | using namespace lldb; |
| 885 | const uint32_t event_type = event.GetType(); |
| 886 | |
| 887 | if (event_type & SBProcess::eBroadcastBitSTDOUT) |
| 888 | { |
| 889 | // The process has stdout available, get it and write it out to the |
| 890 | // appropriate place. |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 891 | GetProcessSTDOUT (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 892 | } |
| 893 | else if (event_type & SBProcess::eBroadcastBitSTDERR) |
| 894 | { |
| 895 | // The process has stderr available, get it and write it out to the |
| 896 | // appropriate place. |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 897 | GetProcessSTDERR (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 898 | } |
| 899 | else if (event_type & SBProcess::eBroadcastBitStateChanged) |
| 900 | { |
| 901 | // Drain all stout and stderr so we don't see any output come after |
| 902 | // we print our prompts |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 903 | GetProcessSTDOUT (); |
| 904 | GetProcessSTDERR (); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 905 | // Something changed in the process; get the event and report the process's current status and location to |
| 906 | // the user. |
| 907 | StateType event_state = SBProcess::GetStateFromEvent (event); |
| 908 | if (event_state == eStateInvalid) |
| 909 | return; |
| 910 | |
| 911 | SBProcess process (SBProcess::GetProcessFromEvent (event)); |
| 912 | assert (process.IsValid()); |
| 913 | |
| 914 | switch (event_state) |
| 915 | { |
| 916 | case eStateInvalid: |
| 917 | case eStateUnloaded: |
Greg Clayton | b766a73 | 2011-02-04 01:58:07 +0000 | [diff] [blame] | 918 | case eStateConnected: |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 919 | case eStateAttaching: |
| 920 | case eStateLaunching: |
| 921 | case eStateStepping: |
| 922 | case eStateDetached: |
| 923 | { |
| 924 | char message[1024]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 925 | int message_len = ::snprintf (message, sizeof(message), "Process %" PRIu64 " %s\n", process.GetProcessID(), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 926 | m_debugger.StateAsCString (event_state)); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 927 | m_io_channel_ap->OutWrite(message, message_len, ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 928 | } |
| 929 | break; |
| 930 | |
| 931 | case eStateRunning: |
| 932 | // Don't be chatty when we run... |
| 933 | break; |
| 934 | |
| 935 | case eStateExited: |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 936 | { |
| 937 | SBCommandReturnObject result; |
| 938 | m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 939 | m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize(), ASYNC); |
| 940 | m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), ASYNC); |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 941 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 942 | break; |
| 943 | |
| 944 | case eStateStopped: |
| 945 | case eStateCrashed: |
| 946 | case eStateSuspended: |
| 947 | // Make sure the program hasn't been auto-restarted: |
| 948 | if (SBProcess::GetRestartedFromEvent (event)) |
| 949 | { |
| 950 | // FIXME: Do we want to report this, or would that just be annoyingly chatty? |
| 951 | char message[1024]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 952 | int message_len = ::snprintf (message, sizeof(message), "Process %" PRIu64 " stopped and was programmatically restarted.\n", |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 953 | process.GetProcessID()); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 954 | m_io_channel_ap->OutWrite(message, message_len, ASYNC); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 955 | } |
| 956 | else |
| 957 | { |
Jim Ingham | 8499e1a | 2012-05-08 23:06:07 +0000 | [diff] [blame] | 958 | if (GetDebugger().GetSelectedTarget() == process.GetTarget()) |
| 959 | { |
| 960 | SBCommandReturnObject result; |
| 961 | UpdateSelectedThread (); |
| 962 | m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false); |
| 963 | m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize(), ASYNC); |
| 964 | m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize(), ASYNC); |
| 965 | } |
| 966 | else |
| 967 | { |
| 968 | SBStream out_stream; |
| 969 | uint32_t target_idx = GetDebugger().GetIndexOfTarget(process.GetTarget()); |
| 970 | if (target_idx != UINT32_MAX) |
| 971 | out_stream.Printf ("Target %d: (", target_idx); |
| 972 | else |
| 973 | out_stream.Printf ("Target <unknown index>: ("); |
| 974 | process.GetTarget().GetDescription (out_stream, eDescriptionLevelBrief); |
| 975 | out_stream.Printf (") stopped.\n"); |
| 976 | m_io_channel_ap->OutWrite (out_stream.GetData(), out_stream.GetSize(), ASYNC); |
| 977 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 978 | } |
| 979 | break; |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 984 | void |
| 985 | Driver::HandleThreadEvent (const SBEvent &event) |
| 986 | { |
| 987 | // At present the only thread event we handle is the Frame Changed event, and all we do for that is just |
| 988 | // reprint the thread status for that thread. |
| 989 | using namespace lldb; |
| 990 | const uint32_t event_type = event.GetType(); |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 991 | if (event_type == SBThread::eBroadcastBitStackChanged |
| 992 | || event_type == SBThread::eBroadcastBitThreadSelected) |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 993 | { |
| 994 | SBThread thread = SBThread::GetThreadFromEvent (event); |
| 995 | if (thread.IsValid()) |
| 996 | { |
| 997 | SBStream out_stream; |
| 998 | thread.GetStatus(out_stream); |
| 999 | m_io_channel_ap->OutWrite (out_stream.GetData (), out_stream.GetSize (), ASYNC); |
| 1000 | } |
| 1001 | } |
| 1002 | } |
| 1003 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1004 | // This function handles events broadcast by the IOChannel (HasInput, UserInterrupt, or ThreadShouldExit). |
| 1005 | |
| 1006 | bool |
| 1007 | Driver::HandleIOEvent (const SBEvent &event) |
| 1008 | { |
| 1009 | bool quit = false; |
| 1010 | |
| 1011 | const uint32_t event_type = event.GetType(); |
| 1012 | |
| 1013 | if (event_type & IOChannel::eBroadcastBitHasUserInput) |
| 1014 | { |
| 1015 | // We got some input (i.e. a command string) from the user; pass it off to the command interpreter for |
| 1016 | // handling. |
| 1017 | |
| 1018 | const char *command_string = SBEvent::GetCStringFromEvent(event); |
| 1019 | if (command_string == NULL) |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 1020 | command_string = ""; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1021 | SBCommandReturnObject result; |
Jim Ingham | 85e8b81 | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 1022 | |
Caroline Tice | b5059ac | 2011-05-16 19:20:50 +0000 | [diff] [blame] | 1023 | // We don't want the result to bypass the OutWrite function in IOChannel, as this can result in odd |
| 1024 | // output orderings and problems with the prompt. |
Jim Ingham | 85e8b81 | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 1025 | m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, true); |
| 1026 | |
Enrico Granata | cd4d24d | 2012-10-16 20:57:12 +0000 | [diff] [blame] | 1027 | const bool only_if_no_immediate = true; |
| 1028 | |
Enrico Granata | 430e540 | 2012-10-16 21:11:14 +0000 | [diff] [blame] | 1029 | const size_t output_size = result.GetOutputSize(); |
Enrico Granata | cd4d24d | 2012-10-16 20:57:12 +0000 | [diff] [blame] | 1030 | |
| 1031 | if (output_size > 0) |
| 1032 | m_io_channel_ap->OutWrite (result.GetOutput(only_if_no_immediate), output_size, NO_ASYNC); |
| 1033 | |
Enrico Granata | 430e540 | 2012-10-16 21:11:14 +0000 | [diff] [blame] | 1034 | const size_t error_size = result.GetErrorSize(); |
Enrico Granata | cd4d24d | 2012-10-16 20:57:12 +0000 | [diff] [blame] | 1035 | |
| 1036 | if (error_size > 0) |
| 1037 | m_io_channel_ap->OutWrite (result.GetError(only_if_no_immediate), error_size, NO_ASYNC); |
Caroline Tice | b5059ac | 2011-05-16 19:20:50 +0000 | [diff] [blame] | 1038 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1039 | // We are done getting and running our command, we can now clear the |
| 1040 | // m_waiting_for_command so we can get another one. |
| 1041 | m_waiting_for_command = false; |
| 1042 | |
| 1043 | // If our editline input reader is active, it means another input reader |
| 1044 | // got pushed onto the input reader and caused us to become deactivated. |
| 1045 | // When the input reader above us gets popped, we will get re-activated |
| 1046 | // and our prompt will refresh in our callback |
| 1047 | if (m_editline_reader.IsActive()) |
| 1048 | { |
| 1049 | ReadyForCommand (); |
| 1050 | } |
| 1051 | } |
| 1052 | else if (event_type & IOChannel::eBroadcastBitUserInterrupt) |
| 1053 | { |
| 1054 | // This is here to handle control-c interrupts from the user. It has not yet really been implemented. |
| 1055 | // TO BE DONE: PROPERLY HANDLE CONTROL-C FROM USER |
| 1056 | //m_io_channel_ap->CancelInput(); |
| 1057 | // Anything else? Send Interrupt to process? |
| 1058 | } |
| 1059 | else if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) || |
| 1060 | (event_type & IOChannel::eBroadcastBitThreadDidExit)) |
| 1061 | { |
| 1062 | // If the IOChannel thread is trying to go away, then it is definitely |
| 1063 | // time to end the debugging session. |
| 1064 | quit = true; |
| 1065 | } |
| 1066 | |
| 1067 | return quit; |
| 1068 | } |
| 1069 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1070 | void |
| 1071 | Driver::MasterThreadBytesReceived (void *baton, const void *src, size_t src_len) |
| 1072 | { |
| 1073 | Driver *driver = (Driver*)baton; |
| 1074 | driver->GetFromMaster ((const char *)src, src_len); |
| 1075 | } |
| 1076 | |
| 1077 | void |
| 1078 | Driver::GetFromMaster (const char *src, size_t src_len) |
| 1079 | { |
| 1080 | // Echo the characters back to the Debugger's stdout, that way if you |
| 1081 | // type characters while a command is running, you'll see what you've typed. |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1082 | FILE *out_fh = m_debugger.GetOutputFileHandle(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1083 | if (out_fh) |
| 1084 | ::fwrite (src, 1, src_len, out_fh); |
| 1085 | } |
| 1086 | |
| 1087 | size_t |
| 1088 | Driver::EditLineInputReaderCallback |
| 1089 | ( |
| 1090 | void *baton, |
| 1091 | SBInputReader *reader, |
| 1092 | InputReaderAction notification, |
| 1093 | const char *bytes, |
| 1094 | size_t bytes_len |
| 1095 | ) |
| 1096 | { |
| 1097 | Driver *driver = (Driver *)baton; |
| 1098 | |
| 1099 | switch (notification) |
| 1100 | { |
| 1101 | case eInputReaderActivate: |
| 1102 | break; |
| 1103 | |
| 1104 | case eInputReaderReactivate: |
| 1105 | driver->ReadyForCommand(); |
| 1106 | break; |
| 1107 | |
| 1108 | case eInputReaderDeactivate: |
| 1109 | break; |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1110 | |
| 1111 | case eInputReaderAsynchronousOutputWritten: |
| 1112 | if (driver->m_io_channel_ap.get() != NULL) |
| 1113 | driver->m_io_channel_ap->RefreshPrompt(); |
| 1114 | break; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1115 | |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1116 | case eInputReaderInterrupt: |
| 1117 | if (driver->m_io_channel_ap.get() != NULL) |
| 1118 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1119 | SBProcess process(driver->GetDebugger().GetSelectedTarget().GetProcess()); |
Jim Ingham | d242f1c | 2012-06-01 01:07:02 +0000 | [diff] [blame] | 1120 | if (!driver->m_io_channel_ap->EditLineHasCharacters() |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1121 | && process.IsValid() |
| 1122 | && (process.GetState() == lldb::eStateRunning || process.GetState() == lldb::eStateAttaching)) |
Jim Ingham | d242f1c | 2012-06-01 01:07:02 +0000 | [diff] [blame] | 1123 | { |
Jim Ingham | cfc0935 | 2012-07-27 23:57:19 +0000 | [diff] [blame] | 1124 | process.SendAsyncInterrupt (); |
Jim Ingham | d242f1c | 2012-06-01 01:07:02 +0000 | [diff] [blame] | 1125 | } |
| 1126 | else |
| 1127 | { |
| 1128 | driver->m_io_channel_ap->OutWrite ("^C\n", 3, NO_ASYNC); |
| 1129 | // I wish I could erase the entire input line, but there's no public API for that. |
| 1130 | driver->m_io_channel_ap->EraseCharsBeforeCursor(); |
| 1131 | driver->m_io_channel_ap->RefreshPrompt(); |
| 1132 | } |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1133 | } |
| 1134 | break; |
| 1135 | |
| 1136 | case eInputReaderEndOfFile: |
| 1137 | if (driver->m_io_channel_ap.get() != NULL) |
| 1138 | { |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1139 | driver->m_io_channel_ap->OutWrite ("^D\n", 3, NO_ASYNC); |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1140 | driver->m_io_channel_ap->RefreshPrompt (); |
| 1141 | } |
| 1142 | write (driver->m_editline_pty.GetMasterFileDescriptor(), "quit\n", 5); |
| 1143 | break; |
| 1144 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1145 | case eInputReaderGotToken: |
| 1146 | write (driver->m_editline_pty.GetMasterFileDescriptor(), bytes, bytes_len); |
| 1147 | break; |
| 1148 | |
| 1149 | case eInputReaderDone: |
| 1150 | break; |
| 1151 | } |
| 1152 | return bytes_len; |
| 1153 | } |
| 1154 | |
| 1155 | void |
| 1156 | Driver::MainLoop () |
| 1157 | { |
| 1158 | char error_str[1024]; |
| 1159 | if (m_editline_pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, error_str, sizeof(error_str)) == false) |
| 1160 | { |
| 1161 | ::fprintf (stderr, "error: failed to open driver pseudo terminal : %s", error_str); |
| 1162 | exit(1); |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | const char *driver_slave_name = m_editline_pty.GetSlaveName (error_str, sizeof(error_str)); |
| 1167 | if (driver_slave_name == NULL) |
| 1168 | { |
| 1169 | ::fprintf (stderr, "error: failed to get slave name for driver pseudo terminal : %s", error_str); |
| 1170 | exit(2); |
| 1171 | } |
| 1172 | else |
| 1173 | { |
| 1174 | m_editline_slave_fh = ::fopen (driver_slave_name, "r+"); |
| 1175 | if (m_editline_slave_fh == NULL) |
| 1176 | { |
| 1177 | SBError error; |
| 1178 | error.SetErrorToErrno(); |
| 1179 | ::fprintf (stderr, "error: failed to get open slave for driver pseudo terminal : %s", |
| 1180 | error.GetCString()); |
| 1181 | exit(3); |
| 1182 | } |
| 1183 | |
| 1184 | ::setbuf (m_editline_slave_fh, NULL); |
| 1185 | } |
| 1186 | } |
| 1187 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1188 | lldb_utility::PseudoTerminal editline_output_pty; |
| 1189 | FILE *editline_output_slave_fh = NULL; |
| 1190 | |
| 1191 | if (editline_output_pty.OpenFirstAvailableMaster (O_RDWR|O_NOCTTY, error_str, sizeof (error_str)) == false) |
| 1192 | { |
| 1193 | ::fprintf (stderr, "error: failed to open output pseudo terminal : %s", error_str); |
| 1194 | exit(1); |
| 1195 | } |
| 1196 | else |
| 1197 | { |
| 1198 | const char *output_slave_name = editline_output_pty.GetSlaveName (error_str, sizeof(error_str)); |
| 1199 | if (output_slave_name == NULL) |
| 1200 | { |
| 1201 | ::fprintf (stderr, "error: failed to get slave name for output pseudo terminal : %s", error_str); |
| 1202 | exit(2); |
| 1203 | } |
| 1204 | else |
| 1205 | { |
| 1206 | editline_output_slave_fh = ::fopen (output_slave_name, "r+"); |
| 1207 | if (editline_output_slave_fh == NULL) |
| 1208 | { |
| 1209 | SBError error; |
| 1210 | error.SetErrorToErrno(); |
| 1211 | ::fprintf (stderr, "error: failed to get open slave for output pseudo terminal : %s", |
| 1212 | error.GetCString()); |
| 1213 | exit(3); |
| 1214 | } |
| 1215 | ::setbuf (editline_output_slave_fh, NULL); |
| 1216 | } |
| 1217 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1218 | |
| 1219 | // struct termios stdin_termios; |
| 1220 | |
| 1221 | if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0) |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 1222 | { |
| 1223 | g_old_stdin_termios_is_valid = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1224 | atexit (reset_stdin_termios); |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 1225 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1226 | |
| 1227 | ::setbuf (stdin, NULL); |
| 1228 | ::setbuf (stdout, NULL); |
| 1229 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1230 | m_debugger.SetErrorFileHandle (stderr, false); |
| 1231 | m_debugger.SetOutputFileHandle (stdout, false); |
| 1232 | m_debugger.SetInputFileHandle (stdin, true); |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 1233 | |
| 1234 | m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1235 | |
| 1236 | // You have to drain anything that comes to the master side of the PTY. master_out_comm is |
| 1237 | // for that purpose. The reason you need to do this is a curious reason... editline will echo |
| 1238 | // characters to the PTY when it gets characters while el_gets is not running, and then when |
| 1239 | // you call el_gets (or el_getc) it will try to reset the terminal back to raw mode which blocks |
| 1240 | // if there are unconsumed characters in the out buffer. |
| 1241 | // However, you don't need to do anything with the characters, since editline will dump these |
| 1242 | // unconsumed characters after printing the prompt again in el_gets. |
| 1243 | |
Greg Clayton | d46c87a | 2010-12-04 02:39:47 +0000 | [diff] [blame] | 1244 | SBCommunication master_out_comm("driver.editline"); |
| 1245 | master_out_comm.SetCloseOnEOF (false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1246 | master_out_comm.AdoptFileDesriptor(m_editline_pty.GetMasterFileDescriptor(), false); |
| 1247 | master_out_comm.SetReadThreadBytesReceivedCallback(Driver::MasterThreadBytesReceived, this); |
| 1248 | |
| 1249 | if (master_out_comm.ReadThreadStart () == false) |
| 1250 | { |
| 1251 | ::fprintf (stderr, "error: failed to start master out read thread"); |
| 1252 | exit(5); |
| 1253 | } |
| 1254 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1255 | SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1256 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 1257 | m_io_channel_ap.reset (new IOChannel(m_editline_slave_fh, editline_output_slave_fh, stdout, stderr, this)); |
| 1258 | |
| 1259 | SBCommunication out_comm_2("driver.editline_output"); |
| 1260 | out_comm_2.SetCloseOnEOF (false); |
| 1261 | out_comm_2.AdoptFileDesriptor (editline_output_pty.GetMasterFileDescriptor(), false); |
| 1262 | out_comm_2.SetReadThreadBytesReceivedCallback (IOChannel::LibeditOutputBytesReceived, m_io_channel_ap.get()); |
| 1263 | |
| 1264 | if (out_comm_2.ReadThreadStart () == false) |
| 1265 | { |
| 1266 | ::fprintf (stderr, "error: failed to start libedit output read thread"); |
| 1267 | exit (5); |
| 1268 | } |
| 1269 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1270 | |
| 1271 | struct winsize window_size; |
| 1272 | if (isatty (STDIN_FILENO) |
| 1273 | && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0) |
| 1274 | { |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 1275 | if (window_size.ws_col > 0) |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 1276 | m_debugger.SetTerminalWidth (window_size.ws_col); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | // Since input can be redirected by the debugger, we must insert our editline |
| 1280 | // input reader in the queue so we know when our reader should be active |
| 1281 | // and so we can receive bytes only when we are supposed to. |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1282 | SBError err (m_editline_reader.Initialize (m_debugger, |
| 1283 | Driver::EditLineInputReaderCallback, // callback |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1284 | this, // baton |
| 1285 | eInputReaderGranularityByte, // token_size |
| 1286 | NULL, // end token - NULL means never done |
| 1287 | NULL, // prompt - taken care of elsewhere |
| 1288 | false)); // echo input - don't need Debugger |
| 1289 | // to do this, we handle it elsewhere |
| 1290 | |
| 1291 | if (err.Fail()) |
| 1292 | { |
| 1293 | ::fprintf (stderr, "error: %s", err.GetCString()); |
| 1294 | exit (6); |
| 1295 | } |
| 1296 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1297 | m_debugger.PushInputReader (m_editline_reader); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1298 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1299 | SBListener listener(m_debugger.GetListener()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1300 | if (listener.IsValid()) |
| 1301 | { |
| 1302 | |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1303 | listener.StartListeningForEventClass(m_debugger, |
| 1304 | SBTarget::GetBroadcasterClassName(), |
| 1305 | SBTarget::eBroadcastBitBreakpointChanged); |
| 1306 | listener.StartListeningForEventClass(m_debugger, |
| 1307 | SBThread::GetBroadcasterClassName(), |
Jim Ingham | c3faa19 | 2012-12-11 02:31:48 +0000 | [diff] [blame] | 1308 | SBThread::eBroadcastBitStackChanged | |
| 1309 | SBThread::eBroadcastBitThreadSelected); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1310 | listener.StartListeningForEvents (*m_io_channel_ap, |
| 1311 | IOChannel::eBroadcastBitHasUserInput | |
| 1312 | IOChannel::eBroadcastBitUserInterrupt | |
| 1313 | IOChannel::eBroadcastBitThreadShouldExit | |
| 1314 | IOChannel::eBroadcastBitThreadDidStart | |
| 1315 | IOChannel::eBroadcastBitThreadDidExit); |
| 1316 | |
| 1317 | if (m_io_channel_ap->Start ()) |
| 1318 | { |
| 1319 | bool iochannel_thread_exited = false; |
| 1320 | |
| 1321 | listener.StartListeningForEvents (sb_interpreter.GetBroadcaster(), |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 1322 | SBCommandInterpreter::eBroadcastBitQuitCommandReceived | |
| 1323 | SBCommandInterpreter::eBroadcastBitAsynchronousOutputData | |
| 1324 | SBCommandInterpreter::eBroadcastBitAsynchronousErrorData); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1325 | |
| 1326 | // Before we handle any options from the command line, we parse the |
| 1327 | // .lldbinit file in the user's home directory. |
| 1328 | SBCommandReturnObject result; |
| 1329 | sb_interpreter.SourceInitFileInHomeDirectory(result); |
| 1330 | if (GetDebugMode()) |
| 1331 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1332 | result.PutError (m_debugger.GetErrorFileHandle()); |
| 1333 | result.PutOutput (m_debugger.GetOutputFileHandle()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | // Now we handle options we got from the command line |
| 1337 | char command_string[PATH_MAX * 2]; |
| 1338 | const size_t num_source_command_files = GetNumSourceCommandFiles(); |
Enrico Granata | aa0c8ff | 2012-12-13 20:20:11 +0000 | [diff] [blame] | 1339 | const bool dump_stream_only_if_no_immediate = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1340 | if (num_source_command_files > 0) |
| 1341 | { |
| 1342 | for (size_t i=0; i < num_source_command_files; ++i) |
| 1343 | { |
| 1344 | const char *command_file = GetSourceCommandFileAtIndex(i); |
Johnny Chen | 85ffddc | 2010-07-28 21:16:11 +0000 | [diff] [blame] | 1345 | ::snprintf (command_string, sizeof(command_string), "command source '%s'", command_file); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1346 | m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, false); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1347 | if (GetDebugMode()) |
| 1348 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1349 | result.PutError (m_debugger.GetErrorFileHandle()); |
| 1350 | result.PutOutput (m_debugger.GetOutputFileHandle()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1351 | } |
Enrico Granata | aa0c8ff | 2012-12-13 20:20:11 +0000 | [diff] [blame] | 1352 | |
| 1353 | // if the command sourcing generated an error - dump the result object |
Enrico Granata | dc3f4f9 | 2012-12-14 00:52:54 +0000 | [diff] [blame] | 1354 | if (result.Succeeded() == false) |
Enrico Granata | aa0c8ff | 2012-12-13 20:20:11 +0000 | [diff] [blame] | 1355 | { |
| 1356 | const size_t output_size = result.GetOutputSize(); |
| 1357 | if (output_size > 0) |
| 1358 | m_io_channel_ap->OutWrite (result.GetOutput(dump_stream_only_if_no_immediate), output_size, NO_ASYNC); |
Enrico Granata | dc3f4f9 | 2012-12-14 00:52:54 +0000 | [diff] [blame] | 1359 | const size_t error_size = result.GetErrorSize(); |
| 1360 | if (error_size > 0) |
| 1361 | m_io_channel_ap->OutWrite (result.GetError(dump_stream_only_if_no_immediate), error_size, NO_ASYNC); |
Enrico Granata | aa0c8ff | 2012-12-13 20:20:11 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | result.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1365 | } |
| 1366 | } |
| 1367 | |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 1368 | // Was there a core file specified? |
| 1369 | std::string core_file_spec(""); |
| 1370 | if (!m_option_data.m_core_file.empty()) |
| 1371 | core_file_spec.append("--core ").append(m_option_data.m_core_file); |
| 1372 | |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1373 | const size_t num_args = m_option_data.m_args.size(); |
| 1374 | if (num_args > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1375 | { |
| 1376 | char arch_name[64]; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1377 | if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name))) |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1378 | ::snprintf (command_string, |
| 1379 | sizeof (command_string), |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 1380 | "target create --arch=%s %s \"%s\"", |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1381 | arch_name, |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 1382 | core_file_spec.c_str(), |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1383 | m_option_data.m_args[0].c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1384 | else |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1385 | ::snprintf (command_string, |
| 1386 | sizeof(command_string), |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 1387 | "target create %s \"%s\"", |
| 1388 | core_file_spec.c_str(), |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1389 | m_option_data.m_args[0].c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1390 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1391 | m_debugger.HandleCommand (command_string); |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1392 | |
| 1393 | if (num_args > 1) |
| 1394 | { |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 1395 | m_debugger.HandleCommand ("settings clear target.run-args"); |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1396 | char arg_cstr[1024]; |
| 1397 | for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx) |
| 1398 | { |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 1399 | ::snprintf (arg_cstr, |
| 1400 | sizeof(arg_cstr), |
Greg Clayton | 1d88596 | 2011-11-08 02:43:13 +0000 | [diff] [blame] | 1401 | "settings append target.run-args \"%s\"", |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 1402 | m_option_data.m_args[arg_idx].c_str()); |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 1403 | m_debugger.HandleCommand (arg_cstr); |
| 1404 | } |
| 1405 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1406 | } |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 1407 | else if (!core_file_spec.empty()) |
| 1408 | { |
| 1409 | ::snprintf (command_string, |
| 1410 | sizeof(command_string), |
| 1411 | "target create %s", |
| 1412 | core_file_spec.c_str()); |
| 1413 | m_debugger.HandleCommand (command_string);; |
| 1414 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1415 | |
| 1416 | // Now that all option parsing is done, we try and parse the .lldbinit |
| 1417 | // file in the current working directory |
| 1418 | sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result); |
| 1419 | if (GetDebugMode()) |
| 1420 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1421 | result.PutError(m_debugger.GetErrorFileHandle()); |
| 1422 | result.PutOutput(m_debugger.GetOutputFileHandle()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
| 1425 | SBEvent event; |
| 1426 | |
| 1427 | // Make sure the IO channel is started up before we try to tell it we |
| 1428 | // are ready for input |
| 1429 | listener.WaitForEventForBroadcasterWithType (UINT32_MAX, |
| 1430 | *m_io_channel_ap, |
| 1431 | IOChannel::eBroadcastBitThreadDidStart, |
| 1432 | event); |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 1433 | // If we were asked to attach, then do that here: |
| 1434 | // I'm going to use the command string rather than directly |
| 1435 | // calling the API's because then I don't have to recode the |
| 1436 | // event handling here. |
| 1437 | if (!m_option_data.m_process_name.empty() |
| 1438 | || m_option_data.m_process_pid != LLDB_INVALID_PROCESS_ID) |
| 1439 | { |
| 1440 | std::string command_str("process attach "); |
| 1441 | if (m_option_data.m_process_pid != LLDB_INVALID_PROCESS_ID) |
| 1442 | { |
| 1443 | command_str.append("-p "); |
| 1444 | char pid_buffer[32]; |
Daniel Malea | d01b295 | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 1445 | ::snprintf (pid_buffer, sizeof(pid_buffer), "%" PRIu64, m_option_data.m_process_pid); |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 1446 | command_str.append(pid_buffer); |
| 1447 | } |
| 1448 | else |
| 1449 | { |
| 1450 | command_str.append("-n \""); |
| 1451 | command_str.append(m_option_data.m_process_name); |
| 1452 | command_str.push_back('\"'); |
| 1453 | if (m_option_data.m_wait_for) |
| 1454 | command_str.append(" -w"); |
| 1455 | } |
| 1456 | |
| 1457 | if (m_debugger.GetOutputFileHandle()) |
| 1458 | ::fprintf (m_debugger.GetOutputFileHandle(), |
| 1459 | "Attaching to process with:\n %s\n", |
| 1460 | command_str.c_str()); |
| 1461 | |
| 1462 | // Force the attach to be synchronous: |
| 1463 | bool orig_async = m_debugger.GetAsync(); |
| 1464 | m_debugger.SetAsync(true); |
| 1465 | m_debugger.HandleCommand(command_str.c_str()); |
| 1466 | m_debugger.SetAsync(orig_async); |
| 1467 | } |
| 1468 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1469 | ReadyForCommand (); |
| 1470 | |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 1471 | while (!GetIsDone()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1472 | { |
| 1473 | listener.WaitForEvent (UINT32_MAX, event); |
| 1474 | if (event.IsValid()) |
| 1475 | { |
| 1476 | if (event.GetBroadcaster().IsValid()) |
| 1477 | { |
| 1478 | uint32_t event_type = event.GetType(); |
| 1479 | if (event.BroadcasterMatchesRef (*m_io_channel_ap)) |
| 1480 | { |
| 1481 | if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) || |
| 1482 | (event_type & IOChannel::eBroadcastBitThreadDidExit)) |
| 1483 | { |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 1484 | SetIsDone(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1485 | if (event_type & IOChannel::eBroadcastBitThreadDidExit) |
| 1486 | iochannel_thread_exited = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1487 | } |
| 1488 | else |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 1489 | { |
| 1490 | if (HandleIOEvent (event)) |
| 1491 | SetIsDone(); |
| 1492 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1493 | } |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 1494 | else if (SBProcess::EventIsProcessEvent (event)) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1495 | { |
| 1496 | HandleProcessEvent (event); |
| 1497 | } |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 1498 | else if (SBBreakpoint::EventIsBreakpointEvent (event)) |
| 1499 | { |
| 1500 | HandleBreakpointEvent (event); |
| 1501 | } |
Jim Ingham | 4f465cf | 2012-10-10 18:32:14 +0000 | [diff] [blame] | 1502 | else if (SBThread::EventIsThreadEvent (event)) |
| 1503 | { |
| 1504 | HandleThreadEvent (event); |
| 1505 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1506 | else if (event.BroadcasterMatchesRef (sb_interpreter.GetBroadcaster())) |
| 1507 | { |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 1508 | // TODO: deprecate the eBroadcastBitQuitCommandReceived event |
| 1509 | // now that we have SBCommandInterpreter::SetCommandOverrideCallback() |
| 1510 | // that can take over a command |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1511 | if (event_type & SBCommandInterpreter::eBroadcastBitQuitCommandReceived) |
Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 1512 | { |
Greg Clayton | a9f7b79 | 2012-02-29 04:21:24 +0000 | [diff] [blame] | 1513 | SetIsDone(); |
Greg Clayton | 74d4193 | 2012-01-31 04:56:17 +0000 | [diff] [blame] | 1514 | } |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 1515 | else if (event_type & SBCommandInterpreter::eBroadcastBitAsynchronousErrorData) |
| 1516 | { |
| 1517 | const char *data = SBEvent::GetCStringFromEvent (event); |
| 1518 | m_io_channel_ap->ErrWrite (data, strlen(data), ASYNC); |
| 1519 | } |
| 1520 | else if (event_type & SBCommandInterpreter::eBroadcastBitAsynchronousOutputData) |
| 1521 | { |
| 1522 | const char *data = SBEvent::GetCStringFromEvent (event); |
| 1523 | m_io_channel_ap->OutWrite (data, strlen(data), ASYNC); |
| 1524 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1525 | } |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 1530 | editline_output_pty.CloseMasterFileDescriptor(); |
| 1531 | master_out_comm.Disconnect(); |
| 1532 | out_comm_2.Disconnect(); |
| 1533 | reset_stdin_termios(); |
| 1534 | fclose (stdin); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1535 | |
| 1536 | CloseIOChannelFile (); |
| 1537 | |
| 1538 | if (!iochannel_thread_exited) |
| 1539 | { |
Greg Clayton | b132097 | 2010-07-14 00:18:15 +0000 | [diff] [blame] | 1540 | event.Clear(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1541 | listener.GetNextEventForBroadcasterWithType (*m_io_channel_ap, |
| 1542 | IOChannel::eBroadcastBitThreadDidExit, |
| 1543 | event); |
| 1544 | if (!event.IsValid()) |
| 1545 | { |
| 1546 | // Send end EOF to the driver file descriptor |
| 1547 | m_io_channel_ap->Stop(); |
| 1548 | } |
| 1549 | } |
| 1550 | |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 1551 | SBDebugger::Destroy (m_debugger); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | |
| 1557 | void |
| 1558 | Driver::ReadyForCommand () |
| 1559 | { |
| 1560 | if (m_waiting_for_command == false) |
| 1561 | { |
| 1562 | m_waiting_for_command = true; |
| 1563 | BroadcastEventByType (Driver::eBroadcastBitReadyForInput, true); |
| 1564 | } |
| 1565 | } |
| 1566 | |
| 1567 | |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1568 | void |
| 1569 | sigwinch_handler (int signo) |
| 1570 | { |
| 1571 | struct winsize window_size; |
| 1572 | if (isatty (STDIN_FILENO) |
| 1573 | && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0) |
| 1574 | { |
Jim Ingham | 57190ba | 2012-04-26 21:39:32 +0000 | [diff] [blame] | 1575 | if ((window_size.ws_col > 0) && g_driver != NULL) |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1576 | { |
Jim Ingham | 57190ba | 2012-04-26 21:39:32 +0000 | [diff] [blame] | 1577 | g_driver->GetDebugger().SetTerminalWidth (window_size.ws_col); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1582 | void |
| 1583 | sigint_handler (int signo) |
| 1584 | { |
| 1585 | static bool g_interrupt_sent = false; |
| 1586 | if (g_driver) |
| 1587 | { |
| 1588 | if (!g_interrupt_sent) |
| 1589 | { |
| 1590 | g_interrupt_sent = true; |
| 1591 | g_driver->GetDebugger().DispatchInputInterrupt(); |
| 1592 | g_interrupt_sent = false; |
| 1593 | return; |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | exit (signo); |
| 1598 | } |
| 1599 | |
Jim Ingham | c5917d9 | 2012-11-30 20:23:19 +0000 | [diff] [blame] | 1600 | void |
| 1601 | sigtstp_handler (int signo) |
| 1602 | { |
| 1603 | g_driver->GetDebugger().SaveInputTerminalState(); |
| 1604 | signal (signo, SIG_DFL); |
| 1605 | kill (getpid(), signo); |
| 1606 | signal (signo, sigtstp_handler); |
| 1607 | } |
| 1608 | |
| 1609 | void |
| 1610 | sigcont_handler (int signo) |
| 1611 | { |
| 1612 | g_driver->GetDebugger().RestoreInputTerminalState(); |
| 1613 | signal (signo, SIG_DFL); |
| 1614 | kill (getpid(), signo); |
| 1615 | signal (signo, sigcont_handler); |
| 1616 | } |
| 1617 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1618 | int |
Jim Ingham | a462f5c | 2011-01-27 20:15:39 +0000 | [diff] [blame] | 1619 | main (int argc, char const *argv[], const char *envp[]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1620 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1621 | SBDebugger::Initialize(); |
| 1622 | |
Greg Clayton | 2ccf8cf | 2010-11-07 21:02:03 +0000 | [diff] [blame] | 1623 | SBHostOS::ThreadCreated ("<lldb.driver.main-thread>"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1624 | |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1625 | signal (SIGPIPE, SIG_IGN); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1626 | signal (SIGWINCH, sigwinch_handler); |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1627 | signal (SIGINT, sigint_handler); |
Jim Ingham | c5917d9 | 2012-11-30 20:23:19 +0000 | [diff] [blame] | 1628 | signal (SIGTSTP, sigtstp_handler); |
| 1629 | signal (SIGCONT, sigcont_handler); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1630 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1631 | // Create a scope for driver so that the driver object will destroy itself |
| 1632 | // before SBDebugger::Terminate() is called. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1633 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1634 | Driver driver; |
| 1635 | |
| 1636 | bool exit = false; |
| 1637 | SBError error (driver.ParseArgs (argc, argv, stdout, exit)); |
| 1638 | if (error.Fail()) |
| 1639 | { |
| 1640 | const char *error_cstr = error.GetCString (); |
| 1641 | if (error_cstr) |
| 1642 | ::fprintf (stderr, "error: %s\n", error_cstr); |
| 1643 | } |
| 1644 | else if (!exit) |
| 1645 | { |
| 1646 | driver.MainLoop (); |
| 1647 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | SBDebugger::Terminate(); |
| 1651 | return 0; |
| 1652 | } |