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 *.dylib
# Executables # Executables
bin/
*.exe *.exe
*.out *.out
*.app *.app

View File

@ -1,7 +1,7 @@
CC = gcc CC = gcc
OUT_DIR = ./build OUT_DIR = ./build
TARGET = proxmon TARGET = ./bin/proxmon
SRC_DIRS = ./src SRC_DIRS = ./src
# Collect all C source files in the current directory # 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 # Generate object files in the build directory
OBJS := $(patsubst $(SRC_DIRS)/%.c, $(OUT_DIR)/%.o, $(SRCS)) 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_DIRS := $(shell find $(SRC_DIRS) -type d) $(INC_DIRS)
INC_FLAGS := $(addprefix -I,$(INC_DIRS)) INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CCFLAGS := $(INC_FLAGS) CCFLAGS := $(INC_FLAGS)
./bin:
@mkdir -p ./bin
$(OUT_DIR): $(OUT_DIR):
@mkdir -p $@ @mkdir -p $@
$(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR) $(OUT_DIR)/%.o: $(SRC_DIRS)/%.c | $(OUT_DIR)
$(CC) $(CCFLAGS) -c $< -o $@ $(CC) $(CCFLAGS) -c $< -o $@
$(TARGET): $(OBJS) $(TARGET): $(OBJS) | ./bin
$(CC) $(CCFLAGS) $(OBJS) -o $@ $(CC) $(CCFLAGS) $(OBJS) -o $@
.PHONY: all clean .PHONY: all clean
all: $(TARGET) all: $(TARGET)
clean: 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 // For our own purposes
struct st_process *next; struct st_process *next;
struct st_process *prev; struct st_process *prev;
uint32_t flags;
// attributes identifying thread // attributes identifying thread
int lxc; int lxc;
int pid; int pid;

View File

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