Skip to content

Instantly share code, notes, and snippets.

@ashgti
Created January 31, 2013 19:40
Show Gist options
  • Save ashgti/4685743 to your computer and use it in GitHub Desktop.
Save ashgti/4685743 to your computer and use it in GitHub Desktop.
Index: lib/profile/GCDAProfiling.c
===================================================================
--- lib/profile/GCDAProfiling.c (revision 173961)
+++ lib/profile/GCDAProfiling.c (working copy)
@@ -269,6 +269,9 @@
for (i = 0; i < num_counters; ++i)
fprintf(stderr, "llvmgcda: %llu\n", (unsigned long long)counters[i]);
#endif
+
+ for (i = 0; i < num_counters; ++i)
+ counters[i] = 0;
}
void llvm_gcda_end_file() {
@@ -282,3 +285,34 @@
fprintf(stderr, "llvmgcda: -----\n");
#endif
}
+
+struct llvm_gcda_list {
+ struct llvm_gcda_list *next; /* next in list */
+ void (*fn_ptr)(void); /* function pointer */
+};
+
+static struct llvm_gcda_list *__llvm_gcda_list = NULL; /* points to head of LIFO stack */
+
+void __llvm_gcda_register(void (*func)(void)) {
+ struct llvm_gcda_list *p = malloc(sizeof(*p));
+
+ if (__llvm_gcda_list == NULL) {
+ __llvm_gcda_list = malloc(sizeof(*p));
+ __llvm_gcda_list->next = NULL;
+ __llvm_gcda_list->fn_ptr = NULL;
+ }
+
+ p->fn_ptr = func;
+ p->next = __llvm_gcda_list;
+ __llvm_gcda_list = p;
+}
+
+void __llvm_gcov_flush() {
+ struct llvm_gcda_list *p = __llvm_gcda_list;
+
+ while (p->next != NULL) {
+ if (p->fn_ptr)
+ p->fn_ptr();
+ p = p->next;
+ }
+}
\ No newline at end of file
Index: lib/Transforms/Instrumentation/GCOVProfiling.cpp
===================================================================
--- lib/Transforms/Instrumentation/GCOVProfiling.cpp (revision 173961)
+++ lib/Transforms/Instrumentation/GCOVProfiling.cpp (working copy)
@@ -700,6 +700,12 @@
PointerType::get(FTy, 0), false);
Constant *AtExitFn = M->getOrInsertFunction("atexit", FTy);
Builder.CreateCall(AtExitFn, WriteoutF);
+
+ // Register the write function with the flush utility so its possible to
+ // flush the gcov data before atexit as well.
+ Constant *GcdaRegisterFn = M->getOrInsertFunction("__llvm_gcda_register",
+ FTy);
+ Builder.CreateCall(GcdaRegisterFn, WriteoutF);
Builder.CreateRetVoid();
appendToGlobalCtors(*M, F, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment