Skip to content

Commit

Permalink
Clang/gcov: Supports gcc standard output interface "__gcov_dump"
Browse files Browse the repository at this point in the history
Signed-off-by: wangmingrong1 <wangmingrong1@xiaomi.com>
  • Loading branch information
W-M-R committed Nov 21, 2024
1 parent feb38c4 commit f2c7d83
Showing 1 changed file with 29 additions and 58 deletions.
87 changes: 29 additions & 58 deletions libs/libbuiltin/compiler-rt/coverage.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@

#include <fcntl.h>
#include <nuttx/fs/fs.h>
#include <nuttx/streams.h>

/****************************************************************************
* Pre-processor Definitions
****************************************************************************/

#define INSTR_PROF_RAW_VERSION 8
#define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
#define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime

/* Magic number to detect file format and endianness.
Expand Down Expand Up @@ -98,12 +98,6 @@ typedef struct __llvm_profile_header
enum value_kind value_kind_last;
}__llvm_profile_header;

/****************************************************************************
* Private Data
****************************************************************************/

static uint64_t INSTR_PROF_RAW_VERSION_VAR = INSTR_PROF_RAW_VERSION;

/****************************************************************************
* Public Data
****************************************************************************/
Expand Down Expand Up @@ -138,7 +132,7 @@ static uint64_t __llvm_profile_get_magic(void)

static uint64_t __llvm_profile_get_version(void)
{
return __llvm_profile_raw_version;
return INSTR_PROF_RAW_VERSION;
}

static uint64_t __llvm_profile_get_num_counters(const char *begin,
Expand Down Expand Up @@ -225,14 +219,9 @@ void __llvm_profile_register_names_function(void *names_start,
* llvm-prof. See the clang profiling documentation for details.
*/

void __llvm_profile_dump(const char *path)
void __llvm_profile_dump(lib_puts_t puts, FAR void *stream)
{
int fd;
int ret;

/* Header: __llvm_profile_header from InstrProfData.inc */

const char *filename = path;
const char c = '\0';

/* Calculate size of sections. */

Expand Down Expand Up @@ -266,61 +255,43 @@ void __llvm_profile_dump(const char *path)
hdr.names_delta = (uintptr_t)names_begin;
hdr.value_kind_last = IPVK_LAST;

fd = _NX_OPEN(filename, O_WRONLY | O_CREAT);
if (fd < 0)
{
_NX_SETERRNO(fd);
return;
}

/* Header */

ret = _NX_WRITE(fd, &hdr, sizeof(hdr));
if (ret != sizeof(hdr))
{
_NX_SETERRNO(ret);
goto exit;
}

/* Data */
puts(stream, &hdr, sizeof(hdr));
puts(stream, data_begin, sizeof(__llvm_profile_data) * num_data);
puts(stream, counters_begin, sizeof(uint64_t) * num_counters);
puts(stream, names_begin, names_size);

ret = _NX_WRITE(fd, data_begin, sizeof(__llvm_profile_data) * num_data);
if (ret != sizeof(__llvm_profile_data) * num_data)
for (; padding_bytes_after_names != 0; --padding_bytes_after_names)
{
_NX_SETERRNO(ret);
goto exit;
puts(stream, &c, 1);
}
}

/* Counters */
void __gcov_dump(void)
{
struct lib_rawoutstream_s stream;
char *path;
int fd;

ret = _NX_WRITE(fd, counters_begin, sizeof(uint64_t) * num_counters);
if (ret != sizeof(uint64_t) * num_counters)
path = getenv("GCOV_PREFIX");
if (!path)
{
_NX_SETERRNO(ret);
goto exit;
return;
}

/* Names */

ret = _NX_WRITE(fd, names_begin, names_size);
if (ret != names_size)
fd = _NX_OPEN(path, O_WRONLY | O_CREAT);
if (fd < 0)
{
_NX_SETERRNO(ret);
goto exit;
_NX_SETERRNO(fd);
return;
}

/* Padding */
lib_rawoutstream(&stream, fd);

for (; padding_bytes_after_names != 0; --padding_bytes_after_names)
{
ret = _NX_WRITE(fd, "\0", 1);
if (ret != 1)
{
_NX_SETERRNO(ret);
break;
}
}
__llvm_profile_dump(stream.common.puts, &stream);

exit:
_NX_CLOSE(fd);
}

void __gcov_reset(void)
{
}

0 comments on commit f2c7d83

Please sign in to comment.