Exemple #1
0
int
main (int argc, char const *argv[], const char *envp[])
{
    SBDebugger::Initialize();
    
    SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");

    signal (SIGPIPE, SIG_IGN);
    signal (SIGWINCH, sigwinch_handler);
    signal (SIGINT, sigint_handler);
    signal (SIGTSTP, sigtstp_handler);
    signal (SIGCONT, sigcont_handler);

    // Create a scope for driver so that the driver object will destroy itself
    // before SBDebugger::Terminate() is called.
    {
        Driver driver;

        bool exiting = false;
        SBError error (driver.ParseArgs (argc, argv, stdout, exiting));
        if (error.Fail())
        {
            const char *error_cstr = error.GetCString ();
            if (error_cstr)
                ::fprintf (stderr, "error: %s\n", error_cstr);
        }
        else if (!exiting)
        {
            driver.MainLoop ();
        }
    }

    SBDebugger::Terminate();
    return 0;
}
Exemple #2
0
int
main (int argc, char const *argv[], const char *envp[])
{
#if 1 // Enable for debug logging
    lldb::StreamSP logStream(new lldb_private::StreamCallback(LogOutput, 0));
    const char* logCategories[] = { 0 };
    lldb::LogSP log = lldb_private::EnableLog(logStream, 0, logCategories, 0);
    log->GetMask().Reset(LIBLLDB_LOG_ALL);
#endif
    SBDebugger::Initialize();
    
    SBHostOS::ThreadCreated ("<lldb.driver.main-thread>");

	SetupPosixSignals();
    // Create a scope for driver so that the driver object will destroy itself
    // before SBDebugger::Terminate() is called.
    {
        Driver driver;

        bool exit = false;
        SBError error = driver.ParseArgs (argc, argv, stdout, exit);
        if (error.Fail())
        {
            const char *error_cstr = error.GetCString ();
            if (error_cstr)
                ::fprintf (stderr, "error: %s\n", error_cstr);
        }
        else if (!exit)
        {
            driver.Initialize();
        }
    }

    SBDebugger::Terminate();
    return 0;
}
Exemple #3
0
void
Driver::MainLoop ()
{
    if (::tcgetattr(STDIN_FILENO, &g_old_stdin_termios) == 0)
    {
        g_old_stdin_termios_is_valid = true;
        atexit (reset_stdin_termios);
    }

    ::setbuf (stdin, NULL);
    ::setbuf (stdout, NULL);

    m_debugger.SetErrorFileHandle (stderr, false);
    m_debugger.SetOutputFileHandle (stdout, false);
    m_debugger.SetInputFileHandle (stdin, false); // Don't take ownership of STDIN yet...

    m_debugger.SetUseExternalEditor(m_option_data.m_use_external_editor);

    struct winsize window_size;
    if (isatty (STDIN_FILENO)
        && ::ioctl (STDIN_FILENO, TIOCGWINSZ, &window_size) == 0)
    {
        if (window_size.ws_col > 0)
            m_debugger.SetTerminalWidth (window_size.ws_col);
    }

    SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
    
    // Before we handle any options from the command line, we parse the
    // .lldbinit file in the user's home directory.
    SBCommandReturnObject result;
    sb_interpreter.SourceInitFileInHomeDirectory(result);
    if (GetDebugMode())
    {
        result.PutError (m_debugger.GetErrorFileHandle());
        result.PutOutput (m_debugger.GetOutputFileHandle());
    }

    // Now we handle options we got from the command line
    SBStream commands_stream;
    
    // First source in the commands specified to be run before the file arguments are processed.
    WriteCommandsForSourcing(eCommandPlacementBeforeFile, commands_stream);
        
    const size_t num_args = m_option_data.m_args.size();
    if (num_args > 0)
    {
        char arch_name[64];
        if (m_debugger.GetDefaultArchitecture (arch_name, sizeof (arch_name)))
            commands_stream.Printf("target create --arch=%s %s", arch_name, EscapeString(m_option_data.m_args[0]).c_str());
        else
            commands_stream.Printf("target create %s", EscapeString(m_option_data.m_args[0]).c_str());

        if (!m_option_data.m_core_file.empty())
        {
            commands_stream.Printf(" --core %s", EscapeString(m_option_data.m_core_file).c_str());
        }
        commands_stream.Printf("\n");
        
        if (num_args > 1)
        {
            commands_stream.Printf ("settings set -- target.run-args ");
            for (size_t arg_idx = 1; arg_idx < num_args; ++arg_idx)
                commands_stream.Printf(" %s", EscapeString(m_option_data.m_args[arg_idx]).c_str());
            commands_stream.Printf("\n");
        }
    }
    else if (!m_option_data.m_core_file.empty())
    {
        commands_stream.Printf("target create --core %s\n", EscapeString(m_option_data.m_core_file).c_str());
    }
    else if (!m_option_data.m_process_name.empty())
    {
        commands_stream.Printf ("process attach --name %s", EscapeString(m_option_data.m_process_name).c_str());
        
        if (m_option_data.m_wait_for)
            commands_stream.Printf(" --waitfor");

        commands_stream.Printf("\n");

    }
    else if (LLDB_INVALID_PROCESS_ID != m_option_data.m_process_pid)
    {
        commands_stream.Printf ("process attach --pid %" PRIu64 "\n", m_option_data.m_process_pid);
    }

    WriteCommandsForSourcing(eCommandPlacementAfterFile, commands_stream);
    
    if (GetDebugMode())
    {
        result.PutError(m_debugger.GetErrorFileHandle());
        result.PutOutput(m_debugger.GetOutputFileHandle());
    }
    
    bool handle_events = true;
    bool spawn_thread = false;

    if (m_option_data.m_repl)
    {
        const char *repl_options = NULL;
        if (!m_option_data.m_repl_options.empty())
            repl_options = m_option_data.m_repl_options.c_str();
        SBError error (m_debugger.RunREPL(m_option_data.m_repl_lang, repl_options));
        if (error.Fail())
        {
            const char *error_cstr = error.GetCString();
            if (error_cstr && error_cstr[0])
                fprintf (stderr, "error: %s\n", error_cstr);
            else
                fprintf (stderr, "error: %u\n", error.GetError());
        }
    }
    else
    {
        // Check if we have any data in the commands stream, and if so, save it to a temp file
        // so we can then run the command interpreter using the file contents.
        const char *commands_data = commands_stream.GetData();
        const size_t commands_size = commands_stream.GetSize();

        // The command file might have requested that we quit, this variable will track that.
        bool quit_requested = false;
        bool stopped_for_crash = false;
        if (commands_data && commands_size)
        {
            int initial_commands_fds[2];
            bool success = true;
            FILE *commands_file = PrepareCommandsForSourcing (commands_data, commands_size, initial_commands_fds);
            if (commands_file)
            {
                m_debugger.SetInputFileHandle (commands_file, true);

                // Set the debugger into Sync mode when running the command file.  Otherwise command files
                // that run the target won't run in a sensible way.
                bool old_async = m_debugger.GetAsync();
                m_debugger.SetAsync(false);
                int num_errors;
                
                SBCommandInterpreterRunOptions options;
                options.SetStopOnError (true);
                if (m_option_data.m_batch)
                    options.SetStopOnCrash (true);

                m_debugger.RunCommandInterpreter(handle_events,
                                                 spawn_thread,
                                                 options,
                                                 num_errors,
                                                 quit_requested,
                                                 stopped_for_crash);

                if (m_option_data.m_batch && stopped_for_crash && !m_option_data.m_after_crash_commands.empty())
                {
                    int crash_command_fds[2];
                    SBStream crash_commands_stream;
                    WriteCommandsForSourcing (eCommandPlacementAfterCrash, crash_commands_stream);
                    const char *crash_commands_data = crash_commands_stream.GetData();
                    const size_t crash_commands_size = crash_commands_stream.GetSize();
                    commands_file  = PrepareCommandsForSourcing (crash_commands_data, crash_commands_size, crash_command_fds);
                    if (commands_file)
                    {
                        bool local_quit_requested;
                        bool local_stopped_for_crash;
                        m_debugger.SetInputFileHandle (commands_file, true);

                        m_debugger.RunCommandInterpreter(handle_events,
                                                         spawn_thread,
                                                         options,
                                                         num_errors,
                                                         local_quit_requested,
                                                         local_stopped_for_crash);
                        if (local_quit_requested)
                            quit_requested = true;

                    }
                }
                m_debugger.SetAsync(old_async);
            }
            else
                success = false;

            // Close any pipes that we still have ownership of
            CleanupAfterCommandSourcing(initial_commands_fds);

            // Something went wrong with command pipe
            if (!success)
            {
                exit(1);
            }

        }

        // Now set the input file handle to STDIN and run the command
        // interpreter again in interactive mode and let the debugger
        // take ownership of stdin

        bool go_interactive = true;
        if (quit_requested)
            go_interactive = false;
        else if (m_option_data.m_batch && !stopped_for_crash)
            go_interactive = false;

        if (go_interactive)
        {
            m_debugger.SetInputFileHandle (stdin, true);
            m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
        }
    }
    
    reset_stdin_termios();
    fclose (stdin);
    
    SBDebugger::Destroy (m_debugger);
}
Exemple #4
0
SBError
Driver::ParseArgs (int argc, const char *argv[], FILE *out_fh, bool &exiting)
{
    ResetOptionValues ();

    SBCommandReturnObject result;

    SBError error;
    std::string option_string;
    struct option *long_options = NULL;
    std::vector<struct option> long_options_vector;
    uint32_t num_options;

    for (num_options = 0; g_options[num_options].long_option != NULL; ++num_options)
        /* Do Nothing. */;

    if (num_options == 0)
    {
        if (argc > 1)
            error.SetErrorStringWithFormat ("invalid number of options");
        return error;
    }

    BuildGetOptTable (g_options, long_options_vector, num_options);

    if (long_options_vector.empty())
        long_options = NULL;
    else
        long_options = &long_options_vector.front();

    if (long_options == NULL)
    {
        error.SetErrorStringWithFormat ("invalid long options");
        return error;
    }

    // Build the option_string argument for call to getopt_long_only.

    for (int i = 0; long_options[i].name != NULL; ++i)
    {
        if (long_options[i].flag == NULL)
        {
            option_string.push_back ((char) long_options[i].val);
            switch (long_options[i].has_arg)
            {
                default:
                case no_argument:
                    break;
                case required_argument:
                    option_string.push_back (':');
                    break;
                case optional_argument:
                    option_string.append ("::");
                    break;
            }
        }
    }

    // This is kind of a pain, but since we make the debugger in the Driver's constructor, we can't
    // know at that point whether we should read in init files yet.  So we don't read them in in the
    // Driver constructor, then set the flags back to "read them in" here, and then if we see the
    // "-n" flag, we'll turn it off again.  Finally we have to read them in by hand later in the
    // main loop.
    
    m_debugger.SkipLLDBInitFiles (false);
    m_debugger.SkipAppInitFiles (false);

    // Prepare for & make calls to getopt_long_only.
#if __GLIBC__
    optind = 0;
#else
    optreset = 1;
    optind = 1;
#endif
    int val;
    while (1)
    {
        int long_options_index = -1;
        val = ::getopt_long_only (argc, const_cast<char **>(argv), option_string.c_str(), long_options, &long_options_index);

        if (val == -1)
            break;
        else if (val == '?')
        {
            m_option_data.m_print_help = true;
            error.SetErrorStringWithFormat ("unknown or ambiguous option");
            break;
        }
        else if (val == 0)
            continue;
        else
        {
            m_option_data.m_seen_options.insert ((char) val);
            if (long_options_index == -1)
            {
                for (int i = 0;
                     long_options[i].name || long_options[i].has_arg || long_options[i].flag || long_options[i].val;
                     ++i)
                {
                    if (long_options[i].val == val)
                    {
                        long_options_index = i;
                        break;
                    }
                }
            }

            if (long_options_index >= 0)
            {
                const int short_option = g_options[long_options_index].short_option;

                switch (short_option)
                {
                    case 'h':
                        m_option_data.m_print_help = true;
                        break;

                    case 'v':
                        m_option_data.m_print_version = true;
                        break;

                    case 'P':
                        m_option_data.m_print_python_path = true;
                        break;

                    case 'b':
                        m_option_data.m_batch = true;
                        break;

                    case 'c':
                        {
                            SBFileSpec file(optarg);
                            if (file.Exists())
                            {
                                m_option_data.m_core_file = optarg;
                            }
                            else
                                error.SetErrorStringWithFormat("file specified in --core (-c) option doesn't exist: '%s'", optarg);
                        }
                        break;
                    
                    case 'e':
                        m_option_data.m_use_external_editor = true;
                        break;

                    case 'x':
                        m_debugger.SkipLLDBInitFiles (true);
                        m_debugger.SkipAppInitFiles (true);
                        break;

                    case 'X':
                        m_debugger.SetUseColor (false);
                        break;

                    case 'f':
                        {
                            SBFileSpec file(optarg);
                            if (file.Exists())
                            {
                                m_option_data.m_args.push_back (optarg);
                            }
                            else if (file.ResolveExecutableLocation())
                            {
                                char path[PATH_MAX];
                                file.GetPath (path, sizeof(path));
                                m_option_data.m_args.push_back (path);
                            }
                            else
                                error.SetErrorStringWithFormat("file specified in --file (-f) option doesn't exist: '%s'", optarg);
                        }
                        break;

                    case 'a':
                        if (!m_debugger.SetDefaultArchitecture (optarg))
                            error.SetErrorStringWithFormat("invalid architecture in the -a or --arch option: '%s'", optarg);
                        break;

                    case 'l':
                        m_option_data.m_script_lang = m_debugger.GetScriptingLanguage (optarg);
                        break;

                    case 'd':
                        m_option_data.m_debug_mode = true;
                        break;

                    case 'Q':
                        m_option_data.m_source_quietly = true;
                        break;

                    case 'K':
                        m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterCrash, true, error);
                        break;
                    case 'k':
                        m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterCrash, false, error);
                        break;

                    case 'n':
                        m_option_data.m_process_name = optarg;
                        break;
                    
                    case 'w':
                        m_option_data.m_wait_for = true;
                        break;
                        
                    case 'p':
                        {
                            char *remainder;
                            m_option_data.m_process_pid = strtol (optarg, &remainder, 0);
                            if (remainder == optarg || *remainder != '\0')
                                error.SetErrorStringWithFormat ("Could not convert process PID: \"%s\" into a pid.",
                                                                optarg);
                        }
                        break;
                        
                    case 'r':
                        m_option_data.m_repl = true;
                        if (optarg && optarg[0])
                            m_option_data.m_repl_options = optarg;
                        else
                            m_option_data.m_repl_options.clear();
                        break;
                    
                    case 'R':
                        m_option_data.m_repl_lang = SBLanguageRuntime::GetLanguageTypeFromString (optarg);
                        if (m_option_data.m_repl_lang == eLanguageTypeUnknown)
                        {
                            error.SetErrorStringWithFormat ("Unrecognized language name: \"%s\"", optarg);
                        }
                        break;

                    case 's':
                        m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterFile, true, error);
                        break;
                    case 'o':
                        m_option_data.AddInitialCommand(optarg, eCommandPlacementAfterFile, false, error);
                        break;
                    case 'S':
                        m_option_data.AddInitialCommand(optarg, eCommandPlacementBeforeFile, true, error);
                        break;
                    case 'O':
                        m_option_data.AddInitialCommand(optarg, eCommandPlacementBeforeFile, false, error);
                        break;
                    default:
                        m_option_data.m_print_help = true;
                        error.SetErrorStringWithFormat ("unrecognized option %c", short_option);
                        break;
                }
            }
            else
            {
                error.SetErrorStringWithFormat ("invalid option with value %i", val);
            }
            if (error.Fail())
            {
                return error;
            }
        }
    }
    
    if (error.Fail() || m_option_data.m_print_help)
    {
        ShowUsage (out_fh, g_options, m_option_data);
        exiting = true;
    }
    else if (m_option_data.m_print_version)
    {
        ::fprintf (out_fh, "%s\n", m_debugger.GetVersionString());
        exiting = true;
    }
    else if (m_option_data.m_print_python_path)
    {
        SBFileSpec python_file_spec = SBHostOS::GetLLDBPythonPath();
        if (python_file_spec.IsValid())
        {
            char python_path[PATH_MAX];
            size_t num_chars = python_file_spec.GetPath(python_path, PATH_MAX);
            if (num_chars < PATH_MAX)
            {
                ::fprintf (out_fh, "%s\n", python_path);
            }
            else
                ::fprintf (out_fh, "<PATH TOO LONG>\n");
        }
        else
            ::fprintf (out_fh, "<COULD NOT FIND PATH>\n");
        exiting = true;
    }
    else if (m_option_data.m_process_name.empty() && m_option_data.m_process_pid == LLDB_INVALID_PROCESS_ID)
    {
        // Any arguments that are left over after option parsing are for
        // the program. If a file was specified with -f then the filename
        // is already in the m_option_data.m_args array, and any remaining args
        // are arguments for the inferior program. If no file was specified with
        // -f, then what is left is the program name followed by any arguments.

        // Skip any options we consumed with getopt_long_only
        argc -= optind;
        argv += optind;

        if (argc > 0)
        {
            for (int arg_idx=0; arg_idx<argc; ++arg_idx)
            {
                const char *arg = argv[arg_idx];
                if (arg)
                    m_option_data.m_args.push_back (arg);
            }
        }
        
    }
    else
    {
        // Skip any options we consumed with getopt_long_only
        argc -= optind;
        //argv += optind; // Commented out to keep static analyzer happy

        if (argc > 0)
            ::fprintf (out_fh, "Warning: program arguments are ignored when attaching.\n");
    }

    return error;
}