base.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * This code maintains a list of active profiling data structures.
  3. *
  4. * Copyright IBM Corp. 2009
  5. * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  6. *
  7. * Uses gcc-internal data definitions.
  8. * Based on the gcov-kernel patch by:
  9. * Hubertus Franke <frankeh@us.ibm.com>
  10. * Nigel Hinds <nhinds@us.ibm.com>
  11. * Rajan Ravindran <rajancr@us.ibm.com>
  12. * Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  13. * Paul Larson
  14. */
  15. #define pr_fmt(fmt) "gcov: " fmt
  16. #include <linux/init.h>
  17. #include <linux/module.h>
  18. #include <linux/mutex.h>
  19. #include <linux/sched.h>
  20. #include "gcov.h"
  21. static int gcov_events_enabled;
  22. static DEFINE_MUTEX(gcov_lock);
  23. /*
  24. * __gcov_init is called by gcc-generated constructor code for each object
  25. * file compiled with -fprofile-arcs.
  26. */
  27. void __gcov_init(struct gcov_info *info)
  28. {
  29. static unsigned int gcov_version;
  30. mutex_lock(&gcov_lock);
  31. if (gcov_version == 0) {
  32. gcov_version = gcov_info_version(info);
  33. /*
  34. * Printing gcc's version magic may prove useful for debugging
  35. * incompatibility reports.
  36. */
  37. pr_info("version magic: 0x%x\n", gcov_version);
  38. }
  39. /*
  40. * Add new profiling data structure to list and inform event
  41. * listener.
  42. */
  43. gcov_info_link(info);
  44. if (gcov_events_enabled)
  45. gcov_event(GCOV_ADD, info);
  46. mutex_unlock(&gcov_lock);
  47. }
  48. EXPORT_SYMBOL(__gcov_init);
  49. /*
  50. * These functions may be referenced by gcc-generated profiling code but serve
  51. * no function for kernel profiling.
  52. */
  53. void __gcov_flush(void)
  54. {
  55. /* Unused. */
  56. }
  57. EXPORT_SYMBOL(__gcov_flush);
  58. void __gcov_merge_add(gcov_type *counters, unsigned int n_counters)
  59. {
  60. /* Unused. */
  61. }
  62. EXPORT_SYMBOL(__gcov_merge_add);
  63. void __gcov_merge_single(gcov_type *counters, unsigned int n_counters)
  64. {
  65. /* Unused. */
  66. }
  67. EXPORT_SYMBOL(__gcov_merge_single);
  68. void __gcov_merge_delta(gcov_type *counters, unsigned int n_counters)
  69. {
  70. /* Unused. */
  71. }
  72. EXPORT_SYMBOL(__gcov_merge_delta);
  73. void __gcov_merge_ior(gcov_type *counters, unsigned int n_counters)
  74. {
  75. /* Unused. */
  76. }
  77. EXPORT_SYMBOL(__gcov_merge_ior);
  78. void __gcov_merge_time_profile(gcov_type *counters, unsigned int n_counters)
  79. {
  80. /* Unused. */
  81. }
  82. EXPORT_SYMBOL(__gcov_merge_time_profile);
  83. void __gcov_merge_icall_topn(gcov_type *counters, unsigned int n_counters)
  84. {
  85. /* Unused. */
  86. }
  87. EXPORT_SYMBOL(__gcov_merge_icall_topn);
  88. void __gcov_exit(void)
  89. {
  90. /* Unused. */
  91. }
  92. EXPORT_SYMBOL(__gcov_exit);
  93. /**
  94. * gcov_enable_events - enable event reporting through gcov_event()
  95. *
  96. * Turn on reporting of profiling data load/unload-events through the
  97. * gcov_event() callback. Also replay all previous events once. This function
  98. * is needed because some events are potentially generated too early for the
  99. * callback implementation to handle them initially.
  100. */
  101. void gcov_enable_events(void)
  102. {
  103. struct gcov_info *info = NULL;
  104. mutex_lock(&gcov_lock);
  105. gcov_events_enabled = 1;
  106. /* Perform event callback for previously registered entries. */
  107. while ((info = gcov_info_next(info))) {
  108. gcov_event(GCOV_ADD, info);
  109. cond_resched();
  110. }
  111. mutex_unlock(&gcov_lock);
  112. }
  113. #ifdef CONFIG_MODULES
  114. static inline int within(void *addr, void *start, unsigned long size)
  115. {
  116. return ((addr >= start) && (addr < start + size));
  117. }
  118. /* Update list and generate events when modules are unloaded. */
  119. static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,
  120. void *data)
  121. {
  122. struct module *mod = data;
  123. struct gcov_info *info = NULL;
  124. struct gcov_info *prev = NULL;
  125. if (event != MODULE_STATE_GOING)
  126. return NOTIFY_OK;
  127. mutex_lock(&gcov_lock);
  128. /* Remove entries located in module from linked list. */
  129. while ((info = gcov_info_next(info))) {
  130. if (within(info, mod->module_core, mod->core_size)) {
  131. gcov_info_unlink(prev, info);
  132. if (gcov_events_enabled)
  133. gcov_event(GCOV_REMOVE, info);
  134. } else
  135. prev = info;
  136. }
  137. mutex_unlock(&gcov_lock);
  138. return NOTIFY_OK;
  139. }
  140. static struct notifier_block gcov_nb = {
  141. .notifier_call = gcov_module_notifier,
  142. };
  143. static int __init gcov_init(void)
  144. {
  145. return register_module_notifier(&gcov_nb);
  146. }
  147. device_initcall(gcov_init);
  148. #endif /* CONFIG_MODULES */