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

Debugging zig in VSCode #7361

Closed
clankill3r opened this issue Dec 9, 2020 · 7 comments
Closed

Debugging zig in VSCode #7361

clankill3r opened this issue Dec 9, 2020 · 7 comments
Milestone

Comments

@clankill3r
Copy link

I found this post from more then a year ago:

https://www.reddit.com/r/Zig/comments/cl0x6k/debugging_zig_in_vscode/

First off, it's a bit outdated, for example:

Edit ~/.vscode/extensions/webfreak.debug-0.23.1/package.json. (<- this is the path for Linux, you'll need to find the corresponding path for other OSs). In the file there's a list of supported languages (here, it's pretty obvious when you edit the file.). Add a newline with 'zig' there.

Is not needed anymore, cause 'zig' is already in tat file now (nice!).

But the thing is, I can't get the debugger to work.

My task.json looks like this:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "zig build-exe main.zig"
        }
    ]
}

And my launch.json looks like this:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "gdb",
            "request": "launch",
            "target": "main",
            "cwd": "${workspaceRoot}",
            "valuesFormatting": "parseText",
            "preLaunchTask": "build"
        }
    ]
}

I was hoping if someone can help me. Preferred with using ZLS as well (https://github.com/zigtools/zls).

If I manage to get it working I will make a video of how to set it up.

@clankill3r
Copy link
Author

clankill3r commented Dec 9, 2020

I also founded this one:
https://dev.to/watzon/debugging-zig-with-vs-code-44ca

But no luck either. I don't have the "C++ (GDB/LLDB)" option he is talking about but I do have the GDB option.

Hope someone can help.

I can place breakpoints, just nothing happend with them. No errors either.

@raulgrell
Copy link
Contributor

image

This is what I can get for debugging Zig. Locals mostly work, watch mostly doesn't and the inline info is usually alright.
I used to get a lot of "this variable has been optimized out" issues after the return location semantics came along, but that issue seems to be fixed.

This is my launch.json:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(Windows) Attach",
            "type": "cppvsdbg",
            "request": "attach",
            "processId": "${command:pickProcess}"
        },
        {
            "name": "Launch Script",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/zig-cache/bin/zox.exe",
            "args": ["./lib/script.zox"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        },
        {
            "name": "Launch Command",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${workspaceFolder}/zig-cache/bin/zox.exe",
            "args": ["run", "print \"Hello, world!\""],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false
        }
    ]
}

@raulgrell
Copy link
Contributor

I think this is the extension that provides it:
image

@clankill3r
Copy link
Author

I keep a log here off all the thins I do to try to make this work.
As of now I have installed the C/C++ extension as mentioned in the post above.

I made a new launch.json that looks like this now:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            // "MIMode": "lldb"
        }
    ]
}

When I try to debug now, I get:

Screenshot 2020-12-16 at 14 14 53

and:

Screenshot 2020-12-16 at 14 15 00

Which I will attempt to fix now, which I will post below.

@clankill3r
Copy link
Author

clankill3r commented Dec 16, 2020

Ok, I codesigned gdb using this:

https://gcc.gnu.org/onlinedocs/gcc-4.8.1/gnat_ugn_unw/Codesigning-the-Debugger.html

The last command was for me codesign -s gdb-cert /usr/local/bin/gdb.

Now I get this: (Note that I had this error before, but only no panel for the error, probably because the panel was used for the 'not signed' error).

Screenshot 2020-12-18 at 20 10 43

The debug console only shows this now (note that the red line gone is what I get as a the pannel in the image above):

Screenshot 2020-12-18 at 20 15 05

The program '/Users/dw/Desktop/zig_learn/main' has exited with code 42 (0x0000002a).

@clankill3r
Copy link
Author

Ok, I got it working!
First I had //"MIMode": "lldb" in my launch.json, which I changed too "MIMode": "lldb" and now it works.

So this is how my launch.json looks like (I also added a preLaunchTask):

    "version": "0.2.0",
    "configurations": [        
        {
            "name": "(lldb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "build"
        }
    ]
}

Screenshot 2020-12-18 at 20 25 41

I hope this helps someone

@CreatorMetaSky
Copy link

zig-debug-test

this template can help you debug zig with vscode on macos, 😀

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

No branches or pull requests

4 participants