From 1f63f81979f5d316053f4ebf8068c7aebcb4e2fd Mon Sep 17 00:00:00 2001 From: diamante0018 Date: Sat, 22 Mar 2025 15:18:26 +0100 Subject: [PATCH] feat: ultimate dev container --- .devcontainer/Dockerfile | 55 +++++++++++++++++++++++++++++++++ .devcontainer/devcontainer.json | 32 +++++++++++++++++++ .vscode/launch.json | 51 ++++++++++++++++++++++++++++++ .vscode/tasks.json | 54 ++++++++++++++++++++++++++++++++ src/main.c | 6 ++++ src/monitor.c | 8 +++++ 6 files changed, 206 insertions(+) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 0000000..b9249b3 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,55 @@ +FROM ubuntu:noble + +# Official workaround for "groupadd: GID '1000' already exists" +RUN userdel -r ubuntu + +# Set non-interactive mode for apt-get +ENV DEBIAN_FRONTEND=noninteractive + +# Install basic dependencies for compilation +RUN apt-get update && apt-get install -y --no-install-recommends \ + build-essential \ + gcc \ + gcc-multilib \ + g++-multilib \ + make \ + wget \ + unzip \ + git \ + gdb \ + libc6-dbg \ + valgrind \ + strace \ + clang-format \ + python3 python3-pip python3-venv \ + && apt-get clean && rm -rf /var/lib/apt/lists/* + +# Create a virtual environment and install colour-valgrind +RUN python3 -m venv /opt/venv && \ + /opt/venv/bin/pip install colour-valgrind + +# Add the virtual environment to PATH for all users +ENV PATH="/opt/venv/bin:$PATH" + +# Download and install Premake5 +RUN wget https://github.com/premake/premake-core/releases/download/v5.0.0-beta5/premake-5.0.0-beta5-linux.tar.gz \ + && tar -xvf premake-5.0.0-beta5-linux.tar.gz \ + && mv premake5 /usr/local/bin/ \ + && chmod +x /usr/local/bin/premake5 \ + && rm premake-5.0.0-beta5-linux.tar.gz + +ARG USERNAME=vscode +ARG USER_UID=1000 +ARG USER_GID=$USER_UID + +# Create the user +RUN groupadd --gid $USER_GID $USERNAME \ + && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ + # + # [Optional] Add sudo support. Omit if you don't need to install software after connecting. + && apt-get update \ + && apt-get install -y sudo \ + && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ + && chmod 0440 /etc/sudoers.d/$USERNAME + +USER $USERNAME diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..d3ad5f6 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,32 @@ +{ + "name": "MonitorRam Dev Container", + "build": { + "context": "..", + "dockerfile": "./Dockerfile" + }, + "capAdd": [ + "SYS_PTRACE" + ], + "securityOpt": [ + "seccomp=unconfined" + ], + "customizations": { + "vscode": { + "extensions": [ + "ms-vscode.cpptools-extension-pack" + ], + "settings": { + "C_Cpp.clang_format_style": "file", + "C_Cpp.formatting": "clangFormat", + "editor.formatOnSave": true + } + } + }, + "runArgs": [ + "--network=host" + ], + "forwardPorts": [ + 3000 + ], + "remoteUser": "vscode" +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..af99076 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,51 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "GDB Debug (Debug Build)", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/bin/x64/Debug/MonitorRam", + "args": [ + "" + ], // Arguments to pass to your program + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", // Path to gdb inside the container + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "build-debug" // Task to build the Debug version + }, + { + "name": "GDB Debug (Release Build)", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/bin/x64/Release/MonitorRam", + "args": [ + "" + ], // Arguments to pass to your program + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "build-release" // Task to build the Release version + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..aa71d27 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,54 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "generate-makefiles", + "type": "shell", + "command": "premake5", + "args": [ + "gmake" + ], // Generate Makefiles + "group": { + "kind": "build", + "isDefault": false + }, + "problemMatcher": [] + }, + { + "label": "build-debug", + "type": "shell", + "command": "make", + "args": [ + "-C", + "build", + "config=debug_x64" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [ + "$gcc" + ], + "dependsOn": "generate-makefiles" + }, + { + "label": "build-release", + "type": "shell", + "command": "make", + "args": [ + "-C", + "build", + "config=release_x64" + ], + "group": { + "kind": "build", + "isDefault": false + }, + "problemMatcher": [ + "$gcc" + ], + "dependsOn": "generate-makefiles" + } + ] +} \ No newline at end of file diff --git a/src/main.c b/src/main.c index 680fa71..df4b489 100644 --- a/src/main.c +++ b/src/main.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "common.h" @@ -19,7 +20,12 @@ int main(int argc, char* argv[]) { } struct sigaction act; + memset(&act, 0, sizeof(act)); + act.sa_handler = sigint_handler; + sigemptyset(&act.sa_mask); // Initialize the signal mask to empty + act.sa_flags = 0; // Set flags to 0 + sigaction(SIGINT, &act, NULL); int result = setup(argv[1]); diff --git a/src/monitor.c b/src/monitor.c index a8995ec..4374421 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -108,6 +109,13 @@ static int is_process_wild(int curr_real_mem, int peak_real_mem, } int setup(const char* pid) { + assert(pid); + + if (*pid == '\0') { + printf("Invalid PID\n"); + return 1; + } + memset(&processes, 0, sizeof(processes)); char* command = va("pgrep %s", pid);