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#define pr_fmt(fmt) "gcov: " fmt
46
47#include <linux/kernel.h>
48#include <linux/list.h>
49#include <linux/printk.h>
50#include <linux/ratelimit.h>
51#include <linux/seq_file.h>
52#include <linux/slab.h>
53#include <linux/vmalloc.h>
54#include "gcov.h"
55
56typedef void (*llvm_gcov_callback)(void);
57
58struct gcov_info {
59 struct list_head head;
60
61 const char *filename;
62 unsigned int version;
63 u32 checksum;
64
65 struct list_head functions;
66};
67
68struct gcov_fn_info {
69 struct list_head head;
70
71 u32 ident;
72 u32 checksum;
73#if CONFIG_CLANG_VERSION < 110000
74 u8 use_extra_checksum;
75#endif
76 u32 cfg_checksum;
77
78 u32 num_counters;
79 u64 *counters;
80#if CONFIG_CLANG_VERSION < 110000
81 const char *function_name;
82#endif
83};
84
85static struct gcov_info *current_info;
86
87static LIST_HEAD(clang_gcov_list);
88
89void llvm_gcov_init(llvm_gcov_callback writeout, llvm_gcov_callback flush)
90{
91 struct gcov_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
92
93 if (!info)
94 return;
95
96 INIT_LIST_HEAD(&info->head);
97 INIT_LIST_HEAD(&info->functions);
98
99 mutex_lock(&gcov_lock);
100
101 list_add_tail(&info->head, &clang_gcov_list);
102 current_info = info;
103 writeout();
104 current_info = NULL;
105 if (gcov_events_enabled)
106 gcov_event(GCOV_ADD, info);
107
108 mutex_unlock(&gcov_lock);
109}
110EXPORT_SYMBOL(llvm_gcov_init);
111
112#if CONFIG_CLANG_VERSION < 110000
113void llvm_gcda_start_file(const char *orig_filename, const char version[4],
114 u32 checksum)
115{
116 current_info->filename = orig_filename;
117 memcpy(¤t_info->version, version, sizeof(current_info->version));
118 current_info->checksum = checksum;
119}
120EXPORT_SYMBOL(llvm_gcda_start_file);
121#else
122void llvm_gcda_start_file(const char *orig_filename, u32 version, u32 checksum)
123{
124 current_info->filename = orig_filename;
125 current_info->version = version;
126 current_info->checksum = checksum;
127}
128EXPORT_SYMBOL(llvm_gcda_start_file);
129#endif
130
131#if CONFIG_CLANG_VERSION < 110000
132void llvm_gcda_emit_function(u32 ident, const char *function_name,
133 u32 func_checksum, u8 use_extra_checksum, u32 cfg_checksum)
134{
135 struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
136
137 if (!info)
138 return;
139
140 INIT_LIST_HEAD(&info->head);
141 info->ident = ident;
142 info->checksum = func_checksum;
143 info->use_extra_checksum = use_extra_checksum;
144 info->cfg_checksum = cfg_checksum;
145 if (function_name)
146 info->function_name = kstrdup(function_name, GFP_KERNEL);
147
148 list_add_tail(&info->head, ¤t_info->functions);
149}
150#else
151void llvm_gcda_emit_function(u32 ident, u32 func_checksum, u32 cfg_checksum)
152{
153 struct gcov_fn_info *info = kzalloc(sizeof(*info), GFP_KERNEL);
154
155 if (!info)
156 return;
157
158 INIT_LIST_HEAD(&info->head);
159 info->ident = ident;
160 info->checksum = func_checksum;
161 info->cfg_checksum = cfg_checksum;
162 list_add_tail(&info->head, ¤t_info->functions);
163}
164#endif
165EXPORT_SYMBOL(llvm_gcda_emit_function);
166
167void llvm_gcda_emit_arcs(u32 num_counters, u64 *counters)
168{
169 struct gcov_fn_info *info = list_last_entry(¤t_info->functions,
170 struct gcov_fn_info, head);
171
172 info->num_counters = num_counters;
173 info->counters = counters;
174}
175EXPORT_SYMBOL(llvm_gcda_emit_arcs);
176
177void llvm_gcda_summary_info(void)
178{
179}
180EXPORT_SYMBOL(llvm_gcda_summary_info);
181
182void llvm_gcda_end_file(void)
183{
184}
185EXPORT_SYMBOL(llvm_gcda_end_file);
186
187
188
189
190
191const char *gcov_info_filename(struct gcov_info *info)
192{
193 return info->filename;
194}
195
196
197
198
199
200unsigned int gcov_info_version(struct gcov_info *info)
201{
202 return info->version;
203}
204
205
206
207
208
209
210
211
212struct gcov_info *gcov_info_next(struct gcov_info *info)
213{
214 if (!info)
215 return list_first_entry_or_null(&clang_gcov_list,
216 struct gcov_info, head);
217 if (list_is_last(&info->head, &clang_gcov_list))
218 return NULL;
219 return list_next_entry(info, head);
220}
221
222
223
224
225
226void gcov_info_link(struct gcov_info *info)
227{
228 list_add_tail(&info->head, &clang_gcov_list);
229}
230
231
232
233
234
235
236void gcov_info_unlink(struct gcov_info *prev, struct gcov_info *info)
237{
238
239 __list_del_entry(&info->head);
240}
241
242
243
244
245
246
247
248
249bool gcov_info_within_module(struct gcov_info *info, struct module *mod)
250{
251 return within_module((unsigned long)info->filename, mod);
252}
253
254
255const struct gcov_link gcov_link[] = {
256 { OBJ_TREE, "gcno" },
257 { 0, NULL},
258};
259
260
261
262
263
264void gcov_info_reset(struct gcov_info *info)
265{
266 struct gcov_fn_info *fn;
267
268 list_for_each_entry(fn, &info->functions, head)
269 memset(fn->counters, 0,
270 sizeof(fn->counters[0]) * fn->num_counters);
271}
272
273
274
275
276
277
278
279
280int gcov_info_is_compatible(struct gcov_info *info1, struct gcov_info *info2)
281{
282 struct gcov_fn_info *fn_ptr1 = list_first_entry_or_null(
283 &info1->functions, struct gcov_fn_info, head);
284 struct gcov_fn_info *fn_ptr2 = list_first_entry_or_null(
285 &info2->functions, struct gcov_fn_info, head);
286
287 if (info1->checksum != info2->checksum)
288 return false;
289 if (!fn_ptr1)
290 return fn_ptr1 == fn_ptr2;
291 while (!list_is_last(&fn_ptr1->head, &info1->functions) &&
292 !list_is_last(&fn_ptr2->head, &info2->functions)) {
293 if (fn_ptr1->checksum != fn_ptr2->checksum)
294 return false;
295#if CONFIG_CLANG_VERSION < 110000
296 if (fn_ptr1->use_extra_checksum != fn_ptr2->use_extra_checksum)
297 return false;
298 if (fn_ptr1->use_extra_checksum &&
299 fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
300 return false;
301#else
302 if (fn_ptr1->cfg_checksum != fn_ptr2->cfg_checksum)
303 return false;
304#endif
305 fn_ptr1 = list_next_entry(fn_ptr1, head);
306 fn_ptr2 = list_next_entry(fn_ptr2, head);
307 }
308 return list_is_last(&fn_ptr1->head, &info1->functions) &&
309 list_is_last(&fn_ptr2->head, &info2->functions);
310}
311
312
313
314
315
316
317
318
319void gcov_info_add(struct gcov_info *dst, struct gcov_info *src)
320{
321 struct gcov_fn_info *dfn_ptr;
322 struct gcov_fn_info *sfn_ptr = list_first_entry_or_null(&src->functions,
323 struct gcov_fn_info, head);
324
325 list_for_each_entry(dfn_ptr, &dst->functions, head) {
326 u32 i;
327
328 for (i = 0; i < sfn_ptr->num_counters; i++)
329 dfn_ptr->counters[i] += sfn_ptr->counters[i];
330 }
331}
332
333#if CONFIG_CLANG_VERSION < 110000
334static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
335{
336 size_t cv_size;
337 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
338 GFP_KERNEL);
339 if (!fn_dup)
340 return NULL;
341 INIT_LIST_HEAD(&fn_dup->head);
342
343 fn_dup->function_name = kstrdup(fn->function_name, GFP_KERNEL);
344 if (!fn_dup->function_name)
345 goto err_name;
346
347 cv_size = fn->num_counters * sizeof(fn->counters[0]);
348 fn_dup->counters = vmalloc(cv_size);
349 if (!fn_dup->counters)
350 goto err_counters;
351 memcpy(fn_dup->counters, fn->counters, cv_size);
352
353 return fn_dup;
354
355err_counters:
356 kfree(fn_dup->function_name);
357err_name:
358 kfree(fn_dup);
359 return NULL;
360}
361#else
362static struct gcov_fn_info *gcov_fn_info_dup(struct gcov_fn_info *fn)
363{
364 size_t cv_size;
365 struct gcov_fn_info *fn_dup = kmemdup(fn, sizeof(*fn),
366 GFP_KERNEL);
367 if (!fn_dup)
368 return NULL;
369 INIT_LIST_HEAD(&fn_dup->head);
370
371 cv_size = fn->num_counters * sizeof(fn->counters[0]);
372 fn_dup->counters = vmalloc(cv_size);
373 if (!fn_dup->counters) {
374 kfree(fn_dup);
375 return NULL;
376 }
377
378 memcpy(fn_dup->counters, fn->counters, cv_size);
379
380 return fn_dup;
381}
382#endif
383
384
385
386
387
388
389
390struct gcov_info *gcov_info_dup(struct gcov_info *info)
391{
392 struct gcov_info *dup;
393 struct gcov_fn_info *fn;
394
395 dup = kmemdup(info, sizeof(*dup), GFP_KERNEL);
396 if (!dup)
397 return NULL;
398 INIT_LIST_HEAD(&dup->head);
399 INIT_LIST_HEAD(&dup->functions);
400 dup->filename = kstrdup(info->filename, GFP_KERNEL);
401 if (!dup->filename)
402 goto err;
403
404 list_for_each_entry(fn, &info->functions, head) {
405 struct gcov_fn_info *fn_dup = gcov_fn_info_dup(fn);
406
407 if (!fn_dup)
408 goto err;
409 list_add_tail(&fn_dup->head, &dup->functions);
410 }
411
412 return dup;
413
414err:
415 gcov_info_free(dup);
416 return NULL;
417}
418
419
420
421
422
423#if CONFIG_CLANG_VERSION < 110000
424void gcov_info_free(struct gcov_info *info)
425{
426 struct gcov_fn_info *fn, *tmp;
427
428 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
429 kfree(fn->function_name);
430 vfree(fn->counters);
431 list_del(&fn->head);
432 kfree(fn);
433 }
434 kfree(info->filename);
435 kfree(info);
436}
437#else
438void gcov_info_free(struct gcov_info *info)
439{
440 struct gcov_fn_info *fn, *tmp;
441
442 list_for_each_entry_safe(fn, tmp, &info->functions, head) {
443 vfree(fn->counters);
444 list_del(&fn->head);
445 kfree(fn);
446 }
447 kfree(info->filename);
448 kfree(info);
449}
450#endif
451
452#define ITER_STRIDE PAGE_SIZE
453
454
455
456
457
458
459
460
461struct gcov_iterator {
462 struct gcov_info *info;
463 void *buffer;
464 size_t size;
465 loff_t pos;
466};
467
468
469
470
471
472
473
474
475
476
477
478
479static size_t store_gcov_u32(void *buffer, size_t off, u32 v)
480{
481 u32 *data;
482
483 if (buffer) {
484 data = buffer + off;
485 *data = v;
486 }
487
488 return sizeof(*data);
489}
490
491
492
493
494
495
496
497
498
499
500
501
502
503static size_t store_gcov_u64(void *buffer, size_t off, u64 v)
504{
505 u32 *data;
506
507 if (buffer) {
508 data = buffer + off;
509
510 data[0] = (v & 0xffffffffUL);
511 data[1] = (v >> 32);
512 }
513
514 return sizeof(*data) * 2;
515}
516
517
518
519
520
521
522
523
524static size_t convert_to_gcda(char *buffer, struct gcov_info *info)
525{
526 struct gcov_fn_info *fi_ptr;
527 size_t pos = 0;
528
529
530 pos += store_gcov_u32(buffer, pos, GCOV_DATA_MAGIC);
531 pos += store_gcov_u32(buffer, pos, info->version);
532 pos += store_gcov_u32(buffer, pos, info->checksum);
533
534 list_for_each_entry(fi_ptr, &info->functions, head) {
535 u32 i;
536
537 pos += store_gcov_u32(buffer, pos, GCOV_TAG_FUNCTION);
538#if CONFIG_CLANG_VERSION < 110000
539 pos += store_gcov_u32(buffer, pos,
540 fi_ptr->use_extra_checksum ? 3 : 2);
541#else
542 pos += store_gcov_u32(buffer, pos, 3);
543#endif
544 pos += store_gcov_u32(buffer, pos, fi_ptr->ident);
545 pos += store_gcov_u32(buffer, pos, fi_ptr->checksum);
546#if CONFIG_CLANG_VERSION < 110000
547 if (fi_ptr->use_extra_checksum)
548 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
549#else
550 pos += store_gcov_u32(buffer, pos, fi_ptr->cfg_checksum);
551#endif
552
553 pos += store_gcov_u32(buffer, pos, GCOV_TAG_COUNTER_BASE);
554 pos += store_gcov_u32(buffer, pos, fi_ptr->num_counters * 2);
555 for (i = 0; i < fi_ptr->num_counters; i++)
556 pos += store_gcov_u64(buffer, pos, fi_ptr->counters[i]);
557 }
558
559 return pos;
560}
561
562
563
564
565
566
567
568struct gcov_iterator *gcov_iter_new(struct gcov_info *info)
569{
570 struct gcov_iterator *iter;
571
572 iter = kzalloc(sizeof(struct gcov_iterator), GFP_KERNEL);
573 if (!iter)
574 goto err_free;
575
576 iter->info = info;
577
578 iter->size = convert_to_gcda(NULL, info);
579 iter->buffer = vmalloc(iter->size);
580 if (!iter->buffer)
581 goto err_free;
582
583 convert_to_gcda(iter->buffer, info);
584
585 return iter;
586
587err_free:
588 kfree(iter);
589 return NULL;
590}
591
592
593
594
595
596
597void gcov_iter_free(struct gcov_iterator *iter)
598{
599 vfree(iter->buffer);
600 kfree(iter);
601}
602
603
604
605
606
607struct gcov_info *gcov_iter_get_info(struct gcov_iterator *iter)
608{
609 return iter->info;
610}
611
612
613
614
615
616void gcov_iter_start(struct gcov_iterator *iter)
617{
618 iter->pos = 0;
619}
620
621
622
623
624
625
626
627int gcov_iter_next(struct gcov_iterator *iter)
628{
629 if (iter->pos < iter->size)
630 iter->pos += ITER_STRIDE;
631
632 if (iter->pos >= iter->size)
633 return -EINVAL;
634
635 return 0;
636}
637
638
639
640
641
642
643
644
645int gcov_iter_write(struct gcov_iterator *iter, struct seq_file *seq)
646{
647 size_t len;
648
649 if (iter->pos >= iter->size)
650 return -EINVAL;
651
652 len = ITER_STRIDE;
653 if (iter->pos + len > iter->size)
654 len = iter->size - iter->pos;
655
656 seq_write(seq, iter->buffer + iter->pos, len);
657
658 return 0;
659}
660