当未在launch.json断点中添加命令行参数时击中Visual Studio代码

时间:2018-12-22 18:05:52

标签: c linux debugging visual-studio-code visual-studio-debugging

***请参阅底部进行编辑*

我正在尝试在VS Code中调试C程序,但断点没有达到。在上周调试的另一个程序中,我遇到了类似的问题,然后将代码移至另一个文件,在VS Code中打开了文件,断点工作正常。 因此,我认为这是我的错误,但我不知道这是什么。 我在堆栈溢出时阅读的大多数内容是,人们在编译时忘记添加-g。 这些是我查看过的一些资源:

https://code.visualstudio.com/docs/languages/cpp

https://github.com/Microsoft/vscode-cpptools/issues/1685

teeCommand.c

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>

#include <unistd.h>
#include <fcntl.h>

#define BUF_SIZE 1024
#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)


int main(int argc, char *argv[])
{
int fileLength;
int inputFd, outputFd;
char* buffer;
ssize_t numRead;
char buf[BUF_SIZE];

if(argc < 5){
    printf("not enough arguments given");
}

inputFd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(inputFd == -1){
    handle_error("opening input file");
}

outputFd = open(argv[4], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(outputFd == -1){
    handle_error("opening file output file");
}

while((numRead = read(inputFd, buf, BUF_SIZE)) > 0){
    if(write(outputFd, buf, numRead) != numRead){
        handle_error("could not write the whole buffer");
    }
}
if(numRead == -1){
    exit(1);
}

if(close(inputFd) == -1){
    exit(1);
}
if(close(outputFd) == -1){
    exit(1);
}

exit(EXIT_SUCCESS);
}

tasks.json

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
    {
        "label": "echo",
        "type": "shell",
        "command": "gcc -g teeCommand.c -o teeCommand",
        "group":{
            "kind": "build",
            "isDefault": true
        }
    }
]
}

launch.json

{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "(gdb) Launch",
        "type": "cppdbg",
        "request": "launch",
        "program": "${workspaceFolder}/teeCommand",
        "args": ["file.txt", "|", "tee", "file2.txt"],
        "stopAtEntry": false,
        "cwd": "${workspaceFolder}",
        "environment": [],
        "externalConsole": true,
        "MIMode": "gdb",
        "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
        ]
    }
]
}

我的输出是:

&“警告:GDB:无法设置控制终端:不允许的操作\ n” 给出的参数不足[1] +完成 / usr / bin / gdb --interpreter = mi --tty = $ {DbgTerm} 0 / tmp / Microsoft-MIEngine-Out-nwqez75o.cn0 按任意键继续...

所以我看到它达到了我的代码(以粗体显示),但是没有达到我在VS Code中在main()顶部设置的断点

编辑:我看到问题出在我的launch.json中,当有参数添加到args属性时,代码不会在断点处停止。当我删除参数时,代码会正确地停在断点处

1 个答案:

答案 0 :(得分:0)

强烈建议:

gdb teecommand
br main
r "file.txt", "|", "tee", "file2.txt"

可以按预期工作,因此问题出在.json脚本中