citrun

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

commit 6416018d99646d622531a46abb7d1a863108ec24
parent 66aabf51accfff8785776697a0ac9ebe9a4a94eb
Author: Kyle Milz <kyle@0x30.net>
Date:   Fri, 16 Dec 2016 19:05:41 -0700

src: rename rt to lib

Diffstat:
MJamrules | 2+-
Msrc/Jamfile | 6+++---
Msrc/inst_action.cc | 4++--
Msrc/inst_frontend.cc | 2+-
Asrc/lib.c | 146+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Rsrc/rt.h -> src/lib.h | 0
Msrc/process_file.cc | 2+-
Dsrc/rt.c | 146-------------------------------------------------------------------------------
Mt/rt_badver.sh | 2+-
9 files changed, 155 insertions(+), 155 deletions(-)

diff --git a/Jamrules b/Jamrules @@ -76,7 +76,7 @@ actions TestCoverage prove # prove tt - gcov -o src src/rt.c + gcov -o src src/lib.c egcov -r src/*.cc } diff --git a/src/Jamfile b/src/Jamfile @@ -3,8 +3,8 @@ SubDir TOP src ; # # libcitrun.a # -ObjectCcFlags rt.c : -fPIC -ansi ; -Library libcitrun : rt.c ; +ObjectCcFlags lib.c : -fPIC -ansi ; +Library libcitrun : lib.c ; # # citrun-wrap, citrun-check @@ -62,7 +62,7 @@ INST_SRCS = inst_action.cc inst_visitor.cc ; -Stringize rt_h.h : rt.h ; +Stringize lib_h.h : lib.h ; ObjectC++Flags $(INST_SRCS) : `llvm-config --cxxflags` ; ObjectDefines $(INST_SRCS) : CITRUN_SHARE=\\\"$(CITRUN_SHARE)\\\" ; diff --git a/src/inst_action.cc b/src/inst_action.cc @@ -14,7 +14,7 @@ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // #include "inst_action.h" -#include "rt_h.h" +#include "lib_h.h" #include <clang/Frontend/CompilerInstance.h> #include <err.h> @@ -77,7 +77,7 @@ InstrumentAction::EndSourceFileAction() preamble << "#ifdef __cplusplus\n" << "extern \"C\" {\n" << "#endif\n"; - preamble << rt_h; + preamble << lib_h; preamble << "static struct citrun_node _citrun = {\n" << " " << num_lines << ",\n" << " \"" << m_compiler_file_name << "\",\n" diff --git a/src/inst_frontend.cc b/src/inst_frontend.cc @@ -14,7 +14,7 @@ // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. // #include "inst_frontend.h" -#include "rt.h" // citrun_major, citrun_minor +#include "lib.h" // citrun_major, citrun_minor #include <sys/stat.h> // stat #include <sys/time.h> // utimes diff --git a/src/lib.c b/src/lib.c @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2016 Kyle Milz <kyle@0x30.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ +#include <sys/mman.h> /* mmap */ +#include <sys/stat.h> /* S_IRUSR, S_IWUSR, mkdir */ + +#include <assert.h> +#include <err.h> +#include <errno.h> /* EEXIST */ +#include <fcntl.h> /* O_CREAT */ +#include <limits.h> /* PATH_MAX */ +#include <stdlib.h> /* get{env,progname} */ +#include <string.h> /* str{l,n}cpy */ +#include <unistd.h> /* lseek get{cwd,pid,ppid,pgrp} */ + +#include "lib.h" /* citrun_*, struct citrun_{header,node} */ + + +static int shm_fd = 0; +static struct citrun_header *shm_header; + +/* + * Extends the file and memory mapping length of shm_fd by a requested amount of + * bytes (rounded up to the next page size). + * Returns a pointer to the extended region on success, exits on failure. + */ +static void * +shm_extend(size_t requested_bytes) +{ + size_t aligned_bytes, page_mask; + off_t shm_len; + void *shm; + + page_mask = getpagesize() - 1; + aligned_bytes = (requested_bytes + page_mask) & ~page_mask; + + /* Get current file length. */ + if ((shm_len = lseek(shm_fd, 0, SEEK_END)) < 0) + err(1, "lseek"); + + /* Increase file length. */ + if (ftruncate(shm_fd, shm_len + aligned_bytes) < 0) + err(1, "ftruncate from %lld to %llu", shm_len, shm_len + aligned_bytes); + + /* Increase memory mapping length. */ + shm = mmap(NULL, requested_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, + shm_fd, shm_len); + + if (shm == MAP_FAILED) + err(1, "mmap %zu bytes @ %llu", requested_bytes, shm_len); + + return shm; +} + +/* + * Creates a new shared memory file with a header. Exits on error. + */ +static void +shm_create() +{ + char *procdir; + char procfile[PATH_MAX]; + + /* User of this env var must give trailing slash */ + if ((procdir = getenv("CITRUN_PROCDIR")) == NULL) + procdir = "/tmp/citrun/"; + + if (mkdir(procdir, S_IRWXU) && errno != EEXIST) + err(1, "mkdir '%s'", procdir); + + strlcpy(procfile, procdir, PATH_MAX); + strlcat(procfile, getprogname(), PATH_MAX); + strlcat(procfile, "_XXXXXXXXXX", PATH_MAX); + + if ((shm_fd = mkstemp(procfile)) < 0) + err(1, "mkstemp"); + + /* Add header. */ + assert(sizeof(struct citrun_header) < getpagesize()); + shm_header = shm_extend(sizeof(struct citrun_header)); + + /* Purposefully not null terminated. */ + strncpy(shm_header->magic, "ctrn", sizeof(shm_header->magic)); + + shm_header->major = citrun_major; + shm_header->minor = citrun_minor; + shm_header->pids[0] = getpid(); + shm_header->pids[1] = getppid(); + shm_header->pids[2] = getpgrp(); + shm_header->units = 0; + shm_header->loc = 0; + + /* getprogname() should never fail. */ + strlcpy(shm_header->progname, getprogname(), sizeof(shm_header->progname)); + + if (getcwd(shm_header->cwd, sizeof(shm_header->cwd)) == NULL) + err(1, "getcwd"); +} + +/* + * Public interface: Called by all instrumented translation units. + * Copies n into the shared memory file and then points n->data to a region of + * memory located right after n that's at least 8 * n->size large. + * Exits on failure. + */ +void +citrun_node_add(unsigned int major, unsigned int minor, struct citrun_node *n) +{ + size_t sz; + struct citrun_node *shm_node; + + /* Binary compatibility between versions not guaranteed. */ + if (major != citrun_major || minor != citrun_minor) + errx(1, "libcitrun-%i.%i: incompatible version %i.%i, " + "try cleaning and rebuilding your project", + citrun_major, citrun_minor, major, minor); + + if (shm_fd == 0) + shm_create(); + + sz = sizeof(struct citrun_node); + sz += n->size * sizeof(unsigned long long); + + shm_header->units++; + shm_header->loc += n->size; + + shm_node = shm_extend(sz); + + shm_node->size = n->size; + strlcpy(shm_node->comp_file_path, n->comp_file_path, 1024); + strlcpy(shm_node->abs_file_path, n->abs_file_path, 1024); + + n->data = (unsigned long long *)(shm_node + 1); +} diff --git a/src/rt.h b/src/lib.h diff --git a/src/process_file.cc b/src/process_file.cc @@ -27,7 +27,7 @@ #include <unistd.h> // getpagesize #include "process_file.h" -#include "rt.h" // citrun_major, struct citrun_{node,header} +#include "lib.h" // citrun_major, struct citrun_{node,header} // diff --git a/src/rt.c b/src/rt.c @@ -1,146 +0,0 @@ -/* - * Copyright (c) 2016 Kyle Milz <kyle@0x30.net> - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ -#include <sys/mman.h> /* mmap */ -#include <sys/stat.h> /* S_IRUSR, S_IWUSR, mkdir */ - -#include <assert.h> -#include <err.h> -#include <errno.h> /* EEXIST */ -#include <fcntl.h> /* O_CREAT */ -#include <limits.h> /* PATH_MAX */ -#include <stdlib.h> /* get{env,progname} */ -#include <string.h> /* str{l,n}cpy */ -#include <unistd.h> /* lseek get{cwd,pid,ppid,pgrp} */ - -#include "rt.h" /* citrun_*, struct citrun_{header,node} */ - - -static int shm_fd = 0; -static struct citrun_header *shm_header; - -/* - * Extends the file and memory mapping length of shm_fd by a requested amount of - * bytes (rounded up to the next page size). - * Returns a pointer to the extended region on success, exits on failure. - */ -static void * -shm_extend(size_t requested_bytes) -{ - size_t aligned_bytes, page_mask; - off_t shm_len; - void *shm; - - page_mask = getpagesize() - 1; - aligned_bytes = (requested_bytes + page_mask) & ~page_mask; - - /* Get current file length. */ - if ((shm_len = lseek(shm_fd, 0, SEEK_END)) < 0) - err(1, "lseek"); - - /* Increase file length. */ - if (ftruncate(shm_fd, shm_len + aligned_bytes) < 0) - err(1, "ftruncate from %lld to %llu", shm_len, shm_len + aligned_bytes); - - /* Increase memory mapping length. */ - shm = mmap(NULL, requested_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, - shm_fd, shm_len); - - if (shm == MAP_FAILED) - err(1, "mmap %zu bytes @ %llu", requested_bytes, shm_len); - - return shm; -} - -/* - * Creates a new shared memory file with a header. Exits on error. - */ -static void -shm_create() -{ - char *procdir; - char procfile[PATH_MAX]; - - /* User of this env var must give trailing slash */ - if ((procdir = getenv("CITRUN_PROCDIR")) == NULL) - procdir = "/tmp/citrun/"; - - if (mkdir(procdir, S_IRWXU) && errno != EEXIST) - err(1, "mkdir '%s'", procdir); - - strlcpy(procfile, procdir, PATH_MAX); - strlcat(procfile, getprogname(), PATH_MAX); - strlcat(procfile, "_XXXXXXXXXX", PATH_MAX); - - if ((shm_fd = mkstemp(procfile)) < 0) - err(1, "mkstemp"); - - /* Add header. */ - assert(sizeof(struct citrun_header) < getpagesize()); - shm_header = shm_extend(sizeof(struct citrun_header)); - - /* Purposefully not null terminated. */ - strncpy(shm_header->magic, "ctrn", sizeof(shm_header->magic)); - - shm_header->major = citrun_major; - shm_header->minor = citrun_minor; - shm_header->pids[0] = getpid(); - shm_header->pids[1] = getppid(); - shm_header->pids[2] = getpgrp(); - shm_header->units = 0; - shm_header->loc = 0; - - /* getprogname() should never fail. */ - strlcpy(shm_header->progname, getprogname(), sizeof(shm_header->progname)); - - if (getcwd(shm_header->cwd, sizeof(shm_header->cwd)) == NULL) - err(1, "getcwd"); -} - -/* - * Public interface: Called by all instrumented translation units. - * Copies n into the shared memory file and then points n->data to a region of - * memory located right after n that's at least 8 * n->size large. - * Exits on failure. - */ -void -citrun_node_add(unsigned int major, unsigned int minor, struct citrun_node *n) -{ - size_t sz; - struct citrun_node *shm_node; - - /* Binary compatibility between versions not guaranteed. */ - if (major != citrun_major || minor != citrun_minor) - errx(1, "libcitrun-%i.%i: incompatible version %i.%i, " - "try cleaning and rebuilding your project", - citrun_major, citrun_minor, major, minor); - - if (shm_fd == 0) - shm_create(); - - sz = sizeof(struct citrun_node); - sz += n->size * sizeof(unsigned long long); - - shm_header->units++; - shm_header->loc += n->size; - - shm_node = shm_extend(sz); - - shm_node->size = n->size; - strlcpy(shm_node->comp_file_path, n->comp_file_path, 1024); - strlcpy(shm_node->abs_file_path, n->abs_file_path, 1024); - - n->data = (unsigned long long *)(shm_node + 1); -} diff --git a/t/rt_badver.sh b/t/rt_badver.sh @@ -21,7 +21,7 @@ main(int argc, char *argv[]) } EOF -ok "compile fake node" cc -include $src_dir/rt.h -c main.c +ok "compile fake node" cc -include $src_dir/lib.h -c main.c ok "link fake node to libcitrun.a" cc -o main main.o $src_dir/libcitrun.a output_good="main: libcitrun-0.0: incompatible version 0.255, try cleaning and rebuilding your project"