Skip to content
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

Not able to get debugpy attach (PID and host & port) working inside Zed code editor #1820

Open
RemcoSmitsDev opened this issue Jan 29, 2025 · 11 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@RemcoSmitsDev
Copy link

RemcoSmitsDev commented Jan 29, 2025

Hi debugpy team,

I'm one of the guys that is working on the client-side implementation of the DAP spec for the Zed code editor.

We are struggling a bit to get the attach (PID and host & port) feature to work, and haven't found any documentation on how to do it.
Could you please point me in the right direction to find out how to configure debugpy to work with attach?

About our implementation:

We use the debugpy library that is installed inside the user’s virtual environment. We start the debug adapter with the following command:

path/to/python debugpy.adapter --port=1234 --host=localhost

Our Setup

We start a simple Python script with the following commands:

mkdir test-python-project
cd test-python-project
python3 -m venv ./.venv
source ./.venv/bin/activate
pip install debugpy
touch test.py

Add the following content to the test.py file:

x = 1 // added breakpoint here
print("Script has started")

while True:
    x += 1

Start to listen for debugger:

debugpy --listen localhost:5678 --wait-for-client test.py

Our Issue

So one of the issues we are facing is that we never receive an attach response back, so it seems like debugpy is waiting for something/someone. So I looked at our request/response log which shows that we receive two custom events debugpySockets and debugpyWaitingForServer. Which results into the following error log Cancelled DAP request for "attach" id 2 which took over 12s.

Which to my understanding is not more than just an informational event, to notify us about the sockets it is listening on and that it is waiting for a server to connect to it. So while I was debugging this issue, I looked at the vscode-python-debugger implementation, it seems like they don't really act on these events.

So my first question is should we act on these events? And if so, how should we act on them?
I also read through some of the debugpy internal code, which seems to support the StartDebuggingRequest reverse request, so should we act on these events if we could receive the StartDebuggingRequest request?

I've seen examples online where two instances of debugpy are run, one for the debuggee program and another to connect to the first instance. Is this a pattern that we'll have to replicate?

Request log for debugpy attach

// Send
{
  "type": "request",
  "seq": 1,
  "command": "initialize",
  "arguments": {
    "clientID": "zed",
    "clientName": "Zed",
    "adapterID": "debugpy",
    "locale": "en-US",
    "linesStartAt1": true,
    "columnsStartAt1": true,
    "pathFormat": "path",
    "supportsVariableType": true,
    "supportsVariablePaging": false,
    "supportsRunInTerminalRequest": true,
    "supportsMemoryReferences": true,
    "supportsProgressReporting": false,
    "supportsInvalidatedEvent": false,
    "supportsMemoryEvent": false,
    "supportsArgsCanBeInterpretedByShell": false,
    "supportsStartDebuggingRequest": true
  }
}
// Receive
{
  "seq": 1,
  "type": "event",
  "event": "output",
  "body": {
    "category": "telemetry",
    "output": "ptvsd",
    "data": {
      "packageVersion": "1.8.12"
    }
  }
}
{
  "seq": 2,
  "type": "event",
  "event": "output",
  "body": {
    "category": "telemetry",
    "output": "debugpy",
    "data": {
      "packageVersion": "1.8.12"
    }
  }
}
{
  "seq": 3,
  "type": "event",
  "event": "debugpySockets",
  "body": {
    "sockets": [
      {
        "host": "127.0.0.1",
        "port": 52653,
        "internal": false
      }
    ]
  }
}
{
  "seq": 4,
  "type": "response",
  "request_seq": 1,
  "success": true,
  "command": "initialize",
  "body": {
    "supportsCompletionsRequest": true,
    "supportsConditionalBreakpoints": true,
    "supportsConfigurationDoneRequest": true,
    "supportsDebuggerProperties": true,
    "supportsDelayedStackTraceLoading": true,
    "supportsEvaluateForHovers": true,
    "supportsExceptionInfoRequest": true,
    "supportsExceptionOptions": true,
    "supportsFunctionBreakpoints": true,
    "supportsHitConditionalBreakpoints": true,
    "supportsLogPoints": true,
    "supportsModulesRequest": true,
    "supportsSetExpression": true,
    "supportsSetVariable": true,
    "supportsValueFormattingOptions": true,
    "supportsTerminateRequest": true,
    "supportsGotoTargetsRequest": true,
    "supportsClipboardContext": true,
    "exceptionBreakpointFilters": [
      {
        "filter": "raised",
        "label": "Raised Exceptions",
        "default": false,
        "description": "Break whenever any exception is raised."
      },
      {
        "filter": "uncaught",
        "label": "Uncaught Exceptions",
        "default": true,
        "description": "Break when the process is exiting due to unhandled exception."
      },
      {
        "filter": "userUnhandled",
        "label": "User Uncaught Exceptions",
        "default": false,
        "description": "Break when exception escapes into library code."
      }
    ],
    "supportsStepInTargetsRequest": true
  }
}
// Send
{
  "type": "request",
  "seq": 2,
  "command": "attach",
  "arguments": {
    "request": "attach",
    "cwd": "/Users/remcosmits/Documents/code/test-python-project",
    "type": "debugpy",
    "logToFile": true,
    "connect": {
      "host": "localhost",
      "port": 5678
    },
    "clientOS": "unix",
    "justMyCode": true,
    "showReturnValue": true,
    "workspaceFolder": "/Users/remcosmits/Documents/code/test-python-project"
  }
}
// Receive
{
  "seq": 5,
  "type": "event",
  "event": "debugpySockets",
  "body": {
    "sockets": [
      {
        "host": "127.0.0.1",
        "port": 52653,
        "internal": false
      },
      {
        "host": "127.0.0.1",
        "port": 52656,
        "internal": true
      }
    ]
  }
}
{
  "seq": 6,
  "type": "event",
  "event": "debugpyWaitingForServer",
  "body": {
    "host": "127.0.0.1",
    "port": 52656
  }
}
@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Jan 29, 2025
@rchiodo
Copy link
Contributor

rchiodo commented Jan 29, 2025

Have you tried doing the same thing in VS code with logging and see what the difference is in the debupgy and vscode logs and your output? That's what I'd do to debug the problem.

@RemcoSmitsDev
Copy link
Author

Hey, thanks for the fast response!

Yeah, we already tried sending the same information as vscode does with their requests,
but that didn't seem to make a difference.

Here is the log from the same attach flow in vscode

0 Starting Session:
{
    "name": "Python Debugger: Attach Port",
    "type": "debugpy",
    "request": "attach",
    "connect": {
        "port": 5678,
        "host": "127.0.0.1"
    },
    "logToFile": true,
    "__configurationTarget": 6,
    "clientOS": "unix",
    "debugOptions": [
        "RedirectOutput",
        "ShowReturnValue"
    ],
    "justMyCode": true,
    "showReturnValue": true,
    "workspaceFolder": "/Users/remcosmits/Documents/code/test-python-project"
}
5 Client <-- Adapter:
{
    "seq": 1,
    "type": "event",
    "event": "output",
    "body": {
        "category": "telemetry",
        "output": "ptvsd",
        "data": {
            "packageVersion": "1.8.12"
        }
    }
}
5 Client <-- Adapter:
{
    "seq": 2,
    "type": "event",
    "event": "output",
    "body": {
        "category": "telemetry",
        "output": "debugpy",
        "data": {
            "packageVersion": "1.8.12"
        }
    }
}
5 Client <-- Adapter:
{
    "seq": 3,
    "type": "event",
    "event": "debugpySockets",
    "body": {
        "sockets": [
            {
                "host": "127.0.0.1",
                "port": 5678,
                "internal": false
            },
            {
                "host": "127.0.0.1",
                "port": 53180,
                "internal": true
            }
        ]
    }
}
7 Client --> Adapter:
{
    "command": "initialize",
    "arguments": {
        "clientID": "vscode",
        "clientName": "Visual Studio Code",
        "adapterID": "debugpy",
        "pathFormat": "path",
        "linesStartAt1": true,
        "columnsStartAt1": true,
        "supportsVariableType": true,
        "supportsVariablePaging": true,
        "supportsRunInTerminalRequest": true,
        "locale": "en",
        "supportsProgressReporting": true,
        "supportsInvalidatedEvent": true,
        "supportsMemoryReferences": true,
        "supportsArgsCanBeInterpretedByShell": true,
        "supportsMemoryEvent": true,
        "supportsStartDebuggingRequest": true,
        "supportsANSIStyling": true
    },
    "type": "request",
    "seq": 1
}
7 Client <-- Adapter:
{
    "seq": 4,
    "type": "response",
    "request_seq": 1,
    "success": true,
    "command": "initialize",
    "body": {
        "supportsCompletionsRequest": true,
        "supportsConditionalBreakpoints": true,
        "supportsConfigurationDoneRequest": true,
        "supportsDebuggerProperties": true,
        "supportsDelayedStackTraceLoading": true,
        "supportsEvaluateForHovers": true,
        "supportsExceptionInfoRequest": true,
        "supportsExceptionOptions": true,
        "supportsFunctionBreakpoints": true,
        "supportsHitConditionalBreakpoints": true,
        "supportsLogPoints": true,
        "supportsModulesRequest": true,
        "supportsSetExpression": true,
        "supportsSetVariable": true,
        "supportsValueFormattingOptions": true,
        "supportsTerminateRequest": true,
        "supportsGotoTargetsRequest": true,
        "supportsClipboardContext": true,
        "exceptionBreakpointFilters": [
            {
                "filter": "raised",
                "label": "Raised Exceptions",
                "default": false,
                "description": "Break whenever any exception is raised."
            },
            {
                "filter": "uncaught",
                "label": "Uncaught Exceptions",
                "default": true,
                "description": "Break when the process is exiting due to unhandled exception."
            },
            {
                "filter": "userUnhandled",
                "label": "User Uncaught Exceptions",
                "default": false,
                "description": "Break when exception escapes into library code."
            }
        ],
        "supportsStepInTargetsRequest": true
    }
}
14 Client --> Adapter:
{
    "command": "attach",
    "arguments": {
        "name": "Python Debugger: Attach Port",
        "type": "debugpy",
        "request": "attach",
        "connect": {
            "port": 5678,
            "host": "127.0.0.1"
        },
        "logToFile": true,
        "__configurationTarget": 6,
        "clientOS": "unix",
        "debugOptions": [
            "RedirectOutput",
            "ShowReturnValue"
        ],
        "justMyCode": true,
        "showReturnValue": true,
        "workspaceFolder": "/Users/remcosmits/Documents/code/test-python-project",
        "__sessionId": "aa999f13-3d1b-4366-8dd7-c3429c05848f"
    },
    "type": "request",
    "seq": 2
}
14 Client <-- Adapter:
{
    "seq": 5,
    "type": "event",
    "event": "debugpyWaitingForServer",
    "body": {
        "host": "127.0.0.1",
        "port": 53180
    }
}
21 Client <-- Adapter:
{
    "seq": 6,
    "type": "event",
    "event": "initialized"
}

@rchiodo
Copy link
Contributor

rchiodo commented Jan 29, 2025

It looks like in your case the initialized event is not coming back. That should happen as a result of the attach request.

That's here:

def _handle_launch_or_attach_request(self, py_db, request, start_reason):

I guess I'd add more logging to pydevd and then look at the pydevd.log that should have been generated to see why it's not making to the initialized event that should occur.

@rchiodo
Copy link
Contributor

rchiodo commented Jan 29, 2025

You can add more logging with lines like this:

pydev_log.debug("Any string")

@RemcoSmitsDev
Copy link
Author

Thanks for your help so far, will look into this more this weekend will let you know if I need some more guidance.

@RemcoSmitsDev
Copy link
Author

Where should the pydevd.log file be located on my pc? Tried to find it using find / -name "pydevd.log" 2>/dev/null but couldn't find it. I also tried adding the following env variables, which didn't make any difference.

export PYDEVD_DEBUG=1
export PYDEVD_DEBUG_FILE=/Users/remcosmits/Documents/code/test-python-project/file.log

@RemcoSmitsDev
Copy link
Author

Not sure if It's related, but I also get the following error back from the dap server, when I'm trying to use the attach process feature. I also get this when I'm trying this feature inside VSCode.

E+00001.884: Code injection into PID=39241 failed

--- Starting attach to pid: 39241 ---\n(lldb) process attach --pid 39241\nProcess 39241 stopped\n* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGSTOP\n frame #0: 0x000000018ca3a6ec libsystem_kernel.dylib__psynch_cvwait + 8\nlibsystem_kernel.dylib__psynch_cvwait:\n-> 0x18ca3a6ec <+8>: b.lo 0x18ca3a70c ; <+40>\n 0x18ca3a6f0 <+12>: pacibsp \n 0x18ca3a6f4 <+16>: stp x29, x30, [sp, #-0x10]!\n 0x18ca3a6f8 <+20>: mov x29, sp\nTarget 0: (Python) stopped.\nExecutable module set to "/opt/homebrew/Cellar/[email protected]/3.13.1/Frameworks/Python.framework/Versions/3.13/Resources/Python.app/Contents/MacOS/Python".\nArchitecture set to: arm64-apple-macosx-.\n(lldb) command script import "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac/lldb_prepare.py"\n(lldb) load_lib_and_attach "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_x86_64.dylib" 0 "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 85, 115, 101, 114, 115, 47, 114, 101, 109, 99, 111, 115, 109, 105, 116, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 115, 47, 99, 111, 100, 101, 47, 116, 101, 115, 116, 45, 112, 121, 116, 104, 111, 110, 45, 112, 114, 111, 106, 101, 99, 116, 47, 46, 118, 101, 110, 118, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 51, 46, 49, 51, 47, 115, 105, 116, 101, 45, 112, 97, 99, 107, 97, 103, 101, 115, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 53, 48, 56, 51, 50, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 49, 52, 50, 52, 49, 97, 56, 53, 100, 51, 55, 56, 100, 56, 97, 102, 52, 56, 101, 51, 99, 98, 100, 52, 54, 57, 54, 102, 101, 56, 49, 102, 57, 100, 53, 56, 50, 99, 49, 100, 57, 56, 54, 51, 98, 49, 53, 55, 100, 52, 51, 99, 101, 102, 99, 49, 102, 56, 51, 57, 57, 51, 55, 53, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);" 0\n/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_x86_64.dylib\nsuccess\nimport codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 85, 115, 101, 114, 115, 47, 114, 101, 109, 99, 111, 115, 109, 105, 116, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 115, 47, 99, 111, 100, 101, 47, 116, 101, 115, 116, 45, 112, 121, 116, 104, 111, 110, 45, 112, 114, 111, 106, 101, 99, 116, 47, 46, 118, 101, 110, 118, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 51, 46, 49, 51, 47, 115, 105, 116, 101, 45, 112, 97, 99, 107, 97, 103, 101, 115, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 53, 48, 56, 51, 50, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 49, 52, 50, 52, 49, 97, 56, 53, 100, 51, 55, 56, 100, 56, 97, 102, 52, 56, 101, 51, 99, 98, 100, 52, 54, 57, 54, 102, 101, 56, 49, 102, 57, 100, 53, 56, 50, 99, 49, 100, 57, 56, 54, 51, 98, 49, 53, 55, 100, 52, 51, 99, 101, 102, 99, 49, 102, 56, 51, 57, 57, 51, 55, 53, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);\nerror: error: <user expression 1>:1:6: use of undeclared identifier 'DoAttach'\n 1 | (int)DoAttach(0, "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 85, 115, 101, 114, 115, 47, 114, 101, 109, 99, 111, 115, 109, 105, 116, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 115, 47, 99, 111, 100, 101, 47, 116, 101, 115, 116, 45, 112, 121, 116, 104, 111, 110, 45, 112, 114, 111, 106, 101, 99, 116, 47, 46, 118, 101, 110, 118, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 51, 46, 49, 51, 47, 115, 105, 116, 101, 45, 112, 97, 99, 107, 97, 103, 101, 115, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 53, 48, 56, 51, 50, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 49, 52, 50, 52, 49, 97, 56, 53, 100, 51, 55, 56, 100, 56, 97, 102, 52, 56, 101, 51, 99, 98, 100, 52, 54, 57, 54, 102, 101, 56, 49, 102, 57, 100, 53, 56, 50, 99, 49, 100, 57, 56, 54, 51, 98, 49, 53, 55, 100, 52, 51, 99, 101, 102, 99, 49, 102, 56, 51, 57, 57, 51, 55, 53, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);", 0);\n | ^\n(lldb) process detach\nProcess 39241 detached\n(lldb) script import os; os._exit(1)\nE+00001.884: Code injection into PID=39241 failed:\n \n Traceback (most recent call last):\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/server/cli.py", line 461, in attach_to_pid\n add_code_to_python_process.run_python_code(\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^\n pid,\n ^^^^\n ...<2 lines>...\n show_debug_info=int(os.getenv("DEBUGPY_ATTACH_BY_PID_DEBUG_INFO", "0")),\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n )\n ^\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py", line 540, in run_python_code_mac\n subprocess.check_call(" ".join(cmd), shell=True, env=env)\n ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/opt/homebrew/Cellar/[email protected]/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/subprocess.py", line 419, in check_call\n raise CalledProcessError(retcode, cmd)\n subprocess.CalledProcessError: Command 'lldb --no-lldbinit --script-language Python -o 'process attach --pid 39241' -o 'command script import "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac/lldb_prepare.py"' -o 'load_lib_and_attach "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_x86_64.dylib" 0 "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 85, 115, 101, 114, 115, 47, 114, 101, 109, 99, 111, 115, 109, 105, 116, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 115, 47, 99, 111, 100, 101, 47, 116, 101, 115, 116, 45, 112, 121, 116, 104, 111, 110, 45, 112, 114, 111, 106, 101, 99, 116, 47, 46, 118, 101, 110, 118, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 51, 46, 49, 51, 47, 115, 105, 116, 101, 45, 112, 97, 99, 107, 97, 103, 101, 115, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 53, 48, 56, 51, 50, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 49, 52, 50, 52, 49, 97, 56, 53, 100, 51, 55, 56, 100, 56, 97, 102, 52, 56, 101, 51, 99, 98, 100, 52, 54, 57, 54, 102, 101, 56, 49, 102, 57, 100, 53, 56, 50, 99, 49, 100, 57, 56, 54, 51, 98, 49, 53, 55, 100, 52, 51, 99, 101, 102, 99, 49, 102, 56, 51, 57, 57, 51, 55, 53, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);" 0' -o 'process detach' -o 'script import os; os._exit(1)'' returned non-zero exit status 1.\n \n Stack where logged:\n File "", line 198, in _run_module_as_main\n File "", line 88, in _run_code\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/main.py", line 71, in \n cli.main()\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/server/cli.py", line 501, in main\n run()\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/server/cli.py", line 468, in attach_to_pid\n log.reraise_exception("Code injection into PID={0} failed:", pid)\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/common/log.py", line 222, in reraise_exception\n _exception(format_string, *args, **kwargs)\n \n\nTraceback (most recent call last):\n File "", line 198, in _run_module_as_main\n File "", line 88, in _run_code\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/main.py", line 71, in \n cli.main()\n ~~~~~~~~^^\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/server/cli.py", line 501, in main\n run()\n ~~~^^\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/server/cli.py", line 468, in attach_to_pid\n log.reraise_exception("Code injection into PID={0} failed:", pid)\n ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/../debugpy/server/cli.py", line 461, in attach_to_pid\n add_code_to_python_process.run_python_code(\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^\n pid,\n ^^^^\n ...<2 lines>...\n show_debug_info=int(os.getenv("DEBUGPY_ATTACH_BY_PID_DEBUG_INFO", "0")),\n ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n )\n ^\n File "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/add_code_to_python_process.py", line 540, in run_python_code_mac\n subprocess.check_call(" ".join(cmd), shell=True, env=env)\n ~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File "/opt/homebrew/Cellar/[email protected]/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/subprocess.py", line 419, in check_call\n raise CalledProcessError(retcode, cmd)\nsubprocess.CalledProcessError: Command 'lldb --no-lldbinit --script-language Python -o 'process attach --pid 39241' -o 'command script import "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac/lldb_prepare.py"' -o 'load_lib_and_attach "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_x86_64.dylib" 0 "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 85, 115, 101, 114, 115, 47, 114, 101, 109, 99, 111, 115, 109, 105, 116, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 115, 47, 99, 111, 100, 101, 47, 116, 101, 115, 116, 45, 112, 121, 116, 104, 111, 110, 45, 112, 114, 111, 106, 101, 99, 116, 47, 46, 118, 101, 110, 118, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 51, 46, 49, 51, 47, 115, 105, 116, 101, 45, 112, 97, 99, 107, 97, 103, 101, 115, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 53, 48, 56, 51, 50, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 49, 52, 50, 52, 49, 97, 56, 53, 100, 51, 55, 56, 100, 56, 97, 102, 52, 56, 101, 51, 99, 98, 100, 52, 54, 57, 54, 102, 101, 56, 49, 102, 57, 100, 53, 56, 50, 99, 49, 100, 57, 56, 54, 51, 98, 49, 53, 55, 100, 52, 51, 99, 101, 102, 99, 49, 102, 56, 51, 57, 57, 51, 55, 53, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);" 0' -o 'process detach' -o 'script import os; os._exit(1)'' returned non-zero exit status 1.\nUsing /Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_x86_64.dylib in arch: arm64.\nRunning: lldb --no-lldbinit --script-language Python -o 'process attach --pid 39241' -o 'command script import "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/linux_and_mac/lldb_prepare.py"' -o 'load_lib_and_attach "/Users/remcosmits/Documents/code/test-python-project/.venv/lib/python3.13/site-packages/debugpy/_vendored/pydevd/pydevd_attach_to_process/attach_x86_64.dylib" 0 "import codecs;import json;import sys;decode = lambda s: codecs.utf_8_decode(bytearray(s))[0] if s is not None else None;script_dir = decode([47, 85, 115, 101, 114, 115, 47, 114, 101, 109, 99, 111, 115, 109, 105, 116, 115, 47, 68, 111, 99, 117, 109, 101, 110, 116, 115, 47, 99, 111, 100, 101, 47, 116, 101, 115, 116, 45, 112, 121, 116, 104, 111, 110, 45, 112, 114, 111, 106, 101, 99, 116, 47, 46, 118, 101, 110, 118, 47, 108, 105, 98, 47, 112, 121, 116, 104, 111, 110, 51, 46, 49, 51, 47, 115, 105, 116, 101, 45, 112, 97, 99, 107, 97, 103, 101, 115, 47, 100, 101, 98, 117, 103, 112, 121, 47, 46, 46, 47, 100, 101, 98, 117, 103, 112, 121, 47, 115, 101, 114, 118, 101, 114]);setup = json.loads(decode([123, 34, 109, 111, 100, 101, 34, 58, 32, 34, 99, 111, 110, 110, 101, 99, 116, 34, 44, 32, 34, 97, 100, 100, 114, 101, 115, 115, 34, 58, 32, 91, 34, 49, 50, 55, 46, 48, 46, 48, 46, 49, 34, 44, 32, 53, 48, 56, 51, 50, 93, 44, 32, 34, 119, 97, 105, 116, 95, 102, 111, 114, 95, 99, 108, 105, 101, 110, 116, 34, 58, 32, 102, 97, 108, 115, 101, 44, 32, 34, 108, 111, 103, 95, 116, 111, 34, 58, 32, 110, 117, 108, 108, 44, 32, 34, 97, 100, 97, 112, 116, 101, 114, 95, 97, 99, 99, 101, 115, 115, 95, 116, 111, 107, 101, 110, 34, 58, 32, 34, 49, 52, 50, 52, 49, 97, 56, 53, 100, 51, 55, 56, 100, 56, 97, 102, 52, 56, 101, 51, 99, 98, 100, 52, 54, 57, 54, 102, 101, 56, 49, 102, 57, 100, 53, 56, 50, 99, 49, 100, 57, 56, 54, 51, 98, 49, 53, 55, 100, 52, 51, 99, 101, 102, 99, 49, 102, 56, 51, 57, 57, 51, 55, 53, 34, 125]));sys.path.insert(0, script_dir);import attach_pid_injected;del sys.path[0];attach_pid_injected.attach(setup);" 0' -o 'process detach' -o 'script import os; os._exit(1)'

@rchiodo
Copy link
Contributor

rchiodo commented Feb 3, 2025

Attach failing to inject would certainly prevent attach from working. The inject logic uses gdb on mac to inject python code into the process that gets debugpy working.

Your attach problem might be this:
#1480

As for the log, the PYDEVD_DEBUG_FILE should be where it writes the log. In debugpy this is set here:

os.environ[str("PYDEVD_DEBUG_FILE")] = os.environ["DEBUGPY_LOG_DIR"] + str("/debugpy.pydevd.log")

@Anthony-Eid
Copy link

Thanks for the heads up @rchiodo. MacOS on Arm doesn't support gdb is there a work around Remco and I could use to get it working with lldb?

@rchiodo
Copy link
Contributor

rchiodo commented Feb 3, 2025

Sorry I mispoke, mac uses lldb already. This code here:

def run_python_code_mac(pid, python_code, connect_debugger_tracing=False, show_debug_info=0):

There were some issues that I was having trouble finding where the debugger would actually break on enter instead of just running and evaling. Those issues if I recall correctly were on Mac.

@rchiodo
Copy link
Contributor

rchiodo commented Feb 3, 2025

Ah this one. Attach doesn't work on arm, probably because the dylib isn't compiled for arm:
#1220 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

4 participants