base.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * This code maintains a list of active profiling data structures.
  4. *
  5. * Copyright IBM Corp. 2009
  6. * Author(s): Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  7. *
  8. * Uses gcc-internal data definitions.
  9. * Based on the gcov-kernel patch by:
  10. * Hubertus Franke <frankeh@us.ibm.com>
  11. * Nigel Hinds <nhinds@us.ibm.com>
  12. * Rajan Ravindran <rajancr@us.ibm.com>
  13. * Peter Oberparleiter <oberpar@linux.vnet.ibm.com>
  14. * Paul Larson
  15. */
  16. #define pr_fmt(fmt) "gcov: " fmt
  17. #include <linux/init.h>
  18. #include <linux/module.h>
  19. #include <linux/mutex.h>
  20. #include <linux/sched.h>
  21. #include "gcov.h"
  22. int gcov_events_enabled;
  23. DEFINE_MUTEX(gcov_lock);
  24. /**
  25. * gcov_enable_events - enable event reporting through gcov_event()
  26. *
  27. * Turn on reporting of profiling data load/unload-events through the
  28. * gcov_event() callback. Also replay all previous events once. This function
  29. * is needed because some events are potentially generated too early for the
  30. * callback implementation to handle them initially.
  31. */
  32. void gcov_enable_events(void)
  33. {
  34. struct gcov_info *info = NULL;
  35. mutex_lock(&gcov_lock);
  36. gcov_events_enabled = 1;
  37. /* Perform event callback for previously registered entries. */
  38. while ((info = gcov_info_next(info))) {
  39. gcov_event(GCOV_ADD, info);
  40. cond_resched();
  41. }
  42. mutex_unlock(&gcov_lock);
  43. }
  44. #ifdef CONFIG_MODULES
  45. /* Update list and generate events when modules are unloaded. */
  46. static int gcov_module_notifier(struct notifier_block *nb, unsigned long event,
  47. void *data)
  48. {
  49. struct module *mod = data;
  50. struct gcov_info *info = NULL;
  51. struct gcov_info *prev = NULL;
  52. if (event != MODULE_STATE_GOING)
  53. return NOTIFY_OK;
  54. mutex_lock(&gcov_lock);
  55. /* Remove entries located in module from linked list. */
  56. while ((info = gcov_info_next(info))) {
  57. if (gcov_info_within_module(info, mod)) {
  58. gcov_info_unlink(prev, info);
  59. if (gcov_events_enabled)
  60. gcov_event(GCOV_REMOVE, info);
  61. } else
  62. prev = info;
  63. }
  64. mutex_unlock(&gcov_lock);
  65. return NOTIFY_OK;
  66. }
  67. static struct notifier_block gcov_nb = {
  68. .notifier_call = gcov_module_notifier,
  69. };
  70. static int __init gcov_init(void)
  71. {
  72. return register_module_notifier(&gcov_nb);
  73. }
  74. device_initcall(gcov_init);
  75. #endif /* CONFIG_MODULES */