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
22const void *__llvm_profile_begin_data(void);
23const void *__llvm_profile_end_data(void);
24const char *__llvm_profile_begin_names(void);
25const char *__llvm_profile_end_names(void);
26char *__llvm_profile_begin_counters(void);
27char *__llvm_profile_end_counters(void);
28char *__llvm_profile_begin_bitmap(void);
29char *__llvm_profile_end_bitmap(void);
30
31uint64_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);
35
36int __llvm_profile_write_buffer_internal(
37 char *Buffer, const void *DataBegin, const void *DataEnd,
38 const char *CountersBegin, const char *CountersEnd, const char *BitmapBegin,
39 const char *BitmapEnd, const char *NamesBegin, const char *NamesEnd);
40
41void __llvm_profile_set_dumped(void);
42
43int main(int argc, const char *argv[]) {
44 uint64_t bufsize = __llvm_profile_get_size_for_buffer_internal(
45 DataBegin: __llvm_profile_begin_data(), DataEnd: __llvm_profile_end_data(),
46 CountersBegin: __llvm_profile_begin_counters(), CountersEnd: __llvm_profile_end_counters(),
47 BitmapBegin: __llvm_profile_begin_bitmap(), BitmapEnd: __llvm_profile_end_bitmap(),
48 NamesBegin: __llvm_profile_begin_names(), NamesEnd: __llvm_profile_end_names());
49
50 char *buf = malloc(size: bufsize);
51 int ret = __llvm_profile_write_buffer_internal(
52 Buffer: buf, DataBegin: __llvm_profile_begin_data(), DataEnd: __llvm_profile_end_data(),
53 CountersBegin: __llvm_profile_begin_counters(), CountersEnd: __llvm_profile_end_counters(),
54 BitmapBegin: __llvm_profile_begin_bitmap(), BitmapEnd: __llvm_profile_end_bitmap(),
55 NamesBegin: __llvm_profile_begin_names(), NamesEnd: __llvm_profile_end_names());
56
57 if (ret != 0) {
58 fprintf(stderr, format: "failed to write buffer");
59 return ret;
60 }
61
62 FILE *f = fopen(filename: argv[1], modes: "w");
63 fwrite(ptr: buf, size: bufsize, n: 1, s: f);
64 fclose(stream: f);
65 free(ptr: buf);
66
67 __llvm_profile_set_dumped();
68
69 return 0;
70}
71

source code of compiler-rt/test/profile/instrprof-write-buffer-internal.c