citrun

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

commit a5f81e8e1e0cc0e7a7c56b90d36263801e0944a6
parent bdf608af723f6756e19cc29edd2338ba513db2e1
Author: Kyle Milz <kyle@0x30.net>
Date:   Mon, 22 Aug 2016 19:33:01 -0600

src: parse page aligned memory mapped files

Diffstat:
Msrc/Jamfile | 4++--
Asrc/process_dir.cc | 28++++++++++++++++++++++++++++
Asrc/process_dir.h | 12++++++++++++
Asrc/process_file.cc | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/process_file.h | 39+++++++++++++++++++++++++++++++++++++++
Dsrc/runtime_proc.cc | 86-------------------------------------------------------------------------------
Dsrc/runtime_proc.h | 38--------------------------------------
Msrc/shm.cc | 17+++++++----------
Msrc/shm.h | 14++++++++------
9 files changed, 206 insertions(+), 142 deletions(-)

diff --git a/src/Jamfile b/src/Jamfile @@ -1,12 +1,12 @@ SubDir TOP src ; # -# libraries +# libcitrun.a and utils.a # ObjectCcFlags runtime.c : -fPIC -ansi ; Library libcitrun : runtime.c ; -Library utils : shm.cc runtime_proc.cc ; +Library utils : shm.cc process_file.cc process_dir.cc ; LinkLibraries citrun-dump citrun-term citrun-gl : utils ; # diff --git a/src/process_dir.cc b/src/process_dir.cc @@ -0,0 +1,28 @@ +#include "process_dir.h" + +#include <sys/types.h> + +#include <err.h> +#include <cstring> +#include <iostream> +#include <dirent.h> // opendir, readdir + +ProcessDir::ProcessDir() +{ + DIR *dirp; + if ((dirp = opendir("/tmp/citrun")) == NULL) + err(1, "opendir"); + + struct dirent *dp; + while ((dp = readdir(dirp)) != NULL) { + + if (std::strcmp(dp->d_name, ".") == 0 || + std::strcmp(dp->d_name, "..") == 0) + continue; + + std::string p("/tmp/citrun/"); + p.append(dp->d_name); + + m_procfiles.push_back(ProcessFile(p)); + } +} diff --git a/src/process_dir.h b/src/process_dir.h @@ -0,0 +1,12 @@ +#include "process_file.h" + +#include <vector> + +class ProcessDir { +public: + ProcessDir(); + + std::vector<ProcessFile> m_procfiles; + +private: +}; diff --git a/src/process_file.cc b/src/process_file.cc @@ -0,0 +1,110 @@ +// +// 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 <cassert> +#include <csignal> // kill +#include <err.h> +#include <fstream> + +#include "process_file.h" +#include "version.h" // citrun_major + +ProcessFile::ProcessFile(std::string const &path) : + m_shm(path), + m_tus_with_execs(0), + m_program_loc(0) +{ + assert(sizeof(pid_t) == 4); + + m_shm.read_all(&m_major); + assert(m_major == citrun_major); + m_shm.read_all(&m_minor); + m_shm.read_all(&m_pid); + m_shm.read_all(&m_ppid); + m_shm.read_all(&m_pgrp); + m_shm.read_cstring(&m_progname); + m_shm.read_cstring(&m_cwd); + m_shm.next_page(); + + while (m_shm.at_end() == false) { + TranslationUnit t; + + uint8_t ready; + m_shm.read_all(&ready); + + m_shm.read_all(&t.num_lines); + + m_shm.read_cstring(&t.comp_file_path); + m_shm.read_cstring(&t.abs_file_path); + + t.exec_diffs = (uint64_t *)m_shm.get_block(t.num_lines * 8); + t.source.resize(t.num_lines); + m_program_loc += t.num_lines; + read_source(t); + + m_tus.push_back(t); + + m_shm.next_page(); + } +} + +bool +ProcessFile::is_alive() +{ + if (kill(m_pid, 0) == 0) + return 1; + return 0; +} + + +void +ProcessFile::read_source(struct TranslationUnit &t) +{ + std::ifstream file_stream(t.abs_file_path); + + if (file_stream.is_open() == 0) { + warnx("ifstream.open(%s)", t.abs_file_path); + return; + } + + for (auto &l : t.source) + std::getline(file_stream, l); +} + +uint64_t +ProcessFile::total_execs() +{ + uint64_t count = 0; + + for (auto &t : m_tus) + for (unsigned int i = 0; i < t.num_lines; ++i) + count += t.exec_diffs[i]; + + return count; +} + +const TranslationUnit * +ProcessFile::find_tu(std::string const &srcname) const +{ + for (auto &i : m_tus) + if (srcname == i.comp_file_path) + return &i; + return NULL; +} + +void +ProcessFile::read_executions() +{ +} diff --git a/src/process_file.h b/src/process_file.h @@ -0,0 +1,39 @@ +#include <string> +#include <vector> + +#include "shm.h" + +struct TranslationUnit { + const char *comp_file_path; + const char *abs_file_path; + uint32_t num_lines; + uint8_t has_execs; + uint64_t *exec_diffs; + std::vector<std::string> source; +}; + +class ProcessFile { +private: + void read_source(struct TranslationUnit &); + + Shm m_shm; +public: + ProcessFile(std::string const &); + + const TranslationUnit *find_tu(std::string const &) const; + uint64_t total_execs(); + bool is_alive(); + void read_executions(); + + uint8_t m_major; + uint8_t m_minor; + const char *m_progname; + const char *m_cwd; + uint32_t m_pid; + uint32_t m_ppid; + uint32_t m_pgrp; + std::vector<TranslationUnit> m_tus; + int m_tus_with_execs; + + uint32_t m_program_loc; +}; diff --git a/src/runtime_proc.cc b/src/runtime_proc.cc @@ -1,86 +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 <cassert> -#include <err.h> -#include <fstream> - -#include "version.h" // citrun_major -#include "runtime_proc.h" - -RuntimeProcess::RuntimeProcess(shm &s) : - m_shm(s), - m_tus_with_execs(0) -{ - assert(sizeof(pid_t) == 4); - - m_shm.read_all(&m_major); - assert(m_major == citrun_major); - m_shm.read_all(&m_minor); - m_shm.read_all(&m_pid); - m_shm.read_all(&m_ppid); - m_shm.read_all(&m_pgrp); - m_shm.read_cstring(&m_progname); - m_shm.read_cstring(&m_cwd); - - while (m_shm.at_end() == false) { - TranslationUnit t; - - while (m_shm.get_pos() == 0); - // Hack to increment the counter. - uint8_t ready; - m_shm.read_all(&ready); - - m_shm.read_all(&t.num_lines); - - m_shm.read_cstring(&t.comp_file_path); - m_shm.read_cstring(&t.abs_file_path); - - t.exec_diffs = (uint64_t *)m_shm.get_block(t.num_lines * 8); - t.source.resize(t.num_lines); - read_source(t); - - m_tus.push_back(t); - } -} - -void -RuntimeProcess::read_source(struct TranslationUnit &t) -{ - std::string line; - std::ifstream file_stream(t.abs_file_path); - - if (file_stream.is_open() == 0) { - warnx("ifstream.open(%s)", t.abs_file_path); - return; - } - - for (auto &l : t.source) - std::getline(file_stream, l); -} - -const TranslationUnit * -RuntimeProcess::find_tu(std::string const &srcname) const -{ - for (auto &i : m_tus) - if (srcname == i.comp_file_path) - return &i; - return NULL; -} - -void -RuntimeProcess::read_executions() -{ -} diff --git a/src/runtime_proc.h b/src/runtime_proc.h @@ -1,38 +0,0 @@ -#ifndef TEXT_H -#define TEXT_H -#include <string> -#include <vector> - -#include "shm.h" - -struct TranslationUnit { - const char *comp_file_path; - const char *abs_file_path; - uint32_t num_lines; - uint8_t has_execs; - uint64_t *exec_diffs; - std::vector<std::string> source; -}; - -class RuntimeProcess { -public: - RuntimeProcess(shm &); - const TranslationUnit *find_tu(std::string const &) const; - void read_executions(); - - uint8_t m_major; - uint8_t m_minor; - const char *m_progname; - const char *m_cwd; - uint32_t m_pid; - uint32_t m_ppid; - uint32_t m_pgrp; - std::vector<TranslationUnit> m_tus; - int m_tus_with_execs; -private: - void read_source(struct TranslationUnit &); - - shm m_shm; -}; - -#endif diff --git a/src/shm.cc b/src/shm.cc @@ -1,6 +1,6 @@ #include "shm.h" -#include <sys/mman.h> // shm_open, mmap +#include <sys/mman.h> // mmap #include <sys/stat.h> // S_IRUSR #include <cassert> @@ -9,16 +9,13 @@ #include <stdlib.h> // getenv #include <unistd.h> -shm::shm() : +Shm::Shm(std::string const &path) : + m_path(path), m_fd(0), m_mem(NULL), m_pos(0) { - const char *shm_path; - if ((shm_path = getenv("CITRUN_SHMPATH")) == NULL) - shm_path = "/tmp/citrun.shared"; - - if ((m_fd = shm_open(shm_path, O_RDONLY, S_IRUSR | S_IWUSR)) < 0) + if ((m_fd = open(m_path.c_str(), O_RDONLY, S_IRUSR | S_IWUSR)) < 0) err(1, "shm_open"); struct stat sb; @@ -35,7 +32,7 @@ shm::shm() : } void -shm::read_cstring(const char **c_str) +Shm::read_cstring(const char **c_str) { size_t sz = strlen((const char *)m_mem + m_pos) + 1; if (sz > 1025) @@ -46,7 +43,7 @@ shm::read_cstring(const char **c_str) } void * -shm::get_block(size_t inc) +Shm::get_block(size_t inc) { void *block = m_mem + m_pos; m_pos += inc; @@ -55,7 +52,7 @@ shm::get_block(size_t inc) } bool -shm::at_end() +Shm::at_end() { assert(m_pos <= m_size); return (m_pos == m_size ? true : false); diff --git a/src/shm.h b/src/shm.h @@ -3,10 +3,11 @@ #include <string.h> #include <string> +#include <unistd.h> // getpagesize -class shm { +class Shm { public: - shm(); + Shm(std::string const &); template<typename T> void read_all(T *buf) @@ -15,21 +16,22 @@ public: m_pos += sizeof(T); }; - uint8_t get_pos() + void next_page() { - return m_mem[m_pos]; + int page_size = getpagesize(); + m_pos += page_size - (m_pos % page_size); } void read_cstring(const char **); void *get_block(size_t); bool at_end(); - //void read_string(std::string &); private: + std::string m_path; int m_fd; uint8_t *m_mem; size_t m_pos; - off_t m_size; + size_t m_size; }; #endif // SHM_H