summaryrefslogtreecommitdiffstats
path: root/src/lib/gcov-glue.c
blob: d8ebe70e9e4207deb3c0a8fb927bf0b4866539fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* SPDX-License-Identifier: GPL-2.0-only */

#include <stdint.h>
#include <bootstate.h>
#include <cbmem.h>

typedef struct file {
	uint32_t magic;
	struct file *next;
	char *filename;
	char *data;
	int offset;
	int len;
} FILE;

#define SEEK_SET       0       /* Seek from beginning of file.  */

#define DIR_SEPARATOR '/'
#define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
#define HAS_DRIVE_SPEC(f) (0)

#define COVERAGE_SIZE (32*1024)

#define COVERAGE_MAGIC 0x584d4153

static FILE *current_file = NULL;
static FILE *previous_file = NULL;

static FILE *fopen(const char *path, const char *mode)
{
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "%s %s with mode %s\n", __func__, path, mode);
#endif
	if (!current_file) {
		current_file = cbmem_add(CBMEM_ID_COVERAGE, 32*1024);
	} else {
		previous_file = current_file;
		current_file =
			(FILE *)(ALIGN_UP(((unsigned long)previous_file->data
			+ previous_file->len), 16));
	}

	// TODO check if we're at the end of the CBMEM region (ENOMEM)
	if (current_file) {
		current_file->magic = COVERAGE_MAGIC;
		current_file->next = NULL;
		if (previous_file)
			previous_file->next = current_file;
		current_file->filename = (char *)&current_file[1];
		strcpy(current_file->filename, path);
		current_file->data =
			(char *)ALIGN_UP(((unsigned long)current_file->filename
			+ strlen(path) + 1), 16);
		current_file->offset = 0;
		current_file->len = 0;
	}

	return current_file;
}

static int fclose(FILE *stream)
{
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "%s %s\n", __func__, stream->filename);
#endif
	return 0;
}

static int fseek(FILE *stream, long offset, int whence)
{
	/* fseek should only be called with offset==0 and whence==SEEK_SET
	 * to a freshly opened file. */
	gcc_assert(offset == 0 && whence == SEEK_SET);
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "%s %s offset=%ld whence=%d\n",
		__func__, stream->filename, offset, whence);
#endif
	return 0;
}

static long ftell(FILE *stream)
{
	/* ftell should currently not be called */
	BUG();
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "%s %s\n", __func__, stream->filename);
#endif
	return 0;
}

static size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "%s: ptr=%p size=%zd nmemb=%zd FILE*=%p\n",
		__func__, ptr, size, nmemb, stream);
#endif
	return 0;
}

static size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "%s: %zd * %zd bytes to file %s\n",
		__func__, nmemb, size, stream->filename);
#endif
	// TODO check if file is last opened file and fail otherwise.

	memcpy(stream->data + stream->offset, ptr, size * nmemb);
	stream->len += (nmemb * size) - (stream->len - stream->offset);
	stream->offset += nmemb * size;
	return nmemb;
}

static void setbuf(FILE *stream, char *buf)
{
	gcc_assert(buf == 0);
}

static void coverage_init(void *unused)
{
	extern long __CTOR_LIST__;
	typedef void (*func_ptr)(void);
	func_ptr *ctor = (func_ptr *) &__CTOR_LIST__;
	if (ctor == NULL)
		return;

	for (; *ctor != (func_ptr) 0; ctor++)
		(*ctor)();
}

void __gcov_flush(void);
static void coverage_exit(void *unused)
{
#if CONFIG(DEBUG_COVERAGE)
	printk(BIOS_DEBUG, "Syncing coverage data.\n");
#endif
	__gcov_flush();
}

BOOT_STATE_INIT_ENTRY(BS_PRE_DEVICE, BS_ON_ENTRY, coverage_init, NULL);
BOOT_STATE_INIT_ENTRY(BS_OS_RESUME, BS_ON_ENTRY, coverage_exit, NULL);
BOOT_STATE_INIT_ENTRY(BS_PAYLOAD_LOAD, BS_ON_EXIT, coverage_exit, NULL);