[Lldb-commits] [PATCH] D28944: Provide option to set pc of the file loaded in memory.

2017-01-22 Thread Hafiz Abid Qadeer via Phabricator via lldb-commits
abidh updated this revision to Diff 85278.
abidh added a comment.

Added a check for case when entry address is not valid.


https://reviews.llvm.org/D28944

Files:
  include/lldb/Core/Module.h
  include/lldb/Symbol/ObjectFile.h
  source/Commands/CommandObjectTarget.cpp
  source/Core/Module.cpp
  source/Symbol/ObjectFile.cpp

Index: source/Symbol/ObjectFile.cpp
===
--- source/Symbol/ObjectFile.cpp
+++ source/Symbol/ObjectFile.cpp
@@ -652,11 +652,13 @@
   return ConstString(ss.GetString());
 }
 
-Error ObjectFile::LoadInMemory(Target &target) {
+Error ObjectFile::LoadInMemory(Target &target, bool set_pc) {
   Error error;
   ProcessSP process = target.CalculateProcess();
   if (!process)
 return Error("No Process");
+  if (set_pc && !GetEntryPointAddress().IsValid())
+return Error("No entry address in object file");
 
   SectionList *section_list = GetSectionList();
   if (!section_list)
@@ -677,5 +679,12 @@
 return error;
 }
   }
+  if (set_pc) {
+ThreadList &thread_list = process->GetThreadList();
+ThreadSP curr_thread(thread_list.GetSelectedThread());
+RegisterContextSP reg_context(curr_thread->GetRegisterContext());
+Address file_entry = GetEntryPointAddress();
+reg_context->SetPC(file_entry.GetLoadAddress(&target));
+  }
   return error;
 }
Index: source/Core/Module.cpp
===
--- source/Core/Module.cpp
+++ source/Core/Module.cpp
@@ -1665,6 +1665,6 @@
   return false;
 }
 
-Error Module::LoadInMemory(Target &target) {
-  return m_objfile_sp->LoadInMemory(target);
+Error Module::LoadInMemory(Target &target, bool set_pc) {
+  return m_objfile_sp->LoadInMemory(target, set_pc);
 }
Index: source/Commands/CommandObjectTarget.cpp
===
--- source/Commands/CommandObjectTarget.cpp
+++ source/Commands/CommandObjectTarget.cpp
@@ -2568,16 +2568,20 @@
 m_file_option(LLDB_OPT_SET_1, false, "file", 'f', 0, eArgTypeName,
   "Fullpath or basename for module to load.", ""),
 m_load_option(LLDB_OPT_SET_1, false, "load", 'l',
-  "Write file contents to the memory.",
-  false, true),
+  "Write file contents to the memory.", false, true),
+m_pc_option(LLDB_OPT_SET_1, false, "--set-pc-to-entry", 'p',
+"Set PC to the entry point."
+" Only applicable with '--load' option.",
+false, true),
 m_slide_option(LLDB_OPT_SET_1, false, "slide", 's', 0, eArgTypeOffset,
"Set the load address for all sections to be the "
"virtual address in the file plus the offset.",
0) {
 m_option_group.Append(&m_uuid_option_group, LLDB_OPT_SET_ALL,
   LLDB_OPT_SET_1);
 m_option_group.Append(&m_file_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
 m_option_group.Append(&m_load_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
+m_option_group.Append(&m_pc_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
 m_option_group.Append(&m_slide_option, LLDB_OPT_SET_ALL, LLDB_OPT_SET_1);
 m_option_group.Finalize();
   }
@@ -2590,6 +2594,7 @@
   bool DoExecute(Args &args, CommandReturnObject &result) override {
 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
 const bool load = m_load_option.GetOptionValue().GetCurrentValue();
+const bool set_pc = m_pc_option.GetOptionValue().GetCurrentValue();
 if (target == nullptr) {
   result.AppendError("invalid target, create a debug target using the "
  "'target create' command");
@@ -2742,7 +2747,7 @@
 process->Flush();
 }
 if (load) {
-  Error error = module->LoadInMemory(*target);
+  Error error = module->LoadInMemory(*target, set_pc);
   if (error.Fail()) {
 result.AppendError(error.AsCString());
 return false;
@@ -2811,6 +2816,7 @@
   OptionGroupUUID m_uuid_option_group;
   OptionGroupString m_file_option;
   OptionGroupBoolean m_load_option;
+  OptionGroupBoolean m_pc_option;
   OptionGroupUInt64 m_slide_option;
 };
 
Index: include/lldb/Symbol/ObjectFile.h
===
--- include/lldb/Symbol/ObjectFile.h
+++ include/lldb/Symbol/ObjectFile.h
@@ -786,7 +786,7 @@
   ///
   /// @return
   //--
-  virtual Error LoadInMemory(Target &target);
+  virtual Error LoadInMemory(Target &target, bool set_pc);
 
 protected:
   //--
Index: include/lldb/Core/Module.h
===
--- include/lldb/Core/Modul

[Lldb-commits] [PATCH] D28808: Fix a bug where lldb does not respect the packet size.

2017-01-22 Thread Hafiz Abid Qadeer via Phabricator via lldb-commits
abidh updated this revision to Diff 85280.
abidh added a comment.

Use GetLogIfAnyCategoryIsSet as advised in comments.


https://reviews.llvm.org/D28808

Files:
  source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp


Index: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
===
--- source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -2729,16 +2729,19 @@
 size_t ProcessGDBRemote::DoReadMemory(addr_t addr, void *buf, size_t size,
   Error &error) {
   GetMaxMemorySize();
-  if (size > m_max_memory_size) {
+  bool binary_memory_read = m_gdb_comm.GetxPacketSupported();
+  // M and m packets take 2 bytes for 1 byte of memory
+  size_t max_memory_size =
+  binary_memory_read ? m_max_memory_size : m_max_memory_size / 2;
+  if (size > max_memory_size) {
 // Keep memory read sizes down to a sane limit. This function will be
 // called multiple times in order to complete the task by
 // lldb_private::Process so it is ok to do this.
-size = m_max_memory_size;
+size = max_memory_size;
   }
 
   char packet[64];
   int packet_len;
-  bool binary_memory_read = m_gdb_comm.GetxPacketSupported();
   packet_len = ::snprintf(packet, sizeof(packet), "%c%" PRIx64 ",%" PRIx64,
   binary_memory_read ? 'x' : 'm', (uint64_t)addr,
   (uint64_t)size);
@@ -2785,11 +2788,13 @@
 size_t ProcessGDBRemote::DoWriteMemory(addr_t addr, const void *buf,
size_t size, Error &error) {
   GetMaxMemorySize();
-  if (size > m_max_memory_size) {
+  // M and m packets take 2 bytes for 1 byte of memory
+  size_t max_memory_size = m_max_memory_size / 2;
+  if (size > max_memory_size) {
 // Keep memory read sizes down to a sane limit. This function will be
 // called multiple times in order to complete the task by
 // lldb_private::Process so it is ok to do this.
-size = m_max_memory_size;
+size = max_memory_size;
   }
 
   StreamString packet;
@@ -3988,6 +3993,21 @@
 stub_max_size = reasonable_largeish_default;
   }
 
+  // Memory packet have other overheads too like Maddr,size:#NN
+  // Instead of calculating the bytes taken by size and addr every
+  // time, we take a maximum guess here.
+  if (stub_max_size > 70)
+stub_max_size -= 32 + 32 + 6;
+  else {
+// In unlikely scenario that max packet size is less then 70, we will
+// hope that data being written is small enough to fit.
+Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(
+GDBR_LOG_COMM | GDBR_LOG_MEMORY));
+if (log)
+  log->Warning("Packet size is too small. "
+   "LLDB may face problems while writing memory");
+  }
+
   m_max_memory_size = stub_max_size;
 } else {
   m_max_memory_size = conservative_default;


Index: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
===
--- source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -2729,16 +2729,19 @@
 size_t ProcessGDBRemote::DoReadMemory(addr_t addr, void *buf, size_t size,
   Error &error) {
   GetMaxMemorySize();
-  if (size > m_max_memory_size) {
+  bool binary_memory_read = m_gdb_comm.GetxPacketSupported();
+  // M and m packets take 2 bytes for 1 byte of memory
+  size_t max_memory_size =
+  binary_memory_read ? m_max_memory_size : m_max_memory_size / 2;
+  if (size > max_memory_size) {
 // Keep memory read sizes down to a sane limit. This function will be
 // called multiple times in order to complete the task by
 // lldb_private::Process so it is ok to do this.
-size = m_max_memory_size;
+size = max_memory_size;
   }
 
   char packet[64];
   int packet_len;
-  bool binary_memory_read = m_gdb_comm.GetxPacketSupported();
   packet_len = ::snprintf(packet, sizeof(packet), "%c%" PRIx64 ",%" PRIx64,
   binary_memory_read ? 'x' : 'm', (uint64_t)addr,
   (uint64_t)size);
@@ -2785,11 +2788,13 @@
 size_t ProcessGDBRemote::DoWriteMemory(addr_t addr, const void *buf,
size_t size, Error &error) {
   GetMaxMemorySize();
-  if (size > m_max_memory_size) {
+  // M and m packets take 2 bytes for 1 byte of memory
+  size_t max_memory_size = m_max_memory_size / 2;
+  if (size > max_memory_size) {
 // Keep memory read sizes down to a sane limit. This function will be
 // called multiple times in order to complete the task by
 // lldb_private::Process so it is ok to do this.
-size = m_max_memory_size;
+size = max_memory_size;
   }
 
   StreamString packet;
@@ -3988,6 +3993,21 @@
 stub_max_size = reasonable_largeish_default