citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 1ca98f7d4a9d45c8ba150eb8fbb8af74e778dd5b
parent da06da573d534a2900d18437219ff9841b9593a1
Author: kyle <kyle@getaddrinfo.net>
Date:   Sun,  1 Nov 2015 15:25:30 -0700

instrument: write instrumented source to intermediate file

- write an instrumented source file to <filename>.inst
  - copy this file overtop of original for native compiler execution
  - then restore the original file
- updated tests/fibonacci to use this

Diffstat:
Minstrument/instrumenter.cpp | 3++-
Minstrument/main.cpp | 27++++++++++++++++++---------
Mrun_tests.sh | 21+++++++++------------
Mtests/fibonacci/Makefile | 6+++---
Atests/fibonacci/fib.c.inst_good | 38++++++++++++++++++++++++++++++++++++++
Dtests/fibonacci/fib_reference.c | 37-------------------------------------
6 files changed, 70 insertions(+), 62 deletions(-)

diff --git a/instrument/instrumenter.cpp b/instrument/instrumenter.cpp @@ -137,7 +137,8 @@ MyFrontendAction::EndSourceFileAction() TheRewriter.InsertTextAfter(start, ss.str()); // rewrite the original source file - int fd = open(file_name.c_str(), O_WRONLY | O_CREAT); + int fd = open(file_name.append(".inst").c_str(), O_WRONLY | O_CREAT, + S_IRUSR | S_IWUSR); if (fd < 0) err(1, "open"); llvm::raw_fd_ostream output(fd, /* close */ 1); diff --git a/instrument/main.cpp b/instrument/main.cpp @@ -60,19 +60,19 @@ clean_path() setenv("PATH", new_path, 1); #ifdef DEBUG std::cout << "new " << new_path << std::endl; - system("env"); + system("env | grep PATH"); #endif } void -instrument(int argc, char *argv[], std::vector<const char *> &source_files) +instrument(int argc, char *argv[], std::vector<std::string> &source_files) { const char *clang_argv[source_files.size() + 1 + argc]; int clang_argc = 0; clang_argv[clang_argc++] = argv[0]; for (auto s : source_files) - clang_argv[clang_argc++] = s; + clang_argv[clang_argc++] = s.c_str(); clang_argv[clang_argc++] = "--"; @@ -105,7 +105,7 @@ instrument(int argc, char *argv[], std::vector<const char *> &source_files) int main(int argc, char *argv[]) { - std::vector<const char *> source_files; + std::vector<std::string> source_files; for (int i = 0; i < argc; i++) { int arg_len = strlen(argv[i]); @@ -116,7 +116,7 @@ main(int argc, char *argv[]) if (strcmp(argv[i] + arg_len - 4, ".cpp") == 0 || strcmp(argv[i] + arg_len - 2, ".c") == 0) // keep track of original source file names - source_files.push_back(argv[i]); + source_files.push_back(std::string(argv[i])); } // very important that argv passed to execvp is NULL terminated argv[argc] = NULL; @@ -134,8 +134,7 @@ main(int argc, char *argv[]) // backup original source files for (auto s : source_files) { std::ifstream src(s, std::ios::binary); - std::string dst_fn(s); - std::ofstream dst(dst_fn.append(".backup"), std::ios::binary); + std::ofstream dst(s + ".backup", std::ios::binary); dst << src.rdbuf(); @@ -146,6 +145,17 @@ main(int argc, char *argv[]) // run instrumentation on detected source files instrument(argc, argv, source_files); + // copy instrumented files ontop of original + for (auto s : source_files) { + std::ofstream dst(s, std::ios::binary); + std::ifstream src(s + ".inst", std::ios::binary); + + dst << src.rdbuf(); + + src.close(); + dst.close(); + } + #if DEBUG std::cout << "Calling real compiler" << std::endl; #endif @@ -175,8 +185,7 @@ main(int argc, char *argv[]) // restore original source files for (auto s : source_files) { std::ofstream dst(s, std::ios::binary); - std::string src_fn(s); - std::ifstream src(src_fn.append(".backup"), std::ios::binary); + std::ifstream src(s + ".backup", std::ios::binary); dst << src.rdbuf(); diff --git a/run_tests.sh b/run_tests.sh @@ -6,35 +6,32 @@ if which tput > /dev/null; then RESET=`tput sgr0` fi -export SCV_PATH="$HOME/src/scv/instrument" +export SCV_PATH="$HOME/src/scv/compilers" export PATH="$SCV_PATH:$PATH" -export CFLAGS="-pthread" -export LDFLAGS="-L$HOME/src/scv/runtime -pthread" - -export LD_LIBRARY_PATH="$HOME/src/scv/runtime" -export LDLIBS="-lruntime" -for t in `ls tests/*/prog.c`; do +export CFLAGS="-pthread -fPIC" +export LDLIBS="-L../../runtime -lruntime -pthread" +for t in `ls tests/fibonacci/Makefile`; do dirname=`dirname ${t}` - make -C $dirname clean + make -s -C $dirname clean - if ! make -C $dirname prog; then + if ! make -s -C $dirname; then echo "$dirname:$RED make prog failed!$RESET" continue fi # diff against the last known good instrumented source - if ! diff -u $dirname/instrumented.c $dirname/prog_inst.c; then + if ! make -s -C $dirname "diff"; then echo "$dirname:$RED source compare failed$RESET" continue fi # test that the instrumented binary works properly - if ! make -C $dirname "test"; then + if ! make -s -C $dirname "test"; then echo "$dirname:$RED test failed!$RESET" continue fi - make -C $dirname clean + make -s -C $dirname clean echo "$dirname:$GREEN ok$RESET" done diff --git a/tests/fibonacci/Makefile b/tests/fibonacci/Makefile @@ -6,10 +6,10 @@ $(BIN): $(OBJS) $(CC) -o $(BIN) $(OBJS) $(LDLIBS) test: - @sh test.sh + sh test.sh diff: - @diff -u fib.c.inst fib.c.inst_good + diff -u fib.c.inst fib.c.inst_good clean: - rm -f $(OBJS) $(BIN) *.backup + rm -f $(OBJS) $(BIN) *.{backup,inst} diff --git a/tests/fibonacci/fib.c.inst_good b/tests/fibonacci/fib.c.inst_good @@ -0,0 +1,38 @@ +unsigned int lines[512]; +int size = 512; +char file_name[] = "/home/kyle/src/scv/tests/fibonacci/fib.c"; +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <limits.h> + +long long +fibonacci(long long n) +{ + if ((lines[9] = 1, n == 0)) + return (lines[10] = 1, 0); + else if ((lines[11] = 1, n == 1)) + return (lines[12] = 1, 1); + + return (lines[14] = 1, (lines[14] = 1, fibonacci(n - 1)) + (lines[14] = 1, fibonacci(n - 2))); +} + +int +main(int argc, char *argv[]) +{ + long long n; + const char *errstr = NULL; + + if ((lines[23] = 1, argc != 2)) { + (lines[24] = 1, printf("usage: %s <N>\n", argv[0])); + return (lines[25] = 1, 1); + } + + n = (lines[28] = 1, strtonum(argv[1], LONG_MIN, LONG_MAX, &errstr)); + if ((lines[29] = 1, errstr)) + (lines[30] = 1, err(1, "%s", errstr)); + + (lines[32] = 1, printf("result: %lli\n", (lines[32] = 1, fibonacci(n)))); + + return (lines[34] = 1, 0); +} diff --git a/tests/fibonacci/fib_reference.c b/tests/fibonacci/fib_reference.c @@ -1,37 +0,0 @@ -unsigned int lines[512]; -int size = 512; -#include <err.h> -#include <stdio.h> -#include <stdlib.h> -#include <limits.h> - -long long -fibonacci(long long n) -{ - if ((lines[9] = 1, n == 0)) - return (lines[10] = 1, 0); - else if ((lines[11] = 1, n == 1)) - return (lines[12] = 1, 1); - - return (lines[14] = 1, (lines[14] = 1, fibonacci(n - 1)) + (lines[14] = 1, fibonacci(n - 2))); -} - -int -main(int argc, char *argv[]) -{ - long long n; - const char *errstr = NULL; - - if ((lines[23] = 1, argc != 2)) { - (lines[24] = 1, printf("usage: %s <N>\n", argv[0])); - return (lines[25] = 1, 1); - } - - n = (lines[28] = 1, strtonum(argv[1], LONG_MIN, LONG_MAX, &errstr)); - if ((lines[29] = 1, errstr)) - (lines[30] = 1, err(1, "%s", errstr)); - - (lines[32] = 1, printf("result: %lli\n", (lines[32] = 1, fibonacci(n)))); - - return (lines[34] = 1, 0); -}