citrun

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

commit 95d06b205bf783d30500e7b15b3c7f534abc1f23
parent 7f1db2c6f09e916a2db5a851184ed0b7a1b59fad
Author: Kyle Milz <kyle@0x30.net>
Date:   Mon, 22 Aug 2016 22:22:50 -0600

t: get runtime tests in better order

Diffstat:
Msrc/dump_main.cc | 65+++++++++++++++++++++++++++--------------------------------------
Msrc/process_dir.cc | 11+++++++++--
Msrc/runtime.c | 28+++++++++++++++++-----------
Ct/rt_ver.sh -> t/rt_badver.sh | 0
Mt/rt_dynamic.sh | 2--
At/rt_file_names.sh | 16++++++++++++++++
At/rt_header.sh | 20++++++++++++++++++++
Mt/rt_source.sh | 2--
Dt/rt_static.sh | 32--------------------------------
Mt/rt_ver.sh | 30+++++++-----------------------
10 files changed, 96 insertions(+), 110 deletions(-)

diff --git a/src/dump_main.cc b/src/dump_main.cc @@ -1,10 +1,7 @@ // // Tool used by tests. // -#include "shm.h" -#include "runtime_proc.h" - -#include <sys/mman.h> // shm_unlink +#include "process_dir.h" #include <cstring> #include <err.h> @@ -14,32 +11,45 @@ static void usage() { - std::cerr << "usage: citrun-dump [-ft] [-s srcfile] [-u shm path]" << std::endl; + std::cerr << "usage: citrun-dump [-ft] [-s srcfile]" << std::endl; exit(1); } static void -count_execs(RuntimeProcess &rt) +print_summary(ProcessDir const &pdir) { - uint64_t total = 0; - - for (auto &t : rt.m_tus) - for (int i = 0; i < t.num_lines; ++i) - total += t.exec_diffs[i]; + for (auto &f : pdir.m_procfiles) { + std::cout << "Found " << (f.is_alive() ? "alive" : "dead") + << " program with PID '" << f.m_pid << "'\n"; + std::cout << " Runtime version: " + << unsigned(f.m_major) << "." + << unsigned(f.m_minor) << "\n"; + std::cout << " Translation units: " << f.m_tus.size() << "\n"; + std::cout << " Lines of code: " << f.m_program_loc << "\n"; + std::cout << " Working directory: '" << f.m_cwd << "'\n"; + } - std::cout << total << std::endl; + exit(0); } int main(int argc, char *argv[]) { + ProcessDir pdir; int ch; - int orig_argc = argc; int fflag = 0; char *sarg = NULL; int tflag = 0; - while ((ch = getopt(argc, argv, "fs:tu:")) != -1) { + if (argc == 1) + print_summary(pdir); + + if (pdir.m_procfiles.size() > 1) + errx(1, "more than 1 process file found in directory!"); + + ProcessFile f = pdir.m_procfiles[0]; + + while ((ch = getopt(argc, argv, "fs:t")) != -1) { switch (ch) { case 'f': fflag = 1; @@ -50,9 +60,6 @@ main(int argc, char *argv[]) case 's': sarg = optarg; break; - case 'u': - shm_unlink(optarg); - return 0; default: usage(); break; @@ -61,39 +68,21 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - shm shm_conn; - RuntimeProcess rt(shm_conn); - - if (orig_argc == 1) { - std::cout << "Version: " - << unsigned(rt.m_major) << "." - << unsigned(rt.m_minor) << "\n" - << "Program name: " << rt.m_progname << "\n" - << "Translation units: " << rt.m_tus.size() << "\n"; - } - if (fflag) { - for (auto &t : rt.m_tus) { + for (auto &t : f.m_tus) { std::cout << t.comp_file_path << " " << t.num_lines << std::endl; } } - if (0) { - std::cout << "Working directory:\t" << rt.m_cwd << "\n" - << "Process ID:\t" << rt.m_pid << "\n" - << "Parent process ID:\t" << rt.m_ppid << "\n" - << "Process group ID:\t" << rt.m_pgrp << "\n"; - } - if (tflag) { for (int i = 0; i < 60; i++) - count_execs(rt); + f.total_execs(); } if (sarg) { const TranslationUnit *t; - if ((t = rt.find_tu(sarg)) == NULL) + if ((t = f.find_tu(sarg)) == NULL) errx(1, "no source named '%s'\n", sarg); for (auto &l : t->source) diff --git a/src/process_dir.cc b/src/process_dir.cc @@ -3,14 +3,21 @@ #include <sys/types.h> #include <err.h> +#include <cstdlib> // getenv #include <cstring> #include <iostream> #include <dirent.h> // opendir, readdir ProcessDir::ProcessDir() { + const char *process_dir; + if (std::getenv("CITRUN_TESTING") != NULL) + process_dir = "runtime/"; + else + process_dir = "/tmp/citrun/"; + DIR *dirp; - if ((dirp = opendir("/tmp/citrun")) == NULL) + if ((dirp = opendir(process_dir)) == NULL) err(1, "opendir"); struct dirent *dp; @@ -20,7 +27,7 @@ ProcessDir::ProcessDir() std::strcmp(dp->d_name, "..") == 0) continue; - std::string p("/tmp/citrun/"); + std::string p(process_dir); p.append(dp->d_name); m_procfiles.push_back(ProcessFile(p)); diff --git a/src/runtime.c b/src/runtime.c @@ -66,13 +66,13 @@ add_new_region(int bytes) aligned_bytes = ((bytes) + page_mask) & ~page_mask; if (ftruncate(shm_fd, shm_len + aligned_bytes) < 0) - err(1, "ftruncate from %i to %i", shm_len, shm_len + aligned_bytes); + err(1, "ftruncate from %zu to %zu", shm_len, shm_len + aligned_bytes); shm = mmap(NULL, bytes, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, shm_len); if (shm == MAP_FAILED) - err(1, "mmap %i bytes @ %i", bytes, shm_len); + err(1, "mmap %i bytes @ %zu", bytes, shm_len); shm_len += aligned_bytes; return shm; @@ -124,21 +124,27 @@ write_header() static void create_memory_file() { + char memfile_path[23]; + char *template; + char *process_dir; + assert(shm_fd == 0); assert(shm_len == 0); -#if 0 - if ((shm_path = getenv("CITRUN_SHMPATH")) == NULL) - shm_path = SHM_PATH; -#endif + if (getenv("CITRUN_TESTING") != NULL) { + template = "runtime/XXXXXXXXXX"; + process_dir = "runtime"; + } else { + template = "/tmp/citrun/XXXXXXXXXX"; + process_dir = "/tmp/citrun"; + } /* Existing directory is OK. */ - if (mkdir("/tmp/citrun", S_IRWXU) && errno != EEXIST) - err(1, "mkdir"); + if (mkdir(process_dir, S_IRWXU) && errno != EEXIST) + err(1, "mkdir '%s'", process_dir); - char shm_path[23]; - strlcpy(shm_path, "/tmp/citrun/pid.XXXXXX", sizeof(shm_path)); - if ((shm_fd = mkstemp(shm_path)) == -1) + strlcpy(memfile_path, template, sizeof(memfile_path)); + if ((shm_fd = mkstemp(memfile_path)) == -1) err(1, "mkstemp"); init++; diff --git a/t/rt_ver.sh b/t/rt_badver.sh diff --git a/t/rt_dynamic.sh b/t/rt_dynamic.sh @@ -11,5 +11,3 @@ test_total_execs 2 kill -USR1 $pid wait - -unlink_shm diff --git a/t/rt_file_names.sh b/t/rt_file_names.sh @@ -0,0 +1,16 @@ +# +# Test that translation unit file names are what we expect. +# +echo 1..2 +. test/project.sh + +./program 1 + +$TEST_TOOLS/citrun-dump -f > filelist.out + +cat <<EOF > filelist.good +one.c 34 +three.c 9 +two.c 11 +EOF +filelist_diff 4 diff --git a/t/rt_header.sh b/t/rt_header.sh @@ -0,0 +1,20 @@ +# +# Test that the shared memory header is what we expect. +# +echo 1..2 +. test/project.sh + +./program 1 & +pid=$! +wait + +cat <<EOF | sed -e "s,%PID%,$pid," -e "s,%CWD%,`pwd`," > dump.good +Found dead program with PID '%PID%' + Runtime version: 0.0 + Translation units: 3 + Lines of code: 54 + Working directory: '%CWD%' +EOF + +$TEST_TOOLS/citrun-dump > dump.out +test_diff 2 "citrun-dump diff" dump.good dump.out diff --git a/t/rt_source.sh b/t/rt_source.sh @@ -23,5 +23,3 @@ echo >> three.c test_diff 2 "one.c diff runtime and disk" one.c one.c.runtime test_diff 3 "two.c diff runtime and disk" two.c two.c.runtime test_diff 4 "three.c diff runtime and disk" three.c three.c.runtime - -unlink_shm diff --git a/t/rt_static.sh b/t/rt_static.sh @@ -1,32 +0,0 @@ -# -# Test that the basic static structure of the shared memory region is what we -# expect. -# -echo 1..4 -. test/project.sh - -./program 45 & -pid=$! - -$TEST_TOOLS/citrun-dump | grep -e "Versi" -e "Progr" -e "Translat" > dump.out -$TEST_TOOLS/citrun-dump -f > filelist.out - -kill -USR1 $pid -wait -[ $? -eq 0 ] && echo ok 2 - program return code after SIGUSR1 - -cat <<EOF > dump.good -Version: 0.0 -Program name: program -Translation units: 3 -EOF -test_diff 3 "citrun-dump output" dump.good dump.out - -cat <<EOF > filelist.good -one.c 34 -three.c 9 -two.c 11 -EOF -filelist_diff 4 - -unlink_shm diff --git a/t/rt_ver.sh b/t/rt_ver.sh @@ -1,28 +1,12 @@ # -# Check that linking object files of one citrun version with libcitrun of -# another errors. +# Check that the runtime version of the shared memory file is what we expect. # -echo 1..3 -. test/utils.sh +echo 1..2 +. test/project.sh -cat <<EOF > main.c -#include <stddef.h> +./program 2 -int -main(int argc, char *argv[]) -{ - citrun_node_add(0, 255, NULL); -} -EOF +xxd -p -l 2 runtime/* > hex_version +echo "0000" > hex_version.good -/usr/bin/cc -c main.c -/usr/bin/cc -o main main.o $TEST_TOOLS/libcitrun.a - -main 2> out -[ $? -eq 1 ] && echo ok 2 - runtime errored program out - -cat <<EOF > good -main: libcitrun 0.0: incompatible node version 0.255 -EOF - -diff -u good out && echo ok 3 - error message +test_diff 2 "shared memory file version" hex_version.good hex_version