blob: d331ece88f1e294b9b32a0f9a77c37544b91cfb5 [file] [log] [blame]
Chris Lattner24943d22010-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
12#include <getopt.h>
13#include <libgen.h>
14#include <sys/ioctl.h>
15#include <termios.h>
16#include <unistd.h>
Eli Friedmanf2f321d2010-06-09 09:50:17 +000017#include <string.h>
18#include <stdlib.h>
19#include <limits.h>
Eli Friedmand6e167d2010-06-09 19:11:30 +000020#include <fcntl.h>
Chris Lattner24943d22010-06-08 16:52:24 +000021
22#include <string>
23
24#include "IOChannel.h"
Eli Friedmanf2f321d2010-06-09 09:50:17 +000025#include "lldb/API/SBCommandInterpreter.h"
26#include "lldb/API/SBCommandReturnObject.h"
27#include "lldb/API/SBCommunication.h"
28#include "lldb/API/SBDebugger.h"
29#include "lldb/API/SBEvent.h"
30#include "lldb/API/SBHostOS.h"
31#include "lldb/API/SBListener.h"
32#include "lldb/API/SBSourceManager.h"
33#include "lldb/API/SBTarget.h"
34#include "lldb/API/SBThread.h"
35#include "lldb/API/SBProcess.h"
Chris Lattner24943d22010-06-08 16:52:24 +000036
37using namespace lldb;
38
39static void reset_stdin_termios ();
40static struct termios g_old_stdin_termios;
41
Caroline Ticeb8314fe2010-09-09 17:45:09 +000042static char *g_debugger_name = (char *) "";
Caroline Ticec4f55fe2010-11-19 20:47:54 +000043static Driver *g_driver = NULL;
Caroline Ticeb8314fe2010-09-09 17:45:09 +000044
Chris Lattner24943d22010-06-08 16:52:24 +000045// In the Driver::MainLoop, we change the terminal settings. This function is
46// added as an atexit handler to make sure we clean them up.
47static void
48reset_stdin_termios ()
49{
50 ::tcsetattr (STDIN_FILENO, TCSANOW, &g_old_stdin_termios);
51}
52
53static lldb::OptionDefinition g_options[] =
54{
Caroline Tice4d6675c2010-10-01 19:59:14 +000055 { LLDB_OPT_SET_1, true, "help", 'h', no_argument, NULL, NULL, eArgTypeNone,
Chris Lattner24943d22010-06-08 16:52:24 +000056 "Prints out the usage information for the LLDB debugger." },
57
Caroline Tice4d6675c2010-10-01 19:59:14 +000058 { LLDB_OPT_SET_2, true, "version", 'v', no_argument, NULL, NULL, eArgTypeNone,
Chris Lattner24943d22010-06-08 16:52:24 +000059 "Prints out the current version number of the LLDB debugger." },
60
Caroline Tice4d6675c2010-10-01 19:59:14 +000061 { LLDB_OPT_SET_3, true, "arch", 'a', required_argument, NULL, NULL, eArgTypeArchitecture,
Chris Lattner24943d22010-06-08 16:52:24 +000062 "Tells the debugger to use the specified architecture when starting and running the program. <architecture> must be one of the architectures for which the program was compiled." },
63
Caroline Tice4d6675c2010-10-01 19:59:14 +000064 { LLDB_OPT_SET_3 | LLDB_OPT_SET_4, false, "script-language",'l', required_argument, NULL, NULL, eArgTypeScriptLang,
Chris Lattner24943d22010-06-08 16:52:24 +000065 "Tells the debugger to use the specified scripting language for user-defined scripts, rather than the default. Valid scripting languages that can be specified include Python, Perl, Ruby and Tcl. Currently only the Python extensions have been implemented." },
66
Caroline Tice4d6675c2010-10-01 19:59:14 +000067 { LLDB_OPT_SET_3 | LLDB_OPT_SET_4, false, "debug", 'd', no_argument, NULL, NULL, eArgTypeNone,
Chris Lattner24943d22010-06-08 16:52:24 +000068 "Tells the debugger to print out extra information for debugging itself." },
69
Caroline Tice4d6675c2010-10-01 19:59:14 +000070 { LLDB_OPT_SET_3 | LLDB_OPT_SET_4, false, "source", 's', required_argument, NULL, NULL, eArgTypeFilename,
Chris Lattner24943d22010-06-08 16:52:24 +000071 "Tells the debugger to read in and execute the file <file>, which should contain lldb commands." },
72
Caroline Tice4d6675c2010-10-01 19:59:14 +000073 { LLDB_OPT_SET_3, true, "file", 'f', required_argument, NULL, NULL, eArgTypeFilename,
Jim Ingham34e9a982010-06-15 18:47:14 +000074 "Tells the debugger to use the file <filename> as the program to be debugged." },
75
Caroline Tice4d6675c2010-10-01 19:59:14 +000076 { LLDB_OPT_SET_ALL, false, "editor", 'e', no_argument, NULL, NULL, eArgTypeNone,
Jim Ingham74989e82010-08-30 19:44:40 +000077 "Tells the debugger to open source files using the host's \"external editor\" mechanism." },
78
Greg Clayton887aa282010-10-11 01:05:37 +000079 { LLDB_OPT_SET_ALL, false, "no-lldbinit", 'n', no_argument, NULL, NULL, eArgTypeNone,
80 "Do not automatically parse any '.lldbinit' files." },
81
Caroline Tice4d6675c2010-10-01 19:59:14 +000082// { LLDB_OPT_SET_4, true, "crash-log", 'c', required_argument, NULL, NULL, eArgTypeFilename,
Greg Clayton12bec712010-06-28 21:30:43 +000083// "Load executable images from a crash log for symbolication." },
Chris Lattner24943d22010-06-08 16:52:24 +000084
Caroline Tice4d6675c2010-10-01 19:59:14 +000085 { 0, false, NULL, 0, 0, NULL, NULL, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +000086};
87
88
89Driver::Driver () :
90 SBBroadcaster ("Driver"),
Greg Clayton63094e02010-06-23 01:19:29 +000091 m_debugger (SBDebugger::Create()),
Chris Lattner24943d22010-06-08 16:52:24 +000092 m_editline_pty (),
93 m_editline_slave_fh (NULL),
94 m_editline_reader (),
95 m_io_channel_ap (),
96 m_option_data (),
97 m_waiting_for_command (false)
98{
Caroline Ticeb8314fe2010-09-09 17:45:09 +000099 g_debugger_name = (char *) m_debugger.GetInstanceName();
100 if (g_debugger_name == NULL)
101 g_debugger_name = (char *) "";
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000102 g_driver = this;
Chris Lattner24943d22010-06-08 16:52:24 +0000103}
104
105Driver::~Driver ()
106{
Caroline Ticec4f55fe2010-11-19 20:47:54 +0000107 g_driver = NULL;
108 g_debugger_name = NULL;
Chris Lattner24943d22010-06-08 16:52:24 +0000109}
110
111void
112Driver::CloseIOChannelFile ()
113{
114 // Write and End of File sequence to the file descriptor to ensure any
115 // read functions can exit.
116 char eof_str[] = "\x04";
117 ::write (m_editline_pty.GetMasterFileDescriptor(), eof_str, strlen(eof_str));
118
119 m_editline_pty.CloseMasterFileDescriptor();
120
121 if (m_editline_slave_fh)
122 {
123 ::fclose (m_editline_slave_fh);
124 m_editline_slave_fh = NULL;
125 }
126}
127
Greg Clayton54e7afa2010-07-09 20:39:50 +0000128// This function takes INDENT, which tells how many spaces to output at the front
129// of each line; TEXT, which is the text that is to be output. It outputs the
130// text, on multiple lines if necessary, to RESULT, with INDENT spaces at the
131// front of each line. It breaks lines on spaces, tabs or newlines, shortening
132// the line if necessary to not break in the middle of a word. It assumes that
133// each output line should contain a maximum of OUTPUT_MAX_COLUMNS characters.
Chris Lattner24943d22010-06-08 16:52:24 +0000134
135void
Greg Clayton54e7afa2010-07-09 20:39:50 +0000136OutputFormattedUsageText (FILE *out, int indent, const char *text, int output_max_columns)
Chris Lattner24943d22010-06-08 16:52:24 +0000137{
138 int len = strlen (text);
139 std::string text_string (text);
Chris Lattner24943d22010-06-08 16:52:24 +0000140
141 // Force indentation to be reasonable.
142 if (indent >= output_max_columns)
143 indent = 0;
144
145 // Will it all fit on one line?
146
147 if (len + indent < output_max_columns)
148 // Output as a single line
Greg Clayton54e7afa2010-07-09 20:39:50 +0000149 fprintf (out, "%*s%s\n", indent, "", text);
Chris Lattner24943d22010-06-08 16:52:24 +0000150 else
151 {
152 // We need to break it up into multiple lines.
153 int text_width = output_max_columns - indent - 1;
154 int start = 0;
155 int end = start;
156 int final_end = len;
157 int sub_len;
158
159 while (end < final_end)
160 {
161 // Dont start the 'text' on a space, since we're already outputting the indentation.
162 while ((start < final_end) && (text[start] == ' '))
163 start++;
164
165 end = start + text_width;
166 if (end > final_end)
167 end = final_end;
168 else
169 {
170 // If we're not at the end of the text, make sure we break the line on white space.
171 while (end > start
172 && text[end] != ' ' && text[end] != '\t' && text[end] != '\n')
173 end--;
174 }
175 sub_len = end - start;
176 std::string substring = text_string.substr (start, sub_len);
Greg Clayton54e7afa2010-07-09 20:39:50 +0000177 fprintf (out, "%*s%s\n", indent, "", substring.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000178 start = end + 1;
179 }
180 }
181}
182
Caroline Tice4d6675c2010-10-01 19:59:14 +0000183void
184GetArgumentName (const CommandArgumentType arg_type, std::string &arg_name)
185{
186 //Fudge this function here, since we can't call the "real" version in lldb_private::CommandObject...
187
188 switch (arg_type)
189 {
190 // Make cases for all the arg_types used in Driver.cpp
191
192 case eArgTypeNone:
193 arg_name = "";
194 break;
195
196 case eArgTypeArchitecture:
197 arg_name = "architecture";
198 break;
199
200 case eArgTypeScriptLang:
201 arg_name = "script-language";
202 break;
203
204 case eArgTypeFilename:
205 arg_name = "filename";
206 break;
207 }
208 return;
209}
210
211
Chris Lattner24943d22010-06-08 16:52:24 +0000212void
213ShowUsage (FILE *out, lldb::OptionDefinition *option_table, Driver::OptionData data)
214{
215 uint32_t screen_width = 80;
216 uint32_t indent_level = 0;
217 const char *name = "lldb";
Jim Ingham34e9a982010-06-15 18:47:14 +0000218
Chris Lattner24943d22010-06-08 16:52:24 +0000219 fprintf (out, "\nUsage:\n\n");
220
221 indent_level += 2;
222
223
224 // First, show each usage level set of options, e.g. <cmd> [options-for-level-0]
225 // <cmd> [options-for-level-1]
226 // etc.
227
Chris Lattner24943d22010-06-08 16:52:24 +0000228 uint32_t num_options;
Jim Ingham34e9a982010-06-15 18:47:14 +0000229 uint32_t num_option_sets = 0;
230
231 for (num_options = 0; option_table[num_options].long_option != NULL; ++num_options)
Chris Lattner24943d22010-06-08 16:52:24 +0000232 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000233 uint32_t this_usage_mask = option_table[num_options].usage_mask;
234 if (this_usage_mask == LLDB_OPT_SET_ALL)
Chris Lattner24943d22010-06-08 16:52:24 +0000235 {
Jim Ingham34e9a982010-06-15 18:47:14 +0000236 if (num_option_sets == 0)
237 num_option_sets = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000238 }
239 else
240 {
Greg Clayton54e7afa2010-07-09 20:39:50 +0000241 for (uint32_t j = 0; j < LLDB_MAX_NUM_OPTION_SETS; j++)
Jim Ingham34e9a982010-06-15 18:47:14 +0000242 {
243 if (this_usage_mask & 1 << j)
244 {
245 if (num_option_sets <= j)
246 num_option_sets = j + 1;
247 }
248 }
249 }
250 }
251
252 for (uint32_t opt_set = 0; opt_set < num_option_sets; opt_set++)
253 {
254 uint32_t opt_set_mask;
255
256 opt_set_mask = 1 << opt_set;
257
258 if (opt_set > 0)
259 fprintf (out, "\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000260 fprintf (out, "%*s%s", indent_level, "", name);
Jim Ingham34e9a982010-06-15 18:47:14 +0000261
262 for (uint32_t i = 0; i < num_options; ++i)
263 {
264 if (option_table[i].usage_mask & opt_set_mask)
265 {
Caroline Tice4d6675c2010-10-01 19:59:14 +0000266 CommandArgumentType arg_type = option_table[i].argument_type;
267 std::string arg_name;
268 GetArgumentName (arg_type, arg_name);
Jim Ingham34e9a982010-06-15 18:47:14 +0000269 if (option_table[i].required)
270 {
271 if (option_table[i].option_has_arg == required_argument)
Caroline Tice4d6675c2010-10-01 19:59:14 +0000272 fprintf (out, " -%c <%s>", option_table[i].short_option, arg_name.c_str());
Jim Ingham34e9a982010-06-15 18:47:14 +0000273 else if (option_table[i].option_has_arg == optional_argument)
Caroline Tice4d6675c2010-10-01 19:59:14 +0000274 fprintf (out, " -%c [<%s>]", option_table[i].short_option, arg_name.c_str());
Jim Ingham34e9a982010-06-15 18:47:14 +0000275 else
276 fprintf (out, " -%c", option_table[i].short_option);
277 }
278 else
279 {
280 if (option_table[i].option_has_arg == required_argument)
Caroline Tice4d6675c2010-10-01 19:59:14 +0000281 fprintf (out, " [-%c <%s>]", option_table[i].short_option, arg_name.c_str());
Jim Ingham34e9a982010-06-15 18:47:14 +0000282 else if (option_table[i].option_has_arg == optional_argument)
Caroline Tice4d6675c2010-10-01 19:59:14 +0000283 fprintf (out, " [-%c [<%s>]]", option_table[i].short_option, arg_name.c_str());
Jim Ingham34e9a982010-06-15 18:47:14 +0000284 else
285 fprintf (out, " [-%c]", option_table[i].short_option);
286 }
287 }
Chris Lattner24943d22010-06-08 16:52:24 +0000288 }
289 }
290
291 fprintf (out, "\n\n");
292
293 // Now print out all the detailed information about the various options: long form, short form and help text:
294 // -- long_name <argument>
295 // - short <argument>
296 // help text
297
298 // This variable is used to keep track of which options' info we've printed out, because some options can be in
299 // more than one usage level, but we only want to print the long form of its information once.
300
301 Driver::OptionData::OptionSet options_seen;
302 Driver::OptionData::OptionSet::iterator pos;
303
304 indent_level += 5;
305
Jim Ingham34e9a982010-06-15 18:47:14 +0000306 for (uint32_t i = 0; i < num_options; ++i)
Chris Lattner24943d22010-06-08 16:52:24 +0000307 {
308 // Only print this option if we haven't already seen it.
309 pos = options_seen.find (option_table[i].short_option);
310 if (pos == options_seen.end())
311 {
Caroline Tice4d6675c2010-10-01 19:59:14 +0000312 CommandArgumentType arg_type = option_table[i].argument_type;
313 std::string arg_name;
314 GetArgumentName (arg_type, arg_name);
315
Chris Lattner24943d22010-06-08 16:52:24 +0000316 options_seen.insert (option_table[i].short_option);
Greg Clayton54e7afa2010-07-09 20:39:50 +0000317 fprintf (out, "%*s-%c ", indent_level, "", option_table[i].short_option);
Caroline Tice4d6675c2010-10-01 19:59:14 +0000318 if (arg_type != eArgTypeNone)
319 fprintf (out, "<%s>", arg_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000320 fprintf (out, "\n");
Greg Clayton54e7afa2010-07-09 20:39:50 +0000321 fprintf (out, "%*s--%s ", indent_level, "", option_table[i].long_option);
Caroline Tice4d6675c2010-10-01 19:59:14 +0000322 if (arg_type != eArgTypeNone)
323 fprintf (out, "<%s>", arg_name.c_str());
Chris Lattner24943d22010-06-08 16:52:24 +0000324 fprintf (out, "\n");
325 indent_level += 5;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000326 OutputFormattedUsageText (out, indent_level, option_table[i].usage_text, screen_width);
Chris Lattner24943d22010-06-08 16:52:24 +0000327 indent_level -= 5;
328 fprintf (out, "\n");
329 }
330 }
331
332 indent_level -= 5;
333
Greg Clayton54e7afa2010-07-09 20:39:50 +0000334 fprintf (out, "\n%*s('%s <filename>' also works, to specify the file to be debugged.)\n\n",
335 indent_level, "", name);
Chris Lattner24943d22010-06-08 16:52:24 +0000336}
337
338void
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000339BuildGetOptTable (lldb::OptionDefinition *expanded_option_table, std::vector<struct option> &getopt_table,
340 uint32_t num_options)
Chris Lattner24943d22010-06-08 16:52:24 +0000341{
342 if (num_options == 0)
343 return;
344
345 uint32_t i;
346 uint32_t j;
347 std::bitset<256> option_seen;
348
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000349 getopt_table.resize (num_options + 1);
350
Chris Lattner24943d22010-06-08 16:52:24 +0000351 for (i = 0, j = 0; i < num_options; ++i)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000352 {
Chris Lattner24943d22010-06-08 16:52:24 +0000353 char short_opt = expanded_option_table[i].short_option;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000354
Chris Lattner24943d22010-06-08 16:52:24 +0000355 if (option_seen.test(short_opt) == false)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000356 {
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000357 getopt_table[j].name = expanded_option_table[i].long_option;
358 getopt_table[j].has_arg = expanded_option_table[i].option_has_arg;
359 getopt_table[j].flag = NULL;
360 getopt_table[j].val = expanded_option_table[i].short_option;
Chris Lattner24943d22010-06-08 16:52:24 +0000361 option_seen.set(short_opt);
362 ++j;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000363 }
364 }
Chris Lattner24943d22010-06-08 16:52:24 +0000365
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000366 getopt_table[j].name = NULL;
367 getopt_table[j].has_arg = 0;
368 getopt_table[j].flag = NULL;
369 getopt_table[j].val = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000370
371}
372
Greg Clayton63094e02010-06-23 01:19:29 +0000373Driver::OptionData::OptionData () :
Greg Clayton4dc18922010-12-08 22:23:24 +0000374 m_args(),
Greg Clayton63094e02010-06-23 01:19:29 +0000375 m_script_lang (lldb::eScriptLanguageDefault),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000376 m_crash_log (),
Greg Clayton63094e02010-06-23 01:19:29 +0000377 m_source_command_files (),
378 m_debug_mode (false),
Greg Clayton54e7afa2010-07-09 20:39:50 +0000379 m_print_version (false),
Greg Clayton63094e02010-06-23 01:19:29 +0000380 m_print_help (false),
Jim Ingham74989e82010-08-30 19:44:40 +0000381 m_seen_options(),
382 m_use_external_editor(false)
Chris Lattner24943d22010-06-08 16:52:24 +0000383{
Greg Clayton63094e02010-06-23 01:19:29 +0000384}
385
386Driver::OptionData::~OptionData ()
387{
388}
389
390void
391Driver::OptionData::Clear ()
392{
Greg Clayton4dc18922010-12-08 22:23:24 +0000393 m_args.clear ();
Greg Clayton63094e02010-06-23 01:19:29 +0000394 m_script_lang = lldb::eScriptLanguageDefault;
395 m_source_command_files.clear ();
396 m_debug_mode = false;
397 m_print_help = false;
398 m_print_version = false;
Jim Ingham74989e82010-08-30 19:44:40 +0000399 m_use_external_editor = false;
Greg Clayton63094e02010-06-23 01:19:29 +0000400}
401
402void
403Driver::ResetOptionValues ()
404{
405 m_option_data.Clear ();
406}
407
408const char *
409Driver::GetFilename() const
410{
Greg Clayton4dc18922010-12-08 22:23:24 +0000411 if (m_option_data.m_args.empty())
Greg Clayton63094e02010-06-23 01:19:29 +0000412 return NULL;
Greg Clayton4dc18922010-12-08 22:23:24 +0000413 return m_option_data.m_args.front().c_str();
Greg Clayton63094e02010-06-23 01:19:29 +0000414}
415
416const char *
417Driver::GetCrashLogFilename() const
418{
419 if (m_option_data.m_crash_log.empty())
420 return NULL;
421 return m_option_data.m_crash_log.c_str();
422}
423
424lldb::ScriptLanguage
425Driver::GetScriptLanguage() const
426{
427 return m_option_data.m_script_lang;
428}
429
430size_t
431Driver::GetNumSourceCommandFiles () const
432{
433 return m_option_data.m_source_command_files.size();
434}
435
436const char *
437Driver::GetSourceCommandFileAtIndex (uint32_t idx) const
438{
439 if (idx < m_option_data.m_source_command_files.size())
440 return m_option_data.m_source_command_files[idx].c_str();
441 return NULL;
442}
443
444bool
445Driver::GetDebugMode() const
446{
447 return m_option_data.m_debug_mode;
448}
449
450
451// Check the arguments that were passed to this program to make sure they are valid and to get their
452// argument values (if any). Return a boolean value indicating whether or not to start up the full
453// debugger (i.e. the Command Interpreter) or not. Return FALSE if the arguments were invalid OR
454// if the user only wanted help or version information.
455
456SBError
457Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exit)
458{
459 ResetOptionValues ();
460
461 SBCommandReturnObject result;
462
Chris Lattner24943d22010-06-08 16:52:24 +0000463 SBError error;
464 std::string option_string;
465 struct option *long_options = NULL;
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000466 std::vector<struct option> long_options_vector;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000467 uint32_t num_options;
Chris Lattner24943d22010-06-08 16:52:24 +0000468
Greg Clayton54e7afa2010-07-09 20:39:50 +0000469 for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
470 /* Do Nothing. */;
Chris Lattner24943d22010-06-08 16:52:24 +0000471
472 if (num_options == 0)
473 {
474 if (argc > 1)
475 error.SetErrorStringWithFormat ("invalid number of options");
476 return error;
477 }
478
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000479 BuildGetOptTable (g_options, long_options_vector, num_options);
Chris Lattner24943d22010-06-08 16:52:24 +0000480
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000481 if (long_options_vector.empty())
482 long_options = NULL;
483 else
484 long_options = &long_options_vector.front();
Chris Lattner24943d22010-06-08 16:52:24 +0000485
486 if (long_options == NULL)
487 {
488 error.SetErrorStringWithFormat ("invalid long options");
489 return error;
490 }
491
492 // Build the option_string argument for call to getopt_long.
493
494 for (int i = 0; long_options[i].name != NULL; ++i)
495 {
496 if (long_options[i].flag == NULL)
497 {
498 option_string.push_back ((char) long_options[i].val);
499 switch (long_options[i].has_arg)
500 {
501 default:
502 case no_argument:
503 break;
504 case required_argument:
505 option_string.push_back (':');
506 break;
507 case optional_argument:
508 option_string.append ("::");
509 break;
510 }
511 }
512 }
513
514 // Prepare for & make calls to getopt_long.
Eli Friedmanef2bc872010-06-13 19:18:49 +0000515#if __GLIBC__
516 optind = 0;
517#else
Chris Lattner24943d22010-06-08 16:52:24 +0000518 optreset = 1;
519 optind = 1;
Eli Friedmanef2bc872010-06-13 19:18:49 +0000520#endif
Chris Lattner24943d22010-06-08 16:52:24 +0000521 int val;
522 while (1)
523 {
524 int long_options_index = -1;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000525 val = ::getopt_long (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index);
Chris Lattner24943d22010-06-08 16:52:24 +0000526
527 if (val == -1)
528 break;
529 else if (val == '?')
530 {
Greg Clayton63094e02010-06-23 01:19:29 +0000531 m_option_data.m_print_help = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000532 error.SetErrorStringWithFormat ("unknown or ambiguous option");
533 break;
534 }
535 else if (val == 0)
536 continue;
537 else
538 {
Greg Clayton63094e02010-06-23 01:19:29 +0000539 m_option_data.m_seen_options.insert ((char) val);
Chris Lattner24943d22010-06-08 16:52:24 +0000540 if (long_options_index == -1)
541 {
542 for (int i = 0;
543 long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
544 ++i)
545 {
546 if (long_options[i].val == val)
547 {
548 long_options_index = i;
549 break;
550 }
551 }
552 }
553
554 if (long_options_index >= 0)
555 {
Greg Clayton63094e02010-06-23 01:19:29 +0000556 const char short_option = (char) g_options[long_options_index].short_option;
557
558 switch (short_option)
559 {
560 case 'h':
561 m_option_data.m_print_help = true;
562 break;
563
564 case 'v':
565 m_option_data.m_print_version = true;
566 break;
567
568 case 'c':
569 m_option_data.m_crash_log = optarg;
570 break;
Greg Clayton887aa282010-10-11 01:05:37 +0000571
Jim Ingham74989e82010-08-30 19:44:40 +0000572 case 'e':
573 m_option_data.m_use_external_editor = true;
574 break;
Greg Clayton887aa282010-10-11 01:05:37 +0000575
576 case 'n':
577 m_debugger.SkipLLDBInitFiles (true);
578 break;
579
Greg Clayton63094e02010-06-23 01:19:29 +0000580 case 'f':
581 {
582 SBFileSpec file(optarg);
583 if (file.Exists())
Greg Clayton4dc18922010-12-08 22:23:24 +0000584 {
585 m_option_data.m_args.push_back (optarg);
586 }
Caroline Ticeeddffe92010-09-10 04:48:55 +0000587 else if (file.ResolveExecutableLocation())
588 {
589 char path[PATH_MAX];
590 int path_len;
591 file.GetPath (path, path_len);
Greg Clayton4dc18922010-12-08 22:23:24 +0000592 m_option_data.m_args.push_back (path);
Caroline Ticeeddffe92010-09-10 04:48:55 +0000593 }
Greg Clayton63094e02010-06-23 01:19:29 +0000594 else
595 error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
596 }
597 break;
598
599 case 'a':
600 if (!m_debugger.SetDefaultArchitecture (optarg))
601 error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
602 break;
603
604 case 'l':
605 m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
606 break;
607
608 case 'd':
609 m_option_data.m_debug_mode = true;
610 break;
611
612 case 's':
613 {
614 SBFileSpec file(optarg);
615 if (file.Exists())
616 m_option_data.m_source_command_files.push_back (optarg);
Caroline Ticeeddffe92010-09-10 04:48:55 +0000617 else if (file.ResolveExecutableLocation())
618 {
619 char final_path[PATH_MAX];
620 size_t path_len;
621 file.GetPath (final_path, path_len);
622 std::string path_str (final_path);
623 m_option_data.m_source_command_files.push_back (path_str);
624 }
Greg Clayton63094e02010-06-23 01:19:29 +0000625 else
626 error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg);
627 }
628 break;
629
630 default:
631 m_option_data.m_print_help = true;
632 error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
633 break;
634 }
Chris Lattner24943d22010-06-08 16:52:24 +0000635 }
636 else
637 {
638 error.SetErrorStringWithFormat ("invalid option with value %i", val);
639 }
640 if (error.Fail())
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000641 {
Greg Clayton63094e02010-06-23 01:19:29 +0000642 return error;
Caroline Ticebd5c63e2010-10-12 21:57:09 +0000643 }
Chris Lattner24943d22010-06-08 16:52:24 +0000644 }
645 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000646
Greg Clayton63094e02010-06-23 01:19:29 +0000647 if (error.Fail() || m_option_data.m_print_help)
Chris Lattner24943d22010-06-08 16:52:24 +0000648 {
649 ShowUsage (out_fh, g_options, m_option_data);
Greg Clayton9caa9462010-10-11 01:13:37 +0000650 exit = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000651 }
652 else if (m_option_data.m_print_version)
653 {
Greg Clayton63094e02010-06-23 01:19:29 +0000654 ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
655 exit = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000656 }
657 else if (! m_option_data.m_crash_log.empty())
658 {
659 // Handle crash log stuff here.
660 }
661 else
662 {
Greg Clayton4dc18922010-12-08 22:23:24 +0000663 // Any arguments that are left over after option parsing are for
664 // the program. If a file was specified with -f then the filename
665 // is already in the m_option_data.m_args array, and any remaining args
666 // are arguments for the inferior program. If no file was specified with
667 // -f, then what is left is the program name followed by any arguments.
668
669 // Skip any options we consumed with getopt_long
670 argc -= optind;
671 argv += optind;
672
673 if (argc > 0)
674 {
675 for (int arg_idx=0; arg_idx<argc; ++arg_idx)
676 {
677 const char *arg = argv[arg_idx];
678 if (arg)
679 m_option_data.m_args.push_back (arg);
680 }
681 }
682
Chris Lattner24943d22010-06-08 16:52:24 +0000683 }
684
Greg Clayton63094e02010-06-23 01:19:29 +0000685 return error;
Chris Lattner24943d22010-06-08 16:52:24 +0000686}
687
Caroline Tice757500e2010-09-29 18:35:42 +0000688size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000689Driver::GetProcessSTDOUT ()
690{
691 // The process has stuff waiting for stdout; get it and write it out to the appropriate place.
692 char stdio_buffer[1024];
693 size_t len;
Caroline Tice757500e2010-09-29 18:35:42 +0000694 size_t total_bytes = 0;
Jim Inghamc8332952010-08-26 21:32:51 +0000695 while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDOUT (stdio_buffer, sizeof (stdio_buffer))) > 0)
Caroline Tice757500e2010-09-29 18:35:42 +0000696 {
Chris Lattner24943d22010-06-08 16:52:24 +0000697 m_io_channel_ap->OutWrite (stdio_buffer, len);
Caroline Tice757500e2010-09-29 18:35:42 +0000698 total_bytes += len;
699 }
700 return total_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000701}
702
Caroline Tice757500e2010-09-29 18:35:42 +0000703size_t
Chris Lattner24943d22010-06-08 16:52:24 +0000704Driver::GetProcessSTDERR ()
705{
706 // The process has stuff waiting for stderr; get it and write it out to the appropriate place.
707 char stdio_buffer[1024];
708 size_t len;
Caroline Tice757500e2010-09-29 18:35:42 +0000709 size_t total_bytes = 0;
Jim Inghamc8332952010-08-26 21:32:51 +0000710 while ((len = m_debugger.GetSelectedTarget().GetProcess().GetSTDERR (stdio_buffer, sizeof (stdio_buffer))) > 0)
Caroline Tice757500e2010-09-29 18:35:42 +0000711 {
Chris Lattner24943d22010-06-08 16:52:24 +0000712 m_io_channel_ap->ErrWrite (stdio_buffer, len);
Caroline Tice757500e2010-09-29 18:35:42 +0000713 total_bytes += len;
714 }
715 return total_bytes;
Chris Lattner24943d22010-06-08 16:52:24 +0000716}
717
718void
Jim Inghamc8332952010-08-26 21:32:51 +0000719Driver::UpdateSelectedThread ()
Chris Lattner24943d22010-06-08 16:52:24 +0000720{
721 using namespace lldb;
Jim Inghamc8332952010-08-26 21:32:51 +0000722 SBProcess process(m_debugger.GetSelectedTarget().GetProcess());
Chris Lattner24943d22010-06-08 16:52:24 +0000723 if (process.IsValid())
724 {
Jim Inghamc8332952010-08-26 21:32:51 +0000725 SBThread curr_thread (process.GetSelectedThread());
Chris Lattner24943d22010-06-08 16:52:24 +0000726 SBThread thread;
727 StopReason curr_thread_stop_reason = eStopReasonInvalid;
728 curr_thread_stop_reason = curr_thread.GetStopReason();
729
730 if (!curr_thread.IsValid() ||
731 curr_thread_stop_reason == eStopReasonInvalid ||
732 curr_thread_stop_reason == eStopReasonNone)
733 {
734 // Prefer a thread that has just completed its plan over another thread as current thread.
735 SBThread plan_thread;
736 SBThread other_thread;
737 const size_t num_threads = process.GetNumThreads();
738 size_t i;
739 for (i = 0; i < num_threads; ++i)
740 {
741 thread = process.GetThreadAtIndex(i);
742 StopReason thread_stop_reason = thread.GetStopReason();
743 switch (thread_stop_reason)
744 {
745 default:
746 case eStopReasonInvalid:
747 case eStopReasonNone:
748 break;
749
750 case eStopReasonTrace:
751 case eStopReasonBreakpoint:
752 case eStopReasonWatchpoint:
753 case eStopReasonSignal:
754 case eStopReasonException:
755 if (!other_thread.IsValid())
756 other_thread = thread;
757 break;
758 case eStopReasonPlanComplete:
759 if (!plan_thread.IsValid())
760 plan_thread = thread;
761 break;
762 }
763 }
764 if (plan_thread.IsValid())
Jim Inghamc8332952010-08-26 21:32:51 +0000765 process.SetSelectedThread (plan_thread);
Chris Lattner24943d22010-06-08 16:52:24 +0000766 else if (other_thread.IsValid())
Jim Inghamc8332952010-08-26 21:32:51 +0000767 process.SetSelectedThread (other_thread);
Chris Lattner24943d22010-06-08 16:52:24 +0000768 else
769 {
770 if (curr_thread.IsValid())
771 thread = curr_thread;
772 else
773 thread = process.GetThreadAtIndex(0);
774
775 if (thread.IsValid())
Jim Inghamc8332952010-08-26 21:32:51 +0000776 process.SetSelectedThread (thread);
Chris Lattner24943d22010-06-08 16:52:24 +0000777 }
778 }
779 }
780}
781
782
783// This function handles events that were broadcast by the process.
784void
785Driver::HandleProcessEvent (const SBEvent &event)
786{
787 using namespace lldb;
788 const uint32_t event_type = event.GetType();
789
790 if (event_type & SBProcess::eBroadcastBitSTDOUT)
791 {
792 // The process has stdout available, get it and write it out to the
793 // appropriate place.
Caroline Tice757500e2010-09-29 18:35:42 +0000794 if (GetProcessSTDOUT ())
795 m_io_channel_ap->RefreshPrompt();
Chris Lattner24943d22010-06-08 16:52:24 +0000796 }
797 else if (event_type & SBProcess::eBroadcastBitSTDERR)
798 {
799 // The process has stderr available, get it and write it out to the
800 // appropriate place.
Caroline Tice757500e2010-09-29 18:35:42 +0000801 if (GetProcessSTDERR ())
802 m_io_channel_ap->RefreshPrompt();
Chris Lattner24943d22010-06-08 16:52:24 +0000803 }
804 else if (event_type & SBProcess::eBroadcastBitStateChanged)
805 {
806 // Drain all stout and stderr so we don't see any output come after
807 // we print our prompts
Caroline Tice757500e2010-09-29 18:35:42 +0000808 if (GetProcessSTDOUT ()
809 || GetProcessSTDERR ())
810 m_io_channel_ap->RefreshPrompt();
Chris Lattner24943d22010-06-08 16:52:24 +0000811
812 // Something changed in the process; get the event and report the process's current status and location to
813 // the user.
814 StateType event_state = SBProcess::GetStateFromEvent (event);
815 if (event_state == eStateInvalid)
816 return;
817
818 SBProcess process (SBProcess::GetProcessFromEvent (event));
819 assert (process.IsValid());
820
821 switch (event_state)
822 {
823 case eStateInvalid:
824 case eStateUnloaded:
825 case eStateAttaching:
826 case eStateLaunching:
827 case eStateStepping:
828 case eStateDetached:
829 {
830 char message[1024];
831 int message_len = ::snprintf (message, sizeof(message), "Process %d %s\n", process.GetProcessID(),
Greg Clayton63094e02010-06-23 01:19:29 +0000832 m_debugger.StateAsCString (event_state));
Chris Lattner24943d22010-06-08 16:52:24 +0000833 m_io_channel_ap->OutWrite(message, message_len);
834 }
835 break;
836
837 case eStateRunning:
838 // Don't be chatty when we run...
839 break;
840
841 case eStateExited:
Caroline Tice757500e2010-09-29 18:35:42 +0000842 {
843 SBCommandReturnObject result;
844 m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false);
845 m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize());
846 m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize());
847 m_io_channel_ap->RefreshPrompt();
848 }
Chris Lattner24943d22010-06-08 16:52:24 +0000849 break;
850
851 case eStateStopped:
852 case eStateCrashed:
853 case eStateSuspended:
854 // Make sure the program hasn't been auto-restarted:
855 if (SBProcess::GetRestartedFromEvent (event))
856 {
857 // FIXME: Do we want to report this, or would that just be annoyingly chatty?
858 char message[1024];
859 int message_len = ::snprintf (message, sizeof(message), "Process %d stopped and was programmatically restarted.\n",
860 process.GetProcessID());
861 m_io_channel_ap->OutWrite(message, message_len);
Caroline Tice757500e2010-09-29 18:35:42 +0000862 m_io_channel_ap->RefreshPrompt ();
Chris Lattner24943d22010-06-08 16:52:24 +0000863 }
864 else
865 {
Caroline Tice757500e2010-09-29 18:35:42 +0000866 SBCommandReturnObject result;
Jim Inghamc8332952010-08-26 21:32:51 +0000867 UpdateSelectedThread ();
Caroline Tice757500e2010-09-29 18:35:42 +0000868 m_debugger.GetCommandInterpreter().HandleCommand("process status", result, false);
869 m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize());
870 m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize());
871 m_io_channel_ap->RefreshPrompt ();
Chris Lattner24943d22010-06-08 16:52:24 +0000872 }
873 break;
874 }
875 }
876}
877
878// This function handles events broadcast by the IOChannel (HasInput, UserInterrupt, or ThreadShouldExit).
879
880bool
881Driver::HandleIOEvent (const SBEvent &event)
882{
883 bool quit = false;
884
885 const uint32_t event_type = event.GetType();
886
887 if (event_type & IOChannel::eBroadcastBitHasUserInput)
888 {
889 // We got some input (i.e. a command string) from the user; pass it off to the command interpreter for
890 // handling.
891
892 const char *command_string = SBEvent::GetCStringFromEvent(event);
893 if (command_string == NULL)
Greg Clayton54e7afa2010-07-09 20:39:50 +0000894 command_string = "";
Chris Lattner24943d22010-06-08 16:52:24 +0000895 SBCommandReturnObject result;
Greg Clayton63094e02010-06-23 01:19:29 +0000896 if (m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, true) != lldb::eReturnStatusQuit)
Chris Lattner24943d22010-06-08 16:52:24 +0000897 {
898 m_io_channel_ap->ErrWrite (result.GetError(), result.GetErrorSize());
899 m_io_channel_ap->OutWrite (result.GetOutput(), result.GetOutputSize());
900 }
901 // We are done getting and running our command, we can now clear the
902 // m_waiting_for_command so we can get another one.
903 m_waiting_for_command = false;
904
905 // If our editline input reader is active, it means another input reader
906 // got pushed onto the input reader and caused us to become deactivated.
907 // When the input reader above us gets popped, we will get re-activated
908 // and our prompt will refresh in our callback
909 if (m_editline_reader.IsActive())
910 {
911 ReadyForCommand ();
912 }
913 }
914 else if (event_type & IOChannel::eBroadcastBitUserInterrupt)
915 {
916 // This is here to handle control-c interrupts from the user. It has not yet really been implemented.
917 // TO BE DONE: PROPERLY HANDLE CONTROL-C FROM USER
918 //m_io_channel_ap->CancelInput();
919 // Anything else? Send Interrupt to process?
920 }
921 else if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) ||
922 (event_type & IOChannel::eBroadcastBitThreadDidExit))
923 {
924 // If the IOChannel thread is trying to go away, then it is definitely
925 // time to end the debugging session.
926 quit = true;
927 }
928
929 return quit;
930}
931
932
933//struct CrashImageInfo
934//{
935// std::string path;
936// VMRange text_range;
937// UUID uuid;
938//};
939//
940//void
941//Driver::ParseCrashLog (const char *crash_log)
942//{
943// printf("Parsing crash log: %s\n", crash_log);
944//
945// char image_path[PATH_MAX];
946// std::vector<CrashImageInfo> crash_infos;
947// if (crash_log && crash_log[0])
948// {
949// FileSpec crash_log_file (crash_log);
950// STLStringArray crash_log_lines;
951// if (crash_log_file.ReadFileLines (crash_log_lines))
952// {
953// const size_t num_crash_log_lines = crash_log_lines.size();
954// size_t i;
955// for (i=0; i<num_crash_log_lines; ++i)
956// {
957// const char *line = crash_log_lines[i].c_str();
958// if (strstr (line, "Code Type:"))
959// {
960// char arch_string[256];
961// if (sscanf(line, "%s", arch_string))
962// {
963// if (strcmp(arch_string, "X86-64"))
964// lldb::GetDefaultArchitecture ().SetArch ("x86_64");
965// else if (strcmp(arch_string, "X86"))
966// lldb::GetDefaultArchitecture ().SetArch ("i386");
967// else
968// {
969// ArchSpec arch(arch_string);
970// if (arch.IsValid ())
971// lldb::GetDefaultArchitecture () = arch;
972// else
973// fprintf(stderr, "Unrecognized architecture: %s\n", arch_string);
974// }
975// }
976// }
977// else
978// if (strstr(line, "Path:"))
979// {
980// const char *p = line + strlen("Path:");
981// while (isspace(*p))
982// ++p;
983//
984// m_option_data.m_filename.assign (p);
985// }
986// else
987// if (strstr(line, "Binary Images:"))
988// {
989// while (++i < num_crash_log_lines)
990// {
991// if (crash_log_lines[i].empty())
992// break;
993//
994// line = crash_log_lines[i].c_str();
995// uint64_t text_start_addr;
996// uint64_t text_end_addr;
997// char uuid_cstr[64];
998// int bytes_consumed_before_uuid = 0;
999// int bytes_consumed_after_uuid = 0;
1000//
1001// int items_parsed = ::sscanf (line,
1002// "%llx - %llx %*s %*s %*s %n%s %n",
1003// &text_start_addr,
1004// &text_end_addr,
1005// &bytes_consumed_before_uuid,
1006// uuid_cstr,
1007// &bytes_consumed_after_uuid);
1008//
1009// if (items_parsed == 3)
1010// {
1011//
1012// CrashImageInfo info;
1013// info.text_range.SetBaseAddress(text_start_addr);
1014// info.text_range.SetEndAddress(text_end_addr);
1015//
1016// if (uuid_cstr[0] == '<')
1017// {
1018// if (info.uuid.SetfromCString (&uuid_cstr[1]) == 0)
1019// info.uuid.Clear();
1020//
1021// ::strncpy (image_path, line + bytes_consumed_after_uuid, sizeof(image_path));
1022// }
1023// else
1024// {
1025// ::strncpy (image_path, line + bytes_consumed_before_uuid, sizeof(image_path));
1026// }
1027//
1028// info.path = image_path;
1029//
1030// crash_infos.push_back (info);
1031//
1032// info.uuid.GetAsCString(uuid_cstr, sizeof(uuid_cstr));
1033//
1034// printf("0x%16.16llx - 0x%16.16llx <%s> %s\n",
1035// text_start_addr,
1036// text_end_addr,
1037// uuid_cstr,
1038// image_path);
1039// }
1040// }
1041// }
1042// }
1043// }
1044//
1045// if (crash_infos.size())
1046// {
Greg Clayton63094e02010-06-23 01:19:29 +00001047// SBTarget target (m_debugger.CreateTarget (crash_infos.front().path.c_str(),
Chris Lattner24943d22010-06-08 16:52:24 +00001048// lldb::GetDefaultArchitecture().AsCString (),
1049// false));
1050// if (target.IsValid())
1051// {
1052//
1053// }
1054// }
1055// }
1056//}
1057//
1058
1059void
1060Driver::MasterThreadBytesReceived (void *baton, const void *src, size_t src_len)
1061{
1062 Driver *driver = (Driver*)baton;
1063 driver->GetFromMaster ((const char *)src, src_len);
1064}
1065
1066void
1067Driver::GetFromMaster (const char *src, size_t src_len)
1068{
1069 // Echo the characters back to the Debugger's stdout, that way if you
1070 // type characters while a command is running, you'll see what you've typed.
Greg Clayton63094e02010-06-23 01:19:29 +00001071 FILE *out_fh = m_debugger.GetOutputFileHandle();
Chris Lattner24943d22010-06-08 16:52:24 +00001072 if (out_fh)
1073 ::fwrite (src, 1, src_len, out_fh);
1074}
1075
1076size_t
1077Driver::EditLineInputReaderCallback
1078(
1079 void *baton,
1080 SBInputReader *reader,
1081 InputReaderAction notification,
1082 const char *bytes,
1083 size_t bytes_len
1084)
1085{
1086 Driver *driver = (Driver *)baton;
1087
1088 switch (notification)
1089 {
1090 case eInputReaderActivate:
1091 break;
1092
1093 case eInputReaderReactivate:
1094 driver->ReadyForCommand();
1095 break;
1096
1097 case eInputReaderDeactivate:
1098 break;
1099
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001100 case eInputReaderInterrupt:
1101 if (driver->m_io_channel_ap.get() != NULL)
1102 {
1103 driver->m_io_channel_ap->OutWrite ("^C\n", 3);
1104 driver->m_io_channel_ap->RefreshPrompt();
1105 }
1106 break;
1107
1108 case eInputReaderEndOfFile:
1109 if (driver->m_io_channel_ap.get() != NULL)
1110 {
1111 driver->m_io_channel_ap->OutWrite ("^D\n", 3);
1112 driver->m_io_channel_ap->RefreshPrompt ();
1113 }
1114 write (driver->m_editline_pty.GetMasterFileDescriptor(), "quit\n", 5);
1115 break;
1116
Chris Lattner24943d22010-06-08 16:52:24 +00001117 case eInputReaderGotToken:
1118 write (driver->m_editline_pty.GetMasterFileDescriptor(), bytes, bytes_len);
1119 break;
1120
1121 case eInputReaderDone:
1122 break;
1123 }
1124 return bytes_len;
1125}
1126
1127void
1128Driver::MainLoop ()
1129{
1130 char error_str[1024];
1131 if (m_editline_pty.OpenFirstAvailableMaster(O_RDWR|O_NOCTTY, error_str, sizeof(error_str)) == false)
1132 {
1133 ::fprintf (stderr, "error: failed to open driver pseudo terminal : %s", error_str);
1134 exit(1);
1135 }
1136 else
1137 {
1138 const char *driver_slave_name = m_editline_pty.GetSlaveName (error_str, sizeof(error_str));
1139 if (driver_slave_name == NULL)
1140 {
1141 ::fprintf (stderr, "error: failed to get slave name for driver pseudo terminal : %s", error_str);
1142 exit(2);
1143 }
1144 else
1145 {
1146 m_editline_slave_fh = ::fopen (driver_slave_name, "r+");
1147 if (m_editline_slave_fh == NULL)
1148 {
1149 SBError error;
1150 error.SetErrorToErrno();
1151 ::fprintf (stderr, "error: failed to get open slave for driver pseudo terminal : %s",
1152 error.GetCString());
1153 exit(3);
1154 }
1155
1156 ::setbuf (m_editline_slave_fh, NULL);
1157 }
1158 }
1159
1160
1161 // struct termios stdin_termios;
1162
1163 if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
1164 atexit (reset_stdin_termios);
1165
1166 ::setbuf (stdin, NULL);
1167 ::setbuf (stdout, NULL);
1168
Greg Clayton63094e02010-06-23 01:19:29 +00001169 m_debugger.SetErrorFileHandle (stderr, false);
1170 m_debugger.SetOutputFileHandle (stdout, false);
1171 m_debugger.SetInputFileHandle (stdin, true);
Jim Ingham74989e82010-08-30 19:44:40 +00001172
1173 m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);
Chris Lattner24943d22010-06-08 16:52:24 +00001174
1175 // You have to drain anything that comes to the master side of the PTY. master_out_comm is
1176 // for that purpose. The reason you need to do this is a curious reason... editline will echo
1177 // characters to the PTY when it gets characters while el_gets is not running, and then when
1178 // you call el_gets (or el_getc) it will try to reset the terminal back to raw mode which blocks
1179 // if there are unconsumed characters in the out buffer.
1180 // However, you don't need to do anything with the characters, since editline will dump these
1181 // unconsumed characters after printing the prompt again in el_gets.
1182
Greg Claytoneecb0f32010-12-04 02:39:47 +00001183 SBCommunication master_out_comm("driver.editline");
1184 master_out_comm.SetCloseOnEOF (false);
Chris Lattner24943d22010-06-08 16:52:24 +00001185 master_out_comm.AdoptFileDesriptor(m_editline_pty.GetMasterFileDescriptor(), false);
1186 master_out_comm.SetReadThreadBytesReceivedCallback(Driver::MasterThreadBytesReceived, this);
1187
1188 if (master_out_comm.ReadThreadStart () == false)
1189 {
1190 ::fprintf (stderr, "error: failed to start master out read thread");
1191 exit(5);
1192 }
1193
1194// const char *crash_log = GetCrashLogFilename();
1195// if (crash_log)
1196// {
1197// ParseCrashLog (crash_log);
1198// }
1199//
Greg Clayton63094e02010-06-23 01:19:29 +00001200 SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
Chris Lattner24943d22010-06-08 16:52:24 +00001201
1202 m_io_channel_ap.reset (new IOChannel(m_editline_slave_fh, stdout, stderr, this));
1203
1204 struct winsize window_size;
1205 if (isatty (STDIN_FILENO)
1206 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1207 {
Caroline Tice6e4c5ce2010-09-04 00:03:46 +00001208 if (window_size.ws_col > 0)
Greg Clayton238c0a12010-09-18 01:14:36 +00001209 m_debugger.SetTerminalWidth (window_size.ws_col);
Chris Lattner24943d22010-06-08 16:52:24 +00001210 }
1211
1212 // Since input can be redirected by the debugger, we must insert our editline
1213 // input reader in the queue so we know when our reader should be active
1214 // and so we can receive bytes only when we are supposed to.
Greg Clayton63094e02010-06-23 01:19:29 +00001215 SBError err (m_editline_reader.Initialize (m_debugger,
1216 Driver::EditLineInputReaderCallback, // callback
Chris Lattner24943d22010-06-08 16:52:24 +00001217 this, // baton
1218 eInputReaderGranularityByte, // token_size
1219 NULL, // end token - NULL means never done
1220 NULL, // prompt - taken care of elsewhere
1221 false)); // echo input - don't need Debugger
1222 // to do this, we handle it elsewhere
1223
1224 if (err.Fail())
1225 {
1226 ::fprintf (stderr, "error: %s", err.GetCString());
1227 exit (6);
1228 }
1229
Greg Clayton63094e02010-06-23 01:19:29 +00001230 m_debugger.PushInputReader (m_editline_reader);
Chris Lattner24943d22010-06-08 16:52:24 +00001231
Greg Clayton63094e02010-06-23 01:19:29 +00001232 SBListener listener(m_debugger.GetListener());
Chris Lattner24943d22010-06-08 16:52:24 +00001233 if (listener.IsValid())
1234 {
1235
1236 listener.StartListeningForEvents (*m_io_channel_ap,
1237 IOChannel::eBroadcastBitHasUserInput |
1238 IOChannel::eBroadcastBitUserInterrupt |
1239 IOChannel::eBroadcastBitThreadShouldExit |
1240 IOChannel::eBroadcastBitThreadDidStart |
1241 IOChannel::eBroadcastBitThreadDidExit);
1242
1243 if (m_io_channel_ap->Start ())
1244 {
1245 bool iochannel_thread_exited = false;
1246
1247 listener.StartListeningForEvents (sb_interpreter.GetBroadcaster(),
1248 SBCommandInterpreter::eBroadcastBitQuitCommandReceived);
1249
1250 // Before we handle any options from the command line, we parse the
1251 // .lldbinit file in the user's home directory.
1252 SBCommandReturnObject result;
1253 sb_interpreter.SourceInitFileInHomeDirectory(result);
1254 if (GetDebugMode())
1255 {
Greg Clayton63094e02010-06-23 01:19:29 +00001256 result.PutError (m_debugger.GetErrorFileHandle());
1257 result.PutOutput (m_debugger.GetOutputFileHandle());
Chris Lattner24943d22010-06-08 16:52:24 +00001258 }
1259
1260 // Now we handle options we got from the command line
1261 char command_string[PATH_MAX * 2];
1262 const size_t num_source_command_files = GetNumSourceCommandFiles();
1263 if (num_source_command_files > 0)
1264 {
1265 for (size_t i=0; i < num_source_command_files; ++i)
1266 {
1267 const char *command_file = GetSourceCommandFileAtIndex(i);
Johnny Chen7c984242010-07-28 21:16:11 +00001268 ::snprintf (command_string, sizeof(command_string), "command source '%s'", command_file);
Greg Clayton63094e02010-06-23 01:19:29 +00001269 m_debugger.GetCommandInterpreter().HandleCommand (command_string, result, false);
Chris Lattner24943d22010-06-08 16:52:24 +00001270 if (GetDebugMode())
1271 {
Greg Clayton63094e02010-06-23 01:19:29 +00001272 result.PutError (m_debugger.GetErrorFileHandle());
1273 result.PutOutput (m_debugger.GetOutputFileHandle());
Chris Lattner24943d22010-06-08 16:52:24 +00001274 }
1275 }
1276 }
1277
Greg Clayton4dc18922010-12-08 22:23:24 +00001278 const size_t num_args = m_option_data.m_args.size();
1279 if (num_args > 0)
Chris Lattner24943d22010-06-08 16:52:24 +00001280 {
1281 char arch_name[64];
Greg Clayton63094e02010-06-23 01:19:29 +00001282 if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
Greg Clayton4dc18922010-12-08 22:23:24 +00001283 ::snprintf (command_string,
1284 sizeof (command_string),
1285 "file --arch=%s '%s'",
1286 arch_name,
1287 m_option_data.m_args[0].c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001288 else
Greg Clayton4dc18922010-12-08 22:23:24 +00001289 ::snprintf (command_string,
1290 sizeof(command_string),
1291 "file '%s'",
1292 m_option_data.m_args[0].c_str());
Chris Lattner24943d22010-06-08 16:52:24 +00001293
Greg Clayton63094e02010-06-23 01:19:29 +00001294 m_debugger.HandleCommand (command_string);
Greg Clayton4dc18922010-12-08 22:23:24 +00001295
1296 if (num_args > 1)
1297 {
1298 m_debugger.HandleCommand ("settings clear target.process.run-args");
1299 char arg_cstr[1024];
1300 for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
1301 {
1302 ::snprintf (arg_cstr, sizeof(arg_cstr), "settings append target.process.run-args \"%s\"", m_option_data.m_args[arg_idx].c_str());
1303 m_debugger.HandleCommand (arg_cstr);
1304 }
1305 }
Chris Lattner24943d22010-06-08 16:52:24 +00001306 }
1307
1308 // Now that all option parsing is done, we try and parse the .lldbinit
1309 // file in the current working directory
1310 sb_interpreter.SourceInitFileInCurrentWorkingDirectory (result);
1311 if (GetDebugMode())
1312 {
Greg Clayton63094e02010-06-23 01:19:29 +00001313 result.PutError(m_debugger.GetErrorFileHandle());
1314 result.PutOutput(m_debugger.GetOutputFileHandle());
Chris Lattner24943d22010-06-08 16:52:24 +00001315 }
1316
1317 SBEvent event;
1318
1319 // Make sure the IO channel is started up before we try to tell it we
1320 // are ready for input
1321 listener.WaitForEventForBroadcasterWithType (UINT32_MAX,
1322 *m_io_channel_ap,
1323 IOChannel::eBroadcastBitThreadDidStart,
1324 event);
1325
1326 ReadyForCommand ();
1327
1328 bool done = false;
1329 while (!done)
1330 {
1331 listener.WaitForEvent (UINT32_MAX, event);
1332 if (event.IsValid())
1333 {
1334 if (event.GetBroadcaster().IsValid())
1335 {
1336 uint32_t event_type = event.GetType();
1337 if (event.BroadcasterMatchesRef (*m_io_channel_ap))
1338 {
1339 if ((event_type & IOChannel::eBroadcastBitThreadShouldExit) ||
1340 (event_type & IOChannel::eBroadcastBitThreadDidExit))
1341 {
1342 done = true;
1343 if (event_type & IOChannel::eBroadcastBitThreadDidExit)
1344 iochannel_thread_exited = true;
1345 break;
1346 }
1347 else
1348 done = HandleIOEvent (event);
1349 }
Jim Inghamc8332952010-08-26 21:32:51 +00001350 else if (event.BroadcasterMatchesRef (m_debugger.GetSelectedTarget().GetProcess().GetBroadcaster()))
Chris Lattner24943d22010-06-08 16:52:24 +00001351 {
1352 HandleProcessEvent (event);
1353 }
1354 else if (event.BroadcasterMatchesRef (sb_interpreter.GetBroadcaster()))
1355 {
1356 if (event_type & SBCommandInterpreter::eBroadcastBitQuitCommandReceived)
1357 done = true;
1358 }
1359 }
1360 }
1361 }
1362
1363 reset_stdin_termios ();
1364
1365 CloseIOChannelFile ();
1366
1367 if (!iochannel_thread_exited)
1368 {
Greg Claytonbef15832010-07-14 00:18:15 +00001369 event.Clear();
Chris Lattner24943d22010-06-08 16:52:24 +00001370 listener.GetNextEventForBroadcasterWithType (*m_io_channel_ap,
1371 IOChannel::eBroadcastBitThreadDidExit,
1372 event);
1373 if (!event.IsValid())
1374 {
1375 // Send end EOF to the driver file descriptor
1376 m_io_channel_ap->Stop();
1377 }
1378 }
1379
Jim Inghamc8332952010-08-26 21:32:51 +00001380 SBProcess process = m_debugger.GetSelectedTarget().GetProcess();
Chris Lattner24943d22010-06-08 16:52:24 +00001381 if (process.IsValid())
1382 process.Destroy();
1383 }
1384 }
1385}
1386
1387
1388void
1389Driver::ReadyForCommand ()
1390{
1391 if (m_waiting_for_command == false)
1392 {
1393 m_waiting_for_command = true;
1394 BroadcastEventByType (Driver::eBroadcastBitReadyForInput, true);
1395 }
1396}
1397
1398
Caroline Ticeb8314fe2010-09-09 17:45:09 +00001399void
1400sigwinch_handler (int signo)
1401{
1402 struct winsize window_size;
1403 if (isatty (STDIN_FILENO)
1404 && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
1405 {
1406 if ((window_size.ws_col > 0) && (strlen (g_debugger_name) > 0))
1407 {
1408 char width_str_buffer[25];
1409 ::sprintf (width_str_buffer, "%d", window_size.ws_col);
1410 SBDebugger::SetInternalVariable ("term-width", width_str_buffer, g_debugger_name);
1411 }
1412 }
1413}
1414
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001415void
1416sigint_handler (int signo)
1417{
1418 static bool g_interrupt_sent = false;
1419 if (g_driver)
1420 {
1421 if (!g_interrupt_sent)
1422 {
1423 g_interrupt_sent = true;
1424 g_driver->GetDebugger().DispatchInputInterrupt();
1425 g_interrupt_sent = false;
1426 return;
1427 }
1428 }
1429
1430 exit (signo);
1431}
1432
Chris Lattner24943d22010-06-08 16:52:24 +00001433int
1434main (int argc, char const *argv[])
1435{
Chris Lattner24943d22010-06-08 16:52:24 +00001436 SBDebugger::Initialize();
1437
Greg Clayton95e33b72010-11-07 21:02:03 +00001438 SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");
Chris Lattner24943d22010-06-08 16:52:24 +00001439
Greg Clayton36f63a92010-10-19 03:25:40 +00001440 signal (SIGPIPE, SIG_IGN);
Caroline Ticeb8314fe2010-09-09 17:45:09 +00001441 signal (SIGWINCH, sigwinch_handler);
Caroline Ticec4f55fe2010-11-19 20:47:54 +00001442 signal (SIGINT, sigint_handler);
Caroline Ticeb8314fe2010-09-09 17:45:09 +00001443
Greg Clayton63094e02010-06-23 01:19:29 +00001444 // Create a scope for driver so that the driver object will destroy itself
1445 // before SBDebugger::Terminate() is called.
Chris Lattner24943d22010-06-08 16:52:24 +00001446 {
Greg Clayton63094e02010-06-23 01:19:29 +00001447 Driver driver;
1448
1449 bool exit = false;
1450 SBError error (driver.ParseArgs (argc, argv, stdout, exit));
1451 if (error.Fail())
1452 {
1453 const char *error_cstr = error.GetCString ();
1454 if (error_cstr)
1455 ::fprintf (stderr, "error: %s\n", error_cstr);
1456 }
1457 else if (!exit)
1458 {
1459 driver.MainLoop ();
1460 }
Chris Lattner24943d22010-06-08 16:52:24 +00001461 }
1462
1463 SBDebugger::Terminate();
1464 return 0;
1465}