Fixed build and fixed flag processing

This commit is contained in:
root 2025-01-13 22:27:17 -05:00
parent a5e06d2b93
commit 341a1228aa
5 changed files with 36 additions and 25 deletions

1
.gitignore vendored
View File

@ -29,6 +29,7 @@
*.dylib
# Executables
bin/
*.exe
*.out
*.app

View File

@ -1,7 +1,7 @@
CC = gcc
OUT_DIR = ./build
TARGET = proxmon
TARGET = ./bin/proxmon
SRC_DIRS = ./src
# Collect all C source files in the current directory
@ -10,24 +10,26 @@ 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)
./bin:
@mkdir -p ./bin
$(OUT_DIR):
@mkdir -p $@
$(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR)
$(CC) $(CCFLAGS) -c $< -o $@
$(TARGET): $(OBJS)
$(TARGET): $(OBJS) | ./bin
$(CC) $(CCFLAGS) $(OBJS) -o $@
.PHONY: all clean
all: $(TARGET)
clean:
@rm -rf $(TARGET) $(OUT_DIR)
@rm -rf bin/ $(OUT_DIR)

BIN
proxmon

Binary file not shown.

View File

@ -7,6 +7,7 @@ typedef struct st_process {
// For our own purposes
struct st_process *next;
struct st_process *prev;
uint32_t flags;
// attributes identifying thread
int lxc;
int pid;
@ -22,4 +23,4 @@ typedef struct st_process {
uint64_t resident;
} Process;
#endif
#endif

View File

@ -8,9 +8,12 @@
#include <unistd.h>
#include <stdint.h>
#include <dirent.h>
#include <linux/sched.h>
#include "process.h"
#ifndef PF_KTHREAD
# define PF_KTHREAD 0x00200000
#endif
static inline uint64_t fast_str2ull(char** str) {
uint64_t result = 0;
int maxlen = 20; // length of maximum value of 18446744073709551615
@ -66,18 +69,22 @@ int parseStatFile(Process *proc, char *filedata) {
proc->tgid = fast_str2ll(&location);
location += 1;
if (proc->tgid != proc->pid) return -1;
if (proc->tgid && proc->tgid != proc->pid) return -1;
// (6) session - %d
location = strchr(location, ' ') + 1;
// skip [7 - 14)
skipRange(7,14);
//skipRange(7, 9);
//skipRange(7,14);
skipRange(7, 9);
// (9) flags - %u
//proc->iskernel = fast_str2ull(&location) & PF_KTHREAD;
//location += 1;
proc->flags = fast_str2ull(&location);
location += 1;
printf("Flags for %d: %u %d\n", proc->pid, proc->flags, proc->flags & PF_KTHREAD);
proc->iskernel = (proc->flags & PF_KTHREAD) ? true : false;
location += 1;
//skipRange(10, 14);
@ -135,7 +142,7 @@ int main(int argc, char *argv[]) {
// Process each entry.
char *filedata = malloc(4096);
char *buffer = malloc(4096);
char *fname = malloc(1024);
FILE *file;
@ -148,10 +155,10 @@ int main(int argc, char *argv[]) {
if (first < '0' || first > '9') continue;
sprintf(fname, "/proc/%s/stat", pDirent->d_name);
file = fopen(fname, "rb");
fread(filedata, 1, 4096, file);
fread(buffer, 1, 4096, file);
fclose(file);
if (parseStatFile(cur, filedata)) continue;
if (parseStatFile(cur, buffer)) continue;
cur->next = malloc(sizeof(Process));
cur->next->label = malloc(256);
cur->next->prev = cur;
@ -159,30 +166,30 @@ int main(int argc, char *argv[]) {
strcat(fname, "us");
file = fopen(fname, "rb");
fread(filedata, 1, 4096, file);
fread(buffer, 1, 4096, file);
fclose(file);
strtok(filedata, "\n");
printf("[%s]\n", filedata);
strtok(buffer, "\n");
}
closedir(pDir);
cur = cur->prev;
free(cur->next);
cur->next = NULL;
int clocks = sysconf(_SC_CLK_TCK);
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);
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);
// create process descriptor
sprintf(buffer, "pid=\"%d\",ppid=\"%d\",label=\"%s\",kernel=\"%d\"", cur->pid, cur->ppid, cur->label, cur->iskernel);
printf("proc_cpu_time{%s} %d\n", buffer, cur->cpuTime);
printf("proc_child_cpu_time{%s} %d\n", buffer, cur->childCpuTime / clocks);
printf("proc_num_threads{%s} %d\n", buffer, cur->nThreads);
printf("proc_resident_bytes{%s} %d\n", buffer, cur->resident);
cur = cur->next;
}
// Close directory and exit.
closedir(pDir);
return 0;
}