Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command-line arguments to debugger using VSCode?

I'm using VSCode on Linux and I've came up with a the following launch configuration in my attempt to fire the VSCode debugger, which, in turn, would rely on gdb:

{
    "version": "0.2.0",
    "configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${fileDirname}/${fileBasenameNoExtension}",
        "args": ["a", "b", "c", "d", "e"],
        "stopAtEntry": false,
        "cwd": "${fileDirname}",
        "environment": [],
        "externalConsole": false,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ],
        "preLaunchTask": "make project"
    }]

}

Here, using the args attribute I'd like to pass 5 arguments to the process I'm debugging, namely: "a", "b", "c", "d", "e".

However, when I run the debugger, the argc value is correctly set to 6, but the values themselves, stored by argv are not present.

enter image description here

like image 724
Radu Stoenescu Avatar asked Jul 02 '19 11:07

Radu Stoenescu


People also ask

How do you pass command line arguments in VS Code?

To set command-line arguments in Visual Studio, right click on the project name, then go to Properties. In the Properties Pane, go to "Debugging", and in this pane is a line for "Command-line arguments." Add the values you would like to use on this line. They will be passed to the program via the argv array.

How do I debug a command line argument in Visual Studio?

In Visual Studio 2010, right click the project, choose Properties, click the configuring properties section on the left pane, then click Debugging, then on the right pane there is a box for command arguments. In that enter the command line arguments. You are good to go. Now debug and see the result.

How do I debug code in Visual Studio line by line?

Run to a breakpoint in code You can also select the line and then select F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert Breakpoint. The breakpoint appears as a red dot in the left margin next to the line of code. The debugger suspends execution just before the line runs.

How do I debug console in VS Code?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.


2 Answers

The first argument is always the executable. This is expected behaviour.

like image 53
bazz-dee Avatar answered Oct 10 '22 04:10

bazz-dee


That's cause the type of argv is char**. The debugger doesn't know if it's pointing at a single element or an array.

In VS you can use format specifiers. With gdb you should be able to use something like this in the Watch view:

(char*[6])argv

https://github.com/Microsoft/vscode-cpptools/issues/688#issuecomment-685956825

like image 2
Trass3r Avatar answered Oct 10 '22 04:10

Trass3r