Skip to content

Commit

Permalink
system: support gcov output to stdout
Browse files Browse the repository at this point in the history
Signed-off-by: yinshengkai <[email protected]>
  • Loading branch information
Gary-Hobson committed Nov 19, 2024
1 parent b3e1077 commit 3c32e9a
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 27 deletions.
2 changes: 1 addition & 1 deletion system/gcov/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

config SYSTEM_GCOV
tristate "gcov tool"
depends on SCHED_GCOV
depends on !COVERAGE_NONE
---help---
Enable support for the 'gcov' command.

Expand Down
127 changes: 101 additions & 26 deletions system/gcov/gcov.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,16 @@
* Included Files
****************************************************************************/

#include <gcov.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>

#include <nuttx/crc16.h>
#include <nuttx/streams.h>

/****************************************************************************
* Private Functions
****************************************************************************/
Expand All @@ -38,22 +44,16 @@ static void show_usage(FAR const char *progname)
{
printf("\nUsage: %s [-d path] [-t strip] [-r] [-h]\n", progname);
printf("\nWhere:\n");
printf(" -d dump the coverage, path is the path to the coverage file\n");
printf(" -d dump the coverage, path is the path to the coverage file, "
"the default output is to stdout\n");
printf(" -t strip the path prefix number\n");
printf(" -r reset the coverage\n");
printf(" -h show this text and exits.\n");
exit(EXIT_FAILURE);
}

/****************************************************************************
* Public Function Prototypes
****************************************************************************/

void __gcov_dump(void);
void __gcov_reset(void);

/****************************************************************************
* Private Functions
* Name: gcov_dump
****************************************************************************/

static void gcov_dump(FAR const char * path, FAR const char *strip)
Expand All @@ -69,6 +69,72 @@ static void gcov_dump(FAR const char * path, FAR const char *strip)
__gcov_dump();
}

/****************************************************************************
* Name: stdout_dump
****************************************************************************/

#ifndef CONFIG_COVERAGE_TOOLCHAIN
static void stdout_dump(FAR const void *buffer, size_t size,
FAR void *arg)
{
FAR const char *name = *((FAR const char **)arg);
struct lib_stdoutstream_s stdoutstream;
struct lib_hexdumpstream_s hexstream;
uint16_t checksum = 0;
int i;

if (size == 0)
{
return;
}

for (i = 0; i < size; i++)
{
checksum += ((uint8_t *)buffer)[i];
}

printf("gcov start filename:%s size: %zuByte\n", name, size);
lib_stdoutstream(&stdoutstream, stdout);
lib_hexdumpstream(&hexstream, &stdoutstream.common);
lib_stream_puts(&hexstream, buffer, size);
lib_stream_flush(&hexstream);

printf("gcov end filename:%s checksum: %#0x\n", name, checksum);
}

/****************************************************************************
* Name: stdout_filename
****************************************************************************/

static void stdout_filename(const char *name, void *arg)
{
*((FAR const char **)arg) = name;
__gcov_filename_to_gcfn(name, NULL, NULL);
}

/****************************************************************************
* Name: gcov_stdout_dump
*
* Description:
* Dump the gcov information of all translation units to stdout.
*
****************************************************************************/

static void gcov_stdout_dump(void)
{
FAR struct gcov_info *info = __gcov_info_start;
FAR struct gcov_info *end = __gcov_info_end;
FAR char *filename = NULL;

while (info != end)
{
__gcov_info_to_gcda(info, stdout_filename, stdout_dump, NULL,
&filename);
info = info->next;
}
}
#endif

/****************************************************************************
* Public Functions
****************************************************************************/
Expand All @@ -84,32 +150,41 @@ int main(int argc, FAR char *argv[])
show_usage(argv[0]);
}

while ((option = getopt(argc, argv, "d:t:rh")) != ERROR)
while ((option = getopt(argc, argv, "d::t:rh")) != ERROR)
{
switch (option)
{
case 'd':
path = optarg;
break;

case 't':
strip = optarg;
case 'd':
path = optarg;
break;

break;
case 't':
strip = optarg;
break;

case 'r':
__gcov_reset();
break;
case 'r':
__gcov_reset();
break;

case '?':
default:
fprintf(stderr, "ERROR: Unrecognized option\n");
case '?':
default:
fprintf(stderr, "ERROR: Unrecognized option\n");

case 'h':
show_usage(argv[0]);
case 'h':
show_usage(argv[0]);
}
}

gcov_dump(path, strip);
#ifndef CONFIG_COVERAGE_TOOLCHAIN
if (path == NULL)
{
gcov_stdout_dump();
}
else
#endif
{
gcov_dump(path, strip);
}

return 0;
}

0 comments on commit 3c32e9a

Please sign in to comment.