[Lldb-commits] [PATCH] D156066: [lldb][LocateModuleCallback] Call locate module callback in Platform too

2023-07-25 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG8c61c9b02b49: [lldb][LocateModuleCallback] Call locate 
module callback in Platform too (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156066

Files:
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -231,6 +231,7 @@
 EXPECT_TRUE(m_process_sp);
 
 m_module_spec = GetTestModuleSpec();
+m_module_spec_without_uuid = ModuleSpec(GetRemotePath(), ArchSpec(k_arch));
   }
 
   void TearDown() override {
@@ -244,15 +245,33 @@
   }
 
   void CheckCallbackArgs(const ModuleSpec _spec,
- FileSpec _file_spec,
- FileSpec _file_spec) {
-EXPECT_TRUE(m_module_spec.Matches(module_spec,
-  /*exact_arch_match=*/true));
+ FileSpec _file_spec, FileSpec _file_spec,
+ const ModuleSpec _module_spec,
+ int expected_callback_call_count) {
+EXPECT_TRUE(expected_module_spec.Matches(module_spec,
+ /*exact_arch_match=*/true));
 EXPECT_FALSE(module_file_spec);
 EXPECT_FALSE(symbol_file_spec);
 
-EXPECT_EQ(m_callback_call_count, 0);
-m_callback_call_count++;
+EXPECT_EQ(++m_callback_call_count, expected_callback_call_count);
+  }
+
+  void CheckCallbackArgsWithUUID(const ModuleSpec _spec,
+ FileSpec _file_spec,
+ FileSpec _file_spec,
+ int expected_callback_call_count) {
+CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
+  m_module_spec, expected_callback_call_count);
+EXPECT_TRUE(module_spec.GetUUID().IsValid());
+  }
+
+  void CheckCallbackArgsWithoutUUID(const ModuleSpec _spec,
+FileSpec _file_spec,
+FileSpec _file_spec,
+int expected_callback_call_count) {
+CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
+  m_module_spec_without_uuid, expected_callback_call_count);
+EXPECT_FALSE(module_spec.GetUUID().IsValid());
   }
 
 protected:
@@ -262,6 +281,7 @@
   TargetSP m_target_sp;
   ProcessSP m_process_sp;
   ModuleSpec m_module_spec;
+  ModuleSpec m_module_spec_without_uuid;
   ModuleSP m_module_sp;
   int m_callback_call_count = 0;
 };
@@ -331,14 +351,18 @@
   // download the module and fails.
   BuildEmptyCacheDir(m_test_dir);
 
-  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec _spec,
-FileSpec _file_spec,
-FileSpec _file_spec) {
-CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec);
-return Status("The locate module callback failed");
-  });
+  int callback_call_count = 0;
+  m_platform_sp->SetLocateModuleCallback(
+  [this, _call_count](const ModuleSpec _spec,
+   FileSpec _file_spec,
+   FileSpec _file_spec) {
+CheckCallbackArgsWithUUID(module_spec, module_file_spec,
+  symbol_file_spec, ++callback_call_count);
+return Status("The locate module callback failed");
+  });
 
   m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_EQ(callback_call_count, 2);
   ASSERT_FALSE(m_module_sp);
 }
 
@@ -348,14 +372,18 @@
   // some reason.
   FileSpec uuid_view = BuildCacheDir(m_test_dir);
 
-  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec _spec,
-FileSpec _file_spec,
-FileSpec _file_spec) {
-CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec);
-return Status("The locate module callback failed");
-  });
+  int callback_call_count = 0;
+  m_platform_sp->SetLocateModuleCallback(
+  [this, _call_count](const ModuleSpec _spec,
+   FileSpec _file_spec,
+   FileSpec _file_spec) {
+CheckCallbackArgsWithUUID(module_spec, module_file_spec,
+  symbol_file_spec, ++callback_call_count);
+return Status("The locate module callback failed");
+  });
 
   m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, 

[Lldb-commits] [PATCH] D156066: [lldb][LocateModuleCallback] Call locate module callback in Platform too

2023-07-23 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 543329.
splhack added a comment.

Remove wrong comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D156066

Files:
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -231,6 +231,7 @@
 EXPECT_TRUE(m_process_sp);
 
 m_module_spec = GetTestModuleSpec();
+m_module_spec_without_uuid = ModuleSpec(GetRemotePath(), ArchSpec(k_arch));
   }
 
   void TearDown() override {
@@ -244,15 +245,33 @@
   }
 
   void CheckCallbackArgs(const ModuleSpec _spec,
- FileSpec _file_spec,
- FileSpec _file_spec) {
-EXPECT_TRUE(m_module_spec.Matches(module_spec,
-  /*exact_arch_match=*/true));
+ FileSpec _file_spec, FileSpec _file_spec,
+ const ModuleSpec _module_spec,
+ int expected_callback_call_count) {
+EXPECT_TRUE(expected_module_spec.Matches(module_spec,
+ /*exact_arch_match=*/true));
 EXPECT_FALSE(module_file_spec);
 EXPECT_FALSE(symbol_file_spec);
 
-EXPECT_EQ(m_callback_call_count, 0);
-m_callback_call_count++;
+EXPECT_EQ(++m_callback_call_count, expected_callback_call_count);
+  }
+
+  void CheckCallbackArgsWithUUID(const ModuleSpec _spec,
+ FileSpec _file_spec,
+ FileSpec _file_spec,
+ int expected_callback_call_count) {
+CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
+  m_module_spec, expected_callback_call_count);
+EXPECT_TRUE(module_spec.GetUUID().IsValid());
+  }
+
+  void CheckCallbackArgsWithoutUUID(const ModuleSpec _spec,
+FileSpec _file_spec,
+FileSpec _file_spec,
+int expected_callback_call_count) {
+CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
+  m_module_spec_without_uuid, expected_callback_call_count);
+EXPECT_FALSE(module_spec.GetUUID().IsValid());
   }
 
 protected:
@@ -262,6 +281,7 @@
   TargetSP m_target_sp;
   ProcessSP m_process_sp;
   ModuleSpec m_module_spec;
+  ModuleSpec m_module_spec_without_uuid;
   ModuleSP m_module_sp;
   int m_callback_call_count = 0;
 };
@@ -331,14 +351,18 @@
   // download the module and fails.
   BuildEmptyCacheDir(m_test_dir);
 
-  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec _spec,
-FileSpec _file_spec,
-FileSpec _file_spec) {
-CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec);
-return Status("The locate module callback failed");
-  });
+  int callback_call_count = 0;
+  m_platform_sp->SetLocateModuleCallback(
+  [this, _call_count](const ModuleSpec _spec,
+   FileSpec _file_spec,
+   FileSpec _file_spec) {
+CheckCallbackArgsWithUUID(module_spec, module_file_spec,
+  symbol_file_spec, ++callback_call_count);
+return Status("The locate module callback failed");
+  });
 
   m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_EQ(callback_call_count, 2);
   ASSERT_FALSE(m_module_sp);
 }
 
@@ -348,14 +372,18 @@
   // some reason.
   FileSpec uuid_view = BuildCacheDir(m_test_dir);
 
-  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec _spec,
-FileSpec _file_spec,
-FileSpec _file_spec) {
-CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec);
-return Status("The locate module callback failed");
-  });
+  int callback_call_count = 0;
+  m_platform_sp->SetLocateModuleCallback(
+  [this, _call_count](const ModuleSpec _spec,
+   FileSpec _file_spec,
+   FileSpec _file_spec) {
+CheckCallbackArgsWithUUID(module_spec, module_file_spec,
+  symbol_file_spec, ++callback_call_count);
+return Status("The locate module callback failed");
+  });
 
   m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_EQ(callback_call_count, 2);
   CheckModule(m_module_sp);
   

[Lldb-commits] [PATCH] D156066: [lldb][LocateModuleCallback] Call locate module callback in Platform too

2023-07-23 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
splhack added reviewers: clayborg, jingham, bulbazord, jasonmolenda, 
JDevlieghere, mib.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

This is an enhancement for the locate module callback.
https://discourse.llvm.org/t/rfc-python-callback-for-target-get-module/71580/6

On Android remote platform, module UUID is resolved by
Platform::GetRemoteSharedModule. Which means the current
Target::CallLocateModuleCallbackIfSet() call undesirably is not able to pass the
module UUID to the locate module callback.

This diff moves the CallLocateModuleCallbackIfSet() implementation from Target
to Platform to allows both Target and Platform can call it. One is from the
current Target call site, and second is from Platform after resolving the module
UUID.

As the result of this change, the locate module callback may be called twice
for a same module on remote platforms. And it should be ok.

- First, without UUID.
  - The locate module callback is allowed to return an error if the callback 
requires UUID.
- Second, with UUID, if the first callback call did not return a module.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156066

Files:
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -231,6 +231,7 @@
 EXPECT_TRUE(m_process_sp);
 
 m_module_spec = GetTestModuleSpec();
+m_module_spec_without_uuid = ModuleSpec(GetRemotePath(), ArchSpec(k_arch));
   }
 
   void TearDown() override {
@@ -244,15 +245,33 @@
   }
 
   void CheckCallbackArgs(const ModuleSpec _spec,
- FileSpec _file_spec,
- FileSpec _file_spec) {
-EXPECT_TRUE(m_module_spec.Matches(module_spec,
-  /*exact_arch_match=*/true));
+ FileSpec _file_spec, FileSpec _file_spec,
+ const ModuleSpec _module_spec,
+ int expected_callback_call_count) {
+EXPECT_TRUE(expected_module_spec.Matches(module_spec,
+ /*exact_arch_match=*/true));
 EXPECT_FALSE(module_file_spec);
 EXPECT_FALSE(symbol_file_spec);
 
-EXPECT_EQ(m_callback_call_count, 0);
-m_callback_call_count++;
+EXPECT_EQ(++m_callback_call_count, expected_callback_call_count);
+  }
+
+  void CheckCallbackArgsWithUUID(const ModuleSpec _spec,
+ FileSpec _file_spec,
+ FileSpec _file_spec,
+ int expected_callback_call_count) {
+CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
+  m_module_spec, expected_callback_call_count);
+EXPECT_TRUE(module_spec.GetUUID().IsValid());
+  }
+
+  void CheckCallbackArgsWithoutUUID(const ModuleSpec _spec,
+FileSpec _file_spec,
+FileSpec _file_spec,
+int expected_callback_call_count) {
+CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec,
+  m_module_spec_without_uuid, expected_callback_call_count);
+EXPECT_FALSE(module_spec.GetUUID().IsValid());
   }
 
 protected:
@@ -262,6 +281,7 @@
   TargetSP m_target_sp;
   ProcessSP m_process_sp;
   ModuleSpec m_module_spec;
+  ModuleSpec m_module_spec_without_uuid;
   ModuleSP m_module_sp;
   int m_callback_call_count = 0;
 };
@@ -331,14 +351,18 @@
   // download the module and fails.
   BuildEmptyCacheDir(m_test_dir);
 
-  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec _spec,
-FileSpec _file_spec,
-FileSpec _file_spec) {
-CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec);
-return Status("The locate module callback failed");
-  });
+  int callback_call_count = 0;
+  m_platform_sp->SetLocateModuleCallback(
+  [this, _call_count](const ModuleSpec _spec,
+   FileSpec _file_spec,
+   FileSpec _file_spec) {
+CheckCallbackArgsWithUUID(module_spec, module_file_spec,
+  symbol_file_spec, ++callback_call_count);
+return Status("The locate module callback failed");
+  });
 
   m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_EQ(callback_call_count, 2);
   ASSERT_FALSE(m_module_sp);
 }
 
@@ -348,14 +372,18 @@
   // some reason.
   

[Lldb-commits] [PATCH] D155333: [lldb][LocateModuleCallback] Fix LocateModuleCallbackTest

2023-07-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rG39299f0d367d: [lldb][LocateModuleCallback] Fix 
LocateModuleCallbackTest (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155333

Files:
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -233,6 +233,11 @@
 m_module_spec = GetTestModuleSpec();
   }
 
+  void TearDown() override {
+if (m_module_sp)
+  ModuleList::RemoveSharedModule(m_module_sp);
+  }
+
   void CheckNoCallback() {
 EXPECT_FALSE(m_platform_sp->GetLocateModuleCallback());
 EXPECT_EQ(m_callback_call_count, 0);
@@ -257,51 +262,39 @@
   TargetSP m_target_sp;
   ProcessSP m_process_sp;
   ModuleSpec m_module_spec;
+  ModuleSP m_module_sp;
   int m_callback_call_count = 0;
 };
 
 } // namespace
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModule) {
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   // The module file is cached, and the locate module callback is not set.
   // GetOrCreateModule should succeed to return the module from the cache.
   FileSpec uuid_view = BuildCacheDir(m_test_dir);
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
-  CheckStrippedSymbol(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  CheckModule(m_module_sp);
+  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
+  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
+  CheckStrippedSymbol(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModuleAndSymbol) {
   // The module and symbol files are cached, and the locate module callback is
   // not set. GetOrCreateModule should succeed to return the module from the
   // cache with the symbol.
-
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   FileSpec uuid_view = BuildCacheDirWithSymbol(m_test_dir);
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
-  CheckUnstrippedSymbol(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  CheckModule(m_module_sp);
+  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
+  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckUnstrippedSymbol(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest,
@@ -313,12 +306,11 @@
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
-  CheckUnstrippedSymbol(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  CheckModule(m_module_sp);
+  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
+  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckUnstrippedSymbol(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleFailure) {
@@ -329,9 +321,8 @@
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  ASSERT_FALSE(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_FALSE(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackFailureNoCache) {
@@ -347,9 +338,8 @@
 return Status("The locate module callback failed");
   });
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  ASSERT_FALSE(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_FALSE(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackFailureCached) {
@@ -365,12 +355,11 @@
 return Status("The locate module callback failed");
   });
 
-  ModuleSP module_sp =
-  

[Lldb-commits] [PATCH] D155333: [lldb][LocateModuleCallback] Fix FileSpec compare

2023-07-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@jasonmolenda thank you for looking into this!
I was finally able to repro the test failure on arm64 macOS with this diff (the 
version with `ASSERT_EQ(a_file_spec.GetPath(), b_file_spec.GetPath()`) and this 
CMake config.

  cmake \
  ../llvm \
  -G Ninja \
  -DCMAKE_BUILD_TYPE=RelWithDebInfo \
  -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
  -DLLVM_TARGETS_TO_BUILD=X86;ARM;AArch64 \
  -DLLDB_TEST_USER_ARGS="--build-dir;$(pwd);-t;--env;TERM=vt100" \
  -DLLDB_ENFORCE_STRICT_TEST_REQUIREMENTS=Off \
  -DLLDB_ENABLE_PYTHON=On \
  -DLLVM_ENABLE_ASSERTIONS:BOOL=TRUE \
  -DLLVM_ENABLE_MODULES=On \
  -DLLVM_ENABLE_PROJECTS="clang;lld;lldb;cross-project-tests" \
  -DLLVM_LIT_ARGS="-v --time-tests --shuffle 
--xunit-xml-output=$(pwd)/results.xml -v -j 8" \
  -DLLVM_VERSION_PATCH=99 \
  -DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;compiler-rt" \
  -DPython3_EXECUTABLE=/usr/bin/python3 \
  -DSWIG_EXECUTABLE=/opt/brew/Cellar/swig@3/3.0.12/bin/swig



   TEST 'lldb-unit :: Target/./TargetTests/6/11' FAILED 

  Script(shard):
  --
  
GTEST_OUTPUT=json:/Users/user/Sources/llvm-project/build/tools/lldb/unittests/Target/./TargetTests-lldb-unit-45687-6-11.json
 GTEST_SHUFFLE=1 GTEST_TOTAL_SHARDS=11 GTEST_SHARD_INDEX=6 
GTEST_RANDOM_SEED=20766 
/Users/user/Sources/llvm-project/build/tools/lldb/unittests/Target/./TargetTests
  --
  
  Script:
  --
  
/Users/user/Sources/llvm-project/build/tools/lldb/unittests/Target/./TargetTests
 --gtest_filter=LocateModuleCallbackTest.GetOrCreateModuleWithCachedModule
  --
  
/Users/user/Sources/llvm-project/lldb/unittests/Target/LocateModuleCallbackTest.cpp:152:
 Failure
  Expected equality of these values:
a_file_spec.GetPath()
  Which is: 
"/Users/user/Sources/llvm-project/build/tools/lldb/unittests/Target/Inputs/AndroidModule.unstripped.so"
b_file_spec.GetPath()
  Which is: 
"/var/folders/hv/pgk33lbd2snf__0bd0v30yycgn/T/lit-tmp-qrocj9y_/lldb/45872/LocateModuleCallbackTest-GetOrCreateModuleWithCachedModule/remote-android/.cache/80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC/AndroidModule.so"
  
/Users/user/Sources/llvm-project/lldb/unittests/Target/LocateModuleCallbackTest.cpp:280:
 Failure
  Value of: module_sp->GetSymbolFileFileSpec()
Actual: true
  Expected: false
  
  
/Users/user/Sources/llvm-project/lldb/unittests/Target/LocateModuleCallbackTest.cpp:152
  Expected equality of these values:
a_file_spec.GetPath()
  Which is: 
"/Users/user/Sources/llvm-project/build/tools/lldb/unittests/Target/Inputs/AndroidModule.unstripped.so"
b_file_spec.GetPath()
  Which is: 
"/var/folders/hv/pgk33lbd2snf__0bd0v30yycgn/T/lit-tmp-qrocj9y_/lldb/45872/LocateModuleCallbackTest-GetOrCreateModuleWithCachedModule/remote-android/.cache/80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC/AndroidModule.so"
  
/Users/user/Sources/llvm-project/lldb/unittests/Target/LocateModuleCallbackTest.cpp:280
  Value of: module_sp->GetSymbolFileFileSpec()
Actual: true
  Expected: false

The root cause is that the module is cached in this specific test, 
`GetOrCreateModuleWithCachedModule`.
Because `llvm-lit` runs multiple tests in a same process by this CMake config.
Most runtime data is surely initialized by gtest `SetUp` but `ModuleList` 
global instance lives beyond the test lifespan.
Fixed it by tear down the cached module.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155333

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


[Lldb-commits] [PATCH] D155333: [lldb][LocateModuleCallback] Fix FileSpec compare

2023-07-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 540648.
splhack added a comment.

Tear down the module cache.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155333

Files:
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -233,6 +233,11 @@
 m_module_spec = GetTestModuleSpec();
   }
 
+  void TearDown() override {
+if (m_module_sp)
+  ModuleList::RemoveSharedModule(m_module_sp);
+  }
+
   void CheckNoCallback() {
 EXPECT_FALSE(m_platform_sp->GetLocateModuleCallback());
 EXPECT_EQ(m_callback_call_count, 0);
@@ -257,51 +262,39 @@
   TargetSP m_target_sp;
   ProcessSP m_process_sp;
   ModuleSpec m_module_spec;
+  ModuleSP m_module_sp;
   int m_callback_call_count = 0;
 };
 
 } // namespace
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModule) {
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   // The module file is cached, and the locate module callback is not set.
   // GetOrCreateModule should succeed to return the module from the cache.
   FileSpec uuid_view = BuildCacheDir(m_test_dir);
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
-  CheckStrippedSymbol(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  CheckModule(m_module_sp);
+  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
+  ASSERT_FALSE(m_module_sp->GetSymbolFileFileSpec());
+  CheckStrippedSymbol(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModuleAndSymbol) {
   // The module and symbol files are cached, and the locate module callback is
   // not set. GetOrCreateModule should succeed to return the module from the
   // cache with the symbol.
-
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   FileSpec uuid_view = BuildCacheDirWithSymbol(m_test_dir);
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
-  CheckUnstrippedSymbol(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  CheckModule(m_module_sp);
+  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
+  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckUnstrippedSymbol(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest,
@@ -313,12 +306,11 @@
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
-  CheckUnstrippedSymbol(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  CheckModule(m_module_sp);
+  ASSERT_EQ(m_module_sp->GetFileSpec(), uuid_view);
+  ASSERT_EQ(m_module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckUnstrippedSymbol(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleFailure) {
@@ -329,9 +321,8 @@
 
   CheckNoCallback();
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  ASSERT_FALSE(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_FALSE(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackFailureNoCache) {
@@ -347,9 +338,8 @@
 return Status("The locate module callback failed");
   });
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  ASSERT_FALSE(module_sp);
+  m_module_sp = m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
+  ASSERT_FALSE(m_module_sp);
 }
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleCallbackFailureCached) {
@@ -365,12 +355,11 @@
 return Status("The locate module callback failed");
   });
 
-  ModuleSP module_sp =
-  m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
-  CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  

[Lldb-commits] [PATCH] D155333: [lldb][LocateModuleCallback] Fix FileSpec compare

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 540594.
splhack added a comment.

Use GetPath() instead of Compare().


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155333

Files:
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -148,11 +148,15 @@
   return module_spec;
 }
 
+void CheckFileSpecs(const FileSpec _file_spec, const FileSpec _file_spec) {
+  ASSERT_EQ(a_file_spec.GetPath(), b_file_spec.GetPath());
+}
+
 void CheckModule(const ModuleSP _sp) {
   ASSERT_TRUE(module_sp);
   ASSERT_EQ(module_sp->GetUUID().GetAsString(), k_module_uuid);
   ASSERT_EQ(module_sp->GetObjectOffset(), 0U);
-  ASSERT_EQ(module_sp->GetPlatformFileSpec(), GetRemotePath());
+  CheckFileSpecs(module_sp->GetPlatformFileSpec(), GetRemotePath());
 }
 
 SymbolContextList FindFunctions(const ModuleSP _sp,
@@ -263,11 +267,6 @@
 } // namespace
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModule) {
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   // The module file is cached, and the locate module callback is not set.
   // GetOrCreateModule should succeed to return the module from the cache.
   FileSpec uuid_view = BuildCacheDir(m_test_dir);
@@ -277,7 +276,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -286,12 +285,6 @@
   // The module and symbol files are cached, and the locate module callback is
   // not set. GetOrCreateModule should succeed to return the module from the
   // cache with the symbol.
-
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   FileSpec uuid_view = BuildCacheDirWithSymbol(m_test_dir);
 
   CheckNoCallback();
@@ -299,8 +292,8 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
   CheckUnstrippedSymbol(module_sp);
 }
 
@@ -316,8 +309,8 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
   CheckUnstrippedSymbol(module_sp);
 }
 
@@ -368,7 +361,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -391,7 +384,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -413,7 +406,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -438,7 +431,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_TRUE(module_sp->GetSymbolFileFileSpec().GetPath().empty());
   CheckStrippedSymbol(module_sp);
 }
@@ -459,8 +452,8 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(),
-FileSpec(GetInputFilePath(k_module_file)));
+  

[Lldb-commits] [PATCH] D155333: [lldb][LocateModuleCallback] Fix FileSpec compare

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

thanks @JDevlieghere @jasonmolenda, yeah `GetPath()` makes sense it could show 
the mismatch in human readable form.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155333

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


[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

D155333  should fix the flaky arm64 tests by 
replacing `ASSERT_EQ(FileSpec, FileSpec)` with 
`ASSERT_EQ(FileSpec::Compare(FileSpec, FileSpec, true), 0)`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D155333: [lldb][LocateModuleCallback] Fix FileSpec compare

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
splhack added reviewers: clayborg, jingham, bulbazord, jasonmolenda, 
JDevlieghere, rastogishubham.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

ASSERT_EQ FileSpec is flaky on arm64. Use FileSpec::Compare instead.

- 
https://green.lab.llvm.org/green/view/LLDB/job/as-lldb-cmake/2260/testReport/junit/lldb-unit/Target___TargetTests_LocateModuleCallbackTest/GetOrCreateModuleWithCachedModule/
- 
https://green.lab.llvm.org/green/view/LLDB/job/as-lldb-cmake/2260/testReport/junit/lldb-unit/Target___TargetTests_LocateModuleCallbackTest/GetOrCreateModuleWithCachedModuleAndSymbol/


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155333

Files:
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -148,11 +148,15 @@
   return module_spec;
 }
 
+void CheckFileSpecs(const FileSpec _file_spec, const FileSpec _file_spec) {
+  ASSERT_EQ(FileSpec::Compare(a_file_spec, b_file_spec, /*full=*/true), 0);
+}
+
 void CheckModule(const ModuleSP _sp) {
   ASSERT_TRUE(module_sp);
   ASSERT_EQ(module_sp->GetUUID().GetAsString(), k_module_uuid);
   ASSERT_EQ(module_sp->GetObjectOffset(), 0U);
-  ASSERT_EQ(module_sp->GetPlatformFileSpec(), GetRemotePath());
+  CheckFileSpecs(module_sp->GetPlatformFileSpec(), GetRemotePath());
 }
 
 SymbolContextList FindFunctions(const ModuleSP _sp,
@@ -263,11 +267,6 @@
 } // namespace
 
 TEST_F(LocateModuleCallbackTest, GetOrCreateModuleWithCachedModule) {
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   // The module file is cached, and the locate module callback is not set.
   // GetOrCreateModule should succeed to return the module from the cache.
   FileSpec uuid_view = BuildCacheDir(m_test_dir);
@@ -277,7 +276,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -286,12 +285,6 @@
   // The module and symbol files are cached, and the locate module callback is
   // not set. GetOrCreateModule should succeed to return the module from the
   // cache with the symbol.
-
-  // Disable test on arm64 because of failures in the lldb incremental arm64
-  // bot.
-#if defined(__arm64__) || defined(__aarch64__) || defined(_M_ARM64)
-  GTEST_SKIP() << "broken on arm64.";
-#endif
   FileSpec uuid_view = BuildCacheDirWithSymbol(m_test_dir);
 
   CheckNoCallback();
@@ -299,8 +292,8 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
   CheckUnstrippedSymbol(module_sp);
 }
 
@@ -316,8 +309,8 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
-  ASSERT_EQ(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetSymbolFileFileSpec(), GetSymFileSpec(uuid_view));
   CheckUnstrippedSymbol(module_sp);
 }
 
@@ -368,7 +361,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -391,7 +384,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -413,7 +406,7 @@
   ModuleSP module_sp =
   m_target_sp->GetOrCreateModule(m_module_spec, /*notify=*/false);
   CheckModule(module_sp);
-  ASSERT_EQ(module_sp->GetFileSpec(), uuid_view);
+  CheckFileSpecs(module_sp->GetFileSpec(), uuid_view);
   ASSERT_FALSE(module_sp->GetSymbolFileFileSpec());
   CheckStrippedSymbol(module_sp);
 }
@@ -438,7 

[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

ok, sounds like ASSERT_EQ FileSpec does not work on Linux aarch64.
will use FileSpec::Compare and double check on Linux aarch64.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D155124: [lldb][Windows] Fix TestLocateModuleCallback

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG43b9d62018fe: [lldb][Windows] Fix TestLocateModuleCallback 
(authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155124

Files:
  lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py


Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -49,7 +49,9 @@
 
 self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, 
module_spec.GetFileSpec().fullpath)
+self.assertEqual(
+MODULE_PLATFORM_PATH, 
Path(module_spec.GetFileSpec().fullpath).as_posix()
+)
 
 def check_module(self, module: lldb.SBModule, symbol_file: str, 
symbol_kind: str):
 self.assertTrue(module.IsValid())
@@ -61,16 +63,20 @@
 
 self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, 
module.GetPlatformFileSpec().fullpath)
-
 self.assertEqual(
-str(self.input_dir / MODULE_FILE),
-module.GetFileSpec().fullpath,
+MODULE_PLATFORM_PATH, 
Path(module.GetPlatformFileSpec().fullpath).as_posix()
 )
 
-self.assertEqual(
-str(self.input_dir / symbol_file),
-module.GetSymbolFileSpec().fullpath,
+self.assertTrue(
+(self.input_dir / MODULE_FILE)
+.resolve()
+.samefile(Path(module.GetFileSpec().fullpath).resolve())
+)
+
+self.assertTrue(
+(self.input_dir / symbol_file)
+.resolve()
+.samefile(Path(module.GetSymbolFileSpec().fullpath).resolve())
 )
 
 sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)


Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -49,7 +49,9 @@
 
 self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+self.assertEqual(
+MODULE_PLATFORM_PATH, Path(module_spec.GetFileSpec().fullpath).as_posix()
+)
 
 def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
 self.assertTrue(module.IsValid())
@@ -61,16 +63,20 @@
 
 self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
-
 self.assertEqual(
-str(self.input_dir / MODULE_FILE),
-module.GetFileSpec().fullpath,
+MODULE_PLATFORM_PATH, Path(module.GetPlatformFileSpec().fullpath).as_posix()
 )
 
-self.assertEqual(
-str(self.input_dir / symbol_file),
-module.GetSymbolFileSpec().fullpath,
+self.assertTrue(
+(self.input_dir / MODULE_FILE)
+.resolve()
+.samefile(Path(module.GetFileSpec().fullpath).resolve())
+)
+
+self.assertTrue(
+(self.input_dir / symbol_file)
+.resolve()
+.samefile(Path(module.GetSymbolFileSpec().fullpath).resolve())
 )
 
 sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@rastogishubham let me check.

btw I'll commit D155124  to fix Windows tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

In D153735#4494999 , @splhack wrote:

> https://lab.llvm.org/buildbot/#/builders/219/builds/4084
>
> now fixing Windows tests.

D155124  should fix the Windows test failures.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D155135: [lldb][LocateModuleCallback] Fix LocateModuleCallbackTest

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGfb087c17c823: [lldb][LocateModuleCallback] Fix 
LocateModuleCallbackTest (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155135

Files:
  lldb/unittests/Target/LocateModuleCallbackTest.cpp


Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -22,6 +22,7 @@
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::platform_android;
+using namespace lldb_private::platform_linux;
 using namespace lldb_private::breakpad;
 using namespace testing;
 
@@ -190,7 +191,8 @@
 
 class LocateModuleCallbackTest : public testing::Test {
   SubsystemRAII
+PlatformAndroid, PlatformLinux, SymbolFileBreakpad,
+SymbolFileSymtab>
   subsystems;
 
 public:
@@ -202,6 +204,9 @@
 m_test_dir);
 
 // Create Debugger.
+ArchSpec host_arch("i386-pc-linux");
+Platform::SetHostPlatform(
+platform_linux::PlatformLinux::CreateInstance(true, _arch));
 m_debugger_sp = Debugger::CreateInstance();
 EXPECT_TRUE(m_debugger_sp);
 


Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -22,6 +22,7 @@
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::platform_android;
+using namespace lldb_private::platform_linux;
 using namespace lldb_private::breakpad;
 using namespace testing;
 
@@ -190,7 +191,8 @@
 
 class LocateModuleCallbackTest : public testing::Test {
   SubsystemRAII
+PlatformAndroid, PlatformLinux, SymbolFileBreakpad,
+SymbolFileSymtab>
   subsystems;
 
 public:
@@ -202,6 +204,9 @@
 m_test_dir);
 
 // Create Debugger.
+ArchSpec host_arch("i386-pc-linux");
+Platform::SetHostPlatform(
+platform_linux::PlatformLinux::CreateInstance(true, _arch));
 m_debugger_sp = Debugger::CreateInstance();
 EXPECT_TRUE(m_debugger_sp);
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D155135: [lldb][LocateModuleCallback] Fix LocateModuleCallbackTest

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@jasonmolenda thanks for verifying this! and yes, I didn't realize 
`-DLLVM_ENABLE_ASSERTIONS=1` is missing.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D155135

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


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@jasonmolenda D155135  should fix the 
`Debugger::CreateInstance` assert.
(The assert does not repro locally though)


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D155135: [lldb][LocateModuleCallback] Fix LocateModuleCallbackTest

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
splhack added reviewers: clayborg, jingham, bulbazord, jasonmolenda, 
JDevlieghere, mib.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

D153734  forgot to call 
`Platform::SetHostPlatform` before
`Debugger::CreateInstance`. Copy similar code.
https://github.com/llvm/llvm-project/blob/5007c78ce0936824972ec6ba3cfa598192087ee1/lldb/unittests/Expression/DWARFExpressionTest.cpp#L371-L374


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155135

Files:
  lldb/unittests/Target/LocateModuleCallbackTest.cpp


Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -22,6 +22,7 @@
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::platform_android;
+using namespace lldb_private::platform_linux;
 using namespace lldb_private::breakpad;
 using namespace testing;
 
@@ -190,7 +191,8 @@
 
 class LocateModuleCallbackTest : public testing::Test {
   SubsystemRAII
+PlatformAndroid, PlatformLinux, SymbolFileBreakpad,
+SymbolFileSymtab>
   subsystems;
 
 public:
@@ -202,6 +204,9 @@
 m_test_dir);
 
 // Create Debugger.
+ArchSpec host_arch("i386-pc-linux");
+Platform::SetHostPlatform(
+platform_linux::PlatformLinux::CreateInstance(true, _arch));
 m_debugger_sp = Debugger::CreateInstance();
 EXPECT_TRUE(m_debugger_sp);
 


Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- lldb/unittests/Target/LocateModuleCallbackTest.cpp
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -22,6 +22,7 @@
 using namespace lldb;
 using namespace lldb_private;
 using namespace lldb_private::platform_android;
+using namespace lldb_private::platform_linux;
 using namespace lldb_private::breakpad;
 using namespace testing;
 
@@ -190,7 +191,8 @@
 
 class LocateModuleCallbackTest : public testing::Test {
   SubsystemRAII
+PlatformAndroid, PlatformLinux, SymbolFileBreakpad,
+SymbolFileSymtab>
   subsystems;
 
 public:
@@ -202,6 +204,9 @@
 m_test_dir);
 
 // Create Debugger.
+ArchSpec host_arch("i386-pc-linux");
+Platform::SetHostPlatform(
+platform_linux::PlatformLinux::CreateInstance(true, _arch));
 m_debugger_sp = Debugger::CreateInstance();
 EXPECT_TRUE(m_debugger_sp);
 
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

I'm checking other `Debugger::CreateInstance` calls in unittests.
Looks like it needs to call `Platform::SetHostPlatform` which is missing in 
LocateModuleCallbackTest.cpp.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@jasonmolenda thanks for the report. let me check.

At least `ninja check-lldb-unit check-lldb-api-python_api-sbplatform` is ok on 
macOS arm64.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D155124: [lldb][Windows] Fix TestLocateModuleCallback

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
splhack added reviewers: clayborg, jingham, bulbazord, jasonmolenda, 
JDevlieghere, mib.
Herald added a project: All.
splhack requested review of this revision.
Herald added subscribers: lldb-commits, wangpc.
Herald added a project: LLDB.

D153735  added the tests but it is failing 
due to POSIX path vs Windows path.
https://lab.llvm.org/buildbot/#/builders/219/builds/4084

Fix it.

- MODULE_PLATFORM_PATH is POSIX path.
- Normalize self.input_dir, FileSpec and SymbolFileSpec fullpath.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155124

Files:
  lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py


Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -49,7 +49,9 @@
 
 self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, 
module_spec.GetFileSpec().fullpath)
+self.assertEqual(
+MODULE_PLATFORM_PATH, 
Path(module_spec.GetFileSpec().fullpath).as_posix()
+)
 
 def check_module(self, module: lldb.SBModule, symbol_file: str, 
symbol_kind: str):
 self.assertTrue(module.IsValid())
@@ -61,16 +63,20 @@
 
 self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, 
module.GetPlatformFileSpec().fullpath)
-
 self.assertEqual(
-str(self.input_dir / MODULE_FILE),
-module.GetFileSpec().fullpath,
+MODULE_PLATFORM_PATH, 
Path(module.GetPlatformFileSpec().fullpath).as_posix()
 )
 
-self.assertEqual(
-str(self.input_dir / symbol_file),
-module.GetSymbolFileSpec().fullpath,
+self.assertTrue(
+(self.input_dir / MODULE_FILE)
+.resolve()
+.samefile(Path(module.GetFileSpec().fullpath).resolve())
+)
+
+self.assertTrue(
+(self.input_dir / symbol_file)
+.resolve()
+.samefile(Path(module.GetSymbolFileSpec().fullpath).resolve())
 )
 
 sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)


Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -49,7 +49,9 @@
 
 self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+self.assertEqual(
+MODULE_PLATFORM_PATH, Path(module_spec.GetFileSpec().fullpath).as_posix()
+)
 
 def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
 self.assertTrue(module.IsValid())
@@ -61,16 +63,20 @@
 
 self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
 
-self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
-
 self.assertEqual(
-str(self.input_dir / MODULE_FILE),
-module.GetFileSpec().fullpath,
+MODULE_PLATFORM_PATH, Path(module.GetPlatformFileSpec().fullpath).as_posix()
 )
 
-self.assertEqual(
-str(self.input_dir / symbol_file),
-module.GetSymbolFileSpec().fullpath,
+self.assertTrue(
+(self.input_dir / MODULE_FILE)
+.resolve()
+.samefile(Path(module.GetFileSpec().fullpath).resolve())
+)
+
+self.assertTrue(
+(self.input_dir / symbol_file)
+.resolve()
+.samefile(Path(module.GetSymbolFileSpec().fullpath).resolve())
 )
 
 sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

https://lab.llvm.org/buildbot/#/builders/219/builds/4084

now fixing Windows tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGf03dbdb70aed: [lldb][LocateModuleCallback] Implement API, 
Python interface (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBPlatform.h
  lldb/source/API/SBPlatform.cpp
  lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py

Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -0,0 +1,338 @@
+"""
+Test platform locate module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class LocateModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.platform = self.dbg.GetSelectedPlatform()
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.platform.SetLocateModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 3 argument.
+def test_args2(a, b):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 3 argument callable object"):
+self.platform.SetLocateModuleCallback(test_args2)
+
+def test_default(self):
+# The default behavior is to locate the module with LLDB implementation
+# and AddModule should fail.
+module = self.target.AddModule(
+MODULE_PLATFORM_PATH,
+MODULE_TRIPLE,
+MODULE_UUID,
+)
+
+self.assertFalse(module)
+
+def test_set_none(self):
+# SetLocateModuleCallback should succeed to clear the callback with None.
+# and AddModule should fail.
+self.assertTrue(self.platform.SetLocateModuleCallback(None).Success())
+
+module = self.target.AddModule(
+MODULE_PLATFORM_PATH,
+MODULE_TRIPLE,
+MODULE_UUID,
+)
+
+self.assertFalse(module)
+
+def test_return_error(self):
+# The callback fails, AddModule should fail.
+def test_locate_module(
+module_spec: lldb.SBModuleSpec,
+module_file_spec: 

[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG7f1028e9df52: [lldb][LocateModuleCallback] Call locate 
module callback (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

Files:
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/CMakeLists.txt
  lldb/unittests/Target/Inputs/AndroidModule.c
  lldb/unittests/Target/Inputs/AndroidModule.so
  lldb/unittests/Target/Inputs/AndroidModule.so.sym
  lldb/unittests/Target/Inputs/AndroidModule.unstripped.so
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- /dev/null
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -0,0 +1,640 @@
+//===-- LocateModuleCallbackTest.cpp --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
+#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Target.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace lldb_private::breakpad;
+using namespace testing;
+
+namespace {
+
+constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
+constexpr llvm::StringLiteral k_platform_dir("remote-android");
+constexpr llvm::StringLiteral k_cache_dir(".cache");
+constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
+constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
+constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
+constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
+constexpr llvm::StringLiteral
+k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
+constexpr llvm::StringLiteral k_function_symbol("boom");
+constexpr llvm::StringLiteral k_hidden_function_symbol("boom_hidden");
+const size_t k_module_size = 3784;
+
+ModuleSpec GetTestModuleSpec();
+
+class MockProcess : public Process {
+public:
+  MockProcess(TargetSP target_sp, ListenerSP listener_sp)
+  : Process(target_sp, listener_sp) {}
+
+  llvm::StringRef GetPluginName() override { return k_process_plugin; };
+
+  bool CanDebug(TargetSP target, bool plugin_specified_by_name) override {
+return true;
+  }
+
+  Status DoDestroy() override { return Status(); }
+
+  void RefreshStateAfterStop() override {}
+
+  bool DoUpdateThreadList(ThreadList _thread_list,
+  ThreadList _thread_list) override {
+return false;
+  }
+
+  size_t DoReadMemory(addr_t vm_addr, void *buf, size_t size,
+  Status ) override {
+return 0;
+  }
+
+  bool GetModuleSpec(const FileSpec _file_spec, const ArchSpec ,
+ ModuleSpec _spec) override {
+module_spec = GetTestModuleSpec();
+return true;
+  }
+};
+
+FileSpec GetTestDir() {
+  const auto *info = UnitTest::GetInstance()->current_test_info();
+  FileSpec test_dir = HostInfo::GetProcessTempDir();
+  test_dir.AppendPathComponent(std::string(info->test_case_name()) + "-" +
+   info->name());
+  std::error_code ec = llvm::sys::fs::create_directory(test_dir.GetPath());
+  EXPECT_FALSE(ec);
+  return test_dir;
+}
+
+FileSpec GetRemotePath() {
+  FileSpec fs("/", FileSpec::Style::posix);
+  fs.AppendPathComponent("bin");
+  fs.AppendPathComponent(k_module_file);
+  return fs;
+}
+
+FileSpec GetUuidView(FileSpec spec) {
+  spec.AppendPathComponent(k_platform_dir);
+  spec.AppendPathComponent(k_cache_dir);
+  spec.AppendPathComponent(k_module_uuid);
+  spec.AppendPathComponent(k_module_file);
+  return spec;
+}
+
+void BuildEmptyCacheDir(const FileSpec _dir) {
+  FileSpec cache_dir(test_dir);
+  cache_dir.AppendPathComponent(k_platform_dir);
+  cache_dir.AppendPathComponent(k_cache_dir);
+  std::error_code ec = llvm::sys::fs::create_directories(cache_dir.GetPath());
+  EXPECT_FALSE(ec);
+}
+
+FileSpec BuildCacheDir(const FileSpec _dir) {
+  FileSpec uuid_view = GetUuidView(test_dir);
+  std::error_code ec =
+  

[Lldb-commits] [PATCH] D153733: [lldb][LocateModuleCallback] Update SBFileSpec/SBModuleSpec

2023-07-12 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGc4fa6fafc43a: [lldb][LocateModuleCallback] Update 
SBFileSpec/SBModuleSpec (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153733

Files:
  lldb/bindings/python/python-swigsafecast.swig
  lldb/include/lldb/API/SBModuleSpec.h
  lldb/source/API/SBModuleSpec.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/test/API/python_api/module_spec/TestModuleSpec.py

Index: lldb/test/API/python_api/module_spec/TestModuleSpec.py
===
--- /dev/null
+++ lldb/test/API/python_api/module_spec/TestModuleSpec.py
@@ -0,0 +1,24 @@
+"""
+Test some SBModuleSpec APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class ModuleSpecAPIsTestCase(TestBase):
+def test_object_offset_and_size(self):
+module_spec = lldb.SBModuleSpec()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
+
+module_spec.SetObjectOffset(4096)
+self.assertEqual(module_spec.GetObjectOffset(), 4096)
+
+module_spec.SetObjectSize(3600)
+self.assertEqual(module_spec.GetObjectSize(), 3600)
+
+module_spec.Clear()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
Index: lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -30,6 +30,8 @@
 class SBValue;
 class SBStream;
 class SBStructuredData;
+class SBFileSpec;
+class SBModuleSpec;
 } // namespace lldb
 
 namespace lldb_private {
@@ -102,6 +104,10 @@
   static PythonObject ToSWIGWrapper(std::unique_ptr stream_sb);
   static PythonObject
   ToSWIGWrapper(std::unique_ptr data_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr file_spec_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr module_spec_sb);
 
   static python::ScopedPythonObject
   ToSWIGWrapper(CommandReturnObject _retobj);
Index: lldb/source/API/SBModuleSpec.cpp
===
--- lldb/source/API/SBModuleSpec.cpp
+++ lldb/source/API/SBModuleSpec.cpp
@@ -29,6 +29,11 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
+SBModuleSpec::SBModuleSpec(const lldb_private::ModuleSpec _spec)
+: m_opaque_up(new lldb_private::ModuleSpec(module_spec)) {
+  LLDB_INSTRUMENT_VA(this, module_spec);
+}
+
 const SBModuleSpec ::operator=(const SBModuleSpec ) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
@@ -145,6 +150,30 @@
   return true;
 }
 
+uint64_t SBModuleSpec::GetObjectOffset() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectOffset();
+}
+
+void SBModuleSpec::SetObjectOffset(uint64_t object_offset) {
+  LLDB_INSTRUMENT_VA(this, object_offset);
+
+  m_opaque_up->SetObjectOffset(object_offset);
+}
+
+uint64_t SBModuleSpec::GetObjectSize() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectSize();
+}
+
+void SBModuleSpec::SetObjectSize(uint64_t object_size) {
+  LLDB_INSTRUMENT_VA(this, object_size);
+
+  m_opaque_up->SetObjectSize(object_size);
+}
+
 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
   LLDB_INSTRUMENT_VA(this);
 }
Index: lldb/include/lldb/API/SBModuleSpec.h
===
--- lldb/include/lldb/API/SBModuleSpec.h
+++ lldb/include/lldb/API/SBModuleSpec.h
@@ -77,13 +77,24 @@
 
   bool SetUUIDBytes(const uint8_t *uuid, size_t uuid_len);
 
+  uint64_t GetObjectOffset();
+
+  void SetObjectOffset(uint64_t object_offset);
+
+  uint64_t GetObjectSize();
+
+  void SetObjectSize(uint64_t object_size);
+
   bool GetDescription(lldb::SBStream );
 
 private:
   friend class SBModuleSpecList;
   friend class SBModule;
+  friend class SBPlatform;
   friend class SBTarget;
 
+  SBModuleSpec(const lldb_private::ModuleSpec _spec);
+
   std::unique_ptr m_opaque_up;
 };
 
Index: lldb/bindings/python/python-swigsafecast.swig
===
--- lldb/bindings/python/python-swigsafecast.swig
+++ lldb/bindings/python/python-swigsafecast.swig
@@ -120,5 +120,15 @@
SWIGTYPE_p_lldb__SBEvent);
 }
 
+PythonObject SWIGBridge::ToSWIGWrapper(
+std::unique_ptr file_spec_sb) {
+  return ToSWIGHelper(file_spec_sb.release(), SWIGTYPE_p_lldb__SBFileSpec);
+}
+
+PythonObject SWIGBridge::ToSWIGWrapper(
+std::unique_ptr module_spec_sb) {
+  return ToSWIGHelper(module_spec_sb.release(), SWIGTYPE_p_lldb__SBModuleSpec);
+}
+
 } // namespace python
 } // namespace lldb_private

[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/source/API/SBPlatform.cpp:664-689
+  if (platform_sp) {
+if (callback) {
+  platform_sp->SetLocateModuleCallback(
+  [callback, callback_baton](const ModuleSpec _spec,
+ FileSpec _file_spec,
+ FileSpec _file_spec) {
+SBModuleSpec module_spec_sb(module_spec);

bulbazord wrote:
> Suggestion: Invert the conditions and use early returns. This function has 
> quite a bit of nesting and I think the readability could be improved.
Thanks, yes it looks better. Also added a comment.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 539351.
splhack added a comment.

update SetLocateModuleCallback


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBPlatform.h
  lldb/source/API/SBPlatform.cpp
  lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py

Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -0,0 +1,338 @@
+"""
+Test platform locate module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class LocateModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.platform = self.dbg.GetSelectedPlatform()
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.platform.SetLocateModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 3 argument.
+def test_args2(a, b):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 3 argument callable object"):
+self.platform.SetLocateModuleCallback(test_args2)
+
+def test_default(self):
+# The default behavior is to locate the module with LLDB implementation
+# and AddModule should fail.
+module = self.target.AddModule(
+MODULE_PLATFORM_PATH,
+MODULE_TRIPLE,
+MODULE_UUID,
+)
+
+self.assertFalse(module)
+
+def test_set_none(self):
+# SetLocateModuleCallback should succeed to clear the callback with None.
+# and AddModule should fail.
+self.assertTrue(self.platform.SetLocateModuleCallback(None).Success())
+
+module = self.target.AddModule(
+MODULE_PLATFORM_PATH,
+MODULE_TRIPLE,
+MODULE_UUID,
+)
+
+self.assertFalse(module)
+
+def test_return_error(self):
+# The callback fails, AddModule should fail.
+def test_locate_module(
+module_spec: lldb.SBModuleSpec,
+module_file_spec: lldb.SBFileSpec,
+symbol_file_spec: lldb.SBFileSpec,
+):
+

[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/include/lldb/Target/Platform.h:884-887
+  typedef std::function
+  LocateModuleCallback;

clayborg wrote:
> I think we still need a baton for the callback so clients can register a 
> callback + void *.
Yes, callback and baton(`void *`) are retained by SBPlatform.
The detailed flow is here https://reviews.llvm.org/D153735#4491892



Comment at: lldb/include/lldb/Target/Platform.h:944
   const std::unique_ptr m_module_cache;
+  LocateModuleCallback m_locate_module_callback;
 

splhack wrote:
> clayborg wrote:
> > We probably need a baton still. In order to make this work for python, we 
> > need to be able to set a callback and a baton for the python callable?
> @clayborg Yes, in D153735, both the callback and the baton are captured and 
> retained by SBPlatform. The reason of why SBPlatform captures and retains the 
> callback and the baton is the SBPlatformLocateModuleCallback type contains 
> SBModuleSpec type and SBFileSpec type in the argument. Which are not 
> available in Platform.h.
> 
> ```
> typedef SBError (*SBPlatformLocateModuleCallback)(
> void *baton, const SBModuleSpec _spec, SBFileSpec 
> _file_spec,
> SBFileSpec _file_spec);
> ```
commented the detailed callback and baton flow here.
https://reviews.llvm.org/D153735#4491892


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D153735: [lldb][LocateModuleCallback] Implement API, Python interface

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@clayborg yes, it'd be possible to pass the baton down to Platform.h since it 
is just `void *`, so no dependencies required.

But, first, let me explain the callback and baton flow.



For Python, user will set a callable Python object as the callback.
For C++ API, user will set a function pointer with a baton as the callback.

Both Python callback and C++ API callback will go into SBPlatform 
SetLocateModuleCallback.
SetLocateModuleCallback will use a lambda in order to convert ModuleSpec and 
FileSpec to SBModuleSpec and SBFileSpec.
This is because Platform.h and Target.cpp are not able to use SBModuleSpec and 
SBFileSpec.

  |   |  |   |  
   |  |
  | user callback |<-(SBModuleSpec, SBFileSpec)->| SBPlatform lambda 
|<-(ModuleSpec, FileSpec)>| Platform |
  |  baton|  |  captures callback|  
   |  |
  |   |  |   baton   |  
   |  |

This summary is what things are retained by which layer.

- Python
  - the user callable object
- SBPlatform
  - C++ API: the user callback and the baton
  - Python: LLDBSwigPythonCallLocateModuleCallback and the user callable object 
as a baton
- Platform
  - SBPlatform lambda in std::function

There are three things.

- SBPlatform lambda created by SetLocateModuleCallback, the type is 
Platform::LocateModuleCallback.
- The user callback or LLDBSwigPythonCallLocateModuleCallback, the type is 
SBPlatformLocateModuleCallback.
- The baton (for Python, this is the callable object)



And back to 'passing the baton down to Platform.h'.
As the locate callback implementation in D153734 
 
lldb/unittests/Target/LocateModuleCallbackTest.cpp,
internal users can use lambda to capture/retain anything for the callback. For 
example, `this` is captured by the locate callback in this case.
Therefore, no need to pass the baton down to Platform.h.

  m_platform_sp->SetLocateModuleCallback([this](const ModuleSpec _spec,
FileSpec _file_spec,
FileSpec _file_spec) {
CheckCallbackArgs(module_spec, module_file_spec, symbol_file_spec);
symbol_file_spec.SetPath(GetInputFilePath(k_breakpad_symbol_file));
return Status();
  });




Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/include/lldb/Target/Platform.h:944
   const std::unique_ptr m_module_cache;
+  LocateModuleCallback m_locate_module_callback;
 

clayborg wrote:
> We probably need a baton still. In order to make this work for python, we 
> need to be able to set a callback and a baton for the python callable?
@clayborg Yes, in D153735, both the callback and the baton are captured and 
retained by SBPlatform. The reason of why SBPlatform captures and retains the 
callback and the baton is the SBPlatformLocateModuleCallback type contains 
SBModuleSpec type and SBFileSpec type in the argument. Which are not available 
in Platform.h.

```
typedef SBError (*SBPlatformLocateModuleCallback)(
void *baton, const SBModuleSpec _spec, SBFileSpec _file_spec,
SBFileSpec _file_spec);
```


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D153735: [lldb][TargetGetModuleCallback] Implement Python interface

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 539238.
splhack added a comment.

- Rename 'get module callback' to 'locate module callback'
- SBPlatform will do
  - capture callback(SBPlatformLocateModuleCallback) and baton(void *)
  - convert ModuleSpec/FileSpec from/to SBModuleSpec/SBFileSpec for calling the 
callback


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBPlatform.h
  lldb/source/API/SBPlatform.cpp
  lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py

Index: lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/sbplatform/TestLocateModuleCallback.py
@@ -0,0 +1,338 @@
+"""
+Test platform locate module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class LocateModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.platform = self.dbg.GetSelectedPlatform()
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.platform.SetLocateModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 3 argument.
+def test_args2(a, b):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 3 argument callable object"):
+self.platform.SetLocateModuleCallback(test_args2)
+
+def test_default(self):
+# The default behavior is to locate the module with LLDB implementation
+# and AddModule should fail.
+module = self.target.AddModule(
+MODULE_PLATFORM_PATH,
+MODULE_TRIPLE,
+MODULE_UUID,
+)
+
+self.assertFalse(module)
+
+def test_set_none(self):
+# SetLocateModuleCallback should succeed to clear the callback with None.
+# and AddModule should fail.
+self.assertTrue(self.platform.SetLocateModuleCallback(None).Success())
+
+module = self.target.AddModule(
+MODULE_PLATFORM_PATH,
+MODULE_TRIPLE,
+MODULE_UUID,
+)
+
+self.assertFalse(module)
+
+def test_return_error(self):
+# The callback fails, AddModule should 

[Lldb-commits] [PATCH] D153734: [lldb][LocateModuleCallback] Call locate module callback

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack marked 3 inline comments as done.
splhack added a comment.

SBPlatform API and Python will use SBPlatformLocateModuleCallback type.

  typedef SBError (*SBPlatformLocateModuleCallback)(
  void *baton,
  const SBModuleSpec _spec,
  SBFileSpec _file_spec,
  SBFileSpec _file_spec);

Platform.h will use LocateModuleCallback type.

  typedef std::function LocateModuleCallback;

So, Platform.h does not need to have SB* dependencies,
and SBPlatform can convert ModuleSpec/FileSpec from/to SBModuleSpec/SBFileSpec 
for the callback.




Comment at: lldb/include/lldb/Target/Platform.h:888
+  /// from symbol servers.
+  void SetTargetGetModuleCallback(void *callback_baton);
+

clayborg wrote:
> You actually need a callback along with the baton here. We probably don't 
> need the word "Target" in the function name?
> 
> Maybe better named as 
> ```
> void SetLocationModuleCallback(PlatformLocateModuleCallback callback, void 
> *baton);
> ```
Platform.h now only retains a std::function for the locate module callback.

In D153735, SBPlatform will use a lambda
- to capture callback(SBPlatformLocateModuleCallback) and baton(void *)
- to convert ModuleSpec/FileSpec from/to SBModuleSpec/SBFileSpec

Therefore, Platform.h does not need SB* dependencies.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D153734: [lldb][TargetGetModuleCallback] Call get module callback

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 539213.
splhack added a comment.

- Rename 'get module callback' to 'locate module callback'
- Enable C++ callback aside from Python callback
- Introduce LocateModuleCallback typedef to use std::function in order to 
capture callback and baton, to convert ModuleSpec/FileSpec from/to 
SBModuleSpec/SBFileSpec.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

Files:
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/CMakeLists.txt
  lldb/unittests/Target/Inputs/AndroidModule.c
  lldb/unittests/Target/Inputs/AndroidModule.so
  lldb/unittests/Target/Inputs/AndroidModule.so.sym
  lldb/unittests/Target/Inputs/AndroidModule.unstripped.so
  lldb/unittests/Target/LocateModuleCallbackTest.cpp

Index: lldb/unittests/Target/LocateModuleCallbackTest.cpp
===
--- /dev/null
+++ lldb/unittests/Target/LocateModuleCallbackTest.cpp
@@ -0,0 +1,640 @@
+//===-- LocateModuleCallbackTest.cpp --===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
+#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Target.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace lldb_private::breakpad;
+using namespace testing;
+
+namespace {
+
+constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
+constexpr llvm::StringLiteral k_platform_dir("remote-android");
+constexpr llvm::StringLiteral k_cache_dir(".cache");
+constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
+constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
+constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
+constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
+constexpr llvm::StringLiteral
+k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
+constexpr llvm::StringLiteral k_function_symbol("boom");
+constexpr llvm::StringLiteral k_hidden_function_symbol("boom_hidden");
+const size_t k_module_size = 3784;
+
+ModuleSpec GetTestModuleSpec();
+
+class MockProcess : public Process {
+public:
+  MockProcess(TargetSP target_sp, ListenerSP listener_sp)
+  : Process(target_sp, listener_sp) {}
+
+  llvm::StringRef GetPluginName() override { return k_process_plugin; };
+
+  bool CanDebug(TargetSP target, bool plugin_specified_by_name) override {
+return true;
+  }
+
+  Status DoDestroy() override { return Status(); }
+
+  void RefreshStateAfterStop() override {}
+
+  bool DoUpdateThreadList(ThreadList _thread_list,
+  ThreadList _thread_list) override {
+return false;
+  }
+
+  size_t DoReadMemory(addr_t vm_addr, void *buf, size_t size,
+  Status ) override {
+return 0;
+  }
+
+  bool GetModuleSpec(const FileSpec _file_spec, const ArchSpec ,
+ ModuleSpec _spec) override {
+module_spec = GetTestModuleSpec();
+return true;
+  }
+};
+
+FileSpec GetTestDir() {
+  const auto *info = UnitTest::GetInstance()->current_test_info();
+  FileSpec test_dir = HostInfo::GetProcessTempDir();
+  test_dir.AppendPathComponent(std::string(info->test_case_name()) + "-" +
+   info->name());
+  std::error_code ec = llvm::sys::fs::create_directory(test_dir.GetPath());
+  EXPECT_FALSE(ec);
+  return test_dir;
+}
+
+FileSpec GetRemotePath() {
+  FileSpec fs("/", FileSpec::Style::posix);
+  fs.AppendPathComponent("bin");
+  fs.AppendPathComponent(k_module_file);
+  return fs;
+}
+
+FileSpec GetUuidView(FileSpec spec) {
+  spec.AppendPathComponent(k_platform_dir);
+  spec.AppendPathComponent(k_cache_dir);
+  spec.AppendPathComponent(k_module_uuid);
+  spec.AppendPathComponent(k_module_file);
+  return spec;
+}
+
+void BuildEmptyCacheDir(const FileSpec _dir) {
+  FileSpec cache_dir(test_dir);
+  cache_dir.AppendPathComponent(k_platform_dir);
+  cache_dir.AppendPathComponent(k_cache_dir);
+  std::error_code ec = llvm::sys::fs::create_directories(cache_dir.GetPath());
+  EXPECT_FALSE(ec);
+}
+

[Lldb-commits] [PATCH] D153733: [lldb][TargetGetModuleCallback] Update SBFileSpec/SBModuleSpec

2023-07-11 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 539207.
splhack added a comment.

sync with D153734  and D153735 
 changes


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153733

Files:
  lldb/bindings/python/python-swigsafecast.swig
  lldb/include/lldb/API/SBModuleSpec.h
  lldb/source/API/SBModuleSpec.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/test/API/python_api/module_spec/TestModuleSpec.py

Index: lldb/test/API/python_api/module_spec/TestModuleSpec.py
===
--- /dev/null
+++ lldb/test/API/python_api/module_spec/TestModuleSpec.py
@@ -0,0 +1,24 @@
+"""
+Test some SBModuleSpec APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class ModuleSpecAPIsTestCase(TestBase):
+def test_object_offset_and_size(self):
+module_spec = lldb.SBModuleSpec()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
+
+module_spec.SetObjectOffset(4096)
+self.assertEqual(module_spec.GetObjectOffset(), 4096)
+
+module_spec.SetObjectSize(3600)
+self.assertEqual(module_spec.GetObjectSize(), 3600)
+
+module_spec.Clear()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
Index: lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -30,6 +30,8 @@
 class SBValue;
 class SBStream;
 class SBStructuredData;
+class SBFileSpec;
+class SBModuleSpec;
 } // namespace lldb
 
 namespace lldb_private {
@@ -102,6 +104,10 @@
   static PythonObject ToSWIGWrapper(std::unique_ptr stream_sb);
   static PythonObject
   ToSWIGWrapper(std::unique_ptr data_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr file_spec_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr module_spec_sb);
 
   static python::ScopedPythonObject
   ToSWIGWrapper(CommandReturnObject _retobj);
Index: lldb/source/API/SBModuleSpec.cpp
===
--- lldb/source/API/SBModuleSpec.cpp
+++ lldb/source/API/SBModuleSpec.cpp
@@ -29,6 +29,11 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
+SBModuleSpec::SBModuleSpec(const lldb_private::ModuleSpec _spec)
+: m_opaque_up(new lldb_private::ModuleSpec(module_spec)) {
+  LLDB_INSTRUMENT_VA(this, module_spec);
+}
+
 const SBModuleSpec ::operator=(const SBModuleSpec ) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
@@ -145,6 +150,30 @@
   return true;
 }
 
+uint64_t SBModuleSpec::GetObjectOffset() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectOffset();
+}
+
+void SBModuleSpec::SetObjectOffset(uint64_t object_offset) {
+  LLDB_INSTRUMENT_VA(this, object_offset);
+
+  m_opaque_up->SetObjectOffset(object_offset);
+}
+
+uint64_t SBModuleSpec::GetObjectSize() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectSize();
+}
+
+void SBModuleSpec::SetObjectSize(uint64_t object_size) {
+  LLDB_INSTRUMENT_VA(this, object_size);
+
+  m_opaque_up->SetObjectSize(object_size);
+}
+
 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
   LLDB_INSTRUMENT_VA(this);
 }
Index: lldb/include/lldb/API/SBModuleSpec.h
===
--- lldb/include/lldb/API/SBModuleSpec.h
+++ lldb/include/lldb/API/SBModuleSpec.h
@@ -77,13 +77,24 @@
 
   bool SetUUIDBytes(const uint8_t *uuid, size_t uuid_len);
 
+  uint64_t GetObjectOffset();
+
+  void SetObjectOffset(uint64_t object_offset);
+
+  uint64_t GetObjectSize();
+
+  void SetObjectSize(uint64_t object_size);
+
   bool GetDescription(lldb::SBStream );
 
 private:
   friend class SBModuleSpecList;
   friend class SBModule;
+  friend class SBPlatform;
   friend class SBTarget;
 
+  SBModuleSpec(const lldb_private::ModuleSpec _spec);
+
   std::unique_ptr m_opaque_up;
 };
 
Index: lldb/bindings/python/python-swigsafecast.swig
===
--- lldb/bindings/python/python-swigsafecast.swig
+++ lldb/bindings/python/python-swigsafecast.swig
@@ -120,5 +120,15 @@
SWIGTYPE_p_lldb__SBEvent);
 }
 
+PythonObject SWIGBridge::ToSWIGWrapper(
+std::unique_ptr file_spec_sb) {
+  return ToSWIGHelper(file_spec_sb.release(), SWIGTYPE_p_lldb__SBFileSpec);
+}
+
+PythonObject SWIGBridge::ToSWIGWrapper(
+std::unique_ptr module_spec_sb) {
+  return ToSWIGHelper(module_spec_sb.release(), SWIGTYPE_p_lldb__SBModuleSpec);
+}
+
 } // namespace python
 } // namespace lldb_private

[Lldb-commits] [PATCH] D153735: [lldb][TargetGetModuleCallback] Implement Python interface

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/include/lldb/API/SBDefines.h:129
 
+typedef SBError (*SBTargetGetModuleCallback)(SBDebugger debugger,
+ SBModuleSpec _spec,

clayborg wrote:
> If we are putting this into SBPlatform, this should probably be named 
> "SBPlatformGetModuleCallback". 
> 
> The name might be a bit more clear if it was 
> "SBPlatformLocateModuleCallback"? Open to suggestions or fine to lave this as 
> "SBPlatformGetModuleCallback" if everyone likes that.
As commented on D153734, I'll rename it to `SBPlatformLocateModuleCallback`.

```
typedef SBError (*SBPlatformLocateModuleCallback)(
SBDebugger debugger,
SBModuleSpec _spec,
SBFileSpec _file_spec,
SBFileSpec _file_spec);
```



Comment at: lldb/include/lldb/API/SBPlatform.h:181
+  /// nullptr or None is set.
+  SBError SetTargetGetModuleCallback(lldb::SBTargetGetModuleCallback callback,
+ void *callback_baton);

clayborg wrote:
> remove "Target" from the function name? Or rename to 
> "SetLocateModuleCallback(...)" if we end up renaming the callback type to 
> SBPlatformLocateModuleCallback
I'll rename it to `SetLocateModuleCallback`.
```
SBError SetLocateModuleCallback(lldb::SBPlatformLocateModuleCallback callback,
   void *callback_baton);
```



Comment at: lldb/source/API/SBPlatform.cpp:668
+// Platform. 'callback_baton' is the actual callback Python callable 
object.
+platform_sp->SetTargetGetModuleCallback(callback_baton);
+return SBError();

clayborg wrote:
> You are not passing the "callback" into Platform::SetTargetGetModuleCallback??
> 
> Are we missing changes to the file "lldb/include/lldb/Target/Platform.h"? 
will update it to support C callback function as well as Python.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

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


[Lldb-commits] [PATCH] D153734: [lldb][TargetGetModuleCallback] Call get module callback

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@clayborg 
Sure, I'll rename the type and function to `Locate` instead of `Get`.

  typedef SBError (*SBPlatformLocateModuleCallback)(
  SBDebugger debugger,
  SBModuleSpec _spec,
  SBFileSpec _file_spec,
  SBFileSpec _file_spec);

This `SBPlatformLocateModuleCallback` typedef is required for the SWIG type 
conversion.

However it will introduce the SB* dependencies to `Platform.h` if we use the 
type. Is it ok?

  void SetLocateModuleCallback(SBPlatformLocateModuleCallback callback, void 
*baton);

I think `Platform.h` should use `void *` instead of 
`SBPlatformLocateModuleCallback`. Is it correct?

  void SetLocateModuleCallback(void *callback, void *baton);




Comment at: lldb/source/Core/Debugger.cpp:2170-2181
+
+Status Debugger::CallTargetGetModuleCallback(
+void *target_get_module_callback_baton, const ModuleSpec _spec,
+FileSpec _file_spec, FileSpec _file_spec) {
+  ScriptInterpreter *script_interpreter = GetScriptInterpreter();
+  if (!script_interpreter)
+return Status("Cannot find ScriptInterpreter");

clayborg wrote:
> I don't think we need any of this in debugger. when someone, through python 
> calls the "SetTargetGetModuleCallback" that code should register a static 
> function as the callback to use that will end up calling into the script 
> interpreter.
> 
> We don't always want to call the script interpreter for all cases. Users 
> should be able to register a native callback for this, and it shouldn't force 
> us to always use the script interpreter.
> 
There was another reason why `Debugger` has this method. The reason was that 
`Debugger` is easily `gMock`-able but `Target` is not.
Please take a look at `MockDebugger` in 
`lldb/unittests/Target/GetModuleCallbackTest.cpp` in this diff.

I will update this diff to allow C function callback aside from Python 
function, but probably we need to keep this kind of method outside `Target` for 
`gMock` to write unit tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

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


[Lldb-commits] [PATCH] D153735: [lldb][TargetGetModuleCallback] Implement Python interface

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538835.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBPlatform.h
  lldb/source/API/SBPlatform.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/API/python_api/target/TestGetModuleCallback.py
  lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Index: lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
===
--- lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -321,3 +321,11 @@
 lldb_private::python::SWIGBridge::ToSWIGWrapper(lldb::DataExtractorSP) {
   return python::PythonObject();
 }
+
+Status
+lldb_private::python::SWIGBridge::LLDBSWIGPythonCallTargetGetModuleCallback(
+void *callback_baton, lldb::DebuggerSP debugger_sp,
+const ModuleSpec _spec, FileSpec _file_spec,
+FileSpec _file_spec) {
+  return Status();
+}
Index: lldb/test/API/python_api/target/TestGetModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/target/TestGetModuleCallback.py
@@ -0,0 +1,303 @@
+"""
+Test Target get module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class GetModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.platform = self.dbg.GetSelectedPlatform()
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.platform.SetTargetGetModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 4 argument.
+def test_args3(a, b, c):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 4 argument callable object"):
+self.platform.SetTargetGetModuleCallback(test_args3)
+
+def test_set_none(self):
+# SetTargetGetModuleCallback should succeed to clear the callback with None.
+

[Lldb-commits] [PATCH] D153734: [lldb][TargetGetModuleCallback] Call get module callback

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538741.
splhack added a comment.

Fix comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

Files:
  lldb/include/lldb/Core/Debugger.h
  lldb/include/lldb/Interpreter/ScriptInterpreter.h
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Core/Debugger.cpp
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/CMakeLists.txt
  lldb/unittests/Target/GetModuleCallbackTest.cpp
  lldb/unittests/Target/Inputs/AndroidModule.c
  lldb/unittests/Target/Inputs/AndroidModule.so
  lldb/unittests/Target/Inputs/AndroidModule.so.sym
  lldb/unittests/Target/Inputs/AndroidModule.unstripped.so

Index: lldb/unittests/Target/Inputs/AndroidModule.so.sym
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.so.sym
@@ -0,0 +1,21 @@
+MODULE Linux arm64 38830080A082E5515922C905D23890DA0 AndroidModule.so
+INFO CODE_ID 8000833882A051E55922C905D23890DABDDEFECC
+FILE 0 /private/tmp/test/AndroidModule.c
+FUNC 162c 8 0 boom
+162c 8 8 0
+FUNC 1634 8 0 boom_hidden
+1634 8 12 0
+PUBLIC 15cc 0 __on_dlclose
+PUBLIC 15dc 0 __emutls_unregister_key
+PUBLIC 15e4 0 __on_dlclose_late
+PUBLIC 15ec 0 __atexit_handler_wrapper
+PUBLIC 1600 0 atexit
+PUBLIC 161c 0 pthread_atfork
+STACK CFI INIT 15cc 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15dc 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15e4 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15ec 14 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1600 1c .cfa: sp 0 + .ra: x30
+STACK CFI INIT 161c 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 162c 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1634 8 .cfa: sp 0 + .ra: x30
Index: lldb/unittests/Target/Inputs/AndroidModule.c
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.c
@@ -0,0 +1,13 @@
+// aarch64-linux-android29-clang -shared -Os -glldb -g3 -Wl,--build-id=sha1 \
+// AndroidModule.c -o AndroidModule.so
+// dump_syms AndroidModule.so > AndroidModule.so.sym
+// cp AndroidModule.so AndroidModule.unstripped.so
+// llvm-strip --strip-unneeded AndroidModule.so
+
+int boom(void) {
+  return 47;
+}
+
+__attribute__((visibility("hidden"))) int boom_hidden(void) {
+  return 48;
+}
Index: lldb/unittests/Target/GetModuleCallbackTest.cpp
===
--- /dev/null
+++ lldb/unittests/Target/GetModuleCallbackTest.cpp
@@ -0,0 +1,675 @@
+//===-- GetModuleCallbackTest.cpp -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
+#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Target.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace lldb_private::breakpad;
+using namespace testing;
+
+namespace {
+
+constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
+constexpr llvm::StringLiteral k_platform_dir("remote-android");
+constexpr llvm::StringLiteral k_cache_dir(".cache");
+constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
+constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
+constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
+constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
+constexpr llvm::StringLiteral
+k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
+constexpr llvm::StringLiteral k_function_symbol("boom");
+constexpr llvm::StringLiteral k_hidden_function_symbol("boom_hidden");
+const size_t k_module_size = 3784;
+
+class MockDebugger : public Debugger {
+public:
+  MockDebugger() : Debugger(nullptr, nullptr) {}
+
+  MOCK_METHOD4(CallTargetGetModuleCallback,
+   Status(void *, const ModuleSpec &, FileSpec &, FileSpec &));
+};
+
+ModuleSpec GetTestModuleSpec();
+
+class MockProcess : public Process {
+public:
+  MockProcess(TargetSP target_sp, ListenerSP listener_sp)
+  : Process(target_sp, listener_sp) {}
+
+  llvm::StringRef GetPluginName() override { return k_process_plugin; };
+
+  bool CanDebug(TargetSP target, bool 

[Lldb-commits] [PATCH] D153735: [lldb][TargetGetModuleCallback] Implement Python interface

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538713.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBPlatform.h
  lldb/source/API/SBPlatform.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/API/python_api/target/TestGetModuleCallback.py
  lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Index: lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
===
--- lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -321,3 +321,11 @@
 lldb_private::python::SWIGBridge::ToSWIGWrapper(lldb::DataExtractorSP) {
   return python::PythonObject();
 }
+
+Status
+lldb_private::python::SWIGBridge::LLDBSWIGPythonCallTargetGetModuleCallback(
+void *callback_baton, lldb::DebuggerSP debugger_sp,
+const ModuleSpec _spec, FileSpec _file_spec,
+FileSpec _file_spec) {
+  return Status();
+}
Index: lldb/test/API/python_api/target/TestGetModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/target/TestGetModuleCallback.py
@@ -0,0 +1,303 @@
+"""
+Test Target get module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class GetModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.platform = self.dbg.GetSelectedPlatform()
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.platform.SetTargetGetModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 4 argument.
+def test_args3(a, b, c):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 4 argument callable object"):
+self.platform.SetTargetGetModuleCallback(test_args3)
+
+def test_set_none(self):
+# SetTargetGetModuleCallback should succeed to clear the callback with None.
+

[Lldb-commits] [PATCH] D153734: [lldb][TargetGetModuleCallback] Call get module callback

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538696.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

Files:
  lldb/include/lldb/Core/Debugger.h
  lldb/include/lldb/Interpreter/ScriptInterpreter.h
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Core/Debugger.cpp
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/CMakeLists.txt
  lldb/unittests/Target/GetModuleCallbackTest.cpp
  lldb/unittests/Target/Inputs/AndroidModule.c
  lldb/unittests/Target/Inputs/AndroidModule.so
  lldb/unittests/Target/Inputs/AndroidModule.so.sym
  lldb/unittests/Target/Inputs/AndroidModule.unstripped.so

Index: lldb/unittests/Target/Inputs/AndroidModule.so.sym
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.so.sym
@@ -0,0 +1,21 @@
+MODULE Linux arm64 38830080A082E5515922C905D23890DA0 AndroidModule.so
+INFO CODE_ID 8000833882A051E55922C905D23890DABDDEFECC
+FILE 0 /private/tmp/test/AndroidModule.c
+FUNC 162c 8 0 boom
+162c 8 8 0
+FUNC 1634 8 0 boom_hidden
+1634 8 12 0
+PUBLIC 15cc 0 __on_dlclose
+PUBLIC 15dc 0 __emutls_unregister_key
+PUBLIC 15e4 0 __on_dlclose_late
+PUBLIC 15ec 0 __atexit_handler_wrapper
+PUBLIC 1600 0 atexit
+PUBLIC 161c 0 pthread_atfork
+STACK CFI INIT 15cc 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15dc 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15e4 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15ec 14 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1600 1c .cfa: sp 0 + .ra: x30
+STACK CFI INIT 161c 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 162c 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1634 8 .cfa: sp 0 + .ra: x30
Index: lldb/unittests/Target/Inputs/AndroidModule.c
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.c
@@ -0,0 +1,13 @@
+// aarch64-linux-android29-clang -shared -Os -glldb -g3 -Wl,--build-id=sha1 \
+// AndroidModule.c -o AndroidModule.so
+// dump_syms AndroidModule.so > AndroidModule.so.sym
+// cp AndroidModule.so AndroidModule.unstripped.so
+// llvm-strip --strip-unneeded AndroidModule.so
+
+int boom(void) {
+  return 47;
+}
+
+__attribute__((visibility("hidden"))) int boom_hidden(void) {
+  return 48;
+}
Index: lldb/unittests/Target/GetModuleCallbackTest.cpp
===
--- /dev/null
+++ lldb/unittests/Target/GetModuleCallbackTest.cpp
@@ -0,0 +1,675 @@
+//===-- GetModuleCallbackTest.cpp -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
+#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Target.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace lldb_private::breakpad;
+using namespace testing;
+
+namespace {
+
+constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
+constexpr llvm::StringLiteral k_platform_dir("remote-android");
+constexpr llvm::StringLiteral k_cache_dir(".cache");
+constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
+constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
+constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
+constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
+constexpr llvm::StringLiteral
+k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
+constexpr llvm::StringLiteral k_function_symbol("boom");
+constexpr llvm::StringLiteral k_hidden_function_symbol("boom_hidden");
+const size_t k_module_size = 3784;
+
+class MockDebugger : public Debugger {
+public:
+  MockDebugger() : Debugger(nullptr, nullptr) {}
+
+  MOCK_METHOD4(CallTargetGetModuleCallback,
+   Status(void *, const ModuleSpec &, FileSpec &, FileSpec &));
+};
+
+ModuleSpec GetTestModuleSpec();
+
+class MockProcess : public Process {
+public:
+  MockProcess(TargetSP target_sp, ListenerSP listener_sp)
+  : Process(target_sp, listener_sp) {}
+
+  llvm::StringRef GetPluginName() override { return k_process_plugin; };
+
+  bool CanDebug(TargetSP target, bool plugin_specified_by_name) 

[Lldb-commits] [PATCH] D153733: [lldb][TargetGetModuleCallback] Update SBFileSpec/SBModuleSpec

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538662.
splhack added a comment.

fix LLDB_INSTRUMENT_VA for SBModuleSpec constructor too


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153733

Files:
  lldb/bindings/python/python-swigsafecast.swig
  lldb/include/lldb/API/SBFileSpec.h
  lldb/include/lldb/API/SBModuleSpec.h
  lldb/source/API/SBModuleSpec.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/test/API/python_api/module_spec/TestModuleSpec.py

Index: lldb/test/API/python_api/module_spec/TestModuleSpec.py
===
--- /dev/null
+++ lldb/test/API/python_api/module_spec/TestModuleSpec.py
@@ -0,0 +1,24 @@
+"""
+Test some SBModuleSpec APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class ModuleSpecAPIsTestCase(TestBase):
+def test_object_offset_and_size(self):
+module_spec = lldb.SBModuleSpec()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
+
+module_spec.SetObjectOffset(4096)
+self.assertEqual(module_spec.GetObjectOffset(), 4096)
+
+module_spec.SetObjectSize(3600)
+self.assertEqual(module_spec.GetObjectSize(), 3600)
+
+module_spec.Clear()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
Index: lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -30,6 +30,8 @@
 class SBValue;
 class SBStream;
 class SBStructuredData;
+class SBFileSpec;
+class SBModuleSpec;
 } // namespace lldb
 
 namespace lldb_private {
@@ -102,6 +104,10 @@
   static PythonObject ToSWIGWrapper(std::unique_ptr stream_sb);
   static PythonObject
   ToSWIGWrapper(std::unique_ptr data_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr file_spec_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr module_spec_sb);
 
   static python::ScopedPythonObject
   ToSWIGWrapper(CommandReturnObject _retobj);
Index: lldb/source/API/SBModuleSpec.cpp
===
--- lldb/source/API/SBModuleSpec.cpp
+++ lldb/source/API/SBModuleSpec.cpp
@@ -29,6 +29,11 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
+SBModuleSpec::SBModuleSpec(const lldb_private::ModuleSpec _spec)
+: m_opaque_up(new lldb_private::ModuleSpec(module_spec)) {
+  LLDB_INSTRUMENT_VA(this, module_spec);
+}
+
 const SBModuleSpec ::operator=(const SBModuleSpec ) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
@@ -145,6 +150,30 @@
   return true;
 }
 
+uint64_t SBModuleSpec::GetObjectOffset() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectOffset();
+}
+
+void SBModuleSpec::SetObjectOffset(uint64_t object_offset) {
+  LLDB_INSTRUMENT_VA(this, object_offset);
+
+  m_opaque_up->SetObjectOffset(object_offset);
+}
+
+uint64_t SBModuleSpec::GetObjectSize() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectSize();
+}
+
+void SBModuleSpec::SetObjectSize(uint64_t object_size) {
+  LLDB_INSTRUMENT_VA(this, object_size);
+
+  m_opaque_up->SetObjectSize(object_size);
+}
+
 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
   LLDB_INSTRUMENT_VA(this);
 }
Index: lldb/include/lldb/API/SBModuleSpec.h
===
--- lldb/include/lldb/API/SBModuleSpec.h
+++ lldb/include/lldb/API/SBModuleSpec.h
@@ -12,6 +12,12 @@
 #include "lldb/API/SBDefines.h"
 #include "lldb/API/SBFileSpec.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBModuleSpec {
@@ -77,6 +83,14 @@
 
   bool SetUUIDBytes(const uint8_t *uuid, size_t uuid_len);
 
+  uint64_t GetObjectOffset();
+
+  void SetObjectOffset(uint64_t object_offset);
+
+  uint64_t GetObjectSize();
+
+  void SetObjectSize(uint64_t object_size);
+
   bool GetDescription(lldb::SBStream );
 
 private:
@@ -84,6 +98,10 @@
   friend class SBModule;
   friend class SBTarget;
 
+  friend class lldb_private::python::SWIGBridge;
+
+  SBModuleSpec(const lldb_private::ModuleSpec _spec);
+
   std::unique_ptr m_opaque_up;
 };
 
Index: lldb/include/lldb/API/SBFileSpec.h
===
--- lldb/include/lldb/API/SBFileSpec.h
+++ lldb/include/lldb/API/SBFileSpec.h
@@ -11,6 +11,12 @@
 
 #include "lldb/API/SBDefines.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBFileSpec {
@@ -79,6 +85,8 @@
   friend class SBThread;
   

[Lldb-commits] [PATCH] D153733: [lldb][TargetGetModuleCallback] Update SBFileSpec/SBModuleSpec

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538660.
splhack added a comment.

Fix LLDB_INSTRUMENT_VA args


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153733

Files:
  lldb/bindings/python/python-swigsafecast.swig
  lldb/include/lldb/API/SBFileSpec.h
  lldb/include/lldb/API/SBModuleSpec.h
  lldb/source/API/SBModuleSpec.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/test/API/python_api/module_spec/TestModuleSpec.py

Index: lldb/test/API/python_api/module_spec/TestModuleSpec.py
===
--- /dev/null
+++ lldb/test/API/python_api/module_spec/TestModuleSpec.py
@@ -0,0 +1,24 @@
+"""
+Test some SBModuleSpec APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class ModuleSpecAPIsTestCase(TestBase):
+def test_object_offset_and_size(self):
+module_spec = lldb.SBModuleSpec()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
+
+module_spec.SetObjectOffset(4096)
+self.assertEqual(module_spec.GetObjectOffset(), 4096)
+
+module_spec.SetObjectSize(3600)
+self.assertEqual(module_spec.GetObjectSize(), 3600)
+
+module_spec.Clear()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
Index: lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -30,6 +30,8 @@
 class SBValue;
 class SBStream;
 class SBStructuredData;
+class SBFileSpec;
+class SBModuleSpec;
 } // namespace lldb
 
 namespace lldb_private {
@@ -102,6 +104,10 @@
   static PythonObject ToSWIGWrapper(std::unique_ptr stream_sb);
   static PythonObject
   ToSWIGWrapper(std::unique_ptr data_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr file_spec_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr module_spec_sb);
 
   static python::ScopedPythonObject
   ToSWIGWrapper(CommandReturnObject _retobj);
Index: lldb/source/API/SBModuleSpec.cpp
===
--- lldb/source/API/SBModuleSpec.cpp
+++ lldb/source/API/SBModuleSpec.cpp
@@ -29,6 +29,11 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
+SBModuleSpec::SBModuleSpec(const lldb_private::ModuleSpec _spec)
+: m_opaque_up(new lldb_private::ModuleSpec(module_spec)) {
+  LLDB_INSTRUMENT_VA(this);
+}
+
 const SBModuleSpec ::operator=(const SBModuleSpec ) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
@@ -145,6 +150,30 @@
   return true;
 }
 
+uint64_t SBModuleSpec::GetObjectOffset() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectOffset();
+}
+
+void SBModuleSpec::SetObjectOffset(uint64_t object_offset) {
+  LLDB_INSTRUMENT_VA(this, object_offset);
+
+  m_opaque_up->SetObjectOffset(object_offset);
+}
+
+uint64_t SBModuleSpec::GetObjectSize() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectSize();
+}
+
+void SBModuleSpec::SetObjectSize(uint64_t object_size) {
+  LLDB_INSTRUMENT_VA(this, object_size);
+
+  m_opaque_up->SetObjectSize(object_size);
+}
+
 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
   LLDB_INSTRUMENT_VA(this);
 }
Index: lldb/include/lldb/API/SBModuleSpec.h
===
--- lldb/include/lldb/API/SBModuleSpec.h
+++ lldb/include/lldb/API/SBModuleSpec.h
@@ -12,6 +12,12 @@
 #include "lldb/API/SBDefines.h"
 #include "lldb/API/SBFileSpec.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBModuleSpec {
@@ -77,6 +83,14 @@
 
   bool SetUUIDBytes(const uint8_t *uuid, size_t uuid_len);
 
+  uint64_t GetObjectOffset();
+
+  void SetObjectOffset(uint64_t object_offset);
+
+  uint64_t GetObjectSize();
+
+  void SetObjectSize(uint64_t object_size);
+
   bool GetDescription(lldb::SBStream );
 
 private:
@@ -84,6 +98,10 @@
   friend class SBModule;
   friend class SBTarget;
 
+  friend class lldb_private::python::SWIGBridge;
+
+  SBModuleSpec(const lldb_private::ModuleSpec _spec);
+
   std::unique_ptr m_opaque_up;
 };
 
Index: lldb/include/lldb/API/SBFileSpec.h
===
--- lldb/include/lldb/API/SBFileSpec.h
+++ lldb/include/lldb/API/SBFileSpec.h
@@ -11,6 +11,12 @@
 
 #include "lldb/API/SBDefines.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBFileSpec {
@@ -79,6 +85,8 @@
   friend class SBThread;
   friend class SBTrace;
 
+  friend class 

[Lldb-commits] [PATCH] D153733: [lldb][TargetGetModuleCallback] Update SBFileSpec/SBModuleSpec

2023-07-10 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 538649.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153733

Files:
  lldb/bindings/python/python-swigsafecast.swig
  lldb/include/lldb/API/SBFileSpec.h
  lldb/include/lldb/API/SBModuleSpec.h
  lldb/source/API/SBModuleSpec.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/test/API/python_api/module_spec/TestModuleSpec.py

Index: lldb/test/API/python_api/module_spec/TestModuleSpec.py
===
--- /dev/null
+++ lldb/test/API/python_api/module_spec/TestModuleSpec.py
@@ -0,0 +1,24 @@
+"""
+Test some SBModuleSpec APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class ModuleSpecAPIsTestCase(TestBase):
+def test_object_offset_and_size(self):
+module_spec = lldb.SBModuleSpec()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
+
+module_spec.SetObjectOffset(4096)
+self.assertEqual(module_spec.GetObjectOffset(), 4096)
+
+module_spec.SetObjectSize(3600)
+self.assertEqual(module_spec.GetObjectSize(), 3600)
+
+module_spec.Clear()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
Index: lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -30,6 +30,8 @@
 class SBValue;
 class SBStream;
 class SBStructuredData;
+class SBFileSpec;
+class SBModuleSpec;
 } // namespace lldb
 
 namespace lldb_private {
@@ -102,6 +104,10 @@
   static PythonObject ToSWIGWrapper(std::unique_ptr stream_sb);
   static PythonObject
   ToSWIGWrapper(std::unique_ptr data_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr file_spec_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr module_spec_sb);
 
   static python::ScopedPythonObject
   ToSWIGWrapper(CommandReturnObject _retobj);
Index: lldb/source/API/SBModuleSpec.cpp
===
--- lldb/source/API/SBModuleSpec.cpp
+++ lldb/source/API/SBModuleSpec.cpp
@@ -29,6 +29,11 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
+SBModuleSpec::SBModuleSpec(const lldb_private::ModuleSpec _spec)
+: m_opaque_up(new lldb_private::ModuleSpec(module_spec)) {
+  LLDB_INSTRUMENT_VA(this);
+}
+
 const SBModuleSpec ::operator=(const SBModuleSpec ) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
@@ -145,6 +150,30 @@
   return true;
 }
 
+uint64_t SBModuleSpec::GetObjectOffset() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectOffset();
+}
+
+void SBModuleSpec::SetObjectOffset(uint64_t object_offset) {
+  LLDB_INSTRUMENT_VA(this);
+
+  m_opaque_up->SetObjectOffset(object_offset);
+}
+
+uint64_t SBModuleSpec::GetObjectSize() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectSize();
+}
+
+void SBModuleSpec::SetObjectSize(uint64_t object_size) {
+  LLDB_INSTRUMENT_VA(this);
+
+  m_opaque_up->SetObjectSize(object_size);
+}
+
 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
   LLDB_INSTRUMENT_VA(this);
 }
Index: lldb/include/lldb/API/SBModuleSpec.h
===
--- lldb/include/lldb/API/SBModuleSpec.h
+++ lldb/include/lldb/API/SBModuleSpec.h
@@ -12,6 +12,12 @@
 #include "lldb/API/SBDefines.h"
 #include "lldb/API/SBFileSpec.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBModuleSpec {
@@ -77,6 +83,14 @@
 
   bool SetUUIDBytes(const uint8_t *uuid, size_t uuid_len);
 
+  uint64_t GetObjectOffset();
+
+  void SetObjectOffset(uint64_t object_offset);
+
+  uint64_t GetObjectSize();
+
+  void SetObjectSize(uint64_t object_size);
+
   bool GetDescription(lldb::SBStream );
 
 private:
@@ -84,6 +98,10 @@
   friend class SBModule;
   friend class SBTarget;
 
+  friend class lldb_private::python::SWIGBridge;
+
+  SBModuleSpec(const lldb_private::ModuleSpec _spec);
+
   std::unique_ptr m_opaque_up;
 };
 
Index: lldb/include/lldb/API/SBFileSpec.h
===
--- lldb/include/lldb/API/SBFileSpec.h
+++ lldb/include/lldb/API/SBFileSpec.h
@@ -11,6 +11,12 @@
 
 #include "lldb/API/SBDefines.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBFileSpec {
@@ -79,6 +85,8 @@
   friend class SBThread;
   friend class SBTrace;
 
+  friend class lldb_private::python::SWIGBridge;
+
   

[Lldb-commits] [PATCH] D153735: [lldb][TargetGetModuleCallback] Implement Python interface

2023-06-26 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 534804.
splhack added a comment.

Move SetTargetGetModuleCallback from SBDebugger to SBPlatform.
https://discourse.llvm.org/t/rfc-python-callback-for-target-get-module/71580/4?u=splhack


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDefines.h
  lldb/include/lldb/API/SBPlatform.h
  lldb/source/API/SBPlatform.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/API/python_api/target/TestGetModuleCallback.py
  lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Index: lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
===
--- lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -321,3 +321,11 @@
 lldb_private::python::SWIGBridge::ToSWIGWrapper(lldb::DataExtractorSP) {
   return python::PythonObject();
 }
+
+Status
+lldb_private::python::SWIGBridge::LLDBSWIGPythonCallTargetGetModuleCallback(
+void *callback_baton, lldb::DebuggerSP debugger_sp,
+const ModuleSpec _spec, FileSpec _file_spec,
+FileSpec _file_spec) {
+  return Status();
+}
Index: lldb/test/API/python_api/target/TestGetModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/target/TestGetModuleCallback.py
@@ -0,0 +1,303 @@
+"""
+Test Target get module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class GetModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.platform = self.dbg.GetSelectedPlatform()
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.platform.SetTargetGetModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 4 argument.
+def test_args3(a, b, c):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 4 argument callable object"):
+self.platform.SetTargetGetModuleCallback(test_args3)
+
+def 

[Lldb-commits] [PATCH] D153734: [lldb][TargetGetModuleCallback] Call get module callback

2023-06-26 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 534803.
splhack added a comment.

Move SetTargetGetModuleCallback from Debugger to Platform.
https://discourse.llvm.org/t/rfc-python-callback-for-target-get-module/71580/4?u=splhack


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153734

Files:
  lldb/include/lldb/Core/Debugger.h
  lldb/include/lldb/Interpreter/ScriptInterpreter.h
  lldb/include/lldb/Target/Platform.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Core/Debugger.cpp
  lldb/source/Target/Platform.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/CMakeLists.txt
  lldb/unittests/Target/GetModuleCallbackTest.cpp
  lldb/unittests/Target/Inputs/AndroidModule.c
  lldb/unittests/Target/Inputs/AndroidModule.so
  lldb/unittests/Target/Inputs/AndroidModule.so.sym
  lldb/unittests/Target/Inputs/AndroidModule.unstripped.so

Index: lldb/unittests/Target/Inputs/AndroidModule.so.sym
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.so.sym
@@ -0,0 +1,21 @@
+MODULE Linux arm64 38830080A082E5515922C905D23890DA0 AndroidModule.so
+INFO CODE_ID 8000833882A051E55922C905D23890DABDDEFECC
+FILE 0 /private/tmp/test/AndroidModule.c
+FUNC 162c 8 0 boom
+162c 8 8 0
+FUNC 1634 8 0 boom_hidden
+1634 8 12 0
+PUBLIC 15cc 0 __on_dlclose
+PUBLIC 15dc 0 __emutls_unregister_key
+PUBLIC 15e4 0 __on_dlclose_late
+PUBLIC 15ec 0 __atexit_handler_wrapper
+PUBLIC 1600 0 atexit
+PUBLIC 161c 0 pthread_atfork
+STACK CFI INIT 15cc 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15dc 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15e4 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15ec 14 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1600 1c .cfa: sp 0 + .ra: x30
+STACK CFI INIT 161c 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 162c 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1634 8 .cfa: sp 0 + .ra: x30
Index: lldb/unittests/Target/Inputs/AndroidModule.c
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.c
@@ -0,0 +1,13 @@
+// aarch64-linux-android29-clang -shared -Os -glldb -g3 -Wl,--build-id=sha1 \
+// AndroidModule.c -o AndroidModule.so
+// dump_syms AndroidModule.so > AndroidModule.so.sym
+// cp AndroidModule.so AndroidModule.unstripped.so
+// llvm-strip --strip-unneeded AndroidModule.so
+
+int boom(void) {
+  return 47;
+}
+
+__attribute__((visibility("hidden"))) int boom_hidden(void) {
+  return 48;
+}
Index: lldb/unittests/Target/GetModuleCallbackTest.cpp
===
--- /dev/null
+++ lldb/unittests/Target/GetModuleCallbackTest.cpp
@@ -0,0 +1,675 @@
+//===-- GetModuleCallbackTest.cpp -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
+#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Target.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace lldb_private::breakpad;
+using namespace testing;
+
+namespace {
+
+constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
+constexpr llvm::StringLiteral k_platform_dir("remote-android");
+constexpr llvm::StringLiteral k_cache_dir(".cache");
+constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
+constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
+constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
+constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
+constexpr llvm::StringLiteral
+k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
+constexpr llvm::StringLiteral k_function_symbol("boom");
+constexpr llvm::StringLiteral k_hidden_function_symbol("boom_hidden");
+const size_t k_module_size = 3784;
+
+class MockDebugger : public Debugger {
+public:
+  MockDebugger() : Debugger(nullptr, nullptr) {}
+
+  MOCK_METHOD4(CallTargetGetModuleCallback,
+   Status(void *, const ModuleSpec &, FileSpec &, FileSpec &));
+};
+
+ModuleSpec GetTestModuleSpec();
+
+class MockProcess : public Process {
+public:
+  MockProcess(TargetSP target_sp, ListenerSP listener_sp)
+  : Process(target_sp, listener_sp) {}
+
+  

[Lldb-commits] [PATCH] D153735: [lldb][TargetGetModuleCallback] Implement Python interface

2023-06-25 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

RFC https://discourse.llvm.org/t/rfc-python-callback-for-target-get-module/71580

Use SWIG for the target get module callback the same as other Python callbacks.
TestGetModuleCallback.py verifies the functionalities.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153735

Files:
  lldb/bindings/python/python-typemaps.swig
  lldb/bindings/python/python-wrapper.swig
  lldb/include/lldb/API/SBDebugger.h
  lldb/include/lldb/API/SBDefines.h
  lldb/source/API/SBDebugger.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/API/python_api/target/TestGetModuleCallback.py
  lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp

Index: lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
===
--- lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
+++ lldb/unittests/ScriptInterpreter/Python/PythonTestSuite.cpp
@@ -321,3 +321,11 @@
 lldb_private::python::SWIGBridge::ToSWIGWrapper(lldb::DataExtractorSP) {
   return python::PythonObject();
 }
+
+Status
+lldb_private::python::SWIGBridge::LLDBSWIGPythonCallTargetGetModuleCallback(
+void *callback_baton, lldb::DebuggerSP debugger_sp,
+const ModuleSpec _spec, FileSpec _file_spec,
+FileSpec _file_spec) {
+  return Status();
+}
Index: lldb/test/API/python_api/target/TestGetModuleCallback.py
===
--- /dev/null
+++ lldb/test/API/python_api/target/TestGetModuleCallback.py
@@ -0,0 +1,288 @@
+"""
+Test Target get module callback functionality
+"""
+
+import ctypes
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from pathlib import Path
+
+import lldb
+
+UNITTESTS_TARGET_INPUTS_PATH = "../../../../unittests/Target/Inputs"
+MODULE_PLATFORM_PATH = "/system/lib64/AndroidModule.so"
+MODULE_TRIPLE = "aarch64-none-linux"
+MODULE_RESOLVED_TRIPLE = "aarch64--linux-android"
+MODULE_UUID = "80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC"
+MODULE_FUNCTION = "boom"
+MODULE_HIDDEN_FUNCTION = "boom_hidden"
+MODULE_FILE = "AndroidModule.so"
+MODULE_NON_EXISTENT_FILE = "non-existent-file"
+SYMBOL_FILE = "AndroidModule.unstripped.so"
+BREAKPAD_SYMBOL_FILE = "AndroidModule.so.sym"
+SYMBOL_STRIPPED = "stripped"
+SYMBOL_UNSTRIPPED = "unstripped"
+
+
+class GetModuleCallbackTestCase(TestBase):
+def setUp(self):
+TestBase.setUp(self)
+self.target = self.dbg.CreateTarget("")
+self.assertTrue(self.target)
+
+self.input_dir = (
+Path(self.getSourceDir()) / UNITTESTS_TARGET_INPUTS_PATH
+).resolve()
+self.assertTrue(self.input_dir.is_dir())
+
+def check_module_spec(self, module_spec: lldb.SBModuleSpec):
+self.assertEqual(
+MODULE_UUID.replace("-", ""),
+ctypes.string_at(
+int(module_spec.GetUUIDBytes()),
+module_spec.GetUUIDLength(),
+)
+.hex()
+.upper(),
+)
+
+self.assertEqual(MODULE_TRIPLE, module_spec.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module_spec.GetFileSpec().fullpath)
+
+def check_module(self, module: lldb.SBModule, symbol_file: str, symbol_kind: str):
+self.assertTrue(module.IsValid())
+
+self.assertEqual(
+MODULE_UUID,
+module.GetUUIDString(),
+)
+
+self.assertEqual(MODULE_RESOLVED_TRIPLE, module.GetTriple())
+
+self.assertEqual(MODULE_PLATFORM_PATH, module.GetPlatformFileSpec().fullpath)
+
+self.assertEqual(
+str(self.input_dir / MODULE_FILE),
+module.GetFileSpec().fullpath,
+)
+
+self.assertEqual(
+str(self.input_dir / symbol_file),
+module.GetSymbolFileSpec().fullpath,
+)
+
+sc_list = module.FindFunctions(MODULE_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(1, sc_list.GetSize())
+sc_list = module.FindFunctions(MODULE_HIDDEN_FUNCTION, lldb.eSymbolTypeCode)
+self.assertEqual(0 if symbol_kind == SYMBOL_STRIPPED else 1, sc_list.GetSize())
+
+def test_set_non_callable(self):
+# The callback should be callable.
+non_callable = "a"
+
+with self.assertRaises(TypeError, msg="Need a callable object or None!"):
+self.dbg.SetTargetGetModuleCallback(non_callable)
+
+def test_set_wrong_args(self):
+# The callback should accept 4 argument.
+def test_args3(a, b, c):
+pass
+
+with self.assertRaises(TypeError, msg="Expected 4 argument callable object"):
+

[Lldb-commits] [PATCH] D153734: [lldb][TargetGetModuleCallback] Call get module callback

2023-06-25 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

RFC https://discourse.llvm.org/t/rfc-python-callback-for-target-get-module/71580

Updated Target::GetOrCreateModule to call get module callback if set.

- include/lldb/Core/Debugger.h, source/Core/Debugger.cpp
  - Changed the constructor access from private to protected for unittest mock.
  - Implemented SetTargetGetModuleCallback and CallTargetGetModuleCallbackIfSet

- include/lldb/Interpreter/ScriptInterpreter.h
  - Added CallTargetGetModuleCallback as the script interpreter interface.

- include/lldb/Target/Target.h, source/Target/Target.cpp
  - Implemented CallGetModuleCallbackIfSet.

- unittests/Target/GetModuleCallbackTest.cpp
  - Added comprehensive GetOrCreateModule tests with Debugger mock.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153734

Files:
  lldb/include/lldb/Core/Debugger.h
  lldb/include/lldb/Interpreter/ScriptInterpreter.h
  lldb/include/lldb/Target/Target.h
  lldb/source/Core/Debugger.cpp
  lldb/source/Target/Target.cpp
  lldb/unittests/Target/CMakeLists.txt
  lldb/unittests/Target/GetModuleCallbackTest.cpp
  lldb/unittests/Target/Inputs/AndroidModule.c
  lldb/unittests/Target/Inputs/AndroidModule.so
  lldb/unittests/Target/Inputs/AndroidModule.so.sym
  lldb/unittests/Target/Inputs/AndroidModule.unstripped.so

Index: lldb/unittests/Target/Inputs/AndroidModule.so.sym
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.so.sym
@@ -0,0 +1,21 @@
+MODULE Linux arm64 38830080A082E5515922C905D23890DA0 AndroidModule.so
+INFO CODE_ID 8000833882A051E55922C905D23890DABDDEFECC
+FILE 0 /private/tmp/test/AndroidModule.c
+FUNC 162c 8 0 boom
+162c 8 8 0
+FUNC 1634 8 0 boom_hidden
+1634 8 12 0
+PUBLIC 15cc 0 __on_dlclose
+PUBLIC 15dc 0 __emutls_unregister_key
+PUBLIC 15e4 0 __on_dlclose_late
+PUBLIC 15ec 0 __atexit_handler_wrapper
+PUBLIC 1600 0 atexit
+PUBLIC 161c 0 pthread_atfork
+STACK CFI INIT 15cc 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15dc 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15e4 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 15ec 14 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1600 1c .cfa: sp 0 + .ra: x30
+STACK CFI INIT 161c 10 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 162c 8 .cfa: sp 0 + .ra: x30
+STACK CFI INIT 1634 8 .cfa: sp 0 + .ra: x30
Index: lldb/unittests/Target/Inputs/AndroidModule.c
===
--- /dev/null
+++ lldb/unittests/Target/Inputs/AndroidModule.c
@@ -0,0 +1,13 @@
+// aarch64-linux-android29-clang -shared -Os -glldb -g3 -Wl,--build-id=sha1 \
+// AndroidModule.c -o AndroidModule.so
+// dump_syms AndroidModule.so > AndroidModule.so.sym
+// cp AndroidModule.so AndroidModule.unstripped.so
+// llvm-strip --strip-unneeded AndroidModule.so
+
+int boom(void) {
+  return 47;
+}
+
+__attribute__((visibility("hidden"))) int boom_hidden(void) {
+  return 48;
+}
Index: lldb/unittests/Target/GetModuleCallbackTest.cpp
===
--- /dev/null
+++ lldb/unittests/Target/GetModuleCallbackTest.cpp
@@ -0,0 +1,663 @@
+//===-- GetModuleCallbackTest.cpp -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
+#include "Plugins/ObjectFile/ELF/ObjectFileELF.h"
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
+#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Core/Debugger.h"
+#include "lldb/Core/PluginManager.h"
+#include "lldb/Host/HostInfo.h"
+#include "lldb/Target/Target.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace lldb_private::breakpad;
+using namespace testing;
+
+namespace {
+
+constexpr llvm::StringLiteral k_process_plugin("mock-process-plugin");
+constexpr llvm::StringLiteral k_platform_dir("remote-android");
+constexpr llvm::StringLiteral k_cache_dir(".cache");
+constexpr llvm::StringLiteral k_module_file("AndroidModule.so");
+constexpr llvm::StringLiteral k_symbol_file("AndroidModule.unstripped.so");
+constexpr llvm::StringLiteral k_breakpad_symbol_file("AndroidModule.so.sym");
+constexpr llvm::StringLiteral k_arch("aarch64-none-linux");
+constexpr llvm::StringLiteral
+k_module_uuid("80008338-82A0-51E5-5922-C905D23890DA-BDDEFECC");
+constexpr llvm::StringLiteral k_function_symbol("boom");

[Lldb-commits] [PATCH] D153733: [lldb][TargetGetModuleCallback] Update SBFileSpec/SBModuleSpec

2023-06-25 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

RFC https://discourse.llvm.org/t/rfc-python-callback-for-target-get-module/71580

SBFileSpec and SBModuleSpec will be used for Target get module callback as
Python function arguments. This diff allows these things.

- Can be instantiated from lldb_private::python::SWIGBridge.
- Can be passed to/from Python.
- Can be accessed for object offset and size.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153733

Files:
  lldb/bindings/python/python-swigsafecast.swig
  lldb/include/lldb/API/SBFileSpec.h
  lldb/include/lldb/API/SBModuleSpec.h
  lldb/source/API/SBModuleSpec.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
  lldb/test/API/python_api/module_spec/TestModuleSpec.py

Index: lldb/test/API/python_api/module_spec/TestModuleSpec.py
===
--- /dev/null
+++ lldb/test/API/python_api/module_spec/TestModuleSpec.py
@@ -0,0 +1,24 @@
+"""
+Test some SBModuleSpec APIs.
+"""
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+
+class ModuleSpecAPIsTestCase(TestBase):
+def test_object_offset_and_size(self):
+module_spec = lldb.SBModuleSpec()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
+
+module_spec.SetObjectOffset(4096)
+self.assertEqual(module_spec.GetObjectOffset(), 4096)
+
+module_spec.SetObjectSize(3600)
+self.assertEqual(module_spec.GetObjectSize(), 3600)
+
+module_spec.Clear()
+self.assertEqual(module_spec.GetObjectOffset(), 0)
+self.assertEqual(module_spec.GetObjectSize(), 0)
Index: lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/SWIGPythonBridge.h
@@ -30,6 +30,8 @@
 class SBValue;
 class SBStream;
 class SBStructuredData;
+class SBFileSpec;
+class SBModuleSpec;
 } // namespace lldb
 
 namespace lldb_private {
@@ -102,6 +104,10 @@
   static PythonObject ToSWIGWrapper(std::unique_ptr stream_sb);
   static PythonObject
   ToSWIGWrapper(std::unique_ptr data_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr file_spec_sb);
+  static PythonObject
+  ToSWIGWrapper(std::unique_ptr module_spec_sb);
 
   static python::ScopedPythonObject
   ToSWIGWrapper(CommandReturnObject _retobj);
Index: lldb/source/API/SBModuleSpec.cpp
===
--- lldb/source/API/SBModuleSpec.cpp
+++ lldb/source/API/SBModuleSpec.cpp
@@ -29,6 +29,11 @@
   m_opaque_up = clone(rhs.m_opaque_up);
 }
 
+SBModuleSpec::SBModuleSpec(const lldb_private::ModuleSpec _spec)
+: m_opaque_up(new lldb_private::ModuleSpec(module_spec)) {
+  LLDB_INSTRUMENT_VA(this);
+}
+
 const SBModuleSpec ::operator=(const SBModuleSpec ) {
   LLDB_INSTRUMENT_VA(this, rhs);
 
@@ -145,6 +150,30 @@
   return true;
 }
 
+uint64_t SBModuleSpec::GetObjectOffset() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectOffset();
+}
+
+void SBModuleSpec::SetObjectOffset(uint64_t object_offset) {
+  LLDB_INSTRUMENT_VA(this);
+
+  m_opaque_up->SetObjectOffset(object_offset);
+}
+
+uint64_t SBModuleSpec::GetObjectSize() {
+  LLDB_INSTRUMENT_VA(this);
+
+  return m_opaque_up->GetObjectSize();
+}
+
+void SBModuleSpec::SetObjectSize(uint64_t object_size) {
+  LLDB_INSTRUMENT_VA(this);
+
+  m_opaque_up->SetObjectSize(object_size);
+}
+
 SBModuleSpecList::SBModuleSpecList() : m_opaque_up(new ModuleSpecList()) {
   LLDB_INSTRUMENT_VA(this);
 }
Index: lldb/include/lldb/API/SBModuleSpec.h
===
--- lldb/include/lldb/API/SBModuleSpec.h
+++ lldb/include/lldb/API/SBModuleSpec.h
@@ -12,6 +12,12 @@
 #include "lldb/API/SBDefines.h"
 #include "lldb/API/SBFileSpec.h"
 
+namespace lldb_private {
+namespace python {
+class SWIGBridge;
+} // namespace python
+} // namespace lldb_private
+
 namespace lldb {
 
 class LLDB_API SBModuleSpec {
@@ -77,6 +83,14 @@
 
   bool SetUUIDBytes(const uint8_t *uuid, size_t uuid_len);
 
+  uint64_t GetObjectOffset();
+
+  void SetObjectOffset(uint64_t object_offset);
+
+  uint64_t GetObjectSize();
+
+  void SetObjectSize(uint64_t object_size);
+
   bool GetDescription(lldb::SBStream );
 
 private:
@@ -84,6 +98,10 @@
   friend class SBModule;
   friend class SBTarget;
 
+  friend class lldb_private::python::SWIGBridge;
+
+  SBModuleSpec(const lldb_private::ModuleSpec _spec);
+
   std::unique_ptr m_opaque_up;
 };
 
Index: lldb/include/lldb/API/SBFileSpec.h
===
--- lldb/include/lldb/API/SBFileSpec.h
+++ 

[Lldb-commits] [PATCH] D153390: [lldb][Windows] Fix ZipFileResolver tests

2023-06-22 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

verified, Windows buildbot is now green.
https://lab.llvm.org/buildbot/#/builders/219


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153390

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


[Lldb-commits] [PATCH] D153390: [lldb][Windows] Fix ZipFileResolver tests

2023-06-22 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG5c4071d22516: [lldb][Windows] Fix ZipFileResolver tests 
(authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153390

Files:
  lldb/source/Host/common/ZipFileResolver.cpp


Index: lldb/source/Host/common/ZipFileResolver.cpp
===
--- lldb/source/Host/common/ZipFileResolver.cpp
+++ lldb/source/Host/common/ZipFileResolver.cpp
@@ -25,6 +25,15 @@
   static constexpr llvm::StringLiteral k_zip_separator("!/");
   std::string path(file_spec.GetPath());
   size_t pos = path.find(k_zip_separator);
+
+#if defined(_WIN32)
+  // When the file_spec is resolved as a Windows path, the zip .so path will be
+  // "zip_path!\so_path". Support both patterns on Windows.
+  static constexpr llvm::StringLiteral k_zip_separator_win("!\\");
+  if (pos == std::string::npos)
+pos = path.find(k_zip_separator_win);
+#endif
+
   if (pos == std::string::npos) {
 // This file_spec does not contain the zip separator.
 // Treat this file_spec as a normal file.
@@ -40,6 +49,12 @@
   std::string zip_path(path.substr(0, pos));
   std::string so_path(path.substr(pos + k_zip_separator.size()));
 
+#if defined(_WIN32)
+  // Replace the .so path to use POSIX file separator for file searching inside
+  // the zip file.
+  std::replace(so_path.begin(), so_path.end(), '\\', '/');
+#endif
+
   // Try to find the .so file from the zip file.
   FileSpec zip_file_spec(zip_path);
   uint64_t zip_file_size = FileSystem::Instance().GetByteSize(zip_file_spec);


Index: lldb/source/Host/common/ZipFileResolver.cpp
===
--- lldb/source/Host/common/ZipFileResolver.cpp
+++ lldb/source/Host/common/ZipFileResolver.cpp
@@ -25,6 +25,15 @@
   static constexpr llvm::StringLiteral k_zip_separator("!/");
   std::string path(file_spec.GetPath());
   size_t pos = path.find(k_zip_separator);
+
+#if defined(_WIN32)
+  // When the file_spec is resolved as a Windows path, the zip .so path will be
+  // "zip_path!\so_path". Support both patterns on Windows.
+  static constexpr llvm::StringLiteral k_zip_separator_win("!\\");
+  if (pos == std::string::npos)
+pos = path.find(k_zip_separator_win);
+#endif
+
   if (pos == std::string::npos) {
 // This file_spec does not contain the zip separator.
 // Treat this file_spec as a normal file.
@@ -40,6 +49,12 @@
   std::string zip_path(path.substr(0, pos));
   std::string so_path(path.substr(pos + k_zip_separator.size()));
 
+#if defined(_WIN32)
+  // Replace the .so path to use POSIX file separator for file searching inside
+  // the zip file.
+  std::replace(so_path.begin(), so_path.end(), '\\', '/');
+#endif
+
   // Try to find the .so file from the zip file.
   FileSpec zip_file_spec(zip_path);
   uint64_t zip_file_size = FileSystem::Instance().GetByteSize(zip_file_spec);
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D153390: [lldb][Windows] Fix ZipFileResolver tests

2023-06-22 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

Yes, in fact ZipFileResolverTest is the test what is running on Windows, and 
failing due to POSIX vs Windows path. This diff will fix the tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153390

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


[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

Fixed Windows test failures in D153390 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

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


[Lldb-commits] [PATCH] D153390: [lldb][Windows] Fix ZipFileResolver tests

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

D152759  introduced the Android .zip so file 
support, but it only considered
POSIX path. The code also runs on Windows, so the path could be Windows path.
Support both patterns on Windows.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153390

Files:
  lldb/source/Host/common/ZipFileResolver.cpp


Index: lldb/source/Host/common/ZipFileResolver.cpp
===
--- lldb/source/Host/common/ZipFileResolver.cpp
+++ lldb/source/Host/common/ZipFileResolver.cpp
@@ -25,6 +25,15 @@
   static constexpr llvm::StringLiteral k_zip_separator("!/");
   std::string path(file_spec.GetPath());
   size_t pos = path.find(k_zip_separator);
+
+#if defined(_WIN32)
+  // When the file_spec is resolved as a Windows path, the zip .so path will be
+  // "zip_path!\so_path". Support both patterns on Windows.
+  static constexpr llvm::StringLiteral k_zip_separator_win("!\\");
+  if (pos == std::string::npos)
+pos = path.find(k_zip_separator_win);
+#endif
+
   if (pos == std::string::npos) {
 // This file_spec does not contain the zip separator.
 // Treat this file_spec as a normal file.
@@ -40,6 +49,12 @@
   std::string zip_path(path.substr(0, pos));
   std::string so_path(path.substr(pos + k_zip_separator.size()));
 
+#if defined(_WIN32)
+  // Replace the .so path to use POSIX file separator for file searching inside
+  // the zip file.
+  std::replace(so_path.begin(), so_path.end(), '\\', '/');
+#endif
+
   // Try to find the .so file from the zip file.
   FileSpec zip_file_spec(zip_path);
   uint64_t zip_file_size = FileSystem::Instance().GetByteSize(zip_file_spec);


Index: lldb/source/Host/common/ZipFileResolver.cpp
===
--- lldb/source/Host/common/ZipFileResolver.cpp
+++ lldb/source/Host/common/ZipFileResolver.cpp
@@ -25,6 +25,15 @@
   static constexpr llvm::StringLiteral k_zip_separator("!/");
   std::string path(file_spec.GetPath());
   size_t pos = path.find(k_zip_separator);
+
+#if defined(_WIN32)
+  // When the file_spec is resolved as a Windows path, the zip .so path will be
+  // "zip_path!\so_path". Support both patterns on Windows.
+  static constexpr llvm::StringLiteral k_zip_separator_win("!\\");
+  if (pos == std::string::npos)
+pos = path.find(k_zip_separator_win);
+#endif
+
   if (pos == std::string::npos) {
 // This file_spec does not contain the zip separator.
 // Treat this file_spec as a normal file.
@@ -40,6 +49,12 @@
   std::string zip_path(path.substr(0, pos));
   std::string so_path(path.substr(pos + k_zip_separator.size()));
 
+#if defined(_WIN32)
+  // Replace the .so path to use POSIX file separator for file searching inside
+  // the zip file.
+  std::replace(so_path.begin(), so_path.end(), '\\', '/');
+#endif
+
   // Try to find the .so file from the zip file.
   FileSpec zip_file_spec(zip_path);
   uint64_t zip_file_size = FileSystem::Instance().GetByteSize(zip_file_spec);
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

ResolveSharedLibraryPathWithZipExisting and 
ResolveSharedLibraryPathWithZipMissing are failing on Windows. Looking into.
https://lab.llvm.org/buildbot/#/builders/219/builds/3674


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

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


[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.package-name

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGfabd16c7460e: [lldb][Android] Add 
platform.plugin.remote-android.package-name (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -49,6 +49,7 @@
   }
 
   MOCK_METHOD1(GetAdbClient, AdbClientUP(Status ));
+  MOCK_METHOD0(GetPropertyPackageName, llvm::StringRef());
 };
 
 } // namespace
@@ -112,6 +113,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyPackageName())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -164,3 +191,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(SyncServiceUP(sync_service;
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyPackageName())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(2)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client0
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client1;
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformPackageName: Property<"package-name", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientUP;
   virtual AdbClientUP GetAdbClient(Status );
 
+  virtual llvm::StringRef GetPropertyPackageName();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
Index: 

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG49f55b025d81: [lldb][Android] Add PlatformAndroidTest 
(authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

Files:
  lldb/source/Plugins/Platform/Android/AdbClient.h
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/unittests/Platform/Android/CMakeLists.txt
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- /dev/null
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -0,0 +1,166 @@
+//===-- PlatformAndroidTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Utility/Connection.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace testing;
+
+namespace {
+
+class MockSyncService : public AdbClient::SyncService {
+public:
+  MockSyncService() : SyncService(std::unique_ptr()) {}
+
+  MOCK_METHOD2(PullFile,
+   Status(const FileSpec _file, const FileSpec _file));
+  MOCK_METHOD4(Stat, Status(const FileSpec _file, uint32_t ,
+uint32_t , uint32_t ));
+};
+
+typedef std::unique_ptr SyncServiceUP;
+
+class MockAdbClient : public AdbClient {
+public:
+  explicit MockAdbClient() : AdbClient("mock") {}
+
+  MOCK_METHOD3(ShellToFile,
+   Status(const char *command, std::chrono::milliseconds timeout,
+  const FileSpec _file_spec));
+  MOCK_METHOD1(GetSyncService, SyncServiceUP(Status ));
+};
+
+class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
+public:
+  PlatformAndroidTest() : PlatformAndroid(false) {
+m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
+  }
+
+  MOCK_METHOD1(GetAdbClient, AdbClientUP(Status ));
+};
+
+} // namespace
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithAdbClientError) {
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(DoAll(WithArg<0>([](auto ) {
+arg = Status("Failed to create AdbClient");
+  }),
+  Return(ByMove(AdbClientUP();
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Fail());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/system/lib64/libc.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/system/lib64/libc.so"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(SyncServiceUP(sync_service;
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFile) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, 

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG12dee9d3cd76: [lldb][Android] Support zip .so file (authored 
by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include 

[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb0b9605a544d: [lldb][ObjectFileELF] Set ModuleSpec file 
offset and size (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

Files:
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/unittests/ObjectFile/ELF/CMakeLists.txt
  lldb/unittests/ObjectFile/ELF/Inputs/liboffset-test.so
  lldb/unittests/ObjectFile/ELF/Inputs/offset-test.bin
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  // The contents of offset-test.bin are
+  // -0-1023: \0
+  // - 1024-4623: liboffset-test.so (offset: 1024, size: 3600, CRC32: 7D6E4738)
+  // - 4624-4639: \0
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt
===
--- lldb/unittests/ObjectFile/ELF/CMakeLists.txt
+++ lldb/unittests/ObjectFile/ELF/CMakeLists.txt
@@ -11,5 +11,7 @@
 
 set(test_inputs
   early-section-headers.so
+  liboffset-test.so
+  offset-test.bin
   )
 add_unittest_inputs(ObjectFileELFTests "${test_inputs}")
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -555,6 +555,14 @@
 if (header.Parse(data, _offset)) {
   if (data_sp) {
 ModuleSpec spec(file);
+// In Android API level 23 and above, bionic dynamic linker is able to
+// load .so file directly from zip file. In that case, .so file is
+// page aligned and uncompressed, and this module spec should retain 
the
+// .so file offset and file size to pass through the information from
+// lldb-server to LLDB. For normal file, file_offset should be 0,
+// length should be the size of the file.
+spec.SetObjectOffset(file_offset);
+spec.SetObjectSize(length);
 
 const uint32_t sub_type = subTypeFromElfHeader(header);
 spec.GetArchitecture().SetArchitecture(
@@ -586,8 +594,12 @@
   __FUNCTION__, file.GetPath().c_str());
   }
 
+  // When ELF file does not contain GNU build ID, the later code will
+  // calculate CRC32 with this data_sp file_offset and length. It is
+  // important for Android zip .so file, which is a slice of a file,
+  // to not access the outside of the file slice range.
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)
 data.SetData(data_sp);
   // In case there is header extension in the section #0, the header we


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
+  

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-20 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

I will submit a diff to deal with how to use the AdbClient in PlatformAndroid 
later. Most likely we could use only one instance of AdbClient.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

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


[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.package-name

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 532245.
splhack added a comment.

update commit message


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -49,6 +49,7 @@
   }
 
   MOCK_METHOD1(GetAdbClient, AdbClientUP(Status ));
+  MOCK_METHOD0(GetPropertyPackageName, llvm::StringRef());
 };
 
 } // namespace
@@ -112,6 +113,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyPackageName())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -164,3 +191,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(SyncServiceUP(sync_service;
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyPackageName())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(2)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client0
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client1;
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformPackageName: Property<"package-name", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientUP;
   virtual AdbClientUP GetAdbClient(Status );
 
+  virtual llvm::StringRef GetPropertyPackageName();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp

[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.run-as

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 532243.
splhack added a comment.

Rename it to platform.plugin.remote-android.package-name


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -49,6 +49,7 @@
   }
 
   MOCK_METHOD1(GetAdbClient, AdbClientUP(Status ));
+  MOCK_METHOD0(GetPropertyPackageName, llvm::StringRef());
 };
 
 } // namespace
@@ -112,6 +113,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyPackageName())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -164,3 +191,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(SyncServiceUP(sync_service;
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyPackageName())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(2)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client0
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client1;
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformPackageName: Property<"package-name", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientUP;
   virtual AdbClientUP GetAdbClient(Status );
 
+  virtual llvm::StringRef GetPropertyPackageName();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 532196.
splhack added a comment.

- s/AdbClientSP/AdbClientUP/g
- Updated `Status ` arg to `GetAdbClient`
- Added error checks in AndroidPlatform for `GetAdbClient`
- Added DownloadModuleSliceWithAdbClientError test for the GetAdbClient error


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

Files:
  lldb/source/Plugins/Platform/Android/AdbClient.h
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/unittests/Platform/Android/CMakeLists.txt
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- /dev/null
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -0,0 +1,166 @@
+//===-- PlatformAndroidTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Utility/Connection.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace testing;
+
+namespace {
+
+class MockSyncService : public AdbClient::SyncService {
+public:
+  MockSyncService() : SyncService(std::unique_ptr()) {}
+
+  MOCK_METHOD2(PullFile,
+   Status(const FileSpec _file, const FileSpec _file));
+  MOCK_METHOD4(Stat, Status(const FileSpec _file, uint32_t ,
+uint32_t , uint32_t ));
+};
+
+typedef std::unique_ptr SyncServiceUP;
+
+class MockAdbClient : public AdbClient {
+public:
+  explicit MockAdbClient() : AdbClient("mock") {}
+
+  MOCK_METHOD3(ShellToFile,
+   Status(const char *command, std::chrono::milliseconds timeout,
+  const FileSpec _file_spec));
+  MOCK_METHOD1(GetSyncService, SyncServiceUP(Status ));
+};
+
+class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
+public:
+  PlatformAndroidTest() : PlatformAndroid(false) {
+m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
+  }
+
+  MOCK_METHOD1(GetAdbClient, AdbClientUP(Status ));
+};
+
+} // namespace
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithAdbClientError) {
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(DoAll(WithArg<0>([](auto ) {
+arg = Status("Failed to create AdbClient");
+  }),
+  Return(ByMove(AdbClientUP();
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Fail());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/system/lib64/libc.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/system/lib64/libc.so"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(SyncServiceUP(sync_service;
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFile) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetAdbClient(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(AdbClientUP(adb_client;
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
+  .Times(1)
+  

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 532165.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace lldb_private;
+using namespace llvm::support;
+

[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 532163.
splhack added a comment.

removed abandoned dependency


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

Files:
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/unittests/ObjectFile/ELF/CMakeLists.txt
  lldb/unittests/ObjectFile/ELF/Inputs/liboffset-test.so
  lldb/unittests/ObjectFile/ELF/Inputs/offset-test.bin
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  // The contents of offset-test.bin are
+  // -0-1023: \0
+  // - 1024-4623: liboffset-test.so (offset: 1024, size: 3600, CRC32: 7D6E4738)
+  // - 4624-4639: \0
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt
===
--- lldb/unittests/ObjectFile/ELF/CMakeLists.txt
+++ lldb/unittests/ObjectFile/ELF/CMakeLists.txt
@@ -11,5 +11,7 @@
 
 set(test_inputs
   early-section-headers.so
+  liboffset-test.so
+  offset-test.bin
   )
 add_unittest_inputs(ObjectFileELFTests "${test_inputs}")
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -555,6 +555,14 @@
 if (header.Parse(data, _offset)) {
   if (data_sp) {
 ModuleSpec spec(file);
+// In Android API level 23 and above, bionic dynamic linker is able to
+// load .so file directly from zip file. In that case, .so file is
+// page aligned and uncompressed, and this module spec should retain 
the
+// .so file offset and file size to pass through the information from
+// lldb-server to LLDB. For normal file, file_offset should be 0,
+// length should be the size of the file.
+spec.SetObjectOffset(file_offset);
+spec.SetObjectSize(length);
 
 const uint32_t sub_type = subTypeFromElfHeader(header);
 spec.GetArchitecture().SetArchitecture(
@@ -586,8 +594,12 @@
   __FUNCTION__, file.GetPath().c_str());
   }
 
+  // When ELF file does not contain GNU build ID, the later code will
+  // calculate CRC32 with this data_sp file_offset and length. It is
+  // important for Android zip .so file, which is a slice of a file,
+  // to not access the outside of the file slice range.
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)
 data.SetData(data_sp);
   // In case there is header extension in the section #0, the header we


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  

[Lldb-commits] [PATCH] D153102: [lldb][MainLoopTest] Fix assertion failure on arm Linux

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack abandoned this revision.
splhack added a comment.

@DavidSpickett thanks for the heads up, and thanks @labath to the commit


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153102

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


[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-16 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531992.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace lldb_private;
+using namespace llvm::support;
+

[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531990.
splhack added a comment.

fixed typo and rebase onto D153102 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

Files:
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/unittests/ObjectFile/ELF/CMakeLists.txt
  lldb/unittests/ObjectFile/ELF/Inputs/liboffset-test.so
  lldb/unittests/ObjectFile/ELF/Inputs/offset-test.bin
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  // The contents of offset-test.bin are
+  // -0-1023: \0
+  // - 1024-4623: liboffset-test.so (offset: 1024, size: 3600, CRC32: 7D6E4738)
+  // - 4624-4639: \0
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt
===
--- lldb/unittests/ObjectFile/ELF/CMakeLists.txt
+++ lldb/unittests/ObjectFile/ELF/CMakeLists.txt
@@ -11,5 +11,7 @@
 
 set(test_inputs
   early-section-headers.so
+  liboffset-test.so
+  offset-test.bin
   )
 add_unittest_inputs(ObjectFileELFTests "${test_inputs}")
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -555,6 +555,14 @@
 if (header.Parse(data, _offset)) {
   if (data_sp) {
 ModuleSpec spec(file);
+// In Android API level 23 and above, bionic dynamic linker is able to
+// load .so file directly from zip file. In that case, .so file is
+// page aligned and uncompressed, and this module spec should retain 
the
+// .so file offset and file size to pass through the information from
+// lldb-server to LLDB. For normal file, file_offset should be 0,
+// length should be the size of the file.
+spec.SetObjectOffset(file_offset);
+spec.SetObjectSize(length);
 
 const uint32_t sub_type = subTypeFromElfHeader(header);
 spec.GetArchitecture().SetArchitecture(
@@ -586,8 +594,12 @@
   __FUNCTION__, file.GetPath().c_str());
   }
 
+  // When ELF file does not contain GNU build ID, the later code will
+  // calculate CRC32 with this data_sp file_offset and length. It is
+  // important for Android zip .so file, which is a slice of a file,
+  // to not access the outside of the file slice range.
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)
 data.SetData(data_sp);
   // In case there is header extension in the section #0, the header we


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
+  ModuleSpec Spec;
+  

[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

D153102  should fix the last test failure.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D153102: [lldb][MainLoopTest] Fix assertion failure on arm Linux

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
splhack added reviewers: clayborg, labath, bulbazord, yinghuitan, JDevlieghere, 
leonardchan.
Herald added subscribers: omjavaid, kristof.beyls.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

D152712  replaced 
`llvm::sys::RetryAfterSignal(-1, ::open)` with
`FileSystem::Instance().Open()` for bionic and `FileSystem::Instance()` is
failing with the re-initialization assertion on arm Linux test job.
This is because MainLoopTest does not tear down the FileSystem instance.

https://lab.llvm.org/buildbot/#/builders/96/builds/40781

Add FileSystem to SubsystemRAII<> to ensure destroying the FileSystem instance.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153102

Files:
  lldb/unittests/Host/MainLoopTest.cpp


Index: lldb/unittests/Host/MainLoopTest.cpp
===
--- lldb/unittests/Host/MainLoopTest.cpp
+++ lldb/unittests/Host/MainLoopTest.cpp
@@ -9,6 +9,7 @@
 #include "lldb/Host/MainLoop.h"
 #include "TestingSupport/SubsystemRAII.h"
 #include "lldb/Host/ConnectionFileDescriptor.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/common/TCPSocket.h"
 #include "llvm/Testing/Support/Error.h"
@@ -20,7 +21,7 @@
 namespace {
 class MainLoopTest : public testing::Test {
 public:
-  SubsystemRAII subsystems;
+  SubsystemRAII subsystems;
 
   void SetUp() override {
 bool child_processes_inherit = false;


Index: lldb/unittests/Host/MainLoopTest.cpp
===
--- lldb/unittests/Host/MainLoopTest.cpp
+++ lldb/unittests/Host/MainLoopTest.cpp
@@ -9,6 +9,7 @@
 #include "lldb/Host/MainLoop.h"
 #include "TestingSupport/SubsystemRAII.h"
 #include "lldb/Host/ConnectionFileDescriptor.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/common/TCPSocket.h"
 #include "llvm/Testing/Support/Error.h"
@@ -20,7 +21,7 @@
 namespace {
 class MainLoopTest : public testing::Test {
 public:
-  SubsystemRAII subsystems;
+  SubsystemRAII subsystems;
 
   void SetUp() override {
 bool child_processes_inherit = false;
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

Verified D153091  fixed TerminalTest failures.

from `Failed Tests (13)`
https://lab.llvm.org/buildbot/#/builders/96/builds/40769

to `Failed Tests (1)`
https://lab.llvm.org/buildbot/#/builders/96/builds/40781

will submit a diff to fix the last one, MainLoopTest.DetectsEOF

  [ RUN  ] MainLoopTest.DetectsEOF
   #0 0xe2a5c738 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xa0738)
   #1 0xe2a5ac6c llvm::sys::RunSignalHandlers() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x9ec6c)
   #2 0xe2a5cfdc SignalHandler(int) Signals.cpp:0:0
   #3 0x957c0598 (linux-vdso.so.1+0x598)
   #4 0x94fcf200 __pthread_kill_implementation 
./nptl/./nptl/pthread_kill.c:44:76
   #5 0x94f8a67c gsignal ./signal/../sysdeps/posix/raise.c:27:6
   #6 0x94f77130 abort ./stdlib/./stdlib/abort.c:81:7
   #7 0xe29e52e4 testing::AssertionResult 
testing::internal::CmpHelperEQ(char 
const*, char const*, lldb_private::URI const&, lldb_private::URI const&) 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x292e4)
   #8 0xe2aaa234 lldb_private::FileSystem::Instance() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xee234)
   #9 0xe2ab2f7c lldb_private::PseudoTerminal::OpenSecondary(int) 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xf6f7c)
  #10 0xe29f5458 MainLoopTest_DetectsEOF_Test::TestBody() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x39458)
  #11 0xe2a7ae80 testing::Test::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xbee80)
  #12 0xe2a7c5e4 testing::TestInfo::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xc05e4)
  #13 0xe2a7cd0c testing::TestSuite::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xc0d0c)
  #14 0xe2a8a5c0 testing::internal::UnitTestImpl::RunAllTests() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xce5c0)
  #15 0xe2a89e1c testing::UnitTest::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xcde1c)
  #16 0xe2a725a4 main 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xb65a4)
  #17 0x94f773fc __libc_start_call_main 
./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
  #18 0x94f774cc call_init ./csu/../csu/libc-start.c:128:20
  #19 0x94f774cc __libc_start_main ./csu/../csu/libc-start.c:379:5
  #20 0xe29e3670 _start 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x27670)
  
  --
  exit: -6


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D153091: [lldb][TerminalTest] Fix assertion failure

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG75e93ec72073: [lldb][TerminalTest] Fix assertion failure 
(authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D153091

Files:
  lldb/unittests/Host/posix/TerminalTest.cpp


Index: lldb/unittests/Host/posix/TerminalTest.cpp
===
--- lldb/unittests/Host/posix/TerminalTest.cpp
+++ lldb/unittests/Host/posix/TerminalTest.cpp
@@ -6,9 +6,11 @@
 //
 
//===--===//
 
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Terminal.h"
 #include "llvm/Testing/Support/Error.h"
+#include "TestingSupport/SubsystemRAII.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -20,6 +22,7 @@
 
 class TerminalTest : public ::testing::Test {
 protected:
+  SubsystemRAII subsystems;
   PseudoTerminal m_pty;
   int m_fd;
   Terminal m_term;


Index: lldb/unittests/Host/posix/TerminalTest.cpp
===
--- lldb/unittests/Host/posix/TerminalTest.cpp
+++ lldb/unittests/Host/posix/TerminalTest.cpp
@@ -6,9 +6,11 @@
 //
 //===--===//
 
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Terminal.h"
 #include "llvm/Testing/Support/Error.h"
+#include "TestingSupport/SubsystemRAII.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -20,6 +22,7 @@
 
 class TerminalTest : public ::testing::Test {
 protected:
+  SubsystemRAII subsystems;
   PseudoTerminal m_pty;
   int m_fd;
   Terminal m_term;
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531974.
splhack added a comment.

Added eFileKindInvalid with assert


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace lldb_private;
+using 

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: 
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp:1336
+  // "zip_path!/so_path". Resolve the zip file path, .so file offset and size.
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;

clayborg wrote:
> might be good to init this to an invalid value in case code changes over time?
yup, sounds good. will add with assert.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

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


[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531969.
splhack added a comment.

Rename `ResolveBionicPath` to `ResolveSharedLibraryPath`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveSharedLibraryPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveSharedLibraryPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace 

[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@leonardchan I think D153091  will fix the 
abort (`-6` should be `SIGABRT`), therefore it should be the assertion check of 
re-initialization in `FileSystem::Instance`.
Is there a way to run the Linux builder with D153091 
 to make sure it will fix it?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D153091: [lldb][TerminalTest] Fix assertion failure

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack created this revision.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
splhack requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

D152712  replaced 
`llvm::sys::RetryAfterSignal(-1, ::open)` with
`FileSystem::Instance().Open` for bionic in PseudoTerminal::OpenSecondary, and
FileSystem::Instance() is failing with assertion on arm Linux.

The assertion should be FileSystem re-initialization check, therefore the
hypothesis is that TerminalTest tests are initializing FileSystem instance
repeatedly.

Use SubsystemRAII to ensure tearing down the FileSystem instance.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D153091

Files:
  lldb/unittests/Host/posix/TerminalTest.cpp


Index: lldb/unittests/Host/posix/TerminalTest.cpp
===
--- lldb/unittests/Host/posix/TerminalTest.cpp
+++ lldb/unittests/Host/posix/TerminalTest.cpp
@@ -6,9 +6,11 @@
 //
 
//===--===//
 
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Terminal.h"
 #include "llvm/Testing/Support/Error.h"
+#include "TestingSupport/SubsystemRAII.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -20,6 +22,7 @@
 
 class TerminalTest : public ::testing::Test {
 protected:
+  SubsystemRAII subsystems;
   PseudoTerminal m_pty;
   int m_fd;
   Terminal m_term;


Index: lldb/unittests/Host/posix/TerminalTest.cpp
===
--- lldb/unittests/Host/posix/TerminalTest.cpp
+++ lldb/unittests/Host/posix/TerminalTest.cpp
@@ -6,9 +6,11 @@
 //
 //===--===//
 
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Terminal.h"
 #include "llvm/Testing/Support/Error.h"
+#include "TestingSupport/SubsystemRAII.h"
 
 #include "gmock/gmock.h"
 #include "gtest/gtest.h"
@@ -20,6 +22,7 @@
 
 class TerminalTest : public ::testing::Test {
 protected:
+  SubsystemRAII subsystems;
   PseudoTerminal m_pty;
   int m_fd;
   Terminal m_term;
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531954.
splhack added a comment.

added non zip .so file (normal file) case to the comment


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

Files:
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/unittests/ObjectFile/ELF/CMakeLists.txt
  lldb/unittests/ObjectFile/ELF/Inputs/liboffset-test.so
  lldb/unittests/ObjectFile/ELF/Inputs/offset-test.bin
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  // The contents of offset-test.bin are
+  // -0-1023: \0
+  // - 1024-4623: liboffset-test.so (offset: 1024, size: 3600, CRC32: 7D6E4738)
+  // - 4624-4639: \0
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt
===
--- lldb/unittests/ObjectFile/ELF/CMakeLists.txt
+++ lldb/unittests/ObjectFile/ELF/CMakeLists.txt
@@ -11,5 +11,7 @@
 
 set(test_inputs
   early-section-headers.so
+  liboffset-test.so
+  offset-test.bin
   )
 add_unittest_inputs(ObjectFileELFTests "${test_inputs}")
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -555,6 +555,14 @@
 if (header.Parse(data, _offset)) {
   if (data_sp) {
 ModuleSpec spec(file);
+// In Android API level 23 and above, bionic dynamic linker is able to
+// load .so file directly from zip file. In that case, .so file is
+// page aligned and uncompressed, and this module spec should retain 
the
+// .so file offset and file size to pass throught the information from
+// lldb-server to LLDB. For normal file, file_offset should be 0,
+// length should be the size of the file.
+spec.SetObjectOffset(file_offset);
+spec.SetObjectSize(length);
 
 const uint32_t sub_type = subTypeFromElfHeader(header);
 spec.GetArchitecture().SetArchitecture(
@@ -586,8 +594,12 @@
   __FUNCTION__, file.GetPath().c_str());
   }
 
+  // When ELF file does not contain GNU build ID, the later code will
+  // calculate CRC32 with this data_sp file_offset and length. It is
+  // important for Android zip .so file, which is a slice of a file,
+  // to not access the outside of the file slice range.
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)
 data.SetData(data_sp);
   // In case there is header extension in the section #0, the header we


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, 

[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

found symbolicated stack trace in
https://lab.llvm.org/buildbot/#/builders/96/builds/40769/steps/6/logs/stdio

TerminalTest does not instantiate FileSystem. now preparing to submit a diff to 
fix it.

   #0 0xcc46d6d4 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xa06d4)
   #1 0xcc46bc08 llvm::sys::RunSignalHandlers() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x9ec08)
   #2 0xcc46df78 SignalHandler(int) Signals.cpp:0:0
   #3 0xb0a38598 (linux-vdso.so.1+0x598)
   #4 0xb023f200 __pthread_kill_implementation 
./nptl/./nptl/pthread_kill.c:44:76
   #5 0xb01fa67c gsignal ./signal/../sysdeps/posix/raise.c:27:6
   #6 0xb01e7130 abort ./stdlib/./stdlib/abort.c:81:7
   #7 0xcc3f62e4 testing::AssertionResult 
testing::internal::CmpHelperEQ(char 
const*, char const*, lldb_private::URI const&, lldb_private::URI const&) 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x292e4)
   #8 0xcc4bb1d0 lldb_private::FileSystem::Instance() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xee1d0)
   #9 0xcc4c3f18 lldb_private::PseudoTerminal::OpenSecondary(int) 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xf6f18)
  #10 0xcc45304c TerminalTest::SetUp() crtstuff.c:0:0
  #11 0xcc48bccc testing::Test::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xbeccc)
  #12 0xcc48d580 testing::TestInfo::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xc0580)
  #13 0xcc48dca8 testing::TestSuite::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xc0ca8)
  #14 0xcc49b55c testing::internal::UnitTestImpl::RunAllTests() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xce55c)
  #15 0xcc49adb8 testing::UnitTest::Run() 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xcddb8)
  #16 0xcc483540 main 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0xb6540)
  #17 0xb01e73fc __libc_start_call_main 
./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
  #18 0xb01e74cc call_init ./csu/../csu/libc-start.c:128:20
  #19 0xb01e74cc __libc_start_main ./csu/../csu/libc-start.c:379:5
  #20 0xcc3f4670 _start 
(/home/tcwg-buildbot/worker/lldb-aarch64-ubuntu/build/tools/lldb/unittests/Host/./HostTests+0x27670)
  --
  exit: -6
  --


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531950.
splhack added a comment.

add comments


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

Files:
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/unittests/ObjectFile/ELF/CMakeLists.txt
  lldb/unittests/ObjectFile/ELF/Inputs/liboffset-test.so
  lldb/unittests/ObjectFile/ELF/Inputs/offset-test.bin
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  // The contents of offset-test.bin are
+  // -0-1023: \0
+  // - 1024-4623: liboffset-test.so (offset: 1024, size: 3600, CRC32: 7D6E4738)
+  // - 4624-4639: \0
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt
===
--- lldb/unittests/ObjectFile/ELF/CMakeLists.txt
+++ lldb/unittests/ObjectFile/ELF/CMakeLists.txt
@@ -11,5 +11,7 @@
 
 set(test_inputs
   early-section-headers.so
+  liboffset-test.so
+  offset-test.bin
   )
 add_unittest_inputs(ObjectFileELFTests "${test_inputs}")
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -555,6 +555,13 @@
 if (header.Parse(data, _offset)) {
   if (data_sp) {
 ModuleSpec spec(file);
+// In Android API level 23 and above, bionic dynamic linker is able to
+// load .so file directly from zip file. In that case, .so file is
+// page aligned and uncompressed, and this module spec should retain 
the
+// .so file offset and file size to pass throught the information from
+// lldb-server to LLDB.
+spec.SetObjectOffset(file_offset);
+spec.SetObjectSize(length);
 
 const uint32_t sub_type = subTypeFromElfHeader(header);
 spec.GetArchitecture().SetArchitecture(
@@ -586,8 +593,12 @@
   __FUNCTION__, file.GetPath().c_str());
   }
 
+  // When ELF file does not contain GNU build ID, the later code will
+  // calculate CRC32 with this data_sp file_offset and length. It is
+  // important for Android zip .so file, which is a slice of a file,
+  // to not access the outside of the file slice range.
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)
 data.SetData(data_sp);
   // In case there is header extension in the section #0, the header we


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,39 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 

[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@leonardchan yes, I'm looking into. -6 is ENXIO? Is it using POSIX code path? 
Is it possible to see symbolicated stack trace of the crash?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.run-as

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@clayborg

> platform.plugin.remote-android.package-name

sure, sounds good. will update.

@bulbazord

> What happens if you do run-as with an empty package name?



  $ adb shell "run-as '' ls"; echo $?
  run-as: unknown package:
  1

which is the same as when specify non-existing package.

  $ adb shell "run-as 'unknown' ls"; echo $?
  run-as: unknown package: unknown
  1


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

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


[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp:209
 
-  return adb.ShellToFile(cmd, minutes(1), destination);
+  return adb->ShellToFile(cmd, minutes(1), destination);
 }

bulbazord wrote:
> `GetAdbClient()` calls `make_unique` which can fail. You'll want to add some 
> checks when using all the `adb` objects in this file.
will do!



Comment at: lldb/source/Plugins/Platform/Android/PlatformAndroid.h:73
 
+  typedef std::unique_ptr AdbClientSP;
+  virtual AdbClientSP GetAdbClient();

clayborg wrote:
> bulbazord wrote:
> > `SP` -> `shared_ptr`. Was this supposed to be `std::shared_ptr`? If it's 
> > supposed to be a unique_ptr, you probably want `AdbClientUP`.
> Using the "SP" suffix is for std::shared_ptr, "UP" is for unique pointers.
@bulbazord @clayborg thanks, and oops didn't realize UP. will fix.



Comment at: lldb/source/Plugins/Platform/Android/PlatformAndroid.h:74
+  typedef std::unique_ptr AdbClientSP;
+  virtual AdbClientSP GetAdbClient();
+

bulbazord wrote:
> I feel a little strange about this one because it's possible to create 2 
> `AdbClient`s from the same PlatformAndroid ID, both are unique pointers and 
> essentially do the same thing. Maybe `PlatformAndroid` can own an `AdbClient` 
> that it vends through `GetAdbClient`?
Yes, it's a bit strange how PlatformAndroid currently uses AdbClient. But, the 
scope of this diff is to unblock us first to write PlatformAndroid unittest 
with gmock + virtual. We will revisit for the structure change later.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

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


[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rGb4d710e41059: [lldb][Android] Use a lambda for calls to 
::open in RetryAfterSignal (authored by splhack).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

Files:
  lldb/include/lldb/Host/FileSystem.h
  lldb/source/Host/common/PseudoTerminal.cpp
  lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
  lldb/source/Host/posix/FileSystemPosix.cpp
  lldb/source/Host/posix/PipePosix.cpp
  lldb/source/Host/posix/ProcessLauncherPosixFork.cpp

Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -7,6 +7,7 @@
 //===--===//
 
 #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Host.h"
 #include "lldb/Host/HostProcess.h"
 #include "lldb/Host/Pipe.h"
@@ -14,7 +15,6 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
-#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -71,7 +71,7 @@
 }
 
 static void DupDescriptor(int error_fd, const char *file, int fd, int flags) {
-  int target_fd = llvm::sys::RetryAfterSignal(-1, ::open, file, flags, 0666);
+  int target_fd = FileSystem::Instance().Open(file, flags, 0666);
 
   if (target_fd == -1)
 ExitWithError(error_fd, "DupDescriptor-open");
Index: lldb/source/Host/posix/PipePosix.cpp
===
--- lldb/source/Host/posix/PipePosix.cpp
+++ lldb/source/Host/posix/PipePosix.cpp
@@ -7,11 +7,11 @@
 //===--===//
 
 #include "lldb/Host/posix/PipePosix.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Utility/SelectHelper.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Errno.h"
-#include "llvm/Support/FileSystem.h"
 #include 
 #include 
 
@@ -148,7 +148,7 @@
 flags |= O_CLOEXEC;
 
   Status error;
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.str().c_str(), flags);
+  int fd = FileSystem::Instance().Open(name.str().c_str(), flags);
   if (fd != -1)
 m_fds[READ] = fd;
   else
Index: lldb/source/Host/posix/FileSystemPosix.cpp
===
--- lldb/source/Host/posix/FileSystemPosix.cpp
+++ lldb/source/Host/posix/FileSystemPosix.cpp
@@ -77,5 +77,8 @@
 }
 
 int FileSystem::Open(const char *path, int flags, int mode) {
-  return llvm::sys::RetryAfterSignal(-1, ::open, path, flags, mode);
+  // Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
+  // when open is overloaded, such as in Bionic.
+  auto lambda = [&]() { return ::open(path, flags, mode); };
+  return llvm::sys::RetryAfterSignal(-1, lambda);
 }
Index: lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
===
--- lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
+++ lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
@@ -15,6 +15,7 @@
 
 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
 #include "lldb/Host/Config.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Socket.h"
 #include "lldb/Host/SocketAddress.h"
 #include "lldb/Utility/LLDBLog.h"
@@ -726,7 +727,7 @@
 #if LLDB_ENABLE_POSIX
   std::string addr_str = s.str();
   // file:///PATH
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR);
+  int fd = FileSystem::Instance().Open(addr_str.c_str(), O_RDWR);
   if (fd == -1) {
 if (error_ptr)
   error_ptr->SetErrorToErrno();
@@ -776,7 +777,7 @@
 return eConnectionStatusError;
   }
 
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, path.str().c_str(), O_RDWR);
+  int fd = FileSystem::Instance().Open(path.str().c_str(), O_RDWR);
   if (fd == -1) {
 if (error_ptr)
   error_ptr->SetErrorToErrno();
Index: lldb/source/Host/common/PseudoTerminal.cpp
===
--- lldb/source/Host/common/PseudoTerminal.cpp
+++ lldb/source/Host/common/PseudoTerminal.cpp
@@ -8,6 +8,7 @@
 
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Config.h"
+#include "lldb/Host/FileSystem.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Errno.h"
 #include 
@@ -95,7 +96,7 @@
   CloseSecondaryFileDescriptor();
 
   std::string name = GetSecondaryName();
-  m_secondary_fd = llvm::sys::RetryAfterSignal(-1, ::open, name.c_str(), oflag);
+  m_secondary_fd = FileSystem::Instance().Open(name.c_str(), oflag);
   if (m_secondary_fd >= 0)
 return llvm::Error::success();
 
Index: lldb/include/lldb/Host/FileSystem.h

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/include/lldb/Host/common/ZipFileResolver.h:30
+
+  static bool ResolveBionicPath(const FileSpec _spec, FileKind _kind,
+std::string _path,

mib wrote:
> mib wrote:
> > This function name sounds like the the bionic linker is in the zip file 
> > which is not the case IIUC. I'm fine with adding a zip file resolver in 
> > lldb but I'd prefer if it had a generic name, otherwise, this should be a 
> > plugin.
> what about this instead ?
Yes, should be good. will update this diff.

(Why it had "Bionic" because I thought the path encoding "zip_path!/lib_path" 
is bionic specific.)



Comment at: lldb/source/Utility/ZipFile.cpp:19-21
+// Zip headers.
+// https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
+

mib wrote:
> Did you just copy & past the this file from somewhere else of did you 
> implement it yourself ?
I implemented this code.

logic
- Linear search the end of central directory record from the file end because 
it is located at the end of the file with comment (64KB max)
- Linear search the file from the central directory records that is pointed by 
the end of central directory record.
- Get the file offset and size from the local file header that is pointed by 
the central directory record
- Use unaligned_uint16_t/unaligned_uint32_t since Zip header is 1 byte aligned. 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

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


[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-15 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added inline comments.



Comment at: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp:592
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)

bulbazord wrote:
> A length of `-1` means "map the whole file", if my understanding is correct. 
> Why do we want to change this to `length` instead of just -1? Or is this 
> `file` the entire zip file?
I'm now updating this diff to add comments to clarify the intention.

For Android zip .so file (D152759),
the ModuleSpec `spec` will have these info with this diff.
- object offset = .so file offset inside the zip file
- object size = .so file size

PlatformAndroid requires this module spec to call DownloadModuleSlice in order 
to download the .so file from the zip file using adb shell dd.
- file spec = "zip_path!/lib_path"
- file offset = .so file offset inside the zip file == dd skip parameter
- file size = .so file size == dd count parameter

And ObjectFileELF will calculate CRC32 if the ELF does not have GNU BuildID, 
that will use the `data_sp` size. Which is set by `MapFileData`. Currently `-1` 
== the whole zip file size. (In fact, lldb-server is crashing due to SIGSEGV.) 
It is supposed to be the .so file size. 
In the unittest, the test .so file has CRC32 `7D6E4738`. And it should be the 
same for the file size.

offset-test.bin contains
-1024bytes of '\0'
- liboffset-test.so (file offset = 1024, file size = 3600, CRC32 '7D6E4738')
- 16bytes of '\0'


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

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


[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.run-as

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531579.
splhack added a comment.

Fixed diff dependencies in order to fix CI
https://reviews.llvm.org/B238946


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -52,6 +52,7 @@
   }
 
   MOCK_METHOD0(GetAdbClient, AdbClientSP());
+  MOCK_METHOD0(GetPropertyRunAs, llvm::StringRef());
 };
 
 } // namespace
@@ -100,6 +101,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -152,3 +179,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(2)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client0)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client1);
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformRunAs: Property<"run-as", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientSP;
   virtual AdbClientSP GetAdbClient();
 
+  virtual llvm::StringRef GetPropertyRunAs();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
@@ -81,7 +87,7 @@
   uint32_t m_sdk_version;
 };
 
-} // namespace 

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531578.
splhack added a comment.

Fixed diff dependencies in order to fix CI
https://reviews.llvm.org/B238938


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

Files:
  lldb/source/Plugins/Platform/Android/AdbClient.h
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/unittests/Platform/Android/CMakeLists.txt
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- /dev/null
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -0,0 +1,154 @@
+//===-- PlatformAndroidTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Utility/Connection.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace testing;
+
+namespace {
+
+class MockSyncService : public AdbClient::SyncService {
+public:
+  MockSyncService() : SyncService(std::move(m_mock_conn)) {}
+
+  MOCK_METHOD2(PullFile,
+   Status(const FileSpec _file, const FileSpec _file));
+  MOCK_METHOD4(Stat, Status(const FileSpec _file, uint32_t ,
+uint32_t , uint32_t ));
+
+private:
+  std::unique_ptr m_mock_conn;
+};
+
+typedef std::unique_ptr SyncServiceSP;
+
+class MockAdbClient : public AdbClient {
+public:
+  explicit MockAdbClient() : AdbClient("mock") {}
+
+  MOCK_METHOD3(ShellToFile,
+   Status(const char *command, std::chrono::milliseconds timeout,
+  const FileSpec _file_spec));
+  MOCK_METHOD1(GetSyncService, SyncServiceSP(Status ));
+};
+
+class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
+public:
+  PlatformAndroidTest() : PlatformAndroid(false) {
+m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
+  }
+
+  MOCK_METHOD0(GetAdbClient, AdbClientSP());
+};
+
+} // namespace
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/system/lib64/libc.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/system/lib64/libc.so"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFile) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/data/local/tmp/test"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(GetFile(FileSpec("/data/local/tmp/test"), 

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531576.
splhack added a comment.

Fixed diff dependencies in order to fix CI
https://reviews.llvm.org/B238937


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace lldb_private;
+using 

[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.run-as

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531509.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -52,6 +52,7 @@
   }
 
   MOCK_METHOD0(GetAdbClient, AdbClientSP());
+  MOCK_METHOD0(GetPropertyRunAs, llvm::StringRef());
 };
 
 } // namespace
@@ -100,6 +101,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -152,3 +179,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(2)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client0)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client1);
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformRunAs: Property<"run-as", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientSP;
   virtual AdbClientSP GetAdbClient();
 
+  virtual llvm::StringRef GetPropertyRunAs();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
@@ -81,7 +87,7 @@
   uint32_t m_sdk_version;
 };
 
-} // namespace platofor_android
+} // namespace platform_android
 } // namespace lldb_private
 
 

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531500.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

Files:
  lldb/source/Plugins/Platform/Android/AdbClient.h
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/unittests/Platform/Android/CMakeLists.txt
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- /dev/null
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -0,0 +1,154 @@
+//===-- PlatformAndroidTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Utility/Connection.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace testing;
+
+namespace {
+
+class MockSyncService : public AdbClient::SyncService {
+public:
+  MockSyncService() : SyncService(std::move(m_mock_conn)) {}
+
+  MOCK_METHOD2(PullFile,
+   Status(const FileSpec _file, const FileSpec _file));
+  MOCK_METHOD4(Stat, Status(const FileSpec _file, uint32_t ,
+uint32_t , uint32_t ));
+
+private:
+  std::unique_ptr m_mock_conn;
+};
+
+typedef std::unique_ptr SyncServiceSP;
+
+class MockAdbClient : public AdbClient {
+public:
+  explicit MockAdbClient() : AdbClient("mock") {}
+
+  MOCK_METHOD3(ShellToFile,
+   Status(const char *command, std::chrono::milliseconds timeout,
+  const FileSpec _file_spec));
+  MOCK_METHOD1(GetSyncService, SyncServiceSP(Status ));
+};
+
+class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
+public:
+  PlatformAndroidTest() : PlatformAndroid(false) {
+m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
+  }
+
+  MOCK_METHOD0(GetAdbClient, AdbClientSP());
+};
+
+} // namespace
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/system/lib64/libc.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/system/lib64/libc.so"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFile) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/data/local/tmp/test"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(GetFile(FileSpec("/data/local/tmp/test"), FileSpec()).Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallback) 

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531499.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace lldb_private;
+using namespace llvm::support;
+
+namespace {
+
+// Zip headers.
+// 

[Lldb-commits] [PATCH] D152757: [lldb][ObjectFileELF] Set ModuleSpec file offset and size

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531498.
splhack added a comment.

rebase


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152757

Files:
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/unittests/ObjectFile/ELF/CMakeLists.txt
  lldb/unittests/ObjectFile/ELF/Inputs/liboffset-test.so
  lldb/unittests/ObjectFile/ELF/Inputs/offset-test.bin
  lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,35 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, 
Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt
===
--- lldb/unittests/ObjectFile/ELF/CMakeLists.txt
+++ lldb/unittests/ObjectFile/ELF/CMakeLists.txt
@@ -11,5 +11,7 @@
 
 set(test_inputs
   early-section-headers.so
+  liboffset-test.so
+  offset-test.bin
   )
 add_unittest_inputs(ObjectFileELFTests "${test_inputs}")
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -555,6 +555,8 @@
 if (header.Parse(data, _offset)) {
   if (data_sp) {
 ModuleSpec spec(file);
+spec.SetObjectOffset(file_offset);
+spec.SetObjectSize(length);
 
 const uint32_t sub_type = subTypeFromElfHeader(header);
 spec.GetArchitecture().SetArchitecture(
@@ -587,7 +589,7 @@
   }
 
   if (data_sp->GetByteSize() < length)
-data_sp = MapFileData(file, -1, file_offset);
+data_sp = MapFileData(file, length, file_offset);
   if (data_sp)
 data.SetData(data_sp);
   // In case there is header extension in the section #0, the header we


Index: lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
===
--- lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
+++ lldb/unittests/ObjectFile/ELF/TestObjectFileELF.cpp
@@ -156,6 +156,35 @@
   EXPECT_EQ(Spec.GetUUID(), Uuid);
 }
 
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithNormalFile) {
+  std::string SO = GetInputFilePath("liboffset-test.so");
+  ModuleSpecList Specs;
+  ASSERT_EQ(1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 0, 0, Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 0UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 3600UL);
+}
+
+TEST_F(ObjectFileELFTest, GetModuleSpecifications_OffsetSizeWithOffsetFile) {
+  std::string SO = GetInputFilePath("offset-test.bin");
+  ModuleSpecList Specs;
+  ASSERT_EQ(
+  1u, ObjectFile::GetModuleSpecifications(FileSpec(SO), 1024, 3600, Specs));
+  ModuleSpec Spec;
+  ASSERT_TRUE(Specs.GetModuleSpecAtIndex(0, Spec)) ;
+  UUID Uuid;
+  Uuid.SetFromStringRef("7D6E4738");
+  EXPECT_EQ(Spec.GetUUID(), Uuid);
+  EXPECT_EQ(Spec.GetObjectOffset(), 1024UL);
+  EXPECT_EQ(Spec.GetObjectSize(), 3600UL);
+  EXPECT_EQ(FileSystem::Instance().GetByteSize(FileSpec(SO)), 4640UL);
+}
+
 TEST_F(ObjectFileELFTest, GetSymtab_NoSymEntryPointArmThumbAddressClass) {
   /*
   // nosym-entrypoint-arm-thumb.s
Index: lldb/unittests/ObjectFile/ELF/CMakeLists.txt

[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531483.
splhack added a comment.

Replace `llvm::sys::RetryAfterSignal(-1, ::open)` with
`FileSystem::Instance().Open`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

Files:
  lldb/include/lldb/Host/FileSystem.h
  lldb/source/Host/common/PseudoTerminal.cpp
  lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
  lldb/source/Host/posix/FileSystemPosix.cpp
  lldb/source/Host/posix/PipePosix.cpp
  lldb/source/Host/posix/ProcessLauncherPosixFork.cpp

Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -7,6 +7,7 @@
 //===--===//
 
 #include "lldb/Host/posix/ProcessLauncherPosixFork.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Host.h"
 #include "lldb/Host/HostProcess.h"
 #include "lldb/Host/Pipe.h"
@@ -14,7 +15,6 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
-#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -71,7 +71,7 @@
 }
 
 static void DupDescriptor(int error_fd, const char *file, int fd, int flags) {
-  int target_fd = llvm::sys::RetryAfterSignal(-1, ::open, file, flags, 0666);
+  int target_fd = FileSystem::Instance().Open(file, flags, 0666);
 
   if (target_fd == -1)
 ExitWithError(error_fd, "DupDescriptor-open");
Index: lldb/source/Host/posix/PipePosix.cpp
===
--- lldb/source/Host/posix/PipePosix.cpp
+++ lldb/source/Host/posix/PipePosix.cpp
@@ -7,11 +7,11 @@
 //===--===//
 
 #include "lldb/Host/posix/PipePosix.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/HostInfo.h"
 #include "lldb/Utility/SelectHelper.h"
 #include "llvm/ADT/SmallString.h"
 #include "llvm/Support/Errno.h"
-#include "llvm/Support/FileSystem.h"
 #include 
 #include 
 
@@ -148,7 +148,7 @@
 flags |= O_CLOEXEC;
 
   Status error;
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, name.str().c_str(), flags);
+  int fd = FileSystem::Instance().Open(name.str().c_str(), flags);
   if (fd != -1)
 m_fds[READ] = fd;
   else
Index: lldb/source/Host/posix/FileSystemPosix.cpp
===
--- lldb/source/Host/posix/FileSystemPosix.cpp
+++ lldb/source/Host/posix/FileSystemPosix.cpp
@@ -77,5 +77,8 @@
 }
 
 int FileSystem::Open(const char *path, int flags, int mode) {
-  return llvm::sys::RetryAfterSignal(-1, ::open, path, flags, mode);
+  // Call ::open in a lambda to avoid overload resolution in RetryAfterSignal
+  // when open is overloaded, such as in Bionic.
+  auto lambda = [&]() { return ::open(path, flags, mode); };
+  return llvm::sys::RetryAfterSignal(-1, lambda);
 }
Index: lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
===
--- lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
+++ lldb/source/Host/posix/ConnectionFileDescriptorPosix.cpp
@@ -15,6 +15,7 @@
 
 #include "lldb/Host/posix/ConnectionFileDescriptorPosix.h"
 #include "lldb/Host/Config.h"
+#include "lldb/Host/FileSystem.h"
 #include "lldb/Host/Socket.h"
 #include "lldb/Host/SocketAddress.h"
 #include "lldb/Utility/LLDBLog.h"
@@ -726,7 +727,7 @@
 #if LLDB_ENABLE_POSIX
   std::string addr_str = s.str();
   // file:///PATH
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, addr_str.c_str(), O_RDWR);
+  int fd = FileSystem::Instance().Open(addr_str.c_str(), O_RDWR);
   if (fd == -1) {
 if (error_ptr)
   error_ptr->SetErrorToErrno();
@@ -776,7 +777,7 @@
 return eConnectionStatusError;
   }
 
-  int fd = llvm::sys::RetryAfterSignal(-1, ::open, path.str().c_str(), O_RDWR);
+  int fd = FileSystem::Instance().Open(path.str().c_str(), O_RDWR);
   if (fd == -1) {
 if (error_ptr)
   error_ptr->SetErrorToErrno();
Index: lldb/source/Host/common/PseudoTerminal.cpp
===
--- lldb/source/Host/common/PseudoTerminal.cpp
+++ lldb/source/Host/common/PseudoTerminal.cpp
@@ -8,6 +8,7 @@
 
 #include "lldb/Host/PseudoTerminal.h"
 #include "lldb/Host/Config.h"
+#include "lldb/Host/FileSystem.h"
 #include "llvm/Support/Errc.h"
 #include "llvm/Support/Errno.h"
 #include 
@@ -95,7 +96,7 @@
   CloseSecondaryFileDescriptor();
 
   std::string name = GetSecondaryName();
-  m_secondary_fd = llvm::sys::RetryAfterSignal(-1, ::open, name.c_str(), oflag);
+  m_secondary_fd = FileSystem::Instance().Open(name.c_str(), oflag);
   if (m_secondary_fd >= 0)
 return llvm::Error::success();
 
Index: lldb/include/lldb/Host/FileSystem.h

[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

oh wait, `FileSystem::Open` is already `RetryAfterSignal::Open` in this diff.
will replace `RetryAfterSignal::Open` with `FileSystem::Open`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D152712: [lldb][Android] Use a lambda for calls to ::open in RetryAfterSignal

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack added a comment.

@JDevlieghere does `FileSystem::RetryAfterSignal::Open` sound good to you?
Or `FileSystem::RetryAfterSignalOpen`?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152712

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


[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.run-as

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531398.
splhack added a comment.

remove 'Depends on' from commit message


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -52,6 +52,7 @@
   }
 
   MOCK_METHOD0(GetAdbClient, AdbClientSP());
+  MOCK_METHOD0(GetPropertyRunAs, llvm::StringRef());
 };
 
 } // namespace
@@ -100,6 +101,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -152,3 +179,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(2)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client0)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client1);
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformRunAs: Property<"run-as", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientSP;
   virtual AdbClientSP GetAdbClient();
 
+  virtual llvm::StringRef GetPropertyRunAs();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
@@ -81,7 +87,7 @@
   uint32_t m_sdk_version;
 };
 
-} // namespace platofor_android
+} // namespace platform_android

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531396.
splhack added a comment.

remove 'Depend on' from commit message


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

Files:
  lldb/source/Plugins/Platform/Android/AdbClient.h
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/unittests/Platform/Android/CMakeLists.txt
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- /dev/null
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -0,0 +1,154 @@
+//===-- PlatformAndroidTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Utility/Connection.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace testing;
+
+namespace {
+
+class MockSyncService : public AdbClient::SyncService {
+public:
+  MockSyncService() : SyncService(std::move(m_mock_conn)) {}
+
+  MOCK_METHOD2(PullFile,
+   Status(const FileSpec _file, const FileSpec _file));
+  MOCK_METHOD4(Stat, Status(const FileSpec _file, uint32_t ,
+uint32_t , uint32_t ));
+
+private:
+  std::unique_ptr m_mock_conn;
+};
+
+typedef std::unique_ptr SyncServiceSP;
+
+class MockAdbClient : public AdbClient {
+public:
+  explicit MockAdbClient() : AdbClient("mock") {}
+
+  MOCK_METHOD3(ShellToFile,
+   Status(const char *command, std::chrono::milliseconds timeout,
+  const FileSpec _file_spec));
+  MOCK_METHOD1(GetSyncService, SyncServiceSP(Status ));
+};
+
+class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
+public:
+  PlatformAndroidTest() : PlatformAndroid(false) {
+m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
+  }
+
+  MOCK_METHOD0(GetAdbClient, AdbClientSP());
+};
+
+} // namespace
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/system/lib64/libc.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/system/lib64/libc.so"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFile) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/data/local/tmp/test"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(GetFile(FileSpec("/data/local/tmp/test"), FileSpec()).Success());
+}
+

[Lldb-commits] [PATCH] D152759: [lldb][Android] Support zip .so file

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531394.
splhack added a comment.

remove 'Depends on' from commit message.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152759

Files:
  lldb/include/lldb/Host/common/ZipFileResolver.h
  lldb/include/lldb/Utility/ZipFile.h
  lldb/source/Host/CMakeLists.txt
  lldb/source/Host/common/ZipFileResolver.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerCommon.cpp
  lldb/source/Utility/CMakeLists.txt
  lldb/source/Utility/ZipFile.cpp
  lldb/unittests/Host/CMakeLists.txt
  lldb/unittests/Host/common/CMakeLists.txt
  lldb/unittests/Host/common/Inputs/zip-test.zip
  lldb/unittests/Host/common/ZipFileResolverTest.cpp

Index: lldb/unittests/Host/common/ZipFileResolverTest.cpp
===
--- /dev/null
+++ lldb/unittests/Host/common/ZipFileResolverTest.cpp
@@ -0,0 +1,72 @@
+//===-- ZipFileResolverTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Host/common/ZipFileResolver.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "gtest/gtest.h"
+
+using namespace lldb_private;
+using namespace llvm;
+
+namespace {
+class ZipFileResolverTest : public ::testing::Test {
+  SubsystemRAII subsystems;
+};
+
+std::string TestZipPath() {
+  FileSpec zip_spec(GetInputFilePath("zip-test.zip"));
+  FileSystem::Instance().Resolve(zip_spec);
+  return zip_spec.GetPath();
+}
+} // namespace
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithNormalFile) {
+  const FileSpec file_spec("/system/lib64/libtest.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindNormal);
+  EXPECT_EQ(file_path, file_spec.GetPath());
+  EXPECT_EQ(file_offset, 0UL);
+  EXPECT_EQ(file_size, 0UL);
+}
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithZipMissing) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libmissing.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_FALSE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+}
+
+TEST_F(ZipFileResolverTest, ResolveBionicPathWithZipExisting) {
+  const std::string zip_path = TestZipPath();
+  const FileSpec file_spec(zip_path + "!/lib/arm64-v8a/libzip-test.so");
+
+  ZipFileResolver::FileKind file_kind;
+  std::string file_path;
+  lldb::offset_t file_offset;
+  lldb::offset_t file_size;
+  ASSERT_TRUE(ZipFileResolver::ResolveBionicPath(
+  file_spec, file_kind, file_path, file_offset, file_size));
+
+  EXPECT_EQ(file_kind, ZipFileResolver::FileKind::eFileKindZip);
+  EXPECT_EQ(file_path, zip_path);
+  EXPECT_EQ(file_offset, 4096UL);
+  EXPECT_EQ(file_size, 3600UL);
+}
Index: lldb/unittests/Host/common/CMakeLists.txt
===
--- /dev/null
+++ lldb/unittests/Host/common/CMakeLists.txt
@@ -0,0 +1,15 @@
+set (FILES
+  ZipFileResolverTest.cpp
+)
+
+add_lldb_unittest(HostCommonTests
+  ${FILES}
+  LINK_LIBS
+lldbHost
+lldbUtilityHelpers
+  )
+
+set(test_inputs
+  zip-test.zip
+  )
+add_unittest_inputs(HostCommonTests "${test_inputs}")
Index: lldb/unittests/Host/CMakeLists.txt
===
--- lldb/unittests/Host/CMakeLists.txt
+++ lldb/unittests/Host/CMakeLists.txt
@@ -38,3 +38,5 @@
 LLVMTestingSupport
 LLVMTargetParser
   )
+
+add_subdirectory(common)
Index: lldb/source/Utility/ZipFile.cpp
===
--- /dev/null
+++ lldb/source/Utility/ZipFile.cpp
@@ -0,0 +1,180 @@
+//===-- ZipFile.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "lldb/Utility/ZipFile.h"
+#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/FileSpec.h"
+#include "llvm/Support/Endian.h"
+
+using namespace lldb_private;
+using namespace llvm::support;
+
+namespace 

[Lldb-commits] [PATCH] D152933: [lldb][Android] Add platform.plugin.remote-android.run-as

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531392.
splhack added a comment.
Herald added a subscriber: JDevlieghere.

rebase onto D152855 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152933

Files:
  lldb/source/Plugins/Platform/Android/CMakeLists.txt
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -52,6 +52,7 @@
   }
 
   MOCK_METHOD0(GetAdbClient, AdbClientSP());
+  MOCK_METHOD0(GetPropertyRunAs, llvm::StringRef());
 };
 
 } // namespace
@@ -100,6 +101,32 @@
   .Success());
 }
 
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFileAndRunAs) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("run-as 'com.example.test' "
+"dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.test")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
 TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
   auto sync_service = new MockSyncService();
   EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
@@ -152,3 +179,40 @@
   FileSpec())
   .Success());
 }
+
+TEST_F(PlatformAndroidTest, GetFileWithCatFallbackAndRunAs) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(
+  *sync_service,
+  Stat(FileSpec("/data/data/com.example.app/lib-main/libtest.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(0), Return(Status(;
+
+  auto adb_client0 = new MockAdbClient();
+  EXPECT_CALL(*adb_client0, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  auto adb_client1 = new MockAdbClient();
+  EXPECT_CALL(
+  *adb_client1,
+  ShellToFile(StrEq("run-as 'com.example.app' "
+"cat '/data/data/com.example.app/lib-main/libtest.so'"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetPropertyRunAs())
+  .Times(1)
+  .WillOnce(Return(llvm::StringRef("com.example.app")));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(2)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client0)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client1);
+
+  EXPECT_TRUE(
+  GetFile(FileSpec("/data/data/com.example.app/lib-main/libtest.so"),
+  FileSpec())
+  .Success());
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
===
--- /dev/null
+++ lldb/source/Plugins/Platform/Android/PlatformAndroidProperties.td
@@ -0,0 +1,9 @@
+include "../../../../include/lldb/Core/PropertiesBase.td"
+
+let Definition = "android" in {
+  def PlatformRunAs: Property<"run-as", "String">,
+Global,
+DefaultStringValue<"">,
+Desc<"Specify package name to run adb shell command with 'run-as' as the "
+ "package user when necessary (e.g. to get file with 'cat' and 'dd').">;
+}
Index: lldb/source/Plugins/Platform/Android/PlatformAndroid.h
===
--- lldb/source/Plugins/Platform/Android/PlatformAndroid.h
+++ lldb/source/Plugins/Platform/Android/PlatformAndroid.h
@@ -30,6 +30,8 @@
   // lldb_private::PluginInterface functions
   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
 
+  static void DebuggerInitialize(lldb_private::Debugger );
+
   static llvm::StringRef GetPluginNameStatic(bool is_host) {
 return is_host ? Platform::GetHostPlatformName() : "remote-android";
   }
@@ -73,6 +75,10 @@
   typedef std::unique_ptr AdbClientSP;
   virtual AdbClientSP GetAdbClient();
 
+  virtual llvm::StringRef GetPropertyRunAs();
+
+  std::string GetRunAs();
+
 private:
   AdbClient::SyncService *GetSyncService(Status );
 
@@ -81,7 +87,7 @@
   uint32_t m_sdk_version;
 };
 
-} // 

[Lldb-commits] [PATCH] D152855: [lldb][Android] Add PlatformAndroidTest

2023-06-14 Thread Kazuki Sakamoto via Phabricator via lldb-commits
splhack updated this revision to Diff 531389.
splhack added a comment.

rebase onto D152759 


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D152855

Files:
  lldb/source/Plugins/Platform/Android/AdbClient.h
  lldb/source/Plugins/Platform/Android/PlatformAndroid.cpp
  lldb/source/Plugins/Platform/Android/PlatformAndroid.h
  lldb/unittests/Platform/Android/CMakeLists.txt
  lldb/unittests/Platform/Android/PlatformAndroidTest.cpp

Index: lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
===
--- /dev/null
+++ lldb/unittests/Platform/Android/PlatformAndroidTest.cpp
@@ -0,0 +1,154 @@
+//===-- PlatformAndroidTest.cpp ---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+#include "Plugins/Platform/Android/PlatformAndroid.h"
+#include "Plugins/Platform/Android/PlatformAndroidRemoteGDBServer.h"
+#include "TestingSupport/SubsystemRAII.h"
+#include "TestingSupport/TestUtilities.h"
+#include "lldb/Utility/Connection.h"
+#include "gmock/gmock.h"
+
+using namespace lldb;
+using namespace lldb_private;
+using namespace lldb_private::platform_android;
+using namespace testing;
+
+namespace {
+
+class MockSyncService : public AdbClient::SyncService {
+public:
+  MockSyncService() : SyncService(std::move(m_mock_conn)) {}
+
+  MOCK_METHOD2(PullFile,
+   Status(const FileSpec _file, const FileSpec _file));
+  MOCK_METHOD4(Stat, Status(const FileSpec _file, uint32_t ,
+uint32_t , uint32_t ));
+
+private:
+  std::unique_ptr m_mock_conn;
+};
+
+typedef std::unique_ptr SyncServiceSP;
+
+class MockAdbClient : public AdbClient {
+public:
+  explicit MockAdbClient() : AdbClient("mock") {}
+
+  MOCK_METHOD3(ShellToFile,
+   Status(const char *command, std::chrono::milliseconds timeout,
+  const FileSpec _file_spec));
+  MOCK_METHOD1(GetSyncService, SyncServiceSP(Status ));
+};
+
+class PlatformAndroidTest : public PlatformAndroid, public ::testing::Test {
+public:
+  PlatformAndroidTest() : PlatformAndroid(false) {
+m_remote_platform_sp = PlatformSP(new PlatformAndroidRemoteGDBServer());
+  }
+
+  MOCK_METHOD0(GetAdbClient, AdbClientSP());
+};
+
+} // namespace
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/system/lib64/libc.so"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/system/lib64/libc.so"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(FileSpec("/system/lib64/libc.so"), 0, 0, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, DownloadModuleSliceWithZipFile) {
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client,
+  ShellToFile(StrEq("dd if='/system/app/Test/Test.apk' "
+"iflag=skip_bytes,count_bytes "
+"skip=4096 count=3600 status=none"),
+  _, _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(
+  DownloadModuleSlice(
+  FileSpec("/system/app/Test/Test.apk!/lib/arm64-v8a/libtest.so"), 4096,
+  3600, FileSpec())
+  .Success());
+}
+
+TEST_F(PlatformAndroidTest, GetFileWithNormalFile) {
+  auto sync_service = new MockSyncService();
+  EXPECT_CALL(*sync_service, Stat(FileSpec("/data/local/tmp/test"), _, _, _))
+  .Times(1)
+  .WillOnce(DoAll(SetArgReferee<1>(1), Return(Status(;
+  EXPECT_CALL(*sync_service, PullFile(FileSpec("/data/local/tmp/test"), _))
+  .Times(1)
+  .WillOnce(Return(Status()));
+
+  auto adb_client = new MockAdbClient();
+  EXPECT_CALL(*adb_client, GetSyncService(_))
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(SyncServiceSP(sync_service);
+
+  EXPECT_CALL(*this, GetAdbClient())
+  .Times(1)
+  .WillOnce(Return(ByMove(std::move(AdbClientSP(adb_client);
+
+  EXPECT_TRUE(GetFile(FileSpec("/data/local/tmp/test"), FileSpec()).Success());
+}
+

  1   2   >