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 | |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 18 | // Includes for pipe() |
| 19 | #if defined(_WIN32) |
| 20 | #include <io.h> |
| 21 | #include <fcntl.h> |
Shawn Best | 8da0bf3 | 2014-11-08 01:41:49 +0000 | [diff] [blame] | 22 | #elif defined(__ANDROID_NDK__) |
| 23 | #include <errno.h> |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 24 | #else |
| 25 | #include <unistd.h> |
| 26 | #endif |
| 27 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 28 | #include <string> |
| 29 | |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 30 | #include <thread> |
Jim Ingham | e6bc6cb | 2012-02-08 05:23:15 +0000 | [diff] [blame] | 31 | #include "lldb/API/SBBreakpoint.h" |
Eli Friedman | a382d47 | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 32 | #include "lldb/API/SBCommandInterpreter.h" |
| 33 | #include "lldb/API/SBCommandReturnObject.h" |
| 34 | #include "lldb/API/SBCommunication.h" |
| 35 | #include "lldb/API/SBDebugger.h" |
| 36 | #include "lldb/API/SBEvent.h" |
| 37 | #include "lldb/API/SBHostOS.h" |
| 38 | #include "lldb/API/SBListener.h" |
Jim Ingham | 85e8b81 | 2011-02-19 02:53:09 +0000 | [diff] [blame] | 39 | #include "lldb/API/SBStream.h" |
Eli Friedman | a382d47 | 2010-06-09 09:50:17 +0000 | [diff] [blame] | 40 | #include "lldb/API/SBTarget.h" |
| 41 | #include "lldb/API/SBThread.h" |
| 42 | #include "lldb/API/SBProcess.h" |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 43 | |
Todd Fiala | b82ad2a | 2014-09-15 15:17:13 +0000 | [diff] [blame] | 44 | #if !defined(__APPLE__) |
Zachary Turner | 40a069a | 2014-09-11 20:26:49 +0000 | [diff] [blame] | 45 | #include "llvm/Support/DataTypes.h" |
Todd Fiala | b82ad2a | 2014-09-15 15:17:13 +0000 | [diff] [blame] | 46 | #endif |
Zachary Turner | 40a069a | 2014-09-11 20:26:49 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 48 | using namespace lldb; |
| 49 | |
| 50 | static void reset_stdin_termios (); |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 51 | static bool g_old_stdin_termios_is_valid = false; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 52 | static struct termios g_old_stdin_termios; |
| 53 | |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 54 | static char *g_debugger_name = (char *) ""; |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 55 | static Driver *g_driver = NULL; |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 57 | // In the Driver::MainLoop, we change the terminal settings. This function is |
| 58 | // added as an atexit handler to make sure we clean them up. |
| 59 | static void |
| 60 | reset_stdin_termios () |
| 61 | { |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 62 | if (g_old_stdin_termios_is_valid) |
| 63 | { |
| 64 | g_old_stdin_termios_is_valid = false; |
| 65 | ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios); |
| 66 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 69 | typedef struct |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | { |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 71 | uint32_t usage_mask; // Used to mark options that can be used together. If (1 << n & usage_mask) != 0 |
| 72 | // then this option belongs to option set n. |
| 73 | bool required; // This option is required (in the current usage level) |
| 74 | const char * long_option; // Full name for this option. |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 75 | int short_option; // Single character for this option. |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 76 | int option_has_arg; // no_argument, required_argument or optional_argument |
Greg Clayton | ab65b34 | 2011-04-13 22:47:15 +0000 | [diff] [blame] | 77 | 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] | 78 | lldb::CommandArgumentType argument_type; // Type of argument this option takes |
| 79 | const char * usage_text; // Full text explaining what this options does and what (if any) argument to |
| 80 | // pass it. |
| 81 | } OptionDefinition; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 82 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 83 | #define LLDB_3_TO_5 LLDB_OPT_SET_3|LLDB_OPT_SET_4|LLDB_OPT_SET_5 |
| 84 | #define LLDB_4_TO_5 LLDB_OPT_SET_4|LLDB_OPT_SET_5 |
| 85 | |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 86 | static OptionDefinition g_options[] = |
| 87 | { |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 88 | { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 89 | "Prints out the usage information for the LLDB debugger." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 90 | { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 91 | "Prints out the current version number of the LLDB debugger." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 92 | { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, 0, eArgTypeArchitecture, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 93 | "Tells the debugger to use the specified architecture when starting and running the program. <architecture> must " |
| 94 | "be one of the architectures for which the program was compiled." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 95 | { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, 0, eArgTypeFilename, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 96 | "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] | 97 | { LLDB_OPT_SET_3, false, "core" , 'c', required_argument, 0, eArgTypeFilename, |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 98 | "Tells the debugger to use the fullpath to <path> as the core file." }, |
Jim Ingham | f0c63b9 | 2014-02-05 21:35:09 +0000 | [diff] [blame] | 99 | { LLDB_OPT_SET_5, true , "attach-pid" , 'p', required_argument, 0, eArgTypePid, |
| 100 | "Tells the debugger to attach to a process with the given pid." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 101 | { LLDB_OPT_SET_4, true , "attach-name" , 'n', required_argument, 0, eArgTypeProcessName, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 102 | "Tells the debugger to attach to a process with the given name." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 103 | { LLDB_OPT_SET_4, true , "wait-for" , 'w', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 104 | "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] | 105 | { LLDB_3_TO_5, false, "source" , 's', required_argument, 0, eArgTypeFilename, |
Jim Ingham | 47ea51f | 2013-09-17 01:53:35 +0000 | [diff] [blame] | 106 | "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] | 107 | { LLDB_3_TO_5, false, "one-line" , 'o', required_argument, 0, eArgTypeNone, |
Jim Ingham | 47ea51f | 2013-09-17 01:53:35 +0000 | [diff] [blame] | 108 | "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] | 109 | { 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] | 110 | "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] | 111 | { 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] | 112 | "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." }, |
Jim Ingham | f0c63b9 | 2014-02-05 21:35:09 +0000 | [diff] [blame] | 113 | { LLDB_3_TO_5, false, "source-quietly" , 'Q', no_argument , 0, eArgTypeNone, |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 114 | "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." }, |
| 115 | { LLDB_3_TO_5, false, "batch" , 'b', no_argument , 0, eArgTypeNone, |
| 116 | "Tells the debugger to running the commands from -s, -S, -o & -O, and then quit. However if any run command stopped due to a signal or crash, " |
| 117 | "the debugger will return to the interactive prompt at the place of the crash." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 118 | { LLDB_3_TO_5, false, "editor" , 'e', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 119 | "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] | 120 | { LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone, |
Jim Ingham | 12e9a20 | 2011-09-15 21:30:02 +0000 | [diff] [blame] | 121 | "Do not automatically parse any '.lldbinit' files." }, |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 122 | { 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] | 123 | "Do not use colors." }, |
| 124 | { LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone, |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 125 | "Prints out the path to the lldb.py file for this version of lldb." }, |
Jim Ingham | f0c63b9 | 2014-02-05 21:35:09 +0000 | [diff] [blame] | 126 | { LLDB_3_TO_5, false, "script-language", 'l', required_argument, 0, eArgTypeScriptLang, |
| 127 | "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. " |
| 128 | "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python " |
| 129 | "extensions have been implemented." }, |
| 130 | { LLDB_3_TO_5, false, "debug" , 'd', no_argument , 0, eArgTypeNone, |
| 131 | "Tells the debugger to print out extra information for debugging itself." }, |
Filipe Cabecinhas | d0b87d8 | 2012-09-11 18:11:16 +0000 | [diff] [blame] | 132 | { 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 133 | }; |
| 134 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 135 | static const uint32_t last_option_set_with_args = 2; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 136 | |
| 137 | Driver::Driver () : |
| 138 | SBBroadcaster ("Driver"), |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 139 | m_debugger (SBDebugger::Create(false)), |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 140 | m_option_data () |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 141 | { |
Greg Clayton | fc3f027 | 2011-05-29 04:06:55 +0000 | [diff] [blame] | 142 | // We want to be able to handle CTRL+D in the terminal to have it terminate |
| 143 | // certain input |
| 144 | m_debugger.SetCloseInputOnEOF (false); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 145 | g_debugger_name = (char *) m_debugger.GetInstanceName(); |
| 146 | if (g_debugger_name == NULL) |
| 147 | g_debugger_name = (char *) ""; |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 148 | g_driver = this; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | Driver::~Driver () |
| 152 | { |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 153 | g_driver = NULL; |
| 154 | g_debugger_name = NULL; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 157 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 158 | // This function takes INDENT, which tells how many spaces to output at the front |
| 159 | // of each line; TEXT, which is the text that is to be output. It outputs the |
| 160 | // text, on multiple lines if necessary, to RESULT, with INDENT spaces at the |
| 161 | // front of each line. It breaks lines on spaces, tabs or newlines, shortening |
| 162 | // the line if necessary to not break in the middle of a word. It assumes that |
| 163 | // each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 164 | |
| 165 | void |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 166 | OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 167 | { |
| 168 | int len = strlen (text); |
| 169 | std::string text_string (text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 170 | |
| 171 | // Force indentation to be reasonable. |
| 172 | if (indent >= output_max_columns) |
| 173 | indent = 0; |
| 174 | |
| 175 | // Will it all fit on one line? |
| 176 | |
| 177 | if (len + indent < output_max_columns) |
| 178 | // Output as a single line |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 179 | fprintf (out, "%*s%s\n", indent, "", text); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 180 | else |
| 181 | { |
| 182 | // We need to break it up into multiple lines. |
| 183 | int text_width = output_max_columns - indent - 1; |
| 184 | int start = 0; |
| 185 | int end = start; |
| 186 | int final_end = len; |
| 187 | int sub_len; |
| 188 | |
| 189 | while (end < final_end) |
| 190 | { |
| 191 | // Dont start the 'text' on a space, since we're already outputting the indentation. |
| 192 | while ((start < final_end) && (text[start] == ' ')) |
| 193 | start++; |
| 194 | |
| 195 | end = start + text_width; |
| 196 | if (end > final_end) |
| 197 | end = final_end; |
| 198 | else |
| 199 | { |
| 200 | // If we're not at the end of the text, make sure we break the line on white space. |
| 201 | while (end > start |
| 202 | && text[end] != ' ' && text[end] != '\t' && text[end] != '\n') |
| 203 | end--; |
| 204 | } |
| 205 | sub_len = end - start; |
| 206 | std::string substring = text_string.substr (start, sub_len); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 207 | fprintf (out, "%*s%s\n", indent, "", substring.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 208 | start = end + 1; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | void |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 214 | ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 215 | { |
| 216 | uint32_t screen_width = 80; |
| 217 | uint32_t indent_level = 0; |
| 218 | const char *name = "lldb"; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 219 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 220 | fprintf (out, "\nUsage:\n\n"); |
| 221 | |
| 222 | indent_level += 2; |
| 223 | |
| 224 | |
| 225 | // First, show each usage level set of options, e.g. <cmd> [options-for-level-0] |
| 226 | // <cmd> [options-for-level-1] |
| 227 | // etc. |
| 228 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 229 | uint32_t num_options; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 230 | uint32_t num_option_sets = 0; |
| 231 | |
| 232 | 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] | 233 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 234 | uint32_t this_usage_mask = option_table[num_options].usage_mask; |
| 235 | if (this_usage_mask == LLDB_OPT_SET_ALL) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 236 | { |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 237 | if (num_option_sets == 0) |
| 238 | num_option_sets = 1; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | { |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 242 | for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++) |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 243 | { |
| 244 | if (this_usage_mask & 1 << j) |
| 245 | { |
| 246 | if (num_option_sets <= j) |
| 247 | num_option_sets = j + 1; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++) |
| 254 | { |
| 255 | uint32_t opt_set_mask; |
| 256 | |
| 257 | opt_set_mask = 1 << opt_set; |
| 258 | |
| 259 | if (opt_set > 0) |
| 260 | fprintf (out, "\n"); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 261 | fprintf (out, "%*s%s", indent_level, "", name); |
Jim Ingham | 556e653 | 2011-08-16 23:15:02 +0000 | [diff] [blame] | 262 | bool is_help_line = false; |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 263 | |
| 264 | for (uint32_t i = 0; i < num_options; ++i) |
| 265 | { |
| 266 | if (option_table[i].usage_mask & opt_set_mask) |
| 267 | { |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 268 | CommandArgumentType arg_type = option_table[i].argument_type; |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 269 | const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type); |
Jim Ingham | 556e653 | 2011-08-16 23:15:02 +0000 | [diff] [blame] | 270 | // This is a bit of a hack, but there's no way to say certain options don't have arguments yet... |
| 271 | // so we do it by hand here. |
| 272 | if (option_table[i].short_option == 'h') |
| 273 | is_help_line = true; |
| 274 | |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 275 | if (option_table[i].required) |
| 276 | { |
| 277 | if (option_table[i].option_has_arg == required_argument) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 278 | fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 279 | else if (option_table[i].option_has_arg == optional_argument) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 280 | fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name); |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 281 | else |
| 282 | fprintf (out, " -%c", option_table[i].short_option); |
| 283 | } |
| 284 | else |
| 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 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 294 | } |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 295 | if (!is_help_line && (opt_set <= last_option_set_with_args)) |
Jim Ingham | 556e653 | 2011-08-16 23:15:02 +0000 | [diff] [blame] | 296 | fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | fprintf (out, "\n\n"); |
| 300 | |
| 301 | // Now print out all the detailed information about the various options: long form, short form and help text: |
| 302 | // -- long_name <argument> |
| 303 | // - short <argument> |
| 304 | // help text |
| 305 | |
| 306 | // This variable is used to keep track of which options' info we've printed out, because some options can be in |
| 307 | // more than one usage level, but we only want to print the long form of its information once. |
| 308 | |
| 309 | Driver::OptionData::OptionSet options_seen; |
| 310 | Driver::OptionData::OptionSet::iterator pos; |
| 311 | |
| 312 | indent_level += 5; |
| 313 | |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 314 | for (uint32_t i = 0; i < num_options; ++i) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | { |
| 316 | // Only print this option if we haven't already seen it. |
| 317 | pos = options_seen.find (option_table[i].short_option); |
| 318 | if (pos == options_seen.end()) |
| 319 | { |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 320 | CommandArgumentType arg_type = option_table[i].argument_type; |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 321 | const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type); |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 323 | options_seen.insert (option_table[i].short_option); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 324 | fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option); |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 325 | if (arg_type != eArgTypeNone) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 326 | fprintf (out, "<%s>", arg_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 327 | fprintf (out, "\n"); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 328 | fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option); |
Caroline Tice | deaab22 | 2010-10-01 19:59:14 +0000 | [diff] [blame] | 329 | if (arg_type != eArgTypeNone) |
Greg Clayton | 9d0402b | 2011-02-20 02:15:07 +0000 | [diff] [blame] | 330 | fprintf (out, "<%s>", arg_name); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 331 | fprintf (out, "\n"); |
| 332 | indent_level += 5; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 333 | OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 334 | indent_level -= 5; |
| 335 | fprintf (out, "\n"); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | indent_level -= 5; |
Jim Ingham | f0c63b9 | 2014-02-05 21:35:09 +0000 | [diff] [blame] | 340 | |
| 341 | fprintf (out, "\n%*sNotes:\n", |
| 342 | indent_level, ""); |
| 343 | indent_level += 5; |
| 344 | |
Jim Ingham | 47ea51f | 2013-09-17 01:53:35 +0000 | [diff] [blame] | 345 | fprintf (out, "\n%*sMultiple \"-s\" and \"-o\" options can be provided. They will be processed from left to right in order, " |
| 346 | "\n%*swith the source files and commands interleaved. The same is true of the \"-S\" and \"-O\" options." |
| 347 | "\n%*sThe before file and after file sets can intermixed freely, the command parser will sort them out." |
| 348 | "\n%*sThe order of the file specifiers (\"-c\", \"-f\", etc.) is not significant in this regard.\n\n", |
| 349 | indent_level, "", |
| 350 | indent_level, "", |
| 351 | indent_level, "", |
| 352 | indent_level, ""); |
| 353 | |
Jim Ingham | f0c63b9 | 2014-02-05 21:35:09 +0000 | [diff] [blame] | 354 | fprintf (out, "\n%*sIf you don't provide -f then the first argument will be the file to be debugged" |
| 355 | "\n%*swhich means that '%s -- <filename> [<ARG1> [<ARG2>]]' also works." |
| 356 | "\n%*sBut remember to end the options with \"--\" if any of your arguments have a \"-\" in them.\n\n", |
Jim Ingham | a9deaf9 | 2011-08-16 23:57:58 +0000 | [diff] [blame] | 357 | indent_level, "", |
| 358 | indent_level, "", |
| 359 | name, |
| 360 | indent_level, ""); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void |
Greg Clayton | e0d378b | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 364 | BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table, |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 365 | uint32_t num_options) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 366 | { |
| 367 | if (num_options == 0) |
| 368 | return; |
| 369 | |
| 370 | uint32_t i; |
| 371 | uint32_t j; |
| 372 | std::bitset<256> option_seen; |
| 373 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 374 | getopt_table.resize (num_options + 1); |
| 375 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 376 | for (i = 0, j = 0; i < num_options; ++i) |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 377 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 378 | char short_opt = expanded_option_table[i].short_option; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 379 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 380 | if (option_seen.test(short_opt) == false) |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 381 | { |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 382 | getopt_table[j].name = expanded_option_table[i].long_option; |
| 383 | getopt_table[j].has_arg = expanded_option_table[i].option_has_arg; |
| 384 | getopt_table[j].flag = NULL; |
| 385 | getopt_table[j].val = expanded_option_table[i].short_option; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 386 | option_seen.set(short_opt); |
| 387 | ++j; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 388 | } |
| 389 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 390 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 391 | getopt_table[j].name = NULL; |
| 392 | getopt_table[j].has_arg = 0; |
| 393 | getopt_table[j].flag = NULL; |
| 394 | getopt_table[j].val = 0; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 395 | |
| 396 | } |
| 397 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 398 | Driver::OptionData::OptionData () : |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 399 | m_args(), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 400 | m_script_lang (lldb::eScriptLanguageDefault), |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 401 | m_core_file (), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 402 | m_crash_log (), |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 403 | m_initial_commands (), |
| 404 | m_after_file_commands (), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 405 | m_debug_mode (false), |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 406 | m_source_quietly(false), |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 407 | m_print_version (false), |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 408 | m_print_python_path (false), |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 409 | m_print_help (false), |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 410 | m_wait_for(false), |
| 411 | m_process_name(), |
| 412 | m_process_pid(LLDB_INVALID_PROCESS_ID), |
Daniel Dunbar | a08823f | 2011-10-31 22:50:49 +0000 | [diff] [blame] | 413 | m_use_external_editor(false), |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 414 | m_batch(false), |
Stephen Wilson | 71c21d1 | 2011-04-11 19:41:40 +0000 | [diff] [blame] | 415 | m_seen_options() |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 416 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | Driver::OptionData::~OptionData () |
| 420 | { |
| 421 | } |
| 422 | |
| 423 | void |
| 424 | Driver::OptionData::Clear () |
| 425 | { |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 426 | m_args.clear (); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 427 | m_script_lang = lldb::eScriptLanguageDefault; |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 428 | m_initial_commands.clear (); |
| 429 | m_after_file_commands.clear (); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 430 | m_debug_mode = false; |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 431 | m_source_quietly = false; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 432 | m_print_help = false; |
| 433 | m_print_version = false; |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 434 | m_print_python_path = false; |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 435 | m_use_external_editor = false; |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 436 | m_wait_for = false; |
| 437 | m_process_name.erase(); |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 438 | m_batch = false; |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 439 | m_process_pid = LLDB_INVALID_PROCESS_ID; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | void |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 443 | Driver::OptionData::AddInitialCommand (const char *command, bool before_file, bool is_file, SBError &error) |
| 444 | { |
| 445 | std::vector<std::pair<bool, std::string> > *command_set; |
| 446 | if (before_file) |
| 447 | command_set = &(m_initial_commands); |
| 448 | else |
| 449 | command_set = &(m_after_file_commands); |
| 450 | |
| 451 | if (is_file) |
| 452 | { |
| 453 | SBFileSpec file(command); |
| 454 | if (file.Exists()) |
| 455 | command_set->push_back (std::pair<bool, std::string> (true, optarg)); |
| 456 | else if (file.ResolveExecutableLocation()) |
| 457 | { |
| 458 | char final_path[PATH_MAX]; |
| 459 | file.GetPath (final_path, sizeof(final_path)); |
| 460 | std::string path_str (final_path); |
| 461 | command_set->push_back (std::pair<bool, std::string> (true, path_str)); |
| 462 | } |
| 463 | else |
| 464 | error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg); |
| 465 | } |
| 466 | else |
| 467 | command_set->push_back (std::pair<bool, std::string> (false, optarg)); |
| 468 | } |
| 469 | |
| 470 | void |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 471 | Driver::ResetOptionValues () |
| 472 | { |
| 473 | m_option_data.Clear (); |
| 474 | } |
| 475 | |
| 476 | const char * |
| 477 | Driver::GetFilename() const |
| 478 | { |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 479 | if (m_option_data.m_args.empty()) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 480 | return NULL; |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 481 | return m_option_data.m_args.front().c_str(); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | const char * |
| 485 | Driver::GetCrashLogFilename() const |
| 486 | { |
| 487 | if (m_option_data.m_crash_log.empty()) |
| 488 | return NULL; |
| 489 | return m_option_data.m_crash_log.c_str(); |
| 490 | } |
| 491 | |
| 492 | lldb::ScriptLanguage |
| 493 | Driver::GetScriptLanguage() const |
| 494 | { |
| 495 | return m_option_data.m_script_lang; |
| 496 | } |
| 497 | |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 498 | void |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 499 | Driver::WriteInitialCommands (bool before_file, SBStream &strm) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 500 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 501 | std::vector<std::pair<bool, std::string> > &command_set = before_file ? m_option_data.m_initial_commands : |
| 502 | m_option_data.m_after_file_commands; |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 503 | |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 504 | for (const auto &command_pair : command_set) |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 505 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 506 | const char *command = command_pair.second.c_str(); |
| 507 | if (command_pair.first) |
| 508 | strm.Printf("command source -s %i '%s'\n", m_option_data.m_source_quietly, command); |
| 509 | else |
| 510 | strm.Printf("%s\n", command); |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 511 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | bool |
| 515 | Driver::GetDebugMode() const |
| 516 | { |
| 517 | return m_option_data.m_debug_mode; |
| 518 | } |
| 519 | |
| 520 | |
| 521 | // Check the arguments that were passed to this program to make sure they are valid and to get their |
| 522 | // argument values (if any). Return a boolean value indicating whether or not to start up the full |
| 523 | // debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR |
| 524 | // if the user only wanted help or version information. |
| 525 | |
| 526 | SBError |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 527 | Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exiting) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 528 | { |
| 529 | ResetOptionValues (); |
| 530 | |
| 531 | SBCommandReturnObject result; |
| 532 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 533 | SBError error; |
| 534 | std::string option_string; |
| 535 | struct option *long_options = NULL; |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 536 | std::vector<struct option> long_options_vector; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 537 | uint32_t num_options; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 538 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 539 | for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options) |
| 540 | /* Do Nothing. */; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 541 | |
| 542 | if (num_options == 0) |
| 543 | { |
| 544 | if (argc > 1) |
| 545 | error.SetErrorStringWithFormat ("invalid number of options"); |
| 546 | return error; |
| 547 | } |
| 548 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 549 | BuildGetOptTable (g_options, long_options_vector, num_options); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 550 | |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 551 | if (long_options_vector.empty()) |
| 552 | long_options = NULL; |
| 553 | else |
| 554 | long_options = &long_options_vector.front(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 555 | |
| 556 | if (long_options == NULL) |
| 557 | { |
| 558 | error.SetErrorStringWithFormat ("invalid long options"); |
| 559 | return error; |
| 560 | } |
| 561 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 562 | // Build the option_string argument for call to getopt_long_only. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 563 | |
| 564 | for (int i = 0; long_options[i].name != NULL; ++i) |
| 565 | { |
| 566 | if (long_options[i].flag == NULL) |
| 567 | { |
| 568 | option_string.push_back ((char) long_options[i].val); |
| 569 | switch (long_options[i].has_arg) |
| 570 | { |
| 571 | default: |
| 572 | case no_argument: |
| 573 | break; |
| 574 | case required_argument: |
| 575 | option_string.push_back (':'); |
| 576 | break; |
| 577 | case optional_argument: |
| 578 | option_string.append ("::"); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 584 | // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't |
| 585 | // know at that point whether we should read in init files yet. So we don't read them in in the |
| 586 | // Driver constructor, then set the flags back to "read them in" here, and then if we see the |
| 587 | // "-n" flag, we'll turn it off again. Finally we have to read them in by hand later in the |
| 588 | // main loop. |
| 589 | |
| 590 | m_debugger.SkipLLDBInitFiles (false); |
| 591 | m_debugger.SkipAppInitFiles (false); |
| 592 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 593 | // Prepare for & make calls to getopt_long_only. |
Eli Friedman | adb3502 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 594 | #if __GLIBC__ |
| 595 | optind = 0; |
| 596 | #else |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 597 | optreset = 1; |
| 598 | optind = 1; |
Eli Friedman | adb3502 | 2010-06-13 19:18:49 +0000 | [diff] [blame] | 599 | #endif |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 600 | int val; |
| 601 | while (1) |
| 602 | { |
| 603 | int long_options_index = -1; |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 604 | 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] | 605 | |
| 606 | if (val == -1) |
| 607 | break; |
| 608 | else if (val == '?') |
| 609 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 610 | m_option_data.m_print_help = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 611 | error.SetErrorStringWithFormat ("unknown or ambiguous option"); |
| 612 | break; |
| 613 | } |
| 614 | else if (val == 0) |
| 615 | continue; |
| 616 | else |
| 617 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 618 | m_option_data.m_seen_options.insert ((char) val); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 619 | if (long_options_index == -1) |
| 620 | { |
| 621 | for (int i = 0; |
| 622 | long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val; |
| 623 | ++i) |
| 624 | { |
| 625 | if (long_options[i].val == val) |
| 626 | { |
| 627 | long_options_index = i; |
| 628 | break; |
| 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | if (long_options_index >= 0) |
| 634 | { |
Greg Clayton | 3bcdfc0 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 635 | const int short_option = g_options[long_options_index].short_option; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 636 | |
| 637 | switch (short_option) |
| 638 | { |
| 639 | case 'h': |
| 640 | m_option_data.m_print_help = true; |
| 641 | break; |
| 642 | |
| 643 | case 'v': |
| 644 | m_option_data.m_print_version = true; |
| 645 | break; |
| 646 | |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 647 | case 'P': |
| 648 | m_option_data.m_print_python_path = true; |
| 649 | break; |
| 650 | |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 651 | case 'b': |
| 652 | m_option_data.m_batch = true; |
| 653 | break; |
| 654 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 655 | case 'c': |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 656 | { |
| 657 | SBFileSpec file(optarg); |
| 658 | if (file.Exists()) |
| 659 | { |
| 660 | m_option_data.m_core_file = optarg; |
| 661 | } |
| 662 | else |
| 663 | error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg); |
| 664 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 665 | break; |
Johnny Chen | eb46f78 | 2012-08-15 22:10:42 +0000 | [diff] [blame] | 666 | |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 667 | case 'e': |
| 668 | m_option_data.m_use_external_editor = true; |
| 669 | break; |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 670 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 671 | case 'x': |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 672 | m_debugger.SkipLLDBInitFiles (true); |
Jim Ingham | 0694269 | 2011-08-13 00:22:20 +0000 | [diff] [blame] | 673 | m_debugger.SkipAppInitFiles (true); |
Greg Clayton | 6eee5aa | 2010-10-11 01:05:37 +0000 | [diff] [blame] | 674 | break; |
| 675 | |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 676 | case 'X': |
Michael Sartain | c3ce7f27 | 2013-05-23 20:47:45 +0000 | [diff] [blame] | 677 | m_debugger.SetUseColor (false); |
| 678 | break; |
| 679 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 680 | case 'f': |
| 681 | { |
| 682 | SBFileSpec file(optarg); |
| 683 | if (file.Exists()) |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 684 | { |
| 685 | m_option_data.m_args.push_back (optarg); |
| 686 | } |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 687 | else if (file.ResolveExecutableLocation()) |
| 688 | { |
| 689 | char path[PATH_MAX]; |
Johnny Chen | 25f3a3c | 2011-08-10 22:06:24 +0000 | [diff] [blame] | 690 | file.GetPath (path, sizeof(path)); |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 691 | m_option_data.m_args.push_back (path); |
Caroline Tice | 428a9a5 | 2010-09-10 04:48:55 +0000 | [diff] [blame] | 692 | } |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 693 | else |
| 694 | error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg); |
| 695 | } |
| 696 | break; |
| 697 | |
| 698 | case 'a': |
| 699 | if (!m_debugger.SetDefaultArchitecture (optarg)) |
| 700 | error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg); |
| 701 | break; |
| 702 | |
| 703 | case 'l': |
| 704 | m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg); |
| 705 | break; |
| 706 | |
| 707 | case 'd': |
| 708 | m_option_data.m_debug_mode = true; |
| 709 | break; |
| 710 | |
Jim Ingham | f0c63b9 | 2014-02-05 21:35:09 +0000 | [diff] [blame] | 711 | case 'Q': |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 712 | m_option_data.m_source_quietly = true; |
| 713 | break; |
| 714 | |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 715 | case 'n': |
| 716 | m_option_data.m_process_name = optarg; |
| 717 | break; |
| 718 | |
| 719 | case 'w': |
| 720 | m_option_data.m_wait_for = true; |
| 721 | break; |
| 722 | |
| 723 | case 'p': |
| 724 | { |
| 725 | char *remainder; |
| 726 | m_option_data.m_process_pid = strtol (optarg, &remainder, 0); |
| 727 | if (remainder == optarg || *remainder != '\0') |
| 728 | error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.", |
| 729 | optarg); |
| 730 | } |
| 731 | break; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 732 | case 's': |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 733 | m_option_data.AddInitialCommand(optarg, false, true, error); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 734 | break; |
Jim Ingham | ed3252f | 2013-09-14 00:20:24 +0000 | [diff] [blame] | 735 | case 'o': |
| 736 | m_option_data.AddInitialCommand(optarg, false, false, error); |
| 737 | break; |
| 738 | case 'S': |
| 739 | m_option_data.AddInitialCommand(optarg, true, true, error); |
| 740 | break; |
| 741 | case 'O': |
| 742 | m_option_data.AddInitialCommand(optarg, true, false, error); |
| 743 | break; |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 744 | default: |
| 745 | m_option_data.m_print_help = true; |
| 746 | error.SetErrorStringWithFormat ("unrecognized option %c", short_option); |
| 747 | break; |
| 748 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 749 | } |
| 750 | else |
| 751 | { |
| 752 | error.SetErrorStringWithFormat ("invalid option with value %i", val); |
| 753 | } |
| 754 | if (error.Fail()) |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 755 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 756 | return error; |
Caroline Tice | 4ab31c9 | 2010-10-12 21:57:09 +0000 | [diff] [blame] | 757 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
Jim Ingham | 8651121 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 760 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 761 | if (error.Fail() || m_option_data.m_print_help) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 762 | { |
| 763 | ShowUsage (out_fh, g_options, m_option_data); |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 764 | exiting = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 765 | } |
| 766 | else if (m_option_data.m_print_version) |
| 767 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 768 | ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString()); |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 769 | exiting = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 770 | } |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 771 | else if (m_option_data.m_print_python_path) |
| 772 | { |
| 773 | SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath(); |
| 774 | if (python_file_spec.IsValid()) |
| 775 | { |
| 776 | char python_path[PATH_MAX]; |
| 777 | size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX); |
| 778 | if (num_chars < PATH_MAX) |
| 779 | { |
| 780 | ::fprintf (out_fh, "%s\n", python_path); |
| 781 | } |
| 782 | else |
| 783 | ::fprintf (out_fh, "<PATH TOO LONG>\n"); |
| 784 | } |
| 785 | else |
| 786 | ::fprintf (out_fh, "<COULD NOT FIND PATH>\n"); |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 787 | exiting = true; |
Jim Ingham | e2231ac | 2012-12-21 22:22:26 +0000 | [diff] [blame] | 788 | } |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 789 | 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] | 790 | { |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 791 | // Any arguments that are left over after option parsing are for |
| 792 | // the program. If a file was specified with -f then the filename |
| 793 | // is already in the m_option_data.m_args array, and any remaining args |
| 794 | // are arguments for the inferior program. If no file was specified with |
| 795 | // -f, then what is left is the program name followed by any arguments. |
| 796 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 797 | // Skip any options we consumed with getopt_long_only |
Greg Clayton | 8d846da | 2010-12-08 22:23:24 +0000 | [diff] [blame] | 798 | argc -= optind; |
| 799 | argv += optind; |
| 800 | |
| 801 | if (argc > 0) |
| 802 | { |
| 803 | for (int arg_idx=0; arg_idx<argc; ++arg_idx) |
| 804 | { |
| 805 | const char *arg = argv[arg_idx]; |
| 806 | if (arg) |
| 807 | m_option_data.m_args.push_back (arg); |
| 808 | } |
| 809 | } |
| 810 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 811 | } |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 812 | else |
| 813 | { |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 814 | // Skip any options we consumed with getopt_long_only |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 815 | argc -= optind; |
Greg Clayton | 23f5950 | 2012-07-17 03:23:13 +0000 | [diff] [blame] | 816 | //argv += optind; // Commented out to keep static analyzer happy |
Jim Ingham | e64f0dc | 2011-09-13 23:25:31 +0000 | [diff] [blame] | 817 | |
| 818 | if (argc > 0) |
| 819 | ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n"); |
| 820 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 821 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 822 | return error; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 825 | void |
| 826 | Driver::MainLoop () |
| 827 | { |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 828 | if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0) |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 829 | { |
| 830 | g_old_stdin_termios_is_valid = true; |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 831 | atexit (reset_stdin_termios); |
Greg Clayton | f571b89 | 2012-02-02 19:28:31 +0000 | [diff] [blame] | 832 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 833 | |
| 834 | ::setbuf (stdin, NULL); |
| 835 | ::setbuf (stdout, NULL); |
| 836 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 837 | m_debugger.SetErrorFileHandle (stderr, false); |
| 838 | m_debugger.SetOutputFileHandle (stdout, false); |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 839 | m_debugger.SetInputFileHandle (stdin, false); // Don't take ownership of STDIN yet... |
| 840 | |
Jim Ingham | e40e421 | 2010-08-30 19:44:40 +0000 | [diff] [blame] | 841 | m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 842 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 843 | struct winsize window_size; |
| 844 | if (isatty (STDIN_FILENO) |
| 845 | && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0) |
| 846 | { |
Caroline Tice | 3df9a8d | 2010-09-04 00:03:46 +0000 | [diff] [blame] | 847 | if (window_size.ws_col > 0) |
Greg Clayton | a701509 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 848 | m_debugger.SetTerminalWidth (window_size.ws_col); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 849 | } |
| 850 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 851 | SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter(); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 852 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 853 | // Before we handle any options from the command line, we parse the |
| 854 | // .lldbinit file in the user's home directory. |
| 855 | SBCommandReturnObject result; |
| 856 | sb_interpreter.SourceInitFileInHomeDirectory(result); |
| 857 | if (GetDebugMode()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 858 | { |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 859 | result.PutError (m_debugger.GetErrorFileHandle()); |
| 860 | result.PutOutput (m_debugger.GetOutputFileHandle()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 861 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 862 | |
| 863 | // Now we handle options we got from the command line |
Ed Maste | f831453 | 2014-07-30 19:26:11 +0000 | [diff] [blame] | 864 | SBStream commands_stream; |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 865 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 866 | // First source in the commands specified to be run before the file arguments are processed. |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 867 | WriteInitialCommands(true, commands_stream); |
| 868 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 869 | const size_t num_args = m_option_data.m_args.size(); |
| 870 | if (num_args > 0) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 871 | { |
Jim Ingham | 5b1fe95 | 2014-08-07 23:01:31 +0000 | [diff] [blame] | 872 | char arch_name[64]; |
| 873 | if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name))) |
| 874 | commands_stream.Printf("target create --arch=%s \"%s\"", arch_name, m_option_data.m_args[0].c_str()); |
| 875 | else |
| 876 | commands_stream.Printf("target create \"%s\"", m_option_data.m_args[0].c_str()); |
| 877 | |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 878 | if (!m_option_data.m_core_file.empty()) |
| 879 | { |
| 880 | commands_stream.Printf(" --core \"%s\"", m_option_data.m_core_file.c_str()); |
| 881 | } |
| 882 | commands_stream.Printf("\n"); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 883 | |
| 884 | if (num_args > 1) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 885 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 886 | commands_stream.Printf ("settings set -- target.run-args "); |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 887 | for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 888 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 889 | const char *arg_cstr = m_option_data.m_args[arg_idx].c_str(); |
| 890 | if (strchr(arg_cstr, '"') == NULL) |
| 891 | commands_stream.Printf(" \"%s\"", arg_cstr); |
| 892 | else |
| 893 | commands_stream.Printf(" '%s'", arg_cstr); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 894 | } |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 895 | commands_stream.Printf("\n"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 896 | } |
| 897 | } |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 898 | else if (!m_option_data.m_core_file.empty()) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 899 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 900 | commands_stream.Printf("target create --core \"%s\"\n", m_option_data.m_core_file.c_str()); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 901 | } |
Greg Clayton | 9c0b64c | 2014-02-06 18:22:44 +0000 | [diff] [blame] | 902 | else if (!m_option_data.m_process_name.empty()) |
| 903 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 904 | commands_stream.Printf ("process attach --name \"%s\"", m_option_data.m_process_name.c_str()); |
| 905 | |
| 906 | if (m_option_data.m_wait_for) |
| 907 | commands_stream.Printf(" --waitfor"); |
| 908 | |
| 909 | commands_stream.Printf("\n"); |
| 910 | |
Greg Clayton | 9c0b64c | 2014-02-06 18:22:44 +0000 | [diff] [blame] | 911 | } |
| 912 | else if (LLDB_INVALID_PROCESS_ID != m_option_data.m_process_pid) |
| 913 | { |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 914 | commands_stream.Printf ("process attach --pid %" PRIu64 "\n", m_option_data.m_process_pid); |
Greg Clayton | 9c0b64c | 2014-02-06 18:22:44 +0000 | [diff] [blame] | 915 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 916 | |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 917 | WriteInitialCommands(false, commands_stream); |
| 918 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 919 | // Now that all option parsing is done, we try and parse the .lldbinit |
| 920 | // file in the current working directory |
| 921 | sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result); |
| 922 | if (GetDebugMode()) |
| 923 | { |
| 924 | result.PutError(m_debugger.GetErrorFileHandle()); |
| 925 | result.PutOutput(m_debugger.GetOutputFileHandle()); |
| 926 | } |
| 927 | |
| 928 | bool handle_events = true; |
| 929 | bool spawn_thread = false; |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 930 | |
| 931 | // Check if we have any data in the commands stream, and if so, save it to a temp file |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 932 | // so we can then run the command interpreter using the file contents. |
| 933 | const char *commands_data = commands_stream.GetData(); |
| 934 | const size_t commands_size = commands_stream.GetSize(); |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 935 | |
| 936 | // The command file might have requested that we quit, this variable will track that. |
| 937 | bool quit_requested = false; |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 938 | bool stopped_for_crash = false; |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 939 | if (commands_data && commands_size) |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 940 | { |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 941 | enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 942 | |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 943 | bool success = true; |
| 944 | int fds[2] = { -1, -1 }; |
| 945 | int err = 0; |
| 946 | #ifdef _WIN32 |
| 947 | err = _pipe(fds, commands_size, O_BINARY); |
| 948 | #else |
| 949 | err = pipe(fds); |
| 950 | #endif |
| 951 | if (err == 0) |
| 952 | { |
Saleem Abdulrasool | 9e5b498 | 2014-09-08 02:47:13 +0000 | [diff] [blame] | 953 | ssize_t nrwr = write(fds[WRITE], commands_data, commands_size); |
| 954 | if (nrwr < 0) |
| 955 | { |
| 956 | fprintf(stderr, "error: write(%i, %p, %zd) failed (errno = %i) " |
| 957 | "when trying to open LLDB commands pipe\n", |
| 958 | fds[WRITE], commands_data, commands_size, errno); |
| 959 | success = false; |
| 960 | } |
| 961 | else if (static_cast<size_t>(nrwr) == commands_size) |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 962 | { |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 963 | // Close the write end of the pipe so when we give the read end to |
| 964 | // the debugger/command interpreter it will exit when it consumes all |
| 965 | // of the data |
| 966 | #ifdef _WIN32 |
| 967 | _close(fds[WRITE]); fds[WRITE] = -1; |
| 968 | #else |
| 969 | close(fds[WRITE]); fds[WRITE] = -1; |
| 970 | #endif |
| 971 | // Now open the read file descriptor in a FILE * that we can give to |
| 972 | // the debugger as an input handle |
| 973 | FILE *commands_file = fdopen(fds[READ], "r"); |
| 974 | if (commands_file) |
| 975 | { |
| 976 | fds[READ] = -1; // The FILE * 'commands_file' now owns the read descriptor |
| 977 | // Hand ownership if the FILE * over to the debugger for "commands_file". |
| 978 | m_debugger.SetInputFileHandle (commands_file, true); |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 979 | |
| 980 | // Set the debugger into Sync mode when running the command file. Otherwise command files |
| 981 | // that run the target won't run in a sensible way. |
| 982 | bool old_async = m_debugger.GetAsync(); |
| 983 | m_debugger.SetAsync(false); |
| 984 | int num_errors; |
| 985 | |
| 986 | SBCommandInterpreterRunOptions options; |
| 987 | options.SetStopOnError (true); |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 988 | if (m_option_data.m_batch) |
| 989 | options.SetStopOnCrash (true); |
| 990 | |
| 991 | m_debugger.RunCommandInterpreter(handle_events, |
| 992 | spawn_thread, |
| 993 | options, |
| 994 | num_errors, |
| 995 | quit_requested, |
| 996 | stopped_for_crash); |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 997 | m_debugger.SetAsync(old_async); |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 998 | } |
| 999 | else |
| 1000 | { |
Saleem Abdulrasool | 9e5b498 | 2014-09-08 02:47:13 +0000 | [diff] [blame] | 1001 | fprintf(stderr, |
| 1002 | "error: fdopen(%i, \"r\") failed (errno = %i) when " |
| 1003 | "trying to open LLDB commands pipe\n", |
| 1004 | fds[READ], errno); |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 1005 | success = false; |
| 1006 | } |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 1007 | } |
Saleem Abdulrasool | 9e5b498 | 2014-09-08 02:47:13 +0000 | [diff] [blame] | 1008 | else |
| 1009 | { |
| 1010 | assert(!"partial writes not handled"); |
| 1011 | success = false; |
| 1012 | } |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 1013 | } |
Greg Clayton | 4966876 | 2014-08-01 18:32:07 +0000 | [diff] [blame] | 1014 | else |
| 1015 | { |
| 1016 | fprintf(stderr, "error: can't create pipe file descriptors for LLDB commands\n"); |
| 1017 | success = false; |
| 1018 | } |
| 1019 | |
| 1020 | // Close any pipes that we still have ownership of |
| 1021 | if ( fds[WRITE] != -1) |
| 1022 | { |
| 1023 | #ifdef _WIN32 |
| 1024 | _close(fds[WRITE]); fds[WRITE] = -1; |
| 1025 | #else |
| 1026 | close(fds[WRITE]); fds[WRITE] = -1; |
| 1027 | #endif |
| 1028 | |
| 1029 | } |
| 1030 | |
| 1031 | if ( fds[READ] != -1) |
| 1032 | { |
| 1033 | #ifdef _WIN32 |
| 1034 | _close(fds[READ]); fds[READ] = -1; |
| 1035 | #else |
| 1036 | close(fds[READ]); fds[READ] = -1; |
| 1037 | #endif |
| 1038 | } |
| 1039 | |
| 1040 | // Something went wrong with command pipe |
| 1041 | if (!success) |
| 1042 | { |
| 1043 | exit(1); |
| 1044 | } |
| 1045 | |
Greg Clayton | 06357c9 | 2014-07-30 17:38:47 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | // Now set the input file handle to STDIN and run the command |
| 1049 | // interpreter again in interactive mode and let the debugger |
| 1050 | // take ownership of stdin |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 1051 | |
Jim Ingham | ffc9f1d | 2014-10-14 01:20:07 +0000 | [diff] [blame] | 1052 | bool go_interactive = true; |
| 1053 | if (quit_requested) |
| 1054 | go_interactive = false; |
| 1055 | else if (m_option_data.m_batch && !stopped_for_crash) |
| 1056 | go_interactive = false; |
| 1057 | |
| 1058 | if (go_interactive) |
Jim Ingham | 26c7bf9 | 2014-10-11 00:38:27 +0000 | [diff] [blame] | 1059 | { |
| 1060 | m_debugger.SetInputFileHandle (stdin, true); |
| 1061 | m_debugger.RunCommandInterpreter(handle_events, spawn_thread); |
| 1062 | } |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1063 | |
| 1064 | reset_stdin_termios(); |
| 1065 | fclose (stdin); |
| 1066 | |
| 1067 | SBDebugger::Destroy (m_debugger); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
Greg Clayton | 44d9378 | 2014-01-27 23:43:24 +0000 | [diff] [blame] | 1070 | |
Jim Ingham | c46fe7c | 2013-02-22 22:56:55 +0000 | [diff] [blame] | 1071 | void |
| 1072 | Driver::ResizeWindow (unsigned short col) |
| 1073 | { |
| 1074 | GetDebugger().SetTerminalWidth (col); |
Jim Ingham | c46fe7c | 2013-02-22 22:56:55 +0000 | [diff] [blame] | 1075 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1076 | |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1077 | void |
| 1078 | sigwinch_handler (int signo) |
| 1079 | { |
| 1080 | struct winsize window_size; |
| 1081 | if (isatty (STDIN_FILENO) |
| 1082 | && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0) |
| 1083 | { |
Jim Ingham | 57190ba | 2012-04-26 21:39:32 +0000 | [diff] [blame] | 1084 | if ((window_size.ws_col > 0) && g_driver != NULL) |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1085 | { |
Jim Ingham | c46fe7c | 2013-02-22 22:56:55 +0000 | [diff] [blame] | 1086 | g_driver->ResizeWindow (window_size.ws_col); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1091 | void |
| 1092 | sigint_handler (int signo) |
| 1093 | { |
| 1094 | static bool g_interrupt_sent = false; |
| 1095 | if (g_driver) |
| 1096 | { |
| 1097 | if (!g_interrupt_sent) |
| 1098 | { |
| 1099 | g_interrupt_sent = true; |
| 1100 | g_driver->GetDebugger().DispatchInputInterrupt(); |
| 1101 | g_interrupt_sent = false; |
| 1102 | return; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | exit (signo); |
| 1107 | } |
| 1108 | |
Jim Ingham | c5917d9 | 2012-11-30 20:23:19 +0000 | [diff] [blame] | 1109 | void |
| 1110 | sigtstp_handler (int signo) |
| 1111 | { |
| 1112 | g_driver->GetDebugger().SaveInputTerminalState(); |
| 1113 | signal (signo, SIG_DFL); |
| 1114 | kill (getpid(), signo); |
| 1115 | signal (signo, sigtstp_handler); |
| 1116 | } |
| 1117 | |
| 1118 | void |
| 1119 | sigcont_handler (int signo) |
| 1120 | { |
| 1121 | g_driver->GetDebugger().RestoreInputTerminalState(); |
| 1122 | signal (signo, SIG_DFL); |
| 1123 | kill (getpid(), signo); |
| 1124 | signal (signo, sigcont_handler); |
| 1125 | } |
| 1126 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1127 | int |
Jim Ingham | a462f5c | 2011-01-27 20:15:39 +0000 | [diff] [blame] | 1128 | main (int argc, char const *argv[], const char *envp[]) |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1129 | { |
Deepak Panickal | 99fbc07 | 2014-03-03 15:39:47 +0000 | [diff] [blame] | 1130 | #ifdef _MSC_VER |
| 1131 | // disable buffering on windows |
| 1132 | setvbuf(stdout, NULL, _IONBF, 0); |
| 1133 | setvbuf(stdin , NULL, _IONBF, 0); |
| 1134 | #endif |
| 1135 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1136 | SBDebugger::Initialize(); |
| 1137 | |
Greg Clayton | 2ccf8cf | 2010-11-07 21:02:03 +0000 | [diff] [blame] | 1138 | SBHostOS::ThreadCreated ("<lldb.driver.main-thread>"); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1139 | |
Greg Clayton | 3fcbed6 | 2010-10-19 03:25:40 +0000 | [diff] [blame] | 1140 | signal (SIGPIPE, SIG_IGN); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1141 | signal (SIGWINCH, sigwinch_handler); |
Caroline Tice | efed613 | 2010-11-19 20:47:54 +0000 | [diff] [blame] | 1142 | signal (SIGINT, sigint_handler); |
Jim Ingham | c5917d9 | 2012-11-30 20:23:19 +0000 | [diff] [blame] | 1143 | signal (SIGTSTP, sigtstp_handler); |
| 1144 | signal (SIGCONT, sigcont_handler); |
Caroline Tice | dd75985 | 2010-09-09 17:45:09 +0000 | [diff] [blame] | 1145 | |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1146 | // Create a scope for driver so that the driver object will destroy itself |
| 1147 | // before SBDebugger::Terminate() is called. |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1148 | { |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1149 | Driver driver; |
| 1150 | |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 1151 | bool exiting = false; |
| 1152 | SBError error (driver.ParseArgs (argc, argv, stdout, exiting)); |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1153 | if (error.Fail()) |
| 1154 | { |
| 1155 | const char *error_cstr = error.GetCString (); |
| 1156 | if (error_cstr) |
| 1157 | ::fprintf (stderr, "error: %s\n", error_cstr); |
| 1158 | } |
Deepak Panickal | 429222c | 2013-10-15 15:46:40 +0000 | [diff] [blame] | 1159 | else if (!exiting) |
Greg Clayton | 6611103 | 2010-06-23 01:19:29 +0000 | [diff] [blame] | 1160 | { |
| 1161 | driver.MainLoop (); |
| 1162 | } |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | SBDebugger::Terminate(); |
| 1166 | return 0; |
| 1167 | } |