1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/errno.h>
18#include <linux/slab.h>
19#include <linux/string.h>
20#include <linux/seq_file.h>
21#include <linux/vmalloc.h>
22#include "gcov.h"
23
24#define GCOV_COUNTERS 5
25
26static struct gcov_info *gcov_info_head;
27
28
29
30
31
32
33
34
35
36
37struct gcov_fn_info {
38 unsigned int ident;
39 unsigned int checksum;
40 unsigned int n_ctrs[0];
41};
42
43
44
45
46
47
48
49
50
51
52struct gcov_ctr_info {
53 unsigned int num;
54 gcov_type *values;
55 void (*merge)(gcov_type *, unsigned int);
56};
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72struct gcov_info {
73 unsigned int version;
74 struct gcov_info *next;
75 unsigned int stamp;
76 const char *filename;
77 unsigned int n_functions;
78 const struct gcov_fn_info *functions;
79 unsigned int ctr_mask;
80 struct gcov_ctr_info counts[0];
81};
82
83
84
85
86
87const char *gcov_info_filename(struct gcov_info *info)
88{
89 return info->filename;
90}
91
92
93
94
95
96unsigned int gcov_info_version(struct gcov_info *info)
97{
98 return info->version;
99}
100
101
102
103
104
105
106
107
108struct gcov_info *gcov_info_next(struct gcov_info *info)
109{
110 if (!info)
111 return gcov_info_head;
112
113 return info->next;
114}
115
116
117
118
119
120void gcov_info_link(struct gcov_info *info)
121{
122 info->next = gcov_info_head;
123 gcov_info_head = info;
124}
125
126
127
128
129
130
131void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
132{
133 if (prev)
134 prev->next = info->next;
135 else
136 gcov_info_head = info->next;
137}
138
139
140const struct gcov_link gcov_link[] = {
141 { OBJ_TREE, "gcno" },
142 { 0, NULL},
143};
144
145
146
147
148
149static int counter_active(struct gcov_info *info, unsigned int type)
150{
151 return (1 << type) & info->ctr_mask;
152}
153
154
155static unsigned int num_counter_active(struct gcov_info *info)
156{
157 unsigned int i;
158 unsigned int result = 0;
159
160 for (i = 0; i < GCOV_COUNTERS; i++) {
161 if (counter_active(info, i))
162 result++;
163 }
164 return result;
165}
166
167
168
169
170
171void gcov_info_reset(struct gcov_info *info)
172{
173 unsigned int active = num_counter_active(info);
174 unsigned int i;
175
176 for (i = 0; i < active; i++) {
177 memset(info->counts[i].values, 0,
178 info->counts[i].num * sizeof(gcov_type));
179 }
180}
181
182
183
184
185
186
187
188
189int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
190{
191 return (info1->stamp == info2->stamp);
192}
193
194
195
196
197
198
199
200
201void gcov_info_add(struct gcov_info *dest, struct gcov_info *source)
202{
203 unsigned int i;
204 unsigned int j;
205
206 for (i = 0; i < num_counter_active(dest); i++) {
207 for (j = 0; j < dest->counts[i].num; j++) {
208 dest->counts[i].values[j] +=
209 source->counts[i].values[j];
210 }
211 }
212}
213
214
215static size_t get_fn_size(struct gcov_info *info)
216{
217 size_t size;
218
219 size = sizeof(struct gcov_fn_info) + num_counter_active(info) *
220 sizeof(unsigned int);
221 if (__alignof__(struct gcov_fn_info) > sizeof(unsigned int))
222 size = ALIGN(size, __alignof__(struct gcov_fn_info));
223 return size;
224}
225
226
227static struct gcov_fn_info *get_fn_info(struct gcov_info *info, unsigned int fn)
228{
229 return (struct gcov_fn_info *)
230 ((char *) info->functions + fn * get_fn_size(info));
231}
232
233
234
235
236
237
238
239struct gcov_info *gcov_info_dup(struct gcov_info *info)
240{
241 struct gcov_info *dup;
242 unsigned int i;
243 unsigned int active;
244
245
246 active = num_counter_active(info);
247 dup = kzalloc(sizeof(struct gcov_info) +
248 sizeof(struct gcov_ctr_info) * active, GFP_KERNEL);
249 if (!dup)
250 return NULL;
251 dup->version = info->version;
252 dup->stamp = info->stamp;
253 dup->n_functions = info->n_functions;
254 dup->ctr_mask = info->ctr_mask;
255
256 dup->filename = kstrdup(info->filename, GFP_KERNEL);
257 if (!dup->filename)
258 goto err_free;
259
260 dup->functions = kmemdup(info->functions, info->n_functions *
261 get_fn_size(info), GFP_KERNEL);
262 if (!dup->functions)
263 goto err_free;
264
265 for (i = 0; i < active ; i++) {
266 struct gcov_ctr_info *ctr = &info->counts[i];
267 size_t size = ctr->num * sizeof(gcov_type);
268
269 dup->counts[i].num = ctr->num;
270 dup->counts[i].merge = ctr->merge;
271 dup->counts[i].values = vmalloc(size);
272 if (!dup->counts[i].values)
273 goto err_free;
274 memcpy(dup->counts[i].values, ctr->values, size);
275 }
276 return dup;
277
278err_free:
279 gcov_info_free(dup);
280 return NULL;
281}
282
283
284
285
286
287void gcov_info_free(struct gcov_info *info)
288{
289 unsigned int active = num_counter_active(info);
290 unsigned int i;
291
292 for (i = 0; i < active ; i++)
293 vfree(info->counts[i].values);
294 kfree(info->functions);
295 kfree(info->filename);
296 kfree(info);
297}
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319struct type_info {
320 int ctr_type;
321 unsigned int offset;
322};
323
324
325
326
327
328
329
330
331
332
333
334struct gcov_iterator {
335 struct gcov_info *info;
336
337 int record;
338 unsigned int function;
339 unsigned int type;
340 unsigned int count;
341
342 int num_types;
343 struct type_info type_info[0];
344};
345
346static struct gcov_fn_info *get_func(struct gcov_iterator *iter)
347{
348 return get_fn_info(iter->info, iter->function);
349}
350
351static struct type_info *get_type(struct gcov_iterator *iter)
352{
353 return &iter->type_info[iter->type];
354}
355
356
357
358
359
360
361
362struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
363{
364 struct gcov_iterator *iter;
365
366 iter = kzalloc(sizeof(struct gcov_iterator) +
367 num_counter_active(info) * sizeof(struct type_info),
368 GFP_KERNEL);
369 if (iter)
370 iter->info = info;
371
372 return iter;
373}
374
375
376
377
378
379void gcov_iter_free(struct gcov_iterator *iter)
380{
381 kfree(iter);
382}
383
384
385
386
387
388struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
389{
390 return iter->info;
391}
392
393
394
395
396
397void gcov_iter_start(struct gcov_iterator *iter)
398{
399 int i;
400
401 iter->record = 0;
402 iter->function = 0;
403 iter->type = 0;
404 iter->count = 0;
405 iter->num_types = 0;
406 for (i = 0; i < GCOV_COUNTERS; i++) {
407 if (counter_active(iter->info, i)) {
408 iter->type_info[iter->num_types].ctr_type = i;
409 iter->type_info[iter->num_types++].offset = 0;
410 }
411 }
412}
413
414
415#define RECORD_FILE_MAGIC 0
416#define RECORD_GCOV_VERSION 1
417#define RECORD_TIME_STAMP 2
418#define RECORD_FUNCTION_TAG 3
419#define RECORD_FUNCTON_TAG_LEN 4
420#define RECORD_FUNCTION_IDENT 5
421#define RECORD_FUNCTION_CHECK 6
422#define RECORD_COUNT_TAG 7
423#define RECORD_COUNT_LEN 8
424#define RECORD_COUNT 9
425
426
427
428
429
430
431
432int gcov_iter_next(struct gcov_iterator *iter)
433{
434 switch (iter->record) {
435 case RECORD_FILE_MAGIC:
436 case RECORD_GCOV_VERSION:
437 case RECORD_FUNCTION_TAG:
438 case RECORD_FUNCTON_TAG_LEN:
439 case RECORD_FUNCTION_IDENT:
440 case RECORD_COUNT_TAG:
441
442 iter->record++;
443 break;
444 case RECORD_COUNT:
445
446 iter->count++;
447
448 case RECORD_COUNT_LEN:
449 if (iter->count < get_func(iter)->n_ctrs[iter->type]) {
450 iter->record = 9;
451 break;
452 }
453
454 get_type(iter)->offset += iter->count;
455 iter->count = 0;
456 iter->type++;
457
458 case RECORD_FUNCTION_CHECK:
459 if (iter->type < iter->num_types) {
460 iter->record = 7;
461 break;
462 }
463
464 iter->type = 0;
465 iter->function++;
466
467 case RECORD_TIME_STAMP:
468 if (iter->function < iter->info->n_functions)
469 iter->record = 3;
470 else
471 iter->record = -1;
472 break;
473 }
474
475 if (iter->record == -1)
476 return -EINVAL;
477 else
478 return 0;
479}
480
481
482
483
484
485
486
487
488
489
490static int seq_write_gcov_u32(struct seq_file *seq, u32 v)
491{
492 return seq_write(seq, &v, sizeof(v));
493}
494
495
496
497
498
499
500
501
502
503
504
505static int seq_write_gcov_u64(struct seq_file *seq, u64 v)
506{
507 u32 data[2];
508
509 data[0] = (v & 0xffffffffUL);
510 data[1] = (v >> 32);
511 return seq_write(seq, data, sizeof(data));
512}
513
514
515
516
517
518
519
520
521int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
522{
523 int rc = -EINVAL;
524
525 switch (iter->record) {
526 case RECORD_FILE_MAGIC:
527 rc = seq_write_gcov_u32(seq, GCOV_DATA_MAGIC);
528 break;
529 case RECORD_GCOV_VERSION:
530 rc = seq_write_gcov_u32(seq, iter->info->version);
531 break;
532 case RECORD_TIME_STAMP:
533 rc = seq_write_gcov_u32(seq, iter->info->stamp);
534 break;
535 case RECORD_FUNCTION_TAG:
536 rc = seq_write_gcov_u32(seq, GCOV_TAG_FUNCTION);
537 break;
538 case RECORD_FUNCTON_TAG_LEN:
539 rc = seq_write_gcov_u32(seq, 2);
540 break;
541 case RECORD_FUNCTION_IDENT:
542 rc = seq_write_gcov_u32(seq, get_func(iter)->ident);
543 break;
544 case RECORD_FUNCTION_CHECK:
545 rc = seq_write_gcov_u32(seq, get_func(iter)->checksum);
546 break;
547 case RECORD_COUNT_TAG:
548 rc = seq_write_gcov_u32(seq,
549 GCOV_TAG_FOR_COUNTER(get_type(iter)->ctr_type));
550 break;
551 case RECORD_COUNT_LEN:
552 rc = seq_write_gcov_u32(seq,
553 get_func(iter)->n_ctrs[iter->type] * 2);
554 break;
555 case RECORD_COUNT:
556 rc = seq_write_gcov_u64(seq,
557 iter->info->counts[iter->type].
558 values[iter->count + get_type(iter)->offset]);
559 break;
560 }
561 return rc;
562}
563