Author: Jason Molenda
Date: 2021-05-13T00:47:58-07:00
New Revision: afee09751d2d744a753ef4d3e8d83857dcd0f682

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

LOG: [NFC] Add GetInferiorAddrSize method, unify code to compute

MachProcess.mm has a sequence to get the address size in
the inferior in three places; and I'm about to add a fourth
in a future patch.  Not a fan.

Added: 
    

Modified: 
    lldb/tools/debugserver/source/MacOSX/MachProcess.h
    lldb/tools/debugserver/source/MacOSX/MachProcess.mm

Removed: 
    


################################################################################
diff  --git a/lldb/tools/debugserver/source/MacOSX/MachProcess.h 
b/lldb/tools/debugserver/source/MacOSX/MachProcess.h
index 282366d073887..b295dfecec41a 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachProcess.h
+++ b/lldb/tools/debugserver/source/MacOSX/MachProcess.h
@@ -343,6 +343,9 @@ class MachProcess {
 
   bool ProcessUsingFrontBoard();
 
+  // Size of addresses in the inferior process (4 or 8).
+  int GetInferiorAddrSize(pid_t pid);
+
   Genealogy::ThreadActivitySP GetGenealogyInfoForThread(nub_thread_t tid,
                                                         bool &timed_out);
 

diff  --git a/lldb/tools/debugserver/source/MacOSX/MachProcess.mm 
b/lldb/tools/debugserver/source/MacOSX/MachProcess.mm
index 921e4ffb8ae5e..0a6ef61617117 100644
--- a/lldb/tools/debugserver/source/MacOSX/MachProcess.mm
+++ b/lldb/tools/debugserver/source/MacOSX/MachProcess.mm
@@ -987,23 +987,15 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary 
*options,
     nub_process_t pid, nub_addr_t image_list_address, nub_addr_t image_count) {
   JSONGenerator::DictionarySP reply_sp;
 
-  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
-  struct kinfo_proc processInfo;
-  size_t bufsize = sizeof(processInfo);
-  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, 
&bufsize,
-             NULL, 0) == 0 &&
-      bufsize > 0) {
-    uint32_t pointer_size = 4;
-    if (processInfo.kp_proc.p_flag & P_LP64)
-      pointer_size = 8;
+  int pointer_size = GetInferiorAddrSize(pid);
 
-    std::vector<struct binary_image_information> image_infos;
-    size_t image_infos_size = image_count * 3 * pointer_size;
+  std::vector<struct binary_image_information> image_infos;
+  size_t image_infos_size = image_count * 3 * pointer_size;
 
-    uint8_t *image_info_buf = (uint8_t *)malloc(image_infos_size);
-    if (image_info_buf == NULL) {
-      return reply_sp;
-    }
+  uint8_t *image_info_buf = (uint8_t *)malloc(image_infos_size);
+  if (image_info_buf == NULL) {
+    return reply_sp;
+  }
     if (ReadMemory(image_list_address, image_infos_size, image_info_buf) !=
         image_infos_size) {
       return reply_sp;
@@ -1085,7 +1077,6 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary 
*options,
     ////  Third, format all of the above in the JSONGenerator object.
 
     return FormatDynamicLibrariesIntoJSON(image_infos);
-  }
 
   return reply_sp;
 }
@@ -1146,28 +1137,16 @@ static bool 
FBSAddEventDataToOptions(NSMutableDictionary *options,
 MachProcess::GetAllLoadedLibrariesInfos(nub_process_t pid) {
   JSONGenerator::DictionarySP reply_sp;
 
-  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
-  struct kinfo_proc processInfo;
-  size_t bufsize = sizeof(processInfo);
-  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, 
&bufsize,
-             NULL, 0) == 0 &&
-      bufsize > 0) {
-    uint32_t pointer_size = 4;
-    if (processInfo.kp_proc.p_flag & P_LP64)
-      pointer_size = 8;
-
-    std::vector<struct binary_image_information> image_infos;
-    GetAllLoadedBinariesViaDYLDSPI(image_infos);
-    uint32_t platform = GetProcessPlatformViaDYLDSPI();
-    const size_t image_count = image_infos.size();
-    for (size_t i = 0; i < image_count; i++) {
-      GetMachOInformationFromMemory(platform,
-                                    image_infos[i].load_address, pointer_size,
-                                    image_infos[i].macho_info);
-    }
-    return FormatDynamicLibrariesIntoJSON(image_infos);
+  int pointer_size = GetInferiorAddrSize(pid);
+  std::vector<struct binary_image_information> image_infos;
+  GetAllLoadedBinariesViaDYLDSPI(image_infos);
+  uint32_t platform = GetProcessPlatformViaDYLDSPI();
+  const size_t image_count = image_infos.size();
+  for (size_t i = 0; i < image_count; i++) {
+    GetMachOInformationFromMemory(platform, image_infos[i].load_address,
+                                  pointer_size, image_infos[i].macho_info);
   }
-  return reply_sp;
+    return FormatDynamicLibrariesIntoJSON(image_infos);
 }
 
 // Fetch information about the shared libraries at the given load addresses
@@ -1177,30 +1156,22 @@ static bool 
FBSAddEventDataToOptions(NSMutableDictionary *options,
     nub_process_t pid, std::vector<uint64_t> &macho_addresses) {
   JSONGenerator::DictionarySP reply_sp;
 
-  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
-  struct kinfo_proc processInfo;
-  size_t bufsize = sizeof(processInfo);
-  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, 
&bufsize,
-             NULL, 0) == 0 &&
-      bufsize > 0) {
-    uint32_t pointer_size = 4;
-    if (processInfo.kp_proc.p_flag & P_LP64)
-      pointer_size = 8;
-
-    std::vector<struct binary_image_information> all_image_infos;
-    GetAllLoadedBinariesViaDYLDSPI(all_image_infos);
-    uint32_t platform = GetProcessPlatformViaDYLDSPI();
-
-    std::vector<struct binary_image_information> image_infos;
-    const size_t macho_addresses_count = macho_addresses.size();
-    const size_t all_image_infos_count = all_image_infos.size();
-    for (size_t i = 0; i < macho_addresses_count; i++) {
-      for (size_t j = 0; j < all_image_infos_count; j++) {
-        if (all_image_infos[j].load_address == macho_addresses[i]) {
-          image_infos.push_back(all_image_infos[j]);
-        }
+  int pointer_size = GetInferiorAddrSize(pid);
+
+  std::vector<struct binary_image_information> all_image_infos;
+  GetAllLoadedBinariesViaDYLDSPI(all_image_infos);
+  uint32_t platform = GetProcessPlatformViaDYLDSPI();
+
+  std::vector<struct binary_image_information> image_infos;
+  const size_t macho_addresses_count = macho_addresses.size();
+  const size_t all_image_infos_count = all_image_infos.size();
+  for (size_t i = 0; i < macho_addresses_count; i++) {
+    for (size_t j = 0; j < all_image_infos_count; j++) {
+      if (all_image_infos[j].load_address == macho_addresses[i]) {
+        image_infos.push_back(all_image_infos[j]);
       }
     }
+  }
 
     const size_t image_infos_count = image_infos.size();
     for (size_t i = 0; i < image_infos_count; i++) {
@@ -1209,8 +1180,6 @@ static bool FBSAddEventDataToOptions(NSMutableDictionary 
*options,
                                     image_infos[i].macho_info);
     }
     return FormatDynamicLibrariesIntoJSON(image_infos);
-  }
-  return reply_sp;
 }
 
 // From dyld's internal podyld_process_info.h:
@@ -4193,3 +4162,17 @@ static CFStringRef CopyBundleIDForPath(const char 
*app_bundle_path,
   CalculateBoardStatus();
   return (m_flags & eMachProcessFlagsUsingFBS) != 0;
 }
+
+int MachProcess::GetInferiorAddrSize(pid_t pid) {
+  int pointer_size = 8;
+  int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
+  struct kinfo_proc processInfo;
+  size_t bufsize = sizeof(processInfo);
+  if (sysctl(mib, (unsigned)(sizeof(mib) / sizeof(int)), &processInfo, 
&bufsize,
+             NULL, 0) == 0 &&
+      bufsize > 0) {
+    if ((processInfo.kp_proc.p_flag & P_LP64) == 0)
+      pointer_size = 4;
+  }
+  return pointer_size;
+}


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

Reply via email to