citrun

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

commit 214bd113298b0a1c674e76951cffb1c20aa7ec79
parent 68f507a51a5d7c417446b4f52ab8999dfbd20ac0
Author: kyle <kyle@getaddrinfo.net>
Date:   Wed,  9 Mar 2016 22:52:28 -0700

two small fixes on the way to better runtime

- monotonically increase line counters instead of setting them on
- use line counts in buffer sizes instead of file size bytes

Diffstat:
Minstrument/instrumenter.cpp | 13+++++++------
Mruntime/runtime.c | 43+++++++++++++++++++++++++++----------------
Mt/fibonacci.t | 31++++++++++++++++---------------
Mt/for.t | 8++++----
Mt/hello_world.t | 8++++----
Mt/if.t | 20++++++++++----------
Mt/return.t | 12++++++------
Mt/switch.t | 8++++----
Mt/while.t | 8++++----
9 files changed, 82 insertions(+), 69 deletions(-)

diff --git a/instrument/instrumenter.cpp b/instrument/instrumenter.cpp @@ -63,7 +63,7 @@ instrumenter::VisitStmt(Stmt *s) if (stmt_to_inst == NULL) return true; - ss << "(lines[" << line << "] = 1, "; + ss << "(++lines[" << line << "], "; if (TheRewriter.InsertTextBefore(stmt_to_inst->getLocStart(), ss.str())) // writing failed, don't attempt to add ")" return true; @@ -169,17 +169,18 @@ MyFrontendAction::EndSourceFileAction() // << "\n"; SourceLocation start = sm.getLocForStartOfFile(main_fid); - std::string file_name = getCurrentFile(); + SourceLocation end = sm.getLocForEndOfFile(main_fid); + unsigned int num_lines = sm.getPresumedLineNumber(end); + + std::string file_name = getCurrentFile(); unsigned int tu_number = get_src_number(); std::stringstream ss; - // Add declarations for coverage buffers - int file_bytes = sm.getFileIDSize(main_fid); ss << "#include <scv_global.h>" << std::endl; // Define storage for coverage data - ss << "static unsigned int lines[" << file_bytes << "];" << std::endl; + ss << "static unsigned int lines[" << num_lines << "];" << std::endl; // Always declare this. The next TU will overwrite this or there won't // be a next TU. @@ -188,7 +189,7 @@ MyFrontendAction::EndSourceFileAction() // Define this translation units main book keeping data structure ss << "struct scv_node node" << tu_number << " = {" << std::endl << " .lines_ptr = lines," << std::endl - << " .size = " << file_bytes << "," << std::endl + << " .size = " << num_lines << "," << std::endl << " .file_name = \"" << file_name << "\"," << std::endl; ss << " .next = &node" << tu_number + 1 << "," << std::endl; ss << "};" << std::endl; diff --git a/runtime/runtime.c b/runtime/runtime.c @@ -10,24 +10,25 @@ /* Entry point into an instrumented application */ extern struct scv_node node0; -void * -walk_nodes(void *arg) +void +walk_nodes() { int i; - printf("%s: alive", __func__); + fprintf(stderr, "%s: alive", __func__); - struct scv_node *temp = &node0; - while (temp) { - printf("filename = %s\n", temp->file_name); - printf("size = %u\n", temp->size); - for (i = 0; i < temp->size; i++) { - printf(" ln %i = %u\n", i, temp->lines_ptr[i]); + /* Copy node0, don't use it directly */ + struct scv_node temp = node0; + while (temp.size != 0) { + printf("filename = %s\n", temp.file_name); + printf("size = %u\n", temp.size); + for (i = 0; i < temp.size; i++) { + fprintf(stderr, " ln %i = %u\n", i, temp.lines_ptr[i]); } - temp = temp->next; + temp = *temp.next; } - printf("%s: done", __func__); + fprintf(stderr, "%s: done", __func__); } void * @@ -43,15 +44,25 @@ control_thread(void *arg) struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, "socket", sizeof(addr.sun_path) - 1); + strncpy(addr.sun_path, "/tmp/viewer_test.socket", sizeof(addr.sun_path) - 1); - return; // fprintf(stderr, "%s: initialized\n", __func__); - while (1) { - if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) - err(1, "connect"); + if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) { + // warn("connect"); + return 0; } + + uint8_t version; + uint8_t msg_type; + read(fd, version, 1); + read(fd, msg_type, 1); + + if (version != 0) + err(1, "version != 0"); + + if (msg_type == 0) + walk_nodes(); } __attribute__((constructor)) diff --git a/t/fibonacci.t b/t/fibonacci.t @@ -1,5 +1,6 @@ use strict; use SCV::Project; +use SCV::Viewer; use Test::More tests => 7; use Test::Differences; @@ -52,11 +53,11 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[530]; +static unsigned int lines[36]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 530, + .size = 36, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; @@ -68,12 +69,12 @@ struct scv_node node0 = { long long fibonacci(long long n) { - if ((lines[9] = 1, n == 0)) - return (lines[10] = 1, 0); - else if ((lines[11] = 1, n == 1)) - return (lines[12] = 1, 1); + if ((++lines[9], n == 0)) + return (++lines[10], 0); + else if ((++lines[11], n == 1)) + return (++lines[12], 1); - return (lines[14] = 1, (lines[14] = 1, fibonacci(n - 1)) + (lines[14] = 1, fibonacci(n - 2))); + return (++lines[14], (++lines[14], fibonacci(n - 1)) + (++lines[14], fibonacci(n - 2))); } int @@ -82,18 +83,18 @@ main(int argc, char *argv[]) long long n; const char *errstr = NULL; - if ((lines[23] = 1, argc != 2)) { - (lines[24] = 1, fprintf(stderr, "usage: %s <N>\\n", argv[0])); - return (lines[25] = 1, 1); + if ((++lines[23], argc != 2)) { + (++lines[24], fprintf(stderr, "usage: %s <N>\\n", argv[0])); + return (++lines[25], 1); } - n = (lines[28] = 1, strtonum(argv[1], LONG_MIN, LONG_MAX, &errstr)); - if ((lines[29] = 1, errstr)) - (lines[30] = 1, err(1, "%s", errstr)); + n = (++lines[28], strtonum(argv[1], LONG_MIN, LONG_MAX, &errstr)); + if ((++lines[29], errstr)) + (++lines[30], err(1, "%s", errstr)); - (lines[32] = 1, fprintf(stderr, "result: %lli\\n", (lines[32] = 1, fibonacci(n)))); + (++lines[32], fprintf(stderr, "result: %lli\\n", (++lines[32], fibonacci(n)))); - return (lines[34] = 1, 0); + return (++lines[34], 0); } EOF diff --git a/t/for.t b/t/for.t @@ -28,11 +28,11 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[78]; +static unsigned int lines[12]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 78, + .size = 12, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; @@ -41,11 +41,11 @@ main(void) { int i; - for (i = 0; (lines[6] = 1, i < 19); i++) { + for (i = 0; (++lines[6], i < 19); i++) { i++; } - return (lines[10] = 1, i); + return (++lines[10], i); } EOF diff --git a/t/hello_world.t b/t/hello_world.t @@ -25,11 +25,11 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[85]; +static unsigned int lines[9]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 85, + .size = 9, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; @@ -38,8 +38,8 @@ struct scv_node node0 = { int main(void) { - (lines[6] = 1, fprintf(stderr, "hello, world!")); - return (lines[7] = 1, 0); + (++lines[6], fprintf(stderr, "hello, world!")); + return (++lines[7], 0); } EOF diff --git a/t/if.t b/t/if.t @@ -37,11 +37,11 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[198]; +static unsigned int lines[21]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 198, + .size = 21, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; @@ -50,19 +50,19 @@ struct scv_node node0 = { int main(int argc, char *argv[]) { - if ((lines[6] = 1, argc == 1)) - return (lines[7] = 1, 1); + if ((++lines[6], argc == 1)) + return (++lines[7], 1); else - (lines[9] = 1, exit(14)); + (++lines[9], exit(14)); - if ((lines[11] = 1, argc == 2)) { - return (lines[12] = 1, 5); + if ((++lines[11], argc == 2)) { + return (++lines[12], 5); } - else if ((lines[14] = 1, argc == 3)) { - return (lines[15] = 1, 0); + else if ((++lines[14], argc == 3)) { + return (++lines[15], 0); } else { - (lines[18] = 1, exit(0)); + (++lines[18], exit(0)); } } EOF diff --git a/t/return.t b/t/return.t @@ -28,24 +28,24 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[91]; +static unsigned int lines[12]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 91, + .size = 12, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; int foo() { - return (lines[2] = 1, 0); + return (++lines[2], 0); } int main(void) { - return (lines[6] = 1, 10); + return (++lines[6], 10); - return (lines[8] = 1, 10 + 10); + return (++lines[8], 10 + 10); - return (lines[10] = 1, (lines[10] = 1, foo())); + return (++lines[10], (++lines[10], foo())); } EOF diff --git a/t/switch.t b/t/switch.t @@ -31,11 +31,11 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[93]; +static unsigned int lines[15]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 93, + .size = 15, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; @@ -44,14 +44,14 @@ main(void) { int i; - switch ((lines[6] = 1, i)) { + switch ((++lines[6], i)) { case 0: break; case 1: break; } - return (lines[13] = 1, 0); + return (++lines[13], 0); } EOF diff --git a/t/while.t b/t/while.t @@ -29,11 +29,11 @@ my $tmp_dir = $project->get_tmpdir(); my $inst_src_good = <<EOF; #include <scv_global.h> -static unsigned int lines[76]; +static unsigned int lines[13]; struct scv_node node1; struct scv_node node0 = { .lines_ptr = lines, - .size = 76, + .size = 13, .file_name = "$tmp_dir/source_0.c", .next = &node1, }; @@ -43,11 +43,11 @@ main(void) int i; i = 0; - while ((lines[7] = 1, i < 17)) { + while ((++lines[7], i < 17)) { i++; } - return (lines[11] = 1, i); + return (++lines[11], i); } EOF