Use Timeout<> in EvaluateExpressionOptions class
authorPavel Labath <labath@google.com>
Tue, 6 Dec 2016 11:24:51 +0000 (11:24 +0000)
committerPavel Labath <labath@google.com>
Tue, 6 Dec 2016 11:24:51 +0000 (11:24 +0000)
llvm-svn: 288797

19 files changed:
lldb/include/lldb/Target/Target.h
lldb/source/API/SBExpressionOptions.cpp
lldb/source/Commands/CommandObjectExpression.cpp
lldb/source/Commands/CommandObjectWatchpoint.cpp
lldb/source/Expression/REPL.cpp
lldb/source/Interpreter/CommandInterpreter.cpp
lldb/source/Plugins/InstrumentationRuntime/AddressSanitizer/AddressSanitizerRuntime.cpp
lldb/source/Plugins/InstrumentationRuntime/ThreadSanitizer/ThreadSanitizerRuntime.cpp
lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntime.cpp
lldb/source/Plugins/LanguageRuntime/ObjC/AppleObjCRuntime/AppleObjCRuntimeV2.cpp
lldb/source/Plugins/MemoryHistory/asan/MemoryHistoryASan.cpp
lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
lldb/source/Plugins/Process/Utility/InferiorCallPOSIX.cpp
lldb/source/Plugins/SystemRuntime/MacOSX/AppleGetItemInfoHandler.cpp
lldb/source/Plugins/SystemRuntime/MacOSX/AppleGetPendingItemsHandler.cpp
lldb/source/Plugins/SystemRuntime/MacOSX/AppleGetQueuesHandler.cpp
lldb/source/Plugins/SystemRuntime/MacOSX/AppleGetThreadItemInfoHandler.cpp
lldb/source/Target/Process.cpp
lldb/source/Target/Target.cpp

index 224af6f..5588bdc 100644 (file)
@@ -33,6 +33,7 @@
 #include "lldb/Target/PathMappingList.h"
 #include "lldb/Target/ProcessLaunchInfo.h"
 #include "lldb/Target/SectionLoadHistory.h"
+#include "lldb/Utility/Timeout.h"
 #include "lldb/lldb-public.h"
 
 namespace lldb_private {
@@ -224,23 +225,11 @@ private:
 
 class EvaluateExpressionOptions {
 public:
-  static const uint32_t default_timeout = 500000;
-  static const ExecutionPolicy default_execution_policy =
+  static constexpr std::chrono::milliseconds default_timeout{500};
+  static constexpr ExecutionPolicy default_execution_policy =
       eExecutionPolicyOnlyWhenNeeded;
 
-  EvaluateExpressionOptions()
-      : m_execution_policy(default_execution_policy),
-        m_language(lldb::eLanguageTypeUnknown),
-        m_prefix(), // A prefix specific to this expression that is added after
-                    // the prefix from the settings (if any)
-        m_coerce_to_id(false), m_unwind_on_error(true),
-        m_ignore_breakpoints(false), m_keep_in_memory(false),
-        m_try_others(true), m_stop_others(true), m_debug(false),
-        m_trap_exceptions(true), m_generate_debug_info(false),
-        m_result_is_internal(false), m_auto_apply_fixits(true),
-        m_use_dynamic(lldb::eNoDynamicValues), m_timeout_usec(default_timeout),
-        m_one_thread_timeout_usec(0), m_cancel_callback(nullptr),
-        m_cancel_callback_baton(nullptr) {}
+  EvaluateExpressionOptions() = default;
 
   ExecutionPolicy GetExecutionPolicy() const { return m_execution_policy; }
 
@@ -288,14 +277,16 @@ public:
     m_use_dynamic = dynamic;
   }
 
-  uint32_t GetTimeoutUsec() const { return m_timeout_usec; }
+  const Timeout<std::micro> &GetTimeout() const { return m_timeout; }
 
-  void SetTimeoutUsec(uint32_t timeout = 0) { m_timeout_usec = timeout; }
+  void SetTimeout(const Timeout<std::micro> &timeout) { m_timeout = timeout; }
 
-  uint32_t GetOneThreadTimeoutUsec() const { return m_one_thread_timeout_usec; }
+  const Timeout<std::micro> &GetOneThreadTimeout() const {
+    return m_one_thread_timeout;
+  }
 
-  void SetOneThreadTimeoutUsec(uint32_t timeout = 0) {
-    m_one_thread_timeout_usec = timeout;
+  void SetOneThreadTimeout(const Timeout<std::micro> &timeout) {
+    m_one_thread_timeout = timeout;
   }
 
   bool GetTryAllThreads() const { return m_try_others; }
@@ -369,27 +360,27 @@ public:
   bool GetAutoApplyFixIts() const { return m_auto_apply_fixits; }
 
 private:
-  ExecutionPolicy m_execution_policy;
-  lldb::LanguageType m_language;
+  ExecutionPolicy m_execution_policy = default_execution_policy;
+  lldb::LanguageType m_language = lldb::eLanguageTypeUnknown;
   std::string m_prefix;
-  bool m_coerce_to_id;
-  bool m_unwind_on_error;
-  bool m_ignore_breakpoints;
-  bool m_keep_in_memory;
-  bool m_try_others;
-  bool m_stop_others;
-  bool m_debug;
-  bool m_trap_exceptions;
-  bool m_repl;
-  bool m_generate_debug_info;
-  bool m_ansi_color_errors;
-  bool m_result_is_internal;
-  bool m_auto_apply_fixits;
-  lldb::DynamicValueType m_use_dynamic;
-  uint32_t m_timeout_usec;
-  uint32_t m_one_thread_timeout_usec;
-  lldb::ExpressionCancelCallback m_cancel_callback;
-  void *m_cancel_callback_baton;
+  bool m_coerce_to_id = false;
+  bool m_unwind_on_error = true;
+  bool m_ignore_breakpoints = false;
+  bool m_keep_in_memory = false;
+  bool m_try_others = true;
+  bool m_stop_others = true;
+  bool m_debug = false;
+  bool m_trap_exceptions = true;
+  bool m_repl = false;
+  bool m_generate_debug_info = false;
+  bool m_ansi_color_errors = false;
+  bool m_result_is_internal = false;
+  bool m_auto_apply_fixits = true;
+  lldb::DynamicValueType m_use_dynamic = lldb::eNoDynamicValues;
+  Timeout<std::micro> m_timeout = default_timeout;
+  Timeout<std::micro> m_one_thread_timeout = llvm::None;
+  lldb::ExpressionCancelCallback m_cancel_callback = nullptr;
+  void *m_cancel_callback_baton = nullptr;
   // If m_pound_line_file is not empty and m_pound_line_line is non-zero,
   // use #line %u "%s" before the expression content to remap where the source
   // originates
index 218d93e..e26fa11 100644 (file)
@@ -67,19 +67,22 @@ void SBExpressionOptions::SetFetchDynamicValue(lldb::DynamicValueType dynamic) {
 }
 
 uint32_t SBExpressionOptions::GetTimeoutInMicroSeconds() const {
-  return m_opaque_ap->GetTimeoutUsec();
+  return m_opaque_ap->GetTimeout() ? m_opaque_ap->GetTimeout()->count() : 0;
 }
 
 void SBExpressionOptions::SetTimeoutInMicroSeconds(uint32_t timeout) {
-  m_opaque_ap->SetTimeoutUsec(timeout);
+  m_opaque_ap->SetTimeout(timeout == 0 ? Timeout<std::micro>(llvm::None)
+                                       : std::chrono::microseconds(timeout));
 }
 
 uint32_t SBExpressionOptions::GetOneThreadTimeoutInMicroSeconds() const {
-  return m_opaque_ap->GetOneThreadTimeoutUsec();
+  return m_opaque_ap->GetOneThreadTimeout() ? m_opaque_ap->GetOneThreadTimeout()->count() : 0;
 }
 
 void SBExpressionOptions::SetOneThreadTimeoutInMicroSeconds(uint32_t timeout) {
-  m_opaque_ap->SetOneThreadTimeoutUsec(timeout);
+  m_opaque_ap->SetOneThreadTimeout(timeout == 0
+                                       ? Timeout<std::micro>(llvm::None)
+                                       : std::chrono::microseconds(timeout));
 }
 
 bool SBExpressionOptions::GetTryAllThreads() const {
index ba477cd..cfb3a6d 100644 (file)
@@ -358,9 +358,9 @@ bool CommandObjectExpression::EvaluateExpression(const char *expr,
       options.SetGenerateDebugInfo(true);
 
     if (m_command_options.timeout > 0)
-      options.SetTimeoutUsec(m_command_options.timeout);
+      options.SetTimeout(std::chrono::microseconds(m_command_options.timeout));
     else
-      options.SetTimeoutUsec(0);
+      options.SetTimeout(llvm::None);
 
     ExpressionResults success = target->EvaluateExpression(
         expr, frame, result_valobj_sp, options, &m_fixed_expression);
index f143f5f..baa9f41 100644 (file)
@@ -1100,7 +1100,7 @@ protected:
     options.SetUnwindOnError(true);
     options.SetKeepInMemory(false);
     options.SetTryAllThreads(true);
-    options.SetTimeoutUsec(0);
+    options.SetTimeout(llvm::None);
 
     ExpressionResults expr_result =
         target->EvaluateExpression(expr, frame, valobj_sp, options);
index 7c3cd7e..e404537 100644 (file)
@@ -296,9 +296,9 @@ void REPL::IOHandlerInputComplete(IOHandler &io_handler, std::string &code) {
       expr_options.SetPoundLine(m_repl_source_path.c_str(),
                                 m_code.GetSize() + 1);
       if (m_command_options.timeout > 0)
-        expr_options.SetTimeoutUsec(m_command_options.timeout);
+        expr_options.SetTimeout(std::chrono::microseconds(m_command_options.timeout));
       else
-        expr_options.SetTimeoutUsec(0);
+        expr_options.SetTimeout(llvm::None);
 
       expr_options.SetLanguage(GetLanguage());
 
index 3665151..bb23d98 100644 (file)
@@ -1418,7 +1418,7 @@ Error CommandInterpreter::PreprocessCommand(std::string &command) {
           options.SetIgnoreBreakpoints(true);
           options.SetKeepInMemory(false);
           options.SetTryAllThreads(true);
-          options.SetTimeoutUsec(0);
+          options.SetTimeout(llvm::None);
 
           ExpressionResults expr_result = target->EvaluateExpression(
               expr_str.c_str(), exe_ctx.GetFramePtr(), expr_result_valobj_sp,
index 2f82328..db626b0 100644 (file)
@@ -72,7 +72,7 @@ bool AddressSanitizerRuntime::CheckIfRuntimeIsValid(
   return symbol != nullptr;
 }
 
-#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2 * 1000 * 1000
+static constexpr std::chrono::seconds g_retrieve_report_data_function_timeout(2);
 const char *address_sanitizer_retrieve_report_data_prefix = R"(
 extern "C"
 {
@@ -127,7 +127,7 @@ StructuredData::ObjectSP AddressSanitizerRuntime::RetrieveReportData() {
   options.SetTryAllThreads(true);
   options.SetStopOthers(true);
   options.SetIgnoreBreakpoints(true);
-  options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC);
+  options.SetTimeout(g_retrieve_report_data_function_timeout);
   options.SetPrefix(address_sanitizer_retrieve_report_data_prefix);
   options.SetAutoApplyFixIts(false);
   options.SetLanguage(eLanguageTypeObjC_plus_plus);
index 84c8b64..3010724 100644 (file)
@@ -59,7 +59,7 @@ lldb::InstrumentationRuntimeType ThreadSanitizerRuntime::GetTypeStatic() {
 
 ThreadSanitizerRuntime::~ThreadSanitizerRuntime() { Deactivate(); }
 
-#define RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC 2 * 1000 * 1000
+static constexpr std::chrono::seconds g_retrieve_data_function_timeout(2);
 
 const char *thread_sanitizer_retrieve_report_data_prefix = R"(
 extern "C"
@@ -308,7 +308,7 @@ ThreadSanitizerRuntime::RetrieveReportData(ExecutionContextRef exe_ctx_ref) {
   options.SetTryAllThreads(true);
   options.SetStopOthers(true);
   options.SetIgnoreBreakpoints(true);
-  options.SetTimeoutUsec(RETRIEVE_REPORT_DATA_FUNCTION_TIMEOUT_USEC);
+  options.SetTimeout(g_retrieve_data_function_timeout);
   options.SetPrefix(thread_sanitizer_retrieve_report_data_prefix);
   options.SetAutoApplyFixIts(false);
   options.SetLanguage(eLanguageTypeObjC_plus_plus);
index 24eddb5..4a90f5c 100644 (file)
@@ -40,7 +40,7 @@
 using namespace lldb;
 using namespace lldb_private;
 
-#define PO_FUNCTION_TIMEOUT_USEC 15 * 1000 * 1000
+static constexpr std::chrono::seconds g_po_function_timeout(15);
 
 AppleObjCRuntime::~AppleObjCRuntime() {}
 
@@ -169,7 +169,7 @@ bool AppleObjCRuntime::GetObjectDescription(Stream &strm, Value &value,
   options.SetTryAllThreads(true);
   options.SetStopOthers(true);
   options.SetIgnoreBreakpoints(true);
-  options.SetTimeoutUsec(PO_FUNCTION_TIMEOUT_USEC);
+  options.SetTimeout(g_po_function_timeout);
 
   ExpressionResults results = m_print_object_caller_up->ExecuteFunction(
       exe_ctx, &wrapper_struct_addr, options, diagnostics, ret);
index 47bf4ac..e9958a5 100644 (file)
@@ -66,7 +66,7 @@ using namespace lldb;
 using namespace lldb_private;
 
 // 2 second timeout when running utility functions
-#define UTILITY_FUNCTION_TIMEOUT_USEC 2 * 1000 * 1000
+static constexpr std::chrono::seconds g_utility_function_timeout(2);
 
 static const char *g_get_dynamic_class_info_name =
     "__lldb_apple_objc_v2_get_dynamic_class_info";
@@ -1411,7 +1411,7 @@ AppleObjCRuntimeV2::UpdateISAToDescriptorMapDynamic(
     options.SetTryAllThreads(false);
     options.SetStopOthers(true);
     options.SetIgnoreBreakpoints(true);
-    options.SetTimeoutUsec(UTILITY_FUNCTION_TIMEOUT_USEC);
+    options.SetTimeout(g_utility_function_timeout);
 
     Value return_value;
     return_value.SetValueType(Value::eValueTypeScalar);
@@ -1656,7 +1656,7 @@ AppleObjCRuntimeV2::UpdateISAToDescriptorMapSharedCache() {
     options.SetTryAllThreads(false);
     options.SetStopOthers(true);
     options.SetIgnoreBreakpoints(true);
-    options.SetTimeoutUsec(UTILITY_FUNCTION_TIMEOUT_USEC);
+    options.SetTimeout(g_utility_function_timeout);
 
     Value return_value;
     return_value.SetValueType(Value::eValueTypeScalar);
index 620e839..35247ed 100644 (file)
@@ -149,7 +149,7 @@ static void CreateHistoryThreadFromValueObject(ProcessSP process_sp,
   result.push_back(new_thread_sp);
 }
 
-#define GET_STACK_FUNCTION_TIMEOUT_USEC 2 * 1000 * 1000
+static constexpr std::chrono::seconds g_get_stack_function_timeout(2);
 
 HistoryThreads MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address) {
   HistoryThreads result;
@@ -178,7 +178,7 @@ HistoryThreads MemoryHistoryASan::GetHistoryThreads(lldb::addr_t address) {
   options.SetTryAllThreads(true);
   options.SetStopOthers(true);
   options.SetIgnoreBreakpoints(true);
-  options.SetTimeoutUsec(GET_STACK_FUNCTION_TIMEOUT_USEC);
+  options.SetTimeout(g_get_stack_function_timeout);
   options.SetPrefix(memory_history_asan_command_prefix);
   options.SetAutoApplyFixIts(false);
   options.SetLanguage(eLanguageTypeObjC_plus_plus);
index 676e330..e51029c 100644 (file)
@@ -734,7 +734,7 @@ Error PlatformPOSIX::EvaluateLibdlExpression(
   expr_options.SetLanguage(eLanguageTypeC_plus_plus);
   expr_options.SetTrapExceptions(false); // dlopen can't throw exceptions, so
                                          // don't do the work to trap them.
-  expr_options.SetTimeoutUsec(2000000);  // 2 seconds
+  expr_options.SetTimeout(std::chrono::seconds(2));
 
   Error expr_error;
   ExpressionResults result =
index e10e9a2..4e1f10c 100644 (file)
@@ -61,7 +61,7 @@ bool lldb_private::InferiorCallMmap(Process *process, addr_t &allocated_addr,
       options.SetIgnoreBreakpoints(true);
       options.SetTryAllThreads(true);
       options.SetDebug(false);
-      options.SetTimeoutUsec(500000);
+      options.SetTimeout(std::chrono::milliseconds(500));
       options.SetTrapExceptions(false);
 
       addr_t prot_arg, flags_arg = 0;
@@ -151,7 +151,7 @@ bool lldb_private::InferiorCallMunmap(Process *process, addr_t addr,
       options.SetIgnoreBreakpoints(true);
       options.SetTryAllThreads(true);
       options.SetDebug(false);
-      options.SetTimeoutUsec(500000);
+      options.SetTimeout(std::chrono::milliseconds(500));
       options.SetTrapExceptions(false);
 
       AddressRange munmap_range;
@@ -200,7 +200,7 @@ bool lldb_private::InferiorCall(Process *process, const Address *address,
   options.SetIgnoreBreakpoints(true);
   options.SetTryAllThreads(true);
   options.SetDebug(false);
-  options.SetTimeoutUsec(500000);
+  options.SetTimeout(std::chrono::milliseconds(500));
   options.SetTrapExceptions(trap_exceptions);
 
   ClangASTContext *clang_ast_context =
index 9d0f0da..dcc532d 100644 (file)
@@ -340,7 +340,7 @@ AppleGetItemInfoHandler::GetItemInfo(Thread &thread, uint64_t item,
   options.SetUnwindOnError(true);
   options.SetIgnoreBreakpoints(true);
   options.SetStopOthers(true);
-  options.SetTimeoutUsec(500000);
+  options.SetTimeout(std::chrono::milliseconds(500));
   options.SetTryAllThreads(false);
   thread.CalculateExecutionContext(exe_ctx);
 
index 38bf336..fc91ba4 100644 (file)
@@ -351,7 +351,7 @@ AppleGetPendingItemsHandler::GetPendingItems(Thread &thread, addr_t queue,
   options.SetUnwindOnError(true);
   options.SetIgnoreBreakpoints(true);
   options.SetStopOthers(true);
-  options.SetTimeoutUsec(500000);
+  options.SetTimeout(std::chrono::milliseconds(500));
   options.SetTryAllThreads(false);
   thread.CalculateExecutionContext(exe_ctx);
 
index 3994c28..ddc56d8 100644 (file)
@@ -357,7 +357,7 @@ AppleGetQueuesHandler::GetCurrentQueues(Thread &thread, addr_t page_to_free,
   options.SetUnwindOnError(true);
   options.SetIgnoreBreakpoints(true);
   options.SetStopOthers(true);
-  options.SetTimeoutUsec(500000);
+  options.SetTimeout(std::chrono::milliseconds(500));
   options.SetTryAllThreads(false);
   thread.CalculateExecutionContext(exe_ctx);
 
index 4ed43e5..c05523e 100644 (file)
@@ -355,7 +355,7 @@ AppleGetThreadItemInfoHandler::GetThreadItemInfo(Thread &thread,
   options.SetUnwindOnError(true);
   options.SetIgnoreBreakpoints(true);
   options.SetStopOthers(true);
-  options.SetTimeoutUsec(500000);
+  options.SetTimeout(std::chrono::milliseconds(500));
   options.SetTryAllThreads(false);
   thread.CalculateExecutionContext(exe_ctx);
 
index 7031267..76b95f2 100644 (file)
@@ -70,18 +70,8 @@ using namespace lldb;
 using namespace lldb_private;
 using namespace std::chrono;
 
-// A temporary function to convert between old representations of timeouts (0
-// means infinite wait) and new Timeout class (0 means "poll").
-// TODO(labath): Fix up all callers and remove this.
-static Timeout<std::micro> ConvertTimeout(std::chrono::microseconds t) {
-  if (t == std::chrono::microseconds(0))
-    return llvm::None;
-  return t;
-}
-
 // Comment out line below to disable memory caching, overriding the process
-// setting
-// target.process.disable-memory-cache
+// setting target.process.disable-memory-cache
 #define ENABLE_MEMORY_CACHING
 
 #ifdef ENABLE_MEMORY_CACHING
@@ -4805,20 +4795,19 @@ GetOneThreadExpressionTimeout(const EvaluateExpressionOptions &options) {
   const milliseconds default_one_thread_timeout(250);
 
   // If the overall wait is forever, then we don't need to worry about it.
-  if (options.GetTimeoutUsec() == 0) {
-    if (options.GetOneThreadTimeoutUsec() != 0)
-      return microseconds(options.GetOneThreadTimeoutUsec());
-    return default_one_thread_timeout;
+  if (!options.GetTimeout()) {
+    return options.GetOneThreadTimeout() ? *options.GetOneThreadTimeout()
+                                         : default_one_thread_timeout;
   }
 
   // If the one thread timeout is set, use it.
-  if (options.GetOneThreadTimeoutUsec() != 0)
-    return microseconds(options.GetOneThreadTimeoutUsec());
+  if (options.GetOneThreadTimeout())
+    return *options.GetOneThreadTimeout();
 
   // Otherwise use half the total timeout, bounded by the
   // default_one_thread_timeout.
   return std::min<microseconds>(default_one_thread_timeout,
-                                microseconds(options.GetTimeoutUsec()) / 2);
+                                *options.GetTimeout() / 2);
 }
 
 static Timeout<std::micro>
@@ -4827,16 +4816,15 @@ GetExpressionTimeout(const EvaluateExpressionOptions &options,
   // If we are going to run all threads the whole time, or if we are only
   // going to run one thread, we can just return the overall timeout.
   if (!options.GetStopOthers() || !options.GetTryAllThreads())
-    return ConvertTimeout(microseconds(options.GetTimeoutUsec()));
+    return options.GetTimeout();
 
   if (before_first_timeout)
     return GetOneThreadExpressionTimeout(options);
 
-  if (options.GetTimeoutUsec() == 0)
+  if (!options.GetTimeout())
     return llvm::None;
   else
-    return microseconds(options.GetTimeoutUsec()) -
-           GetOneThreadExpressionTimeout(options);
+    return *options.GetTimeout() - GetOneThreadExpressionTimeout(options);
 }
 
 ExpressionResults
@@ -4921,8 +4909,8 @@ Process::RunThreadPlan(ExecutionContext &exe_ctx,
 
   // Make sure the timeout values make sense. The one thread timeout needs to be
   // smaller than the overall timeout.
-  if (options.GetOneThreadTimeoutUsec() != 0 && options.GetTimeoutUsec() != 0 &&
-      options.GetTimeoutUsec() < options.GetOneThreadTimeoutUsec()) {
+  if (options.GetOneThreadTimeout() && options.GetTimeout() &&
+      *options.GetTimeout() < *options.GetOneThreadTimeout()) {
     diagnostic_manager.PutString(eDiagnosticSeverityError,
                                  "RunThreadPlan called with one thread "
                                  "timeout greater than total timeout");
index 8f50124..160f831 100644 (file)
@@ -62,6 +62,8 @@
 using namespace lldb;
 using namespace lldb_private;
 
+constexpr std::chrono::milliseconds EvaluateExpressionOptions::default_timeout;
+
 ConstString &Target::GetStaticBroadcasterClass() {
   static ConstString class_name("lldb.target");
   return class_name;