Index: lib/profile/GCDAProfiling.c =================================================================== --- lib/profile/GCDAProfiling.c +++ lib/profile/GCDAProfiling.c @@ -86,31 +86,57 @@ static int new_file = 0; static int fd = -1; -/* - * A list of functions to write out the data. - */ -typedef void (*writeout_fn)(); +typedef void (*fn_ptr)(); + +struct fn_node { + fn_ptr fn; + struct fn_node* next; +}; -struct writeout_fn_node { - writeout_fn fn; - struct writeout_fn_node *next; +struct fn_list { + struct fn_node *head, *tail; }; -static struct writeout_fn_node *writeout_fn_head = NULL; -static struct writeout_fn_node *writeout_fn_tail = NULL; +void fn_list_insert(struct fn_list* list, struct fn_node* element) { + if (!list->head) { + list->head = list->tail = element; + } else { + list->tail->next = element; + list->tail = element; + } +} + +void fn_list_remove(struct fn_list* list, struct fn_node* element) { + struct fn_node* prev = NULL; + + if (element == list->head) { + list->head = element->next; + } else { + prev = list->head; + while (prev->next != element); + prev->next = element->next; + } + + if (element == list->tail) { + list->tail = prev; + } +} /* - * A list of flush functions that our __gcov_flush() function should call. + * A list of functions to write out the data. */ -typedef void (*flush_fn)(); +// This is the writeout list specific to a given dynamic object. +static struct fn_list writeout_fn_list_current; +// This is the writeout list shared between all dynamic objects. +struct fn_list writeout_fn_list_shared; -struct flush_fn_node { - flush_fn fn; - struct flush_fn_node *next; -}; - -static struct flush_fn_node *flush_fn_head = NULL; -static struct flush_fn_node *flush_fn_tail = NULL; +/* + * A list of flush functions that our __gcov_flush() function should call. + */ +// This is the flush list specific to a given dynamic object. +static struct fn_list flush_fn_list_current; +// This is the flush list shared between all dynamic objects. +struct fn_list flush_fn_list_shared; static void resize_write_buffer(uint64_t size) { if (!new_file) return; @@ -403,6 +429,7 @@ const uint32_t obj_summary_len = 9; /* Length for gcov compatibility. */ uint32_t i; uint32_t runs = 1; + static uint32_t run_counted = 0; // We only want to increase the run count once. uint32_t val = 0; uint64_t save_cur_pos = cur_pos; @@ -429,7 +456,9 @@ read_32bit_value(); /* checksum, unused */ read_32bit_value(); /* num, unused */ - runs += read_32bit_value(); /* Add previous run count to new counter. */ + uint32_t prev_runs = read_32bit_value(); + /* Add previous run count to new counter, if not already counted before. */ + runs = run_counted ? prev_runs : prev_runs + 1; } cur_pos = save_cur_pos; @@ -447,6 +476,8 @@ write_bytes("\0\0\0\xa3", 4); /* tag indicates 1 program */ write_32bit_value(0); /* 0 length */ + run_counted = 1; + #ifdef DEBUG_GCDAPROFILING fprintf(stderr, "llvmgcda: %u runs\n", runs); #endif @@ -479,22 +510,18 @@ } COMPILER_RT_VISIBILITY -void llvm_register_writeout_function(writeout_fn fn) { - struct writeout_fn_node *new_node = malloc(sizeof(struct writeout_fn_node)); +void llvm_register_writeout_function(fn_ptr fn) { + struct fn_node* new_node = malloc(sizeof(struct fn_node)); new_node->fn = fn; new_node->next = NULL; - if (!writeout_fn_head) { - writeout_fn_head = writeout_fn_tail = new_node; - } else { - writeout_fn_tail->next = new_node; - writeout_fn_tail = new_node; - } + fn_list_insert(&writeout_fn_list_current, new_node); + fn_list_insert(&writeout_fn_list_shared, new_node); } COMPILER_RT_VISIBILITY void llvm_writeout_files(void) { - struct writeout_fn_node *curr = writeout_fn_head; + struct fn_node *curr = writeout_fn_list_current.head; while (curr) { curr->fn(); @@ -504,32 +531,29 @@ COMPILER_RT_VISIBILITY void llvm_delete_writeout_function_list(void) { - while (writeout_fn_head) { - struct writeout_fn_node *node = writeout_fn_head; - writeout_fn_head = writeout_fn_head->next; - free(node); + struct fn_node* curr = writeout_fn_list_current.head; + + while (curr) { + fn_list_remove(&writeout_fn_list_current, curr); + fn_list_remove(&writeout_fn_list_shared, curr); + curr = curr->next; + free(curr); } - - writeout_fn_head = writeout_fn_tail = NULL; } COMPILER_RT_VISIBILITY -void llvm_register_flush_function(flush_fn fn) { - struct flush_fn_node *new_node = malloc(sizeof(struct flush_fn_node)); +void llvm_register_flush_function(fn_ptr fn) { + struct fn_node* new_node = malloc(sizeof(struct fn_node)); new_node->fn = fn; new_node->next = NULL; - if (!flush_fn_head) { - flush_fn_head = flush_fn_tail = new_node; - } else { - flush_fn_tail->next = new_node; - flush_fn_tail = new_node; - } + fn_list_insert(&flush_fn_list_current, new_node); + fn_list_insert(&flush_fn_list_shared, new_node); } COMPILER_RT_VISIBILITY void __gcov_flush() { - struct flush_fn_node *curr = flush_fn_head; + struct fn_node *curr = flush_fn_list_shared.head; while (curr) { curr->fn(); @@ -539,17 +563,18 @@ COMPILER_RT_VISIBILITY void llvm_delete_flush_function_list(void) { - while (flush_fn_head) { - struct flush_fn_node *node = flush_fn_head; - flush_fn_head = flush_fn_head->next; - free(node); - } + struct fn_node* curr = flush_fn_list_current.head; - flush_fn_head = flush_fn_tail = NULL; + while (curr) { + fn_list_remove(&flush_fn_list_current, curr); + fn_list_remove(&flush_fn_list_shared, curr); + curr = curr->next; + free(curr); + } } COMPILER_RT_VISIBILITY -void llvm_gcov_init(writeout_fn wfn, flush_fn ffn) { +void llvm_gcov_init(fn_ptr wfn, fn_ptr ffn) { static int atexit_ran = 0; if (wfn) Index: test/profile/Inputs/instrprof-dlopen-dlclose-main.c =================================================================== --- test/profile/Inputs/instrprof-dlopen-dlclose-main.c +++ test/profile/Inputs/instrprof-dlopen-dlclose-main.c @@ -10,17 +10,41 @@ return EXIT_FAILURE; } + void (*func)(void) = (void (*)(void))dlsym(f1_handle, "func"); + if (func == NULL) { + fprintf(stderr, "unable to lookup symbol 'func': %s\n", dlerror()); + return EXIT_FAILURE; + } + void *f2_handle = dlopen("func2.shared", RTLD_LAZY | RTLD_GLOBAL); if (f2_handle == NULL) { fprintf(stderr, "unable to open 'func2.shared': %s\n", dlerror()); return EXIT_FAILURE; } + void (*func2)(void) = (void (*)(void))dlsym(f2_handle, "func2"); + if (func2 == NULL) { + fprintf(stderr, "unable to lookup symbol 'func2': %s\n", dlerror()); + return EXIT_FAILURE; + } + func2(); + if (dlclose(f2_handle) != 0) { fprintf(stderr, "unable to close 'func2.shared': %s\n", dlerror()); return EXIT_FAILURE; } + func(); + + int g1 = 0; + int g2 = 0; + int n = 10; + + if (n % 5 == 0) + g1++; + else + g2++; + return EXIT_SUCCESS; } Index: test/profile/Inputs/instrprof-dlopen-dlclose-main.c.gcov =================================================================== --- test/profile/Inputs/instrprof-dlopen-dlclose-main.c.gcov +++ test/profile/Inputs/instrprof-dlopen-dlclose-main.c.gcov @@ -0,0 +1,55 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-dlopen-dlclose-main.c +// CHECK-NEXT: -: 0:Graph:instrprof-dlopen-dlclose-main.gcno +// CHECK-NEXT: -: 0:Data:instrprof-dlopen-dlclose-main.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:#include +// CHECK-NEXT: -: 2:#include +// CHECK-NEXT: -: 3:#include +// CHECK-NEXT: -: 4: +// CHECK-NEXT: -: 5:int main(int argc, char *argv[]) { +// CHECK-NEXT: 1: 6: dlerror(); +// CHECK-NEXT: 1: 7: void *f1_handle = dlopen("func.shared", RTLD_LAZY | RTLD_GLOBAL); +// CHECK-NEXT: 1: 8: if (f1_handle == NULL) { +// CHECK-NEXT: #####: 9: fprintf(stderr, "unable to open 'func.shared': %s\n", dlerror()); +// CHECK-NEXT: #####: 10: return EXIT_FAILURE; +// CHECK-NEXT: -: 11: } +// CHECK-NEXT: -: 12: +// CHECK-NEXT: 1: 13: void (*func)(void) = (void (*)(void))dlsym(f1_handle, "func"); +// CHECK-NEXT: 1: 14: if (func == NULL) { +// CHECK-NEXT: #####: 15: fprintf(stderr, "unable to lookup symbol 'func': %s\n", dlerror()); +// CHECK-NEXT: #####: 16: return EXIT_FAILURE; +// CHECK-NEXT: -: 17: } +// CHECK-NEXT: -: 18: +// CHECK-NEXT: 1: 19: void *f2_handle = dlopen("func2.shared", RTLD_LAZY | RTLD_GLOBAL); +// CHECK-NEXT: 1: 20: if (f2_handle == NULL) { +// CHECK-NEXT: #####: 21: fprintf(stderr, "unable to open 'func2.shared': %s\n", dlerror()); +// CHECK-NEXT: #####: 22: return EXIT_FAILURE; +// CHECK-NEXT: -: 23: } +// CHECK-NEXT: -: 24: +// CHECK-NEXT: 1: 25: void (*func2)(void) = (void (*)(void))dlsym(f2_handle, "func2"); +// CHECK-NEXT: 1: 26: if (func2 == NULL) { +// CHECK-NEXT: #####: 27: fprintf(stderr, "unable to lookup symbol 'func2': %s\n", dlerror()); +// CHECK-NEXT: #####: 28: return EXIT_FAILURE; +// CHECK-NEXT: -: 29: } +// CHECK-NEXT: 1: 30: func2(); +// CHECK-NEXT: -: 31: +// CHECK-NEXT: 1: 32: if (dlclose(f2_handle) != 0) { +// CHECK-NEXT: #####: 33: fprintf(stderr, "unable to close 'func2.shared': %s\n", dlerror()); +// CHECK-NEXT: #####: 34: return EXIT_FAILURE; +// CHECK-NEXT: -: 35: } +// CHECK-NEXT: -: 36: +// CHECK-NEXT: 1: 37: func(); +// CHECK-NEXT: -: 38: +// CHECK-NEXT: 1: 39: int g1 = 0; +// CHECK-NEXT: 1: 40: int g2 = 0; +// CHECK-NEXT: 1: 41: int n = 10; +// CHECK-NEXT: -: 42: +// CHECK-NEXT: 1: 43: if (n % 5 == 0) +// CHECK-NEXT: 1: 44: g1++; +// CHECK-NEXT: -: 45: else +// CHECK-NEXT: #####: 46: g2++; +// CHECK-NEXT: -: 47: +// CHECK-NEXT: 1: 48: return EXIT_SUCCESS; +// CHECK-NEXT: 1: 49:} +// CHECK-NEXT: -: 50: Index: test/profile/Inputs/instrprof-dlopen-func.c.gcov =================================================================== --- test/profile/Inputs/instrprof-dlopen-func.c.gcov +++ test/profile/Inputs/instrprof-dlopen-func.c.gcov @@ -0,0 +1,6 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-dlopen-func.c +// CHECK-NEXT: -: 0:Graph:instrprof-dlopen-func.gcno +// CHECK-NEXT: -: 0:Data:instrprof-dlopen-func.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: 3: 1:void func(int K) { if (K) {} } Index: test/profile/Inputs/instrprof-dlopen-func2.c.gcov =================================================================== --- test/profile/Inputs/instrprof-dlopen-func2.c.gcov +++ test/profile/Inputs/instrprof-dlopen-func2.c.gcov @@ -0,0 +1,6 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-dlopen-func2.c +// CHECK-NEXT: -: 0:Graph:instrprof-dlopen-func2.gcno +// CHECK-NEXT: -: 0:Data:instrprof-dlopen-func2.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: 3: 1:void func2(int K) { if (K) {} } Index: test/profile/Inputs/instrprof-shared-lib.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-lib.c.gcov +++ test/profile/Inputs/instrprof-shared-lib.c.gcov @@ -0,0 +1,14 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-lib.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-lib.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-lib.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:int g1 = 0; +// CHECK-NEXT: -: 2:int g2 = 1; +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:void foo(int n) { +// CHECK-NEXT: 1: 5: if (n % 5 == 0) +// CHECK-NEXT: #####: 6: g1++; +// CHECK-NEXT: -: 7: else +// CHECK-NEXT: 1: 8: g2++; +// CHECK-NEXT: 1: 9:} Index: test/profile/Inputs/instrprof-shared-lib_called-twice.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-lib_called-twice.c.gcov +++ test/profile/Inputs/instrprof-shared-lib_called-twice.c.gcov @@ -0,0 +1,14 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-lib.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-lib.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-lib.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:int g1 = 0; +// CHECK-NEXT: -: 2:int g2 = 1; +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:void foo(int n) { +// CHECK-NEXT: 2: 5: if (n % 5 == 0) +// CHECK-NEXT: #####: 6: g1++; +// CHECK-NEXT: -: 7: else +// CHECK-NEXT: 2: 8: g2++; +// CHECK-NEXT: 2: 9:} Index: test/profile/Inputs/instrprof-shared-lib_in-loop.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-lib_in-loop.c.gcov +++ test/profile/Inputs/instrprof-shared-lib_in-loop.c.gcov @@ -0,0 +1,14 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-lib.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-lib.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-lib.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:int g1 = 0; +// CHECK-NEXT: -: 2:int g2 = 1; +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:void foo(int n) { +// CHECK-NEXT: 1000000: 5: if (n % 5 == 0) +// CHECK-NEXT: 360000: 6: g1++; +// CHECK-NEXT: -: 7: else +// CHECK-NEXT: 640000: 8: g2++; +// CHECK-NEXT: 1000000: 9:} Index: test/profile/Inputs/instrprof-shared-main-gcov-flush.c =================================================================== --- test/profile/Inputs/instrprof-shared-main-gcov-flush.c +++ test/profile/Inputs/instrprof-shared-main-gcov-flush.c @@ -0,0 +1,36 @@ +extern void foo(int n); +extern void __gcov_flush(void); + +int bar1 = 0; +int bar2 = 1; + +void bar(int n) { + if (n % 5 == 0) + bar1++; + else + bar2++; +} + +int main(int argc, char *argv[]) { +#ifdef SHARED_CALL_BEFORE_GCOV_FLUSH + foo(1); +#endif + + bar(5); + + __gcov_flush(); + + bar(5); + +#ifdef SHARED_CALL_AFTER_GCOV_FLUSH + foo(1); +#endif + +#ifdef EXIT_ABRUPTLY + _exit(0); +#endif + + bar(5); + + return 0; +} Index: test/profile/Inputs/instrprof-shared-main-gcov-flush_no-writeout.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-main-gcov-flush_no-writeout.c.gcov +++ test/profile/Inputs/instrprof-shared-main-gcov-flush_no-writeout.c.gcov @@ -0,0 +1,41 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-main-gcov-flush.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-main-gcov-flush.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-main-gcov-flush.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:extern void foo(int n); +// CHECK-NEXT: -: 2:extern void __gcov_flush(void); +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:int bar1 = 0; +// CHECK-NEXT: -: 5:int bar2 = 1; +// CHECK-NEXT: -: 6: +// CHECK-NEXT: -: 7:void bar(int n) { +// CHECK-NEXT: 1: 8: if (n % 5 == 0) +// CHECK-NEXT: 1: 9: bar1++; +// CHECK-NEXT: -: 10: else +// CHECK-NEXT: #####: 11: bar2++; +// CHECK-NEXT: 1: 12:} +// CHECK-NEXT: -: 13: +// CHECK-NEXT: -: 14:int main(int argc, char *argv[]) { +// CHECK-NEXT: -: 15:#ifdef SHARED_CALL_BEFORE_GCOV_FLUSH +// CHECK-NEXT: 1: 16: foo(1); +// CHECK-NEXT: -: 17:#endif +// CHECK-NEXT: -: 18: +// CHECK-NEXT: 1: 19: bar(5); +// CHECK-NEXT: -: 20: +// CHECK-NEXT: 1: 21: __gcov_flush(); +// CHECK-NEXT: -: 22: +// CHECK-NEXT: 1: 23: bar(5); +// CHECK-NEXT: -: 24: +// CHECK-NEXT: -: 25:#ifdef SHARED_CALL_AFTER_GCOV_FLUSH +// CHECK-NEXT: 1: 26: foo(1); +// CHECK-NEXT: -: 27:#endif +// CHECK-NEXT: -: 28: +// CHECK-NEXT: -: 29:#ifdef EXIT_ABRUPTLY +// CHECK-NEXT: 1: 30: _exit(0); +// CHECK-NEXT: -: 31:#endif +// CHECK-NEXT: -: 32: +// CHECK-NEXT: -: 33: bar(5); +// CHECK-NEXT: -: 34: +// CHECK-NEXT: -: 35: return 0; +// CHECK-NEXT: #####: 36:} Index: test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-after.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-after.c.gcov +++ test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-after.c.gcov @@ -0,0 +1,41 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-main-gcov-flush.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-main-gcov-flush.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-main-gcov-flush.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:extern void foo(int n); +// CHECK-NEXT: -: 2:extern void __gcov_flush(void); +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:int bar1 = 0; +// CHECK-NEXT: -: 5:int bar2 = 1; +// CHECK-NEXT: -: 6: +// CHECK-NEXT: -: 7:void bar(int n) { +// CHECK-NEXT: 3: 8: if (n % 5 == 0) +// CHECK-NEXT: 3: 9: bar1++; +// CHECK-NEXT: -: 10: else +// CHECK-NEXT: #####: 11: bar2++; +// CHECK-NEXT: 3: 12:} +// CHECK-NEXT: -: 13: +// CHECK-NEXT: -: 14:int main(int argc, char *argv[]) { +// CHECK-NEXT: -: 15:#ifdef SHARED_CALL_BEFORE_GCOV_FLUSH +// CHECK-NEXT: -: 16: foo(1); +// CHECK-NEXT: -: 17:#endif +// CHECK-NEXT: -: 18: +// CHECK-NEXT: 1: 19: bar(5); +// CHECK-NEXT: -: 20: +// CHECK-NEXT: 1: 21: __gcov_flush(); +// CHECK-NEXT: -: 22: +// CHECK-NEXT: 1: 23: bar(5); +// CHECK-NEXT: -: 24: +// CHECK-NEXT: -: 25:#ifdef SHARED_CALL_AFTER_GCOV_FLUSH +// CHECK-NEXT: 1: 26: foo(1); +// CHECK-NEXT: -: 27:#endif +// CHECK-NEXT: -: 28: +// CHECK-NEXT: -: 29:#ifdef EXIT_ABRUPTLY +// CHECK-NEXT: -: 30: _exit(0); +// CHECK-NEXT: -: 31:#endif +// CHECK-NEXT: -: 32: +// CHECK-NEXT: 1: 33: bar(5); +// CHECK-NEXT: -: 34: +// CHECK-NEXT: 1: 35: return 0; +// CHECK-NEXT: -: 36:} Index: test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-before-after.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-before-after.c.gcov +++ test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-before-after.c.gcov @@ -0,0 +1,41 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-main-gcov-flush.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-main-gcov-flush.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-main-gcov-flush.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:extern void foo(int n); +// CHECK-NEXT: -: 2:extern void __gcov_flush(void); +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:int bar1 = 0; +// CHECK-NEXT: -: 5:int bar2 = 1; +// CHECK-NEXT: -: 6: +// CHECK-NEXT: -: 7:void bar(int n) { +// CHECK-NEXT: 3: 8: if (n % 5 == 0) +// CHECK-NEXT: 3: 9: bar1++; +// CHECK-NEXT: -: 10: else +// CHECK-NEXT: #####: 11: bar2++; +// CHECK-NEXT: 3: 12:} +// CHECK-NEXT: -: 13: +// CHECK-NEXT: -: 14:int main(int argc, char *argv[]) { +// CHECK-NEXT: -: 15:#ifdef SHARED_CALL_BEFORE_GCOV_FLUSH +// CHECK-NEXT: 1: 16: foo(1); +// CHECK-NEXT: -: 17:#endif +// CHECK-NEXT: -: 18: +// CHECK-NEXT: 1: 19: bar(5); +// CHECK-NEXT: -: 20: +// CHECK-NEXT: 1: 21: __gcov_flush(); +// CHECK-NEXT: -: 22: +// CHECK-NEXT: 1: 23: bar(5); +// CHECK-NEXT: -: 24: +// CHECK-NEXT: -: 25:#ifdef SHARED_CALL_AFTER_GCOV_FLUSH +// CHECK-NEXT: 1: 26: foo(1); +// CHECK-NEXT: -: 27:#endif +// CHECK-NEXT: -: 28: +// CHECK-NEXT: -: 29:#ifdef EXIT_ABRUPTLY +// CHECK-NEXT: -: 30: _exit(0); +// CHECK-NEXT: -: 31:#endif +// CHECK-NEXT: -: 32: +// CHECK-NEXT: 1: 33: bar(5); +// CHECK-NEXT: -: 34: +// CHECK-NEXT: 1: 35: return 0; +// CHECK-NEXT: -: 36:} Index: test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-before.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-before.c.gcov +++ test/profile/Inputs/instrprof-shared-main-gcov-flush_shared-call-before.c.gcov @@ -0,0 +1,41 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-main-gcov-flush.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-main-gcov-flush.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-main-gcov-flush.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:extern void foo(int n); +// CHECK-NEXT: -: 2:extern void __gcov_flush(void); +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:int bar1 = 0; +// CHECK-NEXT: -: 5:int bar2 = 1; +// CHECK-NEXT: -: 6: +// CHECK-NEXT: -: 7:void bar(int n) { +// CHECK-NEXT: 3: 8: if (n % 5 == 0) +// CHECK-NEXT: 3: 9: bar1++; +// CHECK-NEXT: -: 10: else +// CHECK-NEXT: #####: 11: bar2++; +// CHECK-NEXT: 3: 12:} +// CHECK-NEXT: -: 13: +// CHECK-NEXT: -: 14:int main(int argc, char *argv[]) { +// CHECK-NEXT: -: 15:#ifdef SHARED_CALL_BEFORE_GCOV_FLUSH +// CHECK-NEXT: 1: 16: foo(1); +// CHECK-NEXT: -: 17:#endif +// CHECK-NEXT: -: 18: +// CHECK-NEXT: 1: 19: bar(5); +// CHECK-NEXT: -: 20: +// CHECK-NEXT: 1: 21: __gcov_flush(); +// CHECK-NEXT: -: 22: +// CHECK-NEXT: 1: 23: bar(5); +// CHECK-NEXT: -: 24: +// CHECK-NEXT: -: 25:#ifdef SHARED_CALL_AFTER_GCOV_FLUSH +// CHECK-NEXT: -: 26: foo(1); +// CHECK-NEXT: -: 27:#endif +// CHECK-NEXT: -: 28: +// CHECK-NEXT: -: 29:#ifdef EXIT_ABRUPTLY +// CHECK-NEXT: -: 30: _exit(0); +// CHECK-NEXT: -: 31:#endif +// CHECK-NEXT: -: 32: +// CHECK-NEXT: 1: 33: bar(5); +// CHECK-NEXT: -: 34: +// CHECK-NEXT: 1: 35: return 0; +// CHECK-NEXT: -: 36:} Index: test/profile/Inputs/instrprof-shared-main.c.gcov =================================================================== --- test/profile/Inputs/instrprof-shared-main.c.gcov +++ test/profile/Inputs/instrprof-shared-main.c.gcov @@ -0,0 +1,18 @@ +// CHECK: -: 0:Source:{{.*}}Inputs/instrprof-shared-main.c +// CHECK-NEXT: -: 0:Graph:instrprof-shared-main.gcno +// CHECK-NEXT: -: 0:Data:instrprof-shared-main.gcda +// CHECK-NEXT: -: 0:Runs:1 +// CHECK-NEXT: -: 0:Programs:1 +// CHECK-NEXT: -: 1:extern int g1, g2; +// CHECK-NEXT: -: 2:extern void foo(int n); +// CHECK-NEXT: -: 3: +// CHECK-NEXT: -: 4:int main() { +// CHECK-NEXT: -: 5: int i, j; +// CHECK-NEXT: 2002: 6: for (i = 0; i < 1000; i++) +// CHECK-NEXT: 2002000: 7: for (j = 0; j < 1000; j++) +// CHECK-NEXT: 1001000: 8: foo(i * j); +// CHECK-NEXT: -: 9: +// CHECK-NEXT: 1: 10: if (g2 - g1 == 280001) +// CHECK-NEXT: 1: 11: return 0; +// CHECK-NEXT: #####: 12: return 1; +// CHECK-NEXT: 1: 13:} Index: test/profile/instrprof-dlopen-dlclose-gcov.test =================================================================== --- test/profile/instrprof-dlopen-dlclose-gcov.test +++ test/profile/instrprof-dlopen-dlclose-gcov.test @@ -1,6 +1,15 @@ RUN: mkdir -p %t.d -RUN: %clang --coverage -o %t.d/func.shared -fPIC -shared %S/Inputs/instrprof-dlopen-func.c -RUN: %clang --coverage -o %t.d/func2.shared -fPIC -shared %S/Inputs/instrprof-dlopen-func2.c +RUN: cd %t.d + +RUN: %clang --coverage -o func.shared -fPIC -shared %S/Inputs/instrprof-dlopen-func.c +RUN: %clang --coverage -o func2.shared -fPIC -shared %S/Inputs/instrprof-dlopen-func2.c RUN: %clang --coverage -o %t -fPIC -rpath %t.d %S/Inputs/instrprof-dlopen-dlclose-main.c RUN: %run %t +RUN: llvm-cov gcov instrprof-dlopen-dlclose-main.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-dlopen-dlclose-main.c.gcov %S/Inputs/instrprof-dlopen-dlclose-main.c.gcov +RUN: llvm-cov gcov instrprof-dlopen-func.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-dlopen-func.c.gcov %S/Inputs/instrprof-dlopen-func.c.gcov +RUN: llvm-cov gcov instrprof-dlopen-func2.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-dlopen-func2.c.gcov %S/Inputs/instrprof-dlopen-func2.c.gcov +RUN: rm instrprof-dlopen-dlclose-main.gcda instrprof-dlopen-func.gcda instrprof-dlopen-func2.gcda Index: test/profile/instrprof-gcov-two-objects.test =================================================================== --- test/profile/instrprof-gcov-two-objects.test +++ test/profile/instrprof-gcov-two-objects.test @@ -0,0 +1,18 @@ +RUN: mkdir -p %t.d +RUN: cd %t.d + +RUN: %clang --coverage -o instrprof-shared-lib.o -c %S/Inputs/instrprof-shared-lib.c +RUN: test -f instrprof-shared-lib.gcno + +RUN: %clang --coverage -o instrprof-shared-main.o -c %S/Inputs/instrprof-shared-main.c +RUN: test -f instrprof-shared-main.gcno + +RUN: %clang --coverage -o %t instrprof-shared-main.o instrprof-shared-lib.o +RUN: test -f %t + +RUN: %run %t +RUN: llvm-cov gcov instrprof-shared-main.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-main.c.gcov %S/Inputs/instrprof-shared-main.c.gcov +RUN: llvm-cov gcov instrprof-shared-lib.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-lib.c.gcov %S/Inputs/instrprof-shared-lib_in-loop.c.gcov +RUN: rm instrprof-shared-main.gcda instrprof-shared-lib.gcda Index: test/profile/instrprof-shared-gcov-flush.test =================================================================== --- test/profile/instrprof-shared-gcov-flush.test +++ test/profile/instrprof-shared-gcov-flush.test @@ -0,0 +1,49 @@ +RUN: mkdir -p %t.d +RUN: cd %t.d + +RUN: %clang --coverage -o libfunc.so -fPIC -shared %S/Inputs/instrprof-shared-lib.c +RUN: test -f instrprof-shared-lib.gcno + +# Test the case where we exit abruptly after calling __gcov_flush, which means we don't write out the counters at exit. +RUN: %clang -DEXIT_ABRUPTLY -DSHARED_CALL_BEFORE_GCOV_FLUSH -DSHARED_CALL_AFTER_GCOV_FLUSH --coverage -o %t -L%t.d -rpath %t.d -lfunc %S/Inputs/instrprof-shared-main-gcov-flush.c +RUN: test -f instrprof-shared-main-gcov-flush.gcno + +RUN: %run %t +RUN: llvm-cov gcov instrprof-shared-main-gcov-flush.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-main-gcov-flush.c.gcov %S/Inputs/instrprof-shared-main-gcov-flush_no-writeout.c.gcov +RUN: llvm-cov gcov instrprof-shared-lib.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-lib.c.gcov %S/Inputs/instrprof-shared-lib.c.gcov +RUN: rm instrprof-shared-main-gcov-flush.gcda instrprof-shared-lib.gcda + +# Test the case where we exit normally and we have a call to the shared library function before __gcov_flush. +RUN: %clang -DSHARED_CALL_BEFORE_GCOV_FLUSH --coverage -o %t -L%t.d -rpath %t.d -lfunc %S/Inputs/instrprof-shared-main-gcov-flush.c +RUN: test -f instrprof-shared-main-gcov-flush.gcno + +RUN: %run %t +RUN: llvm-cov gcov instrprof-shared-main-gcov-flush.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-main-gcov-flush.c.gcov %S/Inputs/instrprof-shared-main-gcov-flush_shared-call-before.c.gcov +RUN: llvm-cov gcov instrprof-shared-lib.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-lib.c.gcov %S/Inputs/instrprof-shared-lib.c.gcov +RUN: rm instrprof-shared-main-gcov-flush.gcda instrprof-shared-lib.gcda + +# Test the case where we exit normally and we have a call to the shared library function after __gcov_flush. +RUN: %clang -DSHARED_CALL_AFTER_GCOV_FLUSH --coverage -o %t -L%t.d -rpath %t.d -lfunc %S/Inputs/instrprof-shared-main-gcov-flush.c +RUN: test -f instrprof-shared-main-gcov-flush.gcno + +RUN: %run %t +RUN: llvm-cov gcov instrprof-shared-main-gcov-flush.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-main-gcov-flush.c.gcov %S/Inputs/instrprof-shared-main-gcov-flush_shared-call-after.c.gcov +RUN: llvm-cov gcov instrprof-shared-lib.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-lib.c.gcov %S/Inputs/instrprof-shared-lib.c.gcov +RUN: rm instrprof-shared-main-gcov-flush.gcda instrprof-shared-lib.gcda + +# Test the case where we exit normally and we have calls to the shared library function before and after __gcov_flush. +RUN: %clang -DSHARED_CALL_BEFORE_GCOV_FLUSH -DSHARED_CALL_AFTER_GCOV_FLUSH --coverage -o %t -L%t.d -rpath %t.d -lfunc %S/Inputs/instrprof-shared-main-gcov-flush.c +RUN: test -f instrprof-shared-main-gcov-flush.gcno + +RUN: %run %t +RUN: llvm-cov gcov instrprof-shared-main-gcov-flush.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-main-gcov-flush.c.gcov %S/Inputs/instrprof-shared-main-gcov-flush_shared-call-before-after.c.gcov +RUN: llvm-cov gcov instrprof-shared-lib.gcda +RUN: FileCheck --match-full-lines --strict-whitespace --input-file instrprof-shared-lib.c.gcov %S/Inputs/instrprof-shared-lib_called-twice.c.gcov +RUN: rm instrprof-shared-main-gcov-flush.gcda instrprof-shared-lib.gcda