citrun

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

commit 63f4d92a099a2a31d569163fa125aad7a8b877ce
parent 2fae7c381b2a204aef2dfc73bdd9314af051e91e
Author: Kyle Milz <kyle@0x30.net>
Date:   Sun, 25 Sep 2016 18:30:58 -0600

src: merge shm.{cc,h} into process_file.{cc,h}

Diffstat:
Msrc/Jamfile | 2+-
Msrc/gl_main.cc | 1-
Msrc/process_file.cc | 121++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/process_file.h | 28+++++++++++++++++++++++-----
Dsrc/shm.cc | 77-----------------------------------------------------------------------------
Dsrc/shm.h | 33---------------------------------
6 files changed, 119 insertions(+), 143 deletions(-)

diff --git a/src/Jamfile b/src/Jamfile @@ -6,7 +6,7 @@ SubDir TOP src ; ObjectCcFlags rt.c : -fPIC -ansi ; Library libcitrun : rt.c ; -Library utils : shm.cc process_file.cc process_dir.cc ; +Library utils : process_file.cc process_dir.cc ; LinkLibraries citrun-term citrun-gl : utils ; # diff --git a/src/gl_main.cc b/src/gl_main.cc @@ -9,7 +9,6 @@ #include "gl_view.h" #include "process_dir.h" #include "process_file.h" -#include "shm.h" #if defined(__OpenBSD__) #define FONT_PATH "/usr/X11R6/lib/X11/fonts/TTF/DejaVuSansMono.ttf" diff --git a/src/process_file.cc b/src/process_file.cc @@ -13,43 +13,68 @@ // 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 + #include <cassert> #include <csignal> // kill #include <err.h> +#include <fcntl.h> // O_RDONLY #include <fstream> +#include <stdlib.h> // getenv +#include <unistd.h> // getpagesize #include "process_file.h" #include "version.h" // citrun_major + ProcessFile::ProcessFile(std::string const &path) : - m_shm(path), + m_path(path), + m_fd(0), + m_mem(NULL), + m_pos(0), m_tus_with_execs(0), m_program_loc(0) { + if ((m_fd = open(m_path.c_str(), O_RDONLY, S_IRUSR | S_IWUSR)) < 0) + err(1, "open"); + + struct stat sb; + fstat(m_fd, &sb); + + if (sb.st_size > 1024 * 1024 * 1024) + errx(1, "shared memory too large: %lli", sb.st_size); + + m_mem = (uint8_t *)mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, m_fd, 0); + if (m_mem == MAP_FAILED) + err(1, "mmap"); + + m_size = sb.st_size; + std::string magic; assert(sizeof(pid_t) == 4); - m_shm.read_magic(magic); + shm_read_magic(magic); assert(magic == "citrun"); - m_shm.read_all(&m_major); + 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_string(m_progname); - m_shm.read_string(m_cwd); - m_shm.next_page(); - - while (m_shm.at_end() == false) { + shm_read_all(&m_minor); + shm_read_all(&m_pid); + shm_read_all(&m_ppid); + shm_read_all(&m_pgrp); + shm_read_string(m_progname); + shm_read_string(m_cwd); + shm_next_page(); + + while (shm_at_end() == false) { TranslationUnit t; - m_shm.read_all(&t.num_lines); + shm_read_all(&t.num_lines); - m_shm.read_string(t.comp_file_path); - m_shm.read_string(t.abs_file_path); + shm_read_string(t.comp_file_path); + shm_read_string(t.abs_file_path); - t.exec_counts = (uint64_t *)m_shm.get_block(t.num_lines * 8); + t.exec_counts = (uint64_t *)shm_get_block(t.num_lines * 8); t.exec_counts_last = new uint64_t[t.num_lines](); t.source.resize(t.num_lines); @@ -58,19 +83,10 @@ ProcessFile::ProcessFile(std::string const &path) : m_tus.push_back(t); - m_shm.next_page(); + shm_next_page(); } } -bool -ProcessFile::is_alive() const -{ - if (kill(m_pid, 0) == 0) - return 1; - return 0; -} - - void ProcessFile::read_source(struct TranslationUnit &t) { @@ -85,6 +101,59 @@ ProcessFile::read_source(struct TranslationUnit &t) std::getline(file_stream, l); } +void +ProcessFile::shm_next_page() +{ + int page_size = getpagesize(); + m_pos += page_size - (m_pos % page_size); +} + +void +ProcessFile::shm_read_magic(std::string &magic) +{ + magic.resize(6); + + memcpy(&magic[0], m_mem + m_pos, 6); + m_pos += 6; +} + +void +ProcessFile::shm_read_string(std::string &str) +{ + uint16_t len; + + memcpy(&len, m_mem + m_pos, sizeof(len)); + m_pos += sizeof(len); + + str.resize(len); + memcpy(&str[0], m_mem + m_pos, len); + m_pos += len; +} + +void * +ProcessFile::shm_get_block(size_t inc) +{ + void *block = m_mem + m_pos; + m_pos += inc; + + return block; +} + +bool +ProcessFile::shm_at_end() +{ + assert(m_pos <= m_size); + return (m_pos == m_size ? true : false); +} + +bool +ProcessFile::is_alive() const +{ + if (kill(m_pid, 0) == 0) + return 1; + return 0; +} + const TranslationUnit * ProcessFile::find_tu(std::string const &srcname) const { diff --git a/src/process_file.h b/src/process_file.h @@ -1,23 +1,41 @@ #include <string> +#include <string.h> // memcpy #include <vector> -#include "shm.h" struct TranslationUnit { + std::vector<std::string> source; std::string comp_file_path; std::string abs_file_path; - uint32_t num_lines; - uint8_t has_execs; uint64_t *exec_counts; uint64_t *exec_counts_last; - std::vector<std::string> source; + uint32_t num_lines; + uint8_t has_execs; }; class ProcessFile { private: void read_source(struct TranslationUnit &); - Shm m_shm; + template<typename T> + void shm_read_all(T *buf) + { + memcpy(buf, m_mem + m_pos, sizeof(T)); + m_pos += sizeof(T); + }; + + void shm_next_page(); + void shm_read_string(std::string &); + void shm_read_magic(std::string &); + void *shm_get_block(size_t); + bool shm_at_end(); + + std::string m_path; + int m_fd; + uint8_t *m_mem; + size_t m_pos; + size_t m_size; + public: ProcessFile(std::string const &); diff --git a/src/shm.cc b/src/shm.cc @@ -1,77 +0,0 @@ -#include "shm.h" - -#include <sys/mman.h> // mmap -#include <sys/stat.h> // S_IRUSR - -#include <cassert> -#include <err.h> -#include <fcntl.h> // O_RDONLY -#include <stdlib.h> // getenv -#include <unistd.h> - -Shm::Shm(std::string const &path) : - m_path(path), - m_fd(0), - m_mem(NULL), - m_pos(0) -{ - if ((m_fd = open(m_path.c_str(), O_RDONLY, S_IRUSR | S_IWUSR)) < 0) - err(1, "open"); - - struct stat sb; - fstat(m_fd, &sb); - - if (sb.st_size > 1024 * 1024 * 1024) - errx(1, "shared memory too large: %lli", sb.st_size); - - m_mem = (uint8_t *)mmap(NULL, sb.st_size, PROT_READ, MAP_SHARED, m_fd, 0); - if (m_mem == MAP_FAILED) - err(1, "mmap"); - - m_size = sb.st_size; -} - -void -Shm::next_page() -{ - int page_size = getpagesize(); - m_pos += page_size - (m_pos % page_size); -} - -void -Shm::read_magic(std::string &magic) -{ - magic.resize(6); - - memcpy(&magic[0], m_mem + m_pos, 6); - m_pos += 6; -} - -void -Shm::read_string(std::string &str) -{ - uint16_t len; - - memcpy(&len, m_mem + m_pos, sizeof(len)); - m_pos += sizeof(len); - - str.resize(len); - memcpy(&str[0], m_mem + m_pos, len); - m_pos += len; -} - -void * -Shm::get_block(size_t inc) -{ - void *block = m_mem + m_pos; - m_pos += inc; - - return block; -} - -bool -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 @@ -1,33 +0,0 @@ -#ifndef SHM_H -#define SHM_H - -#include <string.h> -#include <string> -#include <unistd.h> // getpagesize - -class Shm { -public: - Shm(std::string const &); - - template<typename T> - void read_all(T *buf) - { - memcpy(buf, m_mem + m_pos, sizeof(T)); - m_pos += sizeof(T); - }; - - void next_page(); - void read_string(std::string &); - void read_magic(std::string &); - void *get_block(size_t); - bool at_end(); - -private: - std::string m_path; - int m_fd; - uint8_t *m_mem; - size_t m_pos; - size_t m_size; -}; - -#endif // SHM_H