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