xref: /llvm-project/compiler-rt/test/profile/instrprof-write-buffer-internal.c (revision 16e74fd48988ac95551d0f64e1b36f78a82a89a2)
1 // UNSUPPORTED: target={{.*windows.*}}
2 // The sanitizer-windows bot is saying:
3 // instrprof-write-buffer-internal.c.tmp.buf.profraw: Invalid instrumentation profile data (file header is corrupt)
4 
5 // RUN: rm -f %t.buf.profraw %t.profraw
6 // RUN: %clang_profgen -w -o %t %s
7 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t %t.buf.profraw
8 // RUN: llvm-profdata show %t.buf.profraw | FileCheck %s -check-prefix=WRITE-BUFFER
9 // RUN: not llvm-profdata show %t.profraw 2>&1 | FileCheck %s -check-prefix=ALREADY-DUMPED
10 
11 // WRITE-BUFFER: Instrumentation level: Front-end
12 // WRITE-BUFFER: Total functions: 1
13 // WRITE-BUFFER: Maximum function count: 1
14 // WRITE-BUFFER: Maximum internal block count: 0
15 
16 // ALREADY-DUMPED: error: {{.+}}: empty raw profile file
17 
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 const void *__llvm_profile_begin_data(void);
23 const void *__llvm_profile_end_data(void);
24 const char *__llvm_profile_begin_names(void);
25 const char *__llvm_profile_end_names(void);
26 char *__llvm_profile_begin_counters(void);
27 char *__llvm_profile_end_counters(void);
28 char *__llvm_profile_begin_bitmap(void);
29 char *__llvm_profile_end_bitmap(void);
30 
31 uint64_t __llvm_profile_get_size_for_buffer_internal(
32     const void *DataBegin, const void *DataEnd, const char *CountersBegin,
33     const char *CountersEnd, const char *BitmapBegin, const char *BitmapEnd,
34     const char *NamesBegin, const char *NamesEnd, const void *VTableBegin,
35     const void *VTableEnd, const char *VNamesBegin, const char *VNamesEnd);
36 
37 int __llvm_profile_write_buffer_internal(
38     char *Buffer, const void *DataBegin, const void *DataEnd,
39     const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,
40     const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd);
41 
42 void __llvm_profile_set_dumped(void);
43 
main(int argc,const char * argv[])44 int main(int argc, const char *argv[]) {
45   uint64_t bufsize = __llvm_profile_get_size_for_buffer_internal(
46       __llvm_profile_begin_data(), __llvm_profile_end_data(),
47       __llvm_profile_begin_counters(), __llvm_profile_end_counters(),
48       __llvm_profile_begin_bitmap(), __llvm_profile_end_bitmap(),
49       __llvm_profile_begin_names(), __llvm_profile_end_names(), NULL, NULL,
50       NULL, NULL);
51 
52   char *buf = malloc(bufsize);
53   int ret = __llvm_profile_write_buffer_internal(
54       buf, __llvm_profile_begin_data(), __llvm_profile_end_data(),
55       __llvm_profile_begin_counters(), __llvm_profile_end_counters(),
56       __llvm_profile_begin_bitmap(), __llvm_profile_end_bitmap(),
57       __llvm_profile_begin_names(), __llvm_profile_end_names());
58 
59   if (ret != 0) {
60     fprintf(stderr, "failed to write buffer");
61     return ret;
62   }
63 
64   FILE *f = fopen(argv[1], "w");
65   fwrite(buf, bufsize, 1, f);
66   fclose(f);
67   free(buf);
68 
69   __llvm_profile_set_dumped();
70 
71   return 0;
72 }
73