aelitashen updated this revision to Diff 278591.
aelitashen added a comment.

Add Test for Debug Info Size


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D83731/new/

https://reviews.llvm.org/D83731

Files:
  lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/lldbvscode_testcase.py
  lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py
  lldb/tools/lldb-vscode/JSONUtils.cpp

Index: lldb/tools/lldb-vscode/JSONUtils.cpp
===================================================================
--- lldb/tools/lldb-vscode/JSONUtils.cpp
+++ lldb/tools/lldb-vscode/JSONUtils.cpp
@@ -327,6 +327,51 @@
   return llvm::json::Value(std::move(object));
 }
 
+static uint64_t GetDebugInfoSizeInSection(lldb::SBSection section) {
+  uint64_t debug_info_size = 0;
+  llvm::StringRef section_name(section.GetName());
+  if (section_name.startswith(".debug") || section_name.startswith("__debug"))
+    debug_info_size += section.GetFileByteSize();
+  size_t num_sub_sections = section.GetNumSubSections();
+  std::ofstream mylog;
+  for (size_t i = 0; i < num_sub_sections; i++) {
+    // lldb::SBSection sub_section = section.GetSubSectionAtIndex(i);
+    // debug_info_size += sub_section.GetFileByteSize();
+    mylog.open("sec_name.txt");
+    mylog << num_sub_sections;
+    mylog << section_name.data();
+    debug_info_size += GetDebugInfoSizeInSection(section.GetSubSectionAtIndex(i));
+  }
+  mylog.close();
+  return debug_info_size;
+}
+
+static uint64_t GetDebugInfoSize(lldb::SBModule module) {
+  uint64_t debug_info_size = 0;
+  size_t num_sections = module.GetNumSections();
+  for (size_t i = 0; i < num_sections; i++) {
+    lldb::SBSection section = module.GetSectionAtIndex(i);
+    debug_info_size += GetDebugInfoSizeInSection(section);
+  }
+  return debug_info_size;
+}
+
+static std::string ConvertDebugInfoSize(uint64_t debug_info) {
+  char debug_info_size[32];
+  if (debug_info < 1024) {
+    snprintf(debug_info_size, sizeof(debug_info_size), " (%"  PRIu64 "B)", debug_info);
+  } else if (debug_info < 1024*1024) {
+    double kb = double(debug_info)/1024.0;
+    snprintf(debug_info_size, sizeof(debug_info_size), " (%.1fKB)", kb);
+  } else if (debug_info < 1024*1024*1024) {
+    double mb = double(debug_info)/(1024.0*1024.0);
+    snprintf(debug_info_size, sizeof(debug_info_size), " (%.1fMB)", mb);
+  } else {
+    double gb = double(debug_info)/(1024.0*1024.0*1024.0);
+    snprintf(debug_info_size, sizeof(debug_info_size), " (%.1fGB)", gb);
+  }
+  return std::string(debug_info_size);
+}
 llvm::json::Value CreateModule(lldb::SBModule &module) {
   llvm::json::Object object;
   if (!module.IsValid())
@@ -339,7 +384,13 @@
   std::string module_path(module_path_arr);
   object.try_emplace("path", module_path);
   if (module.GetNumCompileUnits() > 0) {
-    object.try_emplace("symbolStatus", "Symbols loaded.");
+    std::string symbol_str = "Symbols loaded.";
+    uint64_t debug_info = GetDebugInfoSize(module);
+    if (debug_info > 0) {
+      std::string debug_info_size = ConvertDebugInfoSize(debug_info);
+      symbol_str = symbol_str + debug_info_size;
+    }
+    object.try_emplace("symbolStatus", symbol_str);
     char symbol_path_arr[PATH_MAX];
     module.GetSymbolFileSpec().GetPath(symbol_path_arr, sizeof(symbol_path_arr));
     std::string symbol_path(symbol_path_arr);
Index: lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py
===================================================================
--- lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py
+++ lldb/test/API/tools/lldb-vscode/module/TestVSCode_module.py
@@ -37,22 +37,22 @@
         self.assertEqual(program, program_module['path'])
         self.assertTrue('symbolFilePath' not in program_module, 'Make sure a.out.stripped has no debug info')
         self.assertEqual('Symbols not found.', program_module['symbolStatus'])
-        symbol_path = self.getBuildArtifact("a.out")
-        self.vscode.request_evaluate('`%s' % ('target symbols add -s "%s" "%s"' % (program, symbol_path)))
+        symbols_path = self.getBuildArtifact("a.out")
+        self.vscode.request_evaluate('`%s' % ('target symbols add -s "%s" "%s"' % (program, symbols_path)))
 
         def checkSymbolsLoaded():
             active_modules = self.vscode.get_active_modules()
             program_module = active_modules[program_basename]
             return 'Symbols loaded.' == program_module['symbolStatus']
-        self.waitUntil(checkSymbolsLoaded)
 
+        self.waitUntil(checkSymbolsLoaded)
         active_modules = self.vscode.get_active_modules()
         program_module = active_modules[program_basename]
         self.assertEqual(program_basename, program_module['name'])
         self.assertEqual(program, program_module['path'])
         self.assertEqual('Symbols loaded.', program_module['symbolStatus'])
         self.assertIn('symbolFilePath', program_module)
-        self.assertEqual(symbol_path, program_module['symbolFilePath'])
+        self.assertEqual(symbols_path, program_module['symbolFilePath'])
         self.assertIn('addressRange', program_module)
 
     @skipIfWindows
@@ -75,3 +75,31 @@
         self.assertTrue(response['body']['compileUnits'][0]['compileUnitPath'] == main_source_path,
                         'Real path to main.cpp matches')
 
+
+    @skipIfWindows
+    @skipUnlessDarwin
+    @skipIfRemote
+    def test_debug_info_size(self):
+        program_basename = "a.out.stripped"
+        program= self.getBuildArtifact(program_basename)
+        self.build_and_launch(program)
+        functions = ['foo']
+        breakpoint_ids = self.set_function_breakpoints(functions)
+        self.assertEquals(len(breakpoint_ids), len(functions),
+                        'expect one breakpoint')
+        self.continue_to_breakpoints(breakpoint_ids)
+        symbols_path = self.getBuildArtifact("a.out.dSYM")
+        self.vscode.request_evaluate('`%s' % ('target symbols add -s "%s" "%s"' % (program, symbols_path)))
+        active_modules = self.vscode.get_active_modules()
+        program_module = active_modules[program_basename]
+
+        def checkSymbolsSize():
+            return 'Symbols loaded.' in  program_module['symbolStatus']
+            
+        self.assertTrue(self.waitUntil(checkSymbolsSize))
+        symbol_regex = re.compile(r"Symbols loaded. \([0-9]+(\.[0-9]*)?[KMG]?B\)")
+        self.assertTrue(symbol_regex.match(program_module['symbolStatus']))
+
+
+
+        
\ No newline at end of file
Index: lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/lldbvscode_testcase.py
===================================================================
--- lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/lldbvscode_testcase.py
+++ lldb/packages/Python/lldbsuite/test/tools/lldb-vscode/lldbvscode_testcase.py
@@ -53,11 +53,12 @@
             breakpoint_ids.append('%i' % (breakpoint['id']))
         return breakpoint_ids
 
-    def waitUntil(self, condition):
-        while True:
-            if condition():
-                break
+    def waitUntil(self, condition_callback):
+        for _i in range(20):
+            if condition_callback():
+                return True
             time.sleep(0.5)
+        return False
 
     def verify_breakpoint_hit(self, breakpoint_ids):
         '''Wait for the process we are debugging to stop, and verify we hit
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to