[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-10 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

With this patch, it looks like the test added in this patch fails for 
lldb-arm-ubuntu:
https://lab.llvm.org/buildbot/#/builders/17/builds/10498

Everything passes on these buildbots:  lldb-aarch64-ubuntu, 
lldb-x64-windows-ninja, lldb-x86_64-debian.

The test logic checks for breakpoint for main as well as for shared library 
load. It is able to extract that information using thread.GetFrameAtIndex(0). 
However, when it gets the signal, it is not able to extract the back trace and 
hence fails with the following error:

  ERROR: test (TestDyldLaunchLinux.TestLinux64LaunchingViaDynamicLoader)
  Traceback (most recent call last):
File 
"/home/tcwg-buildslave/worker/lldb-arm-ubuntu/llvm-project/lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py",
 line 57, in test
  self.assertIn("raise", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
File 
"/home/tcwg-buildslave/worker/lldb-arm-ubuntu/llvm-project/lldb/third_party/Python/module/unittest2/unittest2/case.py",
 line 884, in assertIn
  if member not in container:
  TypeError: argument of type 'NoneType' is not iterable
  Config=arm-/home/tcwg-buildslave/worker/lldb-arm-ubuntu/build/bin/clang

@DavidSpickett, @zatrazz: If possible, could you please take a look. Is this 
expected behavior and test should be marked as unsupported for arm, or am I 
missing something here?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-10 Thread Rumeet Dhindsa via Phabricator via lldb-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG03df97101287: [lldb] Add support for debugging via the 
dynamic linker. (authored by rdhindsa).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.h
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash();
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,6 @@
+#include "signal_file.h"
+
+int main() {
+  // Break here
+  return get_signal_crash();
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,58 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+@skipIf(oslist=no_match(['linux']))
+@no_debug_info_test
+def test(self):
+self.build()
+
+# Extracts path of the interpreter.
+spec = lldb.SBModuleSpec()
+spec.SetFileSpec(lldb.SBFileSpec(self.getBuildArtifact("a.out")))
+interp_section = lldb.SBModule(spec).FindSection(".interp")
+if not interp_section:
+  return
+section_data = interp_section.GetSectionData()
+error = lldb.SBError()
+exe = section_data.GetString(error,0)
+if error.Fail():
+  return
+
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+
+# Set breakpoints both on shared library function as well as on
+# main. Both of them will be pending breakpoints.
+breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
+breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
+launch_info = lldb.SBLaunchInfo([ "--library-path", self.get_process_working_directory(), self.getBuildArtifact("a.out")])
+launch_info.SetWorkingDirectory(self.get_process_working_directory())
+error = lldb.SBError()
+process = target.Launch(launch_info, error)
+self.assertTrue(error.Success())
+
+# Stopped on main here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped on get_signal_crash function here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped because of generated signal.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("raise", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(1).GetDisplayFunctionName())
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/Makefile
@@ -0,0 +1,4 @@
+CXX_SOURCES := main.cpp
+DYLIB_NAME := signal_file
+DYLIB_CXX_SOURCES := signal_file.cpp
+include Makefile.rules

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-10 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

@teemperor: I believe I have addressed your comments. If there is any other 
concern, I can address it post-commit.

Thank you everyone for the review and help with the patch!


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-09 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 371644.
rdhindsa marked 6 inline comments as done.
rdhindsa added a comment.

Thanks Pavel for investigating and figuring out that the upstream failing test 
had libxml support disabled. I was able to reproduce the failure locally by 
adding -DLLDB_ENABLE_LIBXML2=False to cmake. 
Updated UpdateFileSpecIfNecessary function to only update filename if the 
region.GetName().AsCString()  is not empty, which checks for both null and 
empty.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.h
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash();
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,6 @@
+#include "signal_file.h"
+
+int main() {
+  // Break here
+  return get_signal_crash();
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,58 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+@skipIf(oslist=no_match(['linux']))
+@no_debug_info_test
+def test(self):
+self.build()
+
+# Extracts path of the interpreter.
+spec = lldb.SBModuleSpec()
+spec.SetFileSpec(lldb.SBFileSpec(self.getBuildArtifact("a.out")))
+interp_section = lldb.SBModule(spec).FindSection(".interp")
+if not interp_section:
+  return
+section_data = interp_section.GetSectionData()
+error = lldb.SBError()
+exe = section_data.GetString(error,0)
+if error.Fail():
+  return
+
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+
+# Set breakpoints both on shared library function as well as on
+# main. Both of them will be pending breakpoints.
+breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
+breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
+launch_info = lldb.SBLaunchInfo([ "--library-path", self.get_process_working_directory(), self.getBuildArtifact("a.out")])
+launch_info.SetWorkingDirectory(self.get_process_working_directory())
+error = lldb.SBError()
+process = target.Launch(launch_info, error)
+self.assertTrue(error.Success())
+
+# Stopped on main here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped on get_signal_crash function here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped because of generated signal.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("raise", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(1).GetDisplayFunctionName())
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ 

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-08 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 371425.
rdhindsa marked 2 inline comments as done.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.h
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash();
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,6 @@
+#include "signal_file.h"
+
+int main() {
+  // Break here
+  return get_signal_crash();
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,52 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+@skipIf(oslist=no_match(['linux']))
+@no_debug_info_test
+def test(self):
+self.build()
+candidates = [
+"/lib64/ld-linux-x86-64.so.2",
+"/usr/lib/ld-linux-x86-64.so.2"
+]
+exe = next((c for c in candidates if os.path.exists(c)), None)
+if not os.path.exists(exe):
+return
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+
+# Set breakpoints both on shared library function as well as on
+# main. Both of them will be pending breakpoints.
+breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
+breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
+launch_info = lldb.SBLaunchInfo([ "--library-path", self.get_process_working_directory(), self.getBuildArtifact("a.out")])
+launch_info.SetWorkingDirectory(self.get_process_working_directory())
+error = lldb.SBError()
+process = target.Launch(launch_info, error)
+self.assertTrue(error.Success())
+
+# Stopped on main here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped on get_signal_crash function here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped because of generated signal.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("raise", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(1).GetDisplayFunctionName())
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/Makefile
@@ -0,0 +1,4 @@
+CXX_SOURCES := main.cpp
+DYLIB_NAME := signal_file
+DYLIB_CXX_SOURCES := signal_file.cpp
+include Makefile.rules
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -333,28 +333,37 @@
 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
   "Trying to locate rendezvous 

[Lldb-commits] [PATCH] D109249: [lldb] Add Getdescription function for SBInstruction.

2021-09-03 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 370660.
rdhindsa marked an inline comment as done.
rdhindsa added a comment.

Added LLDB_REGISTER_METHOD for GetDescription.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D109249

Files:
  lldb/bindings/interface/SBInstruction.i
  lldb/include/lldb/API/SBInstruction.h
  lldb/source/API/SBInstruction.cpp
  lldb/test/API/functionalities/disassemble/Makefile
  lldb/test/API/functionalities/disassemble/TestDisassemble.py
  lldb/test/API/functionalities/disassemble/main.cpp

Index: lldb/test/API/functionalities/disassemble/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/main.cpp
@@ -0,0 +1,6 @@
+#include 
+int main() {
+  // Break here
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/disassemble/TestDisassemble.py
===
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/TestDisassemble.py
@@ -0,0 +1,26 @@
+"""
+Test that description for instruction can print address if target is provided.
+"""
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class TestDisassembledInstructionPrint(TestBase):
+  mydir = TestBase.compute_mydir(__file__)
+
+  def test(self):
+
+self.build()
+exe = self.getBuildArtifact("a.out")
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+self.runCmd("run", RUN_SUCCEEDED)
+
+thread = target.GetProcess().GetSelectedThread()
+instructions = target.ReadInstructions(thread.GetFrameAtIndex(0).GetPCAddress(),1)
+instr = instructions.GetInstructionAtIndex(0)
+strm_instr = lldb.SBStream()
+instr.GetDescription(strm_instr, target)
+disasm_instr = strm_instr.GetData()
+self.assertTrue(disasm_instr.startswith("0x"))
Index: lldb/test/API/functionalities/disassemble/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules
Index: lldb/source/API/SBInstruction.cpp
===
--- lldb/source/API/SBInstruction.cpp
+++ lldb/source/API/SBInstruction.cpp
@@ -256,6 +256,33 @@
   return false;
 }
 
+bool SBInstruction::GetDescription(lldb::SBStream , SBTarget target) {
+  LLDB_RECORD_METHOD(bool, SBInstruction, GetDescription,
+ (lldb::SBStream &, lldb::SBTarget), s, target);
+
+  lldb::InstructionSP inst_sp(GetOpaque());
+  if (inst_sp) {
+ExecutionContext exe_ctx;
+TargetSP target_sp(target.GetSP());
+std::unique_lock lock;
+if (target_sp) {
+  lock = std::unique_lock(target_sp->GetAPIMutex());
+  target_sp->CalculateExecutionContext(exe_ctx);
+}
+SymbolContext sc;
+const Address  = inst_sp->GetAddress();
+ModuleSP module_sp(addr.GetModule());
+if (module_sp)
+  module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
+sc);
+FormatEntity::Entry format;
+FormatEntity::Parse("${addr}:", format);
+inst_sp->Dump((), 0, true, false, _ctx, , nullptr, , 0);
+return true;
+  }
+  return false;
+}
+
 void SBInstruction::Print(FILE *outp) {
   LLDB_RECORD_METHOD(void, SBInstruction, Print, (FILE *), outp);
   FileSP out = std::make_shared(outp, /*take_ownership=*/false);
@@ -369,6 +396,8 @@
   LLDB_REGISTER_METHOD(bool, SBInstruction, CanSetBreakpoint, ());
   LLDB_REGISTER_METHOD(bool, SBInstruction, GetDescription,
(lldb::SBStream &));
+  LLDB_REGISTER_METHOD(bool, SBInstruction, GetDescription,
+   (lldb::SBStream &, lldb::SBTarget));
   LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FILE *));
   LLDB_REGISTER_METHOD(void, SBInstruction, Print, (SBFile));
   LLDB_REGISTER_METHOD(void, SBInstruction, Print, (FileSP));
Index: lldb/include/lldb/API/SBInstruction.h
===
--- lldb/include/lldb/API/SBInstruction.h
+++ lldb/include/lldb/API/SBInstruction.h
@@ -61,6 +61,8 @@
 
   bool GetDescription(lldb::SBStream );
 
+  bool GetDescription(lldb::SBStream , lldb::SBTarget target);
+
   bool EmulateWithFrame(lldb::SBFrame , uint32_t evaluate_options);
 
   bool DumpEmulation(const char *triple); // triple is to specify the
Index: lldb/bindings/interface/SBInstruction.i
===
--- lldb/bindings/interface/SBInstruction.i
+++ lldb/bindings/interface/SBInstruction.i
@@ -68,6 +68,9 @@
 bool
 GetDescription (lldb::SBStream );
 
+bool
+GetDescription (lldb::SBStream , lldb::SBTarget target);
+
 bool
 EmulateWithFrame (lldb::SBFrame , uint32_t 

[Lldb-commits] [PATCH] D109249: [lldb] Add Getdescription function for SBInstruction.

2021-09-03 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa created this revision.
rdhindsa added reviewers: labath, clayborg.
rdhindsa requested review of this revision.
Herald added a project: LLDB.

Add another Getdescription function for SBInstruction.

When trying to print the instruction, existing GetDescription function doesn't 
have access to Execution Context. Hence, it prints instructions of the 
following form for the added test case:
libc.so.6[0x3bce1]: movq   0x108(%rsp), %rax

When a target is passed with this new API call, it is able to extract execution 
context and hence it prints in the following form:
0x77af6ce1:movq   0x108(%rsp), %rax


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D109249

Files:
  lldb/bindings/interface/SBInstruction.i
  lldb/include/lldb/API/SBInstruction.h
  lldb/source/API/SBInstruction.cpp
  lldb/test/API/functionalities/disassemble/Makefile
  lldb/test/API/functionalities/disassemble/TestDisassemble.py
  lldb/test/API/functionalities/disassemble/main.cpp

Index: lldb/test/API/functionalities/disassemble/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/main.cpp
@@ -0,0 +1,6 @@
+#include 
+int main() {
+  // Break here
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/disassemble/TestDisassemble.py
===
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/TestDisassemble.py
@@ -0,0 +1,26 @@
+"""
+Test that description for instruction can print address if target is provided.
+"""
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class TestDisassembledInstructionPrint(TestBase):
+  mydir = TestBase.compute_mydir(__file__)
+
+  def test(self):
+
+self.build()
+exe = self.getBuildArtifact("a.out")
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+self.runCmd("run", RUN_SUCCEEDED)
+
+thread = target.GetProcess().GetSelectedThread()
+instructions = target.ReadInstructions(thread.GetFrameAtIndex(0).GetPCAddress(),1)
+instr = instructions.GetInstructionAtIndex(0)
+strm_instr = lldb.SBStream()
+instr.GetDescription(strm_instr, target)
+disasm_instr = strm_instr.GetData()
+self.assertTrue(disasm_instr.startswith("0x"))
Index: lldb/test/API/functionalities/disassemble/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/disassemble/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules
Index: lldb/source/API/SBInstruction.cpp
===
--- lldb/source/API/SBInstruction.cpp
+++ lldb/source/API/SBInstruction.cpp
@@ -256,6 +256,33 @@
   return false;
 }
 
+bool SBInstruction::GetDescription(lldb::SBStream , SBTarget target) {
+  LLDB_RECORD_METHOD(bool, SBInstruction, GetDescription,
+ (lldb::SBStream &, lldb::SBTarget), s, target);
+
+  lldb::InstructionSP inst_sp(GetOpaque());
+  if (inst_sp) {
+ExecutionContext exe_ctx;
+TargetSP target_sp(target.GetSP());
+std::unique_lock lock;
+if (target_sp) {
+  lock = std::unique_lock(target_sp->GetAPIMutex());
+  target_sp->CalculateExecutionContext(exe_ctx);
+}
+SymbolContext sc;
+const Address  = inst_sp->GetAddress();
+ModuleSP module_sp(addr.GetModule());
+if (module_sp)
+  module_sp->ResolveSymbolContextForAddress(addr, eSymbolContextEverything,
+sc);
+FormatEntity::Entry format;
+FormatEntity::Parse("${addr}:", format);
+inst_sp->Dump((), 0, true, false, _ctx, , nullptr, , 0);
+return true;
+  }
+  return false;
+}
+
 void SBInstruction::Print(FILE *outp) {
   LLDB_RECORD_METHOD(void, SBInstruction, Print, (FILE *), outp);
   FileSP out = std::make_shared(outp, /*take_ownership=*/false);
Index: lldb/include/lldb/API/SBInstruction.h
===
--- lldb/include/lldb/API/SBInstruction.h
+++ lldb/include/lldb/API/SBInstruction.h
@@ -61,6 +61,8 @@
 
   bool GetDescription(lldb::SBStream );
 
+  bool GetDescription(lldb::SBStream , lldb::SBTarget target);
+
   bool EmulateWithFrame(lldb::SBFrame , uint32_t evaluate_options);
 
   bool DumpEmulation(const char *triple); // triple is to specify the
Index: lldb/bindings/interface/SBInstruction.i
===
--- lldb/bindings/interface/SBInstruction.i
+++ lldb/bindings/interface/SBInstruction.i
@@ -68,6 +68,9 @@
 bool
 GetDescription (lldb::SBStream );
 
+bool
+GetDescription (lldb::SBStream , lldb::SBTarget target);
+
 bool
 EmulateWithFrame (lldb::SBFrame , uint32_t evaluate_options);
 
___
lldb-commits mailing list

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-02 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

Thank you for suggestion to try! 
After adding the assertions in memory region info trick, 3 testcases from only 
one test failed because of assertion : 
lldb/test/API/functionalities/load_unload/TestLoadUnload.py

test_lldb_process_load_and_unload_commands
test_load_unload
test_step_over_load

File path found for these test cases corresponds to the respective a.out files: 
 
lldb-test-build.noindex/functionalities/load_unload/TestLoadUnload.test_lldb_process_load_and_unload_commands/a.out
lldb-test-build.noindex/functionalities/load_unload/TestLoadUnload.test_load_unload/a.out
 
lldb-test-build.noindex/functionalities/load_unload/TestLoadUnload.test_step_over_load/a.out

So this test seems to be working as expected with the patch. It passes if I 
remove the assertions. However, I didn't find any other test failing due to 
assertions. Even the multithreaded test passes with assertions.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-09-01 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

Thank you for giving it a try! It passes for me for both Release and 
RelWithDebInfo along with asserts enabled in each case. I can give it a try by 
recommitting and checking the status, this patch is in `Needs Review` status 
presently. Hence, requesting for a review.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-30 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

For me, this test passes every time. I have tried multiple times with 
instructions from failing bot, but haven't been able to reproduce the failure 
locally yet.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-26 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa marked 2 inline comments as done and an inline comment as not done.
rdhindsa added a comment.

This patch caused a test failure when it was submitted two days ago, so I 
reverted it that day. However, I have not been able to reproduce the failure 
locally even using the cmake config from bot.

Failure was in api/multithreaded/TestMultithreaded.py

Failing bot link: 
https://lab.llvm.org/buildbot/#/builders/68/builds/17556/steps/6/logs/stdio

I would appreciate any ideas to be able to repro it locally. The email from 
buildbot showed Sigsegv:

  Traceback (most recent call last):
File 
"/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/packages/Python/lldbsuite/test/decorators.py",
 line 149, in wrapper
  return func(*args, **kwargs)
File 
"/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/api/multithreaded/TestMultithreaded.py",
 line 37, in test_python_stop_hook
  'test_python_stop_hook')
File 
"/home/worker/2.0.1/lldb-x86_64-debian/llvm-project/lldb/test/API/api/multithreaded/TestMultithreaded.py",
 line 116, in build_and_test
  check_call(exe, env=env, stdout=fnull, stderr=fnull)
File "/usr/lib/python3.7/subprocess.py", line 347, in check_call
  raise CalledProcessError(retcode, cmd)
  subprocess.CalledProcessError: Command 
'['/home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/api/multithreaded/TestMultithreaded.test_python_stop_hook/test_python_stop_hook',
 
'/home/worker/2.0.1/lldb-x86_64-debian/build/lldb-test-build.noindex/api/multithreaded/TestMultithreaded.test_python_stop_hook/inferior_program']'
 died with .
  Config=x86_64-/home/worker/2.0.1/lldb-x86_64-debian/build/bin/clang


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-26 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 368988.
rdhindsa marked 6 inline comments as done.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash();
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,6 @@
+#include "signal_file.h"
+
+int main() {
+  // Break here
+  return get_signal_crash();
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,52 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+@skipIf(oslist=no_match(['linux']))
+@no_debug_info_test
+def test(self):
+self.build()
+candidates = [
+"/lib64/ld-linux-x86-64.so.2",
+"/usr/lib/ld-linux-x86-64.so.2"
+]
+exe = next((c for c in candidates if os.path.exists(c)), None)
+if not os.path.exists(exe):
+return
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+
+# Set breakpoints both on shared library function as well as on
+# main. Both of them will be pending breakpoints.
+breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
+breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
+launch_info = lldb.SBLaunchInfo([ "--library-path", self.get_process_working_directory(), self.getBuildArtifact("a.out")])
+launch_info.SetWorkingDirectory(self.get_process_working_directory())
+error = lldb.SBError()
+process = target.Launch(launch_info, error)
+self.assertTrue(error.Success())
+
+# Stopped on main here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertIn("main", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped on get_signal_crash function here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped because of generated signal.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("raise", thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash", thread.GetFrameAtIndex(1).GetDisplayFunctionName())
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/Makefile
@@ -0,0 +1,4 @@
+CXX_SOURCES := main.cpp
+DYLIB_NAME := signal_file
+DYLIB_CXX_SOURCES := signal_file.cpp
+include Makefile.rules
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -333,28 +333,48 @@
 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
   "Trying to locate rendezvous breakpoint in the interpreter "
   "by symbol name.");

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-24 Thread Rumeet Dhindsa via Phabricator via lldb-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rG1cbdc07ec015: [lldb] Add support for debugging via the 
dynamic linker. (authored by rdhindsa).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash(void);
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,6 @@
+#include "signal_file.h"
+
+int main() {
+  // Break here
+  return get_signal_crash();
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,46 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.lldbtest import *
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+def test(self):
+self.build()
+exe = "/lib64/ld-linux-x86-64.so.2"
+if(os.path.exists(exe)):
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+
+# Set breakpoints both on shared library function as well as on
+# main. Both of them will be pending breakpoints.
+breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
+breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
+launch_info = lldb.SBLaunchInfo([ "--library-path",self.get_process_working_directory(),self.getBuildArtifact("a.out")])
+launch_info.SetWorkingDirectory(self.get_process_working_directory())
+error = lldb.SBError()
+process = target.Launch(launch_info,error)
+self.assertTrue(error.Success())
+
+# Stopped on main here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertIn("main",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped on get_signal_crash function here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("get_signal_crash",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped because of generated signal.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("raise",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash",thread.GetFrameAtIndex(1).GetDisplayFunctionName())
+
+
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/Makefile
@@ -0,0 +1,15 @@
+CXX_SOURCES := main.cpp
+LD_EXTRAS := -Wl,-rpath "-Wl,$(shell pwd)"
+USE_LIBDL := 1
+
+include Makefile.rules
+
+# The following shared library will be used to test breakpoints under dynamic loading
+libsignal_file.so:  signal_file.cpp
+	$(MAKE) -f $(MAKEFILE_RULES) \
+		DYLIB_ONLY=YES DYLIB_CXX_SOURCES=signal_file.cpp DYLIB_NAME=signal_file
+
+a.out: libsignal_file.so main.cpp
+	$(MAKE) -f $(MAKEFILE_RULES) \
+CXX_SOURCES=main.cpp LD_EXTRAS=libsignal_file.so
+
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- 

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-24 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

Thank you for the review and suggestion to check memory region.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-19 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 367715.
rdhindsa added a comment.

Updated to extract executable file name using GetMemoryRegionInfo since ld.so 
returns empty file name for executable file name.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash(void);
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,6 @@
+#include "signal_file.h"
+
+int main() {
+  // Break here
+  return get_signal_crash();
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,46 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.lldbtest import *
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+def test(self):
+self.build()
+exe = "/lib64/ld-linux-x86-64.so.2"
+if(os.path.exists(exe)):
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+
+# Set breakpoints both on shared library function as well as on
+# main. Both of them will be pending breakpoints.
+breakpoint_main = target.BreakpointCreateBySourceRegex("// Break here", lldb.SBFileSpec("main.cpp"))
+breakpoint_shared_library = target.BreakpointCreateBySourceRegex("get_signal_crash", lldb.SBFileSpec("signal_file.cpp"))
+launch_info = lldb.SBLaunchInfo([ "--library-path",self.get_process_working_directory(),self.getBuildArtifact("a.out")])
+launch_info.SetWorkingDirectory(self.get_process_working_directory())
+error = lldb.SBError()
+process = target.Launch(launch_info,error)
+self.assertTrue(error.Success())
+
+# Stopped on main here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertIn("main",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped on get_signal_crash function here.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("get_signal_crash",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+process.Continue()
+
+# Stopped because of generated signal.
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+self.assertIn("raise",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash",thread.GetFrameAtIndex(1).GetDisplayFunctionName())
+
+
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/Makefile
@@ -0,0 +1,15 @@
+CXX_SOURCES := main.cpp
+LD_EXTRAS := -Wl,-rpath "-Wl,$(shell pwd)"
+USE_LIBDL := 1
+
+include Makefile.rules
+
+# The following shared library will be used to test breakpoints under dynamic loading
+libsignal_file.so:  signal_file.cpp
+	$(MAKE) -f $(MAKEFILE_RULES) \
+		DYLIB_ONLY=YES DYLIB_CXX_SOURCES=signal_file.cpp DYLIB_NAME=signal_file
+
+a.out: libsignal_file.so main.cpp
+	$(MAKE) -f $(MAKEFILE_RULES) \
+CXX_SOURCES=main.cpp LD_EXTRAS=libsignal_file.so
+
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- 

[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-08-19 Thread Rumeet Dhindsa via Phabricator via lldb-commits
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
Closed by commit rGd9c5613e856c: Update logic to close inherited file 
descriptors. (authored by rdhindsa).

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105732

Files:
  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
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+const llvm::StringRef proc_fd_path = "/proc/self/fd";
+std::error_code ec;
+bool result;
+ec = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
+   iter != file_end && !ec; iter.increment(ec)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are stdin, stdout and
+// stderr.
+if (fd > 2 && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (int file_to_close : files_to_close)
+close(file_to_close);
+} else {
+  // Since /proc/self/fd didn't work, trying the slow way instead.
+  int max_fd = sysconf(_SC_OPEN_MAX);
+  for (int fd = 3; fd < max_fd; ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd)
+  close(fd);
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)


Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+const llvm::StringRef proc_fd_path = "/proc/self/fd";
+std::error_code ec;
+bool result;
+ec = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
+   iter != file_end && !ec; iter.increment(ec)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are stdin, stdout and
+// stderr.
+if (fd > 2 && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (int file_to_close : files_to_close)
+close(file_to_close);
+} else {
+  // Since /proc/self/fd didn't work, trying the slow way instead.
+  int max_fd = sysconf(_SC_OPEN_MAX);
+  for (int fd = 3; fd < max_fd; ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd)
+  close(fd);
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-19 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

Looking further as to why executable symbols are not loaded, it looks like when 
we read the link_map in lldb, AddSOEntries() is called for all the entries in 
link_map read from ld.so. The first entry's name(which corresponds to 
executable) is empty, which seems to be set by ld.so here 
(https://github.com/bminor/glibc/blob/master/elf/rtld.c#L1389). main_map at 
this location has the entry for respective executable, but its name is removed, 
hence lldb while going through entries can't read entry about main executable.

I verified that lldb does iterate through the link_map list with this patch, 
reading that file name as empty. I also verified that if respective line is 
commented in ld.so , and hence the name of executable is retained, lldb with 
this patch is able to set breakpoint on main as well. Hence, it would require 
change to glibc as well. Once that is in, we can add additional test for that 
feature later on as a follow-up patch. Does that sound okay?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-08-18 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 367311.
rdhindsa added a comment.

Updated comments as suggested.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105732

Files:
  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
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+const llvm::StringRef proc_fd_path = "/proc/self/fd";
+std::error_code ec;
+bool result;
+ec = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
+   iter != file_end && !ec; iter.increment(ec)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are stdin, stdout and
+// stderr.
+if (fd > 2 && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (int file_to_close : files_to_close)
+close(file_to_close);
+} else {
+  // Since /proc/self/fd didn't work, trying the slow way instead.
+  int max_fd = sysconf(_SC_OPEN_MAX);
+  for (int fd = 3; fd < max_fd; ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd)
+  close(fd);
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)


Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+const llvm::StringRef proc_fd_path = "/proc/self/fd";
+std::error_code ec;
+bool result;
+ec = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
+   iter != file_end && !ec; iter.increment(ec)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are stdin, stdout and
+// stderr.
+if (fd > 2 && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (int file_to_close : files_to_close)
+close(file_to_close);
+} else {
+  // Since /proc/self/fd didn't work, trying the slow way instead.
+  int max_fd = sysconf(_SC_OPEN_MAX);
+  for (int fd = 3; fd < max_fd; ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd)
+  close(fd);
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-18 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

I checked the status with both gdb and lldb using test case added here. It 
looks like breakpoint on main doesn't get hit even for gdb when called through 
ld.so. However, breakpoint on shared library functions is supported by gdb. 
With this patch, we are now able to set breakpoint on functions in shared 
libraries as supported by gdb.

  bin/lldb -- /lib64/ld-linux-x86-64.so.2 --library-path $PWD ./a.out
  (lldb) target create "/lib64/ld-linux-x86-64.so.2"
   Current executable set to '/lib64/ld-linux-x86-64.so.2' (x86_64).
  (lldb) settings set -- target.run-args  "--library-path" "" "./a.out"
  (lldb) b main
  Breakpoint 1: no locations (pending).
  WARNING:  Unable to resolve breakpoint to any actual locations.
  (lldb) b get_signal_crash
  Breakpoint 2: no locations (pending).
  WARNING:  Unable to resolve breakpoint to any actual locations.
  (lldb) run
  Process 526132 launched: '/lib64/ld-linux-x86-64.so.2' (x86_64)
  1 location added to breakpoint 2
  Process 526132 stopped
  * thread #1, name = 'ld-linux-x86-64', stop reason = breakpoint 2.1
  frame #0: 0x77fa1110 libsignal_file.so`get_signal_crash()
  
  gdb --args /lib64/ld-linux-x86-64.so.2 --library-path $PWD ./a.out
  (gdb) b main
  Function "main" not defined.
  Make breakpoint pending on future shared library load? (y or [n]) y
  Breakpoint 1 (main) pending.
  (gdb) b get_signal_crash
  Function "get_signal_crash" not defined.
  Make breakpoint pending on future shared library load? (y or [n]) y
  Breakpoint 2 (get_signal_crash) pending.
  (gdb) run
  Starting program: /lib64/ld-linux-x86-64.so.2 --library-path .. ./a.out
  Breakpoint 2, 0x77fa1114 in get_signal_crash()

This patch adds the support for shared library load. Do you think if it is okay 
if the functionality to be able to breakpoint on the main executable is added 
as next step?


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-08-18 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 367286.
rdhindsa marked 7 inline comments as done.

Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105732

Files:
  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
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+const llvm::StringRef proc_fd_path = "/proc/self/fd";
+std::error_code ec;
+bool result;
+ec = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
+   iter != file_end && !ec; iter.increment(ec)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are
+// stdin/stdout/stderr
+if (fd > 2 && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (int file_to_close : files_to_close)
+close(file_to_close);
+} else {
+  // /proc/self/fd didn't work - trying the slow way instead
+  int max_fd = sysconf(_SC_OPEN_MAX);
+  for (int fd = 3; fd < max_fd; ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd)
+  close(fd);
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)


Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+const llvm::StringRef proc_fd_path = "/proc/self/fd";
+std::error_code ec;
+bool result;
+ec = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
+   iter != file_end && !ec; iter.increment(ec)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are
+// stdin/stdout/stderr
+if (fd > 2 && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (int file_to_close : files_to_close)
+close(file_to_close);
+} else {
+  // /proc/self/fd didn't work - trying the slow way instead
+  int max_fd = sysconf(_SC_OPEN_MAX);
+  for (int fd = 3; fd < max_fd; ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd)
+  close(fd);
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-08-17 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 367067.
rdhindsa added a comment.
Herald added a project: LLDB.

Updated it to use llvm's filesystem.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D105732

Files:
  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
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+std::string proc_fd_path = "/proc/self/fd";
+std::error_code EC;
+bool result;
+EC = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, EC), FileEnd;
+   iter != FileEnd && !EC; iter.increment(EC)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are
+// stdin/stdout/stderr
+if ((fd > 2) && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (auto _to_close : files_to_close)
+close(file_to_close);
+} else {
+  // /proc/self/fd didn't work - trying the slow way instead
+  for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd) {
+  close(fd);
+}
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)


Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -14,6 +14,7 @@
 #include "lldb/Utility/FileSpec.h"
 #include "lldb/Utility/Log.h"
 #include "llvm/Support/Errno.h"
+#include "llvm/Support/FileSystem.h"
 
 #include 
 #include 
@@ -143,9 +144,32 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
+
+std::string proc_fd_path = "/proc/self/fd";
+std::error_code EC;
+bool result;
+EC = llvm::sys::fs::is_directory(proc_fd_path, result);
+if (result) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (llvm::sys::fs::directory_iterator iter(proc_fd_path, EC), FileEnd;
+   iter != FileEnd && !EC; iter.increment(EC)) {
+int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are
+// stdin/stdout/stderr
+if ((fd > 2) && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (auto _to_close : files_to_close)
+close(file_to_close);
+} else {
+  // /proc/self/fd didn't work - trying the slow way instead
+  for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd) {
+  close(fd);
+}
+}
 
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-17 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa marked an inline comment as done.
rdhindsa added a comment.

Updated the test to check for the respective function name from the shared 
library in the frames.
I didn't add run_to_source_breakpoint since we can't set a breakpoint on main 
initially before running the program.  Hence it would need to be run first and 
then we can check later to see if the function name is available. Please let me 
know if you think there is any concern with this way.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

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


[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-17 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 367047.
rdhindsa marked an inline comment as done.
rdhindsa added a comment.

Updated the test to check for the respective function name from the shared 
library in the frames.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/API/functionalities/dyld-launch-linux/Makefile
  lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
  lldb/test/API/functionalities/dyld-launch-linux/main.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
  lldb/test/API/functionalities/dyld-launch-linux/signal_file.h

Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash(void);
Index: lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/API/functionalities/dyld-launch-linux/main.cpp
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/main.cpp
@@ -0,0 +1,3 @@
+#include "signal_file.h"
+
+int main() { return get_signal_crash(); }
Index: lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/TestDyldLaunchLinux.py
@@ -0,0 +1,31 @@
+"""
+Test that LLDB can launch a linux executable through the dynamic loader and still hit a breakpoint.
+"""
+
+import lldb
+import os
+
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+class TestLinux64LaunchingViaDynamicLoader(TestBase):
+mydir = TestBase.compute_mydir(__file__)
+
+def test(self):
+"""Test we can launch and hit a breakpoint when we run our program through the dynamic loader"""
+self.build()
+exe = "/lib64/ld-linux-x86-64.so.2"
+
+if(os.path.exists(exe)):
+# Create a target by the debugger.
+target = self.dbg.CreateTarget(exe)
+self.assertTrue(target, VALID_TARGET)
+process = target.LaunchSimple(
+["--library-path",self.get_process_working_directory(),self.getBuildArtifact("a.out")], None, self.get_process_working_directory())
+
+self.assertEqual(process.GetState(), lldb.eStateStopped)
+thread = process.GetSelectedThread()
+self.assertEqual(thread.GetStopReason(), lldb.eStopReasonSignal)
+self.assertIn("raise",thread.GetFrameAtIndex(0).GetDisplayFunctionName())
+self.assertIn("get_signal_crash",thread.GetFrameAtIndex(1).GetDisplayFunctionName())
Index: lldb/test/API/functionalities/dyld-launch-linux/Makefile
===
--- /dev/null
+++ lldb/test/API/functionalities/dyld-launch-linux/Makefile
@@ -0,0 +1,15 @@
+CXX_SOURCES := main.cpp
+LD_EXTRAS := -Wl,-rpath "-Wl,$(shell pwd)"
+USE_LIBDL := 1
+
+include Makefile.rules
+
+# The following shared library will be used to test breakpoints under dynamic loading
+libsignal_file.so:  signal_file.cpp
+	$(MAKE) -f $(MAKEFILE_RULES) \
+		DYLIB_ONLY=YES DYLIB_CXX_SOURCES=signal_file.cpp DYLIB_NAME=signal_file
+
+a.out: libsignal_file.so main.cpp
+	$(MAKE) -f $(MAKEFILE_RULES) \
+CXX_SOURCES=main.cpp LD_EXTRAS=libsignal_file.so
+
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -333,28 +333,48 @@
 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
   "Trying to locate rendezvous breakpoint in the interpreter "
   "by symbol name.");
-ModuleSP interpreter = LoadInterpreterModule();
-if (!interpreter) {
-  LLDB_LOG(log, "Can't find interpreter, rendezvous breakpoint isn't set.");
-  return false;
-}
-
-// Function names from different dynamic loaders that are known to be used
-// as rendezvous between the loader and debuggers.
+// Function names from different dynamic loaders that are known to be
+// used as rendezvous between the loader and debuggers.
 static std::vector 

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-16 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 366692.
rdhindsa added a comment.

Reverted code changes from ObjectFileELF as suggested and added directly to 
DYLDRendezvous.cpp.

Is there a better place to place the test, or is it okay to keep under 
ObjectFile.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D108061

Files:
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/test/Shell/ObjectFile/ELF/Inputs/main.cpp
  lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.cpp
  lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.h
  lldb/test/Shell/ObjectFile/ELF/ld_test.test

Index: lldb/test/Shell/ObjectFile/ELF/ld_test.test
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/ld_test.test
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# REQUIRES: system-linux
+#
+# RUN: %clang -target x86_64-pc-linux -o %t1.o -c %S/Inputs/signal_file.cpp
+# RUN: %clang -target x86_64-pc-linux -o %t2.o -c %S/Inputs/main.cpp
+# RUN: %clang -target x86_64-pc-linux -shared %t1.o -o %t3.so
+# RUN: %clang -o %tmain %t2.o %t3.so -L. -Wl,-rpath,%t
+#
+# RUN: echo '-n' > %t.in
+# RUN: echo 'run' >> %t.in
+# RUN: echo 'bt' >> %t.in
+#
+# RUN: %lldb -b -s %t.in -- %tmain 2>&1 | FileCheck %s
+#
+# RUN: %lldb -b -s %t.in -- /lib64/ld-linux-x86-64.so.2 --library-path %t  %tmain 2>&1 | FileCheck %s 
+#
+# bt
+# CHECK: (lldb) bt
+# CHECK: ld_test.test.tmp3.so`get_signal_crash()
+#
Index: lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.h
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash(void);
Index: lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.cpp
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/Shell/ObjectFile/ELF/Inputs/main.cpp
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/Inputs/main.cpp
@@ -0,0 +1,3 @@
+#include "signal_file.h"
+
+int main() { return get_signal_crash(); }
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -333,28 +333,48 @@
 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
   "Trying to locate rendezvous breakpoint in the interpreter "
   "by symbol name.");
-ModuleSP interpreter = LoadInterpreterModule();
-if (!interpreter) {
-  LLDB_LOG(log, "Can't find interpreter, rendezvous breakpoint isn't set.");
-  return false;
-}
-
-// Function names from different dynamic loaders that are known to be used
-// as rendezvous between the loader and debuggers.
+// Function names from different dynamic loaders that are known to be
+// used as rendezvous between the loader and debuggers.
 static std::vector DebugStateCandidates{
 "_dl_debug_state", "rtld_db_dlactivity", "__dl_rtld_db_dlactivity",
 "r_debug_state",   "_r_debug_state", "_rtld_debug_state",
 };
 
-FileSpecList containingModules;
-containingModules.Append(interpreter->GetFileSpec());
-dyld_break = target.CreateBreakpoint(
-, nullptr /* containingSourceFiles */,
-DebugStateCandidates, eFunctionNameTypeFull, eLanguageTypeC,
-0,   /* offset */
-eLazyBoolNo, /* skip_prologue */
-true,/* internal */
-false /* request_hardware */);
+ModuleSP interpreter = LoadInterpreterModule();
+if (!interpreter) {
+  if (NameMatches(m_process->GetTarget()
+  .GetExecutableModulePointer()
+  ->GetFileSpec()
+  .GetFilename()
+  .GetCString(),
+  NameMatch::StartsWith, "ld-")) {
+FileSpecList containingModules;
+containingModules.Append(
+m_process->GetTarget().GetExecutableModulePointer()->GetFileSpec());
+
+dyld_break = target.CreateBreakpoint(
+, nullptr /* containingSourceFiles */,
+DebugStateCandidates, eFunctionNameTypeFull, eLanguageTypeC,
+0,   /* offset */
+eLazyBoolNo, /* skip_prologue */
+true,/* internal */
+false /* request_hardware */);
+  } else {
+LLDB_LOG(log,
+ "Can't find interpreter, rendezvous breakpoint isn't set.");
+  

[Lldb-commits] [PATCH] D108061: [lldb] Add support for shared library load when executable called through ld.

2021-08-13 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa created this revision.
rdhindsa added reviewers: labath, clayborg.
Herald added a subscriber: emaste.
rdhindsa requested review of this revision.
Herald added a subscriber: MaskRay.
Herald added a project: LLDB.

Add support for shared library load when executable called through ld.

Currently, when an executable is called like: ld-linux --library-path  
executable, lldb is not able to load the shared libraries needed by the 
executable provided by library-path. lldb is not able to set Rendezvous 
breakpoint in this case.

This patch adds the support to enable setting rendezvous breakpoint when called 
using ld-*.so. It enables it to hit the breakpoint and extract the address of 
the rendezvous structure, which is how lldb is now able to load the shared 
libraries.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108061

Files:
  lldb/include/lldb/Symbol/ObjectFile.h
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DYLDRendezvous.cpp
  lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h
  lldb/test/Shell/ObjectFile/ELF/Inputs/main.cpp
  lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.cpp
  lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.h
  lldb/test/Shell/ObjectFile/ELF/ld_test.test

Index: lldb/test/Shell/ObjectFile/ELF/ld_test.test
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/ld_test.test
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# REQUIRES: system-linux
+#
+# RUN: %clang -target x86_64-pc-linux -o %t1.o -c %S/Inputs/signal_file.cpp
+# RUN: %clang -target x86_64-pc-linux -o %t2.o -c %S/Inputs/main.cpp
+# RUN: %clang -target x86_64-pc-linux -shared %t1.o -o %t3.so
+# RUN: %clang -o %tmain %t2.o %t3.so -L. -Wl,-rpath,%t
+#
+# RUN: echo '-n' > %t.in
+# RUN: echo 'run' >> %t.in
+# RUN: echo 'bt' >> %t.in
+#
+# RUN: %lldb -b -s %t.in -- %tmain 2>&1 | FileCheck %s
+#
+# RUN: %lldb -b -s %t.in -- /lib64/ld-linux-x86-64.so.2 --library-path %t  %tmain 2>&1 | FileCheck %s 
+#
+# bt
+# CHECK: (lldb) bt
+# CHECK: ld_test.test.tmp3.so`get_signal_crash()
+#
Index: lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.h
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.h
@@ -0,0 +1 @@
+int get_signal_crash(void);
Index: lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.cpp
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/Inputs/signal_file.cpp
@@ -0,0 +1,7 @@
+#include "signal_file.h"
+#include 
+
+int get_signal_crash(void) {
+  raise(SIGSEGV);
+  return 0;
+}
Index: lldb/test/Shell/ObjectFile/ELF/Inputs/main.cpp
===
--- /dev/null
+++ lldb/test/Shell/ObjectFile/ELF/Inputs/main.cpp
@@ -0,0 +1,3 @@
+#include "signal_file.h"
+
+int main() { return get_signal_crash(); }
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h
@@ -131,6 +131,9 @@
   lldb_private::Address
   GetImageInfoAddress(lldb_private::Target *target) override;
 
+  lldb_private::Address
+  GetRendezvousStructureAddress(lldb_private::Target *target) override;
+
   lldb_private::Address GetEntryPointAddress() override;
 
   lldb_private::Address GetBaseAddress() override;
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -882,6 +882,21 @@
   return Address();
 }
 
+Address ObjectFileELF::GetRendezvousStructureAddress(Target *target) {
+  ModuleSP module_sp(GetModule());
+  if (module_sp) {
+SymbolContextList contexts;
+SymbolContext context;
+module_sp->FindSymbolsWithNameAndType(ConstString("_r_debug"),
+  eSymbolTypeAny, contexts);
+if (contexts.GetSize()) {
+  if (contexts.GetContextAtIndex(0, context))
+return context.symbol->GetAddress();
+}
+  }
+  return Address();
+}
+
 lldb_private::Address ObjectFileELF::GetEntryPointAddress() {
   if (m_entry_point_address.IsValid())
 return m_entry_point_address;
Index: lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
===
--- lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
+++ lldb/source/Plugins/DynamicLoader/POSIX-DYLD/DynamicLoaderPOSIXDYLD.cpp
@@ -333,28 +333,48 @@
 LLDB_LOG(log, "Rendezvous structure is not set up yet. "
   "Trying to locate rendezvous breakpoint in the interpreter "
   

[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-08-12 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

fork is being called in LaunchProcess function in this 
file(ProcessLauncherPosixFork.cpp) itself.

Thank you for the review!


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

https://reviews.llvm.org/D105732

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


[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-07-21 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa added a comment.

Ping.


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

https://reviews.llvm.org/D105732

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


[Lldb-commits] [PATCH] D105732: [lldb] Update logic to close inherited file descriptors.

2021-07-13 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 358429.

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

https://reviews.llvm.org/D105732

Files:
  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
@@ -16,6 +16,7 @@
 #include "llvm/Support/Errno.h"
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -143,10 +144,30 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
 
+std::string proc_fd_path = "/proc/self/fd";
+std::filesystem::path fp(proc_fd_path);
+if (std::filesystem::is_directory(fp)) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (auto  : std::filesystem::directory_iterator(proc_fd_path)) {
+int fd =
+std::stoi(entry.path().string().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are
+// stdin/stdout/stderr
+if ((fd > 2) && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (auto _to_close : files_to_close)
+close(file_to_close);
+} else {
+  // /proc/self/fd didn't work - trying the slow way instead
+  for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd) {
+  close(fd);
+}
+}
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
   ExitWithError(error_fd, "ptrace");


Index: lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
===
--- lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -16,6 +16,7 @@
 #include "llvm/Support/Errno.h"
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -143,10 +144,30 @@
 // Close everything besides stdin, stdout, and stderr that has no file
 // action to avoid leaking. Only do this when debugging, as elsewhere we
 // actually rely on passing open descriptors to child processes.
-for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
-  if (!info.GetFileActionForFD(fd) && fd != error_fd)
-close(fd);
 
+std::string proc_fd_path = "/proc/self/fd";
+std::filesystem::path fp(proc_fd_path);
+if (std::filesystem::is_directory(fp)) {
+  std::vector files_to_close;
+  // Directory iterator doesn't ensure any sequence.
+  for (auto  : std::filesystem::directory_iterator(proc_fd_path)) {
+int fd =
+std::stoi(entry.path().string().substr(proc_fd_path.size() + 1));
+
+// Don't close first three entries since they are
+// stdin/stdout/stderr
+if ((fd > 2) && !info.GetFileActionForFD(fd) && fd != error_fd)
+  files_to_close.push_back(fd);
+  }
+  for (auto _to_close : files_to_close)
+close(file_to_close);
+} else {
+  // /proc/self/fd didn't work - trying the slow way instead
+  for (int fd = 3; fd < sysconf(_SC_OPEN_MAX); ++fd)
+if (!info.GetFileActionForFD(fd) && fd != error_fd) {
+  close(fd);
+}
+}
 // Start tracing this child that is about to exec.
 if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
   ExitWithError(error_fd, "ptrace");
___
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits


[Lldb-commits] [PATCH] D96370: Pass enviroment variables to python scripts.

2021-02-09 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa updated this revision to Diff 322510.

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

https://reviews.llvm.org/D96370

Files:
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
  lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test


Index: lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
@@ -0,0 +1,10 @@
+# REQUIRES: python
+
+# RUN: %lldb --batch \
+# RUN:--script-language python \
+# RUN:-O 'settings append target.env-vars PYTHONPATH=/tmp' \
+# RUN:-O 'command script import %S/Inputs/environ.py' \
+# RUN:-O 'test' 2>&1  | FileCheck %s
+
+# CHECK: :/tmp
+# CHECK: '/tmp'
Index: lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
@@ -0,0 +1,10 @@
+import os
+import sys
+
+def test(debugger, command, result, internal_dict):
+  print(os.environ["PYTHONPATH"])
+  print(sys.path)
+
+def __lldb_init_module(debugger, internal_dict):
+  debugger.HandleCommand('command script add -f environ.test test')
+
Index: 
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
@@ -341,6 +341,8 @@
  lldb::user_id_t watch_id);
   static void InitializePrivate();
 
+  static void SetPythonEnvironment(Debugger );
+
   class SynchronicityHandler {
   private:
 lldb::DebuggerSP m_debugger_sp;
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -537,6 +537,8 @@
   PyRun_SimpleString(run_string.GetData());
   run_string.Clear();
 
+  SetPythonEnvironment(m_debugger);
+
   run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from 
"
 "lldb.embedded_interpreter import run_python_interpreter; "
 "from lldb.embedded_interpreter import run_one_line')",
@@ -3269,6 +3271,26 @@
  "from lldb.embedded_interpreter import run_one_line");
 }
 
+void ScriptInterpreterPythonImpl::SetPythonEnvironment(Debugger ) {
+  StreamString run_string;
+  lldb::TargetSP target_get = debugger.GetTargetList().GetSelectedTarget();
+  const Environment  = target_get->GetGlobalProperties()->GetEnvironment();
+
+  PyRun_SimpleString("import os");
+  for (const auto  : env) {
+if (strcmp(KV.getKey().data(), "PYTHONPATH") == 0 ||
+strcmp(KV.getKey().data(), "PATH") == 0) {
+  run_string.Clear();
+  run_string.Printf(
+  "os.environ[\"%s\"] = os.environ.get(\"%s\",\"\")+ os.pathsep +"
+  "\"%s\"",
+  KV.getKey().data(), KV.getKey().data(), KV.getValue().data());
+  PyRun_SimpleString(run_string.GetData());
+  AddToSysPath(AddLocation::End, KV.getValue().data());
+}
+  }
+}
+
 void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
std::string path) {
   std::string path_copy;


Index: lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
@@ -0,0 +1,10 @@
+# REQUIRES: python
+
+# RUN: %lldb --batch \
+# RUN:--script-language python \
+# RUN:-O 'settings append target.env-vars PYTHONPATH=/tmp' \
+# RUN:-O 'command script import %S/Inputs/environ.py' \
+# RUN:-O 'test' 2>&1  | FileCheck %s
+
+# CHECK: :/tmp
+# CHECK: '/tmp'
Index: lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
@@ -0,0 +1,10 @@
+import os
+import sys
+
+def test(debugger, command, result, internal_dict):
+  print(os.environ["PYTHONPATH"])
+  print(sys.path)
+
+def __lldb_init_module(debugger, internal_dict):
+  debugger.HandleCommand('command script add -f environ.test test')
+
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
+++ 

[Lldb-commits] [PATCH] D96370: Pass enviroment variables to python scripts.

2021-02-09 Thread Rumeet Dhindsa via Phabricator via lldb-commits
rdhindsa created this revision.
rdhindsa added a reviewer: labath.
rdhindsa requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

It enables environment variables set for lldb targets to be passed to python 
scripts. This allows the user to put Python scripts at different locations, but 
being able to import them by setting pythonpath.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D96370

Files:
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
  lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
  lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test


Index: lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
@@ -0,0 +1,10 @@
+# REQUIRES: python
+
+# RUN: %lldb --batch \
+# RUN:--script-language python \
+# RUN:-O 'settings append target.env-vars PYTHONPATH=/tmp' \
+# RUN:-O 'command script import %S/Inputs/environ.py' \
+# RUN:-O 'test' 2>&1  | FileCheck %s
+
+# CHECK: :/tmp
+# CHECK: '/tmp'
Index: lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
@@ -0,0 +1,10 @@
+import os
+import sys
+
+def test(debugger, command, result, internal_dict):
+  print(os.environ["PYTHONPATH"])
+  print(sys.path)
+
+def __lldb_init_module(debugger, internal_dict):
+  debugger.HandleCommand('command script add -f environ.test test')
+
Index: 
lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
===
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPythonImpl.h
@@ -341,6 +341,8 @@
  lldb::user_id_t watch_id);
   static void InitializePrivate();
 
+  static void SetPythonEnvironment(Debugger& debugger);
+
   class SynchronicityHandler {
   private:
 lldb::DebuggerSP m_debugger_sp;
Index: lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
===
--- lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
+++ lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
@@ -537,6 +537,8 @@
   PyRun_SimpleString(run_string.GetData());
   run_string.Clear();
 
+  SetPythonEnvironment(m_debugger);
+
   run_string.Printf("run_one_line (%s, 'import lldb.embedded_interpreter; from 
"
 "lldb.embedded_interpreter import run_python_interpreter; "
 "from lldb.embedded_interpreter import run_one_line')",
@@ -3269,6 +3271,26 @@
  "from lldb.embedded_interpreter import run_one_line");
 }
 
+void ScriptInterpreterPythonImpl::SetPythonEnvironment(Debugger ) {
+  StreamString run_string;
+  lldb::TargetSP target_get = debugger.GetTargetList().GetSelectedTarget();
+  const Environment  = target_get->GetGlobalProperties()->GetEnvironment();
+
+  PyRun_SimpleString("import os");
+  for (const auto  : env) {
+if (strcmp(KV.getKey().data(), "PYTHONPATH") == 0 ||
+strcmp(KV.getKey().data(), "PATH") == 0) {
+  run_string.Clear();
+  run_string.Printf(
+  "os.environ[\"%s\"] = os.environ.get(\"%s\",\"\")+ os.pathsep +"
+  "\"%s\"",
+  KV.getKey().data(), KV.getKey().data(), KV.getValue().data());
+  PyRun_SimpleString(run_string.GetData());
+  AddToSysPath(AddLocation::End, KV.getValue().data());
+}
+  }
+}
+
 void ScriptInterpreterPythonImpl::AddToSysPath(AddLocation location,
std::string path) {
   std::string path_copy;


Index: lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/pass_environment.test
@@ -0,0 +1,10 @@
+# REQUIRES: python
+
+# RUN: %lldb --batch \
+# RUN:--script-language python \
+# RUN:-O 'settings append target.env-vars PYTHONPATH=/tmp' \
+# RUN:-O 'command script import %S/Inputs/environ.py' \
+# RUN:-O 'test' 2>&1  | FileCheck %s
+
+# CHECK: :/tmp
+# CHECK: '/tmp'
Index: lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
===
--- /dev/null
+++ lldb/test/Shell/ScriptInterpreter/Python/Inputs/environ.py
@@ -0,0 +1,10 @@
+import os
+import sys
+
+def test(debugger, command, result, internal_dict):
+  print(os.environ["PYTHONPATH"])
+  print(sys.path)
+
+def __lldb_init_module(debugger, internal_dict):
+