|
| 1 | +// RUN: rm -f %t.buf.profraw %t.profraw |
| 2 | +// RUN: %clang_profgen -o %t %s |
| 3 | +// RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t %t.buf.profraw |
| 4 | +// RUN: llvm-profdata show %t.buf.profraw | FileCheck %s -check-prefix=WRITE-BUFFER |
| 5 | +// RUN: not llvm-profdata show %t.profraw 2>&1 | FileCheck %s -check-prefix=ALREADY-DUMPED |
| 6 | + |
| 7 | +// WRITE-BUFFER: Instrumentation level: Front-end |
| 8 | +// WRITE-BUFFER: Total functions: 1 |
| 9 | +// WRITE-BUFFER: Maximum function count: 1 |
| 10 | +// WRITE-BUFFER: Maximum internal block count: 0 |
| 11 | + |
| 12 | +// ALREADY-DUMPED: error: {{.*}} Empty raw profile file |
| 13 | + |
| 14 | +#include <stdint.h> |
| 15 | +#include <stdio.h> |
| 16 | +#include <stdlib.h> |
| 17 | + |
| 18 | +const void *__llvm_profile_begin_data(void); |
| 19 | +const void *__llvm_profile_end_data(void); |
| 20 | +const char *__llvm_profile_begin_names(void); |
| 21 | +const char *__llvm_profile_end_names(void); |
| 22 | +uint64_t *__llvm_profile_begin_counters(void); |
| 23 | +uint64_t *__llvm_profile_end_counters(void); |
| 24 | + |
| 25 | +uint64_t __llvm_profile_get_size_for_buffer_internal( |
| 26 | + const void *DataBegin, const void *DataEnd, |
| 27 | + const uint64_t *CountersBegin, const uint64_t *CountersEnd, |
| 28 | + const char *NamesBegin, const char *NamesEnd); |
| 29 | + |
| 30 | +int __llvm_profile_write_buffer_internal( |
| 31 | + char *Buffer, const void *DataBegin, |
| 32 | + const void *DataEnd, const uint64_t *CountersBegin, |
| 33 | + const uint64_t *CountersEnd, const char *NamesBegin, |
| 34 | + const char *NamesEnd); |
| 35 | + |
| 36 | +void __llvm_profile_set_dumped(void); |
| 37 | + |
| 38 | +int main(int argc, const char *argv[]) { |
| 39 | + uint64_t bufsize = __llvm_profile_get_size_for_buffer_internal( |
| 40 | + __llvm_profile_begin_data(), __llvm_profile_end_data(), |
| 41 | + __llvm_profile_begin_counters(), __llvm_profile_end_counters(), |
| 42 | + __llvm_profile_begin_names(), __llvm_profile_end_names()); |
| 43 | + |
| 44 | + char *buf = malloc(bufsize); |
| 45 | + int ret = __llvm_profile_write_buffer_internal(buf, |
| 46 | + __llvm_profile_begin_data(), __llvm_profile_end_data(), |
| 47 | + __llvm_profile_begin_counters(), __llvm_profile_end_counters(), |
| 48 | + __llvm_profile_begin_names(), __llvm_profile_end_names()); |
| 49 | + |
| 50 | + if (ret != 0) { |
| 51 | + fprintf(stderr, "failed to write buffer"); |
| 52 | + return ret; |
| 53 | + } |
| 54 | + |
| 55 | + FILE *f = fopen(argv[1], "w"); |
| 56 | + fwrite(buf, bufsize, 1, f); |
| 57 | + fclose(f); |
| 58 | + free(buf); |
| 59 | + |
| 60 | + __llvm_profile_set_dumped(); |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
0 commit comments