添加调试

This commit is contained in:
2025-10-19 11:44:16 +00:00
parent 9eb6989ad0
commit 97d7d66548
6 changed files with 192 additions and 51 deletions

View File

@@ -1,16 +1,18 @@
FROM mcr.microsoft.com/devcontainers/base:dev-ubuntu-20.04 FROM mcr.microsoft.com/devcontainers/base:dev-ubuntu-20.04
# 安装 build-essential # 安装 build-essential 和调试工具
RUN apt-get update && \ RUN apt-get update && \
apt-get install -y --no-install-recommends build-essential && \ apt-get install -y --no-install-recommends \
build-essential \
gdb \ # 添加GDB调试器
gcc \
make && \
# 清理 apt 缓存 # 清理 apt 缓存
apt-get autoremove -y && \ apt-get autoremove -y && \
apt-get clean && \ apt-get clean && \
rm -rf /var/lib/apt/lists/* rm -rf /var/lib/apt/lists/*
# 设置工作目录(可选) # 设置工作目录
WORKDIR /workspace WORKDIR /workspace
# 可以添加其他的自定义设置,比如复制源代码等 CMD ["/bin/bash"]
CMD ["/bin/bash"] # 可以添加默认命令,比如进入 bash

View File

@@ -1,11 +1,14 @@
{ {
"name": "DevContainerExample", "name": "DevContainerExample",
"image": "mcr.microsoft.com/devcontainers/base:dev-ubuntu-20.04", "image": "mcr.microsoft.com/devcontainers/base:dev-ubuntu-20.04",
"postCreateCommand": "sudo apt-get update && sudo apt-get install -y gdb && make",
"customizations": { "customizations": {
"vscode": { "vscode": {
"settings": {}, "settings": {
"debug.onTaskErrors": "debugAnyway"
},
"extensions": [ "extensions": [
"ms-vscode.cpptools"
] ]
} }
}, },
@@ -14,5 +17,7 @@
"label": "Web Server", "label": "Web Server",
"onAutoForward": "notify" "onAutoForward": "notify"
} }
} },
"initializeCommand": "gdb -ex 'set confirm off' -ex quit", // 初始化GDB配置
"postAttachCommand": "make debug" // 假设你的Makefile有debug目标
} }

65
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,65 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug fork",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/fork",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build-fork"
},
{
"name": "Debug execlp",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/execlp",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build-execlp"
},
{
"name": "Debug All",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/fork", // 默认调试fork
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build-all"
}
]
}

35
.vscode/tasks.json vendored Normal file
View File

@@ -0,0 +1,35 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build-fork",
"type": "shell",
"command": "make",
"args": ["fork"],
"group": "build",
"problemMatcher": ["$gcc"]
},
{
"label": "build-execlp",
"type": "shell",
"command": "make",
"args": ["execlp"],
"group": "build",
"problemMatcher": ["$gcc"]
},
{
"label": "build-all",
"type": "shell",
"command": "make",
"group": "build",
"problemMatcher": ["$gcc"]
},
{
"label": "clean",
"type": "shell",
"command": "make",
"args": ["clean"],
"group": "build"
}
]
}

View File

@@ -3,15 +3,22 @@
# #
include ./make.h include ./make.h
# 目标和源文件 # 目标和源文件
TARGETS = fork execlp TARGETS = fork execlp
SOURCES = $(wildcard $(SRC_DIR)/*.c) # 找到所有的 .c 文件 SOURCES = $(wildcard $(SRC_DIR)/*.c) # 找到所有的 .c 文件
OBJECTS = $(patsubst $(SRC_DIR)/%.c,%.o,$(SOURCES)) # 将 .c 替换为 .o OBJECTS = $(patsubst $(SRC_DIR)/%.c,%.o,$(SOURCES)) # 将 .c 替换为 .o
# 调试标志
DEBUG_FLAGS = -g -O0
CFLAGS += $(DEBUG_FLAGS) # 确保包含调试信息
# 默认目标 # 默认目标
all: $(TARGETS) all: $(TARGETS)
# 调试目标与preLaunchTask对应
debug: CFLAGS += -DDEBUG -Wall -Wextra
debug: $(TARGETS)
# 链接目标文件生成可执行文件 # 链接目标文件生成可执行文件
fork: fork.o fork: fork.o
$(CC) -o fork fork.o $(CC) -o fork fork.o
@@ -23,7 +30,26 @@ execlp: execlp.o
%.o: $(SRC_DIR)/%.c %.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) $(COMMON_INCLUDE_DIRS) -c $< -o $@ $(CC) $(CFLAGS) $(COMMON_INCLUDE_DIRS) -c $< -o $@
# 清理目标
clean: clean:
$(RM) -f $(TARGETS) $(OBJECTS) $(RM) -f $(TARGETS) $(OBJECTS) *.o
# 运行目标
run-fork: fork
./fork
run-execlp: execlp
./execlp
# GDB调试目标
gdb-fork: fork
gdb ./fork
gdb-execlp: execlp
gdb ./execlp
# 安装目录结构(如果需要)
install-dirs:
mkdir -p $(SRC_DIR) $(BUILD_DIR)
.PHONY: all debug clean run-fork run-execlp gdb-fork gdb-execlp install-dirs

View File

@@ -5,27 +5,35 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int pid; (void)argc; // 明确标记未使用
/* fork another process */ (void)argv; // 消除编译器警告
pid = fork();
if (pid<0) pid_t pid = fork(); // 使用pid_t类型更规范
{
/* error occurred */ if (pid < 0) {
fprintf(stderr,"Fork Failed!"); /* 错误处理 */
exit(-1); perror("Fork Failed");
exit(EXIT_FAILURE); // 使用标准失败码
} }
else if (pid==0) else if (pid == 0) {
{ /* 子进程 */
/* child process */ execlp("/bin/ls", "ls", (char *)NULL); // 确保NULL转换为(char *)
execlp("/bin/ls","ls",NULL);
} // 如果exec失败才会执行到这里
else perror("Exec Failed");
{ exit(EXIT_FAILURE);
/* parent process */
/* parent will wait for the child to complete*/
wait(NULL);
printf("Child Complete!");
exit(0);
} }
else {
/* 父进程 */
int status;
wait(&status); // 获取子进程退出状态
if (WIFEXITED(status)) {
printf("Child Completed with status: %d\n", WEXITSTATUS(status));
} else {
printf("Child terminated abnormally\n");
} }
exit(EXIT_SUCCESS); // 使用标准成功码
}
}