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