citrun

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

commit f7b33434bca4ec7ecd93667b23a0ea7575a032ba
parent 85829938dd914e0047d9dcaf9772920c13f315f5
Author: Kyle Milz <kyle@0x30.net>
Date:   Sun, 25 Sep 2016 13:56:23 -0600

src: add file magic 'citrun' to offset 0 of memory files

Diffstat:
Msrc/process_file.cc | 3+++
Msrc/rt.c | 19++++++++++++++++---
Msrc/shm.cc | 9+++++++++
Msrc/shm.h | 9+++++----
Mt/rt_header.t | 3++-
Mtlib/shm.pm | 5+++--
6 files changed, 38 insertions(+), 10 deletions(-)

diff --git a/src/process_file.cc b/src/process_file.cc @@ -26,8 +26,11 @@ ProcessFile::ProcessFile(std::string const &path) : m_tus_with_execs(0), m_program_loc(0) { + std::string magic; assert(sizeof(pid_t) == 4); + m_shm.read_magic(magic); + assert(magic == "citrun"); m_shm.read_all(&m_major); assert(m_major == citrun_major); m_shm.read_all(&m_minor); diff --git a/src/rt.c b/src/rt.c @@ -42,6 +42,13 @@ add_1(uint8_t *shm, size_t shm_pos, uint8_t data) } static size_t +add_2(uint8_t *shm, size_t shm_pos, uint16_t data) +{ + memcpy(shm + shm_pos, &data, sizeof(data)); + return shm_pos + sizeof(data); +} + +static size_t add_4(uint8_t *shm, size_t shm_pos, uint32_t data) { memcpy(shm + shm_pos, &data, sizeof(data)); @@ -51,9 +58,6 @@ add_4(uint8_t *shm, size_t shm_pos, uint32_t data) static size_t add_str(uint8_t *shm, size_t shm_pos, const char *str, uint16_t len) { - memcpy(shm + shm_pos, &len, sizeof(len)); - shm_pos += sizeof(len); - memcpy(shm + shm_pos, str, len); return shm_pos + len; } @@ -102,6 +106,7 @@ shm_extend(int bytes) static void shm_add_header() { + char magic[6] = "citrun"; char *cwd_buf; const char *progname; uint8_t *shm; @@ -117,6 +122,7 @@ shm_add_header() cwd_sz = strnlen(cwd_buf, PATH_MAX); sz = 0; + sz += sizeof(magic); sz += sizeof(uint8_t) * 2; sz += sizeof(uint32_t) * 3; sz += sizeof(prog_sz); @@ -127,6 +133,8 @@ shm_add_header() shm = shm_extend(sz); shm_pos = 0; + shm_pos = add_str(shm, shm_pos, magic, sizeof(magic)); + shm_pos = add_1(shm, shm_pos, citrun_major); shm_pos = add_1(shm, shm_pos, citrun_minor); @@ -134,7 +142,10 @@ shm_add_header() shm_pos = add_4(shm, shm_pos, getppid()); shm_pos = add_4(shm, shm_pos, getpgrp()); + shm_pos = add_2(shm, shm_pos, prog_sz); shm_pos = add_str(shm, shm_pos, progname, prog_sz); + + shm_pos = add_2(shm, shm_pos, cwd_sz); shm_pos = add_str(shm, shm_pos, cwd_buf, cwd_sz); assert(shm_pos == sz); @@ -168,7 +179,9 @@ shm_add_node(struct citrun_node *n) shm_pos = 0; shm_pos = add_4(shm, shm_pos, n->size); + shm_pos = add_2(shm, shm_pos, comp_sz); shm_pos = add_str(shm, shm_pos, n->comp_file_path, comp_sz); + shm_pos = add_2(shm, shm_pos, abs_sz); shm_pos = add_str(shm, shm_pos, n->abs_file_path, abs_sz); n->data = (uint64_t *)&shm[shm_pos]; diff --git a/src/shm.cc b/src/shm.cc @@ -39,6 +39,15 @@ Shm::next_page() } 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; diff --git a/src/shm.h b/src/shm.h @@ -16,10 +16,11 @@ public: m_pos += sizeof(T); }; - void next_page(); - void read_string(std::string &); - void *get_block(size_t); - bool at_end(); + 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; diff --git a/t/rt_header.t b/t/rt_header.t @@ -3,7 +3,7 @@ # use strict; use warnings; -use Test::More tests => 14; +use Test::More tests => 15; use tlib::program; use tlib::shm; @@ -11,6 +11,7 @@ my $ret = system('tlib/program/program 1'); is $ret >> 8, 0, "is program exit code 0"; my $shm = tlib::shm->new(); +is $shm->{magic}, "citrun", "is file magic correct"; is $shm->{major}, 0, "is major correct"; is $shm->{minor}, 0, "is minor correct"; diff --git a/tlib/shm.pm b/tlib/shm.pm @@ -16,6 +16,7 @@ sub new { $self->{fh} = $fh; $self->{size} = (stat "procfile.shm")[7]; + ($self->{magic}) = xread($fh, 6); ($self->{major}, $self->{minor}) = unpack("C2", xread($fh, 2)); @{ $self->{pids} } = unpack("L3", xread($fh, 12)); @@ -85,8 +86,8 @@ sub xread { while ($bytes_total > 0) { my $read = read($fh, $data, $bytes_total, $bytes_read); - die "error: read failed: $!" if (!defined $read); - die "disconnected!\n" if ($read == 0); + die "read failed: $!" if (!defined $read); + die "end of file\n" if ($read == 0); $bytes_total -= $read; $bytes_read += $read;