【发布时间】:2018-12-22 18:05:51
【问题描述】:
***查看底部进行编辑*
我正在尝试在 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 按任意键继续...
所以我看到它正在命中我的代码(通过粗体字),但它没有达到我在 main() 顶部的 VS Code 中设置的断点
编辑:我发现问题出在我的 launch.json 中,当向 args 属性添加参数时,代码不会在断点处停止。当我删除参数时,代码会正确地停在断点处
【问题讨论】:
-
快速尝试禁用所有优化,即插入 -O0 标志。这样就不会跳过断点。
-
@John 我试过了,但不幸的是它仍然没有在断点处停止
-
关于;
inputFd = open(argv[1], O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);为什么参数:O_CREAT用于输入文件?此外,如果输入文件由于其权限而无法读取,则列出一堆权限将无济于事。建议使用:inputFd = open( argv[1], O_RDWR ); -
这段代码也发生了同样的事情,当我将它移动到不同目录中的不同文件时,断点起作用。我不太熟悉编译中幕后发生的事情。是否有可能会搞砸的临时调试文件夹?
-
关于:
"command": "gcc -g teeCommand.c -o teeCommand",这不是启用编译器警告。建议插入:-Wall -Wextra -Wconversion -pedantic -std=gnu11
标签: c linux debugging visual-studio-code visual-studio-debugging