skip to Main Content

I would like to see how Bash implements command line argument parsing and stepping through the code as it parses some trivial command should be a good way to do that. How do I set this up? Bash is normally run with

./configure
make

which creates a bash executable in the top level directory of the source code. I wanted to run that executable though GDB but it doesn’t support M1 Macs so I was thinking to do it through VS Code but I don’t know where to start.

2

Answers


  1. Chosen as BEST ANSWER

    (on macOS)

    1. git clone https://git.savannah.gnu.org/git/bash.git

    2. Install the CodeLLDB VS Code extension

    3. Add a launch.json file with these contents (set args to the arguments you want to pass to the bash executable):

    {
        "version": "0.2.0",
        "configurations": [
            {
                "type": "lldb",
                "request": "launch",
                "name": "Debug",
                "program": "${workspaceFolder}/bash",
                "args": ["-c", "echo hello world"],
                "cwd": "${workspaceFolder}"
            }
        ]
    }
    
    1. Compile bash (see the INSTALL file for details), which will generate an executable file called "bash":
    ./configure
    make
    
    1. Add a breakpoint somewhere (for example in the main() function) and hit F5 to run the command

  2. To set up C/C++ debugging on VS Code, create a .vscode/launch.json file and fill in the arguments with whatever you need. See the docs here: https://code.visualstudio.com/docs/cpp/launch-json-reference.

    That being said, VS Code’s C/C++ debugger just hooks into an existing debugger like GDB or LLDB. If GDB can’t debug your program, try LLDB (might require building Bash with LLVM instead of GCC- not 100% sure). If that’s not an option, you might be out of luck with this exact question.

    For setup with LLDB, see How to debug in VS Code using lldb?.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search