commit c462065658e00942b453ce2306c90c46778626f5
parent 34c138436084858425552666902130d29c7bfa5b
Author: Kyle Milz <kyle@0x30.net>
Date: Thu, 23 Jun 2016 19:25:52 -0600
src: call start function after main to start control thread
Diffstat:
9 files changed, 43 insertions(+), 44 deletions(-)
diff --git a/lib/runtime.c b/lib/runtime.c
@@ -18,6 +18,12 @@
static struct citrun_node *citrun_nodes_head;
static struct citrun_node *citrun_nodes_tail;
+static void *control_thread(void *);
+
+/*
+ * Public interface.
+ */
+
void
citrun_node_add(struct citrun_node *node)
{
@@ -32,6 +38,17 @@ citrun_node_add(struct citrun_node *node)
citrun_nodes_tail = node;
}
+void
+citrun_start()
+{
+ pthread_t tid;
+ pthread_create(&tid, NULL, control_thread, NULL);
+}
+
+/*
+ * Private interface.
+ */
+
static int
xread(int d, const void *buf, size_t bytes_total)
{
@@ -142,31 +159,6 @@ send_execution_data(int fd)
}
}
-static void
-settle(void)
-{
- struct citrun_node *walk;
- unsigned int previous_total;
- unsigned int total = 0;
- unsigned int spun = 0;
-
- do {
- usleep(100 * 1000);
-
- walk = citrun_nodes_head;
- previous_total = total;
- total = 0;
-
- while (walk != NULL) {
- walk = walk->next;
- ++total;
- }
- ++spun;
- } while (previous_total != total);
-
- warnx("spun %u times, settled on %u nodes", spun, total);
-}
-
/* Sets up connection to the server socket and drops into an io loop. */
static void *
control_thread(void *arg)
@@ -181,7 +173,7 @@ control_thread(void *arg)
/* The default socket location can be overridden */
if ((viewer_sock = getenv("CITRUN_SOCKET")) == NULL)
- /* There was an error getting the env var, use the default */
+ /* Error, use the default */
viewer_sock = "/tmp/citrun-gl.socket";
/* Connect the socket to the server */
@@ -189,9 +181,6 @@ control_thread(void *arg)
addr.sun_family = AF_UNIX;
strlcpy(addr.sun_path, viewer_sock, sizeof(addr.sun_path));
- /* Make sure the translation unit linked list is consistent. */
- settle();
-
while (1) {
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
warn("connect");
@@ -209,11 +198,3 @@ control_thread(void *arg)
}
}
}
-
-/* Grab an execution context and start up the control thread. */
-__attribute__((constructor))
-static void runtime_init()
-{
- pthread_t tid;
- pthread_create(&tid, NULL, control_thread, NULL);
-}
diff --git a/src/inst_ast_visitor.cc b/src/inst_ast_visitor.cc
@@ -62,6 +62,24 @@ RewriteASTVisitor::VisitStmt(clang::Stmt *s)
bool
RewriteASTVisitor::VisitFunctionDecl(clang::FunctionDecl *f)
{
+ // Only function definitions (with bodies), not declarations.
+ if (f->hasBody() == 0)
+ return true;
+
+ clang::Stmt *FuncBody = f->getBody();
+
+ clang::DeclarationName DeclName = f->getNameInfo().getName();
+ std::string FuncName = DeclName.getAsString();
+
+ if (FuncName.compare("main") != 0)
+ // Function is not main
+ return true;
+
+ std::string start_function("citrun_start();");
+
+ clang::SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1));
+ TheRewriter.InsertTextBefore(curly_brace, start_function);
+
return true;
}
diff --git a/t/fibonacci.t b/t/fibonacci.t
@@ -61,7 +61,7 @@ fibonacci(long long n)
int
main(int argc, char *argv[])
-{
+{citrun_start();
long long n;
if ((++_citrun_lines[20], argc != 2)) {
diff --git a/t/for.t b/t/for.t
@@ -28,7 +28,7 @@ $project->compile();
my $inst_src_good = <<EOF;
int
main(void)
-{
+{citrun_start();
int i;
for (i = 0; (++_citrun_lines[6], i < 19); i++) {
diff --git a/t/hello_world.t b/t/hello_world.t
@@ -27,7 +27,7 @@ my $inst_src_good = <<EOF;
int
main(void)
-{
+{citrun_start();
(++_citrun_lines[6], printf("hello, world!"));
return (++_citrun_lines[7], 0);
}
diff --git a/t/if.t b/t/if.t
@@ -38,7 +38,7 @@ my $inst_src_good = <<EOF;
int
main(int argc, char *argv[])
-{
+{citrun_start();
if ((++_citrun_lines[6], argc == 1))
return (++_citrun_lines[7], 1);
else
diff --git a/t/return.t b/t/return.t
@@ -31,7 +31,7 @@ int foo() {
return (++_citrun_lines[2], 0);
}
-int main(void) {
+int main(void) {citrun_start();
return (++_citrun_lines[6], 10);
return (++_citrun_lines[8], 10 + 10);
diff --git a/t/switch.t b/t/switch.t
@@ -32,7 +32,7 @@ $project->compile();
my $inst_src_good = <<EOF;
int
main(void)
-{
+{citrun_start();
int i;
switch ((++_citrun_lines[6], i)) {
diff --git a/t/while.t b/t/while.t
@@ -30,7 +30,7 @@ $project->compile();
my $inst_src_good = <<EOF;
int
main(void)
-{
+{citrun_start();
int i;
i = 0;