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