-2

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.

starball
  • 20,030
  • 7
  • 43
  • 238
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
  • Are you asking how to debug bash scripts with VSCode? – Eugene Sh. Feb 15 '23 at 18:20
  • 2
    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. if that's not an option, you might be out of luck with this exact question. – starball Feb 15 '23 at 18:20
  • No I am asking how to debug Bash itself, the C program that executes Bash scripts. – Boris Verkhovskiy Feb 15 '23 at 18:20
  • Maybe investigate the [bash debugging features](https://www.cyberciti.biz/tips/debugging-shell-script.html) e.g. `-x` will help. – jarmod Feb 15 '23 at 18:29
  • @jarmod as I said already, I want to learn how Bash itself works under the hood, how it parses text into [a `word_list`](https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/command.h#L130-L140), etc., not debug a script. – Boris Verkhovskiy Feb 15 '23 at 18:30
  • 3
    Sounds like the question is "how to set up VSCode to use lldb?". And then it becomes a duplicate of https://stackoverflow.com/questions/70245851/how-to-debug-in-vs-code-using-lldb – Eugene Sh. Feb 15 '23 at 18:32
  • @EugeneSh. that's correct. I opened the Bash source code in VS Code, ran `make`, clicked "Add Configuration..." > "C/C++: (lldb) Attach" in launch.json and set `"program": "${workspaceFolder}/bash",` and `"args": ["-c", "ls"],` added a breakpoint and it's running something but it doesn't do anything, it spews out a bunch of output, the first line of which is "`Warning: Debuggee TargetArchitecture not detected, assuming x86_64.`" (which is concerning since I'm on ARM not x86) and the last line which it hangs on is "`Loaded '/opt/homebrew/Cellar/coreutils/9.1/bin/gls'. Symbols loaded.`" – Boris Verkhovskiy Feb 15 '23 at 18:36
  • 1
    FWIW, it looks like [some people](https://www.reddit.com/r/mac/comments/sb3t2h/gcc_and_gdb_on_m1_macs/) had some success building `gdb` for `M1`. – Eugene Sh. Feb 15 '23 at 18:37
  • 1
    @BorisVerkhovskiy Then your next stop is [here](https://stackoverflow.com/questions/67270447/vscode-lldb-on-macos-error-when-starting-debugging-session) :) – Eugene Sh. Feb 15 '23 at 18:41
  • 1
    @EugeneSh. thank you for your [first link](https://stackoverflow.com/questions/70245851/how-to-debug-in-vs-code-using-lldb), I think I got it working! As to your second link just now, if you look [closely](https://stackoverflow.com/questions/67270447/vscode-lldb-on-macos-error-when-starting-debugging-session#comment133147875_67693086) at the answer there you'll see I was already on that question before asking this one :) – Boris Verkhovskiy Feb 15 '23 at 18:43

2 Answers2

0

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?.

starball
  • 20,030
  • 7
  • 43
  • 238
  • I started down this path after your comment gave me the confidence, but can you write the launch.json config for me please? You can get the Bash code with `git clone https://git.savannah.gnu.org/git/bash.git` – Boris Verkhovskiy Feb 15 '23 at 18:26
  • @BorisVerkhovskiy launch.json for what? GDB? LLDB? You should sort out what can even work for you first. – starball Feb 15 '23 at 18:39
  • I already mentioned I can't use GDB. I think I got it working with Eugene's help though – Boris Verkhovskiy Feb 15 '23 at 18:45
0

(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
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103