Always return a valid answer for qMemoryRegionInfo if the packet is supported.
We will return a valid range when possible and omit the "permissions" key
when the memory is not readable, writeable or executeable. This will help us
know the difference between an error back from this packet and unsupported,
from just "this address isn't in a valid region".

llvm-svn: 146394
diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp
index 5a9276d..c3c66f8 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp
+++ b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.cpp
@@ -52,7 +52,7 @@
     return count;
 }
 
-int
+nub_bool_t
 MachVMMemory::GetMemoryRegionInfo(task_t task, nub_addr_t address, DNBRegionInfo *region_info)
 {
     MachVMRegion vmRegion(task);
@@ -62,12 +62,30 @@
         region_info->addr = vmRegion.StartAddress();
         region_info->size = vmRegion.GetByteSize();
         region_info->permissions = vmRegion.GetDNBPermissions();
-        return 1;
     }
-    region_info->addr = 0;
-    region_info->size = 0;
-    region_info->permissions = 0;
-    return 0;
+    else
+    {
+        region_info->addr = address;
+        region_info->size = 0;
+        if (vmRegion.GetError().Success())
+        {
+            // vmRegion.GetRegionForAddress() return false, indicating that "address"
+            // wasn't in a valid region, but the "vmRegion" info was successfully 
+            // read from the task which means the info describes the next valid
+            // region from which we can infer the size of this invalid region
+            mach_vm_address_t start_addr = vmRegion.StartAddress();
+            if (address < start_addr)
+                region_info->size = start_addr - address;
+        }
+        // If we can't get any infor about the size from the next region, just fill
+        // 1 in as the byte size
+        if (region_info->size == 0)
+            region_info->size = 1;
+
+        // Not readable, writeable or executable
+        region_info->permissions = 0;
+    }
+    return true;
 }
 
 nub_size_t
diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h
index e32fa4f..d7162d0 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h
+++ b/lldb/tools/debugserver/source/MacOSX/MachVMMemory.h
@@ -27,7 +27,7 @@
     nub_size_t Read(task_t task, nub_addr_t address, void *data, nub_size_t data_count);
     nub_size_t Write(task_t task, nub_addr_t address, const void *data, nub_size_t data_count);
     nub_size_t PageSize();
-    int        GetMemoryRegionInfo(task_t task, nub_addr_t address, DNBRegionInfo *region_info);
+    nub_bool_t GetMemoryRegionInfo(task_t task, nub_addr_t address, DNBRegionInfo *region_info);
 
 protected:
     nub_size_t MaxBytesLeftInPage(nub_addr_t addr, nub_size_t count);
diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMRegion.cpp b/lldb/tools/debugserver/source/MacOSX/MachVMRegion.cpp
index e1667f0..3875759 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachVMRegion.cpp
+++ b/lldb/tools/debugserver/source/MacOSX/MachVMRegion.cpp
@@ -128,66 +128,59 @@
 {
     // Restore any original protections and clear our vars
     Clear();
+    m_err.Clear();
     m_addr = addr;
     m_start = addr;
     m_depth = 1024;
     mach_msg_type_number_t info_size = kRegionInfoSize;
     assert(sizeof(info_size) == 4);
     m_err = ::mach_vm_region_recurse (m_task, &m_start, &m_size, &m_depth, (vm_region_recurse_info_t)&m_data, &info_size);
-    const bool log_protections = DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS);
-    if (m_err.Success())
-    {
-        if ((addr < m_start) || (addr >= (m_start + m_size)))
-        {
-            m_err.SetErrorString("no region for address");
-            m_err.SetError(-1, DNBError::Generic);
-            if (log_protections)
-                m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => 0x%8.8llx, size => %llu, nesting_depth => %d, info => %p, infoCnt => %d) addr = 0x%8.8llx not in range [0x%8.8llx - 0x%8.8llx)", 
-                                  m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth, &m_data, info_size, (uint64_t)addr, (uint64_t)m_start, (uint64_t)m_start + m_size);
-            return false;
-        }
-    }
     
-    if (log_protections || m_err.Fail())
-        m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => 0x%8.8llx, size => %llu, nesting_depth => %d, info => %p, infoCnt => %d) addr = 0x%8.8llx ", m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth, &m_data, info_size, (uint64_t)addr);
-    if (m_err.Fail())
-    {
-        return false;
-    }
-    else
-    {
-        if (log_protections)
-        {
-            DNBLogThreaded("info = { prot = %u, "
-                             "max_prot = %u, "
-                             "inheritance = 0x%8.8x, "
-                             "offset = 0x%8.8llx, "
-                             "user_tag = 0x%8.8x, "
-                             "ref_count = %u, "
-                             "shadow_depth = %u, "
-                             "ext_pager = %u, "
-                             "share_mode = %u, "
-                             "is_submap = %d, "
-                             "behavior = %d, "
-                             "object_id = 0x%8.8x, "
-                             "user_wired_count = 0x%4.4x }",
-                             m_data.protection,
-                             m_data.max_protection,
-                             m_data.inheritance,
-                             (uint64_t)m_data.offset,
-                             m_data.user_tag,
-                             m_data.ref_count,
-                             m_data.shadow_depth,
-                             m_data.external_pager,
-                             m_data.share_mode,
-                             m_data.is_submap,
-                             m_data.behavior,
-                             m_data.object_id,
-                             m_data.user_wired_count);
-        }
-    }
+    const bool failed = m_err.Fail();
+    const bool log_protections = DNBLogCheckLogBit(LOG_MEMORY_PROTECTIONS);
 
+    if (log_protections || failed)
+        m_err.LogThreaded("::mach_vm_region_recurse ( task = 0x%4.4x, address => 0x%8.8llx, size => %llu, nesting_depth => %d, info => %p, infoCnt => %d) addr = 0x%8.8llx ", m_task, (uint64_t)m_start, (uint64_t)m_size, m_depth, &m_data, info_size, (uint64_t)addr);
+
+    if (failed)
+        return false;
+    if (log_protections)
+    {
+        DNBLogThreaded("info = { prot = %u, "
+                         "max_prot = %u, "
+                         "inheritance = 0x%8.8x, "
+                         "offset = 0x%8.8llx, "
+                         "user_tag = 0x%8.8x, "
+                         "ref_count = %u, "
+                         "shadow_depth = %u, "
+                         "ext_pager = %u, "
+                         "share_mode = %u, "
+                         "is_submap = %d, "
+                         "behavior = %d, "
+                         "object_id = 0x%8.8x, "
+                         "user_wired_count = 0x%4.4x }",
+                         m_data.protection,
+                         m_data.max_protection,
+                         m_data.inheritance,
+                         (uint64_t)m_data.offset,
+                         m_data.user_tag,
+                         m_data.ref_count,
+                         m_data.shadow_depth,
+                         m_data.external_pager,
+                         m_data.share_mode,
+                         m_data.is_submap,
+                         m_data.behavior,
+                         m_data.object_id,
+                         m_data.user_wired_count);
+    }
     m_curr_protection = m_data.protection;
+    
+    // We make a request for an address and got no error back, but this
+    // doesn't mean that "addr" is in the range. The data in this object will 
+    // be valid though, so you could see where the next region begins. So we
+    // return false, yet leave "m_err" with a successfull return code.
+    if ((addr < m_start) || (addr >= (m_start + m_size)))
+        return false;
 
     return true;
 }
diff --git a/lldb/tools/debugserver/source/MacOSX/MachVMRegion.h b/lldb/tools/debugserver/source/MacOSX/MachVMRegion.h
index e856f0b..bcac60b 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachVMRegion.h
+++ b/lldb/tools/debugserver/source/MacOSX/MachVMRegion.h
@@ -47,6 +47,11 @@
     uint32_t
     GetDNBPermissions () const;
 
+    const DNBError &
+    GetError ()
+    {
+        return m_err;
+    }
 protected:
 #if defined (VM_REGION_SUBMAP_SHORT_INFO_COUNT_64)
     typedef vm_region_submap_short_info_data_64_t RegionInfo;