blob: 9b1e5887ef9483df4ef25a62adecb78630678988 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- 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 Panickal429222c2013-10-15 15:46:40 +000012#include <stdio.h>
Eli Friedmana382d472010-06-09 09:50:17 +000013#include <string.h>
14#include <stdlib.h>
15#include <limits.h>
Eli Friedman07b16272010-06-09 19:11:30 +000016#include <fcntl.h>
Chris Lattner30fdc8d2010-06-08 16:52:24 +000017
Greg Clayton49668762014-08-01 18:32:07 +000018// Includes for pipe()
19#if defined(_WIN32)
20#include <io.h>
21#include <fcntl.h>
Shawn Best8da0bf32014-11-08 01:41:49 +000022#elif defined(__ANDROID_NDK__)
23#include <errno.h>
Greg Clayton49668762014-08-01 18:32:07 +000024#else
25#include <unistd.h>
26#endif
27
Chris Lattner30fdc8d2010-06-08 16:52:24 +000028#include <string>
29
Deepak Panickal429222c2013-10-15 15:46:40 +000030#include <thread>
Jim Inghame6bc6cb2012-02-08 05:23:15 +000031#include "lldb/API/SBBreakpoint.h"
Eli Friedmana382d472010-06-09 09:50:17 +000032#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 Ingham85e8b812011-02-19 02:53:09 +000039#include "lldb/API/SBStream.h"
Eli Friedmana382d472010-06-09 09:50:17 +000040#include "lldb/API/SBTarget.h"
41#include "lldb/API/SBThread.h"
42#include "lldb/API/SBProcess.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043
Todd Fialab82ad2a2014-09-15 15:17:13 +000044#if !defined(__APPLE__)
Zachary Turner40a069a2014-09-11 20:26:49 +000045#include "llvm/Support/DataTypes.h"
Todd Fialab82ad2a2014-09-15 15:17:13 +000046#endif
Zachary Turner40a069a2014-09-11 20:26:49 +000047
Chris Lattner30fdc8d2010-06-08 16:52:24 +000048using namespace lldb;
49
50static void reset_stdin_termios ();
Greg Claytonf571b892012-02-02 19:28:31 +000051static bool g_old_stdin_termios_is_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052static struct termios g_old_stdin_termios;
53
Caroline Ticedd759852010-09-09 17:45:09 +000054static char *g_debugger_name = (char *) "";
Caroline Ticeefed6132010-11-19 20:47:54 +000055static Driver *g_driver = NULL;
Caroline Ticedd759852010-09-09 17:45:09 +000056
Chris Lattner30fdc8d2010-06-08 16:52:24 +000057// 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.
59static void
60reset_stdin_termios ()
61{
Greg Claytonf571b892012-02-02 19:28:31 +000062 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 Lattner30fdc8d2010-06-08 16:52:24 +000067}
68
Greg Claytone0d378b2011-03-24 21:19:54 +000069typedef struct
Chris Lattner30fdc8d2010-06-08 16:52:24 +000070{
Greg Claytone0d378b2011-03-24 21:19:54 +000071 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 Clayton3bcdfc02012-12-04 00:32:51 +000075 int short_option; // Single character for this option.
Greg Claytone0d378b2011-03-24 21:19:54 +000076 int option_has_arg; // no_argument, required_argument or optional_argument
Greg Claytonab65b342011-04-13 22:47:15 +000077 uint32_t completion_type; // Cookie the option class can use to do define the argument completion.
Greg Claytone0d378b2011-03-24 21:19:54 +000078 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 Lattner30fdc8d2010-06-08 16:52:24 +000082
Jim Inghame64f0dc2011-09-13 23:25:31 +000083#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 Claytone0d378b2011-03-24 21:19:54 +000086static OptionDefinition g_options[] =
87{
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000088 { LLDB_OPT_SET_1, true , "help" , 'h', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000089 "Prints out the usage information for the LLDB debugger." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000090 { LLDB_OPT_SET_2, true , "version" , 'v', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +000091 "Prints out the current version number of the LLDB debugger." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +000092 { LLDB_OPT_SET_3, true , "arch" , 'a', required_argument, 0, eArgTypeArchitecture,
Jim Ingham12e9a202011-09-15 21:30:02 +000093 "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 Cabecinhasd0b87d82012-09-11 18:11:16 +000095 { LLDB_OPT_SET_3, true , "file" , 'f', required_argument, 0, eArgTypeFilename,
Jim Ingham12e9a202011-09-15 21:30:02 +000096 "Tells the debugger to use the file <filename> as the program to be debugged." },
Jason Molenda67c3cf52012-10-24 03:29:40 +000097 { LLDB_OPT_SET_3, false, "core" , 'c', required_argument, 0, eArgTypeFilename,
Johnny Cheneb46f782012-08-15 22:10:42 +000098 "Tells the debugger to use the fullpath to <path> as the core file." },
Jim Inghamf0c63b92014-02-05 21:35:09 +000099 { 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 Cabecinhasd0b87d82012-09-11 18:11:16 +0000101 { LLDB_OPT_SET_4, true , "attach-name" , 'n', required_argument, 0, eArgTypeProcessName,
Jim Ingham12e9a202011-09-15 21:30:02 +0000102 "Tells the debugger to attach to a process with the given name." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000103 { LLDB_OPT_SET_4, true , "wait-for" , 'w', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000104 "Tells the debugger to wait for a process with the given pid or name to launch before attaching." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000105 { LLDB_3_TO_5, false, "source" , 's', required_argument, 0, eArgTypeFilename,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000106 "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 Inghamed3252f2013-09-14 00:20:24 +0000107 { LLDB_3_TO_5, false, "one-line" , 'o', required_argument, 0, eArgTypeNone,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000108 "Tells the debugger to execute this one-line lldb command after any file provided on the command line has been loaded." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000109 { LLDB_3_TO_5, false, "source-before-file" , 'S', required_argument, 0, eArgTypeFilename,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000110 "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 Inghamed3252f2013-09-14 00:20:24 +0000111 { LLDB_3_TO_5, false, "one-line-before-file" , 'O', required_argument, 0, eArgTypeNone,
Jim Ingham47ea51f2013-09-17 01:53:35 +0000112 "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." },
Jim Ingham4add3b12014-11-19 01:28:13 +0000113 { LLDB_3_TO_5, false, "one-line-on-crash" , 'k', required_argument, 0, eArgTypeNone,
114 "When in batch mode, tells the debugger to execute this one-line lldb command if the target crashes." },
115 { LLDB_3_TO_5, false, "source-on-crash" , 'K', required_argument, 0, eArgTypeFilename,
116 "When in batch mode, tells the debugger to source this file of lldb commands if the target crashes." },
Jim Inghamf0c63b92014-02-05 21:35:09 +0000117 { LLDB_3_TO_5, false, "source-quietly" , 'Q', no_argument , 0, eArgTypeNone,
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000118 "Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded." },
119 { LLDB_3_TO_5, false, "batch" , 'b', no_argument , 0, eArgTypeNone,
120 "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, "
121 "the debugger will return to the interactive prompt at the place of the crash." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000122 { LLDB_3_TO_5, false, "editor" , 'e', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000123 "Tells the debugger to open source files using the host's \"external editor\" mechanism." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000124 { LLDB_3_TO_5, false, "no-lldbinit" , 'x', no_argument , 0, eArgTypeNone,
Jim Ingham12e9a202011-09-15 21:30:02 +0000125 "Do not automatically parse any '.lldbinit' files." },
Jim Inghamed3252f2013-09-14 00:20:24 +0000126 { LLDB_3_TO_5, false, "no-use-colors" , 'X', no_argument , 0, eArgTypeNone,
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000127 "Do not use colors." },
128 { LLDB_OPT_SET_6, true , "python-path" , 'P', no_argument , 0, eArgTypeNone,
Jim Inghame2231ac2012-12-21 22:22:26 +0000129 "Prints out the path to the lldb.py file for this version of lldb." },
Jim Inghamf0c63b92014-02-05 21:35:09 +0000130 { LLDB_3_TO_5, false, "script-language", 'l', required_argument, 0, eArgTypeScriptLang,
131 "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. "
132 "Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python "
133 "extensions have been implemented." },
134 { LLDB_3_TO_5, false, "debug" , 'd', no_argument , 0, eArgTypeNone,
135 "Tells the debugger to print out extra information for debugging itself." },
Filipe Cabecinhasd0b87d82012-09-11 18:11:16 +0000136 { 0, false, NULL , 0 , 0 , 0, eArgTypeNone, NULL }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000137};
138
Jim Inghame64f0dc2011-09-13 23:25:31 +0000139static const uint32_t last_option_set_with_args = 2;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000140
141Driver::Driver () :
142 SBBroadcaster ("Driver"),
Jim Ingham06942692011-08-13 00:22:20 +0000143 m_debugger (SBDebugger::Create(false)),
Greg Clayton44d93782014-01-27 23:43:24 +0000144 m_option_data ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000145{
Greg Claytonfc3f0272011-05-29 04:06:55 +0000146 // We want to be able to handle CTRL+D in the terminal to have it terminate
147 // certain input
148 m_debugger.SetCloseInputOnEOF (false);
Caroline Ticedd759852010-09-09 17:45:09 +0000149 g_debugger_name = (char *) m_debugger.GetInstanceName();
150 if (g_debugger_name == NULL)
151 g_debugger_name = (char *) "";
Caroline Ticeefed6132010-11-19 20:47:54 +0000152 g_driver = this;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000153}
154
155Driver::~Driver ()
156{
Caroline Ticeefed6132010-11-19 20:47:54 +0000157 g_driver = NULL;
158 g_debugger_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000159}
160
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000161
Greg Claytonc982c762010-07-09 20:39:50 +0000162// This function takes INDENT, which tells how many spaces to output at the front
163// of each line; TEXT, which is the text that is to be output. It outputs the
164// text, on multiple lines if necessary, to RESULT, with INDENT spaces at the
165// front of each line. It breaks lines on spaces, tabs or newlines, shortening
166// the line if necessary to not break in the middle of a word. It assumes that
167// each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000168
169void
Greg Claytonc982c762010-07-09 20:39:50 +0000170OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000171{
172 int len = strlen (text);
173 std::string text_string (text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000174
175 // Force indentation to be reasonable.
176 if (indent >= output_max_columns)
177 indent = 0;
178
179 // Will it all fit on one line?
180
181 if (len + indent < output_max_columns)
182 // Output as a single line
Greg Claytonc982c762010-07-09 20:39:50 +0000183 fprintf (out, "%*s%s\n", indent, "", text);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184 else
185 {
186 // We need to break it up into multiple lines.
187 int text_width = output_max_columns - indent - 1;
188 int start = 0;
189 int end = start;
190 int final_end = len;
191 int sub_len;
192
193 while (end < final_end)
194 {
195 // Dont start the 'text' on a space, since we're already outputting the indentation.
196 while ((start < final_end) && (text[start] == ' '))
197 start++;
198
199 end = start + text_width;
200 if (end > final_end)
201 end = final_end;
202 else
203 {
204 // If we're not at the end of the text, make sure we break the line on white space.
205 while (end > start
206 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
207 end--;
208 }
209 sub_len = end - start;
210 std::string substring = text_string.substr (start, sub_len);
Greg Claytonc982c762010-07-09 20:39:50 +0000211 fprintf (out, "%*s%s\n", indent, "", substring.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000212 start = end + 1;
213 }
214 }
215}
216
217void
Greg Claytone0d378b2011-03-24 21:19:54 +0000218ShowUsage (FILE *out, OptionDefinition *option_table, Driver::OptionData data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000219{
220 uint32_t screen_width = 80;
221 uint32_t indent_level = 0;
222 const char *name = "lldb";
Jim Ingham86511212010-06-15 18:47:14 +0000223
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000224 fprintf (out, "\nUsage:\n\n");
225
226 indent_level += 2;
227
228
229 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
230 // <cmd> [options-for-level-1]
231 // etc.
232
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000233 uint32_t num_options;
Jim Ingham86511212010-06-15 18:47:14 +0000234 uint32_t num_option_sets = 0;
235
236 for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237 {
Jim Ingham86511212010-06-15 18:47:14 +0000238 uint32_t this_usage_mask = option_table[num_options].usage_mask;
239 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000240 {
Jim Ingham86511212010-06-15 18:47:14 +0000241 if (num_option_sets == 0)
242 num_option_sets = 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000243 }
244 else
245 {
Greg Claytonc982c762010-07-09 20:39:50 +0000246 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
Jim Ingham86511212010-06-15 18:47:14 +0000247 {
248 if (this_usage_mask & 1 << j)
249 {
250 if (num_option_sets <= j)
251 num_option_sets = j + 1;
252 }
253 }
254 }
255 }
256
257 for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++)
258 {
259 uint32_t opt_set_mask;
260
261 opt_set_mask = 1 << opt_set;
262
263 if (opt_set > 0)
264 fprintf (out, "\n");
Greg Claytonc982c762010-07-09 20:39:50 +0000265 fprintf (out, "%*s%s", indent_level, "", name);
Jim Ingham556e6532011-08-16 23:15:02 +0000266 bool is_help_line = false;
Jim Ingham86511212010-06-15 18:47:14 +0000267
268 for (uint32_t i = 0; i < num_options; ++i)
269 {
270 if (option_table[i].usage_mask & opt_set_mask)
271 {
Caroline Ticedeaab222010-10-01 19:59:14 +0000272 CommandArgumentType arg_type = option_table[i].argument_type;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000273 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
Jim Ingham556e6532011-08-16 23:15:02 +0000274 // This is a bit of a hack, but there's no way to say certain options don't have arguments yet...
275 // so we do it by hand here.
276 if (option_table[i].short_option == 'h')
277 is_help_line = true;
278
Jim Ingham86511212010-06-15 18:47:14 +0000279 if (option_table[i].required)
280 {
281 if (option_table[i].option_has_arg == required_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000282 fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000283 else if (option_table[i].option_has_arg == optional_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000284 fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000285 else
286 fprintf (out, " -%c", option_table[i].short_option);
287 }
288 else
289 {
290 if (option_table[i].option_has_arg == required_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000291 fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000292 else if (option_table[i].option_has_arg == optional_argument)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000293 fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name);
Jim Ingham86511212010-06-15 18:47:14 +0000294 else
295 fprintf (out, " [-%c]", option_table[i].short_option);
296 }
297 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000298 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000299 if (!is_help_line && (opt_set <= last_option_set_with_args))
Jim Ingham556e6532011-08-16 23:15:02 +0000300 fprintf (out, " [[--] <PROGRAM-ARG-1> [<PROGRAM_ARG-2> ...]]");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000301 }
302
303 fprintf (out, "\n\n");
304
305 // Now print out all the detailed information about the various options: long form, short form and help text:
306 // -- long_name <argument>
307 // - short <argument>
308 // help text
309
310 // This variable is used to keep track of which options' info we've printed out, because some options can be in
311 // more than one usage level, but we only want to print the long form of its information once.
312
313 Driver::OptionData::OptionSet options_seen;
314 Driver::OptionData::OptionSet::iterator pos;
315
316 indent_level += 5;
317
Jim Ingham86511212010-06-15 18:47:14 +0000318 for (uint32_t i = 0; i < num_options; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000319 {
320 // Only print this option if we haven't already seen it.
321 pos = options_seen.find (option_table[i].short_option);
322 if (pos == options_seen.end())
323 {
Caroline Ticedeaab222010-10-01 19:59:14 +0000324 CommandArgumentType arg_type = option_table[i].argument_type;
Greg Clayton9d0402b2011-02-20 02:15:07 +0000325 const char *arg_name = SBCommandInterpreter::GetArgumentTypeAsCString (arg_type);
Caroline Ticedeaab222010-10-01 19:59:14 +0000326
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000327 options_seen.insert (option_table[i].short_option);
Greg Claytonc982c762010-07-09 20:39:50 +0000328 fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option);
Caroline Ticedeaab222010-10-01 19:59:14 +0000329 if (arg_type != eArgTypeNone)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000330 fprintf (out, "<%s>", arg_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000331 fprintf (out, "\n");
Greg Claytonc982c762010-07-09 20:39:50 +0000332 fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option);
Caroline Ticedeaab222010-10-01 19:59:14 +0000333 if (arg_type != eArgTypeNone)
Greg Clayton9d0402b2011-02-20 02:15:07 +0000334 fprintf (out, "<%s>", arg_name);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 fprintf (out, "\n");
336 indent_level += 5;
Greg Claytonc982c762010-07-09 20:39:50 +0000337 OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000338 indent_level -= 5;
339 fprintf (out, "\n");
340 }
341 }
342
343 indent_level -= 5;
Jim Inghamf0c63b92014-02-05 21:35:09 +0000344
345 fprintf (out, "\n%*sNotes:\n",
346 indent_level, "");
347 indent_level += 5;
348
Jim Ingham47ea51f2013-09-17 01:53:35 +0000349 fprintf (out, "\n%*sMultiple \"-s\" and \"-o\" options can be provided. They will be processed from left to right in order, "
350 "\n%*swith the source files and commands interleaved. The same is true of the \"-S\" and \"-O\" options."
351 "\n%*sThe before file and after file sets can intermixed freely, the command parser will sort them out."
352 "\n%*sThe order of the file specifiers (\"-c\", \"-f\", etc.) is not significant in this regard.\n\n",
353 indent_level, "",
354 indent_level, "",
355 indent_level, "",
356 indent_level, "");
357
Jim Inghamf0c63b92014-02-05 21:35:09 +0000358 fprintf (out, "\n%*sIf you don't provide -f then the first argument will be the file to be debugged"
359 "\n%*swhich means that '%s -- <filename> [<ARG1> [<ARG2>]]' also works."
360 "\n%*sBut remember to end the options with \"--\" if any of your arguments have a \"-\" in them.\n\n",
Jim Inghama9deaf92011-08-16 23:57:58 +0000361 indent_level, "",
362 indent_level, "",
363 name,
364 indent_level, "");
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000365}
366
367void
Greg Claytone0d378b2011-03-24 21:19:54 +0000368BuildGetOptTable (OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table,
Caroline Tice4ab31c92010-10-12 21:57:09 +0000369 uint32_t num_options)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000370{
371 if (num_options == 0)
372 return;
373
374 uint32_t i;
375 uint32_t j;
376 std::bitset<256> option_seen;
377
Caroline Tice4ab31c92010-10-12 21:57:09 +0000378 getopt_table.resize (num_options + 1);
379
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000380 for (i = 0, j = 0; i < num_options; ++i)
Greg Claytonc982c762010-07-09 20:39:50 +0000381 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 char short_opt = expanded_option_table[i].short_option;
Greg Claytonc982c762010-07-09 20:39:50 +0000383
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000384 if (option_seen.test(short_opt) == false)
Greg Claytonc982c762010-07-09 20:39:50 +0000385 {
Caroline Tice4ab31c92010-10-12 21:57:09 +0000386 getopt_table[j].name = expanded_option_table[i].long_option;
387 getopt_table[j].has_arg = expanded_option_table[i].option_has_arg;
388 getopt_table[j].flag = NULL;
389 getopt_table[j].val = expanded_option_table[i].short_option;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000390 option_seen.set(short_opt);
391 ++j;
Greg Claytonc982c762010-07-09 20:39:50 +0000392 }
393 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000394
Caroline Tice4ab31c92010-10-12 21:57:09 +0000395 getopt_table[j].name = NULL;
396 getopt_table[j].has_arg = 0;
397 getopt_table[j].flag = NULL;
398 getopt_table[j].val = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399
400}
401
Greg Clayton66111032010-06-23 01:19:29 +0000402Driver::OptionData::OptionData () :
Greg Clayton8d846da2010-12-08 22:23:24 +0000403 m_args(),
Greg Clayton66111032010-06-23 01:19:29 +0000404 m_script_lang (lldb::eScriptLanguageDefault),
Johnny Cheneb46f782012-08-15 22:10:42 +0000405 m_core_file (),
Greg Claytonc982c762010-07-09 20:39:50 +0000406 m_crash_log (),
Jim Inghamed3252f2013-09-14 00:20:24 +0000407 m_initial_commands (),
408 m_after_file_commands (),
Jim Ingham4add3b12014-11-19 01:28:13 +0000409 m_after_crash_commands(),
Greg Clayton66111032010-06-23 01:19:29 +0000410 m_debug_mode (false),
Jim Inghamed3252f2013-09-14 00:20:24 +0000411 m_source_quietly(false),
Greg Claytonc982c762010-07-09 20:39:50 +0000412 m_print_version (false),
Jim Inghame2231ac2012-12-21 22:22:26 +0000413 m_print_python_path (false),
Greg Clayton66111032010-06-23 01:19:29 +0000414 m_print_help (false),
Jim Inghame64f0dc2011-09-13 23:25:31 +0000415 m_wait_for(false),
416 m_process_name(),
417 m_process_pid(LLDB_INVALID_PROCESS_ID),
Daniel Dunbara08823f2011-10-31 22:50:49 +0000418 m_use_external_editor(false),
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000419 m_batch(false),
Stephen Wilson71c21d12011-04-11 19:41:40 +0000420 m_seen_options()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421{
Greg Clayton66111032010-06-23 01:19:29 +0000422}
423
424Driver::OptionData::~OptionData ()
425{
426}
427
428void
429Driver::OptionData::Clear ()
430{
Greg Clayton8d846da2010-12-08 22:23:24 +0000431 m_args.clear ();
Greg Clayton66111032010-06-23 01:19:29 +0000432 m_script_lang = lldb::eScriptLanguageDefault;
Jim Inghamed3252f2013-09-14 00:20:24 +0000433 m_initial_commands.clear ();
434 m_after_file_commands.clear ();
Jim Ingham4add3b12014-11-19 01:28:13 +0000435 // If there is a local .lldbinit, source that:
436 SBFileSpec local_lldbinit("./.lldbinit", true);
437 if (local_lldbinit.Exists())
438 {
439 char path[2048];
440 local_lldbinit.GetPath(path, 2047);
441 m_after_file_commands.push_back (std::pair<bool, std::string> (true, path));
442 }
443
Greg Clayton66111032010-06-23 01:19:29 +0000444 m_debug_mode = false;
Jim Inghamed3252f2013-09-14 00:20:24 +0000445 m_source_quietly = false;
Greg Clayton66111032010-06-23 01:19:29 +0000446 m_print_help = false;
447 m_print_version = false;
Jim Inghame2231ac2012-12-21 22:22:26 +0000448 m_print_python_path = false;
Jim Inghame40e4212010-08-30 19:44:40 +0000449 m_use_external_editor = false;
Jim Inghame64f0dc2011-09-13 23:25:31 +0000450 m_wait_for = false;
451 m_process_name.erase();
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000452 m_batch = false;
Jim Ingham4add3b12014-11-19 01:28:13 +0000453 m_after_crash_commands.clear();
454
Jim Inghame64f0dc2011-09-13 23:25:31 +0000455 m_process_pid = LLDB_INVALID_PROCESS_ID;
Greg Clayton66111032010-06-23 01:19:29 +0000456}
457
458void
Jim Ingham4add3b12014-11-19 01:28:13 +0000459Driver::OptionData::AddInitialCommand (const char *command, CommandPlacement placement, bool is_file, SBError &error)
Jim Inghamed3252f2013-09-14 00:20:24 +0000460{
461 std::vector<std::pair<bool, std::string> > *command_set;
Jim Ingham4add3b12014-11-19 01:28:13 +0000462 switch (placement)
463 {
464 case eCommandPlacementBeforeFile:
Jim Inghamed3252f2013-09-14 00:20:24 +0000465 command_set = &(m_initial_commands);
Jim Ingham4add3b12014-11-19 01:28:13 +0000466 break;
467 case eCommandPlacementAfterFile:
Jim Inghamed3252f2013-09-14 00:20:24 +0000468 command_set = &(m_after_file_commands);
Jim Ingham4add3b12014-11-19 01:28:13 +0000469 break;
470 case eCommandPlacementAfterCrash:
471 command_set = &(m_after_crash_commands);
472 break;
473 }
Jim Inghamed3252f2013-09-14 00:20:24 +0000474
475 if (is_file)
476 {
477 SBFileSpec file(command);
478 if (file.Exists())
479 command_set->push_back (std::pair<bool, std::string> (true, optarg));
480 else if (file.ResolveExecutableLocation())
481 {
482 char final_path[PATH_MAX];
483 file.GetPath (final_path, sizeof(final_path));
484 std::string path_str (final_path);
485 command_set->push_back (std::pair<bool, std::string> (true, path_str));
486 }
487 else
488 error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg);
489 }
490 else
491 command_set->push_back (std::pair<bool, std::string> (false, optarg));
492}
493
494void
Greg Clayton66111032010-06-23 01:19:29 +0000495Driver::ResetOptionValues ()
496{
497 m_option_data.Clear ();
498}
499
500const char *
501Driver::GetFilename() const
502{
Greg Clayton8d846da2010-12-08 22:23:24 +0000503 if (m_option_data.m_args.empty())
Greg Clayton66111032010-06-23 01:19:29 +0000504 return NULL;
Greg Clayton8d846da2010-12-08 22:23:24 +0000505 return m_option_data.m_args.front().c_str();
Greg Clayton66111032010-06-23 01:19:29 +0000506}
507
508const char *
509Driver::GetCrashLogFilename() const
510{
511 if (m_option_data.m_crash_log.empty())
512 return NULL;
513 return m_option_data.m_crash_log.c_str();
514}
515
516lldb::ScriptLanguage
517Driver::GetScriptLanguage() const
518{
519 return m_option_data.m_script_lang;
520}
521
Jim Inghamed3252f2013-09-14 00:20:24 +0000522void
Jim Ingham4add3b12014-11-19 01:28:13 +0000523Driver::WriteCommandsForSourcing (CommandPlacement placement, SBStream &strm)
Greg Clayton66111032010-06-23 01:19:29 +0000524{
Jim Ingham4add3b12014-11-19 01:28:13 +0000525 std::vector<std::pair<bool, std::string> > *command_set;
526 switch (placement)
527 {
528 case eCommandPlacementBeforeFile:
529 command_set = &m_option_data.m_initial_commands;
530 break;
531 case eCommandPlacementAfterFile:
532 command_set = &m_option_data.m_after_file_commands;
533 break;
534 case eCommandPlacementAfterCrash:
535 command_set = &m_option_data.m_after_crash_commands;
536 break;
537 }
Jim Inghamed3252f2013-09-14 00:20:24 +0000538
Jim Ingham4add3b12014-11-19 01:28:13 +0000539 for (const auto &command_pair : *command_set)
Jim Inghamed3252f2013-09-14 00:20:24 +0000540 {
Greg Clayton06357c92014-07-30 17:38:47 +0000541 const char *command = command_pair.second.c_str();
542 if (command_pair.first)
543 strm.Printf("command source -s %i '%s'\n", m_option_data.m_source_quietly, command);
544 else
545 strm.Printf("%s\n", command);
Jim Inghamed3252f2013-09-14 00:20:24 +0000546 }
Greg Clayton66111032010-06-23 01:19:29 +0000547}
548
549bool
550Driver::GetDebugMode() const
551{
552 return m_option_data.m_debug_mode;
553}
554
555
556// Check the arguments that were passed to this program to make sure they are valid and to get their
557// argument values (if any). Return a boolean value indicating whether or not to start up the full
558// debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR
559// if the user only wanted help or version information.
560
561SBError
Deepak Panickal429222c2013-10-15 15:46:40 +0000562Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exiting)
Greg Clayton66111032010-06-23 01:19:29 +0000563{
564 ResetOptionValues ();
565
566 SBCommandReturnObject result;
567
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000568 SBError error;
569 std::string option_string;
570 struct option *long_options = NULL;
Caroline Tice4ab31c92010-10-12 21:57:09 +0000571 std::vector<struct option> long_options_vector;
Greg Claytonc982c762010-07-09 20:39:50 +0000572 uint32_t num_options;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573
Greg Claytonc982c762010-07-09 20:39:50 +0000574 for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
575 /* Do Nothing. */;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576
577 if (num_options == 0)
578 {
579 if (argc > 1)
580 error.SetErrorStringWithFormat ("invalid number of options");
581 return error;
582 }
583
Caroline Tice4ab31c92010-10-12 21:57:09 +0000584 BuildGetOptTable (g_options, long_options_vector, num_options);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585
Caroline Tice4ab31c92010-10-12 21:57:09 +0000586 if (long_options_vector.empty())
587 long_options = NULL;
588 else
589 long_options = &long_options_vector.front();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590
591 if (long_options == NULL)
592 {
593 error.SetErrorStringWithFormat ("invalid long options");
594 return error;
595 }
596
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000597 // Build the option_string argument for call to getopt_long_only.
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598
599 for (int i = 0; long_options[i].name != NULL; ++i)
600 {
601 if (long_options[i].flag == NULL)
602 {
603 option_string.push_back ((char) long_options[i].val);
604 switch (long_options[i].has_arg)
605 {
606 default:
607 case no_argument:
608 break;
609 case required_argument:
610 option_string.push_back (':');
611 break;
612 case optional_argument:
613 option_string.append ("::");
614 break;
615 }
616 }
617 }
618
Jim Ingham06942692011-08-13 00:22:20 +0000619 // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't
620 // know at that point whether we should read in init files yet. So we don't read them in in the
621 // Driver constructor, then set the flags back to "read them in" here, and then if we see the
622 // "-n" flag, we'll turn it off again. Finally we have to read them in by hand later in the
623 // main loop.
624
625 m_debugger.SkipLLDBInitFiles (false);
626 m_debugger.SkipAppInitFiles (false);
627
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000628 // Prepare for & make calls to getopt_long_only.
Eli Friedmanadb35022010-06-13 19:18:49 +0000629#if __GLIBC__
630 optind = 0;
631#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 optreset = 1;
633 optind = 1;
Eli Friedmanadb35022010-06-13 19:18:49 +0000634#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000635 int val;
636 while (1)
637 {
638 int long_options_index = -1;
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000639 val = ::getopt_long_only (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000640
641 if (val == -1)
642 break;
643 else if (val == '?')
644 {
Greg Clayton66111032010-06-23 01:19:29 +0000645 m_option_data.m_print_help = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000646 error.SetErrorStringWithFormat ("unknown or ambiguous option");
647 break;
648 }
649 else if (val == 0)
650 continue;
651 else
652 {
Greg Clayton66111032010-06-23 01:19:29 +0000653 m_option_data.m_seen_options.insert ((char) val);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000654 if (long_options_index == -1)
655 {
656 for (int i = 0;
657 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
658 ++i)
659 {
660 if (long_options[i].val == val)
661 {
662 long_options_index = i;
663 break;
664 }
665 }
666 }
667
668 if (long_options_index >= 0)
669 {
Greg Clayton3bcdfc02012-12-04 00:32:51 +0000670 const int short_option = g_options[long_options_index].short_option;
Greg Clayton66111032010-06-23 01:19:29 +0000671
672 switch (short_option)
673 {
674 case 'h':
675 m_option_data.m_print_help = true;
676 break;
677
678 case 'v':
679 m_option_data.m_print_version = true;
680 break;
681
Jim Inghame2231ac2012-12-21 22:22:26 +0000682 case 'P':
683 m_option_data.m_print_python_path = true;
684 break;
685
Jim Inghamffc9f1d2014-10-14 01:20:07 +0000686 case 'b':
687 m_option_data.m_batch = true;
688 break;
689
Greg Clayton66111032010-06-23 01:19:29 +0000690 case 'c':
Johnny Cheneb46f782012-08-15 22:10:42 +0000691 {
692 SBFileSpec file(optarg);
693 if (file.Exists())
694 {
695 m_option_data.m_core_file = optarg;
696 }
697 else
698 error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg);
699 }
Greg Clayton66111032010-06-23 01:19:29 +0000700 break;
Johnny Cheneb46f782012-08-15 22:10:42 +0000701
Jim Inghame40e4212010-08-30 19:44:40 +0000702 case 'e':
703 m_option_data.m_use_external_editor = true;
704 break;
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000705
Jim Inghame64f0dc2011-09-13 23:25:31 +0000706 case 'x':
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000707 m_debugger.SkipLLDBInitFiles (true);
Jim Ingham06942692011-08-13 00:22:20 +0000708 m_debugger.SkipAppInitFiles (true);
Greg Clayton6eee5aa2010-10-11 01:05:37 +0000709 break;
710
Jim Inghamed3252f2013-09-14 00:20:24 +0000711 case 'X':
Michael Sartainc3ce7f272013-05-23 20:47:45 +0000712 m_debugger.SetUseColor (false);
713 break;
714
Greg Clayton66111032010-06-23 01:19:29 +0000715 case 'f':
716 {
717 SBFileSpec file(optarg);
718 if (file.Exists())
Greg Clayton8d846da2010-12-08 22:23:24 +0000719 {
720 m_option_data.m_args.push_back (optarg);
721 }
Caroline Tice428a9a52010-09-10 04:48:55 +0000722 else if (file.ResolveExecutableLocation())
723 {
724 char path[PATH_MAX];
Johnny Chen25f3a3c2011-08-10 22:06:24 +0000725 file.GetPath (path, sizeof(path));
Greg Clayton8d846da2010-12-08 22:23:24 +0000726 m_option_data.m_args.push_back (path);
Caroline Tice428a9a52010-09-10 04:48:55 +0000727 }
Greg Clayton66111032010-06-23 01:19:29 +0000728 else
729 error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
730 }
731 break;
732
733 case 'a':
734 if (!m_debugger.SetDefaultArchitecture (optarg))
735 error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
736 break;
737
738 case 'l':
739 m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
740 break;
741
742 case 'd':
743 m_option_data.m_debug_mode = true;
744 break;
745
Jim Inghamf0c63b92014-02-05 21:35:09 +0000746 case 'Q':
Jim Inghamed3252f2013-09-14 00:20:24 +0000747 m_option_data.m_source_quietly = true;
748 break;
749
Jim Ingham661f29d2014-11-20 23:37:13 +0000750 case 'K':
Jim Ingham4add3b12014-11-19 01:28:13 +0000751 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterCrash, true, error);
752 break;
Jim Ingham661f29d2014-11-20 23:37:13 +0000753 case 'k':
Jim Ingham4add3b12014-11-19 01:28:13 +0000754 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterCrash, false, error);
755 break;
756
Jim Inghame64f0dc2011-09-13 23:25:31 +0000757 case 'n':
758 m_option_data.m_process_name = optarg;
759 break;
760
761 case 'w':
762 m_option_data.m_wait_for = true;
763 break;
764
765 case 'p':
766 {
767 char *remainder;
768 m_option_data.m_process_pid = strtol (optarg, &remainder, 0);
769 if (remainder == optarg || *remainder != '\0')
770 error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.",
771 optarg);
772 }
773 break;
Greg Clayton66111032010-06-23 01:19:29 +0000774 case 's':
Jim Ingham4add3b12014-11-19 01:28:13 +0000775 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterFile, true, error);
Greg Clayton66111032010-06-23 01:19:29 +0000776 break;
Jim Inghamed3252f2013-09-14 00:20:24 +0000777 case 'o':
Jim Ingham4add3b12014-11-19 01:28:13 +0000778 m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterFile, false, error);
Jim Inghamed3252f2013-09-14 00:20:24 +0000779 break;
780 case 'S':
Jim Ingham4add3b12014-11-19 01:28:13 +0000781 m_option_data.AddInitialCommand(optarg, eCommandPlacementBeforeFile, true, error);
Jim Inghamed3252f2013-09-14 00:20:24 +0000782 break;
783 case 'O':
Jim Ingham4add3b12014-11-19 01:28:13 +0000784 m_option_data.AddInitialCommand(optarg, eCommandPlacementBeforeFile, false, error);
Jim Inghamed3252f2013-09-14 00:20:24 +0000785 break;
Greg Clayton66111032010-06-23 01:19:29 +0000786 default:
787 m_option_data.m_print_help = true;
788 error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
789 break;
790 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791 }
792 else
793 {
794 error.SetErrorStringWithFormat ("invalid option with value %i", val);
795 }
796 if (error.Fail())
Caroline Tice4ab31c92010-10-12 21:57:09 +0000797 {
Greg Clayton66111032010-06-23 01:19:29 +0000798 return error;
Caroline Tice4ab31c92010-10-12 21:57:09 +0000799 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800 }
801 }
Jim Ingham86511212010-06-15 18:47:14 +0000802
Greg Clayton66111032010-06-23 01:19:29 +0000803 if (error.Fail() || m_option_data.m_print_help)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 {
805 ShowUsage (out_fh, g_options, m_option_data);
Deepak Panickal429222c2013-10-15 15:46:40 +0000806 exiting = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000807 }
808 else if (m_option_data.m_print_version)
809 {
Greg Clayton66111032010-06-23 01:19:29 +0000810 ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
Deepak Panickal429222c2013-10-15 15:46:40 +0000811 exiting = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812 }
Jim Inghame2231ac2012-12-21 22:22:26 +0000813 else if (m_option_data.m_print_python_path)
814 {
815 SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath();
816 if (python_file_spec.IsValid())
817 {
818 char python_path[PATH_MAX];
819 size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX);
820 if (num_chars < PATH_MAX)
821 {
822 ::fprintf (out_fh, "%s\n", python_path);
823 }
824 else
825 ::fprintf (out_fh, "<PATH TOO LONG>\n");
826 }
827 else
828 ::fprintf (out_fh, "<COULD NOT FIND PATH>\n");
Deepak Panickal429222c2013-10-15 15:46:40 +0000829 exiting = true;
Jim Inghame2231ac2012-12-21 22:22:26 +0000830 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000831 else if (m_option_data.m_process_name.empty() && m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000832 {
Greg Clayton8d846da2010-12-08 22:23:24 +0000833 // Any arguments that are left over after option parsing are for
834 // the program. If a file was specified with -f then the filename
835 // is already in the m_option_data.m_args array, and any remaining args
836 // are arguments for the inferior program. If no file was specified with
837 // -f, then what is left is the program name followed by any arguments.
838
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000839 // Skip any options we consumed with getopt_long_only
Greg Clayton8d846da2010-12-08 22:23:24 +0000840 argc -= optind;
841 argv += optind;
842
843 if (argc > 0)
844 {
845 for (int arg_idx=0; arg_idx<argc; ++arg_idx)
846 {
847 const char *arg = argv[arg_idx];
848 if (arg)
849 m_option_data.m_args.push_back (arg);
850 }
851 }
852
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000853 }
Jim Inghame64f0dc2011-09-13 23:25:31 +0000854 else
855 {
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000856 // Skip any options we consumed with getopt_long_only
Jim Inghame64f0dc2011-09-13 23:25:31 +0000857 argc -= optind;
Greg Clayton23f59502012-07-17 03:23:13 +0000858 //argv += optind; // Commented out to keep static analyzer happy
Jim Inghame64f0dc2011-09-13 23:25:31 +0000859
860 if (argc > 0)
861 ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n");
862 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000863
Greg Clayton66111032010-06-23 01:19:29 +0000864 return error;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000865}
866
Jim Ingham4add3b12014-11-19 01:28:13 +0000867static ::FILE *
868PrepareCommandsForSourcing (const char *commands_data, size_t commands_size, int fds[2])
869{
870 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
871
872 bool success = true;
873 ::FILE *commands_file = NULL;
874 fds[0] = -1;
875 fds[1] = -1;
876 int err = 0;
877#ifdef _WIN32
878 err = _pipe(fds, commands_size, O_BINARY);
879#else
880 err = pipe(fds);
881#endif
882 if (err == 0)
883 {
884 ssize_t nrwr = write(fds[WRITE], commands_data, commands_size);
885 if (nrwr < 0)
886 {
887 fprintf(stderr, "error: write(%i, %p, %zd) failed (errno = %i) "
888 "when trying to open LLDB commands pipe\n",
889 fds[WRITE], commands_data, commands_size, errno);
890 success = false;
891 }
892 else if (static_cast<size_t>(nrwr) == commands_size)
893 {
894 // Close the write end of the pipe so when we give the read end to
895 // the debugger/command interpreter it will exit when it consumes all
896 // of the data
897#ifdef _WIN32
898 _close(fds[WRITE]); fds[WRITE] = -1;
899#else
900 close(fds[WRITE]); fds[WRITE] = -1;
901#endif
902 // Now open the read file descriptor in a FILE * that we can give to
903 // the debugger as an input handle
904 commands_file = fdopen(fds[READ], "r");
905 if (commands_file)
906 {
907 fds[READ] = -1; // The FILE * 'commands_file' now owns the read descriptor
908 // Hand ownership if the FILE * over to the debugger for "commands_file".
909 }
910 else
911 {
912 fprintf(stderr,
913 "error: fdopen(%i, \"r\") failed (errno = %i) when "
914 "trying to open LLDB commands pipe\n",
915 fds[READ], errno);
916 success = false;
917 }
918 }
919 }
920 else
921 {
922 fprintf(stderr, "error: can't create pipe file descriptors for LLDB commands\n");
923 success = false;
924 }
925
926 return commands_file;
927}
928
929void
930CleanupAfterCommandSourcing (int fds[2])
931{
932 enum PIPES { READ, WRITE }; // Constants 0 and 1 for READ and WRITE
933
934 // Close any pipes that we still have ownership of
935 if ( fds[WRITE] != -1)
936 {
937#ifdef _WIN32
938 _close(fds[WRITE]); fds[WRITE] = -1;
939#else
940 close(fds[WRITE]); fds[WRITE] = -1;
941#endif
942
943 }
944
945 if ( fds[READ] != -1)
946 {
947#ifdef _WIN32
948 _close(fds[READ]); fds[READ] = -1;
949#else
950 close(fds[READ]); fds[READ] = -1;
951#endif
952 }
953
954}
955
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000956void
957Driver::MainLoop ()
958{
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000959 if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
Greg Claytonf571b892012-02-02 19:28:31 +0000960 {
961 g_old_stdin_termios_is_valid = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000962 atexit (reset_stdin_termios);
Greg Claytonf571b892012-02-02 19:28:31 +0000963 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000964
965 ::setbuf (stdin, NULL);
966 ::setbuf (stdout, NULL);
967
Greg Clayton66111032010-06-23 01:19:29 +0000968 m_debugger.SetErrorFileHandle (stderr, false);
969 m_debugger.SetOutputFileHandle (stdout, false);
Greg Clayton06357c92014-07-30 17:38:47 +0000970 m_debugger.SetInputFileHandle (stdin, false); // Don't take ownership of STDIN yet...
971
Jim Inghame40e4212010-08-30 19:44:40 +0000972 m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000973
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000974 struct winsize window_size;
975 if (isatty (STDIN_FILENO)
976 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
977 {
Caroline Tice3df9a8d2010-09-04 00:03:46 +0000978 if (window_size.ws_col > 0)
Greg Claytona7015092010-09-18 01:14:36 +0000979 m_debugger.SetTerminalWidth (window_size.ws_col);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000980 }
981
Greg Clayton44d93782014-01-27 23:43:24 +0000982 SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000983
Greg Clayton44d93782014-01-27 23:43:24 +0000984 // Before we handle any options from the command line, we parse the
985 // .lldbinit file in the user's home directory.
986 SBCommandReturnObject result;
987 sb_interpreter.SourceInitFileInHomeDirectory(result);
988 if (GetDebugMode())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000989 {
Greg Clayton44d93782014-01-27 23:43:24 +0000990 result.PutError (m_debugger.GetErrorFileHandle());
991 result.PutOutput (m_debugger.GetOutputFileHandle());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000992 }
Greg Clayton44d93782014-01-27 23:43:24 +0000993
994 // Now we handle options we got from the command line
Ed Mastef8314532014-07-30 19:26:11 +0000995 SBStream commands_stream;
Greg Clayton06357c92014-07-30 17:38:47 +0000996
Greg Clayton44d93782014-01-27 23:43:24 +0000997 // First source in the commands specified to be run before the file arguments are processed.
Jim Ingham4add3b12014-11-19 01:28:13 +0000998 WriteCommandsForSourcing(eCommandPlacementBeforeFile, commands_stream);
Greg Clayton06357c92014-07-30 17:38:47 +0000999
Greg Clayton44d93782014-01-27 23:43:24 +00001000 const size_t num_args = m_option_data.m_args.size();
1001 if (num_args > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001002 {
Jim Ingham5b1fe952014-08-07 23:01:31 +00001003 char arch_name[64];
1004 if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
1005 commands_stream.Printf("target create --arch=%s \"%s\"", arch_name, m_option_data.m_args[0].c_str());
1006 else
1007 commands_stream.Printf("target create \"%s\"", m_option_data.m_args[0].c_str());
1008
Greg Clayton06357c92014-07-30 17:38:47 +00001009 if (!m_option_data.m_core_file.empty())
1010 {
1011 commands_stream.Printf(" --core \"%s\"", m_option_data.m_core_file.c_str());
1012 }
1013 commands_stream.Printf("\n");
Greg Clayton44d93782014-01-27 23:43:24 +00001014
1015 if (num_args > 1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001016 {
Greg Clayton06357c92014-07-30 17:38:47 +00001017 commands_stream.Printf ("settings set -- target.run-args ");
Greg Clayton44d93782014-01-27 23:43:24 +00001018 for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001019 {
Greg Clayton06357c92014-07-30 17:38:47 +00001020 const char *arg_cstr = m_option_data.m_args[arg_idx].c_str();
1021 if (strchr(arg_cstr, '"') == NULL)
1022 commands_stream.Printf(" \"%s\"", arg_cstr);
1023 else
1024 commands_stream.Printf(" '%s'", arg_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001025 }
Greg Clayton06357c92014-07-30 17:38:47 +00001026 commands_stream.Printf("\n");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001027 }
1028 }
Greg Clayton06357c92014-07-30 17:38:47 +00001029 else if (!m_option_data.m_core_file.empty())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001030 {
Greg Clayton06357c92014-07-30 17:38:47 +00001031 commands_stream.Printf("target create --core \"%s\"\n", m_option_data.m_core_file.c_str());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001032 }
Greg Clayton9c0b64c2014-02-06 18:22:44 +00001033 else if (!m_option_data.m_process_name.empty())
1034 {
Greg Clayton06357c92014-07-30 17:38:47 +00001035 commands_stream.Printf ("process attach --name \"%s\"", m_option_data.m_process_name.c_str());
1036
1037 if (m_option_data.m_wait_for)
1038 commands_stream.Printf(" --waitfor");
1039
1040 commands_stream.Printf("\n");
1041
Greg Clayton9c0b64c2014-02-06 18:22:44 +00001042 }
1043 else if (LLDB_INVALID_PROCESS_ID != m_option_data.m_process_pid)
1044 {
Greg Clayton06357c92014-07-30 17:38:47 +00001045 commands_stream.Printf ("process attach --pid %" PRIu64 "\n", m_option_data.m_process_pid);
Greg Clayton9c0b64c2014-02-06 18:22:44 +00001046 }
Greg Clayton44d93782014-01-27 23:43:24 +00001047
Jim Ingham4add3b12014-11-19 01:28:13 +00001048 WriteCommandsForSourcing(eCommandPlacementAfterFile, commands_stream);
Greg Clayton06357c92014-07-30 17:38:47 +00001049
Greg Clayton44d93782014-01-27 23:43:24 +00001050 if (GetDebugMode())
1051 {
1052 result.PutError(m_debugger.GetErrorFileHandle());
1053 result.PutOutput(m_debugger.GetOutputFileHandle());
1054 }
1055
1056 bool handle_events = true;
1057 bool spawn_thread = false;
Greg Clayton06357c92014-07-30 17:38:47 +00001058
1059 // Check if we have any data in the commands stream, and if so, save it to a temp file
Greg Clayton49668762014-08-01 18:32:07 +00001060 // so we can then run the command interpreter using the file contents.
1061 const char *commands_data = commands_stream.GetData();
1062 const size_t commands_size = commands_stream.GetSize();
Jim Ingham26c7bf92014-10-11 00:38:27 +00001063
1064 // The command file might have requested that we quit, this variable will track that.
1065 bool quit_requested = false;
Jim Inghamffc9f1d2014-10-14 01:20:07 +00001066 bool stopped_for_crash = false;
Greg Clayton49668762014-08-01 18:32:07 +00001067 if (commands_data && commands_size)
Greg Clayton06357c92014-07-30 17:38:47 +00001068 {
Jim Ingham4add3b12014-11-19 01:28:13 +00001069 int initial_commands_fds[2];
Greg Clayton49668762014-08-01 18:32:07 +00001070 bool success = true;
Jim Ingham4add3b12014-11-19 01:28:13 +00001071 FILE *commands_file = PrepareCommandsForSourcing (commands_data, commands_size, initial_commands_fds);
1072 if (commands_file)
Greg Clayton49668762014-08-01 18:32:07 +00001073 {
Jim Ingham4add3b12014-11-19 01:28:13 +00001074 m_debugger.SetInputFileHandle (commands_file, true);
1075
1076 // Set the debugger into Sync mode when running the command file. Otherwise command files
1077 // that run the target won't run in a sensible way.
1078 bool old_async = m_debugger.GetAsync();
1079 m_debugger.SetAsync(false);
1080 int num_errors;
1081
1082 SBCommandInterpreterRunOptions options;
1083 options.SetStopOnError (true);
1084 if (m_option_data.m_batch)
1085 options.SetStopOnCrash (true);
1086
1087 m_debugger.RunCommandInterpreter(handle_events,
1088 spawn_thread,
1089 options,
1090 num_errors,
1091 quit_requested,
1092 stopped_for_crash);
1093
1094 if (m_option_data.m_batch && stopped_for_crash && !m_option_data.m_after_crash_commands.empty())
Saleem Abdulrasool9e5b4982014-09-08 02:47:13 +00001095 {
Jim Ingham4add3b12014-11-19 01:28:13 +00001096 int crash_command_fds[2];
1097 SBStream crash_commands_stream;
1098 WriteCommandsForSourcing (eCommandPlacementAfterCrash, crash_commands_stream);
1099 const char *crash_commands_data = crash_commands_stream.GetData();
1100 const size_t crash_commands_size = crash_commands_stream.GetSize();
1101 commands_file = PrepareCommandsForSourcing (crash_commands_data, crash_commands_size, crash_command_fds);
Greg Clayton49668762014-08-01 18:32:07 +00001102 if (commands_file)
1103 {
Jim Ingham4add3b12014-11-19 01:28:13 +00001104 bool local_quit_requested;
1105 bool local_stopped_for_crash;
Greg Clayton49668762014-08-01 18:32:07 +00001106 m_debugger.SetInputFileHandle (commands_file, true);
Jim Ingham26c7bf92014-10-11 00:38:27 +00001107
Jim Inghamffc9f1d2014-10-14 01:20:07 +00001108 m_debugger.RunCommandInterpreter(handle_events,
1109 spawn_thread,
1110 options,
1111 num_errors,
Jim Ingham4add3b12014-11-19 01:28:13 +00001112 local_quit_requested,
1113 local_stopped_for_crash);
1114 if (local_quit_requested)
1115 quit_requested = true;
1116
Greg Clayton49668762014-08-01 18:32:07 +00001117 }
Greg Clayton06357c92014-07-30 17:38:47 +00001118 }
Jim Ingham4add3b12014-11-19 01:28:13 +00001119 m_debugger.SetAsync(old_async);
Greg Clayton06357c92014-07-30 17:38:47 +00001120 }
Greg Clayton49668762014-08-01 18:32:07 +00001121 else
Greg Clayton49668762014-08-01 18:32:07 +00001122 success = false;
Greg Clayton49668762014-08-01 18:32:07 +00001123
1124 // Close any pipes that we still have ownership of
Jim Ingham4add3b12014-11-19 01:28:13 +00001125 CleanupAfterCommandSourcing(initial_commands_fds);
Greg Clayton49668762014-08-01 18:32:07 +00001126
1127 // Something went wrong with command pipe
1128 if (!success)
1129 {
1130 exit(1);
1131 }
1132
Greg Clayton06357c92014-07-30 17:38:47 +00001133 }
1134
1135 // Now set the input file handle to STDIN and run the command
1136 // interpreter again in interactive mode and let the debugger
1137 // take ownership of stdin
Jim Ingham26c7bf92014-10-11 00:38:27 +00001138
Jim Inghamffc9f1d2014-10-14 01:20:07 +00001139 bool go_interactive = true;
1140 if (quit_requested)
1141 go_interactive = false;
1142 else if (m_option_data.m_batch && !stopped_for_crash)
1143 go_interactive = false;
1144
1145 if (go_interactive)
Jim Ingham26c7bf92014-10-11 00:38:27 +00001146 {
1147 m_debugger.SetInputFileHandle (stdin, true);
1148 m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
1149 }
Greg Clayton44d93782014-01-27 23:43:24 +00001150
1151 reset_stdin_termios();
1152 fclose (stdin);
1153
1154 SBDebugger::Destroy (m_debugger);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155}
1156
Greg Clayton44d93782014-01-27 23:43:24 +00001157
Jim Inghamc46fe7c2013-02-22 22:56:55 +00001158void
1159Driver::ResizeWindow (unsigned short col)
1160{
1161 GetDebugger().SetTerminalWidth (col);
Jim Inghamc46fe7c2013-02-22 22:56:55 +00001162}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001163
Caroline Ticedd759852010-09-09 17:45:09 +00001164void
1165sigwinch_handler (int signo)
1166{
1167 struct winsize window_size;
1168 if (isatty (STDIN_FILENO)
1169 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1170 {
Jim Ingham57190ba2012-04-26 21:39:32 +00001171 if ((window_size.ws_col > 0) && g_driver != NULL)
Caroline Ticedd759852010-09-09 17:45:09 +00001172 {
Jim Inghamc46fe7c2013-02-22 22:56:55 +00001173 g_driver->ResizeWindow (window_size.ws_col);
Caroline Ticedd759852010-09-09 17:45:09 +00001174 }
1175 }
1176}
1177
Caroline Ticeefed6132010-11-19 20:47:54 +00001178void
1179sigint_handler (int signo)
1180{
1181 static bool g_interrupt_sent = false;
1182 if (g_driver)
1183 {
1184 if (!g_interrupt_sent)
1185 {
1186 g_interrupt_sent = true;
1187 g_driver->GetDebugger().DispatchInputInterrupt();
1188 g_interrupt_sent = false;
1189 return;
1190 }
1191 }
1192
1193 exit (signo);
1194}
1195
Jim Inghamc5917d92012-11-30 20:23:19 +00001196void
1197sigtstp_handler (int signo)
1198{
1199 g_driver->GetDebugger().SaveInputTerminalState();
1200 signal (signo, SIG_DFL);
1201 kill (getpid(), signo);
1202 signal (signo, sigtstp_handler);
1203}
1204
1205void
1206sigcont_handler (int signo)
1207{
1208 g_driver->GetDebugger().RestoreInputTerminalState();
1209 signal (signo, SIG_DFL);
1210 kill (getpid(), signo);
1211 signal (signo, sigcont_handler);
1212}
1213
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001214int
Jim Inghama462f5c2011-01-27 20:15:39 +00001215main (int argc, char const *argv[], const char *envp[])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001216{
Deepak Panickal99fbc072014-03-03 15:39:47 +00001217#ifdef _MSC_VER
1218 // disable buffering on windows
1219 setvbuf(stdout, NULL, _IONBF, 0);
1220 setvbuf(stdin , NULL, _IONBF, 0);
1221#endif
1222
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001223 SBDebugger::Initialize();
1224
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00001225 SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001226
Greg Clayton3fcbed62010-10-19 03:25:40 +00001227 signal (SIGPIPE, SIG_IGN);
Caroline Ticedd759852010-09-09 17:45:09 +00001228 signal (SIGWINCH, sigwinch_handler);
Caroline Ticeefed6132010-11-19 20:47:54 +00001229 signal (SIGINT, sigint_handler);
Jim Inghamc5917d92012-11-30 20:23:19 +00001230 signal (SIGTSTP, sigtstp_handler);
1231 signal (SIGCONT, sigcont_handler);
Caroline Ticedd759852010-09-09 17:45:09 +00001232
Greg Clayton66111032010-06-23 01:19:29 +00001233 // Create a scope for driver so that the driver object will destroy itself
1234 // before SBDebugger::Terminate() is called.
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001235 {
Greg Clayton66111032010-06-23 01:19:29 +00001236 Driver driver;
1237
Deepak Panickal429222c2013-10-15 15:46:40 +00001238 bool exiting = false;
1239 SBError error (driver.ParseArgs (argc, argv, stdout, exiting));
Greg Clayton66111032010-06-23 01:19:29 +00001240 if (error.Fail())
1241 {
1242 const char *error_cstr = error.GetCString ();
1243 if (error_cstr)
1244 ::fprintf (stderr, "error: %s\n", error_cstr);
1245 }
Deepak Panickal429222c2013-10-15 15:46:40 +00001246 else if (!exiting)
Greg Clayton66111032010-06-23 01:19:29 +00001247 {
1248 driver.MainLoop ();
1249 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001250 }
1251
1252 SBDebugger::Terminate();
1253 return 0;
1254}