Search code examples
openglrustlldb

Window doesn't open when running a Rust OpenGL program inside of LLDB on Windows


I have a minimal Rust/OpenGL app on Windows. I'm using Visual Studio Code, LLDB, and Glutin (a library resembling GLFW).

Launching via cargo run opens an empty window, but when launching via LLDB, no window opens. I've confirmed both in LLDB and with println! that the context-creation functions are being called and the main loop is executing. In other words, I've verified that all lines of code are reached. The same holds true whether running from within VSCode or not.

I'm using a 32-bit Rust toolchain, stable-i686-pc-windows-gnu, because LLDB doesn't fully support 64-bit Windows. Aside from this issue, LLDB seems to be working as expected.

Below is main.rs, which is adapted from the Glutin readme. (Glutin is a Rust library similar to GLFW.) I've deleted all but the essentials necessary to open a window.

Desired behavior: The window opens when the program launches from LLDB, the same as it does when the program launches from outside LLDB.

Actual behavior: The window doesn't open when the program launches from LLDB.

Question: What could explain this difference in behavior? I.e. why would the window not open from LLDB when it does from the terminal?


extern crate gl;
extern crate glutin;

fn main() {
    let events_loop = glutin::EventsLoop::new();
    let window = glutin::WindowBuilder::new();
    let context = glutin::ContextBuilder::new();

    // When running outside LLDB, this line causes the window to appear.
    // The let binding is necessary because without it, the value will be dropped
    // and the window will close before the loop starts.
    let gl_window = glutin::GlWindow::new(window, context, &events_loop).unwrap();

    // Normally, we'd make the window current here. But it's not necessary
    // to reproduce the problem.
    loop {
        // This is where we'd swap the buffers and clear. But it's not necessary
        // to reproduce the problem.
    }
}

Solution

  • Partial answer: As a workaround, you can attach LLDB to a running process instead of launching the process from LLDB. In VSCode, you can do this with: Add Configuration -> LLDB: Attach by Name. With this workflow, the OpenGL window opens just like it would if LLDB weren't involved. Unfortunately, attaching is significantly less ergonomic.

    Update: I prefer to launch with the debugger rather than attach. I've found that Rust's MSVC x64 toolchain along with Microsoft's C/C++ debugger works well for this use case. The steps that work for me are:

    1. If necessary, install MSVC toolchain: rustup install stable-x86_64-pc-windows-msvc
    2. Set the MSVC toolchain as default: rustup default stable-x86_64-pc-windows-msvc
    3. Update Rust: rustup update
    4. Install Microsoft's C/C++ extension for Visual Studio Code. The extension includes a debugger that's compatible with the MSVC binaries that Rust compiles.
    5. Add a debug configuration to Visual Studio Code. I started by adding the default configuration but had to modify it. Ultimately, this is what I had in .vs-code/launch.json--note that the string rust-test is unique to the project:

    -

    {
      "name": "(Windows) Launch",
      "type": "cppvsdbg",
      "request": "launch",
      "program": "${workspaceFolder}/target/debug/rust-test.exe",
      "args": [],
      "symbolSearchPath": "${workspaceFolder}/target/debug/rust-test.pdb",
      "stopAtEntry": false,
      "cwd": "${workspaceFolder}/target/debug",
      "environment": [],
      "externalConsole": true
    }
    

    I'd still be grateful if anyone has any thoughts about the LLDB issue. Although the MSVC toolchain solves my problem for now, there may be others out there who really want to use LLDB and come across this question.