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