Convert all RunShellCommand functions to use the Timeout class
authorPavel Labath <labath@google.com>
Thu, 10 May 2018 10:46:03 +0000 (10:46 +0000)
committerPavel Labath <labath@google.com>
Thu, 10 May 2018 10:46:03 +0000 (10:46 +0000)
this completes the Timeout migration started in r331880 with the
Predicate class.

llvm-svn: 331970

15 files changed:
lldb/include/lldb/Host/Host.h
lldb/include/lldb/Target/Platform.h
lldb/source/API/SBPlatform.cpp
lldb/source/Commands/CommandObjectPlatform.cpp
lldb/source/Host/common/Host.cpp
lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
lldb/source/Plugins/Platform/MacOSX/PlatformMacOSX.cpp
lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.h
lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
lldb/source/Target/Platform.cpp

index 0ff38c404b037e5caaf757150818407e1fdf3a37..c6d5c3c4b67af48d0c48989872f093c91f80b390 100644 (file)
@@ -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 <cerrno>
@@ -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<std::micro> &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<std::micro> &timeout, bool run_in_default_shell = true);
 
   static bool OpenFileInExternalEditor(const FileSpec &file_spec,
                                        uint32_t line_no);
index 62a536be10f7c52bb3702ea7815b9c202a7c58cf..e0ae6577fd94e4eee451cff4ff15abc48e9f963b 100644 (file)
@@ -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<std::micro> &timeout);
 
   virtual void SetLocalCacheDirectory(const char *local);
 
index 8d2c401358243b089b5e363b28f2dde9d224cf10..d559a66070f519ceeb9a2ea3a0ef63f00b7759d5 100644 (file)
@@ -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<std::ratio<1>> 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);
   });
 }
 
index 379e8507f4e3d2f649cd0baae1ff5c85b3ff8f0f..0c979032708fe3212ebc1343f50b755b0965ddb5 100644 (file)
@@ -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<std::micro> timeout = std::chrono::seconds(10);
   };
 
   CommandObjectPlatformShell(CommandInterpreter &interpreter)
index becd914650bbff8a1001a79bfed4143b9fa46151..1b779449b5f071d85a610cedfbc90458da369fe3 100644 (file)
@@ -463,15 +463,17 @@ MonitorShellCommand(std::shared_ptr<ShellInfo> 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<std::micro> &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<std::micro> &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<std::micro> 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");
 
index e6ed57ac7165d86074f381add455350a1da3afd8..73b4003ff05aa12828f4bcdb495e4706b1d09828 100644 (file)
@@ -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) {
index e6311abfc04eb9faffd3bd9101017f84eb704ec8..50b5988b48a835e41f16279deba0ab7d99b9a0fe 100644 (file)
@@ -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)
index c5b796d850da7b2e7d5c2006a3b0af589aa1f1f9..f7a82651022df643292ac1b1f6b69e71ab8a7db7 100644 (file)
@@ -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<std::micro> &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
index d9ebdff72df84719106608b5c5710bd34eff2d3c..1235e21f30dfcc57532808e7789d55ef9eca7741 100644 (file)
@@ -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<std::micro> &timeout) override;
 
   lldb_private::Status ResolveExecutable(
       const lldb_private::ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
index a67980156f3b4e937193fd8faf93a4c19065ab2e..10cff9e59439e3f4c1f10e495a19956062f1f5ee 100644 (file)
@@ -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<std::micro> &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() {
index 210544f752e61bd343caa9671c704173aa43d1a1..944871052fc3af3ec552a2f74b5dad44d3f270ec 100644 (file)
@@ -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<std::micro> &timeout) override;
 
   void CalculateTrapHandlerSymbolNames() override;
 
index 231fa9043c443eda45832974b7ed276487c6ad86..844e248dd0aa3920e1efc02d1472a6ccfc60b5a3 100644 (file)
@@ -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<std::micro> &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<double>(*timeout).count());
+  }
   stream.PutHex32(timeout_sec);
   if (working_dir) {
     std::string path{working_dir.GetPath(false)};
index 8898767e83c28b18a3ede5959fef0bfa11eddec0..fcf578f9918adfd84727f4c3cce7582ae2691f12 100644 (file)
@@ -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<std::micro> &timeout);
 
   bool CalculateMD5(const FileSpec &file_spec, uint64_t &high, uint64_t &low);
 
index 47dca596eb416d02abe88f66b47af8666a7e5890..c73fc56119cc374d4e2681b35a8a1a7a7e42861d 100644 (file)
@@ -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,");
index e5d1f47347f85a2760df31d639186ffd6564e15d..be94f37aea91d8fe9ee48faa8c3efb051c0bbd6a 100644 (file)
@@ -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<std::micro> &timeout) {
   if (IsHost())
     return Host::RunShellCommand(command, working_dir, status_ptr, signo_ptr,
-                                 command_output, timeout_sec);
+                                 command_output, timeout);
   else
     return Status("unimplemented");
 }