citrun

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

commit 40de032983a728090c7612a3cf68b5b04a999420
parent 09c6495a694f136ad9d51062aa664b30b921b2d7
Author: Kyle Milz <kyle@0x30.net>
Date:   Fri, 29 Jul 2016 19:02:35 -0600

lib: put protocol on a diet

Diffstat:
MTest/Viewer.pm | 29+++++++++++++----------------
Mlib/runtime.c | 37+++++++++++++++++++++++++------------
2 files changed, 38 insertions(+), 28 deletions(-)

diff --git a/Test/Viewer.pm b/Test/Viewer.pm @@ -32,22 +32,19 @@ sub accept { my $client = $socket->accept(); $self->{client_socket} = $client; - # Read arbitrarily sized program name string - my $buf = read_all($client, 8); - my $progname_sz = unpack("Q", $buf); + # Protocol defined in lib/runtime.c function send_static(). + # + my $buf = read_all($client, 1 + 4 + 4 + 12); + ($self->{ver}, $self->{num_tus}, $self->{lines_total}, $self->{pid}, $self->{ppid}, $self->{pgrp}) + = unpack("CL5", $buf); + + my $buf = read_all($client, 2); + my $progname_sz = unpack("S", $buf); my $progname = read_all($client, $progname_sz); - # Read the total number of instrumented translation units. - my $buf = read_all($client, 8); - $self->{num_tus} = unpack("Q", $buf); - - # Read total code lines in program. - $buf = read_all($client, 8); - $self->{lines_total} = unpack("Q", $buf); - - # Read three 4 byte pid_t's - $buf = read_all($client, 12); - ($self->{pid}, $self->{ppid}, $self->{pgrp}) = unpack("L3", $buf); + my $buf = read_all($client, 2); + my $cwd_sz = unpack("S", $buf); + my $cwd = read_all($client, $cwd_sz); # Always sanity check these. cmp_ok( $self->{pid}, ">", 1, "pid lower bound check" ); @@ -61,8 +58,8 @@ sub accept { my @tus; for (1..$self->{num_tus}) { # Size of absolute file path. - $buf = read_all($client, 8); - my $file_name_sz = unpack("Q", $buf); + $buf = read_all($client, 2); + my $file_name_sz = unpack("S", $buf); # Absolute file path. my $file_name = read_all($client, $file_name_sz); diff --git a/lib/runtime.c b/lib/runtime.c @@ -25,14 +25,14 @@ #include <limits.h> /* PATH_MAX */ #include <pthread.h> /* pthread_create */ #include <stdlib.h> /* getenv */ -#include <string.h> /* strlcpy */ -#include <unistd.h> /* access, get{pid,ppid,pgrp}, read, write */ +#include <string.h> /* strlcpy, strnlen */ +#include <unistd.h> /* access, get{cwd,pid,ppid,pgrp}, read, write */ #include "runtime.h" static struct citrun_node *nodes_head; -static uint64_t nodes_total; -static uint64_t lines_total; +static uint32_t nodes_total; +static uint32_t lines_total; static void *relay_thread(void *); @@ -132,11 +132,14 @@ xwrite(int d, const void *buf, size_t bytes_total) /* * Send static information contained in each instrumented node. * Sent program wide values: - * - length of program name - * - program name + * - version * - total number of translation units * - total number of lines in program * - process id, parent process id, group process id + * - length of program name + * - program name + * - length of current working directory + * - current working directory * Sent for each instrumented translation unit: * - length of source file name * - source file name @@ -150,24 +153,34 @@ send_static(int fd) pid_t pids[3]; struct citrun_node *w; const char *progname; - size_t sz; + char *cwd_buf; + uint16_t sz; + uint8_t ver = 0; int i; - progname = getprogname(); - sz = strlen(progname); + assert(sizeof(pid_t) == 4); - xwrite(fd, &sz, sizeof(sz)); - xwrite(fd, progname, sz); + xwrite(fd, &ver, sizeof(ver)); xwrite(fd, &nodes_total, sizeof(nodes_total)); xwrite(fd, &lines_total, sizeof(lines_total)); pids[0] = getpid(); pids[1] = getppid(); pids[2] = getpgrp(); - assert(sizeof(pid_t) == 4); for (i = 0; i < (sizeof(pids) / sizeof(pids[0])); i++) xwrite(fd, &pids[i], sizeof(pid_t)); + progname = getprogname(); + sz = strnlen(progname, PATH_MAX); + xwrite(fd, &sz, sizeof(sz)); + xwrite(fd, progname, sz); + + if ((cwd_buf = getcwd(NULL, 0)) == NULL) + err(1, "getcwd"); + sz = strnlen(cwd_buf, PATH_MAX); + xwrite(fd, &sz, sizeof(sz)); + xwrite(fd, cwd_buf, sz); + for (w = nodes_head, i = 0; w != NULL; w = w->next, i++) { node = *w; sz = strnlen(node.file_name, PATH_MAX);