From 19dd1a0ea6fed11b40cb9f83adbce17c04d569f3 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Thu, 10 May 2018 10:46:03 +0000 Subject: [PATCH] Convert all RunShellCommand functions to use the Timeout class this completes the Timeout migration started in r331880 with the Predicate class. llvm-svn: 331970 --- lldb/include/lldb/Host/Host.h | 7 +++--- lldb/include/lldb/Target/Platform.h | 4 ++-- lldb/source/API/SBPlatform.cpp | 25 ++++++++++++--------- lldb/source/Commands/CommandObjectPlatform.cpp | 10 +++++---- lldb/source/Host/common/Host.cpp | 12 +++++----- .../Plugins/Platform/MacOSX/PlatformDarwin.cpp | 4 ++-- .../Plugins/Platform/MacOSX/PlatformMacOSX.cpp | 2 +- .../Plugins/Platform/POSIX/PlatformPOSIX.cpp | 26 ++++++++++++---------- lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h | 3 +-- .../gdb-server/PlatformRemoteGDBServer.cpp | 6 ++--- .../Platform/gdb-server/PlatformRemoteGDBServer.h | 3 +-- .../gdb-remote/GDBRemoteCommunicationClient.cpp | 9 +++++--- .../gdb-remote/GDBRemoteCommunicationClient.h | 3 +-- .../GDBRemoteCommunicationServerCommon.cpp | 7 +++--- lldb/source/Target/Platform.cpp | 6 ++--- 15 files changed, 63 insertions(+), 64 deletions(-) diff --git a/lldb/include/lldb/Host/Host.h b/lldb/include/lldb/Host/Host.h index 0ff38c4..c6d5c3c 100644 --- a/lldb/include/lldb/Host/Host.h +++ b/lldb/include/lldb/Host/Host.h @@ -14,6 +14,7 @@ #include "lldb/Host/HostThread.h" #include "lldb/Utility/Environment.h" #include "lldb/Utility/FileSpec.h" +#include "lldb/Utility/Timeout.h" #include "lldb/lldb-private-forward.h" #include "lldb/lldb-private.h" #include @@ -219,8 +220,7 @@ public: // process to exit std::string *command_output, // Pass NULL if you don't want the command output - uint32_t timeout_sec, - bool run_in_default_shell = true); + const Timeout &timeout, bool run_in_default_shell = true); static Status RunShellCommand( const Args &args, @@ -231,8 +231,7 @@ public: // process to exit std::string *command_output, // Pass NULL if you don't want the command output - uint32_t timeout_sec, - bool run_in_default_shell = true); + const Timeout &timeout, bool run_in_default_shell = true); static bool OpenFileInExternalEditor(const FileSpec &file_spec, uint32_t line_no); diff --git a/lldb/include/lldb/Target/Platform.h b/lldb/include/lldb/Target/Platform.h index 62a536b..e0ae657 100644 --- a/lldb/include/lldb/Target/Platform.h +++ b/lldb/include/lldb/Target/Platform.h @@ -27,6 +27,7 @@ #include "lldb/Utility/ArchSpec.h" #include "lldb/Utility/ConstString.h" #include "lldb/Utility/FileSpec.h" +#include "lldb/Utility/Timeout.h" #include "lldb/lldb-private-forward.h" #include "lldb/lldb-public.h" @@ -676,8 +677,7 @@ public: // the process to exit std::string *command_output, // Pass nullptr if you don't want the command output - uint32_t timeout_sec); // Timeout in seconds to wait for shell program to - // finish + const Timeout &timeout); virtual void SetLocalCacheDirectory(const char *local); diff --git a/lldb/source/API/SBPlatform.cpp b/lldb/source/API/SBPlatform.cpp index 8d2c401..d559a66 100644 --- a/lldb/source/API/SBPlatform.cpp +++ b/lldb/source/API/SBPlatform.cpp @@ -53,8 +53,7 @@ struct PlatformConnectOptions { //---------------------------------------------------------------------- struct PlatformShellCommand { PlatformShellCommand(const char *shell_command = NULL) - : m_command(), m_working_dir(), m_status(0), m_signo(0), - m_timeout_sec(UINT32_MAX) { + : m_command(), m_working_dir(), m_status(0), m_signo(0) { if (shell_command && shell_command[0]) m_command = shell_command; } @@ -66,7 +65,7 @@ struct PlatformShellCommand { std::string m_output; int m_status; int m_signo; - uint32_t m_timeout_sec; + Timeout> m_timeout = llvm::None; }; //---------------------------------------------------------------------- // SBPlatformConnectOptions @@ -182,11 +181,16 @@ void SBPlatformShellCommand::SetWorkingDirectory(const char *path) { } uint32_t SBPlatformShellCommand::GetTimeoutSeconds() { - return m_opaque_ptr->m_timeout_sec; + if (m_opaque_ptr->m_timeout) + return m_opaque_ptr->m_timeout->count(); + return UINT32_MAX; } void SBPlatformShellCommand::SetTimeoutSeconds(uint32_t sec) { - m_opaque_ptr->m_timeout_sec = sec; + if (sec == UINT32_MAX) + m_opaque_ptr->m_timeout = llvm::None; + else + m_opaque_ptr->m_timeout = std::chrono::seconds(sec); } int SBPlatformShellCommand::GetSignal() { return m_opaque_ptr->m_signo; } @@ -405,12 +409,11 @@ SBError SBPlatform::Run(SBPlatformShellCommand &shell_command) { if (working_dir) shell_command.SetWorkingDirectory(working_dir); } - return platform_sp->RunShellCommand( - command, FileSpec{working_dir, false}, - &shell_command.m_opaque_ptr->m_status, - &shell_command.m_opaque_ptr->m_signo, - &shell_command.m_opaque_ptr->m_output, - shell_command.m_opaque_ptr->m_timeout_sec); + return platform_sp->RunShellCommand(command, FileSpec{working_dir, false}, + &shell_command.m_opaque_ptr->m_status, + &shell_command.m_opaque_ptr->m_signo, + &shell_command.m_opaque_ptr->m_output, + shell_command.m_opaque_ptr->m_timeout); }); } diff --git a/lldb/source/Commands/CommandObjectPlatform.cpp b/lldb/source/Commands/CommandObjectPlatform.cpp index 379e850..0c97903 100644 --- a/lldb/source/Commands/CommandObjectPlatform.cpp +++ b/lldb/source/Commands/CommandObjectPlatform.cpp @@ -1692,7 +1692,7 @@ class CommandObjectPlatformShell : public CommandObjectRaw { public: class CommandOptions : public Options { public: - CommandOptions() : Options(), timeout(10) {} + CommandOptions() : Options() {} ~CommandOptions() override = default; @@ -1708,11 +1708,13 @@ public: switch (short_option) { case 't': - timeout = 10; - if (option_arg.getAsInteger(10, timeout)) + uint32_t timeout_sec; + if (option_arg.getAsInteger(10, timeout_sec)) error.SetErrorStringWithFormat( "could not convert \"%s\" to a numeric value.", option_arg.str().c_str()); + else + timeout = std::chrono::seconds(timeout_sec); break; default: error.SetErrorStringWithFormat("invalid short option character '%c'", @@ -1725,7 +1727,7 @@ public: void OptionParsingStarting(ExecutionContext *execution_context) override {} - uint32_t timeout; + Timeout timeout = std::chrono::seconds(10); }; CommandObjectPlatformShell(CommandInterpreter &interpreter) diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp index becd914..1b77944 100644 --- a/lldb/source/Host/common/Host.cpp +++ b/lldb/source/Host/common/Host.cpp @@ -463,15 +463,17 @@ MonitorShellCommand(std::shared_ptr shell_info, lldb::pid_t pid, Status Host::RunShellCommand(const char *command, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output_ptr, - uint32_t timeout_sec, bool run_in_default_shell) { + const Timeout &timeout, + bool run_in_default_shell) { return RunShellCommand(Args(command), working_dir, status_ptr, signo_ptr, - command_output_ptr, timeout_sec, run_in_default_shell); + command_output_ptr, timeout, run_in_default_shell); } Status Host::RunShellCommand(const Args &args, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output_ptr, - uint32_t timeout_sec, bool run_in_default_shell) { + const Timeout &timeout, + bool run_in_default_shell) { Status error; ProcessLaunchInfo launch_info; launch_info.SetArchitecture(HostInfo::GetArchitecture()); @@ -536,10 +538,6 @@ Status Host::RunShellCommand(const Args &args, const FileSpec &working_dir, error.SetErrorString("failed to get process ID"); if (error.Success()) { - // TODO: Remove this and make the function take Timeout<> argument. - Timeout timeout(llvm::None); - if (timeout_sec != 0) - timeout = std::chrono::seconds(timeout_sec); if (!shell_info_sp->process_reaped.WaitForValueEqualTo(true, timeout)) { error.SetErrorString("timed out waiting for shell command to complete"); diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp index e6ed57a..73b4003 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -1193,7 +1193,7 @@ const char *PlatformDarwin::GetDeveloperDirectory() { Host::RunShellCommand("/usr/bin/xcode-select --print-path", NULL, // current working directory &exit_status, &signo, &command_output, - 2, // short timeout + std::chrono::seconds(2), // short timeout false); // don't run in a shell if (error.Success() && exit_status == 0 && !command_output.empty()) { const char *cmd_output_ptr = command_output.c_str(); @@ -1365,7 +1365,7 @@ static FileSpec GetXcodeContentsPath() { &signo, // Put the signal that caused the process to exit in here &output, // Get the output from the command and place it in this // string - 3); // Timeout in seconds to wait for shell program to finish + std::chrono::seconds(3)); if (status == 0 && !output.empty()) { size_t first_non_newline = output.find_last_not_of("\r\n"); if (first_non_newline != std::string::npos) { diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp index e6311ab..50b5988 100644 --- a/lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp +++ b/lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp @@ -196,7 +196,7 @@ ConstString PlatformMacOSX::GetSDKDirectory(lldb_private::Target &target) { // here &output, // Get the output from the command and place it in this // string - 3); // Timeout in seconds to wait for shell program to finish + std::chrono::seconds(3)); if (status == 0 && !output.empty()) { size_t first_non_newline = output.find_last_not_of("\r\n"); if (first_non_newline != std::string::npos) diff --git a/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp b/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp index c5b796d..f7a8265 100644 --- a/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp +++ b/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp @@ -102,17 +102,14 @@ lldb_private::Status PlatformPOSIX::RunShellCommand( // process to exit std::string *command_output, // Pass NULL if you don't want the command output - uint32_t - timeout_sec) // Timeout in seconds to wait for shell program to finish -{ + const Timeout &timeout) { if (IsHost()) return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr, - command_output, timeout_sec); + command_output, timeout); else { if (m_remote_platform_sp) - return m_remote_platform_sp->RunShellCommand(command, working_dir, - status_ptr, signo_ptr, - command_output, timeout_sec); + return m_remote_platform_sp->RunShellCommand( + command, working_dir, status_ptr, signo_ptr, command_output, timeout); else return Status("unable to run a remote command without a platform"); } @@ -372,7 +369,8 @@ static uint32_t chown_file(Platform *platform, const char *path, command.Printf(":%d", gid); command.Printf("%s", path); int status; - platform->RunShellCommand(command.GetData(), NULL, &status, NULL, NULL, 10); + platform->RunShellCommand(command.GetData(), NULL, &status, NULL, NULL, + std::chrono::seconds(10)); return status; } @@ -396,7 +394,8 @@ PlatformPOSIX::PutFile(const lldb_private::FileSpec &source, StreamString command; command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str()); int status; - RunShellCommand(command.GetData(), NULL, &status, NULL, NULL, 10); + RunShellCommand(command.GetData(), NULL, &status, NULL, NULL, + std::chrono::seconds(10)); if (status != 0) return Status("unable to perform copy"); if (uid == UINT32_MAX && gid == UINT32_MAX) @@ -426,7 +425,8 @@ PlatformPOSIX::PutFile(const lldb_private::FileSpec &source, if (log) log->Printf("[PutFile] Running command: %s\n", command.GetData()); int retcode; - Host::RunShellCommand(command.GetData(), NULL, &retcode, NULL, NULL, 60); + Host::RunShellCommand(command.GetData(), NULL, &retcode, NULL, NULL, + std::chrono::minutes(1)); if (retcode == 0) { // Don't chown a local file for a remote system // if (chown_file(this,dst_path.c_str(),uid,gid) != 0) @@ -500,7 +500,8 @@ lldb_private::Status PlatformPOSIX::GetFile( StreamString cp_command; cp_command.Printf("cp %s %s", src_path.c_str(), dst_path.c_str()); int status; - RunShellCommand(cp_command.GetData(), NULL, &status, NULL, NULL, 10); + RunShellCommand(cp_command.GetData(), NULL, &status, NULL, NULL, + std::chrono::seconds(10)); if (status != 0) return Status("unable to perform copy"); return Status(); @@ -521,7 +522,8 @@ lldb_private::Status PlatformPOSIX::GetFile( if (log) log->Printf("[GetFile] Running command: %s\n", command.GetData()); int retcode; - Host::RunShellCommand(command.GetData(), NULL, &retcode, NULL, NULL, 60); + Host::RunShellCommand(command.GetData(), NULL, &retcode, NULL, NULL, + std::chrono::minutes(1)); if (retcode == 0) return Status(); // If we are here, rsync has failed - let's try the slow way before diff --git a/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h b/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h index d9ebdff..1235e21 100644 --- a/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h +++ b/lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h @@ -99,8 +99,7 @@ public: // the process to exit std::string *command_output, // Pass nullptr if you don't want the command output - uint32_t timeout_sec) - override; // Timeout in seconds to wait for shell program to finish + const lldb_private::Timeout &timeout) override; lldb_private::Status ResolveExecutable( const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp, diff --git a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp index a679801..10cff9e 100644 --- a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp +++ b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp @@ -720,11 +720,9 @@ Status PlatformRemoteGDBServer::RunShellCommand( // process to exit std::string *command_output, // Pass NULL if you don't want the command output - uint32_t - timeout_sec) // Timeout in seconds to wait for shell program to finish -{ + const Timeout &timeout) { return m_gdb_client.RunShellCommand(command, working_dir, status_ptr, - signo_ptr, command_output, timeout_sec); + signo_ptr, command_output, timeout); } void PlatformRemoteGDBServer::CalculateTrapHandlerSymbolNames() { diff --git a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h index 210544f..9448710 100644 --- a/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h +++ b/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h @@ -155,8 +155,7 @@ public: // process to exit std::string *command_output, // Pass NULL if you don't want the command output - uint32_t timeout_sec) - override; // Timeout in seconds to wait for shell program to finish + const lldb_private::Timeout &timeout) override; void CalculateTrapHandlerSymbolNames() override; diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp index 231fa90..844e248 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp @@ -2796,13 +2796,16 @@ lldb_private::Status GDBRemoteCommunicationClient::RunShellCommand( // process to exit std::string *command_output, // Pass NULL if you don't want the command output - uint32_t - timeout_sec) // Timeout in seconds to wait for shell program to finish -{ + const Timeout &timeout) { lldb_private::StreamString stream; stream.PutCString("qPlatform_shell:"); stream.PutBytesAsRawHex8(command, strlen(command)); stream.PutChar(','); + uint32_t timeout_sec = UINT32_MAX; + if (timeout) { + // TODO: Use chrono version of std::ceil once c++17 is available. + timeout_sec = std::ceil(std::chrono::duration(*timeout).count()); + } stream.PutHex32(timeout_sec); if (working_dir) { std::string path{working_dir.GetPath(false)}; diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h index 8898767..fcf578f 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h @@ -404,8 +404,7 @@ public: // the process to exit std::string *command_output, // Pass nullptr if you don't want the command output - uint32_t timeout_sec); // Timeout in seconds to wait for shell program to - // finish + const Timeout &timeout); bool CalculateMD5(const FileSpec &file_spec, uint64_t &high, uint64_t &low); diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp index 47dca59..c73fc56 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp @@ -735,14 +735,13 @@ GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell( if (packet.GetChar() == ',') { // FIXME: add timeout to qPlatform_shell packet // uint32_t timeout = packet.GetHexMaxU32(false, 32); - uint32_t timeout = 10; if (packet.GetChar() == ',') packet.GetHexByteString(working_dir); int status, signo; std::string output; - Status err = - Host::RunShellCommand(path.c_str(), FileSpec{working_dir, true}, - &status, &signo, &output, timeout); + Status err = Host::RunShellCommand( + path.c_str(), FileSpec{working_dir, true}, &status, &signo, &output, + std::chrono::seconds(10)); StreamGDBRemote response; if (err.Fail()) { response.PutCString("F,"); diff --git a/lldb/source/Target/Platform.cpp b/lldb/source/Target/Platform.cpp index e5d1f47..be94f37 100644 --- a/lldb/source/Target/Platform.cpp +++ b/lldb/source/Target/Platform.cpp @@ -1368,12 +1368,10 @@ lldb_private::Status Platform::RunShellCommand( // process to exit std::string *command_output, // Pass nullptr if you don't want the command output - uint32_t - timeout_sec) // Timeout in seconds to wait for shell program to finish -{ + const Timeout &timeout) { if (IsHost()) return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr, - command_output, timeout_sec); + command_output, timeout); else return Status("unimplemented"); } -- 2.7.4