Created
November 9, 2015 19:08
-
-
Save dvyukov/a9983694b1826340a3f5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/stat.h> | |
#include <sys/mman.h> | |
#include <pthread.h> | |
#include <fcntl.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <string.h> | |
#include <unistd.h> | |
#include <stdint.h> | |
#include <stddef.h> | |
#define SIZE (128*1024) | |
int fd; | |
void *cov; | |
int cover[5]; | |
uint32_t pcs[5][SIZE]; | |
uint32_t tmppcs[5][SIZE]; | |
void *thr(void *p) | |
{ | |
int n = write(fd, "enable", strlen("enable")); | |
if (n != strlen("enable")) | |
exit(printf("enable write failed (%d)\n", errno)); | |
int tmp[5] = {}; | |
int fds[2]; | |
*(volatile uint32_t*)cov = 0; | |
pipe(fds); | |
tmp[0] = *(volatile uint32_t*)cov; | |
memcpy(tmppcs[0], &((uint32_t*)cov)[1], tmp[0] * sizeof(uint32_t)); | |
*(volatile uint32_t*)cov = 0; | |
write(fds[1], &n, 1); | |
tmp[1] = *(volatile uint32_t*)cov; | |
memcpy(tmppcs[1], &((uint32_t*)cov)[1], tmp[1] * sizeof(uint32_t)); | |
*(volatile uint32_t*)cov = 0; | |
read(fds[0], &n, 1); | |
tmp[2] = *(volatile uint32_t*)cov; | |
memcpy(tmppcs[2], &((uint32_t*)cov)[1], tmp[2] * sizeof(uint32_t)); | |
*(volatile uint32_t*)cov = 0; | |
close(fds[0]); | |
tmp[3] = *(volatile uint32_t*)cov; | |
memcpy(tmppcs[3], &((uint32_t*)cov)[1], tmp[3] * sizeof(uint32_t)); | |
*(volatile uint32_t*)cov = 0; | |
close(fds[1]); | |
tmp[4] = *(volatile uint32_t*)cov; | |
memcpy(tmppcs[4], &((uint32_t*)cov)[1], tmp[4] * sizeof(uint32_t)); | |
*(volatile uint32_t*)cov = 0; | |
printf("cover: %d/%d/%d/%d/%d\n", tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]); | |
int i; | |
for (i = 0; i < 5; i++) { | |
if (tmp[i] == 0) | |
exit(printf("u no cover?\n")); | |
if (cover[i] == 0) { | |
cover[i] = tmp[i]; | |
memcpy(pcs[i], tmppcs[i], tmp[i] * sizeof(uint32_t)); | |
} | |
if (cover[i] != tmp[i] || memcmp(pcs[i], tmppcs[i], tmp[i] * sizeof(uint32_t))) { | |
int start, end, x; | |
for (start = 0; start < tmp[i] && start < cover[i] && pcs[i][start] == tmppcs[i][start]; start++); | |
for (end = 0; end < tmp[i] && end < cover[i] && pcs[i][cover[i] - end - 1] == tmppcs[i][tmp[i] - end - 1]; end++); | |
printf("diff %d: %d/%d %d/%d\n", i, cover[i], tmp[i], start, end); | |
printf("old:"); | |
for (x = start; x < cover[i] - end; x++) | |
printf(" 0x%lx", pcs[i][x] + 0xffffffff00000000ul); | |
printf("\n\n"); | |
printf("new:"); | |
for (x = start; x < tmp[i] - end; x++) | |
printf(" 0x%lx", tmppcs[i][x] + 0xffffffff00000000ul); | |
printf("\n\n"); | |
} | |
} | |
return 0; | |
} | |
int main() | |
{ | |
fd = open("/sys/kernel/debug/kcov", O_RDWR); | |
if (fd == -1) | |
exit(printf("open failed (%d)\n", errno)); | |
char cmd[128]; | |
sprintf(cmd, "trace=%d", SIZE); | |
int n = write(fd, cmd, strlen(cmd)); | |
if (n != strlen(cmd)) | |
exit(printf("write failed (%d)\n", errno)); | |
cov = mmap(NULL, SIZE * sizeof(uint32_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
if (cov == MAP_FAILED) | |
exit(printf("mmap failed (%d)\n", errno)); | |
int i; | |
for (i = 0; i < 5; i++) { | |
pthread_t th; | |
pthread_create(&th, 0, thr, 0); | |
pthread_join(th, 0); | |
usleep(10000); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment