Shark代码分析笔记(1)——Makefile

Shark项目的Makefile

#Define BPF_ENABLE if Linux kernel is 4.0+
#BPF_DISABLE=1

CFLAGS=-I. -I core/ -I core/libuv/include -I core/luajit/src/ -I bpf/libbpf/

CORE_LIB=core/luajit/src/libluajit.a core/libuv/.libs/libuv.a

PERF_LIBS= perf/libperf.a perf/libtraceevent.a perf/libapikfs.a
LIB=$(CORE_LIB) $(PERF_LIBS) -lm -ldl -lelf -lc -lpthread

OBJS=core/shark.o core/luv/luv.o perf/perf.o

BUILTIN_LUA_OBJS = perf/perf_builtin_lua.o
OBJS += $(BUILTIN_LUA_OBJS)

ifndef BPF_DISABLE
OBJS += bpf/bpf.o bpf/libbpf/bpf_load.o bpf/libbpf/libbpf.o
BUILTIN_LUA_OBJS += bpf/bpf_builtin_lua.o
else
CFLAGS += -DBPF_DISABLE
endif

TARGET=shark

#ffi need to call some functions in library, so add -rdynamic option
$(TARGET) : core/luajit/src/libluajit.a core/libuv/.libs/libuv.a core/shark_init.h $(OBJS) force
    $(CC) -o $(TARGET) -rdynamic $(OBJS) $(LIB)

core/luajit/src/libluajit.a:
    @cd core/luajit; make

core/libuv/.libs/libuv.a:
    @cd core/libuv; ./autogen.sh; ./configure; make


DEPS := $(OBJS:.o=.d)
-include $(DEPS)

%.o : %.c
    $(CC) -MD -g -c $(CFLAGS) $< -o $@

LUAJIT_BIN=core/luajit/src/luajit

core/shark_init.h : core/shark_init.lua
    cd core/luajit/src; ./luajit -b ../../shark_init.lua ../../shark_init.h

bpf/bpf_builtin_lua.o : bpf/bpf.lua
    cd core/luajit/src; ./luajit -b ../../../bpf/bpf.lua ../../../bpf/bpf_builtin_lua.o

perf/perf_builtin_lua.o : perf/perf.lua
    cd core/luajit/src; ./luajit -b ../../../perf/perf.lua ../../../perf/perf_builtin_lua.o

force:
    true

clean:
    @rm -rf $(TARGET) *.d *.o core/*.d core/*.o bpf/*.d bpf/*.o perf/*.d perf/*.o core/shark_builtin.h bpf/bpf_builtin_lua.h perf/perf_builtin_lua.h

(1)因为有些BPF的选项只在比较高版本的Linux kernel上才支持,所以加了一个编译开关BPF_DISABLE,可以用来关闭BPF功能( 关闭BPF编译:make BPF_DISABLE=1 )。

(2)

PERF_LIBS= perf/libperf.a perf/libtraceevent.a perf/libapikfs.a

core/luajit/src/libluajit.a:
    @cd core/luajit; make

core/libuv/.libs/libuv.a:
    @cd core/libuv; ./autogen.sh; ./configure; make

使用了三个从Linux kernel生成的perf相关的库:perf/libperf.aperf/libtraceevent.aperf/libapikfs.a

还有三个第三方库:luajit(core/luajit)libuv(core/libuv))luv(core/luv)

(3)

core/shark_init.h : core/shark_init.lua
    cd core/luajit/src; ./luajit -b ../../shark_init.lua ../../shark_init.h

bpf/bpf_builtin_lua.o : bpf/bpf.lua
    cd core/luajit/src; ./luajit -b ../../../bpf/bpf.lua ../../../bpf/bpf_builtin_lua.o

perf/perf_builtin_lua.o : perf/perf.lua
    cd core/luajit/src; ./luajit -b ../../../perf/perf.lua ../../../perf/perf_builtin_lua.o

core/shark_init.lua用来生成core/shark_init.hbpf/bpf.luaperf/perf.lua分别用来生成bpf/bpf_builtin_lua.operf/perf_builtin_lua.o

(4)

DEPS := $(OBJS:.o=.d)
    -include $(DEPS)

%.o : %.c
    $(CC) -MD -g -c $(CFLAGS) $< -o $@

编译生成object文件,并会生成依赖文件。

(5)

TARGET=shark

#ffi need to call some functions in library, so add -rdynamic option
$(TARGET) : core/luajit/src/libluajit.a core/libuv/.libs/libuv.a core/shark_init.h $(OBJS) force
    $(CC) -o $(TARGET) -rdynamic $(OBJS) $(LIB)

最终编译生成一个可执行文件:shark

*NIX & Hacking —— 第6期

做一本我感兴趣的杂志,就这么简单!

CPU

Is there a way to dump a CPU’s CPUID information?

DTrace

Reducing RAM usage in pkgin

Git

365GIT
Deal with git am failures
Ry’s Git Tutorial

Golang

cgasm
Embedding Lua in Go
GopherCon 2015
GoWork
go-torch

Kernel

Getting into Linux Kernel Development
KernelDebuggingTricks
vmlinuz Definition

Python

Functional Programming in Python
Fun with BPF, or, shutting down a TCP listening socket the hard way

Unix

On Hurd, Linux and the (mis)adventures of cross-compiling a GNU Hurd toolchain
Unix as IDE: Introduction

Easter egg

What are some good computer tricks that are not commonly known?
Why aren’t there a lot of old programmers at software companies?
x86 Exploitation 101: “House of Lore” – People and traditions