[llvm-branch-commits] [lldb] 7832d7e - [lldb] Modernize TargetList for-loops, NFC

2020-12-12 Thread Tatyana Krasnukha via llvm-branch-commits

Author: Tatyana Krasnukha
Date: 2020-12-12T16:40:58+03:00
New Revision: 7832d7e95ace589b2275bafe701ccb377a16b1b2

URL: 
https://github.com/llvm/llvm-project/commit/7832d7e95ace589b2275bafe701ccb377a16b1b2
DIFF: 
https://github.com/llvm/llvm-project/commit/7832d7e95ace589b2275bafe701ccb377a16b1b2.diff

LOG: [lldb] Modernize TargetList for-loops, NFC

Replace loops with standard algorithms or range-based loops.

Added: 


Modified: 
lldb/source/Target/TargetList.cpp

Removed: 




diff  --git a/lldb/source/Target/TargetList.cpp 
b/lldb/source/Target/TargetList.cpp
index bbef3b63ba42..5bb6ca2a73e9 100644
--- a/lldb/source/Target/TargetList.cpp
+++ b/lldb/source/Target/TargetList.cpp
@@ -405,80 +405,76 @@ Status TargetList::CreateTargetInternal(Debugger 
&debugger,
 
 bool TargetList::DeleteTarget(TargetSP &target_sp) {
   std::lock_guard guard(m_target_list_mutex);
-  collection::iterator pos, end = m_target_list.end();
+  auto it = std::find(m_target_list.begin(), m_target_list.end(), target_sp);
+  if (it == m_target_list.end())
+return false;
 
-  for (pos = m_target_list.begin(); pos != end; ++pos) {
-if (pos->get() == target_sp.get()) {
-  m_target_list.erase(pos);
-  return true;
-}
-  }
-  return false;
+  m_target_list.erase(it);
+  return true;
 }
 
 TargetSP TargetList::FindTargetWithExecutableAndArchitecture(
 const FileSpec &exe_file_spec, const ArchSpec *exe_arch_ptr) const {
   std::lock_guard guard(m_target_list_mutex);
-  TargetSP target_sp;
-  collection::const_iterator pos, end = m_target_list.end();
-  for (pos = m_target_list.begin(); pos != end; ++pos) {
-Module *exe_module = (*pos)->GetExecutableModulePointer();
-
-if (exe_module) {
-  if (FileSpec::Match(exe_file_spec, exe_module->GetFileSpec())) {
-if (exe_arch_ptr) {
-  if (!exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture()))
-continue;
-}
-target_sp = *pos;
-break;
-  }
-}
-  }
-  return target_sp;
+  auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
+  [&exe_file_spec, exe_arch_ptr](const TargetSP &item) {
+Module *exe_module = item->GetExecutableModulePointer();
+if (!exe_module ||
+!FileSpec::Match(exe_file_spec, exe_module->GetFileSpec()))
+  return false;
+
+return !exe_arch_ptr ||
+   exe_arch_ptr->IsCompatibleMatch(exe_module->GetArchitecture());
+  });
+
+  if (it != m_target_list.end())
+return *it;
+
+  return TargetSP();
 }
 
 TargetSP TargetList::FindTargetWithProcessID(lldb::pid_t pid) const {
   std::lock_guard guard(m_target_list_mutex);
-  TargetSP target_sp;
-  collection::const_iterator pos, end = m_target_list.end();
-  for (pos = m_target_list.begin(); pos != end; ++pos) {
-Process *process = (*pos)->GetProcessSP().get();
-if (process && process->GetID() == pid) {
-  target_sp = *pos;
-  break;
-}
-  }
-  return target_sp;
+  auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
+  [pid](const TargetSP &item) {
+auto *process_ptr = item->GetProcessSP().get();
+return process_ptr && (process_ptr->GetID() == pid);
+  });
+
+  if (it != m_target_list.end())
+return *it;
+
+  return TargetSP();
 }
 
 TargetSP TargetList::FindTargetWithProcess(Process *process) const {
   TargetSP target_sp;
-  if (process) {
-std::lock_guard guard(m_target_list_mutex);
-collection::const_iterator pos, end = m_target_list.end();
-for (pos = m_target_list.begin(); pos != end; ++pos) {
-  if (process == (*pos)->GetProcessSP().get()) {
-target_sp = *pos;
-break;
-  }
-}
-  }
+  if (!process)
+return target_sp;
+
+  std::lock_guard guard(m_target_list_mutex);
+  auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
+  [process](const TargetSP &item) {
+return item->GetProcessSP().get() == process;
+  });
+
+  if (it != m_target_list.end())
+target_sp = *it;
+
   return target_sp;
 }
 
 TargetSP TargetList::GetTargetSP(Target *target) const {
   TargetSP target_sp;
-  if (target) {
-std::lock_guard guard(m_target_list_mutex);
-collection::const_iterator pos, end = m_target_list.end();
-for (pos = m_target_list.begin(); pos != end; ++pos) {
-  if (target == (*pos).get()) {
-target_sp = *pos;
-break;
-  }
-}
-  }
+  if (!target)
+return target_sp;
+
+  std::lock_guard guard(m_target_list_mutex);
+  auto it = std::find_if(m_target_list.begin(), m_target_list.end(),
+  [target](const TargetSP &item) { return item.get() == target; });
+  if (it != m_target_list.end())
+target_sp = *it;
+
   return target_sp;
 }
 
@@ -509,14 +505,11 @@ uint32_t TargetList::SignalIfRunning(lldb::pid_t pid, int 
signo) {
   if (pid == LLDB_INVALID_PROCESS_ID) {
 // Signal all processes with sig

[llvm-branch-commits] [lldb] 2634ec6 - [lldb] "target create" shouldn't save target if the command failed

2020-12-12 Thread Tatyana Krasnukha via llvm-branch-commits

Author: Tatyana Krasnukha
Date: 2020-12-12T16:40:58+03:00
New Revision: 2634ec6ce9007f2406545ca28b4c72961f1e8f67

URL: 
https://github.com/llvm/llvm-project/commit/2634ec6ce9007f2406545ca28b4c72961f1e8f67
DIFF: 
https://github.com/llvm/llvm-project/commit/2634ec6ce9007f2406545ca28b4c72961f1e8f67.diff

LOG: [lldb] "target create" shouldn't save target if the command failed

TargetList::CreateTarget automatically adds created target to the list, however,
CommandObjectTargetCreate does some additional preparation after creating a 
target
and which can fail. The command should remove created target if it failed. Since
the function has many ways to return, scope guard does this work safely.

Changes to the TargetList make target adding and selection more transparent.

Other changes remove unnecessary SetSelectedTarget after CreateTarget.

Differential Revision: https://reviews.llvm.org/D93052

Added: 


Modified: 
lldb/include/lldb/Target/TargetList.h
lldb/source/API/SBDebugger.cpp
lldb/source/Commands/CommandObjectProcess.cpp
lldb/source/Commands/CommandObjectTarget.cpp
lldb/source/Plugins/Platform/POSIX/PlatformPOSIX.cpp
lldb/source/Plugins/Platform/Windows/PlatformWindows.cpp
lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.cpp
lldb/source/Target/Platform.cpp
lldb/source/Target/TargetList.cpp
lldb/source/Target/TraceSessionFileParser.cpp
lldb/unittests/Process/ProcessEventDataTest.cpp
lldb/unittests/Thread/ThreadTest.cpp

Removed: 




diff  --git a/lldb/include/lldb/Target/TargetList.h 
b/lldb/include/lldb/Target/TargetList.h
index 94b25f65863a..903ca4bcefbc 100644
--- a/lldb/include/lldb/Target/TargetList.h
+++ b/lldb/include/lldb/Target/TargetList.h
@@ -173,7 +173,9 @@ class TargetList : public Broadcaster {
 
   uint32_t SignalIfRunning(lldb::pid_t pid, int signo);
 
-  uint32_t SetSelectedTarget(Target *target);
+  void SetSelectedTarget(uint32_t index);
+
+  void SetSelectedTarget(const lldb::TargetSP &target);
 
   lldb::TargetSP GetSelectedTarget();
 
@@ -185,17 +187,21 @@ class TargetList : public Broadcaster {
   uint32_t m_selected_target_idx;
 
 private:
-  Status CreateTargetInternal(Debugger &debugger, llvm::StringRef 
user_exe_path,
-  llvm::StringRef triple_str,
-  LoadDependentFiles load_dependent_files,
-  const OptionGroupPlatform *platform_options,
-  lldb::TargetSP &target_sp);
-
-  Status CreateTargetInternal(Debugger &debugger, llvm::StringRef 
user_exe_path,
-  const ArchSpec &arch,
-  LoadDependentFiles get_dependent_modules,
-  lldb::PlatformSP &platform_sp,
-  lldb::TargetSP &target_sp);
+  static Status CreateTargetInternal(
+  Debugger &debugger, llvm::StringRef user_exe_path,
+  llvm::StringRef triple_str, LoadDependentFiles load_dependent_files,
+  const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp);
+
+  static Status CreateTargetInternal(Debugger &debugger,
+ llvm::StringRef user_exe_path,
+ const ArchSpec &arch,
+ LoadDependentFiles get_dependent_modules,
+ lldb::PlatformSP &platform_sp,
+ lldb::TargetSP &target_sp);
+
+  void AddTargetInternal(lldb::TargetSP target_sp, bool do_select);
+
+  void SetSelectedTargetInternal(uint32_t index);
 
   TargetList(const TargetList &) = delete;
   const TargetList &operator=(const TargetList &) = delete;

diff  --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp
index a5bf457e90f9..dc1cc91c705c 100644
--- a/lldb/source/API/SBDebugger.cpp
+++ b/lldb/source/API/SBDebugger.cpp
@@ -811,10 +811,8 @@ SBTarget SBDebugger::CreateTargetWithFileAndArch(const 
char *filename,
 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, 
nullptr,
 target_sp);
 
-if (error.Success()) {
-  m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
+if (error.Success())
   sb_target.SetSP(target_sp);
-}
   }
 
   LLDB_LOGF(log,
@@ -840,10 +838,8 @@ SBTarget SBDebugger::CreateTarget(const char *filename) {
 add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, 
nullptr,
 target_sp);
 
-if (error.Success()) {
-  m_opaque_sp->GetTargetList().SetSelectedTarget(target_sp.get());
+if (error.Success())
   sb_target.SetSP(target_sp);
-}
   }
   Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_API));
   LLDB_LOGF(log,
@@ -998,7 +994,7 @@ void SBDebugger::SetSelectedTarget(SBTarget &sb_target) {
 
   TargetSP target_sp(sb_target.GetSP());
   if (m_opaque_sp) {
-m_opaque_sp

[llvm-branch-commits] [lldb] a01b26f - [lldb] Make CommandInterpreter's execution context the same as debugger's one.

2020-12-12 Thread Tatyana Krasnukha via llvm-branch-commits

Author: Tatyana Krasnukha
Date: 2020-12-12T16:40:59+03:00
New Revision: a01b26fb51c710a3a8ef88cc83b0701461f5b9ab

URL: 
https://github.com/llvm/llvm-project/commit/a01b26fb51c710a3a8ef88cc83b0701461f5b9ab
DIFF: 
https://github.com/llvm/llvm-project/commit/a01b26fb51c710a3a8ef88cc83b0701461f5b9ab.diff

LOG: [lldb] Make CommandInterpreter's execution context the same as debugger's 
one.

Currently, the interpreter's context is not updated until a command is executed.
This has resulted in the behavior of SB-interface functions and some commands
depends on previous user actions. The interpreter's context can stay 
uninitialized,
point to a currently selected target, or point to one of previously selected 
targets.

This patch removes any usages of CommandInterpreter::UpdateExecutionContext.
CommandInterpreter::HandleCommand* functions still may override context 
temporarily,
but now they always restore it before exiting. CommandInterpreter saves 
overriden
contexts to the stack, that makes nesting commands possible.

Added test reproduces one of the issues. Without this fix, the last assertion 
fails
because interpreter's execution context is empty until running "target list", 
so,
the value of the global property was updated instead of process's local 
instance.

Differential Revision: https://reviews.llvm.org/D92164

Added: 
lldb/test/API/python_api/debugger/Makefile
lldb/test/API/python_api/debugger/main.cpp

Modified: 
lldb/include/lldb/Interpreter/CommandInterpreter.h
lldb/source/API/SBCommandInterpreter.cpp
lldb/source/Breakpoint/BreakpointOptions.cpp
lldb/source/Commands/CommandObjectCommands.cpp
lldb/source/Commands/CommandObjectExpression.cpp
lldb/source/Commands/CommandObjectProcess.cpp
lldb/source/Commands/CommandObjectRegexCommand.cpp
lldb/source/Commands/CommandObjectSettings.cpp
lldb/source/Commands/CommandObjectWatchpointCommand.cpp
lldb/source/Core/IOHandlerCursesGUI.cpp
lldb/source/Interpreter/CommandInterpreter.cpp
lldb/source/Target/Target.cpp
lldb/test/API/python_api/debugger/TestDebuggerAPI.py

Removed: 




diff  --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h 
b/lldb/include/lldb/Interpreter/CommandInterpreter.h
index d35f7e22b9ea..40b649411f7f 100644
--- a/lldb/include/lldb/Interpreter/CommandInterpreter.h
+++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h
@@ -24,7 +24,9 @@
 #include "lldb/Utility/StringList.h"
 #include "lldb/lldb-forward.h"
 #include "lldb/lldb-private.h"
+
 #include 
+#include 
 
 namespace lldb_private {
 class CommandInterpreter;
@@ -245,7 +247,7 @@ class CommandInterpreter : public Broadcaster,
 
   CommandInterpreter(Debugger &debugger, bool synchronous_execution);
 
-  ~CommandInterpreter() override;
+  ~CommandInterpreter() override = default;
 
   // These two functions fill out the Broadcaster interface:
 
@@ -300,10 +302,11 @@ class CommandInterpreter : public Broadcaster,
   CommandReturnObject &result);
 
   bool HandleCommand(const char *command_line, LazyBool add_to_history,
- CommandReturnObject &result,
- ExecutionContext *override_context = nullptr,
- bool repeat_on_empty_command = true,
- bool no_context_switching = false);
+ const ExecutionContext &override_context,
+ CommandReturnObject &result);
+
+  bool HandleCommand(const char *command_line, LazyBool add_to_history,
+ CommandReturnObject &result);
 
   bool WasInterrupted() const;
 
@@ -312,9 +315,7 @@ class CommandInterpreter : public Broadcaster,
   /// \param[in] commands
   ///The list of commands to execute.
   /// \param[in,out] context
-  ///The execution context in which to run the commands. Can be nullptr in
-  ///which case the default
-  ///context will be used.
+  ///The execution context in which to run the commands.
   /// \param[in] options
   ///This object holds the options used to control when to stop, whether to
   ///execute commands,
@@ -324,8 +325,13 @@ class CommandInterpreter : public Broadcaster,
   ///safely,
   ///and failed with some explanation if we aborted executing the commands
   ///at some point.
-  void HandleCommands(const StringList &commands, ExecutionContext *context,
-  CommandInterpreterRunOptions &options,
+  void HandleCommands(const StringList &commands,
+  const ExecutionContext &context,
+  const CommandInterpreterRunOptions &options,
+  CommandReturnObject &result);
+
+  void HandleCommands(const StringList &commands,
+  const CommandInterpreterRunOptions &options,
   CommandReturnObject &result);
 
   /// Execute a list of commands from a file.
@@ -333,9 +339,7 @