Skip to content

[lldb] Fix regex support in SBTarget.modules_access #116452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

kastiglione
Copy link
Contributor

@kastiglione kastiglione commented Nov 15, 2024

First, SRE_Pattern does not exist on newer Python's, use type(re.compile('')) like other Python extensions do. The dynamic type is because some earlier versions of Python 3 do not have re.Pattern.

Second, SBModule has a file property, not a path property.

First, `SRE_Pattern` does not exist on newer Python's, use
`type(re.compile(''))` like other Python extensions do. The dynamic type is
because some earlier versions of Python 3 do not have `re.Pattern`.

Second, `SBModule` has a `file` property, not a `path` property.
matching_modules = []
for idx in range(num_modules):
module = self.sbtarget.GetModuleAtIndex(idx)
re_match = key.search(module.path.fullpath)
re_match = key.search(module.file.fullpath)
Copy link
Contributor Author

@kastiglione kastiglione Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this was broken, and obviously not used, so I'd like to also change this from fullpath to basename. Opinions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean you'd be able to search for modules just by the module's base name instead of the complete path? e.g. You could find libc++ with libc++ instead of /usr/lib/libc++.dylib or whatever.

If so, I'd be supportive of that kind of change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to find libc++ via regex, the alternatives regexes are:

  1. with fullpath: r"/libc\+\+"
  2. with basepath: r"^libc\+\+"

so the difference is a bit minor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that lookup supports string keys, one can write target.module["libc++.dylib"]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave such a change for a follow up.

@llvmbot
Copy link
Member

llvmbot commented Nov 15, 2024

@llvm/pr-subscribers-lldb

Author: Dave Lee (kastiglione)

Changes

First, SRE_Pattern does not exist on newer Python's, use type(re.compile('')) like other Python extensions do. The dynamic type is because some earlier versions of Python 3 do not have re.Pattern.

Second, SBModule has a file property, not a path property.


Full diff: https://github.com/llvm/llvm-project/pull/116452.diff

2 Files Affected:

  • (modified) lldb/bindings/interface/SBTargetExtensions.i (+2-2)
  • (modified) lldb/test/API/lang/cpp/stl/TestStdCXXDisassembly.py (+6-9)
diff --git a/lldb/bindings/interface/SBTargetExtensions.i b/lldb/bindings/interface/SBTargetExtensions.i
index d756a351a810ab..43125d8970615b 100644
--- a/lldb/bindings/interface/SBTargetExtensions.i
+++ b/lldb/bindings/interface/SBTargetExtensions.i
@@ -79,11 +79,11 @@ STRING_EXTENSION_LEVEL_OUTSIDE(SBTarget, lldb::eDescriptionLevelBrief)
                         module = self.sbtarget.GetModuleAtIndex(idx)
                         if module.uuid == key:
                             return module
-                elif type(key) is re.SRE_Pattern:
+                elif isinstance(key, type(re.compile(''))):
                     matching_modules = []
                     for idx in range(num_modules):
                         module = self.sbtarget.GetModuleAtIndex(idx)
-                        re_match = key.search(module.path.fullpath)
+                        re_match = key.search(module.file.fullpath)
                         if re_match:
                             matching_modules.append(module)
                     return matching_modules
diff --git a/lldb/test/API/lang/cpp/stl/TestStdCXXDisassembly.py b/lldb/test/API/lang/cpp/stl/TestStdCXXDisassembly.py
index 06f338b3ed1ded..bcf8735c7c3f98 100644
--- a/lldb/test/API/lang/cpp/stl/TestStdCXXDisassembly.py
+++ b/lldb/test/API/lang/cpp/stl/TestStdCXXDisassembly.py
@@ -3,6 +3,7 @@
 """
 
 import os
+import re
 import lldb
 from lldbsuite.test.lldbtest import *
 import lldbsuite.test.lldbutil as lldbutil
@@ -30,15 +31,11 @@ def test_stdcxx_disasm(self):
                 self.runCmd("disassemble -n '%s'" % function.GetName())
 
         lib_stdcxx = "FAILHORRIBLYHERE"
-        # Iterate through the available modules, looking for stdc++ library...
-        for i in range(target.GetNumModules()):
-            module = target.GetModuleAtIndex(i)
-            fs = module.GetFileSpec()
-            if fs.GetFilename().startswith("libstdc++") or fs.GetFilename().startswith(
-                "libc++"
-            ):
-                lib_stdcxx = str(fs)
-                break
+        # Find the stdc++ library...
+        stdlib_regex = re.compile(r"/lib(std)?c\+\+")
+        for module in target.module[stdlib_regex]:
+            lib_stdcxx = module.file.fullpath
+            break
 
         # At this point, lib_stdcxx is the full path to the stdc++ library and
         # module is the corresponding SBModule.

matching_modules = []
for idx in range(num_modules):
module = self.sbtarget.GetModuleAtIndex(idx)
re_match = key.search(module.path.fullpath)
re_match = key.search(module.file.fullpath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean you'd be able to search for modules just by the module's base name instead of the complete path? e.g. You could find libc++ with libc++ instead of /usr/lib/libc++.dylib or whatever.

If so, I'd be supportive of that kind of change.

@kastiglione kastiglione merged commit 170e1fe into llvm:main Nov 19, 2024
9 checks passed
@kastiglione kastiglione deleted the lldb-Fix-regex-support-in-SBTarget.modules_access branch November 19, 2024 00:15
kastiglione added a commit to swiftlang/llvm-project that referenced this pull request Apr 3, 2025
First, `SRE_Pattern` does not exist on newer Python's, use
`type(re.compile(''))` like other Python extensions do. The dynamic type
is because some earlier versions of Python 3 do not have `re.Pattern`.

Second, `SBModule` has a `file` property, not a `path` property.
(cherry-picked from commit 170e1fe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants