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

Improve debugging with dlv dap (in VSCode, for example) #2651

Merged

Conversation

norio-nomura
Copy link
Contributor

@norio-nomura norio-nomura commented Sep 24, 2024

- hack/toolexec-for-codesign.sh: add a wrapper script for -toolexec to codesign an executable produced by linker

By passing -toolexec hack/toolexec-for-codesign.sh to the Go build options, the executable will be codesigned with vz.entitlements after linking. This eliminates the need to prepare a pre-signed debug binary when running dlv dap (in VSCode, for example) for debugging.
usage in launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug limactl hostagent for debug instance",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceFolder}/cmd/limactl",
            "buildFlags": [
                "-toolexec",
                "${workspaceFolder}/hack/toolexec-for-codesign.sh",
            ],
            "env": {
                "CGO_ENABLED": "1"
            },
            "cwd": "${userHome}/.lima/debug",
            "args": [
                "hostagent",
                "--pidfile",
                "ha.pid",
                "--socket",
                "ha.sock",
                "debug"
            ],
            "preLaunchTask": "prepare launching hostagent for debug instance",
            "postDebugTask": "clean up after stopping hostagent for debug instance"
        },
    ]
}

- pkg/usrlocalsharelima: add a candidate directory to searching lima-guestagent

When debugging cmd/limactl with dlv dap, the executable appears as follows:
cmd/limactl/__debug_bin2611107549.
In this case, limactl fails to locate lima-guestagent relative to the expected executable path and results in the following error:

{
  "level": "fatal",
  "msg": "failed to find \"lima-guestagent.Linux-aarch64\" binary for \"/Users/norio/ghq/github.com/lima-vm/lima/cmd/limactl/__debug_bin2611107549\", attempted [/Users/norio/ghq/github.com/lima-vm/lima/cmd/limactl/lima-guestagent.Linux-aarch64 /Users/norio/ghq/github.com/lima-vm/lima/cmd/share/lima/lima-guestagent.Linux-aarch64]",
  "time": "2024-09-24T15:02:23+09:00"
}

To avoid this error, it was necessary to create a symbolic link (ln -sf ../_output/share cmd/share) so that lima-guestagent* exists at the expected relative path.
This change adds _output/share/lima/lima-guestagent.* to the search directories, making the symbolic link creation unnecessary before running dlv dap for debugging.

@norio-nomura norio-nomura force-pushed the improve-debugging-with-dlv-dap branch 2 times, most recently from 8db66e1 to bfe4623 Compare October 2, 2024 23:44
…o `codesign` a executable produced by linker

By passing `-toolexec hack/toolexec-for-codesign.sh` to the Go build options, the executable will be `codesign`ed with `vz.entitlements` after linking. This eliminates the need to prepare a pre-signed debug binary when running `dlv dap` (in VSCode, for example) for debugging.

usage in `launch.json`:
```json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug limactl hostagent for debug instance",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceFolder}/cmd/limactl",
            "buildFlags": [
                "-toolexec",
                "${workspaceFolder}/hack/toolexec-for-codesign.sh",
            ],
            "env": {
                "CGO_ENABLED": "1"
            },
            "cwd": "${userHome}/.lima/debug",
            "args": [
                "hostagent",
                "--pidfile",
                "ha.pid",
                "--socket",
                "ha.sock",
                "debug"
            ],
            "preLaunchTask": "prepare launching hostagent for debug instance",
            "postDebugTask": "clean up after stopping hostagent for debug instance"
        },
    ]
}
```

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
…uestagent`

When debugging `cmd/limactl` with `dlv dap`, the executable appears as follows:
`cmd/limactl/__debug_bin2611107549`.
In this case, `limactl` fails to locate `lima-guestagent` relative to the expected executable path and results in the following error:
```json
{
  "level": "fatal",
  "msg": "failed to find \"lima-guestagent.Linux-aarch64\" binary for \"/Users/norio/ghq/github.com/lima-vm/lima/cmd/limactl/__debug_bin2611107549\", attempted [/Users/norio/ghq/github.com/lima-vm/lima/cmd/limactl/lima-guestagent.Linux-aarch64 /Users/norio/ghq/github.com/lima-vm/lima/cmd/share/lima/lima-guestagent.Linux-aarch64]",
  "time": "2024-09-24T15:02:23+09:00"
}
```
To avoid this error, it was necessary to create a symbolic link (`ln -sf ../_output/share cmd/share`) so that `lima-guestagent*` exists at the expected relative path.
This change adds `_output/share/lima/lima-guestagent.*` to the search directories, making the symbolic link creation unnecessary before running `dlv dap` for debugging.

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
… when `logrus.GetLevel() == logrus.DebugLevel`.

Signed-off-by: Norio Nomura <norio.nomura@gmail.com>
Copy link
Member

@AkihiroSuda AkihiroSuda left a comment

Choose a reason for hiding this comment

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

Thanks

@AkihiroSuda AkihiroSuda merged commit abbc487 into lima-vm:master Oct 3, 2024
27 checks passed
@AkihiroSuda AkihiroSuda added this to the v1.0 milestone Oct 3, 2024
@norio-nomura
Copy link
Contributor Author

Thanks! 🙏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants