citrun

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

commit c5fa1af46fb18c83f9d5b280c81749f5cd4248f1
parent b179e065566e9a5a942c3f38482405979d739edf
Author: Kyle Milz <kyle@getaddrinfo.net>
Date:   Fri, 18 Mar 2016 21:50:01 -0600

instrument: add runtime function call to instrumentation

- when visiting the 'main' function add a call to libscv_init()
  - libscv() init is provided by the runtime
- this is needed because otherwise linux will not link against the runtime
  library

Diffstat:
Minstrument/instrumenter.cxx | 41++++++++++++++++-------------------------
Mlib/runtime.c | 10++++++++++
Mt/fibonacci.t | 2+-
Mt/for.t | 2+-
Mt/hello_world.t | 2+-
Mt/if.t | 2+-
Mt/return.t | 2+-
Mt/switch.t | 2+-
Mt/while.t | 2+-
9 files changed, 33 insertions(+), 32 deletions(-)

diff --git a/instrument/instrumenter.cxx b/instrument/instrumenter.cxx @@ -77,31 +77,22 @@ bool instrumenter::VisitFunctionDecl(FunctionDecl *f) { // Only function definitions (with bodies), not declarations. - if (f->hasBody()) { -#if 0 - Stmt *FuncBody = f->getBody(); - // Type name as string - QualType QT = f->getReturnType(); - std::string TypeStr = QT.getAsString(); - - // Function name - DeclarationName DeclName = f->getNameInfo().getName(); - std::string FuncName = DeclName.getAsString(); - - // Add comment before - std::stringstream SSBefore; - SSBefore << "// Begin function " << FuncName << " returning " << TypeStr - << "\n"; - SourceLocation ST = f->getSourceRange().getBegin(); - TheRewriter.InsertText(ST, SSBefore.str(), true, true); - - // And after - std::stringstream SSAfter; - SSAfter << "\n// End function " << FuncName; - ST = FuncBody->getLocEnd().getLocWithOffset(1); - TheRewriter.InsertText(ST, SSAfter.str(), true, true); -#endif - } + if (f->hasBody() == 0) + return true; + + Stmt *FuncBody = f->getBody(); + + DeclarationName DeclName = f->getNameInfo().getName(); + std::string FuncName = DeclName.getAsString(); + + if (FuncName.compare("main") != 0) + // Function is not main + return true; + + std::stringstream ss; + ss << "libscv_init();"; + SourceLocation curly_brace(FuncBody->getLocStart().getLocWithOffset(1)); + TheRewriter.InsertTextBefore(curly_brace, ss.str()); return true; } diff --git a/lib/runtime.c b/lib/runtime.c @@ -22,6 +22,16 @@ void send_execution_data(int); int xread(int d, const void *buf, size_t bytes_total); int xwrite(int d, const void *buf, size_t bytes_total); +/* + * Dummy function to make sure that the instrumented program gets linked against + * this library. + * Linux likes to liberally discard -l... flags given when linking. + */ +void +libscv_init() +{ +} + void * control_thread(void *arg) { diff --git a/t/fibonacci.t b/t/fibonacci.t @@ -73,7 +73,7 @@ fibonacci(long long n) int main(int argc, char *argv[]) -{ +{libscv_init(); long long n; if ((++lines[20], argc != 2)) { diff --git a/t/for.t b/t/for.t @@ -40,7 +40,7 @@ struct scv_node node0 = { }; int main(void) -{ +{libscv_init(); int i; for (i = 0; (++lines[6], i < 19); i++) { diff --git a/t/hello_world.t b/t/hello_world.t @@ -39,7 +39,7 @@ struct scv_node node0 = { int main(void) -{ +{libscv_init(); (++lines[6], fprintf(stderr, "hello, world!")); return (++lines[7], 0); } diff --git a/t/if.t b/t/if.t @@ -49,7 +49,7 @@ struct scv_node node0 = { int main(int argc, char *argv[]) -{ +{libscv_init(); if ((++lines[6], argc == 1)) return (++lines[7], 1); else diff --git a/t/return.t b/t/return.t @@ -42,7 +42,7 @@ int foo() { return (++lines[2], 0); } -int main(void) { +int main(void) {libscv_init(); return (++lines[6], 10); return (++lines[8], 10 + 10); diff --git a/t/switch.t b/t/switch.t @@ -43,7 +43,7 @@ struct scv_node node0 = { }; int main(void) -{ +{libscv_init(); int i; switch ((++lines[6], i)) { diff --git a/t/while.t b/t/while.t @@ -41,7 +41,7 @@ struct scv_node node0 = { }; int main(void) -{ +{libscv_init(); int i; i = 0;