modularml / mojo

The Mojo Programming Language
https://docs.modular.com/mojo/manual/
Other
22.84k stars 2.58k forks source link

[Feature Request] Debugging Support for Mojo in VS Code and PyCharm #1829

Open hkhk368 opened 6 months ago

hkhk368 commented 6 months ago

Review Mojo's priorities

What is your request?

image

I attempted to use breakpoints in VS Code for debugging, but the program did not pause at the breakpoint. I would like to inquire if it's possible to perform visual debugging with Mojo in either VS Code or PyCharm. If this feature is already available, could you please provide detailed instructions? I am a beginner in the Mojo language and have read through the following documentation, but it does not describe how to configure this: https://docs.modular.com/mojo/cli/debug.html. Thank you.

What is your motivation for this change?

This feature is very important for debugging.

Any other details?

image

I attempted to use breakpoints in VS Code for debugging, but the program did not pause at the breakpoint. I would like to inquire if it's possible to perform visual debugging with Mojo in either VS Code or PyCharm. If this feature is already available, could you please provide detailed instructions? I am a beginner in the Mojo language and have read through the following documentation, but it does not describe how to configure this: https://docs.modular.com/mojo/cli/debug.html. Thank you.

arthurevans commented 6 months ago

This should be working in VS Code with 24.1 (make sure you have the 24.1 version of the extension, too).

https://docs.modular.com/mojo/tools/debugging

hkhk368 commented 6 months ago

This should be working in VS Code with 24.1 (make sure you have the 24.1 version of the extension, too).

https://docs.modular.com/mojo/tools/debugging

@arthurevans Thank you for your reply, but I have already installed the latest version 24.1. However, I am still unable to perform step-by-step debugging, and breakpoints are not being successfully hit. Please refer to the screenshot at the top. As soon as I click 'debug' to run, it directly outputs the result without pausing at the breakpoint.

RtyLiu commented 6 months ago

It works for me (VS Code):

image
hkhk368 commented 6 months ago

It works for me (VS Code): image

@RtyLiu Thank you for sharing. My environment is Ubuntu 22.04. I have installed the latest version of VS Code and the Mojo 24.1 plugin. Have I missed anything?

RtyLiu commented 6 months ago

Is your VS Code Mojo extension v24.1.0?

hkhk368 commented 6 months ago

Is your VS Code Mojo extension v24.1.0?

Yes. But not working.

RtyLiu commented 6 months ago

Yes, you are right. I have tested and install it on another computer. The same Ubuntu, WSL 2, VS Code and Mojo extension for VS Code. It does not stop on breakpoint. The only difference is, that on the computer, where is working I have installed Mojo with Max . But I do not thing, that this the reason is.

josiahls commented 4 months ago

Getting a parallel issue with debugging via vscode. I get personality set failed: Operation not permitted. It does corectly detect the debugger since I see "Welcome to Mojo debugging" but vscode still fails.

Let me know if I should make a separate github issue. Posting it here as a comment since this is open still and generally related to vscode debugger support.

Mojo version is: mojo 24.2.1 (2f0dcf11) and the vscode extension is the same

Edit: Does not work for mojo 24.3.0 either

Screenshot from 2024-04-21 17-00-28

lbartworks commented 3 months ago

I see the same issue, not stopping at the right place, as well some crash messages on the debug console: sytem (Win 10, WSL 2, mojo 24.3)

i get this on a simple hello world program

error: JIT(0x7f98dfdd5000)(0x00007f98dfdd5000) The parsed MLIR structure type '!kgen.variant<struct<()>, scalar, scalar, scalar, struct<(pointer, index)>, pointer, pointer, pointer>' has not byte size. The MLIR type won't be used and regular MLIR-agnostic DWARF parsing will be performed. error: JIT(0x7f98dfdd5000)(0x00007f98dfdd5000) [MojoDWARFParser::ParseTypeFromDWARF]: Unhandled type tag. Die = 0x403, tag = DW_TAG_variant_part, name = DW_TAG_variant_part error: JIT(0x7f98dfdd5000)(0x00007f98dfdd5000) [MojoDWARFParser::CompleteTypeFromDWARF]: Couldn't complete the struct type '!kgen.variant<struct<()>, scalar, scalar, scalar, struct<(pointer, index)>, pointer, pointer, pointer>' because one of its fields has no size. Die = 0x3e9, member die = 0x3f0. error: JIT(0x7f98dfdd5000)(0x00007f98dfdd5000) [MojoDWARFParser::CompleteTypeFromDWARF]: The struct type 'pkg stdlib::pkg builtin::module object::struct object' doesn't have the same size as reported in the DWARF after type completion. Die = 0x3d6.

Ivo-Balbaert commented 3 months ago

I have the same experience (in WSL Ubuntu on Windows 11):

josiahls commented 1 month ago

Note for those interested. This might be an issue with "remote/docker" environments:

One of the last blocks in my docker does:

RUN echo 'settings set target.disable-aslr false' > '/home/fastrl_mojo_user/.lldbinit'

Note max-nightly.path should be mojo.path if you're just using mojo

RUN BASHRC=$( [ -f "$HOME/.bash_profile" ] && echo "$HOME/.bash_profile" || echo "$HOME/.bashrc" ) \
  && MOJO_PATH=$(modular config max-nightly.path) \
  && echo 'export MODULAR_HOME="'$HOME'/.modular"' >> "$BASHRC" \
  && echo 'export PATH="'$MOJO_PATH'/bin:$PATH"' >> "$BASHRC" \
  && echo 'export LLDBINIT=/home/fastrl_mojo_user/.lldbinit' >> "$BASHRC" \
  && source "$BASHRC"
josiahls commented 1 month ago

I think the mojo nightly extension doesn't actually have a working debugger.

I suspect it is trying to do mojo-lldb tests/data/datapipes/base_test.mojo which is wrong. It doesn't look like its possible to make the launch.json work correctly enough to make debugging via vscode possible. I don't think the vscode extension auto-gens the correct launch.json. It should use mojo debug not mojo-lldb right? Also there is no logging / errors in this situation. This also still requires my above fix to make any of this possible.

Note the version mojo --version mojo 2024.8.305 (f99b2e40)

fastrl_mojo_user@josiah-laptop:~/fastrl_mojo$ mojo-lldb tests/data/datapipes/base_test.mojo 
(lldb) target create "tests/data/datapipes/base_test.mojo"
error: '/home/fastrl_mojo_user/fastrl_mojo/tests/data/datapipes/base_test.mojo' is not a valid executable
(lldb) ^D
fastrl_mojo_user@josiah-laptop:~/fastrl_mojo$ mojo-lldb mojo tests/data/datapipes/base_test.mojo 
(lldb) target create "mojo"
Current executable set to '/home/fastrl_mojo_user/.modular/pkg/packages.modular.com_nightly_max/bin/mojo' (x86_64).
(lldb) settings set -- target.run-args  "tests/data/datapipes/base_test.mojo"
(lldb) r
Process 10797 launched: '/home/fastrl_mojo_user/.modular/pkg/packages.modular.com_nightly_max/bin/mojo' (x86_64)
Process 10797 stopped and restarted: thread 1 received signal: SIGCHLD
1
2
3
4
5
Process 10797 exited with status = 0 (0x00000000) 
(lldb) ^D
fastrl_mojo_user@josiah-laptop:~/fastrl_mojo$ mojo debug tests/data/datapipes/base_test.mojo 
Current executable set to '/home/fastrl_mojo_user/.modular/pkg/packages.modular.com_nightly_max/bin/mojo' (x86_64).
(lldb) r
Process 10941 launched: '/home/fastrl_mojo_user/.modular/pkg/packages.modular.com_nightly_max/bin/mojo' (x86_64)
Process 10941 stopped and restarted: thread 1 received signal: SIGCHLD
1
2
3
4
5