Added Makefile, cleaned up some C code

This commit is contained in:
root 2025-01-13 20:14:30 -05:00
parent 53f8871381
commit a5e06d2b93
5 changed files with 45 additions and 40 deletions

33
Makefile Normal file
View File

@ -0,0 +1,33 @@
CC = gcc
OUT_DIR = ./build
TARGET = proxmon
SRC_DIRS = ./src
# Collect all C source files in the current directory
SRCS := $(shell find $(SRC_DIRS) -name '*.c')
# Generate object files in the build directory
OBJS := $(patsubst $(SRC_DIRS)/%.c, $(OUT_DIR)/%.o, $(SRCS))
INC_DIRS := /usr/src/linux-headers-$(uname -r)/include
INC_DIRS := $(shell find $(SRC_DIRS) -type d) $(INC_DIRS)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CCFLAGS := $(INC_FLAGS)
$(OUT_DIR):
@mkdir -p $@
$(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR)
$(CC) $(CCFLAGS) -c $< -o $@
$(TARGET): $(OBJS)
$(CC) $(CCFLAGS) $(OBJS) -o $@
.PHONY: all clean
all: $(TARGET)
clean:
@rm -rf $(TARGET) $(OUT_DIR)

BIN
proxmon Executable file

Binary file not shown.

View File

@ -1,33 +0,0 @@
#include <stdio.h>
#include <dirent.h>
int main(int argc, char *argv[]) {
struct dirent *pDirent;
DIR *pDir;
// Ensure correct argument count.
if (argc != 2) {
printf("Usage: testprog <dirname>\n");
return 1;
}
// Ensure we can open directory.
pDir = opendir(argv[1]);
if (pDir == NULL) {
printf("Cannot open directory '%s'\n", argv[1]);
return 1;
}
// Process each entry.
while ((pDirent = readdir(pDir)) != NULL) {
printf("[%s]\n", pDirent->d_name);
}
// Close directory and exit.
closedir(pDir);
return 0;
}

View File

@ -2,11 +2,11 @@
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char *argv[]) {
int main3(int argc, char *argv[]) {
DIR *procdir = opendir("/proc");
struct dirent *pDirent;
while ((pDirent = readdir(procdir)) != NULL) {
printf("[%s]\n", pDirent->d_name);
}
}
}

View File

@ -8,6 +8,7 @@
#include <unistd.h>
#include <stdint.h>
#include <dirent.h>
#include <linux/sched.h>
#include "process.h"
static inline uint64_t fast_str2ull(char** str) {
@ -71,13 +72,14 @@ int parseStatFile(Process *proc, char *filedata) {
location = strchr(location, ' ') + 1;
// skip [7 - 14)
skipRange(7, 9);
skipRange(7,14);
//skipRange(7, 9);
// (9) flags - %u
proc->iskernel = fast_str2ull(&location) & PS_KTHREAD;
location += 1;
//proc->iskernel = fast_str2ull(&location) & PF_KTHREAD;
//location += 1;
skipRange(10, 14);
//skipRange(10, 14);
// (14) utime - %lu
proc->cpuTime = fast_str2ull(&location);
@ -172,7 +174,10 @@ int main(int argc, char *argv[]) {
cur = head;
while (cur != NULL) {
printf("proc_cpu_time{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->cpuTime);
cur = cur->next;
printf("proc_child_cpu_time{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->childCpuTime / clocks);
printf("proc_num_threads{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->nThreads);
printf("proc_resident_bytes{pid=\"%d\",ppid=\"%d\",label=\"%s\"} %d\n", cur->pid, cur->ppid, cur->label, cur->resident);
cur = cur->next;
}
// Close directory and exit.