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