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

2017-01-23 Thread Greg Clayton via Phabricator via lldb-commits
clayborg accepted this revision.
clayborg added a comment.
This revision is now accepted and ready to land.

Looks good.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[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 ) {
   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 ) {
   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 ) {
   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 ) {
   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;
   }
 
+  // 

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

2017-01-20 Thread Greg Clayton via Phabricator via lldb-commits
clayborg requested changes to this revision.
clayborg added a comment.
This revision now requires changes to proceed.

See easy inline fix and this will be good to go




Comment at: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp:4004
+// hope that data being written is small enough to fit.
+Log *log(ProcessGDBRemoteLog::GetLogIfAllCategoriesSet(
+GDBR_LOG_COMM | GDBR_LOG_MEMORY));

LogIfAny, not LogIfAll. Then either bit will work to show this.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


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

2017-01-20 Thread Hafiz Abid Qadeer via Phabricator via lldb-commits
abidh marked an inline comment as done.
abidh added a comment.

Greg, any further comment on this patch.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


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

2017-01-18 Thread Hafiz Abid Qadeer via Phabricator via lldb-commits
abidh marked 2 inline comments as done.
abidh added inline comments.



Comment at: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp:4006-4007
+if (log)
+  log->Warning("Packet size is too small."
+   "LLDB may face problems while writing memory");
+  }

clayborg wrote:
> Missing space between "small." and "LLDB". We should also warn once, not 
> every time.
This function has a check at the top so the packet size is checked only once.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


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

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

Updated log calls as advised.


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 ) {
   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 ) {
   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::GetLogIfAllCategoriesSet(
+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 ) {
   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 ) {
   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 

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

2017-01-18 Thread Greg Clayton via Phabricator via lldb-commits
clayborg requested changes to this revision.
clayborg added a comment.
This revision now requires changes to proceed.

A few cleanups on the logging. See inlined comments.




Comment at: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp:4004
+// hope that data being written is small enough to fit.
+Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
+if (log)

We should use a GDB remote log channel instead of the LLDB channel. Since this 
is communication based I suggest:

```
  Log *log(ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_COMM| 
GDBR_LOG_MEMORY));
```




Comment at: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp:4006-4007
+if (log)
+  log->Warning("Packet size is too small."
+   "LLDB may face problems while writing memory");
+  }

Missing space between "small." and "LLDB". We should also warn once, not every 
time.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


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

2017-01-18 Thread Hafiz Abid Qadeer via Phabricator via lldb-commits
abidh added inline comments.



Comment at: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp:3999
+  // time, we take a maximum guess here.
+  stub_max_size -= 32 + 32 + 6;
   m_max_memory_size = stub_max_size;

clayborg wrote:
> You need to check "sub_max_size" here. What if it says 64? We will then use a 
> very large unsigned number UINT64_MAX - 6.
Thanks for review. I had a check in my development branch then removed it as 
the scenario seemed unlikely and there was not a good way to return error from 
this function. I was inclined to put this check in DoMemoryWrite/read and check 
the exact size every time but it looks overkill. The check added in this 
revision probably should be enough.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


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

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

Added the check to avoid integer underflow.


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 ) {
   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 ) {
   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,20 @@
 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(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_PROCESS));
+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 ) {
   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 ) {
   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,20 @@
 stub_max_size = reasonable_largeish_default;
   }
 
+  // Memory packet have other overheads too like 

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

2017-01-17 Thread Greg Clayton via Phabricator via lldb-commits
clayborg requested changes to this revision.
clayborg added a comment.
This revision now requires changes to proceed.

We need to check stub_max_size to make sure we don't get an integer underflow.




Comment at: source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp:3999
+  // time, we take a maximum guess here.
+  stub_max_size -= 32 + 32 + 6;
   m_max_memory_size = stub_max_size;

You need to check "sub_max_size" here. What if it says 64? We will then use a 
very large unsigned number UINT64_MAX - 6.


https://reviews.llvm.org/D28808



___
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


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

2017-01-17 Thread Hafiz Abid Qadeer via Phabricator via lldb-commits
abidh created this revision.

LLDB was using packet size advertised by the target as the max memory size to 
write in one go. It is wrong because packets have other overhead apart from 
memory payload. Also memory transferred through 'm' and 'M' packets needs 2 
bytes in packet to transfer 1 of memory.


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 ) {
   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 ) {
   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,10 @@
 stub_max_size = reasonable_largeish_default;
   }
 
+  // Memory packets 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.
+  stub_max_size -= 32 + 32 + 6;
   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 ) {
   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 ) {
   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,10 @@
 stub_max_size = reasonable_largeish_default;
   }
 
+  // Memory packets 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.
+  stub_max_size -= 32 + 32 + 6;
   m_max_memory_size = stub_max_size;
 }