base.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. /**
  84. * gcov_enable_events - enable event reporting through gcov_event()
  85. *
  86. * Turn on reporting of profiling data load/unload-events through the
  87. * gcov_event() callback. Also replay all previous events once. This function
  88. * is needed because some events are potentially generated too early for the
  89. * callback implementation to handle them initially.
  90. */
  91. void gcov_enable_events(void)
  92. {
  93. struct gcov_info *info = NULL;
  94. mutex_lock(&gcov_lock);
  95. gcov_events_enabled = 1;
  96. /* Perform event callback for previously registered entries. */
  97. while ((info = gcov_info_next(info))) {
  98. gcov_event(GCOV_ADD, info);
  99. cond_resched();
  100. }
  101. mutex_unlock(&gcov_lock);
  102. }
  103. #ifdef CONFIG_MODULES
  104. static inline int within(void *addr, void *start, unsigned long size)
  105. {
  106. return ((addr >= start) && (addr < start + size));
  107. }
  108. /* Update list and generate events when modules are unloaded. */
  109. static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,
  110. void *data)
  111. {
  112. struct module *mod = data;
  113. struct gcov_info *info = NULL;
  114. struct gcov_info *prev = NULL;
  115. if (event != MODULE_STATE_GOING)
  116. return NOTIFY_OK;
  117. mutex_lock(&gcov_lock);
  118. /* Remove entries located in module from linked list. */
  119. while ((info = gcov_info_next(info))) {
  120. if (within(info, mod->module_core, mod->core_size)) {
  121. gcov_info_unlink(prev, info);
  122. if (gcov_events_enabled)
  123. gcov_event(GCOV_REMOVE, info);
  124. } else
  125. prev = info;
  126. }
  127. mutex_unlock(&gcov_lock);
  128. return NOTIFY_OK;
  129. }
  130. static struct notifier_block gcov_nb = {
  131. .notifier_call = gcov_module_notifier,
  132. };
  133. static int __init gcov_init(void)
  134. {
  135. return register_module_notifier(&gcov_nb);
  136. }
  137. device_initcall(gcov_init);
  138. #endif /* CONFIG_MODULES */