Skip to content

Commit 0d65353

Browse files
ConradIrwinartemmukhincole-miller
authored
Fix attach to process on arm64 Mac. (#1917)
* Before this change it was not possible to attach to a process on arm64 mac. The primary issue was that we weren't building the attach.dylib for all targets; but even once we did that we had to ensure that we were exiting successfully after injecting into the process. We pulled in the compile changes from JetBrains/intellij-community@2e7109f Co-authored-by: @artemmukhin <Artem.Mukhin@jetbrains.com> Co-authored-by: Cole Miller <cole@zed.dev> * Use g++ on macOS (with -D_FORTIFY_SOURCE=2) --------- Co-authored-by: @artemmukhin <Artem.Mukhin@jetbrains.com> Co-authored-by: Cole Miller <cole@zed.dev>
1 parent 99b202e commit 0d65353

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

build_attach_binaries.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This script is used for building the pydevd binaries
22
import argparse
33
import os
4+
import platform
45

56
def build_pydevd_binaries(force: bool):
67
os.environ["PYDEVD_USE_CYTHON"] = "yes"
@@ -20,16 +21,19 @@ def build_pydevd_binaries(force: bool):
2021

2122
# Run the appropriate batch script to build the binaries if necessary.
2223
pydevd_attach_to_process_root = os.path.join(pydevd_root, "pydevd_attach_to_process")
23-
if os.name == "nt":
24+
if platform.system() == "Windows":
2425
if not os.path.exists(os.path.join(pydevd_attach_to_process_root, "attach_amd64.dll")) or force:
2526
os.system(os.path.join(pydevd_attach_to_process_root, "windows", "compile_windows.bat"))
26-
elif os.name == "posix":
27+
elif platform.system() == "Linux":
2728
if not os.path.exists(os.path.join(pydevd_attach_to_process_root, "attach_linux_amd64.so")) or force:
2829
os.system(os.path.join(pydevd_attach_to_process_root, "linux_and_mac", "compile_linux.sh"))
29-
else:
30-
if not os.path.exists(os.path.join(pydevd_attach_to_process_root, "attach_x86_64.dylib")) or force:
30+
elif platform.system() == "Darwin":
31+
if not os.path.exists(os.path.join(pydevd_attach_to_process_root, "attach.dylib")) or force:
3132
os.system(os.path.join(pydevd_attach_to_process_root, "linux_and_mac", "compile_mac.sh"))
32-
33+
else:
34+
print("unsupported platform.system(): {}".format(platform.system()))
35+
exit(1)
36+
3337

3438
if __name__ == "__main__":
3539
arg_parser = argparse.ArgumentParser(description="Build the pydevd binaries.")

src/debugpy/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def get_target_filename(is_target_process_64=None, prefix=None, extension=None):
183183
print("Unable to attach to process in platform: %s", sys.platform)
184184
return None
185185

186-
if arch.lower() not in ("amd64", "x86", "x86_64", "i386", "x86"):
186+
if arch.lower() not in ("arm64", "amd64", "x86", "x86_64", "i386", "x86"):
187187
# We don't support this processor by default. Still, let's support the case where the
188188
# user manually compiled it himself with some heuristics.
189189
#
@@ -237,8 +237,11 @@ def get_target_filename(is_target_process_64=None, prefix=None, extension=None):
237237

238238
if not prefix:
239239
# Default is looking for the attach_ / attach_linux
240-
if IS_WINDOWS or IS_MAC: # just the extension changes
240+
if IS_WINDOWS: # just the extension changes
241241
prefix = "attach_"
242+
elif IS_MAC:
243+
prefix = "attach"
244+
suffix = ""
242245
elif IS_LINUX:
243246
prefix = "attach_linux_" # historically it has a different name
244247
else:
@@ -525,7 +528,7 @@ def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_d
525528
cmd.extend(
526529
[
527530
"-o 'process detach'",
528-
"-o 'script import os; os._exit(1)'",
531+
"-o 'script import os; os._exit(0)'",
529532
]
530533
)
531534

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1+
#!/bin/bash
12
set -e
23
SRC="$(dirname "$0")/.."
3-
g++ -fPIC -D_REENTRANT -std=c++11 -D_FORTIFY_SOURCE=2 -arch x86_64 -c $SRC/linux_and_mac/attach.cpp -o $SRC/attach_x86_64.o
4-
g++ -dynamiclib -nostartfiles -arch x86_64 -lc $SRC/attach_x86_64.o -o $SRC/attach_x86_64.dylib
4+
5+
g++ -fPIC -D_REENTRANT -std=c++11 -D_FORTIFY_SOURCE=2 -arch arm64 -c -o "$SRC/attach_arm64.o" "$SRC/linux_and_mac/attach.cpp"
6+
g++ -dynamiclib -nostartfiles -arch arm64 -o "$SRC/attach_arm64.dylib" "$SRC/attach_arm64.o" -lc
7+
rm "$SRC/attach_arm64.o"
8+
9+
g++ -fPIC -D_REENTRANT -std=c++11 -D_FORTIFY_SOURCE=2 -arch x86_64 -c -o "$SRC/attach_x86_64.o" "$SRC/linux_and_mac/attach.cpp"
10+
g++ -dynamiclib -nostartfiles -arch x86_64 -o "$SRC/attach_x86_64.dylib" "$SRC/attach_x86_64.o" -lc
11+
rm "$SRC/attach_x86_64.o"
12+
13+
lipo -create "$SRC/attach_arm64.dylib" "$SRC/attach_x86_64.dylib" -output "$SRC/attach.dylib"
14+
rm "$SRC/attach_arm64.dylib" "$SRC/attach_x86_64.dylib"

src/debugpy/_vendored/pydevd/pydevd_tracing.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def get_python_helper_lib_filename():
201201
pydev_log.info("Unable to set trace to all threads in platform: %s", sys.platform)
202202
return None
203203

204-
if arch.lower() not in ("amd64", "x86", "x86_64", "i386", "x86"):
204+
if arch.lower() not in ("arm64", "amd64", "x86", "x86_64", "i386", "x86"):
205205
# We don't support this processor by default. Still, let's support the case where the
206206
# user manually compiled it himself with some heuristics.
207207
#
@@ -251,7 +251,8 @@ def get_python_helper_lib_filename():
251251
suffix = suffix_32
252252

253253
if IS_WINDOWS or IS_MAC: # just the extension changes
254-
prefix = "attach_"
254+
prefix = "attach"
255+
suffix = ""
255256
elif IS_LINUX: #
256257
prefix = "attach_linux_" # historically it has a different name
257258
else:

0 commit comments

Comments
 (0)