2022-06-27 19:36:56 +02:00
|
|
|
/*
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
|
|
|
UMKa - User-Mode KolibriOS developer tools
|
|
|
|
|
|
|
|
Copyright (C) 2020 Ivan Baravy <dunkaist@gmail.com>
|
|
|
|
*/
|
|
|
|
|
2020-01-29 11:53:13 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2020-02-21 03:21:09 +01:00
|
|
|
#define MAX_COVERED_CODE_SIZE (256*1024)
|
2020-01-29 11:53:13 +01:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint64_t to_cnt, from_cnt;
|
|
|
|
} branch;
|
|
|
|
|
|
|
|
branch branches[MAX_COVERED_CODE_SIZE];
|
|
|
|
|
|
|
|
uint32_t coverage_offset, coverage_begin, coverage_end;
|
|
|
|
|
|
|
|
void read_coverage_file(const char *fname) {
|
2020-01-30 02:01:36 +01:00
|
|
|
FILE *f = fopen(fname, "r");
|
2020-01-29 11:53:13 +01:00
|
|
|
fseeko(f, 0, SEEK_END);
|
|
|
|
off_t fsize = ftello(f);
|
|
|
|
fseeko(f, 0, SEEK_SET);
|
2020-02-21 03:21:09 +01:00
|
|
|
fread(&coverage_begin, sizeof(uint32_t), 1, f);
|
|
|
|
fread(&coverage_end, sizeof(uint32_t), 1, f);
|
|
|
|
size_t branch_cnt = (fsize-4*2)/(2*4);
|
2020-01-29 11:53:13 +01:00
|
|
|
for (size_t i = 0; i < branch_cnt; i++) {
|
|
|
|
uint32_t from, to;
|
|
|
|
fread(&from, sizeof(uint32_t), 1, f);
|
|
|
|
fread(&to, sizeof(uint32_t), 1, f);
|
2020-02-21 03:21:09 +01:00
|
|
|
if (from >= coverage_begin && from < coverage_end) {
|
2020-01-29 11:53:13 +01:00
|
|
|
from = from - coverage_begin + coverage_offset;
|
|
|
|
branches[from].from_cnt++;
|
|
|
|
}
|
2020-02-21 03:21:09 +01:00
|
|
|
if (to >= coverage_begin && to < coverage_end) {
|
2020-01-29 11:53:13 +01:00
|
|
|
to = to - coverage_begin + coverage_offset;
|
|
|
|
branches[to].to_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count_line_bytes(const char *s) {
|
|
|
|
size_t cnt = 0;
|
|
|
|
for (size_t i = 10; i <= 58; i += 3) {
|
|
|
|
if (s[i] == ' ') break;
|
|
|
|
cnt++;
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t count_block_bytes(FILE *f) {
|
|
|
|
char tmp[1024];
|
|
|
|
size_t cnt = 0;
|
|
|
|
if (fgets(tmp, 1024, f) && strspn(tmp, "0123456789ABCDEF") == 8) {
|
|
|
|
cnt = count_line_bytes(tmp);
|
|
|
|
while (fgets(tmp, 1024, f) && tmp[0] == ' ' && tmp[10] != ' ') {
|
|
|
|
cnt += count_line_bytes(tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2020-01-30 02:01:36 +01:00
|
|
|
int is_cond_jump(const char *s) {
|
|
|
|
s += strspn(s, " \t");
|
2020-03-09 22:03:57 +01:00
|
|
|
int found = !strncmp(s, "jo", 2)
|
|
|
|
|| !strncmp(s, "jno", 3)
|
|
|
|
|| !strncmp(s, "js", 2)
|
|
|
|
|| !strncmp(s, "jns", 3)
|
|
|
|
|| !strncmp(s, "je", 2)
|
|
|
|
|| !strncmp(s, "jne", 3)
|
|
|
|
|| !strncmp(s, "jz", 2)
|
|
|
|
|| !strncmp(s, "jnz", 3)
|
|
|
|
|| !strncmp(s, "jb", 2)
|
|
|
|
|| !strncmp(s, "jnb", 3)
|
|
|
|
|| !strncmp(s, "jc", 2)
|
|
|
|
|| !strncmp(s, "jnc", 3)
|
|
|
|
|| !strncmp(s, "jae", 3)
|
|
|
|
|| !strncmp(s, "jnae", 4)
|
|
|
|
|| !strncmp(s, "jbe", 3)
|
|
|
|
|| !strncmp(s, "jna", 3)
|
|
|
|
|| !strncmp(s, "ja", 2)
|
|
|
|
|| !strncmp(s, "jnbe", 4)
|
|
|
|
|| !strncmp(s, "jl", 2)
|
|
|
|
|| !strncmp(s, "jnge", 4)
|
|
|
|
|| !strncmp(s, "jge", 3)
|
|
|
|
|| !strncmp(s, "jnl", 3)
|
|
|
|
|| !strncmp(s, "jle", 3)
|
|
|
|
|| !strncmp(s, "jng", 3)
|
|
|
|
|| !strncmp(s, "jg",2)
|
|
|
|
|| !strncmp(s, "jnle", 4)
|
|
|
|
|| !strncmp(s, "jp", 2)
|
|
|
|
|| !strncmp(s, "jpe", 3)
|
|
|
|
|| !strncmp(s, "jnp", 3)
|
|
|
|
|| !strncmp(s, "jpo", 3)
|
|
|
|
|| !strncmp(s, "jcxz", 4)
|
|
|
|
|| !strncmp(s, "jecxz", 5);
|
2020-01-30 02:01:36 +01:00
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2020-01-29 11:53:13 +01:00
|
|
|
int main(int argc, char **argv) {
|
2020-02-21 03:21:09 +01:00
|
|
|
if (argc < 4) {
|
|
|
|
fprintf(stderr, "usage: covpreproc <listing file> <coverage_begin offset> <coverage files ...>\n");
|
2020-01-29 11:53:13 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
sscanf(argv[2], "%" SCNx32, &coverage_offset);
|
|
|
|
|
2020-02-21 03:21:09 +01:00
|
|
|
for (int i = 3; i < argc; i++) {
|
2020-01-29 11:53:13 +01:00
|
|
|
read_coverage_file(argv[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE *f = fopen(argv[1], "r");
|
|
|
|
char tmp[1024];
|
2020-01-30 02:01:36 +01:00
|
|
|
uint64_t cur = 0, taken, not_taken;
|
2020-01-29 11:53:13 +01:00
|
|
|
while (1) {
|
|
|
|
off_t fpos_before = ftello(f);
|
|
|
|
if (!fgets(tmp, 1024, f)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
off_t fpos_after = ftello(f);
|
|
|
|
if (strspn(tmp, "0123456789ABCDEF") == 8) {
|
2020-01-30 02:01:36 +01:00
|
|
|
int is_cond = is_cond_jump(tmp + 64);
|
2020-01-29 11:53:13 +01:00
|
|
|
fseeko(f, fpos_before, SEEK_SET);
|
|
|
|
size_t inst_len = count_block_bytes(f);
|
|
|
|
fseeko(f, fpos_after, SEEK_SET);
|
|
|
|
unsigned long pos = strtoul(tmp, NULL, 16);
|
|
|
|
size_t total_to = 0, total_from = 0;
|
|
|
|
for (size_t i = 0; i < inst_len; i++) {
|
2020-02-21 03:21:09 +01:00
|
|
|
if (pos + i < coverage_end - coverage_begin + coverage_offset) {
|
|
|
|
total_to += branches[pos + i].to_cnt;
|
|
|
|
total_from += branches[pos + i].from_cnt;
|
|
|
|
}
|
2020-01-29 11:53:13 +01:00
|
|
|
}
|
|
|
|
cur += total_to;
|
2020-01-30 02:01:36 +01:00
|
|
|
if (is_cond) {
|
|
|
|
taken = total_from;
|
|
|
|
not_taken = cur - taken;
|
|
|
|
if (taken && not_taken) {
|
|
|
|
putchar(' ');
|
|
|
|
} else {
|
|
|
|
putchar('-');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
putchar(' ');
|
|
|
|
}
|
2020-01-29 11:53:13 +01:00
|
|
|
if (cur) {
|
|
|
|
putchar(' ');
|
|
|
|
} else {
|
|
|
|
putchar('-');
|
|
|
|
}
|
2020-01-30 02:01:36 +01:00
|
|
|
if (is_cond) {
|
|
|
|
int spaces = 19 - printf("%10" PRIu64 "/%" PRIu64, taken, not_taken);
|
|
|
|
while (spaces-- > 0)
|
|
|
|
putchar(' ');
|
2020-01-29 11:53:13 +01:00
|
|
|
} else {
|
2020-01-30 02:01:36 +01:00
|
|
|
printf(" ");
|
2020-01-29 11:53:13 +01:00
|
|
|
}
|
2020-01-30 02:01:36 +01:00
|
|
|
printf(" %10" PRIu64, cur);
|
2020-01-29 11:53:13 +01:00
|
|
|
cur -= total_from;
|
|
|
|
} else {
|
2020-01-30 02:01:36 +01:00
|
|
|
printf(" ");
|
2020-01-29 11:53:13 +01:00
|
|
|
}
|
|
|
|
printf(" : %s", tmp + 64);
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|