From 510ba202a75dd9043836512b8300a973f7037f41 Mon Sep 17 00:00:00 2001 From: mollismerx Date: Sun, 9 Oct 2016 15:37:55 +0200 Subject: [PATCH] Moved examples to separate dir --- example.cpp | 105 -------------------------- demo.cpp => examples/demo.cpp | 0 demo.h => examples/demo.h | 0 example1.cpp => examples/example1.cpp | 0 example2.cpp => examples/example2.cpp | 0 example3.cpp => examples/example3.cpp | 0 example5.cpp => examples/example5.cpp | 0 example6.cpp => examples/example6.cpp | 0 example7.cpp => examples/example7.cpp | 0 example9.cpp => examples/example9.cpp | 0 examples/makefile | 30 ++++++++ makefile.inc | 21 ++++++ 12 files changed, 51 insertions(+), 105 deletions(-) delete mode 100644 example.cpp rename demo.cpp => examples/demo.cpp (100%) rename demo.h => examples/demo.h (100%) rename example1.cpp => examples/example1.cpp (100%) rename example2.cpp => examples/example2.cpp (100%) rename example3.cpp => examples/example3.cpp (100%) rename example5.cpp => examples/example5.cpp (100%) rename example6.cpp => examples/example6.cpp (100%) rename example7.cpp => examples/example7.cpp (100%) rename example9.cpp => examples/example9.cpp (100%) create mode 100644 examples/makefile create mode 100644 makefile.inc diff --git a/example.cpp b/example.cpp deleted file mode 100644 index 755b5b3..0000000 --- a/example.cpp +++ /dev/null @@ -1,105 +0,0 @@ -#include -#include -#include -#include -#include - -#include "elfspy/SPY.h" -#include "elfspy/Call.h" -#include "elfspy/Arg.h" -#include "elfspy/Result.h" -#include "elfspy/Fake.h" -#include "elfspy/Profiler.h" -//#include "elfspy/StackTrace.h" -//#include "elfspy/StackFrame.h" - -#include "elfspy/demo.h" - -int main(int argc, char** argv) -{ - // initialise - spy::initialise(argc, argv); - { - // add some spies about things that happen in f() - // auto f_call = spy::call(SPY(&f)); // nicer - // auto f_call = spy::call(SPY(&f)); - auto add_spy = SPY(&add); - auto add_arg0 = spy::arg<0>(add_spy); - auto add_arg1 = spy::arg<1>(add_spy); - auto add_call = spy::call(add_spy); - auto add_result = spy::result(add_spy); - //auto add_stack = spy::stack_trace(add_spy); - int rv = f(); - assert(rv == 10); - // assert(f_call->count() == 1); - assert(add_call.count() == 2); - assert(add_arg0.value(0) == 1); - assert(add_arg1.value(0) == 2); - assert(add_arg0.value(1) == 3); - assert(add_arg1.value(1) == 4); - assert(add_result.value(0) == 3); - assert(add_result.value(1) == 7); - } - { - auto add_spy = SPY(&add); - auto add_patch = spy::fake(add_spy, &sub); - int rv = f(); - assert(rv == -2); - } - { - auto add_spy = SPY(&add); - auto lambda = [](int a, int b) { return a - b; }; - auto add_patch = spy::fake(add_spy, lambda); - int rv = f(); - assert(rv == -2); - } - int rv = f(); - assert(rv == 10); - { - time_t time_diff = 0; - auto time_spy = SPY(&time); - auto time_changer = [&time_diff, &time_spy](time_t* tloc) -> time_t { - time_t fake_time = time_spy.invoke_real(tloc) + time_diff; - if (tloc) { - *tloc = fake_time; - } - return fake_time; - }; - auto time_fake = spy::fake(time_spy, time_changer); - // relive Y2K - struct tm false_time; - memset(&false_time, 0, sizeof(false_time)); - false_time.tm_sec = 58; - false_time.tm_min = 59; - false_time.tm_hour = 23; - false_time.tm_mday = 31; - false_time.tm_mon = 11; - false_time.tm_year = 1999 - 1900; - false_time.tm_isdst = -1; - time_t in_the_past = mktime(&false_time); - time_t now = time(nullptr); - time_diff = in_the_past - now; // set the time_changer difference - time_t reported_time = time(nullptr); - std::cout << ctime(&reported_time) << std::endl; - sleep(4); - reported_time = time(nullptr); - std::cout << ctime(&reported_time) << std::endl; - } -#if 0 - assert(add_stack->value().contains(SPY(&f))); - for (spy::StackFrame frame : add_stack->value()) { - std::cout << frame.name() << std::endl; - } -#endif - { - auto add_spy = SPY(&add); - auto add_profiler = spy::profiler(add_spy); - int rv = f(); - int run = 0; - for (auto duration : add_profiler) { - std::cout << ++run << ": " << duration << " nanoseconds" << std::endl; - } - } - std::cout << "test passed" << std::endl; - return 0; -} diff --git a/demo.cpp b/examples/demo.cpp similarity index 100% rename from demo.cpp rename to examples/demo.cpp diff --git a/demo.h b/examples/demo.h similarity index 100% rename from demo.h rename to examples/demo.h diff --git a/example1.cpp b/examples/example1.cpp similarity index 100% rename from example1.cpp rename to examples/example1.cpp diff --git a/example2.cpp b/examples/example2.cpp similarity index 100% rename from example2.cpp rename to examples/example2.cpp diff --git a/example3.cpp b/examples/example3.cpp similarity index 100% rename from example3.cpp rename to examples/example3.cpp diff --git a/example5.cpp b/examples/example5.cpp similarity index 100% rename from example5.cpp rename to examples/example5.cpp diff --git a/example6.cpp b/examples/example6.cpp similarity index 100% rename from example6.cpp rename to examples/example6.cpp diff --git a/example7.cpp b/examples/example7.cpp similarity index 100% rename from example7.cpp rename to examples/example7.cpp diff --git a/example9.cpp b/examples/example9.cpp similarity index 100% rename from example9.cpp rename to examples/example9.cpp diff --git a/examples/makefile b/examples/makefile new file mode 100644 index 0000000..b18844d --- /dev/null +++ b/examples/makefile @@ -0,0 +1,30 @@ +.SECONDEXPANSION: + +LIBRARIES := demo +BINARIES := example1 example2 example3 example5 example6 example7 example9 + +demo_OBJS := \ + demo.o + +example1_LIBRARIES := demo +example2_LIBRARIES := demo +example3_LIBRARIES := demo +example5_LIBRARIES := demo +example6_LIBRARIES := demo +example7_LIBRARIES := demo +example9_LIBRARIES := demo +example1_LIBS := elfspy +example2_LIBS := elfspy +example3_LIBS := elfspy +example5_LIBS := elfspy +example6_LIBS := elfspy +example7_LIBS := elfspy +example9_LIBS := elfspy + +INC_DIR := ../.. +LIB_DIR := . .. + +all : $(BINARIES) + +include ../makefile.inc + diff --git a/makefile.inc b/makefile.inc new file mode 100644 index 0000000..b3e22db --- /dev/null +++ b/makefile.inc @@ -0,0 +1,21 @@ +CXXFLAGS := $(patsubst %,-I%,$(INC_DIR)) -std=c++14 -g -fPIC -O0 + +BIND_ALL := -z now + +LD_FLAGS := # $(BIND_ALL) + +%.o : %.cpp + g++ $(CXXFLAGS) -c $< -o $@ + +LIB_TARGETS=$(patsubst %,lib%.so,$(LIBRARIES)) + +$(LIB_TARGETS) : $$($$(patsubst lib%.so,%,$$@)_OBJS) + g++ $(CXXFLAGS) -shared $^ -o $@ $(LD_FLAGS) + readelf -Wa $@ | c++filt > $(@:%.so=%.elf) + +$(BINARIES) : $$(@).cpp $$(patsubst %,lib%.so,$$($$@_LIBRARIES)) + g++ $(CXXFLAGS) $(@).cpp -o $@ $(LD_FLAGS) $(patsubst %,-L%,$(LIB_DIR)) $(patsubst %,-l%,$($@_LIBRARIES) $($@_LIBS)) -rdynamic -ldl + readelf -Wa $@ | c++filt > $(@).elf + +clean : + rm -f *.o *.so $(BINARIES) *.elf