添加调试
This commit is contained in:
		@@ -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 \
 | 
				
			||||||
#清理 apt 缓存  
 | 
					    build-essential \
 | 
				
			||||||
 | 
					    gdb \          # 添加GDB调试器
 | 
				
			||||||
 | 
					    gcc \
 | 
				
			||||||
 | 
					    make && \
 | 
				
			||||||
 | 
					    # 清理 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 
 | 
					 | 
				
			||||||
@@ -1,18 +1,23 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    "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": {
 | 
				
			||||||
        "extensions": [
 | 
					                "debug.onTaskErrors": "debugAnyway"
 | 
				
			||||||
        ]
 | 
					            },
 | 
				
			||||||
      }
 | 
					            "extensions": [
 | 
				
			||||||
 | 
					                "ms-vscode.cpptools"
 | 
				
			||||||
 | 
					            ]
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "portsAttributes": {
 | 
					    "portsAttributes": {
 | 
				
			||||||
      "3000": {
 | 
					        "3000": {
 | 
				
			||||||
        "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
									
								
							
							
						
						
									
										65
									
								
								.vscode/launch.json
									
									
									
									
										vendored
									
									
										Normal 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
									
								
							
							
						
						
									
										35
									
								
								.vscode/tasks.json
									
									
									
									
										vendored
									
									
										Normal 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"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    ]
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										34
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								Makefile
									
									
									
									
									
								
							@@ -3,27 +3,53 @@
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
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  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
execlp: execlp.o  
 | 
					execlp: execlp.o  
 | 
				
			||||||
	$(CC) -o execlp execlp.o  
 | 
						$(CC) -o execlp execlp.o  
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#  从 .c 文件编译生成 .o 文件  
 | 
					# 从 .c 文件编译生成 .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
 | 
				
			||||||
							
								
								
									
										56
									
								
								src/execlp.c
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								src/execlp.c
									
									
									
									
									
								
							@@ -3,29 +3,37 @@
 | 
				
			|||||||
#include <unistd.h>
 | 
					#include <unistd.h>
 | 
				
			||||||
#include <sys/wait.h> 
 | 
					#include <sys/wait.h> 
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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) 
 | 
					 | 
				
			||||||
	{ 
 | 
					 | 
				
			||||||
		/* error occurred */
 | 
					 | 
				
			||||||
		fprintf(stderr,"Fork Failed!");
 | 
					 | 
				
			||||||
		exit(-1);
 | 
					 | 
				
			||||||
	} 
 | 
					 | 
				
			||||||
	else if (pid==0) 
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		/*	 child process 	*/
 | 
					 | 
				
			||||||
		execlp("/bin/ls","ls",NULL);
 | 
					 | 
				
			||||||
	} 
 | 
					 | 
				
			||||||
	else 
 | 
					 | 
				
			||||||
	{ 	
 | 
					 | 
				
			||||||
		/* 	parent process	 */
 | 
					 | 
				
			||||||
		/* parent will wait for the child to complete*/
 | 
					 | 
				
			||||||
		wait(NULL);
 | 
					 | 
				
			||||||
		printf("Child Complete!");
 | 
					 | 
				
			||||||
		exit(0);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    pid_t pid = fork(); // 使用pid_t类型更规范
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    if (pid < 0) { 
 | 
				
			||||||
 | 
					        /* 错误处理 */
 | 
				
			||||||
 | 
					        perror("Fork Failed"); 
 | 
				
			||||||
 | 
					        exit(EXIT_FAILURE);    // 使用标准失败码
 | 
				
			||||||
 | 
					    } 
 | 
				
			||||||
 | 
					    else if (pid == 0) {
 | 
				
			||||||
 | 
					        /* 子进程 */
 | 
				
			||||||
 | 
					        execlp("/bin/ls", "ls", (char *)NULL); // 确保NULL转换为(char *)
 | 
				
			||||||
 | 
					        
 | 
				
			||||||
 | 
					        // 如果exec失败才会执行到这里
 | 
				
			||||||
 | 
					        perror("Exec Failed");
 | 
				
			||||||
 | 
					        exit(EXIT_FAILURE);
 | 
				
			||||||
 | 
					    } 
 | 
				
			||||||
 | 
					    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); // 使用标准成功码
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
		Reference in New Issue
	
	Block a user